blob: 79f1366d7ab1af9a7e7d6fdc9ae345f28b2a74ad [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()) {
519 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
520 nl(Out);
521 UnresolvedTypes[Ty] = typeName;
522 }
523 return true;
524 }
525
526 // We're going to print a derived type which, by definition, contains other
527 // types. So, push this one we're printing onto the type stack to assist with
528 // recursive definitions.
529 TypeStack.push_back(Ty);
530
531 // Print the type definition
532 switch (Ty->getTypeID()) {
533 case Type::FunctionTyID: {
534 const FunctionType* FT = cast<FunctionType>(Ty);
535 Out << "std::vector<const Type*>" << typeName << "_args;";
536 nl(Out);
537 FunctionType::param_iterator PI = FT->param_begin();
538 FunctionType::param_iterator PE = FT->param_end();
539 for (; PI != PE; ++PI) {
540 const Type* argTy = static_cast<const Type*>(*PI);
541 bool isForward = printTypeInternal(argTy);
542 std::string argName(getCppName(argTy));
543 Out << typeName << "_args.push_back(" << argName;
544 if (isForward)
545 Out << "_fwd";
546 Out << ");";
547 nl(Out);
548 }
549 bool isForward = printTypeInternal(FT->getReturnType());
550 std::string retTypeName(getCppName(FT->getReturnType()));
551 Out << "FunctionType* " << typeName << " = FunctionType::get(";
552 in(); nl(Out) << "/*Result=*/" << retTypeName;
553 if (isForward)
554 Out << "_fwd";
555 Out << ",";
556 nl(Out) << "/*Params=*/" << typeName << "_args,";
557 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
558 out();
559 nl(Out);
560 break;
561 }
562 case Type::StructTyID: {
563 const StructType* ST = cast<StructType>(Ty);
564 Out << "std::vector<const Type*>" << typeName << "_fields;";
565 nl(Out);
566 StructType::element_iterator EI = ST->element_begin();
567 StructType::element_iterator EE = ST->element_end();
568 for (; EI != EE; ++EI) {
569 const Type* fieldTy = static_cast<const Type*>(*EI);
570 bool isForward = printTypeInternal(fieldTy);
571 std::string fieldName(getCppName(fieldTy));
572 Out << typeName << "_fields.push_back(" << fieldName;
573 if (isForward)
574 Out << "_fwd";
575 Out << ");";
576 nl(Out);
577 }
578 Out << "StructType* " << typeName << " = StructType::get("
Nicolas Geoffray6f62cff2009-08-06 21:31:35 +0000579 << "mod->getContext(), "
Anton Korobeynikov50276522008-04-23 22:29:24 +0000580 << typeName << "_fields, /*isPacked=*/"
581 << (ST->isPacked() ? "true" : "false") << ");";
582 nl(Out);
583 break;
584 }
585 case Type::ArrayTyID: {
586 const ArrayType* AT = cast<ArrayType>(Ty);
587 const Type* ET = AT->getElementType();
588 bool isForward = printTypeInternal(ET);
589 std::string elemName(getCppName(ET));
590 Out << "ArrayType* " << typeName << " = ArrayType::get("
591 << elemName << (isForward ? "_fwd" : "")
592 << ", " << utostr(AT->getNumElements()) << ");";
593 nl(Out);
594 break;
595 }
596 case Type::PointerTyID: {
597 const PointerType* PT = cast<PointerType>(Ty);
598 const Type* ET = PT->getElementType();
599 bool isForward = printTypeInternal(ET);
600 std::string elemName(getCppName(ET));
601 Out << "PointerType* " << typeName << " = PointerType::get("
602 << elemName << (isForward ? "_fwd" : "")
603 << ", " << utostr(PT->getAddressSpace()) << ");";
604 nl(Out);
605 break;
606 }
607 case Type::VectorTyID: {
608 const VectorType* PT = cast<VectorType>(Ty);
609 const Type* ET = PT->getElementType();
610 bool isForward = printTypeInternal(ET);
611 std::string elemName(getCppName(ET));
612 Out << "VectorType* " << typeName << " = VectorType::get("
613 << elemName << (isForward ? "_fwd" : "")
614 << ", " << utostr(PT->getNumElements()) << ");";
615 nl(Out);
616 break;
617 }
618 case Type::OpaqueTyID: {
619 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
620 nl(Out);
621 break;
622 }
623 default:
624 error("Invalid TypeID");
625 }
626
627 // If the type had a name, make sure we recreate it.
628 const std::string* progTypeName =
629 findTypeName(TheModule->getTypeSymbolTable(),Ty);
630 if (progTypeName) {
631 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
632 << typeName << ");";
633 nl(Out);
634 }
635
636 // Pop us off the type stack
637 TypeStack.pop_back();
638
639 // Indicate that this type is now defined.
640 DefinedTypes.insert(Ty);
641
642 // Early resolve as many unresolved types as possible. Search the unresolved
643 // types map for the type we just printed. Now that its definition is complete
644 // we can resolve any previous references to it. This prevents a cascade of
645 // unresolved types.
646 TypeMap::iterator I = UnresolvedTypes.find(Ty);
647 if (I != UnresolvedTypes.end()) {
648 Out << "cast<OpaqueType>(" << I->second
649 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
650 nl(Out);
651 Out << I->second << " = cast<";
652 switch (Ty->getTypeID()) {
653 case Type::FunctionTyID: Out << "FunctionType"; break;
654 case Type::ArrayTyID: Out << "ArrayType"; break;
655 case Type::StructTyID: Out << "StructType"; break;
656 case Type::VectorTyID: Out << "VectorType"; break;
657 case Type::PointerTyID: Out << "PointerType"; break;
658 case Type::OpaqueTyID: Out << "OpaqueType"; break;
659 default: Out << "NoSuchDerivedType"; break;
660 }
661 Out << ">(" << I->second << "_fwd.get());";
662 nl(Out); nl(Out);
663 UnresolvedTypes.erase(I);
664 }
665
666 // Finally, separate the type definition from other with a newline.
667 nl(Out);
668
669 // We weren't a recursive type
670 return false;
671 }
672
673 // Prints a type definition. Returns true if it could not resolve all the
674 // types in the definition but had to use a forward reference.
675 void CppWriter::printType(const Type* Ty) {
676 assert(TypeStack.empty());
677 TypeStack.clear();
678 printTypeInternal(Ty);
679 assert(TypeStack.empty());
680 }
681
682 void CppWriter::printTypes(const Module* M) {
683 // Walk the symbol table and print out all its types
684 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
685 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
686 TI != TE; ++TI) {
687
688 // For primitive types and types already defined, just add a name
689 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
690 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
691 TNI != TypeNames.end()) {
692 Out << "mod->addTypeName(\"";
693 printEscapedString(TI->first);
694 Out << "\", " << getCppName(TI->second) << ");";
695 nl(Out);
696 // For everything else, define the type
697 } else {
698 printType(TI->second);
699 }
700 }
701
702 // Add all of the global variables to the value table...
703 for (Module::const_global_iterator I = TheModule->global_begin(),
704 E = TheModule->global_end(); I != E; ++I) {
705 if (I->hasInitializer())
706 printType(I->getInitializer()->getType());
707 printType(I->getType());
708 }
709
710 // Add all the functions to the table
711 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
712 FI != FE; ++FI) {
713 printType(FI->getReturnType());
714 printType(FI->getFunctionType());
715 // Add all the function arguments
716 for (Function::const_arg_iterator AI = FI->arg_begin(),
717 AE = FI->arg_end(); AI != AE; ++AI) {
718 printType(AI->getType());
719 }
720
721 // Add all of the basic blocks and instructions
722 for (Function::const_iterator BB = FI->begin(),
723 E = FI->end(); BB != E; ++BB) {
724 printType(BB->getType());
725 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
726 ++I) {
727 printType(I->getType());
728 for (unsigned i = 0; i < I->getNumOperands(); ++i)
729 printType(I->getOperand(i)->getType());
730 }
731 }
732 }
733 }
734
735
736 // printConstant - Print out a constant pool entry...
737 void CppWriter::printConstant(const Constant *CV) {
738 // First, if the constant is actually a GlobalValue (variable or function)
739 // or its already in the constant list then we've printed it already and we
740 // can just return.
741 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
742 return;
743
744 std::string constName(getCppName(CV));
745 std::string typeName(getCppName(CV->getType()));
Anton Korobeynikovff4ca2e2008-10-05 15:07:06 +0000746
Anton Korobeynikov50276522008-04-23 22:29:24 +0000747 if (isa<GlobalValue>(CV)) {
748 // Skip variables and functions, we emit them elsewhere
749 return;
750 }
Anton Korobeynikovff4ca2e2008-10-05 15:07:06 +0000751
Anton Korobeynikov50276522008-04-23 22:29:24 +0000752 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Anton Korobeynikov70053c32008-08-18 20:03:45 +0000753 std::string constValue = CI->getValue().toString(10, true);
Owen Anderson267a0ff2009-08-14 17:41:33 +0000754 Out << "ConstantInt* " << constName
755 << " = ConstantInt::get(getGlobalContext(), APInt("
756 << cast<IntegerType>(CI->getType())->getBitWidth()
757 << ", StringRef(\"" << constValue << "\"), 10));";
Anton Korobeynikov50276522008-04-23 22:29:24 +0000758 } else if (isa<ConstantAggregateZero>(CV)) {
759 Out << "ConstantAggregateZero* " << constName
760 << " = ConstantAggregateZero::get(" << typeName << ");";
761 } else if (isa<ConstantPointerNull>(CV)) {
762 Out << "ConstantPointerNull* " << constName
Anton Korobeynikovff4ca2e2008-10-05 15:07:06 +0000763 << " = ConstantPointerNull::get(" << typeName << ");";
Anton Korobeynikov50276522008-04-23 22:29:24 +0000764 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
765 Out << "ConstantFP* " << constName << " = ";
766 printCFP(CFP);
767 Out << ";";
768 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000769 if (CA->isString() &&
770 CA->getType()->getElementType() ==
771 Type::getInt8Ty(CA->getContext())) {
Owen Anderson267a0ff2009-08-14 17:41:33 +0000772 Out << "Constant* " << constName <<
773 " = ConstantArray::get(getGlobalContext(), \"";
Anton Korobeynikov50276522008-04-23 22:29:24 +0000774 std::string tmp = CA->getAsString();
775 bool nullTerminate = false;
776 if (tmp[tmp.length()-1] == 0) {
777 tmp.erase(tmp.length()-1);
778 nullTerminate = true;
779 }
780 printEscapedString(tmp);
781 // Determine if we want null termination or not.
782 if (nullTerminate)
783 Out << "\", true"; // Indicate that the null terminator should be
784 // added.
785 else
786 Out << "\", false";// No null terminator
787 Out << ");";
788 } else {
789 Out << "std::vector<Constant*> " << constName << "_elems;";
790 nl(Out);
791 unsigned N = CA->getNumOperands();
792 for (unsigned i = 0; i < N; ++i) {
793 printConstant(CA->getOperand(i)); // recurse to print operands
794 Out << constName << "_elems.push_back("
795 << getCppName(CA->getOperand(i)) << ");";
796 nl(Out);
797 }
798 Out << "Constant* " << constName << " = ConstantArray::get("
799 << typeName << ", " << constName << "_elems);";
800 }
801 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
802 Out << "std::vector<Constant*> " << constName << "_fields;";
803 nl(Out);
804 unsigned N = CS->getNumOperands();
805 for (unsigned i = 0; i < N; i++) {
806 printConstant(CS->getOperand(i));
807 Out << constName << "_fields.push_back("
808 << getCppName(CS->getOperand(i)) << ");";
809 nl(Out);
810 }
811 Out << "Constant* " << constName << " = ConstantStruct::get("
812 << typeName << ", " << constName << "_fields);";
813 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
814 Out << "std::vector<Constant*> " << constName << "_elems;";
815 nl(Out);
816 unsigned N = CP->getNumOperands();
817 for (unsigned i = 0; i < N; ++i) {
818 printConstant(CP->getOperand(i));
819 Out << constName << "_elems.push_back("
820 << getCppName(CP->getOperand(i)) << ");";
821 nl(Out);
822 }
823 Out << "Constant* " << constName << " = ConstantVector::get("
824 << typeName << ", " << constName << "_elems);";
825 } else if (isa<UndefValue>(CV)) {
826 Out << "UndefValue* " << constName << " = UndefValue::get("
827 << typeName << ");";
828 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
829 if (CE->getOpcode() == Instruction::GetElementPtr) {
830 Out << "std::vector<Constant*> " << constName << "_indices;";
831 nl(Out);
832 printConstant(CE->getOperand(0));
833 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
834 printConstant(CE->getOperand(i));
835 Out << constName << "_indices.push_back("
836 << getCppName(CE->getOperand(i)) << ");";
837 nl(Out);
838 }
839 Out << "Constant* " << constName
840 << " = ConstantExpr::getGetElementPtr("
841 << getCppName(CE->getOperand(0)) << ", "
842 << "&" << constName << "_indices[0], "
843 << constName << "_indices.size()"
844 << " );";
845 } else if (CE->isCast()) {
846 printConstant(CE->getOperand(0));
847 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
848 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000849 default: llvm_unreachable("Invalid cast opcode");
Anton Korobeynikov50276522008-04-23 22:29:24 +0000850 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
851 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
852 case Instruction::SExt: Out << "Instruction::SExt"; break;
853 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
854 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
855 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
856 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
857 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
858 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
859 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
860 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
861 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
862 }
863 Out << ", " << getCppName(CE->getOperand(0)) << ", "
864 << getCppName(CE->getType()) << ");";
865 } else {
866 unsigned N = CE->getNumOperands();
867 for (unsigned i = 0; i < N; ++i ) {
868 printConstant(CE->getOperand(i));
869 }
870 Out << "Constant* " << constName << " = ConstantExpr::";
871 switch (CE->getOpcode()) {
872 case Instruction::Add: Out << "getAdd("; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000873 case Instruction::FAdd: Out << "getFAdd("; break;
Anton Korobeynikov50276522008-04-23 22:29:24 +0000874 case Instruction::Sub: Out << "getSub("; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000875 case Instruction::FSub: Out << "getFSub("; break;
Anton Korobeynikov50276522008-04-23 22:29:24 +0000876 case Instruction::Mul: Out << "getMul("; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000877 case Instruction::FMul: Out << "getFMul("; break;
Anton Korobeynikov50276522008-04-23 22:29:24 +0000878 case Instruction::UDiv: Out << "getUDiv("; break;
879 case Instruction::SDiv: Out << "getSDiv("; break;
880 case Instruction::FDiv: Out << "getFDiv("; break;
881 case Instruction::URem: Out << "getURem("; break;
882 case Instruction::SRem: Out << "getSRem("; break;
883 case Instruction::FRem: Out << "getFRem("; break;
884 case Instruction::And: Out << "getAnd("; break;
885 case Instruction::Or: Out << "getOr("; break;
886 case Instruction::Xor: Out << "getXor("; break;
887 case Instruction::ICmp:
888 Out << "getICmp(ICmpInst::ICMP_";
889 switch (CE->getPredicate()) {
890 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
891 case ICmpInst::ICMP_NE: Out << "NE"; break;
892 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
893 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
894 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
895 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
896 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
897 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
898 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
899 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
900 default: error("Invalid ICmp Predicate");
901 }
902 break;
903 case Instruction::FCmp:
904 Out << "getFCmp(FCmpInst::FCMP_";
905 switch (CE->getPredicate()) {
906 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
907 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
908 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
909 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
910 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
911 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
912 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
913 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
914 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
915 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
916 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
917 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
918 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
919 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
920 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
921 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
922 default: error("Invalid FCmp Predicate");
923 }
924 break;
925 case Instruction::Shl: Out << "getShl("; break;
926 case Instruction::LShr: Out << "getLShr("; break;
927 case Instruction::AShr: Out << "getAShr("; break;
928 case Instruction::Select: Out << "getSelect("; break;
929 case Instruction::ExtractElement: Out << "getExtractElement("; break;
930 case Instruction::InsertElement: Out << "getInsertElement("; break;
931 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
932 default:
933 error("Invalid constant expression");
934 break;
935 }
936 Out << getCppName(CE->getOperand(0));
937 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
938 Out << ", " << getCppName(CE->getOperand(i));
939 Out << ");";
940 }
941 } else {
942 error("Bad Constant");
943 Out << "Constant* " << constName << " = 0; ";
944 }
945 nl(Out);
946 }
947
948 void CppWriter::printConstants(const Module* M) {
949 // Traverse all the global variables looking for constant initializers
950 for (Module::const_global_iterator I = TheModule->global_begin(),
951 E = TheModule->global_end(); I != E; ++I)
952 if (I->hasInitializer())
953 printConstant(I->getInitializer());
954
955 // Traverse the LLVM functions looking for constants
956 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
957 FI != FE; ++FI) {
958 // Add all of the basic blocks and instructions
959 for (Function::const_iterator BB = FI->begin(),
960 E = FI->end(); BB != E; ++BB) {
961 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
962 ++I) {
963 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
964 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
965 printConstant(C);
966 }
967 }
968 }
969 }
970 }
971 }
972
973 void CppWriter::printVariableUses(const GlobalVariable *GV) {
974 nl(Out) << "// Type Definitions";
975 nl(Out);
976 printType(GV->getType());
977 if (GV->hasInitializer()) {
978 Constant* Init = GV->getInitializer();
979 printType(Init->getType());
980 if (Function* F = dyn_cast<Function>(Init)) {
981 nl(Out)<< "/ Function Declarations"; nl(Out);
982 printFunctionHead(F);
983 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
984 nl(Out) << "// Global Variable Declarations"; nl(Out);
985 printVariableHead(gv);
986 } else {
987 nl(Out) << "// Constant Definitions"; nl(Out);
988 printConstant(gv);
989 }
990 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
991 nl(Out) << "// Global Variable Definitions"; nl(Out);
992 printVariableBody(gv);
993 }
994 }
995 }
996
997 void CppWriter::printVariableHead(const GlobalVariable *GV) {
998 nl(Out) << "GlobalVariable* " << getCppName(GV);
999 if (is_inline) {
Owen Anderson267a0ff2009-08-14 17:41:33 +00001000 Out << " = mod->getGlobalVariable(getGlobalContext(), ";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001001 printEscapedString(GV->getName());
1002 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
1003 nl(Out) << "if (!" << getCppName(GV) << ") {";
1004 in(); nl(Out) << getCppName(GV);
1005 }
Owen Anderson267a0ff2009-08-14 17:41:33 +00001006 Out << " = new GlobalVariable(/*Module=*/*mod, ";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001007 nl(Out) << "/*Type=*/";
1008 printCppName(GV->getType()->getElementType());
1009 Out << ",";
1010 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
1011 Out << ",";
1012 nl(Out) << "/*Linkage=*/";
1013 printLinkageType(GV->getLinkage());
1014 Out << ",";
1015 nl(Out) << "/*Initializer=*/0, ";
1016 if (GV->hasInitializer()) {
1017 Out << "// has initializer, specified below";
1018 }
1019 nl(Out) << "/*Name=*/\"";
1020 printEscapedString(GV->getName());
Owen Anderson16a412e2009-07-10 16:42:19 +00001021 Out << "\");";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001022 nl(Out);
1023
1024 if (GV->hasSection()) {
1025 printCppName(GV);
1026 Out << "->setSection(\"";
1027 printEscapedString(GV->getSection());
1028 Out << "\");";
1029 nl(Out);
1030 }
1031 if (GV->getAlignment()) {
1032 printCppName(GV);
1033 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
1034 nl(Out);
1035 }
1036 if (GV->getVisibility() != GlobalValue::DefaultVisibility) {
1037 printCppName(GV);
1038 Out << "->setVisibility(";
1039 printVisibilityType(GV->getVisibility());
1040 Out << ");";
1041 nl(Out);
1042 }
1043 if (is_inline) {
1044 out(); Out << "}"; nl(Out);
1045 }
1046 }
1047
1048 void CppWriter::printVariableBody(const GlobalVariable *GV) {
1049 if (GV->hasInitializer()) {
1050 printCppName(GV);
1051 Out << "->setInitializer(";
1052 Out << getCppName(GV->getInitializer()) << ");";
1053 nl(Out);
1054 }
1055 }
1056
1057 std::string CppWriter::getOpName(Value* V) {
1058 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
1059 return getCppName(V);
1060
1061 // See if its alread in the map of forward references, if so just return the
1062 // name we already set up for it
1063 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
1064 if (I != ForwardRefs.end())
1065 return I->second;
1066
1067 // This is a new forward reference. Generate a unique name for it
1068 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
1069
1070 // Yes, this is a hack. An Argument is the smallest instantiable value that
1071 // we can make as a placeholder for the real value. We'll replace these
1072 // Argument instances later.
1073 Out << "Argument* " << result << " = new Argument("
1074 << getCppName(V->getType()) << ");";
1075 nl(Out);
1076 ForwardRefs[V] = result;
1077 return result;
1078 }
1079
1080 // printInstruction - This member is called for each Instruction in a function.
1081 void CppWriter::printInstruction(const Instruction *I,
1082 const std::string& bbname) {
1083 std::string iName(getCppName(I));
1084
1085 // Before we emit this instruction, we need to take care of generating any
1086 // forward references. So, we get the names of all the operands in advance
1087 std::string* opNames = new std::string[I->getNumOperands()];
1088 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1089 opNames[i] = getOpName(I->getOperand(i));
1090 }
1091
1092 switch (I->getOpcode()) {
Dan Gohman26825a82008-06-09 14:09:13 +00001093 default:
1094 error("Invalid instruction");
1095 break;
1096
Anton Korobeynikov50276522008-04-23 22:29:24 +00001097 case Instruction::Ret: {
1098 const ReturnInst* ret = cast<ReturnInst>(I);
Owen Anderson267a0ff2009-08-14 17:41:33 +00001099 Out << "ReturnInst::Create(getGlobalContext(), "
Anton Korobeynikov50276522008-04-23 22:29:24 +00001100 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
1101 break;
1102 }
1103 case Instruction::Br: {
1104 const BranchInst* br = cast<BranchInst>(I);
1105 Out << "BranchInst::Create(" ;
1106 if (br->getNumOperands() == 3 ) {
Anton Korobeynikovcffb5282009-05-04 19:10:38 +00001107 Out << opNames[2] << ", "
Anton Korobeynikov50276522008-04-23 22:29:24 +00001108 << opNames[1] << ", "
Anton Korobeynikovcffb5282009-05-04 19:10:38 +00001109 << opNames[0] << ", ";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001110
1111 } else if (br->getNumOperands() == 1) {
1112 Out << opNames[0] << ", ";
1113 } else {
1114 error("Branch with 2 operands?");
1115 }
1116 Out << bbname << ");";
1117 break;
1118 }
1119 case Instruction::Switch: {
1120 const SwitchInst* sw = cast<SwitchInst>(I);
1121 Out << "SwitchInst* " << iName << " = SwitchInst::Create("
1122 << opNames[0] << ", "
1123 << opNames[1] << ", "
1124 << sw->getNumCases() << ", " << bbname << ");";
1125 nl(Out);
1126 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
1127 Out << iName << "->addCase("
1128 << opNames[i] << ", "
1129 << opNames[i+1] << ");";
1130 nl(Out);
1131 }
1132 break;
1133 }
1134 case Instruction::Invoke: {
1135 const InvokeInst* inv = cast<InvokeInst>(I);
1136 Out << "std::vector<Value*> " << iName << "_params;";
1137 nl(Out);
1138 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1139 Out << iName << "_params.push_back("
1140 << opNames[i] << ");";
1141 nl(Out);
1142 }
1143 Out << "InvokeInst *" << iName << " = InvokeInst::Create("
1144 << opNames[0] << ", "
1145 << opNames[1] << ", "
1146 << opNames[2] << ", "
1147 << iName << "_params.begin(), " << iName << "_params.end(), \"";
1148 printEscapedString(inv->getName());
1149 Out << "\", " << bbname << ");";
1150 nl(Out) << iName << "->setCallingConv(";
1151 printCallingConv(inv->getCallingConv());
1152 Out << ");";
Devang Patel05988662008-09-25 21:00:45 +00001153 printAttributes(inv->getAttributes(), iName);
1154 Out << iName << "->setAttributes(" << iName << "_PAL);";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001155 nl(Out);
1156 break;
1157 }
1158 case Instruction::Unwind: {
1159 Out << "new UnwindInst("
1160 << bbname << ");";
1161 break;
1162 }
1163 case Instruction::Unreachable:{
1164 Out << "new UnreachableInst("
1165 << bbname << ");";
1166 break;
1167 }
1168 case Instruction::Add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001169 case Instruction::FAdd:
Anton Korobeynikov50276522008-04-23 22:29:24 +00001170 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001171 case Instruction::FSub:
Anton Korobeynikov50276522008-04-23 22:29:24 +00001172 case Instruction::Mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001173 case Instruction::FMul:
Anton Korobeynikov50276522008-04-23 22:29:24 +00001174 case Instruction::UDiv:
1175 case Instruction::SDiv:
1176 case Instruction::FDiv:
1177 case Instruction::URem:
1178 case Instruction::SRem:
1179 case Instruction::FRem:
1180 case Instruction::And:
1181 case Instruction::Or:
1182 case Instruction::Xor:
1183 case Instruction::Shl:
1184 case Instruction::LShr:
1185 case Instruction::AShr:{
Gabor Greif7cbd8a32008-05-16 19:29:10 +00001186 Out << "BinaryOperator* " << iName << " = BinaryOperator::Create(";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001187 switch (I->getOpcode()) {
1188 case Instruction::Add: Out << "Instruction::Add"; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001189 case Instruction::FAdd: Out << "Instruction::FAdd"; break;
Anton Korobeynikov50276522008-04-23 22:29:24 +00001190 case Instruction::Sub: Out << "Instruction::Sub"; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001191 case Instruction::FSub: Out << "Instruction::FSub"; break;
Anton Korobeynikov50276522008-04-23 22:29:24 +00001192 case Instruction::Mul: Out << "Instruction::Mul"; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +00001193 case Instruction::FMul: Out << "Instruction::FMul"; break;
Anton Korobeynikov50276522008-04-23 22:29:24 +00001194 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1195 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1196 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
1197 case Instruction::URem:Out << "Instruction::URem"; break;
1198 case Instruction::SRem:Out << "Instruction::SRem"; break;
1199 case Instruction::FRem:Out << "Instruction::FRem"; break;
1200 case Instruction::And: Out << "Instruction::And"; break;
1201 case Instruction::Or: Out << "Instruction::Or"; break;
1202 case Instruction::Xor: Out << "Instruction::Xor"; break;
1203 case Instruction::Shl: Out << "Instruction::Shl"; break;
1204 case Instruction::LShr:Out << "Instruction::LShr"; break;
1205 case Instruction::AShr:Out << "Instruction::AShr"; break;
1206 default: Out << "Instruction::BadOpCode"; break;
1207 }
1208 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1209 printEscapedString(I->getName());
1210 Out << "\", " << bbname << ");";
1211 break;
1212 }
1213 case Instruction::FCmp: {
1214 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1215 switch (cast<FCmpInst>(I)->getPredicate()) {
1216 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1217 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1218 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1219 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1220 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1221 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1222 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1223 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1224 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1225 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1226 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1227 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1228 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1229 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1230 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1231 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1232 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1233 }
1234 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1235 printEscapedString(I->getName());
1236 Out << "\", " << bbname << ");";
1237 break;
1238 }
1239 case Instruction::ICmp: {
1240 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1241 switch (cast<ICmpInst>(I)->getPredicate()) {
1242 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1243 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1244 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1245 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1246 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1247 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1248 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1249 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1250 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1251 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1252 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
1253 }
1254 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1255 printEscapedString(I->getName());
1256 Out << "\", " << bbname << ");";
1257 break;
1258 }
1259 case Instruction::Malloc: {
1260 const MallocInst* mallocI = cast<MallocInst>(I);
1261 Out << "MallocInst* " << iName << " = new MallocInst("
1262 << getCppName(mallocI->getAllocatedType()) << ", ";
1263 if (mallocI->isArrayAllocation())
1264 Out << opNames[0] << ", " ;
1265 Out << "\"";
1266 printEscapedString(mallocI->getName());
1267 Out << "\", " << bbname << ");";
1268 if (mallocI->getAlignment())
1269 nl(Out) << iName << "->setAlignment("
1270 << mallocI->getAlignment() << ");";
1271 break;
1272 }
1273 case Instruction::Free: {
1274 Out << "FreeInst* " << iName << " = new FreeInst("
1275 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1276 break;
1277 }
1278 case Instruction::Alloca: {
1279 const AllocaInst* allocaI = cast<AllocaInst>(I);
1280 Out << "AllocaInst* " << iName << " = new AllocaInst("
1281 << getCppName(allocaI->getAllocatedType()) << ", ";
1282 if (allocaI->isArrayAllocation())
1283 Out << opNames[0] << ", ";
1284 Out << "\"";
1285 printEscapedString(allocaI->getName());
1286 Out << "\", " << bbname << ");";
1287 if (allocaI->getAlignment())
1288 nl(Out) << iName << "->setAlignment("
1289 << allocaI->getAlignment() << ");";
1290 break;
1291 }
1292 case Instruction::Load:{
1293 const LoadInst* load = cast<LoadInst>(I);
1294 Out << "LoadInst* " << iName << " = new LoadInst("
1295 << opNames[0] << ", \"";
1296 printEscapedString(load->getName());
1297 Out << "\", " << (load->isVolatile() ? "true" : "false" )
1298 << ", " << bbname << ");";
1299 break;
1300 }
1301 case Instruction::Store: {
1302 const StoreInst* store = cast<StoreInst>(I);
Anton Korobeynikovb0714db2008-11-09 02:54:13 +00001303 Out << " new StoreInst("
Anton Korobeynikov50276522008-04-23 22:29:24 +00001304 << opNames[0] << ", "
1305 << opNames[1] << ", "
1306 << (store->isVolatile() ? "true" : "false")
1307 << ", " << bbname << ");";
1308 break;
1309 }
1310 case Instruction::GetElementPtr: {
1311 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1312 if (gep->getNumOperands() <= 2) {
1313 Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
1314 << opNames[0];
1315 if (gep->getNumOperands() == 2)
1316 Out << ", " << opNames[1];
1317 } else {
1318 Out << "std::vector<Value*> " << iName << "_indices;";
1319 nl(Out);
1320 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
1321 Out << iName << "_indices.push_back("
1322 << opNames[i] << ");";
1323 nl(Out);
1324 }
1325 Out << "Instruction* " << iName << " = GetElementPtrInst::Create("
1326 << opNames[0] << ", " << iName << "_indices.begin(), "
1327 << iName << "_indices.end()";
1328 }
1329 Out << ", \"";
1330 printEscapedString(gep->getName());
1331 Out << "\", " << bbname << ");";
1332 break;
1333 }
1334 case Instruction::PHI: {
1335 const PHINode* phi = cast<PHINode>(I);
1336
1337 Out << "PHINode* " << iName << " = PHINode::Create("
1338 << getCppName(phi->getType()) << ", \"";
1339 printEscapedString(phi->getName());
1340 Out << "\", " << bbname << ");";
1341 nl(Out) << iName << "->reserveOperandSpace("
1342 << phi->getNumIncomingValues()
1343 << ");";
1344 nl(Out);
1345 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
1346 Out << iName << "->addIncoming("
1347 << opNames[i] << ", " << opNames[i+1] << ");";
1348 nl(Out);
1349 }
1350 break;
1351 }
1352 case Instruction::Trunc:
1353 case Instruction::ZExt:
1354 case Instruction::SExt:
1355 case Instruction::FPTrunc:
1356 case Instruction::FPExt:
1357 case Instruction::FPToUI:
1358 case Instruction::FPToSI:
1359 case Instruction::UIToFP:
1360 case Instruction::SIToFP:
1361 case Instruction::PtrToInt:
1362 case Instruction::IntToPtr:
1363 case Instruction::BitCast: {
1364 const CastInst* cst = cast<CastInst>(I);
1365 Out << "CastInst* " << iName << " = new ";
1366 switch (I->getOpcode()) {
1367 case Instruction::Trunc: Out << "TruncInst"; break;
1368 case Instruction::ZExt: Out << "ZExtInst"; break;
1369 case Instruction::SExt: Out << "SExtInst"; break;
1370 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1371 case Instruction::FPExt: Out << "FPExtInst"; break;
1372 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1373 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1374 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1375 case Instruction::SIToFP: Out << "SIToFPInst"; break;
1376 case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
1377 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1378 case Instruction::BitCast: Out << "BitCastInst"; break;
1379 default: assert(!"Unreachable"); break;
1380 }
1381 Out << "(" << opNames[0] << ", "
1382 << getCppName(cst->getType()) << ", \"";
1383 printEscapedString(cst->getName());
1384 Out << "\", " << bbname << ");";
1385 break;
1386 }
1387 case Instruction::Call:{
1388 const CallInst* call = cast<CallInst>(I);
Gabor Greif0c8f7dc2009-03-25 06:32:59 +00001389 if (const InlineAsm* ila = dyn_cast<InlineAsm>(call->getCalledValue())) {
Anton Korobeynikov50276522008-04-23 22:29:24 +00001390 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
1391 << getCppName(ila->getFunctionType()) << ", \""
1392 << ila->getAsmString() << "\", \""
1393 << ila->getConstraintString() << "\","
1394 << (ila->hasSideEffects() ? "true" : "false") << ");";
1395 nl(Out);
1396 }
1397 if (call->getNumOperands() > 2) {
1398 Out << "std::vector<Value*> " << iName << "_params;";
1399 nl(Out);
1400 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1401 Out << iName << "_params.push_back(" << opNames[i] << ");";
1402 nl(Out);
1403 }
1404 Out << "CallInst* " << iName << " = CallInst::Create("
1405 << opNames[0] << ", " << iName << "_params.begin(), "
1406 << iName << "_params.end(), \"";
1407 } else if (call->getNumOperands() == 2) {
1408 Out << "CallInst* " << iName << " = CallInst::Create("
1409 << opNames[0] << ", " << opNames[1] << ", \"";
1410 } else {
1411 Out << "CallInst* " << iName << " = CallInst::Create(" << opNames[0]
1412 << ", \"";
1413 }
1414 printEscapedString(call->getName());
1415 Out << "\", " << bbname << ");";
1416 nl(Out) << iName << "->setCallingConv(";
1417 printCallingConv(call->getCallingConv());
1418 Out << ");";
1419 nl(Out) << iName << "->setTailCall("
1420 << (call->isTailCall() ? "true":"false");
1421 Out << ");";
Devang Patel05988662008-09-25 21:00:45 +00001422 printAttributes(call->getAttributes(), iName);
1423 Out << iName << "->setAttributes(" << iName << "_PAL);";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001424 nl(Out);
1425 break;
1426 }
1427 case Instruction::Select: {
1428 const SelectInst* sel = cast<SelectInst>(I);
1429 Out << "SelectInst* " << getCppName(sel) << " = SelectInst::Create(";
1430 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1431 printEscapedString(sel->getName());
1432 Out << "\", " << bbname << ");";
1433 break;
1434 }
1435 case Instruction::UserOp1:
1436 /// FALL THROUGH
1437 case Instruction::UserOp2: {
1438 /// FIXME: What should be done here?
1439 break;
1440 }
1441 case Instruction::VAArg: {
1442 const VAArgInst* va = cast<VAArgInst>(I);
1443 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
1444 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
1445 printEscapedString(va->getName());
1446 Out << "\", " << bbname << ");";
1447 break;
1448 }
1449 case Instruction::ExtractElement: {
1450 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
1451 Out << "ExtractElementInst* " << getCppName(eei)
1452 << " = new ExtractElementInst(" << opNames[0]
1453 << ", " << opNames[1] << ", \"";
1454 printEscapedString(eei->getName());
1455 Out << "\", " << bbname << ");";
1456 break;
1457 }
1458 case Instruction::InsertElement: {
1459 const InsertElementInst* iei = cast<InsertElementInst>(I);
1460 Out << "InsertElementInst* " << getCppName(iei)
1461 << " = InsertElementInst::Create(" << opNames[0]
1462 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1463 printEscapedString(iei->getName());
1464 Out << "\", " << bbname << ");";
1465 break;
1466 }
1467 case Instruction::ShuffleVector: {
1468 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
1469 Out << "ShuffleVectorInst* " << getCppName(svi)
1470 << " = new ShuffleVectorInst(" << opNames[0]
1471 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1472 printEscapedString(svi->getName());
1473 Out << "\", " << bbname << ");";
1474 break;
1475 }
Dan Gohman75146a62008-06-09 14:12:10 +00001476 case Instruction::ExtractValue: {
1477 const ExtractValueInst *evi = cast<ExtractValueInst>(I);
1478 Out << "std::vector<unsigned> " << iName << "_indices;";
1479 nl(Out);
1480 for (unsigned i = 0; i < evi->getNumIndices(); ++i) {
1481 Out << iName << "_indices.push_back("
1482 << evi->idx_begin()[i] << ");";
1483 nl(Out);
1484 }
1485 Out << "ExtractValueInst* " << getCppName(evi)
1486 << " = ExtractValueInst::Create(" << opNames[0]
1487 << ", "
1488 << iName << "_indices.begin(), " << iName << "_indices.end(), \"";
1489 printEscapedString(evi->getName());
1490 Out << "\", " << bbname << ");";
1491 break;
1492 }
1493 case Instruction::InsertValue: {
1494 const InsertValueInst *ivi = cast<InsertValueInst>(I);
1495 Out << "std::vector<unsigned> " << iName << "_indices;";
1496 nl(Out);
1497 for (unsigned i = 0; i < ivi->getNumIndices(); ++i) {
1498 Out << iName << "_indices.push_back("
1499 << ivi->idx_begin()[i] << ");";
1500 nl(Out);
1501 }
1502 Out << "InsertValueInst* " << getCppName(ivi)
1503 << " = InsertValueInst::Create(" << opNames[0]
1504 << ", " << opNames[1] << ", "
1505 << iName << "_indices.begin(), " << iName << "_indices.end(), \"";
1506 printEscapedString(ivi->getName());
1507 Out << "\", " << bbname << ");";
1508 break;
1509 }
Anton Korobeynikov50276522008-04-23 22:29:24 +00001510 }
1511 DefinedValues.insert(I);
1512 nl(Out);
1513 delete [] opNames;
1514}
1515
1516 // Print out the types, constants and declarations needed by one function
1517 void CppWriter::printFunctionUses(const Function* F) {
1518 nl(Out) << "// Type Definitions"; nl(Out);
1519 if (!is_inline) {
1520 // Print the function's return type
1521 printType(F->getReturnType());
1522
1523 // Print the function's function type
1524 printType(F->getFunctionType());
1525
1526 // Print the types of each of the function's arguments
1527 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1528 AI != AE; ++AI) {
1529 printType(AI->getType());
1530 }
1531 }
1532
1533 // Print type definitions for every type referenced by an instruction and
1534 // make a note of any global values or constants that are referenced
1535 SmallPtrSet<GlobalValue*,64> gvs;
1536 SmallPtrSet<Constant*,64> consts;
1537 for (Function::const_iterator BB = F->begin(), BE = F->end();
1538 BB != BE; ++BB){
1539 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1540 I != E; ++I) {
1541 // Print the type of the instruction itself
1542 printType(I->getType());
1543
1544 // Print the type of each of the instruction's operands
1545 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1546 Value* operand = I->getOperand(i);
1547 printType(operand->getType());
1548
1549 // If the operand references a GVal or Constant, make a note of it
1550 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
1551 gvs.insert(GV);
1552 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1553 if (GVar->hasInitializer())
1554 consts.insert(GVar->getInitializer());
1555 } else if (Constant* C = dyn_cast<Constant>(operand))
1556 consts.insert(C);
1557 }
1558 }
1559 }
1560
1561 // Print the function declarations for any functions encountered
1562 nl(Out) << "// Function Declarations"; nl(Out);
1563 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1564 I != E; ++I) {
1565 if (Function* Fun = dyn_cast<Function>(*I)) {
1566 if (!is_inline || Fun != F)
1567 printFunctionHead(Fun);
1568 }
1569 }
1570
1571 // Print the global variable declarations for any variables encountered
1572 nl(Out) << "// Global Variable Declarations"; nl(Out);
1573 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1574 I != E; ++I) {
1575 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1576 printVariableHead(F);
1577 }
1578
1579 // Print the constants found
1580 nl(Out) << "// Constant Definitions"; nl(Out);
1581 for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(),
1582 E = consts.end(); I != E; ++I) {
1583 printConstant(*I);
1584 }
1585
1586 // Process the global variables definitions now that all the constants have
1587 // been emitted. These definitions just couple the gvars with their constant
1588 // initializers.
1589 nl(Out) << "// Global Variable Definitions"; nl(Out);
1590 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1591 I != E; ++I) {
1592 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1593 printVariableBody(GV);
1594 }
1595 }
1596
1597 void CppWriter::printFunctionHead(const Function* F) {
1598 nl(Out) << "Function* " << getCppName(F);
1599 if (is_inline) {
1600 Out << " = mod->getFunction(\"";
1601 printEscapedString(F->getName());
1602 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1603 nl(Out) << "if (!" << getCppName(F) << ") {";
1604 nl(Out) << getCppName(F);
1605 }
1606 Out<< " = Function::Create(";
1607 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1608 nl(Out) << "/*Linkage=*/";
1609 printLinkageType(F->getLinkage());
1610 Out << ",";
1611 nl(Out) << "/*Name=*/\"";
1612 printEscapedString(F->getName());
1613 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
1614 nl(Out,-1);
1615 printCppName(F);
1616 Out << "->setCallingConv(";
1617 printCallingConv(F->getCallingConv());
1618 Out << ");";
1619 nl(Out);
1620 if (F->hasSection()) {
1621 printCppName(F);
1622 Out << "->setSection(\"" << F->getSection() << "\");";
1623 nl(Out);
1624 }
1625 if (F->getAlignment()) {
1626 printCppName(F);
1627 Out << "->setAlignment(" << F->getAlignment() << ");";
1628 nl(Out);
1629 }
1630 if (F->getVisibility() != GlobalValue::DefaultVisibility) {
1631 printCppName(F);
1632 Out << "->setVisibility(";
1633 printVisibilityType(F->getVisibility());
1634 Out << ");";
1635 nl(Out);
1636 }
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001637 if (F->hasGC()) {
Anton Korobeynikov50276522008-04-23 22:29:24 +00001638 printCppName(F);
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001639 Out << "->setGC(\"" << F->getGC() << "\");";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001640 nl(Out);
1641 }
1642 if (is_inline) {
1643 Out << "}";
1644 nl(Out);
1645 }
Devang Patel05988662008-09-25 21:00:45 +00001646 printAttributes(F->getAttributes(), getCppName(F));
Anton Korobeynikov50276522008-04-23 22:29:24 +00001647 printCppName(F);
Devang Patel05988662008-09-25 21:00:45 +00001648 Out << "->setAttributes(" << getCppName(F) << "_PAL);";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001649 nl(Out);
1650 }
1651
1652 void CppWriter::printFunctionBody(const Function *F) {
1653 if (F->isDeclaration())
1654 return; // external functions have no bodies.
1655
1656 // Clear the DefinedValues and ForwardRefs maps because we can't have
1657 // cross-function forward refs
1658 ForwardRefs.clear();
1659 DefinedValues.clear();
1660
1661 // Create all the argument values
1662 if (!is_inline) {
1663 if (!F->arg_empty()) {
1664 Out << "Function::arg_iterator args = " << getCppName(F)
1665 << "->arg_begin();";
1666 nl(Out);
1667 }
1668 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1669 AI != AE; ++AI) {
1670 Out << "Value* " << getCppName(AI) << " = args++;";
1671 nl(Out);
1672 if (AI->hasName()) {
1673 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1674 nl(Out);
1675 }
1676 }
1677 }
1678
1679 // Create all the basic blocks
1680 nl(Out);
1681 for (Function::const_iterator BI = F->begin(), BE = F->end();
1682 BI != BE; ++BI) {
1683 std::string bbname(getCppName(BI));
Owen Anderson267a0ff2009-08-14 17:41:33 +00001684 Out << "BasicBlock* " << bbname <<
1685 " = BasicBlock::Create(getGlobalContext(), \"";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001686 if (BI->hasName())
1687 printEscapedString(BI->getName());
1688 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1689 nl(Out);
1690 }
1691
1692 // Output all of its basic blocks... for the function
1693 for (Function::const_iterator BI = F->begin(), BE = F->end();
1694 BI != BE; ++BI) {
1695 std::string bbname(getCppName(BI));
1696 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1697 nl(Out);
1698
1699 // Output all of the instructions in the basic block...
1700 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1701 I != E; ++I) {
1702 printInstruction(I,bbname);
1703 }
1704 }
1705
1706 // Loop over the ForwardRefs and resolve them now that all instructions
1707 // are generated.
1708 if (!ForwardRefs.empty()) {
1709 nl(Out) << "// Resolve Forward References";
1710 nl(Out);
1711 }
1712
1713 while (!ForwardRefs.empty()) {
1714 ForwardRefMap::iterator I = ForwardRefs.begin();
1715 Out << I->second << "->replaceAllUsesWith("
1716 << getCppName(I->first) << "); delete " << I->second << ";";
1717 nl(Out);
1718 ForwardRefs.erase(I);
1719 }
1720 }
1721
1722 void CppWriter::printInline(const std::string& fname,
1723 const std::string& func) {
1724 const Function* F = TheModule->getFunction(func);
1725 if (!F) {
1726 error(std::string("Function '") + func + "' not found in input module");
1727 return;
1728 }
1729 if (F->isDeclaration()) {
1730 error(std::string("Function '") + func + "' is external!");
1731 return;
1732 }
1733 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
1734 << getCppName(F);
1735 unsigned arg_count = 1;
1736 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1737 AI != AE; ++AI) {
1738 Out << ", Value* arg_" << arg_count;
1739 }
1740 Out << ") {";
1741 nl(Out);
1742 is_inline = true;
1743 printFunctionUses(F);
1744 printFunctionBody(F);
1745 is_inline = false;
1746 Out << "return " << getCppName(F->begin()) << ";";
1747 nl(Out) << "}";
1748 nl(Out);
1749 }
1750
1751 void CppWriter::printModuleBody() {
1752 // Print out all the type definitions
1753 nl(Out) << "// Type Definitions"; nl(Out);
1754 printTypes(TheModule);
1755
1756 // Functions can call each other and global variables can reference them so
1757 // define all the functions first before emitting their function bodies.
1758 nl(Out) << "// Function Declarations"; nl(Out);
1759 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1760 I != E; ++I)
1761 printFunctionHead(I);
1762
1763 // Process the global variables declarations. We can't initialze them until
1764 // after the constants are printed so just print a header for each global
1765 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
1766 for (Module::const_global_iterator I = TheModule->global_begin(),
1767 E = TheModule->global_end(); I != E; ++I) {
1768 printVariableHead(I);
1769 }
1770
1771 // Print out all the constants definitions. Constants don't recurse except
1772 // through GlobalValues. All GlobalValues have been declared at this point
1773 // so we can proceed to generate the constants.
1774 nl(Out) << "// Constant Definitions"; nl(Out);
1775 printConstants(TheModule);
1776
1777 // Process the global variables definitions now that all the constants have
1778 // been emitted. These definitions just couple the gvars with their constant
1779 // initializers.
1780 nl(Out) << "// Global Variable Definitions"; nl(Out);
1781 for (Module::const_global_iterator I = TheModule->global_begin(),
1782 E = TheModule->global_end(); I != E; ++I) {
1783 printVariableBody(I);
1784 }
1785
1786 // Finally, we can safely put out all of the function bodies.
1787 nl(Out) << "// Function Definitions"; nl(Out);
1788 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1789 I != E; ++I) {
1790 if (!I->isDeclaration()) {
1791 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1792 << ")";
1793 nl(Out) << "{";
1794 nl(Out,1);
1795 printFunctionBody(I);
1796 nl(Out,-1) << "}";
1797 nl(Out);
1798 }
1799 }
1800 }
1801
1802 void CppWriter::printProgram(const std::string& fname,
1803 const std::string& mName) {
Owen Anderson267a0ff2009-08-14 17:41:33 +00001804 Out << "#include <llvm/LLVMContext.h>\n";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001805 Out << "#include <llvm/Module.h>\n";
1806 Out << "#include <llvm/DerivedTypes.h>\n";
1807 Out << "#include <llvm/Constants.h>\n";
1808 Out << "#include <llvm/GlobalVariable.h>\n";
1809 Out << "#include <llvm/Function.h>\n";
1810 Out << "#include <llvm/CallingConv.h>\n";
1811 Out << "#include <llvm/BasicBlock.h>\n";
1812 Out << "#include <llvm/Instructions.h>\n";
1813 Out << "#include <llvm/InlineAsm.h>\n";
David Greene71847812009-07-14 20:18:05 +00001814 Out << "#include <llvm/Support/FormattedStream.h>\n";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001815 Out << "#include <llvm/Support/MathExtras.h>\n";
1816 Out << "#include <llvm/Pass.h>\n";
1817 Out << "#include <llvm/PassManager.h>\n";
Nicolas Geoffray9474ede2008-05-14 07:52:03 +00001818 Out << "#include <llvm/ADT/SmallVector.h>\n";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001819 Out << "#include <llvm/Analysis/Verifier.h>\n";
1820 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1821 Out << "#include <algorithm>\n";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001822 Out << "using namespace llvm;\n\n";
1823 Out << "Module* " << fname << "();\n\n";
1824 Out << "int main(int argc, char**argv) {\n";
1825 Out << " Module* Mod = " << fname << "();\n";
1826 Out << " verifyModule(*Mod, PrintMessageAction);\n";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001827 Out << " PassManager PM;\n";
Dan Gohmanf9231292008-12-08 07:07:24 +00001828 Out << " PM.add(createPrintModulePass(&outs()));\n";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001829 Out << " PM.run(*Mod);\n";
1830 Out << " return 0;\n";
1831 Out << "}\n\n";
1832 printModule(fname,mName);
1833 }
1834
1835 void CppWriter::printModule(const std::string& fname,
1836 const std::string& mName) {
1837 nl(Out) << "Module* " << fname << "() {";
1838 nl(Out,1) << "// Module Construction";
Nick Lewyckyb8b73472009-06-26 04:33:37 +00001839 nl(Out) << "Module* mod = new Module(\"";
1840 printEscapedString(mName);
Owen Anderson267a0ff2009-08-14 17:41:33 +00001841 Out << "\", getGlobalContext());";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001842 if (!TheModule->getTargetTriple().empty()) {
1843 nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
1844 }
1845 if (!TheModule->getTargetTriple().empty()) {
1846 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1847 << "\");";
1848 }
1849
1850 if (!TheModule->getModuleInlineAsm().empty()) {
1851 nl(Out) << "mod->setModuleInlineAsm(\"";
1852 printEscapedString(TheModule->getModuleInlineAsm());
1853 Out << "\");";
1854 }
1855 nl(Out);
1856
1857 // Loop over the dependent libraries and emit them.
1858 Module::lib_iterator LI = TheModule->lib_begin();
1859 Module::lib_iterator LE = TheModule->lib_end();
1860 while (LI != LE) {
1861 Out << "mod->addLibrary(\"" << *LI << "\");";
1862 nl(Out);
1863 ++LI;
1864 }
1865 printModuleBody();
1866 nl(Out) << "return mod;";
1867 nl(Out,-1) << "}";
1868 nl(Out);
1869 }
1870
1871 void CppWriter::printContents(const std::string& fname,
1872 const std::string& mName) {
1873 Out << "\nModule* " << fname << "(Module *mod) {\n";
Nick Lewyckyb8b73472009-06-26 04:33:37 +00001874 Out << "\nmod->setModuleIdentifier(\"";
1875 printEscapedString(mName);
1876 Out << "\");\n";
Anton Korobeynikov50276522008-04-23 22:29:24 +00001877 printModuleBody();
1878 Out << "\nreturn mod;\n";
1879 Out << "\n}\n";
1880 }
1881
1882 void CppWriter::printFunction(const std::string& fname,
1883 const std::string& funcName) {
1884 const Function* F = TheModule->getFunction(funcName);
1885 if (!F) {
1886 error(std::string("Function '") + funcName + "' not found in input module");
1887 return;
1888 }
1889 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1890 printFunctionUses(F);
1891 printFunctionHead(F);
1892 printFunctionBody(F);
1893 Out << "return " << getCppName(F) << ";\n";
1894 Out << "}\n";
1895 }
1896
1897 void CppWriter::printFunctions() {
1898 const Module::FunctionListType &funcs = TheModule->getFunctionList();
1899 Module::const_iterator I = funcs.begin();
1900 Module::const_iterator IE = funcs.end();
1901
1902 for (; I != IE; ++I) {
1903 const Function &func = *I;
1904 if (!func.isDeclaration()) {
1905 std::string name("define_");
1906 name += func.getName();
1907 printFunction(name, func.getName());
1908 }
1909 }
1910 }
1911
1912 void CppWriter::printVariable(const std::string& fname,
1913 const std::string& varName) {
1914 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1915
1916 if (!GV) {
1917 error(std::string("Variable '") + varName + "' not found in input module");
1918 return;
1919 }
1920 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1921 printVariableUses(GV);
1922 printVariableHead(GV);
1923 printVariableBody(GV);
1924 Out << "return " << getCppName(GV) << ";\n";
1925 Out << "}\n";
1926 }
1927
1928 void CppWriter::printType(const std::string& fname,
1929 const std::string& typeName) {
1930 const Type* Ty = TheModule->getTypeByName(typeName);
1931 if (!Ty) {
1932 error(std::string("Type '") + typeName + "' not found in input module");
1933 return;
1934 }
1935 Out << "\nType* " << fname << "(Module *mod) {\n";
1936 printType(Ty);
1937 Out << "return " << getCppName(Ty) << ";\n";
1938 Out << "}\n";
1939 }
1940
1941 bool CppWriter::runOnModule(Module &M) {
1942 TheModule = &M;
1943
1944 // Emit a header
1945 Out << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
1946
1947 // Get the name of the function we're supposed to generate
1948 std::string fname = FuncName.getValue();
1949
1950 // Get the name of the thing we are to generate
1951 std::string tgtname = NameToGenerate.getValue();
1952 if (GenerationType == GenModule ||
1953 GenerationType == GenContents ||
1954 GenerationType == GenProgram ||
1955 GenerationType == GenFunctions) {
1956 if (tgtname == "!bad!") {
1957 if (M.getModuleIdentifier() == "-")
1958 tgtname = "<stdin>";
1959 else
1960 tgtname = M.getModuleIdentifier();
1961 }
1962 } else if (tgtname == "!bad!")
1963 error("You must use the -for option with -gen-{function,variable,type}");
1964
1965 switch (WhatToGenerate(GenerationType)) {
1966 case GenProgram:
1967 if (fname.empty())
1968 fname = "makeLLVMModule";
1969 printProgram(fname,tgtname);
1970 break;
1971 case GenModule:
1972 if (fname.empty())
1973 fname = "makeLLVMModule";
1974 printModule(fname,tgtname);
1975 break;
1976 case GenContents:
1977 if (fname.empty())
1978 fname = "makeLLVMModuleContents";
1979 printContents(fname,tgtname);
1980 break;
1981 case GenFunction:
1982 if (fname.empty())
1983 fname = "makeLLVMFunction";
1984 printFunction(fname,tgtname);
1985 break;
1986 case GenFunctions:
1987 printFunctions();
1988 break;
1989 case GenInline:
1990 if (fname.empty())
1991 fname = "makeLLVMInline";
1992 printInline(fname,tgtname);
1993 break;
1994 case GenVariable:
1995 if (fname.empty())
1996 fname = "makeLLVMVariable";
1997 printVariable(fname,tgtname);
1998 break;
1999 case GenType:
2000 if (fname.empty())
2001 fname = "makeLLVMType";
2002 printType(fname,tgtname);
2003 break;
2004 default:
2005 error("Invalid generation option");
2006 }
2007
2008 return false;
2009 }
2010}
2011
2012char CppWriter::ID = 0;
2013
2014//===----------------------------------------------------------------------===//
2015// External Interface declaration
2016//===----------------------------------------------------------------------===//
2017
2018bool CPPTargetMachine::addPassesToEmitWholeFile(PassManager &PM,
David Greene71847812009-07-14 20:18:05 +00002019 formatted_raw_ostream &o,
Anton Korobeynikov50276522008-04-23 22:29:24 +00002020 CodeGenFileType FileType,
Bill Wendling98a366d2009-04-29 23:29:43 +00002021 CodeGenOpt::Level OptLevel) {
Anton Korobeynikov50276522008-04-23 22:29:24 +00002022 if (FileType != TargetMachine::AssemblyFile) return true;
2023 PM.add(new CppWriter(o));
2024 return false;
2025}