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