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" |
| 21 | #include "llvm/Module.h" |
| 22 | #include "llvm/SymbolTable.h" |
| 23 | #include "llvm/Support/CFG.h" |
| 24 | #include "llvm/ADT/StringExtras.h" |
| 25 | #include "llvm/ADT/STLExtras.h" |
| 26 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 27 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <iostream> |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 30 | #include <set> |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 34 | static cl::opt<std::string> |
| 35 | ModName("modname", cl::desc("Specify the module name to use"), |
| 36 | cl::value_desc("module name")); |
| 37 | |
| 38 | static cl::opt<std::string> |
| 39 | FuncName("funcname", cl::desc("Specify the name of the generated function"), |
| 40 | cl::value_desc("function name")); |
| 41 | |
| 42 | static cl::opt<bool> |
| 43 | Fragment("fragment", cl::desc("Don't generate a complete program")); |
| 44 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 45 | namespace { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 46 | typedef std::vector<const Type*> TypeList; |
| 47 | typedef std::map<const Type*,std::string> TypeMap; |
| 48 | typedef std::map<const Value*,std::string> ValueMap; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 49 | typedef std::set<std::string> NameSet; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 50 | typedef std::set<const Type*> TypeSet; |
| 51 | typedef std::set<const Value*> ValueSet; |
| 52 | typedef std::map<const Value*,std::string> ForwardRefMap; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 53 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 54 | class CppWriter { |
| 55 | std::ostream &Out; |
| 56 | const Module *TheModule; |
| 57 | unsigned long uniqueNum; |
| 58 | TypeMap TypeNames; |
| 59 | ValueMap ValueNames; |
| 60 | TypeMap UnresolvedTypes; |
| 61 | TypeList TypeStack; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 62 | NameSet UsedNames; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 63 | TypeSet DefinedTypes; |
| 64 | ValueSet DefinedValues; |
| 65 | ForwardRefMap ForwardRefs; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 66 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 67 | public: |
| 68 | inline CppWriter(std::ostream &o, const Module *M) |
| 69 | : Out(o), TheModule(M), uniqueNum(0), TypeNames(), |
| 70 | ValueNames(), UnresolvedTypes(), TypeStack() { } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 71 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 72 | const Module* getModule() { return TheModule; } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 73 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 74 | void printModule(const Module *M); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 75 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 76 | private: |
| 77 | void printTypes(const Module* M); |
| 78 | void printConstants(const Module* M); |
| 79 | void printConstant(const Constant *CPV); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 80 | void printGlobalHead(const GlobalVariable *GV); |
| 81 | void printGlobalBody(const GlobalVariable *GV); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 82 | void printFunctionHead(const Function *F); |
| 83 | void printFunctionBody(const Function *F); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 84 | void printInstruction(const Instruction *I, const std::string& bbname); |
| 85 | void printSymbolTable(const SymbolTable &ST); |
| 86 | void printLinkageType(GlobalValue::LinkageTypes LT); |
| 87 | void printCallingConv(unsigned cc); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 88 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 89 | std::string getCppName(const Type* val); |
| 90 | std::string getCppName(const Value* val); |
| 91 | inline void printCppName(const Value* val); |
| 92 | inline void printCppName(const Type* val); |
| 93 | bool isOnStack(const Type*) const; |
| 94 | inline void printTypeDef(const Type* Ty); |
| 95 | bool printTypeDefInternal(const Type* Ty); |
| 96 | void printEscapedString(const std::string& str); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 97 | |
| 98 | std::string getOpName(Value*); |
| 99 | |
| 100 | void printCFP(const ConstantFP* CFP); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 101 | }; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 102 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 103 | // printCFP - Print a floating point constant .. very carefully :) |
| 104 | // This makes sure that conversion to/from floating yields the same binary |
| 105 | // result so that we don't lose precision. |
| 106 | void |
| 107 | CppWriter::printCFP(const ConstantFP *CFP) { |
| 108 | #if HAVE_PRINTF_A |
| 109 | char Buffer[100]; |
| 110 | sprintf(Buffer, "%A", CFP->getValue()); |
| 111 | if ((!strncmp(Buffer, "0x", 2) || |
| 112 | !strncmp(Buffer, "-0x", 3) || |
| 113 | !strncmp(Buffer, "+0x", 3)) && |
| 114 | (atof(Buffer) == CFP->getValue())) |
| 115 | Out << Buffer; |
| 116 | else { |
| 117 | #else |
| 118 | std::string StrVal = ftostr(CFP->getValue()); |
| 119 | |
| 120 | while (StrVal[0] == ' ') |
| 121 | StrVal.erase(StrVal.begin()); |
| 122 | |
| 123 | // Check to make sure that the stringized number is not some string like "Inf" |
| 124 | // or NaN. Check that the string matches the "[-+]?[0-9]" regex. |
| 125 | if (((StrVal[0] >= '0' && StrVal[0] <= '9') || |
| 126 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
| 127 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) && |
| 128 | (atof(StrVal.c_str()) == CFP->getValue())) |
| 129 | Out << StrVal; |
| 130 | else if (CFP->getType() == Type::DoubleTy) { |
| 131 | Out << "0x" << std::hex << DoubleToBits(CFP->getValue()) << std::dec |
| 132 | << "ULL /* " << StrVal << " */"; |
| 133 | } else { |
| 134 | Out << "0x" << std::hex << FloatToBits(CFP->getValue()) << std::dec |
| 135 | << "U /* " << StrVal << " */"; |
| 136 | } |
| 137 | #endif |
| 138 | #if HAVE_PRINTF_A |
| 139 | } |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | std::string |
| 144 | CppWriter::getOpName(Value* V) { |
| 145 | if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end()) |
| 146 | return getCppName(V); |
| 147 | |
| 148 | // See if its alread in the map of forward references, if so just return the |
| 149 | // name we already set up for it |
| 150 | ForwardRefMap::const_iterator I = ForwardRefs.find(V); |
| 151 | if (I != ForwardRefs.end()) |
| 152 | return I->second; |
| 153 | |
| 154 | // This is a new forward reference. Generate a unique name for it |
| 155 | std::string result(std::string("fwdref_") + utostr(uniqueNum++)); |
| 156 | |
| 157 | // Yes, this is a hack. An Argument is the smallest instantiable value that |
| 158 | // we can make as a placeholder for the real value. We'll replace these |
| 159 | // Argument instances later. |
| 160 | Out << " Argument* " << result << " = new Argument(" |
| 161 | << getCppName(V->getType()) << ");\n"; |
| 162 | ForwardRefs[V] = result; |
| 163 | return result; |
| 164 | } |
| 165 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 166 | // printEscapedString - Print each character of the specified string, escaping |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 167 | // 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] | 168 | void |
| 169 | CppWriter::printEscapedString(const std::string &Str) { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 170 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 171 | unsigned char C = Str[i]; |
| 172 | if (isprint(C) && C != '"' && C != '\\') { |
| 173 | Out << C; |
| 174 | } else { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 175 | Out << "\\x" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 176 | << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A')) |
| 177 | << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A')); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 182 | inline void |
| 183 | sanitize(std::string& str) { |
| 184 | for (size_t i = 0; i < str.length(); ++i) |
| 185 | if (!isalnum(str[i]) && str[i] != '_') |
| 186 | str[i] = '_'; |
| 187 | } |
| 188 | |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 189 | inline const char* |
| 190 | getTypePrefix(const Type* Ty ) { |
| 191 | const char* prefix; |
| 192 | switch (Ty->getTypeID()) { |
| 193 | case Type::VoidTyID: prefix = "void_"; break; |
| 194 | case Type::BoolTyID: prefix = "bool_"; break; |
| 195 | case Type::UByteTyID: prefix = "ubyte_"; break; |
| 196 | case Type::SByteTyID: prefix = "sbyte_"; break; |
| 197 | case Type::UShortTyID: prefix = "ushort_"; break; |
| 198 | case Type::ShortTyID: prefix = "short_"; break; |
| 199 | case Type::UIntTyID: prefix = "uint_"; break; |
| 200 | case Type::IntTyID: prefix = "int_"; break; |
| 201 | case Type::ULongTyID: prefix = "ulong_"; break; |
| 202 | case Type::LongTyID: prefix = "long_"; break; |
| 203 | case Type::FloatTyID: prefix = "float_"; break; |
| 204 | case Type::DoubleTyID: prefix = "double_"; break; |
| 205 | case Type::LabelTyID: prefix = "label_"; break; |
| 206 | case Type::FunctionTyID: prefix = "func_"; break; |
| 207 | case Type::StructTyID: prefix = "struct_"; break; |
| 208 | case Type::ArrayTyID: prefix = "array_"; break; |
| 209 | case Type::PointerTyID: prefix = "ptr_"; break; |
| 210 | case Type::PackedTyID: prefix = "packed_"; break; |
| 211 | case Type::OpaqueTyID: prefix = "opaque_"; break; |
| 212 | default: prefix = "other_"; break; |
| 213 | } |
| 214 | return prefix; |
| 215 | } |
| 216 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 217 | std::string |
| 218 | CppWriter::getCppName(const Value* val) { |
| 219 | std::string name; |
| 220 | ValueMap::iterator I = ValueNames.find(val); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 221 | if (I != ValueNames.end() && I->first == val) |
| 222 | return I->second; |
| 223 | |
| 224 | if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) { |
| 225 | name = std::string("gvar_") + |
| 226 | getTypePrefix(GV->getType()->getElementType()); |
| 227 | } else if (const Function* F = dyn_cast<Function>(val)) { |
| 228 | name = std::string("func_"); |
| 229 | } else if (const Constant* C = dyn_cast<Constant>(val)) { |
| 230 | name = std::string("const_") + getTypePrefix(C->getType()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 231 | } else { |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 232 | name = getTypePrefix(val->getType()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 233 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 234 | name += (val->hasName() ? val->getName() : utostr(uniqueNum++)); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 235 | sanitize(name); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 236 | NameSet::iterator NI = UsedNames.find(name); |
| 237 | if (NI != UsedNames.end()) |
| 238 | name += std::string("_") + utostr(uniqueNum++); |
| 239 | UsedNames.insert(name); |
| 240 | return ValueNames[val] = name; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void |
| 244 | CppWriter::printCppName(const Value* val) { |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 245 | printEscapedString(getCppName(val)); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
| 249 | CppWriter::printCppName(const Type* Ty) |
| 250 | { |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 251 | printEscapedString(getCppName(Ty)); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // Gets the C++ name for a type. Returns true if we already saw the type, |
| 255 | // false otherwise. |
| 256 | // |
| 257 | inline const std::string* |
| 258 | findTypeName(const SymbolTable& ST, const Type* Ty) |
| 259 | { |
| 260 | SymbolTable::type_const_iterator TI = ST.type_begin(); |
| 261 | SymbolTable::type_const_iterator TE = ST.type_end(); |
| 262 | for (;TI != TE; ++TI) |
| 263 | if (TI->second == Ty) |
| 264 | return &(TI->first); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | std::string |
| 269 | CppWriter::getCppName(const Type* Ty) |
| 270 | { |
| 271 | // First, handle the primitive types .. easy |
| 272 | if (Ty->isPrimitiveType()) { |
| 273 | switch (Ty->getTypeID()) { |
| 274 | case Type::VoidTyID: return "Type::VoidTy"; |
| 275 | case Type::BoolTyID: return "Type::BoolTy"; |
| 276 | case Type::UByteTyID: return "Type::UByteTy"; |
| 277 | case Type::SByteTyID: return "Type::SByteTy"; |
| 278 | case Type::UShortTyID: return "Type::UShortTy"; |
| 279 | case Type::ShortTyID: return "Type::ShortTy"; |
| 280 | case Type::UIntTyID: return "Type::UIntTy"; |
| 281 | case Type::IntTyID: return "Type::IntTy"; |
| 282 | case Type::ULongTyID: return "Type::ULongTy"; |
| 283 | case Type::LongTyID: return "Type::LongTy"; |
| 284 | case Type::FloatTyID: return "Type::FloatTy"; |
| 285 | case Type::DoubleTyID: return "Type::DoubleTy"; |
| 286 | case Type::LabelTyID: return "Type::LabelTy"; |
| 287 | default: |
| 288 | assert(!"Can't get here"); |
| 289 | break; |
| 290 | } |
| 291 | return "Type::VoidTy"; // shouldn't be returned, but make it sensible |
| 292 | } |
| 293 | |
| 294 | // Now, see if we've seen the type before and return that |
| 295 | TypeMap::iterator I = TypeNames.find(Ty); |
| 296 | if (I != TypeNames.end()) |
| 297 | return I->second; |
| 298 | |
| 299 | // Okay, let's build a new name for this type. Start with a prefix |
| 300 | const char* prefix = 0; |
| 301 | switch (Ty->getTypeID()) { |
| 302 | case Type::FunctionTyID: prefix = "FuncTy_"; break; |
| 303 | case Type::StructTyID: prefix = "StructTy_"; break; |
| 304 | case Type::ArrayTyID: prefix = "ArrayTy_"; break; |
| 305 | case Type::PointerTyID: prefix = "PointerTy_"; break; |
| 306 | case Type::OpaqueTyID: prefix = "OpaqueTy_"; break; |
| 307 | case Type::PackedTyID: prefix = "PackedTy_"; break; |
| 308 | default: prefix = "OtherTy_"; break; // prevent breakage |
| 309 | } |
| 310 | |
| 311 | // See if the type has a name in the symboltable and build accordingly |
| 312 | const std::string* tName = findTypeName(TheModule->getSymbolTable(), Ty); |
| 313 | std::string name; |
| 314 | if (tName) |
| 315 | name = std::string(prefix) + *tName; |
| 316 | else |
| 317 | name = std::string(prefix) + utostr(uniqueNum++); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 318 | sanitize(name); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 319 | |
| 320 | // Save the name |
| 321 | return TypeNames[Ty] = name; |
| 322 | } |
| 323 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 324 | void CppWriter::printModule(const Module *M) { |
| 325 | Out << "\n// Module Construction\n"; |
| 326 | Out << "Module* mod = new Module(\""; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 327 | if (!ModName.empty()) |
| 328 | printEscapedString(ModName); |
| 329 | else if (M->getModuleIdentifier() == "-") |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 330 | printEscapedString("<stdin>"); |
| 331 | else |
| 332 | printEscapedString(M->getModuleIdentifier()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 333 | Out << "\");\n"; |
| 334 | Out << "mod->setEndianness("; |
| 335 | switch (M->getEndianness()) { |
| 336 | case Module::LittleEndian: Out << "Module::LittleEndian);\n"; break; |
| 337 | case Module::BigEndian: Out << "Module::BigEndian);\n"; break; |
| 338 | case Module::AnyEndianness:Out << "Module::AnyEndianness);\n"; break; |
| 339 | } |
| 340 | Out << "mod->setPointerSize("; |
| 341 | switch (M->getPointerSize()) { |
| 342 | case Module::Pointer32: Out << "Module::Pointer32);\n"; break; |
| 343 | case Module::Pointer64: Out << "Module::Pointer64);\n"; break; |
| 344 | case Module::AnyPointerSize: Out << "Module::AnyPointerSize);\n"; break; |
| 345 | } |
| 346 | if (!M->getTargetTriple().empty()) |
| 347 | Out << "mod->setTargetTriple(\"" << M->getTargetTriple() << "\");\n"; |
| 348 | |
| 349 | if (!M->getModuleInlineAsm().empty()) { |
| 350 | Out << "mod->setModuleInlineAsm(\""; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 351 | printEscapedString(M->getModuleInlineAsm()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 352 | Out << "\");\n"; |
| 353 | } |
| 354 | |
| 355 | // Loop over the dependent libraries and emit them. |
| 356 | Module::lib_iterator LI = M->lib_begin(); |
| 357 | Module::lib_iterator LE = M->lib_end(); |
| 358 | while (LI != LE) { |
| 359 | Out << "mod->addLibrary(\"" << *LI << "\");\n"; |
| 360 | ++LI; |
| 361 | } |
| 362 | |
| 363 | // Print out all the type definitions |
| 364 | Out << "\n// Type Definitions\n"; |
| 365 | printTypes(M); |
| 366 | |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 367 | // Functions can call each other and global variables can reference them so |
| 368 | // define all the functions first before emitting their function bodies. |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 369 | Out << "\n// Function Declarations\n"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 370 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 371 | printFunctionHead(I); |
| 372 | |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 373 | // Process the global variables declarations. We can't initialze them until |
| 374 | // after the constants are printed so just print a header for each global |
| 375 | Out << "\n// Global Variable Declarations\n"; |
| 376 | for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
| 377 | I != E; ++I) { |
| 378 | printGlobalHead(I); |
| 379 | } |
| 380 | |
| 381 | // Print out all the constants definitions. Constants don't recurse except |
| 382 | // through GlobalValues. All GlobalValues have been declared at this point |
| 383 | // so we can proceed to generate the constants. |
| 384 | Out << "\n// Constant Definitions\n"; |
| 385 | printConstants(M); |
| 386 | |
| 387 | // Process the global variables definitions now that all the constants have |
| 388 | // been emitted. These definitions just couple the gvars with their constant |
| 389 | // initializers. |
| 390 | Out << "\n// Global Variable Definitions\n"; |
| 391 | for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
| 392 | I != E; ++I) { |
| 393 | printGlobalBody(I); |
| 394 | } |
| 395 | |
| 396 | // Finally, we can safely put out all of the function bodies. |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 397 | Out << "\n// Function Definitions\n"; |
| 398 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 399 | if (!I->isExternal()) { |
| 400 | Out << "\n// Function: " << I->getName() << " (" << getCppName(I) |
| 401 | << ")\n{\n"; |
| 402 | printFunctionBody(I); |
| 403 | Out << "}\n"; |
| 404 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 405 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | void |
| 409 | CppWriter::printCallingConv(unsigned cc){ |
| 410 | // Print the calling convention. |
| 411 | switch (cc) { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 412 | case CallingConv::C: Out << "CallingConv::C"; break; |
| 413 | case CallingConv::CSRet: Out << "CallingConv::CSRet"; break; |
| 414 | case CallingConv::Fast: Out << "CallingConv::Fast"; break; |
| 415 | case CallingConv::Cold: Out << "CallingConv::Cold"; break; |
| 416 | case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 417 | default: Out << cc; break; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | void |
| 422 | CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) { |
| 423 | switch (LT) { |
| 424 | case GlobalValue::InternalLinkage: |
| 425 | Out << "GlobalValue::InternalLinkage"; break; |
| 426 | case GlobalValue::LinkOnceLinkage: |
| 427 | Out << "GlobalValue::LinkOnceLinkage "; break; |
| 428 | case GlobalValue::WeakLinkage: |
| 429 | Out << "GlobalValue::WeakLinkage"; break; |
| 430 | case GlobalValue::AppendingLinkage: |
| 431 | Out << "GlobalValue::AppendingLinkage"; break; |
| 432 | case GlobalValue::ExternalLinkage: |
| 433 | Out << "GlobalValue::ExternalLinkage"; break; |
| 434 | case GlobalValue::GhostLinkage: |
| 435 | Out << "GlobalValue::GhostLinkage"; break; |
| 436 | } |
| 437 | } |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 438 | |
| 439 | void CppWriter::printGlobalHead(const GlobalVariable *GV) { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 440 | Out << "\n"; |
| 441 | Out << "GlobalVariable* "; |
| 442 | printCppName(GV); |
| 443 | Out << " = new GlobalVariable(\n"; |
| 444 | Out << " /*Type=*/"; |
| 445 | printCppName(GV->getType()->getElementType()); |
| 446 | Out << ",\n"; |
| 447 | Out << " /*isConstant=*/" << (GV->isConstant()?"true":"false") |
| 448 | << ",\n /*Linkage=*/"; |
| 449 | printLinkageType(GV->getLinkage()); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 450 | Out << ",\n /*Initializer=*/0, "; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 451 | if (GV->hasInitializer()) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 452 | Out << "// has initializer, specified below"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 453 | } |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 454 | Out << "\n /*Name=*/\""; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 455 | printEscapedString(GV->getName()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 456 | Out << "\",\n mod);\n"; |
| 457 | |
| 458 | if (GV->hasSection()) { |
| 459 | printCppName(GV); |
| 460 | Out << "->setSection(\""; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 461 | printEscapedString(GV->getSection()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 462 | Out << "\");\n"; |
| 463 | } |
| 464 | if (GV->getAlignment()) { |
| 465 | printCppName(GV); |
| 466 | Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");\n"; |
| 467 | }; |
| 468 | } |
| 469 | |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 470 | void |
| 471 | CppWriter::printGlobalBody(const GlobalVariable *GV) { |
| 472 | if (GV->hasInitializer()) { |
| 473 | printCppName(GV); |
| 474 | Out << "->setInitializer("; |
| 475 | //if (!isa<GlobalValue(GV->getInitializer())) |
| 476 | //else |
| 477 | Out << getCppName(GV->getInitializer()) << ");\n"; |
| 478 | } |
| 479 | } |
| 480 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 481 | bool |
| 482 | CppWriter::isOnStack(const Type* Ty) const { |
| 483 | TypeList::const_iterator TI = |
| 484 | std::find(TypeStack.begin(),TypeStack.end(),Ty); |
| 485 | return TI != TypeStack.end(); |
| 486 | } |
| 487 | |
| 488 | // Prints a type definition. Returns true if it could not resolve all the types |
| 489 | // in the definition but had to use a forward reference. |
| 490 | void |
| 491 | CppWriter::printTypeDef(const Type* Ty) { |
| 492 | assert(TypeStack.empty()); |
| 493 | TypeStack.clear(); |
| 494 | printTypeDefInternal(Ty); |
| 495 | assert(TypeStack.empty()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | bool |
| 499 | CppWriter::printTypeDefInternal(const Type* Ty) { |
| 500 | // We don't print definitions for primitive types |
| 501 | if (Ty->isPrimitiveType()) |
| 502 | return false; |
| 503 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 504 | // If we already defined this type, we don't need to define it again. |
| 505 | if (DefinedTypes.find(Ty) != DefinedTypes.end()) |
| 506 | return false; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 507 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 508 | // Everything below needs the name for the type so get it now. |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 509 | std::string typeName(getCppName(Ty)); |
| 510 | |
| 511 | // Search the type stack for recursion. If we find it, then generate this |
| 512 | // as an OpaqueType, but make sure not to do this multiple times because |
| 513 | // 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^] | 514 | // 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] | 515 | // check the UnresolvedTypes list as well. |
| 516 | if (isOnStack(Ty)) { |
| 517 | TypeMap::const_iterator I = UnresolvedTypes.find(Ty); |
| 518 | if (I == UnresolvedTypes.end()) { |
| 519 | Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();\n"; |
| 520 | UnresolvedTypes[Ty] = typeName; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 521 | } |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 522 | return true; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 525 | // We're going to print a derived type which, by definition, contains other |
| 526 | // types. So, push this one we're printing onto the type stack to assist with |
| 527 | // recursive definitions. |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 528 | TypeStack.push_back(Ty); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 529 | |
| 530 | // Print the type definition |
| 531 | switch (Ty->getTypeID()) { |
| 532 | case Type::FunctionTyID: { |
| 533 | const FunctionType* FT = cast<FunctionType>(Ty); |
| 534 | Out << "std::vector<const Type*>" << typeName << "_args;\n"; |
| 535 | FunctionType::param_iterator PI = FT->param_begin(); |
| 536 | FunctionType::param_iterator PE = FT->param_end(); |
| 537 | for (; PI != PE; ++PI) { |
| 538 | const Type* argTy = static_cast<const Type*>(*PI); |
| 539 | bool isForward = printTypeDefInternal(argTy); |
| 540 | std::string argName(getCppName(argTy)); |
| 541 | Out << typeName << "_args.push_back(" << argName; |
| 542 | if (isForward) |
| 543 | Out << "_fwd"; |
| 544 | Out << ");\n"; |
| 545 | } |
| 546 | bool isForward = printTypeDefInternal(FT->getReturnType()); |
| 547 | std::string retTypeName(getCppName(FT->getReturnType())); |
| 548 | Out << "FunctionType* " << typeName << " = FunctionType::get(\n" |
| 549 | << " /*Result=*/" << retTypeName; |
| 550 | if (isForward) |
| 551 | Out << "_fwd"; |
| 552 | Out << ",\n /*Params=*/" << typeName << "_args,\n /*isVarArg=*/" |
| 553 | << (FT->isVarArg() ? "true" : "false") << ");\n"; |
| 554 | break; |
| 555 | } |
| 556 | case Type::StructTyID: { |
| 557 | const StructType* ST = cast<StructType>(Ty); |
| 558 | Out << "std::vector<const Type*>" << typeName << "_fields;\n"; |
| 559 | StructType::element_iterator EI = ST->element_begin(); |
| 560 | StructType::element_iterator EE = ST->element_end(); |
| 561 | for (; EI != EE; ++EI) { |
| 562 | const Type* fieldTy = static_cast<const Type*>(*EI); |
| 563 | bool isForward = printTypeDefInternal(fieldTy); |
| 564 | std::string fieldName(getCppName(fieldTy)); |
| 565 | Out << typeName << "_fields.push_back(" << fieldName; |
| 566 | if (isForward) |
| 567 | Out << "_fwd"; |
| 568 | Out << ");\n"; |
| 569 | } |
| 570 | Out << "StructType* " << typeName << " = StructType::get(" |
| 571 | << typeName << "_fields);\n"; |
| 572 | break; |
| 573 | } |
| 574 | case Type::ArrayTyID: { |
| 575 | const ArrayType* AT = cast<ArrayType>(Ty); |
| 576 | const Type* ET = AT->getElementType(); |
| 577 | bool isForward = printTypeDefInternal(ET); |
| 578 | std::string elemName(getCppName(ET)); |
| 579 | Out << "ArrayType* " << typeName << " = ArrayType::get(" |
| 580 | << elemName << (isForward ? "_fwd" : "") |
| 581 | << ", " << utostr(AT->getNumElements()) << ");\n"; |
| 582 | break; |
| 583 | } |
| 584 | case Type::PointerTyID: { |
| 585 | const PointerType* PT = cast<PointerType>(Ty); |
| 586 | const Type* ET = PT->getElementType(); |
| 587 | bool isForward = printTypeDefInternal(ET); |
| 588 | std::string elemName(getCppName(ET)); |
| 589 | Out << "PointerType* " << typeName << " = PointerType::get(" |
| 590 | << elemName << (isForward ? "_fwd" : "") << ");\n"; |
| 591 | break; |
| 592 | } |
| 593 | case Type::PackedTyID: { |
| 594 | const PackedType* PT = cast<PackedType>(Ty); |
| 595 | const Type* ET = PT->getElementType(); |
| 596 | bool isForward = printTypeDefInternal(ET); |
| 597 | std::string elemName(getCppName(ET)); |
| 598 | Out << "PackedType* " << typeName << " = PackedType::get(" |
| 599 | << elemName << (isForward ? "_fwd" : "") |
| 600 | << ", " << utostr(PT->getNumElements()) << ");\n"; |
| 601 | break; |
| 602 | } |
| 603 | case Type::OpaqueTyID: { |
| 604 | const OpaqueType* OT = cast<OpaqueType>(Ty); |
| 605 | Out << "OpaqueType* " << typeName << " = OpaqueType::get();\n"; |
| 606 | break; |
| 607 | } |
| 608 | default: |
| 609 | assert(!"Invalid TypeID"); |
| 610 | } |
| 611 | |
Reid Spencer | 74e032a | 2006-05-29 02:58:15 +0000 | [diff] [blame] | 612 | // If the type had a name, make sure we recreate it. |
| 613 | const std::string* progTypeName = |
| 614 | findTypeName(TheModule->getSymbolTable(),Ty); |
| 615 | if (progTypeName) |
| 616 | Out << "mod->addTypeName(\"" << *progTypeName << "\", " |
| 617 | << typeName << ");\n"; |
| 618 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 619 | // Pop us off the type stack |
| 620 | TypeStack.pop_back(); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 621 | |
| 622 | // Indicate that this type is now defined. |
| 623 | DefinedTypes.insert(Ty); |
| 624 | |
| 625 | // Early resolve as many unresolved types as possible. Search the unresolved |
| 626 | // types map for the type we just printed. Now that its definition is complete |
| 627 | // we can resolve any previous references to it. This prevents a cascade of |
| 628 | // unresolved types. |
| 629 | TypeMap::iterator I = UnresolvedTypes.find(Ty); |
| 630 | if (I != UnresolvedTypes.end()) { |
| 631 | Out << "cast<OpaqueType>(" << I->second |
| 632 | << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");\n"; |
| 633 | Out << I->second << " = cast<"; |
| 634 | switch (Ty->getTypeID()) { |
| 635 | case Type::FunctionTyID: Out << "FunctionType"; break; |
| 636 | case Type::ArrayTyID: Out << "ArrayType"; break; |
| 637 | case Type::StructTyID: Out << "StructType"; break; |
| 638 | case Type::PackedTyID: Out << "PackedType"; break; |
| 639 | case Type::PointerTyID: Out << "PointerType"; break; |
| 640 | case Type::OpaqueTyID: Out << "OpaqueType"; break; |
| 641 | default: Out << "NoSuchDerivedType"; break; |
| 642 | } |
| 643 | Out << ">(" << I->second << "_fwd.get());\n\n"; |
| 644 | UnresolvedTypes.erase(I); |
| 645 | } |
| 646 | |
| 647 | // Finally, separate the type definition from other with a newline. |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 648 | Out << "\n"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 649 | |
| 650 | // We weren't a recursive type |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | void |
| 655 | CppWriter::printTypes(const Module* M) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 656 | |
| 657 | // Walk the symbol table and print out all its types |
| 658 | const SymbolTable& symtab = M->getSymbolTable(); |
| 659 | for (SymbolTable::type_const_iterator TI = symtab.type_begin(), |
| 660 | TE = symtab.type_end(); TI != TE; ++TI) { |
| 661 | |
| 662 | // For primitive types and types already defined, just add a name |
| 663 | TypeMap::const_iterator TNI = TypeNames.find(TI->second); |
| 664 | if (TI->second->isPrimitiveType() || TNI != TypeNames.end()) { |
| 665 | Out << "mod->addTypeName(\""; |
| 666 | printEscapedString(TI->first); |
| 667 | Out << "\", " << getCppName(TI->second) << ");\n"; |
| 668 | // For everything else, define the type |
| 669 | } else { |
| 670 | printTypeDef(TI->second); |
| 671 | } |
| 672 | } |
| 673 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 674 | // Add all of the global variables to the value table... |
| 675 | for (Module::const_global_iterator I = TheModule->global_begin(), |
| 676 | E = TheModule->global_end(); I != E; ++I) { |
| 677 | if (I->hasInitializer()) |
| 678 | printTypeDef(I->getInitializer()->getType()); |
| 679 | printTypeDef(I->getType()); |
| 680 | } |
| 681 | |
| 682 | // Add all the functions to the table |
| 683 | for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 684 | FI != FE; ++FI) { |
| 685 | printTypeDef(FI->getReturnType()); |
| 686 | printTypeDef(FI->getFunctionType()); |
| 687 | // Add all the function arguments |
| 688 | for(Function::const_arg_iterator AI = FI->arg_begin(), |
| 689 | AE = FI->arg_end(); AI != AE; ++AI) { |
| 690 | printTypeDef(AI->getType()); |
| 691 | } |
| 692 | |
| 693 | // Add all of the basic blocks and instructions |
| 694 | for (Function::const_iterator BB = FI->begin(), |
| 695 | E = FI->end(); BB != E; ++BB) { |
| 696 | printTypeDef(BB->getType()); |
| 697 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; |
| 698 | ++I) { |
| 699 | printTypeDef(I->getType()); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 700 | for (unsigned i = 0; i < I->getNumOperands(); ++i) |
| 701 | printTypeDef(I->getOperand(i)->getType()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | void |
| 708 | CppWriter::printConstants(const Module* M) { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 709 | // Add all of the global variables to the value table... |
| 710 | for (Module::const_global_iterator I = TheModule->global_begin(), |
| 711 | E = TheModule->global_end(); I != E; ++I) |
| 712 | if (I->hasInitializer()) |
| 713 | printConstant(I->getInitializer()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 714 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 715 | // Traverse the LLVM functions looking for constants |
| 716 | for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 717 | FI != FE; ++FI) { |
| 718 | // Add all of the basic blocks and instructions |
| 719 | for (Function::const_iterator BB = FI->begin(), |
| 720 | E = FI->end(); BB != E; ++BB) { |
| 721 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; |
| 722 | ++I) { |
| 723 | for (unsigned i = 0; i < I->getNumOperands(); ++i) { |
| 724 | if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) { |
| 725 | printConstant(C); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 730 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 733 | // printConstant - Print out a constant pool entry... |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 734 | void CppWriter::printConstant(const Constant *CV) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 735 | // First, if the constant is actually a GlobalValue (variable or function) or |
| 736 | // its already in the constant list then we've printed it already and we can |
| 737 | // just return. |
| 738 | if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end()) |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 739 | return; |
| 740 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 741 | const int IndentSize = 2; |
| 742 | static std::string Indent = "\n"; |
| 743 | std::string constName(getCppName(CV)); |
| 744 | std::string typeName(getCppName(CV->getType())); |
| 745 | if (CV->isNullValue()) { |
| 746 | Out << "Constant* " << constName << " = Constant::getNullValue(" |
| 747 | << typeName << ");\n"; |
| 748 | return; |
| 749 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 750 | if (isa<GlobalValue>(CV)) { |
| 751 | // Skip variables and functions, we emit them elsewhere |
| 752 | return; |
| 753 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 754 | if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 755 | Out << "ConstantBool* " << constName << " = ConstantBool::get(" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 756 | << (CB == ConstantBool::True ? "true" : "false") |
| 757 | << ");"; |
| 758 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 759 | Out << "ConstantSInt* " << constName << " = ConstantSInt::get(" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 760 | << typeName << ", " << CI->getValue() << ");"; |
| 761 | } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 762 | Out << "ConstantUInt* " << constName << " = ConstantUInt::get(" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 763 | << typeName << ", " << CI->getValue() << ");"; |
| 764 | } else if (isa<ConstantAggregateZero>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 765 | Out << "ConstantAggregateZero* " << constName |
| 766 | << " = ConstantAggregateZero::get(" << typeName << ");"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 767 | } else if (isa<ConstantPointerNull>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 768 | Out << "ConstantPointerNull* " << constName |
| 769 | << " = ConstanPointerNull::get(" << typeName << ");"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 770 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 771 | Out << "ConstantFP* " << constName << " = ConstantFP::get(" << typeName |
| 772 | << ", "; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 773 | char buffer[64]; |
| 774 | sprintf(buffer,"%A",CFP->getValue()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 775 | // We would like to output the FP constant value in exponential notation, |
| 776 | // but we cannot do this if doing so will lose precision. Check here to |
| 777 | // make sure that we only output it in exponential format if we can parse |
| 778 | // the value back and get the same value. |
| 779 | // |
| 780 | std::string StrVal = ftostr(CFP->getValue()); |
| 781 | |
| 782 | // Check to make sure that the stringized number is not some string like |
| 783 | // "Inf" or NaN, that atof will accept, but the lexer will not. Check that |
| 784 | // the string matches the "[-+]?[0-9]" regex. |
| 785 | // |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 786 | if (((StrVal[0] >= '0' && StrVal[0] <= '9') || |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 787 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 788 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) && |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 789 | (atof(StrVal.c_str()) == CFP->getValue())) |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 790 | { |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 791 | Out << StrVal; |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 792 | } else { |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 793 | |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 794 | // Otherwise we could not reparse it to exactly the same value, so we must |
| 795 | // output the string in hexadecimal format! |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 796 | assert(sizeof(double) == sizeof(uint64_t) && |
| 797 | "assuming that double is 64 bits!"); |
| 798 | Out << "0x" << std::hex << DoubleToBits(CFP->getValue()) << std::dec |
| 799 | << "ULL /* " << StrVal << " */"; |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 800 | } |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 801 | Out << ");"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 802 | } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 803 | if (CA->isString() && CA->getType()->getElementType() == Type::SByteTy) { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 804 | Out << "Constant* " << constName << " = ConstantArray::get(\""; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 805 | printEscapedString(CA->getAsString()); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 806 | // Determine if we want null termination or not. |
| 807 | if (CA->getType()->getNumElements() <= CA->getAsString().length()) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 808 | Out << "\", false";// No null terminator |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 809 | else |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 810 | Out << "\", true"; // Indicate that the null terminator should be added. |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 811 | Out << ");"; |
| 812 | } else { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 813 | Out << "std::vector<Constant*> " << constName << "_elems;\n"; |
| 814 | unsigned N = CA->getNumOperands(); |
| 815 | for (unsigned i = 0; i < N; ++i) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 816 | printConstant(CA->getOperand(i)); // recurse to print operands |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 817 | Out << constName << "_elems.push_back(" |
| 818 | << getCppName(CA->getOperand(i)) << ");\n"; |
| 819 | } |
| 820 | Out << "Constant* " << constName << " = ConstantArray::get(" |
| 821 | << typeName << ", " << constName << "_elems);"; |
| 822 | } |
| 823 | } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { |
| 824 | Out << "std::vector<Constant*> " << constName << "_fields;\n"; |
| 825 | unsigned N = CS->getNumOperands(); |
| 826 | for (unsigned i = 0; i < N; i++) { |
| 827 | printConstant(CS->getOperand(i)); |
| 828 | Out << constName << "_fields.push_back(" |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 829 | << getCppName(CS->getOperand(i)) << ");\n"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 830 | } |
| 831 | Out << "Constant* " << constName << " = ConstantStruct::get(" |
| 832 | << typeName << ", " << constName << "_fields);"; |
| 833 | } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) { |
| 834 | Out << "std::vector<Constant*> " << constName << "_elems;\n"; |
| 835 | unsigned N = CP->getNumOperands(); |
| 836 | for (unsigned i = 0; i < N; ++i) { |
| 837 | printConstant(CP->getOperand(i)); |
| 838 | Out << constName << "_elems.push_back(" |
| 839 | << getCppName(CP->getOperand(i)) << ");\n"; |
| 840 | } |
| 841 | Out << "Constant* " << constName << " = ConstantPacked::get(" |
| 842 | << typeName << ", " << constName << "_elems);"; |
| 843 | } else if (isa<UndefValue>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 844 | Out << "UndefValue* " << constName << " = UndefValue::get(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 845 | << typeName << ");"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 846 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 847 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 848 | Out << "std::vector<Constant*> " << constName << "_indices;\n"; |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 849 | printConstant(CE->getOperand(0)); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 850 | for (unsigned i = 1; i < CE->getNumOperands(); ++i ) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 851 | printConstant(CE->getOperand(i)); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 852 | Out << constName << "_indices.push_back(" |
| 853 | << getCppName(CE->getOperand(i)) << ");\n"; |
| 854 | } |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 855 | Out << "Constant* " << constName |
| 856 | << " = ConstantExpr::getGetElementPtr(" |
| 857 | << getCppName(CE->getOperand(0)) << ", " |
| 858 | << constName << "_indices);"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 859 | } else if (CE->getOpcode() == Instruction::Cast) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 860 | printConstant(CE->getOperand(0)); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 861 | Out << "Constant* " << constName << " = ConstantExpr::getCast("; |
| 862 | Out << getCppName(CE->getOperand(0)) << ", " << getCppName(CE->getType()) |
| 863 | << ");"; |
| 864 | } else { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 865 | unsigned N = CE->getNumOperands(); |
| 866 | for (unsigned i = 0; i < N; ++i ) { |
| 867 | printConstant(CE->getOperand(i)); |
| 868 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 869 | Out << "Constant* " << constName << " = ConstantExpr::"; |
| 870 | switch (CE->getOpcode()) { |
| 871 | case Instruction::Add: Out << "getAdd"; break; |
| 872 | case Instruction::Sub: Out << "getSub"; break; |
| 873 | case Instruction::Mul: Out << "getMul"; break; |
| 874 | case Instruction::Div: Out << "getDiv"; break; |
| 875 | case Instruction::Rem: Out << "getRem"; break; |
| 876 | case Instruction::And: Out << "getAnd"; break; |
| 877 | case Instruction::Or: Out << "getOr"; break; |
| 878 | case Instruction::Xor: Out << "getXor"; break; |
| 879 | case Instruction::SetEQ: Out << "getSetEQ"; break; |
| 880 | case Instruction::SetNE: Out << "getSetNE"; break; |
| 881 | case Instruction::SetLE: Out << "getSetLE"; break; |
| 882 | case Instruction::SetGE: Out << "getSetGE"; break; |
| 883 | case Instruction::SetLT: Out << "getSetLT"; break; |
| 884 | case Instruction::SetGT: Out << "getSetGT"; break; |
| 885 | case Instruction::Shl: Out << "getShl"; break; |
| 886 | case Instruction::Shr: Out << "getShr"; break; |
| 887 | case Instruction::Select: Out << "getSelect"; break; |
| 888 | case Instruction::ExtractElement: Out << "getExtractElement"; break; |
| 889 | case Instruction::InsertElement: Out << "getInsertElement"; break; |
| 890 | case Instruction::ShuffleVector: Out << "getShuffleVector"; break; |
| 891 | default: |
| 892 | assert(!"Invalid constant expression"); |
| 893 | break; |
| 894 | } |
| 895 | Out << getCppName(CE->getOperand(0)); |
| 896 | for (unsigned i = 1; i < CE->getNumOperands(); ++i) |
| 897 | Out << ", " << getCppName(CE->getOperand(i)); |
| 898 | Out << ");"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 899 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 900 | } else { |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 901 | assert(!"Bad Constant"); |
| 902 | Out << "Constant* " << constName << " = 0; "; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 903 | } |
| 904 | Out << "\n"; |
| 905 | } |
| 906 | |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 907 | void CppWriter::printFunctionHead(const Function* F) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 908 | Out << "\nFunction* " << getCppName(F) << " = new Function(\n" |
| 909 | << " /*Type=*/" << getCppName(F->getFunctionType()) << ",\n" |
| 910 | << " /*Linkage=*/"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 911 | printLinkageType(F->getLinkage()); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 912 | Out << ",\n /*Name=*/\""; |
| 913 | printEscapedString(F->getName()); |
| 914 | Out << "\", mod); " |
| 915 | << (F->isExternal()? "// (external, no body)" : "") << "\n"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 916 | printCppName(F); |
| 917 | Out << "->setCallingConv("; |
| 918 | printCallingConv(F->getCallingConv()); |
| 919 | Out << ");\n"; |
| 920 | if (F->hasSection()) { |
| 921 | printCppName(F); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 922 | Out << "->setSection(\"" << F->getSection() << "\");\n"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 923 | } |
| 924 | if (F->getAlignment()) { |
| 925 | printCppName(F); |
| 926 | Out << "->setAlignment(" << F->getAlignment() << ");\n"; |
| 927 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 928 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 929 | |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 930 | void CppWriter::printFunctionBody(const Function *F) { |
| 931 | if (F->isExternal()) |
| 932 | return; // external functions have no bodies. |
| 933 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 934 | // Clear the DefinedValues and ForwardRefs maps because we can't have |
| 935 | // cross-function forward refs |
| 936 | ForwardRefs.clear(); |
| 937 | DefinedValues.clear(); |
| 938 | |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 939 | // Create all the argument values |
| 940 | if (!F->arg_empty()) { |
| 941 | Out << " Function::arg_iterator args = " << getCppName(F) |
| 942 | << "->arg_begin();\n"; |
| 943 | } |
| 944 | for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 945 | AI != AE; ++AI) { |
| 946 | Out << " Value* " << getCppName(AI) << " = args++;\n"; |
| 947 | if (AI->hasName()) |
| 948 | Out << " " << getCppName(AI) << "->setName(\"" << AI->getName() |
| 949 | << "\");\n"; |
| 950 | } |
| 951 | |
| 952 | // Create all the basic blocks |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 953 | Out << "\n"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 954 | for (Function::const_iterator BI = F->begin(), BE = F->end(); |
| 955 | BI != BE; ++BI) { |
| 956 | std::string bbname(getCppName(BI)); |
| 957 | Out << " BasicBlock* " << bbname << " = new BasicBlock(\""; |
| 958 | if (BI->hasName()) |
| 959 | printEscapedString(BI->getName()); |
| 960 | Out << "\"," << getCppName(BI->getParent()) << ",0);\n"; |
| 961 | } |
| 962 | |
| 963 | // Output all of its basic blocks... for the function |
| 964 | for (Function::const_iterator BI = F->begin(), BE = F->end(); |
| 965 | BI != BE; ++BI) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 966 | std::string bbname(getCppName(BI)); |
| 967 | Out << "\n // Block " << BI->getName() << " (" << bbname << ")\n"; |
| 968 | |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 969 | // Output all of the instructions in the basic block... |
| 970 | for (BasicBlock::const_iterator I = BI->begin(), E = BI->end(); |
| 971 | I != E; ++I) { |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 972 | printInstruction(I,bbname); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 973 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 974 | } |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 975 | |
| 976 | // Loop over the ForwardRefs and resolve them now that all instructions |
| 977 | // are generated. |
| 978 | if (!ForwardRefs.empty()) |
| 979 | Out << "\n // Resolve Forward References\n"; |
| 980 | while (!ForwardRefs.empty()) { |
| 981 | ForwardRefMap::iterator I = ForwardRefs.begin(); |
| 982 | Out << " " << I->second << "->replaceAllUsesWith(" |
| 983 | << getCppName(I->first) << "); delete " << I->second << ";\n"; |
| 984 | ForwardRefs.erase(I); |
| 985 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 988 | // printInstruction - This member is called for each Instruction in a function. |
| 989 | void |
| 990 | CppWriter::printInstruction(const Instruction *I, const std::string& bbname) |
| 991 | { |
| 992 | std::string iName(getCppName(I)); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 993 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 994 | // Before we emit this instruction, we need to take care of generating any |
| 995 | // forward references. So, we get the names of all the operands in advance |
| 996 | std::string* opNames = new std::string[I->getNumOperands()]; |
| 997 | for (unsigned i = 0; i < I->getNumOperands(); i++) { |
| 998 | opNames[i] = getOpName(I->getOperand(i)); |
| 999 | } |
| 1000 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1001 | switch (I->getOpcode()) { |
| 1002 | case Instruction::Ret: { |
| 1003 | const ReturnInst* ret = cast<ReturnInst>(I); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1004 | Out << " ReturnInst* " << iName << " = new ReturnInst(" |
| 1005 | << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1006 | break; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1007 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1008 | case Instruction::Br: { |
| 1009 | const BranchInst* br = cast<BranchInst>(I); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1010 | Out << " BranchInst* " << iName << " = new BranchInst(" ; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1011 | if (br->getNumOperands() == 3 ) { |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1012 | Out << opNames[0] << ", " |
| 1013 | << opNames[1] << ", " |
| 1014 | << opNames[2] << ", "; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1015 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1016 | } else if (br->getNumOperands() == 1) { |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1017 | Out << opNames[0] << ", "; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1018 | } else { |
| 1019 | assert(!"branch with 2 operands?"); |
| 1020 | } |
| 1021 | Out << bbname << ");"; |
| 1022 | break; |
| 1023 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1024 | case Instruction::Switch: { |
| 1025 | const SwitchInst* sw = cast<SwitchInst>(I); |
| 1026 | Out << " SwitchInst* " << iName << " = new SwitchInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1027 | << opNames[0] << ", " |
| 1028 | << opNames[1] << ", " |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1029 | << sw->getNumCases() << ", " << bbname << ");\n"; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1030 | for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) { |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1031 | Out << " " << iName << "->addCase(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1032 | << opNames[i] << ", " |
| 1033 | << opNames[i+1] << ");\n"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1034 | } |
| 1035 | break; |
| 1036 | } |
| 1037 | case Instruction::Invoke: { |
| 1038 | const InvokeInst* inv = cast<InvokeInst>(I); |
| 1039 | Out << " std::vector<Value*> " << iName << "_params;\n"; |
| 1040 | for (unsigned i = 3; i < inv->getNumOperands(); ++i) |
| 1041 | Out << " " << iName << "_params.push_back(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1042 | << opNames[i] << ");\n"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1043 | Out << " InvokeInst* " << iName << " = new InvokeInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1044 | << opNames[0] << ", " |
| 1045 | << opNames[1] << ", " |
| 1046 | << opNames[2] << ", " |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1047 | << iName << "_params, \""; |
| 1048 | printEscapedString(inv->getName()); |
| 1049 | Out << "\", " << bbname << ");\n"; |
| 1050 | Out << iName << "->setCallingConv("; |
| 1051 | printCallingConv(inv->getCallingConv()); |
| 1052 | Out << ");"; |
| 1053 | break; |
| 1054 | } |
| 1055 | case Instruction::Unwind: { |
| 1056 | Out << " UnwindInst* " << iName << " = new UnwindInst(" |
| 1057 | << bbname << ");"; |
| 1058 | break; |
| 1059 | } |
| 1060 | case Instruction::Unreachable:{ |
| 1061 | Out << " UnreachableInst* " << iName << " = new UnreachableInst(" |
| 1062 | << bbname << ");"; |
| 1063 | break; |
| 1064 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1065 | case Instruction::Add: |
| 1066 | case Instruction::Sub: |
| 1067 | case Instruction::Mul: |
| 1068 | case Instruction::Div: |
| 1069 | case Instruction::Rem: |
| 1070 | case Instruction::And: |
| 1071 | case Instruction::Or: |
| 1072 | case Instruction::Xor: |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1073 | case Instruction::Shl: |
| 1074 | case Instruction::Shr:{ |
| 1075 | Out << " BinaryOperator* " << iName << " = BinaryOperator::create("; |
| 1076 | switch (I->getOpcode()) { |
| 1077 | case Instruction::Add: Out << "Instruction::Add"; break; |
| 1078 | case Instruction::Sub: Out << "Instruction::Sub"; break; |
| 1079 | case Instruction::Mul: Out << "Instruction::Mul"; break; |
| 1080 | case Instruction::Div: Out << "Instruction::Div"; break; |
| 1081 | case Instruction::Rem: Out << "Instruction::Rem"; break; |
| 1082 | case Instruction::And: Out << "Instruction::And"; break; |
| 1083 | case Instruction::Or: Out << "Instruction::Or"; break; |
| 1084 | case Instruction::Xor: Out << "Instruction::Xor"; break; |
| 1085 | case Instruction::Shl: Out << "Instruction::Shl"; break; |
| 1086 | case Instruction::Shr: Out << "Instruction::Shr"; break; |
| 1087 | default: Out << "Instruction::BadOpCode"; break; |
| 1088 | } |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1089 | Out << ", " << opNames[0] << ", " << opNames[1] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1090 | printEscapedString(I->getName()); |
| 1091 | Out << "\", " << bbname << ");"; |
| 1092 | break; |
| 1093 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1094 | case Instruction::SetEQ: |
| 1095 | case Instruction::SetNE: |
| 1096 | case Instruction::SetLE: |
| 1097 | case Instruction::SetGE: |
| 1098 | case Instruction::SetLT: |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1099 | case Instruction::SetGT: { |
| 1100 | Out << " SetCondInst* " << iName << " = new SetCondInst("; |
| 1101 | switch (I->getOpcode()) { |
| 1102 | case Instruction::SetEQ: Out << "Instruction::SetEQ"; break; |
| 1103 | case Instruction::SetNE: Out << "Instruction::SetNE"; break; |
| 1104 | case Instruction::SetLE: Out << "Instruction::SetLE"; break; |
| 1105 | case Instruction::SetGE: Out << "Instruction::SetGE"; break; |
| 1106 | case Instruction::SetLT: Out << "Instruction::SetLT"; break; |
| 1107 | case Instruction::SetGT: Out << "Instruction::SetGT"; break; |
| 1108 | default: Out << "Instruction::BadOpCode"; break; |
| 1109 | } |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1110 | Out << ", " << opNames[0] << ", " << opNames[1] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1111 | printEscapedString(I->getName()); |
| 1112 | Out << "\", " << bbname << ");"; |
| 1113 | break; |
| 1114 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1115 | case Instruction::Malloc: { |
| 1116 | const MallocInst* mallocI = cast<MallocInst>(I); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1117 | Out << " MallocInst* " << iName << " = new MallocInst(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1118 | << getCppName(mallocI->getAllocatedType()) << ", "; |
| 1119 | if (mallocI->isArrayAllocation()) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1120 | Out << opNames[0] << ", " ; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1121 | Out << "\""; |
| 1122 | printEscapedString(mallocI->getName()); |
| 1123 | Out << "\", " << bbname << ");"; |
| 1124 | if (mallocI->getAlignment()) |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1125 | Out << "\n " << iName << "->setAlignment(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1126 | << mallocI->getAlignment() << ");"; |
| 1127 | break; |
| 1128 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1129 | case Instruction::Free: { |
| 1130 | Out << " FreeInst* " << iName << " = new FreeInst(" |
| 1131 | << getCppName(I->getOperand(0)) << ", " << bbname << ");"; |
| 1132 | break; |
| 1133 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1134 | case Instruction::Alloca: { |
| 1135 | const AllocaInst* allocaI = cast<AllocaInst>(I); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1136 | Out << " AllocaInst* " << iName << " = new AllocaInst(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1137 | << getCppName(allocaI->getAllocatedType()) << ", "; |
| 1138 | if (allocaI->isArrayAllocation()) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1139 | Out << opNames[0] << ", "; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1140 | Out << "\""; |
| 1141 | printEscapedString(allocaI->getName()); |
| 1142 | Out << "\", " << bbname << ");"; |
| 1143 | if (allocaI->getAlignment()) |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1144 | Out << "\n " << iName << "->setAlignment(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1145 | << allocaI->getAlignment() << ");"; |
| 1146 | break; |
| 1147 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1148 | case Instruction::Load:{ |
| 1149 | const LoadInst* load = cast<LoadInst>(I); |
| 1150 | Out << " LoadInst* " << iName << " = new LoadInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1151 | << opNames[0] << ", \""; |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1152 | printEscapedString(load->getName()); |
| 1153 | Out << "\", " << (load->isVolatile() ? "true" : "false" ) |
| 1154 | << ", " << bbname << ");\n"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1155 | break; |
| 1156 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1157 | case Instruction::Store: { |
| 1158 | const StoreInst* store = cast<StoreInst>(I); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1159 | Out << " StoreInst* " << iName << " = new StoreInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1160 | << opNames[0] << ", " |
| 1161 | << opNames[1] << ", " |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1162 | << (store->isVolatile() ? "true" : "false") |
| 1163 | << ", " << bbname << ");\n"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1164 | break; |
| 1165 | } |
| 1166 | case Instruction::GetElementPtr: { |
| 1167 | const GetElementPtrInst* gep = cast<GetElementPtrInst>(I); |
| 1168 | if (gep->getNumOperands() <= 2) { |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1169 | Out << " GetElementPtrInst* " << iName << " = new GetElementPtrInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1170 | << opNames[0]; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1171 | if (gep->getNumOperands() == 2) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1172 | Out << ", " << opNames[1]; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1173 | } else { |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1174 | Out << " std::vector<Value*> " << iName << "_indices;\n"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1175 | for (unsigned i = 1; i < gep->getNumOperands(); ++i ) { |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1176 | Out << " " << iName << "_indices.push_back(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1177 | << opNames[i] << ");\n"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1178 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1179 | Out << " Instruction* " << iName << " = new GetElementPtrInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1180 | << opNames[0] << ", " << iName << "_indices"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1181 | } |
| 1182 | Out << ", \""; |
| 1183 | printEscapedString(gep->getName()); |
| 1184 | Out << "\", " << bbname << ");"; |
| 1185 | break; |
| 1186 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1187 | case Instruction::PHI: { |
| 1188 | const PHINode* phi = cast<PHINode>(I); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1189 | |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1190 | Out << " PHINode* " << iName << " = new PHINode(" |
| 1191 | << getCppName(phi->getType()) << ", \""; |
| 1192 | printEscapedString(phi->getName()); |
| 1193 | Out << "\", " << bbname << ");\n"; |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1194 | Out << " " << iName << "->reserveOperandSpace(" |
| 1195 | << phi->getNumIncomingValues() |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1196 | << ");\n"; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1197 | for (unsigned i = 0; i < phi->getNumOperands(); i+=2) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1198 | Out << " " << iName << "->addIncoming(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1199 | << opNames[i] << ", " << opNames[i+1] << ");\n"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1200 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1201 | break; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1202 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1203 | case Instruction::Cast: { |
| 1204 | const CastInst* cst = cast<CastInst>(I); |
| 1205 | Out << " CastInst* " << iName << " = new CastInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1206 | << opNames[0] << ", " |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1207 | << getCppName(cst->getType()) << ", \""; |
| 1208 | printEscapedString(cst->getName()); |
| 1209 | Out << "\", " << bbname << ");\n"; |
| 1210 | break; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1211 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1212 | case Instruction::Call:{ |
| 1213 | const CallInst* call = cast<CallInst>(I); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1214 | if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) { |
| 1215 | Out << " InlineAsm* " << getCppName(ila) << " = InlineAsm::get(" |
| 1216 | << getCppName(ila->getFunctionType()) << ", \"" |
| 1217 | << ila->getAsmString() << "\", \"" |
| 1218 | << ila->getConstraintString() << "\"," |
| 1219 | << (ila->hasSideEffects() ? "true" : "false") << ");\n"; |
| 1220 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1221 | if (call->getNumOperands() > 3) { |
| 1222 | Out << " std::vector<Value*> " << iName << "_params;\n"; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1223 | for (unsigned i = 1; i < call->getNumOperands(); ++i) |
| 1224 | Out << " " << iName << "_params.push_back(" << opNames[i] << ");\n"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1225 | Out << " CallInst* " << iName << " = new CallInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1226 | << opNames[0] << ", " << iName << "_params, \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1227 | } else if (call->getNumOperands() == 3) { |
| 1228 | Out << " CallInst* " << iName << " = new CallInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1229 | << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1230 | } else if (call->getNumOperands() == 2) { |
| 1231 | Out << " CallInst* " << iName << " = new CallInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1232 | << opNames[0] << ", " << opNames[1] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1233 | } else { |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1234 | Out << " CallInst* " << iName << " = new CallInst(" << opNames[0] |
| 1235 | << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1236 | } |
| 1237 | printEscapedString(call->getName()); |
| 1238 | Out << "\", " << bbname << ");\n"; |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1239 | Out << " " << iName << "->setCallingConv("; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1240 | printCallingConv(call->getCallingConv()); |
| 1241 | Out << ");\n"; |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1242 | Out << " " << iName << "->setTailCall(" |
| 1243 | << (call->isTailCall() ? "true":"false"); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1244 | Out << ");"; |
| 1245 | break; |
| 1246 | } |
| 1247 | case Instruction::Select: { |
| 1248 | const SelectInst* sel = cast<SelectInst>(I); |
| 1249 | Out << " SelectInst* " << getCppName(sel) << " = new SelectInst("; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1250 | Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1251 | printEscapedString(sel->getName()); |
| 1252 | Out << "\", " << bbname << ");\n"; |
| 1253 | break; |
| 1254 | } |
| 1255 | case Instruction::UserOp1: |
| 1256 | /// FALL THROUGH |
| 1257 | case Instruction::UserOp2: { |
| 1258 | /// FIXME: What should be done here? |
| 1259 | break; |
| 1260 | } |
| 1261 | case Instruction::VAArg: { |
| 1262 | const VAArgInst* va = cast<VAArgInst>(I); |
| 1263 | Out << " VAArgInst* " << getCppName(va) << " = new VAArgInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1264 | << opNames[0] << ", " << getCppName(va->getType()) << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1265 | printEscapedString(va->getName()); |
| 1266 | Out << "\", " << bbname << ");\n"; |
| 1267 | break; |
| 1268 | } |
| 1269 | case Instruction::ExtractElement: { |
| 1270 | const ExtractElementInst* eei = cast<ExtractElementInst>(I); |
| 1271 | Out << " ExtractElementInst* " << getCppName(eei) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1272 | << " = new ExtractElementInst(" << opNames[0] |
| 1273 | << ", " << opNames[1] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1274 | printEscapedString(eei->getName()); |
| 1275 | Out << "\", " << bbname << ");\n"; |
| 1276 | break; |
| 1277 | } |
| 1278 | case Instruction::InsertElement: { |
| 1279 | const InsertElementInst* iei = cast<InsertElementInst>(I); |
| 1280 | Out << " InsertElementInst* " << getCppName(iei) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1281 | << " = new InsertElementInst(" << opNames[0] |
| 1282 | << ", " << opNames[1] << ", " << opNames[2] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1283 | printEscapedString(iei->getName()); |
| 1284 | Out << "\", " << bbname << ");\n"; |
| 1285 | break; |
| 1286 | } |
| 1287 | case Instruction::ShuffleVector: { |
| 1288 | const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I); |
| 1289 | Out << " ShuffleVectorInst* " << getCppName(svi) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1290 | << " = new ShuffleVectorInst(" << opNames[0] |
| 1291 | << ", " << opNames[1] << ", " << opNames[2] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1292 | printEscapedString(svi->getName()); |
| 1293 | Out << "\", " << bbname << ");\n"; |
| 1294 | break; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1297 | Out << "\n"; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1298 | delete [] opNames; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | } // end anonymous llvm |
| 1302 | |
| 1303 | namespace llvm { |
| 1304 | |
| 1305 | void WriteModuleToCppFile(Module* mod, std::ostream& o) { |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame^] | 1306 | std::string fname = FuncName.getValue(); |
| 1307 | if (fname.empty()) |
| 1308 | fname = "makeLLVMModule"; |
| 1309 | if (Fragment) { |
| 1310 | o << "Module* " << fname << "() {\n"; |
| 1311 | CppWriter W(o, mod); |
| 1312 | W.printModule(mod); |
| 1313 | o << "return mod;\n"; |
| 1314 | o << "}\n"; |
| 1315 | } else { |
| 1316 | o << "#include <llvm/Module.h>\n"; |
| 1317 | o << "#include <llvm/DerivedTypes.h>\n"; |
| 1318 | o << "#include <llvm/Constants.h>\n"; |
| 1319 | o << "#include <llvm/GlobalVariable.h>\n"; |
| 1320 | o << "#include <llvm/Function.h>\n"; |
| 1321 | o << "#include <llvm/CallingConv.h>\n"; |
| 1322 | o << "#include <llvm/BasicBlock.h>\n"; |
| 1323 | o << "#include <llvm/Instructions.h>\n"; |
| 1324 | o << "#include <llvm/InlineAsm.h>\n"; |
| 1325 | o << "#include <llvm/Pass.h>\n"; |
| 1326 | o << "#include <llvm/PassManager.h>\n"; |
| 1327 | o << "#include <llvm/Analysis/Verifier.h>\n"; |
| 1328 | o << "#include <llvm/Assembly/PrintModulePass.h>\n"; |
| 1329 | o << "#include <algorithm>\n"; |
| 1330 | o << "#include <iostream>\n\n"; |
| 1331 | o << "using namespace llvm;\n\n"; |
| 1332 | o << "Module* " << fname << "();\n\n"; |
| 1333 | o << "int main(int argc, char**argv) {\n"; |
| 1334 | o << " Module* Mod = makeLLVMModule();\n"; |
| 1335 | o << " verifyModule(*Mod, PrintMessageAction);\n"; |
| 1336 | o << " std::cerr.flush();\n"; |
| 1337 | o << " std::cout.flush();\n"; |
| 1338 | o << " PassManager PM;\n"; |
| 1339 | o << " PM.add(new PrintModulePass(&std::cout));\n"; |
| 1340 | o << " PM.run(*Mod);\n"; |
| 1341 | o << " return 0;\n"; |
| 1342 | o << "}\n\n"; |
| 1343 | o << "Module* " << fname << "() {\n"; |
| 1344 | CppWriter W(o, mod); |
| 1345 | W.printModule(mod); |
| 1346 | o << "return mod;\n"; |
| 1347 | o << "}\n"; |
| 1348 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | } |