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