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