* Clean up how PHI nodes are handled
[oota-llvm.git] / lib / Target / CBackend / CBackend.cpp
1 //===-- Writer.cpp - Library for writing C files --------------------------===//
2 //
3 // This library implements the functionality defined in llvm/Assembly/CWriter.h
4 // and CLocalVars.h
5 //
6 // TODO : Recursive types.
7 //
8 //===-----------------------------------------------------------------------==//
9
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"
29
30 #include <algorithm>
31 #include <strstream>
32 using std::string;
33 using std::map;
34 using std::vector;
35 using std::ostream;
36
37 //===-----------------------------------------------------------------------==//
38 //
39 // Implementation of the CLocalVars methods
40
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);
48   } 
49 }
50
51 static std::string getConstStrValue(const Constant* CPV);
52
53
54 static std::string getConstArrayStrValue(const Constant* CPV) {
55   std::string Result;
56   
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.
59   // 
60   const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
61   bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
62
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) {
67         isString = false;
68         break;
69       }
70   }
71   if (isString) {
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())
75       isString = false;
76   }
77   
78   if (isString) {
79     Result = "\"";
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();
85       
86       if (isprint(C)) {
87         Result += C;
88       } else {
89         switch (C) {
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;
95         default:
96           Result += "\\x";
97           Result += ( C/16  < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
98           Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
99           break;
100         }
101       }
102     }
103     Result += "\"";
104     
105   } else {
106     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     Result += " }";
113   }
114   
115   return Result;
116 }
117
118 static std::string getConstStrValue(const Constant* CPV) {
119   switch (CPV->getType()->getPrimitiveID()) {
120   case Type::BoolTyID:
121     return CPV == ConstantBool::False ? "0" : "1";
122   case Type::SByteTyID:
123   case Type::ShortTyID:
124   case Type::IntTyID:
125     return itostr(cast<ConstantSInt>(CPV)->getValue());
126   case Type::LongTyID:
127     return itostr(cast<ConstantSInt>(CPV)->getValue()) + "ll";
128
129   case Type::UByteTyID:
130     return utostr(cast<ConstantUInt>(CPV)->getValue());
131   case Type::UShortTyID:
132     return utostr(cast<ConstantUInt>(CPV)->getValue());
133   case Type::UIntTyID:
134     return utostr(cast<ConstantUInt>(CPV)->getValue())+"u";
135   case Type::ULongTyID:
136     return utostr(cast<ConstantUInt>(CPV)->getValue())+"ull";
137
138   case Type::FloatTyID:
139   case Type::DoubleTyID:
140     return ftostr(cast<ConstantFP>(CPV)->getValue());
141
142   case Type::ArrayTyID:
143     return getConstArrayStrValue(CPV);
144
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)));
151     }
152     return Result + " }";
153   }
154
155   default:
156     cerr << "Unknown constant type: " << CPV << "\n";
157     abort();
158   }
159 }
160
161 // Pass the Type* variable and and the variable name and this prints out the 
162 // variable declaration.
163 //
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;
181     default :
182       cerr << "Unknown primitive type: " << Ty << "\n";
183       abort();
184     }
185   
186   // Check to see if the type is named.
187   if (!ignoreName) {
188     map<const Type *, string>::iterator I = TypeNames.find(Ty);
189     if (I != TypeNames.end())
190       return I->second + " " + NameSoFar;
191   }  
192
193   string Result;
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;
199     Result += " (";
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())
204         Result += ", ";
205       Result += calcTypeNameVar(*I, TypeNames, "");
206     }
207     if (MTy->isVarArg()) {
208       if (!MTy->getParamTypes().empty()) 
209         Result += ", ";
210       Result += "...";
211     }
212     Result += ")";
213     break;
214   }
215   case Type::StructTyID: {
216     const StructType *STy = cast<const StructType>(Ty);
217     Result = NameSoFar + " {\n";
218     unsigned indx = 0;
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++));
223       Result += ";\n";
224     }
225     Result += "}";
226     break;
227   }  
228
229   case Type::PointerTyID: {
230     Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(), 
231                              TypeNames, "*" + NameSoFar);
232     break;
233   }
234   
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) + "]");
240     break;
241   }
242   default:
243     assert(0 && "Unhandled case in getTypeProps!");
244     Result = "<error>";
245   }
246
247   return Result;
248 }
249
250 namespace {
251   class CWriter {
252     ostream& Out; 
253     SlotCalculator &Table;
254     const Module *TheModule;
255     map<const Type *, string> TypeNames;
256   public:
257     inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
258       : Out(o), Table(Tab), TheModule(M) {
259     }
260     
261     inline void write(Module *M) { printModule(M); }
262
263     ostream& printTypeVar(const Type *Ty, const string &VariableName) {
264       return Out << calcTypeNameVar(Ty, TypeNames, VariableName);
265     }
266
267     ostream& printType(const Type *Ty) {
268       return Out << calcTypeNameVar(Ty, TypeNames, "");
269     }
270
271     void writeOperand(const Value *Operand);
272
273     string getValueName(const Value *V);
274   private :
275
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
281     
282     void printFunction(Function *);
283   };
284   /* END class CWriter */
285
286
287   /* CLASS InstLocalVarsVisitor */
288   class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
289     CWriter& CW;
290     void handleTerminator(TerminatorInst *tI, int indx);
291   public:
292     CLocalVars CLV;
293     
294     InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
295     
296     void visitInstruction(Instruction *I) {
297       if (I->getType() != Type::VoidTy)
298         CLV.addLocalVar(I->getType(), CW.getValueName(I));
299     }
300
301     void visitBranchInst(BranchInst *I) {
302       handleTerminator(I, 0);
303       if (I->isConditional())
304         handleTerminator(I, 1);
305     }
306   };
307 }
308
309 void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
310   BasicBlock *bb = tI->getSuccessor(indx);
311
312   BasicBlock::const_iterator insIt = bb->begin();
313   while (insIt != bb->end()) {
314     if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
315       // Its a phinode!
316       // Calculate the incoming index for this
317       assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
318
319       CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
320     } else
321       break;
322     insIt++;
323   }
324 }
325
326 namespace {
327   /* CLASS CInstPrintVisitor */
328
329   class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
330     CWriter& CW;
331     SlotCalculator& Table;
332     ostream &Out;
333
334     void outputLValue(Instruction *);
335     void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
336                             unsigned Indent);
337     void printIndexingExpr(MemAccessInst *MAI);
338
339   public:
340     CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o) 
341       : CW(cw), Table(table), Out(o) {}
342     
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) {}
357
358     void visitNot(GenericUnaryInst *I);
359     void visitBinaryOperator(Instruction *I);
360   };
361 }
362
363 void CInstPrintVisitor::outputLValue(Instruction *I) {
364   Out << "  " << CW.getValueName(I) << " = ";
365 }
366
367 // Implement all "other" instructions, except for PHINode
368 void CInstPrintVisitor::visitCastInst(CastInst *I) {
369   outputLValue(I);
370   Out << "(";
371   CW.printType(I->getType());
372   Out << ")";
373   CW.writeOperand(I->getOperand(0));
374   Out << ";\n";
375 }
376
377 void CInstPrintVisitor::visitCallInst(CallInst *I) {
378   if (I->getType() != Type::VoidTy)
379     outputLValue(I);
380   else
381     Out << "  ";
382
383   const PointerType  *PTy   = cast<PointerType>(I->getCalledValue()->getType());
384   const FunctionType *FTy   = cast<FunctionType>(PTy->getElementType());
385   const Type         *RetTy = FTy->getReturnType();
386   
387   Out << CW.getValueName(I->getOperand(0)) << "(";
388
389   if (I->getNumOperands() > 1) {
390     CW.writeOperand(I->getOperand(1));
391
392     for (unsigned op = 2, Eop = I->getNumOperands(); op != Eop; ++op) {
393       Out << ", ";
394       CW.writeOperand(I->getOperand(op));
395     }
396   }
397   Out << ");\n";
398
399  
400 // Specific Instruction type classes... note that all of the casts are
401 // neccesary because we use the instruction classes as opaque types...
402 //
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())
407     return;
408
409   Out << "  return";
410   if (I->getNumOperands()) {
411     Out << " ";
412     CW.writeOperand(I->getOperand(0));
413   }
414   Out << ";\n";
415 }
416
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;  
423 }
424
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
429     return true;
430
431   // Otherwise we don't need the code.
432   return false;
433 }
434
435 void CInstPrintVisitor::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
436                                            unsigned Indent) {
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, ' ');
441     outputLValue(PN);
442     CW.writeOperand(PN->getIncomingValue(PN->getBasicBlockIndex(CurBB)));
443     Out << ";   /* for PHI node */\n";
444   }
445
446   if (!BBFollowsBB(CurBB, Succ)) {
447     Out << string(Indent, ' ') << "  goto ";
448     CW.writeOperand(Succ);
449     Out << ";\n";
450   }
451 }
452
453 // Brach instruction printing - Avoid printing out a brach to a basic block that
454 // immediately succeeds the current one.
455 //
456 void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
457   if (I->isConditional()) {
458     if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(0))) {
459       Out << "  if (";
460       CW.writeOperand(I->getCondition());
461       Out << ") {\n";
462       
463       printBranchToBlock(I->getParent(), I->getSuccessor(0), 2);
464       
465       if (isGotoCodeNeccessary(I->getParent(), I->getSuccessor(1))) {
466         Out << "  } else {\n";
467         printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
468       }
469     } else {
470       // First goto not neccesary, assume second one is...
471       Out << "  if (!";
472       CW.writeOperand(I->getCondition());
473       Out << ") {\n";
474
475       printBranchToBlock(I->getParent(), I->getSuccessor(1), 2);
476     }
477
478     Out << "  }\n";
479   } else {
480     printBranchToBlock(I->getParent(), I->getSuccessor(0), 0);
481   }
482   Out << "\n";
483 }
484
485 void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
486   assert(0 && "Switch not implemented!");
487 }
488
489 void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
490   assert(0 && "Invoke not implemented!");
491 }
492
493 void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
494   outputLValue(I);
495   Out << "(";
496   CW.printType(I->getType());
497   Out << ")malloc(sizeof(";
498   CW.printType(I->getType()->getElementType());
499   Out << ")";
500
501   if (I->isArrayAllocation()) {
502     Out << " * " ;
503     CW.writeOperand(I->getOperand(0));
504   }
505   Out << ");\n";
506 }
507
508 void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
509   outputLValue(I);
510   Out << "(";
511   CW.printType(I->getType());
512   Out << ") alloca(sizeof(";
513   CW.printType(I->getType()->getElementType());
514   Out << ")";
515   if (I->isArrayAllocation()) {
516     Out << " * " ;
517     CW.writeOperand(I->getOperand(0));
518   }
519   Out << ");\n";
520 }
521
522 void CInstPrintVisitor::visitFreeInst(FreeInst *I) {
523   Out << "  free(";
524   CW.writeOperand(I->getOperand(0));
525   Out << ");\n";
526 }
527
528 void CInstPrintVisitor::printIndexingExpr(MemAccessInst *MAI) {
529   MemAccessInst::op_iterator I = MAI->idx_begin(), E = MAI->idx_end();
530   if (I == E)
531     Out << "*";  // Implicit zero first argument: '*x' is equivalent to 'x[0]'
532
533   CW.writeOperand(MAI->getPointerOperand());
534
535   if (I == E) return;
536
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) {
541     ++I;
542     Out << "->field" << cast<ConstantUInt>(*I)->getValue();
543     ++I;
544   }
545     
546   for (; I != E; ++I)
547     if ((*I)->getType() == Type::UIntTy) {
548       Out << "[";
549       CW.writeOperand(*I);
550       Out << "]";
551     } else {
552       Out << ".field" << cast<ConstantUInt>(*I)->getValue();
553     }
554 }
555
556 void CInstPrintVisitor::visitLoadInst(LoadInst *I) {
557   outputLValue(I);
558   printIndexingExpr(I);
559   Out << ";\n";
560 }
561
562 void CInstPrintVisitor::visitStoreInst(StoreInst *I) {
563   Out << "  ";
564   printIndexingExpr(I);
565   Out << " = ";
566   CW.writeOperand(I->getOperand(0));
567   Out << ";\n";
568 }
569
570 void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
571   outputLValue(I);
572   Out << "&";
573   printIndexingExpr(I);
574   Out << ";\n";
575 }
576
577 void CInstPrintVisitor::visitNot(GenericUnaryInst *I) {
578   outputLValue(I);
579   Out << "~";
580   CW.writeOperand(I->getOperand(0));
581   Out << ";\n";
582 }
583
584 void CInstPrintVisitor::visitBinaryOperator(Instruction *I) {
585   // binary instructions, shift instructions, setCond instructions.
586   outputLValue(I);
587   if (isa<PointerType>(I->getType())) {
588     Out << "(";
589     CW.printType(I->getType());
590     Out << ")";
591   }
592       
593   if (isa<PointerType>(I->getType())) Out << "(long long)";
594   CW.writeOperand(I->getOperand(0));
595
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();
614   }
615
616   if (isa<PointerType>(I->getType())) Out << "(long long)";
617   CW.writeOperand(I->getOperand(1));
618   Out << ";\n";
619 }
620
621 /* END : CInstPrintVisitor implementation */
622
623 // We dont want identifier names with ., space, -  in them. 
624 // So we replace them with _
625 static string makeNameProper(string x) {
626   string tmp;
627   for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
628     switch (*sI) {
629     case '.': tmp += "d_"; break;
630     case ' ': tmp += "s_"; break;
631     case '-': tmp += "D_"; break;
632     case '_': tmp += "__"; break;
633     default:  tmp += *sI;
634     }
635
636   return tmp;
637 }
638
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());
643
644     return "l" + utostr(V->getType()->getUniqueID()) + "_" +
645            makeNameProper(V->getName());      
646   }
647
648   int Slot = Table.getValSlot(V);
649   assert(Slot >= 0 && "Invalid value!");
650   return "ltmp_" + itostr(Slot) + "_" + utostr(V->getType()->getUniqueID());
651 }
652
653 void CWriter::printModule(Module *M) {
654   // printing stdlib inclusion
655   // Out << "#include <stdlib.h>\n";
656
657   // get declaration for alloca
658   Out << "/* Provide Declarations */\n"
659       << "#include <alloca.h>\n\n"
660
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"
664
665       << "\n\n/* Global Symbols */\n";
666
667   // Loop over the symbol table, emitting all named constants...
668   if (M->hasSymbolTable())
669     printSymbolTable(*M->getSymbolTable());
670
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));
676
677     if (GV->hasInitializer()) {
678       Out << " = " ;
679       writeOperand(GV->getInitializer());
680     }
681     Out << ";\n";
682   }
683
684   // First output all the declarations of the functions as C requires Functions 
685   // be declared before they are used.
686   //
687   Out << "\n\n/* Function Declarations */\n";
688   for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
689   
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));
693 }
694
695
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);
703     
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";
708
709         TypeNames.insert(std::make_pair(Ty, Name));
710       }
711   }
712
713   Out << "\n";
714
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);
718     
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;
725         else
726           Out << "typedef ";
727
728         Out << calcTypeNameVar(Ty, TypeNames, Name, true) << ";\n";
729       }
730     }
731   }
732 }
733
734
735 // printFunctionDecl - Print function declaration
736 //
737 void CWriter::printFunctionDecl(const Function *F) {
738   printFunctionSignature(F);
739   Out << ";\n";
740 }
741
742 void CWriter::printFunctionSignature(const Function *F) {
743   if (F->hasInternalLinkage()) Out << "static ";
744   
745   // Loop over the arguments, printing them...
746   const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
747   
748   // Print out the return type and name...
749   printType(F->getReturnType());
750   Out << getValueName(F) << "(";
751     
752   if (!F->isExternal()) {
753     if (!F->getArgumentList().empty()) {
754       printTypeVar(F->getArgumentList().front()->getType(),
755                    getValueName(F->getArgumentList().front()));
756
757       for (Function::ArgumentListType::const_iterator
758              I = F->getArgumentList().begin()+1,
759              E = F->getArgumentList().end(); I != E; ++I) {
760         Out << ", ";
761         printTypeVar((*I)->getType(), getValueName(*I));
762       }
763     }
764   } else {
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 << ", ";
770       printType(*I);
771     }
772   }
773
774   // Finish printing arguments...
775   if (FT->isVarArg()) {
776     if (FT->getParamTypes().size()) Out << ", ";
777     Out << "...";  // Output varargs portion of signature!
778   }
779   Out << ")";
780 }
781
782
783 void CWriter::printFunction(Function *F) {
784   if (F->isExternal()) return;
785
786   Table.incorporateFunction(F);
787
788   printFunctionSignature(F);
789   Out << " {\n";
790
791   // Process each of the basic blocks, gather information and call the  
792   // output methods on the CLocalVars and Function* objects.
793     
794   // gather local variable information for each basic block
795   InstLocalVarsVisitor ILV(*this);
796   ILV.visit(F);
797
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;
801   
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(); 
806          ++listiter) {
807       Out << "  ";
808       printTypeVar(iter->first, *listiter);
809       Out << ";\n";
810     }
811   }
812  
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;
816
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.
821     //
822     bool NeedsLabel = false;
823     for (Value::use_iterator UI = BB->use_begin(), UE = BB->use_end();
824          UI != UE; ++UI)
825       if (TerminatorInst *TI = dyn_cast<TerminatorInst>(*UI))
826         if (TI != Prev->getTerminator()) {
827           NeedsLabel = true;
828           break;        
829         }
830
831     if (NeedsLabel) Out << getValueName(BB) << ":\n";
832
833     // Output all of the instructions in the basic block...
834     // print the basic blocks
835     CInstPrintVisitor CIPV(*this, Table, Out);
836     CIPV.visit(BB);
837   }
838   
839   Out << "}\n\n";
840   Table.purgeFunction();
841 }
842
843 void CWriter::writeOperand(const Value *Operand) {
844   if (isa<GlobalVariable>(Operand))
845     Out << "(&";  // Global variables are references as their addresses by llvm
846
847   if (Operand->hasName()) {   
848     Out << getValueName(Operand);
849   } else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
850     if (isa<ConstantPointerNull>(CPV)) {
851       Out << "((";
852       printTypeVar(CPV->getType(), "");
853       Out << ")NULL)";
854     } else
855       Out << getConstStrValue(CPV); 
856   } else {
857     int Slot = Table.getValSlot(Operand);
858     assert(Slot >= 0 && "Malformed LLVM!");
859     Out << "ltmp_" << Slot << "_" << Operand->getType()->getUniqueID();
860   }
861
862   if (isa<GlobalVariable>(Operand))
863     Out << ")";
864 }
865
866
867 //===----------------------------------------------------------------------===//
868 //                       External Interface declaration
869 //===----------------------------------------------------------------------===//
870
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);
875   W.write((Module*)M);
876   Out.flush();
877 }