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/Support/CommandLine.h"
27 #include "llvm/Support/CFG.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/Config/config.h"
37 static cl::opt<std::string>
38 FuncName("funcname", cl::desc("Specify the name of the generated function"),
39 cl::value_desc("function name"));
51 static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
52 cl::desc("Choose what kind of output to generate"),
55 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
56 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
57 clEnumValN(GenContents,"gen-contents", "Generate contents of a module"),
58 clEnumValN(GenFunction,"gen-function", "Generate a function definition"),
59 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
60 clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"),
61 clEnumValN(GenType, "gen-type", "Generate a type definition"),
66 static cl::opt<std::string> NameToGenerate("for", cl::Optional,
67 cl::desc("Specify the name of the thing to generate"),
71 typedef std::vector<const Type*> TypeList;
72 typedef std::map<const Type*,std::string> TypeMap;
73 typedef std::map<const Value*,std::string> ValueMap;
74 typedef std::set<std::string> NameSet;
75 typedef std::set<const Type*> TypeSet;
76 typedef std::set<const Value*> ValueSet;
77 typedef std::map<const Value*,std::string> ForwardRefMap;
82 const Module *TheModule;
86 TypeMap UnresolvedTypes;
90 ValueSet DefinedValues;
91 ForwardRefMap ForwardRefs;
95 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
96 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
97 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
99 const Module* getModule() { return TheModule; }
101 void printProgram(const std::string& fname, const std::string& modName );
102 void printModule(const std::string& fname, const std::string& modName );
103 void printContents(const std::string& fname, const std::string& modName );
104 void printFunction(const std::string& fname, const std::string& funcName );
105 void printInline(const std::string& fname, const std::string& funcName );
106 void printVariable(const std::string& fname, const std::string& varName );
107 void printType(const std::string& fname, const std::string& typeName );
109 void error(const std::string& msg);
112 void printLinkageType(GlobalValue::LinkageTypes LT);
113 void printCallingConv(unsigned cc);
114 void printEscapedString(const std::string& str);
115 void printCFP(const ConstantFP* CFP);
117 std::string getCppName(const Type* val);
118 inline void printCppName(const Type* val);
120 std::string getCppName(const Value* val);
121 inline void printCppName(const Value* val);
123 bool printTypeInternal(const Type* Ty);
124 inline void printType(const Type* Ty);
125 void printTypes(const Module* M);
127 void printConstant(const Constant *CPV);
128 void printConstants(const Module* M);
130 void printVariableUses(const GlobalVariable *GV);
131 void printVariableHead(const GlobalVariable *GV);
132 void printVariableBody(const GlobalVariable *GV);
134 void printFunctionUses(const Function *F);
135 void printFunctionHead(const Function *F);
136 void printFunctionBody(const Function *F);
137 void printInstruction(const Instruction *I, const std::string& bbname);
138 std::string getOpName(Value*);
140 void printModuleBody();
144 static unsigned indent_level = 0;
145 inline std::ostream& nl(std::ostream& Out, int delta = 0) {
147 if (delta >= 0 || indent_level >= unsigned(-delta))
148 indent_level += delta;
149 for (unsigned i = 0; i < indent_level; ++i)
154 inline void in() { indent_level++; }
155 inline void out() { if (indent_level >0) indent_level--; }
158 sanitize(std::string& str) {
159 for (size_t i = 0; i < str.length(); ++i)
160 if (!isalnum(str[i]) && str[i] != '_')
165 getTypePrefix(const Type* Ty ) {
166 switch (Ty->getTypeID()) {
167 case Type::VoidTyID: return "void_";
168 case Type::IntegerTyID:
169 return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
171 case Type::FloatTyID: return "float_";
172 case Type::DoubleTyID: return "double_";
173 case Type::LabelTyID: return "label_";
174 case Type::FunctionTyID: return "func_";
175 case Type::StructTyID: return "struct_";
176 case Type::ArrayTyID: return "array_";
177 case Type::PointerTyID: return "ptr_";
178 case Type::VectorTyID: return "packed_";
179 case Type::OpaqueTyID: return "opaque_";
180 default: return "other_";
185 // Looks up the type in the symbol table and returns a pointer to its name or
186 // a null pointer if it wasn't found. Note that this isn't the same as the
187 // Mode::getTypeName function which will return an empty string, not a null
188 // pointer if the name is not found.
189 inline const std::string*
190 findTypeName(const TypeSymbolTable& ST, const Type* Ty)
192 TypeSymbolTable::const_iterator TI = ST.begin();
193 TypeSymbolTable::const_iterator TE = ST.end();
194 for (;TI != TE; ++TI)
195 if (TI->second == Ty)
201 CppWriter::error(const std::string& msg) {
202 std::cerr << progname << ": " << msg << "\n";
206 // printCFP - Print a floating point constant .. very carefully :)
207 // This makes sure that conversion to/from floating yields the same binary
208 // result so that we don't lose precision.
210 CppWriter::printCFP(const ConstantFP *CFP) {
211 Out << "ConstantFP::get(";
212 if (CFP->getType() == Type::DoubleTy)
213 Out << "Type::DoubleTy, ";
215 Out << "Type::FloatTy, ";
218 sprintf(Buffer, "%A", CFP->getValue());
219 if ((!strncmp(Buffer, "0x", 2) ||
220 !strncmp(Buffer, "-0x", 3) ||
221 !strncmp(Buffer, "+0x", 3)) &&
222 (atof(Buffer) == CFP->getValue()))
223 if (CFP->getType() == Type::DoubleTy)
224 Out << "BitsToDouble(" << Buffer << ")";
226 Out << "BitsToFloat(" << Buffer << ")";
229 std::string StrVal = ftostr(CFP->getValue());
231 while (StrVal[0] == ' ')
232 StrVal.erase(StrVal.begin());
234 // Check to make sure that the stringized number is not some string like
235 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
236 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
237 ((StrVal[0] == '-' || StrVal[0] == '+') &&
238 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
239 (atof(StrVal.c_str()) == CFP->getValue()))
240 if (CFP->getType() == Type::DoubleTy)
244 else if (CFP->getType() == Type::DoubleTy)
245 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
246 << std::dec << "ULL) /* " << StrVal << " */";
248 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
249 << std::dec << "U) /* " << StrVal << " */";
257 CppWriter::printCallingConv(unsigned cc){
258 // Print the calling convention.
260 case CallingConv::C: Out << "CallingConv::C"; break;
261 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
262 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
263 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
264 default: Out << cc; break;
269 CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
271 case GlobalValue::InternalLinkage:
272 Out << "GlobalValue::InternalLinkage"; break;
273 case GlobalValue::LinkOnceLinkage:
274 Out << "GlobalValue::LinkOnceLinkage "; break;
275 case GlobalValue::WeakLinkage:
276 Out << "GlobalValue::WeakLinkage"; break;
277 case GlobalValue::AppendingLinkage:
278 Out << "GlobalValue::AppendingLinkage"; break;
279 case GlobalValue::ExternalLinkage:
280 Out << "GlobalValue::ExternalLinkage"; break;
281 case GlobalValue::DLLImportLinkage:
282 Out << "GlobalValue::DllImportLinkage"; break;
283 case GlobalValue::DLLExportLinkage:
284 Out << "GlobalValue::DllExportLinkage"; break;
285 case GlobalValue::ExternalWeakLinkage:
286 Out << "GlobalValue::ExternalWeakLinkage"; break;
287 case GlobalValue::GhostLinkage:
288 Out << "GlobalValue::GhostLinkage"; break;
292 // printEscapedString - Print each character of the specified string, escaping
293 // it if it is not printable or if it is an escape char.
295 CppWriter::printEscapedString(const std::string &Str) {
296 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
297 unsigned char C = Str[i];
298 if (isprint(C) && C != '"' && C != '\\') {
302 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
303 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
309 CppWriter::getCppName(const Type* Ty)
311 // First, handle the primitive types .. easy
312 if (Ty->isPrimitiveType() || Ty->isInteger()) {
313 switch (Ty->getTypeID()) {
314 case Type::VoidTyID: return "Type::VoidTy";
315 case Type::IntegerTyID: {
316 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
317 return "IntegerType::get(" + utostr(BitWidth) + ")";
319 case Type::FloatTyID: return "Type::FloatTy";
320 case Type::DoubleTyID: return "Type::DoubleTy";
321 case Type::LabelTyID: return "Type::LabelTy";
323 error("Invalid primitive type");
326 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
329 // Now, see if we've seen the type before and return that
330 TypeMap::iterator I = TypeNames.find(Ty);
331 if (I != TypeNames.end())
334 // Okay, let's build a new name for this type. Start with a prefix
335 const char* prefix = 0;
336 switch (Ty->getTypeID()) {
337 case Type::FunctionTyID: prefix = "FuncTy_"; break;
338 case Type::StructTyID: prefix = "StructTy_"; break;
339 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
340 case Type::PointerTyID: prefix = "PointerTy_"; break;
341 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
342 case Type::VectorTyID: prefix = "VectorTy_"; break;
343 default: prefix = "OtherTy_"; break; // prevent breakage
346 // See if the type has a name in the symboltable and build accordingly
347 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
350 name = std::string(prefix) + *tName;
352 name = std::string(prefix) + utostr(uniqueNum++);
356 return TypeNames[Ty] = name;
360 CppWriter::printCppName(const Type* Ty)
362 printEscapedString(getCppName(Ty));
366 CppWriter::getCppName(const Value* val) {
368 ValueMap::iterator I = ValueNames.find(val);
369 if (I != ValueNames.end() && I->first == val)
372 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
373 name = std::string("gvar_") +
374 getTypePrefix(GV->getType()->getElementType());
375 } else if (isa<Function>(val)) {
376 name = std::string("func_");
377 } else if (const Constant* C = dyn_cast<Constant>(val)) {
378 name = std::string("const_") + getTypePrefix(C->getType());
379 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
381 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
382 Function::const_arg_iterator(Arg)) + 1;
383 name = std::string("arg_") + utostr(argNum);
384 NameSet::iterator NI = UsedNames.find(name);
385 if (NI != UsedNames.end())
386 name += std::string("_") + utostr(uniqueNum++);
387 UsedNames.insert(name);
388 return ValueNames[val] = name;
390 name = getTypePrefix(val->getType());
393 name = getTypePrefix(val->getType());
395 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
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;
405 CppWriter::printCppName(const Value* val) {
406 printEscapedString(getCppName(val));
410 CppWriter::printTypeInternal(const Type* Ty) {
411 // We don't print definitions for primitive types
412 if (Ty->isPrimitiveType() || Ty->isInteger())
415 // If we already defined this type, we don't need to define it again.
416 if (DefinedTypes.find(Ty) != DefinedTypes.end())
419 // Everything below needs the name for the type so get it now.
420 std::string typeName(getCppName(Ty));
422 // Search the type stack for recursion. If we find it, then generate this
423 // as an OpaqueType, but make sure not to do this multiple times because
424 // the type could appear in multiple places on the stack. Once the opaque
425 // definition is issued, it must not be re-issued. Consequently we have to
426 // check the UnresolvedTypes list as well.
427 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
428 if (TI != TypeStack.end()) {
429 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
430 if (I == UnresolvedTypes.end()) {
431 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
433 UnresolvedTypes[Ty] = typeName;
438 // We're going to print a derived type which, by definition, contains other
439 // types. So, push this one we're printing onto the type stack to assist with
440 // recursive definitions.
441 TypeStack.push_back(Ty);
443 // Print the type definition
444 switch (Ty->getTypeID()) {
445 case Type::FunctionTyID: {
446 const FunctionType* FT = cast<FunctionType>(Ty);
447 Out << "std::vector<const Type*>" << typeName << "_args;";
449 FunctionType::param_iterator PI = FT->param_begin();
450 FunctionType::param_iterator PE = FT->param_end();
451 for (; PI != PE; ++PI) {
452 const Type* argTy = static_cast<const Type*>(*PI);
453 bool isForward = printTypeInternal(argTy);
454 std::string argName(getCppName(argTy));
455 Out << typeName << "_args.push_back(" << argName;
461 const ParamAttrsList *PAL = FT->getParamAttrs();
462 Out << "ParamAttrsList *" << typeName << "_PAL = 0;";
464 if (PAL && !PAL->empty()) {
465 Out << typeName << "_PAL = new ParamAttrsList();";
467 for (unsigned i = 0; i < PAL->size(); ++i) {
468 uint16_t index = PAL->getParamIndex(i);
469 uint16_t attrs = PAL->getParamAttrs(index);
470 Out << typeName << "_PAL->addAttribute(" << index << ", 0";
471 if (attrs & ParamAttr::SExt)
472 Out << " | ParamAttr::SExt";
473 if (attrs & ParamAttr::ZExt)
474 Out << " | ParamAttr::ZExt";
475 if (attrs & ParamAttr::StructRet)
476 Out << " | ParamAttr::StructRet";
477 if (attrs & ParamAttr::InReg)
478 Out << " | ParamAttr::InReg";
479 if (attrs & ParamAttr::NoReturn)
480 Out << " | ParamAttr::NoReturn";
481 if (attrs & ParamAttr::NoUnwind)
482 Out << " | ParamAttr::NoUnwind";
487 bool isForward = printTypeInternal(FT->getReturnType());
488 std::string retTypeName(getCppName(FT->getReturnType()));
489 Out << "FunctionType* " << typeName << " = FunctionType::get(";
490 in(); nl(Out) << "/*Result=*/" << retTypeName;
494 nl(Out) << "/*Params=*/" << typeName << "_args,";
495 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") ;
496 nl(Out) << "/*ParamAttrs=*/" << typeName << "_PAL" << ");";
501 case Type::StructTyID: {
502 const StructType* ST = cast<StructType>(Ty);
503 Out << "std::vector<const Type*>" << typeName << "_fields;";
505 StructType::element_iterator EI = ST->element_begin();
506 StructType::element_iterator EE = ST->element_end();
507 for (; EI != EE; ++EI) {
508 const Type* fieldTy = static_cast<const Type*>(*EI);
509 bool isForward = printTypeInternal(fieldTy);
510 std::string fieldName(getCppName(fieldTy));
511 Out << typeName << "_fields.push_back(" << fieldName;
517 Out << "StructType* " << typeName << " = StructType::get("
518 << typeName << "_fields);";
522 case Type::ArrayTyID: {
523 const ArrayType* AT = cast<ArrayType>(Ty);
524 const Type* ET = AT->getElementType();
525 bool isForward = printTypeInternal(ET);
526 std::string elemName(getCppName(ET));
527 Out << "ArrayType* " << typeName << " = ArrayType::get("
528 << elemName << (isForward ? "_fwd" : "")
529 << ", " << utostr(AT->getNumElements()) << ");";
533 case Type::PointerTyID: {
534 const PointerType* PT = cast<PointerType>(Ty);
535 const Type* ET = PT->getElementType();
536 bool isForward = printTypeInternal(ET);
537 std::string elemName(getCppName(ET));
538 Out << "PointerType* " << typeName << " = PointerType::get("
539 << elemName << (isForward ? "_fwd" : "") << ");";
543 case Type::VectorTyID: {
544 const VectorType* PT = cast<VectorType>(Ty);
545 const Type* ET = PT->getElementType();
546 bool isForward = printTypeInternal(ET);
547 std::string elemName(getCppName(ET));
548 Out << "VectorType* " << typeName << " = VectorType::get("
549 << elemName << (isForward ? "_fwd" : "")
550 << ", " << utostr(PT->getNumElements()) << ");";
554 case Type::OpaqueTyID: {
555 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
560 error("Invalid TypeID");
563 // If the type had a name, make sure we recreate it.
564 const std::string* progTypeName =
565 findTypeName(TheModule->getTypeSymbolTable(),Ty);
567 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
571 // Pop us off the type stack
572 TypeStack.pop_back();
574 // Indicate that this type is now defined.
575 DefinedTypes.insert(Ty);
577 // Early resolve as many unresolved types as possible. Search the unresolved
578 // types map for the type we just printed. Now that its definition is complete
579 // we can resolve any previous references to it. This prevents a cascade of
581 TypeMap::iterator I = UnresolvedTypes.find(Ty);
582 if (I != UnresolvedTypes.end()) {
583 Out << "cast<OpaqueType>(" << I->second
584 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
586 Out << I->second << " = cast<";
587 switch (Ty->getTypeID()) {
588 case Type::FunctionTyID: Out << "FunctionType"; break;
589 case Type::ArrayTyID: Out << "ArrayType"; break;
590 case Type::StructTyID: Out << "StructType"; break;
591 case Type::VectorTyID: Out << "VectorType"; break;
592 case Type::PointerTyID: Out << "PointerType"; break;
593 case Type::OpaqueTyID: Out << "OpaqueType"; break;
594 default: Out << "NoSuchDerivedType"; break;
596 Out << ">(" << I->second << "_fwd.get());";
598 UnresolvedTypes.erase(I);
601 // Finally, separate the type definition from other with a newline.
604 // We weren't a recursive type
608 // Prints a type definition. Returns true if it could not resolve all the types
609 // in the definition but had to use a forward reference.
611 CppWriter::printType(const Type* Ty) {
612 assert(TypeStack.empty());
614 printTypeInternal(Ty);
615 assert(TypeStack.empty());
619 CppWriter::printTypes(const Module* M) {
621 // Walk the symbol table and print out all its types
622 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
623 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
626 // For primitive types and types already defined, just add a name
627 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
628 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
629 TNI != TypeNames.end()) {
630 Out << "mod->addTypeName(\"";
631 printEscapedString(TI->first);
632 Out << "\", " << getCppName(TI->second) << ");";
634 // For everything else, define the type
636 printType(TI->second);
640 // Add all of the global variables to the value table...
641 for (Module::const_global_iterator I = TheModule->global_begin(),
642 E = TheModule->global_end(); I != E; ++I) {
643 if (I->hasInitializer())
644 printType(I->getInitializer()->getType());
645 printType(I->getType());
648 // Add all the functions to the table
649 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
651 printType(FI->getReturnType());
652 printType(FI->getFunctionType());
653 // Add all the function arguments
654 for(Function::const_arg_iterator AI = FI->arg_begin(),
655 AE = FI->arg_end(); AI != AE; ++AI) {
656 printType(AI->getType());
659 // Add all of the basic blocks and instructions
660 for (Function::const_iterator BB = FI->begin(),
661 E = FI->end(); BB != E; ++BB) {
662 printType(BB->getType());
663 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
665 printType(I->getType());
666 for (unsigned i = 0; i < I->getNumOperands(); ++i)
667 printType(I->getOperand(i)->getType());
674 // printConstant - Print out a constant pool entry...
675 void CppWriter::printConstant(const Constant *CV) {
676 // First, if the constant is actually a GlobalValue (variable or function) or
677 // its already in the constant list then we've printed it already and we can
679 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
682 std::string constName(getCppName(CV));
683 std::string typeName(getCppName(CV->getType()));
684 if (CV->isNullValue()) {
685 Out << "Constant* " << constName << " = Constant::getNullValue("
690 if (isa<GlobalValue>(CV)) {
691 // Skip variables and functions, we emit them elsewhere
694 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
695 Out << "ConstantInt* " << constName << " = ConstantInt::get("
696 << "APInt(cast<IntegerTyp>(" << typeName << ")->getBitWidth(),"
697 << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
698 } else if (isa<ConstantAggregateZero>(CV)) {
699 Out << "ConstantAggregateZero* " << constName
700 << " = ConstantAggregateZero::get(" << typeName << ");";
701 } else if (isa<ConstantPointerNull>(CV)) {
702 Out << "ConstantPointerNull* " << constName
703 << " = ConstanPointerNull::get(" << typeName << ");";
704 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
705 Out << "ConstantFP* " << constName << " = ";
708 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
709 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
710 Out << "Constant* " << constName << " = ConstantArray::get(\"";
711 printEscapedString(CA->getAsString());
712 // Determine if we want null termination or not.
713 if (CA->getType()->getNumElements() <= CA->getAsString().length())
714 Out << "\", false";// No null terminator
716 Out << "\", true"; // Indicate that the null terminator should be added.
719 Out << "std::vector<Constant*> " << constName << "_elems;";
721 unsigned N = CA->getNumOperands();
722 for (unsigned i = 0; i < N; ++i) {
723 printConstant(CA->getOperand(i)); // recurse to print operands
724 Out << constName << "_elems.push_back("
725 << getCppName(CA->getOperand(i)) << ");";
728 Out << "Constant* " << constName << " = ConstantArray::get("
729 << typeName << ", " << constName << "_elems);";
731 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
732 Out << "std::vector<Constant*> " << constName << "_fields;";
734 unsigned N = CS->getNumOperands();
735 for (unsigned i = 0; i < N; i++) {
736 printConstant(CS->getOperand(i));
737 Out << constName << "_fields.push_back("
738 << getCppName(CS->getOperand(i)) << ");";
741 Out << "Constant* " << constName << " = ConstantStruct::get("
742 << typeName << ", " << constName << "_fields);";
743 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
744 Out << "std::vector<Constant*> " << constName << "_elems;";
746 unsigned N = CP->getNumOperands();
747 for (unsigned i = 0; i < N; ++i) {
748 printConstant(CP->getOperand(i));
749 Out << constName << "_elems.push_back("
750 << getCppName(CP->getOperand(i)) << ");";
753 Out << "Constant* " << constName << " = ConstantVector::get("
754 << typeName << ", " << constName << "_elems);";
755 } else if (isa<UndefValue>(CV)) {
756 Out << "UndefValue* " << constName << " = UndefValue::get("
758 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
759 if (CE->getOpcode() == Instruction::GetElementPtr) {
760 Out << "std::vector<Constant*> " << constName << "_indices;";
762 printConstant(CE->getOperand(0));
763 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
764 printConstant(CE->getOperand(i));
765 Out << constName << "_indices.push_back("
766 << getCppName(CE->getOperand(i)) << ");";
769 Out << "Constant* " << constName
770 << " = ConstantExpr::getGetElementPtr("
771 << getCppName(CE->getOperand(0)) << ", "
772 << constName << "_indices);";
773 } else if (CE->isCast()) {
774 printConstant(CE->getOperand(0));
775 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
776 switch (CE->getOpcode()) {
777 default: assert(0 && "Invalid cast opcode");
778 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
779 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
780 case Instruction::SExt: Out << "Instruction::SExt"; break;
781 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
782 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
783 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
784 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
785 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
786 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
787 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
788 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
789 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
791 Out << ", " << getCppName(CE->getOperand(0)) << ", "
792 << getCppName(CE->getType()) << ");";
794 unsigned N = CE->getNumOperands();
795 for (unsigned i = 0; i < N; ++i ) {
796 printConstant(CE->getOperand(i));
798 Out << "Constant* " << constName << " = ConstantExpr::";
799 switch (CE->getOpcode()) {
800 case Instruction::Add: Out << "getAdd("; break;
801 case Instruction::Sub: Out << "getSub("; break;
802 case Instruction::Mul: Out << "getMul("; break;
803 case Instruction::UDiv: Out << "getUDiv("; break;
804 case Instruction::SDiv: Out << "getSDiv("; break;
805 case Instruction::FDiv: Out << "getFDiv("; break;
806 case Instruction::URem: Out << "getURem("; break;
807 case Instruction::SRem: Out << "getSRem("; break;
808 case Instruction::FRem: Out << "getFRem("; break;
809 case Instruction::And: Out << "getAnd("; break;
810 case Instruction::Or: Out << "getOr("; break;
811 case Instruction::Xor: Out << "getXor("; break;
812 case Instruction::ICmp:
813 Out << "getICmp(ICmpInst::ICMP_";
814 switch (CE->getPredicate()) {
815 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
816 case ICmpInst::ICMP_NE: Out << "NE"; break;
817 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
818 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
819 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
820 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
821 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
822 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
823 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
824 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
825 default: error("Invalid ICmp Predicate");
828 case Instruction::FCmp:
829 Out << "getFCmp(FCmpInst::FCMP_";
830 switch (CE->getPredicate()) {
831 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
832 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
833 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
834 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
835 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
836 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
837 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
838 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
839 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
840 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
841 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
842 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
843 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
844 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
845 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
846 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
847 default: error("Invalid FCmp Predicate");
850 case Instruction::Shl: Out << "getShl("; break;
851 case Instruction::LShr: Out << "getLShr("; break;
852 case Instruction::AShr: Out << "getAShr("; break;
853 case Instruction::Select: Out << "getSelect("; break;
854 case Instruction::ExtractElement: Out << "getExtractElement("; break;
855 case Instruction::InsertElement: Out << "getInsertElement("; break;
856 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
858 error("Invalid constant expression");
861 Out << getCppName(CE->getOperand(0));
862 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
863 Out << ", " << getCppName(CE->getOperand(i));
867 error("Bad Constant");
868 Out << "Constant* " << constName << " = 0; ";
874 CppWriter::printConstants(const Module* M) {
875 // Traverse all the global variables looking for constant initializers
876 for (Module::const_global_iterator I = TheModule->global_begin(),
877 E = TheModule->global_end(); I != E; ++I)
878 if (I->hasInitializer())
879 printConstant(I->getInitializer());
881 // Traverse the LLVM functions looking for constants
882 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
884 // Add all of the basic blocks and instructions
885 for (Function::const_iterator BB = FI->begin(),
886 E = FI->end(); BB != E; ++BB) {
887 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
889 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
890 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
899 void CppWriter::printVariableUses(const GlobalVariable *GV) {
900 nl(Out) << "// Type Definitions";
902 printType(GV->getType());
903 if (GV->hasInitializer()) {
904 Constant* Init = GV->getInitializer();
905 printType(Init->getType());
906 if (Function* F = dyn_cast<Function>(Init)) {
907 nl(Out)<< "/ Function Declarations"; nl(Out);
908 printFunctionHead(F);
909 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
910 nl(Out) << "// Global Variable Declarations"; nl(Out);
911 printVariableHead(gv);
913 nl(Out) << "// Constant Definitions"; nl(Out);
916 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
917 nl(Out) << "// Global Variable Definitions"; nl(Out);
918 printVariableBody(gv);
923 void CppWriter::printVariableHead(const GlobalVariable *GV) {
924 nl(Out) << "GlobalVariable* " << getCppName(GV);
926 Out << " = mod->getGlobalVariable(";
927 printEscapedString(GV->getName());
928 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
929 nl(Out) << "if (!" << getCppName(GV) << ") {";
930 in(); nl(Out) << getCppName(GV);
932 Out << " = new GlobalVariable(";
933 nl(Out) << "/*Type=*/";
934 printCppName(GV->getType()->getElementType());
936 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
938 nl(Out) << "/*Linkage=*/";
939 printLinkageType(GV->getLinkage());
941 nl(Out) << "/*Initializer=*/0, ";
942 if (GV->hasInitializer()) {
943 Out << "// has initializer, specified below";
945 nl(Out) << "/*Name=*/\"";
946 printEscapedString(GV->getName());
951 if (GV->hasSection()) {
953 Out << "->setSection(\"";
954 printEscapedString(GV->getSection());
958 if (GV->getAlignment()) {
960 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
964 out(); Out << "}"; nl(Out);
969 CppWriter::printVariableBody(const GlobalVariable *GV) {
970 if (GV->hasInitializer()) {
972 Out << "->setInitializer(";
973 //if (!isa<GlobalValue(GV->getInitializer()))
975 Out << getCppName(GV->getInitializer()) << ");";
981 CppWriter::getOpName(Value* V) {
982 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
983 return getCppName(V);
985 // See if its alread in the map of forward references, if so just return the
986 // name we already set up for it
987 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
988 if (I != ForwardRefs.end())
991 // This is a new forward reference. Generate a unique name for it
992 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
994 // Yes, this is a hack. An Argument is the smallest instantiable value that
995 // we can make as a placeholder for the real value. We'll replace these
996 // Argument instances later.
997 Out << "Argument* " << result << " = new Argument("
998 << getCppName(V->getType()) << ");";
1000 ForwardRefs[V] = result;
1004 // printInstruction - This member is called for each Instruction in a function.
1006 CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
1007 std::string iName(getCppName(I));
1009 // Before we emit this instruction, we need to take care of generating any
1010 // forward references. So, we get the names of all the operands in advance
1011 std::string* opNames = new std::string[I->getNumOperands()];
1012 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1013 opNames[i] = getOpName(I->getOperand(i));
1016 switch (I->getOpcode()) {
1017 case Instruction::Ret: {
1018 const ReturnInst* ret = cast<ReturnInst>(I);
1019 Out << "ReturnInst* " << iName << " = new ReturnInst("
1020 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
1023 case Instruction::Br: {
1024 const BranchInst* br = cast<BranchInst>(I);
1025 Out << "BranchInst* " << iName << " = new BranchInst(" ;
1026 if (br->getNumOperands() == 3 ) {
1027 Out << opNames[0] << ", "
1028 << opNames[1] << ", "
1029 << opNames[2] << ", ";
1031 } else if (br->getNumOperands() == 1) {
1032 Out << opNames[0] << ", ";
1034 error("Branch with 2 operands?");
1036 Out << bbname << ");";
1039 case Instruction::Switch: {
1040 const SwitchInst* sw = cast<SwitchInst>(I);
1041 Out << "SwitchInst* " << iName << " = new SwitchInst("
1042 << opNames[0] << ", "
1043 << opNames[1] << ", "
1044 << sw->getNumCases() << ", " << bbname << ");";
1046 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
1047 Out << iName << "->addCase("
1048 << opNames[i] << ", "
1049 << opNames[i+1] << ");";
1054 case Instruction::Invoke: {
1055 const InvokeInst* inv = cast<InvokeInst>(I);
1056 Out << "std::vector<Value*> " << iName << "_params;";
1058 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1059 Out << iName << "_params.push_back("
1060 << opNames[i] << ");";
1063 Out << "InvokeInst* " << iName << " = new InvokeInst("
1064 << opNames[0] << ", "
1065 << opNames[1] << ", "
1066 << opNames[2] << ", "
1067 << iName << "_params, \"";
1068 printEscapedString(inv->getName());
1069 Out << "\", " << bbname << ");";
1070 nl(Out) << iName << "->setCallingConv(";
1071 printCallingConv(inv->getCallingConv());
1075 case Instruction::Unwind: {
1076 Out << "UnwindInst* " << iName << " = new UnwindInst("
1080 case Instruction::Unreachable:{
1081 Out << "UnreachableInst* " << iName << " = new UnreachableInst("
1085 case Instruction::Add:
1086 case Instruction::Sub:
1087 case Instruction::Mul:
1088 case Instruction::UDiv:
1089 case Instruction::SDiv:
1090 case Instruction::FDiv:
1091 case Instruction::URem:
1092 case Instruction::SRem:
1093 case Instruction::FRem:
1094 case Instruction::And:
1095 case Instruction::Or:
1096 case Instruction::Xor:
1097 case Instruction::Shl:
1098 case Instruction::LShr:
1099 case Instruction::AShr:{
1100 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
1101 switch (I->getOpcode()) {
1102 case Instruction::Add: Out << "Instruction::Add"; break;
1103 case Instruction::Sub: Out << "Instruction::Sub"; break;
1104 case Instruction::Mul: Out << "Instruction::Mul"; break;
1105 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1106 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1107 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
1108 case Instruction::URem:Out << "Instruction::URem"; break;
1109 case Instruction::SRem:Out << "Instruction::SRem"; break;
1110 case Instruction::FRem:Out << "Instruction::FRem"; break;
1111 case Instruction::And: Out << "Instruction::And"; break;
1112 case Instruction::Or: Out << "Instruction::Or"; break;
1113 case Instruction::Xor: Out << "Instruction::Xor"; break;
1114 case Instruction::Shl: Out << "Instruction::Shl"; break;
1115 case Instruction::LShr:Out << "Instruction::LShr"; break;
1116 case Instruction::AShr:Out << "Instruction::AShr"; break;
1117 default: Out << "Instruction::BadOpCode"; break;
1119 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1120 printEscapedString(I->getName());
1121 Out << "\", " << bbname << ");";
1124 case Instruction::FCmp: {
1125 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1126 switch (cast<FCmpInst>(I)->getPredicate()) {
1127 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1128 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1129 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1130 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1131 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1132 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1133 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1134 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1135 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1136 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1137 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1138 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1139 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1140 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1141 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1142 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1143 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1145 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1146 printEscapedString(I->getName());
1147 Out << "\", " << bbname << ");";
1150 case Instruction::ICmp: {
1151 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1152 switch (cast<ICmpInst>(I)->getPredicate()) {
1153 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1154 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1155 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1156 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1157 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1158 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1159 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1160 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1161 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1162 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1163 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
1165 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1166 printEscapedString(I->getName());
1167 Out << "\", " << bbname << ");";
1170 case Instruction::Malloc: {
1171 const MallocInst* mallocI = cast<MallocInst>(I);
1172 Out << "MallocInst* " << iName << " = new MallocInst("
1173 << getCppName(mallocI->getAllocatedType()) << ", ";
1174 if (mallocI->isArrayAllocation())
1175 Out << opNames[0] << ", " ;
1177 printEscapedString(mallocI->getName());
1178 Out << "\", " << bbname << ");";
1179 if (mallocI->getAlignment())
1180 nl(Out) << iName << "->setAlignment("
1181 << mallocI->getAlignment() << ");";
1184 case Instruction::Free: {
1185 Out << "FreeInst* " << iName << " = new FreeInst("
1186 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1189 case Instruction::Alloca: {
1190 const AllocaInst* allocaI = cast<AllocaInst>(I);
1191 Out << "AllocaInst* " << iName << " = new AllocaInst("
1192 << getCppName(allocaI->getAllocatedType()) << ", ";
1193 if (allocaI->isArrayAllocation())
1194 Out << opNames[0] << ", ";
1196 printEscapedString(allocaI->getName());
1197 Out << "\", " << bbname << ");";
1198 if (allocaI->getAlignment())
1199 nl(Out) << iName << "->setAlignment("
1200 << allocaI->getAlignment() << ");";
1203 case Instruction::Load:{
1204 const LoadInst* load = cast<LoadInst>(I);
1205 Out << "LoadInst* " << iName << " = new LoadInst("
1206 << opNames[0] << ", \"";
1207 printEscapedString(load->getName());
1208 Out << "\", " << (load->isVolatile() ? "true" : "false" )
1209 << ", " << bbname << ");";
1212 case Instruction::Store: {
1213 const StoreInst* store = cast<StoreInst>(I);
1214 Out << "StoreInst* " << iName << " = new StoreInst("
1215 << opNames[0] << ", "
1216 << opNames[1] << ", "
1217 << (store->isVolatile() ? "true" : "false")
1218 << ", " << bbname << ");";
1221 case Instruction::GetElementPtr: {
1222 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1223 if (gep->getNumOperands() <= 2) {
1224 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
1226 if (gep->getNumOperands() == 2)
1227 Out << ", " << opNames[1];
1229 Out << "std::vector<Value*> " << iName << "_indices;";
1231 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
1232 Out << iName << "_indices.push_back("
1233 << opNames[i] << ");";
1236 Out << "Instruction* " << iName << " = new GetElementPtrInst("
1237 << opNames[0] << ", " << iName << "_indices";
1240 printEscapedString(gep->getName());
1241 Out << "\", " << bbname << ");";
1244 case Instruction::PHI: {
1245 const PHINode* phi = cast<PHINode>(I);
1247 Out << "PHINode* " << iName << " = new PHINode("
1248 << getCppName(phi->getType()) << ", \"";
1249 printEscapedString(phi->getName());
1250 Out << "\", " << bbname << ");";
1251 nl(Out) << iName << "->reserveOperandSpace("
1252 << phi->getNumIncomingValues()
1255 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
1256 Out << iName << "->addIncoming("
1257 << opNames[i] << ", " << opNames[i+1] << ");";
1262 case Instruction::Trunc:
1263 case Instruction::ZExt:
1264 case Instruction::SExt:
1265 case Instruction::FPTrunc:
1266 case Instruction::FPExt:
1267 case Instruction::FPToUI:
1268 case Instruction::FPToSI:
1269 case Instruction::UIToFP:
1270 case Instruction::SIToFP:
1271 case Instruction::PtrToInt:
1272 case Instruction::IntToPtr:
1273 case Instruction::BitCast: {
1274 const CastInst* cst = cast<CastInst>(I);
1275 Out << "CastInst* " << iName << " = new ";
1276 switch (I->getOpcode()) {
1277 case Instruction::Trunc: Out << "TruncInst"; break;
1278 case Instruction::ZExt: Out << "ZExtInst"; break;
1279 case Instruction::SExt: Out << "SExtInst"; break;
1280 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1281 case Instruction::FPExt: Out << "FPExtInst"; break;
1282 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1283 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1284 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1285 case Instruction::SIToFP: Out << "SIToFPInst"; break;
1286 case Instruction::PtrToInt: Out << "PtrToInst"; break;
1287 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1288 case Instruction::BitCast: Out << "BitCastInst"; break;
1289 default: assert(!"Unreachable"); break;
1291 Out << "(" << opNames[0] << ", "
1292 << getCppName(cst->getType()) << ", \"";
1293 printEscapedString(cst->getName());
1294 Out << "\", " << bbname << ");";
1297 case Instruction::Call:{
1298 const CallInst* call = cast<CallInst>(I);
1299 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
1300 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
1301 << getCppName(ila->getFunctionType()) << ", \""
1302 << ila->getAsmString() << "\", \""
1303 << ila->getConstraintString() << "\","
1304 << (ila->hasSideEffects() ? "true" : "false") << ");";
1307 if (call->getNumOperands() > 3) {
1308 Out << "std::vector<Value*> " << iName << "_params;";
1310 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1311 Out << iName << "_params.push_back(" << opNames[i] << ");";
1314 Out << "CallInst* " << iName << " = new CallInst("
1315 << opNames[0] << ", " << iName << "_params, \"";
1316 } else if (call->getNumOperands() == 3) {
1317 Out << "CallInst* " << iName << " = new CallInst("
1318 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1319 } else if (call->getNumOperands() == 2) {
1320 Out << "CallInst* " << iName << " = new CallInst("
1321 << opNames[0] << ", " << opNames[1] << ", \"";
1323 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
1326 printEscapedString(call->getName());
1327 Out << "\", " << bbname << ");";
1328 nl(Out) << iName << "->setCallingConv(";
1329 printCallingConv(call->getCallingConv());
1331 nl(Out) << iName << "->setTailCall("
1332 << (call->isTailCall() ? "true":"false");
1336 case Instruction::Select: {
1337 const SelectInst* sel = cast<SelectInst>(I);
1338 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
1339 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1340 printEscapedString(sel->getName());
1341 Out << "\", " << bbname << ");";
1344 case Instruction::UserOp1:
1346 case Instruction::UserOp2: {
1347 /// FIXME: What should be done here?
1350 case Instruction::VAArg: {
1351 const VAArgInst* va = cast<VAArgInst>(I);
1352 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
1353 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
1354 printEscapedString(va->getName());
1355 Out << "\", " << bbname << ");";
1358 case Instruction::ExtractElement: {
1359 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
1360 Out << "ExtractElementInst* " << getCppName(eei)
1361 << " = new ExtractElementInst(" << opNames[0]
1362 << ", " << opNames[1] << ", \"";
1363 printEscapedString(eei->getName());
1364 Out << "\", " << bbname << ");";
1367 case Instruction::InsertElement: {
1368 const InsertElementInst* iei = cast<InsertElementInst>(I);
1369 Out << "InsertElementInst* " << getCppName(iei)
1370 << " = new InsertElementInst(" << opNames[0]
1371 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1372 printEscapedString(iei->getName());
1373 Out << "\", " << bbname << ");";
1376 case Instruction::ShuffleVector: {
1377 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
1378 Out << "ShuffleVectorInst* " << getCppName(svi)
1379 << " = new ShuffleVectorInst(" << opNames[0]
1380 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1381 printEscapedString(svi->getName());
1382 Out << "\", " << bbname << ");";
1386 DefinedValues.insert(I);
1391 // Print out the types, constants and declarations needed by one function
1392 void CppWriter::printFunctionUses(const Function* F) {
1394 nl(Out) << "// Type Definitions"; nl(Out);
1396 // Print the function's return type
1397 printType(F->getReturnType());
1399 // Print the function's function type
1400 printType(F->getFunctionType());
1402 // Print the types of each of the function's arguments
1403 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1405 printType(AI->getType());
1409 // Print type definitions for every type referenced by an instruction and
1410 // make a note of any global values or constants that are referenced
1411 std::vector<GlobalValue*> gvs;
1412 std::vector<Constant*> consts;
1413 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1414 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1416 // Print the type of the instruction itself
1417 printType(I->getType());
1419 // Print the type of each of the instruction's operands
1420 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1421 Value* operand = I->getOperand(i);
1422 printType(operand->getType());
1423 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1425 else if (Constant* C = dyn_cast<Constant>(operand))
1426 consts.push_back(C);
1431 // Print the function declarations for any functions encountered
1432 nl(Out) << "// Function Declarations"; nl(Out);
1433 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1435 if (Function* Fun = dyn_cast<Function>(*I)) {
1436 if (!is_inline || Fun != F)
1437 printFunctionHead(Fun);
1441 // Print the global variable declarations for any variables encountered
1442 nl(Out) << "// Global Variable Declarations"; nl(Out);
1443 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1445 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1446 printVariableHead(F);
1449 // Print the constants found
1450 nl(Out) << "// Constant Definitions"; nl(Out);
1451 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1456 // Process the global variables definitions now that all the constants have
1457 // been emitted. These definitions just couple the gvars with their constant
1459 nl(Out) << "// Global Variable Definitions"; nl(Out);
1460 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1462 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1463 printVariableBody(GV);
1467 void CppWriter::printFunctionHead(const Function* F) {
1468 nl(Out) << "Function* " << getCppName(F);
1470 Out << " = mod->getFunction(\"";
1471 printEscapedString(F->getName());
1472 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1473 nl(Out) << "if (!" << getCppName(F) << ") {";
1474 nl(Out) << getCppName(F);
1476 Out<< " = new Function(";
1477 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1478 nl(Out) << "/*Linkage=*/";
1479 printLinkageType(F->getLinkage());
1481 nl(Out) << "/*Name=*/\"";
1482 printEscapedString(F->getName());
1483 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
1486 Out << "->setCallingConv(";
1487 printCallingConv(F->getCallingConv());
1490 if (F->hasSection()) {
1492 Out << "->setSection(\"" << F->getSection() << "\");";
1495 if (F->getAlignment()) {
1497 Out << "->setAlignment(" << F->getAlignment() << ");";
1506 void CppWriter::printFunctionBody(const Function *F) {
1507 if (F->isDeclaration())
1508 return; // external functions have no bodies.
1510 // Clear the DefinedValues and ForwardRefs maps because we can't have
1511 // cross-function forward refs
1512 ForwardRefs.clear();
1513 DefinedValues.clear();
1515 // Create all the argument values
1517 if (!F->arg_empty()) {
1518 Out << "Function::arg_iterator args = " << getCppName(F)
1519 << "->arg_begin();";
1522 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1524 Out << "Value* " << getCppName(AI) << " = args++;";
1526 if (AI->hasName()) {
1527 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1533 // Create all the basic blocks
1535 for (Function::const_iterator BI = F->begin(), BE = F->end();
1537 std::string bbname(getCppName(BI));
1538 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
1540 printEscapedString(BI->getName());
1541 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1545 // Output all of its basic blocks... for the function
1546 for (Function::const_iterator BI = F->begin(), BE = F->end();
1548 std::string bbname(getCppName(BI));
1549 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1552 // Output all of the instructions in the basic block...
1553 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1555 printInstruction(I,bbname);
1559 // Loop over the ForwardRefs and resolve them now that all instructions
1561 if (!ForwardRefs.empty()) {
1562 nl(Out) << "// Resolve Forward References";
1566 while (!ForwardRefs.empty()) {
1567 ForwardRefMap::iterator I = ForwardRefs.begin();
1568 Out << I->second << "->replaceAllUsesWith("
1569 << getCppName(I->first) << "); delete " << I->second << ";";
1571 ForwardRefs.erase(I);
1575 void CppWriter::printInline(const std::string& fname, const std::string& func) {
1576 const Function* F = TheModule->getFunction(func);
1578 error(std::string("Function '") + func + "' not found in input module");
1581 if (F->isDeclaration()) {
1582 error(std::string("Function '") + func + "' is external!");
1585 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
1587 unsigned arg_count = 1;
1588 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1590 Out << ", Value* arg_" << arg_count;
1595 printFunctionUses(F);
1596 printFunctionBody(F);
1598 Out << "return " << getCppName(F->begin()) << ";";
1603 void CppWriter::printModuleBody() {
1604 // Print out all the type definitions
1605 nl(Out) << "// Type Definitions"; nl(Out);
1606 printTypes(TheModule);
1608 // Functions can call each other and global variables can reference them so
1609 // define all the functions first before emitting their function bodies.
1610 nl(Out) << "// Function Declarations"; nl(Out);
1611 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1613 printFunctionHead(I);
1615 // Process the global variables declarations. We can't initialze them until
1616 // after the constants are printed so just print a header for each global
1617 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
1618 for (Module::const_global_iterator I = TheModule->global_begin(),
1619 E = TheModule->global_end(); I != E; ++I) {
1620 printVariableHead(I);
1623 // Print out all the constants definitions. Constants don't recurse except
1624 // through GlobalValues. All GlobalValues have been declared at this point
1625 // so we can proceed to generate the constants.
1626 nl(Out) << "// Constant Definitions"; nl(Out);
1627 printConstants(TheModule);
1629 // Process the global variables definitions now that all the constants have
1630 // been emitted. These definitions just couple the gvars with their constant
1632 nl(Out) << "// Global Variable Definitions"; nl(Out);
1633 for (Module::const_global_iterator I = TheModule->global_begin(),
1634 E = TheModule->global_end(); I != E; ++I) {
1635 printVariableBody(I);
1638 // Finally, we can safely put out all of the function bodies.
1639 nl(Out) << "// Function Definitions"; nl(Out);
1640 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1642 if (!I->isDeclaration()) {
1643 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1647 printFunctionBody(I);
1654 void CppWriter::printProgram(
1655 const std::string& fname,
1656 const std::string& mName
1658 Out << "#include <llvm/Module.h>\n";
1659 Out << "#include <llvm/DerivedTypes.h>\n";
1660 Out << "#include <llvm/Constants.h>\n";
1661 Out << "#include <llvm/GlobalVariable.h>\n";
1662 Out << "#include <llvm/Function.h>\n";
1663 Out << "#include <llvm/CallingConv.h>\n";
1664 Out << "#include <llvm/BasicBlock.h>\n";
1665 Out << "#include <llvm/Instructions.h>\n";
1666 Out << "#include <llvm/InlineAsm.h>\n";
1667 Out << "#include <llvm/Support/MathExtras.h>\n";
1668 Out << "#include <llvm/Pass.h>\n";
1669 Out << "#include <llvm/PassManager.h>\n";
1670 Out << "#include <llvm/Analysis/Verifier.h>\n";
1671 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1672 Out << "#include <algorithm>\n";
1673 Out << "#include <iostream>\n\n";
1674 Out << "using namespace llvm;\n\n";
1675 Out << "Module* " << fname << "();\n\n";
1676 Out << "int main(int argc, char**argv) {\n";
1677 Out << " Module* Mod = makeLLVMModule();\n";
1678 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1679 Out << " std::cerr.flush();\n";
1680 Out << " std::cout.flush();\n";
1681 Out << " PassManager PM;\n";
1682 Out << " PM.add(new PrintModulePass(&std::cout));\n";
1683 Out << " PM.run(*Mod);\n";
1684 Out << " return 0;\n";
1686 printModule(fname,mName);
1689 void CppWriter::printModule(
1690 const std::string& fname,
1691 const std::string& mName
1693 nl(Out) << "Module* " << fname << "() {";
1694 nl(Out,1) << "// Module Construction";
1695 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1696 nl(Out) << "mod->setEndianness(";
1697 switch (TheModule->getEndianness()) {
1698 case Module::LittleEndian: Out << "Module::LittleEndian);"; break;
1699 case Module::BigEndian: Out << "Module::BigEndian);"; break;
1700 case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break;
1702 nl(Out) << "mod->setPointerSize(";
1703 switch (TheModule->getPointerSize()) {
1704 case Module::Pointer32: Out << "Module::Pointer32);"; break;
1705 case Module::Pointer64: Out << "Module::Pointer64);"; break;
1706 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break;
1709 if (!TheModule->getTargetTriple().empty()) {
1710 Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1715 if (!TheModule->getModuleInlineAsm().empty()) {
1716 Out << "mod->setModuleInlineAsm(\"";
1717 printEscapedString(TheModule->getModuleInlineAsm());
1722 // Loop over the dependent libraries and emit them.
1723 Module::lib_iterator LI = TheModule->lib_begin();
1724 Module::lib_iterator LE = TheModule->lib_end();
1726 Out << "mod->addLibrary(\"" << *LI << "\");";
1731 nl(Out) << "return mod;";
1736 void CppWriter::printContents(
1737 const std::string& fname, // Name of generated function
1738 const std::string& mName // Name of module generated module
1740 Out << "\nModule* " << fname << "(Module *mod) {\n";
1741 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
1743 Out << "\nreturn mod;\n";
1747 void CppWriter::printFunction(
1748 const std::string& fname, // Name of generated function
1749 const std::string& funcName // Name of function to generate
1751 const Function* F = TheModule->getFunction(funcName);
1753 error(std::string("Function '") + funcName + "' not found in input module");
1756 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1757 printFunctionUses(F);
1758 printFunctionHead(F);
1759 printFunctionBody(F);
1760 Out << "return " << getCppName(F) << ";\n";
1764 void CppWriter::printVariable(
1765 const std::string& fname, /// Name of generated function
1766 const std::string& varName // Name of variable to generate
1768 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1771 error(std::string("Variable '") + varName + "' not found in input module");
1774 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1775 printVariableUses(GV);
1776 printVariableHead(GV);
1777 printVariableBody(GV);
1778 Out << "return " << getCppName(GV) << ";\n";
1782 void CppWriter::printType(
1783 const std::string& fname, /// Name of generated function
1784 const std::string& typeName // Name of type to generate
1786 const Type* Ty = TheModule->getTypeByName(typeName);
1788 error(std::string("Type '") + typeName + "' not found in input module");
1791 Out << "\nType* " << fname << "(Module *mod) {\n";
1793 Out << "return " << getCppName(Ty) << ";\n";
1797 } // end anonymous llvm
1801 void WriteModuleToCppFile(Module* mod, std::ostream& o) {
1802 // Initialize a CppWriter for us to use
1803 CppWriter W(o, mod);
1806 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
1808 // Get the name of the function we're supposed to generate
1809 std::string fname = FuncName.getValue();
1811 // Get the name of the thing we are to generate
1812 std::string tgtname = NameToGenerate.getValue();
1813 if (GenerationType == GenModule ||
1814 GenerationType == GenContents ||
1815 GenerationType == GenProgram) {
1816 if (tgtname == "!bad!") {
1817 if (mod->getModuleIdentifier() == "-")
1818 tgtname = "<stdin>";
1820 tgtname = mod->getModuleIdentifier();
1822 } else if (tgtname == "!bad!") {
1823 W.error("You must use the -for option with -gen-{function,variable,type}");
1826 switch (WhatToGenerate(GenerationType)) {
1829 fname = "makeLLVMModule";
1830 W.printProgram(fname,tgtname);
1834 fname = "makeLLVMModule";
1835 W.printModule(fname,tgtname);
1839 fname = "makeLLVMModuleContents";
1840 W.printContents(fname,tgtname);
1844 fname = "makeLLVMFunction";
1845 W.printFunction(fname,tgtname);
1849 fname = "makeLLVMInline";
1850 W.printInline(fname,tgtname);
1854 fname = "makeLLVMVariable";
1855 W.printVariable(fname,tgtname);
1859 fname = "makeLLVMType";
1860 W.printType(fname,tgtname);
1863 W.error("Invalid generation option");