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