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