blob: b0eb1209e77f661e650103c44df2da1e839dc730 [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 Spencer78d033e2007-01-06 07:24:44 +000023#include "llvm/TypeSymbolTable.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/STLExtras.h"
Reid Spencer15f7e012006-05-30 21:18:23 +000026#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000027#include "llvm/Support/CFG.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/MathExtras.h"
Reid Spencerf977e7b2006-06-01 23:43:47 +000030#include "llvm/Config/config.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000031#include <algorithm>
32#include <iostream>
Reid Spencer66c87342006-05-30 03:43:49 +000033#include <set>
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000034
35using namespace llvm;
36
Reid Spencer15f7e012006-05-30 21:18:23 +000037static cl::opt<std::string>
Reid Spencer15f7e012006-05-30 21:18:23 +000038FuncName("funcname", cl::desc("Specify the name of the generated function"),
39 cl::value_desc("function name"));
40
Reid Spencer12803f52006-05-31 17:31:38 +000041enum WhatToGenerate {
42 GenProgram,
43 GenModule,
44 GenContents,
45 GenFunction,
Reid Spencerf977e7b2006-06-01 23:43:47 +000046 GenInline,
Reid Spencer12803f52006-05-31 17:31:38 +000047 GenVariable,
48 GenType
49};
50
51static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
52 cl::desc("Choose what kind of output to generate"),
53 cl::init(GenProgram),
54 cl::values(
55 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
56 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
57 clEnumValN(GenContents,"gen-contents", "Generate contents of a module"),
58 clEnumValN(GenFunction,"gen-function", "Generate a function definition"),
Reid Spencerf977e7b2006-06-01 23:43:47 +000059 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
Reid Spencer12803f52006-05-31 17:31:38 +000060 clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"),
61 clEnumValN(GenType, "gen-type", "Generate a type definition"),
62 clEnumValEnd
63 )
64);
65
66static cl::opt<std::string> NameToGenerate("for", cl::Optional,
67 cl::desc("Specify the name of the thing to generate"),
68 cl::init("!bad!"));
Reid Spencer15f7e012006-05-30 21:18:23 +000069
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000070namespace {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000071typedef std::vector<const Type*> TypeList;
72typedef std::map<const Type*,std::string> TypeMap;
73typedef std::map<const Value*,std::string> ValueMap;
Reid Spencer66c87342006-05-30 03:43:49 +000074typedef std::set<std::string> NameSet;
Reid Spencer15f7e012006-05-30 21:18:23 +000075typedef std::set<const Type*> TypeSet;
76typedef std::set<const Value*> ValueSet;
77typedef std::map<const Value*,std::string> ForwardRefMap;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000078
Reid Spencere0d133f2006-05-29 18:08:06 +000079class CppWriter {
Reid Spencer12803f52006-05-31 17:31:38 +000080 const char* progname;
Reid Spencere0d133f2006-05-29 18:08:06 +000081 std::ostream &Out;
82 const Module *TheModule;
Andrew Lenharth539d8712006-05-31 20:18:56 +000083 uint64_t uniqueNum;
Reid Spencere0d133f2006-05-29 18:08:06 +000084 TypeMap TypeNames;
85 ValueMap ValueNames;
86 TypeMap UnresolvedTypes;
87 TypeList TypeStack;
Reid Spencer66c87342006-05-30 03:43:49 +000088 NameSet UsedNames;
Reid Spencer15f7e012006-05-30 21:18:23 +000089 TypeSet DefinedTypes;
90 ValueSet DefinedValues;
91 ForwardRefMap ForwardRefs;
Reid Spencerf977e7b2006-06-01 23:43:47 +000092 bool is_inline;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000093
Reid Spencere0d133f2006-05-29 18:08:06 +000094public:
Reid Spencer12803f52006-05-31 17:31:38 +000095 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
96 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
Reid Spencerf977e7b2006-06-01 23:43:47 +000097 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000098
Reid Spencere0d133f2006-05-29 18:08:06 +000099 const Module* getModule() { return TheModule; }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000100
Reid Spencer12803f52006-05-31 17:31:38 +0000101 void printProgram(const std::string& fname, const std::string& modName );
102 void printModule(const std::string& fname, const std::string& modName );
103 void printContents(const std::string& fname, const std::string& modName );
104 void printFunction(const std::string& fname, const std::string& funcName );
Reid Spencerf977e7b2006-06-01 23:43:47 +0000105 void printInline(const std::string& fname, const std::string& funcName );
Reid Spencer12803f52006-05-31 17:31:38 +0000106 void printVariable(const std::string& fname, const std::string& varName );
107 void printType(const std::string& fname, const std::string& typeName );
108
109 void error(const std::string& msg);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000110
Reid Spencere0d133f2006-05-29 18:08:06 +0000111private:
Reid Spencer12803f52006-05-31 17:31:38 +0000112 void printLinkageType(GlobalValue::LinkageTypes LT);
113 void printCallingConv(unsigned cc);
114 void printEscapedString(const std::string& str);
115 void printCFP(const ConstantFP* CFP);
116
117 std::string getCppName(const Type* val);
118 inline void printCppName(const Type* val);
119
120 std::string getCppName(const Value* val);
121 inline void printCppName(const Value* val);
122
123 bool printTypeInternal(const Type* Ty);
124 inline void printType(const Type* Ty);
Reid Spencere0d133f2006-05-29 18:08:06 +0000125 void printTypes(const Module* M);
Reid Spencer12803f52006-05-31 17:31:38 +0000126
Reid Spencere0d133f2006-05-29 18:08:06 +0000127 void printConstant(const Constant *CPV);
Reid Spencer12803f52006-05-31 17:31:38 +0000128 void printConstants(const Module* M);
129
130 void printVariableUses(const GlobalVariable *GV);
131 void printVariableHead(const GlobalVariable *GV);
132 void printVariableBody(const GlobalVariable *GV);
133
134 void printFunctionUses(const Function *F);
Reid Spencer66c87342006-05-30 03:43:49 +0000135 void printFunctionHead(const Function *F);
136 void printFunctionBody(const Function *F);
Reid Spencere0d133f2006-05-29 18:08:06 +0000137 void printInstruction(const Instruction *I, const std::string& bbname);
Reid Spencer15f7e012006-05-30 21:18:23 +0000138 std::string getOpName(Value*);
139
Reid Spencer12803f52006-05-31 17:31:38 +0000140 void printModuleBody();
141
Reid Spencere0d133f2006-05-29 18:08:06 +0000142};
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000143
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000144static unsigned indent_level = 0;
145inline std::ostream& nl(std::ostream& Out, int delta = 0) {
146 Out << "\n";
147 if (delta >= 0 || indent_level >= unsigned(-delta))
148 indent_level += delta;
149 for (unsigned i = 0; i < indent_level; ++i)
150 Out << " ";
151 return Out;
152}
153
154inline void in() { indent_level++; }
155inline void out() { if (indent_level >0) indent_level--; }
156
Reid Spencer12803f52006-05-31 17:31:38 +0000157inline void
158sanitize(std::string& str) {
159 for (size_t i = 0; i < str.length(); ++i)
160 if (!isalnum(str[i]) && str[i] != '_')
161 str[i] = '_';
162}
163
164inline const char*
165getTypePrefix(const Type* Ty ) {
166 const char* prefix;
167 switch (Ty->getTypeID()) {
168 case Type::VoidTyID: prefix = "void_"; break;
169 case Type::BoolTyID: prefix = "bool_"; break;
Reid Spencer71d2ec92006-12-31 06:02:26 +0000170 case Type::Int8TyID: prefix = "int8_"; break;
171 case Type::Int16TyID: prefix = "int16_"; break;
172 case Type::Int32TyID: prefix = "int32_"; break;
173 case Type::Int64TyID: prefix = "int64_"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000174 case Type::FloatTyID: prefix = "float_"; break;
175 case Type::DoubleTyID: prefix = "double_"; break;
176 case Type::LabelTyID: prefix = "label_"; break;
177 case Type::FunctionTyID: prefix = "func_"; break;
178 case Type::StructTyID: prefix = "struct_"; break;
179 case Type::ArrayTyID: prefix = "array_"; break;
180 case Type::PointerTyID: prefix = "ptr_"; break;
181 case Type::PackedTyID: prefix = "packed_"; break;
182 case Type::OpaqueTyID: prefix = "opaque_"; break;
183 default: prefix = "other_"; break;
184 }
185 return prefix;
186}
187
188// Looks up the type in the symbol table and returns a pointer to its name or
189// a null pointer if it wasn't found. Note that this isn't the same as the
190// Mode::getTypeName function which will return an empty string, not a null
191// pointer if the name is not found.
192inline const std::string*
Reid Spencer78d033e2007-01-06 07:24:44 +0000193findTypeName(const TypeSymbolTable& ST, const Type* Ty)
Reid Spencer12803f52006-05-31 17:31:38 +0000194{
Reid Spencer78d033e2007-01-06 07:24:44 +0000195 TypeSymbolTable::const_iterator TI = ST.begin();
196 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencer12803f52006-05-31 17:31:38 +0000197 for (;TI != TE; ++TI)
198 if (TI->second == Ty)
199 return &(TI->first);
200 return 0;
201}
202
203void
204CppWriter::error(const std::string& msg) {
205 std::cerr << progname << ": " << msg << "\n";
206 exit(2);
207}
208
Reid Spencer15f7e012006-05-30 21:18:23 +0000209// printCFP - Print a floating point constant .. very carefully :)
210// This makes sure that conversion to/from floating yields the same binary
211// result so that we don't lose precision.
212void
213CppWriter::printCFP(const ConstantFP *CFP) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000214 Out << "ConstantFP::get(";
215 if (CFP->getType() == Type::DoubleTy)
216 Out << "Type::DoubleTy, ";
217 else
218 Out << "Type::FloatTy, ";
Reid Spencer15f7e012006-05-30 21:18:23 +0000219#if HAVE_PRINTF_A
220 char Buffer[100];
221 sprintf(Buffer, "%A", CFP->getValue());
222 if ((!strncmp(Buffer, "0x", 2) ||
223 !strncmp(Buffer, "-0x", 3) ||
224 !strncmp(Buffer, "+0x", 3)) &&
225 (atof(Buffer) == CFP->getValue()))
Reid Spencerf977e7b2006-06-01 23:43:47 +0000226 if (CFP->getType() == Type::DoubleTy)
227 Out << "BitsToDouble(" << Buffer << ")";
228 else
229 Out << "BitsToFloat(" << Buffer << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000230 else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000231#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000232 std::string StrVal = ftostr(CFP->getValue());
233
234 while (StrVal[0] == ' ')
235 StrVal.erase(StrVal.begin());
236
237 // Check to make sure that the stringized number is not some string like
238 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
239 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
240 ((StrVal[0] == '-' || StrVal[0] == '+') &&
241 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
242 (atof(StrVal.c_str()) == CFP->getValue()))
243 if (CFP->getType() == Type::DoubleTy)
244 Out << StrVal;
245 else
246 Out << StrVal;
247 else if (CFP->getType() == Type::DoubleTy)
248 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
249 << std::dec << "ULL) /* " << StrVal << " */";
250 else
251 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
252 << std::dec << "U) /* " << StrVal << " */";
Reid Spencer15f7e012006-05-30 21:18:23 +0000253#if HAVE_PRINTF_A
254 }
255#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000256 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000257}
258
Reid Spencer12803f52006-05-31 17:31:38 +0000259void
260CppWriter::printCallingConv(unsigned cc){
261 // Print the calling convention.
262 switch (cc) {
263 case CallingConv::C: Out << "CallingConv::C"; break;
264 case CallingConv::CSRet: Out << "CallingConv::CSRet"; break;
265 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
266 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
267 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
268 default: Out << cc; break;
269 }
270}
Reid Spencer15f7e012006-05-30 21:18:23 +0000271
Reid Spencer12803f52006-05-31 17:31:38 +0000272void
273CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
274 switch (LT) {
275 case GlobalValue::InternalLinkage:
276 Out << "GlobalValue::InternalLinkage"; break;
277 case GlobalValue::LinkOnceLinkage:
278 Out << "GlobalValue::LinkOnceLinkage "; break;
279 case GlobalValue::WeakLinkage:
280 Out << "GlobalValue::WeakLinkage"; break;
281 case GlobalValue::AppendingLinkage:
282 Out << "GlobalValue::AppendingLinkage"; break;
283 case GlobalValue::ExternalLinkage:
284 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000285 case GlobalValue::DLLImportLinkage:
286 Out << "GlobalValue::DllImportLinkage"; break;
287 case GlobalValue::DLLExportLinkage:
288 Out << "GlobalValue::DllExportLinkage"; break;
289 case GlobalValue::ExternalWeakLinkage:
290 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000291 case GlobalValue::GhostLinkage:
292 Out << "GlobalValue::GhostLinkage"; break;
293 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000294}
295
Reid Spencere0d133f2006-05-29 18:08:06 +0000296// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000297// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000298void
299CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000300 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
301 unsigned char C = Str[i];
302 if (isprint(C) && C != '"' && C != '\\') {
303 Out << C;
304 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000305 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000306 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
307 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
308 }
309 }
310}
311
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000312std::string
313CppWriter::getCppName(const Type* Ty)
314{
315 // First, handle the primitive types .. easy
316 if (Ty->isPrimitiveType()) {
317 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000318 case Type::VoidTyID: return "Type::VoidTy";
319 case Type::BoolTyID: return "Type::BoolTy";
320 case Type::Int8TyID: return "Type::Int8Ty";
321 case Type::Int16TyID: return "Type::Int16Ty";
322 case Type::Int32TyID: return "Type::Int32Ty";
323 case Type::Int64TyID: return "Type::Int64Ty";
324 case Type::FloatTyID: return "Type::FloatTy";
325 case Type::DoubleTyID: return "Type::DoubleTy";
326 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000327 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000328 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000329 break;
330 }
331 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
332 }
333
334 // Now, see if we've seen the type before and return that
335 TypeMap::iterator I = TypeNames.find(Ty);
336 if (I != TypeNames.end())
337 return I->second;
338
339 // Okay, let's build a new name for this type. Start with a prefix
340 const char* prefix = 0;
341 switch (Ty->getTypeID()) {
342 case Type::FunctionTyID: prefix = "FuncTy_"; break;
343 case Type::StructTyID: prefix = "StructTy_"; break;
344 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
345 case Type::PointerTyID: prefix = "PointerTy_"; break;
346 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
347 case Type::PackedTyID: prefix = "PackedTy_"; break;
348 default: prefix = "OtherTy_"; break; // prevent breakage
349 }
350
351 // See if the type has a name in the symboltable and build accordingly
Reid Spencer78d033e2007-01-06 07:24:44 +0000352 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000353 std::string name;
354 if (tName)
355 name = std::string(prefix) + *tName;
356 else
357 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000358 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000359
360 // Save the name
361 return TypeNames[Ty] = name;
362}
363
Reid Spencer12803f52006-05-31 17:31:38 +0000364void
365CppWriter::printCppName(const Type* Ty)
366{
367 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000368}
369
Reid Spencer12803f52006-05-31 17:31:38 +0000370std::string
371CppWriter::getCppName(const Value* val) {
372 std::string name;
373 ValueMap::iterator I = ValueNames.find(val);
374 if (I != ValueNames.end() && I->first == val)
375 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000376
Reid Spencer12803f52006-05-31 17:31:38 +0000377 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
378 name = std::string("gvar_") +
379 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000380 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000381 name = std::string("func_");
382 } else if (const Constant* C = dyn_cast<Constant>(val)) {
383 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000384 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
385 if (is_inline) {
386 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
387 Function::const_arg_iterator(Arg)) + 1;
388 name = std::string("arg_") + utostr(argNum);
389 NameSet::iterator NI = UsedNames.find(name);
390 if (NI != UsedNames.end())
391 name += std::string("_") + utostr(uniqueNum++);
392 UsedNames.insert(name);
393 return ValueNames[val] = name;
394 } else {
395 name = getTypePrefix(val->getType());
396 }
Reid Spencer12803f52006-05-31 17:31:38 +0000397 } else {
398 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000399 }
Reid Spencer12803f52006-05-31 17:31:38 +0000400 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
401 sanitize(name);
402 NameSet::iterator NI = UsedNames.find(name);
403 if (NI != UsedNames.end())
404 name += std::string("_") + utostr(uniqueNum++);
405 UsedNames.insert(name);
406 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000407}
408
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000409void
Reid Spencer12803f52006-05-31 17:31:38 +0000410CppWriter::printCppName(const Value* val) {
411 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000412}
413
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000414bool
Reid Spencer12803f52006-05-31 17:31:38 +0000415CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000416 // We don't print definitions for primitive types
417 if (Ty->isPrimitiveType())
418 return false;
419
Reid Spencer15f7e012006-05-30 21:18:23 +0000420 // If we already defined this type, we don't need to define it again.
421 if (DefinedTypes.find(Ty) != DefinedTypes.end())
422 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000423
Reid Spencer15f7e012006-05-30 21:18:23 +0000424 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000425 std::string typeName(getCppName(Ty));
426
427 // Search the type stack for recursion. If we find it, then generate this
428 // as an OpaqueType, but make sure not to do this multiple times because
429 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000430 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000431 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000432 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
433 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000434 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
435 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000436 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
437 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000438 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000439 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000440 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000441 }
442
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000443 // We're going to print a derived type which, by definition, contains other
444 // types. So, push this one we're printing onto the type stack to assist with
445 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000446 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000447
448 // Print the type definition
449 switch (Ty->getTypeID()) {
450 case Type::FunctionTyID: {
451 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000452 Out << "std::vector<const Type*>" << typeName << "_args;";
453 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000454 FunctionType::param_iterator PI = FT->param_begin();
455 FunctionType::param_iterator PE = FT->param_end();
456 for (; PI != PE; ++PI) {
457 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000458 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000459 std::string argName(getCppName(argTy));
460 Out << typeName << "_args.push_back(" << argName;
461 if (isForward)
462 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000463 Out << ");";
464 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000465 }
Reid Spencer12803f52006-05-31 17:31:38 +0000466 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000467 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000468 Out << "FunctionType* " << typeName << " = FunctionType::get(";
469 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000470 if (isForward)
471 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000472 Out << ",";
473 nl(Out) << "/*Params=*/" << typeName << "_args,";
474 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
475 out();
476 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000477 break;
478 }
479 case Type::StructTyID: {
480 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000481 Out << "std::vector<const Type*>" << typeName << "_fields;";
482 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000483 StructType::element_iterator EI = ST->element_begin();
484 StructType::element_iterator EE = ST->element_end();
485 for (; EI != EE; ++EI) {
486 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000487 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000488 std::string fieldName(getCppName(fieldTy));
489 Out << typeName << "_fields.push_back(" << fieldName;
490 if (isForward)
491 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000492 Out << ");";
493 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000494 }
495 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000496 << typeName << "_fields);";
497 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000498 break;
499 }
500 case Type::ArrayTyID: {
501 const ArrayType* AT = cast<ArrayType>(Ty);
502 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000503 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000504 std::string elemName(getCppName(ET));
505 Out << "ArrayType* " << typeName << " = ArrayType::get("
506 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000507 << ", " << utostr(AT->getNumElements()) << ");";
508 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000509 break;
510 }
511 case Type::PointerTyID: {
512 const PointerType* PT = cast<PointerType>(Ty);
513 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000514 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000515 std::string elemName(getCppName(ET));
516 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000517 << elemName << (isForward ? "_fwd" : "") << ");";
518 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000519 break;
520 }
521 case Type::PackedTyID: {
522 const PackedType* PT = cast<PackedType>(Ty);
523 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000524 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000525 std::string elemName(getCppName(ET));
526 Out << "PackedType* " << typeName << " = PackedType::get("
527 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000528 << ", " << utostr(PT->getNumElements()) << ");";
529 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000530 break;
531 }
532 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000533 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
534 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000535 break;
536 }
537 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000538 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000539 }
540
Reid Spencer74e032a2006-05-29 02:58:15 +0000541 // If the type had a name, make sure we recreate it.
542 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000543 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer74e032a2006-05-29 02:58:15 +0000544 if (progTypeName)
545 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000546 << typeName << ");";
547 nl(Out);
Reid Spencer74e032a2006-05-29 02:58:15 +0000548
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000549 // Pop us off the type stack
550 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000551
552 // Indicate that this type is now defined.
553 DefinedTypes.insert(Ty);
554
555 // Early resolve as many unresolved types as possible. Search the unresolved
556 // types map for the type we just printed. Now that its definition is complete
557 // we can resolve any previous references to it. This prevents a cascade of
558 // unresolved types.
559 TypeMap::iterator I = UnresolvedTypes.find(Ty);
560 if (I != UnresolvedTypes.end()) {
561 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000562 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
563 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000564 Out << I->second << " = cast<";
565 switch (Ty->getTypeID()) {
566 case Type::FunctionTyID: Out << "FunctionType"; break;
567 case Type::ArrayTyID: Out << "ArrayType"; break;
568 case Type::StructTyID: Out << "StructType"; break;
569 case Type::PackedTyID: Out << "PackedType"; break;
570 case Type::PointerTyID: Out << "PointerType"; break;
571 case Type::OpaqueTyID: Out << "OpaqueType"; break;
572 default: Out << "NoSuchDerivedType"; break;
573 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000574 Out << ">(" << I->second << "_fwd.get());";
575 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000576 UnresolvedTypes.erase(I);
577 }
578
579 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000580 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000581
582 // We weren't a recursive type
583 return false;
584}
585
Reid Spencer12803f52006-05-31 17:31:38 +0000586// Prints a type definition. Returns true if it could not resolve all the types
587// in the definition but had to use a forward reference.
588void
589CppWriter::printType(const Type* Ty) {
590 assert(TypeStack.empty());
591 TypeStack.clear();
592 printTypeInternal(Ty);
593 assert(TypeStack.empty());
594}
595
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000596void
597CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000598
599 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000600 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
601 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
602 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000603
604 // For primitive types and types already defined, just add a name
605 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
606 if (TI->second->isPrimitiveType() || TNI != TypeNames.end()) {
607 Out << "mod->addTypeName(\"";
608 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000609 Out << "\", " << getCppName(TI->second) << ");";
610 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000611 // For everything else, define the type
612 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000613 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000614 }
615 }
616
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000617 // Add all of the global variables to the value table...
618 for (Module::const_global_iterator I = TheModule->global_begin(),
619 E = TheModule->global_end(); I != E; ++I) {
620 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000621 printType(I->getInitializer()->getType());
622 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000623 }
624
625 // Add all the functions to the table
626 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
627 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000628 printType(FI->getReturnType());
629 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000630 // Add all the function arguments
631 for(Function::const_arg_iterator AI = FI->arg_begin(),
632 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000633 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000634 }
635
636 // Add all of the basic blocks and instructions
637 for (Function::const_iterator BB = FI->begin(),
638 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000639 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000640 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
641 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000642 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000643 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000644 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000645 }
646 }
647 }
648}
649
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000650
Reid Spencere0d133f2006-05-29 18:08:06 +0000651// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000652void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000653 // First, if the constant is actually a GlobalValue (variable or function) or
654 // its already in the constant list then we've printed it already and we can
655 // just return.
656 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000657 return;
658
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000659 std::string constName(getCppName(CV));
660 std::string typeName(getCppName(CV->getType()));
661 if (CV->isNullValue()) {
662 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000663 << typeName << ");";
664 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000665 return;
666 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000667 if (isa<GlobalValue>(CV)) {
668 // Skip variables and functions, we emit them elsewhere
669 return;
670 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000671 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000672 Out << "ConstantBool* " << constName << " = ConstantBool::get("
Chris Lattnere354df92006-09-28 23:24:48 +0000673 << (CB->getValue() ? "true" : "false") << ");";
Reid Spencerb83eb642006-10-20 07:07:24 +0000674 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
675 Out << "ConstantInt* " << constName << " = ConstantInt::get("
Reid Spencerf6c511c2006-12-18 07:58:01 +0000676 << typeName << ", " << CI->getZExtValue() << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000677 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000678 Out << "ConstantAggregateZero* " << constName
679 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000680 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000681 Out << "ConstantPointerNull* " << constName
682 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000683 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000684 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000685 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000686 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000687 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000688 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000689 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000690 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000691 // Determine if we want null termination or not.
692 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000693 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000694 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000695 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000696 Out << ");";
697 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000698 Out << "std::vector<Constant*> " << constName << "_elems;";
699 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000700 unsigned N = CA->getNumOperands();
701 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000702 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000703 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000704 << getCppName(CA->getOperand(i)) << ");";
705 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000706 }
707 Out << "Constant* " << constName << " = ConstantArray::get("
708 << typeName << ", " << constName << "_elems);";
709 }
710 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000711 Out << "std::vector<Constant*> " << constName << "_fields;";
712 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000713 unsigned N = CS->getNumOperands();
714 for (unsigned i = 0; i < N; i++) {
715 printConstant(CS->getOperand(i));
716 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000717 << getCppName(CS->getOperand(i)) << ");";
718 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000719 }
720 Out << "Constant* " << constName << " = ConstantStruct::get("
721 << typeName << ", " << constName << "_fields);";
722 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000723 Out << "std::vector<Constant*> " << constName << "_elems;";
724 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000725 unsigned N = CP->getNumOperands();
726 for (unsigned i = 0; i < N; ++i) {
727 printConstant(CP->getOperand(i));
728 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000729 << getCppName(CP->getOperand(i)) << ");";
730 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000731 }
732 Out << "Constant* " << constName << " = ConstantPacked::get("
733 << typeName << ", " << constName << "_elems);";
734 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000735 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000736 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000737 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000738 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000739 Out << "std::vector<Constant*> " << constName << "_indices;";
740 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000741 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000742 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000743 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000744 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000745 << getCppName(CE->getOperand(i)) << ");";
746 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000747 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000748 Out << "Constant* " << constName
749 << " = ConstantExpr::getGetElementPtr("
750 << getCppName(CE->getOperand(0)) << ", "
751 << constName << "_indices);";
Reid Spencer3da59db2006-11-27 01:05:10 +0000752 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000753 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000754 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000755 switch (CE->getOpcode()) {
756 default: assert(0 && "Invalid cast opcode");
757 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
758 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
759 case Instruction::SExt: Out << "Instruction::SExt"; break;
760 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
761 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
762 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
763 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
764 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
765 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
766 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
767 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
768 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
769 }
770 Out << ", " << getCppName(CE->getOperand(0)) << ", "
771 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000772 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000773 unsigned N = CE->getNumOperands();
774 for (unsigned i = 0; i < N; ++i ) {
775 printConstant(CE->getOperand(i));
776 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000777 Out << "Constant* " << constName << " = ConstantExpr::";
778 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000779 case Instruction::Add: Out << "getAdd("; break;
780 case Instruction::Sub: Out << "getSub("; break;
781 case Instruction::Mul: Out << "getMul("; break;
782 case Instruction::UDiv: Out << "getUDiv("; break;
783 case Instruction::SDiv: Out << "getSDiv("; break;
784 case Instruction::FDiv: Out << "getFDiv("; break;
785 case Instruction::URem: Out << "getURem("; break;
786 case Instruction::SRem: Out << "getSRem("; break;
787 case Instruction::FRem: Out << "getFRem("; break;
788 case Instruction::And: Out << "getAnd("; break;
789 case Instruction::Or: Out << "getOr("; break;
790 case Instruction::Xor: Out << "getXor("; break;
791 case Instruction::ICmp:
792 Out << "getICmp(ICmpInst::ICMP_";
793 switch (CE->getPredicate()) {
794 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
795 case ICmpInst::ICMP_NE: Out << "NE"; break;
796 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
797 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
798 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
799 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
800 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
801 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
802 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
803 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
804 default: error("Invalid ICmp Predicate");
805 }
806 break;
807 case Instruction::FCmp:
808 Out << "getFCmp(FCmpInst::FCMP_";
809 switch (CE->getPredicate()) {
810 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
811 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
812 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
813 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
814 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
815 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
816 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
817 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
818 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
819 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
820 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
821 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
822 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
823 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
824 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
825 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
826 default: error("Invalid FCmp Predicate");
827 }
828 break;
829 case Instruction::Shl: Out << "getShl("; break;
830 case Instruction::LShr: Out << "getLShr("; break;
831 case Instruction::AShr: Out << "getAShr("; break;
832 case Instruction::Select: Out << "getSelect("; break;
833 case Instruction::ExtractElement: Out << "getExtractElement("; break;
834 case Instruction::InsertElement: Out << "getInsertElement("; break;
835 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000836 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000837 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000838 break;
839 }
840 Out << getCppName(CE->getOperand(0));
841 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
842 Out << ", " << getCppName(CE->getOperand(i));
843 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000844 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000845 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000846 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000847 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000848 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000849 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000850}
851
Reid Spencer12803f52006-05-31 17:31:38 +0000852void
853CppWriter::printConstants(const Module* M) {
854 // Traverse all the global variables looking for constant initializers
855 for (Module::const_global_iterator I = TheModule->global_begin(),
856 E = TheModule->global_end(); I != E; ++I)
857 if (I->hasInitializer())
858 printConstant(I->getInitializer());
859
860 // Traverse the LLVM functions looking for constants
861 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
862 FI != FE; ++FI) {
863 // Add all of the basic blocks and instructions
864 for (Function::const_iterator BB = FI->begin(),
865 E = FI->end(); BB != E; ++BB) {
866 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
867 ++I) {
868 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
869 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
870 printConstant(C);
871 }
872 }
873 }
874 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000875 }
Reid Spencer66c87342006-05-30 03:43:49 +0000876}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000877
Reid Spencer12803f52006-05-31 17:31:38 +0000878void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000879 nl(Out) << "// Type Definitions";
880 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000881 printType(GV->getType());
882 if (GV->hasInitializer()) {
883 Constant* Init = GV->getInitializer();
884 printType(Init->getType());
885 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000886 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000887 printFunctionHead(F);
888 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000889 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000890 printVariableHead(gv);
891 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000892 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000893 printConstant(gv);
894 }
895 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000896 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000897 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000898 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000899 }
Reid Spencer12803f52006-05-31 17:31:38 +0000900}
Reid Spencer15f7e012006-05-30 21:18:23 +0000901
Reid Spencer12803f52006-05-31 17:31:38 +0000902void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000903 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000904 if (is_inline) {
905 Out << " = mod->getGlobalVariable(";
906 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000907 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
908 nl(Out) << "if (!" << getCppName(GV) << ") {";
909 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000910 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000911 Out << " = new GlobalVariable(";
912 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000913 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000914 Out << ",";
915 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
916 Out << ",";
917 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000918 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000919 Out << ",";
920 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000921 if (GV->hasInitializer()) {
922 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000923 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000924 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000925 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000926 Out << "\",";
927 nl(Out) << "mod);";
928 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000929
930 if (GV->hasSection()) {
931 printCppName(GV);
932 Out << "->setSection(\"";
933 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000934 Out << "\");";
935 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000936 }
937 if (GV->getAlignment()) {
938 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000939 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
940 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000941 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000942 if (is_inline) {
943 out(); Out << "}"; nl(Out);
944 }
Reid Spencer12803f52006-05-31 17:31:38 +0000945}
946
947void
948CppWriter::printVariableBody(const GlobalVariable *GV) {
949 if (GV->hasInitializer()) {
950 printCppName(GV);
951 Out << "->setInitializer(";
952 //if (!isa<GlobalValue(GV->getInitializer()))
953 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000954 Out << getCppName(GV->getInitializer()) << ");";
955 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000956 }
957}
958
959std::string
960CppWriter::getOpName(Value* V) {
961 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
962 return getCppName(V);
963
964 // See if its alread in the map of forward references, if so just return the
965 // name we already set up for it
966 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
967 if (I != ForwardRefs.end())
968 return I->second;
969
970 // This is a new forward reference. Generate a unique name for it
971 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
972
973 // Yes, this is a hack. An Argument is the smallest instantiable value that
974 // we can make as a placeholder for the real value. We'll replace these
975 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000976 Out << "Argument* " << result << " = new Argument("
977 << getCppName(V->getType()) << ");";
978 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000979 ForwardRefs[V] = result;
980 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000981}
982
Reid Spencere0d133f2006-05-29 18:08:06 +0000983// printInstruction - This member is called for each Instruction in a function.
984void
Reid Spencer12803f52006-05-31 17:31:38 +0000985CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000986 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000987
Reid Spencer15f7e012006-05-30 21:18:23 +0000988 // Before we emit this instruction, we need to take care of generating any
989 // forward references. So, we get the names of all the operands in advance
990 std::string* opNames = new std::string[I->getNumOperands()];
991 for (unsigned i = 0; i < I->getNumOperands(); i++) {
992 opNames[i] = getOpName(I->getOperand(i));
993 }
994
Reid Spencere0d133f2006-05-29 18:08:06 +0000995 switch (I->getOpcode()) {
996 case Instruction::Ret: {
997 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000998 Out << "ReturnInst* " << iName << " = new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +0000999 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001000 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001001 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001002 case Instruction::Br: {
1003 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001004 Out << "BranchInst* " << iName << " = new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001005 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001006 Out << opNames[0] << ", "
1007 << opNames[1] << ", "
1008 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001009
Reid Spencere0d133f2006-05-29 18:08:06 +00001010 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001011 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001012 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001013 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001014 }
1015 Out << bbname << ");";
1016 break;
1017 }
Reid Spencer66c87342006-05-30 03:43:49 +00001018 case Instruction::Switch: {
1019 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001020 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001021 << opNames[0] << ", "
1022 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001023 << sw->getNumCases() << ", " << bbname << ");";
1024 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001025 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001026 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001027 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001028 << opNames[i+1] << ");";
1029 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001030 }
1031 break;
1032 }
1033 case Instruction::Invoke: {
1034 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001035 Out << "std::vector<Value*> " << iName << "_params;";
1036 nl(Out);
1037 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1038 Out << iName << "_params.push_back("
1039 << opNames[i] << ");";
1040 nl(Out);
1041 }
1042 Out << "InvokeInst* " << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001043 << opNames[0] << ", "
1044 << opNames[1] << ", "
1045 << opNames[2] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001046 << iName << "_params, \"";
1047 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001048 Out << "\", " << bbname << ");";
1049 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001050 printCallingConv(inv->getCallingConv());
1051 Out << ");";
1052 break;
1053 }
1054 case Instruction::Unwind: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001055 Out << "UnwindInst* " << iName << " = new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001056 << bbname << ");";
1057 break;
1058 }
1059 case Instruction::Unreachable:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001060 Out << "UnreachableInst* " << iName << " = new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001061 << bbname << ");";
1062 break;
1063 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001064 case Instruction::Add:
1065 case Instruction::Sub:
1066 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001067 case Instruction::UDiv:
1068 case Instruction::SDiv:
1069 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001070 case Instruction::URem:
1071 case Instruction::SRem:
1072 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001073 case Instruction::And:
1074 case Instruction::Or:
1075 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001076 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001077 case Instruction::LShr:
1078 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001079 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001080 switch (I->getOpcode()) {
1081 case Instruction::Add: Out << "Instruction::Add"; break;
1082 case Instruction::Sub: Out << "Instruction::Sub"; break;
1083 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001084 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1085 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1086 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001087 case Instruction::URem:Out << "Instruction::URem"; break;
1088 case Instruction::SRem:Out << "Instruction::SRem"; break;
1089 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001090 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001091 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001092 case Instruction::Xor: Out << "Instruction::Xor"; break;
1093 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001094 case Instruction::LShr:Out << "Instruction::LShr"; break;
1095 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001096 default: Out << "Instruction::BadOpCode"; break;
1097 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001098 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001099 printEscapedString(I->getName());
1100 Out << "\", " << bbname << ");";
1101 break;
1102 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001103 case Instruction::FCmp: {
1104 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1105 switch (cast<FCmpInst>(I)->getPredicate()) {
1106 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1107 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1108 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1109 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1110 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1111 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1112 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1113 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1114 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1115 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1116 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1117 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1118 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1119 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1120 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1121 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1122 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1123 }
1124 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1125 printEscapedString(I->getName());
1126 Out << "\", " << bbname << ");";
1127 break;
1128 }
1129 case Instruction::ICmp: {
1130 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1131 switch (cast<ICmpInst>(I)->getPredicate()) {
1132 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1133 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1134 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1135 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1136 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1137 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1138 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1139 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1140 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1141 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1142 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001143 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001144 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001145 printEscapedString(I->getName());
1146 Out << "\", " << bbname << ");";
1147 break;
1148 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001149 case Instruction::Malloc: {
1150 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001151 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001152 << getCppName(mallocI->getAllocatedType()) << ", ";
1153 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001154 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001155 Out << "\"";
1156 printEscapedString(mallocI->getName());
1157 Out << "\", " << bbname << ");";
1158 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001159 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001160 << mallocI->getAlignment() << ");";
1161 break;
1162 }
Reid Spencer66c87342006-05-30 03:43:49 +00001163 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001164 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001165 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1166 break;
1167 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001168 case Instruction::Alloca: {
1169 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001170 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001171 << getCppName(allocaI->getAllocatedType()) << ", ";
1172 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001173 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001174 Out << "\"";
1175 printEscapedString(allocaI->getName());
1176 Out << "\", " << bbname << ");";
1177 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001178 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001179 << allocaI->getAlignment() << ");";
1180 break;
1181 }
Reid Spencer66c87342006-05-30 03:43:49 +00001182 case Instruction::Load:{
1183 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001184 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001185 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001186 printEscapedString(load->getName());
1187 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001188 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001189 break;
1190 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001191 case Instruction::Store: {
1192 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001193 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001194 << opNames[0] << ", "
1195 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001196 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001197 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001198 break;
1199 }
1200 case Instruction::GetElementPtr: {
1201 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1202 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001203 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001204 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001205 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001206 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001207 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001208 Out << "std::vector<Value*> " << iName << "_indices;";
1209 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001210 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001211 Out << iName << "_indices.push_back("
1212 << opNames[i] << ");";
1213 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001214 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001215 Out << "Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001216 << opNames[0] << ", " << iName << "_indices";
Reid Spencere0d133f2006-05-29 18:08:06 +00001217 }
1218 Out << ", \"";
1219 printEscapedString(gep->getName());
1220 Out << "\", " << bbname << ");";
1221 break;
1222 }
Reid Spencer66c87342006-05-30 03:43:49 +00001223 case Instruction::PHI: {
1224 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001225
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001226 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001227 << getCppName(phi->getType()) << ", \"";
1228 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001229 Out << "\", " << bbname << ");";
1230 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001231 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001232 << ");";
1233 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001234 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001235 Out << iName << "->addIncoming("
1236 << opNames[i] << ", " << opNames[i+1] << ");";
1237 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001238 }
Reid Spencer66c87342006-05-30 03:43:49 +00001239 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001240 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001241 case Instruction::Trunc:
1242 case Instruction::ZExt:
1243 case Instruction::SExt:
1244 case Instruction::FPTrunc:
1245 case Instruction::FPExt:
1246 case Instruction::FPToUI:
1247 case Instruction::FPToSI:
1248 case Instruction::UIToFP:
1249 case Instruction::SIToFP:
1250 case Instruction::PtrToInt:
1251 case Instruction::IntToPtr:
1252 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001253 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001254 Out << "CastInst* " << iName << " = new ";
1255 switch (I->getOpcode()) {
1256 case Instruction::Trunc: Out << "TruncInst";
1257 case Instruction::ZExt: Out << "ZExtInst";
1258 case Instruction::SExt: Out << "SExtInst";
1259 case Instruction::FPTrunc: Out << "FPTruncInst";
1260 case Instruction::FPExt: Out << "FPExtInst";
1261 case Instruction::FPToUI: Out << "FPToUIInst";
1262 case Instruction::FPToSI: Out << "FPToSIInst";
1263 case Instruction::UIToFP: Out << "UIToFPInst";
1264 case Instruction::SIToFP: Out << "SIToFPInst";
1265 case Instruction::PtrToInt: Out << "PtrToInst";
1266 case Instruction::IntToPtr: Out << "IntToPtrInst";
1267 case Instruction::BitCast: Out << "BitCastInst";
1268 default: assert(!"Unreachable"); break;
1269 }
1270 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001271 << getCppName(cst->getType()) << ", \"";
1272 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001273 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001274 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001275 }
Reid Spencer66c87342006-05-30 03:43:49 +00001276 case Instruction::Call:{
1277 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001278 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001279 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001280 << getCppName(ila->getFunctionType()) << ", \""
1281 << ila->getAsmString() << "\", \""
1282 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001283 << (ila->hasSideEffects() ? "true" : "false") << ");";
1284 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001285 }
Reid Spencer66c87342006-05-30 03:43:49 +00001286 if (call->getNumOperands() > 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001287 Out << "std::vector<Value*> " << iName << "_params;";
1288 nl(Out);
1289 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1290 Out << iName << "_params.push_back(" << opNames[i] << ");";
1291 nl(Out);
1292 }
1293 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001294 << opNames[0] << ", " << iName << "_params, \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001295 } else if (call->getNumOperands() == 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001296 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001297 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001298 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001299 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001300 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001301 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001302 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001303 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001304 }
1305 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001306 Out << "\", " << bbname << ");";
1307 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001308 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001309 Out << ");";
1310 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001311 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001312 Out << ");";
1313 break;
1314 }
1315 case Instruction::Select: {
1316 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001317 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001318 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001319 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001320 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001321 break;
1322 }
1323 case Instruction::UserOp1:
1324 /// FALL THROUGH
1325 case Instruction::UserOp2: {
1326 /// FIXME: What should be done here?
1327 break;
1328 }
1329 case Instruction::VAArg: {
1330 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001331 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001332 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001333 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001334 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001335 break;
1336 }
1337 case Instruction::ExtractElement: {
1338 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001339 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001340 << " = new ExtractElementInst(" << opNames[0]
1341 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001342 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001343 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001344 break;
1345 }
1346 case Instruction::InsertElement: {
1347 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001348 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001349 << " = new InsertElementInst(" << opNames[0]
1350 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001351 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001352 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001353 break;
1354 }
1355 case Instruction::ShuffleVector: {
1356 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001357 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001358 << " = new ShuffleVectorInst(" << opNames[0]
1359 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001360 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001361 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001362 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001363 }
1364 }
Reid Spencer68464b72006-06-15 16:09:59 +00001365 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001366 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001367 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001368}
1369
Reid Spencer12803f52006-05-31 17:31:38 +00001370// Print out the types, constants and declarations needed by one function
1371void CppWriter::printFunctionUses(const Function* F) {
1372
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001373 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001374 if (!is_inline) {
1375 // Print the function's return type
1376 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001377
Reid Spencerf977e7b2006-06-01 23:43:47 +00001378 // Print the function's function type
1379 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001380
Reid Spencerf977e7b2006-06-01 23:43:47 +00001381 // Print the types of each of the function's arguments
1382 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1383 AI != AE; ++AI) {
1384 printType(AI->getType());
1385 }
Reid Spencer12803f52006-05-31 17:31:38 +00001386 }
1387
1388 // Print type definitions for every type referenced by an instruction and
1389 // make a note of any global values or constants that are referenced
1390 std::vector<GlobalValue*> gvs;
1391 std::vector<Constant*> consts;
1392 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1393 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1394 I != E; ++I) {
1395 // Print the type of the instruction itself
1396 printType(I->getType());
1397
1398 // Print the type of each of the instruction's operands
1399 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1400 Value* operand = I->getOperand(i);
1401 printType(operand->getType());
1402 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1403 gvs.push_back(GV);
1404 else if (Constant* C = dyn_cast<Constant>(operand))
1405 consts.push_back(C);
1406 }
1407 }
1408 }
1409
1410 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001411 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001412 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1413 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001414 if (Function* Fun = dyn_cast<Function>(*I)) {
1415 if (!is_inline || Fun != F)
1416 printFunctionHead(Fun);
1417 }
Reid Spencer12803f52006-05-31 17:31:38 +00001418 }
1419
1420 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001421 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001422 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1423 I != E; ++I) {
1424 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1425 printVariableHead(F);
1426 }
1427
1428 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001429 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001430 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1431 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001432 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001433 }
1434
1435 // Process the global variables definitions now that all the constants have
1436 // been emitted. These definitions just couple the gvars with their constant
1437 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001438 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001439 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1440 I != E; ++I) {
1441 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1442 printVariableBody(GV);
1443 }
1444}
1445
1446void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001447 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001448 if (is_inline) {
1449 Out << " = mod->getFunction(\"";
1450 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001451 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1452 nl(Out) << "if (!" << getCppName(F) << ") {";
1453 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001454 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001455 Out<< " = new Function(";
1456 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1457 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001458 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001459 Out << ",";
1460 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001461 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001462 Out << "\", mod); " << (F->isExternal()? "// (external, no body)" : "");
1463 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001464 printCppName(F);
1465 Out << "->setCallingConv(";
1466 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001467 Out << ");";
1468 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001469 if (F->hasSection()) {
1470 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001471 Out << "->setSection(\"" << F->getSection() << "\");";
1472 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001473 }
1474 if (F->getAlignment()) {
1475 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001476 Out << "->setAlignment(" << F->getAlignment() << ");";
1477 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001478 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001479 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001480 Out << "}";
1481 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001482 }
Reid Spencer12803f52006-05-31 17:31:38 +00001483}
1484
1485void CppWriter::printFunctionBody(const Function *F) {
1486 if (F->isExternal())
1487 return; // external functions have no bodies.
1488
1489 // Clear the DefinedValues and ForwardRefs maps because we can't have
1490 // cross-function forward refs
1491 ForwardRefs.clear();
1492 DefinedValues.clear();
1493
1494 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001495 if (!is_inline) {
1496 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001497 Out << "Function::arg_iterator args = " << getCppName(F)
1498 << "->arg_begin();";
1499 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001500 }
1501 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1502 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001503 Out << "Value* " << getCppName(AI) << " = args++;";
1504 nl(Out);
1505 if (AI->hasName()) {
1506 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1507 nl(Out);
1508 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001509 }
Reid Spencer12803f52006-05-31 17:31:38 +00001510 }
1511
1512 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001513 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001514 for (Function::const_iterator BI = F->begin(), BE = F->end();
1515 BI != BE; ++BI) {
1516 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001517 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001518 if (BI->hasName())
1519 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001520 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1521 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001522 }
1523
1524 // Output all of its basic blocks... for the function
1525 for (Function::const_iterator BI = F->begin(), BE = F->end();
1526 BI != BE; ++BI) {
1527 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001528 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1529 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001530
1531 // Output all of the instructions in the basic block...
1532 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1533 I != E; ++I) {
1534 printInstruction(I,bbname);
1535 }
1536 }
1537
1538 // Loop over the ForwardRefs and resolve them now that all instructions
1539 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001540 if (!ForwardRefs.empty()) {
1541 nl(Out) << "// Resolve Forward References";
1542 nl(Out);
1543 }
1544
Reid Spencer12803f52006-05-31 17:31:38 +00001545 while (!ForwardRefs.empty()) {
1546 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001547 Out << I->second << "->replaceAllUsesWith("
1548 << getCppName(I->first) << "); delete " << I->second << ";";
1549 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001550 ForwardRefs.erase(I);
1551 }
1552}
1553
Reid Spencerf977e7b2006-06-01 23:43:47 +00001554void CppWriter::printInline(const std::string& fname, const std::string& func) {
1555 const Function* F = TheModule->getNamedFunction(func);
1556 if (!F) {
1557 error(std::string("Function '") + func + "' not found in input module");
1558 return;
1559 }
1560 if (F->isExternal()) {
1561 error(std::string("Function '") + func + "' is external!");
1562 return;
1563 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001564 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001565 << getCppName(F);
1566 unsigned arg_count = 1;
1567 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1568 AI != AE; ++AI) {
1569 Out << ", Value* arg_" << arg_count;
1570 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001571 Out << ") {";
1572 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001573 is_inline = true;
1574 printFunctionUses(F);
1575 printFunctionBody(F);
1576 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001577 Out << "return " << getCppName(F->begin()) << ";";
1578 nl(Out) << "}";
1579 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001580}
1581
Reid Spencer12803f52006-05-31 17:31:38 +00001582void CppWriter::printModuleBody() {
1583 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001584 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001585 printTypes(TheModule);
1586
1587 // Functions can call each other and global variables can reference them so
1588 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001589 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001590 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1591 I != E; ++I)
1592 printFunctionHead(I);
1593
1594 // Process the global variables declarations. We can't initialze them until
1595 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001596 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001597 for (Module::const_global_iterator I = TheModule->global_begin(),
1598 E = TheModule->global_end(); I != E; ++I) {
1599 printVariableHead(I);
1600 }
1601
1602 // Print out all the constants definitions. Constants don't recurse except
1603 // through GlobalValues. All GlobalValues have been declared at this point
1604 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001605 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001606 printConstants(TheModule);
1607
1608 // Process the global variables definitions now that all the constants have
1609 // been emitted. These definitions just couple the gvars with their constant
1610 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001611 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001612 for (Module::const_global_iterator I = TheModule->global_begin(),
1613 E = TheModule->global_end(); I != E; ++I) {
1614 printVariableBody(I);
1615 }
1616
1617 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001618 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001619 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1620 I != E; ++I) {
1621 if (!I->isExternal()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001622 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1623 << ")";
1624 nl(Out) << "{";
1625 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001626 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001627 nl(Out,-1) << "}";
1628 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001629 }
1630 }
1631}
1632
1633void CppWriter::printProgram(
1634 const std::string& fname,
1635 const std::string& mName
1636) {
1637 Out << "#include <llvm/Module.h>\n";
1638 Out << "#include <llvm/DerivedTypes.h>\n";
1639 Out << "#include <llvm/Constants.h>\n";
1640 Out << "#include <llvm/GlobalVariable.h>\n";
1641 Out << "#include <llvm/Function.h>\n";
1642 Out << "#include <llvm/CallingConv.h>\n";
1643 Out << "#include <llvm/BasicBlock.h>\n";
1644 Out << "#include <llvm/Instructions.h>\n";
1645 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001646 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001647 Out << "#include <llvm/Pass.h>\n";
1648 Out << "#include <llvm/PassManager.h>\n";
1649 Out << "#include <llvm/Analysis/Verifier.h>\n";
1650 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1651 Out << "#include <algorithm>\n";
1652 Out << "#include <iostream>\n\n";
1653 Out << "using namespace llvm;\n\n";
1654 Out << "Module* " << fname << "();\n\n";
1655 Out << "int main(int argc, char**argv) {\n";
1656 Out << " Module* Mod = makeLLVMModule();\n";
1657 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1658 Out << " std::cerr.flush();\n";
1659 Out << " std::cout.flush();\n";
1660 Out << " PassManager PM;\n";
1661 Out << " PM.add(new PrintModulePass(&std::cout));\n";
1662 Out << " PM.run(*Mod);\n";
1663 Out << " return 0;\n";
1664 Out << "}\n\n";
1665 printModule(fname,mName);
1666}
1667
1668void CppWriter::printModule(
1669 const std::string& fname,
1670 const std::string& mName
1671) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001672 nl(Out) << "Module* " << fname << "() {";
1673 nl(Out,1) << "// Module Construction";
1674 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1675 nl(Out) << "mod->setEndianness(";
Reid Spencer12803f52006-05-31 17:31:38 +00001676 switch (TheModule->getEndianness()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001677 case Module::LittleEndian: Out << "Module::LittleEndian);"; break;
1678 case Module::BigEndian: Out << "Module::BigEndian);"; break;
1679 case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001680 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001681 nl(Out) << "mod->setPointerSize(";
Reid Spencer12803f52006-05-31 17:31:38 +00001682 switch (TheModule->getPointerSize()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001683 case Module::Pointer32: Out << "Module::Pointer32);"; break;
1684 case Module::Pointer64: Out << "Module::Pointer64);"; break;
1685 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001686 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001687 nl(Out);
1688 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer12803f52006-05-31 17:31:38 +00001689 Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001690 << "\");";
1691 nl(Out);
1692 }
Reid Spencer12803f52006-05-31 17:31:38 +00001693
1694 if (!TheModule->getModuleInlineAsm().empty()) {
1695 Out << "mod->setModuleInlineAsm(\"";
1696 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001697 Out << "\");";
1698 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001699 }
1700
1701 // Loop over the dependent libraries and emit them.
1702 Module::lib_iterator LI = TheModule->lib_begin();
1703 Module::lib_iterator LE = TheModule->lib_end();
1704 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001705 Out << "mod->addLibrary(\"" << *LI << "\");";
1706 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001707 ++LI;
1708 }
1709 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001710 nl(Out) << "return mod;";
1711 nl(Out,-1) << "}";
1712 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001713}
1714
1715void CppWriter::printContents(
1716 const std::string& fname, // Name of generated function
1717 const std::string& mName // Name of module generated module
1718) {
1719 Out << "\nModule* " << fname << "(Module *mod) {\n";
1720 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001721 printModuleBody();
1722 Out << "\nreturn mod;\n";
1723 Out << "\n}\n";
1724}
1725
1726void CppWriter::printFunction(
1727 const std::string& fname, // Name of generated function
1728 const std::string& funcName // Name of function to generate
1729) {
1730 const Function* F = TheModule->getNamedFunction(funcName);
1731 if (!F) {
1732 error(std::string("Function '") + funcName + "' not found in input module");
1733 return;
1734 }
1735 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1736 printFunctionUses(F);
1737 printFunctionHead(F);
1738 printFunctionBody(F);
1739 Out << "return " << getCppName(F) << ";\n";
1740 Out << "}\n";
1741}
1742
1743void CppWriter::printVariable(
1744 const std::string& fname, /// Name of generated function
1745 const std::string& varName // Name of variable to generate
1746) {
1747 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1748
1749 if (!GV) {
1750 error(std::string("Variable '") + varName + "' not found in input module");
1751 return;
1752 }
1753 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1754 printVariableUses(GV);
1755 printVariableHead(GV);
1756 printVariableBody(GV);
1757 Out << "return " << getCppName(GV) << ";\n";
1758 Out << "}\n";
1759}
1760
1761void CppWriter::printType(
1762 const std::string& fname, /// Name of generated function
1763 const std::string& typeName // Name of type to generate
1764) {
1765 const Type* Ty = TheModule->getTypeByName(typeName);
1766 if (!Ty) {
1767 error(std::string("Type '") + typeName + "' not found in input module");
1768 return;
1769 }
1770 Out << "\nType* " << fname << "(Module *mod) {\n";
1771 printType(Ty);
1772 Out << "return " << getCppName(Ty) << ";\n";
1773 Out << "}\n";
1774}
1775
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001776} // end anonymous llvm
1777
1778namespace llvm {
1779
1780void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001781 // Initialize a CppWriter for us to use
1782 CppWriter W(o, mod);
1783
1784 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001785 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001786
1787 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001788 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001789
1790 // Get the name of the thing we are to generate
1791 std::string tgtname = NameToGenerate.getValue();
1792 if (GenerationType == GenModule ||
1793 GenerationType == GenContents ||
1794 GenerationType == GenProgram) {
1795 if (tgtname == "!bad!") {
1796 if (mod->getModuleIdentifier() == "-")
1797 tgtname = "<stdin>";
1798 else
1799 tgtname = mod->getModuleIdentifier();
1800 }
1801 } else if (tgtname == "!bad!") {
1802 W.error("You must use the -for option with -gen-{function,variable,type}");
1803 }
1804
1805 switch (WhatToGenerate(GenerationType)) {
1806 case GenProgram:
1807 if (fname.empty())
1808 fname = "makeLLVMModule";
1809 W.printProgram(fname,tgtname);
1810 break;
1811 case GenModule:
1812 if (fname.empty())
1813 fname = "makeLLVMModule";
1814 W.printModule(fname,tgtname);
1815 break;
1816 case GenContents:
1817 if (fname.empty())
1818 fname = "makeLLVMModuleContents";
1819 W.printContents(fname,tgtname);
1820 break;
1821 case GenFunction:
1822 if (fname.empty())
1823 fname = "makeLLVMFunction";
1824 W.printFunction(fname,tgtname);
1825 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001826 case GenInline:
1827 if (fname.empty())
1828 fname = "makeLLVMInline";
1829 W.printInline(fname,tgtname);
1830 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001831 case GenVariable:
1832 if (fname.empty())
1833 fname = "makeLLVMVariable";
1834 W.printVariable(fname,tgtname);
1835 break;
1836 case GenType:
1837 if (fname.empty())
1838 fname = "makeLLVMType";
1839 W.printType(fname,tgtname);
1840 break;
1841 default:
1842 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001843 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001844}
1845
1846}