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