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