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/Config/config.h"
38 static cl::opt<std::string>
39 FuncName("cppfname", cl::desc("Specify the name of the generated function"),
40 cl::value_desc("function name"));
53 static cl::opt<WhatToGenerate> GenerationType("cppgen", cl::Optional,
54 cl::desc("Choose what kind of output to generate"),
57 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"),
69 static cl::opt<std::string> NameToGenerate("cppfor", cl::Optional,
70 cl::desc("Specify the name of the thing to generate"),
73 // Register the target.
74 static RegisterTarget<CPPTargetMachine> X("cpp", " C++ backend");
77 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;
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 {
90 const Module *TheModule;
94 TypeMap UnresolvedTypes;
98 ValueSet DefinedValues;
99 ForwardRefMap ForwardRefs;
104 explicit CppWriter(std::ostream &o) :
105 ModulePass((intptr_t)&ID), Out(o), uniqueNum(0), is_inline(false) {}
107 virtual const char *getPassName() const { return "C++ backend"; }
109 bool runOnModule(Module &M);
111 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 );
120 void error(const std::string& msg);
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);
129 std::string getCppName(const Type* val);
130 inline void printCppName(const Type* val);
132 std::string getCppName(const Value* val);
133 inline void printCppName(const Value* val);
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);
140 void printConstant(const Constant *CPV);
141 void printConstants(const Module* M);
143 void printVariableUses(const GlobalVariable *GV);
144 void printVariableHead(const GlobalVariable *GV);
145 void printVariableBody(const GlobalVariable *GV);
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*);
153 void printModuleBody();
156 static unsigned indent_level = 0;
157 inline std::ostream& nl(std::ostream& Out, int delta = 0) {
159 if (delta >= 0 || indent_level >= unsigned(-delta))
160 indent_level += delta;
161 for (unsigned i = 0; i < indent_level; ++i)
166 inline void in() { indent_level++; }
167 inline void out() { if (indent_level >0) indent_level--; }
170 sanitize(std::string& str) {
171 for (size_t i = 0; i < str.length(); ++i)
172 if (!isalnum(str[i]) && str[i] != '_')
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()) +
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_";
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)
211 void CppWriter::error(const std::string& msg) {
212 std::cerr << progname << ": " << msg << "\n";
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, ";
227 Out << "Type::FloatTy, ";
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 << ")";
239 Out << "BitsToFloat((float)" << Buffer << ")";
243 std::string StrVal = ftostr(CFP->getValueAPF());
245 while (StrVal[0] == ' ')
246 StrVal.erase(StrVal.begin());
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)
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 << " */";
263 Out << "BitsToFloat(0x" << std::hex
264 << (uint32_t)CFP->getValueAPF().convertToAPInt().getZExtValue()
265 << std::dec << "U) /* " << StrVal << " */";
273 void CppWriter::printCallingConv(unsigned cc){
274 // Print the calling convention.
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;
284 void CppWriter::printLinkageType(GlobalValue::LinkageTypes 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;
304 case GlobalValue::CommonLinkage:
305 Out << "GlobalValue::CommonLinkage"; break;
309 void CppWriter::printVisibilityType(GlobalValue::VisibilityTypes VisType) {
311 default: assert(0 && "Unknown GVar visibility");
312 case GlobalValue::DefaultVisibility:
313 Out << "GlobalValue::DefaultVisibility";
315 case GlobalValue::HiddenVisibility:
316 Out << "GlobalValue::HiddenVisibility";
318 case GlobalValue::ProtectedVisibility:
319 Out << "GlobalValue::ProtectedVisibility";
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 != '\\') {
333 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
334 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
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) + ")";
348 case Type::FloatTyID: return "Type::FloatTy";
349 case Type::DoubleTyID: return "Type::DoubleTy";
350 case Type::LabelTyID: return "Type::LabelTy";
352 error("Invalid primitive type");
355 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
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())
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
375 // See if the type has a name in the symboltable and build accordingly
376 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
379 name = std::string(prefix) + *tName;
381 name = std::string(prefix) + utostr(uniqueNum++);
385 return TypeNames[Ty] = name;
388 void CppWriter::printCppName(const Type* Ty) {
389 printEscapedString(getCppName(Ty));
392 std::string CppWriter::getCppName(const Value* val) {
394 ValueMap::iterator I = ValueNames.find(val);
395 if (I != ValueNames.end() && I->first == val)
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)) {
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;
416 name = getTypePrefix(val->getType());
419 name = getTypePrefix(val->getType());
421 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
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;
430 void CppWriter::printCppName(const Value* val) {
431 printEscapedString(getCppName(val));
434 void CppWriter::printParamAttrs(const PAListPtr &PAL,
435 const std::string &name) {
436 Out << "PAListPtr " << name << "_PAL;";
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;
445 Out << "PAWI.Index = " << index << "; PAWI.Attrs = 0 ";
446 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";
470 Out << "Attrs.push_back(PAWI);";
473 Out << name << "_PAL = PAListPtr::get(Attrs.begin(), Attrs.end());";
480 bool CppWriter::printTypeInternal(const Type* Ty) {
481 // We don't print definitions for primitive types
482 if (Ty->isPrimitiveType() || Ty->isInteger())
485 // If we already defined this type, we don't need to define it again.
486 if (DefinedTypes.find(Ty) != DefinedTypes.end())
489 // Everything below needs the name for the type so get it now.
490 std::string typeName(getCppName(Ty));
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(),
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();";
504 UnresolvedTypes[Ty] = typeName;
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);
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;";
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;
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;
539 nl(Out) << "/*Params=*/" << typeName << "_args,";
540 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
545 case Type::StructTyID: {
546 const StructType* ST = cast<StructType>(Ty);
547 Out << "std::vector<const Type*>" << typeName << "_fields;";
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;
561 Out << "StructType* " << typeName << " = StructType::get("
562 << typeName << "_fields, /*isPacked=*/"
563 << (ST->isPacked() ? "true" : "false") << ");";
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()) << ");";
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()) << ");";
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()) << ");";
600 case Type::OpaqueTyID: {
601 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
606 error("Invalid TypeID");
609 // If the type had a name, make sure we recreate it.
610 const std::string* progTypeName =
611 findTypeName(TheModule->getTypeSymbolTable(),Ty);
613 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
618 // Pop us off the type stack
619 TypeStack.pop_back();
621 // Indicate that this type is now defined.
622 DefinedTypes.insert(Ty);
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
628 TypeMap::iterator I = UnresolvedTypes.find(Ty);
629 if (I != UnresolvedTypes.end()) {
630 Out << "cast<OpaqueType>(" << I->second
631 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
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;
643 Out << ">(" << I->second << "_fwd.get());";
645 UnresolvedTypes.erase(I);
648 // Finally, separate the type definition from other with a newline.
651 // We weren't a recursive type
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());
660 printTypeInternal(Ty);
661 assert(TypeStack.empty());
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();
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) << ");";
678 // For everything else, define the type
680 printType(TI->second);
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());
692 // Add all the functions to the table
693 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
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());
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;
709 printType(I->getType());
710 for (unsigned i = 0; i < I->getNumOperands(); ++i)
711 printType(I->getOperand(i)->getType());
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
723 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
726 std::string constName(getCppName(CV));
727 std::string typeName(getCppName(CV->getType()));
728 if (CV->isNullValue()) {
729 Out << "Constant* " << constName << " = Constant::getNullValue("
734 if (isa<GlobalValue>(CV)) {
735 // Skip variables and functions, we emit them elsewhere
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 << " = ";
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;
761 printEscapedString(tmp);
762 // Determine if we want null termination or not.
764 Out << "\", true"; // Indicate that the null terminator should be
767 Out << "\", false";// No null terminator
770 Out << "std::vector<Constant*> " << constName << "_elems;";
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)) << ");";
779 Out << "Constant* " << constName << " = ConstantArray::get("
780 << typeName << ", " << constName << "_elems);";
782 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
783 Out << "std::vector<Constant*> " << constName << "_fields;";
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)) << ");";
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;";
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)) << ");";
804 Out << "Constant* " << constName << " = ConstantVector::get("
805 << typeName << ", " << constName << "_elems);";
806 } else if (isa<UndefValue>(CV)) {
807 Out << "UndefValue* " << constName << " = UndefValue::get("
809 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
810 if (CE->getOpcode() == Instruction::GetElementPtr) {
811 Out << "std::vector<Constant*> " << constName << "_indices;";
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)) << ");";
820 Out << "Constant* " << constName
821 << " = ConstantExpr::getGetElementPtr("
822 << getCppName(CE->getOperand(0)) << ", "
823 << "&" << constName << "_indices[0], "
824 << constName << "_indices.size()"
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;
844 Out << ", " << getCppName(CE->getOperand(0)) << ", "
845 << getCppName(CE->getType()) << ");";
847 unsigned N = CE->getNumOperands();
848 for (unsigned i = 0; i < N; ++i ) {
849 printConstant(CE->getOperand(i));
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");
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");
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;
911 error("Invalid constant expression");
914 Out << getCppName(CE->getOperand(0));
915 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
916 Out << ", " << getCppName(CE->getOperand(i));
920 error("Bad Constant");
921 Out << "Constant* " << constName << " = 0; ";
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());
933 // Traverse the LLVM functions looking for constants
934 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
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;
941 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
942 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
951 void CppWriter::printVariableUses(const GlobalVariable *GV) {
952 nl(Out) << "// Type Definitions";
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);
965 nl(Out) << "// Constant Definitions"; nl(Out);
968 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
969 nl(Out) << "// Global Variable Definitions"; nl(Out);
970 printVariableBody(gv);
975 void CppWriter::printVariableHead(const GlobalVariable *GV) {
976 nl(Out) << "GlobalVariable* " << getCppName(GV);
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);
984 Out << " = new GlobalVariable(";
985 nl(Out) << "/*Type=*/";
986 printCppName(GV->getType()->getElementType());
988 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
990 nl(Out) << "/*Linkage=*/";
991 printLinkageType(GV->getLinkage());
993 nl(Out) << "/*Initializer=*/0, ";
994 if (GV->hasInitializer()) {
995 Out << "// has initializer, specified below";
997 nl(Out) << "/*Name=*/\"";
998 printEscapedString(GV->getName());
1003 if (GV->hasSection()) {
1005 Out << "->setSection(\"";
1006 printEscapedString(GV->getSection());
1010 if (GV->getAlignment()) {
1012 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
1015 if (GV->getVisibility() != GlobalValue::DefaultVisibility) {
1017 Out << "->setVisibility(";
1018 printVisibilityType(GV->getVisibility());
1023 out(); Out << "}"; nl(Out);
1027 void CppWriter::printVariableBody(const GlobalVariable *GV) {
1028 if (GV->hasInitializer()) {
1030 Out << "->setInitializer(";
1031 Out << getCppName(GV->getInitializer()) << ");";
1036 std::string CppWriter::getOpName(Value* V) {
1037 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
1038 return getCppName(V);
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())
1046 // This is a new forward reference. Generate a unique name for it
1047 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
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()) << ");";
1055 ForwardRefs[V] = result;
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));
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));
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 << ");";
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] << ", ";
1086 } else if (br->getNumOperands() == 1) {
1087 Out << opNames[0] << ", ";
1089 error("Branch with 2 operands?");
1091 Out << bbname << ");";
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 << ");";
1101 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
1102 Out << iName << "->addCase("
1103 << opNames[i] << ", "
1104 << opNames[i+1] << ");";
1109 case Instruction::Invoke: {
1110 const InvokeInst* inv = cast<InvokeInst>(I);
1111 Out << "std::vector<Value*> " << iName << "_params;";
1113 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1114 Out << iName << "_params.push_back("
1115 << opNames[i] << ");";
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());
1128 printParamAttrs(inv->getParamAttrs(), iName);
1129 Out << iName << "->setParamAttrs(" << iName << "_PAL);";
1133 case Instruction::Unwind: {
1134 Out << "new UnwindInst("
1138 case Instruction::Unreachable:{
1139 Out << "new UnreachableInst("
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;
1177 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1178 printEscapedString(I->getName());
1179 Out << "\", " << bbname << ");";
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;
1203 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1204 printEscapedString(I->getName());
1205 Out << "\", " << bbname << ");";
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;
1223 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1224 printEscapedString(I->getName());
1225 Out << "\", " << bbname << ");";
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] << ", " ;
1235 printEscapedString(mallocI->getName());
1236 Out << "\", " << bbname << ");";
1237 if (mallocI->getAlignment())
1238 nl(Out) << iName << "->setAlignment("
1239 << mallocI->getAlignment() << ");";
1242 case Instruction::Free: {
1243 Out << "FreeInst* " << iName << " = new FreeInst("
1244 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
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] << ", ";
1254 printEscapedString(allocaI->getName());
1255 Out << "\", " << bbname << ");";
1256 if (allocaI->getAlignment())
1257 nl(Out) << iName << "->setAlignment("
1258 << allocaI->getAlignment() << ");";
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 << ");";
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 << ");";
1279 case Instruction::GetElementPtr: {
1280 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1281 if (gep->getNumOperands() <= 2) {
1282 Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
1284 if (gep->getNumOperands() == 2)
1285 Out << ", " << opNames[1];
1287 Out << "std::vector<Value*> " << iName << "_indices;";
1289 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
1290 Out << iName << "_indices.push_back("
1291 << opNames[i] << ");";
1294 Out << "Instruction* " << iName << " = GetElementPtrInst::Create("
1295 << opNames[0] << ", " << iName << "_indices.begin(), "
1296 << iName << "_indices.end()";
1299 printEscapedString(gep->getName());
1300 Out << "\", " << bbname << ");";
1303 case Instruction::PHI: {
1304 const PHINode* phi = cast<PHINode>(I);
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()
1314 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
1315 Out << iName << "->addIncoming("
1316 << opNames[i] << ", " << opNames[i+1] << ");";
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;
1350 Out << "(" << opNames[0] << ", "
1351 << getCppName(cst->getType()) << ", \"";
1352 printEscapedString(cst->getName());
1353 Out << "\", " << bbname << ");";
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") << ");";
1366 if (call->getNumOperands() > 2) {
1367 Out << "std::vector<Value*> " << iName << "_params;";
1369 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1370 Out << iName << "_params.push_back(" << opNames[i] << ");";
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] << ", \"";
1380 Out << "CallInst* " << iName << " = CallInst::Create(" << opNames[0]
1383 printEscapedString(call->getName());
1384 Out << "\", " << bbname << ");";
1385 nl(Out) << iName << "->setCallingConv(";
1386 printCallingConv(call->getCallingConv());
1388 nl(Out) << iName << "->setTailCall("
1389 << (call->isTailCall() ? "true":"false");
1391 printParamAttrs(call->getParamAttrs(), iName);
1392 Out << iName << "->setParamAttrs(" << iName << "_PAL);";
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 << ");";
1404 case Instruction::UserOp1:
1406 case Instruction::UserOp2: {
1407 /// FIXME: What should be done here?
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 << ");";
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 << ");";
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 << ");";
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 << ");";
1446 DefinedValues.insert(I);
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);
1455 // Print the function's return type
1456 printType(F->getReturnType());
1458 // Print the function's function type
1459 printType(F->getFunctionType());
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();
1464 printType(AI->getType());
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();
1474 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1476 // Print the type of the instruction itself
1477 printType(I->getType());
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());
1484 // If the operand references a GVal or Constant, make a note of it
1485 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
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))
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();
1500 if (Function* Fun = dyn_cast<Function>(*I)) {
1501 if (!is_inline || Fun != F)
1502 printFunctionHead(Fun);
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();
1510 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1511 printVariableHead(F);
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) {
1521 // Process the global variables definitions now that all the constants have
1522 // been emitted. These definitions just couple the gvars with their constant
1524 nl(Out) << "// Global Variable Definitions"; nl(Out);
1525 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1527 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1528 printVariableBody(GV);
1532 void CppWriter::printFunctionHead(const Function* F) {
1533 nl(Out) << "Function* " << getCppName(F);
1535 Out << " = mod->getFunction(\"";
1536 printEscapedString(F->getName());
1537 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1538 nl(Out) << "if (!" << getCppName(F) << ") {";
1539 nl(Out) << getCppName(F);
1541 Out<< " = Function::Create(";
1542 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1543 nl(Out) << "/*Linkage=*/";
1544 printLinkageType(F->getLinkage());
1546 nl(Out) << "/*Name=*/\"";
1547 printEscapedString(F->getName());
1548 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
1551 Out << "->setCallingConv(";
1552 printCallingConv(F->getCallingConv());
1555 if (F->hasSection()) {
1557 Out << "->setSection(\"" << F->getSection() << "\");";
1560 if (F->getAlignment()) {
1562 Out << "->setAlignment(" << F->getAlignment() << ");";
1565 if (F->getVisibility() != GlobalValue::DefaultVisibility) {
1567 Out << "->setVisibility(";
1568 printVisibilityType(F->getVisibility());
1572 if (F->hasCollector()) {
1574 Out << "->setCollector(\"" << F->getCollector() << "\");";
1581 printParamAttrs(F->getParamAttrs(), getCppName(F));
1583 Out << "->setParamAttrs(" << getCppName(F) << "_PAL);";
1587 void CppWriter::printFunctionBody(const Function *F) {
1588 if (F->isDeclaration())
1589 return; // external functions have no bodies.
1591 // Clear the DefinedValues and ForwardRefs maps because we can't have
1592 // cross-function forward refs
1593 ForwardRefs.clear();
1594 DefinedValues.clear();
1596 // Create all the argument values
1598 if (!F->arg_empty()) {
1599 Out << "Function::arg_iterator args = " << getCppName(F)
1600 << "->arg_begin();";
1603 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1605 Out << "Value* " << getCppName(AI) << " = args++;";
1607 if (AI->hasName()) {
1608 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1614 // Create all the basic blocks
1616 for (Function::const_iterator BI = F->begin(), BE = F->end();
1618 std::string bbname(getCppName(BI));
1619 Out << "BasicBlock* " << bbname << " = BasicBlock::Create(\"";
1621 printEscapedString(BI->getName());
1622 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1626 // Output all of its basic blocks... for the function
1627 for (Function::const_iterator BI = F->begin(), BE = F->end();
1629 std::string bbname(getCppName(BI));
1630 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1633 // Output all of the instructions in the basic block...
1634 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1636 printInstruction(I,bbname);
1640 // Loop over the ForwardRefs and resolve them now that all instructions
1642 if (!ForwardRefs.empty()) {
1643 nl(Out) << "// Resolve Forward References";
1647 while (!ForwardRefs.empty()) {
1648 ForwardRefMap::iterator I = ForwardRefs.begin();
1649 Out << I->second << "->replaceAllUsesWith("
1650 << getCppName(I->first) << "); delete " << I->second << ";";
1652 ForwardRefs.erase(I);
1656 void CppWriter::printInline(const std::string& fname,
1657 const std::string& func) {
1658 const Function* F = TheModule->getFunction(func);
1660 error(std::string("Function '") + func + "' not found in input module");
1663 if (F->isDeclaration()) {
1664 error(std::string("Function '") + func + "' is external!");
1667 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
1669 unsigned arg_count = 1;
1670 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1672 Out << ", Value* arg_" << arg_count;
1677 printFunctionUses(F);
1678 printFunctionBody(F);
1680 Out << "return " << getCppName(F->begin()) << ";";
1685 void CppWriter::printModuleBody() {
1686 // Print out all the type definitions
1687 nl(Out) << "// Type Definitions"; nl(Out);
1688 printTypes(TheModule);
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();
1695 printFunctionHead(I);
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);
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);
1711 // Process the global variables definitions now that all the constants have
1712 // been emitted. These definitions just couple the gvars with their constant
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);
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();
1724 if (!I->isDeclaration()) {
1725 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1729 printFunctionBody(I);
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";
1750 Out << "#include <llvm/ADT/SmallVector.h>\n";
1751 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";
1767 printModule(fname,mName);
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() << "\");";
1778 if (!TheModule->getTargetTriple().empty()) {
1779 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1783 if (!TheModule->getModuleInlineAsm().empty()) {
1784 nl(Out) << "mod->setModuleInlineAsm(\"";
1785 printEscapedString(TheModule->getModuleInlineAsm());
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();
1794 Out << "mod->addLibrary(\"" << *LI << "\");";
1799 nl(Out) << "return mod;";
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";
1809 Out << "\nreturn mod;\n";
1813 void CppWriter::printFunction(const std::string& fname,
1814 const std::string& funcName) {
1815 const Function* F = TheModule->getFunction(funcName);
1817 error(std::string("Function '") + funcName + "' not found in input module");
1820 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1821 printFunctionUses(F);
1822 printFunctionHead(F);
1823 printFunctionBody(F);
1824 Out << "return " << getCppName(F) << ";\n";
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();
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());
1843 void CppWriter::printVariable(const std::string& fname,
1844 const std::string& varName) {
1845 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1848 error(std::string("Variable '") + varName + "' not found in input module");
1851 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1852 printVariableUses(GV);
1853 printVariableHead(GV);
1854 printVariableBody(GV);
1855 Out << "return " << getCppName(GV) << ";\n";
1859 void CppWriter::printType(const std::string& fname,
1860 const std::string& typeName) {
1861 const Type* Ty = TheModule->getTypeByName(typeName);
1863 error(std::string("Type '") + typeName + "' not found in input module");
1866 Out << "\nType* " << fname << "(Module *mod) {\n";
1868 Out << "return " << getCppName(Ty) << ";\n";
1872 bool CppWriter::runOnModule(Module &M) {
1876 Out << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
1878 // Get the name of the function we're supposed to generate
1879 std::string fname = FuncName.getValue();
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>";
1891 tgtname = M.getModuleIdentifier();
1893 } else if (tgtname == "!bad!")
1894 error("You must use the -for option with -gen-{function,variable,type}");
1896 switch (WhatToGenerate(GenerationType)) {
1899 fname = "makeLLVMModule";
1900 printProgram(fname,tgtname);
1904 fname = "makeLLVMModule";
1905 printModule(fname,tgtname);
1909 fname = "makeLLVMModuleContents";
1910 printContents(fname,tgtname);
1914 fname = "makeLLVMFunction";
1915 printFunction(fname,tgtname);
1922 fname = "makeLLVMInline";
1923 printInline(fname,tgtname);
1927 fname = "makeLLVMVariable";
1928 printVariable(fname,tgtname);
1932 fname = "makeLLVMType";
1933 printType(fname,tgtname);
1936 error("Invalid generation option");
1943 char CppWriter::ID = 0;
1945 //===----------------------------------------------------------------------===//
1946 // External Interface declaration
1947 //===----------------------------------------------------------------------===//
1949 bool CPPTargetMachine::addPassesToEmitWholeFile(PassManager &PM,
1951 CodeGenFileType FileType,
1953 if (FileType != TargetMachine::AssemblyFile) return true;
1954 PM.add(new CppWriter(o));