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