blob: 2783467aa737eb52d0793313cbd7fd7179e2728c [file] [log] [blame]
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001//===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Reid Spencere0d133f2006-05-29 18:08:06 +00005// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00007//
8//===----------------------------------------------------------------------===//
9//
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.
12//
13//===----------------------------------------------------------------------===//
14
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"
Reid Spencer15f7e012006-05-30 21:18:23 +000027#include "llvm/Support/CommandLine.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000028#include <algorithm>
29#include <iostream>
Reid Spencer66c87342006-05-30 03:43:49 +000030#include <set>
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000031
32using namespace llvm;
33
Reid Spencer15f7e012006-05-30 21:18:23 +000034static cl::opt<std::string>
35ModName("modname", cl::desc("Specify the module name to use"),
36 cl::value_desc("module name"));
37
38static cl::opt<std::string>
39FuncName("funcname", cl::desc("Specify the name of the generated function"),
40 cl::value_desc("function name"));
41
42static cl::opt<bool>
43Fragment("fragment", cl::desc("Don't generate a complete program"));
44
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000045namespace {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000046typedef std::vector<const Type*> TypeList;
47typedef std::map<const Type*,std::string> TypeMap;
48typedef std::map<const Value*,std::string> ValueMap;
Reid Spencer66c87342006-05-30 03:43:49 +000049typedef std::set<std::string> NameSet;
Reid Spencer15f7e012006-05-30 21:18:23 +000050typedef std::set<const Type*> TypeSet;
51typedef std::set<const Value*> ValueSet;
52typedef std::map<const Value*,std::string> ForwardRefMap;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000053
Reid Spencere0d133f2006-05-29 18:08:06 +000054class CppWriter {
55 std::ostream &Out;
56 const Module *TheModule;
57 unsigned long uniqueNum;
58 TypeMap TypeNames;
59 ValueMap ValueNames;
60 TypeMap UnresolvedTypes;
61 TypeList TypeStack;
Reid Spencer66c87342006-05-30 03:43:49 +000062 NameSet UsedNames;
Reid Spencer15f7e012006-05-30 21:18:23 +000063 TypeSet DefinedTypes;
64 ValueSet DefinedValues;
65 ForwardRefMap ForwardRefs;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000066
Reid Spencere0d133f2006-05-29 18:08:06 +000067public:
68 inline CppWriter(std::ostream &o, const Module *M)
69 : Out(o), TheModule(M), uniqueNum(0), TypeNames(),
70 ValueNames(), UnresolvedTypes(), TypeStack() { }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000071
Reid Spencere0d133f2006-05-29 18:08:06 +000072 const Module* getModule() { return TheModule; }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000073
Reid Spencere0d133f2006-05-29 18:08:06 +000074 void printModule(const Module *M);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000075
Reid Spencere0d133f2006-05-29 18:08:06 +000076private:
77 void printTypes(const Module* M);
78 void printConstants(const Module* M);
79 void printConstant(const Constant *CPV);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +000080 void printGlobalHead(const GlobalVariable *GV);
81 void printGlobalBody(const GlobalVariable *GV);
Reid Spencer66c87342006-05-30 03:43:49 +000082 void printFunctionHead(const Function *F);
83 void printFunctionBody(const Function *F);
Reid Spencere0d133f2006-05-29 18:08:06 +000084 void printInstruction(const Instruction *I, const std::string& bbname);
85 void printSymbolTable(const SymbolTable &ST);
86 void printLinkageType(GlobalValue::LinkageTypes LT);
87 void printCallingConv(unsigned cc);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000088
Reid Spencere0d133f2006-05-29 18:08:06 +000089 std::string getCppName(const Type* val);
90 std::string getCppName(const Value* val);
91 inline void printCppName(const Value* val);
92 inline void printCppName(const Type* val);
93 bool isOnStack(const Type*) const;
94 inline void printTypeDef(const Type* Ty);
95 bool printTypeDefInternal(const Type* Ty);
96 void printEscapedString(const std::string& str);
Reid Spencer15f7e012006-05-30 21:18:23 +000097
98 std::string getOpName(Value*);
99
100 void printCFP(const ConstantFP* CFP);
Reid Spencere0d133f2006-05-29 18:08:06 +0000101};
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000102
Reid Spencer15f7e012006-05-30 21:18:23 +0000103// printCFP - Print a floating point constant .. very carefully :)
104// This makes sure that conversion to/from floating yields the same binary
105// result so that we don't lose precision.
106void
107CppWriter::printCFP(const ConstantFP *CFP) {
108#if HAVE_PRINTF_A
109 char Buffer[100];
110 sprintf(Buffer, "%A", CFP->getValue());
111 if ((!strncmp(Buffer, "0x", 2) ||
112 !strncmp(Buffer, "-0x", 3) ||
113 !strncmp(Buffer, "+0x", 3)) &&
114 (atof(Buffer) == CFP->getValue()))
115 Out << Buffer;
116 else {
117#else
118 std::string StrVal = ftostr(CFP->getValue());
119
120 while (StrVal[0] == ' ')
121 StrVal.erase(StrVal.begin());
122
123 // Check to make sure that the stringized number is not some string like "Inf"
124 // or NaN. Check that the string matches the "[-+]?[0-9]" regex.
125 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
126 ((StrVal[0] == '-' || StrVal[0] == '+') &&
127 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
128 (atof(StrVal.c_str()) == CFP->getValue()))
129 Out << StrVal;
130 else if (CFP->getType() == Type::DoubleTy) {
131 Out << "0x" << std::hex << DoubleToBits(CFP->getValue()) << std::dec
132 << "ULL /* " << StrVal << " */";
133 } else {
134 Out << "0x" << std::hex << FloatToBits(CFP->getValue()) << std::dec
135 << "U /* " << StrVal << " */";
136 }
137#endif
138#if HAVE_PRINTF_A
139 }
140#endif
141}
142
143std::string
144CppWriter::getOpName(Value* V) {
145 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
146 return getCppName(V);
147
148 // See if its alread in the map of forward references, if so just return the
149 // name we already set up for it
150 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
151 if (I != ForwardRefs.end())
152 return I->second;
153
154 // This is a new forward reference. Generate a unique name for it
155 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
156
157 // Yes, this is a hack. An Argument is the smallest instantiable value that
158 // we can make as a placeholder for the real value. We'll replace these
159 // Argument instances later.
160 Out << " Argument* " << result << " = new Argument("
161 << getCppName(V->getType()) << ");\n";
162 ForwardRefs[V] = result;
163 return result;
164}
165
Reid Spencere0d133f2006-05-29 18:08:06 +0000166// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000167// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000168void
169CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000170 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
171 unsigned char C = Str[i];
172 if (isprint(C) && C != '"' && C != '\\') {
173 Out << C;
174 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000175 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000176 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
177 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
178 }
179 }
180}
181
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000182inline void
183sanitize(std::string& str) {
184 for (size_t i = 0; i < str.length(); ++i)
185 if (!isalnum(str[i]) && str[i] != '_')
186 str[i] = '_';
187}
188
Reid Spencer66c87342006-05-30 03:43:49 +0000189inline const char*
190getTypePrefix(const Type* Ty ) {
191 const char* prefix;
192 switch (Ty->getTypeID()) {
193 case Type::VoidTyID: prefix = "void_"; break;
194 case Type::BoolTyID: prefix = "bool_"; break;
195 case Type::UByteTyID: prefix = "ubyte_"; break;
196 case Type::SByteTyID: prefix = "sbyte_"; break;
197 case Type::UShortTyID: prefix = "ushort_"; break;
198 case Type::ShortTyID: prefix = "short_"; break;
199 case Type::UIntTyID: prefix = "uint_"; break;
200 case Type::IntTyID: prefix = "int_"; break;
201 case Type::ULongTyID: prefix = "ulong_"; break;
202 case Type::LongTyID: prefix = "long_"; break;
203 case Type::FloatTyID: prefix = "float_"; break;
204 case Type::DoubleTyID: prefix = "double_"; break;
205 case Type::LabelTyID: prefix = "label_"; break;
206 case Type::FunctionTyID: prefix = "func_"; break;
207 case Type::StructTyID: prefix = "struct_"; break;
208 case Type::ArrayTyID: prefix = "array_"; break;
209 case Type::PointerTyID: prefix = "ptr_"; break;
210 case Type::PackedTyID: prefix = "packed_"; break;
211 case Type::OpaqueTyID: prefix = "opaque_"; break;
212 default: prefix = "other_"; break;
213 }
214 return prefix;
215}
216
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000217std::string
218CppWriter::getCppName(const Value* val) {
219 std::string name;
220 ValueMap::iterator I = ValueNames.find(val);
Reid Spencer66c87342006-05-30 03:43:49 +0000221 if (I != ValueNames.end() && I->first == val)
222 return I->second;
223
224 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
225 name = std::string("gvar_") +
226 getTypePrefix(GV->getType()->getElementType());
227 } else if (const Function* F = dyn_cast<Function>(val)) {
228 name = std::string("func_");
229 } else if (const Constant* C = dyn_cast<Constant>(val)) {
230 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000231 } else {
Reid Spencer66c87342006-05-30 03:43:49 +0000232 name = getTypePrefix(val->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000233 }
Reid Spencer66c87342006-05-30 03:43:49 +0000234 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000235 sanitize(name);
Reid Spencer66c87342006-05-30 03:43:49 +0000236 NameSet::iterator NI = UsedNames.find(name);
237 if (NI != UsedNames.end())
238 name += std::string("_") + utostr(uniqueNum++);
239 UsedNames.insert(name);
240 return ValueNames[val] = name;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000241}
242
243void
244CppWriter::printCppName(const Value* val) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000245 printEscapedString(getCppName(val));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000246}
247
248void
249CppWriter::printCppName(const Type* Ty)
250{
Reid Spencere0d133f2006-05-29 18:08:06 +0000251 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000252}
253
254// Gets the C++ name for a type. Returns true if we already saw the type,
255// false otherwise.
256//
257inline const std::string*
258findTypeName(const SymbolTable& ST, const Type* Ty)
259{
260 SymbolTable::type_const_iterator TI = ST.type_begin();
261 SymbolTable::type_const_iterator TE = ST.type_end();
262 for (;TI != TE; ++TI)
263 if (TI->second == Ty)
264 return &(TI->first);
265 return 0;
266}
267
268std::string
269CppWriter::getCppName(const Type* Ty)
270{
271 // First, handle the primitive types .. easy
272 if (Ty->isPrimitiveType()) {
273 switch (Ty->getTypeID()) {
274 case Type::VoidTyID: return "Type::VoidTy";
275 case Type::BoolTyID: return "Type::BoolTy";
276 case Type::UByteTyID: return "Type::UByteTy";
277 case Type::SByteTyID: return "Type::SByteTy";
278 case Type::UShortTyID: return "Type::UShortTy";
279 case Type::ShortTyID: return "Type::ShortTy";
280 case Type::UIntTyID: return "Type::UIntTy";
281 case Type::IntTyID: return "Type::IntTy";
282 case Type::ULongTyID: return "Type::ULongTy";
283 case Type::LongTyID: return "Type::LongTy";
284 case Type::FloatTyID: return "Type::FloatTy";
285 case Type::DoubleTyID: return "Type::DoubleTy";
286 case Type::LabelTyID: return "Type::LabelTy";
287 default:
288 assert(!"Can't get here");
289 break;
290 }
291 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
292 }
293
294 // Now, see if we've seen the type before and return that
295 TypeMap::iterator I = TypeNames.find(Ty);
296 if (I != TypeNames.end())
297 return I->second;
298
299 // Okay, let's build a new name for this type. Start with a prefix
300 const char* prefix = 0;
301 switch (Ty->getTypeID()) {
302 case Type::FunctionTyID: prefix = "FuncTy_"; break;
303 case Type::StructTyID: prefix = "StructTy_"; break;
304 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
305 case Type::PointerTyID: prefix = "PointerTy_"; break;
306 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
307 case Type::PackedTyID: prefix = "PackedTy_"; break;
308 default: prefix = "OtherTy_"; break; // prevent breakage
309 }
310
311 // See if the type has a name in the symboltable and build accordingly
312 const std::string* tName = findTypeName(TheModule->getSymbolTable(), Ty);
313 std::string name;
314 if (tName)
315 name = std::string(prefix) + *tName;
316 else
317 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000318 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000319
320 // Save the name
321 return TypeNames[Ty] = name;
322}
323
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000324void CppWriter::printModule(const Module *M) {
325 Out << "\n// Module Construction\n";
326 Out << "Module* mod = new Module(\"";
Reid Spencer15f7e012006-05-30 21:18:23 +0000327 if (!ModName.empty())
328 printEscapedString(ModName);
329 else if (M->getModuleIdentifier() == "-")
Reid Spencere0d133f2006-05-29 18:08:06 +0000330 printEscapedString("<stdin>");
331 else
332 printEscapedString(M->getModuleIdentifier());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000333 Out << "\");\n";
334 Out << "mod->setEndianness(";
335 switch (M->getEndianness()) {
336 case Module::LittleEndian: Out << "Module::LittleEndian);\n"; break;
337 case Module::BigEndian: Out << "Module::BigEndian);\n"; break;
338 case Module::AnyEndianness:Out << "Module::AnyEndianness);\n"; break;
339 }
340 Out << "mod->setPointerSize(";
341 switch (M->getPointerSize()) {
342 case Module::Pointer32: Out << "Module::Pointer32);\n"; break;
343 case Module::Pointer64: Out << "Module::Pointer64);\n"; break;
344 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);\n"; break;
345 }
346 if (!M->getTargetTriple().empty())
347 Out << "mod->setTargetTriple(\"" << M->getTargetTriple() << "\");\n";
348
349 if (!M->getModuleInlineAsm().empty()) {
350 Out << "mod->setModuleInlineAsm(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000351 printEscapedString(M->getModuleInlineAsm());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000352 Out << "\");\n";
353 }
354
355 // Loop over the dependent libraries and emit them.
356 Module::lib_iterator LI = M->lib_begin();
357 Module::lib_iterator LE = M->lib_end();
358 while (LI != LE) {
359 Out << "mod->addLibrary(\"" << *LI << "\");\n";
360 ++LI;
361 }
362
363 // Print out all the type definitions
364 Out << "\n// Type Definitions\n";
365 printTypes(M);
366
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000367 // Functions can call each other and global variables can reference them so
368 // define all the functions first before emitting their function bodies.
Reid Spencer66c87342006-05-30 03:43:49 +0000369 Out << "\n// Function Declarations\n";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000370 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Reid Spencer66c87342006-05-30 03:43:49 +0000371 printFunctionHead(I);
372
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000373 // Process the global variables declarations. We can't initialze them until
374 // after the constants are printed so just print a header for each global
375 Out << "\n// Global Variable Declarations\n";
376 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
377 I != E; ++I) {
378 printGlobalHead(I);
379 }
380
381 // Print out all the constants definitions. Constants don't recurse except
382 // through GlobalValues. All GlobalValues have been declared at this point
383 // so we can proceed to generate the constants.
384 Out << "\n// Constant Definitions\n";
385 printConstants(M);
386
387 // Process the global variables definitions now that all the constants have
388 // been emitted. These definitions just couple the gvars with their constant
389 // initializers.
390 Out << "\n// Global Variable Definitions\n";
391 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
392 I != E; ++I) {
393 printGlobalBody(I);
394 }
395
396 // Finally, we can safely put out all of the function bodies.
Reid Spencer66c87342006-05-30 03:43:49 +0000397 Out << "\n// Function Definitions\n";
398 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000399 if (!I->isExternal()) {
400 Out << "\n// Function: " << I->getName() << " (" << getCppName(I)
401 << ")\n{\n";
402 printFunctionBody(I);
403 Out << "}\n";
404 }
Reid Spencer66c87342006-05-30 03:43:49 +0000405 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000406}
407
408void
409CppWriter::printCallingConv(unsigned cc){
410 // Print the calling convention.
411 switch (cc) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000412 case CallingConv::C: Out << "CallingConv::C"; break;
413 case CallingConv::CSRet: Out << "CallingConv::CSRet"; break;
414 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
415 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
416 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
Reid Spencer66c87342006-05-30 03:43:49 +0000417 default: Out << cc; break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000418 }
419}
420
421void
422CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
423 switch (LT) {
424 case GlobalValue::InternalLinkage:
425 Out << "GlobalValue::InternalLinkage"; break;
426 case GlobalValue::LinkOnceLinkage:
427 Out << "GlobalValue::LinkOnceLinkage "; break;
428 case GlobalValue::WeakLinkage:
429 Out << "GlobalValue::WeakLinkage"; break;
430 case GlobalValue::AppendingLinkage:
431 Out << "GlobalValue::AppendingLinkage"; break;
432 case GlobalValue::ExternalLinkage:
433 Out << "GlobalValue::ExternalLinkage"; break;
434 case GlobalValue::GhostLinkage:
435 Out << "GlobalValue::GhostLinkage"; break;
436 }
437}
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000438
439void CppWriter::printGlobalHead(const GlobalVariable *GV) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000440 Out << "\n";
441 Out << "GlobalVariable* ";
442 printCppName(GV);
443 Out << " = new GlobalVariable(\n";
444 Out << " /*Type=*/";
445 printCppName(GV->getType()->getElementType());
446 Out << ",\n";
447 Out << " /*isConstant=*/" << (GV->isConstant()?"true":"false")
448 << ",\n /*Linkage=*/";
449 printLinkageType(GV->getLinkage());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000450 Out << ",\n /*Initializer=*/0, ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000451 if (GV->hasInitializer()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000452 Out << "// has initializer, specified below";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000453 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000454 Out << "\n /*Name=*/\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000455 printEscapedString(GV->getName());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000456 Out << "\",\n mod);\n";
457
458 if (GV->hasSection()) {
459 printCppName(GV);
460 Out << "->setSection(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000461 printEscapedString(GV->getSection());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000462 Out << "\");\n";
463 }
464 if (GV->getAlignment()) {
465 printCppName(GV);
466 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");\n";
467 };
468}
469
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000470void
471CppWriter::printGlobalBody(const GlobalVariable *GV) {
472 if (GV->hasInitializer()) {
473 printCppName(GV);
474 Out << "->setInitializer(";
475 //if (!isa<GlobalValue(GV->getInitializer()))
476 //else
477 Out << getCppName(GV->getInitializer()) << ");\n";
478 }
479}
480
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000481bool
482CppWriter::isOnStack(const Type* Ty) const {
483 TypeList::const_iterator TI =
484 std::find(TypeStack.begin(),TypeStack.end(),Ty);
485 return TI != TypeStack.end();
486}
487
488// Prints a type definition. Returns true if it could not resolve all the types
489// in the definition but had to use a forward reference.
490void
491CppWriter::printTypeDef(const Type* Ty) {
492 assert(TypeStack.empty());
493 TypeStack.clear();
494 printTypeDefInternal(Ty);
495 assert(TypeStack.empty());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000496}
497
498bool
499CppWriter::printTypeDefInternal(const Type* Ty) {
500 // We don't print definitions for primitive types
501 if (Ty->isPrimitiveType())
502 return false;
503
Reid Spencer15f7e012006-05-30 21:18:23 +0000504 // If we already defined this type, we don't need to define it again.
505 if (DefinedTypes.find(Ty) != DefinedTypes.end())
506 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000507
Reid Spencer15f7e012006-05-30 21:18:23 +0000508 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000509 std::string typeName(getCppName(Ty));
510
511 // Search the type stack for recursion. If we find it, then generate this
512 // as an OpaqueType, but make sure not to do this multiple times because
513 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000514 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000515 // check the UnresolvedTypes list as well.
516 if (isOnStack(Ty)) {
517 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
518 if (I == UnresolvedTypes.end()) {
519 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();\n";
520 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000521 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000522 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000523 }
524
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000525 // We're going to print a derived type which, by definition, contains other
526 // types. So, push this one we're printing onto the type stack to assist with
527 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000528 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000529
530 // Print the type definition
531 switch (Ty->getTypeID()) {
532 case Type::FunctionTyID: {
533 const FunctionType* FT = cast<FunctionType>(Ty);
534 Out << "std::vector<const Type*>" << typeName << "_args;\n";
535 FunctionType::param_iterator PI = FT->param_begin();
536 FunctionType::param_iterator PE = FT->param_end();
537 for (; PI != PE; ++PI) {
538 const Type* argTy = static_cast<const Type*>(*PI);
539 bool isForward = printTypeDefInternal(argTy);
540 std::string argName(getCppName(argTy));
541 Out << typeName << "_args.push_back(" << argName;
542 if (isForward)
543 Out << "_fwd";
544 Out << ");\n";
545 }
546 bool isForward = printTypeDefInternal(FT->getReturnType());
547 std::string retTypeName(getCppName(FT->getReturnType()));
548 Out << "FunctionType* " << typeName << " = FunctionType::get(\n"
549 << " /*Result=*/" << retTypeName;
550 if (isForward)
551 Out << "_fwd";
552 Out << ",\n /*Params=*/" << typeName << "_args,\n /*isVarArg=*/"
553 << (FT->isVarArg() ? "true" : "false") << ");\n";
554 break;
555 }
556 case Type::StructTyID: {
557 const StructType* ST = cast<StructType>(Ty);
558 Out << "std::vector<const Type*>" << typeName << "_fields;\n";
559 StructType::element_iterator EI = ST->element_begin();
560 StructType::element_iterator EE = ST->element_end();
561 for (; EI != EE; ++EI) {
562 const Type* fieldTy = static_cast<const Type*>(*EI);
563 bool isForward = printTypeDefInternal(fieldTy);
564 std::string fieldName(getCppName(fieldTy));
565 Out << typeName << "_fields.push_back(" << fieldName;
566 if (isForward)
567 Out << "_fwd";
568 Out << ");\n";
569 }
570 Out << "StructType* " << typeName << " = StructType::get("
571 << typeName << "_fields);\n";
572 break;
573 }
574 case Type::ArrayTyID: {
575 const ArrayType* AT = cast<ArrayType>(Ty);
576 const Type* ET = AT->getElementType();
577 bool isForward = printTypeDefInternal(ET);
578 std::string elemName(getCppName(ET));
579 Out << "ArrayType* " << typeName << " = ArrayType::get("
580 << elemName << (isForward ? "_fwd" : "")
581 << ", " << utostr(AT->getNumElements()) << ");\n";
582 break;
583 }
584 case Type::PointerTyID: {
585 const PointerType* PT = cast<PointerType>(Ty);
586 const Type* ET = PT->getElementType();
587 bool isForward = printTypeDefInternal(ET);
588 std::string elemName(getCppName(ET));
589 Out << "PointerType* " << typeName << " = PointerType::get("
590 << elemName << (isForward ? "_fwd" : "") << ");\n";
591 break;
592 }
593 case Type::PackedTyID: {
594 const PackedType* PT = cast<PackedType>(Ty);
595 const Type* ET = PT->getElementType();
596 bool isForward = printTypeDefInternal(ET);
597 std::string elemName(getCppName(ET));
598 Out << "PackedType* " << typeName << " = PackedType::get("
599 << elemName << (isForward ? "_fwd" : "")
600 << ", " << utostr(PT->getNumElements()) << ");\n";
601 break;
602 }
603 case Type::OpaqueTyID: {
604 const OpaqueType* OT = cast<OpaqueType>(Ty);
605 Out << "OpaqueType* " << typeName << " = OpaqueType::get();\n";
606 break;
607 }
608 default:
609 assert(!"Invalid TypeID");
610 }
611
Reid Spencer74e032a2006-05-29 02:58:15 +0000612 // If the type had a name, make sure we recreate it.
613 const std::string* progTypeName =
614 findTypeName(TheModule->getSymbolTable(),Ty);
615 if (progTypeName)
616 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
617 << typeName << ");\n";
618
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000619 // Pop us off the type stack
620 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000621
622 // Indicate that this type is now defined.
623 DefinedTypes.insert(Ty);
624
625 // Early resolve as many unresolved types as possible. Search the unresolved
626 // types map for the type we just printed. Now that its definition is complete
627 // we can resolve any previous references to it. This prevents a cascade of
628 // unresolved types.
629 TypeMap::iterator I = UnresolvedTypes.find(Ty);
630 if (I != UnresolvedTypes.end()) {
631 Out << "cast<OpaqueType>(" << I->second
632 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");\n";
633 Out << I->second << " = cast<";
634 switch (Ty->getTypeID()) {
635 case Type::FunctionTyID: Out << "FunctionType"; break;
636 case Type::ArrayTyID: Out << "ArrayType"; break;
637 case Type::StructTyID: Out << "StructType"; break;
638 case Type::PackedTyID: Out << "PackedType"; break;
639 case Type::PointerTyID: Out << "PointerType"; break;
640 case Type::OpaqueTyID: Out << "OpaqueType"; break;
641 default: Out << "NoSuchDerivedType"; break;
642 }
643 Out << ">(" << I->second << "_fwd.get());\n\n";
644 UnresolvedTypes.erase(I);
645 }
646
647 // Finally, separate the type definition from other with a newline.
Reid Spencere0d133f2006-05-29 18:08:06 +0000648 Out << "\n";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000649
650 // We weren't a recursive type
651 return false;
652}
653
654void
655CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000656
657 // Walk the symbol table and print out all its types
658 const SymbolTable& symtab = M->getSymbolTable();
659 for (SymbolTable::type_const_iterator TI = symtab.type_begin(),
660 TE = symtab.type_end(); TI != TE; ++TI) {
661
662 // For primitive types and types already defined, just add a name
663 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
664 if (TI->second->isPrimitiveType() || TNI != TypeNames.end()) {
665 Out << "mod->addTypeName(\"";
666 printEscapedString(TI->first);
667 Out << "\", " << getCppName(TI->second) << ");\n";
668 // For everything else, define the type
669 } else {
670 printTypeDef(TI->second);
671 }
672 }
673
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000674 // Add all of the global variables to the value table...
675 for (Module::const_global_iterator I = TheModule->global_begin(),
676 E = TheModule->global_end(); I != E; ++I) {
677 if (I->hasInitializer())
678 printTypeDef(I->getInitializer()->getType());
679 printTypeDef(I->getType());
680 }
681
682 // Add all the functions to the table
683 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
684 FI != FE; ++FI) {
685 printTypeDef(FI->getReturnType());
686 printTypeDef(FI->getFunctionType());
687 // Add all the function arguments
688 for(Function::const_arg_iterator AI = FI->arg_begin(),
689 AE = FI->arg_end(); AI != AE; ++AI) {
690 printTypeDef(AI->getType());
691 }
692
693 // Add all of the basic blocks and instructions
694 for (Function::const_iterator BB = FI->begin(),
695 E = FI->end(); BB != E; ++BB) {
696 printTypeDef(BB->getType());
697 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
698 ++I) {
699 printTypeDef(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000700 for (unsigned i = 0; i < I->getNumOperands(); ++i)
701 printTypeDef(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000702 }
703 }
704 }
705}
706
707void
708CppWriter::printConstants(const Module* M) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000709 // Add all of the global variables to the value table...
710 for (Module::const_global_iterator I = TheModule->global_begin(),
711 E = TheModule->global_end(); I != E; ++I)
712 if (I->hasInitializer())
713 printConstant(I->getInitializer());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000714
Reid Spencere0d133f2006-05-29 18:08:06 +0000715 // Traverse the LLVM functions looking for constants
716 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
717 FI != FE; ++FI) {
718 // Add all of the basic blocks and instructions
719 for (Function::const_iterator BB = FI->begin(),
720 E = FI->end(); BB != E; ++BB) {
721 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
722 ++I) {
723 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
724 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
725 printConstant(C);
726 }
727 }
728 }
729 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000730 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000731}
732
Reid Spencere0d133f2006-05-29 18:08:06 +0000733// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000734void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000735 // First, if the constant is actually a GlobalValue (variable or function) or
736 // its already in the constant list then we've printed it already and we can
737 // just return.
738 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000739 return;
740
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000741 const int IndentSize = 2;
742 static std::string Indent = "\n";
743 std::string constName(getCppName(CV));
744 std::string typeName(getCppName(CV->getType()));
745 if (CV->isNullValue()) {
746 Out << "Constant* " << constName << " = Constant::getNullValue("
747 << typeName << ");\n";
748 return;
749 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000750 if (isa<GlobalValue>(CV)) {
751 // Skip variables and functions, we emit them elsewhere
752 return;
753 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000754 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000755 Out << "ConstantBool* " << constName << " = ConstantBool::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000756 << (CB == ConstantBool::True ? "true" : "false")
757 << ");";
758 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000759 Out << "ConstantSInt* " << constName << " = ConstantSInt::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000760 << typeName << ", " << CI->getValue() << ");";
761 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000762 Out << "ConstantUInt* " << constName << " = ConstantUInt::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000763 << typeName << ", " << CI->getValue() << ");";
764 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000765 Out << "ConstantAggregateZero* " << constName
766 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000767 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000768 Out << "ConstantPointerNull* " << constName
769 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000770 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000771 Out << "ConstantFP* " << constName << " = ConstantFP::get(" << typeName
772 << ", ";
Reid Spencer15f7e012006-05-30 21:18:23 +0000773 char buffer[64];
774 sprintf(buffer,"%A",CFP->getValue());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000775 // We would like to output the FP constant value in exponential notation,
776 // but we cannot do this if doing so will lose precision. Check here to
777 // make sure that we only output it in exponential format if we can parse
778 // the value back and get the same value.
779 //
780 std::string StrVal = ftostr(CFP->getValue());
781
782 // Check to make sure that the stringized number is not some string like
783 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
784 // the string matches the "[-+]?[0-9]" regex.
785 //
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000786 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000787 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000788 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
Reid Spencer15f7e012006-05-30 21:18:23 +0000789 (atof(StrVal.c_str()) == CFP->getValue()))
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000790 {
Reid Spencer15f7e012006-05-30 21:18:23 +0000791 Out << StrVal;
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000792 } else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000793
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000794 // Otherwise we could not reparse it to exactly the same value, so we must
795 // output the string in hexadecimal format!
Reid Spencer15f7e012006-05-30 21:18:23 +0000796 assert(sizeof(double) == sizeof(uint64_t) &&
797 "assuming that double is 64 bits!");
798 Out << "0x" << std::hex << DoubleToBits(CFP->getValue()) << std::dec
799 << "ULL /* " << StrVal << " */";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000800 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000801 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000802 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000803 if (CA->isString() && CA->getType()->getElementType() == Type::SByteTy) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000804 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000805 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000806 // Determine if we want null termination or not.
807 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000808 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000809 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000810 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000811 Out << ");";
812 } else {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000813 Out << "std::vector<Constant*> " << constName << "_elems;\n";
814 unsigned N = CA->getNumOperands();
815 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000816 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000817 Out << constName << "_elems.push_back("
818 << getCppName(CA->getOperand(i)) << ");\n";
819 }
820 Out << "Constant* " << constName << " = ConstantArray::get("
821 << typeName << ", " << constName << "_elems);";
822 }
823 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
824 Out << "std::vector<Constant*> " << constName << "_fields;\n";
825 unsigned N = CS->getNumOperands();
826 for (unsigned i = 0; i < N; i++) {
827 printConstant(CS->getOperand(i));
828 Out << constName << "_fields.push_back("
Reid Spencer66c87342006-05-30 03:43:49 +0000829 << getCppName(CS->getOperand(i)) << ");\n";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000830 }
831 Out << "Constant* " << constName << " = ConstantStruct::get("
832 << typeName << ", " << constName << "_fields);";
833 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
834 Out << "std::vector<Constant*> " << constName << "_elems;\n";
835 unsigned N = CP->getNumOperands();
836 for (unsigned i = 0; i < N; ++i) {
837 printConstant(CP->getOperand(i));
838 Out << constName << "_elems.push_back("
839 << getCppName(CP->getOperand(i)) << ");\n";
840 }
841 Out << "Constant* " << constName << " = ConstantPacked::get("
842 << typeName << ", " << constName << "_elems);";
843 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000844 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000845 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000846 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000847 if (CE->getOpcode() == Instruction::GetElementPtr) {
848 Out << "std::vector<Constant*> " << constName << "_indices;\n";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000849 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000850 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000851 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000852 Out << constName << "_indices.push_back("
853 << getCppName(CE->getOperand(i)) << ");\n";
854 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000855 Out << "Constant* " << constName
856 << " = ConstantExpr::getGetElementPtr("
857 << getCppName(CE->getOperand(0)) << ", "
858 << constName << "_indices);";
Reid Spencere0d133f2006-05-29 18:08:06 +0000859 } else if (CE->getOpcode() == Instruction::Cast) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000860 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000861 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
862 Out << getCppName(CE->getOperand(0)) << ", " << getCppName(CE->getType())
863 << ");";
864 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000865 unsigned N = CE->getNumOperands();
866 for (unsigned i = 0; i < N; ++i ) {
867 printConstant(CE->getOperand(i));
868 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000869 Out << "Constant* " << constName << " = ConstantExpr::";
870 switch (CE->getOpcode()) {
871 case Instruction::Add: Out << "getAdd"; break;
872 case Instruction::Sub: Out << "getSub"; break;
873 case Instruction::Mul: Out << "getMul"; break;
874 case Instruction::Div: Out << "getDiv"; break;
875 case Instruction::Rem: Out << "getRem"; break;
876 case Instruction::And: Out << "getAnd"; break;
877 case Instruction::Or: Out << "getOr"; break;
878 case Instruction::Xor: Out << "getXor"; break;
879 case Instruction::SetEQ: Out << "getSetEQ"; break;
880 case Instruction::SetNE: Out << "getSetNE"; break;
881 case Instruction::SetLE: Out << "getSetLE"; break;
882 case Instruction::SetGE: Out << "getSetGE"; break;
883 case Instruction::SetLT: Out << "getSetLT"; break;
884 case Instruction::SetGT: Out << "getSetGT"; break;
885 case Instruction::Shl: Out << "getShl"; break;
886 case Instruction::Shr: Out << "getShr"; break;
887 case Instruction::Select: Out << "getSelect"; break;
888 case Instruction::ExtractElement: Out << "getExtractElement"; break;
889 case Instruction::InsertElement: Out << "getInsertElement"; break;
890 case Instruction::ShuffleVector: Out << "getShuffleVector"; break;
891 default:
892 assert(!"Invalid constant expression");
893 break;
894 }
895 Out << getCppName(CE->getOperand(0));
896 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
897 Out << ", " << getCppName(CE->getOperand(i));
898 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000899 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000900 } else {
Reid Spencere0d133f2006-05-29 18:08:06 +0000901 assert(!"Bad Constant");
902 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000903 }
904 Out << "\n";
905}
906
Reid Spencer66c87342006-05-30 03:43:49 +0000907void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000908 Out << "\nFunction* " << getCppName(F) << " = new Function(\n"
909 << " /*Type=*/" << getCppName(F->getFunctionType()) << ",\n"
910 << " /*Linkage=*/";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000911 printLinkageType(F->getLinkage());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000912 Out << ",\n /*Name=*/\"";
913 printEscapedString(F->getName());
914 Out << "\", mod); "
915 << (F->isExternal()? "// (external, no body)" : "") << "\n";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000916 printCppName(F);
917 Out << "->setCallingConv(";
918 printCallingConv(F->getCallingConv());
919 Out << ");\n";
920 if (F->hasSection()) {
921 printCppName(F);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000922 Out << "->setSection(\"" << F->getSection() << "\");\n";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000923 }
924 if (F->getAlignment()) {
925 printCppName(F);
926 Out << "->setAlignment(" << F->getAlignment() << ");\n";
927 }
Reid Spencer66c87342006-05-30 03:43:49 +0000928}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000929
Reid Spencer66c87342006-05-30 03:43:49 +0000930void CppWriter::printFunctionBody(const Function *F) {
931 if (F->isExternal())
932 return; // external functions have no bodies.
933
Reid Spencer15f7e012006-05-30 21:18:23 +0000934 // Clear the DefinedValues and ForwardRefs maps because we can't have
935 // cross-function forward refs
936 ForwardRefs.clear();
937 DefinedValues.clear();
938
Reid Spencer66c87342006-05-30 03:43:49 +0000939 // Create all the argument values
940 if (!F->arg_empty()) {
941 Out << " Function::arg_iterator args = " << getCppName(F)
942 << "->arg_begin();\n";
943 }
944 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
945 AI != AE; ++AI) {
946 Out << " Value* " << getCppName(AI) << " = args++;\n";
947 if (AI->hasName())
948 Out << " " << getCppName(AI) << "->setName(\"" << AI->getName()
949 << "\");\n";
950 }
951
952 // Create all the basic blocks
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000953 Out << "\n";
Reid Spencer66c87342006-05-30 03:43:49 +0000954 for (Function::const_iterator BI = F->begin(), BE = F->end();
955 BI != BE; ++BI) {
956 std::string bbname(getCppName(BI));
957 Out << " BasicBlock* " << bbname << " = new BasicBlock(\"";
958 if (BI->hasName())
959 printEscapedString(BI->getName());
960 Out << "\"," << getCppName(BI->getParent()) << ",0);\n";
961 }
962
963 // Output all of its basic blocks... for the function
964 for (Function::const_iterator BI = F->begin(), BE = F->end();
965 BI != BE; ++BI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000966 std::string bbname(getCppName(BI));
967 Out << "\n // Block " << BI->getName() << " (" << bbname << ")\n";
968
Reid Spencer66c87342006-05-30 03:43:49 +0000969 // Output all of the instructions in the basic block...
970 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
971 I != E; ++I) {
Reid Spencer66c87342006-05-30 03:43:49 +0000972 printInstruction(I,bbname);
Reid Spencere0d133f2006-05-29 18:08:06 +0000973 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000974 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000975
976 // Loop over the ForwardRefs and resolve them now that all instructions
977 // are generated.
978 if (!ForwardRefs.empty())
979 Out << "\n // Resolve Forward References\n";
980 while (!ForwardRefs.empty()) {
981 ForwardRefMap::iterator I = ForwardRefs.begin();
982 Out << " " << I->second << "->replaceAllUsesWith("
983 << getCppName(I->first) << "); delete " << I->second << ";\n";
984 ForwardRefs.erase(I);
985 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000986}
987
Reid Spencere0d133f2006-05-29 18:08:06 +0000988// printInstruction - This member is called for each Instruction in a function.
989void
990CppWriter::printInstruction(const Instruction *I, const std::string& bbname)
991{
992 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000993
Reid Spencer15f7e012006-05-30 21:18:23 +0000994 // Before we emit this instruction, we need to take care of generating any
995 // forward references. So, we get the names of all the operands in advance
996 std::string* opNames = new std::string[I->getNumOperands()];
997 for (unsigned i = 0; i < I->getNumOperands(); i++) {
998 opNames[i] = getOpName(I->getOperand(i));
999 }
1000
Reid Spencere0d133f2006-05-29 18:08:06 +00001001 switch (I->getOpcode()) {
1002 case Instruction::Ret: {
1003 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer15f7e012006-05-30 21:18:23 +00001004 Out << " ReturnInst* " << iName << " = new ReturnInst("
1005 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001006 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001007 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001008 case Instruction::Br: {
1009 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer66c87342006-05-30 03:43:49 +00001010 Out << " BranchInst* " << iName << " = new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001011 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001012 Out << opNames[0] << ", "
1013 << opNames[1] << ", "
1014 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001015
Reid Spencere0d133f2006-05-29 18:08:06 +00001016 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001017 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001018 } else {
1019 assert(!"branch with 2 operands?");
1020 }
1021 Out << bbname << ");";
1022 break;
1023 }
Reid Spencer66c87342006-05-30 03:43:49 +00001024 case Instruction::Switch: {
1025 const SwitchInst* sw = cast<SwitchInst>(I);
1026 Out << " SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001027 << opNames[0] << ", "
1028 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001029 << sw->getNumCases() << ", " << bbname << ");\n";
Reid Spencer15f7e012006-05-30 21:18:23 +00001030 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer66c87342006-05-30 03:43:49 +00001031 Out << " " << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001032 << opNames[i] << ", "
1033 << opNames[i+1] << ");\n";
Reid Spencer66c87342006-05-30 03:43:49 +00001034 }
1035 break;
1036 }
1037 case Instruction::Invoke: {
1038 const InvokeInst* inv = cast<InvokeInst>(I);
1039 Out << " std::vector<Value*> " << iName << "_params;\n";
1040 for (unsigned i = 3; i < inv->getNumOperands(); ++i)
1041 Out << " " << iName << "_params.push_back("
Reid Spencer15f7e012006-05-30 21:18:23 +00001042 << opNames[i] << ");\n";
Reid Spencer66c87342006-05-30 03:43:49 +00001043 Out << " InvokeInst* " << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001044 << opNames[0] << ", "
1045 << opNames[1] << ", "
1046 << opNames[2] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001047 << iName << "_params, \"";
1048 printEscapedString(inv->getName());
1049 Out << "\", " << bbname << ");\n";
1050 Out << iName << "->setCallingConv(";
1051 printCallingConv(inv->getCallingConv());
1052 Out << ");";
1053 break;
1054 }
1055 case Instruction::Unwind: {
1056 Out << " UnwindInst* " << iName << " = new UnwindInst("
1057 << bbname << ");";
1058 break;
1059 }
1060 case Instruction::Unreachable:{
1061 Out << " UnreachableInst* " << iName << " = new UnreachableInst("
1062 << bbname << ");";
1063 break;
1064 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001065 case Instruction::Add:
1066 case Instruction::Sub:
1067 case Instruction::Mul:
1068 case Instruction::Div:
1069 case Instruction::Rem:
1070 case Instruction::And:
1071 case Instruction::Or:
1072 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001073 case Instruction::Shl:
1074 case Instruction::Shr:{
1075 Out << " BinaryOperator* " << iName << " = BinaryOperator::create(";
1076 switch (I->getOpcode()) {
1077 case Instruction::Add: Out << "Instruction::Add"; break;
1078 case Instruction::Sub: Out << "Instruction::Sub"; break;
1079 case Instruction::Mul: Out << "Instruction::Mul"; break;
1080 case Instruction::Div: Out << "Instruction::Div"; break;
1081 case Instruction::Rem: Out << "Instruction::Rem"; break;
1082 case Instruction::And: Out << "Instruction::And"; break;
1083 case Instruction::Or: Out << "Instruction::Or"; break;
1084 case Instruction::Xor: Out << "Instruction::Xor"; break;
1085 case Instruction::Shl: Out << "Instruction::Shl"; break;
1086 case Instruction::Shr: Out << "Instruction::Shr"; break;
1087 default: Out << "Instruction::BadOpCode"; break;
1088 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001089 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001090 printEscapedString(I->getName());
1091 Out << "\", " << bbname << ");";
1092 break;
1093 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001094 case Instruction::SetEQ:
1095 case Instruction::SetNE:
1096 case Instruction::SetLE:
1097 case Instruction::SetGE:
1098 case Instruction::SetLT:
Reid Spencer66c87342006-05-30 03:43:49 +00001099 case Instruction::SetGT: {
1100 Out << " SetCondInst* " << iName << " = new SetCondInst(";
1101 switch (I->getOpcode()) {
1102 case Instruction::SetEQ: Out << "Instruction::SetEQ"; break;
1103 case Instruction::SetNE: Out << "Instruction::SetNE"; break;
1104 case Instruction::SetLE: Out << "Instruction::SetLE"; break;
1105 case Instruction::SetGE: Out << "Instruction::SetGE"; break;
1106 case Instruction::SetLT: Out << "Instruction::SetLT"; break;
1107 case Instruction::SetGT: Out << "Instruction::SetGT"; break;
1108 default: Out << "Instruction::BadOpCode"; break;
1109 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001110 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001111 printEscapedString(I->getName());
1112 Out << "\", " << bbname << ");";
1113 break;
1114 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001115 case Instruction::Malloc: {
1116 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer66c87342006-05-30 03:43:49 +00001117 Out << " MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001118 << getCppName(mallocI->getAllocatedType()) << ", ";
1119 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001120 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001121 Out << "\"";
1122 printEscapedString(mallocI->getName());
1123 Out << "\", " << bbname << ");";
1124 if (mallocI->getAlignment())
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001125 Out << "\n " << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001126 << mallocI->getAlignment() << ");";
1127 break;
1128 }
Reid Spencer66c87342006-05-30 03:43:49 +00001129 case Instruction::Free: {
1130 Out << " FreeInst* " << iName << " = new FreeInst("
1131 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1132 break;
1133 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001134 case Instruction::Alloca: {
1135 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer66c87342006-05-30 03:43:49 +00001136 Out << " AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001137 << getCppName(allocaI->getAllocatedType()) << ", ";
1138 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001139 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001140 Out << "\"";
1141 printEscapedString(allocaI->getName());
1142 Out << "\", " << bbname << ");";
1143 if (allocaI->getAlignment())
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001144 Out << "\n " << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001145 << allocaI->getAlignment() << ");";
1146 break;
1147 }
Reid Spencer66c87342006-05-30 03:43:49 +00001148 case Instruction::Load:{
1149 const LoadInst* load = cast<LoadInst>(I);
1150 Out << " LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001151 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001152 printEscapedString(load->getName());
1153 Out << "\", " << (load->isVolatile() ? "true" : "false" )
1154 << ", " << bbname << ");\n";
Reid Spencer66c87342006-05-30 03:43:49 +00001155 break;
1156 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001157 case Instruction::Store: {
1158 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer66c87342006-05-30 03:43:49 +00001159 Out << " StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001160 << opNames[0] << ", "
1161 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001162 << (store->isVolatile() ? "true" : "false")
1163 << ", " << bbname << ");\n";
Reid Spencere0d133f2006-05-29 18:08:06 +00001164 break;
1165 }
1166 case Instruction::GetElementPtr: {
1167 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1168 if (gep->getNumOperands() <= 2) {
Reid Spencer66c87342006-05-30 03:43:49 +00001169 Out << " GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001170 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001171 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001172 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001173 } else {
Reid Spencer66c87342006-05-30 03:43:49 +00001174 Out << " std::vector<Value*> " << iName << "_indices;\n";
Reid Spencere0d133f2006-05-29 18:08:06 +00001175 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer66c87342006-05-30 03:43:49 +00001176 Out << " " << iName << "_indices.push_back("
Reid Spencer15f7e012006-05-30 21:18:23 +00001177 << opNames[i] << ");\n";
Reid Spencere0d133f2006-05-29 18:08:06 +00001178 }
Reid Spencer66c87342006-05-30 03:43:49 +00001179 Out << " Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001180 << opNames[0] << ", " << iName << "_indices";
Reid Spencere0d133f2006-05-29 18:08:06 +00001181 }
1182 Out << ", \"";
1183 printEscapedString(gep->getName());
1184 Out << "\", " << bbname << ");";
1185 break;
1186 }
Reid Spencer66c87342006-05-30 03:43:49 +00001187 case Instruction::PHI: {
1188 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001189
Reid Spencer66c87342006-05-30 03:43:49 +00001190 Out << " PHINode* " << iName << " = new PHINode("
1191 << getCppName(phi->getType()) << ", \"";
1192 printEscapedString(phi->getName());
1193 Out << "\", " << bbname << ");\n";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001194 Out << " " << iName << "->reserveOperandSpace("
1195 << phi->getNumIncomingValues()
Reid Spencer66c87342006-05-30 03:43:49 +00001196 << ");\n";
Reid Spencer15f7e012006-05-30 21:18:23 +00001197 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001198 Out << " " << iName << "->addIncoming("
Reid Spencer15f7e012006-05-30 21:18:23 +00001199 << opNames[i] << ", " << opNames[i+1] << ");\n";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001200 }
Reid Spencer66c87342006-05-30 03:43:49 +00001201 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001202 }
Reid Spencer66c87342006-05-30 03:43:49 +00001203 case Instruction::Cast: {
1204 const CastInst* cst = cast<CastInst>(I);
1205 Out << " CastInst* " << iName << " = new CastInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001206 << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001207 << getCppName(cst->getType()) << ", \"";
1208 printEscapedString(cst->getName());
1209 Out << "\", " << bbname << ");\n";
1210 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001211 }
Reid Spencer66c87342006-05-30 03:43:49 +00001212 case Instruction::Call:{
1213 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001214 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
1215 Out << " InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
1216 << getCppName(ila->getFunctionType()) << ", \""
1217 << ila->getAsmString() << "\", \""
1218 << ila->getConstraintString() << "\","
1219 << (ila->hasSideEffects() ? "true" : "false") << ");\n";
1220 }
Reid Spencer66c87342006-05-30 03:43:49 +00001221 if (call->getNumOperands() > 3) {
1222 Out << " std::vector<Value*> " << iName << "_params;\n";
Reid Spencer15f7e012006-05-30 21:18:23 +00001223 for (unsigned i = 1; i < call->getNumOperands(); ++i)
1224 Out << " " << iName << "_params.push_back(" << opNames[i] << ");\n";
Reid Spencer66c87342006-05-30 03:43:49 +00001225 Out << " CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001226 << opNames[0] << ", " << iName << "_params, \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001227 } else if (call->getNumOperands() == 3) {
1228 Out << " CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001229 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001230 } else if (call->getNumOperands() == 2) {
1231 Out << " CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001232 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001233 } else {
Reid Spencer15f7e012006-05-30 21:18:23 +00001234 Out << " CallInst* " << iName << " = new CallInst(" << opNames[0]
1235 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001236 }
1237 printEscapedString(call->getName());
1238 Out << "\", " << bbname << ");\n";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001239 Out << " " << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001240 printCallingConv(call->getCallingConv());
1241 Out << ");\n";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001242 Out << " " << iName << "->setTailCall("
1243 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001244 Out << ");";
1245 break;
1246 }
1247 case Instruction::Select: {
1248 const SelectInst* sel = cast<SelectInst>(I);
1249 Out << " SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001250 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001251 printEscapedString(sel->getName());
1252 Out << "\", " << bbname << ");\n";
1253 break;
1254 }
1255 case Instruction::UserOp1:
1256 /// FALL THROUGH
1257 case Instruction::UserOp2: {
1258 /// FIXME: What should be done here?
1259 break;
1260 }
1261 case Instruction::VAArg: {
1262 const VAArgInst* va = cast<VAArgInst>(I);
1263 Out << " VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001264 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001265 printEscapedString(va->getName());
1266 Out << "\", " << bbname << ");\n";
1267 break;
1268 }
1269 case Instruction::ExtractElement: {
1270 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
1271 Out << " ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001272 << " = new ExtractElementInst(" << opNames[0]
1273 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001274 printEscapedString(eei->getName());
1275 Out << "\", " << bbname << ");\n";
1276 break;
1277 }
1278 case Instruction::InsertElement: {
1279 const InsertElementInst* iei = cast<InsertElementInst>(I);
1280 Out << " InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001281 << " = new InsertElementInst(" << opNames[0]
1282 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001283 printEscapedString(iei->getName());
1284 Out << "\", " << bbname << ");\n";
1285 break;
1286 }
1287 case Instruction::ShuffleVector: {
1288 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
1289 Out << " ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001290 << " = new ShuffleVectorInst(" << opNames[0]
1291 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001292 printEscapedString(svi->getName());
1293 Out << "\", " << bbname << ");\n";
1294 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001295 }
1296 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001297 Out << "\n";
Reid Spencer15f7e012006-05-30 21:18:23 +00001298 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001299}
1300
1301} // end anonymous llvm
1302
1303namespace llvm {
1304
1305void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001306 std::string fname = FuncName.getValue();
1307 if (fname.empty())
1308 fname = "makeLLVMModule";
1309 if (Fragment) {
1310 o << "Module* " << fname << "() {\n";
1311 CppWriter W(o, mod);
1312 W.printModule(mod);
1313 o << "return mod;\n";
1314 o << "}\n";
1315 } else {
1316 o << "#include <llvm/Module.h>\n";
1317 o << "#include <llvm/DerivedTypes.h>\n";
1318 o << "#include <llvm/Constants.h>\n";
1319 o << "#include <llvm/GlobalVariable.h>\n";
1320 o << "#include <llvm/Function.h>\n";
1321 o << "#include <llvm/CallingConv.h>\n";
1322 o << "#include <llvm/BasicBlock.h>\n";
1323 o << "#include <llvm/Instructions.h>\n";
1324 o << "#include <llvm/InlineAsm.h>\n";
1325 o << "#include <llvm/Pass.h>\n";
1326 o << "#include <llvm/PassManager.h>\n";
1327 o << "#include <llvm/Analysis/Verifier.h>\n";
1328 o << "#include <llvm/Assembly/PrintModulePass.h>\n";
1329 o << "#include <algorithm>\n";
1330 o << "#include <iostream>\n\n";
1331 o << "using namespace llvm;\n\n";
1332 o << "Module* " << fname << "();\n\n";
1333 o << "int main(int argc, char**argv) {\n";
1334 o << " Module* Mod = makeLLVMModule();\n";
1335 o << " verifyModule(*Mod, PrintMessageAction);\n";
1336 o << " std::cerr.flush();\n";
1337 o << " std::cout.flush();\n";
1338 o << " PassManager PM;\n";
1339 o << " PM.add(new PrintModulePass(&std::cout));\n";
1340 o << " PM.run(*Mod);\n";
1341 o << " return 0;\n";
1342 o << "}\n\n";
1343 o << "Module* " << fname << "() {\n";
1344 CppWriter W(o, mod);
1345 W.printModule(mod);
1346 o << "return mod;\n";
1347 o << "}\n";
1348 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001349}
1350
1351}