| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1 | //===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===// | 
 | 2 | // | 
 | 3 | //                     The LLVM Compiler Infrastructure | 
 | 4 | // | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 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. | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 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" | 
| Reid Spencer | 6abd3da | 2007-04-11 09:54:08 +0000 | [diff] [blame] | 21 | #include "llvm/ParameterAttributes.h" | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" | 
| Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 23 | #include "llvm/TypeSymbolTable.h" | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" | 
 | 25 | #include "llvm/ADT/STLExtras.h" | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" | 
| Chris Lattner | c30598b | 2006-12-06 01:18:01 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CFG.h" | 
 | 28 | #include "llvm/Support/ManagedStatic.h" | 
 | 29 | #include "llvm/Support/MathExtras.h" | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 30 | #include "llvm/Config/config.h" | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 31 | #include <algorithm> | 
 | 32 | #include <iostream> | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 33 | #include <set> | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 34 |  | 
 | 35 | using namespace llvm; | 
 | 36 |  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 37 | static cl::opt<std::string> | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 38 | FuncName("funcname", cl::desc("Specify the name of the generated function"), | 
 | 39 |          cl::value_desc("function name")); | 
 | 40 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 41 | enum WhatToGenerate { | 
 | 42 |   GenProgram, | 
 | 43 |   GenModule, | 
 | 44 |   GenContents, | 
 | 45 |   GenFunction, | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 46 |   GenInline, | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 47 |   GenVariable, | 
 | 48 |   GenType | 
 | 49 | }; | 
 | 50 |  | 
 | 51 | static cl::opt<WhatToGenerate> GenerationType(cl::Optional, | 
 | 52 |   cl::desc("Choose what kind of output to generate"), | 
 | 53 |   cl::init(GenProgram), | 
 | 54 |   cl::values( | 
 | 55 |     clEnumValN(GenProgram, "gen-program",  "Generate a complete program"), | 
 | 56 |     clEnumValN(GenModule,  "gen-module",   "Generate a module definition"), | 
 | 57 |     clEnumValN(GenContents,"gen-contents", "Generate contents of a module"), | 
 | 58 |     clEnumValN(GenFunction,"gen-function", "Generate a function definition"), | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 59 |     clEnumValN(GenInline,  "gen-inline",   "Generate an inline function"), | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 60 |     clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"), | 
 | 61 |     clEnumValN(GenType,    "gen-type",     "Generate a type definition"), | 
 | 62 |     clEnumValEnd | 
 | 63 |   ) | 
 | 64 | ); | 
 | 65 |  | 
 | 66 | static cl::opt<std::string> NameToGenerate("for", cl::Optional, | 
 | 67 |   cl::desc("Specify the name of the thing to generate"), | 
 | 68 |   cl::init("!bad!")); | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 69 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 70 | namespace { | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 71 | typedef std::vector<const Type*> TypeList; | 
 | 72 | typedef std::map<const Type*,std::string> TypeMap; | 
 | 73 | typedef std::map<const Value*,std::string> ValueMap; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 74 | typedef std::set<std::string> NameSet; | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 75 | typedef std::set<const Type*> TypeSet; | 
 | 76 | typedef std::set<const Value*> ValueSet; | 
 | 77 | typedef std::map<const Value*,std::string> ForwardRefMap; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 78 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 79 | class CppWriter { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 80 |   const char* progname; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 81 |   std::ostream &Out; | 
 | 82 |   const Module *TheModule; | 
| Andrew Lenharth | 539d871 | 2006-05-31 20:18:56 +0000 | [diff] [blame] | 83 |   uint64_t uniqueNum; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 84 |   TypeMap TypeNames; | 
 | 85 |   ValueMap ValueNames; | 
 | 86 |   TypeMap UnresolvedTypes; | 
 | 87 |   TypeList TypeStack; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 88 |   NameSet UsedNames; | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 89 |   TypeSet DefinedTypes; | 
 | 90 |   ValueSet DefinedValues; | 
 | 91 |   ForwardRefMap ForwardRefs; | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 92 |   bool is_inline; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 93 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 94 | public: | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 95 |   inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp") | 
 | 96 |     : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(), | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 97 |       ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { } | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 98 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 99 |   const Module* getModule() { return TheModule; } | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 100 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 101 |   void printProgram(const std::string& fname, const std::string& modName ); | 
 | 102 |   void printModule(const std::string& fname, const std::string& modName ); | 
 | 103 |   void printContents(const std::string& fname, const std::string& modName ); | 
 | 104 |   void printFunction(const std::string& fname, const std::string& funcName ); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 105 |   void printInline(const std::string& fname, const std::string& funcName ); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 106 |   void printVariable(const std::string& fname, const std::string& varName ); | 
 | 107 |   void printType(const std::string& fname, const std::string& typeName ); | 
 | 108 |  | 
 | 109 |   void error(const std::string& msg); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 110 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 111 | private: | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 112 |   void printLinkageType(GlobalValue::LinkageTypes LT); | 
 | 113 |   void printCallingConv(unsigned cc); | 
 | 114 |   void printEscapedString(const std::string& str); | 
 | 115 |   void printCFP(const ConstantFP* CFP); | 
 | 116 |  | 
 | 117 |   std::string getCppName(const Type* val); | 
 | 118 |   inline void printCppName(const Type* val); | 
 | 119 |  | 
 | 120 |   std::string getCppName(const Value* val); | 
 | 121 |   inline void printCppName(const Value* val); | 
 | 122 |  | 
 | 123 |   bool printTypeInternal(const Type* Ty); | 
 | 124 |   inline void printType(const Type* Ty); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 125 |   void printTypes(const Module* M); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 126 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 127 |   void printConstant(const Constant *CPV); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 128 |   void printConstants(const Module* M); | 
 | 129 |  | 
 | 130 |   void printVariableUses(const GlobalVariable *GV); | 
 | 131 |   void printVariableHead(const GlobalVariable *GV); | 
 | 132 |   void printVariableBody(const GlobalVariable *GV); | 
 | 133 |  | 
 | 134 |   void printFunctionUses(const Function *F); | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 135 |   void printFunctionHead(const Function *F); | 
 | 136 |   void printFunctionBody(const Function *F); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 137 |   void printInstruction(const Instruction *I, const std::string& bbname); | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 138 |   std::string getOpName(Value*); | 
 | 139 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 140 |   void printModuleBody(); | 
 | 141 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 142 | }; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 143 |  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 144 | static unsigned indent_level = 0; | 
 | 145 | inline std::ostream& nl(std::ostream& Out, int delta = 0) { | 
 | 146 |   Out << "\n"; | 
 | 147 |   if (delta >= 0 || indent_level >= unsigned(-delta)) | 
 | 148 |     indent_level += delta; | 
 | 149 |   for (unsigned i = 0; i < indent_level; ++i)  | 
 | 150 |     Out << "  "; | 
 | 151 |   return Out; | 
 | 152 | } | 
 | 153 |  | 
 | 154 | inline void in() { indent_level++; } | 
 | 155 | inline void out() { if (indent_level >0) indent_level--; } | 
 | 156 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 157 | inline void | 
 | 158 | sanitize(std::string& str) { | 
 | 159 |   for (size_t i = 0; i < str.length(); ++i) | 
 | 160 |     if (!isalnum(str[i]) && str[i] != '_') | 
 | 161 |       str[i] = '_'; | 
 | 162 | } | 
 | 163 |  | 
| Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 164 | inline std::string | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 165 | getTypePrefix(const Type* Ty ) { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 166 |   switch (Ty->getTypeID()) { | 
| Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 167 |     case Type::VoidTyID:     return "void_"; | 
 | 168 |     case Type::IntegerTyID:   | 
 | 169 |       return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) + | 
 | 170 |         "_"; | 
 | 171 |     case Type::FloatTyID:    return "float_";  | 
 | 172 |     case Type::DoubleTyID:   return "double_";  | 
 | 173 |     case Type::LabelTyID:    return "label_";  | 
 | 174 |     case Type::FunctionTyID: return "func_";  | 
 | 175 |     case Type::StructTyID:   return "struct_";  | 
 | 176 |     case Type::ArrayTyID:    return "array_";  | 
 | 177 |     case Type::PointerTyID:  return "ptr_";  | 
| Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 178 |     case Type::VectorTyID:   return "packed_";  | 
| Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 179 |     case Type::OpaqueTyID:   return "opaque_";  | 
 | 180 |     default:                 return "other_";  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 181 |   } | 
| Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 182 |   return "unknown_"; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 183 | } | 
 | 184 |  | 
 | 185 | // Looks up the type in the symbol table and returns a pointer to its name or | 
 | 186 | // a null pointer if it wasn't found. Note that this isn't the same as the | 
 | 187 | // Mode::getTypeName function which will return an empty string, not a null | 
 | 188 | // pointer if the name is not found. | 
 | 189 | inline const std::string*  | 
| Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 190 | findTypeName(const TypeSymbolTable& ST, const Type* Ty) | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 191 | { | 
| Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 192 |   TypeSymbolTable::const_iterator TI = ST.begin(); | 
 | 193 |   TypeSymbolTable::const_iterator TE = ST.end(); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 194 |   for (;TI != TE; ++TI) | 
 | 195 |     if (TI->second == Ty) | 
 | 196 |       return &(TI->first); | 
 | 197 |   return 0; | 
 | 198 | } | 
 | 199 |  | 
 | 200 | void | 
 | 201 | CppWriter::error(const std::string& msg) { | 
 | 202 |   std::cerr << progname << ": " << msg << "\n"; | 
 | 203 |   exit(2); | 
 | 204 | } | 
 | 205 |  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 206 | // printCFP - Print a floating point constant .. very carefully :) | 
 | 207 | // This makes sure that conversion to/from floating yields the same binary | 
 | 208 | // result so that we don't lose precision. | 
 | 209 | void  | 
 | 210 | CppWriter::printCFP(const ConstantFP *CFP) { | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 211 |   Out << "ConstantFP::get("; | 
 | 212 |   if (CFP->getType() == Type::DoubleTy) | 
 | 213 |     Out << "Type::DoubleTy, "; | 
 | 214 |   else | 
 | 215 |     Out << "Type::FloatTy, "; | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 216 | #if HAVE_PRINTF_A | 
 | 217 |   char Buffer[100]; | 
 | 218 |   sprintf(Buffer, "%A", CFP->getValue()); | 
 | 219 |   if ((!strncmp(Buffer, "0x", 2) || | 
 | 220 |        !strncmp(Buffer, "-0x", 3) || | 
 | 221 |        !strncmp(Buffer, "+0x", 3)) && | 
 | 222 |       (atof(Buffer) == CFP->getValue())) | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 223 |     if (CFP->getType() == Type::DoubleTy) | 
 | 224 |       Out << "BitsToDouble(" << Buffer << ")"; | 
 | 225 |     else | 
 | 226 |       Out << "BitsToFloat(" << Buffer << ")"; | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 227 |   else { | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 228 | #endif | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 229 |     std::string StrVal = ftostr(CFP->getValue()); | 
 | 230 |  | 
 | 231 |     while (StrVal[0] == ' ') | 
 | 232 |       StrVal.erase(StrVal.begin()); | 
 | 233 |  | 
 | 234 |     // Check to make sure that the stringized number is not some string like  | 
 | 235 |     // "Inf" or NaN.  Check that the string matches the "[-+]?[0-9]" regex. | 
 | 236 |     if (((StrVal[0] >= '0' && StrVal[0] <= '9') || | 
 | 237 |         ((StrVal[0] == '-' || StrVal[0] == '+') && | 
 | 238 |          (StrVal[1] >= '0' && StrVal[1] <= '9'))) && | 
 | 239 |         (atof(StrVal.c_str()) == CFP->getValue())) | 
 | 240 |       if (CFP->getType() == Type::DoubleTy) | 
 | 241 |         Out <<  StrVal; | 
 | 242 |       else | 
 | 243 |         Out << StrVal; | 
 | 244 |     else if (CFP->getType() == Type::DoubleTy) | 
 | 245 |       Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())  | 
 | 246 |           << std::dec << "ULL) /* " << StrVal << " */"; | 
 | 247 |     else  | 
 | 248 |       Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())  | 
 | 249 |           << std::dec << "U) /* " << StrVal << " */"; | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 250 | #if HAVE_PRINTF_A | 
 | 251 |   } | 
 | 252 | #endif | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 253 |   Out << ")"; | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 254 | } | 
 | 255 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 256 | void | 
 | 257 | CppWriter::printCallingConv(unsigned cc){ | 
 | 258 |   // Print the calling convention. | 
 | 259 |   switch (cc) { | 
 | 260 |     case CallingConv::C:     Out << "CallingConv::C"; break; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 261 |     case CallingConv::Fast:  Out << "CallingConv::Fast"; break; | 
 | 262 |     case CallingConv::Cold:  Out << "CallingConv::Cold"; break; | 
 | 263 |     case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break; | 
 | 264 |     default:                 Out << cc; break; | 
 | 265 |   } | 
 | 266 | } | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 267 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 268 | void  | 
 | 269 | CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) { | 
 | 270 |   switch (LT) { | 
 | 271 |     case GlobalValue::InternalLinkage:   | 
 | 272 |       Out << "GlobalValue::InternalLinkage"; break; | 
 | 273 |     case GlobalValue::LinkOnceLinkage:   | 
 | 274 |       Out << "GlobalValue::LinkOnceLinkage "; break; | 
 | 275 |     case GlobalValue::WeakLinkage:       | 
 | 276 |       Out << "GlobalValue::WeakLinkage"; break; | 
 | 277 |     case GlobalValue::AppendingLinkage:  | 
 | 278 |       Out << "GlobalValue::AppendingLinkage"; break; | 
 | 279 |     case GlobalValue::ExternalLinkage:  | 
 | 280 |       Out << "GlobalValue::ExternalLinkage"; break; | 
| Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 281 |     case GlobalValue::DLLImportLinkage:  | 
 | 282 |       Out << "GlobalValue::DllImportLinkage"; break; | 
 | 283 |     case GlobalValue::DLLExportLinkage:  | 
 | 284 |       Out << "GlobalValue::DllExportLinkage"; break; | 
 | 285 |     case GlobalValue::ExternalWeakLinkage:  | 
 | 286 |       Out << "GlobalValue::ExternalWeakLinkage"; break; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 287 |     case GlobalValue::GhostLinkage: | 
 | 288 |       Out << "GlobalValue::GhostLinkage"; break; | 
 | 289 |   } | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 290 | } | 
 | 291 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 292 | // printEscapedString - Print each character of the specified string, escaping | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 293 | // it if it is not printable or if it is an escape char. | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 294 | void  | 
 | 295 | CppWriter::printEscapedString(const std::string &Str) { | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 296 |   for (unsigned i = 0, e = Str.size(); i != e; ++i) { | 
 | 297 |     unsigned char C = Str[i]; | 
 | 298 |     if (isprint(C) && C != '"' && C != '\\') { | 
 | 299 |       Out << C; | 
 | 300 |     } else { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 301 |       Out << "\\x" | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 302 |           << (char) ((C/16  < 10) ? ( C/16 +'0') : ( C/16 -10+'A')) | 
 | 303 |           << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A')); | 
 | 304 |     } | 
 | 305 |   } | 
 | 306 | } | 
 | 307 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 308 | std::string | 
 | 309 | CppWriter::getCppName(const Type* Ty) | 
 | 310 | { | 
 | 311 |   // First, handle the primitive types .. easy | 
| Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 312 |   if (Ty->isPrimitiveType() || Ty->isInteger()) { | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 313 |     switch (Ty->getTypeID()) { | 
| Reid Spencer | 71d2ec9 | 2006-12-31 06:02:26 +0000 | [diff] [blame] | 314 |       case Type::VoidTyID:   return "Type::VoidTy"; | 
| Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 315 |       case Type::IntegerTyID: { | 
 | 316 |         unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth(); | 
 | 317 |         return "IntegerType::get(" + utostr(BitWidth) + ")"; | 
 | 318 |       } | 
| Reid Spencer | 71d2ec9 | 2006-12-31 06:02:26 +0000 | [diff] [blame] | 319 |       case Type::FloatTyID:  return "Type::FloatTy"; | 
 | 320 |       case Type::DoubleTyID: return "Type::DoubleTy"; | 
 | 321 |       case Type::LabelTyID:  return "Type::LabelTy"; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 322 |       default: | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 323 |         error("Invalid primitive type"); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 324 |         break; | 
 | 325 |     } | 
 | 326 |     return "Type::VoidTy"; // shouldn't be returned, but make it sensible | 
 | 327 |   } | 
 | 328 |  | 
 | 329 |   // Now, see if we've seen the type before and return that | 
 | 330 |   TypeMap::iterator I = TypeNames.find(Ty); | 
 | 331 |   if (I != TypeNames.end()) | 
 | 332 |     return I->second; | 
 | 333 |  | 
 | 334 |   // Okay, let's build a new name for this type. Start with a prefix | 
 | 335 |   const char* prefix = 0; | 
 | 336 |   switch (Ty->getTypeID()) { | 
 | 337 |     case Type::FunctionTyID:    prefix = "FuncTy_"; break; | 
 | 338 |     case Type::StructTyID:      prefix = "StructTy_"; break; | 
 | 339 |     case Type::ArrayTyID:       prefix = "ArrayTy_"; break; | 
 | 340 |     case Type::PointerTyID:     prefix = "PointerTy_"; break; | 
 | 341 |     case Type::OpaqueTyID:      prefix = "OpaqueTy_"; break; | 
| Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 342 |     case Type::VectorTyID:      prefix = "VectorTy_"; break; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 343 |     default:                    prefix = "OtherTy_"; break; // prevent breakage | 
 | 344 |   } | 
 | 345 |  | 
 | 346 |   // See if the type has a name in the symboltable and build accordingly | 
| Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 347 |   const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 348 |   std::string name; | 
 | 349 |   if (tName)  | 
 | 350 |     name = std::string(prefix) + *tName; | 
 | 351 |   else | 
 | 352 |     name = std::string(prefix) + utostr(uniqueNum++); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 353 |   sanitize(name); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 354 |  | 
 | 355 |   // Save the name | 
 | 356 |   return TypeNames[Ty] = name; | 
 | 357 | } | 
 | 358 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 359 | void | 
 | 360 | CppWriter::printCppName(const Type* Ty) | 
 | 361 | { | 
 | 362 |   printEscapedString(getCppName(Ty)); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 363 | } | 
 | 364 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 365 | std::string | 
 | 366 | CppWriter::getCppName(const Value* val) { | 
 | 367 |   std::string name; | 
 | 368 |   ValueMap::iterator I = ValueNames.find(val); | 
 | 369 |   if (I != ValueNames.end() && I->first == val) | 
 | 370 |     return  I->second; | 
| Reid Spencer | 25edc35 | 2006-05-31 04:43:19 +0000 | [diff] [blame] | 371 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 372 |   if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) { | 
 | 373 |     name = std::string("gvar_") +  | 
 | 374 |            getTypePrefix(GV->getType()->getElementType()); | 
| Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 375 |   } else if (isa<Function>(val)) { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 376 |     name = std::string("func_"); | 
 | 377 |   } else if (const Constant* C = dyn_cast<Constant>(val)) { | 
 | 378 |     name = std::string("const_") + getTypePrefix(C->getType()); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 379 |   } else if (const Argument* Arg = dyn_cast<Argument>(val)) { | 
 | 380 |     if (is_inline) { | 
 | 381 |       unsigned argNum = std::distance(Arg->getParent()->arg_begin(), | 
 | 382 |           Function::const_arg_iterator(Arg)) + 1; | 
 | 383 |       name = std::string("arg_") + utostr(argNum); | 
 | 384 |       NameSet::iterator NI = UsedNames.find(name); | 
 | 385 |       if (NI != UsedNames.end()) | 
 | 386 |         name += std::string("_") + utostr(uniqueNum++); | 
 | 387 |       UsedNames.insert(name); | 
 | 388 |       return ValueNames[val] = name; | 
 | 389 |     } else { | 
 | 390 |       name = getTypePrefix(val->getType()); | 
 | 391 |     } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 392 |   } else { | 
 | 393 |     name = getTypePrefix(val->getType()); | 
| Reid Spencer | 25edc35 | 2006-05-31 04:43:19 +0000 | [diff] [blame] | 394 |   } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 395 |   name += (val->hasName() ? val->getName() : utostr(uniqueNum++)); | 
 | 396 |   sanitize(name); | 
 | 397 |   NameSet::iterator NI = UsedNames.find(name); | 
 | 398 |   if (NI != UsedNames.end()) | 
 | 399 |     name += std::string("_") + utostr(uniqueNum++); | 
 | 400 |   UsedNames.insert(name); | 
 | 401 |   return ValueNames[val] = name; | 
| Reid Spencer | 25edc35 | 2006-05-31 04:43:19 +0000 | [diff] [blame] | 402 | } | 
 | 403 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 404 | void | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 405 | CppWriter::printCppName(const Value* val) { | 
 | 406 |   printEscapedString(getCppName(val)); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 407 | } | 
 | 408 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 409 | bool | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 410 | CppWriter::printTypeInternal(const Type* Ty) { | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 411 |   // We don't print definitions for primitive types | 
| Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 412 |   if (Ty->isPrimitiveType() || Ty->isInteger()) | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 413 |     return false; | 
 | 414 |  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 415 |   // If we already defined this type, we don't need to define it again. | 
 | 416 |   if (DefinedTypes.find(Ty) != DefinedTypes.end()) | 
 | 417 |     return false; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 418 |  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 419 |   // Everything below needs the name for the type so get it now. | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 420 |   std::string typeName(getCppName(Ty)); | 
 | 421 |  | 
 | 422 |   // Search the type stack for recursion. If we find it, then generate this | 
 | 423 |   // as an OpaqueType, but make sure not to do this multiple times because | 
 | 424 |   // the type could appear in multiple places on the stack. Once the opaque | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 425 |   // definition is issued, it must not be re-issued. Consequently we have to | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 426 |   // check the UnresolvedTypes list as well. | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 427 |   TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty); | 
 | 428 |   if (TI != TypeStack.end()) { | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 429 |     TypeMap::const_iterator I = UnresolvedTypes.find(Ty); | 
 | 430 |     if (I == UnresolvedTypes.end()) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 431 |       Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();"; | 
 | 432 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 433 |       UnresolvedTypes[Ty] = typeName; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 434 |     } | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 435 |     return true; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 436 |   } | 
 | 437 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 438 |   // We're going to print a derived type which, by definition, contains other | 
 | 439 |   // types. So, push this one we're printing onto the type stack to assist with | 
 | 440 |   // recursive definitions. | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 441 |   TypeStack.push_back(Ty); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 442 |  | 
 | 443 |   // Print the type definition | 
 | 444 |   switch (Ty->getTypeID()) { | 
 | 445 |     case Type::FunctionTyID:  { | 
 | 446 |       const FunctionType* FT = cast<FunctionType>(Ty); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 447 |       Out << "std::vector<const Type*>" << typeName << "_args;"; | 
 | 448 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 449 |       FunctionType::param_iterator PI = FT->param_begin(); | 
 | 450 |       FunctionType::param_iterator PE = FT->param_end(); | 
 | 451 |       for (; PI != PE; ++PI) { | 
 | 452 |         const Type* argTy = static_cast<const Type*>(*PI); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 453 |         bool isForward = printTypeInternal(argTy); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 454 |         std::string argName(getCppName(argTy)); | 
 | 455 |         Out << typeName << "_args.push_back(" << argName; | 
 | 456 |         if (isForward) | 
 | 457 |           Out << "_fwd"; | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 458 |         Out << ");"; | 
 | 459 |         nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 460 |       } | 
| Reid Spencer | 6abd3da | 2007-04-11 09:54:08 +0000 | [diff] [blame] | 461 |       const ParamAttrsList *PAL = FT->getParamAttrs(); | 
 | 462 |       Out << "ParamAttrsList *" << typeName << "_PAL = 0;"; | 
| Reid Spencer | a9297b1 | 2007-04-11 10:01:32 +0000 | [diff] [blame] | 463 |       nl(Out); | 
| Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame^] | 464 |       if (PAL) { | 
 | 465 |         Out << '{'; in(); nl(Out); | 
 | 466 |         Out << "ParamAttrsVector Attrs;"; nl(Out); | 
 | 467 |         Out << "ParamAttrsWithIndex PAWI;"; nl(Out); | 
| Reid Spencer | 6abd3da | 2007-04-11 09:54:08 +0000 | [diff] [blame] | 468 |         for (unsigned i = 0; i < PAL->size(); ++i) { | 
 | 469 |           uint16_t index = PAL->getParamIndex(i); | 
 | 470 |           uint16_t attrs = PAL->getParamAttrs(index); | 
| Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame^] | 471 |           Out << "PAWI.index = " << index << "; PAWI.attrs = 0 "; | 
| Reid Spencer | 6abd3da | 2007-04-11 09:54:08 +0000 | [diff] [blame] | 472 |           if (attrs & ParamAttr::SExt) | 
 | 473 |             Out << " | ParamAttr::SExt"; | 
 | 474 |           if (attrs & ParamAttr::ZExt) | 
 | 475 |             Out << " | ParamAttr::ZExt"; | 
 | 476 |           if (attrs & ParamAttr::StructRet) | 
 | 477 |             Out << " | ParamAttr::StructRet"; | 
 | 478 |           if (attrs & ParamAttr::InReg) | 
 | 479 |             Out << " | ParamAttr::InReg"; | 
 | 480 |           if (attrs & ParamAttr::NoReturn) | 
 | 481 |             Out << " | ParamAttr::NoReturn"; | 
 | 482 |           if (attrs & ParamAttr::NoUnwind) | 
 | 483 |             Out << " | ParamAttr::NoUnwind"; | 
| Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame^] | 484 |           Out << ";"; | 
 | 485 |           nl(Out); | 
 | 486 |           Out << "Attrs.push_back(PAWI);"; | 
| Reid Spencer | a9297b1 | 2007-04-11 10:01:32 +0000 | [diff] [blame] | 487 |           nl(Out); | 
| Reid Spencer | 6abd3da | 2007-04-11 09:54:08 +0000 | [diff] [blame] | 488 |         } | 
| Reid Spencer | 4f859aa | 2007-04-22 05:46:44 +0000 | [diff] [blame^] | 489 |         Out << typeName << "_PAL = ParamAttrsList::get(Attrs);"; | 
 | 490 |         nl(Out); | 
 | 491 |         out(); nl(Out); | 
 | 492 |         Out << '}'; nl(Out); | 
