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"
31 class CWriter : public InstVisitor<CWriter> {
33 SlotCalculator &Table;
34 const Module *TheModule;
35 map<const Type *, string> TypeNames;
36 std::set<const Value*> MangledGlobals;
38 inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
39 : Out(o), Table(Tab), TheModule(M) {
42 inline void write(Module *M) { printModule(M); }
44 ostream &printType(const Type *Ty, const string &VariableName = "",
45 bool IgnoreName = false);
47 void writeOperand(Value *Operand);
48 void writeOperandInternal(Value *Operand);
50 string getValueName(const Value *V);
53 void printModule(Module *M);
54 void printSymbolTable(const SymbolTable &ST);
55 void printGlobal(const GlobalVariable *GV);
56 void printFunctionSignature(const Function *F);
57 void printFunctionDecl(const Function *F); // Print just the forward decl
59 void printFunction(Function *);
61 void printConstant(Constant *CPV);
62 void printConstantArray(ConstantArray *CPA);
64 // isInlinableInst - Attempt to inline instructions into their uses to build
65 // trees as much as possible. To do this, we have to consistently decide
66 // what is acceptable to inline, so that variable declarations don't get
67 // printed and an extra copy of the expr is not emitted.
69 static bool isInlinableInst(const Instruction &I) {
70 // Must be an expression, must be used exactly once. If it is dead, we
71 // emit it inline where it would go.
72 if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
73 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
76 // Only inline instruction it it's use is in the same BB as the inst.
77 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
80 // Instruction visitation functions
81 friend class InstVisitor<CWriter>;
83 void visitReturnInst(ReturnInst &I);
84 void visitBranchInst(BranchInst &I);
86 void visitPHINode(PHINode &I) {}
87 void visitBinaryOperator(Instruction &I);
89 void visitCastInst (CastInst &I);
90 void visitCallInst (CallInst &I);
91 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
93 void visitMallocInst(MallocInst &I);
94 void visitAllocaInst(AllocaInst &I);
95 void visitFreeInst (FreeInst &I);
96 void visitLoadInst (LoadInst &I);
97 void visitStoreInst (StoreInst &I);
98 void visitGetElementPtrInst(GetElementPtrInst &I);
100 void visitInstruction(Instruction &I) {
101 std::cerr << "C Writer does not know about " << I;
105 void outputLValue(Instruction *I) {
106 Out << " " << getValueName(I) << " = ";
108 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
110 void printIndexingExpression(Value *Ptr, User::op_iterator I,
111 User::op_iterator E);
115 // We dont want identifier names with ., space, - in them.
116 // So we replace them with _
117 static string makeNameProper(string x) {
119 for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
121 case '.': tmp += "d_"; break;
122 case ' ': tmp += "s_"; break;
123 case '-': tmp += "D_"; break;
130 string CWriter::getValueName(const Value *V) {
131 if (V->hasName()) { // Print out the label if it exists...
132 if (isa<GlobalValue>(V) && // Do not mangle globals...
133 cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
134 !MangledGlobals.count(V)) // Unless the name would collide if we don't
135 return makeNameProper(V->getName());
137 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
138 makeNameProper(V->getName());
141 int Slot = Table.getValSlot(V);
142 assert(Slot >= 0 && "Invalid value!");
143 return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
146 // Pass the Type* and the variable name and this prints out the variable
149 ostream &CWriter::printType(const Type *Ty, const string &NameSoFar,
150 bool IgnoreName = false) {
151 if (Ty->isPrimitiveType())
152 switch (Ty->getPrimitiveID()) {
153 case Type::VoidTyID: return Out << "void " << NameSoFar;
154 case Type::BoolTyID: return Out << "bool " << NameSoFar;
155 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
156 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
157 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
158 case Type::ShortTyID: return Out << "short " << NameSoFar;
159 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
160 case Type::IntTyID: return Out << "int " << NameSoFar;
161 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
162 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
163 case Type::FloatTyID: return Out << "float " << NameSoFar;
164 case Type::DoubleTyID: return Out << "double " << NameSoFar;
166 std::cerr << "Unknown primitive type: " << Ty << "\n";
170 // Check to see if the type is named.
172 map<const Type *, string>::iterator I = TypeNames.find(Ty);
173 if (I != TypeNames.end()) {
174 return Out << I->second << " " << NameSoFar;
178 switch (Ty->getPrimitiveID()) {
179 case Type::FunctionTyID: {
180 const FunctionType *MTy = cast<FunctionType>(Ty);
181 printType(MTy->getReturnType(), "");
182 Out << " " << NameSoFar << " (";
184 for (FunctionType::ParamTypes::const_iterator
185 I = MTy->getParamTypes().begin(),
186 E = MTy->getParamTypes().end(); I != E; ++I) {
187 if (I != MTy->getParamTypes().begin())
191 if (MTy->isVarArg()) {
192 if (!MTy->getParamTypes().empty())
198 case Type::StructTyID: {
199 const StructType *STy = cast<StructType>(Ty);
200 Out << NameSoFar + " {\n";
202 for (StructType::ElementTypes::const_iterator
203 I = STy->getElementTypes().begin(),
204 E = STy->getElementTypes().end(); I != E; ++I) {
206 printType(*I, "field" + utostr(Idx++));
212 case Type::PointerTyID: {
213 const PointerType *PTy = cast<PointerType>(Ty);
214 return printType(PTy->getElementType(), "(*" + NameSoFar + ")");
217 case Type::ArrayTyID: {
218 const ArrayType *ATy = cast<ArrayType>(Ty);
219 unsigned NumElements = ATy->getNumElements();
220 return printType(ATy->getElementType(),
221 NameSoFar + "[" + utostr(NumElements) + "]");
224 assert(0 && "Unhandled case in getTypeProps!");
231 void CWriter::printConstantArray(ConstantArray *CPA) {
233 // As a special case, print the array as a string if it is an array of
234 // ubytes or an array of sbytes with positive values.
236 const Type *ETy = CPA->getType()->getElementType();
237 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
239 // Make sure the last character is a null char, as automatically added by C
240 if (CPA->getNumOperands() == 0 ||
241 !cast<Constant>(*(CPA->op_end()-1))->isNullValue())
246 // Do not include the last character, which we know is null
247 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
248 unsigned char C = (ETy == Type::SByteTy) ?
249 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
250 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
256 case '\n': Out << "\\n"; break;
257 case '\t': Out << "\\t"; break;
258 case '\r': Out << "\\r"; break;
259 case '\v': Out << "\\v"; break;
260 case '\a': Out << "\\a"; break;
263 Out << ( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
264 Out << ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
272 if (CPA->getNumOperands()) {
274 printConstant(cast<Constant>(CPA->getOperand(0)));
275 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
277 printConstant(cast<Constant>(CPA->getOperand(i)));
285 // printConstant - The LLVM Constant to C Constant converter.
286 void CWriter::printConstant(Constant *CPV) {
287 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
288 switch (CE->getOpcode()) {
289 case Instruction::Cast:
291 printType(CPV->getType());
293 printConstant(cast<Constant>(CPV->getOperand(0)));
297 case Instruction::GetElementPtr:
299 printIndexingExpression(CPV->getOperand(0),
300 CPV->op_begin()+1, CPV->op_end());
303 case Instruction::Add:
305 printConstant(cast<Constant>(CPV->getOperand(0)));
307 printConstant(cast<Constant>(CPV->getOperand(1)));
310 case Instruction::Sub:
312 printConstant(cast<Constant>(CPV->getOperand(0)));
314 printConstant(cast<Constant>(CPV->getOperand(1)));
319 std::cerr << "CWriter Error: Unhandled constant expression: "
325 switch (CPV->getType()->getPrimitiveID()) {
327 Out << (CPV == ConstantBool::False ? "0" : "1"); break;
328 case Type::SByteTyID:
329 case Type::ShortTyID:
331 Out << cast<ConstantSInt>(CPV)->getValue(); break;
333 Out << cast<ConstantSInt>(CPV)->getValue() << "ll"; break;
335 case Type::UByteTyID:
336 case Type::UShortTyID:
337 Out << cast<ConstantUInt>(CPV)->getValue(); break;
339 Out << cast<ConstantUInt>(CPV)->getValue() << "u"; break;
340 case Type::ULongTyID:
341 Out << cast<ConstantUInt>(CPV)->getValue() << "ull"; break;
343 case Type::FloatTyID:
344 case Type::DoubleTyID:
345 Out << cast<ConstantFP>(CPV)->getValue(); break;
347 case Type::ArrayTyID:
348 printConstantArray(cast<ConstantArray>(CPV));
351 case Type::StructTyID: {
353 if (CPV->getNumOperands()) {
355 printConstant(cast<Constant>(CPV->getOperand(0)));
356 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
358 printConstant(cast<Constant>(CPV->getOperand(i)));
365 case Type::PointerTyID:
366 if (isa<ConstantPointerNull>(CPV)) {
368 printType(CPV->getType(), "");
371 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CPV)) {
372 writeOperand(CPR->getValue());
377 std::cerr << "Unknown constant type: " << CPV << "\n";
382 void CWriter::writeOperandInternal(Value *Operand) {
383 if (Instruction *I = dyn_cast<Instruction>(Operand))
384 if (isInlinableInst(*I)) {
385 // Should we inline this instruction to build a tree?
392 if (Operand->hasName()) {
393 Out << getValueName(Operand);
394 } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
397 int Slot = Table.getValSlot(Operand);
398 assert(Slot >= 0 && "Malformed LLVM!");
399 Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
403 void CWriter::writeOperand(Value *Operand) {
404 if (isa<GlobalVariable>(Operand))
405 Out << "(&"; // Global variables are references as their addresses by llvm
407 writeOperandInternal(Operand);
409 if (isa<GlobalVariable>(Operand))
413 void CWriter::printModule(Module *M) {
414 // Calculate which global values have names that will collide when we throw
415 // away type information.
416 { // Scope to delete the FoundNames set when we are done with it...
417 std::set<string> FoundNames;
418 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
419 if (I->hasName()) // If the global has a name...
420 if (FoundNames.count(I->getName())) // And the name is already used
421 MangledGlobals.insert(I); // Mangle the name
423 FoundNames.insert(I->getName()); // Otherwise, keep track of name
425 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
426 if (I->hasName()) // If the global has a name...
427 if (FoundNames.count(I->getName())) // And the name is already used
428 MangledGlobals.insert(I); // Mangle the name
430 FoundNames.insert(I->getName()); // Otherwise, keep track of name
434 // printing stdlib inclusion
435 // Out << "#include <stdlib.h>\n";
437 // get declaration for alloca
438 Out << "/* Provide Declarations */\n"
439 << "#include <malloc.h>\n"
440 << "#include <alloca.h>\n\n"
442 // Provide a definition for null if one does not already exist.
443 << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
444 << "typedef unsigned char bool;\n"
446 << "\n\n/* Global Declarations */\n";
448 // First output all the declarations for the program, because C requires
449 // Functions & globals to be declared before they are used.
452 // Loop over the symbol table, emitting all named constants...
453 if (M->hasSymbolTable())
454 printSymbolTable(*M->getSymbolTable());
456 // Global variable declarations...
458 Out << "\n/* Global Variable Declarations */\n";
459 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
460 Out << (I->hasExternalLinkage() ? "extern " : "static ");
461 printType(I->getType()->getElementType(), getValueName(I));
466 // Function declarations
468 Out << "\n/* Function Declarations */\n";
469 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
470 printFunctionDecl(I);
473 // Output the global variable contents...
475 Out << "\n\n/* Global Data */\n";
476 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
477 if (I->hasInternalLinkage()) Out << "static ";
478 printType(I->getType()->getElementType(), getValueName(I));
480 if (I->hasInitializer()) {
482 writeOperand(I->getInitializer());
488 // Output all of the functions...
490 Out << "\n\n/* Function Bodies */\n";
491 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
497 // printSymbolTable - Run through symbol table looking for named constants
498 // if a named constant is found, emit it's declaration...
499 // Assuming that symbol table has only types and constants.
500 void CWriter::printSymbolTable(const SymbolTable &ST) {
501 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
502 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
503 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
505 for (; I != End; ++I)
506 if (const Type *Ty = dyn_cast<StructType>(I->second)) {
507 string Name = "struct l_" + makeNameProper(I->first);
508 Out << Name << ";\n";
509 TypeNames.insert(std::make_pair(Ty, Name));
515 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
516 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
517 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
519 for (; I != End; ++I) {
520 const Value *V = I->second;
521 if (const Type *Ty = dyn_cast<Type>(V)) {
522 string Name = "l_" + makeNameProper(I->first);
523 if (isa<StructType>(Ty))
524 Name = "struct " + makeNameProper(Name);
528 printType(Ty, Name, true);
536 // printFunctionDecl - Print function declaration
538 void CWriter::printFunctionDecl(const Function *F) {
539 printFunctionSignature(F);
543 void CWriter::printFunctionSignature(const Function *F) {
544 if (F->hasInternalLinkage()) Out << "static ";
546 // Loop over the arguments, printing them...
547 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
549 // Print out the return type and name...
550 printType(F->getReturnType());
551 Out << getValueName(F) << "(";
553 if (!F->isExternal()) {
555 printType(F->afront().getType(), getValueName(F->abegin()));
557 for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
560 printType(I->getType(), getValueName(I));
564 // Loop over the arguments, printing them...
565 for (FunctionType::ParamTypes::const_iterator I =
566 FT->getParamTypes().begin(),
567 E = FT->getParamTypes().end(); I != E; ++I) {
568 if (I != FT->getParamTypes().begin()) Out << ", ";
573 // Finish printing arguments...
574 if (FT->isVarArg()) {
575 if (FT->getParamTypes().size()) Out << ", ";
576 Out << "..."; // Output varargs portion of signature!
582 void CWriter::printFunction(Function *F) {
583 if (F->isExternal()) return;
585 Table.incorporateFunction(F);
587 printFunctionSignature(F);
590 // print local variable information for the function
591 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
592 if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
594 printType((*I)->getType(), getValueName(*I));
598 // print the basic blocks
599 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
600 BasicBlock *Prev = BB->getPrev();
602 // Don't print the label for the basic block if there are no uses, or if the
603 // only terminator use is the precessor basic block's terminator. We have
604 // to scan the use list because PHI nodes use basic blocks too but do not
605 // require a label to be generated.
607 bool NeedsLabel = false;
608 for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
610 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
611 if (TI != Prev->getTerminator()) {
616 if (NeedsLabel) Out << getValueName(BB) << ":\n";
618 // Output all of the instructions in the basic block...
619 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
620 if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
621 if (II->getType() != Type::VoidTy)
630 // Don't emit prefix or suffix for the terminator...
631 visit(*BB->getTerminator());
635 Table.purgeFunction();
638 // Specific Instruction type classes... note that all of the casts are
639 // neccesary because we use the instruction classes as opaque types...
641 void CWriter::visitReturnInst(ReturnInst &I) {
642 // Don't output a void return if this is the last basic block in the function
643 if (I.getNumOperands() == 0 &&
644 &*--I.getParent()->getParent()->end() == I.getParent() &&
645 !I.getParent()->size() == 1) {
650 if (I.getNumOperands()) {
652 writeOperand(I.getOperand(0));
657 static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
658 // If PHI nodes need copies, we need the copy code...
659 if (isa<PHINode>(To->front()) ||
660 From->getNext() != To) // Not directly successor, need goto
663 // Otherwise we don't need the code.
667 void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
669 for (BasicBlock::iterator I = Succ->begin();
670 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
671 // now we have to do the printing
672 Out << string(Indent, ' ');
674 writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
675 Out << "; /* for PHI node */\n";
678 if (CurBB->getNext() != Succ) {
679 Out << string(Indent, ' ') << " goto ";
685 // Brach instruction printing - Avoid printing out a brach to a basic block that
686 // immediately succeeds the current one.
688 void CWriter::visitBranchInst(BranchInst &I) {
689 if (I.isConditional()) {
690 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
692 writeOperand(I.getCondition());
695 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
697 if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
698 Out << " } else {\n";
699 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
702 // First goto not neccesary, assume second one is...
704 writeOperand(I.getCondition());
707 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
712 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
718 void CWriter::visitBinaryOperator(Instruction &I) {
719 // binary instructions, shift instructions, setCond instructions.
720 if (isa<PointerType>(I.getType())) {
722 printType(I.getType());
726 if (isa<PointerType>(I.getType())) Out << "(long long)";
727 writeOperand(I.getOperand(0));
729 switch (I.getOpcode()) {
730 case Instruction::Add: Out << " + "; break;
731 case Instruction::Sub: Out << " - "; break;
732 case Instruction::Mul: Out << "*"; break;
733 case Instruction::Div: Out << "/"; break;
734 case Instruction::Rem: Out << "%"; break;
735 case Instruction::And: Out << " & "; break;
736 case Instruction::Or: Out << " | "; break;
737 case Instruction::Xor: Out << " ^ "; break;
738 case Instruction::SetEQ: Out << " == "; break;
739 case Instruction::SetNE: Out << " != "; break;
740 case Instruction::SetLE: Out << " <= "; break;
741 case Instruction::SetGE: Out << " >= "; break;
742 case Instruction::SetLT: Out << " < "; break;
743 case Instruction::SetGT: Out << " > "; break;
744 case Instruction::Shl : Out << " << "; break;
745 case Instruction::Shr : Out << " >> "; break;
746 default: std::cerr << "Invalid operator type!" << I; abort();
749 if (isa<PointerType>(I.getType())) Out << "(long long)";
750 writeOperand(I.getOperand(1));
753 void CWriter::visitCastInst(CastInst &I) {
755 printType(I.getType());
757 writeOperand(I.getOperand(0));
760 void CWriter::visitCallInst(CallInst &I) {
761 const PointerType *PTy = cast<PointerType>(I.getCalledValue()->getType());
762 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
763 const Type *RetTy = FTy->getReturnType();
765 Out << getValueName(I.getOperand(0)) << "(";
767 if (I.getNumOperands() > 1) {
768 writeOperand(I.getOperand(1));
770 for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
772 writeOperand(I.getOperand(op));
778 void CWriter::visitMallocInst(MallocInst &I) {
780 printType(I.getType());
781 Out << ")malloc(sizeof(";
782 printType(I.getType()->getElementType());
785 if (I.isArrayAllocation()) {
787 writeOperand(I.getOperand(0));
792 void CWriter::visitAllocaInst(AllocaInst &I) {
794 printType(I.getType());
795 Out << ") alloca(sizeof(";
796 printType(I.getType()->getElementType());
798 if (I.isArrayAllocation()) {
800 writeOperand(I.getOperand(0));
805 void CWriter::visitFreeInst(FreeInst &I) {
807 writeOperand(I.getOperand(0));
811 void CWriter::printIndexingExpression(Value *Ptr, User::op_iterator I,
812 User::op_iterator E) {
813 bool HasImplicitAddress = false;
814 // If accessing a global value with no indexing, avoid *(&GV) syndrome
815 if (GlobalValue *V = dyn_cast<GlobalValue>(Ptr)) {
816 HasImplicitAddress = true;
817 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Ptr)) {
818 HasImplicitAddress = true;
819 Ptr = CPR->getValue(); // Get to the global...
823 if (!HasImplicitAddress)
824 Out << "*"; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
826 writeOperandInternal(Ptr);
830 const Constant *CI = dyn_cast<Constant>(I->get());
831 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
834 writeOperandInternal(Ptr);
836 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
839 // Print out the -> operator if possible...
840 if (CI && CI->isNullValue() && I+1 != E) {
841 if ((*(I+1))->getType() == Type::UByteTy) {
842 Out << (HasImplicitAddress ? "." : "->");
843 Out << "field" << cast<ConstantUInt>(*(I+1))->getValue();
845 } else { // Performing array indexing. Just skip the 0
848 } else if (HasImplicitAddress) {
853 if ((*I)->getType() == Type::UIntTy) {
858 Out << ".field" << cast<ConstantUInt>(*I)->getValue();
862 void CWriter::visitLoadInst(LoadInst &I) {
863 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
866 void CWriter::visitStoreInst(StoreInst &I) {
867 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
869 writeOperand(I.getOperand(0));
872 void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
874 printIndexingExpression(I.getPointerOperand(), I.idx_begin(), I.idx_end());
877 //===----------------------------------------------------------------------===//
878 // External Interface declaration
879 //===----------------------------------------------------------------------===//
881 void WriteToC(const Module *M, ostream &Out) {
882 assert(M && "You can't write a null module!!");
883 SlotCalculator SlotTable(M, false);
884 CWriter W(Out, SlotTable, M);