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