changes to make it compatible with 64bit gcc
[oota-llvm.git] / lib / Target / CBackend / Writer.cpp
1 //===-- Writer.cpp - Library for converting LLVM code to C ----------------===//
2 //
3 // This library implements the functionality defined in llvm/Assembly/CWriter.h
4 //
5 // TODO : Recursive types.
6 //
7 //===-----------------------------------------------------------------------==//
8
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"
24 #include <algorithm>
25 #include <set>
26 using std::string;
27 using std::map;
28 using std::ostream;
29 using std::cerr;
30
31 static std::string getConstStrValue(const Constant* CPV);
32
33
34 static std::string getConstArrayStrValue(const Constant* CPV) {
35   std::string Result;
36   
37   // As a special case, print the array as a string if it is an array of
38   // ubytes or an array of sbytes with positive values.
39   // 
40   const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
41   bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
42
43   // Make sure the last character is a null char, as automatically added by C
44   if (CPV->getNumOperands() == 0 ||
45       !cast<Constant>(*(CPV->op_end()-1))->isNullValue())
46     isString = false;
47   
48   if (isString) {
49     Result = "\"";
50     // Do not include the last character, which we know is null
51     for (unsigned i = 0, e = CPV->getNumOperands()-1; i != e; ++i) {
52       unsigned char C = (ETy == Type::SByteTy) ?
53         (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
54         (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
55       
56       if (isprint(C)) {
57         Result += C;
58       } else {
59         switch (C) {
60         case '\n': Result += "\\n"; break;
61         case '\t': Result += "\\t"; break;
62         case '\r': Result += "\\r"; break;
63         case '\v': Result += "\\v"; break;
64         case '\a': Result += "\\a"; break;
65         default:
66           Result += "\\x";
67           Result += ( C/16  < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
68           Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
69           break;
70         }
71       }
72     }
73     Result += "\"";
74   } else {
75     Result = "{";
76     if (CPV->getNumOperands()) {
77       Result += " " +  getConstStrValue(cast<Constant>(CPV->getOperand(0)));
78       for (unsigned i = 1; i < CPV->getNumOperands(); i++)
79         Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
80     }
81     Result += " }";
82   }
83   
84   return Result;
85 }
86
87 static std::string getConstStrValue(const Constant* CPV) {
88   switch (CPV->getType()->getPrimitiveID()) {
89   case Type::BoolTyID:  return CPV == ConstantBool::False ? "0" : "1";
90   case Type::SByteTyID:
91   case Type::ShortTyID:
92   case Type::IntTyID:   return itostr(cast<ConstantSInt>(CPV)->getValue());
93   case Type::LongTyID:  return itostr(cast<ConstantSInt>(CPV)->getValue())+"ll";
94
95   case Type::UByteTyID:
96   case Type::UShortTyID:return utostr(cast<ConstantUInt>(CPV)->getValue());
97   case Type::UIntTyID:  return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
98   case Type::ULongTyID:return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
99
100   case Type::FloatTyID:
101   case Type::DoubleTyID: return ftostr(cast<ConstantFP>(CPV)->getValue());
102
103   case Type::ArrayTyID:  return getConstArrayStrValue(CPV);
104
105   case Type::StructTyID: {
106     std::string Result = "{";
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)));
111     }
112     return Result + " }";
113   }
114
115   default:
116     cerr << "Unknown constant type: " << CPV << "\n";
117     abort();
118   }
119 }
120
121 // Pass the Type* variable and and the variable name and this prints out the 
122 // variable declaration.
123 //
124 static string calcTypeNameVar(const Type *Ty,
125                               map<const Type *, string> &TypeNames, 
126                               const string &NameSoFar, bool ignoreName = false){
127   if (Ty->isPrimitiveType())
128     switch (Ty->getPrimitiveID()) {
129     case Type::VoidTyID:   return "void " + NameSoFar;
130     case Type::BoolTyID:   return "bool " + NameSoFar;
131     case Type::UByteTyID:  return "unsigned char " + NameSoFar;
132     case Type::SByteTyID:  return "signed char " + NameSoFar;
133     case Type::UShortTyID: return "unsigned short " + NameSoFar;
134     case Type::ShortTyID:  return "short " + NameSoFar;
135     case Type::UIntTyID:   return "unsigned " + NameSoFar;
136     case Type::IntTyID:    return "int " + NameSoFar;
137     case Type::ULongTyID:  return "unsigned long long " + NameSoFar;
138     case Type::LongTyID:   return "signed long long " + NameSoFar;
139     case Type::FloatTyID:  return "float " + NameSoFar;
140     case Type::DoubleTyID: return "double " + NameSoFar;
141     default :
142       cerr << "Unknown primitive type: " << Ty << "\n";
143       abort();
144     }
145   
146   // Check to see if the type is named.
147   if (!ignoreName) {
148     map<const Type *, string>::iterator I = TypeNames.find(Ty);
149     if (I != TypeNames.end())
150       return I->second + " " + NameSoFar;
151   }  
152
153   string Result;
154   switch (Ty->getPrimitiveID()) {
155   case Type::FunctionTyID: {
156     const FunctionType *MTy = cast<FunctionType>(Ty);
157     Result += calcTypeNameVar(MTy->getReturnType(), TypeNames, "");
158     Result += " " + NameSoFar + " (";
159     for (FunctionType::ParamTypes::const_iterator
160            I = MTy->getParamTypes().begin(),
161            E = MTy->getParamTypes().end(); I != E; ++I) {
162       if (I != MTy->getParamTypes().begin())
163         Result += ", ";
164       Result += calcTypeNameVar(*I, TypeNames, "");
165     }
166     if (MTy->isVarArg()) {
167       if (!MTy->getParamTypes().empty()) 
168         Result += ", ";
169       Result += "...";
170     }
171     return Result + ")";
172   }
173   case Type::StructTyID: {
174     const StructType *STy = cast<const StructType>(Ty);
175     Result = NameSoFar + " {\n";
176     unsigned indx = 0;
177     for (StructType::ElementTypes::const_iterator
178            I = STy->getElementTypes().begin(),
179            E = STy->getElementTypes().end(); I != E; ++I) {
180       Result += "  " +calcTypeNameVar(*I, TypeNames, "field" + utostr(indx++));
181       Result += ";\n";
182     }
183     return Result + "}";
184   }  
185
186   case Type::PointerTyID:
187     return calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(), 
188                            TypeNames, "*" + NameSoFar);
189   
190   case Type::ArrayTyID: {
191     const ArrayType *ATy = cast<const ArrayType>(Ty);
192     int NumElements = ATy->getNumElements();
193     return calcTypeNameVar(ATy->getElementType(), TypeNames, 
194                            NameSoFar + "[" + itostr(NumElements) + "]");
195   }
196   default:
197     assert(0 && "Unhandled case in getTypeProps!");
198     abort();
199   }
200
201   return Result;
202 }
203
204 namespace {
205   class CWriter : public InstVisitor<CWriter> {
206     ostream& Out; 
207     SlotCalculator &Table;
208     const Module *TheModule;
209     map<const Type *, string> TypeNames;
210     std::set<const Value*> MangledGlobals;
211   public:
212     inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
213       : Out(o), Table(Tab), TheModule(M) {
214     }
215     
216     inline void write(Module *M) { printModule(M); }
217
218     ostream& printType(const Type *Ty, const string &VariableName = "") {
219       return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
220     }
221
222     void writeOperand(Value *Operand);
223     void writeOperandInternal(Value *Operand);
224
225     string getValueName(const Value *V);
226
227   private :
228     void printModule(Module *M);
229     void printSymbolTable(const SymbolTable &ST);
230     void printGlobal(const GlobalVariable *GV);
231     void printFunctionSignature(const Function *F);
232     void printFunctionDecl(const Function *F); // Print just the forward decl
233     
234     void printFunction(Function *);
235
236     // isInlinableInst - Attempt to inline instructions into their uses to build
237     // trees as much as possible.  To do this, we have to consistently decide
238     // what is acceptable to inline, so that variable declarations don't get
239     // printed and an extra copy of the expr is not emitted.
240     //
241     static bool isInlinableInst(const Instruction &I) {
242       // Must be an expression, must be used exactly once.  If it is dead, we
243       // emit it inline where it would go.
244       if (I.getType() == Type::VoidTy || I.use_size() != 1 ||
245           isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I))
246         return false;
247
248       // Only inline instruction it it's use is in the same BB as the inst.
249       return I.getParent() == cast<Instruction>(I.use_back())->getParent();
250     }
251
252     // Instruction visitation functions
253     friend class InstVisitor<CWriter>;
254
255     void visitReturnInst(ReturnInst &I);
256     void visitBranchInst(BranchInst &I);
257
258     void visitPHINode(PHINode &I) {}
259     void visitNot(GenericUnaryInst &I);
260     void visitBinaryOperator(Instruction &I);
261
262     void visitCastInst (CastInst &I);
263     void visitCallInst (CallInst &I);
264     void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
265
266     void visitMallocInst(MallocInst &I);
267     void visitAllocaInst(AllocaInst &I);
268     void visitFreeInst  (FreeInst   &I);
269     void visitLoadInst  (LoadInst   &I);
270     void visitStoreInst (StoreInst  &I);
271     void visitGetElementPtrInst(GetElementPtrInst &I);
272
273     void visitInstruction(Instruction &I) {
274       cerr << "C Writer does not know about " << I;
275       abort();
276     }
277
278     void outputLValue(Instruction *I) {
279       Out << "  " << getValueName(I) << " = ";
280     }
281     void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
282                             unsigned Indent);
283     void printIndexingExpr(MemAccessInst &MAI);
284   };
285 }
286
287 // We dont want identifier names with ., space, -  in them. 
288 // So we replace them with _
289 static string makeNameProper(string x) {
290   string tmp;
291   for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
292     switch (*sI) {
293     case '.': tmp += "d_"; break;
294     case ' ': tmp += "s_"; break;
295     case '-': tmp += "D_"; break;
296     default:  tmp += *sI;
297     }
298
299   return tmp;
300 }
301
302 string CWriter::getValueName(const Value *V) {
303   if (V->hasName()) {              // Print out the label if it exists...
304     if (isa<GlobalValue>(V) &&     // Do not mangle globals...
305         cast<GlobalValue>(V)->hasExternalLinkage() && // Unless it's internal or
306         !MangledGlobals.count(V))  // Unless the name would collide if we don't
307       return makeNameProper(V->getName());
308
309     return "l" + utostr(V->getType()->getUniqueID()) + "_" +
310            makeNameProper(V->getName());      
311   }
312
313   int Slot = Table.getValSlot(V);
314   assert(Slot >= 0 && "Invalid value!");
315   return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
316 }
317
318 void CWriter::writeOperandInternal(Value *Operand) {
319   if (Operand->hasName()) {   
320     Out << getValueName(Operand);
321   } else if (Constant *CPV = dyn_cast<Constant>(Operand)) {
322     if (isa<ConstantPointerNull>(CPV)) {
323       Out << "((";
324       printType(CPV->getType(), "");
325       Out << ")NULL)";
326     } else
327       Out << getConstStrValue(CPV); 
328   } else {
329     int Slot = Table.getValSlot(Operand);
330     assert(Slot >= 0 && "Malformed LLVM!");
331     Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
332   }
333 }
334
335 void CWriter::writeOperand(Value *Operand) {
336   if (Instruction *I = dyn_cast<Instruction>(Operand))
337     if (isInlinableInst(*I)) {
338       // Should we inline this instruction to build a tree?
339       Out << "(";
340       visit(*I);
341       Out << ")";    
342       return;
343     }
344
345   if (isa<GlobalVariable>(Operand))
346     Out << "(&";  // Global variables are references as their addresses by llvm
347
348   writeOperandInternal(Operand);
349
350   if (isa<GlobalVariable>(Operand))
351     Out << ")";
352 }
353
354 void CWriter::printModule(Module *M) {
355   // Calculate which global values have names that will collide when we throw
356   // away type information.
357   {  // Scope to delete the FoundNames set when we are done with it...
358     std::set<string> FoundNames;
359     for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
360       if (I->hasName())                      // If the global has a name...
361         if (FoundNames.count(I->getName()))  // And the name is already used
362           MangledGlobals.insert(I);          // Mangle the name
363         else
364           FoundNames.insert(I->getName());   // Otherwise, keep track of name
365
366     for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
367       if (I->hasName())                      // If the global has a name...
368         if (FoundNames.count(I->getName()))  // And the name is already used
369           MangledGlobals.insert(I);          // Mangle the name
370         else
371           FoundNames.insert(I->getName());   // Otherwise, keep track of name
372   }
373
374
375   // printing stdlib inclusion
376   // Out << "#include <stdlib.h>\n";
377
378   // get declaration for alloca
379   Out << "/* Provide Declarations */\n"
380       << "#include <malloc.h>\n"
381       << "#include <alloca.h>\n\n"
382
383     // Provide a definition for null if one does not already exist.
384       << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
385       << "typedef unsigned char bool;\n"
386
387       << "\n\n/* Global Symbols */\n";
388
389   // Loop over the symbol table, emitting all named constants...
390   if (M->hasSymbolTable())
391     printSymbolTable(*M->getSymbolTable());
392
393   Out << "\n\n/* Global Data */\n";
394   for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I) {
395     if (I->hasInternalLinkage()) Out << "static ";
396     printType(I->getType()->getElementType(), getValueName(I));
397
398     if (I->hasInitializer()) {
399       Out << " = " ;
400       writeOperand(I->getInitializer());
401     }
402     Out << ";\n";
403   }
404
405   // First output all the declarations of the functions as C requires Functions 
406   // be declared before they are used.
407   //
408   Out << "\n\n/* Function Declarations */\n";
409   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
410     printFunctionDecl(I);
411   
412   // Output all of the functions...
413   Out << "\n\n/* Function Bodies */\n";
414   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
415     printFunction(I);
416 }
417
418
419 // printSymbolTable - Run through symbol table looking for named constants
420 // if a named constant is found, emit it's declaration...
421 // Assuming that symbol table has only types and constants.
422 void CWriter::printSymbolTable(const SymbolTable &ST) {
423   for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
424     SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
425     SymbolTable::type_const_iterator End = ST.type_end(TI->first);
426     
427     for (; I != End; ++I)
428       if (const Type *Ty = dyn_cast<StructType>(I->second)) {
429         string Name = "struct l_" + makeNameProper(I->first);
430         Out << Name << ";\n";
431
432         TypeNames.insert(std::make_pair(Ty, Name));
433       }
434   }
435
436   Out << "\n";
437
438   for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
439     SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
440     SymbolTable::type_const_iterator End = ST.type_end(TI->first);
441     
442     for (; I != End; ++I) {
443       const Value *V = I->second;
444       if (const Type *Ty = dyn_cast<Type>(V)) {
445         string Name = "l_" + makeNameProper(I->first);
446         if (isa<StructType>(Ty))
447           Name = "struct " + makeNameProper(Name);
448         else
449           Out << "typedef ";
450
451         Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
452       }
453     }
454   }
455 }
456
457
458 // printFunctionDecl - Print function declaration
459 //
460 void CWriter::printFunctionDecl(const Function *F) {
461   printFunctionSignature(F);
462   Out << ";\n";
463 }
464
465 void CWriter::printFunctionSignature(const Function *F) {
466   if (F->hasInternalLinkage()) Out << "static ";
467   
468   // Loop over the arguments, printing them...
469   const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
470   
471   // Print out the return type and name...
472   printType(F->getReturnType());
473   Out << getValueName(F) << "(";
474     
475   if (!F->isExternal()) {
476     if (!F->aempty()) {
477       printType(F->afront().getType(), getValueName(F->abegin()));
478
479       for (Function::const_aiterator I = ++F->abegin(), E = F->aend();
480            I != E; ++I) {
481         Out << ", ";
482         printType(I->getType(), getValueName(I));
483       }
484     }
485   } else {
486     // Loop over the arguments, printing them...
487     for (FunctionType::ParamTypes::const_iterator I = 
488            FT->getParamTypes().begin(),
489            E = FT->getParamTypes().end(); I != E; ++I) {
490       if (I != FT->getParamTypes().begin()) Out << ", ";
491       printType(*I);
492     }
493   }
494
495   // Finish printing arguments...
496   if (FT->isVarArg()) {
497     if (FT->getParamTypes().size()) Out << ", ";
498     Out << "...";  // Output varargs portion of signature!
499   }
500   Out << ")";
501 }
502
503
504 void CWriter::printFunction(Function *F) {
505   if (F->isExternal()) return;
506
507   Table.incorporateFunction(F);
508
509   printFunctionSignature(F);
510   Out << " {\n";
511
512   // print local variable information for the function
513   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
514     if ((*I)->getType() != Type::VoidTy && !isInlinableInst(**I)) {
515       Out << "  ";
516       printType((*I)->getType(), getValueName(*I));
517       Out << ";\n";
518     }
519  
520   // print the basic blocks
521   for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
522     BasicBlock *Prev = BB->getPrev();
523
524     // Don't print the label for the basic block if there are no uses, or if the
525     // only terminator use is the precessor basic block's terminator.  We have
526     // to scan the use list because PHI nodes use basic blocks too but do not
527     // require a label to be generated.
528     //
529     bool NeedsLabel = false;
530     for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
531          UI != UE; ++UI)
532       if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
533         if (TI != Prev->getTerminator()) {
534           NeedsLabel = true;
535           break;        
536         }
537
538     if (NeedsLabel) Out << getValueName(BB) << ":\n";
539
540     // Output all of the instructions in the basic block...
541     for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II){
542       if (!isInlinableInst(*II) && !isa<PHINode>(*II)) {
543         if (II->getType() != Type::VoidTy)
544           outputLValue(II);
545         else
546           Out << "  ";
547         visit(*II);
548         Out << ";\n";
549       }
550     }
551
552     // Don't emit prefix or suffix for the terminator...
553     visit(*BB->getTerminator());
554   }
555   
556   Out << "}\n\n";
557   Table.purgeFunction();
558 }
559
560 // Specific Instruction type classes... note that all of the casts are
561 // neccesary because we use the instruction classes as opaque types...
562 //
563 void CWriter::visitReturnInst(ReturnInst &I) {
564   // Don't output a void return if this is the last basic block in the function
565   if (I.getNumOperands() == 0 && 
566       &*--I.getParent()->getParent()->end() == I.getParent() &&
567       !I.getParent()->size() == 1) {
568     return;
569   }
570
571   Out << "  return";
572   if (I.getNumOperands()) {
573     Out << " ";
574     writeOperand(I.getOperand(0));
575   }
576   Out << ";\n";
577 }
578
579 static bool isGotoCodeNeccessary(BasicBlock *From, BasicBlock *To) {
580   // If PHI nodes need copies, we need the copy code...
581   if (isa<PHINode>(To->front()) ||
582       From->getNext() != To)      // Not directly successor, need goto
583     return true;
584
585   // Otherwise we don't need the code.
586   return false;
587 }
588
589 void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
590                                            unsigned Indent) {
591   for (BasicBlock::iterator I = Succ->begin();
592        PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
593     //  now we have to do the printing
594     Out << string(Indent, ' ');
595     outputLValue(PN);
596     writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
597     Out << ";   /* for PHI node */\n";
598   }
599
600   if (CurBB->getNext() != Succ) {
601     Out << string(Indent, ' ') << "  goto ";
602     writeOperand(Succ);
603     Out << ";\n";
604   }
605 }
606
607 // Brach instruction printing - Avoid printing out a brach to a basic block that
608 // immediately succeeds the current one.
609 //
610 void CWriter::visitBranchInst(BranchInst &I) {
611   if (I.isConditional()) {
612     if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(0))) {
613       Out << "  if (";
614       writeOperand(I.getCondition());
615       Out << ") {\n";
616       
617       printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
618       
619       if (isGotoCodeNeccessary(I.getParent(), I.getSuccessor(1))) {
620         Out << "  } else {\n";
621         printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
622       }
623     } else {
624       // First goto not neccesary, assume second one is...
625       Out << "  if (!";
626       writeOperand(I.getCondition());
627       Out << ") {\n";
628
629       printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
630     }
631
632     Out << "  }\n";
633   } else {
634     printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
635   }
636   Out << "\n";
637 }
638
639
640 void CWriter::visitNot(GenericUnaryInst &I) {
641   Out << "~";
642   writeOperand(I.getOperand(0));
643 }
644
645 void CWriter::visitBinaryOperator(Instruction &I) {
646   // binary instructions, shift instructions, setCond instructions.
647   if (isa<PointerType>(I.getType())) {
648     Out << "(";
649     printType(I.getType());
650     Out << ")";
651   }
652       
653   if (isa<PointerType>(I.getType())) Out << "(long long)";
654   writeOperand(I.getOperand(0));
655
656   switch (I.getOpcode()) {
657   case Instruction::Add: Out << " + "; break;
658   case Instruction::Sub: Out << " - "; break;
659   case Instruction::Mul: Out << "*"; break;
660   case Instruction::Div: Out << "/"; break;
661   case Instruction::Rem: Out << "%"; break;
662   case Instruction::And: Out << " & "; break;
663   case Instruction::Or: Out << " | "; break;
664   case Instruction::Xor: Out << " ^ "; break;
665   case Instruction::SetEQ: Out << " == "; break;
666   case Instruction::SetNE: Out << " != "; break;
667   case Instruction::SetLE: Out << " <= "; break;
668   case Instruction::SetGE: Out << " >= "; break;
669   case Instruction::SetLT: Out << " < "; break;
670   case Instruction::SetGT: Out << " > "; break;
671   case Instruction::Shl : Out << " << "; break;
672   case Instruction::Shr : Out << " >> "; break;
673   default: cerr << "Invalid operator type!" << I; abort();
674   }
675
676   if (isa<PointerType>(I.getType())) Out << "(long long)";
677   writeOperand(I.getOperand(1));
678 }
679
680 void CWriter::visitCastInst(CastInst &I) {
681   Out << "(";
682   printType(I.getType());
683   Out << ")";
684   writeOperand(I.getOperand(0));
685 }
686
687 void CWriter::visitCallInst(CallInst &I) {
688   const PointerType  *PTy   = cast<PointerType>(I.getCalledValue()->getType());
689   const FunctionType *FTy   = cast<FunctionType>(PTy->getElementType());
690   const Type         *RetTy = FTy->getReturnType();
691   
692   Out << getValueName(I.getOperand(0)) << "(";
693
694   if (I.getNumOperands() > 1) {
695     writeOperand(I.getOperand(1));
696
697     for (unsigned op = 2, Eop = I.getNumOperands(); op != Eop; ++op) {
698       Out << ", ";
699       writeOperand(I.getOperand(op));
700     }
701   }
702   Out << ")";
703 }  
704
705 void CWriter::visitMallocInst(MallocInst &I) {
706   Out << "(";
707   printType(I.getType());
708   Out << ")malloc(sizeof(";
709   printType(I.getType()->getElementType());
710   Out << ")";
711
712   if (I.isArrayAllocation()) {
713     Out << " * " ;
714     writeOperand(I.getOperand(0));
715   }
716   Out << ")";
717 }
718
719 void CWriter::visitAllocaInst(AllocaInst &I) {
720   Out << "(";
721   printType(I.getType());
722   Out << ") alloca(sizeof(";
723   printType(I.getType()->getElementType());
724   Out << ")";
725   if (I.isArrayAllocation()) {
726     Out << " * " ;
727     writeOperand(I.getOperand(0));
728   }
729   Out << ")";
730 }
731
732 void CWriter::visitFreeInst(FreeInst &I) {
733   Out << "free(";
734   writeOperand(I.getOperand(0));
735   Out << ")";
736 }
737
738 void CWriter::printIndexingExpr(MemAccessInst &MAI) {
739   MemAccessInst::op_iterator I = MAI.idx_begin(), E = MAI.idx_end();
740   if (I == E) {
741     // If accessing a global value with no indexing, avoid *(&GV) syndrome
742     if (GlobalValue *V = dyn_cast<GlobalValue>(MAI.getPointerOperand())) {
743       writeOperandInternal(V);
744       return;
745     }
746
747     Out << "*";  // Implicit zero first argument: '*x' is equivalent to 'x[0]'
748   }
749
750   writeOperand(MAI.getPointerOperand());
751
752   if (I == E) return;
753
754   // Print out the -> operator if possible...
755   const Constant *CI = dyn_cast<Constant>(I->get());
756   if (CI && CI->isNullValue() && I+1 != E &&
757       (*(I+1))->getType() == Type::UByteTy) {
758     Out << "->field" << cast<ConstantUInt>(*(I+1))->getValue();
759     I += 2;
760   }
761     
762   for (; I != E; ++I)
763     if ((*I)->getType() == Type::UIntTy) {
764       Out << "[";
765       writeOperand(*I);
766       Out << "]";
767     } else {
768       Out << ".field" << cast<ConstantUInt>(*I)->getValue();
769     }
770 }
771
772 void CWriter::visitLoadInst(LoadInst &I) {
773   printIndexingExpr(I);
774 }
775
776 void CWriter::visitStoreInst(StoreInst &I) {
777   printIndexingExpr(I);
778   Out << " = ";
779   writeOperand(I.getOperand(0));
780 }
781
782 void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
783   Out << "&";
784   printIndexingExpr(I);
785 }
786
787 //===----------------------------------------------------------------------===//
788 //                       External Interface declaration
789 //===----------------------------------------------------------------------===//
790
791 void WriteToC(const Module *M, ostream &Out) {
792   assert(M && "You can't write a null module!!");
793   SlotCalculator SlotTable(M, false);
794   CWriter W(Out, SlotTable, M);
795   W.write((Module*)M);
796   Out.flush();
797 }