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