1 //===-- Writer.cpp - Library for converting LLVM code to C ----------------===//
3 // This library implements the functionality defined in llvm/Assembly/CWriter.h
5 // TODO : Recursive types.
7 //===-----------------------------------------------------------------------==//
9 #include "llvm/Assembly/CWriter.h"
10 #include "llvm/Constants.h"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/Module.h"
13 #include "llvm/iMemory.h"
14 #include "llvm/iTerminators.h"
15 #include "llvm/iPHINode.h"
16 #include "llvm/iOther.h"
17 #include "llvm/iOperators.h"
18 #include "llvm/SymbolTable.h"
19 #include "llvm/SlotCalculator.h"
20 #include "llvm/Support/InstVisitor.h"
21 #include "llvm/Support/InstIterator.h"
22 #include "Support/StringExtras.h"
23 #include "Support/STLExtras.h"
30 static std::string getConstStrValue(const Constant* CPV);
33 static std::string getConstArrayStrValue(const Constant* CPV) {
36 // As a special case, print the array as a string if it is an array of
37 // ubytes or an array of sbytes with positive values.
39 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
40 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
42 // Make sure the last character is a null char, as automatically added by C
43 if (CPV->getNumOperands() == 0 ||
44 !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
49 // Do not include the last character, which we know is null
50 for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
51 unsigned char C = (ETy == Type::SByteTy) ?
52 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
53 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
59 case '\n': Result += "\\n"; break;
60 case '\t': Result += "\\t"; break;
61 case '\r': Result += "\\r"; break;
62 case '\v': Result += "\\v"; break;
63 case '\a': Result += "\\a"; break;
66 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
67 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
75 if (CPV->getNumOperands()) {
76 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
77 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
78 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
86 static std::string getConstStrValue(const Constant* CPV) {
87 switch (CPV->getType()->getPrimitiveID()) {
88 case Type::BoolTyID: return CPV == ConstantBool::False ? "0" : "1";
91 case Type::IntTyID: return itostr(cast<ConstantSInt>(CPV)->getValue());
92 case Type::LongTyID: return itostr(cast<ConstantSInt>(CPV)->getValue())+"ll";
95 case Type::UShortTyID:return utostr(cast<ConstantUInt>(CPV)->getValue());
96 case Type::UIntTyID: return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
97 case Type::ULongTyID:return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
100 case Type::DoubleTyID: return ftostr(cast<ConstantFP>(CPV)->getValue());
102 case Type::ArrayTyID: return getConstArrayStrValue(CPV);
104 case Type::StructTyID: {
105 std::string Result = "{";
106 if (CPV->getNumOperands()) {
107 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
108 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
109 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
111 return Result + " }";
115 cerr << "Unknown constant type: " << CPV << "\n";
120 // Pass the Type* variable and and the variable name and this prints out the
121 // variable declaration.
123 static string calcTypeNameVar(const Type *Ty,
124 map<const Type *, string> &TypeNames,
125 const string &NameSoFar, bool ignoreName = false){
126 if (Ty->isPrimitiveType())
127 switch (Ty->getPrimitiveID()) {
128 case Type::VoidTyID: return "void " + NameSoFar;
129 case Type::BoolTyID: return "bool " + NameSoFar;
130 case Type::UByteTyID: return "unsigned char " + NameSoFar;
131 case Type::SByteTyID: return "signed char " + NameSoFar;
132 case Type::UShortTyID: return "unsigned short " + NameSoFar;
133 case Type::ShortTyID: return "short " + NameSoFar;
134 case Type::UIntTyID: return "unsigned " + NameSoFar;
135 case Type::IntTyID: return "int " + NameSoFar;
136 case Type::ULongTyID: return "unsigned long long " + NameSoFar;
137 case Type::LongTyID: return "signed long long " + NameSoFar;
138 case Type::FloatTyID: return "float " + NameSoFar;
139 case Type::DoubleTyID: return "double " + NameSoFar;
141 cerr << "Unknown primitive type: " << Ty << "\n";
145 // Check to see if the type is named.
147 map<const Type *, string>::iterator I = TypeNames.find(Ty);
148 if (I != TypeNames.end())
149 return I->second + " " + NameSoFar;
153 switch (Ty->getPrimitiveID()) {
154 case Type::FunctionTyID: {
155 const FunctionType *MTy = cast<FunctionType>(Ty);
156 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
157 Result += " " + NameSoFar + " (";
158 for (FunctionType::ParamTypes::const_iterator
159 I = MTy->getParamTypes().begin(),
160 E = MTy->getParamTypes().end(); I != E; ++I) {
161 if (I != MTy->getParamTypes().begin())
163 Result += calcTypeNameVar(*I, TypeNames, "");
165 if (MTy->isVarArg()) {
166 if (!MTy->getParamTypes().empty())
172 case Type::StructTyID: {
173 const StructType *STy = cast<const StructType>(Ty);
174 Result = NameSoFar + " {\n";
176 for (StructType::ElementTypes::const_iterator
177 I = STy->getElementTypes().begin(),
178 E = STy->getElementTypes().end(); I != E; ++I) {
179 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
185 case Type::PointerTyID:
186 return calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
187 TypeNames, "*" + NameSoFar);
189 case Type::ArrayTyID: {
190 const ArrayType *ATy = cast<const ArrayType>(Ty);
191 int NumElements = ATy->getNumElements();
192 return calcTypeNameVar(ATy->getElementType(), TypeNames,
193 NameSoFar + "[" + itostr(NumElements) + "]");
196 assert(0 && "Unhandled case in getTypeProps!");
204 class CWriter : public InstVisitor<CWriter> {
206 SlotCalculator &Table;
207 const Module *TheModule;
208 map<const Type *, string> TypeNames;
209 std::set<const Value*> MangledGlobals;
211 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
212 : Out(o), Table(Tab), TheModule(M) {
215 inline void write(Module *M) { printModule(M); }
217 ostream& printType(const Type *Ty, const string &VariableName = "") {
218 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
221 void writeOperand(Value *Operand);
222 void writeOperandInternal(Value *Operand);
224 string getValueName(const Value *V);
227 void printModule(Module *M);
228 void printSymbolTable(const SymbolTable &ST);
229 void printGlobal(const GlobalVariable *GV);
230 void printFunctionSignature(const Function *F);
231 void printFunctionDecl(const Function *F); // Print just the forward decl
233 void printFunction(Function *);
235 // isInlinableInst - Attempt to inline instructions into their uses to build
236 // trees as much as possible. To do this, we have to consistently decide
237 // what is acceptable to inline, so that variable declarations don't get
238 // printed and an extra copy of the expr is not emitted.
240 static bool isInlinableInst(const Instruction &I) {
241 // Must be an expression, must be used exactly once. If it is dead, we
242 // emit it inline where it would go.
243 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
244 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
247 // Only inline instruction it it's use is in the same BB as the inst.
248 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
251 // Instruction visitation functions
252 friend class InstVisitor<CWriter>;
254 void visitReturnInst(ReturnInst &I);
255 void visitBranchInst(BranchInst &I);
257 void visitPHINode(PHINode &I) {}
258 void visitNot(GenericUnaryInst &I);
259 void visitBinaryOperator(Instruction &I);
261 void visitCastInst (CastInst &I);
262 void visitCallInst (CallInst &I);
263 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
265 void visitMallocInst(MallocInst &I);
266 void visitAllocaInst(AllocaInst &I);
267 void visitFreeInst (FreeInst &I);
268 void visitLoadInst (LoadInst &I);
269 void visitStoreInst (StoreInst &I);
270 void visitGetElementPtrInst(GetElementPtrInst &I);
272 void visitInstruction(Instruction &I) {
273 cerr << "C Writer does not know about " << I;
277 void outputLValue(Instruction *I) {
278 Out << " " << getValueName(I) << " = ";
280 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
282 void printIndexingExpr(MemAccessInst &MAI);
286 // We dont want identifier names with ., space, - in them.
287 // So we replace them with _
288 static string makeNameProper(string x) {
290 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
292 case '.': tmp += "d_"; break;
293 case ' ': tmp += "s_"; break;
294 case '-': tmp += "D_"; break;
301 string CWriter::getValueName(const Value *V) {
302 if (V->hasName()) { // Print out the label if it exists...
303 if (isa<GlobalValue>(V) && // Do not mangle globals...
304 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
305 !MangledGlobals.count(V)) // Unless the name would collide if we don't
306 return makeNameProper(V->getName());
308 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
309 makeNameProper(V->getName());
312 int Slot = Table.getValSlot(V);
313 assert(Slot >= 0 && "Invalid value!");
314 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
317 void CWriter::writeOperandInternal(Value *Operand) {
318 if (Operand->hasName()) {
319 Out << getValueName(Operand);
320 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
321 if (isa<ConstantPointerNull>(CPV)) {
323 printType(CPV->getType(), "");
326 Out << getConstStrValue(CPV);
328 int Slot = Table.getValSlot(Operand);
329 assert(Slot >= 0 && "Malformed LLVM!");
330 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
334 void CWriter::writeOperand(Value *Operand) {
335 if (Instruction *I = dyn_cast<Instruction>(Operand))
336 if (isInlinableInst(*I)) {
337 // Should we inline this instruction to build a tree?
344 if (isa<GlobalVariable>(Operand))
345 Out << "(&"; // Global variables are references as their addresses by llvm
347 writeOperandInternal(Operand);
349 if (isa<GlobalVariable>(Operand))
353 void CWriter::printModule(Module *M) {
354 // Calculate which global values have names that will collide when we throw
355 // away type information.
356 { // Scope to delete the FoundNames set when we are done with it...
357 std::set<string> FoundNames;
358 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
359 if (I->hasName()) // If the global has a name...
360 if (FoundNames.count(I->getName())) // And the name is already used
361 MangledGlobals.insert(I); // Mangle the name
363 FoundNames.insert(I->getName()); // Otherwise, keep track of name
365 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
366 if (I->hasName()) // If the global has a name...
367 if (FoundNames.count(I->getName())) // And the name is already used
368 MangledGlobals.insert(I); // Mangle the name
370 FoundNames.insert(I->getName()); // Otherwise, keep track of name
374 // printing stdlib inclusion
375 // Out << "#include <stdlib.h>\n";
377 // get declaration for alloca
378 Out << "/* Provide Declarations */\n"
379 << "#include <malloc.h>\n"
380 << "#include <alloca.h>\n\n"
382 // Provide a definition for null if one does not already exist.
383 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
384 << "typedef unsigned char bool;\n"
386 << "\n\n/* Global Symbols */\n";
388 // Loop over the symbol table, emitting all named constants...
389 if (M->hasSymbolTable())
390 printSymbolTable(*M->getSymbolTable());
392 Out << "\n\n/* Global Data */\n";
393 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
394 if (I->hasInternalLinkage()) Out << "static ";
395 printType(I->getType()->getElementType(), getValueName(I));
397 if (I->hasInitializer()) {
399 writeOperand(I->getInitializer());
404 // First output all the declarations of the functions as C requires Functions
405 // be declared before they are used.
407 Out << "\n\n/* Function Declarations */\n";
408 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
409 printFunctionDecl(I);
411 // Output all of the functions...
412 Out << "\n\n/* Function Bodies */\n";
413 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
418 // printSymbolTable - Run through symbol table looking for named constants
419 // if a named constant is found, emit it's declaration...
420 // Assuming that symbol table has only types and constants.
421 void CWriter::printSymbolTable(const SymbolTable &ST) {
422 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
423 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
424 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
426 for (; I != End; ++I)
427 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
428 string Name = "struct l_" + makeNameProper(I->first);
429 Out << Name << ";\n";
431 TypeNames.insert(std::make_pair(Ty, Name));
437 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
438 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
439 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
441 for (; I != End; ++I) {
442 const Value *V = I->second;
443 if (const Type *Ty = dyn_cast<Type>(V)) {
444 string Name = "l_" + makeNameProper(I->first);
445 if (isa<StructType>(Ty))
446 Name = "struct " + makeNameProper(Name);
450 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
457 // printFunctionDecl - Print function declaration
459 void CWriter::printFunctionDecl(const Function *F) {
460 printFunctionSignature(F);
464 void CWriter::printFunctionSignature(const Function *F) {
465 if (F->hasInternalLinkage()) Out << "static ";
467 // Loop over the arguments, printing them...
468 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
470 // Print out the return type and name...
471 printType(F->getReturnType());
472 Out << getValueName(F) << "(";
474 if (!F->isExternal()) {
476 printType(F->afront().getType(), getValueName(F->abegin()));
478 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
481 printType(I->getType(), getValueName(I));
485 // Loop over the arguments, printing them...
486 for (FunctionType::ParamTypes::const_iterator I =
487 FT->getParamTypes().begin(),
488 E = FT->getParamTypes().end(); I != E; ++I) {
489 if (I != FT->getParamTypes().begin()) Out << ", ";
494 // Finish printing arguments...
495 if (FT->isVarArg()) {
496 if (FT->getParamTypes().size()) Out << ", ";
497 Out << "..."; // Output varargs portion of signature!
503 void CWriter::printFunction(Function *F) {
504 if (F->isExternal()) return;
506 Table.incorporateFunction(F);
508 printFunctionSignature(F);
511 // print local variable information for the function
512 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
513 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
515 printType((*I)->getType(), getValueName(*I));
519 // print the basic blocks
520 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
521 BasicBlock *Prev = BB->getPrev();
523 // Don't print the label for the basic block if there are no uses, or if the
524 // only terminator use is the precessor basic block's terminator. We have
525 // to scan the use list because PHI nodes use basic blocks too but do not
526 // require a label to be generated.
528 bool NeedsLabel = false;
529 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
531 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
532 if (TI != Prev->getTerminator()) {
537 if (NeedsLabel) Out << getValueName(BB) << ":\n";
539 // Output all of the instructions in the basic block...
540 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
541 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
542 if (II->getType() != Type::VoidTy)
551 // Don't emit prefix or suffix for the terminator...
552 visit(*BB->getTerminator());
556 Table.purgeFunction();
559 // Specific Instruction type classes... note that all of the casts are
560 // neccesary because we use the instruction classes as opaque types...
562 void CWriter::visitReturnInst(ReturnInst &I) {
563 // Don't output a void return if this is the last basic block in the function
564 if (I.getNumOperands() == 0 &&
565 &*--I.getParent()->getParent()->end() == I.getParent() &&
566 !I.getParent()->size() == 1) {
571 if (I.getNumOperands()) {
573 writeOperand(I.getOperand(0));
578 static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
579 // If PHI nodes need copies, we need the copy code...
580 if (isa<PHINode>(To->front()) ||
581 From->getNext() != To) // Not directly successor, need goto
584 // Otherwise we don't need the code.
588 void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
590 for (BasicBlock::iterator I = Succ->begin();
591 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
592 // now we have to do the printing
593 Out << string(Indent, ' ');
595 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
596 Out << "; /* for PHI node */\n";
599 if (CurBB->getNext() != Succ) {
600 Out << string(Indent, ' ') << " goto ";
606 // Brach instruction printing - Avoid printing out a brach to a basic block that
607 // immediately succeeds the current one.
609 void CWriter::visitBranchInst(BranchInst &I) {
610 if (I.isConditional()) {
611 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
613 writeOperand(I.getCondition());
616 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
618 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
619 Out << " } else {\n";
620 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
623 // First goto not neccesary, assume second one is...
625 writeOperand(I.getCondition());
628 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
633 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
639 void CWriter::visitNot(GenericUnaryInst &I) {
641 writeOperand(I.getOperand(0));
644 void CWriter::visitBinaryOperator(Instruction &I) {
645 // binary instructions, shift instructions, setCond instructions.
646 if (isa<PointerType>(I.getType())) {
648 printType(I.getType());
652 if (isa<PointerType>(I.getType())) Out << "(long long)";
653 writeOperand(I.getOperand(0));
655 switch (I.getOpcode()) {
656 case Instruction::Add: Out << " + "; break;
657 case Instruction::Sub: Out << " - "; break;
658 case Instruction::Mul: Out << "*"; break;
659 case Instruction::Div: Out << "/"; break;
660 case Instruction::Rem: Out << "%"; break;
661 case Instruction::And: Out << " & "; break;
662 case Instruction::Or: Out << " | "; break;
663 case Instruction::Xor: Out << " ^ "; break;
664 case Instruction::SetEQ: Out << " == "; break;
665 case Instruction::SetNE: Out << " != "; break;
666 case Instruction::SetLE: Out << " <= "; break;
667 case Instruction::SetGE: Out << " >= "; break;
668 case Instruction::SetLT: Out << " < "; break;
669 case Instruction::SetGT: Out << " > "; break;
670 case Instruction::Shl : Out << " << "; break;
671 case Instruction::Shr : Out << " >> "; break;
672 default: cerr << "Invalid operator type!" << I; abort();
675 if (isa<PointerType>(I.getType())) Out << "(long long)";
676 writeOperand(I.getOperand(1));
679 void CWriter::visitCastInst(CastInst &I) {
681 printType(I.getType());
683 writeOperand(I.getOperand(0));
686 void CWriter::visitCallInst(CallInst &I) {
687 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
688 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
689 const Type *RetTy = FTy->getReturnType();
691 Out << getValueName(I.getOperand(0)) << "(";
693 if (I.getNumOperands() > 1) {
694 writeOperand(I.getOperand(1));
696 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
698 writeOperand(I.getOperand(op));
704 void CWriter::visitMallocInst(MallocInst &I) {
706 printType(I.getType());
707 Out << ")malloc(sizeof(";
708 printType(I.getType()->getElementType());
711 if (I.isArrayAllocation()) {
713 writeOperand(I.getOperand(0));
718 void CWriter::visitAllocaInst(AllocaInst &I) {
720 printType(I.getType());
721 Out << ") alloca(sizeof(";
722 printType(I.getType()->getElementType());
724 if (I.isArrayAllocation()) {
726 writeOperand(I.getOperand(0));
731 void CWriter::visitFreeInst(FreeInst &I) {
733 writeOperand(I.getOperand(0));
737 void CWriter::printIndexingExpr(MemAccessInst &MAI) {
738 MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end();
740 // If accessing a global value with no indexing, avoid *(&GV) syndrome
741 if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) {
742 writeOperandInternal(V);
746 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
749 writeOperand(MAI.getPointerOperand());
753 // Print out the -> operator if possible...
754 const Constant *CI = dyn_cast<Constant>(I->get());
755 if (CI && CI->isNullValue() && I+1 != E &&
756 (*(I+1))->getType() == Type::UByteTy) {
757 Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue();
762 if ((*I)->getType() == Type::UIntTy) {
767 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
771 void CWriter::visitLoadInst(LoadInst &I) {
772 printIndexingExpr(I);
775 void CWriter::visitStoreInst(StoreInst &I) {
776 printIndexingExpr(I);
778 writeOperand(I.getOperand(0));
781 void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
783 printIndexingExpr(I);
786 //===----------------------------------------------------------------------===//
787 // External Interface declaration
788 //===----------------------------------------------------------------------===//
790 void WriteToC(const Module *M, ostream &Out) {
791 assert(M && "You can't write a null module!!");
792 SlotCalculator SlotTable(M, false);
793 CWriter W(Out, SlotTable, M);