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/Module.h"
22 #include "llvm/SymbolTable.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Config/config.h"
36 static cl::opt<std::string>
37 FuncName("funcname", cl::desc("Specify the name of the generated function"),
38 cl::value_desc("function name"));
50 static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
51 cl::desc("Choose what kind of output to generate"),
54 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
55 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
56 clEnumValN(GenContents,"gen-contents", "Generate contents of a module"),
57 clEnumValN(GenFunction,"gen-function", "Generate a function definition"),
58 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
59 clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"),
60 clEnumValN(GenType, "gen-type", "Generate a type definition"),
65 static cl::opt<std::string> NameToGenerate("for", cl::Optional,
66 cl::desc("Specify the name of the thing to generate"),
70 typedef std::vector<const Type*> TypeList;
71 typedef std::map<const Type*,std::string> TypeMap;
72 typedef std::map<const Value*,std::string> ValueMap;
73 typedef std::set<std::string> NameSet;
74 typedef std::set<const Type*> TypeSet;
75 typedef std::set<const Value*> ValueSet;
76 typedef std::map<const Value*,std::string> ForwardRefMap;
81 const Module *TheModule;
85 TypeMap UnresolvedTypes;
89 ValueSet DefinedValues;
90 ForwardRefMap ForwardRefs;
94 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
95 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
96 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
98 const Module* getModule() { return TheModule; }
100 void printProgram(const std::string& fname, const std::string& modName );
101 void printModule(const std::string& fname, const std::string& modName );
102 void printContents(const std::string& fname, const std::string& modName );
103 void printFunction(const std::string& fname, const std::string& funcName );
104 void printInline(const std::string& fname, const std::string& funcName );
105 void printVariable(const std::string& fname, const std::string& varName );
106 void printType(const std::string& fname, const std::string& typeName );
108 void error(const std::string& msg);
111 void printLinkageType(GlobalValue::LinkageTypes LT);
112 void printCallingConv(unsigned cc);
113 void printEscapedString(const std::string& str);
114 void printCFP(const ConstantFP* CFP);
116 std::string getCppName(const Type* val);
117 inline void printCppName(const Type* val);
119 std::string getCppName(const Value* val);
120 inline void printCppName(const Value* val);
122 bool printTypeInternal(const Type* Ty);
123 inline void printType(const Type* Ty);
124 void printTypes(const Module* M);
126 void printConstant(const Constant *CPV);
127 void printConstants(const Module* M);
129 void printVariableUses(const GlobalVariable *GV);
130 void printVariableHead(const GlobalVariable *GV);
131 void printVariableBody(const GlobalVariable *GV);
133 void printFunctionUses(const Function *F);
134 void printFunctionHead(const Function *F);
135 void printFunctionBody(const Function *F);
136 void printInstruction(const Instruction *I, const std::string& bbname);
137 std::string getOpName(Value*);
139 void printModuleBody();
143 static unsigned indent_level = 0;
144 inline std::ostream& nl(std::ostream& Out, int delta = 0) {
146 if (delta >= 0 || indent_level >= unsigned(-delta))
147 indent_level += delta;
148 for (unsigned i = 0; i < indent_level; ++i)
153 inline void in() { indent_level++; }
154 inline void out() { if (indent_level >0) indent_level--; }
157 sanitize(std::string& str) {
158 for (size_t i = 0; i < str.length(); ++i)
159 if (!isalnum(str[i]) && str[i] != '_')
164 getTypePrefix(const Type* Ty ) {
166 switch (Ty->getTypeID()) {
167 case Type::VoidTyID: prefix = "void_"; break;
168 case Type::BoolTyID: prefix = "bool_"; break;
169 case Type::UByteTyID: prefix = "ubyte_"; break;
170 case Type::SByteTyID: prefix = "sbyte_"; break;
171 case Type::UShortTyID: prefix = "ushort_"; break;
172 case Type::ShortTyID: prefix = "short_"; break;
173 case Type::UIntTyID: prefix = "uint_"; break;
174 case Type::IntTyID: prefix = "int_"; break;
175 case Type::ULongTyID: prefix = "ulong_"; break;
176 case Type::LongTyID: prefix = "long_"; break;
177 case Type::FloatTyID: prefix = "float_"; break;
178 case Type::DoubleTyID: prefix = "double_"; break;
179 case Type::LabelTyID: prefix = "label_"; break;
180 case Type::FunctionTyID: prefix = "func_"; break;
181 case Type::StructTyID: prefix = "struct_"; break;
182 case Type::ArrayTyID: prefix = "array_"; break;
183 case Type::PointerTyID: prefix = "ptr_"; break;
184 case Type::PackedTyID: prefix = "packed_"; break;
185 case Type::OpaqueTyID: prefix = "opaque_"; break;
186 default: prefix = "other_"; break;
191 // Looks up the type in the symbol table and returns a pointer to its name or
192 // a null pointer if it wasn't found. Note that this isn't the same as the
193 // Mode::getTypeName function which will return an empty string, not a null
194 // pointer if the name is not found.
195 inline const std::string*
196 findTypeName(const SymbolTable& ST, const Type* Ty)
198 SymbolTable::type_const_iterator TI = ST.type_begin();
199 SymbolTable::type_const_iterator TE = ST.type_end();
200 for (;TI != TE; ++TI)
201 if (TI->second == Ty)
207 CppWriter::error(const std::string& msg) {
208 std::cerr << progname << ": " << msg << "\n";
212 // printCFP - Print a floating point constant .. very carefully :)
213 // This makes sure that conversion to/from floating yields the same binary
214 // result so that we don't lose precision.
216 CppWriter::printCFP(const ConstantFP *CFP) {
217 Out << "ConstantFP::get(";
218 if (CFP->getType() == Type::DoubleTy)
219 Out << "Type::DoubleTy, ";
221 Out << "Type::FloatTy, ";
224 sprintf(Buffer, "%A", CFP->getValue());
225 if ((!strncmp(Buffer, "0x", 2) ||
226 !strncmp(Buffer, "-0x", 3) ||
227 !strncmp(Buffer, "+0x", 3)) &&
228 (atof(Buffer) == CFP->getValue()))
229 if (CFP->getType() == Type::DoubleTy)
230 Out << "BitsToDouble(" << Buffer << ")";
232 Out << "BitsToFloat(" << Buffer << ")";
235 std::string StrVal = ftostr(CFP->getValue());
237 while (StrVal[0] == ' ')
238 StrVal.erase(StrVal.begin());
240 // Check to make sure that the stringized number is not some string like
241 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
242 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
243 ((StrVal[0] == '-' || StrVal[0] == '+') &&
244 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
245 (atof(StrVal.c_str()) == CFP->getValue()))
246 if (CFP->getType() == Type::DoubleTy)
250 else if (CFP->getType() == Type::DoubleTy)
251 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
252 << std::dec << "ULL) /* " << StrVal << " */";
254 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
255 << std::dec << "U) /* " << StrVal << " */";
263 CppWriter::printCallingConv(unsigned cc){
264 // Print the calling convention.
266 case CallingConv::C: Out << "CallingConv::C"; break;
267 case CallingConv::CSRet: Out << "CallingConv::CSRet"; break;
268 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
269 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
270 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
271 default: Out << cc; break;
276 CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
278 case GlobalValue::InternalLinkage:
279 Out << "GlobalValue::InternalLinkage"; break;
280 case GlobalValue::LinkOnceLinkage:
281 Out << "GlobalValue::LinkOnceLinkage "; break;
282 case GlobalValue::WeakLinkage:
283 Out << "GlobalValue::WeakLinkage"; break;
284 case GlobalValue::AppendingLinkage:
285 Out << "GlobalValue::AppendingLinkage"; break;
286 case GlobalValue::ExternalLinkage:
287 Out << "GlobalValue::ExternalLinkage"; break;
288 case GlobalValue::DLLImportLinkage:
289 Out << "GlobalValue::DllImportLinkage"; break;
290 case GlobalValue::DLLExportLinkage:
291 Out << "GlobalValue::DllExportLinkage"; break;
292 case GlobalValue::ExternalWeakLinkage:
293 Out << "GlobalValue::ExternalWeakLinkage"; break;
294 case GlobalValue::GhostLinkage:
295 Out << "GlobalValue::GhostLinkage"; break;
299 // printEscapedString - Print each character of the specified string, escaping
300 // it if it is not printable or if it is an escape char.
302 CppWriter::printEscapedString(const std::string &Str) {
303 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
304 unsigned char C = Str[i];
305 if (isprint(C) && C != '"' && C != '\\') {
309 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
310 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
316 CppWriter::getCppName(const Type* Ty)
318 // First, handle the primitive types .. easy
319 if (Ty->isPrimitiveType()) {
320 switch (Ty->getTypeID()) {
321 case Type::VoidTyID: return "Type::VoidTy";
322 case Type::BoolTyID: return "Type::BoolTy";
323 case Type::UByteTyID: return "Type::UByteTy";
324 case Type::SByteTyID: return "Type::SByteTy";
325 case Type::UShortTyID: return "Type::UShortTy";
326 case Type::ShortTyID: return "Type::ShortTy";
327 case Type::UIntTyID: return "Type::UIntTy";
328 case Type::IntTyID: return "Type::IntTy";
329 case Type::ULongTyID: return "Type::ULongTy";
330 case Type::LongTyID: return "Type::LongTy";
331 case Type::FloatTyID: return "Type::FloatTy";
332 case Type::DoubleTyID: return "Type::DoubleTy";
333 case Type::LabelTyID: return "Type::LabelTy";
335 error("Invalid primitive type");
338 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
341 // Now, see if we've seen the type before and return that
342 TypeMap::iterator I = TypeNames.find(Ty);
343 if (I != TypeNames.end())
346 // Okay, let's build a new name for this type. Start with a prefix
347 const char* prefix = 0;
348 switch (Ty->getTypeID()) {
349 case Type::FunctionTyID: prefix = "FuncTy_"; break;
350 case Type::StructTyID: prefix = "StructTy_"; break;
351 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
352 case Type::PointerTyID: prefix = "PointerTy_"; break;
353 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
354 case Type::PackedTyID: prefix = "PackedTy_"; break;
355 default: prefix = "OtherTy_"; break; // prevent breakage
358 // See if the type has a name in the symboltable and build accordingly
359 const std::string* tName = findTypeName(TheModule->getSymbolTable(), Ty);
362 name = std::string(prefix) + *tName;
364 name = std::string(prefix) + utostr(uniqueNum++);
368 return TypeNames[Ty] = name;
372 CppWriter::printCppName(const Type* Ty)
374 printEscapedString(getCppName(Ty));
378 CppWriter::getCppName(const Value* val) {
380 ValueMap::iterator I = ValueNames.find(val);
381 if (I != ValueNames.end() && I->first == val)
384 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
385 name = std::string("gvar_") +
386 getTypePrefix(GV->getType()->getElementType());
387 } else if (isa<Function>(val)) {
388 name = std::string("func_");
389 } else if (const Constant* C = dyn_cast<Constant>(val)) {
390 name = std::string("const_") + getTypePrefix(C->getType());
391 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
393 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
394 Function::const_arg_iterator(Arg)) + 1;
395 name = std::string("arg_") + utostr(argNum);
396 NameSet::iterator NI = UsedNames.find(name);
397 if (NI != UsedNames.end())
398 name += std::string("_") + utostr(uniqueNum++);
399 UsedNames.insert(name);
400 return ValueNames[val] = name;
402 name = getTypePrefix(val->getType());
405 name = getTypePrefix(val->getType());
407 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
409 NameSet::iterator NI = UsedNames.find(name);
410 if (NI != UsedNames.end())
411 name += std::string("_") + utostr(uniqueNum++);
412 UsedNames.insert(name);
413 return ValueNames[val] = name;
417 CppWriter::printCppName(const Value* val) {
418 printEscapedString(getCppName(val));
422 CppWriter::printTypeInternal(const Type* Ty) {
423 // We don't print definitions for primitive types
424 if (Ty->isPrimitiveType())
427 // If we already defined this type, we don't need to define it again.
428 if (DefinedTypes.find(Ty) != DefinedTypes.end())
431 // Everything below needs the name for the type so get it now.
432 std::string typeName(getCppName(Ty));
434 // Search the type stack for recursion. If we find it, then generate this
435 // as an OpaqueType, but make sure not to do this multiple times because
436 // the type could appear in multiple places on the stack. Once the opaque
437 // definition is issued, it must not be re-issued. Consequently we have to
438 // check the UnresolvedTypes list as well.
439 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
440 if (TI != TypeStack.end()) {
441 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
442 if (I == UnresolvedTypes.end()) {
443 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
445 UnresolvedTypes[Ty] = typeName;
450 // We're going to print a derived type which, by definition, contains other
451 // types. So, push this one we're printing onto the type stack to assist with
452 // recursive definitions.
453 TypeStack.push_back(Ty);
455 // Print the type definition
456 switch (Ty->getTypeID()) {
457 case Type::FunctionTyID: {
458 const FunctionType* FT = cast<FunctionType>(Ty);
459 Out << "std::vector<const Type*>" << typeName << "_args;";
461 FunctionType::param_iterator PI = FT->param_begin();
462 FunctionType::param_iterator PE = FT->param_end();
463 for (; PI != PE; ++PI) {
464 const Type* argTy = static_cast<const Type*>(*PI);
465 bool isForward = printTypeInternal(argTy);
466 std::string argName(getCppName(argTy));
467 Out << typeName << "_args.push_back(" << argName;
473 bool isForward = printTypeInternal(FT->getReturnType());
474 std::string retTypeName(getCppName(FT->getReturnType()));
475 Out << "FunctionType* " << typeName << " = FunctionType::get(";
476 in(); nl(Out) << "/*Result=*/" << retTypeName;
480 nl(Out) << "/*Params=*/" << typeName << "_args,";
481 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
486 case Type::StructTyID: {
487 const StructType* ST = cast<StructType>(Ty);
488 Out << "std::vector<const Type*>" << typeName << "_fields;";
490 StructType::element_iterator EI = ST->element_begin();
491 StructType::element_iterator EE = ST->element_end();
492 for (; EI != EE; ++EI) {
493 const Type* fieldTy = static_cast<const Type*>(*EI);
494 bool isForward = printTypeInternal(fieldTy);
495 std::string fieldName(getCppName(fieldTy));
496 Out << typeName << "_fields.push_back(" << fieldName;
502 Out << "StructType* " << typeName << " = StructType::get("
503 << typeName << "_fields);";
507 case Type::ArrayTyID: {
508 const ArrayType* AT = cast<ArrayType>(Ty);
509 const Type* ET = AT->getElementType();
510 bool isForward = printTypeInternal(ET);
511 std::string elemName(getCppName(ET));
512 Out << "ArrayType* " << typeName << " = ArrayType::get("
513 << elemName << (isForward ? "_fwd" : "")
514 << ", " << utostr(AT->getNumElements()) << ");";
518 case Type::PointerTyID: {
519 const PointerType* PT = cast<PointerType>(Ty);
520 const Type* ET = PT->getElementType();
521 bool isForward = printTypeInternal(ET);
522 std::string elemName(getCppName(ET));
523 Out << "PointerType* " << typeName << " = PointerType::get("
524 << elemName << (isForward ? "_fwd" : "") << ");";
528 case Type::PackedTyID: {
529 const PackedType* PT = cast<PackedType>(Ty);
530 const Type* ET = PT->getElementType();
531 bool isForward = printTypeInternal(ET);
532 std::string elemName(getCppName(ET));
533 Out << "PackedType* " << typeName << " = PackedType::get("
534 << elemName << (isForward ? "_fwd" : "")
535 << ", " << utostr(PT->getNumElements()) << ");";
539 case Type::OpaqueTyID: {
540 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
545 error("Invalid TypeID");
548 // If the type had a name, make sure we recreate it.
549 const std::string* progTypeName =
550 findTypeName(TheModule->getSymbolTable(),Ty);
552 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
556 // Pop us off the type stack
557 TypeStack.pop_back();
559 // Indicate that this type is now defined.
560 DefinedTypes.insert(Ty);
562 // Early resolve as many unresolved types as possible. Search the unresolved
563 // types map for the type we just printed. Now that its definition is complete
564 // we can resolve any previous references to it. This prevents a cascade of
566 TypeMap::iterator I = UnresolvedTypes.find(Ty);
567 if (I != UnresolvedTypes.end()) {
568 Out << "cast<OpaqueType>(" << I->second
569 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
571 Out << I->second << " = cast<";
572 switch (Ty->getTypeID()) {
573 case Type::FunctionTyID: Out << "FunctionType"; break;
574 case Type::ArrayTyID: Out << "ArrayType"; break;
575 case Type::StructTyID: Out << "StructType"; break;
576 case Type::PackedTyID: Out << "PackedType"; break;
577 case Type::PointerTyID: Out << "PointerType"; break;
578 case Type::OpaqueTyID: Out << "OpaqueType"; break;
579 default: Out << "NoSuchDerivedType"; break;
581 Out << ">(" << I->second << "_fwd.get());";
583 UnresolvedTypes.erase(I);
586 // Finally, separate the type definition from other with a newline.
589 // We weren't a recursive type
593 // Prints a type definition. Returns true if it could not resolve all the types
594 // in the definition but had to use a forward reference.
596 CppWriter::printType(const Type* Ty) {
597 assert(TypeStack.empty());
599 printTypeInternal(Ty);
600 assert(TypeStack.empty());
604 CppWriter::printTypes(const Module* M) {
606 // Walk the symbol table and print out all its types
607 const SymbolTable& symtab = M->getSymbolTable();
608 for (SymbolTable::type_const_iterator TI = symtab.type_begin(),
609 TE = symtab.type_end(); TI != TE; ++TI) {
611 // For primitive types and types already defined, just add a name
612 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
613 if (TI->second->isPrimitiveType() || TNI != TypeNames.end()) {
614 Out << "mod->addTypeName(\"";
615 printEscapedString(TI->first);
616 Out << "\", " << getCppName(TI->second) << ");";
618 // For everything else, define the type
620 printType(TI->second);
624 // Add all of the global variables to the value table...
625 for (Module::const_global_iterator I = TheModule->global_begin(),
626 E = TheModule->global_end(); I != E; ++I) {
627 if (I->hasInitializer())
628 printType(I->getInitializer()->getType());
629 printType(I->getType());
632 // Add all the functions to the table
633 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
635 printType(FI->getReturnType());
636 printType(FI->getFunctionType());
637 // Add all the function arguments
638 for(Function::const_arg_iterator AI = FI->arg_begin(),
639 AE = FI->arg_end(); AI != AE; ++AI) {
640 printType(AI->getType());
643 // Add all of the basic blocks and instructions
644 for (Function::const_iterator BB = FI->begin(),
645 E = FI->end(); BB != E; ++BB) {
646 printType(BB->getType());
647 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
649 printType(I->getType());
650 for (unsigned i = 0; i < I->getNumOperands(); ++i)
651 printType(I->getOperand(i)->getType());
658 // printConstant - Print out a constant pool entry...
659 void CppWriter::printConstant(const Constant *CV) {
660 // First, if the constant is actually a GlobalValue (variable or function) or
661 // its already in the constant list then we've printed it already and we can
663 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
666 std::string constName(getCppName(CV));
667 std::string typeName(getCppName(CV->getType()));
668 if (CV->isNullValue()) {
669 Out << "Constant* " << constName << " = Constant::getNullValue("
674 if (isa<GlobalValue>(CV)) {
675 // Skip variables and functions, we emit them elsewhere
678 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
679 Out << "ConstantBool* " << constName << " = ConstantBool::get("
680 << (CB->getValue() ? "true" : "false") << ");";
681 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
682 Out << "ConstantInt* " << constName << " = ConstantInt::get("
683 << typeName << ", " << CI->getZExtValue() << ");";
684 } else if (isa<ConstantAggregateZero>(CV)) {
685 Out << "ConstantAggregateZero* " << constName
686 << " = ConstantAggregateZero::get(" << typeName << ");";
687 } else if (isa<ConstantPointerNull>(CV)) {
688 Out << "ConstantPointerNull* " << constName
689 << " = ConstanPointerNull::get(" << typeName << ");";
690 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
691 Out << "ConstantFP* " << constName << " = ";
694 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
695 if (CA->isString() && CA->getType()->getElementType() == Type::SByteTy) {
696 Out << "Constant* " << constName << " = ConstantArray::get(\"";
697 printEscapedString(CA->getAsString());
698 // Determine if we want null termination or not.
699 if (CA->getType()->getNumElements() <= CA->getAsString().length())
700 Out << "\", false";// No null terminator
702 Out << "\", true"; // Indicate that the null terminator should be added.
705 Out << "std::vector<Constant*> " << constName << "_elems;";
707 unsigned N = CA->getNumOperands();
708 for (unsigned i = 0; i < N; ++i) {
709 printConstant(CA->getOperand(i)); // recurse to print operands
710 Out << constName << "_elems.push_back("
711 << getCppName(CA->getOperand(i)) << ");";
714 Out << "Constant* " << constName << " = ConstantArray::get("
715 << typeName << ", " << constName << "_elems);";
717 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
718 Out << "std::vector<Constant*> " << constName << "_fields;";
720 unsigned N = CS->getNumOperands();
721 for (unsigned i = 0; i < N; i++) {
722 printConstant(CS->getOperand(i));
723 Out << constName << "_fields.push_back("
724 << getCppName(CS->getOperand(i)) << ");";
727 Out << "Constant* " << constName << " = ConstantStruct::get("
728 << typeName << ", " << constName << "_fields);";
729 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
730 Out << "std::vector<Constant*> " << constName << "_elems;";
732 unsigned N = CP->getNumOperands();
733 for (unsigned i = 0; i < N; ++i) {
734 printConstant(CP->getOperand(i));
735 Out << constName << "_elems.push_back("
736 << getCppName(CP->getOperand(i)) << ");";
739 Out << "Constant* " << constName << " = ConstantPacked::get("
740 << typeName << ", " << constName << "_elems);";
741 } else if (isa<UndefValue>(CV)) {
742 Out << "UndefValue* " << constName << " = UndefValue::get("
744 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
745 if (CE->getOpcode() == Instruction::GetElementPtr) {
746 Out << "std::vector<Constant*> " << constName << "_indices;";
748 printConstant(CE->getOperand(0));
749 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
750 printConstant(CE->getOperand(i));
751 Out << constName << "_indices.push_back("
752 << getCppName(CE->getOperand(i)) << ");";
755 Out << "Constant* " << constName
756 << " = ConstantExpr::getGetElementPtr("
757 << getCppName(CE->getOperand(0)) << ", "
758 << constName << "_indices);";
759 } else if (CE->isCast()) {
760 printConstant(CE->getOperand(0));
761 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
762 switch (CE->getOpcode()) {
763 default: assert(0 && "Invalid cast opcode");
764 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
765 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
766 case Instruction::SExt: Out << "Instruction::SExt"; break;
767 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
768 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
769 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
770 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
771 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
772 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
773 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
774 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
775 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
777 Out << ", " << getCppName(CE->getOperand(0)) << ", "
778 << getCppName(CE->getType()) << ");";
780 unsigned N = CE->getNumOperands();
781 for (unsigned i = 0; i < N; ++i ) {
782 printConstant(CE->getOperand(i));
784 Out << "Constant* " << constName << " = ConstantExpr::";
785 switch (CE->getOpcode()) {
786 case Instruction::Add: Out << "getAdd"; break;
787 case Instruction::Sub: Out << "getSub"; break;
788 case Instruction::Mul: Out << "getMul"; break;
789 case Instruction::UDiv: Out << "getUDiv"; break;
790 case Instruction::SDiv: Out << "getSDiv"; break;
791 case Instruction::FDiv: Out << "getFDiv"; break;
792 case Instruction::URem: Out << "getURem"; break;
793 case Instruction::SRem: Out << "getSRem"; break;
794 case Instruction::FRem: Out << "getFRem"; break;
795 case Instruction::And: Out << "getAnd"; break;
796 case Instruction::Or: Out << "getOr"; break;
797 case Instruction::Xor: Out << "getXor"; break;
798 case Instruction::SetEQ: Out << "getSetEQ"; break;
799 case Instruction::SetNE: Out << "getSetNE"; break;
800 case Instruction::SetLE: Out << "getSetLE"; break;
801 case Instruction::SetGE: Out << "getSetGE"; break;
802 case Instruction::SetLT: Out << "getSetLT"; break;
803 case Instruction::SetGT: Out << "getSetGT"; break;
804 case Instruction::Shl: Out << "getShl"; break;
805 case Instruction::LShr: Out << "getLShr"; break;
806 case Instruction::AShr: Out << "getAShr"; break;
807 case Instruction::Select: Out << "getSelect"; break;
808 case Instruction::ExtractElement: Out << "getExtractElement"; break;
809 case Instruction::InsertElement: Out << "getInsertElement"; break;
810 case Instruction::ShuffleVector: Out << "getShuffleVector"; break;
812 error("Invalid constant expression");
815 Out << getCppName(CE->getOperand(0));
816 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
817 Out << ", " << getCppName(CE->getOperand(i));
821 error("Bad Constant");
822 Out << "Constant* " << constName << " = 0; ";
828 CppWriter::printConstants(const Module* M) {
829 // Traverse all the global variables looking for constant initializers
830 for (Module::const_global_iterator I = TheModule->global_begin(),
831 E = TheModule->global_end(); I != E; ++I)
832 if (I->hasInitializer())
833 printConstant(I->getInitializer());
835 // Traverse the LLVM functions looking for constants
836 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
838 // Add all of the basic blocks and instructions
839 for (Function::const_iterator BB = FI->begin(),
840 E = FI->end(); BB != E; ++BB) {
841 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
843 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
844 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
853 void CppWriter::printVariableUses(const GlobalVariable *GV) {
854 nl(Out) << "// Type Definitions";
856 printType(GV->getType());
857 if (GV->hasInitializer()) {
858 Constant* Init = GV->getInitializer();
859 printType(Init->getType());
860 if (Function* F = dyn_cast<Function>(Init)) {
861 nl(Out)<< "/ Function Declarations"; nl(Out);
862 printFunctionHead(F);
863 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
864 nl(Out) << "// Global Variable Declarations"; nl(Out);
865 printVariableHead(gv);
867 nl(Out) << "// Constant Definitions"; nl(Out);
870 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
871 nl(Out) << "// Global Variable Definitions"; nl(Out);
872 printVariableBody(gv);
877 void CppWriter::printVariableHead(const GlobalVariable *GV) {
878 nl(Out) << "GlobalVariable* " << getCppName(GV);
880 Out << " = mod->getGlobalVariable(";
881 printEscapedString(GV->getName());
882 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
883 nl(Out) << "if (!" << getCppName(GV) << ") {";
884 in(); nl(Out) << getCppName(GV);
886 Out << " = new GlobalVariable(";
887 nl(Out) << "/*Type=*/";
888 printCppName(GV->getType()->getElementType());
890 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
892 nl(Out) << "/*Linkage=*/";
893 printLinkageType(GV->getLinkage());
895 nl(Out) << "/*Initializer=*/0, ";
896 if (GV->hasInitializer()) {
897 Out << "// has initializer, specified below";
899 nl(Out) << "/*Name=*/\"";
900 printEscapedString(GV->getName());
905 if (GV->hasSection()) {
907 Out << "->setSection(\"";
908 printEscapedString(GV->getSection());
912 if (GV->getAlignment()) {
914 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
918 out(); Out << "}"; nl(Out);
923 CppWriter::printVariableBody(const GlobalVariable *GV) {
924 if (GV->hasInitializer()) {
926 Out << "->setInitializer(";
927 //if (!isa<GlobalValue(GV->getInitializer()))
929 Out << getCppName(GV->getInitializer()) << ");";
935 CppWriter::getOpName(Value* V) {
936 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
937 return getCppName(V);
939 // See if its alread in the map of forward references, if so just return the
940 // name we already set up for it
941 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
942 if (I != ForwardRefs.end())
945 // This is a new forward reference. Generate a unique name for it
946 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
948 // Yes, this is a hack. An Argument is the smallest instantiable value that
949 // we can make as a placeholder for the real value. We'll replace these
950 // Argument instances later.
951 Out << "Argument* " << result << " = new Argument("
952 << getCppName(V->getType()) << ");";
954 ForwardRefs[V] = result;
958 // printInstruction - This member is called for each Instruction in a function.
960 CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
961 std::string iName(getCppName(I));
963 // Before we emit this instruction, we need to take care of generating any
964 // forward references. So, we get the names of all the operands in advance
965 std::string* opNames = new std::string[I->getNumOperands()];
966 for (unsigned i = 0; i < I->getNumOperands(); i++) {
967 opNames[i] = getOpName(I->getOperand(i));
970 switch (I->getOpcode()) {
971 case Instruction::Ret: {
972 const ReturnInst* ret = cast<ReturnInst>(I);
973 Out << "ReturnInst* " << iName << " = new ReturnInst("
974 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
977 case Instruction::Br: {
978 const BranchInst* br = cast<BranchInst>(I);
979 Out << "BranchInst* " << iName << " = new BranchInst(" ;
980 if (br->getNumOperands() == 3 ) {
981 Out << opNames[0] << ", "
982 << opNames[1] << ", "
983 << opNames[2] << ", ";
985 } else if (br->getNumOperands() == 1) {
986 Out << opNames[0] << ", ";
988 error("Branch with 2 operands?");
990 Out << bbname << ");";
993 case Instruction::Switch: {
994 const SwitchInst* sw = cast<SwitchInst>(I);
995 Out << "SwitchInst* " << iName << " = new SwitchInst("
996 << opNames[0] << ", "
997 << opNames[1] << ", "
998 << sw->getNumCases() << ", " << bbname << ");";
1000 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
1001 Out << iName << "->addCase("
1002 << opNames[i] << ", "
1003 << opNames[i+1] << ");";
1008 case Instruction::Invoke: {
1009 const InvokeInst* inv = cast<InvokeInst>(I);
1010 Out << "std::vector<Value*> " << iName << "_params;";
1012 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1013 Out << iName << "_params.push_back("
1014 << opNames[i] << ");";
1017 Out << "InvokeInst* " << iName << " = new InvokeInst("
1018 << opNames[0] << ", "
1019 << opNames[1] << ", "
1020 << opNames[2] << ", "
1021 << iName << "_params, \"";
1022 printEscapedString(inv->getName());
1023 Out << "\", " << bbname << ");";
1024 nl(Out) << iName << "->setCallingConv(";
1025 printCallingConv(inv->getCallingConv());
1029 case Instruction::Unwind: {
1030 Out << "UnwindInst* " << iName << " = new UnwindInst("
1034 case Instruction::Unreachable:{
1035 Out << "UnreachableInst* " << iName << " = new UnreachableInst("
1039 case Instruction::Add:
1040 case Instruction::Sub:
1041 case Instruction::Mul:
1042 case Instruction::UDiv:
1043 case Instruction::SDiv:
1044 case Instruction::FDiv:
1045 case Instruction::URem:
1046 case Instruction::SRem:
1047 case Instruction::FRem:
1048 case Instruction::And:
1049 case Instruction::Or:
1050 case Instruction::Xor:
1051 case Instruction::Shl:
1052 case Instruction::LShr:
1053 case Instruction::AShr:{
1054 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
1055 switch (I->getOpcode()) {
1056 case Instruction::Add: Out << "Instruction::Add"; break;
1057 case Instruction::Sub: Out << "Instruction::Sub"; break;
1058 case Instruction::Mul: Out << "Instruction::Mul"; break;
1059 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1060 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1061 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
1062 case Instruction::URem:Out << "Instruction::URem"; break;
1063 case Instruction::SRem:Out << "Instruction::SRem"; break;
1064 case Instruction::FRem:Out << "Instruction::FRem"; break;
1065 case Instruction::And: Out << "Instruction::And"; break;
1066 case Instruction::Or: Out << "Instruction::Or"; break;
1067 case Instruction::Xor: Out << "Instruction::Xor"; break;
1068 case Instruction::Shl: Out << "Instruction::Shl"; break;
1069 case Instruction::LShr:Out << "Instruction::LShr"; break;
1070 case Instruction::AShr:Out << "Instruction::AShr"; break;
1071 default: Out << "Instruction::BadOpCode"; break;
1073 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1074 printEscapedString(I->getName());
1075 Out << "\", " << bbname << ");";
1078 case Instruction::SetEQ:
1079 case Instruction::SetNE:
1080 case Instruction::SetLE:
1081 case Instruction::SetGE:
1082 case Instruction::SetLT:
1083 case Instruction::SetGT: {
1084 Out << "SetCondInst* " << iName << " = new SetCondInst(";
1085 switch (I->getOpcode()) {
1086 case Instruction::SetEQ: Out << "Instruction::SetEQ"; break;
1087 case Instruction::SetNE: Out << "Instruction::SetNE"; break;
1088 case Instruction::SetLE: Out << "Instruction::SetLE"; break;
1089 case Instruction::SetGE: Out << "Instruction::SetGE"; break;
1090 case Instruction::SetLT: Out << "Instruction::SetLT"; break;
1091 case Instruction::SetGT: Out << "Instruction::SetGT"; break;
1092 default: Out << "Instruction::BadOpCode"; break;
1094 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1095 printEscapedString(I->getName());
1096 Out << "\", " << bbname << ");";
1099 case Instruction::Malloc: {
1100 const MallocInst* mallocI = cast<MallocInst>(I);
1101 Out << "MallocInst* " << iName << " = new MallocInst("
1102 << getCppName(mallocI->getAllocatedType()) << ", ";
1103 if (mallocI->isArrayAllocation())
1104 Out << opNames[0] << ", " ;
1106 printEscapedString(mallocI->getName());
1107 Out << "\", " << bbname << ");";
1108 if (mallocI->getAlignment())
1109 nl(Out) << iName << "->setAlignment("
1110 << mallocI->getAlignment() << ");";
1113 case Instruction::Free: {
1114 Out << "FreeInst* " << iName << " = new FreeInst("
1115 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1118 case Instruction::Alloca: {
1119 const AllocaInst* allocaI = cast<AllocaInst>(I);
1120 Out << "AllocaInst* " << iName << " = new AllocaInst("
1121 << getCppName(allocaI->getAllocatedType()) << ", ";
1122 if (allocaI->isArrayAllocation())
1123 Out << opNames[0] << ", ";
1125 printEscapedString(allocaI->getName());
1126 Out << "\", " << bbname << ");";
1127 if (allocaI->getAlignment())
1128 nl(Out) << iName << "->setAlignment("
1129 << allocaI->getAlignment() << ");";
1132 case Instruction::Load:{
1133 const LoadInst* load = cast<LoadInst>(I);
1134 Out << "LoadInst* " << iName << " = new LoadInst("
1135 << opNames[0] << ", \"";
1136 printEscapedString(load->getName());
1137 Out << "\", " << (load->isVolatile() ? "true" : "false" )
1138 << ", " << bbname << ");";
1141 case Instruction::Store: {
1142 const StoreInst* store = cast<StoreInst>(I);
1143 Out << "StoreInst* " << iName << " = new StoreInst("
1144 << opNames[0] << ", "
1145 << opNames[1] << ", "
1146 << (store->isVolatile() ? "true" : "false")
1147 << ", " << bbname << ");";
1150 case Instruction::GetElementPtr: {
1151 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1152 if (gep->getNumOperands() <= 2) {
1153 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
1155 if (gep->getNumOperands() == 2)
1156 Out << ", " << opNames[1];
1158 Out << "std::vector<Value*> " << iName << "_indices;";
1160 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
1161 Out << iName << "_indices.push_back("
1162 << opNames[i] << ");";
1165 Out << "Instruction* " << iName << " = new GetElementPtrInst("
1166 << opNames[0] << ", " << iName << "_indices";
1169 printEscapedString(gep->getName());
1170 Out << "\", " << bbname << ");";
1173 case Instruction::PHI: {
1174 const PHINode* phi = cast<PHINode>(I);
1176 Out << "PHINode* " << iName << " = new PHINode("
1177 << getCppName(phi->getType()) << ", \"";
1178 printEscapedString(phi->getName());
1179 Out << "\", " << bbname << ");";
1180 nl(Out) << iName << "->reserveOperandSpace("
1181 << phi->getNumIncomingValues()
1184 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
1185 Out << iName << "->addIncoming("
1186 << opNames[i] << ", " << opNames[i+1] << ");";
1191 case Instruction::Trunc:
1192 case Instruction::ZExt:
1193 case Instruction::SExt:
1194 case Instruction::FPTrunc:
1195 case Instruction::FPExt:
1196 case Instruction::FPToUI:
1197 case Instruction::FPToSI:
1198 case Instruction::UIToFP:
1199 case Instruction::SIToFP:
1200 case Instruction::PtrToInt:
1201 case Instruction::IntToPtr:
1202 case Instruction::BitCast: {
1203 const CastInst* cst = cast<CastInst>(I);
1204 Out << "CastInst* " << iName << " = new ";
1205 switch (I->getOpcode()) {
1206 case Instruction::Trunc: Out << "TruncInst";
1207 case Instruction::ZExt: Out << "ZExtInst";
1208 case Instruction::SExt: Out << "SExtInst";
1209 case Instruction::FPTrunc: Out << "FPTruncInst";
1210 case Instruction::FPExt: Out << "FPExtInst";
1211 case Instruction::FPToUI: Out << "FPToUIInst";
1212 case Instruction::FPToSI: Out << "FPToSIInst";
1213 case Instruction::UIToFP: Out << "UIToFPInst";
1214 case Instruction::SIToFP: Out << "SIToFPInst";
1215 case Instruction::PtrToInt: Out << "PtrToInst";
1216 case Instruction::IntToPtr: Out << "IntToPtrInst";
1217 case Instruction::BitCast: Out << "BitCastInst";
1218 default: assert(!"Unreachable"); break;
1220 Out << "(" << opNames[0] << ", "
1221 << getCppName(cst->getType()) << ", \"";
1222 printEscapedString(cst->getName());
1223 Out << "\", " << bbname << ");";
1226 case Instruction::Call:{
1227 const CallInst* call = cast<CallInst>(I);
1228 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
1229 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
1230 << getCppName(ila->getFunctionType()) << ", \""
1231 << ila->getAsmString() << "\", \""
1232 << ila->getConstraintString() << "\","
1233 << (ila->hasSideEffects() ? "true" : "false") << ");";
1236 if (call->getNumOperands() > 3) {
1237 Out << "std::vector<Value*> " << iName << "_params;";
1239 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1240 Out << iName << "_params.push_back(" << opNames[i] << ");";
1243 Out << "CallInst* " << iName << " = new CallInst("
1244 << opNames[0] << ", " << iName << "_params, \"";
1245 } else if (call->getNumOperands() == 3) {
1246 Out << "CallInst* " << iName << " = new CallInst("
1247 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1248 } else if (call->getNumOperands() == 2) {
1249 Out << "CallInst* " << iName << " = new CallInst("
1250 << opNames[0] << ", " << opNames[1] << ", \"";
1252 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
1255 printEscapedString(call->getName());
1256 Out << "\", " << bbname << ");";
1257 nl(Out) << iName << "->setCallingConv(";
1258 printCallingConv(call->getCallingConv());
1260 nl(Out) << iName << "->setTailCall("
1261 << (call->isTailCall() ? "true":"false");
1265 case Instruction::Select: {
1266 const SelectInst* sel = cast<SelectInst>(I);
1267 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
1268 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1269 printEscapedString(sel->getName());
1270 Out << "\", " << bbname << ");";
1273 case Instruction::UserOp1:
1275 case Instruction::UserOp2: {
1276 /// FIXME: What should be done here?
1279 case Instruction::VAArg: {
1280 const VAArgInst* va = cast<VAArgInst>(I);
1281 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
1282 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
1283 printEscapedString(va->getName());
1284 Out << "\", " << bbname << ");";
1287 case Instruction::ExtractElement: {
1288 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
1289 Out << "ExtractElementInst* " << getCppName(eei)
1290 << " = new ExtractElementInst(" << opNames[0]
1291 << ", " << opNames[1] << ", \"";
1292 printEscapedString(eei->getName());
1293 Out << "\", " << bbname << ");";
1296 case Instruction::InsertElement: {
1297 const InsertElementInst* iei = cast<InsertElementInst>(I);
1298 Out << "InsertElementInst* " << getCppName(iei)
1299 << " = new InsertElementInst(" << opNames[0]
1300 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1301 printEscapedString(iei->getName());
1302 Out << "\", " << bbname << ");";
1305 case Instruction::ShuffleVector: {
1306 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
1307 Out << "ShuffleVectorInst* " << getCppName(svi)
1308 << " = new ShuffleVectorInst(" << opNames[0]
1309 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
1310 printEscapedString(svi->getName());
1311 Out << "\", " << bbname << ");";
1315 DefinedValues.insert(I);
1320 // Print out the types, constants and declarations needed by one function
1321 void CppWriter::printFunctionUses(const Function* F) {
1323 nl(Out) << "// Type Definitions"; nl(Out);
1325 // Print the function's return type
1326 printType(F->getReturnType());
1328 // Print the function's function type
1329 printType(F->getFunctionType());
1331 // Print the types of each of the function's arguments
1332 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1334 printType(AI->getType());
1338 // Print type definitions for every type referenced by an instruction and
1339 // make a note of any global values or constants that are referenced
1340 std::vector<GlobalValue*> gvs;
1341 std::vector<Constant*> consts;
1342 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1343 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1345 // Print the type of the instruction itself
1346 printType(I->getType());
1348 // Print the type of each of the instruction's operands
1349 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1350 Value* operand = I->getOperand(i);
1351 printType(operand->getType());
1352 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1354 else if (Constant* C = dyn_cast<Constant>(operand))
1355 consts.push_back(C);
1360 // Print the function declarations for any functions encountered
1361 nl(Out) << "// Function Declarations"; nl(Out);
1362 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1364 if (Function* Fun = dyn_cast<Function>(*I)) {
1365 if (!is_inline || Fun != F)
1366 printFunctionHead(Fun);
1370 // Print the global variable declarations for any variables encountered
1371 nl(Out) << "// Global Variable Declarations"; nl(Out);
1372 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1374 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1375 printVariableHead(F);
1378 // Print the constants found
1379 nl(Out) << "// Constant Definitions"; nl(Out);
1380 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1385 // Process the global variables definitions now that all the constants have
1386 // been emitted. These definitions just couple the gvars with their constant
1388 nl(Out) << "// Global Variable Definitions"; nl(Out);
1389 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1391 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1392 printVariableBody(GV);
1396 void CppWriter::printFunctionHead(const Function* F) {
1397 nl(Out) << "Function* " << getCppName(F);
1399 Out << " = mod->getFunction(\"";
1400 printEscapedString(F->getName());
1401 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1402 nl(Out) << "if (!" << getCppName(F) << ") {";
1403 nl(Out) << getCppName(F);
1405 Out<< " = new Function(";
1406 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1407 nl(Out) << "/*Linkage=*/";
1408 printLinkageType(F->getLinkage());
1410 nl(Out) << "/*Name=*/\"";
1411 printEscapedString(F->getName());
1412 Out << "\", mod); " << (F->isExternal()? "// (external, no body)" : "");
1415 Out << "->setCallingConv(";
1416 printCallingConv(F->getCallingConv());
1419 if (F->hasSection()) {
1421 Out << "->setSection(\"" << F->getSection() << "\");";
1424 if (F->getAlignment()) {
1426 Out << "->setAlignment(" << F->getAlignment() << ");";
1435 void CppWriter::printFunctionBody(const Function *F) {
1436 if (F->isExternal())
1437 return; // external functions have no bodies.
1439 // Clear the DefinedValues and ForwardRefs maps because we can't have
1440 // cross-function forward refs
1441 ForwardRefs.clear();
1442 DefinedValues.clear();
1444 // Create all the argument values
1446 if (!F->arg_empty()) {
1447 Out << "Function::arg_iterator args = " << getCppName(F)
1448 << "->arg_begin();";
1451 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1453 Out << "Value* " << getCppName(AI) << " = args++;";
1455 if (AI->hasName()) {
1456 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1462 // Create all the basic blocks
1464 for (Function::const_iterator BI = F->begin(), BE = F->end();
1466 std::string bbname(getCppName(BI));
1467 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
1469 printEscapedString(BI->getName());
1470 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1474 // Output all of its basic blocks... for the function
1475 for (Function::const_iterator BI = F->begin(), BE = F->end();
1477 std::string bbname(getCppName(BI));
1478 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1481 // Output all of the instructions in the basic block...
1482 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1484 printInstruction(I,bbname);
1488 // Loop over the ForwardRefs and resolve them now that all instructions
1490 if (!ForwardRefs.empty()) {
1491 nl(Out) << "// Resolve Forward References";
1495 while (!ForwardRefs.empty()) {
1496 ForwardRefMap::iterator I = ForwardRefs.begin();
1497 Out << I->second << "->replaceAllUsesWith("
1498 << getCppName(I->first) << "); delete " << I->second << ";";
1500 ForwardRefs.erase(I);
1504 void CppWriter::printInline(const std::string& fname, const std::string& func) {
1505 const Function* F = TheModule->getNamedFunction(func);
1507 error(std::string("Function '") + func + "' not found in input module");
1510 if (F->isExternal()) {
1511 error(std::string("Function '") + func + "' is external!");
1514 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
1516 unsigned arg_count = 1;
1517 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1519 Out << ", Value* arg_" << arg_count;
1524 printFunctionUses(F);
1525 printFunctionBody(F);
1527 Out << "return " << getCppName(F->begin()) << ";";
1532 void CppWriter::printModuleBody() {
1533 // Print out all the type definitions
1534 nl(Out) << "// Type Definitions"; nl(Out);
1535 printTypes(TheModule);
1537 // Functions can call each other and global variables can reference them so
1538 // define all the functions first before emitting their function bodies.
1539 nl(Out) << "// Function Declarations"; nl(Out);
1540 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1542 printFunctionHead(I);
1544 // Process the global variables declarations. We can't initialze them until
1545 // after the constants are printed so just print a header for each global
1546 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
1547 for (Module::const_global_iterator I = TheModule->global_begin(),
1548 E = TheModule->global_end(); I != E; ++I) {
1549 printVariableHead(I);
1552 // Print out all the constants definitions. Constants don't recurse except
1553 // through GlobalValues. All GlobalValues have been declared at this point
1554 // so we can proceed to generate the constants.
1555 nl(Out) << "// Constant Definitions"; nl(Out);
1556 printConstants(TheModule);
1558 // Process the global variables definitions now that all the constants have
1559 // been emitted. These definitions just couple the gvars with their constant
1561 nl(Out) << "// Global Variable Definitions"; nl(Out);
1562 for (Module::const_global_iterator I = TheModule->global_begin(),
1563 E = TheModule->global_end(); I != E; ++I) {
1564 printVariableBody(I);
1567 // Finally, we can safely put out all of the function bodies.
1568 nl(Out) << "// Function Definitions"; nl(Out);
1569 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1571 if (!I->isExternal()) {
1572 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1576 printFunctionBody(I);
1583 void CppWriter::printProgram(
1584 const std::string& fname,
1585 const std::string& mName
1587 Out << "#include <llvm/Module.h>\n";
1588 Out << "#include <llvm/DerivedTypes.h>\n";
1589 Out << "#include <llvm/Constants.h>\n";
1590 Out << "#include <llvm/GlobalVariable.h>\n";
1591 Out << "#include <llvm/Function.h>\n";
1592 Out << "#include <llvm/CallingConv.h>\n";
1593 Out << "#include <llvm/BasicBlock.h>\n";
1594 Out << "#include <llvm/Instructions.h>\n";
1595 Out << "#include <llvm/InlineAsm.h>\n";
1596 Out << "#include <llvm/Support/MathExtras.h>\n";
1597 Out << "#include <llvm/Pass.h>\n";
1598 Out << "#include <llvm/PassManager.h>\n";
1599 Out << "#include <llvm/Analysis/Verifier.h>\n";
1600 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1601 Out << "#include <algorithm>\n";
1602 Out << "#include <iostream>\n\n";
1603 Out << "using namespace llvm;\n\n";
1604 Out << "Module* " << fname << "();\n\n";
1605 Out << "int main(int argc, char**argv) {\n";
1606 Out << " Module* Mod = makeLLVMModule();\n";
1607 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1608 Out << " std::cerr.flush();\n";
1609 Out << " std::cout.flush();\n";
1610 Out << " PassManager PM;\n";
1611 Out << " PM.add(new PrintModulePass(&std::cout));\n";
1612 Out << " PM.run(*Mod);\n";
1613 Out << " return 0;\n";
1615 printModule(fname,mName);
1618 void CppWriter::printModule(
1619 const std::string& fname,
1620 const std::string& mName
1622 nl(Out) << "Module* " << fname << "() {";
1623 nl(Out,1) << "// Module Construction";
1624 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1625 nl(Out) << "mod->setEndianness(";
1626 switch (TheModule->getEndianness()) {
1627 case Module::LittleEndian: Out << "Module::LittleEndian);"; break;
1628 case Module::BigEndian: Out << "Module::BigEndian);"; break;
1629 case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break;
1631 nl(Out) << "mod->setPointerSize(";
1632 switch (TheModule->getPointerSize()) {
1633 case Module::Pointer32: Out << "Module::Pointer32);"; break;
1634 case Module::Pointer64: Out << "Module::Pointer64);"; break;
1635 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break;
1638 if (!TheModule->getTargetTriple().empty()) {
1639 Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1644 if (!TheModule->getModuleInlineAsm().empty()) {
1645 Out << "mod->setModuleInlineAsm(\"";
1646 printEscapedString(TheModule->getModuleInlineAsm());
1651 // Loop over the dependent libraries and emit them.
1652 Module::lib_iterator LI = TheModule->lib_begin();
1653 Module::lib_iterator LE = TheModule->lib_end();
1655 Out << "mod->addLibrary(\"" << *LI << "\");";
1660 nl(Out) << "return mod;";
1665 void CppWriter::printContents(
1666 const std::string& fname, // Name of generated function
1667 const std::string& mName // Name of module generated module
1669 Out << "\nModule* " << fname << "(Module *mod) {\n";
1670 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
1672 Out << "\nreturn mod;\n";
1676 void CppWriter::printFunction(
1677 const std::string& fname, // Name of generated function
1678 const std::string& funcName // Name of function to generate
1680 const Function* F = TheModule->getNamedFunction(funcName);
1682 error(std::string("Function '") + funcName + "' not found in input module");
1685 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1686 printFunctionUses(F);
1687 printFunctionHead(F);
1688 printFunctionBody(F);
1689 Out << "return " << getCppName(F) << ";\n";
1693 void CppWriter::printVariable(
1694 const std::string& fname, /// Name of generated function
1695 const std::string& varName // Name of variable to generate
1697 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1700 error(std::string("Variable '") + varName + "' not found in input module");
1703 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1704 printVariableUses(GV);
1705 printVariableHead(GV);
1706 printVariableBody(GV);
1707 Out << "return " << getCppName(GV) << ";\n";
1711 void CppWriter::printType(
1712 const std::string& fname, /// Name of generated function
1713 const std::string& typeName // Name of type to generate
1715 const Type* Ty = TheModule->getTypeByName(typeName);
1717 error(std::string("Type '") + typeName + "' not found in input module");
1720 Out << "\nType* " << fname << "(Module *mod) {\n";
1722 Out << "return " << getCppName(Ty) << ";\n";
1726 } // end anonymous llvm
1730 void WriteModuleToCppFile(Module* mod, std::ostream& o) {
1731 // Initialize a CppWriter for us to use
1732 CppWriter W(o, mod);
1735 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
1737 // Get the name of the function we're supposed to generate
1738 std::string fname = FuncName.getValue();
1740 // Get the name of the thing we are to generate
1741 std::string tgtname = NameToGenerate.getValue();
1742 if (GenerationType == GenModule ||
1743 GenerationType == GenContents ||
1744 GenerationType == GenProgram) {
1745 if (tgtname == "!bad!") {
1746 if (mod->getModuleIdentifier() == "-")
1747 tgtname = "<stdin>";
1749 tgtname = mod->getModuleIdentifier();
1751 } else if (tgtname == "!bad!") {
1752 W.error("You must use the -for option with -gen-{function,variable,type}");
1755 switch (WhatToGenerate(GenerationType)) {
1758 fname = "makeLLVMModule";
1759 W.printProgram(fname,tgtname);
1763 fname = "makeLLVMModule";
1764 W.printModule(fname,tgtname);
1768 fname = "makeLLVMModuleContents";
1769 W.printContents(fname,tgtname);
1773 fname = "makeLLVMFunction";
1774 W.printFunction(fname,tgtname);
1778 fname = "makeLLVMInline";
1779 W.printInline(fname,tgtname);
1783 fname = "makeLLVMVariable";
1784 W.printVariable(fname,tgtname);
1788 fname = "makeLLVMType";
1789 W.printType(fname,tgtname);
1792 W.error("Invalid generation option");