1 //===-- Writer.cpp - Library for writing C files --------------------------===//
3 // This library implements the functionality defined in llvm/Assembly/CWriter.h
6 // TODO : Recursive types.
8 //===-----------------------------------------------------------------------==//
10 #include "llvm/Assembly/CWriter.h"
11 #include "CLocalVars.h"
12 #include "llvm/SlotCalculator.h"
13 #include "llvm/Constants.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Module.h"
16 #include "llvm/GlobalVariable.h"
17 #include "llvm/Function.h"
18 #include "llvm/Argument.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/iMemory.h"
21 #include "llvm/iTerminators.h"
22 #include "llvm/iPHINode.h"
23 #include "llvm/iOther.h"
24 #include "llvm/iOperators.h"
25 #include "llvm/SymbolTable.h"
26 #include "llvm/Support/InstVisitor.h"
27 #include "Support/StringExtras.h"
28 #include "Support/STLExtras.h"
37 //===-----------------------------------------------------------------------==//
39 // Implementation of the CLocalVars methods
41 // Appends a variable to the LocalVars map if it does not already exist
42 // Also check that the type exists on the map.
43 void CLocalVars::addLocalVar(const Type *t, const string & var) {
44 if (!LocalVars.count(t) ||
45 find(LocalVars[t].begin(), LocalVars[t].end(), var)
46 == LocalVars[t].end()) {
47 LocalVars[t].push_back(var);
51 static std::string getConstStrValue(const Constant* CPV);
54 static std::string getConstArrayStrValue(const Constant* CPV) {
57 // As a special case, print the array as a string if it is an array of
58 // ubytes or an array of sbytes with positive values.
60 const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
61 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
63 if (ETy == Type::SByteTy) {
64 for (unsigned i = 0; i < CPV->getNumOperands(); ++i)
65 if (ETy == Type::SByteTy &&
66 cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) {
72 // Make sure the last character is a null char, as automatically added by C
73 if (CPV->getNumOperands() == 0 ||
74 !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
80 // Do not include the last character, which we know is null
81 for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
82 unsigned char C = (ETy == Type::SByteTy) ?
83 (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
84 (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
90 case '\n': Result += "\\n"; break;
91 case '\t': Result += "\\t"; break;
92 case '\r': Result += "\\r"; break;
93 case '\v': Result += "\\v"; break;
94 case '\a': Result += "\\a"; break;
97 Result += ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
98 Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
107 if (CPV->getNumOperands()) {
108 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
109 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
110 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
118 static std::string getConstStrValue(const Constant* CPV) {
119 switch (CPV->getType()->getPrimitiveID()) {
121 return CPV == ConstantBool::False ? "0" : "1";
122 case Type::SByteTyID:
123 case Type::ShortTyID:
125 return itostr(cast<ConstantSInt>(CPV)->getValue());
127 return itostr(cast<ConstantSInt>(CPV)->getValue()) + "ll";
129 case Type::UByteTyID:
130 return utostr(cast<ConstantUInt>(CPV)->getValue());
131 case Type::UShortTyID:
132 return utostr(cast<ConstantUInt>(CPV)->getValue());
134 return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
135 case Type::ULongTyID:
136 return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
138 case Type::FloatTyID:
139 case Type::DoubleTyID:
140 return ftostr(cast<ConstantFP>(CPV)->getValue());
142 case Type::ArrayTyID:
143 return getConstArrayStrValue(CPV);
145 case Type::StructTyID: {
146 std::string Result = "{";
147 if (CPV->getNumOperands()) {
148 Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
149 for (unsigned i = 1; i < CPV->getNumOperands(); i++)
150 Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
152 return Result + " }";
156 cerr << "Unknown constant type: " << CPV << "\n";
161 // Pass the Type* variable and and the variable name and this prints out the
162 // variable declaration.
164 static string calcTypeNameVar(const Type *Ty,
165 map<const Type *, string> &TypeNames,
166 const string &NameSoFar, bool ignoreName = false){
167 if (Ty->isPrimitiveType())
168 switch (Ty->getPrimitiveID()) {
169 case Type::VoidTyID: return "void " + NameSoFar;
170 case Type::BoolTyID: return "bool " + NameSoFar;
171 case Type::UByteTyID: return "unsigned char " + NameSoFar;
172 case Type::SByteTyID: return "signed char " + NameSoFar;
173 case Type::UShortTyID: return "unsigned short " + NameSoFar;
174 case Type::ShortTyID: return "short " + NameSoFar;
175 case Type::UIntTyID: return "unsigned " + NameSoFar;
176 case Type::IntTyID: return "int " + NameSoFar;
177 case Type::ULongTyID: return "unsigned long long " + NameSoFar;
178 case Type::LongTyID: return "signed long long " + NameSoFar;
179 case Type::FloatTyID: return "float " + NameSoFar;
180 case Type::DoubleTyID: return "double " + NameSoFar;
182 cerr << "Unknown primitive type: " << Ty << "\n";
186 // Check to see if the type is named.
188 map<const Type *, string>::iterator I = TypeNames.find(Ty);
189 if (I != TypeNames.end())
190 return I->second + " " + NameSoFar;
194 switch (Ty->getPrimitiveID()) {
195 case Type::FunctionTyID: {
196 const FunctionType *MTy = cast<const FunctionType>(Ty);
197 Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
198 Result += " " + NameSoFar;
200 for (FunctionType::ParamTypes::const_iterator
201 I = MTy->getParamTypes().begin(),
202 E = MTy->getParamTypes().end(); I != E; ++I) {
203 if (I != MTy->getParamTypes().begin())
205 Result += calcTypeNameVar(*I, TypeNames, "");
207 if (MTy->isVarArg()) {
208 if (!MTy->getParamTypes().empty())
215 case Type::StructTyID: {
216 const StructType *STy = cast<const StructType>(Ty);
217 Result = NameSoFar + " {\n";
219 for (StructType::ElementTypes::const_iterator
220 I = STy->getElementTypes().begin(),
221 E = STy->getElementTypes().end(); I != E; ++I) {
222 Result += " " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
229 case Type::PointerTyID: {
230 Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(),
231 TypeNames, "*" + NameSoFar);
235 case Type::ArrayTyID: {
236 const ArrayType *ATy = cast<const ArrayType>(Ty);
237 int NumElements = ATy->getNumElements();
238 Result = calcTypeNameVar(ATy->getElementType(), TypeNames,
239 NameSoFar + "[" + itostr(NumElements) + "]");
243 assert(0 && "Unhandled case in getTypeProps!");
253 SlotCalculator &Table;
254 const Module *TheModule;
255 map<const Type *, string> TypeNames;
257 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
258 : Out(o), Table(Tab), TheModule(M) {
261 inline void write(Module *M) { printModule(M); }
263 ostream& printTypeVar(const Type *Ty, const string &VariableName) {
264 return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
267 ostream& printType(const Type *Ty) {
268 return Out << calcTypeNameVar(Ty, TypeNames, "");
271 void writeOperand(const Value *Operand);
273 string getValueName(const Value *V);
276 void printModule(Module *M);
277 void printSymbolTable(const SymbolTable &ST);
278 void printGlobal(const GlobalVariable *GV);
279 void printFunctionSignature(const Function *F);
280 void printFunctionDecl(const Function *F); // Print just the forward decl
282 void printFunction(Function *);
284 /* END class CWriter */
287 /* CLASS InstLocalVarsVisitor */
288 class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
290 void handleTerminator(TerminatorInst *tI, int indx);
294 InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
296 void visitInstruction(Instruction *I) {
297 if (I->getType() != Type::VoidTy)
298 CLV.addLocalVar(I->getType(), CW.getValueName(I));
301 void visitBranchInst(BranchInst *I) {
302 handleTerminator(I, 0);
303 if (I->isConditional())
304 handleTerminator(I, 1);
309 void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
310 BasicBlock *bb = tI->getSuccessor(indx);
312 BasicBlock::const_iterator insIt = bb->begin();
313 while (insIt != bb->end()) {
314 if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
316 // Calculate the incoming index for this
317 assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
319 CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
327 /* CLASS CInstPrintVisitor */
329 class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
331 SlotCalculator& Table;
334 void outputLValue(Instruction *);
335 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
337 void printIndexingExpr(MemAccessInst *MAI);
340 CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o)
341 : CW(cw), Table(table), Out(o) {}
343 void visitCastInst(CastInst *I);
344 void visitCallInst(CallInst *I);
345 void visitShiftInst(ShiftInst *I) { visitBinaryOperator(I); }
346 void visitReturnInst(ReturnInst *I);
347 void visitBranchInst(BranchInst *I);
348 void visitSwitchInst(SwitchInst *I);
349 void visitInvokeInst(InvokeInst *I) ;
350 void visitMallocInst(MallocInst *I);
351 void visitAllocaInst(AllocaInst *I);
352 void visitFreeInst(FreeInst *I);
353 void visitLoadInst(LoadInst *I);
354 void visitStoreInst(StoreInst *I);
355 void visitGetElementPtrInst(GetElementPtrInst *I);
356 void visitPHINode(PHINode *I) {}
358 void visitNot(GenericUnaryInst *I);
359 void visitBinaryOperator(Instruction *I);
363 void CInstPrintVisitor::outputLValue(Instruction *I) {
364 Out << " " << CW.getValueName(I) << " = ";
367 // Implement all "other" instructions, except for PHINode
368 void CInstPrintVisitor::visitCastInst(CastInst *I) {
371 CW.printType(I->getType());
373 CW.writeOperand(I->getOperand(0));
377 void CInstPrintVisitor::visitCallInst(CallInst *I) {
378 if (I->getType() != Type::VoidTy)
383 const PointerType *PTy = cast<PointerType>(I->getCalledValue()->getType());
384 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
385 const Type *RetTy = FTy->getReturnType();
387 Out << CW.getValueName(I->getOperand(0)) << "(";
389 if (I->getNumOperands() > 1) {
390 CW.writeOperand(I->getOperand(1));
392 for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) {
394 CW.writeOperand(I->getOperand(op));
400 // Specific Instruction type classes... note that all of the casts are
401 // neccesary because we use the instruction classes as opaque types...
403 void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
404 // Don't output a void return if this is the last basic block in the function
405 if (I->getNumOperands() == 0 &&
406 *(I->getParent()->getParent()->end()-1) == I->getParent())
410 if (I->getNumOperands()) {
412 CW.writeOperand(I->getOperand(0));
417 // Return true if BB1 immediately preceeds BB2.
418 static bool BBFollowsBB(BasicBlock *BB1, BasicBlock *BB2) {
419 Function *F = BB1->getParent();
420 Function::iterator I = find(F->begin(), F->end(), BB1);
421 assert(I != F->end() && "BB not in function!");
422 return *(I+1) == BB2;
425 static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
426 // If PHI nodes need copies, we need the copy code...
427 if (isa<PHINode>(To->front()) ||
428 !BBFollowsBB(From, To)) // Not directly successor, need goto
431 // Otherwise we don't need the code.
435 void CInstPrintVisitor::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
437 for (BasicBlock::iterator I = Succ->begin();
438 PHINode *PN = dyn_cast<PHINode>(*I); ++I) {
439 // now we have to do the printing
440 Out << string(Indent, ' ');
442 CW.writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
443 Out << "; /* for PHI node */\n";
446 if (!BBFollowsBB(CurBB, Succ)) {
447 Out << string(Indent, ' ') << " goto ";
448 CW.writeOperand(Succ);
453 // Brach instruction printing - Avoid printing out a brach to a basic block that
454 // immediately succeeds the current one.
456 void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
457 if (I->isConditional()) {
458 if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(0))) {
460 CW.writeOperand(I->getCondition());
463 printBranchToBlock(I->getParent(), I->getSuccessor(0), 2);
465 if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(1))) {
466 Out << " } else {\n";
467 printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
470 // First goto not neccesary, assume second one is...
472 CW.writeOperand(I->getCondition());
475 printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
480 printBranchToBlock(I->getParent(), I->getSuccessor(0), 0);
485 void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
486 assert(0 && "Switch not implemented!");
489 void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
490 assert(0 && "Invoke not implemented!");
493 void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
496 CW.printType(I->getType());
497 Out << ")malloc(sizeof(";
498 CW.printType(I->getType()->getElementType());
501 if (I->isArrayAllocation()) {
503 CW.writeOperand(I->getOperand(0));
508 void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
511 CW.printType(I->getType());
512 Out << ") alloca(sizeof(";
513 CW.printType(I->getType()->getElementType());
515 if (I->isArrayAllocation()) {
517 CW.writeOperand(I->getOperand(0));
522 void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
524 CW.writeOperand(I->getOperand(0));
528 void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
529 MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
531 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
533 CW.writeOperand(MAI->getPointerOperand());
537 // Print out the -> operator if possible...
538 Constant *CI = dyn_cast<Constant>(*I);
539 if (CI && CI->isNullValue() && I+1 != E &&
540 (*(I+1))->getType() == Type::UByteTy) {
542 Out << "->field" << cast<ConstantUInt>(*I)->getValue();
547 if ((*I)->getType() == Type::UIntTy) {
552 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
556 void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
558 printIndexingExpr(I);
562 void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
564 printIndexingExpr(I);
566 CW.writeOperand(I->getOperand(0));
570 void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
573 printIndexingExpr(I);
577 void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
580 CW.writeOperand(I->getOperand(0));
584 void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
585 // binary instructions, shift instructions, setCond instructions.
587 if (isa<PointerType>(I->getType())) {
589 CW.printType(I->getType());
593 if (isa<PointerType>(I->getType())) Out << "(long long)";
594 CW.writeOperand(I->getOperand(0));
596 switch (I->getOpcode()) {
597 case Instruction::Add: Out << " + "; break;
598 case Instruction::Sub: Out << " - "; break;
599 case Instruction::Mul: Out << "*"; break;
600 case Instruction::Div: Out << "/"; break;
601 case Instruction::Rem: Out << "%"; break;
602 case Instruction::And: Out << " & "; break;
603 case Instruction::Or: Out << " | "; break;
604 case Instruction::Xor: Out << " ^ "; break;
605 case Instruction::SetEQ: Out << " == "; break;
606 case Instruction::SetNE: Out << " != "; break;
607 case Instruction::SetLE: Out << " <= "; break;
608 case Instruction::SetGE: Out << " >= "; break;
609 case Instruction::SetLT: Out << " < "; break;
610 case Instruction::SetGT: Out << " > "; break;
611 case Instruction::Shl : Out << " << "; break;
612 case Instruction::Shr : Out << " >> "; break;
613 default: cerr << "Invalid operator type!" << I; abort();
616 if (isa<PointerType>(I->getType())) Out << "(long long)";
617 CW.writeOperand(I->getOperand(1));
621 /* END : CInstPrintVisitor implementation */
623 // We dont want identifier names with ., space, - in them.
624 // So we replace them with _
625 static string makeNameProper(string x) {
627 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
629 case '.': tmp += "d_"; break;
630 case ' ': tmp += "s_"; break;
631 case '-': tmp += "D_"; break;
632 case '_': tmp += "__"; break;
639 string CWriter::getValueName(const Value *V) {
640 if (V->hasName()) { // Print out the label if it exists...
641 if (isa<GlobalValue>(V)) // Do not mangle globals...
642 return makeNameProper(V->getName());
644 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
645 makeNameProper(V->getName());
648 int Slot = Table.getValSlot(V);
649 assert(Slot >= 0 && "Invalid value!");
650 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
653 void CWriter::printModule(Module *M) {
654 // printing stdlib inclusion
655 // Out << "#include <stdlib.h>\n";
657 // get declaration for alloca
658 Out << "/* Provide Declarations */\n"
659 << "#include <alloca.h>\n\n"
661 // Provide a definition for null if one does not already exist.
662 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
663 << "typedef unsigned char bool;\n"
665 << "\n\n/* Global Symbols */\n";
667 // Loop over the symbol table, emitting all named constants...
668 if (M->hasSymbolTable())
669 printSymbolTable(*M->getSymbolTable());
671 Out << "\n\n/* Global Data */\n";
672 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
673 GlobalVariable *GV = *I;
674 if (GV->hasInternalLinkage()) Out << "static ";
675 printTypeVar(GV->getType()->getElementType(), getValueName(GV));
677 if (GV->hasInitializer()) {
679 writeOperand(GV->getInitializer());
684 // First output all the declarations of the functions as C requires Functions
685 // be declared before they are used.
687 Out << "\n\n/* Function Declarations */\n";
688 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
690 // Output all of the functions...
691 Out << "\n\n/* Function Bodies */\n";
692 for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
696 // printSymbolTable - Run through symbol table looking for named constants
697 // if a named constant is found, emit it's declaration...
698 // Assuming that symbol table has only types and constants.
699 void CWriter::printSymbolTable(const SymbolTable &ST) {
700 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
701 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
702 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
704 for (; I != End; ++I)
705 if (const Type *Ty = dyn_cast<const StructType>(I->second)) {
706 string Name = "struct l_" + I->first;
707 Out << Name << ";\n";
709 TypeNames.insert(std::make_pair(Ty, Name));
715 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
716 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
717 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
719 for (; I != End; ++I) {
720 const Value *V = I->second;
721 if (const Type *Ty = dyn_cast<const Type>(V)) {
722 string Name = "l_" + I->first;
723 if (isa<StructType>(Ty))
724 Name = "struct " + Name;
728 Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
735 // printFunctionDecl - Print function declaration
737 void CWriter::printFunctionDecl(const Function *F) {
738 printFunctionSignature(F);
742 void CWriter::printFunctionSignature(const Function *F) {
743 if (F->hasInternalLinkage()) Out << "static ";
745 // Loop over the arguments, printing them...
746 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
748 // Print out the return type and name...
749 printType(F->getReturnType());
750 Out << getValueName(F) << "(";
752 if (!F->isExternal()) {
753 if (!F->getArgumentList().empty()) {
754 printTypeVar(F->getArgumentList().front()->getType(),
755 getValueName(F->getArgumentList().front()));
757 for (Function::ArgumentListType::const_iterator
758 I = F->getArgumentList().begin()+1,
759 E = F->getArgumentList().end(); I != E; ++I) {
761 printTypeVar((*I)->getType(), getValueName(*I));
765 // Loop over the arguments, printing them...
766 for (FunctionType::ParamTypes::const_iterator I =
767 FT->getParamTypes().begin(),
768 E = FT->getParamTypes().end(); I != E; ++I) {
769 if (I != FT->getParamTypes().begin()) Out << ", ";
774 // Finish printing arguments...
775 if (FT->isVarArg()) {
776 if (FT->getParamTypes().size()) Out << ", ";
777 Out << "..."; // Output varargs portion of signature!
783 void CWriter::printFunction(Function *F) {
784 if (F->isExternal()) return;
786 Table.incorporateFunction(F);
788 printFunctionSignature(F);
791 // Process each of the basic blocks, gather information and call the
792 // output methods on the CLocalVars and Function* objects.
794 // gather local variable information for each basic block
795 InstLocalVarsVisitor ILV(*this);
798 // print the local variables
799 // we assume that every local variable is alloca'ed in the C code.
800 std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
802 map<const Type*, VarListType>::iterator iter;
803 for (iter = locals.begin(); iter != locals.end(); ++iter) {
804 VarListType::iterator listiter;
805 for (listiter = iter->second.begin(); listiter != iter->second.end();
808 printTypeVar(iter->first, *listiter);
813 // print the basic blocks
814 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
815 BasicBlock *BB = *I, *Prev = I != F->begin() ? *(I-1) : 0;
817 // Don't print the label for the basic block if there are no uses, or if the
818 // only terminator use is the precessor basic block's terminator. We have
819 // to scan the use list because PHI nodes use basic blocks too but do not
820 // require a label to be generated.
822 bool NeedsLabel = false;
823 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
825 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
826 if (TI != Prev->getTerminator()) {
831 if (NeedsLabel) Out << getValueName(BB) << ":\n";
833 // Output all of the instructions in the basic block...
834 // print the basic blocks
835 CInstPrintVisitor CIPV(*this, Table, Out);
840 Table.purgeFunction();
843 void CWriter::writeOperand(const Value *Operand) {
844 if (isa<GlobalVariable>(Operand))
845 Out << "(&"; // Global variables are references as their addresses by llvm
847 if (Operand->hasName()) {
848 Out << getValueName(Operand);
849 } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
850 if (isa<ConstantPointerNull>(CPV)) {
852 printTypeVar(CPV->getType(), "");
855 Out << getConstStrValue(CPV);
857 int Slot = Table.getValSlot(Operand);
858 assert(Slot >= 0 && "Malformed LLVM!");
859 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
862 if (isa<GlobalVariable>(Operand))
867 //===----------------------------------------------------------------------===//
868 // External Interface declaration
869 //===----------------------------------------------------------------------===//
871 void WriteToC(const Module *M, ostream &Out) {
872 assert(M && "You can't write a null module!!");
873 SlotCalculator SlotTable(M, false);
874 CWriter W(Out, SlotTable, M);