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