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/Support/CFG.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/Support/MathExtras.h"
33 typedef std::vector<const Type*> TypeList;
34 typedef std::map<const Type*,std::string> TypeMap;
35 typedef std::map<const Value*,std::string> ValueMap;
39 const Module *TheModule;
40 unsigned long uniqueNum;
43 TypeMap UnresolvedTypes;
47 inline CppWriter(std::ostream &o, const Module *M)
48 : Out(o), TheModule(M), uniqueNum(0), TypeNames(),
49 ValueNames(), UnresolvedTypes(), TypeStack() { }
51 const Module* getModule() { return TheModule; }
53 void printModule(const Module *M);
56 void printTypes(const Module* M);
57 void printConstants(const Module* M);
58 void printConstant(const Constant *CPV);
59 void printGlobal(const GlobalVariable *GV);
60 void printFunction(const Function *F);
61 void printInstruction(const Instruction *I, const std::string& bbname);
62 void printSymbolTable(const SymbolTable &ST);
63 void printLinkageType(GlobalValue::LinkageTypes LT);
64 void printCallingConv(unsigned cc);
66 std::string getCppName(const Type* val);
67 std::string getCppName(const Value* val);
68 inline void printCppName(const Value* val);
69 inline void printCppName(const Type* val);
70 bool isOnStack(const Type*) const;
71 inline void printTypeDef(const Type* Ty);
72 bool printTypeDefInternal(const Type* Ty);
73 void printEscapedString(const std::string& str);
76 // printEscapedString - Print each character of the specified string, escaping
77 // it if it is not printable or if it is an escape char.
79 CppWriter::printEscapedString(const std::string &Str) {
80 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
81 unsigned char C = Str[i];
82 if (isprint(C) && C != '"' && C != '\\') {
86 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
87 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
93 CppWriter::getCppName(const Value* val) {
95 ValueMap::iterator I = ValueNames.find(val);
96 if (I != ValueNames.end()) {
100 switch (val->getType()->getTypeID()) {
101 case Type::VoidTyID: prefix = "void_"; break;
102 case Type::BoolTyID: prefix = "bool_"; break;
103 case Type::UByteTyID: prefix = "ubyte_"; break;
104 case Type::SByteTyID: prefix = "sbyte_"; break;
105 case Type::UShortTyID: prefix = "ushort_"; break;
106 case Type::ShortTyID: prefix = "short_"; break;
107 case Type::UIntTyID: prefix = "uint_"; break;
108 case Type::IntTyID: prefix = "int_"; break;
109 case Type::ULongTyID: prefix = "ulong_"; break;
110 case Type::LongTyID: prefix = "long_"; break;
111 case Type::FloatTyID: prefix = "float_"; break;
112 case Type::DoubleTyID: prefix = "double_"; break;
113 case Type::LabelTyID: prefix = "label_"; break;
114 case Type::FunctionTyID: prefix = "func_"; break;
115 case Type::StructTyID: prefix = "struct_"; break;
116 case Type::ArrayTyID: prefix = "array_"; break;
117 case Type::PointerTyID: prefix = "ptr_"; break;
118 case Type::PackedTyID: prefix = "packed_"; break;
119 default: prefix = "other_"; break;
121 name = ValueNames[val] = std::string(prefix) +
122 (val->hasName() ? val->getName() : utostr(uniqueNum++));
128 CppWriter::printCppName(const Value* val) {
129 printEscapedString(getCppName(val));
133 CppWriter::printCppName(const Type* Ty)
135 printEscapedString(getCppName(Ty));
138 // Gets the C++ name for a type. Returns true if we already saw the type,
141 inline const std::string*
142 findTypeName(const SymbolTable& ST, const Type* Ty)
144 SymbolTable::type_const_iterator TI = ST.type_begin();
145 SymbolTable::type_const_iterator TE = ST.type_end();
146 for (;TI != TE; ++TI)
147 if (TI->second == Ty)
153 CppWriter::getCppName(const Type* Ty)
155 // First, handle the primitive types .. easy
156 if (Ty->isPrimitiveType()) {
157 switch (Ty->getTypeID()) {
158 case Type::VoidTyID: return "Type::VoidTy";
159 case Type::BoolTyID: return "Type::BoolTy";
160 case Type::UByteTyID: return "Type::UByteTy";
161 case Type::SByteTyID: return "Type::SByteTy";
162 case Type::UShortTyID: return "Type::UShortTy";
163 case Type::ShortTyID: return "Type::ShortTy";
164 case Type::UIntTyID: return "Type::UIntTy";
165 case Type::IntTyID: return "Type::IntTy";
166 case Type::ULongTyID: return "Type::ULongTy";
167 case Type::LongTyID: return "Type::LongTy";
168 case Type::FloatTyID: return "Type::FloatTy";
169 case Type::DoubleTyID: return "Type::DoubleTy";
170 case Type::LabelTyID: return "Type::LabelTy";
172 assert(!"Can't get here");
175 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
178 // Now, see if we've seen the type before and return that
179 TypeMap::iterator I = TypeNames.find(Ty);
180 if (I != TypeNames.end())
183 // Okay, let's build a new name for this type. Start with a prefix
184 const char* prefix = 0;
185 switch (Ty->getTypeID()) {
186 case Type::FunctionTyID: prefix = "FuncTy_"; break;
187 case Type::StructTyID: prefix = "StructTy_"; break;
188 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
189 case Type::PointerTyID: prefix = "PointerTy_"; break;
190 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
191 case Type::PackedTyID: prefix = "PackedTy_"; break;
192 default: prefix = "OtherTy_"; break; // prevent breakage
195 // See if the type has a name in the symboltable and build accordingly
196 const std::string* tName = findTypeName(TheModule->getSymbolTable(), Ty);
199 name = std::string(prefix) + *tName;
201 name = std::string(prefix) + utostr(uniqueNum++);
204 return TypeNames[Ty] = name;
207 void CppWriter::printModule(const Module *M) {
208 Out << "\n// Module Construction\n";
209 Out << "Module* mod = new Module(\"";
210 if (M->getModuleIdentifier() == "-")
211 printEscapedString("<stdin>");
213 printEscapedString(M->getModuleIdentifier());
215 Out << "mod->setEndianness(";
216 switch (M->getEndianness()) {
217 case Module::LittleEndian: Out << "Module::LittleEndian);\n"; break;
218 case Module::BigEndian: Out << "Module::BigEndian);\n"; break;
219 case Module::AnyEndianness:Out << "Module::AnyEndianness);\n"; break;
221 Out << "mod->setPointerSize(";
222 switch (M->getPointerSize()) {
223 case Module::Pointer32: Out << "Module::Pointer32);\n"; break;
224 case Module::Pointer64: Out << "Module::Pointer64);\n"; break;
225 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);\n"; break;
227 if (!M->getTargetTriple().empty())
228 Out << "mod->setTargetTriple(\"" << M->getTargetTriple() << "\");\n";
230 if (!M->getModuleInlineAsm().empty()) {
231 Out << "mod->setModuleInlineAsm(\"";
232 printEscapedString(M->getModuleInlineAsm());
236 // Loop over the dependent libraries and emit them.
237 Module::lib_iterator LI = M->lib_begin();
238 Module::lib_iterator LE = M->lib_end();
240 Out << "mod->addLibrary(\"" << *LI << "\");\n";
244 // Print out all the type definitions
245 Out << "\n// Type Definitions\n";
248 // Print out all the constants declarations
249 Out << "\n// Constants Construction\n";
252 // Process the global variables
253 Out << "\n// Global Variable Construction\n";
254 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
259 // Output all of the functions.
260 Out << "\n// Function Construction\n";
261 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
266 CppWriter::printCallingConv(unsigned cc){
267 // Print the calling convention.
270 case CallingConv::C: Out << "CallingConv::C"; break;
271 case CallingConv::CSRet: Out << "CallingConv::CSRet"; break;
272 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
273 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
274 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
279 CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
281 case GlobalValue::InternalLinkage:
282 Out << "GlobalValue::InternalLinkage"; break;
283 case GlobalValue::LinkOnceLinkage:
284 Out << "GlobalValue::LinkOnceLinkage "; break;
285 case GlobalValue::WeakLinkage:
286 Out << "GlobalValue::WeakLinkage"; break;
287 case GlobalValue::AppendingLinkage:
288 Out << "GlobalValue::AppendingLinkage"; break;
289 case GlobalValue::ExternalLinkage:
290 Out << "GlobalValue::ExternalLinkage"; break;
291 case GlobalValue::GhostLinkage:
292 Out << "GlobalValue::GhostLinkage"; break;
295 void CppWriter::printGlobal(const GlobalVariable *GV) {
297 Out << "GlobalVariable* ";
299 Out << " = new GlobalVariable(\n";
301 printCppName(GV->getType()->getElementType());
303 Out << " /*isConstant=*/" << (GV->isConstant()?"true":"false")
304 << ",\n /*Linkage=*/";
305 printLinkageType(GV->getLinkage());
306 Out << ",\n /*Initializer=*/";
307 if (GV->hasInitializer()) {
308 printCppName(GV->getInitializer());
312 Out << ",\n /*Name=*/\"";
313 printEscapedString(GV->getName());
314 Out << "\",\n mod);\n";
316 if (GV->hasSection()) {
318 Out << "->setSection(\"";
319 printEscapedString(GV->getSection());
322 if (GV->getAlignment()) {
324 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");\n";
329 CppWriter::isOnStack(const Type* Ty) const {
330 TypeList::const_iterator TI =
331 std::find(TypeStack.begin(),TypeStack.end(),Ty);
332 return TI != TypeStack.end();
335 // Prints a type definition. Returns true if it could not resolve all the types
336 // in the definition but had to use a forward reference.
338 CppWriter::printTypeDef(const Type* Ty) {
339 assert(TypeStack.empty());
341 printTypeDefInternal(Ty);
342 assert(TypeStack.empty());
343 // early resolve as many unresolved types as possible. Search the unresolved
344 // types map for the type we just printed. Now that its definition is complete
345 // we can resolve any preview references to it. This prevents a cascade of
347 TypeMap::iterator I = UnresolvedTypes.find(Ty);
348 if (I != UnresolvedTypes.end()) {
349 Out << "cast<OpaqueType>(" << I->second
350 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");\n";
351 Out << I->second << " = cast<";
352 switch (Ty->getTypeID()) {
353 case Type::FunctionTyID: Out << "FunctionType"; break;
354 case Type::ArrayTyID: Out << "ArrayType"; break;
355 case Type::StructTyID: Out << "StructType"; break;
356 case Type::PackedTyID: Out << "PackedType"; break;
357 case Type::PointerTyID: Out << "PointerType"; break;
358 case Type::OpaqueTyID: Out << "OpaqueType"; break;
359 default: Out << "NoSuchDerivedType"; break;
361 Out << ">(" << I->second << "_fwd.get());\n\n";
362 UnresolvedTypes.erase(I);
367 CppWriter::printTypeDefInternal(const Type* Ty) {
368 // We don't print definitions for primitive types
369 if (Ty->isPrimitiveType())
372 // Determine if the name is in the name list before we modify that list.
373 TypeMap::const_iterator TNI = TypeNames.find(Ty);
375 // Everything below needs the name for the type so get it now
376 std::string typeName(getCppName(Ty));
378 // Search the type stack for recursion. If we find it, then generate this
379 // as an OpaqueType, but make sure not to do this multiple times because
380 // the type could appear in multiple places on the stack. Once the opaque
381 // definition is issues, it must not be re-issued. Consequently we have to
382 // check the UnresolvedTypes list as well.
384 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
385 if (I == UnresolvedTypes.end()) {
386 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();\n";
387 UnresolvedTypes[Ty] = typeName;
392 // Avoid printing things we have already printed. Since TNI was obtained
393 // before the name was inserted with getCppName and because we know the name
394 // is not on the stack (currently being defined), we can surmise here that if
395 // we got the name we've also already emitted its definition.
396 if (TNI != TypeNames.end())
399 // We're going to print a derived type which, by definition, contains other
400 // types. So, push this one we're printing onto the type stack to assist with
401 // recursive definitions.
402 TypeStack.push_back(Ty); // push on type stack
403 bool didRecurse = false;
405 // Print the type definition
406 switch (Ty->getTypeID()) {
407 case Type::FunctionTyID: {
408 const FunctionType* FT = cast<FunctionType>(Ty);
409 Out << "std::vector<const Type*>" << typeName << "_args;\n";
410 FunctionType::param_iterator PI = FT->param_begin();
411 FunctionType::param_iterator PE = FT->param_end();
412 for (; PI != PE; ++PI) {
413 const Type* argTy = static_cast<const Type*>(*PI);
414 bool isForward = printTypeDefInternal(argTy);
415 std::string argName(getCppName(argTy));
416 Out << typeName << "_args.push_back(" << argName;
421 bool isForward = printTypeDefInternal(FT->getReturnType());
422 std::string retTypeName(getCppName(FT->getReturnType()));
423 Out << "FunctionType* " << typeName << " = FunctionType::get(\n"
424 << " /*Result=*/" << retTypeName;
427 Out << ",\n /*Params=*/" << typeName << "_args,\n /*isVarArg=*/"
428 << (FT->isVarArg() ? "true" : "false") << ");\n";
431 case Type::StructTyID: {
432 const StructType* ST = cast<StructType>(Ty);
433 Out << "std::vector<const Type*>" << typeName << "_fields;\n";
434 StructType::element_iterator EI = ST->element_begin();
435 StructType::element_iterator EE = ST->element_end();
436 for (; EI != EE; ++EI) {
437 const Type* fieldTy = static_cast<const Type*>(*EI);
438 bool isForward = printTypeDefInternal(fieldTy);
439 std::string fieldName(getCppName(fieldTy));
440 Out << typeName << "_fields.push_back(" << fieldName;
445 Out << "StructType* " << typeName << " = StructType::get("
446 << typeName << "_fields);\n";
449 case Type::ArrayTyID: {
450 const ArrayType* AT = cast<ArrayType>(Ty);
451 const Type* ET = AT->getElementType();
452 bool isForward = printTypeDefInternal(ET);
453 std::string elemName(getCppName(ET));
454 Out << "ArrayType* " << typeName << " = ArrayType::get("
455 << elemName << (isForward ? "_fwd" : "")
456 << ", " << utostr(AT->getNumElements()) << ");\n";
459 case Type::PointerTyID: {
460 const PointerType* PT = cast<PointerType>(Ty);
461 const Type* ET = PT->getElementType();
462 bool isForward = printTypeDefInternal(ET);
463 std::string elemName(getCppName(ET));
464 Out << "PointerType* " << typeName << " = PointerType::get("
465 << elemName << (isForward ? "_fwd" : "") << ");\n";
468 case Type::PackedTyID: {
469 const PackedType* PT = cast<PackedType>(Ty);
470 const Type* ET = PT->getElementType();
471 bool isForward = printTypeDefInternal(ET);
472 std::string elemName(getCppName(ET));
473 Out << "PackedType* " << typeName << " = PackedType::get("
474 << elemName << (isForward ? "_fwd" : "")
475 << ", " << utostr(PT->getNumElements()) << ");\n";
478 case Type::OpaqueTyID: {
479 const OpaqueType* OT = cast<OpaqueType>(Ty);
480 Out << "OpaqueType* " << typeName << " = OpaqueType::get();\n";
484 assert(!"Invalid TypeID");
487 // If the type had a name, make sure we recreate it.
488 const std::string* progTypeName =
489 findTypeName(TheModule->getSymbolTable(),Ty);
491 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
492 << typeName << ");\n";
494 // Pop us off the type stack
495 TypeStack.pop_back();
498 // We weren't a recursive type
503 CppWriter::printTypes(const Module* M) {
504 // Add all of the global variables to the value table...
505 for (Module::const_global_iterator I = TheModule->global_begin(),
506 E = TheModule->global_end(); I != E; ++I) {
507 if (I->hasInitializer())
508 printTypeDef(I->getInitializer()->getType());
509 printTypeDef(I->getType());
512 // Add all the functions to the table
513 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
515 printTypeDef(FI->getReturnType());
516 printTypeDef(FI->getFunctionType());
517 // Add all the function arguments
518 for(Function::const_arg_iterator AI = FI->arg_begin(),
519 AE = FI->arg_end(); AI != AE; ++AI) {
520 printTypeDef(AI->getType());
523 // Add all of the basic blocks and instructions
524 for (Function::const_iterator BB = FI->begin(),
525 E = FI->end(); BB != E; ++BB) {
526 printTypeDef(BB->getType());
527 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
529 printTypeDef(I->getType());
536 CppWriter::printConstants(const Module* M) {
537 // Add all of the global variables to the value table...
538 for (Module::const_global_iterator I = TheModule->global_begin(),
539 E = TheModule->global_end(); I != E; ++I)
540 if (I->hasInitializer())
541 printConstant(I->getInitializer());
543 // Traverse the LLVM functions looking for constants
544 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
546 // Add all of the basic blocks and instructions
547 for (Function::const_iterator BB = FI->begin(),
548 E = FI->end(); BB != E; ++BB) {
549 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
551 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
552 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
561 // printConstant - Print out a constant pool entry...
562 void CppWriter::printConstant(const Constant *CV) {
563 // First, if the constant is in the constant list then we've printed it
564 // already and we shouldn't reprint it.
565 if (ValueNames.find(CV) != ValueNames.end())
568 const int IndentSize = 2;
569 static std::string Indent = "\n";
570 std::string constName(getCppName(CV));
571 std::string typeName(getCppName(CV->getType()));
572 if (CV->isNullValue()) {
573 Out << "Constant* " << constName << " = Constant::getNullValue("
574 << typeName << ");\n";
577 if (isa<GlobalValue>(CV)) {
578 // Skip variables and functions, we emit them elsewhere
581 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
582 Out << "Constant* " << constName << " = ConstantBool::get("
583 << (CB == ConstantBool::True ? "true" : "false")
585 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
586 Out << "Constant* " << constName << " = ConstantSInt::get("
587 << typeName << ", " << CI->getValue() << ");";
588 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
589 Out << "Constant* " << constName << " = ConstantUInt::get("
590 << typeName << ", " << CI->getValue() << ");";
591 } else if (isa<ConstantAggregateZero>(CV)) {
592 Out << "Constant* " << constName << " = ConstantAggregateZero::get("
594 } else if (isa<ConstantPointerNull>(CV)) {
595 Out << "Constant* " << constName << " = ConstanPointerNull::get("
597 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
598 Out << "ConstantFP::get(" << typeName << ", ";
599 // We would like to output the FP constant value in exponential notation,
600 // but we cannot do this if doing so will lose precision. Check here to
601 // make sure that we only output it in exponential format if we can parse
602 // the value back and get the same value.
604 std::string StrVal = ftostr(CFP->getValue());
606 // Check to make sure that the stringized number is not some string like
607 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
608 // the string matches the "[-+]?[0-9]" regex.
610 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
611 ((StrVal[0] == '-' || StrVal[0] == '+') &&
612 (StrVal[1] >= '0' && StrVal[1] <= '9')))
613 // Reparse stringized version!
614 if (atof(StrVal.c_str()) == CFP->getValue()) {
619 // Otherwise we could not reparse it to exactly the same value, so we must
620 // output the string in hexadecimal format!
621 assert(sizeof(double) == sizeof(uint64_t) &&
622 "assuming that double is 64 bits!");
623 Out << "0x" << utohexstr(DoubleToBits(CFP->getValue())) << ");";
624 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
625 if (CA->isString() && CA->getType()->getElementType() == Type::SByteTy) {
626 Out << "Constant* " << constName << " = ConstantArray::get(\"";
627 printEscapedString(CA->getAsString());
630 Out << "std::vector<Constant*> " << constName << "_elems;\n";
631 unsigned N = CA->getNumOperands();
632 for (unsigned i = 0; i < N; ++i) {
633 printConstant(CA->getOperand(i));
634 Out << constName << "_elems.push_back("
635 << getCppName(CA->getOperand(i)) << ");\n";
637 Out << "Constant* " << constName << " = ConstantArray::get("
638 << typeName << ", " << constName << "_elems);";
640 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
641 Out << "std::vector<Constant*> " << constName << "_fields;\n";
642 unsigned N = CS->getNumOperands();
643 for (unsigned i = 0; i < N; i++) {
644 printConstant(CS->getOperand(i));
645 Out << constName << "_fields.push_back("
646 << getCppName(CA->getOperand(i)) << ");\n";
648 Out << "Constant* " << constName << " = ConstantStruct::get("
649 << typeName << ", " << constName << "_fields);";
650 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
651 Out << "std::vector<Constant*> " << constName << "_elems;\n";
652 unsigned N = CP->getNumOperands();
653 for (unsigned i = 0; i < N; ++i) {
654 printConstant(CP->getOperand(i));
655 Out << constName << "_elems.push_back("
656 << getCppName(CP->getOperand(i)) << ");\n";
658 Out << "Constant* " << constName << " = ConstantPacked::get("
659 << typeName << ", " << constName << "_elems);";
660 } else if (isa<UndefValue>(CV)) {
661 Out << "Constant* " << constName << " = UndefValue::get("
663 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
664 if (CE->getOpcode() == Instruction::GetElementPtr) {
665 Out << "std::vector<Constant*> " << constName << "_indices;\n";
666 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
667 Out << constName << "_indices.push_back("
668 << getCppName(CE->getOperand(i)) << ");\n";
670 Out << "Constant* " << constName << " = new GetElementPtrInst("
671 << getCppName(CE->getOperand(0)) << ", " << constName << "_indices";
672 } else if (CE->getOpcode() == Instruction::Cast) {
673 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
674 Out << getCppName(CE->getOperand(0)) << ", " << getCppName(CE->getType())
677 Out << "Constant* " << constName << " = ConstantExpr::";
678 switch (CE->getOpcode()) {
679 case Instruction::Add: Out << "getAdd"; break;
680 case Instruction::Sub: Out << "getSub"; break;
681 case Instruction::Mul: Out << "getMul"; break;
682 case Instruction::Div: Out << "getDiv"; break;
683 case Instruction::Rem: Out << "getRem"; break;
684 case Instruction::And: Out << "getAnd"; break;
685 case Instruction::Or: Out << "getOr"; break;
686 case Instruction::Xor: Out << "getXor"; break;
687 case Instruction::SetEQ: Out << "getSetEQ"; break;
688 case Instruction::SetNE: Out << "getSetNE"; break;
689 case Instruction::SetLE: Out << "getSetLE"; break;
690 case Instruction::SetGE: Out << "getSetGE"; break;
691 case Instruction::SetLT: Out << "getSetLT"; break;
692 case Instruction::SetGT: Out << "getSetGT"; break;
693 case Instruction::Shl: Out << "getShl"; break;
694 case Instruction::Shr: Out << "getShr"; break;
695 case Instruction::Select: Out << "getSelect"; break;
696 case Instruction::ExtractElement: Out << "getExtractElement"; break;
697 case Instruction::InsertElement: Out << "getInsertElement"; break;
698 case Instruction::ShuffleVector: Out << "getShuffleVector"; break;
700 assert(!"Invalid constant expression");
703 Out << getCppName(CE->getOperand(0));
704 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
705 Out << ", " << getCppName(CE->getOperand(i));
709 assert(!"Bad Constant");
710 Out << "Constant* " << constName << " = 0; ";
715 /// printFunction - Print all aspects of a function.
717 void CppWriter::printFunction(const Function *F) {
718 std::string funcTypeName(getCppName(F->getFunctionType()));
722 Out << " = new Function(" << funcTypeName << ", " ;
723 printLinkageType(F->getLinkage());
724 Out << ",\n \"" << F->getName() << "\", mod);\n";
726 Out << "->setCallingConv(";
727 printCallingConv(F->getCallingConv());
729 if (F->hasSection()) {
731 Out << "->setSection(" << F->getSection() << ");\n";
733 if (F->getAlignment()) {
735 Out << "->setAlignment(" << F->getAlignment() << ");\n";
738 if (!F->isExternal()) {
740 // Create all the argument values
741 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
743 Out << " Argument* " << getCppName(AI) << " = new Argument("
744 << getCppName(AI->getType()) << ", \"";
745 printEscapedString(AI->getName());
746 Out << "\", " << getCppName(F) << ");\n";
748 // Create all the basic blocks
749 for (Function::const_iterator BI = F->begin(), BE = F->end();
751 std::string bbname(getCppName(BI));
752 Out << " BasicBlock* " << bbname << " = new BasicBlock(\"";
754 printEscapedString(BI->getName());
755 Out << "\"," << getCppName(BI->getParent()) << ",0);\n";
757 // Output all of its basic blocks... for the function
758 for (Function::const_iterator BI = F->begin(), BE = F->end();
760 // Output all of the instructions in the basic block...
762 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
764 std::string bbname(getCppName(BI));
765 printInstruction(I,bbname);
773 // printInstruction - This member is called for each Instruction in a function.
775 CppWriter::printInstruction(const Instruction *I, const std::string& bbname)
777 std::string iName(getCppName(I));
779 switch (I->getOpcode()) {
780 case Instruction::Ret: {
781 const ReturnInst* ret = cast<ReturnInst>(I);
782 Out << " ReturnInst* " << iName << " = new ReturnInst(";
783 if (ret->getReturnValue())
784 Out << getCppName(ret->getReturnValue()) << ", ";
785 Out << bbname << ");";
788 case Instruction::Br: {
789 const BranchInst* br = cast<BranchInst>(I);
790 Out << " BranchInst* " << iName << " = new BranchInst(" ;
791 if (br->getNumOperands() == 3 ) {
792 Out << getCppName(br->getOperand(0)) << ", "
793 << getCppName(br->getOperand(1)) << ", "
794 << getCppName(br->getOperand(2)) << ", ";
796 } else if (br->getNumOperands() == 1) {
797 Out << getCppName(br->getOperand(0)) << ", ";
799 assert(!"branch with 2 operands?");
801 Out << bbname << ");";
804 case Instruction::Switch:
805 case Instruction::Invoke:
806 case Instruction::Unwind:
807 case Instruction::Unreachable:
808 case Instruction::Add:
809 case Instruction::Sub:
810 case Instruction::Mul:
811 case Instruction::Div:
812 case Instruction::Rem:
813 case Instruction::And:
814 case Instruction::Or:
815 case Instruction::Xor:
816 case Instruction::SetEQ:
817 case Instruction::SetNE:
818 case Instruction::SetLE:
819 case Instruction::SetGE:
820 case Instruction::SetLT:
821 case Instruction::SetGT:
823 case Instruction::Malloc: {
824 const MallocInst* mallocI = cast<MallocInst>(I);
825 Out << " MallocInst* " << iName << " = new MallocInst("
826 << getCppName(mallocI->getAllocatedType()) << ", ";
827 if (mallocI->isArrayAllocation())
828 Out << getCppName(mallocI->getArraySize()) << ", ";
830 printEscapedString(mallocI->getName());
831 Out << "\", " << bbname << ");";
832 if (mallocI->getAlignment())
833 Out << "\n " << iName << "->setAlignment("
834 << mallocI->getAlignment() << ");";
837 case Instruction::Free:
838 case Instruction::Alloca: {
839 const AllocaInst* allocaI = cast<AllocaInst>(I);
840 Out << " AllocaInst* " << iName << " = new AllocaInst("
841 << getCppName(allocaI->getAllocatedType()) << ", ";
842 if (allocaI->isArrayAllocation())
843 Out << getCppName(allocaI->getArraySize()) << ", ";
845 printEscapedString(allocaI->getName());
846 Out << "\", " << bbname << ");";
847 if (allocaI->getAlignment())
848 Out << "\n " << iName << "->setAlignment("
849 << allocaI->getAlignment() << ");";
852 case Instruction::Load:
854 case Instruction::Store: {
855 const StoreInst* store = cast<StoreInst>(I);
856 Out << " StoreInst* " << iName << " = new StoreInst("
857 << getCppName(store->getOperand(0)) << ", "
858 << getCppName(store->getOperand(1)) << ", " << bbname << ");\n";
859 if (store->isVolatile())
860 Out << "iName->setVolatile(true);";
863 case Instruction::GetElementPtr: {
864 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
865 if (gep->getNumOperands() <= 2) {
866 Out << " GetElementPtrInst* " << iName << " = new GetElementPtrInst("
867 << getCppName(gep->getOperand(0));
868 if (gep->getNumOperands() == 2)
869 Out << ", " << getCppName(gep->getOperand(1));
870 Out << ", " << bbname;
872 Out << " std::vector<Value*> " << iName << "_indices;\n";
873 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
874 Out << " " << iName << "_indices.push_back("
875 << getCppName(gep->getOperand(i)) << ");\n";
877 Out << " Instruction* " << iName << " = new GetElementPtrInst("
878 << getCppName(gep->getOperand(0)) << ", " << iName << "_indices";
881 printEscapedString(gep->getName());
882 Out << "\", " << bbname << ");";
885 case Instruction::PHI:
886 case Instruction::Cast:
887 case Instruction::Call:
888 case Instruction::Shl:
889 case Instruction::Shr:
890 case Instruction::Select:
891 case Instruction::UserOp1:
892 case Instruction::UserOp2:
893 case Instruction::VAArg:
894 case Instruction::ExtractElement:
895 case Instruction::InsertElement:
896 case Instruction::ShuffleVector:
902 // Print out name if it exists...
904 Out << getLLVMName(I.getName()) << " = ";
906 // If this is a volatile load or store, print out the volatile marker.
907 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
908 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
910 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
911 // If this is a call, check if it's a tail call.
915 // Print out the opcode...
916 Out << I.getOpcodeName();
918 // Print out the type of the operands...
919 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
921 // Special case conditional branches to swizzle the condition out to the front
922 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
923 writeOperand(I.getOperand(2), true);
925 writeOperand(Operand, true);
927 writeOperand(I.getOperand(1), true);
929 } else if (isa<SwitchInst>(I)) {
930 // Special case switch statement to get formatting nice and correct...
931 writeOperand(Operand , true); Out << ',';
932 writeOperand(I.getOperand(1), true); Out << " [";
934 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
936 writeOperand(I.getOperand(op ), true); Out << ',';
937 writeOperand(I.getOperand(op+1), true);
940 } else if (isa<PHINode>(I)) {
942 printType(I.getType());
945 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
948 writeOperand(I.getOperand(op ), false); Out << ',';
949 writeOperand(I.getOperand(op+1), false); Out << " ]";
951 } else if (isa<ReturnInst>(I) && !Operand) {
953 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
954 // Print the calling convention being used.
955 switch (CI->getCallingConv()) {
956 case CallingConv::C: break; // default
957 case CallingConv::CSRet: Out << " csretcc"; break;
958 case CallingConv::Fast: Out << " fastcc"; break;
959 case CallingConv::Cold: Out << " coldcc"; break;
960 default: Out << " cc" << CI->getCallingConv(); break;
963 const PointerType *PTy = cast<PointerType>(Operand->getType());
964 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
965 const Type *RetTy = FTy->getReturnType();
967 // If possible, print out the short form of the call instruction. We can
968 // only do this if the first argument is a pointer to a nonvararg function,
969 // and if the return type is not a pointer to a function.
971 if (!FTy->isVarArg() &&
972 (!isa<PointerType>(RetTy) ||
973 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
974 Out << ' '; printType(RetTy);
975 writeOperand(Operand, false);
977 writeOperand(Operand, true);
980 if (CI->getNumOperands() > 1) writeOperand(CI->getOperand(1), true);
981 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
983 writeOperand(I.getOperand(op), true);
987 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
988 const PointerType *PTy = cast<PointerType>(Operand->getType());
989 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
990 const Type *RetTy = FTy->getReturnType();
992 // Print the calling convention being used.
993 switch (II->getCallingConv()) {
994 case CallingConv::C: break; // default
995 case CallingConv::CSRet: Out << " csretcc"; break;
996 case CallingConv::Fast: Out << " fastcc"; break;
997 case CallingConv::Cold: Out << " coldcc"; break;
998 default: Out << " cc" << II->getCallingConv(); break;
1001 // If possible, print out the short form of the invoke instruction. We can
1002 // only do this if the first argument is a pointer to a nonvararg function,
1003 // and if the return type is not a pointer to a function.
1005 if (!FTy->isVarArg() &&
1006 (!isa<PointerType>(RetTy) ||
1007 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
1008 Out << ' '; printType(RetTy);
1009 writeOperand(Operand, false);
1011 writeOperand(Operand, true);
1015 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
1016 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
1018 writeOperand(I.getOperand(op), true);
1021 Out << " )\n\t\t\tto";
1022 writeOperand(II->getNormalDest(), true);
1024 writeOperand(II->getUnwindDest(), true);
1026 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
1028 printType(AI->getType()->getElementType());
1029 if (AI->isArrayAllocation()) {
1031 writeOperand(AI->getArraySize(), true);
1033 if (AI->getAlignment()) {
1034 Out << ", align " << AI->getAlignment();
1036 } else if (isa<CastInst>(I)) {
1037 if (Operand) writeOperand(Operand, true); // Work with broken code
1039 printType(I.getType());
1040 } else if (isa<VAArgInst>(I)) {
1041 if (Operand) writeOperand(Operand, true); // Work with broken code
1043 printType(I.getType());
1044 } else if (Operand) { // Print the normal way...
1046 // PrintAllTypes - Instructions who have operands of all the same type
1047 // omit the type from all but the first operand. If the instruction has
1048 // different type operands (for example br), then they are all printed.
1049 bool PrintAllTypes = false;
1050 const Type *TheType = Operand->getType();
1052 // Shift Left & Right print both types even for Ubyte LHS, and select prints
1053 // types even if all operands are bools.
1054 if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
1055 isa<ShuffleVectorInst>(I)) {
1056 PrintAllTypes = true;
1058 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1059 Operand = I.getOperand(i);
1060 if (Operand->getType() != TheType) {
1061 PrintAllTypes = true; // We have differing types! Print them all!
1067 if (!PrintAllTypes) {
1072 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
1074 writeOperand(I.getOperand(i), PrintAllTypes);
1082 } // end anonymous llvm
1086 void WriteModuleToCppFile(Module* mod, std::ostream& o) {
1087 o << "#include <llvm/Module.h>\n";
1088 o << "#include <llvm/DerivedTypes.h>\n";
1089 o << "#include <llvm/Constants.h>\n";
1090 o << "#include <llvm/GlobalVariable.h>\n";
1091 o << "#include <llvm/Function.h>\n";
1092 o << "#include <llvm/CallingConv.h>\n";
1093 o << "#include <llvm/BasicBlock.h>\n";
1094 o << "#include <llvm/Instructions.h>\n";
1095 o << "#include <llvm/Pass.h>\n";
1096 o << "#include <llvm/PassManager.h>\n";
1097 o << "#include <llvm/Analysis/Verifier.h>\n";
1098 o << "#include <llvm/Assembly/PrintModulePass.h>\n";
1099 o << "#include <algorithm>\n";
1100 o << "#include <iostream>\n\n";
1101 o << "using namespace llvm;\n\n";
1102 o << "Module* makeLLVMModule();\n\n";
1103 o << "int main(int argc, char**argv) {\n";
1104 o << " Module* Mod = makeLLVMModule();\n";
1105 o << " verifyModule(*Mod, PrintMessageAction);\n";
1106 o << " std::cerr.flush();\n";
1107 o << " std::cout.flush();\n";
1108 o << " PassManager PM;\n";
1109 o << " PM.add(new PrintModulePass(&std::cout));\n";
1110 o << " PM.run(*Mod);\n";
1111 o << " return 0;\n";
1113 o << "Module* makeLLVMModule() {\n";
1114 CppWriter W(o, mod);
1116 o << "return mod;\n";