| Reid Spencer | 6abd3da | 2007-04-11 09:54:08 +0000 | [diff] [blame] | 493 |       } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 494 |       bool isForward = printTypeInternal(FT->getReturnType()); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 495 |       std::string retTypeName(getCppName(FT->getReturnType())); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 496 |       Out << "FunctionType* " << typeName << " = FunctionType::get("; | 
 | 497 |       in(); nl(Out) << "/*Result=*/" << retTypeName; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 498 |       if (isForward) | 
 | 499 |         Out << "_fwd"; | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 500 |       Out << ","; | 
 | 501 |       nl(Out) << "/*Params=*/" << typeName << "_args,"; | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 502 |       nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true," : "false,") ; | 
| Reid Spencer | a9297b1 | 2007-04-11 10:01:32 +0000 | [diff] [blame] | 503 |       nl(Out) << "/*ParamAttrs=*/" << typeName << "_PAL" << ");"; | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 504 |       out();  | 
 | 505 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 506 |       break; | 
 | 507 |     } | 
 | 508 |     case Type::StructTyID: { | 
 | 509 |       const StructType* ST = cast<StructType>(Ty); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 510 |       Out << "std::vector<const Type*>" << typeName << "_fields;"; | 
 | 511 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 512 |       StructType::element_iterator EI = ST->element_begin(); | 
 | 513 |       StructType::element_iterator EE = ST->element_end(); | 
 | 514 |       for (; EI != EE; ++EI) { | 
 | 515 |         const Type* fieldTy = static_cast<const Type*>(*EI); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 516 |         bool isForward = printTypeInternal(fieldTy); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 517 |         std::string fieldName(getCppName(fieldTy)); | 
 | 518 |         Out << typeName << "_fields.push_back(" << fieldName; | 
 | 519 |         if (isForward) | 
 | 520 |           Out << "_fwd"; | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 521 |         Out << ");"; | 
 | 522 |         nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 523 |       } | 
 | 524 |       Out << "StructType* " << typeName << " = StructType::get(" | 
| Reid Spencer | 46fea10 | 2007-04-11 12:41:49 +0000 | [diff] [blame] | 525 |           << typeName << "_fields, /*isPacked=*/" | 
 | 526 |           << (ST->isPacked() ? "true" : "false") << ");"; | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 527 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 528 |       break; | 
 | 529 |     } | 
 | 530 |     case Type::ArrayTyID: { | 
 | 531 |       const ArrayType* AT = cast<ArrayType>(Ty); | 
 | 532 |       const Type* ET = AT->getElementType(); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 533 |       bool isForward = printTypeInternal(ET); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 534 |       std::string elemName(getCppName(ET)); | 
 | 535 |       Out << "ArrayType* " << typeName << " = ArrayType::get(" | 
 | 536 |           << elemName << (isForward ? "_fwd" : "")  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 537 |           << ", " << utostr(AT->getNumElements()) << ");"; | 
 | 538 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 539 |       break; | 
 | 540 |     } | 
 | 541 |     case Type::PointerTyID: { | 
 | 542 |       const PointerType* PT = cast<PointerType>(Ty); | 
 | 543 |       const Type* ET = PT->getElementType(); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 544 |       bool isForward = printTypeInternal(ET); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 545 |       std::string elemName(getCppName(ET)); | 
 | 546 |       Out << "PointerType* " << typeName << " = PointerType::get(" | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 547 |           << elemName << (isForward ? "_fwd" : "") << ");"; | 
 | 548 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 549 |       break; | 
 | 550 |     } | 
| Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 551 |     case Type::VectorTyID: { | 
 | 552 |       const VectorType* PT = cast<VectorType>(Ty); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 553 |       const Type* ET = PT->getElementType(); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 554 |       bool isForward = printTypeInternal(ET); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 555 |       std::string elemName(getCppName(ET)); | 
| Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 556 |       Out << "VectorType* " << typeName << " = VectorType::get(" | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 557 |           << elemName << (isForward ? "_fwd" : "")  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 558 |           << ", " << utostr(PT->getNumElements()) << ");"; | 
 | 559 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 560 |       break; | 
 | 561 |     } | 
 | 562 |     case Type::OpaqueTyID: { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 563 |       Out << "OpaqueType* " << typeName << " = OpaqueType::get();"; | 
 | 564 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 565 |       break; | 
 | 566 |     } | 
 | 567 |     default: | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 568 |       error("Invalid TypeID"); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 569 |   } | 
 | 570 |  | 
| Reid Spencer | 74e032a | 2006-05-29 02:58:15 +0000 | [diff] [blame] | 571 |   // If the type had a name, make sure we recreate it. | 
 | 572 |   const std::string* progTypeName =  | 
| Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 573 |     findTypeName(TheModule->getTypeSymbolTable(),Ty); | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 574 |   if (progTypeName) { | 
| Reid Spencer | 74e032a | 2006-05-29 02:58:15 +0000 | [diff] [blame] | 575 |     Out << "mod->addTypeName(\"" << *progTypeName << "\", "  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 576 |         << typeName << ");"; | 
 | 577 |     nl(Out); | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 578 |   } | 
| Reid Spencer | 74e032a | 2006-05-29 02:58:15 +0000 | [diff] [blame] | 579 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 580 |   // Pop us off the type stack | 
 | 581 |   TypeStack.pop_back(); | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 582 |  | 
 | 583 |   // Indicate that this type is now defined. | 
 | 584 |   DefinedTypes.insert(Ty); | 
 | 585 |  | 
 | 586 |   // Early resolve as many unresolved types as possible. Search the unresolved | 
 | 587 |   // types map for the type we just printed. Now that its definition is complete | 
 | 588 |   // we can resolve any previous references to it. This prevents a cascade of | 
 | 589 |   // unresolved types. | 
 | 590 |   TypeMap::iterator I = UnresolvedTypes.find(Ty); | 
 | 591 |   if (I != UnresolvedTypes.end()) { | 
 | 592 |     Out << "cast<OpaqueType>(" << I->second  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 593 |         << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");"; | 
 | 594 |     nl(Out); | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 595 |     Out << I->second << " = cast<"; | 
 | 596 |     switch (Ty->getTypeID()) { | 
 | 597 |       case Type::FunctionTyID: Out << "FunctionType"; break; | 
 | 598 |       case Type::ArrayTyID:    Out << "ArrayType"; break; | 
 | 599 |       case Type::StructTyID:   Out << "StructType"; break; | 
| Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 600 |       case Type::VectorTyID:   Out << "VectorType"; break; | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 601 |       case Type::PointerTyID:  Out << "PointerType"; break; | 
 | 602 |       case Type::OpaqueTyID:   Out << "OpaqueType"; break; | 
 | 603 |       default:                 Out << "NoSuchDerivedType"; break; | 
 | 604 |     } | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 605 |     Out << ">(" << I->second << "_fwd.get());"; | 
 | 606 |     nl(Out); nl(Out); | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 607 |     UnresolvedTypes.erase(I); | 
 | 608 |   } | 
 | 609 |  | 
 | 610 |   // Finally, separate the type definition from other with a newline. | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 611 |   nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 612 |  | 
 | 613 |   // We weren't a recursive type | 
 | 614 |   return false; | 
 | 615 | } | 
 | 616 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 617 | // Prints a type definition. Returns true if it could not resolve all the types | 
 | 618 | // in the definition but had to use a forward reference. | 
 | 619 | void | 
 | 620 | CppWriter::printType(const Type* Ty) { | 
 | 621 |   assert(TypeStack.empty()); | 
 | 622 |   TypeStack.clear(); | 
 | 623 |   printTypeInternal(Ty); | 
 | 624 |   assert(TypeStack.empty()); | 
 | 625 | } | 
 | 626 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 627 | void | 
 | 628 | CppWriter::printTypes(const Module* M) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 629 |  | 
 | 630 |   // Walk the symbol table and print out all its types | 
| Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 631 |   const TypeSymbolTable& symtab = M->getTypeSymbolTable(); | 
 | 632 |   for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();  | 
 | 633 |        TI != TE; ++TI) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 634 |  | 
 | 635 |     // For primitive types and types already defined, just add a name | 
 | 636 |     TypeMap::const_iterator TNI = TypeNames.find(TI->second); | 
| Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 637 |     if (TI->second->isInteger() || TI->second->isPrimitiveType() ||  | 
| Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 638 |         TNI != TypeNames.end()) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 639 |       Out << "mod->addTypeName(\""; | 
 | 640 |       printEscapedString(TI->first); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 641 |       Out << "\", " << getCppName(TI->second) << ");"; | 
 | 642 |       nl(Out); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 643 |     // For everything else, define the type | 
 | 644 |     } else { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 645 |       printType(TI->second); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 646 |     } | 
 | 647 |   } | 
 | 648 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 649 |   // Add all of the global variables to the value table... | 
 | 650 |   for (Module::const_global_iterator I = TheModule->global_begin(),  | 
 | 651 |        E = TheModule->global_end(); I != E; ++I) { | 
 | 652 |     if (I->hasInitializer()) | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 653 |       printType(I->getInitializer()->getType()); | 
 | 654 |     printType(I->getType()); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 655 |   } | 
 | 656 |  | 
 | 657 |   // Add all the functions to the table | 
 | 658 |   for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end(); | 
 | 659 |        FI != FE; ++FI) { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 660 |     printType(FI->getReturnType()); | 
 | 661 |     printType(FI->getFunctionType()); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 662 |     // Add all the function arguments | 
 | 663 |     for(Function::const_arg_iterator AI = FI->arg_begin(), | 
 | 664 |         AE = FI->arg_end(); AI != AE; ++AI) { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 665 |       printType(AI->getType()); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 666 |     } | 
 | 667 |  | 
 | 668 |     // Add all of the basic blocks and instructions | 
 | 669 |     for (Function::const_iterator BB = FI->begin(), | 
 | 670 |          E = FI->end(); BB != E; ++BB) { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 671 |       printType(BB->getType()); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 672 |       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;  | 
 | 673 |            ++I) { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 674 |         printType(I->getType()); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 675 |         for (unsigned i = 0; i < I->getNumOperands(); ++i) | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 676 |           printType(I->getOperand(i)->getType()); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 677 |       } | 
 | 678 |     } | 
 | 679 |   } | 
 | 680 | } | 
 | 681 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 682 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 683 | // printConstant - Print out a constant pool entry... | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 684 | void CppWriter::printConstant(const Constant *CV) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 685 |   // First, if the constant is actually a GlobalValue (variable or function) or | 
 | 686 |   // its already in the constant list then we've printed it already and we can | 
 | 687 |   // just return. | 
 | 688 |   if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end()) | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 689 |     return; | 
 | 690 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 691 |   std::string constName(getCppName(CV)); | 
 | 692 |   std::string typeName(getCppName(CV->getType())); | 
 | 693 |   if (CV->isNullValue()) { | 
 | 694 |     Out << "Constant* " << constName << " = Constant::getNullValue(" | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 695 |         << typeName << ");"; | 
 | 696 |     nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 697 |     return; | 
 | 698 |   } | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 699 |   if (isa<GlobalValue>(CV)) { | 
 | 700 |     // Skip variables and functions, we emit them elsewhere | 
 | 701 |     return; | 
 | 702 |   } | 
| Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 703 |   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { | 
| Reid Spencer | 4d26a06 | 2007-04-11 13:02:56 +0000 | [diff] [blame] | 704 |     Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("  | 
 | 705 |         << cast<IntegerType>(CI->getType())->getBitWidth() << ", " | 
| Reid Spencer | 7029725 | 2007-03-01 20:55:43 +0000 | [diff] [blame] | 706 |         << " \"" << CI->getValue().toStringSigned(10)  << "\", 10));"; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 707 |   } else if (isa<ConstantAggregateZero>(CV)) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 708 |     Out << "ConstantAggregateZero* " << constName  | 
 | 709 |         << " = ConstantAggregateZero::get(" << typeName << ");"; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 710 |   } else if (isa<ConstantPointerNull>(CV)) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 711 |     Out << "ConstantPointerNull* " << constName  | 
 | 712 |         << " = ConstanPointerNull::get(" << typeName << ");"; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 713 |   } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 714 |     Out << "ConstantFP* " << constName << " = "; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 715 |     printCFP(CFP); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 716 |     Out << ";"; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 717 |   } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { | 
| Reid Spencer | 71d2ec9 | 2006-12-31 06:02:26 +0000 | [diff] [blame] | 718 |     if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) { | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 719 |       Out << "Constant* " << constName << " = ConstantArray::get(\""; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 720 |       printEscapedString(CA->getAsString()); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 721 |       // Determine if we want null termination or not. | 
 | 722 |       if (CA->getType()->getNumElements() <= CA->getAsString().length()) | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 723 |         Out << "\", false";// No null terminator | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 724 |       else | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 725 |         Out << "\", true"; // Indicate that the null terminator should be added. | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 726 |       Out << ");"; | 
 | 727 |     } else {  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 728 |       Out << "std::vector<Constant*> " << constName << "_elems;"; | 
 | 729 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 730 |       unsigned N = CA->getNumOperands(); | 
 | 731 |       for (unsigned i = 0; i < N; ++i) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 732 |         printConstant(CA->getOperand(i)); // recurse to print operands | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 733 |         Out << constName << "_elems.push_back(" | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 734 |             << getCppName(CA->getOperand(i)) << ");"; | 
 | 735 |         nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 736 |       } | 
 | 737 |       Out << "Constant* " << constName << " = ConstantArray::get("  | 
 | 738 |           << typeName << ", " << constName << "_elems);"; | 
 | 739 |     } | 
 | 740 |   } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 741 |     Out << "std::vector<Constant*> " << constName << "_fields;"; | 
 | 742 |     nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 743 |     unsigned N = CS->getNumOperands(); | 
 | 744 |     for (unsigned i = 0; i < N; i++) { | 
 | 745 |       printConstant(CS->getOperand(i)); | 
 | 746 |       Out << constName << "_fields.push_back(" | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 747 |           << getCppName(CS->getOperand(i)) << ");"; | 
 | 748 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 749 |     } | 
 | 750 |     Out << "Constant* " << constName << " = ConstantStruct::get("  | 
 | 751 |         << typeName << ", " << constName << "_fields);"; | 
| Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 752 |   } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 753 |     Out << "std::vector<Constant*> " << constName << "_elems;"; | 
 | 754 |     nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 755 |     unsigned N = CP->getNumOperands(); | 
 | 756 |     for (unsigned i = 0; i < N; ++i) { | 
 | 757 |       printConstant(CP->getOperand(i)); | 
 | 758 |       Out << constName << "_elems.push_back(" | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 759 |           << getCppName(CP->getOperand(i)) << ");"; | 
 | 760 |       nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 761 |     } | 
| Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 762 |     Out << "Constant* " << constName << " = ConstantVector::get("  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 763 |         << typeName << ", " << constName << "_elems);"; | 
 | 764 |   } else if (isa<UndefValue>(CV)) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 765 |     Out << "UndefValue* " << constName << " = UndefValue::get("  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 766 |         << typeName << ");"; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 767 |   } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 768 |     if (CE->getOpcode() == Instruction::GetElementPtr) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 769 |       Out << "std::vector<Constant*> " << constName << "_indices;"; | 
 | 770 |       nl(Out); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 771 |       printConstant(CE->getOperand(0)); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 772 |       for (unsigned i = 1; i < CE->getNumOperands(); ++i ) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 773 |         printConstant(CE->getOperand(i)); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 774 |         Out << constName << "_indices.push_back(" | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 775 |             << getCppName(CE->getOperand(i)) << ");"; | 
 | 776 |         nl(Out); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 777 |       } | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 778 |       Out << "Constant* " << constName  | 
 | 779 |           << " = ConstantExpr::getGetElementPtr("  | 
 | 780 |           << getCppName(CE->getOperand(0)) << ", "  | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 781 |           << "&" << constName << "_indices[0], " << CE->getNumOperands() - 1 | 
 | 782 |           << " );"; | 
| Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 783 |     } else if (CE->isCast()) { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 784 |       printConstant(CE->getOperand(0)); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 785 |       Out << "Constant* " << constName << " = ConstantExpr::getCast("; | 
| Reid Spencer | b0e9f72 | 2006-12-12 01:31:37 +0000 | [diff] [blame] | 786 |       switch (CE->getOpcode()) { | 
 | 787 |         default: assert(0 && "Invalid cast opcode"); | 
 | 788 |         case Instruction::Trunc: Out << "Instruction::Trunc"; break; | 
 | 789 |         case Instruction::ZExt:  Out << "Instruction::ZExt"; break; | 
 | 790 |         case Instruction::SExt:  Out << "Instruction::SExt"; break; | 
 | 791 |         case Instruction::FPTrunc:  Out << "Instruction::FPTrunc"; break; | 
 | 792 |         case Instruction::FPExt:  Out << "Instruction::FPExt"; break; | 
 | 793 |         case Instruction::FPToUI:  Out << "Instruction::FPToUI"; break; | 
 | 794 |         case Instruction::FPToSI:  Out << "Instruction::FPToSI"; break; | 
 | 795 |         case Instruction::UIToFP:  Out << "Instruction::UIToFP"; break; | 
 | 796 |         case Instruction::SIToFP:  Out << "Instruction::SIToFP"; break; | 
 | 797 |         case Instruction::PtrToInt:  Out << "Instruction::PtrToInt"; break; | 
 | 798 |         case Instruction::IntToPtr:  Out << "Instruction::IntToPtr"; break; | 
 | 799 |         case Instruction::BitCast:  Out << "Instruction::BitCast"; break; | 
 | 800 |       } | 
 | 801 |       Out << ", " << getCppName(CE->getOperand(0)) << ", "  | 
 | 802 |           << getCppName(CE->getType()) << ");"; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 803 |     } else { | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 804 |       unsigned N = CE->getNumOperands(); | 
 | 805 |       for (unsigned i = 0; i < N; ++i ) { | 
 | 806 |         printConstant(CE->getOperand(i)); | 
 | 807 |       } | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 808 |       Out << "Constant* " << constName << " = ConstantExpr::"; | 
 | 809 |       switch (CE->getOpcode()) { | 
| Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 810 |         case Instruction::Add:    Out << "getAdd(";  break; | 
 | 811 |         case Instruction::Sub:    Out << "getSub("; break; | 
 | 812 |         case Instruction::Mul:    Out << "getMul("; break; | 
 | 813 |         case Instruction::UDiv:   Out << "getUDiv("; break; | 
 | 814 |         case Instruction::SDiv:   Out << "getSDiv("; break; | 
 | 815 |         case Instruction::FDiv:   Out << "getFDiv("; break; | 
 | 816 |         case Instruction::URem:   Out << "getURem("; break; | 
 | 817 |         case Instruction::SRem:   Out << "getSRem("; break; | 
 | 818 |         case Instruction::FRem:   Out << "getFRem("; break; | 
 | 819 |         case Instruction::And:    Out << "getAnd("; break; | 
 | 820 |         case Instruction::Or:     Out << "getOr("; break; | 
 | 821 |         case Instruction::Xor:    Out << "getXor("; break; | 
 | 822 |         case Instruction::ICmp:    | 
 | 823 |           Out << "getICmp(ICmpInst::ICMP_"; | 
 | 824 |           switch (CE->getPredicate()) { | 
 | 825 |             case ICmpInst::ICMP_EQ:  Out << "EQ"; break; | 
 | 826 |             case ICmpInst::ICMP_NE:  Out << "NE"; break; | 
 | 827 |             case ICmpInst::ICMP_SLT: Out << "SLT"; break; | 
 | 828 |             case ICmpInst::ICMP_ULT: Out << "ULT"; break; | 
 | 829 |             case ICmpInst::ICMP_SGT: Out << "SGT"; break; | 
 | 830 |             case ICmpInst::ICMP_UGT: Out << "UGT"; break; | 
 | 831 |             case ICmpInst::ICMP_SLE: Out << "SLE"; break; | 
 | 832 |             case ICmpInst::ICMP_ULE: Out << "ULE"; break; | 
 | 833 |             case ICmpInst::ICMP_SGE: Out << "SGE"; break; | 
 | 834 |             case ICmpInst::ICMP_UGE: Out << "UGE"; break; | 
 | 835 |             default: error("Invalid ICmp Predicate"); | 
 | 836 |           } | 
 | 837 |           break; | 
 | 838 |         case Instruction::FCmp: | 
 | 839 |           Out << "getFCmp(FCmpInst::FCMP_"; | 
 | 840 |           switch (CE->getPredicate()) { | 
 | 841 |             case FCmpInst::FCMP_FALSE: Out << "FALSE"; break; | 
 | 842 |             case FCmpInst::FCMP_ORD:   Out << "ORD"; break; | 
 | 843 |             case FCmpInst::FCMP_UNO:   Out << "UNO"; break; | 
 | 844 |             case FCmpInst::FCMP_OEQ:   Out << "OEQ"; break; | 
 | 845 |             case FCmpInst::FCMP_UEQ:   Out << "UEQ"; break; | 
 | 846 |             case FCmpInst::FCMP_ONE:   Out << "ONE"; break; | 
 | 847 |             case FCmpInst::FCMP_UNE:   Out << "UNE"; break; | 
 | 848 |             case FCmpInst::FCMP_OLT:   Out << "OLT"; break; | 
 | 849 |             case FCmpInst::FCMP_ULT:   Out << "ULT"; break; | 
 | 850 |             case FCmpInst::FCMP_OGT:   Out << "OGT"; break; | 
 | 851 |             case FCmpInst::FCMP_UGT:   Out << "UGT"; break; | 
 | 852 |             case FCmpInst::FCMP_OLE:   Out << "OLE"; break; | 
 | 853 |             case FCmpInst::FCMP_ULE:   Out << "ULE"; break; | 
 | 854 |             case FCmpInst::FCMP_OGE:   Out << "OGE"; break; | 
 | 855 |             case FCmpInst::FCMP_UGE:   Out << "UGE"; break; | 
 | 856 |             case FCmpInst::FCMP_TRUE:  Out << "TRUE"; break; | 
 | 857 |             default: error("Invalid FCmp Predicate"); | 
 | 858 |           } | 
 | 859 |           break; | 
 | 860 |         case Instruction::Shl:     Out << "getShl("; break; | 
 | 861 |         case Instruction::LShr:    Out << "getLShr("; break; | 
 | 862 |         case Instruction::AShr:    Out << "getAShr("; break; | 
 | 863 |         case Instruction::Select:  Out << "getSelect("; break; | 
 | 864 |         case Instruction::ExtractElement: Out << "getExtractElement("; break; | 
 | 865 |         case Instruction::InsertElement:  Out << "getInsertElement("; break; | 
 | 866 |         case Instruction::ShuffleVector:  Out << "getShuffleVector("; break; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 867 |         default: | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 868 |           error("Invalid constant expression"); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 869 |           break; | 
 | 870 |       } | 
 | 871 |       Out << getCppName(CE->getOperand(0)); | 
 | 872 |       for (unsigned i = 1; i < CE->getNumOperands(); ++i)  | 
 | 873 |         Out << ", " << getCppName(CE->getOperand(i)); | 
 | 874 |       Out << ");"; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 875 |     } | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 876 |   } else { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 877 |     error("Bad Constant"); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 878 |     Out << "Constant* " << constName << " = 0; "; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 879 |   } | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 880 |   nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 881 | } | 
 | 882 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 883 | void | 
 | 884 | CppWriter::printConstants(const Module* M) { | 
 | 885 |   // Traverse all the global variables looking for constant initializers | 
 | 886 |   for (Module::const_global_iterator I = TheModule->global_begin(),  | 
 | 887 |        E = TheModule->global_end(); I != E; ++I) | 
 | 888 |     if (I->hasInitializer()) | 
 | 889 |       printConstant(I->getInitializer()); | 
 | 890 |  | 
 | 891 |   // Traverse the LLVM functions looking for constants | 
 | 892 |   for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end(); | 
 | 893 |        FI != FE; ++FI) { | 
 | 894 |     // Add all of the basic blocks and instructions | 
 | 895 |     for (Function::const_iterator BB = FI->begin(), | 
 | 896 |          E = FI->end(); BB != E; ++BB) { | 
 | 897 |       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;  | 
 | 898 |            ++I) { | 
 | 899 |         for (unsigned i = 0; i < I->getNumOperands(); ++i) { | 
 | 900 |           if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) { | 
 | 901 |             printConstant(C); | 
 | 902 |           } | 
 | 903 |         } | 
 | 904 |       } | 
 | 905 |     } | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 906 |   } | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 907 | } | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 908 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 909 | void CppWriter::printVariableUses(const GlobalVariable *GV) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 910 |   nl(Out) << "// Type Definitions"; | 
 | 911 |   nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 912 |   printType(GV->getType()); | 
 | 913 |   if (GV->hasInitializer()) { | 
 | 914 |     Constant* Init = GV->getInitializer(); | 
 | 915 |     printType(Init->getType()); | 
 | 916 |     if (Function* F = dyn_cast<Function>(Init)) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 917 |       nl(Out)<< "/ Function Declarations"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 918 |       printFunctionHead(F); | 
 | 919 |     } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 920 |       nl(Out) << "// Global Variable Declarations"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 921 |       printVariableHead(gv); | 
 | 922 |     } else  { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 923 |       nl(Out) << "// Constant Definitions"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 924 |       printConstant(gv); | 
 | 925 |     } | 
 | 926 |     if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 927 |       nl(Out) << "// Global Variable Definitions"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 928 |       printVariableBody(gv); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 929 |     } | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 930 |   } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 931 | } | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 932 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 933 | void CppWriter::printVariableHead(const GlobalVariable *GV) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 934 |   nl(Out) << "GlobalVariable* " << getCppName(GV); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 935 |   if (is_inline) { | 
 | 936 |      Out << " = mod->getGlobalVariable("; | 
 | 937 |      printEscapedString(GV->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 938 |      Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)"; | 
 | 939 |      nl(Out) << "if (!" << getCppName(GV) << ") {"; | 
 | 940 |      in(); nl(Out) << getCppName(GV); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 941 |   } | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 942 |   Out << " = new GlobalVariable("; | 
 | 943 |   nl(Out) << "/*Type=*/"; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 944 |   printCppName(GV->getType()->getElementType()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 945 |   Out << ","; | 
 | 946 |   nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false"); | 
 | 947 |   Out << ","; | 
 | 948 |   nl(Out) << "/*Linkage=*/"; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 949 |   printLinkageType(GV->getLinkage()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 950 |   Out << ","; | 
 | 951 |   nl(Out) << "/*Initializer=*/0, "; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 952 |   if (GV->hasInitializer()) { | 
 | 953 |     Out << "// has initializer, specified below"; | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 954 |   } | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 955 |   nl(Out) << "/*Name=*/\""; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 956 |   printEscapedString(GV->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 957 |   Out << "\","; | 
 | 958 |   nl(Out) << "mod);"; | 
 | 959 |   nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 960 |  | 
 | 961 |   if (GV->hasSection()) { | 
 | 962 |     printCppName(GV); | 
 | 963 |     Out << "->setSection(\""; | 
 | 964 |     printEscapedString(GV->getSection()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 965 |     Out << "\");"; | 
 | 966 |     nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 967 |   } | 
 | 968 |   if (GV->getAlignment()) { | 
 | 969 |     printCppName(GV); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 970 |     Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");"; | 
 | 971 |     nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 972 |   }; | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 973 |   if (is_inline) { | 
 | 974 |     out(); Out << "}"; nl(Out); | 
 | 975 |   } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 976 | } | 
 | 977 |  | 
 | 978 | void  | 
 | 979 | CppWriter::printVariableBody(const GlobalVariable *GV) { | 
 | 980 |   if (GV->hasInitializer()) { | 
 | 981 |     printCppName(GV); | 
 | 982 |     Out << "->setInitializer("; | 
 | 983 |     //if (!isa<GlobalValue(GV->getInitializer())) | 
 | 984 |     //else  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 985 |       Out << getCppName(GV->getInitializer()) << ");"; | 
 | 986 |       nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 987 |   } | 
 | 988 | } | 
 | 989 |  | 
 | 990 | std::string | 
 | 991 | CppWriter::getOpName(Value* V) { | 
 | 992 |   if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end()) | 
 | 993 |     return getCppName(V); | 
 | 994 |  | 
 | 995 |   // See if its alread in the map of forward references, if so just return the | 
 | 996 |   // name we already set up for it | 
 | 997 |   ForwardRefMap::const_iterator I = ForwardRefs.find(V); | 
 | 998 |   if (I != ForwardRefs.end()) | 
 | 999 |     return I->second; | 
 | 1000 |  | 
 | 1001 |   // This is a new forward reference. Generate a unique name for it | 
 | 1002 |   std::string result(std::string("fwdref_") + utostr(uniqueNum++)); | 
 | 1003 |  | 
 | 1004 |   // Yes, this is a hack. An Argument is the smallest instantiable value that | 
 | 1005 |   // we can make as a placeholder for the real value. We'll replace these | 
 | 1006 |   // Argument instances later. | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1007 |   Out << "Argument* " << result << " = new Argument("  | 
 | 1008 |       << getCppName(V->getType()) << ");"; | 
 | 1009 |   nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1010 |   ForwardRefs[V] = result; | 
 | 1011 |   return result; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1012 | } | 
 | 1013 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1014 | // printInstruction - This member is called for each Instruction in a function. | 
 | 1015 | void  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1016 | CppWriter::printInstruction(const Instruction *I, const std::string& bbname) { | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1017 |   std::string iName(getCppName(I)); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1018 |  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1019 |   // Before we emit this instruction, we need to take care of generating any | 
 | 1020 |   // forward references. So, we get the names of all the operands in advance | 
 | 1021 |   std::string* opNames = new std::string[I->getNumOperands()]; | 
 | 1022 |   for (unsigned i = 0; i < I->getNumOperands(); i++) { | 
 | 1023 |     opNames[i] = getOpName(I->getOperand(i)); | 
 | 1024 |   } | 
 | 1025 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1026 |   switch (I->getOpcode()) { | 
 | 1027 |     case Instruction::Ret: { | 
 | 1028 |       const ReturnInst* ret =  cast<ReturnInst>(I); | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1029 |       Out << "new ReturnInst(" | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1030 |           << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");"; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1031 |       break; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1032 |     } | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1033 |     case Instruction::Br: { | 
 | 1034 |       const BranchInst* br = cast<BranchInst>(I); | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1035 |       Out << "new BranchInst(" ; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1036 |       if (br->getNumOperands() == 3 ) { | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1037 |         Out << opNames[0] << ", "  | 
 | 1038 |             << opNames[1] << ", " | 
 | 1039 |             << opNames[2] << ", "; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1040 |  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1041 |       } else if (br->getNumOperands() == 1) { | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1042 |         Out << opNames[0] << ", "; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1043 |       } else { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1044 |         error("Branch with 2 operands?"); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1045 |       } | 
 | 1046 |       Out << bbname << ");"; | 
 | 1047 |       break; | 
 | 1048 |     } | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1049 |     case Instruction::Switch: { | 
 | 1050 |       const SwitchInst* sw = cast<SwitchInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1051 |       Out << "SwitchInst* " << iName << " = new SwitchInst(" | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1052 |           << opNames[0] << ", " | 
 | 1053 |           << opNames[1] << ", " | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1054 |           << sw->getNumCases() << ", " << bbname << ");"; | 
 | 1055 |       nl(Out); | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1056 |       for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1057 |         Out << iName << "->addCase("  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1058 |             << opNames[i] << ", " | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1059 |             << opNames[i+1] << ");"; | 
 | 1060 |         nl(Out); | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1061 |       } | 
 | 1062 |       break; | 
 | 1063 |     } | 
 | 1064 |     case Instruction::Invoke: { | 
 | 1065 |       const InvokeInst* inv = cast<InvokeInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1066 |       Out << "std::vector<Value*> " << iName << "_params;"; | 
 | 1067 |       nl(Out); | 
 | 1068 |       for (unsigned i = 3; i < inv->getNumOperands(); ++i) { | 
 | 1069 |         Out << iName << "_params.push_back(" | 
 | 1070 |             << opNames[i] << ");"; | 
 | 1071 |         nl(Out); | 
 | 1072 |       } | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1073 |       Out << "InvokeInst *" << iName << " = new InvokeInst(" | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1074 |           << opNames[0] << ", " | 
 | 1075 |           << opNames[1] << ", " | 
 | 1076 |           << opNames[2] << ", " | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1077 |           << "&" << iName << "_params[0], " << inv->getNumOperands() - 3  | 
 | 1078 |           << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1079 |       printEscapedString(inv->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1080 |       Out << "\", " << bbname << ");"; | 
 | 1081 |       nl(Out) << iName << "->setCallingConv("; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1082 |       printCallingConv(inv->getCallingConv()); | 
 | 1083 |       Out << ");"; | 
 | 1084 |       break; | 
 | 1085 |     } | 
 | 1086 |     case Instruction::Unwind: { | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1087 |       Out << "new UnwindInst(" | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1088 |           << bbname << ");"; | 
 | 1089 |       break; | 
 | 1090 |     } | 
 | 1091 |     case Instruction::Unreachable:{ | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1092 |       Out << "new UnreachableInst(" | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1093 |           << bbname << ");"; | 
 | 1094 |       break; | 
 | 1095 |     } | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1096 |     case Instruction::Add: | 
 | 1097 |     case Instruction::Sub: | 
 | 1098 |     case Instruction::Mul: | 
| Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1099 |     case Instruction::UDiv: | 
 | 1100 |     case Instruction::SDiv: | 
 | 1101 |     case Instruction::FDiv: | 
| Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1102 |     case Instruction::URem: | 
 | 1103 |     case Instruction::SRem: | 
 | 1104 |     case Instruction::FRem: | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1105 |     case Instruction::And: | 
 | 1106 |     case Instruction::Or: | 
 | 1107 |     case Instruction::Xor: | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1108 |     case Instruction::Shl:  | 
| Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1109 |     case Instruction::LShr:  | 
 | 1110 |     case Instruction::AShr:{ | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1111 |       Out << "BinaryOperator* " << iName << " = BinaryOperator::create("; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1112 |       switch (I->getOpcode()) { | 
 | 1113 |         case Instruction::Add: Out << "Instruction::Add"; break; | 
 | 1114 |         case Instruction::Sub: Out << "Instruction::Sub"; break; | 
 | 1115 |         case Instruction::Mul: Out << "Instruction::Mul"; break; | 
| Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1116 |         case Instruction::UDiv:Out << "Instruction::UDiv"; break; | 
 | 1117 |         case Instruction::SDiv:Out << "Instruction::SDiv"; break; | 
 | 1118 |         case Instruction::FDiv:Out << "Instruction::FDiv"; break; | 
| Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1119 |         case Instruction::URem:Out << "Instruction::URem"; break; | 
 | 1120 |         case Instruction::SRem:Out << "Instruction::SRem"; break; | 
 | 1121 |         case Instruction::FRem:Out << "Instruction::FRem"; break; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1122 |         case Instruction::And: Out << "Instruction::And"; break; | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1123 |         case Instruction::Or:  Out << "Instruction::Or";  break; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1124 |         case Instruction::Xor: Out << "Instruction::Xor"; break; | 
 | 1125 |         case Instruction::Shl: Out << "Instruction::Shl"; break; | 
| Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1126 |         case Instruction::LShr:Out << "Instruction::LShr"; break; | 
 | 1127 |         case Instruction::AShr:Out << "Instruction::AShr"; break; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1128 |         default: Out << "Instruction::BadOpCode"; break; | 
 | 1129 |       } | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1130 |       Out << ", " << opNames[0] << ", " << opNames[1] << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1131 |       printEscapedString(I->getName()); | 
 | 1132 |       Out << "\", " << bbname << ");"; | 
 | 1133 |       break; | 
 | 1134 |     } | 
| Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1135 |     case Instruction::FCmp: { | 
 | 1136 |       Out << "FCmpInst* " << iName << " = new FCmpInst("; | 
 | 1137 |       switch (cast<FCmpInst>(I)->getPredicate()) { | 
 | 1138 |         case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break; | 
 | 1139 |         case FCmpInst::FCMP_OEQ  : Out << "FCmpInst::FCMP_OEQ"; break; | 
 | 1140 |         case FCmpInst::FCMP_OGT  : Out << "FCmpInst::FCMP_OGT"; break; | 
 | 1141 |         case FCmpInst::FCMP_OGE  : Out << "FCmpInst::FCMP_OGE"; break; | 
 | 1142 |         case FCmpInst::FCMP_OLT  : Out << "FCmpInst::FCMP_OLT"; break; | 
 | 1143 |         case FCmpInst::FCMP_OLE  : Out << "FCmpInst::FCMP_OLE"; break; | 
 | 1144 |         case FCmpInst::FCMP_ONE  : Out << "FCmpInst::FCMP_ONE"; break; | 
 | 1145 |         case FCmpInst::FCMP_ORD  : Out << "FCmpInst::FCMP_ORD"; break; | 
 | 1146 |         case FCmpInst::FCMP_UNO  : Out << "FCmpInst::FCMP_UNO"; break; | 
 | 1147 |         case FCmpInst::FCMP_UEQ  : Out << "FCmpInst::FCMP_UEQ"; break; | 
 | 1148 |         case FCmpInst::FCMP_UGT  : Out << "FCmpInst::FCMP_UGT"; break; | 
 | 1149 |         case FCmpInst::FCMP_UGE  : Out << "FCmpInst::FCMP_UGE"; break; | 
 | 1150 |         case FCmpInst::FCMP_ULT  : Out << "FCmpInst::FCMP_ULT"; break; | 
 | 1151 |         case FCmpInst::FCMP_ULE  : Out << "FCmpInst::FCMP_ULE"; break; | 
 | 1152 |         case FCmpInst::FCMP_UNE  : Out << "FCmpInst::FCMP_UNE"; break; | 
 | 1153 |         case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break; | 
 | 1154 |         default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break; | 
 | 1155 |       } | 
 | 1156 |       Out << ", " << opNames[0] << ", " << opNames[1] << ", \""; | 
 | 1157 |       printEscapedString(I->getName()); | 
 | 1158 |       Out << "\", " << bbname << ");"; | 
 | 1159 |       break; | 
 | 1160 |     } | 
 | 1161 |     case Instruction::ICmp: { | 
 | 1162 |       Out << "ICmpInst* " << iName << " = new ICmpInst("; | 
 | 1163 |       switch (cast<ICmpInst>(I)->getPredicate()) { | 
 | 1164 |         case ICmpInst::ICMP_EQ:  Out << "ICmpInst::ICMP_EQ";  break; | 
 | 1165 |         case ICmpInst::ICMP_NE:  Out << "ICmpInst::ICMP_NE";  break; | 
 | 1166 |         case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break; | 
 | 1167 |         case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break; | 
 | 1168 |         case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break; | 
 | 1169 |         case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break; | 
 | 1170 |         case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break; | 
 | 1171 |         case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break; | 
 | 1172 |         case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break; | 
 | 1173 |         case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break; | 
 | 1174 |         default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1175 |       } | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1176 |       Out << ", " << opNames[0] << ", " << opNames[1] << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1177 |       printEscapedString(I->getName()); | 
 | 1178 |       Out << "\", " << bbname << ");"; | 
 | 1179 |       break; | 
 | 1180 |     } | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1181 |     case Instruction::Malloc: { | 
 | 1182 |       const MallocInst* mallocI = cast<MallocInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1183 |       Out << "MallocInst* " << iName << " = new MallocInst(" | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1184 |           << getCppName(mallocI->getAllocatedType()) << ", "; | 
 | 1185 |       if (mallocI->isArrayAllocation()) | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1186 |         Out << opNames[0] << ", " ; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1187 |       Out << "\""; | 
 | 1188 |       printEscapedString(mallocI->getName()); | 
 | 1189 |       Out << "\", " << bbname << ");"; | 
 | 1190 |       if (mallocI->getAlignment()) | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1191 |         nl(Out) << iName << "->setAlignment("  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1192 |             << mallocI->getAlignment() << ");"; | 
 | 1193 |       break; | 
 | 1194 |     } | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1195 |     case Instruction::Free: { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1196 |       Out << "FreeInst* " << iName << " = new FreeInst(" | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1197 |           << getCppName(I->getOperand(0)) << ", " << bbname << ");"; | 
 | 1198 |       break; | 
 | 1199 |     } | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1200 |     case Instruction::Alloca: { | 
 | 1201 |       const AllocaInst* allocaI = cast<AllocaInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1202 |       Out << "AllocaInst* " << iName << " = new AllocaInst(" | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1203 |           << getCppName(allocaI->getAllocatedType()) << ", "; | 
 | 1204 |       if (allocaI->isArrayAllocation()) | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1205 |         Out << opNames[0] << ", "; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1206 |       Out << "\""; | 
 | 1207 |       printEscapedString(allocaI->getName()); | 
 | 1208 |       Out << "\", " << bbname << ");"; | 
 | 1209 |       if (allocaI->getAlignment()) | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1210 |         nl(Out) << iName << "->setAlignment("  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1211 |             << allocaI->getAlignment() << ");"; | 
 | 1212 |       break; | 
 | 1213 |     } | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1214 |     case Instruction::Load:{ | 
 | 1215 |       const LoadInst* load = cast<LoadInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1216 |       Out << "LoadInst* " << iName << " = new LoadInst("  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1217 |           << opNames[0] << ", \""; | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1218 |       printEscapedString(load->getName()); | 
 | 1219 |       Out << "\", " << (load->isVolatile() ? "true" : "false" ) | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1220 |           << ", " << bbname << ");"; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1221 |       break; | 
 | 1222 |     } | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1223 |     case Instruction::Store: { | 
 | 1224 |       const StoreInst* store = cast<StoreInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1225 |       Out << "StoreInst* " << iName << " = new StoreInst("  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1226 |           << opNames[0] << ", " | 
 | 1227 |           << opNames[1] << ", " | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1228 |           << (store->isVolatile() ? "true" : "false")  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1229 |           << ", " << bbname << ");"; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1230 |       break; | 
 | 1231 |     } | 
 | 1232 |     case Instruction::GetElementPtr: { | 
 | 1233 |       const GetElementPtrInst* gep = cast<GetElementPtrInst>(I); | 
 | 1234 |       if (gep->getNumOperands() <= 2) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1235 |         Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst(" | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1236 |             << opNames[0];  | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1237 |         if (gep->getNumOperands() == 2) | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1238 |           Out << ", " << opNames[1]; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1239 |       } else { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1240 |         Out << "std::vector<Value*> " << iName << "_indices;"; | 
 | 1241 |         nl(Out); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1242 |         for (unsigned i = 1; i < gep->getNumOperands(); ++i ) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1243 |           Out << iName << "_indices.push_back(" | 
 | 1244 |               << opNames[i] << ");"; | 
 | 1245 |           nl(Out); | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1246 |         } | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1247 |         Out << "Instruction* " << iName << " = new GetElementPtrInst("  | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1248 |             << opNames[0] << ", &" << iName << "_indices[0], "  | 
 | 1249 |             << gep->getNumOperands() - 1; | 
| Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1250 |       } | 
 | 1251 |       Out << ", \""; | 
 | 1252 |       printEscapedString(gep->getName()); | 
 | 1253 |       Out << "\", " << bbname << ");"; | 
 | 1254 |       break; | 
 | 1255 |     } | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1256 |     case Instruction::PHI: { | 
 | 1257 |       const PHINode* phi = cast<PHINode>(I); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1258 |  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1259 |       Out << "PHINode* " << iName << " = new PHINode(" | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1260 |           << getCppName(phi->getType()) << ", \""; | 
 | 1261 |       printEscapedString(phi->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1262 |       Out << "\", " << bbname << ");"; | 
 | 1263 |       nl(Out) << iName << "->reserveOperandSpace("  | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1264 |         << phi->getNumIncomingValues() | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1265 |           << ");"; | 
 | 1266 |       nl(Out); | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1267 |       for (unsigned i = 0; i < phi->getNumOperands(); i+=2) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1268 |         Out << iName << "->addIncoming(" | 
 | 1269 |             << opNames[i] << ", " << opNames[i+1] << ");"; | 
 | 1270 |         nl(Out); | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1271 |       } | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1272 |       break; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1273 |     } | 
| Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1274 |     case Instruction::Trunc:  | 
 | 1275 |     case Instruction::ZExt: | 
 | 1276 |     case Instruction::SExt: | 
 | 1277 |     case Instruction::FPTrunc: | 
 | 1278 |     case Instruction::FPExt: | 
 | 1279 |     case Instruction::FPToUI: | 
 | 1280 |     case Instruction::FPToSI: | 
 | 1281 |     case Instruction::UIToFP: | 
 | 1282 |     case Instruction::SIToFP: | 
 | 1283 |     case Instruction::PtrToInt: | 
 | 1284 |     case Instruction::IntToPtr: | 
 | 1285 |     case Instruction::BitCast: { | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1286 |       const CastInst* cst = cast<CastInst>(I); | 
| Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1287 |       Out << "CastInst* " << iName << " = new "; | 
 | 1288 |       switch (I->getOpcode()) { | 
| Reid Spencer | 3481657 | 2007-02-16 06:34:39 +0000 | [diff] [blame] | 1289 |         case Instruction::Trunc:    Out << "TruncInst"; break; | 
 | 1290 |         case Instruction::ZExt:     Out << "ZExtInst"; break; | 
 | 1291 |         case Instruction::SExt:     Out << "SExtInst"; break; | 
 | 1292 |         case Instruction::FPTrunc:  Out << "FPTruncInst"; break; | 
 | 1293 |         case Instruction::FPExt:    Out << "FPExtInst"; break; | 
 | 1294 |         case Instruction::FPToUI:   Out << "FPToUIInst"; break; | 
 | 1295 |         case Instruction::FPToSI:   Out << "FPToSIInst"; break; | 
 | 1296 |         case Instruction::UIToFP:   Out << "UIToFPInst"; break; | 
 | 1297 |         case Instruction::SIToFP:   Out << "SIToFPInst"; break; | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1298 |         case Instruction::PtrToInt: Out << "PtrToIntInst"; break; | 
| Reid Spencer | 3481657 | 2007-02-16 06:34:39 +0000 | [diff] [blame] | 1299 |         case Instruction::IntToPtr: Out << "IntToPtrInst"; break; | 
 | 1300 |         case Instruction::BitCast:  Out << "BitCastInst"; break; | 
| Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1301 |         default: assert(!"Unreachable"); break; | 
 | 1302 |       } | 
 | 1303 |       Out << "(" << opNames[0] << ", " | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1304 |           << getCppName(cst->getType()) << ", \""; | 
 | 1305 |       printEscapedString(cst->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1306 |       Out << "\", " << bbname << ");"; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1307 |       break; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1308 |     } | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1309 |     case Instruction::Call:{ | 
 | 1310 |       const CallInst* call = cast<CallInst>(I); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1311 |       if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1312 |         Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get(" | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1313 |             << getCppName(ila->getFunctionType()) << ", \"" | 
 | 1314 |             << ila->getAsmString() << "\", \"" | 
 | 1315 |             << ila->getConstraintString() << "\"," | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1316 |             << (ila->hasSideEffects() ? "true" : "false") << ");"; | 
 | 1317 |         nl(Out); | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1318 |       } | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1319 |       if (call->getNumOperands() > 3) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1320 |         Out << "std::vector<Value*> " << iName << "_params;"; | 
 | 1321 |         nl(Out); | 
 | 1322 |         for (unsigned i = 1; i < call->getNumOperands(); ++i) { | 
 | 1323 |           Out << iName << "_params.push_back(" << opNames[i] << ");"; | 
 | 1324 |           nl(Out); | 
 | 1325 |         } | 
 | 1326 |         Out << "CallInst* " << iName << " = new CallInst(" | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1327 |             << opNames[0] << ", &" << iName << "_params[0], "  | 
 | 1328 |             << call->getNumOperands() - 1 << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1329 |       } else if (call->getNumOperands() == 3) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1330 |         Out << "CallInst* " << iName << " = new CallInst(" | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1331 |             << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1332 |       } else if (call->getNumOperands() == 2) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1333 |         Out << "CallInst* " << iName << " = new CallInst(" | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1334 |             << opNames[0] << ", " << opNames[1] << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1335 |       } else { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1336 |         Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1337 |             << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1338 |       } | 
 | 1339 |       printEscapedString(call->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1340 |       Out << "\", " << bbname << ");"; | 
 | 1341 |       nl(Out) << iName << "->setCallingConv("; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1342 |       printCallingConv(call->getCallingConv()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1343 |       Out << ");"; | 
 | 1344 |       nl(Out) << iName << "->setTailCall("  | 
| Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1345 |           << (call->isTailCall() ? "true":"false"); | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1346 |       Out << ");"; | 
 | 1347 |       break; | 
 | 1348 |     } | 
 | 1349 |     case Instruction::Select: { | 
 | 1350 |       const SelectInst* sel = cast<SelectInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1351 |       Out << "SelectInst* " << getCppName(sel) << " = new SelectInst("; | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1352 |       Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1353 |       printEscapedString(sel->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1354 |       Out << "\", " << bbname << ");"; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1355 |       break; | 
 | 1356 |     } | 
 | 1357 |     case Instruction::UserOp1: | 
 | 1358 |       /// FALL THROUGH | 
 | 1359 |     case Instruction::UserOp2: { | 
 | 1360 |       /// FIXME: What should be done here? | 
 | 1361 |       break; | 
 | 1362 |     } | 
 | 1363 |     case Instruction::VAArg: { | 
 | 1364 |       const VAArgInst* va = cast<VAArgInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1365 |       Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst(" | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1366 |           << opNames[0] << ", " << getCppName(va->getType()) << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1367 |       printEscapedString(va->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1368 |       Out << "\", " << bbname << ");"; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1369 |       break; | 
 | 1370 |     } | 
 | 1371 |     case Instruction::ExtractElement: { | 
 | 1372 |       const ExtractElementInst* eei = cast<ExtractElementInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1373 |       Out << "ExtractElementInst* " << getCppName(eei)  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1374 |           << " = new ExtractElementInst(" << opNames[0] | 
 | 1375 |           << ", " << opNames[1] << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1376 |       printEscapedString(eei->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1377 |       Out << "\", " << bbname << ");"; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1378 |       break; | 
 | 1379 |     } | 
 | 1380 |     case Instruction::InsertElement: { | 
 | 1381 |       const InsertElementInst* iei = cast<InsertElementInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1382 |       Out << "InsertElementInst* " << getCppName(iei)  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1383 |           << " = new InsertElementInst(" << opNames[0] | 
 | 1384 |           << ", " << opNames[1] << ", " << opNames[2] << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1385 |       printEscapedString(iei->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1386 |       Out << "\", " << bbname << ");"; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1387 |       break; | 
 | 1388 |     } | 
 | 1389 |     case Instruction::ShuffleVector: { | 
 | 1390 |       const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1391 |       Out << "ShuffleVectorInst* " << getCppName(svi)  | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1392 |           << " = new ShuffleVectorInst(" << opNames[0] | 
 | 1393 |           << ", " << opNames[1] << ", " << opNames[2] << ", \""; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1394 |       printEscapedString(svi->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1395 |       Out << "\", " << bbname << ");"; | 
| Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1396 |       break; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1397 |     } | 
 | 1398 |   } | 
| Reid Spencer | 68464b7 | 2006-06-15 16:09:59 +0000 | [diff] [blame] | 1399 |   DefinedValues.insert(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1400 |   nl(Out); | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1401 |   delete [] opNames; | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1402 | } | 
 | 1403 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1404 | // Print out the types, constants and declarations needed by one function | 
 | 1405 | void CppWriter::printFunctionUses(const Function* F) { | 
 | 1406 |  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1407 |   nl(Out) << "// Type Definitions"; nl(Out); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1408 |   if (!is_inline) { | 
 | 1409 |     // Print the function's return type | 
 | 1410 |     printType(F->getReturnType()); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1411 |  | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1412 |     // Print the function's function type | 
 | 1413 |     printType(F->getFunctionType()); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1414 |  | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1415 |     // Print the types of each of the function's arguments | 
 | 1416 |     for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();  | 
 | 1417 |         AI != AE; ++AI) { | 
 | 1418 |       printType(AI->getType()); | 
 | 1419 |     } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1420 |   } | 
 | 1421 |  | 
 | 1422 |   // Print type definitions for every type referenced by an instruction and | 
 | 1423 |   // make a note of any global values or constants that are referenced | 
 | 1424 |   std::vector<GlobalValue*> gvs; | 
 | 1425 |   std::vector<Constant*> consts; | 
 | 1426 |   for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){ | 
 | 1427 |     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();  | 
 | 1428 |          I != E; ++I) { | 
 | 1429 |       // Print the type of the instruction itself | 
 | 1430 |       printType(I->getType()); | 
 | 1431 |  | 
 | 1432 |       // Print the type of each of the instruction's operands | 
 | 1433 |       for (unsigned i = 0; i < I->getNumOperands(); ++i) { | 
 | 1434 |         Value* operand = I->getOperand(i); | 
 | 1435 |         printType(operand->getType()); | 
 | 1436 |         if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) | 
 | 1437 |           gvs.push_back(GV); | 
 | 1438 |         else if (Constant* C = dyn_cast<Constant>(operand)) | 
 | 1439 |           consts.push_back(C); | 
 | 1440 |       } | 
 | 1441 |     } | 
 | 1442 |   } | 
 | 1443 |  | 
 | 1444 |   // Print the function declarations for any functions encountered | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1445 |   nl(Out) << "// Function Declarations"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1446 |   for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end(); | 
 | 1447 |        I != E; ++I) { | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1448 |     if (Function* Fun = dyn_cast<Function>(*I)) { | 
 | 1449 |       if (!is_inline || Fun != F) | 
 | 1450 |         printFunctionHead(Fun); | 
 | 1451 |     } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1452 |   } | 
 | 1453 |  | 
 | 1454 |   // Print the global variable declarations for any variables encountered | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1455 |   nl(Out) << "// Global Variable Declarations"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1456 |   for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end(); | 
 | 1457 |        I != E; ++I) { | 
 | 1458 |     if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I)) | 
 | 1459 |       printVariableHead(F); | 
 | 1460 |   } | 
 | 1461 |  | 
 | 1462 |   // Print the constants found | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1463 |   nl(Out) << "// Constant Definitions"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1464 |   for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end(); | 
 | 1465 |        I != E; ++I) { | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1466 |       printConstant(*I); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1467 |   } | 
 | 1468 |  | 
 | 1469 |   // Process the global variables definitions now that all the constants have | 
 | 1470 |   // been emitted. These definitions just couple the gvars with their constant | 
 | 1471 |   // initializers. | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1472 |   nl(Out) << "// Global Variable Definitions"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1473 |   for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end(); | 
 | 1474 |        I != E; ++I) { | 
 | 1475 |     if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I)) | 
 | 1476 |       printVariableBody(GV); | 
 | 1477 |   } | 
 | 1478 | } | 
 | 1479 |  | 
 | 1480 | void CppWriter::printFunctionHead(const Function* F) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1481 |   nl(Out) << "Function* " << getCppName(F);  | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1482 |   if (is_inline) { | 
 | 1483 |     Out << " = mod->getFunction(\""; | 
 | 1484 |     printEscapedString(F->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1485 |     Out << "\", " << getCppName(F->getFunctionType()) << ");"; | 
 | 1486 |     nl(Out) << "if (!" << getCppName(F) << ") {"; | 
 | 1487 |     nl(Out) << getCppName(F); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1488 |   } | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1489 |   Out<< " = new Function("; | 
 | 1490 |   nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ","; | 
 | 1491 |   nl(Out) << "/*Linkage=*/"; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1492 |   printLinkageType(F->getLinkage()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1493 |   Out << ","; | 
 | 1494 |   nl(Out) << "/*Name=*/\""; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1495 |   printEscapedString(F->getName()); | 
| Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1496 |   Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : ""); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1497 |   nl(Out,-1); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1498 |   printCppName(F); | 
 | 1499 |   Out << "->setCallingConv("; | 
 | 1500 |   printCallingConv(F->getCallingConv()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1501 |   Out << ");"; | 
 | 1502 |   nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1503 |   if (F->hasSection()) { | 
 | 1504 |     printCppName(F); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1505 |     Out << "->setSection(\"" << F->getSection() << "\");"; | 
 | 1506 |     nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1507 |   } | 
 | 1508 |   if (F->getAlignment()) { | 
 | 1509 |     printCppName(F); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1510 |     Out << "->setAlignment(" << F->getAlignment() << ");"; | 
 | 1511 |     nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1512 |   } | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1513 |   if (is_inline) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1514 |     Out << "}"; | 
 | 1515 |     nl(Out); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1516 |   } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1517 | } | 
 | 1518 |  | 
 | 1519 | void CppWriter::printFunctionBody(const Function *F) { | 
| Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1520 |   if (F->isDeclaration()) | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1521 |     return; // external functions have no bodies. | 
 | 1522 |  | 
 | 1523 |   // Clear the DefinedValues and ForwardRefs maps because we can't have  | 
 | 1524 |   // cross-function forward refs | 
 | 1525 |   ForwardRefs.clear(); | 
 | 1526 |   DefinedValues.clear(); | 
 | 1527 |  | 
 | 1528 |   // Create all the argument values | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1529 |   if (!is_inline) { | 
 | 1530 |     if (!F->arg_empty()) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1531 |       Out << "Function::arg_iterator args = " << getCppName(F)  | 
 | 1532 |           << "->arg_begin();"; | 
 | 1533 |       nl(Out); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1534 |     } | 
 | 1535 |     for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); | 
 | 1536 |          AI != AE; ++AI) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1537 |       Out << "Value* " << getCppName(AI) << " = args++;"; | 
 | 1538 |       nl(Out); | 
 | 1539 |       if (AI->hasName()) { | 
 | 1540 |         Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");"; | 
 | 1541 |         nl(Out); | 
 | 1542 |       } | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1543 |     } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1544 |   } | 
 | 1545 |  | 
 | 1546 |   // Create all the basic blocks | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1547 |   nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1548 |   for (Function::const_iterator BI = F->begin(), BE = F->end();  | 
 | 1549 |        BI != BE; ++BI) { | 
 | 1550 |     std::string bbname(getCppName(BI)); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1551 |     Out << "BasicBlock* " << bbname << " = new BasicBlock(\""; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1552 |     if (BI->hasName()) | 
 | 1553 |       printEscapedString(BI->getName()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1554 |     Out << "\"," << getCppName(BI->getParent()) << ",0);"; | 
 | 1555 |     nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1556 |   } | 
 | 1557 |  | 
 | 1558 |   // Output all of its basic blocks... for the function | 
 | 1559 |   for (Function::const_iterator BI = F->begin(), BE = F->end();  | 
 | 1560 |        BI != BE; ++BI) { | 
 | 1561 |     std::string bbname(getCppName(BI)); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1562 |     nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")"; | 
 | 1563 |     nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1564 |  | 
 | 1565 |     // Output all of the instructions in the basic block... | 
 | 1566 |     for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();  | 
 | 1567 |          I != E; ++I) { | 
 | 1568 |       printInstruction(I,bbname); | 
 | 1569 |     } | 
 | 1570 |   } | 
 | 1571 |  | 
 | 1572 |   // Loop over the ForwardRefs and resolve them now that all instructions | 
 | 1573 |   // are generated. | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1574 |   if (!ForwardRefs.empty()) { | 
 | 1575 |     nl(Out) << "// Resolve Forward References"; | 
 | 1576 |     nl(Out); | 
 | 1577 |   } | 
 | 1578 |    | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1579 |   while (!ForwardRefs.empty()) { | 
 | 1580 |     ForwardRefMap::iterator I = ForwardRefs.begin(); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1581 |     Out << I->second << "->replaceAllUsesWith("  | 
 | 1582 |         << getCppName(I->first) << "); delete " << I->second << ";"; | 
 | 1583 |     nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1584 |     ForwardRefs.erase(I); | 
 | 1585 |   } | 
 | 1586 | } | 
 | 1587 |  | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1588 | void CppWriter::printInline(const std::string& fname, const std::string& func) { | 
| Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 1589 |   const Function* F = TheModule->getFunction(func); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1590 |   if (!F) { | 
 | 1591 |     error(std::string("Function '") + func + "' not found in input module"); | 
 | 1592 |     return; | 
 | 1593 |   } | 
| Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1594 |   if (F->isDeclaration()) { | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1595 |     error(std::string("Function '") + func + "' is external!"); | 
 | 1596 |     return; | 
 | 1597 |   } | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1598 |   nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"  | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1599 |       << getCppName(F); | 
 | 1600 |   unsigned arg_count = 1; | 
 | 1601 |   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); | 
 | 1602 |        AI != AE; ++AI) { | 
 | 1603 |     Out << ", Value* arg_" << arg_count; | 
 | 1604 |   } | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1605 |   Out << ") {"; | 
 | 1606 |   nl(Out); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1607 |   is_inline = true; | 
 | 1608 |   printFunctionUses(F); | 
 | 1609 |   printFunctionBody(F); | 
 | 1610 |   is_inline = false; | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1611 |   Out << "return " << getCppName(F->begin()) << ";"; | 
 | 1612 |   nl(Out) << "}"; | 
 | 1613 |   nl(Out); | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1614 | } | 
 | 1615 |  | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1616 | void CppWriter::printModuleBody() { | 
 | 1617 |   // Print out all the type definitions | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1618 |   nl(Out) << "// Type Definitions"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1619 |   printTypes(TheModule); | 
 | 1620 |  | 
 | 1621 |   // Functions can call each other and global variables can reference them so  | 
 | 1622 |   // define all the functions first before emitting their function bodies. | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1623 |   nl(Out) << "// Function Declarations"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1624 |   for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();  | 
 | 1625 |        I != E; ++I) | 
 | 1626 |     printFunctionHead(I); | 
 | 1627 |  | 
 | 1628 |   // Process the global variables declarations. We can't initialze them until | 
 | 1629 |   // after the constants are printed so just print a header for each global | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1630 |   nl(Out) << "// Global Variable Declarations\n"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1631 |   for (Module::const_global_iterator I = TheModule->global_begin(),  | 
 | 1632 |        E = TheModule->global_end(); I != E; ++I) { | 
 | 1633 |     printVariableHead(I); | 
 | 1634 |   } | 
 | 1635 |  | 
 | 1636 |   // Print out all the constants definitions. Constants don't recurse except | 
 | 1637 |   // through GlobalValues. All GlobalValues have been declared at this point | 
 | 1638 |   // so we can proceed to generate the constants. | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1639 |   nl(Out) << "// Constant Definitions"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1640 |   printConstants(TheModule); | 
 | 1641 |  | 
 | 1642 |   // Process the global variables definitions now that all the constants have | 
 | 1643 |   // been emitted. These definitions just couple the gvars with their constant | 
 | 1644 |   // initializers. | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1645 |   nl(Out) << "// Global Variable Definitions"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1646 |   for (Module::const_global_iterator I = TheModule->global_begin(),  | 
 | 1647 |        E = TheModule->global_end(); I != E; ++I) { | 
 | 1648 |     printVariableBody(I); | 
 | 1649 |   } | 
 | 1650 |  | 
 | 1651 |   // Finally, we can safely put out all of the function bodies. | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1652 |   nl(Out) << "// Function Definitions"; nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1653 |   for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();  | 
 | 1654 |        I != E; ++I) { | 
| Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1655 |     if (!I->isDeclaration()) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1656 |       nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)  | 
 | 1657 |           << ")"; | 
 | 1658 |       nl(Out) << "{"; | 
 | 1659 |       nl(Out,1); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1660 |       printFunctionBody(I); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1661 |       nl(Out,-1) << "}"; | 
 | 1662 |       nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1663 |     } | 
 | 1664 |   } | 
 | 1665 | } | 
 | 1666 |  | 
 | 1667 | void CppWriter::printProgram( | 
 | 1668 |   const std::string& fname,  | 
 | 1669 |   const std::string& mName | 
 | 1670 | ) { | 
 | 1671 |   Out << "#include <llvm/Module.h>\n"; | 
 | 1672 |   Out << "#include <llvm/DerivedTypes.h>\n"; | 
 | 1673 |   Out << "#include <llvm/Constants.h>\n"; | 
 | 1674 |   Out << "#include <llvm/GlobalVariable.h>\n"; | 
 | 1675 |   Out << "#include <llvm/Function.h>\n"; | 
 | 1676 |   Out << "#include <llvm/CallingConv.h>\n"; | 
 | 1677 |   Out << "#include <llvm/BasicBlock.h>\n"; | 
 | 1678 |   Out << "#include <llvm/Instructions.h>\n"; | 
 | 1679 |   Out << "#include <llvm/InlineAsm.h>\n"; | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1680 |   Out << "#include <llvm/ParameterAttributes.h>\n"; | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1681 |   Out << "#include <llvm/Support/MathExtras.h>\n"; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1682 |   Out << "#include <llvm/Pass.h>\n"; | 
 | 1683 |   Out << "#include <llvm/PassManager.h>\n"; | 
 | 1684 |   Out << "#include <llvm/Analysis/Verifier.h>\n"; | 
 | 1685 |   Out << "#include <llvm/Assembly/PrintModulePass.h>\n"; | 
 | 1686 |   Out << "#include <algorithm>\n"; | 
 | 1687 |   Out << "#include <iostream>\n\n"; | 
 | 1688 |   Out << "using namespace llvm;\n\n"; | 
 | 1689 |   Out << "Module* " << fname << "();\n\n"; | 
 | 1690 |   Out << "int main(int argc, char**argv) {\n"; | 
 | 1691 |   Out << "  Module* Mod = makeLLVMModule();\n"; | 
 | 1692 |   Out << "  verifyModule(*Mod, PrintMessageAction);\n"; | 
 | 1693 |   Out << "  std::cerr.flush();\n"; | 
 | 1694 |   Out << "  std::cout.flush();\n"; | 
 | 1695 |   Out << "  PassManager PM;\n"; | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1696 |   Out << "  PM.add(new PrintModulePass(&llvm::cout));\n"; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1697 |   Out << "  PM.run(*Mod);\n"; | 
 | 1698 |   Out << "  return 0;\n"; | 
 | 1699 |   Out << "}\n\n"; | 
 | 1700 |   printModule(fname,mName); | 
 | 1701 | } | 
 | 1702 |  | 
 | 1703 | void CppWriter::printModule( | 
 | 1704 |   const std::string& fname,  | 
 | 1705 |   const std::string& mName | 
 | 1706 | ) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1707 |   nl(Out) << "Module* " << fname << "() {"; | 
 | 1708 |   nl(Out,1) << "// Module Construction"; | 
 | 1709 |   nl(Out) << "Module* mod = new Module(\"" << mName << "\");";  | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1710 |   if (!TheModule->getTargetTriple().empty()) { | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1711 |     nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");"; | 
 | 1712 |   } | 
 | 1713 |   if (!TheModule->getTargetTriple().empty()) { | 
 | 1714 |     nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()  | 
 | 1715 |             << "\");"; | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1716 |   } | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1717 |  | 
 | 1718 |   if (!TheModule->getModuleInlineAsm().empty()) { | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1719 |     nl(Out) << "mod->setModuleInlineAsm(\""; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1720 |     printEscapedString(TheModule->getModuleInlineAsm()); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1721 |     Out << "\");"; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1722 |   } | 
