Next batch of implementation:
[oota-llvm.git] / tools / llvm2cpp / CppWriter.cpp
1 //===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the writing of the LLVM IR as a set of C++ calls to the
11 // LLVM IR interface. The input module is assumed to be verified.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/CallingConv.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/InlineAsm.h"
19 #include "llvm/Instruction.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/SymbolTable.h"
23 #include "llvm/Support/CFG.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/Support/MathExtras.h"
27 #include <algorithm>
28 #include <iostream>
29
30 using namespace llvm;
31
32 namespace {
33 typedef std::vector<const Type*> TypeList;
34 typedef std::map<const Type*,std::string> TypeMap;
35 typedef std::map<const Value*,std::string> ValueMap;
36
37 class CppWriter {
38   std::ostream &Out;
39   const Module *TheModule;
40   unsigned long uniqueNum;
41   TypeMap TypeNames;
42   ValueMap ValueNames;
43   TypeMap UnresolvedTypes;
44   TypeList TypeStack;
45
46 public:
47   inline CppWriter(std::ostream &o, const Module *M)
48     : Out(o), TheModule(M), uniqueNum(0), TypeNames(),
49       ValueNames(), UnresolvedTypes(), TypeStack() { }
50
51   const Module* getModule() { return TheModule; }
52
53   void printModule(const Module *M);
54
55 private:
56   void printTypes(const Module* M);
57   void printConstants(const Module* M);
58   void printConstant(const Constant *CPV);
59   void printGlobal(const GlobalVariable *GV);
60   void printFunction(const Function *F);
61   void printInstruction(const Instruction *I, const std::string& bbname);
62   void printSymbolTable(const SymbolTable &ST);
63   void printLinkageType(GlobalValue::LinkageTypes LT);
64   void printCallingConv(unsigned cc);
65
66   std::string getCppName(const Type* val);
67   std::string getCppName(const Value* val);
68   inline void printCppName(const Value* val);
69   inline void printCppName(const Type* val);
70   bool isOnStack(const Type*) const;
71   inline void printTypeDef(const Type* Ty);
72   bool printTypeDefInternal(const Type* Ty);
73   void printEscapedString(const std::string& str);
74 };
75
76 // printEscapedString - Print each character of the specified string, escaping
77 // it if it is not printable or if it is an escape char.
78 void 
79 CppWriter::printEscapedString(const std::string &Str) {
80   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
81     unsigned char C = Str[i];
82     if (isprint(C) && C != '"' && C != '\\') {
83       Out << C;
84     } else {
85       Out << '\\'
86           << (char) ((C/16  < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
87           << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
88     }
89   }
90 }
91
92 std::string
93 CppWriter::getCppName(const Value* val) {
94   std::string name;
95   ValueMap::iterator I = ValueNames.find(val);
96   if (I != ValueNames.end()) {
97     name = I->second;
98   } else {
99     const char* prefix;
100     switch (val->getType()->getTypeID()) {
101       case Type::VoidTyID:     prefix = "void_"; break;
102       case Type::BoolTyID:     prefix = "bool_"; break; 
103       case Type::UByteTyID:    prefix = "ubyte_"; break;
104       case Type::SByteTyID:    prefix = "sbyte_"; break;
105       case Type::UShortTyID:   prefix = "ushort_"; break;
106       case Type::ShortTyID:    prefix = "short_"; break;
107       case Type::UIntTyID:     prefix = "uint_"; break;
108       case Type::IntTyID:      prefix = "int_"; break;
109       case Type::ULongTyID:    prefix = "ulong_"; break;
110       case Type::LongTyID:     prefix = "long_"; break;
111       case Type::FloatTyID:    prefix = "float_"; break;
112       case Type::DoubleTyID:   prefix = "double_"; break;
113       case Type::LabelTyID:    prefix = "label_"; break;
114       case Type::FunctionTyID: prefix = "func_"; break;
115       case Type::StructTyID:   prefix = "struct_"; break;
116       case Type::ArrayTyID:    prefix = "array_"; break;
117       case Type::PointerTyID:  prefix = "ptr_"; break;
118       case Type::PackedTyID:   prefix = "packed_"; break;
119       default:                 prefix = "other_"; break;
120     }
121     name = ValueNames[val] = std::string(prefix) +
122         (val->hasName() ? val->getName() : utostr(uniqueNum++));
123   }
124   return name;
125 }
126
127 void
128 CppWriter::printCppName(const Value* val) {
129   printEscapedString(getCppName(val));
130 }
131
132 void
133 CppWriter::printCppName(const Type* Ty)
134 {
135   printEscapedString(getCppName(Ty));
136 }
137
138 // Gets the C++ name for a type. Returns true if we already saw the type,
139 // false otherwise.
140 //
141 inline const std::string* 
142 findTypeName(const SymbolTable& ST, const Type* Ty)
143 {
144   SymbolTable::type_const_iterator TI = ST.type_begin();
145   SymbolTable::type_const_iterator TE = ST.type_end();
146   for (;TI != TE; ++TI)
147     if (TI->second == Ty)
148       return &(TI->first);
149   return 0;
150 }
151
152 std::string
153 CppWriter::getCppName(const Type* Ty)
154 {
155   // First, handle the primitive types .. easy
156   if (Ty->isPrimitiveType()) {
157     switch (Ty->getTypeID()) {
158       case Type::VoidTyID:     return "Type::VoidTy";
159       case Type::BoolTyID:     return "Type::BoolTy"; 
160       case Type::UByteTyID:    return "Type::UByteTy";
161       case Type::SByteTyID:    return "Type::SByteTy";
162       case Type::UShortTyID:   return "Type::UShortTy";
163       case Type::ShortTyID:    return "Type::ShortTy";
164       case Type::UIntTyID:     return "Type::UIntTy";
165       case Type::IntTyID:      return "Type::IntTy";
166       case Type::ULongTyID:    return "Type::ULongTy";
167       case Type::LongTyID:     return "Type::LongTy";
168       case Type::FloatTyID:    return "Type::FloatTy";
169       case Type::DoubleTyID:   return "Type::DoubleTy";
170       case Type::LabelTyID:    return "Type::LabelTy";
171       default:
172         assert(!"Can't get here");
173         break;
174     }
175     return "Type::VoidTy"; // shouldn't be returned, but make it sensible
176   }
177
178   // Now, see if we've seen the type before and return that
179   TypeMap::iterator I = TypeNames.find(Ty);
180   if (I != TypeNames.end())
181     return I->second;
182
183   // Okay, let's build a new name for this type. Start with a prefix
184   const char* prefix = 0;
185   switch (Ty->getTypeID()) {
186     case Type::FunctionTyID:    prefix = "FuncTy_"; break;
187     case Type::StructTyID:      prefix = "StructTy_"; break;
188     case Type::ArrayTyID:       prefix = "ArrayTy_"; break;
189     case Type::PointerTyID:     prefix = "PointerTy_"; break;
190     case Type::OpaqueTyID:      prefix = "OpaqueTy_"; break;
191     case Type::PackedTyID:      prefix = "PackedTy_"; break;
192     default:                    prefix = "OtherTy_"; break; // prevent breakage
193   }
194
195   // See if the type has a name in the symboltable and build accordingly
196   const std::string* tName = findTypeName(TheModule->getSymbolTable(), Ty);
197   std::string name;
198   if (tName) 
199     name = std::string(prefix) + *tName;
200   else
201     name = std::string(prefix) + utostr(uniqueNum++);
202
203   // Save the name
204   return TypeNames[Ty] = name;
205 }
206
207 void CppWriter::printModule(const Module *M) {
208   Out << "\n// Module Construction\n";
209   Out << "Module* mod = new Module(\"";
210   if (M->getModuleIdentifier() == "-")
211     printEscapedString("<stdin>");
212   else 
213     printEscapedString(M->getModuleIdentifier());
214   Out << "\");\n";
215   Out << "mod->setEndianness(";
216   switch (M->getEndianness()) {
217     case Module::LittleEndian: Out << "Module::LittleEndian);\n"; break;
218     case Module::BigEndian:    Out << "Module::BigEndian);\n";    break;
219     case Module::AnyEndianness:Out << "Module::AnyEndianness);\n";  break;
220   }
221   Out << "mod->setPointerSize(";
222   switch (M->getPointerSize()) {
223     case Module::Pointer32:      Out << "Module::Pointer32);\n"; break;
224     case Module::Pointer64:      Out << "Module::Pointer64);\n"; break;
225     case Module::AnyPointerSize: Out << "Module::AnyPointerSize);\n"; break;
226   }
227   if (!M->getTargetTriple().empty())
228     Out << "mod->setTargetTriple(\"" << M->getTargetTriple() << "\");\n";
229
230   if (!M->getModuleInlineAsm().empty()) {
231     Out << "mod->setModuleInlineAsm(\"";
232     printEscapedString(M->getModuleInlineAsm());
233     Out << "\");\n";
234   }
235   
236   // Loop over the dependent libraries and emit them.
237   Module::lib_iterator LI = M->lib_begin();
238   Module::lib_iterator LE = M->lib_end();
239   while (LI != LE) {
240     Out << "mod->addLibrary(\"" << *LI << "\");\n";
241     ++LI;
242   }
243
244   // Print out all the type definitions
245   Out << "\n// Type Definitions\n";
246   printTypes(M);
247
248   // Print out all the constants declarations
249   Out << "\n// Constants Construction\n";
250   printConstants(M);
251
252   // Process the global variables
253   Out << "\n// Global Variable Construction\n";
254   for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
255        I != E; ++I) {
256     printGlobal(I);
257   }
258
259   // Output all of the functions.
260   Out << "\n// Function Construction\n";
261   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
262     printFunction(I);
263 }
264
265 void
266 CppWriter::printCallingConv(unsigned cc){
267   // Print the calling convention.
268   switch (cc) {
269     default:
270     case CallingConv::C:     Out << "CallingConv::C"; break;
271     case CallingConv::CSRet: Out << "CallingConv::CSRet"; break;
272     case CallingConv::Fast:  Out << "CallingConv::Fast"; break;
273     case CallingConv::Cold:  Out << "CallingConv::Cold"; break;
274     case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
275   }
276 }
277
278 void 
279 CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
280   switch (LT) {
281     case GlobalValue::InternalLinkage:  
282       Out << "GlobalValue::InternalLinkage"; break;
283     case GlobalValue::LinkOnceLinkage:  
284       Out << "GlobalValue::LinkOnceLinkage "; break;
285     case GlobalValue::WeakLinkage:      
286       Out << "GlobalValue::WeakLinkage"; break;
287     case GlobalValue::AppendingLinkage: 
288       Out << "GlobalValue::AppendingLinkage"; break;
289     case GlobalValue::ExternalLinkage: 
290       Out << "GlobalValue::ExternalLinkage"; break;
291     case GlobalValue::GhostLinkage:
292       Out << "GlobalValue::GhostLinkage"; break;
293   }
294 }
295 void CppWriter::printGlobal(const GlobalVariable *GV) {
296   Out << "\n";
297   Out << "GlobalVariable* ";
298   printCppName(GV);
299   Out << " = new GlobalVariable(\n";
300   Out << "  /*Type=*/";
301   printCppName(GV->getType()->getElementType());
302   Out << ",\n";
303   Out << "  /*isConstant=*/" << (GV->isConstant()?"true":"false") 
304       << ",\n  /*Linkage=*/";
305   printLinkageType(GV->getLinkage());
306   Out << ",\n  /*Initializer=*/";
307   if (GV->hasInitializer()) {
308     printCppName(GV->getInitializer());
309   } else {
310     Out << "0";
311   }
312   Out << ",\n  /*Name=*/\"";
313   printEscapedString(GV->getName());
314   Out << "\",\n  mod);\n";
315
316   if (GV->hasSection()) {
317     printCppName(GV);
318     Out << "->setSection(\"";
319     printEscapedString(GV->getSection());
320     Out << "\");\n";
321   }
322   if (GV->getAlignment()) {
323     printCppName(GV);
324     Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");\n";
325   };
326 }
327
328 bool
329 CppWriter::isOnStack(const Type* Ty) const {
330   TypeList::const_iterator TI = 
331     std::find(TypeStack.begin(),TypeStack.end(),Ty);
332   return TI != TypeStack.end();
333 }
334
335 // Prints a type definition. Returns true if it could not resolve all the types
336 // in the definition but had to use a forward reference.
337 void
338 CppWriter::printTypeDef(const Type* Ty) {
339   assert(TypeStack.empty());
340   TypeStack.clear();
341   printTypeDefInternal(Ty);
342   assert(TypeStack.empty());
343   // early resolve as many unresolved types as possible. Search the unresolved
344   // types map for the type we just printed. Now that its definition is complete
345   // we can resolve any preview references to it. This prevents a cascade of
346   // unresolved types.
347   TypeMap::iterator I = UnresolvedTypes.find(Ty);
348   if (I != UnresolvedTypes.end()) {
349     Out << "cast<OpaqueType>(" << I->second 
350         << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");\n";
351     Out << I->second << " = cast<";
352     switch (Ty->getTypeID()) {
353       case Type::FunctionTyID: Out << "FunctionType"; break;
354       case Type::ArrayTyID:    Out << "ArrayType"; break;
355       case Type::StructTyID:   Out << "StructType"; break;
356       case Type::PackedTyID:   Out << "PackedType"; break;
357       case Type::PointerTyID:  Out << "PointerType"; break;
358       case Type::OpaqueTyID:   Out << "OpaqueType"; break;
359       default:                 Out << "NoSuchDerivedType"; break;
360     }
361     Out << ">(" << I->second << "_fwd.get());\n\n";
362     UnresolvedTypes.erase(I);
363   }
364 }
365
366 bool
367 CppWriter::printTypeDefInternal(const Type* Ty) {
368   // We don't print definitions for primitive types
369   if (Ty->isPrimitiveType())
370     return false;
371
372   // Determine if the name is in the name list before we modify that list.
373   TypeMap::const_iterator TNI = TypeNames.find(Ty);
374
375   // Everything below needs the name for the type so get it now
376   std::string typeName(getCppName(Ty));
377
378   // Search the type stack for recursion. If we find it, then generate this
379   // as an OpaqueType, but make sure not to do this multiple times because
380   // the type could appear in multiple places on the stack. Once the opaque
381   // definition is issues, it must not be re-issued. Consequently we have to
382   // check the UnresolvedTypes list as well.
383   if (isOnStack(Ty)) {
384     TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
385     if (I == UnresolvedTypes.end()) {
386       Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();\n";
387       UnresolvedTypes[Ty] = typeName;
388       return true;
389     }
390   }
391
392   // Avoid printing things we have already printed. Since TNI was obtained
393   // before the name was inserted with getCppName and because we know the name
394   // is not on the stack (currently being defined), we can surmise here that if
395   // we got the name we've also already emitted its definition.
396   if (TNI != TypeNames.end())
397     return false;
398
399   // We're going to print a derived type which, by definition, contains other
400   // types. So, push this one we're printing onto the type stack to assist with
401   // recursive definitions.
402   TypeStack.push_back(Ty); // push on type stack
403   bool didRecurse = false;
404
405   // Print the type definition
406   switch (Ty->getTypeID()) {
407     case Type::FunctionTyID:  {
408       const FunctionType* FT = cast<FunctionType>(Ty);
409       Out << "std::vector<const Type*>" << typeName << "_args;\n";
410       FunctionType::param_iterator PI = FT->param_begin();
411       FunctionType::param_iterator PE = FT->param_end();
412       for (; PI != PE; ++PI) {
413         const Type* argTy = static_cast<const Type*>(*PI);
414         bool isForward = printTypeDefInternal(argTy);
415         std::string argName(getCppName(argTy));
416         Out << typeName << "_args.push_back(" << argName;
417         if (isForward)
418           Out << "_fwd";
419         Out << ");\n";
420       }
421       bool isForward = printTypeDefInternal(FT->getReturnType());
422       std::string retTypeName(getCppName(FT->getReturnType()));
423       Out << "FunctionType* " << typeName << " = FunctionType::get(\n"
424           << "  /*Result=*/" << retTypeName;
425       if (isForward)
426         Out << "_fwd";
427       Out << ",\n  /*Params=*/" << typeName << "_args,\n  /*isVarArg=*/"
428           << (FT->isVarArg() ? "true" : "false") << ");\n";
429       break;
430     }
431     case Type::StructTyID: {
432       const StructType* ST = cast<StructType>(Ty);
433       Out << "std::vector<const Type*>" << typeName << "_fields;\n";
434       StructType::element_iterator EI = ST->element_begin();
435       StructType::element_iterator EE = ST->element_end();
436       for (; EI != EE; ++EI) {
437         const Type* fieldTy = static_cast<const Type*>(*EI);
438         bool isForward = printTypeDefInternal(fieldTy);
439         std::string fieldName(getCppName(fieldTy));
440         Out << typeName << "_fields.push_back(" << fieldName;
441         if (isForward)
442           Out << "_fwd";
443         Out << ");\n";
444       }
445       Out << "StructType* " << typeName << " = StructType::get("
446           << typeName << "_fields);\n";
447       break;
448     }
449     case Type::ArrayTyID: {
450       const ArrayType* AT = cast<ArrayType>(Ty);
451       const Type* ET = AT->getElementType();
452       bool isForward = printTypeDefInternal(ET);
453       std::string elemName(getCppName(ET));
454       Out << "ArrayType* " << typeName << " = ArrayType::get("
455           << elemName << (isForward ? "_fwd" : "") 
456           << ", " << utostr(AT->getNumElements()) << ");\n";
457       break;
458     }
459     case Type::PointerTyID: {
460       const PointerType* PT = cast<PointerType>(Ty);
461       const Type* ET = PT->getElementType();
462       bool isForward = printTypeDefInternal(ET);
463       std::string elemName(getCppName(ET));
464       Out << "PointerType* " << typeName << " = PointerType::get("
465           << elemName << (isForward ? "_fwd" : "") << ");\n";
466       break;
467     }
468     case Type::PackedTyID: {
469       const PackedType* PT = cast<PackedType>(Ty);
470       const Type* ET = PT->getElementType();
471       bool isForward = printTypeDefInternal(ET);
472       std::string elemName(getCppName(ET));
473       Out << "PackedType* " << typeName << " = PackedType::get("
474           << elemName << (isForward ? "_fwd" : "") 
475           << ", " << utostr(PT->getNumElements()) << ");\n";
476       break;
477     }
478     case Type::OpaqueTyID: {
479       const OpaqueType* OT = cast<OpaqueType>(Ty);
480       Out << "OpaqueType* " << typeName << " = OpaqueType::get();\n";
481       break;
482     }
483     default:
484       assert(!"Invalid TypeID");
485   }
486
487   // If the type had a name, make sure we recreate it.
488   const std::string* progTypeName = 
489     findTypeName(TheModule->getSymbolTable(),Ty);
490   if (progTypeName)
491     Out << "mod->addTypeName(\"" << *progTypeName << "\", " 
492         << typeName << ");\n";
493
494   // Pop us off the type stack
495   TypeStack.pop_back();
496   Out << "\n";
497
498   // We weren't a recursive type
499   return false;
500 }
501
502 void
503 CppWriter::printTypes(const Module* M) {
504   // Add all of the global variables to the value table...
505   for (Module::const_global_iterator I = TheModule->global_begin(), 
506        E = TheModule->global_end(); I != E; ++I) {
507     if (I->hasInitializer())
508       printTypeDef(I->getInitializer()->getType());
509     printTypeDef(I->getType());
510   }
511
512   // Add all the functions to the table
513   for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
514        FI != FE; ++FI) {
515     printTypeDef(FI->getReturnType());
516     printTypeDef(FI->getFunctionType());
517     // Add all the function arguments
518     for(Function::const_arg_iterator AI = FI->arg_begin(),
519         AE = FI->arg_end(); AI != AE; ++AI) {
520       printTypeDef(AI->getType());
521     }
522
523     // Add all of the basic blocks and instructions
524     for (Function::const_iterator BB = FI->begin(),
525          E = FI->end(); BB != E; ++BB) {
526       printTypeDef(BB->getType());
527       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; 
528            ++I) {
529         printTypeDef(I->getType());
530       }
531     }
532   }
533 }
534
535 void
536 CppWriter::printConstants(const Module* M) {
537   // Add all of the global variables to the value table...
538   for (Module::const_global_iterator I = TheModule->global_begin(), 
539        E = TheModule->global_end(); I != E; ++I)
540     if (I->hasInitializer())
541       printConstant(I->getInitializer());
542
543   // Traverse the LLVM functions looking for constants
544   for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
545        FI != FE; ++FI) {
546     // Add all of the basic blocks and instructions
547     for (Function::const_iterator BB = FI->begin(),
548          E = FI->end(); BB != E; ++BB) {
549       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; 
550            ++I) {
551         for (unsigned i = 0; i < I->getNumOperands(); ++i) {
552           if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
553             printConstant(C);
554           }
555         }
556       }
557     }
558   }
559 }
560
561 // printConstant - Print out a constant pool entry...
562 void CppWriter::printConstant(const Constant *CV) {
563   // First, if the constant is in the constant list then we've printed it
564   // already and we shouldn't reprint it.
565   if (ValueNames.find(CV) != ValueNames.end())
566     return;
567
568   const int IndentSize = 2;
569   static std::string Indent = "\n";
570   std::string constName(getCppName(CV));
571   std::string typeName(getCppName(CV->getType()));
572   if (CV->isNullValue()) {
573     Out << "Constant* " << constName << " = Constant::getNullValue("
574         << typeName << ");\n";
575     return;
576   }
577   if (isa<GlobalValue>(CV)) {
578     // Skip variables and functions, we emit them elsewhere
579     return;
580   }
581   if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
582     Out << "Constant* " << constName << " = ConstantBool::get(" 
583         << (CB == ConstantBool::True ? "true" : "false")
584         << ");";
585   } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
586     Out << "Constant* " << constName << " = ConstantSInt::get(" 
587         << typeName << ", " << CI->getValue() << ");";
588   } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
589     Out << "Constant* " << constName << " = ConstantUInt::get(" 
590         << typeName << ", " << CI->getValue() << ");";
591   } else if (isa<ConstantAggregateZero>(CV)) {
592     Out << "Constant* " << constName << " = ConstantAggregateZero::get(" 
593         << typeName << ");";
594   } else if (isa<ConstantPointerNull>(CV)) {
595     Out << "Constant* " << constName << " = ConstanPointerNull::get(" 
596         << typeName << ");";
597   } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
598     Out << "ConstantFP::get(" << typeName << ", ";
599     // We would like to output the FP constant value in exponential notation,
600     // but we cannot do this if doing so will lose precision.  Check here to
601     // make sure that we only output it in exponential format if we can parse
602     // the value back and get the same value.
603     //
604     std::string StrVal = ftostr(CFP->getValue());
605
606     // Check to make sure that the stringized number is not some string like
607     // "Inf" or NaN, that atof will accept, but the lexer will not.  Check that
608     // the string matches the "[-+]?[0-9]" regex.
609     //
610     if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
611         ((StrVal[0] == '-' || StrVal[0] == '+') &&
612          (StrVal[1] >= '0' && StrVal[1] <= '9')))
613       // Reparse stringized version!
614       if (atof(StrVal.c_str()) == CFP->getValue()) {
615         Out << StrVal;
616         return;
617       }
618
619     // Otherwise we could not reparse it to exactly the same value, so we must
620     // output the string in hexadecimal format!
621     assert(sizeof(double) == sizeof(uint64_t) &&
622            "assuming that double is 64 bits!");
623     Out << "0x" << utohexstr(DoubleToBits(CFP->getValue())) << ");";
624   } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
625     if (CA->isString() && CA->getType()->getElementType() == Type::SByteTy) {
626       Out << "Constant* " << constName << " = ConstantArray::get(\"";
627       printEscapedString(CA->getAsString());
628       Out << "\");";
629     } else {
630       Out << "std::vector<Constant*> " << constName << "_elems;\n";
631       unsigned N = CA->getNumOperands();
632       for (unsigned i = 0; i < N; ++i) {
633         printConstant(CA->getOperand(i));
634         Out << constName << "_elems.push_back("
635             << getCppName(CA->getOperand(i)) << ");\n";
636       }
637       Out << "Constant* " << constName << " = ConstantArray::get(" 
638           << typeName << ", " << constName << "_elems);";
639     }
640   } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
641     Out << "std::vector<Constant*> " << constName << "_fields;\n";
642     unsigned N = CS->getNumOperands();
643     for (unsigned i = 0; i < N; i++) {
644       printConstant(CS->getOperand(i));
645       Out << constName << "_fields.push_back("
646           << getCppName(CA->getOperand(i)) << ");\n";
647     }
648     Out << "Constant* " << constName << " = ConstantStruct::get(" 
649         << typeName << ", " << constName << "_fields);";
650   } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
651     Out << "std::vector<Constant*> " << constName << "_elems;\n";
652     unsigned N = CP->getNumOperands();
653     for (unsigned i = 0; i < N; ++i) {
654       printConstant(CP->getOperand(i));
655       Out << constName << "_elems.push_back("
656           << getCppName(CP->getOperand(i)) << ");\n";
657     }
658     Out << "Constant* " << constName << " = ConstantPacked::get(" 
659         << typeName << ", " << constName << "_elems);";
660   } else if (isa<UndefValue>(CV)) {
661     Out << "Constant* " << constName << " = UndefValue::get(" 
662         << typeName << ");";
663   } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
664     if (CE->getOpcode() == Instruction::GetElementPtr) {
665       Out << "std::vector<Constant*> " << constName << "_indices;\n";
666       for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
667         Out << constName << "_indices.push_back("
668             << getCppName(CE->getOperand(i)) << ");\n";
669       }
670       Out << "Constant* " << constName << " = new GetElementPtrInst(" 
671           << getCppName(CE->getOperand(0)) << ", " << constName << "_indices";
672     } else if (CE->getOpcode() == Instruction::Cast) {
673       Out << "Constant* " << constName << " = ConstantExpr::getCast(";
674       Out << getCppName(CE->getOperand(0)) << ", " << getCppName(CE->getType())
675           << ");";
676     } else {
677       Out << "Constant* " << constName << " = ConstantExpr::";
678       switch (CE->getOpcode()) {
679         case Instruction::Add:    Out << "getAdd";  break;
680         case Instruction::Sub:    Out << "getSub"; break;
681         case Instruction::Mul:    Out << "getMul"; break;
682         case Instruction::Div:    Out << "getDiv"; break;
683         case Instruction::Rem:    Out << "getRem"; break;
684         case Instruction::And:    Out << "getAnd"; break;
685         case Instruction::Or:     Out << "getOr"; break;
686         case Instruction::Xor:    Out << "getXor"; break;
687         case Instruction::SetEQ:  Out << "getSetEQ"; break;
688         case Instruction::SetNE:  Out << "getSetNE"; break;
689         case Instruction::SetLE:  Out << "getSetLE"; break;
690         case Instruction::SetGE:  Out << "getSetGE"; break;
691         case Instruction::SetLT:  Out << "getSetLT"; break;
692         case Instruction::SetGT:  Out << "getSetGT"; break;
693         case Instruction::Shl:    Out << "getShl"; break;
694         case Instruction::Shr:    Out << "getShr"; break;
695         case Instruction::Select: Out << "getSelect"; break;
696         case Instruction::ExtractElement: Out << "getExtractElement"; break;
697         case Instruction::InsertElement:  Out << "getInsertElement"; break;
698         case Instruction::ShuffleVector:  Out << "getShuffleVector"; break;
699         default:
700           assert(!"Invalid constant expression");
701           break;
702       }
703       Out << getCppName(CE->getOperand(0));
704       for (unsigned i = 1; i < CE->getNumOperands(); ++i) 
705         Out << ", " << getCppName(CE->getOperand(i));
706       Out << ");";
707     }
708   } else {
709     assert(!"Bad Constant");
710     Out << "Constant* " << constName << " = 0; ";
711   }
712   Out << "\n";
713 }
714
715 /// printFunction - Print all aspects of a function.
716 ///
717 void CppWriter::printFunction(const Function *F) {
718   std::string funcTypeName(getCppName(F->getFunctionType()));
719
720   Out << "Function* ";
721   printCppName(F);
722   Out << " = new Function(" << funcTypeName << ", " ;
723   printLinkageType(F->getLinkage());
724   Out << ",\n  \"" << F->getName() << "\", mod);\n";
725   printCppName(F);
726   Out << "->setCallingConv(";
727   printCallingConv(F->getCallingConv());
728   Out << ");\n";
729   if (F->hasSection()) {
730     printCppName(F);
731     Out << "->setSection(" << F->getSection() << ");\n";
732   }
733   if (F->getAlignment()) {
734     printCppName(F);
735     Out << "->setAlignment(" << F->getAlignment() << ");\n";
736   }
737
738   if (!F->isExternal()) {
739     Out << "{\n";
740     // Create all the argument values
741     for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
742          AI != AE; ++AI) {
743       Out << "  Argument* " << getCppName(AI) << " = new Argument("
744           << getCppName(AI->getType()) << ", \"";
745       printEscapedString(AI->getName());
746       Out << "\", " << getCppName(F) << ");\n";
747     }
748     // Create all the basic blocks
749     for (Function::const_iterator BI = F->begin(), BE = F->end(); 
750          BI != BE; ++BI) {
751       std::string bbname(getCppName(BI));
752       Out << "  BasicBlock* " << bbname << " = new BasicBlock(\"";
753       if (BI->hasName())
754         printEscapedString(BI->getName());
755       Out << "\"," << getCppName(BI->getParent()) << ",0);\n";
756     }
757     // Output all of its basic blocks... for the function
758     for (Function::const_iterator BI = F->begin(), BE = F->end(); 
759          BI != BE; ++BI) {
760       // Output all of the instructions in the basic block...
761       Out << "  {\n";
762       for (BasicBlock::const_iterator I = BI->begin(), E = BI->end(); 
763            I != E; ++I) {
764         std::string bbname(getCppName(BI));
765         printInstruction(I,bbname);
766       }
767       Out << "  }\n";
768     }
769     Out << "}\n";
770   }
771 }
772
773 // printInstruction - This member is called for each Instruction in a function.
774 void 
775 CppWriter::printInstruction(const Instruction *I, const std::string& bbname) 
776 {
777   std::string iName(getCppName(I));
778
779   switch (I->getOpcode()) {
780     case Instruction::Ret: {
781       const ReturnInst* ret =  cast<ReturnInst>(I);
782       Out << "    ReturnInst* " << iName << " = new ReturnInst(";
783       if (ret->getReturnValue())
784         Out << getCppName(ret->getReturnValue()) << ", ";
785       Out << bbname << ");";
786       break;
787     }
788     case Instruction::Br: {
789       const BranchInst* br = cast<BranchInst>(I);
790       Out << "    BranchInst* " << iName << " = new BranchInst(" ;
791       if (br->getNumOperands() == 3 ) {
792         Out << getCppName(br->getOperand(0)) << ", " 
793             << getCppName(br->getOperand(1)) << ", "
794             << getCppName(br->getOperand(2)) << ", ";
795
796       } else if (br->getNumOperands() == 1) {
797         Out << getCppName(br->getOperand(0)) << ", ";
798       } else {
799         assert(!"branch with 2 operands?");
800       }
801       Out << bbname << ");";
802       break;
803     }
804     case Instruction::Switch:
805     case Instruction::Invoke:
806     case Instruction::Unwind:
807     case Instruction::Unreachable:
808     case Instruction::Add:
809     case Instruction::Sub:
810     case Instruction::Mul:
811     case Instruction::Div:
812     case Instruction::Rem:
813     case Instruction::And:
814     case Instruction::Or:
815     case Instruction::Xor:
816     case Instruction::SetEQ:
817     case Instruction::SetNE:
818     case Instruction::SetLE:
819     case Instruction::SetGE:
820     case Instruction::SetLT:
821     case Instruction::SetGT:
822         break;
823     case Instruction::Malloc: {
824       const MallocInst* mallocI = cast<MallocInst>(I);
825       Out << "    MallocInst* " << iName << " = new MallocInst("
826           << getCppName(mallocI->getAllocatedType()) << ", ";
827       if (mallocI->isArrayAllocation())
828         Out << getCppName(mallocI->getArraySize()) << ", ";
829       Out << "\"";
830       printEscapedString(mallocI->getName());
831       Out << "\", " << bbname << ");";
832       if (mallocI->getAlignment())
833         Out << "\n    " << iName << "->setAlignment(" 
834             << mallocI->getAlignment() << ");";
835       break;
836     }
837     case Instruction::Free:
838     case Instruction::Alloca: {
839       const AllocaInst* allocaI = cast<AllocaInst>(I);
840       Out << "    AllocaInst* " << iName << " = new AllocaInst("
841           << getCppName(allocaI->getAllocatedType()) << ", ";
842       if (allocaI->isArrayAllocation())
843         Out << getCppName(allocaI->getArraySize()) << ", ";
844       Out << "\"";
845       printEscapedString(allocaI->getName());
846       Out << "\", " << bbname << ");";
847       if (allocaI->getAlignment())
848         Out << "\n    " << iName << "->setAlignment(" 
849             << allocaI->getAlignment() << ");";
850       break;
851     }
852     case Instruction::Load:
853         break;
854     case Instruction::Store: {
855       const StoreInst* store = cast<StoreInst>(I);
856       Out << "    StoreInst* " << iName << " = new StoreInst(" 
857           << getCppName(store->getOperand(0)) << ", "
858           << getCppName(store->getOperand(1)) << ", " << bbname << ");\n";
859       if (store->isVolatile()) 
860         Out << "iName->setVolatile(true);";
861       break;
862     }
863     case Instruction::GetElementPtr: {
864       const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
865       if (gep->getNumOperands() <= 2) {
866         Out << "    GetElementPtrInst* " << iName << " = new GetElementPtrInst("
867             << getCppName(gep->getOperand(0)); 
868         if (gep->getNumOperands() == 2)
869           Out << ", " << getCppName(gep->getOperand(1));
870         Out << ", " << bbname;
871       } else {
872         Out << "    std::vector<Value*> " << iName << "_indices;\n";
873         for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
874           Out << "    " << iName << "_indices.push_back("
875               << getCppName(gep->getOperand(i)) << ");\n";
876         }
877         Out << "    Instruction* " << iName << " = new GetElementPtrInst(" 
878             << getCppName(gep->getOperand(0)) << ", " << iName << "_indices";
879       }
880       Out << ", \"";
881       printEscapedString(gep->getName());
882       Out << "\", " << bbname << ");";
883       break;
884     }
885     case Instruction::PHI:
886     case Instruction::Cast:
887     case Instruction::Call:
888     case Instruction::Shl:
889     case Instruction::Shr:
890     case Instruction::Select:
891     case Instruction::UserOp1:
892     case Instruction::UserOp2:
893     case Instruction::VAArg:
894     case Instruction::ExtractElement:
895     case Instruction::InsertElement:
896     case Instruction::ShuffleVector:
897       break;
898   }
899   Out << "\n";
900
901 /*
902   // Print out name if it exists...
903   if (I.hasName())
904     Out << getLLVMName(I.getName()) << " = ";
905
906   // If this is a volatile load or store, print out the volatile marker.
907   if ((isa<LoadInst>(I)  && cast<LoadInst>(I).isVolatile()) ||
908       (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
909       Out << "volatile ";
910   } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
911     // If this is a call, check if it's a tail call.
912     Out << "tail ";
913   }
914
915   // Print out the opcode...
916   Out << I.getOpcodeName();
917
918   // Print out the type of the operands...
919   const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
920
921   // Special case conditional branches to swizzle the condition out to the front
922   if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
923     writeOperand(I.getOperand(2), true);
924     Out << ',';
925     writeOperand(Operand, true);
926     Out << ',';
927     writeOperand(I.getOperand(1), true);
928
929   } else if (isa<SwitchInst>(I)) {
930     // Special case switch statement to get formatting nice and correct...
931     writeOperand(Operand        , true); Out << ',';
932     writeOperand(I.getOperand(1), true); Out << " [";
933
934     for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
935       Out << "\n\t\t";
936       writeOperand(I.getOperand(op  ), true); Out << ',';
937       writeOperand(I.getOperand(op+1), true);
938     }
939     Out << "\n\t]";
940   } else if (isa<PHINode>(I)) {
941     Out << ' ';
942     printType(I.getType());
943     Out << ' ';
944
945     for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
946       if (op) Out << ", ";
947       Out << '[';
948       writeOperand(I.getOperand(op  ), false); Out << ',';
949       writeOperand(I.getOperand(op+1), false); Out << " ]";
950     }
951   } else if (isa<ReturnInst>(I) && !Operand) {
952     Out << " void";
953   } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
954     // Print the calling convention being used.
955     switch (CI->getCallingConv()) {
956     case CallingConv::C: break;   // default
957     case CallingConv::CSRet: Out << " csretcc"; break;
958     case CallingConv::Fast:  Out << " fastcc"; break;
959     case CallingConv::Cold:  Out << " coldcc"; break;
960     default: Out << " cc" << CI->getCallingConv(); break;
961     }
962
963     const PointerType  *PTy = cast<PointerType>(Operand->getType());
964     const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
965     const Type       *RetTy = FTy->getReturnType();
966
967     // If possible, print out the short form of the call instruction.  We can
968     // only do this if the first argument is a pointer to a nonvararg function,
969     // and if the return type is not a pointer to a function.
970     //
971     if (!FTy->isVarArg() &&
972         (!isa<PointerType>(RetTy) ||
973          !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
974       Out << ' '; printType(RetTy);
975       writeOperand(Operand, false);
976     } else {
977       writeOperand(Operand, true);
978     }
979     Out << '(';
980     if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true);
981     for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
982       Out << ',';
983       writeOperand(I.getOperand(op), true);
984     }
985
986     Out << " )";
987   } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
988     const PointerType  *PTy = cast<PointerType>(Operand->getType());
989     const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
990     const Type       *RetTy = FTy->getReturnType();
991
992     // Print the calling convention being used.
993     switch (II->getCallingConv()) {
994     case CallingConv::C: break;   // default
995     case CallingConv::CSRet: Out << " csretcc"; break;
996     case CallingConv::Fast:  Out << " fastcc"; break;
997     case CallingConv::Cold:  Out << " coldcc"; break;
998     default: Out << " cc" << II->getCallingConv(); break;
999     }
1000
1001     // If possible, print out the short form of the invoke instruction. We can
1002     // only do this if the first argument is a pointer to a nonvararg function,
1003     // and if the return type is not a pointer to a function.
1004     //
1005     if (!FTy->isVarArg() &&
1006         (!isa<PointerType>(RetTy) ||
1007          !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
1008       Out << ' '; printType(RetTy);
1009       writeOperand(Operand, false);
1010     } else {
1011       writeOperand(Operand, true);
1012     }
1013
1014     Out << '(';
1015     if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1016     for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
1017       Out << ',';
1018       writeOperand(I.getOperand(op), true);
1019     }
1020
1021     Out << " )\n\t\t\tto";
1022     writeOperand(II->getNormalDest(), true);
1023     Out << " unwind";
1024     writeOperand(II->getUnwindDest(), true);
1025
1026   } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
1027     Out << ' ';
1028     printType(AI->getType()->getElementType());
1029     if (AI->isArrayAllocation()) {
1030       Out << ',';
1031       writeOperand(AI->getArraySize(), true);
1032     }
1033     if (AI->getAlignment()) {
1034       Out << ", align " << AI->getAlignment();
1035     }
1036   } else if (isa<CastInst>(I)) {
1037     if (Operand) writeOperand(Operand, true);   // Work with broken code
1038     Out << " to ";
1039     printType(I.getType());
1040   } else if (isa<VAArgInst>(I)) {
1041     if (Operand) writeOperand(Operand, true);   // Work with broken code
1042     Out << ", ";
1043     printType(I.getType());
1044   } else if (Operand) {   // Print the normal way...
1045
1046     // PrintAllTypes - Instructions who have operands of all the same type
1047     // omit the type from all but the first operand.  If the instruction has
1048     // different type operands (for example br), then they are all printed.
1049     bool PrintAllTypes = false;
1050     const Type *TheType = Operand->getType();
1051
1052     // Shift Left & Right print both types even for Ubyte LHS, and select prints
1053     // types even if all operands are bools.
1054     if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1055         isa<ShuffleVectorInst>(I)) {
1056       PrintAllTypes = true;
1057     } else {
1058       for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1059         Operand = I.getOperand(i);
1060         if (Operand->getType() != TheType) {
1061           PrintAllTypes = true;    // We have differing types!  Print them all!
1062           break;
1063         }
1064       }
1065     }
1066
1067     if (!PrintAllTypes) {
1068       Out << ' ';
1069       printType(TheType);
1070     }
1071
1072     for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
1073       if (i) Out << ',';
1074       writeOperand(I.getOperand(i), PrintAllTypes);
1075     }
1076   }
1077
1078   Out << "\n";
1079 */
1080 }
1081
1082 }  // end anonymous llvm
1083
1084 namespace llvm {
1085
1086 void WriteModuleToCppFile(Module* mod, std::ostream& o) {
1087   o << "#include <llvm/Module.h>\n";
1088   o << "#include <llvm/DerivedTypes.h>\n";
1089   o << "#include <llvm/Constants.h>\n";
1090   o << "#include <llvm/GlobalVariable.h>\n";
1091   o << "#include <llvm/Function.h>\n";
1092   o << "#include <llvm/CallingConv.h>\n";
1093   o << "#include <llvm/BasicBlock.h>\n";
1094   o << "#include <llvm/Instructions.h>\n";
1095   o << "#include <llvm/Pass.h>\n";
1096   o << "#include <llvm/PassManager.h>\n";
1097   o << "#include <llvm/Analysis/Verifier.h>\n";
1098   o << "#include <llvm/Assembly/PrintModulePass.h>\n";
1099   o << "#include <algorithm>\n";
1100   o << "#include <iostream>\n\n";
1101   o << "using namespace llvm;\n\n";
1102   o << "Module* makeLLVMModule();\n\n";
1103   o << "int main(int argc, char**argv) {\n";
1104   o << "  Module* Mod = makeLLVMModule();\n";
1105   o << "  verifyModule(*Mod, PrintMessageAction);\n";
1106   o << "  std::cerr.flush();\n";
1107   o << "  std::cout.flush();\n";
1108   o << "  PassManager PM;\n";
1109   o << "  PM.add(new PrintModulePass(&std::cout));\n";
1110   o << "  PM.run(*Mod);\n";
1111   o << "  return 0;\n";
1112   o << "}\n\n";
1113   o << "Module* makeLLVMModule() {\n";
1114   CppWriter W(o, mod);
1115   W.printModule(mod);
1116   o << "return mod;\n";
1117   o << "}\n";
1118 }
1119
1120 }