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