Factor out the code to print a value to ONE place.
[oota-llvm.git] / lib / Target / CBackend / Writer.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/Module.h"
14 #include "llvm/Argument.h"
15 #include "llvm/Function.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Constants.h"
18 #include "llvm/GlobalVariable.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/SymbolTable.h"
25 #include "llvm/Support/InstVisitor.h"
26 #include "Support/StringExtras.h"
27 #include "Support/STLExtras.h"
28
29 #include <algorithm>
30 #include <strstream>
31 using std::string;
32 using std::map;
33 using std::vector;
34 using std::ostream;
35
36 //===-----------------------------------------------------------------------==//
37 //
38 // Implementation of the CLocalVars methods
39
40 // Appends a variable to the LocalVars map if it does not already exist
41 // Also check that the type exists on the map.
42 void CLocalVars::addLocalVar(const Type *t, const string & var) {
43   if (!LocalVars.count(t) || 
44       find(LocalVars[t].begin(), LocalVars[t].end(), var) 
45       == LocalVars[t].end()) {
46       LocalVars[t].push_back(var);
47   } 
48 }
49
50 static string calcTypeNameVar(const Type *Ty,
51                               map<const Type *, string> &TypeNames, 
52                               string VariableName, string NameSoFar);
53
54 static std::string getConstStrValue(const Constant* CPV);
55
56
57 //
58 //Getting opcodes in terms of the operator
59 //
60 static const char *getOpcodeOperName(const Instruction *I) {
61   switch (I->getOpcode()) {
62   // Standard binary operators...
63   case Instruction::Add: return "+";
64   case Instruction::Sub: return "-";
65   case Instruction::Mul: return "*";
66   case Instruction::Div: return "/";
67   case Instruction::Rem: return "%";
68     
69     // Logical operators...
70   case Instruction::And: return "&";
71   case Instruction::Or: return "|";
72   case Instruction::Xor: return "^";
73     
74     // SetCond operators...
75   case Instruction::SetEQ: return "==";
76   case Instruction::SetNE: return "!=";
77   case Instruction::SetLE: return "<=";
78   case Instruction::SetGE: return ">=";
79   case Instruction::SetLT: return "<";
80   case Instruction::SetGT: return ">";
81     
82     //ShiftInstruction...
83     
84   case Instruction::Shl : return "<<";
85   case Instruction::Shr : return ">>";
86     
87   default:
88     cerr << "Invalid operator type!" << I->getOpcode() << "\n";
89     abort();
90   }
91   return 0;
92 }
93
94
95 // We dont want identifier names with ., space, -  in them. 
96 // So we replace them with _
97 static string makeNameProper(string x) {
98   string tmp;
99   for (string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++) {
100     if (*sI == '.')
101       tmp += '_';
102     else if (*sI == ' ')
103       tmp += '_';
104     else if (*sI == '-')
105       tmp += "__";
106     else
107       tmp += *sI;
108   }
109   return tmp;
110 }
111
112 static string getConstantName(const Constant *CPV) {
113   return CPV->getName();
114 }
115
116
117 static std::string getConstArrayStrValue(const Constant* CPV) {
118   std::string Result;
119   
120   // As a special case, print the array as a string if it is an array of
121   // ubytes or an array of sbytes with positive values.
122   // 
123   const Type *ETy = cast<ArrayType>(CPV->getType())->getElementType();
124   bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
125
126   if (ETy == Type::SByteTy) {
127     for (unsigned i = 0; i < CPV->getNumOperands(); ++i)
128       if (ETy == Type::SByteTy &&
129           cast<ConstantSInt>(CPV->getOperand(i))->getValue() < 0) {
130         isString = false;
131         break;
132       }
133   }
134   
135   if (isString) {
136     Result = "\"";
137     for (unsigned i = 0; i < CPV->getNumOperands(); ++i) {
138       unsigned char C = (ETy == Type::SByteTy) ?
139         (unsigned char)cast<ConstantSInt>(CPV->getOperand(i))->getValue() :
140         (unsigned char)cast<ConstantUInt>(CPV->getOperand(i))->getValue();
141       
142       if (isprint(C)) {
143         Result += C;
144       } else {
145         Result += "\\x";
146         Result += ( C/16  < 10) ? ( C/16 +'0') : ( C/16 -10+'A');
147         Result += ((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A');
148       }
149     }
150     Result += "\"";
151     
152   } else {
153     Result = "{";
154     if (CPV->getNumOperands()) {
155       Result += " " +  getConstStrValue(cast<Constant>(CPV->getOperand(0)));
156       for (unsigned i = 1; i < CPV->getNumOperands(); i++)
157         Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
158     }
159     Result += " }";
160   }
161   
162   return Result;
163 }
164
165 static std::string getConstStructStrValue(const Constant* CPV) {
166   std::string Result = "{";
167   if (CPV->getNumOperands()) {
168     Result += " " + getConstStrValue(cast<Constant>(CPV->getOperand(0)));
169     for (unsigned i = 1; i < CPV->getNumOperands(); i++)
170       Result += ", " + getConstStrValue(cast<Constant>(CPV->getOperand(i)));
171   }
172
173   return Result + " }";
174 }
175
176 // our own getStrValue function for constant initializers
177 static std::string getConstStrValue(const Constant* CPV) {
178   // Does not handle null pointers, that needs to be checked explicitly
179   string tempstr;
180   if (CPV == ConstantBool::False)
181     return "0";
182   else if (CPV == ConstantBool::True)
183     return "1";
184   
185   else if (isa<ConstantArray>(CPV)) {
186     tempstr = getConstArrayStrValue(CPV);
187   }
188   else  if (isa<ConstantStruct>(CPV)) {
189     tempstr = getConstStructStrValue(CPV);
190   }
191   else if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CPV)) {
192     tempstr = utostr(CUI->getValue());
193   } 
194   else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CPV)) {
195     tempstr = itostr(CSI->getValue());
196   }
197   else if (ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
198     tempstr = ftostr(CFP->getValue());
199   }
200   
201   if (CPV->getType() == Type::ULongTy)
202     tempstr += "ull";
203   else if (CPV->getType() == Type::LongTy)
204     tempstr += "ll";
205   else if (CPV->getType() == Type::UIntTy ||
206            CPV->getType() == Type::UShortTy)
207     tempstr += "u";
208   
209   return tempstr;
210
211 }
212
213 // Internal function
214 // Essentially pass the Type* variable, an empty typestack and this prints 
215 // out the C type
216 static string calcTypeName(const Type *Ty, map<const Type *, string> &TypeNames,
217                            string &FunctionInfo) {
218   
219   // Takin' care of the fact that boolean would be int in C
220   // and that ushort would be unsigned short etc.
221   
222   // Base Case
223   if (Ty->isPrimitiveType())
224     switch (Ty->getPrimitiveID()) {
225     case Type::VoidTyID:   return "void";
226     case Type::BoolTyID:   return "bool";
227     case Type::UByteTyID:  return "unsigned char";
228     case Type::SByteTyID:  return "signed char";
229     case Type::UShortTyID: return "unsigned short";
230     case Type::ShortTyID:  return "short";
231     case Type::UIntTyID:   return "unsigned";
232     case Type::IntTyID:    return "int";
233     case Type::ULongTyID:  return "unsigned long long";
234     case Type::LongTyID:   return "signed long long";
235     case Type::FloatTyID:  return "float";
236     case Type::DoubleTyID: return "double";
237     default : assert(0 && "Unknown primitive type!");
238     }
239   
240   // Check to see if the type is named.
241   map<const Type *, string>::iterator I = TypeNames.find(Ty);
242   if (I != TypeNames.end())
243     return I->second;
244   
245   string Result;
246   string MInfo = "";
247   switch (Ty->getPrimitiveID()) {
248   case Type::FunctionTyID: {
249     const FunctionType *MTy = cast<const FunctionType>(Ty);
250     Result = calcTypeName(MTy->getReturnType(), TypeNames, MInfo);
251     if (MInfo != "")
252       Result += ") " + MInfo;
253     Result += "(";
254     FunctionInfo += " (";
255     for (FunctionType::ParamTypes::const_iterator
256            I = MTy->getParamTypes().begin(),
257            E = MTy->getParamTypes().end(); I != E; ++I) {
258       if (I != MTy->getParamTypes().begin())
259         FunctionInfo += ", ";
260       MInfo = "";
261       FunctionInfo += calcTypeName(*I, TypeNames, MInfo);
262       if (MInfo != "")
263         Result += ") " + MInfo;
264     }
265     if (MTy->isVarArg()) {
266       if (!MTy->getParamTypes().empty()) 
267         FunctionInfo += ", ";
268       FunctionInfo += "...";
269     }
270     FunctionInfo += ")";
271     break;
272   }
273   case Type::StructTyID: {
274     string tempstr = "";
275     const StructType *STy = cast<const StructType>(Ty);
276     Result = " struct {\n ";
277     int indx = 0;
278     for (StructType::ElementTypes::const_iterator
279            I = STy->getElementTypes().begin(),
280            E = STy->getElementTypes().end(); I != E; ++I) {
281       Result += calcTypeNameVar(*I, TypeNames, 
282                                 "field" + itostr(indx++), tempstr);
283       Result += ";\n ";
284     }
285     Result += " } ";
286     break;
287   }
288   case Type::PointerTyID:
289     Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(), 
290                           TypeNames, MInfo);
291     Result += "*";
292     break;
293   case Type::ArrayTyID: {
294     const ArrayType *ATy = cast<const ArrayType>(Ty);
295     int NumElements = ATy->getNumElements();
296     Result = calcTypeName(ATy->getElementType(), TypeNames, MInfo);
297     Result += "*";
298     break;
299   }
300   default:
301     assert(0 && "Unhandled case in getTypeProps!");
302     Result = "<error>";
303   }
304
305   return Result;
306 }
307
308 // Internal function
309 // Pass the Type* variable and and the variable name and this prints out the 
310 // variable declaration.
311 // This is different from calcTypeName because if you need to declare an array
312 // the size of the array would appear after the variable name itself
313 // For eg. int a[10];
314 static string calcTypeNameVar(const Type *Ty,
315                               map<const Type *, string> &TypeNames, 
316                               string VariableName, string NameSoFar) {
317   if (Ty->isPrimitiveType())
318     switch (Ty->getPrimitiveID()) {
319     case Type::BoolTyID: 
320       return "bool " + NameSoFar + VariableName;
321     case Type::UByteTyID: 
322       return "unsigned char " + NameSoFar + VariableName;
323     case Type::SByteTyID:
324       return "signed char " + NameSoFar + VariableName;
325     case Type::UShortTyID:
326       return "unsigned long long " + NameSoFar + VariableName;
327     case Type::ULongTyID:
328       return "unsigned long long " + NameSoFar + VariableName;
329     case Type::LongTyID:
330       return "signed long long " + NameSoFar + VariableName;
331     case Type::UIntTyID:
332       return "unsigned " + NameSoFar + VariableName;
333     default :
334       return Ty->getDescription() + " " + NameSoFar + VariableName; 
335     }
336   
337   // Check to see if the type is named.
338   map<const Type *, string>::iterator I = TypeNames.find(Ty);
339   if (I != TypeNames.end())
340     return I->second + " " + NameSoFar + VariableName;
341   
342   string Result;
343   string tempstr = "";
344
345   switch (Ty->getPrimitiveID()) {
346   case Type::FunctionTyID: {
347     string MInfo = "";
348     const FunctionType *MTy = cast<const FunctionType>(Ty);
349     Result += calcTypeName(MTy->getReturnType(), TypeNames, MInfo);
350     if (MInfo != "")
351       Result += ") " + MInfo;
352     Result += " " + NameSoFar + VariableName;
353     Result += " (";
354     for (FunctionType::ParamTypes::const_iterator
355            I = MTy->getParamTypes().begin(),
356            E = MTy->getParamTypes().end(); I != E; ++I) {
357       if (I != MTy->getParamTypes().begin())
358         Result += ", ";
359       MInfo = "";
360       Result += calcTypeName(*I, TypeNames, MInfo);
361       if (MInfo != "")
362         Result += ") " + MInfo;
363     }
364     if (MTy->isVarArg()) {
365       if (!MTy->getParamTypes().empty()) 
366         Result += ", ";
367       Result += "...";
368     }
369     Result += ")";
370     break;
371   }
372   case Type::StructTyID: {
373     const StructType *STy = cast<const StructType>(Ty);
374     Result = " struct {\n ";
375     int indx = 0;
376     for (StructType::ElementTypes::const_iterator
377            I = STy->getElementTypes().begin(),
378            E = STy->getElementTypes().end(); I != E; ++I) {
379       Result += calcTypeNameVar(*I, TypeNames, 
380                                 "field" + itostr(indx++), "");
381       Result += ";\n ";
382     }
383     Result += " }";
384     Result += " " + NameSoFar + VariableName;
385     break;
386   }  
387
388   case Type::PointerTyID: {
389     Result = calcTypeNameVar(cast<const PointerType>(Ty)->getElementType(), 
390                              TypeNames, tempstr, 
391                              "(*" + NameSoFar + VariableName + ")");
392     break;
393   }
394   
395   case Type::ArrayTyID: {
396     const ArrayType *ATy = cast<const ArrayType>(Ty);
397     int NumElements = ATy->getNumElements();
398     Result = calcTypeNameVar(ATy->getElementType(),  TypeNames, 
399                              tempstr, NameSoFar + VariableName + "[" + 
400                              itostr(NumElements) + "]");
401     break;
402   }
403   default:
404     assert(0 && "Unhandled case in getTypeProps!");
405     Result = "<error>";
406   }
407
408   return Result;
409 }
410
411 // printTypeVarInt - The internal guts of printing out a type that has a
412 // potentially named portion and the variable associated with the type.
413 static ostream &printTypeVarInt(ostream &Out, const Type *Ty,
414                              map<const Type *, string> &TypeNames,
415                              const string &VariableName) {
416   // Primitive types always print out their description, regardless of whether
417   // they have been named or not.
418   
419   if (Ty->isPrimitiveType())
420     switch (Ty->getPrimitiveID()) {
421     case Type::BoolTyID: 
422       return Out << "bool " << VariableName;
423     case Type::UByteTyID:
424       return Out << "unsigned char " << VariableName;
425     case Type::SByteTyID:
426       return Out << "signed char " << VariableName;
427     case Type::UShortTyID:
428       return Out << "unsigned long long " << VariableName;
429     case Type::ULongTyID:
430       return Out << "unsigned long long " << VariableName;
431     case Type::LongTyID:
432       return Out << "signed long long " << VariableName;
433     case Type::UIntTyID:
434       return Out << "unsigned " << VariableName;
435     default :
436       return Out << Ty->getDescription() << " " << VariableName; 
437     }
438   
439   // Check to see if the type is named.
440   map<const Type *, string>::iterator I = TypeNames.find(Ty);
441   if (I != TypeNames.end()) return Out << I->second << " " << VariableName;
442   
443   // Otherwise we have a type that has not been named but is a derived type.
444   // Carefully recurse the type hierarchy to print out any contained symbolic
445   // names.
446   //
447   string TypeNameVar, tempstr = "";
448   TypeNameVar = calcTypeNameVar(Ty, TypeNames, VariableName, tempstr);
449   return Out << TypeNameVar;
450 }
451
452 // Internal guts of printing a type name
453 static ostream &printTypeInt(ostream &Out, const Type *Ty,
454                              map<const Type *, string> &TypeNames) {
455   // Primitive types always print out their description, regardless of whether
456   // they have been named or not.
457   
458   if (Ty->isPrimitiveType())
459     switch (Ty->getPrimitiveID()) {
460     case Type::BoolTyID:
461       return Out << "bool";
462     case Type::UByteTyID:
463       return Out << "unsigned char";
464     case Type::SByteTyID:
465       return Out << "signed char";
466     case Type::UShortTyID:
467       return Out << "unsigned short";
468     case Type::ULongTyID:
469       return Out << "unsigned long long";
470     case Type::LongTyID:
471       return Out << "signed long long";
472     case Type::UIntTyID:
473       return Out << "unsigned";
474     default :
475       return Out << Ty->getDescription(); 
476     }
477   
478   // Check to see if the type is named.
479   map<const Type *, string>::iterator I = TypeNames.find(Ty);
480   if (I != TypeNames.end()) return Out << I->second;
481   
482   // Otherwise we have a type that has not been named but is a derived type.
483   // Carefully recurse the type hierarchy to print out any contained symbolic
484   // names.
485   //
486   string MInfo;
487   string TypeName = calcTypeName(Ty, TypeNames, MInfo);
488   // TypeNames.insert(std::make_pair(Ty, TypeName));
489   //Cache type name for later use
490   if (MInfo != "")
491     return Out << TypeName << ")" << MInfo;
492   else
493     return Out << TypeName;
494 }
495
496 namespace {
497
498   //Internal CWriter class mimics AssemblyWriter.
499   class CWriter {
500     ostream& Out; 
501     SlotCalculator &Table;
502     const Module *TheModule;
503     map<const Type *, string> TypeNames;
504   public:
505     inline CWriter(ostream &o, SlotCalculator &Tab, const Module *M)
506       : Out(o), Table(Tab), TheModule(M) {
507     }
508     
509     inline void write(const Module *M) { printModule(M); }
510
511     ostream& printTypeVar(const Type *Ty, const string &VariableName) {
512       return printTypeVarInt(Out, Ty, TypeNames, VariableName);
513     }
514
515
516
517     ostream& printType(const Type *Ty, ostream &Out);
518     void writeOperand(const Value *Operand, ostream &Out,bool PrintName = true);
519
520     string getValueName(const Value *V);
521   private :
522
523     void printModule(const Module *M);
524     void printSymbolTable(const SymbolTable &ST);
525     void printConstant(const Constant *CPV);
526     void printGlobal(const GlobalVariable *GV);
527     void printFunctionSignature(const Function *F);
528     void printFunctionDecl(const Function *F); // Print just the forward decl
529     void printFunctionArgument(const Argument *FA);
530     
531     void printFunction(const Function *);
532     
533     void outputBasicBlock(const BasicBlock *);
534   };
535   /* END class CWriter */
536
537
538   /* CLASS InstLocalVarsVisitor */
539   class InstLocalVarsVisitor : public InstVisitor<InstLocalVarsVisitor> {
540     CWriter& CW;
541     void handleTerminator(TerminatorInst *tI, int indx);
542   public:
543     CLocalVars CLV;
544     
545     InstLocalVarsVisitor(CWriter &cw) : CW(cw) {}
546     
547     void visitInstruction(Instruction *I) {
548       if (I->getType() != Type::VoidTy) {
549         string tempostr = CW.getValueName(I);
550         CLV.addLocalVar(I->getType(), tempostr);
551       }
552     }
553
554     void visitBranchInst(BranchInst *I) {
555       handleTerminator(I, 0);
556       if (I->isConditional())
557         handleTerminator(I, 1);
558     }
559   };
560 }
561
562 void InstLocalVarsVisitor::handleTerminator(TerminatorInst *tI,int indx) {
563   BasicBlock *bb = tI->getSuccessor(indx);
564
565   BasicBlock::const_iterator insIt = bb->begin();
566   while (insIt != bb->end()) {
567     if (const PHINode *pI = dyn_cast<PHINode>(*insIt)) {
568       // Its a phinode!
569       // Calculate the incoming index for this
570       assert(pI->getBasicBlockIndex(tI->getParent()) != -1);
571
572       CLV.addLocalVar(pI->getType(), CW.getValueName(pI));
573     } else
574       break;
575     insIt++;
576   }
577 }
578
579 namespace {
580   /* CLASS CInstPrintVisitor */
581
582   class CInstPrintVisitor: public InstVisitor<CInstPrintVisitor> {
583     CWriter& CW;
584     SlotCalculator& Table;
585     ostream &Out;
586     const Value *Operand;
587
588     void outputLValue(Instruction *);
589     void printPhiFromNextBlock(TerminatorInst *tI, int indx);
590
591   public:
592     CInstPrintVisitor (CWriter &cw, SlotCalculator& table, ostream& o) 
593       : CW(cw), Table(table), Out(o) {
594       
595     }
596     
597     void visitCastInst(CastInst *I);
598     void visitCallInst(CallInst *I);
599     void visitShr(ShiftInst *I);
600     void visitShl(ShiftInst *I);
601     void visitReturnInst(ReturnInst *I);
602     void visitBranchInst(BranchInst *I);
603     void visitSwitchInst(SwitchInst *I);
604     void visitInvokeInst(InvokeInst *I) ;
605     void visitMallocInst(MallocInst *I);
606     void visitAllocaInst(AllocaInst *I);
607     void visitFreeInst(FreeInst   *I);
608     void visitLoadInst(LoadInst   *I);
609     void visitStoreInst(StoreInst  *I);
610     void visitGetElementPtrInst(GetElementPtrInst *I);
611     void visitPHINode(PHINode *I);
612     void visitUnaryOperator (UnaryOperator *I);
613     void visitBinaryOperator(BinaryOperator *I);
614   };
615 }
616
617 void CInstPrintVisitor::outputLValue(Instruction *I) {
618   Out << "  " << CW.getValueName(I) << " = ";
619 }
620
621 void CInstPrintVisitor::printPhiFromNextBlock(TerminatorInst *tI, int indx) {
622   BasicBlock *bb = tI->getSuccessor(indx);
623   BasicBlock::const_iterator insIt = bb->begin();
624   while (insIt != bb->end()) {
625     if (PHINode *pI = dyn_cast<PHINode>(*insIt)) {
626       //Its a phinode!
627       //Calculate the incoming index for this
628       int incindex = pI->getBasicBlockIndex(tI->getParent());
629       if (incindex != -1) {
630         //now we have to do the printing
631         outputLValue(pI);
632         CW.writeOperand(pI->getIncomingValue(incindex), Out);
633         Out << ";\n";
634       }
635     }
636     else break;
637     insIt++;
638   }
639 }
640
641 // Implement all "other" instructions, except for PHINode
642 void CInstPrintVisitor::visitCastInst(CastInst *I) {
643   outputLValue(I);
644   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
645   Out << "(";
646   CW.printType(I->getType(), Out);
647   Out << ")";
648   CW.writeOperand(Operand, Out);
649   Out << ";\n";
650 }
651
652 void CInstPrintVisitor::visitCallInst(CallInst *I) {
653   outputLValue(I);
654   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
655   const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
656   const FunctionType  *MTy = PTy 
657     ? dyn_cast<FunctionType>(PTy->getElementType()):0;
658   const Type      *RetTy = MTy ? MTy->getReturnType() : 0;
659   
660   // If possible, print out the short form of the call instruction, but we can
661   // only do this if the first argument is a pointer to a nonvararg method,
662   // and if the value returned is not a pointer to a method.
663   //
664   if (RetTy && !MTy->isVarArg() &&
665       (!isa<PointerType>(RetTy)||
666        !isa<FunctionType>(cast<PointerType>(RetTy)))){
667     Out << " ";
668     Out << makeNameProper(Operand->getName());
669   } else {
670     Out << makeNameProper(Operand->getName());      
671   }
672   Out << "(";
673   if (I->getNumOperands() > 1) 
674     CW.writeOperand(I->getOperand(1), Out);
675   for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
676     Out << ",";
677     CW.writeOperand(I->getOperand(op), Out);
678   }
679   
680   Out << " );\n";
681
682  
683 void CInstPrintVisitor::visitShr(ShiftInst *I) {
684   outputLValue(I);
685   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
686   Out << "(";
687   CW.writeOperand(Operand, Out);
688   Out << " >> ";
689   Out << "(";
690   CW.writeOperand(I->getOperand(1), Out);
691   Out << "));\n";
692 }
693
694 void CInstPrintVisitor::visitShl(ShiftInst *I) {
695   outputLValue(I);
696   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
697   Out << "(";
698   CW.writeOperand(Operand, Out);
699   Out << " << ";
700   Out << "(";
701   CW.writeOperand(I->getOperand(1), Out);
702   Out << "));\n";
703 }
704
705 // Specific Instruction type classes... note that all of the casts are
706 // neccesary because we use the instruction classes as opaque types...
707 //
708 void CInstPrintVisitor::visitReturnInst(ReturnInst *I) {
709   Out << "return ";
710   if (I->getNumOperands())
711     CW.writeOperand(I->getOperand(0), Out);
712   Out << ";\n";
713 }
714
715 void CInstPrintVisitor::visitBranchInst(BranchInst *I) {
716   TerminatorInst *tI = cast<TerminatorInst>(I);
717   if (I->isConditional()) {
718     Out << "  if (";
719     CW.writeOperand(I->getCondition(), Out);
720     Out << ")\n";
721     printPhiFromNextBlock(tI,0);
722     Out << "    goto ";
723     CW.writeOperand(I->getOperand(0), Out);
724     Out << ";\n";
725     Out << "  else\n";
726     printPhiFromNextBlock(tI,1);
727     Out << "    goto ";
728     CW.writeOperand(I->getOperand(1), Out);
729     Out << ";\n";
730   } else {
731     printPhiFromNextBlock(tI,0);
732     Out << "  goto ";
733     CW.writeOperand(I->getOperand(0), Out);
734     Out << ";\n";
735   }
736   Out << "\n";
737 }
738
739 void CInstPrintVisitor::visitSwitchInst(SwitchInst *I) {
740   Out << "\n";
741 }
742
743 void CInstPrintVisitor::visitInvokeInst(InvokeInst *I) {
744   Out << "\n";
745 }
746
747 void CInstPrintVisitor::visitMallocInst(MallocInst *I) {
748   outputLValue(I);
749   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
750   string tempstr = "";
751   Out << "(";
752   CW.printType(cast<const PointerType>(I->getType())->getElementType(), Out);
753   Out << "*) malloc(sizeof(";
754   CW.printTypeVar(cast<const PointerType>(I->getType())->getElementType(), 
755                   tempstr);
756   Out << ")";
757   if (I->getNumOperands()) {
758     Out << " * " ;
759     CW.writeOperand(Operand, Out);
760   }
761   Out << ");";
762 }
763
764 void CInstPrintVisitor::visitAllocaInst(AllocaInst *I) {
765   outputLValue(I);
766   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
767   string tempstr = "";
768   Out << "(";
769   CW.printTypeVar(I->getType(), tempstr);
770   Out << ") alloca(sizeof(";
771   CW.printTypeVar(cast<PointerType>(I->getType())->getElementType(), 
772                   tempstr);
773   Out << ")";
774   if (I->getNumOperands()) {
775     Out << " * " ;
776     CW.writeOperand(Operand, Out);
777   }
778   Out << ");\n";
779 }
780
781 void CInstPrintVisitor::visitFreeInst(FreeInst   *I) {
782   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
783   Out << "free(";
784   CW.writeOperand(Operand, Out);
785   Out << ");\n";
786 }
787
788 void CInstPrintVisitor::visitLoadInst(LoadInst   *I) {
789   outputLValue(I);
790   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
791   if (I->getNumOperands() <= 1) {
792     Out << "*";
793     CW.writeOperand(Operand, Out);     
794   }
795   else {
796     //Check if it is an array type or struct type ptr!
797     int arrtype = 1;
798     const PointerType *PTy = dyn_cast<PointerType>(I->getType());
799     if (cast<const PointerType>(Operand->getType())->getElementType()->getPrimitiveID() == Type::StructTyID)
800       arrtype = 0;
801     if (arrtype && isa<GlobalValue>(Operand))
802       Out << "(&";
803     CW.writeOperand(Operand,Out);
804     for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
805       if (i == 1) {
806         if (arrtype || !isa<GlobalValue>(Operand)) {
807           Out << "[";
808           CW.writeOperand(I->getOperand(i),  Out);
809           Out << "]";
810         }
811         if (isa<GlobalValue>(Operand) && arrtype)
812           Out << ")";
813       }
814       else {
815         if (arrtype == 1) Out << "[";
816         else 
817           Out << ".field";
818         CW.writeOperand(I->getOperand(i), Out);
819         if (arrtype == 1) Out << "]";
820       }
821     }
822   }
823   Out << ";\n";
824 }
825
826 void CInstPrintVisitor::visitStoreInst(StoreInst  *I) {
827   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
828   if (I->getNumOperands() <= 2) {
829     Out << "*";
830     CW.writeOperand(I->getOperand(1), Out);
831   }
832   else {
833     //Check if it is an array type or struct type ptr!
834     int arrtype = 1;
835     if (cast<const PointerType>(I->getOperand(1)->getType())->getElementType()->getPrimitiveID() == Type::StructTyID) 
836       arrtype = 0;
837     if (isa<GlobalValue>(I->getOperand(1)) && arrtype)
838       Out << "(&";
839     CW.writeOperand(I->getOperand(1), Out);
840     for (unsigned i = 2, E = I->getNumOperands(); i != E; ++i) {
841       if (i == 2) {
842         if (arrtype || !isa<GlobalValue>(I->getOperand(1))) {
843           Out << "[";
844           CW.writeOperand(I->getOperand(i), Out);
845           Out << "]";
846         }
847         if (isa<GlobalValue>(I->getOperand(1)) && arrtype)
848           Out << ")";
849       }
850       else {
851         if (arrtype == 1) Out << "[";
852         else 
853           Out << ".field";
854         CW.writeOperand(I->getOperand(i), Out);
855         if (arrtype == 1) Out << "]";
856       }
857     }
858   }
859   Out << " = ";
860   CW.writeOperand(Operand, Out);
861   Out << ";\n";
862 }
863
864 void CInstPrintVisitor::visitGetElementPtrInst(GetElementPtrInst *I) {
865   outputLValue(I);
866   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
867   Out << " &(";
868   if (I->getNumOperands() <= 1)
869     CW.writeOperand(Operand, Out);
870   else {
871     //Check if it is an array type or struct type ptr!
872     int arrtype = 1;
873     if ((cast<const PointerType>(Operand->getType()))->getElementType()->getPrimitiveID() == Type::StructTyID) 
874       arrtype = 0;
875     if ((isa<GlobalValue>(Operand)) && arrtype)
876       Out << "(&";    
877     CW.writeOperand(Operand, Out);
878     for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
879       if (i == 1) {
880         if (arrtype || !isa<GlobalValue>(Operand)){
881           Out << "[";
882           CW.writeOperand(I->getOperand(i), Out);
883           Out << "]";
884         }
885         if (isa<GlobalValue>(Operand) && arrtype)
886           Out << ")";
887       }
888       else {
889         if (arrtype == 1) Out << "[";
890         else 
891           Out << ".field";
892         CW.writeOperand(I->getOperand(i), Out);
893         if (arrtype == 1) Out << "]";
894       }
895     }
896   }
897   Out << ");\n";
898 }
899
900 void CInstPrintVisitor::visitPHINode(PHINode *I) {
901   
902 }
903
904 void CInstPrintVisitor::visitUnaryOperator (UnaryOperator *I) {
905   if (I->getOpcode() == Instruction::Not) { 
906     outputLValue(I);
907     Operand = I->getNumOperands() ? I->getOperand(0) : 0;
908     Out << "!(";
909     CW.writeOperand(Operand, Out);
910     Out << ");\n";
911   }
912   else {
913     Out << "<bad unary inst>\n";
914   }
915 }
916
917 void CInstPrintVisitor::visitBinaryOperator(BinaryOperator *I) {
918   //binary instructions, shift instructions, setCond instructions.
919   outputLValue(I);
920   Operand = I->getNumOperands() ? I->getOperand(0) : 0;
921   if (I->getType()->getPrimitiveID() == Type::PointerTyID) {
922     Out << "(";
923     CW.printType(I->getType(), Out);
924     Out << ")";
925   }
926   Out << "(";
927   if (Operand->getType()->getPrimitiveID() == Type::PointerTyID)
928     Out << "(long long)";
929   CW.writeOperand(Operand, Out);
930   Out << getOpcodeOperName(I);
931   // Need the extra parenthisis if the second operand is < 0
932   Out << '(';
933   if (I->getOperand(1)->getType()->getPrimitiveID() == Type::PointerTyID)
934     Out << "(long long)";
935   CW.writeOperand(I->getOperand(1), Out);
936   Out << ')';
937   Out << ");\n";
938 }
939
940 /* END : CInstPrintVisitor implementation */
941
942 string CWriter::getValueName(const Value *V) {
943   if (V->hasName())              // Print out the label if it exists...
944     return "llvm__" + makeNameProper(V->getName()) + "_" +
945            utostr(V->getType()->getUniqueID());
946
947   int Slot = Table.getValSlot(V);
948   assert(Slot >= 0 && "Invalid value!");
949   return "llvm__tmp_" + itostr(Slot) + "_" +
950          utostr(V->getType()->getUniqueID());
951 }
952
953 void CWriter::printModule(const Module *M) {
954   // printing stdlib inclusion
955   // Out << "#include <stdlib.h>\n";
956
957   // get declaration for alloca
958   Out << "/* Provide Declarations */\n"
959       << "#include <alloca.h>\n\n"
960
961     // Provide a definition for null if one does not already exist.
962       << "#ifndef NULL\n#define NULL 0\n#endif\n\n"
963       << "typedef unsigned char bool;\n"
964
965       << "\n\n/* Global Symbols */\n";
966
967   // Loop over the symbol table, emitting all named constants...
968   if (M->hasSymbolTable())
969     printSymbolTable(*M->getSymbolTable());
970
971   Out << "\n\n/* Global Data */\n";
972   for_each(M->gbegin(), M->gend(), 
973            bind_obj(this, &CWriter::printGlobal));
974
975   // First output all the declarations of the functions as C requires Functions 
976   // be declared before they are used.
977   //
978   Out << "\n\n/* Function Declarations */\n";
979   for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunctionDecl));
980   
981   // Output all of the functions...
982   Out << "\n\n/* Function Bodies */\n";
983   for_each(M->begin(), M->end(), bind_obj(this, &CWriter::printFunction));
984 }
985
986 // prints the global constants
987 void CWriter::printGlobal(const GlobalVariable *GV) {
988   string tempostr = getValueName(GV);
989   if (GV->hasInternalLinkage()) Out << "static ";
990
991   printTypeVar(GV->getType()->getElementType(), tempostr);
992
993   if (GV->hasInitializer()) {
994     Out << " = " ;
995     writeOperand(GV->getInitializer(), Out, false);
996   }
997
998   Out << ";\n";
999 }
1000
1001 // printSymbolTable - Run through symbol table looking for named constants
1002 // if a named constant is found, emit it's declaration...
1003 // Assuming that symbol table has only types and constants.
1004 void CWriter::printSymbolTable(const SymbolTable &ST) {
1005   // GraphT G;
1006   for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
1007     SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
1008     SymbolTable::type_const_iterator End = ST.type_end(TI->first);
1009     
1010     // TODO
1011     // Need to run through all the used types in the program
1012     // FindUsedTypes &FUT = new FindUsedTypes();
1013     // const std::set<const Type *> &UsedTypes = FUT.getTypes();
1014     // Filter out the structures printing forward definitions for each of them
1015     // and creating the dependency graph.
1016     // Print forward definitions to all of them
1017     // print the typedefs topologically sorted
1018
1019     // But for now we have
1020     for (; I != End; ++I) {
1021       const Value *V = I->second;
1022       if (const Constant *CPV = dyn_cast<const Constant>(V)) {
1023         printConstant(CPV);
1024       } else if (const Type *Ty = dyn_cast<const Type>(V)) {
1025         string tempostr;
1026         string tempstr = "";
1027         Out << "typedef ";
1028         tempostr = "llvm__" + I->first;
1029         string TypeNameVar = calcTypeNameVar(Ty, TypeNames, 
1030                                              tempostr, tempstr);
1031         Out << TypeNameVar << ";\n";
1032         if (!isa<PointerType>(Ty) ||
1033             !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
1034           TypeNames.insert(std::make_pair(Ty, "llvm__"+I->first));
1035       }
1036     }
1037   }
1038 }
1039
1040
1041 // printConstant - Print out a constant pool entry...
1042 //
1043 void CWriter::printConstant(const Constant *CPV) {
1044   // TODO
1045   // Dinakar : Don't know what to do with unnamed constants
1046   // should do something about it later.
1047
1048   string tempostr = getValueName(CPV);
1049   
1050   // Print out the constant type...
1051   printTypeVar(CPV->getType(), tempostr);
1052   
1053   Out << " = ";
1054   // Write the value out now...
1055   writeOperand(CPV, Out, false);
1056   
1057   Out << "\n";
1058 }
1059
1060 // printFunctionDecl - Print function declaration
1061 //
1062 void CWriter::printFunctionDecl(const Function *F) {
1063   printFunctionSignature(F);
1064   Out << ";\n";
1065 }
1066
1067 void CWriter::printFunctionSignature(const Function *F) {
1068   if (F->hasInternalLinkage()) Out << "static ";
1069   
1070   // Loop over the arguments, printing them...
1071   const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
1072   
1073   // Print out the return type and name...
1074   printType(F->getReturnType(), Out);
1075   Out << " " << makeNameProper(F->getName()) << "(";
1076     
1077   if (!F->isExternal()) {
1078     for_each(F->getArgumentList().begin(), F->getArgumentList().end(),
1079              bind_obj(this, &CWriter::printFunctionArgument));
1080   } else {
1081     // Loop over the arguments, printing them...
1082     for (FunctionType::ParamTypes::const_iterator I = 
1083            FT->getParamTypes().begin(),
1084            E = FT->getParamTypes().end(); I != E; ++I) {
1085       if (I != FT->getParamTypes().begin()) Out << ", ";
1086       printType(*I, Out);
1087     }
1088   }
1089
1090   // Finish printing arguments...
1091   if (FT->isVarArg()) {
1092     if (FT->getParamTypes().size()) Out << ", ";
1093     Out << "...";  // Output varargs portion of signature!
1094   }
1095   Out << ")";
1096 }
1097
1098
1099 // printFunctionArgument - This member is called for every argument that 
1100 // is passed into the method.  Simply print it out
1101 //
1102 void CWriter::printFunctionArgument(const Argument *Arg) {
1103   // Insert commas as we go... the first arg doesn't get a comma
1104   if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
1105   
1106   // Output type...
1107   printTypeVar(Arg->getType(), getValueName(Arg));
1108 }
1109
1110 void CWriter::printFunction(const Function *F) {
1111   if (F->isExternal()) return;
1112
1113   // Process each of the basic blocks, gather information and call the  
1114   // output methods on the CLocalVars and Function* objects.
1115     
1116   // gather local variable information for each basic block
1117   InstLocalVarsVisitor ILV(*this);
1118   ILV.visit((Function *)F);
1119
1120   printFunctionSignature(F);
1121   Out << " {\n";
1122
1123   // Loop over the symbol table, emitting all named constants...
1124   if (F->hasSymbolTable())
1125     printSymbolTable(*F->getSymbolTable()); 
1126   
1127   // print the local variables
1128   // we assume that every local variable is alloca'ed in the C code.
1129   std::map<const Type*, VarListType> &locals = ILV.CLV.LocalVars;
1130   
1131   map<const Type*, VarListType>::iterator iter;
1132   for (iter = locals.begin(); iter != locals.end(); ++iter) {
1133     VarListType::iterator listiter;
1134     for (listiter = iter->second.begin(); listiter != iter->second.end(); 
1135          ++listiter) {
1136       Out << "  ";
1137       printTypeVar(iter->first, *listiter);
1138       Out << ";\n";
1139     }
1140   }
1141  
1142   // print the basic blocks
1143   for_each(F->begin(), F->end(), bind_obj(this, &CWriter::outputBasicBlock));
1144   
1145   Out << "}\n";
1146 }
1147
1148 void CWriter::outputBasicBlock(const BasicBlock* BB) {
1149   Out << getValueName(BB) << ":\n";
1150
1151   // Output all of the instructions in the basic block...
1152   // print the basic blocks
1153   CInstPrintVisitor CIPV(*this, Table, Out);
1154   CIPV.visit((BasicBlock *) BB);
1155 }
1156
1157 // printType - Go to extreme measures to attempt to print out a short, symbolic
1158 // version of a type name.
1159 ostream& CWriter::printType(const Type *Ty, ostream &Out) {
1160   return printTypeInt(Out, Ty, TypeNames);
1161 }
1162
1163
1164 void CWriter::writeOperand(const Value *Operand,
1165                            ostream &Out, bool PrintName = true) {
1166   if (PrintName && Operand->hasName()) {   
1167     // If Operand has a name.
1168     Out << "llvm__" << makeNameProper(Operand->getName()) << "_" << 
1169       Operand->getType()->getUniqueID();
1170     return;
1171   } 
1172   else if (const Constant *CPV = dyn_cast<const Constant>(Operand)) {
1173     if (isa<ConstantPointerNull>(CPV))
1174       Out << "NULL";
1175     else
1176       Out << getConstStrValue(CPV); 
1177   }
1178   else {
1179     int Slot = Table.getValSlot(Operand);
1180     if (Slot >= 0)  
1181       Out << "llvm__tmp_" << Slot << "_" << Operand->getType()->getUniqueID();
1182     else if (PrintName)
1183       Out << "<badref>";
1184   }
1185 }
1186
1187
1188 //===----------------------------------------------------------------------===//
1189 //                       External Interface declaration
1190 //===----------------------------------------------------------------------===//
1191
1192 void WriteToC(const Module *C, ostream &Out) {
1193   assert(C && "You can't write a null module!!");
1194   SlotCalculator SlotTable(C, true);
1195   CWriter W(Out, SlotTable, C);
1196   W.write(C);
1197   Out.flush();
1198 }
1199