1 //===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source 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 "llvm/CallingConv.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/InlineAsm.h"
19 #include "llvm/Instruction.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/ParameterAttributes.h"
22 #include "llvm/Module.h"
23 #include "llvm/TypeSymbolTable.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/CFG.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Config/config.h"
38 static cl::opt<std::string>
39 FuncName("funcname", cl::desc("Specify the name of the generated function"),
40 cl::value_desc("function name"));
53 static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
54 cl::desc("Choose what kind of output to generate"),
57 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
58 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
59 clEnumValN(GenContents, "gen-contents", "Generate contents of a module"),
60 clEnumValN(GenFunction, "gen-function", "Generate a function definition"),
61 clEnumValN(GenFunctions,"gen-functions", "Generate all function definitions"),
62 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
63 clEnumValN(GenVariable, "gen-variable", "Generate a variable definition"),
64 clEnumValN(GenType, "gen-type", "Generate a type definition"),
69 static cl::opt<std::string> NameToGenerate("for", cl::Optional,
70 cl::desc("Specify the name of the thing to generate"),
74 typedef std::vector<const Type*> TypeList;
75 typedef std::map<const Type*,std::string> TypeMap;
76 typedef std::map<const Value*,std::string> ValueMap;
77 typedef std::set<std::string> NameSet;
78 typedef std::set<const Type*> TypeSet;
79 typedef std::set<const Value*> ValueSet;
80 typedef std::map<const Value*,std::string> ForwardRefMap;
85 const Module *TheModule;
89 TypeMap UnresolvedTypes;
93 ValueSet DefinedValues;
94 ForwardRefMap ForwardRefs;
98 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
99 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
100 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
102 const Module* getModule() { return TheModule; }
104 void printProgram(const std::string& fname, const std::string& modName );
105 void printModule(const std::string& fname, const std::string& modName );
106 void printContents(const std::string& fname, const std::string& modName );
107 void printFunction(const std::string& fname, const std::string& funcName );
108 void printFunctions();
109 void printInline(const std::string& fname, const std::string& funcName );
110 void printVariable(const std::string& fname, const std::string& varName );
111 void printType(const std::string& fname, const std::string& typeName );
113 void error(const std::string& msg);
116 void printLinkageType(GlobalValue::LinkageTypes LT);
117 void printCallingConv(unsigned cc);
118 void printEscapedString(const std::string& str);
119 void printCFP(const ConstantFP* CFP);
121 std::string getCppName(const Type* val);
122 inline void printCppName(const Type* val);
124 std::string getCppName(const Value* val);
125 inline void printCppName(const Value* val);
127 bool printTypeInternal(const Type* Ty);
128 inline void printType(const Type* Ty);
129 void printTypes(const Module* M);
131 void printConstant(const Constant *CPV);
132 void printConstants(const Module* M);
134 void printVariableUses(const GlobalVariable *GV);
135 void printVariableHead(const GlobalVariable *GV);
136 void printVariableBody(const GlobalVariable *GV);
138 void printFunctionUses(const Function *F);
139 void printFunctionHead(const Function *F);
140 void printFunctionBody(const Function *F);
141 void printInstruction(const Instruction *I, const std::string& bbname);
142 std::string getOpName(Value*);
144 void printModuleBody();
148 static unsigned indent_level = 0;
149 inline std::ostream& nl(std::ostream& Out, int delta = 0) {
151 if (delta >= 0 || indent_level >= unsigned(-delta))
152 indent_level += delta;
153 for (unsigned i = 0; i < indent_level; ++i)
158 inline void in() { indent_level++; }
159 inline void out() { if (indent_level >0) indent_level--; }
162 sanitize(std::string& str) {
163 for (size_t i = 0; i < str.length(); ++i)
164 if (!isalnum(str[i]) && str[i] != '_')
169 getTypePrefix(const Type* Ty ) {
170 switch (Ty->getTypeID()) {
171 case Type::VoidTyID: return "void_";
172 case Type::IntegerTyID:
173 return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
175 case Type::FloatTyID: return "float_";
176 case Type::DoubleTyID: return "double_";
177 case Type::LabelTyID: return "label_";
178 case Type::FunctionTyID: return "func_";
179 case Type::StructTyID: return "struct_";
180 case Type::ArrayTyID: return "array_";
181 case Type::PointerTyID: return "ptr_";
182 case Type::VectorTyID: return "packed_";
183 case Type::OpaqueTyID: return "opaque_";
184 default: return "other_";
189 // Looks up the type in the symbol table and returns a pointer to its name or
190 // a null pointer if it wasn't found. Note that this isn't the same as the
191 // Mode::getTypeName function which will return an empty string, not a null
192 // pointer if the name is not found.
193 inline const std::string*
194 findTypeName(const TypeSymbolTable& ST, const Type* Ty)
196 TypeSymbolTable::const_iterator TI = ST.begin();
197 TypeSymbolTable::const_iterator TE = ST.end();
198 for (;TI != TE; ++TI)
199 if (TI->second == Ty)
205 CppWriter::error(const std::string& msg) {
206 std::cerr << progname << ": " << msg << "\n";
210 // printCFP - Print a floating point constant .. very carefully :)
211 // This makes sure that conversion to/from floating yields the same binary
212 // result so that we don't lose precision.
214 CppWriter::printCFP(const ConstantFP *CFP) {
215 APFloat APF = APFloat(CFP->getValueAPF()); // copy
216 if (CFP->getType() == Type::FloatTy)
217 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
218 Out << "ConstantFP::get(";
219 if (CFP->getType() == Type::DoubleTy)
220 Out << "Type::DoubleTy, ";
222 Out << "Type::FloatTy, ";
226 sprintf(Buffer, "%A", APF.convertToDouble());
227 if ((!strncmp(Buffer, "0x", 2) ||
228 !strncmp(Buffer, "-0x", 3) ||
229 !strncmp(Buffer, "+0x", 3)) &&
230 APF.bitwiseIsEqual(APFloat(atof(Buffer)))) {
231 if (CFP->getType() == Type::DoubleTy)
232 Out << "BitsToDouble(" << Buffer << ")";
234 Out << "BitsToFloat((float)" << Buffer << ")";
238 std::string StrVal = ftostr(CFP->getValueAPF());
240 while (StrVal[0] == ' ')
241 StrVal.erase(StrVal.begin());
243 // Check to make sure that the stringized number is not some string like
244 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
245 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
246 ((StrVal[0] == '-' || StrVal[0] == '+') &&
247 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
248 (CFP->isExactlyValue(atof(StrVal.c_str())))) {
249 if (CFP->getType() == Type::DoubleTy)
252 Out << StrVal << "f";
254 else if (CFP->getType() == Type::DoubleTy)
255 Out << "BitsToDouble(0x" << std::hex
256 << CFP->getValueAPF().convertToAPInt().getZExtValue()
257 << std::dec << "ULL) /* " << StrVal << " */";
259 Out << "BitsToFloat(0x" << std::hex
260 << (uint32_t)CFP->getValueAPF().convertToAPInt().getZExtValue()
261 << std::dec << "U) /* " << StrVal << " */";
270 CppWriter::printCallingConv(unsigned cc){
271 // Print the calling convention.
273 case CallingConv::C: Out << "CallingConv::C"; break;
274 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
275 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
276 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
277 default: Out << cc; break;
282 CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
284 case GlobalValue::InternalLinkage:
285 Out << "GlobalValue::InternalLinkage"; break;
286 case GlobalValue::LinkOnceLinkage:
287 Out << "GlobalValue::LinkOnceLinkage "; break;
288 case GlobalValue::WeakLinkage:
289 Out << "GlobalValue::WeakLinkage"; break;
290 case GlobalValue::AppendingLinkage:
291 Out << "GlobalValue::AppendingLinkage"; break;
292 case GlobalValue::ExternalLinkage:
293 Out << "GlobalValue::ExternalLinkage"; break;
294 case GlobalValue::DLLImportLinkage:
295 Out << "GlobalValue::DLLImportLinkage"; break;
296 case GlobalValue::DLLExportLinkage:
297 Out << "GlobalValue::DLLExportLinkage"; break;
298 case GlobalValue::ExternalWeakLinkage:
299 Out << "GlobalValue::ExternalWeakLinkage"; break;
300 case GlobalValue::GhostLinkage:
301 Out << "GlobalValue::GhostLinkage"; break;
305 // printEscapedString - Print each character of the specified string, escaping
306 // it if it is not printable or if it is an escape char.
308 CppWriter::printEscapedString(const std::string &Str) {
309 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
310 unsigned char C = Str[i];
311 if (isprint(C) && C != '"' && C != '\\') {
315 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
316 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
322 CppWriter::getCppName(const Type* Ty)
324 // First, handle the primitive types .. easy
325 if (Ty->isPrimitiveType() || Ty->isInteger()) {
326 switch (Ty->getTypeID()) {
327 case Type::VoidTyID: return "Type::VoidTy";
328 case Type::IntegerTyID: {
329 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
330 return "IntegerType::get(" + utostr(BitWidth) + ")";
332 case Type::FloatTyID: return "Type::FloatTy";
333 case Type::DoubleTyID: return "Type::DoubleTy";
334 case Type::LabelTyID: return "Type::LabelTy";
336 error("Invalid primitive type");
339 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
342 // Now, see if we've seen the type before and return that
343 TypeMap::iterator I = TypeNames.find(Ty);
344 if (I != TypeNames.end())
347 // Okay, let's build a new name for this type. Start with a prefix
348 const char* prefix = 0;
349 switch (Ty->getTypeID()) {
350 case Type::FunctionTyID: prefix = "FuncTy_"; break;
351 case Type::StructTyID: prefix = "StructTy_"; break;
352 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
353 case Type::PointerTyID: prefix = "PointerTy_"; break;
354 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
355 case Type::VectorTyID: prefix = "VectorTy_"; break;
356 default: prefix = "OtherTy_"; break; // prevent breakage
359 // See if the type has a name in the symboltable and build accordingly
360 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
363 name = std::string(prefix) + *tName;
365 name = std::string(prefix) + utostr(uniqueNum++);
369 return TypeNames[Ty] = name;
373 CppWriter::printCppName(const Type* Ty)
375 printEscapedString(getCppName(Ty));
379 CppWriter::getCppName(const Value* val) {
381 ValueMap::iterator I = ValueNames.find(val);
382 if (I != ValueNames.end() && I->first == val)
385 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
386 name = std::string("gvar_") +
387 getTypePrefix(GV->getType()->getElementType());
388 } else if (isa<Function>(val)) {
389 name = std::string("func_");
390 } else if (const Constant* C = dyn_cast<Constant>(val)) {
391 name = std::string("const_") + getTypePrefix(C->getType());
392 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
394 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
395 Function::const_arg_iterator(Arg)) + 1;
396 name = std::string("arg_") + utostr(argNum);
397 NameSet::iterator NI = UsedNames.find(name);
398 if (NI != UsedNames.end())
399 name += std::string("_") + utostr(uniqueNum++);
400 UsedNames.insert(name);
401 return ValueNames[val] = name;
403 name = getTypePrefix(val->getType());
406 name = getTypePrefix(val->getType());
408 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
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;
418 CppWriter::printCppName(const Value* val) {
419 printEscapedString(getCppName(val));
423 CppWriter::printTypeInternal(const Type* Ty) {
424 // We don't print definitions for primitive types
425 if (Ty->isPrimitiveType() || Ty->isInteger())
428 // If we already defined this type, we don't need to define it again.
429 if (DefinedTypes.find(Ty) != DefinedTypes.end())
432 // Everything below needs the name for the type so get it now.
433 std::string typeName(getCppName(Ty));
435 // Search the type stack for recursion. If we find it, then generate this
436 // as an OpaqueType, but make sure not to do this multiple times because
437 // the type could appear in multiple places on the stack. Once the opaque
438 // definition is issued, it must not be re-issued. Consequently we have to
439 // check the UnresolvedTypes list as well.
440 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
441 if (TI != TypeStack.end()) {
442 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
443 if (I == UnresolvedTypes.end()) {
444 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
446 UnresolvedTypes[Ty] = typeName;
451 // We're going to print a derived type which, by definition, contains other
452 // types. So, push this one we're printing onto the type stack to assist with
453 // recursive definitions.
454 TypeStack.push_back(Ty);
456 // Print the type definition
457 switch (Ty->getTypeID()) {
458 case Type::FunctionTyID: {
459 const FunctionType* FT = cast<FunctionType>(Ty);
460 Out << "std::vector<const Type*>" << typeName << "_args;";
462 FunctionType::param_iterator PI = FT->param_begin();
463 FunctionType::param_iterator PE = FT->param_end();
464 for (; PI != PE; ++PI) {
465 const Type* argTy = static_cast<const Type*>(*PI);
466 bool isForward = printTypeInternal(argTy);
467 std::string argName(getCppName(argTy));
468 Out << typeName << "_args.push_back(" << argName;
474 const ParamAttrsList *PAL = FT->getParamAttrs();
475 Out << "ParamAttrsList *" << typeName << "_PAL = 0;";
478 Out << '{'; in(); nl(Out);
479 Out << "ParamAttrsVector Attrs;"; nl(Out);
480 Out << "ParamAttrsWithIndex PAWI;"; nl(Out);
481 for (unsigned i = 0; i < PAL->size(); ++i) {
482 uint16_t index = PAL->getParamIndex(i);
483 uint16_t attrs = PAL->getParamAttrs(index);
484 Out << "PAWI.index = " << index << "; PAWI.attrs = 0 ";
485 if (attrs & ParamAttr::SExt)
486 Out << " | ParamAttr::SExt";
487 if (attrs & ParamAttr::ZExt)
488 Out << " | ParamAttr::ZExt";
489 if (attrs & ParamAttr::NoAlias)
490 Out << " | ParamAttr::NoAlias";
491 if (attrs & ParamAttr::StructRet)
492 Out << " | ParamAttr::StructRet";
493 if (attrs & ParamAttr::InReg)
494 Out << " | ParamAttr::InReg";
495 if (attrs & ParamAttr::NoReturn)
496 Out << " | ParamAttr::NoReturn";
497 if (attrs & ParamAttr::NoUnwind)
498 Out << " | ParamAttr::NoUnwind";
501 Out << "Attrs.push_back(PAWI);";
504 Out << typeName << "_PAL = ParamAttrsList::get(Attrs);";
509 bool isForward = printTypeInternal(FT->getReturnType());
510 std::string retTypeName(getCppName(FT->getReturnType()));
511 Out << "FunctionType* " << typeName << " = FunctionType::get(";
512 in(); nl(Out) << "/*Result=*/" << retTypeName;
516 nl(Out) << "/*Params=*/" << typeName << "_args,";
517 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true," : "false,") ;
518 nl(Out) << "/*ParamAttrs=*/" << typeName << "_PAL" << ");";
523 case Type::StructTyID: {
524 const StructType* ST = cast<StructType>(Ty);
525 Out << "std::vector<const Type*>" << typeName << "_fields;";
527 StructType::element_iterator EI = ST->element_begin();
528 StructType::element_iterator EE = ST->element_end();
529 for (; EI != EE; ++EI) {
530 const Type* fieldTy = static_cast<const Type*>(*EI);
531 bool isForward = printTypeInternal(fieldTy);
532 std::string fieldName(getCppName(fieldTy));
533 Out << typeName << "_fields.push_back(" << fieldName;
539 Out << "StructType* " << typeName << " = StructType::get("
540 << typeName << "_fields, /*isPacked=*/"
541 << (ST->isPacked() ? "true" : "false") << ");";
545 case Type::ArrayTyID: {
546 const ArrayType* AT = cast<ArrayType>(Ty);
547 const Type* ET = AT->getElementType();
548 bool isForward = printTypeInternal(ET);
549 std::string elemName(getCppName(ET));
550 Out << "ArrayType* " << typeName << " = ArrayType::get("
551 << elemName << (isForward ? "_fwd" : "")
552 << ", " << utostr(AT->getNumElements()) << ");";
556 case Type::PointerTyID: {
557 const PointerType* PT = cast<PointerType>(Ty);
558 const Type* ET = PT->getElementType();
559 bool isForward = printTypeInternal(ET);
560 std::string elemName(getCppName(ET));
561 Out << "PointerType* " << typeName << " = PointerType::get("
562 << elemName << (isForward ? "_fwd" : "") << ");";
566 case Type::VectorTyID: {
567 const VectorType* PT = cast<VectorType>(Ty);
568 const Type* ET = PT->getElementType();
569 bool isForward = printTypeInternal(ET);
570 std::string elemName(getCppName(ET));
571 Out << "VectorType* " << typeName << " = VectorType::get("
572 << elemName << (isForward ? "_fwd" : "")
573 << ", " << utostr(PT->getNumElements()) << ");";
577 case Type::OpaqueTyID: {
578 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
583 error("Invalid TypeID");
586 // If the type had a name, make sure we recreate it.
587 const std::string* progTypeName =
588 findTypeName(TheModule->getTypeSymbolTable(),Ty);
590 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
595 // Pop us off the type stack
596 TypeStack.pop_back();
598 // Indicate that this type is now defined.
599 DefinedTypes.insert(Ty);
601 // Early resolve as many unresolved types as possible. Search the unresolved
602 // types map for the type we just printed. Now that its definition is complete
603 // we can resolve any previous references to it. This prevents a cascade of
605 TypeMap::iterator I = UnresolvedTypes.find(Ty);
606 if (I != UnresolvedTypes.end()) {
607 Out << "cast<OpaqueType>(" << I->second
608 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
610 Out << I->second << " = cast<";
611 switch (Ty->getTypeID()) {
612 case Type::FunctionTyID: Out << "FunctionType"; break;
613 case Type::ArrayTyID: Out << "ArrayType"; break;
614 case Type::StructTyID: Out << "StructType"; break;
615 case Type::VectorTyID: Out << "VectorType"; break;
616 case Type::PointerTyID: Out << "PointerType"; break;
617 case Type::OpaqueTyID: Out << "OpaqueType"; break;
618 default: Out << "NoSuchDerivedType"; break;
620 Out << ">(" << I->second << "_fwd.get());";
622 UnresolvedTypes.erase(I);
625 // Finally, separate the type definition from other with a newline.
628 // We weren't a recursive type
632 // Prints a type definition. Returns true if it could not resolve all the types
633 // in the definition but had to use a forward reference.
635 CppWriter::printType(const Type* Ty) {
636 assert(TypeStack.empty());
638 printTypeInternal(Ty);
639 assert(TypeStack.empty());
643 CppWriter::printTypes(const Module* M) {
645 // Walk the symbol table and print out all its types
646 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
647 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
650 // For primitive types and types already defined, just add a name
651 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
652 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
653 TNI != TypeNames.end()) {
654 Out << "mod->addTypeName(\"";
655 printEscapedString(TI->first);
656 Out << "\", " << getCppName(TI->second) << ");";
658 // For everything else, define the type
660 printType(TI->second);
664 // Add all of the global variables to the value table...
665 for (Module::const_global_iterator I = TheModule->global_begin(),
666 E = TheModule->global_end(); I != E; ++I) {
667 if (I->hasInitializer())
668 printType(I->getInitializer()->getType());
669 printType(I->getType());
672 // Add all the functions to the table
673 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
675 printType(FI->getReturnType());
676 printType(FI->getFunctionType());
677 // Add all the function arguments
678 for(Function::const_arg_iterator AI = FI->arg_begin(),
679 AE = FI->arg_end(); AI != AE; ++AI) {
680 printType(AI->getType());
683 // Add all of the basic blocks and instructions
684 for (Function::const_iterator BB = FI->begin(),
685 E = FI->end(); BB != E; ++BB) {
686 printType(BB->getType());
687 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
689 printType(I->getType());
690 for (unsigned i = 0; i < I->getNumOperands(); ++i)
691 printType(I->getOperand(i)->getType());
698 // printConstant - Print out a constant pool entry...
699 void CppWriter::printConstant(const Constant *CV) {
700 // First, if the constant is actually a GlobalValue (variable or function) or
701 // its already in the constant list then we've printed it already and we can
703 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
706 std::string constName(getCppName(CV));
707 std::string typeName(getCppName(CV->getType()));
708 if (CV->isNullValue()) {
709 Out << "Constant* " << constName << " = Constant::getNullValue("
714 if (isa<GlobalValue>(CV)) {
715 // Skip variables and functions, we emit them elsewhere
718 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
719 Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
720 << cast<IntegerType>(CI->getType())->getBitWidth() << ", "
721 << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
722 } else if (isa<ConstantAggregateZero>(CV)) {
723 Out << "ConstantAggregateZero* " << constName
724 << " = ConstantAggregateZero::get(" << typeName << ");";
725 } else if (isa<ConstantPointerNull>(CV)) {
726 Out << "ConstantPointerNull* " << constName
727 << " = ConstanPointerNull::get(" << typeName << ");";
728 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
729 Out << "ConstantFP* " << constName << " = ";
732 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
733 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
734 Out << "Constant* " << constName << " = ConstantArray::get(\"";
735 std::string tmp = CA->getAsString();
736 bool nullTerminate = false;
737 if (tmp[tmp.length()-1] == 0) {
738 tmp.erase(tmp.length()-1);
739 nullTerminate = true;
741 printEscapedString(tmp);
742 // Determine if we want null termination or not.
744 Out << "\", true"; // Indicate that the null terminator should be added.
746 Out << "\", false";// No null terminator
749 Out << "std::vector<Constant*> " << constName << "_elems;";
751 unsigned N = CA->getNumOperands();
752 for (unsigned i = 0; i < N; ++i) {
753 printConstant(CA->getOperand(i)); // recurse to print operands
754 Out << constName << "_elems.push_back("
755 << getCppName(CA->getOperand(i)) << ");";
758 Out << "Constant* " << constName << " = ConstantArray::get("
759 << typeName << ", " << constName << "_elems);";
761 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
762 Out << "std::vector<Constant*> " << constName << "_fields;";
764 unsigned N = CS->getNumOperands();
765 for (unsigned i = 0; i < N; i++) {
766 printConstant(CS->getOperand(i));
767 Out << constName << "_fields.push_back("
768 << getCppName(CS->getOperand(i)) << ");";
771 Out << "Constant* " << constName << " = ConstantStruct::get("
772 << typeName << ", " << constName << "_fields);";
773 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
774 Out << "std::vector<Constant*> " << constName << "_elems;";
776 unsigned N = CP->getNumOperands();
777 for (unsigned i = 0; i < N; ++i) {
778 printConstant(CP->getOperand(i));
779 Out << constName << "_elems.push_back("
780 << getCppName(CP->getOperand(i)) << ");";
783 Out << "Constant* " << constName << " = ConstantVector::get("
784 << typeName << ", " << constName << "_elems);";
785 } else if (isa<UndefValue>(CV)) {
786 Out << "UndefValue* " << constName << " = UndefValue::get("
788 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
789 if (CE->getOpcode() == Instruction::GetElementPtr) {
790 Out << "std::vector<Constant*> " << constName << "_indices;";
792 printConstant(CE->getOperand(0));
793 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
794 printConstant(CE->getOperand(i));
795 Out << constName << "_indices.push_back("
796 << getCppName(CE->getOperand(i)) << ");";
799 Out << "Constant* " << constName
800 << " = ConstantExpr::getGetElementPtr("
801 << getCppName(CE->getOperand(0)) << ", "
802 << "&" << constName << "_indices[0], "
803 << constName << "_indices.size()"
805 } else if (CE->isCast()) {
806 printConstant(CE->getOperand(0));
807 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
808 switch (CE->getOpcode()) {
809 default: assert(0 && "Invalid cast opcode");
810 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
811 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
812 case Instruction::SExt: Out << "Instruction::SExt"; break;
813 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
814 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
815 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
816 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
817 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
818 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
819 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
820 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
821 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
823 Out << ", " << getCppName(CE->getOperand(0)) << ", "
824 << getCppName(CE->getType()) << ");";
826 unsigned N = CE->getNumOperands();
827 for (unsigned i = 0; i < N; ++i ) {
828 printConstant(CE->getOperand(i));
830 Out << "Constant* " << constName << " = ConstantExpr::";
831 switch (CE->getOpcode()) {
832 case Instruction::Add: Out << "getAdd("; break;
833 case Instruction::Sub: Out << "getSub("; break;
834 case Instruction::Mul: Out << "getMul("; break;
835 case Instruction::UDiv: Out << "getUDiv("; break;
836 case Instruction::SDiv: Out << "getSDiv("; break;
837 case Instruction::FDiv: Out << "getFDiv("; break;
838 case Instruction::URem: Out << "getURem("; break;
839 case Instruction::SRem: Out << "getSRem("; break;
840 case Instruction::FRem: Out << "getFRem("; break;
841 case Instruction::And: Out << "getAnd("; break;
842 case Instruction::Or: Out << "getOr("; break;
843 case Instruction::Xor: Out << "getXor("; break;
844 case Instruction::ICmp:
845 Out << "getICmp(ICmpInst::ICMP_";
846 switch (CE->getPredicate()) {
847 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
848 case ICmpInst::ICMP_NE: Out << "NE"; break;
849 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
850 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
851 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
852 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
853 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
854 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
855 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
856 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
857 default: error("Invalid ICmp Predicate");
860 case Instruction::FCmp:
861 Out << "getFCmp(FCmpInst::FCMP_";
862 switch (CE->getPredicate()) {
863 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
864 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
865 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
866 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
867 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
868 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
869 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
870 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
871 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
872 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
873 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
874 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
875 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
876 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
877 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
878 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
879 default: error("Invalid FCmp Predicate");
882 case Instruction::Shl: Out << "getShl("; break;
883 case Instruction::LShr: Out << "getLShr("; break;
884 case Instruction::AShr: Out << "getAShr("; break;
885 case Instruction::Select: Out << "getSelect("; break;
886 case Instruction::ExtractElement: Out << "getExtractElement("; break;
887 case Instruction::InsertElement: Out << "getInsertElement("; break;
888 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
890 error("Invalid constant expression");
893 Out << getCppName(CE->getOperand(0));
894 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
895 Out << ", " << getCppName(CE->getOperand(i));
899 error("Bad Constant");
900 Out << "Constant* " << constName << " = 0; ";
906 CppWriter::printConstants(const Module* M) {
907 // Traverse all the global variables looking for constant initializers
908 for (Module::const_global_iterator I = TheModule->global_begin(),
909 E = TheModule->global_end(); I != E; ++I)
910 if (I->hasInitializer())
911 printConstant(I->getInitializer());
913 // Traverse the LLVM functions looking for constants
914 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
916 // Add all of the basic blocks and instructions
917 for (Function::const_iterator BB = FI->begin(),
918 E = FI->end(); BB != E; ++BB) {
919 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
921 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
922 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
931 void CppWriter::printVariableUses(const GlobalVariable *GV) {
932 nl(Out) << "// Type Definitions";
934 printType(GV->getType());
935 if (GV->hasInitializer()) {
936 Constant* Init = GV->getInitializer();
937 printType(Init->getType());
938 if (Function* F = dyn_cast<Function>(Init)) {
939 nl(Out)<< "/ Function Declarations"; nl(Out);
940 printFunctionHead(F);
941 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
942 nl(Out) << "// Global Variable Declarations"; nl(Out);
943 printVariableHead(gv);
945 nl(Out) << "// Constant Definitions"; nl(Out);
948 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
949 nl(Out) << "// Global Variable Definitions"; nl(Out);
950 printVariableBody(gv);
955 void CppWriter::printVariableHead(const GlobalVariable *GV) {
956 nl(Out) << "GlobalVariable* " << getCppName(GV);
958 Out << " = mod->getGlobalVariable(";
959 printEscapedString(GV->getName());
960 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
961 nl(Out) << "if (!" << getCppName(GV) << ") {";
962 in(); nl(Out) << getCppName(GV);
964 Out << " = new GlobalVariable(";
965 nl(Out) << "/*Type=*/";
966 printCppName(GV->getType()->getElementType());
968 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
970 nl(Out) << "/*Linkage=*/";
971 printLinkageType(GV->getLinkage());
973 nl(Out) << "/*Initializer=*/0, ";
974 if (GV->hasInitializer()) {
975 Out << "// has initializer, specified below";
977 nl(Out) << "/*Name=*/\"";
978 printEscapedString(GV->getName());
983 if (GV->hasSection()) {
985 Out << "->setSection(\"";
986 printEscapedString(GV->getSection());
990 if (GV->getAlignment()) {
992 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
996 out(); Out << "}"; nl(Out);
1001 CppWriter::printVariableBody(const GlobalVariable *GV) {
1002 if (GV->hasInitializer()) {
1004 Out << "->setInitializer(";
1005 //if (!isa<GlobalValue(GV->getInitializer()))
1007 Out << getCppName(GV->getInitializer()) << ");";
1013 CppWriter::getOpName(Value* V) {
1014 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
1015 return getCppName(V);
1017 // See if its alread in the map of forward references, if so just return the
1018 // name we already set up for it
1019 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
1020 if (I != ForwardRefs.end())
1023 // This is a new forward reference. Generate a unique name for it
1024 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
1026 // Yes, this is a hack. An Argument is the smallest instantiable value that
1027 // we can make as a placeholder for the real value. We'll replace these
1028 // Argument instances later.
1029 Out << "Argument* " << result << " = new Argument("
1030 << getCppName(V->getType()) << ");";
1032 ForwardRefs[V] = result;
1036 // printInstruction - This member is called for each Instruction in a function.
1038 CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
1039 std::string iName(getCppName(I));
1041 // Before we emit this instruction, we need to take care of generating any
1042 // forward references. So, we get the names of all the operands in advance
1043 std::string* opNames = new std::string[I->getNumOperands()];
1044 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1045 opNames[i] = getOpName(I->getOperand(i));
1048 switch (I->getOpcode()) {
1049 case Instruction::Ret: {
1050 const ReturnInst* ret = cast<ReturnInst>(I);
1051 Out << "new ReturnInst("
1052 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
1055 case Instruction::Br: {
1056 const BranchInst* br = cast<BranchInst>(I);
1057 Out << "new BranchInst(" ;
1058 if (br->getNumOperands() == 3 ) {
1059 Out << opNames[0] << ", "
1060 << opNames[1] << ", "
1061 << opNames[2] << ", ";
1063 } else if (br->getNumOperands() == 1) {
1064 Out << opNames[0] << ", ";
1066 error("Branch with 2 operands?");
1068 Out << bbname << ");";
1071 case Instruction::Switch: {
1072 const SwitchInst* sw = cast<SwitchInst>(I);
1073 Out << "SwitchInst* " << iName << " = new SwitchInst("
1074 << opNames[0] << ", "
1075 << opNames[1] << ", "
1076 << sw->getNumCases() << ", " << bbname << ");";
1078 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
1079 Out << iName << "->addCase("
1080 << opNames[i] << ", "
1081 << opNames[i+1] << ");";
1086 case Instruction::Invoke: {
1087 const InvokeInst* inv = cast<InvokeInst>(I);
1088 Out << "std::vector<Value*> " << iName << "_params;";
1090 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1091 Out << iName << "_params.push_back("
1092 << opNames[i] << ");";
1095 Out << "InvokeInst *" << iName << " = new InvokeInst("
1096 << opNames[0] << ", "
1097 << opNames[1] << ", "
1098 << opNames[2] << ", "
1099 << iName << "_params.begin(), " << iName << "_params.end(), \"";
1100 printEscapedString(inv->getName());
1101 Out << "\", " << bbname << ");";
1102 nl(Out) << iName << "->setCallingConv(";
1103 printCallingConv(inv->getCallingConv());
1107 case Instruction::Unwind: {
1108 Out << "new UnwindInst("
1112 case Instruction::Unreachable:{
1113 Out << "new UnreachableInst("
1117 case Instruction::Add:
1118 case Instruction::Sub:
1119 case Instruction::Mul:
1120 case Instruction::UDiv:
1121 case Instruction::SDiv:
1122 case Instruction::FDiv:
1123 case Instruction::URem:
1124 case Instruction::SRem:
1125 case Instruction::FRem:
1126 case Instruction::And:
1127 case Instruction::Or:
1128 case Instruction::Xor:
1129 case Instruction::Shl:
1130 case Instruction::LShr:
1131 case Instruction::AShr:{
1132 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
1133 switch (I->getOpcode()) {
1134 case Instruction::Add: Out << "Instruction::Add"; break;
1135 case Instruction::Sub: Out << "Instruction::Sub"; break;
1136 case Instruction::Mul: Out << "Instruction::Mul"; break;
1137 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1138 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1139 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
1140 case Instruction::URem:Out << "Instruction::URem"; break;
1141 case Instruction::SRem:Out << "Instruction::SRem"; break;
1142 case Instruction::FRem:Out << "Instruction::FRem"; break;
1143 case Instruction::And: Out << "Instruction::And"; break;
1144 case Instruction::Or: Out << "Instruction::Or"; break;
1145 case Instruction::Xor: Out << "Instruction::Xor"; break;
1146 case Instruction::Shl: Out << "Instruction::Shl"; break;
1147 case Instruction::LShr:Out << "Instruction::LShr"; break;
1148 case Instruction::AShr:Out << "Instruction::AShr"; break;
1149 default: Out << "Instruction::BadOpCode"; break;
1151 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1152 printEscapedString(I->getName());
1153 Out << "\", " << bbname << ");";
1156 case Instruction::FCmp: {
1157 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1158 switch (cast<FCmpInst>(I)->getPredicate()) {
1159 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1160 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1161 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1162 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1163 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1164 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1165 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1166 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1167 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1168 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1169 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1170 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1171 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1172 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1173 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1174 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1175 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1177 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1178 printEscapedString(I->getName());
1179 Out << "\", " << bbname << ");";
1182 case Instruction::ICmp: {
1183 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1184 switch (cast<ICmpInst>(I)->getPredicate()) {
1185 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1186 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1187 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1188 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1189 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1190 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1191 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1192 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1193 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1194 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1195 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
1197 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1198 printEscapedString(I->getName());
1199 Out << "\", " << bbname << ");";
1202 case Instruction::Malloc: {
1203 const MallocInst* mallocI = cast<MallocInst>(I);
1204 Out << "MallocInst* " << iName << " = new MallocInst("
1205 << getCppName(mallocI->getAllocatedType()) << ", ";
1206 if (mallocI->isArrayAllocation())
1207 Out << opNames[0] << ", " ;
1209 printEscapedString(mallocI->getName());
1210 Out << "\", " << bbname << ");";
1211 if (mallocI->getAlignment())
1212 nl(Out) << iName << "->setAlignment("
1213 << mallocI->getAlignment() << ");";
1216 case Instruction::Free: {
1217 Out << "FreeInst* " << iName << " = new FreeInst("
1218 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1221 case Instruction::Alloca: {
1222 const AllocaInst* allocaI = cast<AllocaInst>(I);
1223 Out << "AllocaInst* " << iName << " = new AllocaInst("
1224 << getCppName(allocaI->getAllocatedType()) << ", ";
1225 if (allocaI->isArrayAllocation())
1226 Out << opNames[0] << ", ";
1228 printEscapedString(allocaI->getName());
1229 Out << "\", " << bbname << ");";
1230 if (allocaI->getAlignment())
1231 nl(Out) << iName << "->setAlignment("
1232 << allocaI->getAlignment() << ");";
1235 case Instruction::Load:{
1236 const LoadInst* load = cast<LoadInst>(I);
1237 Out << "LoadInst* " << iName << " = new LoadInst("
1238 << opNames[0] << ", \"";
1239 printEscapedString(load->getName());
1240 Out << "\", " << (load->isVolatile() ? "true" : "false" )
1241 << ", " << bbname << ");";
1244 case Instruction::Store: {
1245 const StoreInst* store = cast<StoreInst>(I);
1246 Out << "StoreInst* " << iName << " = new StoreInst("
1247 << opNames[0] << ", "
1248 << opNames[1] << ", "
1249 << (store->isVolatile() ? "true" : "false")
1250 << ", " << bbname << ");";
1253 case Instruction::GetElementPtr: {
1254 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1255 if (gep->getNumOperands() <= 2) {
1256 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
1258 if (gep->getNumOperands() == 2)
1259 Out << ", " << opNames[1];
1261 Out << "std::vector<Value*> " << iName << "_indices;";
1263 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
1264 Out << iName << "_indices.push_back("
1265 << opNames[i] << ");";
1268 Out << "Instruction* " << iName << " = new GetElementPtrInst("
1269 << opNames[0] << ", " << iName << "_indices.begin(), "
1270 << iName << "_indices.end()";
1273 printEscapedString(gep->getName());
1274 Out << "\", " << bbname << ");";
1277 case Instruction::PHI: {
1278 const PHINode* phi = cast<PHINode>(I);
1280 Out << "PHINode* " << iName << " = new PHINode("
1281 << getCppName(phi->getType()) << ", \"";
1282 printEscapedString(phi->getName());
1283 Out << "\", " << bbname << ");";
1284 nl(Out) << iName << "->reserveOperandSpace("
1285 << phi->getNumIncomingValues()
1288 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
1289 Out << iName << "->addIncoming("
1290 << opNames[i] << ", " << opNames[i+1] << ");";
1295 case Instruction::Trunc:
1296 case Instruction::ZExt:
1297 case Instruction::SExt:
1298 case Instruction::FPTrunc:
1299 case Instruction::FPExt:
1300 case Instruction::FPToUI:
1301 case Instruction::FPToSI:
1302 case Instruction::UIToFP:
1303 case Instruction::SIToFP:
1304 case Instruction::PtrToInt:
1305 case Instruction::IntToPtr:
1306 case Instruction::BitCast: {
1307 const CastInst* cst = cast<CastInst>(I);
1308 Out << "CastInst* " << iName << " = new ";
1309 switch (I->getOpcode()) {
1310 case Instruction::Trunc: Out << "TruncInst"; break;
1311 case Instruction::ZExt: Out << "ZExtInst"; break;
1312 case Instruction::SExt: Out << "SExtInst"; break;
1313 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1314 case Instruction::FPExt: Out << "FPExtInst"; break;
1315 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1316 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1317 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1318 case Instruction::SIToFP: Out << "SIToFPInst"; break;
1319 case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
1320 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1321 case Instruction::BitCast: Out << "BitCastInst"; break;
1322 default: assert(!"Unreachable"); break;
1324 Out << "(" << opNames[0] << ", "
1325 << getCppName(cst->getType()) << ", \"";
1326 printEscapedString(cst->getName());
1327 Out << "\", " << bbname << ");";
1330 case Instruction::Call:{
1331 const CallInst* call = cast<CallInst>(I);
1332 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
1333 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
1334 << getCppName(ila->getFunctionType()) << ", \""
1335 << ila->getAsmString() << "\", \""
1336 << ila->getConstraintString() << "\","
1337 << (ila->hasSideEffects() ? "true" : "false") << ");";
1340 if (call->getNumOperands() > 2) {
1341 Out << "std::vector<Value*> " << iName << "_params;";
1343 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1344 Out << iName << "_params.push_back(" << opNames[i] << ");";
1347 Out << "CallInst* " << iName << " = new CallInst("
1348 << opNames[0] << ", " << iName << "_params.begin(), "
1349 << iName << "_params.end(), \"";
1350 } else if (call->getNumOperands() == 2) {
1351 Out << "CallInst* " << iName << " = new CallInst("
1352 << opNames[0] << ", " << opNames[1] << ", \"";
1354 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
1357 printEscapedString(call->getName());
1358 Out << "\", " << bbname << ");";
1359 nl(Out) << iName << "->setCallingConv(";
1360 printCallingConv(call->getCallingConv());
1362 nl(Out) << iName << "->setTailCall("
1363 << (call->isTailCall() ? "true":"false");
1367 case Instruction::Select: {
1368 const SelectInst* sel = cast<SelectInst>(I);
1369 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
1370 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1371 printEscapedString(sel->getName());
1372 Out << "\", " << bbname << ");";
1375 case Instruction::UserOp1:
1377 case Instruction::UserOp2: {
1378 /// FIXME: What should be done here?
1381 case Instruction::VAArg: {
1382 const VAArgInst* va = cast<VAArgInst>(I);
1383 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
1384 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
1385 printEscapedString(va->getName());
1386 Out << "\", " << bbname << ");";
1389 case Instruction::ExtractElement: {
1390 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
1391 Out << "ExtractElementInst* " << getCppName(eei)
1392 << " = new ExtractElementInst(" << opNames[0]
1393 << ", " << opNames[1] << ", \"";
1394 printEscapedString(eei->getName());
1395 Out << "\", " << bbname << ");";
1398 case Instruction::InsertElement: {
1399 const InsertElementInst* iei = cast<InsertElementInst>(I);
1400 Out << "InsertElementInst* " << getCppName(iei)
1401 << " = new InsertElementInst(" << opNames[0]
1402 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1403 printEscapedString(iei->getName());
1404 Out << "\", " << bbname << ");";
1407 case Instruction::ShuffleVector: {
1408 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
1409 Out << "ShuffleVectorInst* " << getCppName(svi)
1410 << " = new ShuffleVectorInst(" << opNames[0]
1411 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1412 printEscapedString(svi->getName());
1413 Out << "\", " << bbname << ");";
1417 DefinedValues.insert(I);
1422 // Print out the types, constants and declarations needed by one function
1423 void CppWriter::printFunctionUses(const Function* F) {
1425 nl(Out) << "// Type Definitions"; nl(Out);
1427 // Print the function's return type
1428 printType(F->getReturnType());
1430 // Print the function's function type
1431 printType(F->getFunctionType());
1433 // Print the types of each of the function's arguments
1434 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1436 printType(AI->getType());
1440 // Print type definitions for every type referenced by an instruction and
1441 // make a note of any global values or constants that are referenced
1442 SmallPtrSet<GlobalValue*,64> gvs;
1443 SmallPtrSet<Constant*,64> consts;
1444 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1445 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1447 // Print the type of the instruction itself
1448 printType(I->getType());
1450 // Print the type of each of the instruction's operands
1451 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1452 Value* operand = I->getOperand(i);
1453 printType(operand->getType());
1455 // If the operand references a GVal or Constant, make a note of it
1456 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
1458 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1459 if (GVar->hasInitializer())
1460 consts.insert(GVar->getInitializer());
1461 } else if (Constant* C = dyn_cast<Constant>(operand))
1467 // Print the function declarations for any functions encountered
1468 nl(Out) << "// Function Declarations"; nl(Out);
1469 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1471 if (Function* Fun = dyn_cast<Function>(*I)) {
1472 if (!is_inline || Fun != F)
1473 printFunctionHead(Fun);
1477 // Print the global variable declarations for any variables encountered
1478 nl(Out) << "// Global Variable Declarations"; nl(Out);
1479 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1481 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1482 printVariableHead(F);
1485 // Print the constants found
1486 nl(Out) << "// Constant Definitions"; nl(Out);
1487 for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(), E = consts.end();
1492 // Process the global variables definitions now that all the constants have
1493 // been emitted. These definitions just couple the gvars with their constant
1495 nl(Out) << "// Global Variable Definitions"; nl(Out);
1496 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
1498 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1499 printVariableBody(GV);
1503 void CppWriter::printFunctionHead(const Function* F) {
1504 nl(Out) << "Function* " << getCppName(F);
1506 Out << " = mod->getFunction(\"";
1507 printEscapedString(F->getName());
1508 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1509 nl(Out) << "if (!" << getCppName(F) << ") {";
1510 nl(Out) << getCppName(F);
1512 Out<< " = new Function(";
1513 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1514 nl(Out) << "/*Linkage=*/";
1515 printLinkageType(F->getLinkage());
1517 nl(Out) << "/*Name=*/\"";
1518 printEscapedString(F->getName());
1519 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
1522 Out << "->setCallingConv(";
1523 printCallingConv(F->getCallingConv());
1526 if (F->hasSection()) {
1528 Out << "->setSection(\"" << F->getSection() << "\");";
1531 if (F->getAlignment()) {
1533 Out << "->setAlignment(" << F->getAlignment() << ");";
1542 void CppWriter::printFunctionBody(const Function *F) {
1543 if (F->isDeclaration())
1544 return; // external functions have no bodies.
1546 // Clear the DefinedValues and ForwardRefs maps because we can't have
1547 // cross-function forward refs
1548 ForwardRefs.clear();
1549 DefinedValues.clear();
1551 // Create all the argument values
1553 if (!F->arg_empty()) {
1554 Out << "Function::arg_iterator args = " << getCppName(F)
1555 << "->arg_begin();";
1558 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1560 Out << "Value* " << getCppName(AI) << " = args++;";
1562 if (AI->hasName()) {
1563 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1569 // Create all the basic blocks
1571 for (Function::const_iterator BI = F->begin(), BE = F->end();
1573 std::string bbname(getCppName(BI));
1574 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
1576 printEscapedString(BI->getName());
1577 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1581 // Output all of its basic blocks... for the function
1582 for (Function::const_iterator BI = F->begin(), BE = F->end();
1584 std::string bbname(getCppName(BI));
1585 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1588 // Output all of the instructions in the basic block...
1589 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1591 printInstruction(I,bbname);
1595 // Loop over the ForwardRefs and resolve them now that all instructions
1597 if (!ForwardRefs.empty()) {
1598 nl(Out) << "// Resolve Forward References";
1602 while (!ForwardRefs.empty()) {
1603 ForwardRefMap::iterator I = ForwardRefs.begin();
1604 Out << I->second << "->replaceAllUsesWith("
1605 << getCppName(I->first) << "); delete " << I->second << ";";
1607 ForwardRefs.erase(I);
1611 void CppWriter::printInline(const std::string& fname, const std::string& func) {
1612 const Function* F = TheModule->getFunction(func);
1614 error(std::string("Function '") + func + "' not found in input module");
1617 if (F->isDeclaration()) {
1618 error(std::string("Function '") + func + "' is external!");
1621 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
1623 unsigned arg_count = 1;
1624 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1626 Out << ", Value* arg_" << arg_count;
1631 printFunctionUses(F);
1632 printFunctionBody(F);
1634 Out << "return " << getCppName(F->begin()) << ";";
1639 void CppWriter::printModuleBody() {
1640 // Print out all the type definitions
1641 nl(Out) << "// Type Definitions"; nl(Out);
1642 printTypes(TheModule);
1644 // Functions can call each other and global variables can reference them so
1645 // define all the functions first before emitting their function bodies.
1646 nl(Out) << "// Function Declarations"; nl(Out);
1647 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1649 printFunctionHead(I);
1651 // Process the global variables declarations. We can't initialze them until
1652 // after the constants are printed so just print a header for each global
1653 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
1654 for (Module::const_global_iterator I = TheModule->global_begin(),
1655 E = TheModule->global_end(); I != E; ++I) {
1656 printVariableHead(I);
1659 // Print out all the constants definitions. Constants don't recurse except
1660 // through GlobalValues. All GlobalValues have been declared at this point
1661 // so we can proceed to generate the constants.
1662 nl(Out) << "// Constant Definitions"; nl(Out);
1663 printConstants(TheModule);
1665 // Process the global variables definitions now that all the constants have
1666 // been emitted. These definitions just couple the gvars with their constant
1668 nl(Out) << "// Global Variable Definitions"; nl(Out);
1669 for (Module::const_global_iterator I = TheModule->global_begin(),
1670 E = TheModule->global_end(); I != E; ++I) {
1671 printVariableBody(I);
1674 // Finally, we can safely put out all of the function bodies.
1675 nl(Out) << "// Function Definitions"; nl(Out);
1676 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1678 if (!I->isDeclaration()) {
1679 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1683 printFunctionBody(I);
1690 void CppWriter::printProgram(
1691 const std::string& fname,
1692 const std::string& mName
1694 Out << "#include <llvm/Module.h>\n";
1695 Out << "#include <llvm/DerivedTypes.h>\n";
1696 Out << "#include <llvm/Constants.h>\n";
1697 Out << "#include <llvm/GlobalVariable.h>\n";
1698 Out << "#include <llvm/Function.h>\n";
1699 Out << "#include <llvm/CallingConv.h>\n";
1700 Out << "#include <llvm/BasicBlock.h>\n";
1701 Out << "#include <llvm/Instructions.h>\n";
1702 Out << "#include <llvm/InlineAsm.h>\n";
1703 Out << "#include <llvm/ParameterAttributes.h>\n";
1704 Out << "#include <llvm/Support/MathExtras.h>\n";
1705 Out << "#include <llvm/Pass.h>\n";
1706 Out << "#include <llvm/PassManager.h>\n";
1707 Out << "#include <llvm/Analysis/Verifier.h>\n";
1708 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1709 Out << "#include <algorithm>\n";
1710 Out << "#include <iostream>\n\n";
1711 Out << "using namespace llvm;\n\n";
1712 Out << "Module* " << fname << "();\n\n";
1713 Out << "int main(int argc, char**argv) {\n";
1714 Out << " Module* Mod = " << fname << "();\n";
1715 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1716 Out << " std::cerr.flush();\n";
1717 Out << " std::cout.flush();\n";
1718 Out << " PassManager PM;\n";
1719 Out << " PM.add(new PrintModulePass(&llvm::cout));\n";
1720 Out << " PM.run(*Mod);\n";
1721 Out << " return 0;\n";
1723 printModule(fname,mName);
1726 void CppWriter::printModule(
1727 const std::string& fname,
1728 const std::string& mName
1730 nl(Out) << "Module* " << fname << "() {";
1731 nl(Out,1) << "// Module Construction";
1732 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1733 if (!TheModule->getTargetTriple().empty()) {
1734 nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
1736 if (!TheModule->getTargetTriple().empty()) {
1737 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1741 if (!TheModule->getModuleInlineAsm().empty()) {
1742 nl(Out) << "mod->setModuleInlineAsm(\"";
1743 printEscapedString(TheModule->getModuleInlineAsm());
1748 // Loop over the dependent libraries and emit them.
1749 Module::lib_iterator LI = TheModule->lib_begin();
1750 Module::lib_iterator LE = TheModule->lib_end();
1752 Out << "mod->addLibrary(\"" << *LI << "\");";
1757 nl(Out) << "return mod;";
1762 void CppWriter::printContents(
1763 const std::string& fname, // Name of generated function
1764 const std::string& mName // Name of module generated module
1766 Out << "\nModule* " << fname << "(Module *mod) {\n";
1767 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
1769 Out << "\nreturn mod;\n";
1773 void CppWriter::printFunction(
1774 const std::string& fname, // Name of generated function
1775 const std::string& funcName // Name of function to generate
1777 const Function* F = TheModule->getFunction(funcName);
1779 error(std::string("Function '") + funcName + "' not found in input module");
1782 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1783 printFunctionUses(F);
1784 printFunctionHead(F);
1785 printFunctionBody(F);
1786 Out << "return " << getCppName(F) << ";\n";
1790 void CppWriter::printFunctions() {
1791 const Module::FunctionListType &funcs = TheModule->getFunctionList();
1792 Module::const_iterator I = funcs.begin();
1793 Module::const_iterator IE = funcs.end();
1795 for (; I != IE; ++I) {
1796 const Function &func = *I;
1797 if (!func.isDeclaration()) {
1798 std::string name("define_");
1799 name += func.getName();
1800 printFunction(name, func.getName());
1805 void CppWriter::printVariable(
1806 const std::string& fname, /// Name of generated function
1807 const std::string& varName // Name of variable to generate
1809 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1812 error(std::string("Variable '") + varName + "' not found in input module");
1815 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1816 printVariableUses(GV);
1817 printVariableHead(GV);
1818 printVariableBody(GV);
1819 Out << "return " << getCppName(GV) << ";\n";
1823 void CppWriter::printType(
1824 const std::string& fname, /// Name of generated function
1825 const std::string& typeName // Name of type to generate
1827 const Type* Ty = TheModule->getTypeByName(typeName);
1829 error(std::string("Type '") + typeName + "' not found in input module");
1832 Out << "\nType* " << fname << "(Module *mod) {\n";
1834 Out << "return " << getCppName(Ty) << ";\n";
1838 } // end anonymous llvm
1842 void WriteModuleToCppFile(Module* mod, std::ostream& o) {
1843 // Initialize a CppWriter for us to use
1844 CppWriter W(o, mod);
1847 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
1849 // Get the name of the function we're supposed to generate
1850 std::string fname = FuncName.getValue();
1852 // Get the name of the thing we are to generate
1853 std::string tgtname = NameToGenerate.getValue();
1854 if (GenerationType == GenModule ||
1855 GenerationType == GenContents ||
1856 GenerationType == GenProgram ||
1857 GenerationType == GenFunctions) {
1858 if (tgtname == "!bad!") {
1859 if (mod->getModuleIdentifier() == "-")
1860 tgtname = "<stdin>";
1862 tgtname = mod->getModuleIdentifier();
1864 } else if (tgtname == "!bad!") {
1865 W.error("You must use the -for option with -gen-{function,variable,type}");
1868 switch (WhatToGenerate(GenerationType)) {
1871 fname = "makeLLVMModule";
1872 W.printProgram(fname,tgtname);
1876 fname = "makeLLVMModule";
1877 W.printModule(fname,tgtname);
1881 fname = "makeLLVMModuleContents";
1882 W.printContents(fname,tgtname);
1886 fname = "makeLLVMFunction";
1887 W.printFunction(fname,tgtname);
1894 fname = "makeLLVMInline";
1895 W.printInline(fname,tgtname);
1899 fname = "makeLLVMVariable";
1900 W.printVariable(fname,tgtname);
1904 fname = "makeLLVMType";
1905 W.printType(fname,tgtname);
1908 W.error("Invalid generation option");