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