| Reid Spencer | 0744166 | 2007-04-11 12:28:56 +0000 | [diff] [blame] | 1723 |   nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1724 |    | 
 | 1725 |   // Loop over the dependent libraries and emit them. | 
 | 1726 |   Module::lib_iterator LI = TheModule->lib_begin(); | 
 | 1727 |   Module::lib_iterator LE = TheModule->lib_end(); | 
 | 1728 |   while (LI != LE) { | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1729 |     Out << "mod->addLibrary(\"" << *LI << "\");"; | 
 | 1730 |     nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1731 |     ++LI; | 
 | 1732 |   } | 
 | 1733 |   printModuleBody(); | 
| Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1734 |   nl(Out) << "return mod;"; | 
 | 1735 |   nl(Out,-1) << "}"; | 
 | 1736 |   nl(Out); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1737 | } | 
 | 1738 |  | 
 | 1739 | void CppWriter::printContents( | 
 | 1740 |   const std::string& fname, // Name of generated function | 
 | 1741 |   const std::string& mName // Name of module generated module | 
 | 1742 | ) { | 
 | 1743 |   Out << "\nModule* " << fname << "(Module *mod) {\n"; | 
 | 1744 |   Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n"; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1745 |   printModuleBody(); | 
 | 1746 |   Out << "\nreturn mod;\n"; | 
 | 1747 |   Out << "\n}\n"; | 
 | 1748 | } | 
 | 1749 |  | 
 | 1750 | void CppWriter::printFunction( | 
 | 1751 |   const std::string& fname, // Name of generated function | 
 | 1752 |   const std::string& funcName // Name of function to generate | 
 | 1753 | ) { | 
| Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 1754 |   const Function* F = TheModule->getFunction(funcName); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1755 |   if (!F) { | 
 | 1756 |     error(std::string("Function '") + funcName + "' not found in input module"); | 
 | 1757 |     return; | 
 | 1758 |   } | 
 | 1759 |   Out << "\nFunction* " << fname << "(Module *mod) {\n"; | 
 | 1760 |   printFunctionUses(F); | 
 | 1761 |   printFunctionHead(F); | 
 | 1762 |   printFunctionBody(F); | 
 | 1763 |   Out << "return " << getCppName(F) << ";\n"; | 
 | 1764 |   Out << "}\n"; | 
 | 1765 | } | 
 | 1766 |  | 
 | 1767 | void CppWriter::printVariable( | 
 | 1768 |   const std::string& fname,  /// Name of generated function | 
 | 1769 |   const std::string& varName // Name of variable to generate | 
 | 1770 | ) { | 
 | 1771 |   const GlobalVariable* GV = TheModule->getNamedGlobal(varName); | 
 | 1772 |  | 
 | 1773 |   if (!GV) { | 
 | 1774 |     error(std::string("Variable '") + varName + "' not found in input module"); | 
 | 1775 |     return; | 
 | 1776 |   } | 
 | 1777 |   Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n"; | 
 | 1778 |   printVariableUses(GV); | 
 | 1779 |   printVariableHead(GV); | 
 | 1780 |   printVariableBody(GV); | 
 | 1781 |   Out << "return " << getCppName(GV) << ";\n"; | 
 | 1782 |   Out << "}\n"; | 
 | 1783 | } | 
 | 1784 |  | 
 | 1785 | void CppWriter::printType( | 
 | 1786 |   const std::string& fname,  /// Name of generated function | 
 | 1787 |   const std::string& typeName // Name of type to generate | 
 | 1788 | ) { | 
 | 1789 |   const Type* Ty = TheModule->getTypeByName(typeName); | 
 | 1790 |   if (!Ty) { | 
 | 1791 |     error(std::string("Type '") + typeName + "' not found in input module"); | 
 | 1792 |     return; | 
 | 1793 |   } | 
 | 1794 |   Out << "\nType* " << fname << "(Module *mod) {\n"; | 
 | 1795 |   printType(Ty); | 
 | 1796 |   Out << "return " << getCppName(Ty) << ";\n"; | 
 | 1797 |   Out << "}\n"; | 
 | 1798 | } | 
 | 1799 |  | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1800 | }  // end anonymous llvm | 
 | 1801 |  | 
 | 1802 | namespace llvm { | 
 | 1803 |  | 
 | 1804 | void WriteModuleToCppFile(Module* mod, std::ostream& o) { | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1805 |   // Initialize a CppWriter for us to use | 
 | 1806 |   CppWriter W(o, mod); | 
 | 1807 |  | 
 | 1808 |   // Emit a header | 
| Reid Spencer | 25edc35 | 2006-05-31 04:43:19 +0000 | [diff] [blame] | 1809 |   o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n"; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1810 |  | 
 | 1811 |   // Get the name of the function we're supposed to generate | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1812 |   std::string fname = FuncName.getValue(); | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1813 |  | 
 | 1814 |   // Get the name of the thing we are to generate | 
 | 1815 |   std::string tgtname = NameToGenerate.getValue(); | 
 | 1816 |   if (GenerationType == GenModule ||  | 
 | 1817 |       GenerationType == GenContents ||  | 
 | 1818 |       GenerationType == GenProgram) { | 
 | 1819 |     if (tgtname == "!bad!") { | 
 | 1820 |       if (mod->getModuleIdentifier() == "-") | 
 | 1821 |         tgtname = "<stdin>"; | 
 | 1822 |       else | 
 | 1823 |         tgtname = mod->getModuleIdentifier(); | 
 | 1824 |     } | 
 | 1825 |   } else if (tgtname == "!bad!") { | 
 | 1826 |     W.error("You must use the -for option with -gen-{function,variable,type}"); | 
 | 1827 |   } | 
 | 1828 |  | 
 | 1829 |   switch (WhatToGenerate(GenerationType)) { | 
 | 1830 |     case GenProgram: | 
 | 1831 |       if (fname.empty()) | 
 | 1832 |         fname = "makeLLVMModule"; | 
 | 1833 |       W.printProgram(fname,tgtname); | 
 | 1834 |       break; | 
 | 1835 |     case GenModule: | 
 | 1836 |       if (fname.empty()) | 
 | 1837 |         fname = "makeLLVMModule"; | 
 | 1838 |       W.printModule(fname,tgtname); | 
 | 1839 |       break; | 
 | 1840 |     case GenContents: | 
 | 1841 |       if (fname.empty()) | 
 | 1842 |         fname = "makeLLVMModuleContents"; | 
 | 1843 |       W.printContents(fname,tgtname); | 
 | 1844 |       break; | 
 | 1845 |     case GenFunction: | 
 | 1846 |       if (fname.empty()) | 
 | 1847 |         fname = "makeLLVMFunction"; | 
 | 1848 |       W.printFunction(fname,tgtname); | 
 | 1849 |       break; | 
| Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1850 |     case GenInline: | 
 | 1851 |       if (fname.empty()) | 
 | 1852 |         fname = "makeLLVMInline"; | 
 | 1853 |       W.printInline(fname,tgtname); | 
 | 1854 |       break; | 
| Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1855 |     case GenVariable: | 
 | 1856 |       if (fname.empty()) | 
 | 1857 |         fname = "makeLLVMVariable"; | 
 | 1858 |       W.printVariable(fname,tgtname); | 
 | 1859 |       break; | 
 | 1860 |     case GenType: | 
 | 1861 |       if (fname.empty()) | 
 | 1862 |         fname = "makeLLVMType"; | 
 | 1863 |       W.printType(fname,tgtname); | 
 | 1864 |       break; | 
 | 1865 |     default: | 
 | 1866 |       W.error("Invalid generation option"); | 
| Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1867 |   } | 
| Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1868 | } | 
 | 1869 |  | 
 | 1870 | } |