blob: d2d2d849f5f833807fe7b917fc59c6b0ea01e62c [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"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/STLExtras.h"
Reid Spencer15f7e012006-05-30 21:18:23 +000025#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000026#include "llvm/Support/CFG.h"
27#include "llvm/Support/ManagedStatic.h"
28#include "llvm/Support/MathExtras.h"
Reid Spencerf977e7b2006-06-01 23:43:47 +000029#include "llvm/Config/config.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000030#include <algorithm>
31#include <iostream>
Reid Spencer66c87342006-05-30 03:43:49 +000032#include <set>
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000033
34using namespace llvm;
35
Reid Spencer15f7e012006-05-30 21:18:23 +000036static cl::opt<std::string>
Reid Spencer15f7e012006-05-30 21:18:23 +000037FuncName("funcname", cl::desc("Specify the name of the generated function"),
38 cl::value_desc("function name"));
39
Reid Spencer12803f52006-05-31 17:31:38 +000040enum WhatToGenerate {
41 GenProgram,
42 GenModule,
43 GenContents,
44 GenFunction,
Reid Spencerf977e7b2006-06-01 23:43:47 +000045 GenInline,
Reid Spencer12803f52006-05-31 17:31:38 +000046 GenVariable,
47 GenType
48};
49
50static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
51 cl::desc("Choose what kind of output to generate"),
52 cl::init(GenProgram),
53 cl::values(
54 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
55 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
56 clEnumValN(GenContents,"gen-contents", "Generate contents of a module"),
57 clEnumValN(GenFunction,"gen-function", "Generate a function definition"),
Reid Spencerf977e7b2006-06-01 23:43:47 +000058 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
Reid Spencer12803f52006-05-31 17:31:38 +000059 clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"),
60 clEnumValN(GenType, "gen-type", "Generate a type definition"),
61 clEnumValEnd
62 )
63);
64
65static cl::opt<std::string> NameToGenerate("for", cl::Optional,
66 cl::desc("Specify the name of the thing to generate"),
67 cl::init("!bad!"));
Reid Spencer15f7e012006-05-30 21:18:23 +000068
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000069namespace {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000070typedef std::vector<const Type*> TypeList;
71typedef std::map<const Type*,std::string> TypeMap;
72typedef std::map<const Value*,std::string> ValueMap;
Reid Spencer66c87342006-05-30 03:43:49 +000073typedef std::set<std::string> NameSet;
Reid Spencer15f7e012006-05-30 21:18:23 +000074typedef std::set<const Type*> TypeSet;
75typedef std::set<const Value*> ValueSet;
76typedef std::map<const Value*,std::string> ForwardRefMap;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000077
Reid Spencere0d133f2006-05-29 18:08:06 +000078class CppWriter {
Reid Spencer12803f52006-05-31 17:31:38 +000079 const char* progname;
Reid Spencere0d133f2006-05-29 18:08:06 +000080 std::ostream &Out;
81 const Module *TheModule;
Andrew Lenharth539d8712006-05-31 20:18:56 +000082 uint64_t uniqueNum;
Reid Spencere0d133f2006-05-29 18:08:06 +000083 TypeMap TypeNames;
84 ValueMap ValueNames;
85 TypeMap UnresolvedTypes;
86 TypeList TypeStack;
Reid Spencer66c87342006-05-30 03:43:49 +000087 NameSet UsedNames;
Reid Spencer15f7e012006-05-30 21:18:23 +000088 TypeSet DefinedTypes;
89 ValueSet DefinedValues;
90 ForwardRefMap ForwardRefs;
Reid Spencerf977e7b2006-06-01 23:43:47 +000091 bool is_inline;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000092
Reid Spencere0d133f2006-05-29 18:08:06 +000093public:
Reid Spencer12803f52006-05-31 17:31:38 +000094 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
95 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
Reid Spencerf977e7b2006-06-01 23:43:47 +000096 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000097
Reid Spencere0d133f2006-05-29 18:08:06 +000098 const Module* getModule() { return TheModule; }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000099
Reid Spencer12803f52006-05-31 17:31:38 +0000100 void printProgram(const std::string& fname, const std::string& modName );
101 void printModule(const std::string& fname, const std::string& modName );
102 void printContents(const std::string& fname, const std::string& modName );
103 void printFunction(const std::string& fname, const std::string& funcName );
Reid Spencerf977e7b2006-06-01 23:43:47 +0000104 void printInline(const std::string& fname, const std::string& funcName );
Reid Spencer12803f52006-05-31 17:31:38 +0000105 void printVariable(const std::string& fname, const std::string& varName );
106 void printType(const std::string& fname, const std::string& typeName );
107
108 void error(const std::string& msg);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000109
Reid Spencere0d133f2006-05-29 18:08:06 +0000110private:
Reid Spencer12803f52006-05-31 17:31:38 +0000111 void printLinkageType(GlobalValue::LinkageTypes LT);
112 void printCallingConv(unsigned cc);
113 void printEscapedString(const std::string& str);
114 void printCFP(const ConstantFP* CFP);
115
116 std::string getCppName(const Type* val);
117 inline void printCppName(const Type* val);
118
119 std::string getCppName(const Value* val);
120 inline void printCppName(const Value* val);
121
122 bool printTypeInternal(const Type* Ty);
123 inline void printType(const Type* Ty);
Reid Spencere0d133f2006-05-29 18:08:06 +0000124 void printTypes(const Module* M);
Reid Spencer12803f52006-05-31 17:31:38 +0000125
Reid Spencere0d133f2006-05-29 18:08:06 +0000126 void printConstant(const Constant *CPV);
Reid Spencer12803f52006-05-31 17:31:38 +0000127 void printConstants(const Module* M);
128
129 void printVariableUses(const GlobalVariable *GV);
130 void printVariableHead(const GlobalVariable *GV);
131 void printVariableBody(const GlobalVariable *GV);
132
133 void printFunctionUses(const Function *F);
Reid Spencer66c87342006-05-30 03:43:49 +0000134 void printFunctionHead(const Function *F);
135 void printFunctionBody(const Function *F);
Reid Spencere0d133f2006-05-29 18:08:06 +0000136 void printInstruction(const Instruction *I, const std::string& bbname);
Reid Spencer15f7e012006-05-30 21:18:23 +0000137 std::string getOpName(Value*);
138
Reid Spencer12803f52006-05-31 17:31:38 +0000139 void printModuleBody();
140
Reid Spencere0d133f2006-05-29 18:08:06 +0000141};
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000142
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000143static unsigned indent_level = 0;
144inline std::ostream& nl(std::ostream& Out, int delta = 0) {
145 Out << "\n";
146 if (delta >= 0 || indent_level >= unsigned(-delta))
147 indent_level += delta;
148 for (unsigned i = 0; i < indent_level; ++i)
149 Out << " ";
150 return Out;
151}
152
153inline void in() { indent_level++; }
154inline void out() { if (indent_level >0) indent_level--; }
155
Reid Spencer12803f52006-05-31 17:31:38 +0000156inline void
157sanitize(std::string& str) {
158 for (size_t i = 0; i < str.length(); ++i)
159 if (!isalnum(str[i]) && str[i] != '_')
160 str[i] = '_';
161}
162
163inline const char*
164getTypePrefix(const Type* Ty ) {
165 const char* prefix;
166 switch (Ty->getTypeID()) {
167 case Type::VoidTyID: prefix = "void_"; break;
168 case Type::BoolTyID: prefix = "bool_"; break;
Reid Spencer71d2ec92006-12-31 06:02:26 +0000169 case Type::Int8TyID: prefix = "int8_"; break;
170 case Type::Int16TyID: prefix = "int16_"; break;
171 case Type::Int32TyID: prefix = "int32_"; break;
172 case Type::Int64TyID: prefix = "int64_"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000173 case Type::FloatTyID: prefix = "float_"; break;
174 case Type::DoubleTyID: prefix = "double_"; break;
175 case Type::LabelTyID: prefix = "label_"; break;
176 case Type::FunctionTyID: prefix = "func_"; break;
177 case Type::StructTyID: prefix = "struct_"; break;
178 case Type::ArrayTyID: prefix = "array_"; break;
179 case Type::PointerTyID: prefix = "ptr_"; break;
180 case Type::PackedTyID: prefix = "packed_"; break;
181 case Type::OpaqueTyID: prefix = "opaque_"; break;
182 default: prefix = "other_"; break;
183 }
184 return prefix;
185}
186
187// Looks up the type in the symbol table and returns a pointer to its name or
188// a null pointer if it wasn't found. Note that this isn't the same as the
189// Mode::getTypeName function which will return an empty string, not a null
190// pointer if the name is not found.
191inline const std::string*
192findTypeName(const SymbolTable& ST, const Type* Ty)
193{
194 SymbolTable::type_const_iterator TI = ST.type_begin();
195 SymbolTable::type_const_iterator TE = ST.type_end();
196 for (;TI != TE; ++TI)
197 if (TI->second == Ty)
198 return &(TI->first);
199 return 0;
200}
201
202void
203CppWriter::error(const std::string& msg) {
204 std::cerr << progname << ": " << msg << "\n";
205 exit(2);
206}
207
Reid Spencer15f7e012006-05-30 21:18:23 +0000208// printCFP - Print a floating point constant .. very carefully :)
209// This makes sure that conversion to/from floating yields the same binary
210// result so that we don't lose precision.
211void
212CppWriter::printCFP(const ConstantFP *CFP) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000213 Out << "ConstantFP::get(";
214 if (CFP->getType() == Type::DoubleTy)
215 Out << "Type::DoubleTy, ";
216 else
217 Out << "Type::FloatTy, ";
Reid Spencer15f7e012006-05-30 21:18:23 +0000218#if HAVE_PRINTF_A
219 char Buffer[100];
220 sprintf(Buffer, "%A", CFP->getValue());
221 if ((!strncmp(Buffer, "0x", 2) ||
222 !strncmp(Buffer, "-0x", 3) ||
223 !strncmp(Buffer, "+0x", 3)) &&
224 (atof(Buffer) == CFP->getValue()))
Reid Spencerf977e7b2006-06-01 23:43:47 +0000225 if (CFP->getType() == Type::DoubleTy)
226 Out << "BitsToDouble(" << Buffer << ")";
227 else
228 Out << "BitsToFloat(" << Buffer << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000229 else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000230#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000231 std::string StrVal = ftostr(CFP->getValue());
232
233 while (StrVal[0] == ' ')
234 StrVal.erase(StrVal.begin());
235
236 // Check to make sure that the stringized number is not some string like
237 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
238 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
239 ((StrVal[0] == '-' || StrVal[0] == '+') &&
240 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
241 (atof(StrVal.c_str()) == CFP->getValue()))
242 if (CFP->getType() == Type::DoubleTy)
243 Out << StrVal;
244 else
245 Out << StrVal;
246 else if (CFP->getType() == Type::DoubleTy)
247 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
248 << std::dec << "ULL) /* " << StrVal << " */";
249 else
250 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
251 << std::dec << "U) /* " << StrVal << " */";
Reid Spencer15f7e012006-05-30 21:18:23 +0000252#if HAVE_PRINTF_A
253 }
254#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000255 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000256}
257
Reid Spencer12803f52006-05-31 17:31:38 +0000258void
259CppWriter::printCallingConv(unsigned cc){
260 // Print the calling convention.
261 switch (cc) {
262 case CallingConv::C: Out << "CallingConv::C"; break;
263 case CallingConv::CSRet: Out << "CallingConv::CSRet"; break;
264 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
265 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
266 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
267 default: Out << cc; break;
268 }
269}
Reid Spencer15f7e012006-05-30 21:18:23 +0000270
Reid Spencer12803f52006-05-31 17:31:38 +0000271void
272CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
273 switch (LT) {
274 case GlobalValue::InternalLinkage:
275 Out << "GlobalValue::InternalLinkage"; break;
276 case GlobalValue::LinkOnceLinkage:
277 Out << "GlobalValue::LinkOnceLinkage "; break;
278 case GlobalValue::WeakLinkage:
279 Out << "GlobalValue::WeakLinkage"; break;
280 case GlobalValue::AppendingLinkage:
281 Out << "GlobalValue::AppendingLinkage"; break;
282 case GlobalValue::ExternalLinkage:
283 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000284 case GlobalValue::DLLImportLinkage:
285 Out << "GlobalValue::DllImportLinkage"; break;
286 case GlobalValue::DLLExportLinkage:
287 Out << "GlobalValue::DllExportLinkage"; break;
288 case GlobalValue::ExternalWeakLinkage:
289 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000290 case GlobalValue::GhostLinkage:
291 Out << "GlobalValue::GhostLinkage"; break;
292 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000293}
294
Reid Spencere0d133f2006-05-29 18:08:06 +0000295// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000296// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000297void
298CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000299 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
300 unsigned char C = Str[i];
301 if (isprint(C) && C != '"' && C != '\\') {
302 Out << C;
303 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000304 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000305 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
306 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
307 }
308 }
309}
310
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000311std::string
312CppWriter::getCppName(const Type* Ty)
313{
314 // First, handle the primitive types .. easy
315 if (Ty->isPrimitiveType()) {
316 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000317 case Type::VoidTyID: return "Type::VoidTy";
318 case Type::BoolTyID: return "Type::BoolTy";
319 case Type::Int8TyID: return "Type::Int8Ty";
320 case Type::Int16TyID: return "Type::Int16Ty";
321 case Type::Int32TyID: return "Type::Int32Ty";
322 case Type::Int64TyID: return "Type::Int64Ty";
323 case Type::FloatTyID: return "Type::FloatTy";
324 case Type::DoubleTyID: return "Type::DoubleTy";
325 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000326 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000327 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000328 break;
329 }
330 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
331 }
332
333 // Now, see if we've seen the type before and return that
334 TypeMap::iterator I = TypeNames.find(Ty);
335 if (I != TypeNames.end())
336 return I->second;
337
338 // Okay, let's build a new name for this type. Start with a prefix
339 const char* prefix = 0;
340 switch (Ty->getTypeID()) {
341 case Type::FunctionTyID: prefix = "FuncTy_"; break;
342 case Type::StructTyID: prefix = "StructTy_"; break;
343 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
344 case Type::PointerTyID: prefix = "PointerTy_"; break;
345 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
346 case Type::PackedTyID: prefix = "PackedTy_"; break;
347 default: prefix = "OtherTy_"; break; // prevent breakage
348 }
349
350 // See if the type has a name in the symboltable and build accordingly
351 const std::string* tName = findTypeName(TheModule->getSymbolTable(), Ty);
352 std::string name;
353 if (tName)
354 name = std::string(prefix) + *tName;
355 else
356 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000357 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000358
359 // Save the name
360 return TypeNames[Ty] = name;
361}
362
Reid Spencer12803f52006-05-31 17:31:38 +0000363void
364CppWriter::printCppName(const Type* Ty)
365{
366 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000367}
368
Reid Spencer12803f52006-05-31 17:31:38 +0000369std::string
370CppWriter::getCppName(const Value* val) {
371 std::string name;
372 ValueMap::iterator I = ValueNames.find(val);
373 if (I != ValueNames.end() && I->first == val)
374 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000375
Reid Spencer12803f52006-05-31 17:31:38 +0000376 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
377 name = std::string("gvar_") +
378 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000379 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000380 name = std::string("func_");
381 } else if (const Constant* C = dyn_cast<Constant>(val)) {
382 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000383 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
384 if (is_inline) {
385 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
386 Function::const_arg_iterator(Arg)) + 1;
387 name = std::string("arg_") + utostr(argNum);
388 NameSet::iterator NI = UsedNames.find(name);
389 if (NI != UsedNames.end())
390 name += std::string("_") + utostr(uniqueNum++);
391 UsedNames.insert(name);
392 return ValueNames[val] = name;
393 } else {
394 name = getTypePrefix(val->getType());
395 }
Reid Spencer12803f52006-05-31 17:31:38 +0000396 } else {
397 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000398 }
Reid Spencer12803f52006-05-31 17:31:38 +0000399 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
400 sanitize(name);
401 NameSet::iterator NI = UsedNames.find(name);
402 if (NI != UsedNames.end())
403 name += std::string("_") + utostr(uniqueNum++);
404 UsedNames.insert(name);
405 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000406}
407
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000408void
Reid Spencer12803f52006-05-31 17:31:38 +0000409CppWriter::printCppName(const Value* val) {
410 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000411}
412
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000413bool
Reid Spencer12803f52006-05-31 17:31:38 +0000414CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000415 // We don't print definitions for primitive types
416 if (Ty->isPrimitiveType())
417 return false;
418
Reid Spencer15f7e012006-05-30 21:18:23 +0000419 // If we already defined this type, we don't need to define it again.
420 if (DefinedTypes.find(Ty) != DefinedTypes.end())
421 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000422
Reid Spencer15f7e012006-05-30 21:18:23 +0000423 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000424 std::string typeName(getCppName(Ty));
425
426 // Search the type stack for recursion. If we find it, then generate this
427 // as an OpaqueType, but make sure not to do this multiple times because
428 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000429 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000430 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000431 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
432 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000433 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
434 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000435 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
436 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000437 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000438 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000439 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000440 }
441
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000442 // We're going to print a derived type which, by definition, contains other
443 // types. So, push this one we're printing onto the type stack to assist with
444 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000445 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000446
447 // Print the type definition
448 switch (Ty->getTypeID()) {
449 case Type::FunctionTyID: {
450 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000451 Out << "std::vector<const Type*>" << typeName << "_args;";
452 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000453 FunctionType::param_iterator PI = FT->param_begin();
454 FunctionType::param_iterator PE = FT->param_end();
455 for (; PI != PE; ++PI) {
456 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000457 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000458 std::string argName(getCppName(argTy));
459 Out << typeName << "_args.push_back(" << argName;
460 if (isForward)
461 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000462 Out << ");";
463 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000464 }
Reid Spencer12803f52006-05-31 17:31:38 +0000465 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000466 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000467 Out << "FunctionType* " << typeName << " = FunctionType::get(";
468 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000469 if (isForward)
470 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000471 Out << ",";
472 nl(Out) << "/*Params=*/" << typeName << "_args,";
473 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
474 out();
475 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000476 break;
477 }
478 case Type::StructTyID: {
479 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000480 Out << "std::vector<const Type*>" << typeName << "_fields;";
481 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000482 StructType::element_iterator EI = ST->element_begin();
483 StructType::element_iterator EE = ST->element_end();
484 for (; EI != EE; ++EI) {
485 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000486 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000487 std::string fieldName(getCppName(fieldTy));
488 Out << typeName << "_fields.push_back(" << fieldName;
489 if (isForward)
490 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000491 Out << ");";
492 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000493 }
494 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000495 << typeName << "_fields);";
496 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000497 break;
498 }
499 case Type::ArrayTyID: {
500 const ArrayType* AT = cast<ArrayType>(Ty);
501 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000502 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000503 std::string elemName(getCppName(ET));
504 Out << "ArrayType* " << typeName << " = ArrayType::get("
505 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000506 << ", " << utostr(AT->getNumElements()) << ");";
507 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000508 break;
509 }
510 case Type::PointerTyID: {
511 const PointerType* PT = cast<PointerType>(Ty);
512 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000513 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000514 std::string elemName(getCppName(ET));
515 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000516 << elemName << (isForward ? "_fwd" : "") << ");";
517 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000518 break;
519 }
520 case Type::PackedTyID: {
521 const PackedType* PT = cast<PackedType>(Ty);
522 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000523 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000524 std::string elemName(getCppName(ET));
525 Out << "PackedType* " << typeName << " = PackedType::get("
526 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000527 << ", " << utostr(PT->getNumElements()) << ");";
528 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000529 break;
530 }
531 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000532 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
533 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000534 break;
535 }
536 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000537 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000538 }
539
Reid Spencer74e032a2006-05-29 02:58:15 +0000540 // If the type had a name, make sure we recreate it.
541 const std::string* progTypeName =
542 findTypeName(TheModule->getSymbolTable(),Ty);
543 if (progTypeName)
544 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000545 << typeName << ");";
546 nl(Out);
Reid Spencer74e032a2006-05-29 02:58:15 +0000547
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000548 // Pop us off the type stack
549 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000550
551 // Indicate that this type is now defined.
552 DefinedTypes.insert(Ty);
553
554 // Early resolve as many unresolved types as possible. Search the unresolved
555 // types map for the type we just printed. Now that its definition is complete
556 // we can resolve any previous references to it. This prevents a cascade of
557 // unresolved types.
558 TypeMap::iterator I = UnresolvedTypes.find(Ty);
559 if (I != UnresolvedTypes.end()) {
560 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000561 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
562 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000563 Out << I->second << " = cast<";
564 switch (Ty->getTypeID()) {
565 case Type::FunctionTyID: Out << "FunctionType"; break;
566 case Type::ArrayTyID: Out << "ArrayType"; break;
567 case Type::StructTyID: Out << "StructType"; break;
568 case Type::PackedTyID: Out << "PackedType"; break;
569 case Type::PointerTyID: Out << "PointerType"; break;
570 case Type::OpaqueTyID: Out << "OpaqueType"; break;
571 default: Out << "NoSuchDerivedType"; break;
572 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000573 Out << ">(" << I->second << "_fwd.get());";
574 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000575 UnresolvedTypes.erase(I);
576 }
577
578 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000579 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000580
581 // We weren't a recursive type
582 return false;
583}
584
Reid Spencer12803f52006-05-31 17:31:38 +0000585// Prints a type definition. Returns true if it could not resolve all the types
586// in the definition but had to use a forward reference.
587void
588CppWriter::printType(const Type* Ty) {
589 assert(TypeStack.empty());
590 TypeStack.clear();
591 printTypeInternal(Ty);
592 assert(TypeStack.empty());
593}
594
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000595void
596CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000597
598 // Walk the symbol table and print out all its types
599 const SymbolTable& symtab = M->getSymbolTable();
600 for (SymbolTable::type_const_iterator TI = symtab.type_begin(),
601 TE = symtab.type_end(); TI != TE; ++TI) {
602
603 // For primitive types and types already defined, just add a name
604 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
605 if (TI->second->isPrimitiveType() || TNI != TypeNames.end()) {
606 Out << "mod->addTypeName(\"";
607 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000608 Out << "\", " << getCppName(TI->second) << ");";
609 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000610 // For everything else, define the type
611 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000612 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000613 }
614 }
615
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000616 // Add all of the global variables to the value table...
617 for (Module::const_global_iterator I = TheModule->global_begin(),
618 E = TheModule->global_end(); I != E; ++I) {
619 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000620 printType(I->getInitializer()->getType());
621 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000622 }
623
624 // Add all the functions to the table
625 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
626 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000627 printType(FI->getReturnType());
628 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000629 // Add all the function arguments
630 for(Function::const_arg_iterator AI = FI->arg_begin(),
631 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000632 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000633 }
634
635 // Add all of the basic blocks and instructions
636 for (Function::const_iterator BB = FI->begin(),
637 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000638 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000639 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
640 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000641 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000642 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000643 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000644 }
645 }
646 }
647}
648
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000649
Reid Spencere0d133f2006-05-29 18:08:06 +0000650// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000651void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000652 // First, if the constant is actually a GlobalValue (variable or function) or
653 // its already in the constant list then we've printed it already and we can
654 // just return.
655 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000656 return;
657
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000658 std::string constName(getCppName(CV));
659 std::string typeName(getCppName(CV->getType()));
660 if (CV->isNullValue()) {
661 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000662 << typeName << ");";
663 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000664 return;
665 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000666 if (isa<GlobalValue>(CV)) {
667 // Skip variables and functions, we emit them elsewhere
668 return;
669 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000670 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000671 Out << "ConstantBool* " << constName << " = ConstantBool::get("
Chris Lattnere354df92006-09-28 23:24:48 +0000672 << (CB->getValue() ? "true" : "false") << ");";
Reid Spencerb83eb642006-10-20 07:07:24 +0000673 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
674 Out << "ConstantInt* " << constName << " = ConstantInt::get("
Reid Spencerf6c511c2006-12-18 07:58:01 +0000675 << typeName << ", " << CI->getZExtValue() << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000676 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000677 Out << "ConstantAggregateZero* " << constName
678 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000679 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000680 Out << "ConstantPointerNull* " << constName
681 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000682 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000683 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000684 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000685 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000686 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000687 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000688 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000689 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000690 // Determine if we want null termination or not.
691 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000692 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000693 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000694 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000695 Out << ");";
696 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000697 Out << "std::vector<Constant*> " << constName << "_elems;";
698 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000699 unsigned N = CA->getNumOperands();
700 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000701 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000702 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000703 << getCppName(CA->getOperand(i)) << ");";
704 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000705 }
706 Out << "Constant* " << constName << " = ConstantArray::get("
707 << typeName << ", " << constName << "_elems);";
708 }
709 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000710 Out << "std::vector<Constant*> " << constName << "_fields;";
711 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000712 unsigned N = CS->getNumOperands();
713 for (unsigned i = 0; i < N; i++) {
714 printConstant(CS->getOperand(i));
715 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000716 << getCppName(CS->getOperand(i)) << ");";
717 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000718 }
719 Out << "Constant* " << constName << " = ConstantStruct::get("
720 << typeName << ", " << constName << "_fields);";
721 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000722 Out << "std::vector<Constant*> " << constName << "_elems;";
723 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000724 unsigned N = CP->getNumOperands();
725 for (unsigned i = 0; i < N; ++i) {
726 printConstant(CP->getOperand(i));
727 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000728 << getCppName(CP->getOperand(i)) << ");";
729 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000730 }
731 Out << "Constant* " << constName << " = ConstantPacked::get("
732 << typeName << ", " << constName << "_elems);";
733 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000734 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000735 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000736 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000737 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000738 Out << "std::vector<Constant*> " << constName << "_indices;";
739 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000740 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000741 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000742 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000743 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000744 << getCppName(CE->getOperand(i)) << ");";
745 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000746 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000747 Out << "Constant* " << constName
748 << " = ConstantExpr::getGetElementPtr("
749 << getCppName(CE->getOperand(0)) << ", "
750 << constName << "_indices);";
Reid Spencer3da59db2006-11-27 01:05:10 +0000751 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000752 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000753 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000754 switch (CE->getOpcode()) {
755 default: assert(0 && "Invalid cast opcode");
756 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
757 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
758 case Instruction::SExt: Out << "Instruction::SExt"; break;
759 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
760 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
761 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
762 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
763 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
764 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
765 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
766 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
767 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
768 }
769 Out << ", " << getCppName(CE->getOperand(0)) << ", "
770 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000771 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000772 unsigned N = CE->getNumOperands();
773 for (unsigned i = 0; i < N; ++i ) {
774 printConstant(CE->getOperand(i));
775 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000776 Out << "Constant* " << constName << " = ConstantExpr::";
777 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000778 case Instruction::Add: Out << "getAdd("; break;
779 case Instruction::Sub: Out << "getSub("; break;
780 case Instruction::Mul: Out << "getMul("; break;
781 case Instruction::UDiv: Out << "getUDiv("; break;
782 case Instruction::SDiv: Out << "getSDiv("; break;
783 case Instruction::FDiv: Out << "getFDiv("; break;
784 case Instruction::URem: Out << "getURem("; break;
785 case Instruction::SRem: Out << "getSRem("; break;
786 case Instruction::FRem: Out << "getFRem("; break;
787 case Instruction::And: Out << "getAnd("; break;
788 case Instruction::Or: Out << "getOr("; break;
789 case Instruction::Xor: Out << "getXor("; break;
790 case Instruction::ICmp:
791 Out << "getICmp(ICmpInst::ICMP_";
792 switch (CE->getPredicate()) {
793 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
794 case ICmpInst::ICMP_NE: Out << "NE"; break;
795 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
796 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
797 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
798 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
799 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
800 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
801 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
802 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
803 default: error("Invalid ICmp Predicate");
804 }
805 break;
806 case Instruction::FCmp:
807 Out << "getFCmp(FCmpInst::FCMP_";
808 switch (CE->getPredicate()) {
809 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
810 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
811 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
812 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
813 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
814 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
815 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
816 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
817 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
818 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
819 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
820 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
821 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
822 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
823 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
824 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
825 default: error("Invalid FCmp Predicate");
826 }
827 break;
828 case Instruction::Shl: Out << "getShl("; break;
829 case Instruction::LShr: Out << "getLShr("; break;
830 case Instruction::AShr: Out << "getAShr("; break;
831 case Instruction::Select: Out << "getSelect("; break;
832 case Instruction::ExtractElement: Out << "getExtractElement("; break;
833 case Instruction::InsertElement: Out << "getInsertElement("; break;
834 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000835 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000836 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000837 break;
838 }
839 Out << getCppName(CE->getOperand(0));
840 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
841 Out << ", " << getCppName(CE->getOperand(i));
842 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000843 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000844 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000845 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000846 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000847 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000848 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000849}
850
Reid Spencer12803f52006-05-31 17:31:38 +0000851void
852CppWriter::printConstants(const Module* M) {
853 // Traverse all the global variables looking for constant initializers
854 for (Module::const_global_iterator I = TheModule->global_begin(),
855 E = TheModule->global_end(); I != E; ++I)
856 if (I->hasInitializer())
857 printConstant(I->getInitializer());
858
859 // Traverse the LLVM functions looking for constants
860 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
861 FI != FE; ++FI) {
862 // Add all of the basic blocks and instructions
863 for (Function::const_iterator BB = FI->begin(),
864 E = FI->end(); BB != E; ++BB) {
865 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
866 ++I) {
867 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
868 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
869 printConstant(C);
870 }
871 }
872 }
873 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000874 }
Reid Spencer66c87342006-05-30 03:43:49 +0000875}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000876
Reid Spencer12803f52006-05-31 17:31:38 +0000877void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000878 nl(Out) << "// Type Definitions";
879 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000880 printType(GV->getType());
881 if (GV->hasInitializer()) {
882 Constant* Init = GV->getInitializer();
883 printType(Init->getType());
884 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000885 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000886 printFunctionHead(F);
887 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000888 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000889 printVariableHead(gv);
890 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000891 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000892 printConstant(gv);
893 }
894 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000895 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000896 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000897 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000898 }
Reid Spencer12803f52006-05-31 17:31:38 +0000899}
Reid Spencer15f7e012006-05-30 21:18:23 +0000900
Reid Spencer12803f52006-05-31 17:31:38 +0000901void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000902 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000903 if (is_inline) {
904 Out << " = mod->getGlobalVariable(";
905 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000906 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
907 nl(Out) << "if (!" << getCppName(GV) << ") {";
908 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000909 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000910 Out << " = new GlobalVariable(";
911 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000912 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000913 Out << ",";
914 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
915 Out << ",";
916 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000917 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000918 Out << ",";
919 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000920 if (GV->hasInitializer()) {
921 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000922 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000923 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000924 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000925 Out << "\",";
926 nl(Out) << "mod);";
927 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000928
929 if (GV->hasSection()) {
930 printCppName(GV);
931 Out << "->setSection(\"";
932 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000933 Out << "\");";
934 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000935 }
936 if (GV->getAlignment()) {
937 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000938 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
939 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000940 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000941 if (is_inline) {
942 out(); Out << "}"; nl(Out);
943 }
Reid Spencer12803f52006-05-31 17:31:38 +0000944}
945
946void
947CppWriter::printVariableBody(const GlobalVariable *GV) {
948 if (GV->hasInitializer()) {
949 printCppName(GV);
950 Out << "->setInitializer(";
951 //if (!isa<GlobalValue(GV->getInitializer()))
952 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000953 Out << getCppName(GV->getInitializer()) << ");";
954 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000955 }
956}
957
958std::string
959CppWriter::getOpName(Value* V) {
960 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
961 return getCppName(V);
962
963 // See if its alread in the map of forward references, if so just return the
964 // name we already set up for it
965 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
966 if (I != ForwardRefs.end())
967 return I->second;
968
969 // This is a new forward reference. Generate a unique name for it
970 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
971
972 // Yes, this is a hack. An Argument is the smallest instantiable value that
973 // we can make as a placeholder for the real value. We'll replace these
974 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000975 Out << "Argument* " << result << " = new Argument("
976 << getCppName(V->getType()) << ");";
977 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000978 ForwardRefs[V] = result;
979 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000980}
981
Reid Spencere0d133f2006-05-29 18:08:06 +0000982// printInstruction - This member is called for each Instruction in a function.
983void
Reid Spencer12803f52006-05-31 17:31:38 +0000984CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000985 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000986
Reid Spencer15f7e012006-05-30 21:18:23 +0000987 // Before we emit this instruction, we need to take care of generating any
988 // forward references. So, we get the names of all the operands in advance
989 std::string* opNames = new std::string[I->getNumOperands()];
990 for (unsigned i = 0; i < I->getNumOperands(); i++) {
991 opNames[i] = getOpName(I->getOperand(i));
992 }
993
Reid Spencere0d133f2006-05-29 18:08:06 +0000994 switch (I->getOpcode()) {
995 case Instruction::Ret: {
996 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000997 Out << "ReturnInst* " << iName << " = new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +0000998 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000999 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001000 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001001 case Instruction::Br: {
1002 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001003 Out << "BranchInst* " << iName << " = new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001004 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001005 Out << opNames[0] << ", "
1006 << opNames[1] << ", "
1007 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001008
Reid Spencere0d133f2006-05-29 18:08:06 +00001009 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001010 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001011 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001012 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001013 }
1014 Out << bbname << ");";
1015 break;
1016 }
Reid Spencer66c87342006-05-30 03:43:49 +00001017 case Instruction::Switch: {
1018 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001019 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001020 << opNames[0] << ", "
1021 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001022 << sw->getNumCases() << ", " << bbname << ");";
1023 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001024 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001025 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001026 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001027 << opNames[i+1] << ");";
1028 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001029 }
1030 break;
1031 }
1032 case Instruction::Invoke: {
1033 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001034 Out << "std::vector<Value*> " << iName << "_params;";
1035 nl(Out);
1036 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1037 Out << iName << "_params.push_back("
1038 << opNames[i] << ");";
1039 nl(Out);
1040 }
1041 Out << "InvokeInst* " << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001042 << opNames[0] << ", "
1043 << opNames[1] << ", "
1044 << opNames[2] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001045 << iName << "_params, \"";
1046 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001047 Out << "\", " << bbname << ");";
1048 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001049 printCallingConv(inv->getCallingConv());
1050 Out << ");";
1051 break;
1052 }
1053 case Instruction::Unwind: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001054 Out << "UnwindInst* " << iName << " = new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001055 << bbname << ");";
1056 break;
1057 }
1058 case Instruction::Unreachable:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001059 Out << "UnreachableInst* " << iName << " = new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001060 << bbname << ");";
1061 break;
1062 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001063 case Instruction::Add:
1064 case Instruction::Sub:
1065 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001066 case Instruction::UDiv:
1067 case Instruction::SDiv:
1068 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001069 case Instruction::URem:
1070 case Instruction::SRem:
1071 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001072 case Instruction::And:
1073 case Instruction::Or:
1074 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001075 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001076 case Instruction::LShr:
1077 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001078 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001079 switch (I->getOpcode()) {
1080 case Instruction::Add: Out << "Instruction::Add"; break;
1081 case Instruction::Sub: Out << "Instruction::Sub"; break;
1082 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001083 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1084 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1085 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001086 case Instruction::URem:Out << "Instruction::URem"; break;
1087 case Instruction::SRem:Out << "Instruction::SRem"; break;
1088 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001089 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001090 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001091 case Instruction::Xor: Out << "Instruction::Xor"; break;
1092 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001093 case Instruction::LShr:Out << "Instruction::LShr"; break;
1094 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001095 default: Out << "Instruction::BadOpCode"; break;
1096 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001097 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001098 printEscapedString(I->getName());
1099 Out << "\", " << bbname << ");";
1100 break;
1101 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001102 case Instruction::FCmp: {
1103 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1104 switch (cast<FCmpInst>(I)->getPredicate()) {
1105 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1106 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1107 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1108 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1109 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1110 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1111 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1112 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1113 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1114 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1115 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1116 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1117 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1118 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1119 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1120 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1121 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1122 }
1123 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1124 printEscapedString(I->getName());
1125 Out << "\", " << bbname << ");";
1126 break;
1127 }
1128 case Instruction::ICmp: {
1129 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1130 switch (cast<ICmpInst>(I)->getPredicate()) {
1131 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1132 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1133 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1134 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1135 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1136 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1137 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1138 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1139 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1140 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1141 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001142 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001143 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001144 printEscapedString(I->getName());
1145 Out << "\", " << bbname << ");";
1146 break;
1147 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001148 case Instruction::Malloc: {
1149 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001150 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001151 << getCppName(mallocI->getAllocatedType()) << ", ";
1152 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001153 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001154 Out << "\"";
1155 printEscapedString(mallocI->getName());
1156 Out << "\", " << bbname << ");";
1157 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001158 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001159 << mallocI->getAlignment() << ");";
1160 break;
1161 }
Reid Spencer66c87342006-05-30 03:43:49 +00001162 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001163 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001164 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1165 break;
1166 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001167 case Instruction::Alloca: {
1168 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001169 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001170 << getCppName(allocaI->getAllocatedType()) << ", ";
1171 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001172 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001173 Out << "\"";
1174 printEscapedString(allocaI->getName());
1175 Out << "\", " << bbname << ");";
1176 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001177 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001178 << allocaI->getAlignment() << ");";
1179 break;
1180 }
Reid Spencer66c87342006-05-30 03:43:49 +00001181 case Instruction::Load:{
1182 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001183 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001184 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001185 printEscapedString(load->getName());
1186 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001187 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001188 break;
1189 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001190 case Instruction::Store: {
1191 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001192 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001193 << opNames[0] << ", "
1194 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001195 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001196 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001197 break;
1198 }
1199 case Instruction::GetElementPtr: {
1200 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1201 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001202 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001203 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001204 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001205 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001206 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001207 Out << "std::vector<Value*> " << iName << "_indices;";
1208 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001209 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001210 Out << iName << "_indices.push_back("
1211 << opNames[i] << ");";
1212 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001213 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001214 Out << "Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001215 << opNames[0] << ", " << iName << "_indices";
Reid Spencere0d133f2006-05-29 18:08:06 +00001216 }
1217 Out << ", \"";
1218 printEscapedString(gep->getName());
1219 Out << "\", " << bbname << ");";
1220 break;
1221 }
Reid Spencer66c87342006-05-30 03:43:49 +00001222 case Instruction::PHI: {
1223 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001224
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001225 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001226 << getCppName(phi->getType()) << ", \"";
1227 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001228 Out << "\", " << bbname << ");";
1229 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001230 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001231 << ");";
1232 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001233 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001234 Out << iName << "->addIncoming("
1235 << opNames[i] << ", " << opNames[i+1] << ");";
1236 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001237 }
Reid Spencer66c87342006-05-30 03:43:49 +00001238 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001239 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001240 case Instruction::Trunc:
1241 case Instruction::ZExt:
1242 case Instruction::SExt:
1243 case Instruction::FPTrunc:
1244 case Instruction::FPExt:
1245 case Instruction::FPToUI:
1246 case Instruction::FPToSI:
1247 case Instruction::UIToFP:
1248 case Instruction::SIToFP:
1249 case Instruction::PtrToInt:
1250 case Instruction::IntToPtr:
1251 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001252 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001253 Out << "CastInst* " << iName << " = new ";
1254 switch (I->getOpcode()) {
1255 case Instruction::Trunc: Out << "TruncInst";
1256 case Instruction::ZExt: Out << "ZExtInst";
1257 case Instruction::SExt: Out << "SExtInst";
1258 case Instruction::FPTrunc: Out << "FPTruncInst";
1259 case Instruction::FPExt: Out << "FPExtInst";
1260 case Instruction::FPToUI: Out << "FPToUIInst";
1261 case Instruction::FPToSI: Out << "FPToSIInst";
1262 case Instruction::UIToFP: Out << "UIToFPInst";
1263 case Instruction::SIToFP: Out << "SIToFPInst";
1264 case Instruction::PtrToInt: Out << "PtrToInst";
1265 case Instruction::IntToPtr: Out << "IntToPtrInst";
1266 case Instruction::BitCast: Out << "BitCastInst";
1267 default: assert(!"Unreachable"); break;
1268 }
1269 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001270 << getCppName(cst->getType()) << ", \"";
1271 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001272 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001273 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001274 }
Reid Spencer66c87342006-05-30 03:43:49 +00001275 case Instruction::Call:{
1276 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001277 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001278 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001279 << getCppName(ila->getFunctionType()) << ", \""
1280 << ila->getAsmString() << "\", \""
1281 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001282 << (ila->hasSideEffects() ? "true" : "false") << ");";
1283 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001284 }
Reid Spencer66c87342006-05-30 03:43:49 +00001285 if (call->getNumOperands() > 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001286 Out << "std::vector<Value*> " << iName << "_params;";
1287 nl(Out);
1288 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1289 Out << iName << "_params.push_back(" << opNames[i] << ");";
1290 nl(Out);
1291 }
1292 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001293 << opNames[0] << ", " << iName << "_params, \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001294 } else if (call->getNumOperands() == 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001295 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001296 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001297 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001298 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001299 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001300 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001301 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001302 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001303 }
1304 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001305 Out << "\", " << bbname << ");";
1306 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001307 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001308 Out << ");";
1309 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001310 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001311 Out << ");";
1312 break;
1313 }
1314 case Instruction::Select: {
1315 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001316 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001317 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001318 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001319 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001320 break;
1321 }
1322 case Instruction::UserOp1:
1323 /// FALL THROUGH
1324 case Instruction::UserOp2: {
1325 /// FIXME: What should be done here?
1326 break;
1327 }
1328 case Instruction::VAArg: {
1329 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001330 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001331 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001332 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001333 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001334 break;
1335 }
1336 case Instruction::ExtractElement: {
1337 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001338 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001339 << " = new ExtractElementInst(" << opNames[0]
1340 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001341 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001342 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001343 break;
1344 }
1345 case Instruction::InsertElement: {
1346 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001347 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001348 << " = new InsertElementInst(" << opNames[0]
1349 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001350 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001351 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001352 break;
1353 }
1354 case Instruction::ShuffleVector: {
1355 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001356 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001357 << " = new ShuffleVectorInst(" << opNames[0]
1358 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001359 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001360 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001361 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001362 }
1363 }
Reid Spencer68464b72006-06-15 16:09:59 +00001364 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001365 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001366 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001367}
1368
Reid Spencer12803f52006-05-31 17:31:38 +00001369// Print out the types, constants and declarations needed by one function
1370void CppWriter::printFunctionUses(const Function* F) {
1371
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001372 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001373 if (!is_inline) {
1374 // Print the function's return type
1375 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001376
Reid Spencerf977e7b2006-06-01 23:43:47 +00001377 // Print the function's function type
1378 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001379
Reid Spencerf977e7b2006-06-01 23:43:47 +00001380 // Print the types of each of the function's arguments
1381 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1382 AI != AE; ++AI) {
1383 printType(AI->getType());
1384 }
Reid Spencer12803f52006-05-31 17:31:38 +00001385 }
1386
1387 // Print type definitions for every type referenced by an instruction and
1388 // make a note of any global values or constants that are referenced
1389 std::vector<GlobalValue*> gvs;
1390 std::vector<Constant*> consts;
1391 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1392 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1393 I != E; ++I) {
1394 // Print the type of the instruction itself
1395 printType(I->getType());
1396
1397 // Print the type of each of the instruction's operands
1398 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1399 Value* operand = I->getOperand(i);
1400 printType(operand->getType());
1401 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1402 gvs.push_back(GV);
1403 else if (Constant* C = dyn_cast<Constant>(operand))
1404 consts.push_back(C);
1405 }
1406 }
1407 }
1408
1409 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001410 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001411 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1412 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001413 if (Function* Fun = dyn_cast<Function>(*I)) {
1414 if (!is_inline || Fun != F)
1415 printFunctionHead(Fun);
1416 }
Reid Spencer12803f52006-05-31 17:31:38 +00001417 }
1418
1419 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001420 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001421 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1422 I != E; ++I) {
1423 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1424 printVariableHead(F);
1425 }
1426
1427 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001428 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001429 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1430 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001431 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001432 }
1433
1434 // Process the global variables definitions now that all the constants have
1435 // been emitted. These definitions just couple the gvars with their constant
1436 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001437 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001438 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1439 I != E; ++I) {
1440 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1441 printVariableBody(GV);
1442 }
1443}
1444
1445void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001446 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001447 if (is_inline) {
1448 Out << " = mod->getFunction(\"";
1449 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001450 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1451 nl(Out) << "if (!" << getCppName(F) << ") {";
1452 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001453 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001454 Out<< " = new Function(";
1455 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1456 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001457 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001458 Out << ",";
1459 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001460 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001461 Out << "\", mod); " << (F->isExternal()? "// (external, no body)" : "");
1462 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001463 printCppName(F);
1464 Out << "->setCallingConv(";
1465 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001466 Out << ");";
1467 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001468 if (F->hasSection()) {
1469 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001470 Out << "->setSection(\"" << F->getSection() << "\");";
1471 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001472 }
1473 if (F->getAlignment()) {
1474 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001475 Out << "->setAlignment(" << F->getAlignment() << ");";
1476 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001477 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001478 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001479 Out << "}";
1480 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001481 }
Reid Spencer12803f52006-05-31 17:31:38 +00001482}
1483
1484void CppWriter::printFunctionBody(const Function *F) {
1485 if (F->isExternal())
1486 return; // external functions have no bodies.
1487
1488 // Clear the DefinedValues and ForwardRefs maps because we can't have
1489 // cross-function forward refs
1490 ForwardRefs.clear();
1491 DefinedValues.clear();
1492
1493 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001494 if (!is_inline) {
1495 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001496 Out << "Function::arg_iterator args = " << getCppName(F)
1497 << "->arg_begin();";
1498 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001499 }
1500 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1501 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001502 Out << "Value* " << getCppName(AI) << " = args++;";
1503 nl(Out);
1504 if (AI->hasName()) {
1505 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1506 nl(Out);
1507 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001508 }
Reid Spencer12803f52006-05-31 17:31:38 +00001509 }
1510
1511 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001512 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001513 for (Function::const_iterator BI = F->begin(), BE = F->end();
1514 BI != BE; ++BI) {
1515 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001516 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001517 if (BI->hasName())
1518 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001519 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1520 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001521 }
1522
1523 // Output all of its basic blocks... for the function
1524 for (Function::const_iterator BI = F->begin(), BE = F->end();
1525 BI != BE; ++BI) {
1526 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001527 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1528 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001529
1530 // Output all of the instructions in the basic block...
1531 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1532 I != E; ++I) {
1533 printInstruction(I,bbname);
1534 }
1535 }
1536
1537 // Loop over the ForwardRefs and resolve them now that all instructions
1538 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001539 if (!ForwardRefs.empty()) {
1540 nl(Out) << "// Resolve Forward References";
1541 nl(Out);
1542 }
1543
Reid Spencer12803f52006-05-31 17:31:38 +00001544 while (!ForwardRefs.empty()) {
1545 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001546 Out << I->second << "->replaceAllUsesWith("
1547 << getCppName(I->first) << "); delete " << I->second << ";";
1548 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001549 ForwardRefs.erase(I);
1550 }
1551}
1552
Reid Spencerf977e7b2006-06-01 23:43:47 +00001553void CppWriter::printInline(const std::string& fname, const std::string& func) {
1554 const Function* F = TheModule->getNamedFunction(func);
1555 if (!F) {
1556 error(std::string("Function '") + func + "' not found in input module");
1557 return;
1558 }
1559 if (F->isExternal()) {
1560 error(std::string("Function '") + func + "' is external!");
1561 return;
1562 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001563 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001564 << getCppName(F);
1565 unsigned arg_count = 1;
1566 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1567 AI != AE; ++AI) {
1568 Out << ", Value* arg_" << arg_count;
1569 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001570 Out << ") {";
1571 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001572 is_inline = true;
1573 printFunctionUses(F);
1574 printFunctionBody(F);
1575 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001576 Out << "return " << getCppName(F->begin()) << ";";
1577 nl(Out) << "}";
1578 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001579}
1580
Reid Spencer12803f52006-05-31 17:31:38 +00001581void CppWriter::printModuleBody() {
1582 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001583 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001584 printTypes(TheModule);
1585
1586 // Functions can call each other and global variables can reference them so
1587 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001588 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001589 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1590 I != E; ++I)
1591 printFunctionHead(I);
1592
1593 // Process the global variables declarations. We can't initialze them until
1594 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001595 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001596 for (Module::const_global_iterator I = TheModule->global_begin(),
1597 E = TheModule->global_end(); I != E; ++I) {
1598 printVariableHead(I);
1599 }
1600
1601 // Print out all the constants definitions. Constants don't recurse except
1602 // through GlobalValues. All GlobalValues have been declared at this point
1603 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001604 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001605 printConstants(TheModule);
1606
1607 // Process the global variables definitions now that all the constants have
1608 // been emitted. These definitions just couple the gvars with their constant
1609 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001610 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001611 for (Module::const_global_iterator I = TheModule->global_begin(),
1612 E = TheModule->global_end(); I != E; ++I) {
1613 printVariableBody(I);
1614 }
1615
1616 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001617 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001618 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1619 I != E; ++I) {
1620 if (!I->isExternal()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001621 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1622 << ")";
1623 nl(Out) << "{";
1624 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001625 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001626 nl(Out,-1) << "}";
1627 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001628 }
1629 }
1630}
1631
1632void CppWriter::printProgram(
1633 const std::string& fname,
1634 const std::string& mName
1635) {
1636 Out << "#include <llvm/Module.h>\n";
1637 Out << "#include <llvm/DerivedTypes.h>\n";
1638 Out << "#include <llvm/Constants.h>\n";
1639 Out << "#include <llvm/GlobalVariable.h>\n";
1640 Out << "#include <llvm/Function.h>\n";
1641 Out << "#include <llvm/CallingConv.h>\n";
1642 Out << "#include <llvm/BasicBlock.h>\n";
1643 Out << "#include <llvm/Instructions.h>\n";
1644 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001645 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001646 Out << "#include <llvm/Pass.h>\n";
1647 Out << "#include <llvm/PassManager.h>\n";
1648 Out << "#include <llvm/Analysis/Verifier.h>\n";
1649 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1650 Out << "#include <algorithm>\n";
1651 Out << "#include <iostream>\n\n";
1652 Out << "using namespace llvm;\n\n";
1653 Out << "Module* " << fname << "();\n\n";
1654 Out << "int main(int argc, char**argv) {\n";
1655 Out << " Module* Mod = makeLLVMModule();\n";
1656 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1657 Out << " std::cerr.flush();\n";
1658 Out << " std::cout.flush();\n";
1659 Out << " PassManager PM;\n";
1660 Out << " PM.add(new PrintModulePass(&std::cout));\n";
1661 Out << " PM.run(*Mod);\n";
1662 Out << " return 0;\n";
1663 Out << "}\n\n";
1664 printModule(fname,mName);
1665}
1666
1667void CppWriter::printModule(
1668 const std::string& fname,
1669 const std::string& mName
1670) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001671 nl(Out) << "Module* " << fname << "() {";
1672 nl(Out,1) << "// Module Construction";
1673 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1674 nl(Out) << "mod->setEndianness(";
Reid Spencer12803f52006-05-31 17:31:38 +00001675 switch (TheModule->getEndianness()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001676 case Module::LittleEndian: Out << "Module::LittleEndian);"; break;
1677 case Module::BigEndian: Out << "Module::BigEndian);"; break;
1678 case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001679 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001680 nl(Out) << "mod->setPointerSize(";
Reid Spencer12803f52006-05-31 17:31:38 +00001681 switch (TheModule->getPointerSize()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001682 case Module::Pointer32: Out << "Module::Pointer32);"; break;
1683 case Module::Pointer64: Out << "Module::Pointer64);"; break;
1684 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001685 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001686 nl(Out);
1687 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer12803f52006-05-31 17:31:38 +00001688 Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001689 << "\");";
1690 nl(Out);
1691 }
Reid Spencer12803f52006-05-31 17:31:38 +00001692
1693 if (!TheModule->getModuleInlineAsm().empty()) {
1694 Out << "mod->setModuleInlineAsm(\"";
1695 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001696 Out << "\");";
1697 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001698 }
1699
1700 // Loop over the dependent libraries and emit them.
1701 Module::lib_iterator LI = TheModule->lib_begin();
1702 Module::lib_iterator LE = TheModule->lib_end();
1703 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001704 Out << "mod->addLibrary(\"" << *LI << "\");";
1705 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001706 ++LI;
1707 }
1708 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001709 nl(Out) << "return mod;";
1710 nl(Out,-1) << "}";
1711 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001712}
1713
1714void CppWriter::printContents(
1715 const std::string& fname, // Name of generated function
1716 const std::string& mName // Name of module generated module
1717) {
1718 Out << "\nModule* " << fname << "(Module *mod) {\n";
1719 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001720 printModuleBody();
1721 Out << "\nreturn mod;\n";
1722 Out << "\n}\n";
1723}
1724
1725void CppWriter::printFunction(
1726 const std::string& fname, // Name of generated function
1727 const std::string& funcName // Name of function to generate
1728) {
1729 const Function* F = TheModule->getNamedFunction(funcName);
1730 if (!F) {
1731 error(std::string("Function '") + funcName + "' not found in input module");
1732 return;
1733 }
1734 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1735 printFunctionUses(F);
1736 printFunctionHead(F);
1737 printFunctionBody(F);
1738 Out << "return " << getCppName(F) << ";\n";
1739 Out << "}\n";
1740}
1741
1742void CppWriter::printVariable(
1743 const std::string& fname, /// Name of generated function
1744 const std::string& varName // Name of variable to generate
1745) {
1746 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1747
1748 if (!GV) {
1749 error(std::string("Variable '") + varName + "' not found in input module");
1750 return;
1751 }
1752 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1753 printVariableUses(GV);
1754 printVariableHead(GV);
1755 printVariableBody(GV);
1756 Out << "return " << getCppName(GV) << ";\n";
1757 Out << "}\n";
1758}
1759
1760void CppWriter::printType(
1761 const std::string& fname, /// Name of generated function
1762 const std::string& typeName // Name of type to generate
1763) {
1764 const Type* Ty = TheModule->getTypeByName(typeName);
1765 if (!Ty) {
1766 error(std::string("Type '") + typeName + "' not found in input module");
1767 return;
1768 }
1769 Out << "\nType* " << fname << "(Module *mod) {\n";
1770 printType(Ty);
1771 Out << "return " << getCppName(Ty) << ";\n";
1772 Out << "}\n";
1773}
1774
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001775} // end anonymous llvm
1776
1777namespace llvm {
1778
1779void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001780 // Initialize a CppWriter for us to use
1781 CppWriter W(o, mod);
1782
1783 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001784 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001785
1786 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001787 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001788
1789 // Get the name of the thing we are to generate
1790 std::string tgtname = NameToGenerate.getValue();
1791 if (GenerationType == GenModule ||
1792 GenerationType == GenContents ||
1793 GenerationType == GenProgram) {
1794 if (tgtname == "!bad!") {
1795 if (mod->getModuleIdentifier() == "-")
1796 tgtname = "<stdin>";
1797 else
1798 tgtname = mod->getModuleIdentifier();
1799 }
1800 } else if (tgtname == "!bad!") {
1801 W.error("You must use the -for option with -gen-{function,variable,type}");
1802 }
1803
1804 switch (WhatToGenerate(GenerationType)) {
1805 case GenProgram:
1806 if (fname.empty())
1807 fname = "makeLLVMModule";
1808 W.printProgram(fname,tgtname);
1809 break;
1810 case GenModule:
1811 if (fname.empty())
1812 fname = "makeLLVMModule";
1813 W.printModule(fname,tgtname);
1814 break;
1815 case GenContents:
1816 if (fname.empty())
1817 fname = "makeLLVMModuleContents";
1818 W.printContents(fname,tgtname);
1819 break;
1820 case GenFunction:
1821 if (fname.empty())
1822 fname = "makeLLVMFunction";
1823 W.printFunction(fname,tgtname);
1824 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001825 case GenInline:
1826 if (fname.empty())
1827 fname = "makeLLVMInline";
1828 W.printInline(fname,tgtname);
1829 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001830 case GenVariable:
1831 if (fname.empty())
1832 fname = "makeLLVMVariable";
1833 W.printVariable(fname,tgtname);
1834 break;
1835 case GenType:
1836 if (fname.empty())
1837 fname = "makeLLVMType";
1838 W.printType(fname,tgtname);
1839 break;
1840 default:
1841 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001842 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001843}
1844
1845}