blob: 36c421e2b14b997eccd4f1f2f5902f43a4caf9ec [file] [log] [blame]
Anton Korobeynikov78695032008-04-23 22:29:24 +00001//===-- 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 Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallPtrSet.h"
17#include "llvm/ADT/StringExtras.h"
Rafael Espindola5682ce22015-04-09 21:06:08 +000018#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Config/config.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#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 Carruth30d69c22015-02-13 10:01:29 +000026#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
Evan Cheng1705ab02011-07-14 23:50:31 +000028#include "llvm/MC/MCAsmInfo.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000029#include "llvm/MC/MCInstrInfo.h"
Evan Cheng91111d22011-07-09 05:47:46 +000030#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Pass.h"
Anton Korobeynikov78695032008-04-23 22:29:24 +000032#include "llvm/Support/CommandLine.h"
Torok Edwinf8d479c2009-07-08 20:55:50 +000033#include "llvm/Support/ErrorHandling.h"
David Greenea31f96c2009-07-14 20:18:05 +000034#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000035#include "llvm/Support/TargetRegistry.h"
Anton Korobeynikov78695032008-04-23 22:29:24 +000036#include <algorithm>
Will Dietz981af002013-10-12 00:55:57 +000037#include <cctype>
Benjamin Kramerb0640db2012-03-23 11:35:30 +000038#include <cstdio>
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000039#include <map>
Benjamin Kramerb0640db2012-03-23 11:35:30 +000040#include <set>
Anton Korobeynikov78695032008-04-23 22:29:24 +000041using namespace llvm;
42
43static cl::opt<std::string>
Anton Korobeynikov9dcc3e92008-04-23 22:37:03 +000044FuncName("cppfname", cl::desc("Specify the name of the generated function"),
Anton Korobeynikov78695032008-04-23 22:29:24 +000045 cl::value_desc("function name"));
46
47enum WhatToGenerate {
48 GenProgram,
49 GenModule,
50 GenContents,
51 GenFunction,
52 GenFunctions,
53 GenInline,
54 GenVariable,
55 GenType
56};
57
Anton Korobeynikov9dcc3e92008-04-23 22:37:03 +000058static cl::opt<WhatToGenerate> GenerationType("cppgen", cl::Optional,
Anton Korobeynikov78695032008-04-23 22:29:24 +000059 cl::desc("Choose what kind of output to generate"),
60 cl::init(GenProgram),
61 cl::values(
Anton Korobeynikov9dcc3e92008-04-23 22:37:03 +000062 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 Korobeynikov78695032008-04-23 22:29:24 +000070 clEnumValEnd
71 )
72);
73
Anton Korobeynikov9dcc3e92008-04-23 22:37:03 +000074static cl::opt<std::string> NameToGenerate("cppfor", cl::Optional,
Anton Korobeynikov78695032008-04-23 22:29:24 +000075 cl::desc("Specify the name of the thing to generate"),
76 cl::init("!bad!"));
77
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000078extern "C" void LLVMInitializeCppBackendTarget() {
79 // Register the target.
Daniel Dunbar09c1d002009-08-04 04:02:45 +000080 RegisterTargetMachine<CPPTargetMachine> X(TheCppBackendTarget);
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000081}
Douglas Gregor1b731d52009-06-16 20:12:29 +000082
Dan Gohmand78c4002008-05-13 00:00:25 +000083namespace {
Chris Lattner229907c2011-07-18 04:54:35 +000084 typedef std::vector<Type*> TypeList;
85 typedef std::map<Type*,std::string> TypeMap;
Anton Korobeynikov78695032008-04-23 22:29:24 +000086 typedef std::map<const Value*,std::string> ValueMap;
87 typedef std::set<std::string> NameSet;
Chris Lattner229907c2011-07-18 04:54:35 +000088 typedef std::set<Type*> TypeSet;
Anton Korobeynikov78695032008-04-23 22:29:24 +000089 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 Espindola5682ce22015-04-09 21:06:08 +000095 std::unique_ptr<formatted_raw_ostream> OutOwner;
David Greenea31f96c2009-07-14 20:18:05 +000096 formatted_raw_ostream &Out;
Anton Korobeynikov78695032008-04-23 22:29:24 +000097 const Module *TheModule;
98 uint64_t uniqueNum;
99 TypeMap TypeNames;
100 ValueMap ValueNames;
Anton Korobeynikov78695032008-04-23 22:29:24 +0000101 NameSet UsedNames;
102 TypeSet DefinedTypes;
103 ValueSet DefinedValues;
104 ForwardRefMap ForwardRefs;
105 bool is_inline;
Chris Lattnerbb45b962010-06-21 23:14:47 +0000106 unsigned indent_level;
Anton Korobeynikov78695032008-04-23 22:29:24 +0000107
108 public:
109 static char ID;
Rafael Espindola5682ce22015-04-09 21:06:08 +0000110 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 Korobeynikov78695032008-04-23 22:29:24 +0000113
Craig Topper9d74a5a2014-04-29 07:58:41 +0000114 const char *getPassName() const override { return "C++ backend"; }
Anton Korobeynikov78695032008-04-23 22:29:24 +0000115
Craig Topper9d74a5a2014-04-29 07:58:41 +0000116 bool runOnModule(Module &M) override;
Anton Korobeynikov78695032008-04-23 22:29:24 +0000117
Anton Korobeynikov78695032008-04-23 22:29:24 +0000118 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 Lattnerbb45b962010-06-21 23:14:47 +0000129
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 Korobeynikov78695032008-04-23 22:29:24 +0000134 private:
135 void printLinkageType(GlobalValue::LinkageTypes LT);
136 void printVisibilityType(GlobalValue::VisibilityTypes VisTypes);
Nico Rieck7157bb72014-01-14 15:22:47 +0000137 void printDLLStorageClassType(GlobalValue::DLLStorageClassTypes DSCType);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000138 void printThreadLocalMode(GlobalVariable::ThreadLocalMode TLM);
Sandeep Patel68c5f472009-09-02 08:44:58 +0000139 void printCallingConv(CallingConv::ID cc);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000140 void printEscapedString(const std::string& str);
141 void printCFP(const ConstantFP* CFP);
142
Chris Lattner229907c2011-07-18 04:54:35 +0000143 std::string getCppName(Type* val);
144 inline void printCppName(Type* val);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000145
146 std::string getCppName(const Value* val);
147 inline void printCppName(const Value* val);
148
Bill Wendlinge94d8432012-12-07 23:16:57 +0000149 void printAttributes(const AttributeSet &PAL, const std::string &name);
Chris Lattner229907c2011-07-18 04:54:35 +0000150 void printType(Type* Ty);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000151 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 Friedman95031ed2011-09-29 20:21:17 +0000164 std::string getOpName(const Value*);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000165
166 void printModuleBody();
167 };
Chris Lattnera0b8c902010-06-21 23:12:56 +0000168} // end anonymous namespace.
Anton Korobeynikov78695032008-04-23 22:29:24 +0000169
Chris Lattnerbb45b962010-06-21 23:14:47 +0000170formatted_raw_ostream &CppWriter::nl(formatted_raw_ostream &Out, int delta) {
171 Out << '\n';
Chris Lattnera0b8c902010-06-21 23:12:56 +0000172 if (delta >= 0 || indent_level >= unsigned(-delta))
173 indent_level += delta;
Chris Lattnerbb45b962010-06-21 23:14:47 +0000174 Out.indent(indent_level);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000175 return Out;
176}
177
Chris Lattnera0b8c902010-06-21 23:12:56 +0000178static 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 Lattner229907c2011-07-18 04:54:35 +0000184static std::string getTypePrefix(Type *Ty) {
Chris Lattnera0b8c902010-06-21 23:12:56 +0000185 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 Lattnera0b8c902010-06-21 23:12:56 +0000197 default: return "other_";
Anton Korobeynikov78695032008-04-23 22:29:24 +0000198 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000199}
Anton Korobeynikov78695032008-04-23 22:29:24 +0000200
Chris Lattnera0b8c902010-06-21 23:12:56 +0000201void CppWriter::error(const std::string& msg) {
202 report_fatal_error(msg);
203}
Anton Korobeynikov78695032008-04-23 22:29:24 +0000204
Benjamin Kramercbf108e2012-03-23 11:26:29 +0000205static 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 Lattnera0b8c902010-06-21 23:12:56 +0000217// 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.
220void 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 Korobeynikov78695032008-04-23 22:29:24 +0000227#if HAVE_PRINTF_A
Chris Lattnera0b8c902010-06-21 23:12:56 +0000228 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 Korobeynikov78695032008-04-23 22:29:24 +0000236 else
Chris Lattnera0b8c902010-06-21 23:12:56 +0000237 Out << "BitsToFloat((float)" << Buffer << ")";
238 Out << ")";
239 } else {
240#endif
241 std::string StrVal = ftostr(CFP->getValueAPF());
Anton Korobeynikov78695032008-04-23 22:29:24 +0000242
Chris Lattnera0b8c902010-06-21 23:12:56 +0000243 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
272void 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
283void 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 Lattnera0b8c902010-06-21 23:12:56 +0000289 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 Lattnera0b8c902010-06-21 23:12:56 +0000303 case GlobalValue::ExternalWeakLinkage:
304 Out << "GlobalValue::ExternalWeakLinkage"; break;
305 case GlobalValue::CommonLinkage:
306 Out << "GlobalValue::CommonLinkage"; break;
307 }
308}
309
310void CppWriter::printVisibilityType(GlobalValue::VisibilityTypes VisType) {
311 switch (VisType) {
Chris Lattnera0b8c902010-06-21 23:12:56 +0000312 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 Rieck7157bb72014-01-14 15:22:47 +0000324void 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 Wennborgcbe34b42012-06-23 11:37:03 +0000339void 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 Lattnera0b8c902010-06-21 23:12:56 +0000359// printEscapedString - Print each character of the specified string, escaping
360// it if it is not printable or if it is an escape char.
361void 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 Lattner229907c2011-07-18 04:54:35 +0000374std::string CppWriter::getCppName(Type* Ty) {
Rafael Espindola08013342013-12-07 19:34:20 +0000375 switch (Ty->getTypeID()) {
376 default:
377 break;
378 case Type::VoidTyID:
Chris Lattnera0b8c902010-06-21 23:12:56 +0000379 return "Type::getVoidTy(mod->getContext())";
Rafael Espindola08013342013-12-07 19:34:20 +0000380 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 Korobeynikov78695032008-04-23 22:29:24 +0000394 }
395
Chris Lattnera0b8c902010-06-21 23:12:56 +0000396 // 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 Topper062a2ba2014-04-25 05:30:21 +0000402 const char* prefix = nullptr;
Chris Lattnera0b8c902010-06-21 23:12:56 +0000403 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 Lattnera0b8c902010-06-21 23:12:56 +0000408 case Type::VectorTyID: prefix = "VectorTy_"; break;
409 default: prefix = "OtherTy_"; break; // prevent breakage
Anton Korobeynikov78695032008-04-23 22:29:24 +0000410 }
411
Chris Lattnera0b8c902010-06-21 23:12:56 +0000412 // See if the type has a name in the symboltable and build accordingly
Chris Lattnera0b8c902010-06-21 23:12:56 +0000413 std::string name;
Chris Lattner229907c2011-07-18 04:54:35 +0000414 if (StructType *STy = dyn_cast<StructType>(Ty))
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000415 if (STy->hasName())
416 name = STy->getName();
417
418 if (name.empty())
419 name = utostr(uniqueNum++);
420
421 name = std::string(prefix) + name;
Chris Lattnera0b8c902010-06-21 23:12:56 +0000422 sanitize(name);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000423
Chris Lattnera0b8c902010-06-21 23:12:56 +0000424 // Save the name
425 return TypeNames[Ty] = name;
426}
427
Chris Lattner229907c2011-07-18 04:54:35 +0000428void CppWriter::printCppName(Type* Ty) {
Chris Lattnera0b8c902010-06-21 23:12:56 +0000429 printEscapedString(getCppName(Ty));
430}
431
432std::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 Korobeynikov78695032008-04-23 22:29:24 +0000455 } else {
456 name = getTypePrefix(val->getType());
457 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000458 } else {
459 name = getTypePrefix(val->getType());
Anton Korobeynikov78695032008-04-23 22:29:24 +0000460 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000461 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 Korobeynikov78695032008-04-23 22:29:24 +0000472
Chris Lattnera0b8c902010-06-21 23:12:56 +0000473void CppWriter::printCppName(const Value* val) {
474 printEscapedString(getCppName(val));
475}
Anton Korobeynikov78695032008-04-23 22:29:24 +0000476
Bill Wendlinge94d8432012-12-07 23:16:57 +0000477void CppWriter::printAttributes(const AttributeSet &PAL,
Chris Lattnera0b8c902010-06-21 23:12:56 +0000478 const std::string &name) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000479 Out << "AttributeSet " << name << "_PAL;";
Chris Lattnera0b8c902010-06-21 23:12:56 +0000480 nl(Out);
481 if (!PAL.isEmpty()) {
482 Out << '{'; in(); nl(Out);
Bill Wendlingcc1fc942013-01-27 01:22:51 +0000483 Out << "SmallVector<AttributeSet, 4> Attrs;"; nl(Out);
484 Out << "AttributeSet PAS;"; in(); nl(Out);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000485 for (unsigned i = 0; i < PAL.getNumSlots(); ++i) {
Bill Wendling86492832013-01-25 21:46:52 +0000486 unsigned index = PAL.getSlotIndex(i);
Bill Wendling57625a42013-01-25 23:09:36 +0000487 AttrBuilder attrs(PAL.getSlotAttributes(i), index);
Bill Wendlingcc1fc942013-01-27 01:22:51 +0000488 Out << "{"; in(); nl(Out);
489 Out << "AttrBuilder B;"; nl(Out);
Bill Wendlingbbcdf4e2012-10-10 07:36:45 +0000490
Bill Wendlingcc1fc942013-01-27 01:22:51 +0000491#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 Wendlingbbcdf4e2012-10-10 07:36:45 +0000496
Chris Lattnera0b8c902010-06-21 23:12:56 +0000497 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 Klecknera534a382013-12-19 02:14:12 +0000505 HANDLE_ATTR(InAlloca);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000506 HANDLE_ATTR(Nest);
507 HANDLE_ATTR(ReadNone);
508 HANDLE_ATTR(ReadOnly);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000509 HANDLE_ATTR(NoInline);
510 HANDLE_ATTR(AlwaysInline);
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000511 HANDLE_ATTR(OptimizeNone);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000512 HANDLE_ATTR(OptimizeForSize);
513 HANDLE_ATTR(StackProtect);
514 HANDLE_ATTR(StackProtectReq);
Bill Wendlingd154e2832013-01-23 06:41:41 +0000515 HANDLE_ATTR(StackProtectStrong);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000516 HANDLE_ATTR(NoCapture);
Eli Friedmanba9b25a2010-07-16 18:47:20 +0000517 HANDLE_ATTR(NoRedZone);
518 HANDLE_ATTR(NoImplicitFloat);
519 HANDLE_ATTR(Naked);
520 HANDLE_ATTR(InlineHint);
Rafael Espindolacc349c82011-10-03 14:45:37 +0000521 HANDLE_ATTR(ReturnsTwice);
Bill Wendling413bff12011-08-09 00:47:30 +0000522 HANDLE_ATTR(UWTable);
523 HANDLE_ATTR(NonLazyBind);
Quentin Colombet5799e9f2012-10-30 16:32:52 +0000524 HANDLE_ATTR(MinSize);
Chris Lattner1a579352009-01-13 07:22:22 +0000525#undef HANDLE_ATTR
Bill Wendlingcc1fc942013-01-27 01:22:51 +0000526
527 if (attrs.contains(Attribute::StackAlignment)) {
528 Out << "B.addStackAlignmentAttr(" << attrs.getStackAlignment()<<')';
529 nl(Out);
530 attrs.removeAttribute(Attribute::StackAlignment);
531 }
532
Bill Wendlingcc1fc942013-01-27 01:22:51 +0000533 Out << "PAS = AttributeSet::get(mod->getContext(), ";
534 if (index == ~0U)
535 Out << "~0U,";
536 else
537 Out << index << "U,";
538 Out << " B);"; out(); nl(Out);
539 Out << "}"; out(); nl(Out);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000540 nl(Out);
Bill Wendlingcc1fc942013-01-27 01:22:51 +0000541 Out << "Attrs.push_back(PAS);"; nl(Out);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000542 }
Bill Wendlinge94d8432012-12-07 23:16:57 +0000543 Out << name << "_PAL = AttributeSet::get(mod->getContext(), Attrs);";
Chris Lattnera0b8c902010-06-21 23:12:56 +0000544 nl(Out);
545 out(); nl(Out);
546 Out << '}'; nl(Out);
547 }
548}
549
Chris Lattner229907c2011-07-18 04:54:35 +0000550void CppWriter::printType(Type* Ty) {
Chris Lattnera0b8c902010-06-21 23:12:56 +0000551 // We don't print definitions for primitive types
Rafael Espindola08013342013-12-07 19:34:20 +0000552 if (Ty->isFloatingPointTy() || Ty->isX86_MMXTy() || Ty->isIntegerTy() ||
553 Ty->isLabelTy() || Ty->isMetadataTy() || Ty->isVoidTy())
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000554 return;
Chris Lattnera0b8c902010-06-21 23:12:56 +0000555
556 // If we already defined this type, we don't need to define it again.
557 if (DefinedTypes.find(Ty) != DefinedTypes.end())
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000558 return;
Chris Lattnera0b8c902010-06-21 23:12:56 +0000559
560 // Everything below needs the name for the type so get it now.
561 std::string typeName(getCppName(Ty));
562
Chris Lattnera0b8c902010-06-21 23:12:56 +0000563 // Print the type definition
564 switch (Ty->getTypeID()) {
565 case Type::FunctionTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000566 FunctionType* FT = cast<FunctionType>(Ty);
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000567 Out << "std::vector<Type*>" << typeName << "_args;";
Chris Lattnera0b8c902010-06-21 23:12:56 +0000568 nl(Out);
569 FunctionType::param_iterator PI = FT->param_begin();
570 FunctionType::param_iterator PE = FT->param_end();
571 for (; PI != PE; ++PI) {
Chris Lattner229907c2011-07-18 04:54:35 +0000572 Type* argTy = static_cast<Type*>(*PI);
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000573 printType(argTy);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000574 std::string argName(getCppName(argTy));
575 Out << typeName << "_args.push_back(" << argName;
Chris Lattnera0b8c902010-06-21 23:12:56 +0000576 Out << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +0000577 nl(Out);
578 }
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000579 printType(FT->getReturnType());
Chris Lattnera0b8c902010-06-21 23:12:56 +0000580 std::string retTypeName(getCppName(FT->getReturnType()));
581 Out << "FunctionType* " << typeName << " = FunctionType::get(";
582 in(); nl(Out) << "/*Result=*/" << retTypeName;
Chris Lattnera0b8c902010-06-21 23:12:56 +0000583 Out << ",";
584 nl(Out) << "/*Params=*/" << typeName << "_args,";
585 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
586 out();
Anton Korobeynikov78695032008-04-23 22:29:24 +0000587 nl(Out);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000588 break;
Anton Korobeynikov78695032008-04-23 22:29:24 +0000589 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000590 case Type::StructTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000591 StructType* ST = cast<StructType>(Ty);
Chris Lattner01beceb2011-08-12 18:07:07 +0000592 if (!ST->isLiteral()) {
Nicolas Geoffraya0263e72011-10-08 11:56:36 +0000593 Out << "StructType *" << typeName << " = mod->getTypeByName(\"";
594 printEscapedString(ST->getName());
595 Out << "\");";
596 nl(Out);
597 Out << "if (!" << typeName << ") {";
598 nl(Out);
599 Out << typeName << " = ";
Chris Lattner01beceb2011-08-12 18:07:07 +0000600 Out << "StructType::create(mod->getContext(), \"";
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000601 printEscapedString(ST->getName());
602 Out << "\");";
603 nl(Out);
Nicolas Geoffraya0263e72011-10-08 11:56:36 +0000604 Out << "}";
605 nl(Out);
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000606 // Indicate that this type is now defined.
607 DefinedTypes.insert(Ty);
608 }
609
610 Out << "std::vector<Type*>" << typeName << "_fields;";
Chris Lattnera0b8c902010-06-21 23:12:56 +0000611 nl(Out);
612 StructType::element_iterator EI = ST->element_begin();
613 StructType::element_iterator EE = ST->element_end();
614 for (; EI != EE; ++EI) {
Chris Lattner229907c2011-07-18 04:54:35 +0000615 Type* fieldTy = static_cast<Type*>(*EI);
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000616 printType(fieldTy);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000617 std::string fieldName(getCppName(fieldTy));
618 Out << typeName << "_fields.push_back(" << fieldName;
Chris Lattnera0b8c902010-06-21 23:12:56 +0000619 Out << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +0000620 nl(Out);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000621 }
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000622
Chris Lattner01beceb2011-08-12 18:07:07 +0000623 if (ST->isLiteral()) {
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000624 Out << "StructType *" << typeName << " = ";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000625 Out << "StructType::get(" << "mod->getContext(), ";
626 } else {
Nicolas Geoffraya0263e72011-10-08 11:56:36 +0000627 Out << "if (" << typeName << "->isOpaque()) {";
628 nl(Out);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000629 Out << typeName << "->setBody(";
630 }
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000631
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000632 Out << typeName << "_fields, /*isPacked=*/"
Chris Lattnera0b8c902010-06-21 23:12:56 +0000633 << (ST->isPacked() ? "true" : "false") << ");";
634 nl(Out);
Nicolas Geoffraya0263e72011-10-08 11:56:36 +0000635 if (!ST->isLiteral()) {
636 Out << "}";
637 nl(Out);
638 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000639 break;
640 }
641 case Type::ArrayTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000642 ArrayType* AT = cast<ArrayType>(Ty);
643 Type* ET = AT->getElementType();
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000644 printType(ET);
645 if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
646 std::string elemName(getCppName(ET));
647 Out << "ArrayType* " << typeName << " = ArrayType::get("
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000648 << elemName << ", " << AT->getNumElements() << ");";
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000649 nl(Out);
650 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000651 break;
652 }
653 case Type::PointerTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000654 PointerType* PT = cast<PointerType>(Ty);
655 Type* ET = PT->getElementType();
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000656 printType(ET);
657 if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
658 std::string elemName(getCppName(ET));
659 Out << "PointerType* " << typeName << " = PointerType::get("
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000660 << elemName << ", " << PT->getAddressSpace() << ");";
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000661 nl(Out);
662 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000663 break;
664 }
665 case Type::VectorTyID: {
Chris Lattner229907c2011-07-18 04:54:35 +0000666 VectorType* PT = cast<VectorType>(Ty);
667 Type* ET = PT->getElementType();
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000668 printType(ET);
669 if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
670 std::string elemName(getCppName(ET));
671 Out << "VectorType* " << typeName << " = VectorType::get("
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000672 << elemName << ", " << PT->getNumElements() << ");";
Nicolas Geoffrayf470b5b2011-07-14 21:04:35 +0000673 nl(Out);
674 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000675 break;
676 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000677 default:
678 error("Invalid TypeID");
679 }
680
Chris Lattnera0b8c902010-06-21 23:12:56 +0000681 // Indicate that this type is now defined.
682 DefinedTypes.insert(Ty);
683
Chris Lattnera0b8c902010-06-21 23:12:56 +0000684 // Finally, separate the type definition from other with a newline.
685 nl(Out);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000686}
687
688void CppWriter::printTypes(const Module* M) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000689 // Add all of the global variables to the value table.
Chris Lattnera0b8c902010-06-21 23:12:56 +0000690 for (Module::const_global_iterator I = TheModule->global_begin(),
691 E = TheModule->global_end(); I != E; ++I) {
692 if (I->hasInitializer())
693 printType(I->getInitializer()->getType());
694 printType(I->getType());
695 }
696
697 // Add all the functions to the table
698 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
699 FI != FE; ++FI) {
700 printType(FI->getReturnType());
701 printType(FI->getFunctionType());
702 // Add all the function arguments
703 for (Function::const_arg_iterator AI = FI->arg_begin(),
704 AE = FI->arg_end(); AI != AE; ++AI) {
705 printType(AI->getType());
706 }
707
708 // Add all of the basic blocks and instructions
709 for (Function::const_iterator BB = FI->begin(),
710 E = FI->end(); BB != E; ++BB) {
711 printType(BB->getType());
712 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
713 ++I) {
714 printType(I->getType());
715 for (unsigned i = 0; i < I->getNumOperands(); ++i)
716 printType(I->getOperand(i)->getType());
Anton Korobeynikov78695032008-04-23 22:29:24 +0000717 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000718 }
719 }
720}
721
722
723// printConstant - Print out a constant pool entry...
724void CppWriter::printConstant(const Constant *CV) {
725 // First, if the constant is actually a GlobalValue (variable or function)
726 // or its already in the constant list then we've printed it already and we
727 // can just return.
728 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
729 return;
730
731 std::string constName(getCppName(CV));
732 std::string typeName(getCppName(CV->getType()));
733
Chris Lattnera0b8c902010-06-21 23:12:56 +0000734 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
735 std::string constValue = CI->getValue().toString(10, true);
736 Out << "ConstantInt* " << constName
737 << " = ConstantInt::get(mod->getContext(), APInt("
738 << cast<IntegerType>(CI->getType())->getBitWidth()
739 << ", StringRef(\"" << constValue << "\"), 10));";
740 } else if (isa<ConstantAggregateZero>(CV)) {
741 Out << "ConstantAggregateZero* " << constName
742 << " = ConstantAggregateZero::get(" << typeName << ");";
743 } else if (isa<ConstantPointerNull>(CV)) {
744 Out << "ConstantPointerNull* " << constName
745 << " = ConstantPointerNull::get(" << typeName << ");";
746 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
747 Out << "ConstantFP* " << constName << " = ";
748 printCFP(CFP);
749 Out << ";";
750 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000751 Out << "std::vector<Constant*> " << constName << "_elems;";
752 nl(Out);
753 unsigned N = CA->getNumOperands();
754 for (unsigned i = 0; i < N; ++i) {
755 printConstant(CA->getOperand(i)); // recurse to print operands
756 Out << constName << "_elems.push_back("
757 << getCppName(CA->getOperand(i)) << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +0000758 nl(Out);
Chris Lattnera0b8c902010-06-21 23:12:56 +0000759 }
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000760 Out << "Constant* " << constName << " = ConstantArray::get("
761 << typeName << ", " << constName << "_elems);";
Chris Lattnera0b8c902010-06-21 23:12:56 +0000762 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
763 Out << "std::vector<Constant*> " << constName << "_fields;";
764 nl(Out);
765 unsigned N = CS->getNumOperands();
766 for (unsigned i = 0; i < N; i++) {
767 printConstant(CS->getOperand(i));
768 Out << constName << "_fields.push_back("
769 << getCppName(CS->getOperand(i)) << ");";
770 nl(Out);
771 }
772 Out << "Constant* " << constName << " = ConstantStruct::get("
773 << typeName << ", " << constName << "_fields);";
Duncan Sandsefabc252012-02-05 14:16:09 +0000774 } else if (const ConstantVector *CVec = dyn_cast<ConstantVector>(CV)) {
Chris Lattnera0b8c902010-06-21 23:12:56 +0000775 Out << "std::vector<Constant*> " << constName << "_elems;";
776 nl(Out);
Duncan Sandsefabc252012-02-05 14:16:09 +0000777 unsigned N = CVec->getNumOperands();
Chris Lattnera0b8c902010-06-21 23:12:56 +0000778 for (unsigned i = 0; i < N; ++i) {
Duncan Sandsefabc252012-02-05 14:16:09 +0000779 printConstant(CVec->getOperand(i));
Chris Lattnera0b8c902010-06-21 23:12:56 +0000780 Out << constName << "_elems.push_back("
Duncan Sandsefabc252012-02-05 14:16:09 +0000781 << getCppName(CVec->getOperand(i)) << ");";
Chris Lattnera0b8c902010-06-21 23:12:56 +0000782 nl(Out);
783 }
784 Out << "Constant* " << constName << " = ConstantVector::get("
785 << typeName << ", " << constName << "_elems);";
786 } else if (isa<UndefValue>(CV)) {
787 Out << "UndefValue* " << constName << " = UndefValue::get("
788 << typeName << ");";
Chris Lattner139822f2012-01-24 14:17:05 +0000789 } else if (const ConstantDataSequential *CDS =
790 dyn_cast<ConstantDataSequential>(CV)) {
791 if (CDS->isString()) {
792 Out << "Constant *" << constName <<
793 " = ConstantDataArray::getString(mod->getContext(), \"";
Chris Lattnercf9e8f62012-02-05 02:29:43 +0000794 StringRef Str = CDS->getAsString();
Chris Lattner139822f2012-01-24 14:17:05 +0000795 bool nullTerminate = false;
796 if (Str.back() == 0) {
797 Str = Str.drop_back();
798 nullTerminate = true;
799 }
800 printEscapedString(Str);
801 // Determine if we want null termination or not.
802 if (nullTerminate)
803 Out << "\", true);";
804 else
805 Out << "\", false);";// No null terminator
806 } else {
807 // TODO: Could generate more efficient code generating CDS calls instead.
808 Out << "std::vector<Constant*> " << constName << "_elems;";
809 nl(Out);
810 for (unsigned i = 0; i != CDS->getNumElements(); ++i) {
811 Constant *Elt = CDS->getElementAsConstant(i);
812 printConstant(Elt);
813 Out << constName << "_elems.push_back(" << getCppName(Elt) << ");";
814 nl(Out);
815 }
816 Out << "Constant* " << constName;
817
818 if (isa<ArrayType>(CDS->getType()))
819 Out << " = ConstantArray::get(";
820 else
821 Out << " = ConstantVector::get(";
822 Out << typeName << ", " << constName << "_elems);";
823 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000824 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
825 if (CE->getOpcode() == Instruction::GetElementPtr) {
826 Out << "std::vector<Constant*> " << constName << "_indices;";
827 nl(Out);
828 printConstant(CE->getOperand(0));
829 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
830 printConstant(CE->getOperand(i));
831 Out << constName << "_indices.push_back("
832 << getCppName(CE->getOperand(i)) << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +0000833 nl(Out);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000834 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000835 Out << "Constant* " << constName
836 << " = ConstantExpr::getGetElementPtr("
837 << getCppName(CE->getOperand(0)) << ", "
Nicolas Geoffray6820c1e2011-07-21 20:59:21 +0000838 << constName << "_indices);";
Chris Lattnera0b8c902010-06-21 23:12:56 +0000839 } else if (CE->isCast()) {
840 printConstant(CE->getOperand(0));
841 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
842 switch (CE->getOpcode()) {
843 default: llvm_unreachable("Invalid cast opcode");
844 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
845 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
846 case Instruction::SExt: Out << "Instruction::SExt"; break;
847 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
848 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
849 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
850 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
851 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
852 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
853 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
854 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
855 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
856 }
857 Out << ", " << getCppName(CE->getOperand(0)) << ", "
858 << getCppName(CE->getType()) << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +0000859 } else {
Chris Lattnera0b8c902010-06-21 23:12:56 +0000860 unsigned N = CE->getNumOperands();
861 for (unsigned i = 0; i < N; ++i ) {
862 printConstant(CE->getOperand(i));
863 }
864 Out << "Constant* " << constName << " = ConstantExpr::";
865 switch (CE->getOpcode()) {
866 case Instruction::Add: Out << "getAdd("; break;
867 case Instruction::FAdd: Out << "getFAdd("; break;
868 case Instruction::Sub: Out << "getSub("; break;
869 case Instruction::FSub: Out << "getFSub("; break;
870 case Instruction::Mul: Out << "getMul("; break;
871 case Instruction::FMul: Out << "getFMul("; break;
872 case Instruction::UDiv: Out << "getUDiv("; break;
873 case Instruction::SDiv: Out << "getSDiv("; break;
874 case Instruction::FDiv: Out << "getFDiv("; break;
875 case Instruction::URem: Out << "getURem("; break;
876 case Instruction::SRem: Out << "getSRem("; break;
877 case Instruction::FRem: Out << "getFRem("; break;
878 case Instruction::And: Out << "getAnd("; break;
879 case Instruction::Or: Out << "getOr("; break;
880 case Instruction::Xor: Out << "getXor("; break;
881 case Instruction::ICmp:
882 Out << "getICmp(ICmpInst::ICMP_";
883 switch (CE->getPredicate()) {
884 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
885 case ICmpInst::ICMP_NE: Out << "NE"; break;
886 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
887 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
888 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
889 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
890 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
891 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
892 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
893 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
894 default: error("Invalid ICmp Predicate");
895 }
896 break;
897 case Instruction::FCmp:
898 Out << "getFCmp(FCmpInst::FCMP_";
899 switch (CE->getPredicate()) {
900 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
901 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
902 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
903 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
904 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
905 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
906 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
907 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
908 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
909 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
910 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
911 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
912 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
913 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
914 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
915 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
916 default: error("Invalid FCmp Predicate");
917 }
918 break;
919 case Instruction::Shl: Out << "getShl("; break;
920 case Instruction::LShr: Out << "getLShr("; break;
921 case Instruction::AShr: Out << "getAShr("; break;
922 case Instruction::Select: Out << "getSelect("; break;
923 case Instruction::ExtractElement: Out << "getExtractElement("; break;
924 case Instruction::InsertElement: Out << "getInsertElement("; break;
925 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
926 default:
927 error("Invalid constant expression");
928 break;
929 }
930 Out << getCppName(CE->getOperand(0));
931 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
932 Out << ", " << getCppName(CE->getOperand(i));
933 Out << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +0000934 }
Chris Lattner64960f52010-06-21 23:19:36 +0000935 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
936 Out << "Constant* " << constName << " = ";
937 Out << "BlockAddress::get(" << getOpName(BA->getBasicBlock()) << ");";
Chris Lattnera0b8c902010-06-21 23:12:56 +0000938 } else {
939 error("Bad Constant");
940 Out << "Constant* " << constName << " = 0; ";
Anton Korobeynikov78695032008-04-23 22:29:24 +0000941 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000942 nl(Out);
943}
Anton Korobeynikov78695032008-04-23 22:29:24 +0000944
Chris Lattnera0b8c902010-06-21 23:12:56 +0000945void CppWriter::printConstants(const Module* M) {
946 // Traverse all the global variables looking for constant initializers
947 for (Module::const_global_iterator I = TheModule->global_begin(),
948 E = TheModule->global_end(); I != E; ++I)
949 if (I->hasInitializer())
950 printConstant(I->getInitializer());
Anton Korobeynikov78695032008-04-23 22:29:24 +0000951
Chris Lattnera0b8c902010-06-21 23:12:56 +0000952 // Traverse the LLVM functions looking for constants
953 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
954 FI != FE; ++FI) {
955 // Add all of the basic blocks and instructions
956 for (Function::const_iterator BB = FI->begin(),
957 E = FI->end(); BB != E; ++BB) {
958 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
959 ++I) {
960 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
961 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
962 printConstant(C);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000963 }
964 }
965 }
966 }
967 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000968}
Anton Korobeynikov78695032008-04-23 22:29:24 +0000969
Chris Lattnera0b8c902010-06-21 23:12:56 +0000970void CppWriter::printVariableUses(const GlobalVariable *GV) {
971 nl(Out) << "// Type Definitions";
972 nl(Out);
973 printType(GV->getType());
974 if (GV->hasInitializer()) {
Jay Foad60020682011-06-19 18:37:11 +0000975 const Constant *Init = GV->getInitializer();
Chris Lattnera0b8c902010-06-21 23:12:56 +0000976 printType(Init->getType());
Jay Foad60020682011-06-19 18:37:11 +0000977 if (const Function *F = dyn_cast<Function>(Init)) {
Chris Lattnera0b8c902010-06-21 23:12:56 +0000978 nl(Out)<< "/ Function Declarations"; nl(Out);
979 printFunctionHead(F);
Jay Foad60020682011-06-19 18:37:11 +0000980 } else if (const GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Chris Lattnera0b8c902010-06-21 23:12:56 +0000981 nl(Out) << "// Global Variable Declarations"; nl(Out);
982 printVariableHead(gv);
983
984 nl(Out) << "// Global Variable Definitions"; nl(Out);
985 printVariableBody(gv);
986 } else {
987 nl(Out) << "// Constant Definitions"; nl(Out);
988 printConstant(Init);
Anton Korobeynikov78695032008-04-23 22:29:24 +0000989 }
990 }
Chris Lattnera0b8c902010-06-21 23:12:56 +0000991}
Anton Korobeynikov78695032008-04-23 22:29:24 +0000992
Chris Lattnera0b8c902010-06-21 23:12:56 +0000993void CppWriter::printVariableHead(const GlobalVariable *GV) {
994 nl(Out) << "GlobalVariable* " << getCppName(GV);
995 if (is_inline) {
996 Out << " = mod->getGlobalVariable(mod->getContext(), ";
Anton Korobeynikov78695032008-04-23 22:29:24 +0000997 printEscapedString(GV->getName());
Chris Lattnera0b8c902010-06-21 23:12:56 +0000998 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
999 nl(Out) << "if (!" << getCppName(GV) << ") {";
1000 in(); nl(Out) << getCppName(GV);
1001 }
1002 Out << " = new GlobalVariable(/*Module=*/*mod, ";
1003 nl(Out) << "/*Type=*/";
1004 printCppName(GV->getType()->getElementType());
1005 Out << ",";
1006 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
1007 Out << ",";
1008 nl(Out) << "/*Linkage=*/";
1009 printLinkageType(GV->getLinkage());
1010 Out << ",";
1011 nl(Out) << "/*Initializer=*/0, ";
1012 if (GV->hasInitializer()) {
1013 Out << "// has initializer, specified below";
1014 }
1015 nl(Out) << "/*Name=*/\"";
1016 printEscapedString(GV->getName());
1017 Out << "\");";
1018 nl(Out);
1019
1020 if (GV->hasSection()) {
1021 printCppName(GV);
1022 Out << "->setSection(\"";
1023 printEscapedString(GV->getSection());
Owen Anderson96b491a2009-07-10 16:42:19 +00001024 Out << "\");";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001025 nl(Out);
Anton Korobeynikov78695032008-04-23 22:29:24 +00001026 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001027 if (GV->getAlignment()) {
1028 printCppName(GV);
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001029 Out << "->setAlignment(" << GV->getAlignment() << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001030 nl(Out);
Chris Lattnera0b8c902010-06-21 23:12:56 +00001031 }
1032 if (GV->getVisibility() != GlobalValue::DefaultVisibility) {
1033 printCppName(GV);
1034 Out << "->setVisibility(";
1035 printVisibilityType(GV->getVisibility());
1036 Out << ");";
1037 nl(Out);
1038 }
Nico Rieck7157bb72014-01-14 15:22:47 +00001039 if (GV->getDLLStorageClass() != GlobalValue::DefaultStorageClass) {
1040 printCppName(GV);
1041 Out << "->setDLLStorageClass(";
1042 printDLLStorageClassType(GV->getDLLStorageClass());
1043 Out << ");";
1044 nl(Out);
1045 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001046 if (GV->isThreadLocal()) {
1047 printCppName(GV);
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001048 Out << "->setThreadLocalMode(";
1049 printThreadLocalMode(GV->getThreadLocalMode());
1050 Out << ");";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001051 nl(Out);
1052 }
1053 if (is_inline) {
1054 out(); Out << "}"; nl(Out);
1055 }
1056}
1057
1058void CppWriter::printVariableBody(const GlobalVariable *GV) {
1059 if (GV->hasInitializer()) {
1060 printCppName(GV);
1061 Out << "->setInitializer(";
1062 Out << getCppName(GV->getInitializer()) << ");";
1063 nl(Out);
1064 }
1065}
1066
Eli Friedman95031ed2011-09-29 20:21:17 +00001067std::string CppWriter::getOpName(const Value* V) {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001068 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
1069 return getCppName(V);
1070
1071 // See if its alread in the map of forward references, if so just return the
1072 // name we already set up for it
1073 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
1074 if (I != ForwardRefs.end())
1075 return I->second;
1076
1077 // This is a new forward reference. Generate a unique name for it
1078 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
1079
1080 // Yes, this is a hack. An Argument is the smallest instantiable value that
1081 // we can make as a placeholder for the real value. We'll replace these
1082 // Argument instances later.
1083 Out << "Argument* " << result << " = new Argument("
1084 << getCppName(V->getType()) << ");";
1085 nl(Out);
1086 ForwardRefs[V] = result;
1087 return result;
1088}
1089
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001090static StringRef ConvertAtomicOrdering(AtomicOrdering Ordering) {
1091 switch (Ordering) {
1092 case NotAtomic: return "NotAtomic";
1093 case Unordered: return "Unordered";
1094 case Monotonic: return "Monotonic";
1095 case Acquire: return "Acquire";
1096 case Release: return "Release";
1097 case AcquireRelease: return "AcquireRelease";
1098 case SequentiallyConsistent: return "SequentiallyConsistent";
1099 }
1100 llvm_unreachable("Unknown ordering");
1101}
1102
1103static StringRef ConvertAtomicSynchScope(SynchronizationScope SynchScope) {
1104 switch (SynchScope) {
1105 case SingleThread: return "SingleThread";
1106 case CrossThread: return "CrossThread";
1107 }
1108 llvm_unreachable("Unknown synch scope");
1109}
1110
Chris Lattnera0b8c902010-06-21 23:12:56 +00001111// printInstruction - This member is called for each Instruction in a function.
1112void CppWriter::printInstruction(const Instruction *I,
1113 const std::string& bbname) {
1114 std::string iName(getCppName(I));
1115
1116 // Before we emit this instruction, we need to take care of generating any
1117 // forward references. So, we get the names of all the operands in advance
1118 const unsigned Ops(I->getNumOperands());
1119 std::string* opNames = new std::string[Ops];
Chris Lattner64960f52010-06-21 23:19:36 +00001120 for (unsigned i = 0; i < Ops; i++)
Chris Lattnera0b8c902010-06-21 23:12:56 +00001121 opNames[i] = getOpName(I->getOperand(i));
Anton Korobeynikov78695032008-04-23 22:29:24 +00001122
Chris Lattnera0b8c902010-06-21 23:12:56 +00001123 switch (I->getOpcode()) {
1124 default:
1125 error("Invalid instruction");
1126 break;
Anton Korobeynikov78695032008-04-23 22:29:24 +00001127
Chris Lattnera0b8c902010-06-21 23:12:56 +00001128 case Instruction::Ret: {
1129 const ReturnInst* ret = cast<ReturnInst>(I);
1130 Out << "ReturnInst::Create(mod->getContext(), "
1131 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
1132 break;
1133 }
1134 case Instruction::Br: {
1135 const BranchInst* br = cast<BranchInst>(I);
1136 Out << "BranchInst::Create(" ;
Chris Lattner64960f52010-06-21 23:19:36 +00001137 if (br->getNumOperands() == 3) {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001138 Out << opNames[2] << ", "
Anton Korobeynikov78695032008-04-23 22:29:24 +00001139 << opNames[1] << ", "
Chris Lattnera0b8c902010-06-21 23:12:56 +00001140 << opNames[0] << ", ";
1141
1142 } else if (br->getNumOperands() == 1) {
1143 Out << opNames[0] << ", ";
1144 } else {
1145 error("Branch with 2 operands?");
1146 }
1147 Out << bbname << ");";
1148 break;
1149 }
1150 case Instruction::Switch: {
1151 const SwitchInst *SI = cast<SwitchInst>(I);
1152 Out << "SwitchInst* " << iName << " = SwitchInst::Create("
Eli Friedman95031ed2011-09-29 20:21:17 +00001153 << getOpName(SI->getCondition()) << ", "
1154 << getOpName(SI->getDefaultDest()) << ", "
Chris Lattnera0b8c902010-06-21 23:12:56 +00001155 << SI->getNumCases() << ", " << bbname << ");";
1156 nl(Out);
Stepan Dyatkovskiy97b02fc2012-03-11 06:09:17 +00001157 for (SwitchInst::ConstCaseIt i = SI->case_begin(), e = SI->case_end();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001158 i != e; ++i) {
Bob Wilsone4077362013-09-09 19:14:35 +00001159 const ConstantInt* CaseVal = i.getCaseValue();
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00001160 const BasicBlock *BB = i.getCaseSuccessor();
Chris Lattnera0b8c902010-06-21 23:12:56 +00001161 Out << iName << "->addCase("
Eli Friedman95031ed2011-09-29 20:21:17 +00001162 << getOpName(CaseVal) << ", "
1163 << getOpName(BB) << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001164 nl(Out);
Chris Lattnera0b8c902010-06-21 23:12:56 +00001165 }
1166 break;
1167 }
1168 case Instruction::IndirectBr: {
1169 const IndirectBrInst *IBI = cast<IndirectBrInst>(I);
1170 Out << "IndirectBrInst *" << iName << " = IndirectBrInst::Create("
1171 << opNames[0] << ", " << IBI->getNumDestinations() << ");";
1172 nl(Out);
1173 for (unsigned i = 1; i != IBI->getNumOperands(); ++i) {
1174 Out << iName << "->addDestination(" << opNames[i] << ");";
1175 nl(Out);
1176 }
1177 break;
1178 }
Bill Wendlingf891bf82011-07-31 06:30:59 +00001179 case Instruction::Resume: {
Eli Friedman410393a2013-09-24 00:36:09 +00001180 Out << "ResumeInst::Create(" << opNames[0] << ", " << bbname << ");";
Bill Wendlingf891bf82011-07-31 06:30:59 +00001181 break;
1182 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001183 case Instruction::Invoke: {
1184 const InvokeInst* inv = cast<InvokeInst>(I);
1185 Out << "std::vector<Value*> " << iName << "_params;";
1186 nl(Out);
1187 for (unsigned i = 0; i < inv->getNumArgOperands(); ++i) {
1188 Out << iName << "_params.push_back("
1189 << getOpName(inv->getArgOperand(i)) << ");";
1190 nl(Out);
1191 }
1192 // FIXME: This shouldn't use magic numbers -3, -2, and -1.
1193 Out << "InvokeInst *" << iName << " = InvokeInst::Create("
Eli Friedman410393a2013-09-24 00:36:09 +00001194 << getOpName(inv->getCalledValue()) << ", "
Chris Lattnera0b8c902010-06-21 23:12:56 +00001195 << getOpName(inv->getNormalDest()) << ", "
1196 << getOpName(inv->getUnwindDest()) << ", "
Nick Lewyckydf06b6e2011-09-05 18:50:59 +00001197 << iName << "_params, \"";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001198 printEscapedString(inv->getName());
1199 Out << "\", " << bbname << ");";
1200 nl(Out) << iName << "->setCallingConv(";
1201 printCallingConv(inv->getCallingConv());
1202 Out << ");";
1203 printAttributes(inv->getAttributes(), iName);
1204 Out << iName << "->setAttributes(" << iName << "_PAL);";
1205 nl(Out);
1206 break;
1207 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001208 case Instruction::Unreachable: {
1209 Out << "new UnreachableInst("
1210 << "mod->getContext(), "
1211 << bbname << ");";
1212 break;
1213 }
1214 case Instruction::Add:
1215 case Instruction::FAdd:
1216 case Instruction::Sub:
1217 case Instruction::FSub:
1218 case Instruction::Mul:
1219 case Instruction::FMul:
1220 case Instruction::UDiv:
1221 case Instruction::SDiv:
1222 case Instruction::FDiv:
1223 case Instruction::URem:
1224 case Instruction::SRem:
1225 case Instruction::FRem:
1226 case Instruction::And:
1227 case Instruction::Or:
1228 case Instruction::Xor:
1229 case Instruction::Shl:
1230 case Instruction::LShr:
1231 case Instruction::AShr:{
1232 Out << "BinaryOperator* " << iName << " = BinaryOperator::Create(";
1233 switch (I->getOpcode()) {
1234 case Instruction::Add: Out << "Instruction::Add"; break;
1235 case Instruction::FAdd: Out << "Instruction::FAdd"; break;
1236 case Instruction::Sub: Out << "Instruction::Sub"; break;
1237 case Instruction::FSub: Out << "Instruction::FSub"; break;
1238 case Instruction::Mul: Out << "Instruction::Mul"; break;
1239 case Instruction::FMul: Out << "Instruction::FMul"; break;
1240 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1241 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1242 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
1243 case Instruction::URem:Out << "Instruction::URem"; break;
1244 case Instruction::SRem:Out << "Instruction::SRem"; break;
1245 case Instruction::FRem:Out << "Instruction::FRem"; break;
1246 case Instruction::And: Out << "Instruction::And"; break;
1247 case Instruction::Or: Out << "Instruction::Or"; break;
1248 case Instruction::Xor: Out << "Instruction::Xor"; break;
1249 case Instruction::Shl: Out << "Instruction::Shl"; break;
1250 case Instruction::LShr:Out << "Instruction::LShr"; break;
1251 case Instruction::AShr:Out << "Instruction::AShr"; break;
1252 default: Out << "Instruction::BadOpCode"; break;
1253 }
1254 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1255 printEscapedString(I->getName());
1256 Out << "\", " << bbname << ");";
1257 break;
1258 }
1259 case Instruction::FCmp: {
1260 Out << "FCmpInst* " << iName << " = new FCmpInst(*" << bbname << ", ";
1261 switch (cast<FCmpInst>(I)->getPredicate()) {
1262 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1263 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1264 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1265 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1266 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1267 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1268 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1269 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1270 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1271 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1272 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1273 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1274 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1275 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1276 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1277 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1278 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1279 }
1280 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1281 printEscapedString(I->getName());
1282 Out << "\");";
1283 break;
1284 }
1285 case Instruction::ICmp: {
1286 Out << "ICmpInst* " << iName << " = new ICmpInst(*" << bbname << ", ";
1287 switch (cast<ICmpInst>(I)->getPredicate()) {
1288 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1289 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1290 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1291 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1292 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1293 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1294 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1295 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1296 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1297 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1298 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
1299 }
1300 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1301 printEscapedString(I->getName());
1302 Out << "\");";
1303 break;
1304 }
1305 case Instruction::Alloca: {
1306 const AllocaInst* allocaI = cast<AllocaInst>(I);
1307 Out << "AllocaInst* " << iName << " = new AllocaInst("
1308 << getCppName(allocaI->getAllocatedType()) << ", ";
1309 if (allocaI->isArrayAllocation())
1310 Out << opNames[0] << ", ";
1311 Out << "\"";
1312 printEscapedString(allocaI->getName());
1313 Out << "\", " << bbname << ");";
1314 if (allocaI->getAlignment())
1315 nl(Out) << iName << "->setAlignment("
1316 << allocaI->getAlignment() << ");";
1317 break;
1318 }
Gabor Greif7d4038dd2010-06-26 12:17:21 +00001319 case Instruction::Load: {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001320 const LoadInst* load = cast<LoadInst>(I);
1321 Out << "LoadInst* " << iName << " = new LoadInst("
1322 << opNames[0] << ", \"";
1323 printEscapedString(load->getName());
1324 Out << "\", " << (load->isVolatile() ? "true" : "false" )
1325 << ", " << bbname << ");";
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001326 if (load->getAlignment())
1327 nl(Out) << iName << "->setAlignment("
1328 << load->getAlignment() << ");";
1329 if (load->isAtomic()) {
1330 StringRef Ordering = ConvertAtomicOrdering(load->getOrdering());
1331 StringRef CrossThread = ConvertAtomicSynchScope(load->getSynchScope());
1332 nl(Out) << iName << "->setAtomic("
1333 << Ordering << ", " << CrossThread << ");";
1334 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001335 break;
1336 }
1337 case Instruction::Store: {
1338 const StoreInst* store = cast<StoreInst>(I);
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001339 Out << "StoreInst* " << iName << " = new StoreInst("
Chris Lattnera0b8c902010-06-21 23:12:56 +00001340 << opNames[0] << ", "
1341 << opNames[1] << ", "
1342 << (store->isVolatile() ? "true" : "false")
1343 << ", " << bbname << ");";
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001344 if (store->getAlignment())
1345 nl(Out) << iName << "->setAlignment("
1346 << store->getAlignment() << ");";
1347 if (store->isAtomic()) {
1348 StringRef Ordering = ConvertAtomicOrdering(store->getOrdering());
1349 StringRef CrossThread = ConvertAtomicSynchScope(store->getSynchScope());
1350 nl(Out) << iName << "->setAtomic("
1351 << Ordering << ", " << CrossThread << ");";
1352 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001353 break;
1354 }
1355 case Instruction::GetElementPtr: {
1356 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1357 if (gep->getNumOperands() <= 2) {
1358 Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
1359 << opNames[0];
1360 if (gep->getNumOperands() == 2)
1361 Out << ", " << opNames[1];
1362 } else {
1363 Out << "std::vector<Value*> " << iName << "_indices;";
1364 nl(Out);
1365 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
1366 Out << iName << "_indices.push_back("
1367 << opNames[i] << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001368 nl(Out);
1369 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001370 Out << "Instruction* " << iName << " = GetElementPtrInst::Create("
Nicolas Geoffray84c7b9e2011-07-26 20:52:25 +00001371 << opNames[0] << ", " << iName << "_indices";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001372 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001373 Out << ", \"";
1374 printEscapedString(gep->getName());
1375 Out << "\", " << bbname << ");";
1376 break;
1377 }
1378 case Instruction::PHI: {
1379 const PHINode* phi = cast<PHINode>(I);
1380
1381 Out << "PHINode* " << iName << " = PHINode::Create("
Nicolas Geoffray9137ee82011-04-10 17:39:40 +00001382 << getCppName(phi->getType()) << ", "
Jay Foad52131342011-03-30 11:28:46 +00001383 << phi->getNumIncomingValues() << ", \"";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001384 printEscapedString(phi->getName());
1385 Out << "\", " << bbname << ");";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001386 nl(Out);
Jay Foad372ad642011-06-20 14:18:48 +00001387 for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001388 Out << iName << "->addIncoming("
Jay Foad372ad642011-06-20 14:18:48 +00001389 << opNames[PHINode::getOperandNumForIncomingValue(i)] << ", "
Jay Foad61ea0e42011-06-23 09:09:15 +00001390 << getOpName(phi->getIncomingBlock(i)) << ");";
Chris Lattnere8628a02009-10-27 21:24:48 +00001391 nl(Out);
Chris Lattnere8628a02009-10-27 21:24:48 +00001392 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001393 break;
1394 }
1395 case Instruction::Trunc:
1396 case Instruction::ZExt:
1397 case Instruction::SExt:
1398 case Instruction::FPTrunc:
1399 case Instruction::FPExt:
1400 case Instruction::FPToUI:
1401 case Instruction::FPToSI:
1402 case Instruction::UIToFP:
1403 case Instruction::SIToFP:
1404 case Instruction::PtrToInt:
1405 case Instruction::IntToPtr:
1406 case Instruction::BitCast: {
1407 const CastInst* cst = cast<CastInst>(I);
1408 Out << "CastInst* " << iName << " = new ";
1409 switch (I->getOpcode()) {
1410 case Instruction::Trunc: Out << "TruncInst"; break;
1411 case Instruction::ZExt: Out << "ZExtInst"; break;
1412 case Instruction::SExt: Out << "SExtInst"; break;
1413 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1414 case Instruction::FPExt: Out << "FPExtInst"; break;
1415 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1416 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1417 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1418 case Instruction::SIToFP: Out << "SIToFPInst"; break;
1419 case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
1420 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1421 case Instruction::BitCast: Out << "BitCastInst"; break;
Craig Toppere55c5562012-02-07 02:50:20 +00001422 default: llvm_unreachable("Unreachable");
Chris Lattnera0b8c902010-06-21 23:12:56 +00001423 }
1424 Out << "(" << opNames[0] << ", "
1425 << getCppName(cst->getType()) << ", \"";
1426 printEscapedString(cst->getName());
1427 Out << "\", " << bbname << ");";
1428 break;
1429 }
Gabor Greif7d4038dd2010-06-26 12:17:21 +00001430 case Instruction::Call: {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001431 const CallInst* call = cast<CallInst>(I);
1432 if (const InlineAsm* ila = dyn_cast<InlineAsm>(call->getCalledValue())) {
1433 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
1434 << getCppName(ila->getFunctionType()) << ", \""
1435 << ila->getAsmString() << "\", \""
1436 << ila->getConstraintString() << "\","
1437 << (ila->hasSideEffects() ? "true" : "false") << ");";
1438 nl(Out);
1439 }
Gabor Greif7d4038dd2010-06-26 12:17:21 +00001440 if (call->getNumArgOperands() > 1) {
Anton Korobeynikov78695032008-04-23 22:29:24 +00001441 Out << "std::vector<Value*> " << iName << "_params;";
1442 nl(Out);
Gabor Greife537ddb2010-07-02 19:08:46 +00001443 for (unsigned i = 0; i < call->getNumArgOperands(); ++i) {
Gabor Greif03e7e682010-07-13 15:31:36 +00001444 Out << iName << "_params.push_back(" << opNames[i] << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001445 nl(Out);
1446 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001447 Out << "CallInst* " << iName << " = CallInst::Create("
Gabor Greif3e44ea12010-07-22 10:37:47 +00001448 << opNames[call->getNumArgOperands()] << ", "
Nicolas Geoffray6820c1e2011-07-21 20:59:21 +00001449 << iName << "_params, \"";
Gabor Greif7d4038dd2010-06-26 12:17:21 +00001450 } else if (call->getNumArgOperands() == 1) {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001451 Out << "CallInst* " << iName << " = CallInst::Create("
Gabor Greif03e7e682010-07-13 15:31:36 +00001452 << opNames[call->getNumArgOperands()] << ", " << opNames[0] << ", \"";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001453 } else {
Gabor Greif03e7e682010-07-13 15:31:36 +00001454 Out << "CallInst* " << iName << " = CallInst::Create("
1455 << opNames[call->getNumArgOperands()] << ", \"";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001456 }
1457 printEscapedString(call->getName());
1458 Out << "\", " << bbname << ");";
1459 nl(Out) << iName << "->setCallingConv(";
1460 printCallingConv(call->getCallingConv());
1461 Out << ");";
1462 nl(Out) << iName << "->setTailCall("
Gabor Greif7d4038dd2010-06-26 12:17:21 +00001463 << (call->isTailCall() ? "true" : "false");
Chris Lattnera0b8c902010-06-21 23:12:56 +00001464 Out << ");";
Gabor Greif9da02a82010-07-02 19:26:28 +00001465 nl(Out);
Chris Lattnera0b8c902010-06-21 23:12:56 +00001466 printAttributes(call->getAttributes(), iName);
1467 Out << iName << "->setAttributes(" << iName << "_PAL);";
1468 nl(Out);
1469 break;
1470 }
1471 case Instruction::Select: {
1472 const SelectInst* sel = cast<SelectInst>(I);
1473 Out << "SelectInst* " << getCppName(sel) << " = SelectInst::Create(";
1474 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1475 printEscapedString(sel->getName());
1476 Out << "\", " << bbname << ");";
1477 break;
1478 }
1479 case Instruction::UserOp1:
1480 /// FALL THROUGH
1481 case Instruction::UserOp2: {
1482 /// FIXME: What should be done here?
1483 break;
1484 }
1485 case Instruction::VAArg: {
1486 const VAArgInst* va = cast<VAArgInst>(I);
1487 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
1488 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
1489 printEscapedString(va->getName());
1490 Out << "\", " << bbname << ");";
1491 break;
1492 }
1493 case Instruction::ExtractElement: {
1494 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
1495 Out << "ExtractElementInst* " << getCppName(eei)
1496 << " = new ExtractElementInst(" << opNames[0]
1497 << ", " << opNames[1] << ", \"";
1498 printEscapedString(eei->getName());
1499 Out << "\", " << bbname << ");";
1500 break;
1501 }
1502 case Instruction::InsertElement: {
1503 const InsertElementInst* iei = cast<InsertElementInst>(I);
1504 Out << "InsertElementInst* " << getCppName(iei)
1505 << " = InsertElementInst::Create(" << opNames[0]
1506 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1507 printEscapedString(iei->getName());
1508 Out << "\", " << bbname << ");";
1509 break;
1510 }
1511 case Instruction::ShuffleVector: {
1512 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
1513 Out << "ShuffleVectorInst* " << getCppName(svi)
1514 << " = new ShuffleVectorInst(" << opNames[0]
1515 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1516 printEscapedString(svi->getName());
1517 Out << "\", " << bbname << ");";
1518 break;
1519 }
1520 case Instruction::ExtractValue: {
1521 const ExtractValueInst *evi = cast<ExtractValueInst>(I);
1522 Out << "std::vector<unsigned> " << iName << "_indices;";
1523 nl(Out);
1524 for (unsigned i = 0; i < evi->getNumIndices(); ++i) {
1525 Out << iName << "_indices.push_back("
1526 << evi->idx_begin()[i] << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001527 nl(Out);
Anton Korobeynikov78695032008-04-23 22:29:24 +00001528 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001529 Out << "ExtractValueInst* " << getCppName(evi)
1530 << " = ExtractValueInst::Create(" << opNames[0]
1531 << ", "
Nick Lewyckydf06b6e2011-09-05 18:50:59 +00001532 << iName << "_indices, \"";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001533 printEscapedString(evi->getName());
1534 Out << "\", " << bbname << ");";
1535 break;
1536 }
1537 case Instruction::InsertValue: {
1538 const InsertValueInst *ivi = cast<InsertValueInst>(I);
1539 Out << "std::vector<unsigned> " << iName << "_indices;";
1540 nl(Out);
1541 for (unsigned i = 0; i < ivi->getNumIndices(); ++i) {
1542 Out << iName << "_indices.push_back("
1543 << ivi->idx_begin()[i] << ");";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001544 nl(Out);
Anton Korobeynikov78695032008-04-23 22:29:24 +00001545 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001546 Out << "InsertValueInst* " << getCppName(ivi)
1547 << " = InsertValueInst::Create(" << opNames[0]
1548 << ", " << opNames[1] << ", "
Nick Lewyckydf06b6e2011-09-05 18:50:59 +00001549 << iName << "_indices, \"";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001550 printEscapedString(ivi->getName());
1551 Out << "\", " << bbname << ");";
1552 break;
1553 }
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001554 case Instruction::Fence: {
1555 const FenceInst *fi = cast<FenceInst>(I);
1556 StringRef Ordering = ConvertAtomicOrdering(fi->getOrdering());
1557 StringRef CrossThread = ConvertAtomicSynchScope(fi->getSynchScope());
1558 Out << "FenceInst* " << iName
1559 << " = new FenceInst(mod->getContext(), "
Eli Friedman5b693c22011-11-04 17:29:35 +00001560 << Ordering << ", " << CrossThread << ", " << bbname
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001561 << ");";
1562 break;
1563 }
1564 case Instruction::AtomicCmpXchg: {
1565 const AtomicCmpXchgInst *cxi = cast<AtomicCmpXchgInst>(I);
Tim Northovere94a5182014-03-11 10:48:52 +00001566 StringRef SuccessOrdering =
1567 ConvertAtomicOrdering(cxi->getSuccessOrdering());
1568 StringRef FailureOrdering =
1569 ConvertAtomicOrdering(cxi->getFailureOrdering());
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001570 StringRef CrossThread = ConvertAtomicSynchScope(cxi->getSynchScope());
1571 Out << "AtomicCmpXchgInst* " << iName
1572 << " = new AtomicCmpXchgInst("
1573 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", "
Tim Northovere94a5182014-03-11 10:48:52 +00001574 << SuccessOrdering << ", " << FailureOrdering << ", "
1575 << CrossThread << ", " << bbname
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001576 << ");";
1577 nl(Out) << iName << "->setName(\"";
1578 printEscapedString(cxi->getName());
1579 Out << "\");";
Tim Northover24fe2322014-06-13 09:14:50 +00001580 nl(Out) << iName << "->setVolatile("
1581 << (cxi->isVolatile() ? "true" : "false") << ");";
Tim Northover420a2162014-06-13 14:24:07 +00001582 nl(Out) << iName << "->setWeak("
1583 << (cxi->isWeak() ? "true" : "false") << ");";
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001584 break;
1585 }
1586 case Instruction::AtomicRMW: {
1587 const AtomicRMWInst *rmwi = cast<AtomicRMWInst>(I);
1588 StringRef Ordering = ConvertAtomicOrdering(rmwi->getOrdering());
1589 StringRef CrossThread = ConvertAtomicSynchScope(rmwi->getSynchScope());
1590 StringRef Operation;
1591 switch (rmwi->getOperation()) {
1592 case AtomicRMWInst::Xchg: Operation = "AtomicRMWInst::Xchg"; break;
1593 case AtomicRMWInst::Add: Operation = "AtomicRMWInst::Add"; break;
1594 case AtomicRMWInst::Sub: Operation = "AtomicRMWInst::Sub"; break;
1595 case AtomicRMWInst::And: Operation = "AtomicRMWInst::And"; break;
1596 case AtomicRMWInst::Nand: Operation = "AtomicRMWInst::Nand"; break;
1597 case AtomicRMWInst::Or: Operation = "AtomicRMWInst::Or"; break;
1598 case AtomicRMWInst::Xor: Operation = "AtomicRMWInst::Xor"; break;
1599 case AtomicRMWInst::Max: Operation = "AtomicRMWInst::Max"; break;
1600 case AtomicRMWInst::Min: Operation = "AtomicRMWInst::Min"; break;
1601 case AtomicRMWInst::UMax: Operation = "AtomicRMWInst::UMax"; break;
1602 case AtomicRMWInst::UMin: Operation = "AtomicRMWInst::UMin"; break;
1603 case AtomicRMWInst::BAD_BINOP: llvm_unreachable("Bad atomic operation");
1604 }
1605 Out << "AtomicRMWInst* " << iName
1606 << " = new AtomicRMWInst("
1607 << Operation << ", "
1608 << opNames[0] << ", " << opNames[1] << ", "
Eli Friedman5b693c22011-11-04 17:29:35 +00001609 << Ordering << ", " << CrossThread << ", " << bbname
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001610 << ");";
1611 nl(Out) << iName << "->setName(\"";
1612 printEscapedString(rmwi->getName());
1613 Out << "\");";
Tim Northover24fe2322014-06-13 09:14:50 +00001614 nl(Out) << iName << "->setVolatile("
1615 << (rmwi->isVolatile() ? "true" : "false") << ");";
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001616 break;
1617 }
Eli Friedman410393a2013-09-24 00:36:09 +00001618 case Instruction::LandingPad: {
1619 const LandingPadInst *lpi = cast<LandingPadInst>(I);
1620 Out << "LandingPadInst* " << iName << " = LandingPadInst::Create(";
1621 printCppName(lpi->getType());
1622 Out << ", " << opNames[0] << ", " << lpi->getNumClauses() << ", \"";
1623 printEscapedString(lpi->getName());
1624 Out << "\", " << bbname << ");";
1625 nl(Out) << iName << "->setCleanup("
1626 << (lpi->isCleanup() ? "true" : "false")
1627 << ");";
1628 for (unsigned i = 0, e = lpi->getNumClauses(); i != e; ++i)
1629 nl(Out) << iName << "->addClause(" << opNames[i+1] << ");";
1630 break;
1631 }
Anton Korobeynikov78695032008-04-23 22:29:24 +00001632 }
1633 DefinedValues.insert(I);
1634 nl(Out);
1635 delete [] opNames;
1636}
1637
Chris Lattnera0b8c902010-06-21 23:12:56 +00001638// Print out the types, constants and declarations needed by one function
1639void CppWriter::printFunctionUses(const Function* F) {
1640 nl(Out) << "// Type Definitions"; nl(Out);
1641 if (!is_inline) {
1642 // Print the function's return type
1643 printType(F->getReturnType());
Anton Korobeynikov78695032008-04-23 22:29:24 +00001644
Chris Lattnera0b8c902010-06-21 23:12:56 +00001645 // Print the function's function type
1646 printType(F->getFunctionType());
Anton Korobeynikov78695032008-04-23 22:29:24 +00001647
Chris Lattnera0b8c902010-06-21 23:12:56 +00001648 // Print the types of each of the function's arguments
Anton Korobeynikov78695032008-04-23 22:29:24 +00001649 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1650 AI != AE; ++AI) {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001651 printType(AI->getType());
Anton Korobeynikov78695032008-04-23 22:29:24 +00001652 }
Anton Korobeynikov78695032008-04-23 22:29:24 +00001653 }
1654
Chris Lattnera0b8c902010-06-21 23:12:56 +00001655 // Print type definitions for every type referenced by an instruction and
1656 // make a note of any global values or constants that are referenced
1657 SmallPtrSet<GlobalValue*,64> gvs;
1658 SmallPtrSet<Constant*,64> consts;
1659 for (Function::const_iterator BB = F->begin(), BE = F->end();
1660 BB != BE; ++BB){
1661 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
Anton Korobeynikov78695032008-04-23 22:29:24 +00001662 I != E; ++I) {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001663 // Print the type of the instruction itself
1664 printType(I->getType());
1665
1666 // Print the type of each of the instruction's operands
1667 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1668 Value* operand = I->getOperand(i);
1669 printType(operand->getType());
1670
1671 // If the operand references a GVal or Constant, make a note of it
1672 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
1673 gvs.insert(GV);
Nicolas Geoffray235b66c2010-11-28 18:00:53 +00001674 if (GenerationType != GenFunction)
1675 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1676 if (GVar->hasInitializer())
1677 consts.insert(GVar->getInitializer());
1678 } else if (Constant* C = dyn_cast<Constant>(operand)) {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001679 consts.insert(C);
Nicolas Geoffray235b66c2010-11-28 18:00:53 +00001680 for (unsigned j = 0; j < C->getNumOperands(); ++j) {
1681 // If the operand references a GVal or Constant, make a note of it
1682 Value* operand = C->getOperand(j);
1683 printType(operand->getType());
1684 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
1685 gvs.insert(GV);
1686 if (GenerationType != GenFunction)
1687 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1688 if (GVar->hasInitializer())
1689 consts.insert(GVar->getInitializer());
1690 }
1691 }
1692 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001693 }
1694 }
1695 }
1696
1697 // Print the function declarations for any functions encountered
1698 nl(Out) << "// Function Declarations"; nl(Out);
Rafael Espindola331380a2014-05-08 18:40:06 +00001699 for (auto *GV : gvs) {
1700 if (Function *Fun = dyn_cast<Function>(GV)) {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001701 if (!is_inline || Fun != F)
1702 printFunctionHead(Fun);
1703 }
1704 }
1705
1706 // Print the global variable declarations for any variables encountered
1707 nl(Out) << "// Global Variable Declarations"; nl(Out);
Rafael Espindola331380a2014-05-08 18:40:06 +00001708 for (auto *GV : gvs) {
1709 if (GlobalVariable *F = dyn_cast<GlobalVariable>(GV))
Chris Lattnera0b8c902010-06-21 23:12:56 +00001710 printVariableHead(F);
1711 }
1712
Nicolas Geoffray235b66c2010-11-28 18:00:53 +00001713 // Print the constants found
Chris Lattnera0b8c902010-06-21 23:12:56 +00001714 nl(Out) << "// Constant Definitions"; nl(Out);
Rafael Espindola331380a2014-05-08 18:40:06 +00001715 for (const auto *C : consts) {
1716 printConstant(C);
Chris Lattnera0b8c902010-06-21 23:12:56 +00001717 }
1718
1719 // Process the global variables definitions now that all the constants have
1720 // been emitted. These definitions just couple the gvars with their constant
1721 // initializers.
Nicolas Geoffray235b66c2010-11-28 18:00:53 +00001722 if (GenerationType != GenFunction) {
1723 nl(Out) << "// Global Variable Definitions"; nl(Out);
Benjamin Kramer90a84a32015-04-16 12:43:07 +00001724 for (auto *GV : gvs) {
Rafael Espindola331380a2014-05-08 18:40:06 +00001725 if (GlobalVariable *Var = dyn_cast<GlobalVariable>(GV))
1726 printVariableBody(Var);
Nicolas Geoffray235b66c2010-11-28 18:00:53 +00001727 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001728 }
1729}
1730
1731void CppWriter::printFunctionHead(const Function* F) {
1732 nl(Out) << "Function* " << getCppName(F);
Nicolas Geoffraya0263e72011-10-08 11:56:36 +00001733 Out << " = mod->getFunction(\"";
1734 printEscapedString(F->getName());
1735 Out << "\");";
1736 nl(Out) << "if (!" << getCppName(F) << ") {";
1737 nl(Out) << getCppName(F);
1738
Chris Lattnera0b8c902010-06-21 23:12:56 +00001739 Out<< " = Function::Create(";
1740 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1741 nl(Out) << "/*Linkage=*/";
1742 printLinkageType(F->getLinkage());
1743 Out << ",";
1744 nl(Out) << "/*Name=*/\"";
1745 printEscapedString(F->getName());
1746 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
1747 nl(Out,-1);
1748 printCppName(F);
1749 Out << "->setCallingConv(";
1750 printCallingConv(F->getCallingConv());
1751 Out << ");";
1752 nl(Out);
1753 if (F->hasSection()) {
1754 printCppName(F);
1755 Out << "->setSection(\"" << F->getSection() << "\");";
1756 nl(Out);
1757 }
1758 if (F->getAlignment()) {
1759 printCppName(F);
1760 Out << "->setAlignment(" << F->getAlignment() << ");";
1761 nl(Out);
1762 }
1763 if (F->getVisibility() != GlobalValue::DefaultVisibility) {
1764 printCppName(F);
1765 Out << "->setVisibility(";
1766 printVisibilityType(F->getVisibility());
1767 Out << ");";
1768 nl(Out);
1769 }
Nico Rieck7157bb72014-01-14 15:22:47 +00001770 if (F->getDLLStorageClass() != GlobalValue::DefaultStorageClass) {
1771 printCppName(F);
1772 Out << "->setDLLStorageClass(";
1773 printDLLStorageClassType(F->getDLLStorageClass());
1774 Out << ");";
1775 nl(Out);
1776 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001777 if (F->hasGC()) {
1778 printCppName(F);
1779 Out << "->setGC(\"" << F->getGC() << "\");";
1780 nl(Out);
1781 }
Nicolas Geoffraya0263e72011-10-08 11:56:36 +00001782 Out << "}";
1783 nl(Out);
Chris Lattnera0b8c902010-06-21 23:12:56 +00001784 printAttributes(F->getAttributes(), getCppName(F));
1785 printCppName(F);
1786 Out << "->setAttributes(" << getCppName(F) << "_PAL);";
1787 nl(Out);
1788}
1789
1790void CppWriter::printFunctionBody(const Function *F) {
1791 if (F->isDeclaration())
1792 return; // external functions have no bodies.
1793
1794 // Clear the DefinedValues and ForwardRefs maps because we can't have
1795 // cross-function forward refs
1796 ForwardRefs.clear();
1797 DefinedValues.clear();
1798
1799 // Create all the argument values
1800 if (!is_inline) {
1801 if (!F->arg_empty()) {
1802 Out << "Function::arg_iterator args = " << getCppName(F)
1803 << "->arg_begin();";
1804 nl(Out);
1805 }
1806 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1807 AI != AE; ++AI) {
1808 Out << "Value* " << getCppName(AI) << " = args++;";
1809 nl(Out);
1810 if (AI->hasName()) {
Eli Friedmand28ddbf2011-10-31 23:59:22 +00001811 Out << getCppName(AI) << "->setName(\"";
1812 printEscapedString(AI->getName());
1813 Out << "\");";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001814 nl(Out);
1815 }
1816 }
1817 }
1818
Chris Lattnera0b8c902010-06-21 23:12:56 +00001819 // Create all the basic blocks
1820 nl(Out);
1821 for (Function::const_iterator BI = F->begin(), BE = F->end();
1822 BI != BE; ++BI) {
1823 std::string bbname(getCppName(BI));
1824 Out << "BasicBlock* " << bbname <<
1825 " = BasicBlock::Create(mod->getContext(), \"";
1826 if (BI->hasName())
1827 printEscapedString(BI->getName());
1828 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1829 nl(Out);
Anton Korobeynikov78695032008-04-23 22:29:24 +00001830 }
1831
Chris Lattnera0b8c902010-06-21 23:12:56 +00001832 // Output all of its basic blocks... for the function
1833 for (Function::const_iterator BI = F->begin(), BE = F->end();
1834 BI != BE; ++BI) {
1835 std::string bbname(getCppName(BI));
1836 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001837 nl(Out);
1838
Chris Lattnera0b8c902010-06-21 23:12:56 +00001839 // Output all of the instructions in the basic block...
1840 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1841 I != E; ++I) {
1842 printInstruction(I,bbname);
1843 }
1844 }
1845
1846 // Loop over the ForwardRefs and resolve them now that all instructions
1847 // are generated.
1848 if (!ForwardRefs.empty()) {
1849 nl(Out) << "// Resolve Forward References";
1850 nl(Out);
1851 }
1852
1853 while (!ForwardRefs.empty()) {
1854 ForwardRefMap::iterator I = ForwardRefs.begin();
1855 Out << I->second << "->replaceAllUsesWith("
1856 << getCppName(I->first) << "); delete " << I->second << ";";
1857 nl(Out);
1858 ForwardRefs.erase(I);
1859 }
1860}
1861
1862void CppWriter::printInline(const std::string& fname,
1863 const std::string& func) {
1864 const Function* F = TheModule->getFunction(func);
1865 if (!F) {
1866 error(std::string("Function '") + func + "' not found in input module");
1867 return;
1868 }
1869 if (F->isDeclaration()) {
1870 error(std::string("Function '") + func + "' is external!");
1871 return;
1872 }
1873 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
1874 << getCppName(F);
1875 unsigned arg_count = 1;
1876 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1877 AI != AE; ++AI) {
Craig Topper62cb2bc2013-07-31 03:22:07 +00001878 Out << ", Value* arg_" << arg_count++;
Chris Lattnera0b8c902010-06-21 23:12:56 +00001879 }
1880 Out << ") {";
1881 nl(Out);
1882 is_inline = true;
1883 printFunctionUses(F);
1884 printFunctionBody(F);
1885 is_inline = false;
1886 Out << "return " << getCppName(F->begin()) << ";";
1887 nl(Out) << "}";
1888 nl(Out);
1889}
1890
1891void CppWriter::printModuleBody() {
1892 // Print out all the type definitions
1893 nl(Out) << "// Type Definitions"; nl(Out);
1894 printTypes(TheModule);
1895
1896 // Functions can call each other and global variables can reference them so
1897 // define all the functions first before emitting their function bodies.
1898 nl(Out) << "// Function Declarations"; nl(Out);
1899 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1900 I != E; ++I)
1901 printFunctionHead(I);
1902
1903 // Process the global variables declarations. We can't initialze them until
1904 // after the constants are printed so just print a header for each global
1905 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
1906 for (Module::const_global_iterator I = TheModule->global_begin(),
1907 E = TheModule->global_end(); I != E; ++I) {
1908 printVariableHead(I);
1909 }
1910
1911 // Print out all the constants definitions. Constants don't recurse except
1912 // through GlobalValues. All GlobalValues have been declared at this point
1913 // so we can proceed to generate the constants.
1914 nl(Out) << "// Constant Definitions"; nl(Out);
1915 printConstants(TheModule);
1916
1917 // Process the global variables definitions now that all the constants have
1918 // been emitted. These definitions just couple the gvars with their constant
1919 // initializers.
1920 nl(Out) << "// Global Variable Definitions"; nl(Out);
1921 for (Module::const_global_iterator I = TheModule->global_begin(),
1922 E = TheModule->global_end(); I != E; ++I) {
1923 printVariableBody(I);
1924 }
1925
1926 // Finally, we can safely put out all of the function bodies.
1927 nl(Out) << "// Function Definitions"; nl(Out);
1928 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1929 I != E; ++I) {
1930 if (!I->isDeclaration()) {
1931 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1932 << ")";
1933 nl(Out) << "{";
1934 nl(Out,1);
1935 printFunctionBody(I);
1936 nl(Out,-1) << "}";
Anton Korobeynikov78695032008-04-23 22:29:24 +00001937 nl(Out);
Anton Korobeynikov78695032008-04-23 22:29:24 +00001938 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00001939 }
1940}
1941
1942void CppWriter::printProgram(const std::string& fname,
1943 const std::string& mName) {
Chris Lattnera0b8c902010-06-21 23:12:56 +00001944 Out << "#include <llvm/Pass.h>\n";
Bill Wendlingcc1fc942013-01-27 01:22:51 +00001945
Chris Lattnera0b8c902010-06-21 23:12:56 +00001946 Out << "#include <llvm/ADT/SmallVector.h>\n";
1947 Out << "#include <llvm/Analysis/Verifier.h>\n";
Bill Wendlingcc1fc942013-01-27 01:22:51 +00001948 Out << "#include <llvm/IR/BasicBlock.h>\n";
1949 Out << "#include <llvm/IR/CallingConv.h>\n";
1950 Out << "#include <llvm/IR/Constants.h>\n";
1951 Out << "#include <llvm/IR/DerivedTypes.h>\n";
1952 Out << "#include <llvm/IR/Function.h>\n";
1953 Out << "#include <llvm/IR/GlobalVariable.h>\n";
Chandler Carruthb8ddc702014-01-12 11:10:32 +00001954 Out << "#include <llvm/IR/IRPrintingPasses.h>\n";
Bill Wendlingcc1fc942013-01-27 01:22:51 +00001955 Out << "#include <llvm/IR/InlineAsm.h>\n";
1956 Out << "#include <llvm/IR/Instructions.h>\n";
1957 Out << "#include <llvm/IR/LLVMContext.h>\n";
Chandler Carruth30d69c22015-02-13 10:01:29 +00001958 Out << "#include <llvm/IR/LegacyPassManager.h>\n";
Bill Wendlingcc1fc942013-01-27 01:22:51 +00001959 Out << "#include <llvm/IR/Module.h>\n";
1960 Out << "#include <llvm/Support/FormattedStream.h>\n";
1961 Out << "#include <llvm/Support/MathExtras.h>\n";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001962 Out << "#include <algorithm>\n";
1963 Out << "using namespace llvm;\n\n";
1964 Out << "Module* " << fname << "();\n\n";
1965 Out << "int main(int argc, char**argv) {\n";
1966 Out << " Module* Mod = " << fname << "();\n";
1967 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1968 Out << " PassManager PM;\n";
1969 Out << " PM.add(createPrintModulePass(&outs()));\n";
1970 Out << " PM.run(*Mod);\n";
1971 Out << " return 0;\n";
1972 Out << "}\n\n";
1973 printModule(fname,mName);
1974}
1975
1976void CppWriter::printModule(const std::string& fname,
1977 const std::string& mName) {
1978 nl(Out) << "Module* " << fname << "() {";
1979 nl(Out,1) << "// Module Construction";
1980 nl(Out) << "Module* mod = new Module(\"";
1981 printEscapedString(mName);
1982 Out << "\", getGlobalContext());";
1983 if (!TheModule->getTargetTriple().empty()) {
Mehdi Amini46a43552015-03-04 18:43:29 +00001984 nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayoutStr()
1985 << "\");";
Chris Lattnera0b8c902010-06-21 23:12:56 +00001986 }
1987 if (!TheModule->getTargetTriple().empty()) {
1988 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1989 << "\");";
1990 }
1991
1992 if (!TheModule->getModuleInlineAsm().empty()) {
1993 nl(Out) << "mod->setModuleInlineAsm(\"";
1994 printEscapedString(TheModule->getModuleInlineAsm());
1995 Out << "\");";
1996 }
1997 nl(Out);
1998
Chris Lattnera0b8c902010-06-21 23:12:56 +00001999 printModuleBody();
2000 nl(Out) << "return mod;";
2001 nl(Out,-1) << "}";
2002 nl(Out);
2003}
Anton Korobeynikov78695032008-04-23 22:29:24 +00002004
Chris Lattnera0b8c902010-06-21 23:12:56 +00002005void CppWriter::printContents(const std::string& fname,
2006 const std::string& mName) {
2007 Out << "\nModule* " << fname << "(Module *mod) {\n";
2008 Out << "\nmod->setModuleIdentifier(\"";
2009 printEscapedString(mName);
2010 Out << "\");\n";
2011 printModuleBody();
2012 Out << "\nreturn mod;\n";
2013 Out << "\n}\n";
2014}
2015
2016void CppWriter::printFunction(const std::string& fname,
2017 const std::string& funcName) {
2018 const Function* F = TheModule->getFunction(funcName);
2019 if (!F) {
2020 error(std::string("Function '") + funcName + "' not found in input module");
2021 return;
Anton Korobeynikov78695032008-04-23 22:29:24 +00002022 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00002023 Out << "\nFunction* " << fname << "(Module *mod) {\n";
2024 printFunctionUses(F);
2025 printFunctionHead(F);
2026 printFunctionBody(F);
2027 Out << "return " << getCppName(F) << ";\n";
2028 Out << "}\n";
2029}
Anton Korobeynikov78695032008-04-23 22:29:24 +00002030
Chris Lattnera0b8c902010-06-21 23:12:56 +00002031void CppWriter::printFunctions() {
2032 const Module::FunctionListType &funcs = TheModule->getFunctionList();
2033 Module::const_iterator I = funcs.begin();
2034 Module::const_iterator IE = funcs.end();
Anton Korobeynikov78695032008-04-23 22:29:24 +00002035
Chris Lattnera0b8c902010-06-21 23:12:56 +00002036 for (; I != IE; ++I) {
2037 const Function &func = *I;
2038 if (!func.isDeclaration()) {
2039 std::string name("define_");
2040 name += func.getName();
2041 printFunction(name, func.getName());
Anton Korobeynikov78695032008-04-23 22:29:24 +00002042 }
2043 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00002044}
Anton Korobeynikov78695032008-04-23 22:29:24 +00002045
Chris Lattnera0b8c902010-06-21 23:12:56 +00002046void CppWriter::printVariable(const std::string& fname,
2047 const std::string& varName) {
2048 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
Anton Korobeynikov78695032008-04-23 22:29:24 +00002049
Chris Lattnera0b8c902010-06-21 23:12:56 +00002050 if (!GV) {
2051 error(std::string("Variable '") + varName + "' not found in input module");
2052 return;
2053 }
2054 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
2055 printVariableUses(GV);
2056 printVariableHead(GV);
2057 printVariableBody(GV);
2058 Out << "return " << getCppName(GV) << ";\n";
2059 Out << "}\n";
2060}
2061
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002062void CppWriter::printType(const std::string &fname,
2063 const std::string &typeName) {
Chris Lattner229907c2011-07-18 04:54:35 +00002064 Type* Ty = TheModule->getTypeByName(typeName);
Chris Lattnera0b8c902010-06-21 23:12:56 +00002065 if (!Ty) {
2066 error(std::string("Type '") + typeName + "' not found in input module");
2067 return;
2068 }
2069 Out << "\nType* " << fname << "(Module *mod) {\n";
2070 printType(Ty);
2071 Out << "return " << getCppName(Ty) << ";\n";
2072 Out << "}\n";
2073}
2074
2075bool CppWriter::runOnModule(Module &M) {
2076 TheModule = &M;
2077
2078 // Emit a header
2079 Out << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
2080
2081 // Get the name of the function we're supposed to generate
2082 std::string fname = FuncName.getValue();
2083
2084 // Get the name of the thing we are to generate
2085 std::string tgtname = NameToGenerate.getValue();
2086 if (GenerationType == GenModule ||
2087 GenerationType == GenContents ||
2088 GenerationType == GenProgram ||
2089 GenerationType == GenFunctions) {
2090 if (tgtname == "!bad!") {
2091 if (M.getModuleIdentifier() == "-")
2092 tgtname = "<stdin>";
2093 else
2094 tgtname = M.getModuleIdentifier();
Anton Korobeynikov78695032008-04-23 22:29:24 +00002095 }
Chris Lattnera0b8c902010-06-21 23:12:56 +00002096 } else if (tgtname == "!bad!")
2097 error("You must use the -for option with -gen-{function,variable,type}");
2098
2099 switch (WhatToGenerate(GenerationType)) {
2100 case GenProgram:
2101 if (fname.empty())
2102 fname = "makeLLVMModule";
2103 printProgram(fname,tgtname);
2104 break;
2105 case GenModule:
2106 if (fname.empty())
2107 fname = "makeLLVMModule";
2108 printModule(fname,tgtname);
2109 break;
2110 case GenContents:
2111 if (fname.empty())
2112 fname = "makeLLVMModuleContents";
2113 printContents(fname,tgtname);
2114 break;
2115 case GenFunction:
2116 if (fname.empty())
2117 fname = "makeLLVMFunction";
2118 printFunction(fname,tgtname);
2119 break;
2120 case GenFunctions:
2121 printFunctions();
2122 break;
2123 case GenInline:
2124 if (fname.empty())
2125 fname = "makeLLVMInline";
2126 printInline(fname,tgtname);
2127 break;
2128 case GenVariable:
2129 if (fname.empty())
2130 fname = "makeLLVMVariable";
2131 printVariable(fname,tgtname);
2132 break;
2133 case GenType:
2134 if (fname.empty())
2135 fname = "makeLLVMType";
2136 printType(fname,tgtname);
2137 break;
Anton Korobeynikov78695032008-04-23 22:29:24 +00002138 }
2139
Chris Lattnera0b8c902010-06-21 23:12:56 +00002140 return false;
Anton Korobeynikov78695032008-04-23 22:29:24 +00002141}
2142
2143char CppWriter::ID = 0;
2144
2145//===----------------------------------------------------------------------===//
2146// External Interface declaration
2147//===----------------------------------------------------------------------===//
2148
Rafael Espindola5560a4c2015-04-14 22:14:34 +00002149bool CPPTargetMachine::addPassesToEmitFile(
2150 PassManagerBase &PM, raw_pwrite_stream &o, CodeGenFileType FileType,
Alex Lorenz735c47e2015-06-15 20:30:22 +00002151 bool DisableVerify, AnalysisID StartAfter, AnalysisID StopAfter,
2152 MachineFunctionInitializer *MFInitializer) {
Rafael Espindola5682ce22015-04-09 21:06:08 +00002153 if (FileType != TargetMachine::CGFT_AssemblyFile)
2154 return true;
2155 auto FOut = llvm::make_unique<formatted_raw_ostream>(o);
2156 PM.add(new CppWriter(std::move(FOut)));
Anton Korobeynikov78695032008-04-23 22:29:24 +00002157 return false;
2158}