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