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