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