1 //===-- CPPBackend.cpp - Library for converting LLVM code to C++ code -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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"
31 #include "llvm/Support/Streams.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Config/config.h"
39 static cl::opt<std::string>
40 FuncName("cppfname", cl::desc("Specify the name of the generated function"),
41 cl::value_desc("function name"));
54 static cl::opt<WhatToGenerate> GenerationType("cppgen", cl::Optional,
55 cl::desc("Choose what kind of output to generate"),
58 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"),
70 static cl::opt<std::string> NameToGenerate("cppfor", cl::Optional,
71 cl::desc("Specify the name of the thing to generate"),
74 /// 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
79 extern "C" int CppBackendTargetMachineModule;
80 int CppBackendTargetMachineModule = 0;
82 // Register the target.
83 static RegisterTarget<CPPTargetMachine> X("cpp", "C++ backend");
86 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;
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 {
99 const Module *TheModule;
103 TypeMap UnresolvedTypes;
106 TypeSet DefinedTypes;
107 ValueSet DefinedValues;
108 ForwardRefMap ForwardRefs;
113 explicit CppWriter(raw_ostream &o) :
114 ModulePass(&ID), Out(o), uniqueNum(0), is_inline(false) {}
116 virtual const char *getPassName() const { return "C++ backend"; }
118 bool runOnModule(Module &M);
120 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 );
129 void error(const std::string& msg);
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);
138 std::string getCppName(const Type* val);
139 inline void printCppName(const Type* val);
141 std::string getCppName(const Value* val);
142 inline void printCppName(const Value* val);
144 void printAttributes(const AttrListPtr &PAL, const std::string &name);
145 bool printTypeInternal(const Type* Ty);
146 inline void printType(const Type* Ty);
147 void printTypes(const Module* M);
149 void printConstant(const Constant *CPV);
150 void printConstants(const Module* M);
152 void printVariableUses(const GlobalVariable *GV);
153 void printVariableHead(const GlobalVariable *GV);
154 void printVariableBody(const GlobalVariable *GV);
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*);
162 void printModuleBody();
165 static unsigned indent_level = 0;
166 inline raw_ostream& nl(raw_ostream& Out, int delta = 0) {
168 if (delta >= 0 || indent_level >= unsigned(-delta))
169 indent_level += delta;
170 for (unsigned i = 0; i < indent_level; ++i)
175 inline void in() { indent_level++; }
176 inline void out() { if (indent_level >0) indent_level--; }
179 sanitize(std::string& str) {
180 for (size_t i = 0; i < str.length(); ++i)
181 if (!isalnum(str[i]) && str[i] != '_')
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()) +
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_";
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)
220 void CppWriter::error(const std::string& msg) {
221 cerr << progname << ": " << msg << "\n";
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) {
230 APFloat APF = APFloat(CFP->getValueAPF()); // copy
231 if (CFP->getType() == Type::FloatTy)
232 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, &ignored);
233 Out << "ConstantFP::get(";
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 << ")";
245 Out << "BitsToFloat((float)" << Buffer << ")";
249 std::string StrVal = ftostr(CFP->getValueAPF());
251 while (StrVal[0] == ' ')
252 StrVal.erase(StrVal.begin());
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)
263 Out << StrVal << "f";
264 } else if (CFP->getType() == Type::DoubleTy)
265 Out << "BitsToDouble(0x"
266 << utohexstr(CFP->getValueAPF().bitcastToAPInt().getZExtValue())
267 << "ULL) /* " << StrVal << " */";
269 Out << "BitsToFloat(0x"
270 << utohexstr((uint32_t)CFP->getValueAPF().
271 bitcastToAPInt().getZExtValue())
272 << "U) /* " << StrVal << " */";
280 void CppWriter::printCallingConv(unsigned cc){
281 // Print the calling convention.
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;
291 void CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
293 case GlobalValue::InternalLinkage:
294 Out << "GlobalValue::InternalLinkage"; break;
295 case GlobalValue::PrivateLinkage:
296 Out << "GlobalValue::PrivateLinkage"; break;
297 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;
305 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;
313 case GlobalValue::ExternalWeakAnyLinkage:
314 Out << "GlobalValue::ExternalWeakAnyLinkage"; break;
315 case GlobalValue::ExternalWeakODRLinkage:
316 Out << "GlobalValue::ExternalWeakODRLinkage"; break;
317 case GlobalValue::GhostLinkage:
318 Out << "GlobalValue::GhostLinkage"; break;
319 case GlobalValue::CommonAnyLinkage:
320 Out << "GlobalValue::CommonAnyLinkage"; break;
321 case GlobalValue::CommonODRLinkage:
322 Out << "GlobalValue::CommonODRLinkage"; break;
326 void CppWriter::printVisibilityType(GlobalValue::VisibilityTypes VisType) {
328 default: assert(0 && "Unknown GVar visibility");
329 case GlobalValue::DefaultVisibility:
330 Out << "GlobalValue::DefaultVisibility";
332 case GlobalValue::HiddenVisibility:
333 Out << "GlobalValue::HiddenVisibility";
335 case GlobalValue::ProtectedVisibility:
336 Out << "GlobalValue::ProtectedVisibility";
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 != '\\') {
350 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
351 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
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) + ")";
365 case Type::FloatTyID: return "Type::FloatTy";
366 case Type::DoubleTyID: return "Type::DoubleTy";
367 case Type::LabelTyID: return "Type::LabelTy";
369 error("Invalid primitive type");
372 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
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())
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
392 // See if the type has a name in the symboltable and build accordingly
393 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
396 name = std::string(prefix) + *tName;
398 name = std::string(prefix) + utostr(uniqueNum++);
402 return TypeNames[Ty] = name;
405 void CppWriter::printCppName(const Type* Ty) {
406 printEscapedString(getCppName(Ty));
409 std::string CppWriter::getCppName(const Value* val) {
411 ValueMap::iterator I = ValueNames.find(val);
412 if (I != ValueNames.end() && I->first == val)
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)) {
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;
433 name = getTypePrefix(val->getType());
436 name = getTypePrefix(val->getType());
438 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
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;
447 void CppWriter::printCppName(const Value* val) {
448 printEscapedString(getCppName(val));
451 void CppWriter::printAttributes(const AttrListPtr &PAL,
452 const std::string &name) {
453 Out << "AttrListPtr " << name << "_PAL;";
455 if (!PAL.isEmpty()) {
456 Out << '{'; in(); nl(Out);
457 Out << "SmallVector<AttributeWithIndex, 4> Attrs;"; nl(Out);
458 Out << "AttributeWithIndex PAWI;"; nl(Out);
459 for (unsigned i = 0; i < PAL.getNumSlots(); ++i) {
460 unsigned index = PAL.getSlot(i).Index;
461 Attributes attrs = PAL.getSlot(i).Attrs;
462 Out << "PAWI.Index = " << index << "U; PAWI.Attrs = 0 ";
463 #define HANDLE_ATTR(X) \
464 if (attrs & Attribute::X) \
465 Out << " | Attribute::" #X; \
466 attrs &= ~Attribute::X;
470 HANDLE_ATTR(StructRet);
472 HANDLE_ATTR(NoReturn);
473 HANDLE_ATTR(NoUnwind);
475 HANDLE_ATTR(NoAlias);
477 HANDLE_ATTR(ReadNone);
478 HANDLE_ATTR(ReadOnly);
479 HANDLE_ATTR(NoCapture);
481 assert(attrs == 0 && "Unhandled attribute!");
484 Out << "Attrs.push_back(PAWI);";
487 Out << name << "_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());";
494 bool CppWriter::printTypeInternal(const Type* Ty) {
495 // We don't print definitions for primitive types
496 if (Ty->isPrimitiveType() || Ty->isInteger())
499 // If we already defined this type, we don't need to define it again.
500 if (DefinedTypes.find(Ty) != DefinedTypes.end())
503 // Everything below needs the name for the type so get it now.
504 std::string typeName(getCppName(Ty));
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(),
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();";
518 UnresolvedTypes[Ty] = typeName;
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);
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;";
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;
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;
553 nl(Out) << "/*Params=*/" << typeName << "_args,";
554 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
559 case Type::StructTyID: {
560 const StructType* ST = cast<StructType>(Ty);
561 Out << "std::vector<const Type*>" << typeName << "_fields;";
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;
575 Out << "StructType* " << typeName << " = StructType::get("
576 << typeName << "_fields, /*isPacked=*/"
577 << (ST->isPacked() ? "true" : "false") << ");";
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()) << ");";
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()) << ");";
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()) << ");";
614 case Type::OpaqueTyID: {
615 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
620 error("Invalid TypeID");
623 // If the type had a name, make sure we recreate it.
624 const std::string* progTypeName =
625 findTypeName(TheModule->getTypeSymbolTable(),Ty);
627 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
632 // Pop us off the type stack
633 TypeStack.pop_back();
635 // Indicate that this type is now defined.
636 DefinedTypes.insert(Ty);
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
642 TypeMap::iterator I = UnresolvedTypes.find(Ty);
643 if (I != UnresolvedTypes.end()) {
644 Out << "cast<OpaqueType>(" << I->second
645 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
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;
657 Out << ">(" << I->second << "_fwd.get());";
659 UnresolvedTypes.erase(I);
662 // Finally, separate the type definition from other with a newline.
665 // We weren't a recursive type
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());
674 printTypeInternal(Ty);
675 assert(TypeStack.empty());
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();
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) << ");";
692 // For everything else, define the type
694 printType(TI->second);
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());
706 // Add all the functions to the table
707 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
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());
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;
723 printType(I->getType());
724 for (unsigned i = 0; i < I->getNumOperands(); ++i)
725 printType(I->getOperand(i)->getType());
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
737 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
740 std::string constName(getCppName(CV));
741 std::string typeName(getCppName(CV->getType()));
743 if (isa<GlobalValue>(CV)) {
744 // Skip variables and functions, we emit them elsewhere
748 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
749 std::string constValue = CI->getValue().toString(10, true);
750 Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
751 << cast<IntegerType>(CI->getType())->getBitWidth() << ", \""
752 << constValue << "\", " << constValue.length() << ", 10));";
753 } else if (isa<ConstantAggregateZero>(CV)) {
754 Out << "ConstantAggregateZero* " << constName
755 << " = ConstantAggregateZero::get(" << typeName << ");";
756 } else if (isa<ConstantPointerNull>(CV)) {
757 Out << "ConstantPointerNull* " << constName
758 << " = ConstantPointerNull::get(" << typeName << ");";
759 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
760 Out << "ConstantFP* " << constName << " = ";
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;
772 printEscapedString(tmp);
773 // Determine if we want null termination or not.
775 Out << "\", true"; // Indicate that the null terminator should be
778 Out << "\", false";// No null terminator
781 Out << "std::vector<Constant*> " << constName << "_elems;";
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)) << ");";
790 Out << "Constant* " << constName << " = ConstantArray::get("
791 << typeName << ", " << constName << "_elems);";
793 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
794 Out << "std::vector<Constant*> " << constName << "_fields;";
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)) << ");";
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;";
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)) << ");";
815 Out << "Constant* " << constName << " = ConstantVector::get("
816 << typeName << ", " << constName << "_elems);";
817 } else if (isa<UndefValue>(CV)) {
818 Out << "UndefValue* " << constName << " = UndefValue::get("
820 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
821 if (CE->getOpcode() == Instruction::GetElementPtr) {
822 Out << "std::vector<Constant*> " << constName << "_indices;";
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)) << ");";
831 Out << "Constant* " << constName
832 << " = ConstantExpr::getGetElementPtr("
833 << getCppName(CE->getOperand(0)) << ", "
834 << "&" << constName << "_indices[0], "
835 << constName << "_indices.size()"
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;
855 Out << ", " << getCppName(CE->getOperand(0)) << ", "
856 << getCppName(CE->getType()) << ");";
858 unsigned N = CE->getNumOperands();
859 for (unsigned i = 0; i < N; ++i ) {
860 printConstant(CE->getOperand(i));
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");
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");
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;
922 error("Invalid constant expression");
925 Out << getCppName(CE->getOperand(0));
926 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
927 Out << ", " << getCppName(CE->getOperand(i));
931 error("Bad Constant");
932 Out << "Constant* " << constName << " = 0; ";
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());
944 // Traverse the LLVM functions looking for constants
945 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
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;
952 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
953 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
962 void CppWriter::printVariableUses(const GlobalVariable *GV) {
963 nl(Out) << "// Type Definitions";
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);
976 nl(Out) << "// Constant Definitions"; nl(Out);
979 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
980 nl(Out) << "// Global Variable Definitions"; nl(Out);
981 printVariableBody(gv);
986 void CppWriter::printVariableHead(const GlobalVariable *GV) {
987 nl(Out) << "GlobalVariable* " << getCppName(GV);
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);
995 Out << " = new GlobalVariable(";
996 nl(Out) << "/*Type=*/";
997 printCppName(GV->getType()->getElementType());
999 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
1001 nl(Out) << "/*Linkage=*/";
1002 printLinkageType(GV->getLinkage());
1004 nl(Out) << "/*Initializer=*/0, ";
1005 if (GV->hasInitializer()) {
1006 Out << "// has initializer, specified below";
1008 nl(Out) << "/*Name=*/\"";
1009 printEscapedString(GV->getName());
1014 if (GV->hasSection()) {
1016 Out << "->setSection(\"";
1017 printEscapedString(GV->getSection());
1021 if (GV->getAlignment()) {
1023 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
1026 if (GV->getVisibility() != GlobalValue::DefaultVisibility) {
1028 Out << "->setVisibility(";
1029 printVisibilityType(GV->getVisibility());
1034 out(); Out << "}"; nl(Out);
1038 void CppWriter::printVariableBody(const GlobalVariable *GV) {
1039 if (GV->hasInitializer()) {
1041 Out << "->setInitializer(";
1042 Out << getCppName(GV->getInitializer()) << ");";
1047 std::string CppWriter::getOpName(Value* V) {
1048 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
1049 return getCppName(V);
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())
1057 // This is a new forward reference. Generate a unique name for it
1058 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
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()) << ");";
1066 ForwardRefs[V] = result;
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));
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));
1082 switch (I->getOpcode()) {
1084 error("Invalid instruction");
1087 case Instruction::Ret: {
1088 const ReturnInst* ret = cast<ReturnInst>(I);
1089 Out << "ReturnInst::Create("
1090 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
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] << ", ";
1101 } else if (br->getNumOperands() == 1) {
1102 Out << opNames[0] << ", ";
1104 error("Branch with 2 operands?");
1106 Out << bbname << ");";
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 << ");";
1116 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
1117 Out << iName << "->addCase("
1118 << opNames[i] << ", "
1119 << opNames[i+1] << ");";
1124 case Instruction::Invoke: {
1125 const InvokeInst* inv = cast<InvokeInst>(I);
1126 Out << "std::vector<Value*> " << iName << "_params;";
1128 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1129 Out << iName << "_params.push_back("
1130 << opNames[i] << ");";
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());
1143 printAttributes(inv->getAttributes(), iName);
1144 Out << iName << "->setAttributes(" << iName << "_PAL);";
1148 case Instruction::Unwind: {
1149 Out << "new UnwindInst("
1153 case Instruction::Unreachable:{
1154 Out << "new UnreachableInst("
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:{
1173 Out << "BinaryOperator* " << iName << " = BinaryOperator::Create(";
1174 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;
1192 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1193 printEscapedString(I->getName());
1194 Out << "\", " << bbname << ");";
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;
1218 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1219 printEscapedString(I->getName());
1220 Out << "\", " << bbname << ");";
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;
1238 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1239 printEscapedString(I->getName());
1240 Out << "\", " << bbname << ");";
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] << ", " ;
1250 printEscapedString(mallocI->getName());
1251 Out << "\", " << bbname << ");";
1252 if (mallocI->getAlignment())
1253 nl(Out) << iName << "->setAlignment("
1254 << mallocI->getAlignment() << ");";
1257 case Instruction::Free: {
1258 Out << "FreeInst* " << iName << " = new FreeInst("
1259 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
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] << ", ";
1269 printEscapedString(allocaI->getName());
1270 Out << "\", " << bbname << ");";
1271 if (allocaI->getAlignment())
1272 nl(Out) << iName << "->setAlignment("
1273 << allocaI->getAlignment() << ");";
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 << ");";
1285 case Instruction::Store: {
1286 const StoreInst* store = cast<StoreInst>(I);
1287 Out << " new StoreInst("
1288 << opNames[0] << ", "
1289 << opNames[1] << ", "
1290 << (store->isVolatile() ? "true" : "false")
1291 << ", " << bbname << ");";
1294 case Instruction::GetElementPtr: {
1295 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1296 if (gep->getNumOperands() <= 2) {
1297 Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
1299 if (gep->getNumOperands() == 2)
1300 Out << ", " << opNames[1];
1302 Out << "std::vector<Value*> " << iName << "_indices;";
1304 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
1305 Out << iName << "_indices.push_back("
1306 << opNames[i] << ");";
1309 Out << "Instruction* " << iName << " = GetElementPtrInst::Create("
1310 << opNames[0] << ", " << iName << "_indices.begin(), "
1311 << iName << "_indices.end()";
1314 printEscapedString(gep->getName());
1315 Out << "\", " << bbname << ");";
1318 case Instruction::PHI: {
1319 const PHINode* phi = cast<PHINode>(I);
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()
1329 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
1330 Out << iName << "->addIncoming("
1331 << opNames[i] << ", " << opNames[i+1] << ");";
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;
1365 Out << "(" << opNames[0] << ", "
1366 << getCppName(cst->getType()) << ", \"";
1367 printEscapedString(cst->getName());
1368 Out << "\", " << bbname << ");";
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") << ");";
1381 if (call->getNumOperands() > 2) {
1382 Out << "std::vector<Value*> " << iName << "_params;";
1384 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1385 Out << iName << "_params.push_back(" << opNames[i] << ");";
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] << ", \"";
1395 Out << "CallInst* " << iName << " = CallInst::Create(" << opNames[0]
1398 printEscapedString(call->getName());
1399 Out << "\", " << bbname << ");";
1400 nl(Out) << iName << "->setCallingConv(";
1401 printCallingConv(call->getCallingConv());
1403 nl(Out) << iName << "->setTailCall("
1404 << (call->isTailCall() ? "true":"false");
1406 printAttributes(call->getAttributes(), iName);
1407 Out << iName << "->setAttributes(" << iName << "_PAL);";
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 << ");";
1419 case Instruction::UserOp1:
1421 case Instruction::UserOp2: {
1422 /// FIXME: What should be done here?
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 << ");";
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 << ");";
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 << ");";
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 << ");";
1460 case Instruction::ExtractValue: {
1461 const ExtractValueInst *evi = cast<ExtractValueInst>(I);
1462 Out << "std::vector<unsigned> " << iName << "_indices;";
1464 for (unsigned i = 0; i < evi->getNumIndices(); ++i) {
1465 Out << iName << "_indices.push_back("
1466 << evi->idx_begin()[i] << ");";
1469 Out << "ExtractValueInst* " << getCppName(evi)
1470 << " = ExtractValueInst::Create(" << opNames[0]
1472 << iName << "_indices.begin(), " << iName << "_indices.end(), \"";
1473 printEscapedString(evi->getName());
1474 Out << "\", " << bbname << ");";
1477 case Instruction::InsertValue: {
1478 const InsertValueInst *ivi = cast<InsertValueInst>(I);
1479 Out << "std::vector<unsigned> " << iName << "_indices;";
1481 for (unsigned i = 0; i < ivi->getNumIndices(); ++i) {
1482 Out << iName << "_indices.push_back("
1483 << ivi->idx_begin()[i] << ");";
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 << ");";
1495 DefinedValues.insert(I);
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);
1504 // Print the function's return type
1505 printType(F->getReturnType());
1507 // Print the function's function type
1508 printType(F->getFunctionType());
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();
1513 printType(AI->getType());
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();
1523 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1525 // Print the type of the instruction itself
1526 printType(I->getType());
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());
1533 // If the operand references a GVal or Constant, make a note of it
1534 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
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))
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();
1549 if (Function* Fun = dyn_cast<Function>(*I)) {
1550 if (!is_inline || Fun != F)
1551 printFunctionHead(Fun);
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();
1559 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1560 printVariableHead(F);
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) {
1570 // Process the global variables definitions now that all the constants have
1571 // been emitted. These definitions just couple the gvars with their constant
1573 nl(Out) << "// Global Variable Definitions"; nl(Out);
1574 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1576 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1577 printVariableBody(GV);
1581 void CppWriter::printFunctionHead(const Function* F) {
1582 nl(Out) << "Function* " << getCppName(F);
1584 Out << " = mod->getFunction(\"";
1585 printEscapedString(F->getName());
1586 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1587 nl(Out) << "if (!" << getCppName(F) << ") {";
1588 nl(Out) << getCppName(F);
1590 Out<< " = Function::Create(";
1591 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1592 nl(Out) << "/*Linkage=*/";
1593 printLinkageType(F->getLinkage());
1595 nl(Out) << "/*Name=*/\"";
1596 printEscapedString(F->getName());
1597 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
1600 Out << "->setCallingConv(";
1601 printCallingConv(F->getCallingConv());
1604 if (F->hasSection()) {
1606 Out << "->setSection(\"" << F->getSection() << "\");";
1609 if (F->getAlignment()) {
1611 Out << "->setAlignment(" << F->getAlignment() << ");";
1614 if (F->getVisibility() != GlobalValue::DefaultVisibility) {
1616 Out << "->setVisibility(";
1617 printVisibilityType(F->getVisibility());
1623 Out << "->setGC(\"" << F->getGC() << "\");";
1630 printAttributes(F->getAttributes(), getCppName(F));
1632 Out << "->setAttributes(" << getCppName(F) << "_PAL);";
1636 void CppWriter::printFunctionBody(const Function *F) {
1637 if (F->isDeclaration())
1638 return; // external functions have no bodies.
1640 // Clear the DefinedValues and ForwardRefs maps because we can't have
1641 // cross-function forward refs
1642 ForwardRefs.clear();
1643 DefinedValues.clear();
1645 // Create all the argument values
1647 if (!F->arg_empty()) {
1648 Out << "Function::arg_iterator args = " << getCppName(F)
1649 << "->arg_begin();";
1652 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1654 Out << "Value* " << getCppName(AI) << " = args++;";
1656 if (AI->hasName()) {
1657 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1663 // Create all the basic blocks
1665 for (Function::const_iterator BI = F->begin(), BE = F->end();
1667 std::string bbname(getCppName(BI));
1668 Out << "BasicBlock* " << bbname << " = BasicBlock::Create(\"";
1670 printEscapedString(BI->getName());
1671 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1675 // Output all of its basic blocks... for the function
1676 for (Function::const_iterator BI = F->begin(), BE = F->end();
1678 std::string bbname(getCppName(BI));
1679 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1682 // Output all of the instructions in the basic block...
1683 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1685 printInstruction(I,bbname);
1689 // Loop over the ForwardRefs and resolve them now that all instructions
1691 if (!ForwardRefs.empty()) {
1692 nl(Out) << "// Resolve Forward References";
1696 while (!ForwardRefs.empty()) {
1697 ForwardRefMap::iterator I = ForwardRefs.begin();
1698 Out << I->second << "->replaceAllUsesWith("
1699 << getCppName(I->first) << "); delete " << I->second << ";";
1701 ForwardRefs.erase(I);
1705 void CppWriter::printInline(const std::string& fname,
1706 const std::string& func) {
1707 const Function* F = TheModule->getFunction(func);
1709 error(std::string("Function '") + func + "' not found in input module");
1712 if (F->isDeclaration()) {
1713 error(std::string("Function '") + func + "' is external!");
1716 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
1718 unsigned arg_count = 1;
1719 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1721 Out << ", Value* arg_" << arg_count;
1726 printFunctionUses(F);
1727 printFunctionBody(F);
1729 Out << "return " << getCppName(F->begin()) << ";";
1734 void CppWriter::printModuleBody() {
1735 // Print out all the type definitions
1736 nl(Out) << "// Type Definitions"; nl(Out);
1737 printTypes(TheModule);
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();
1744 printFunctionHead(I);
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);
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);
1760 // Process the global variables definitions now that all the constants have
1761 // been emitted. These definitions just couple the gvars with their constant
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);
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();
1773 if (!I->isDeclaration()) {
1774 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1778 printFunctionBody(I);
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";
1797 Out << "#include <llvm/Support/raw_ostream.h>\n";
1798 Out << "#include <llvm/Pass.h>\n";
1799 Out << "#include <llvm/PassManager.h>\n";
1800 Out << "#include <llvm/ADT/SmallVector.h>\n";
1801 Out << "#include <llvm/Analysis/Verifier.h>\n";
1802 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1803 Out << "#include <algorithm>\n";
1804 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";
1809 Out << " errs().flush();\n";
1810 Out << " outs().flush();\n";
1811 Out << " PassManager PM;\n";
1812 Out << " PM.add(createPrintModulePass(&outs()));\n";
1813 Out << " PM.run(*Mod);\n";
1814 Out << " return 0;\n";
1816 printModule(fname,mName);
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() << "\");";
1827 if (!TheModule->getTargetTriple().empty()) {
1828 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1832 if (!TheModule->getModuleInlineAsm().empty()) {
1833 nl(Out) << "mod->setModuleInlineAsm(\"";
1834 printEscapedString(TheModule->getModuleInlineAsm());
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();
1843 Out << "mod->addLibrary(\"" << *LI << "\");";
1848 nl(Out) << "return mod;";
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";
1858 Out << "\nreturn mod;\n";
1862 void CppWriter::printFunction(const std::string& fname,
1863 const std::string& funcName) {
1864 const Function* F = TheModule->getFunction(funcName);
1866 error(std::string("Function '") + funcName + "' not found in input module");
1869 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1870 printFunctionUses(F);
1871 printFunctionHead(F);
1872 printFunctionBody(F);
1873 Out << "return " << getCppName(F) << ";\n";
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();
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());
1892 void CppWriter::printVariable(const std::string& fname,
1893 const std::string& varName) {
1894 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1897 error(std::string("Variable '") + varName + "' not found in input module");
1900 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1901 printVariableUses(GV);
1902 printVariableHead(GV);
1903 printVariableBody(GV);
1904 Out << "return " << getCppName(GV) << ";\n";
1908 void CppWriter::printType(const std::string& fname,
1909 const std::string& typeName) {
1910 const Type* Ty = TheModule->getTypeByName(typeName);
1912 error(std::string("Type '") + typeName + "' not found in input module");
1915 Out << "\nType* " << fname << "(Module *mod) {\n";
1917 Out << "return " << getCppName(Ty) << ";\n";
1921 bool CppWriter::runOnModule(Module &M) {
1925 Out << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
1927 // Get the name of the function we're supposed to generate
1928 std::string fname = FuncName.getValue();
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>";
1940 tgtname = M.getModuleIdentifier();
1942 } else if (tgtname == "!bad!")
1943 error("You must use the -for option with -gen-{function,variable,type}");
1945 switch (WhatToGenerate(GenerationType)) {
1948 fname = "makeLLVMModule";
1949 printProgram(fname,tgtname);
1953 fname = "makeLLVMModule";
1954 printModule(fname,tgtname);
1958 fname = "makeLLVMModuleContents";
1959 printContents(fname,tgtname);
1963 fname = "makeLLVMFunction";
1964 printFunction(fname,tgtname);
1971 fname = "makeLLVMInline";
1972 printInline(fname,tgtname);
1976 fname = "makeLLVMVariable";
1977 printVariable(fname,tgtname);
1981 fname = "makeLLVMType";
1982 printType(fname,tgtname);
1985 error("Invalid generation option");
1992 char CppWriter::ID = 0;
1994 //===----------------------------------------------------------------------===//
1995 // External Interface declaration
1996 //===----------------------------------------------------------------------===//
1998 bool CPPTargetMachine::addPassesToEmitWholeFile(PassManager &PM,
2000 CodeGenFileType FileType,
2002 if (FileType != TargetMachine::AssemblyFile) return true;
2003 PM.add(new CppWriter(o));