//===-- Writer.cpp - Library for converting LLVM code to C ----------------===//
//
// This library implements the functionality defined in llvm/Assembly/CWriter.h
//
// TODO : Recursive types.
//
//===-----------------------------------------------------------------------==//
#include "llvm/Assembly/CWriter.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/iMemory.h"
#include "llvm/iTerminators.h"
#include "llvm/iPHINode.h"
#include "llvm/iOther.h"
#include "llvm/iOperators.h"
#include "llvm/SymbolTable.h"
#include "llvm/SlotCalculator.h"
#include "llvm/Support/InstVisitor.h"
#include "llvm/Support/InstIterator.h"
#include "Support/StringExtras.h"
#include "Support/STLExtras.h"
#include <algorithm>
#include <set>
using std::string;
using std::map;
using std::ostream;
namespace {
class CWriter : public InstVisitor<CWriter> {
ostream& Out;
SlotCalculator &Table;
const Module *TheModule;
map<const Type *, string> TypeNames;
std::set<const Value*> MangledGlobals;
public:
inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
: Out(o), Table(Tab), TheModule(M) {
}
inline void write(Module *M) { printModule(M); }
ostream &printType(const Type *Ty, const string &VariableName = "",
bool IgnoreName = false);
void writeOperand(Value *Operand);
void writeOperandInternal(Value *Operand);
string getValueName(const Value *V);
private :
void printModule(Module *M);
void printSymbolTable(const SymbolTable &ST);
void printGlobal(const GlobalVariable *GV);
void printFunctionSignature(const Function *F, bool Prototype);
void printFunction(Function *);
void printConstant(Constant *CPV);
void printConstantArray(ConstantArray *CPA);
// isInlinableInst - Attempt to inline instructions into their uses to build
// trees as much as possible. To do this, we have to consistently decide
// what is acceptable to inline, so that variable declarations don't get
// printed and an extra copy of the expr is not emitted.
//
static bool isInlinableInst(const Instruction &I) {
// Must be an expression, must be used exactly once. If it is dead, we
// emit it inline where it would go.
if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
return false;
// Only inline instruction it it's use is in the same BB as the inst.
return I.getParent() == cast<Instruction>(I.use_back())->getParent();
}
// Instruction visitation functions
friend class InstVisitor<CWriter>;
void visitReturnInst(ReturnInst &I);
void visitBranchInst(BranchInst &I);
void visitPHINode(PHINode &I) {}
void visitBinaryOperator(Instruction &I);
void visitCastInst (CastInst &I);
void visitCallInst (CallInst &I);
void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
void visitMallocInst(MallocInst &I);
void visitAllocaInst(AllocaInst &I);
void visitFreeInst (FreeInst &I);
void visitLoadInst (LoadInst &I);
void visitStoreInst (StoreInst &I);
void visitGetElementPtrInst(GetElementPtrInst &I);
void visitInstruction(Instruction &I) {
std::cerr << "C Writer does not know about " << I;
abort();
}
void outputLValue(Instruction *I) {
Out << " " << getValueName(I) << " = ";
}
void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
unsigned Indent);
void printIndexingExpression(Value *Ptr, User::op_iterator I,
User::op_iterator E);
};
}
// We dont want identifier names with ., space, - in them.
// So we replace them with _
static string makeNameProper(string x) {
string tmp;
for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
switch (*sI) {
case '.': tmp += "d_"; break;
case ' ': tmp += "s_"; break;
case '-': tmp += "D_"; break;
default: tmp += *sI;
}
return tmp;
}
string CWriter::getValueName(const Value *V) {
if