blob: a2f02c3f202b20973ddf67bd7c01019cc68d83ff [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"
Reid Spencer6abd3da2007-04-11 09:54:08 +000021#include "llvm/ParameterAttributes.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000022#include "llvm/Module.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
Reid Spencera54b7cb2007-01-12 07:05:14 +0000164inline std::string
Reid Spencer12803f52006-05-31 17:31:38 +0000165getTypePrefix(const Type* Ty ) {
Reid Spencer12803f52006-05-31 17:31:38 +0000166 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000167 case Type::VoidTyID: return "void_";
168 case Type::IntegerTyID:
169 return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
170 "_";
171 case Type::FloatTyID: return "float_";
172 case Type::DoubleTyID: return "double_";
173 case Type::LabelTyID: return "label_";
174 case Type::FunctionTyID: return "func_";
175 case Type::StructTyID: return "struct_";
176 case Type::ArrayTyID: return "array_";
177 case Type::PointerTyID: return "ptr_";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000178 case Type::VectorTyID: return "packed_";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000179 case Type::OpaqueTyID: return "opaque_";
180 default: return "other_";
Reid Spencer12803f52006-05-31 17:31:38 +0000181 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000182 return "unknown_";
Reid Spencer12803f52006-05-31 17:31:38 +0000183}
184
185// Looks up the type in the symbol table and returns a pointer to its name or
186// a null pointer if it wasn't found. Note that this isn't the same as the
187// Mode::getTypeName function which will return an empty string, not a null
188// pointer if the name is not found.
189inline const std::string*
Reid Spencer78d033e2007-01-06 07:24:44 +0000190findTypeName(const TypeSymbolTable& ST, const Type* Ty)
Reid Spencer12803f52006-05-31 17:31:38 +0000191{
Reid Spencer78d033e2007-01-06 07:24:44 +0000192 TypeSymbolTable::const_iterator TI = ST.begin();
193 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencer12803f52006-05-31 17:31:38 +0000194 for (;TI != TE; ++TI)
195 if (TI->second == Ty)
196 return &(TI->first);
197 return 0;
198}
199
200void
201CppWriter::error(const std::string& msg) {
202 std::cerr << progname << ": " << msg << "\n";
203 exit(2);
204}
205
Reid Spencer15f7e012006-05-30 21:18:23 +0000206// printCFP - Print a floating point constant .. very carefully :)
207// This makes sure that conversion to/from floating yields the same binary
208// result so that we don't lose precision.
209void
210CppWriter::printCFP(const ConstantFP *CFP) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000211 Out << "ConstantFP::get(";
212 if (CFP->getType() == Type::DoubleTy)
213 Out << "Type::DoubleTy, ";
214 else
215 Out << "Type::FloatTy, ";
Reid Spencer15f7e012006-05-30 21:18:23 +0000216#if HAVE_PRINTF_A
217 char Buffer[100];
218 sprintf(Buffer, "%A", CFP->getValue());
219 if ((!strncmp(Buffer, "0x", 2) ||
220 !strncmp(Buffer, "-0x", 3) ||
221 !strncmp(Buffer, "+0x", 3)) &&
222 (atof(Buffer) == CFP->getValue()))
Reid Spencerf977e7b2006-06-01 23:43:47 +0000223 if (CFP->getType() == Type::DoubleTy)
224 Out << "BitsToDouble(" << Buffer << ")";
225 else
226 Out << "BitsToFloat(" << Buffer << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000227 else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000228#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000229 std::string StrVal = ftostr(CFP->getValue());
230
231 while (StrVal[0] == ' ')
232 StrVal.erase(StrVal.begin());
233
234 // Check to make sure that the stringized number is not some string like
235 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
236 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
237 ((StrVal[0] == '-' || StrVal[0] == '+') &&
238 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
239 (atof(StrVal.c_str()) == CFP->getValue()))
240 if (CFP->getType() == Type::DoubleTy)
241 Out << StrVal;
242 else
243 Out << StrVal;
244 else if (CFP->getType() == Type::DoubleTy)
245 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
246 << std::dec << "ULL) /* " << StrVal << " */";
247 else
248 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
249 << std::dec << "U) /* " << StrVal << " */";
Reid Spencer15f7e012006-05-30 21:18:23 +0000250#if HAVE_PRINTF_A
251 }
252#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000253 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000254}
255
Reid Spencer12803f52006-05-31 17:31:38 +0000256void
257CppWriter::printCallingConv(unsigned cc){
258 // Print the calling convention.
259 switch (cc) {
260 case CallingConv::C: Out << "CallingConv::C"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000261 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
262 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
263 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
264 default: Out << cc; break;
265 }
266}
Reid Spencer15f7e012006-05-30 21:18:23 +0000267
Reid Spencer12803f52006-05-31 17:31:38 +0000268void
269CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
270 switch (LT) {
271 case GlobalValue::InternalLinkage:
272 Out << "GlobalValue::InternalLinkage"; break;
273 case GlobalValue::LinkOnceLinkage:
274 Out << "GlobalValue::LinkOnceLinkage "; break;
275 case GlobalValue::WeakLinkage:
276 Out << "GlobalValue::WeakLinkage"; break;
277 case GlobalValue::AppendingLinkage:
278 Out << "GlobalValue::AppendingLinkage"; break;
279 case GlobalValue::ExternalLinkage:
280 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000281 case GlobalValue::DLLImportLinkage:
282 Out << "GlobalValue::DllImportLinkage"; break;
283 case GlobalValue::DLLExportLinkage:
284 Out << "GlobalValue::DllExportLinkage"; break;
285 case GlobalValue::ExternalWeakLinkage:
286 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000287 case GlobalValue::GhostLinkage:
288 Out << "GlobalValue::GhostLinkage"; break;
289 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000290}
291
Reid Spencere0d133f2006-05-29 18:08:06 +0000292// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000293// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000294void
295CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000296 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
297 unsigned char C = Str[i];
298 if (isprint(C) && C != '"' && C != '\\') {
299 Out << C;
300 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000301 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000302 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
303 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
304 }
305 }
306}
307
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000308std::string
309CppWriter::getCppName(const Type* Ty)
310{
311 // First, handle the primitive types .. easy
Chris Lattner42a75512007-01-15 02:27:26 +0000312 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000313 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000314 case Type::VoidTyID: return "Type::VoidTy";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000315 case Type::IntegerTyID: {
316 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
317 return "IntegerType::get(" + utostr(BitWidth) + ")";
318 }
Reid Spencer71d2ec92006-12-31 06:02:26 +0000319 case Type::FloatTyID: return "Type::FloatTy";
320 case Type::DoubleTyID: return "Type::DoubleTy";
321 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000322 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000323 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000324 break;
325 }
326 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
327 }
328
329 // Now, see if we've seen the type before and return that
330 TypeMap::iterator I = TypeNames.find(Ty);
331 if (I != TypeNames.end())
332 return I->second;
333
334 // Okay, let's build a new name for this type. Start with a prefix
335 const char* prefix = 0;
336 switch (Ty->getTypeID()) {
337 case Type::FunctionTyID: prefix = "FuncTy_"; break;
338 case Type::StructTyID: prefix = "StructTy_"; break;
339 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
340 case Type::PointerTyID: prefix = "PointerTy_"; break;
341 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
Reid Spencerac9dcb92007-02-15 03:39:18 +0000342 case Type::VectorTyID: prefix = "VectorTy_"; break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000343 default: prefix = "OtherTy_"; break; // prevent breakage
344 }
345
346 // See if the type has a name in the symboltable and build accordingly
Reid Spencer78d033e2007-01-06 07:24:44 +0000347 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000348 std::string name;
349 if (tName)
350 name = std::string(prefix) + *tName;
351 else
352 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000353 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000354
355 // Save the name
356 return TypeNames[Ty] = name;
357}
358
Reid Spencer12803f52006-05-31 17:31:38 +0000359void
360CppWriter::printCppName(const Type* Ty)
361{
362 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000363}
364
Reid Spencer12803f52006-05-31 17:31:38 +0000365std::string
366CppWriter::getCppName(const Value* val) {
367 std::string name;
368 ValueMap::iterator I = ValueNames.find(val);
369 if (I != ValueNames.end() && I->first == val)
370 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000371
Reid Spencer12803f52006-05-31 17:31:38 +0000372 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
373 name = std::string("gvar_") +
374 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000375 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000376 name = std::string("func_");
377 } else if (const Constant* C = dyn_cast<Constant>(val)) {
378 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000379 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
380 if (is_inline) {
381 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
382 Function::const_arg_iterator(Arg)) + 1;
383 name = std::string("arg_") + utostr(argNum);
384 NameSet::iterator NI = UsedNames.find(name);
385 if (NI != UsedNames.end())
386 name += std::string("_") + utostr(uniqueNum++);
387 UsedNames.insert(name);
388 return ValueNames[val] = name;
389 } else {
390 name = getTypePrefix(val->getType());
391 }
Reid Spencer12803f52006-05-31 17:31:38 +0000392 } else {
393 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000394 }
Reid Spencer12803f52006-05-31 17:31:38 +0000395 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
396 sanitize(name);
397 NameSet::iterator NI = UsedNames.find(name);
398 if (NI != UsedNames.end())
399 name += std::string("_") + utostr(uniqueNum++);
400 UsedNames.insert(name);
401 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000402}
403
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000404void
Reid Spencer12803f52006-05-31 17:31:38 +0000405CppWriter::printCppName(const Value* val) {
406 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000407}
408
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000409bool
Reid Spencer12803f52006-05-31 17:31:38 +0000410CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000411 // We don't print definitions for primitive types
Chris Lattner42a75512007-01-15 02:27:26 +0000412 if (Ty->isPrimitiveType() || Ty->isInteger())
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000413 return false;
414
Reid Spencer15f7e012006-05-30 21:18:23 +0000415 // If we already defined this type, we don't need to define it again.
416 if (DefinedTypes.find(Ty) != DefinedTypes.end())
417 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000418
Reid Spencer15f7e012006-05-30 21:18:23 +0000419 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000420 std::string typeName(getCppName(Ty));
421
422 // Search the type stack for recursion. If we find it, then generate this
423 // as an OpaqueType, but make sure not to do this multiple times because
424 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000425 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000426 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000427 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
428 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000429 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
430 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000431 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
432 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000433 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000434 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000435 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000436 }
437
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000438 // We're going to print a derived type which, by definition, contains other
439 // types. So, push this one we're printing onto the type stack to assist with
440 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000441 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000442
443 // Print the type definition
444 switch (Ty->getTypeID()) {
445 case Type::FunctionTyID: {
446 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000447 Out << "std::vector<const Type*>" << typeName << "_args;";
448 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000449 FunctionType::param_iterator PI = FT->param_begin();
450 FunctionType::param_iterator PE = FT->param_end();
451 for (; PI != PE; ++PI) {
452 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000453 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000454 std::string argName(getCppName(argTy));
455 Out << typeName << "_args.push_back(" << argName;
456 if (isForward)
457 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000458 Out << ");";
459 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000460 }
Reid Spencer6abd3da2007-04-11 09:54:08 +0000461 const ParamAttrsList *PAL = FT->getParamAttrs();
462 Out << "ParamAttrsList *" << typeName << "_PAL = 0;";
463 if (PAL && !PAL->empty()) {
464 Out << typeName << "_PAL = new ParamAttrsList();";
465 for (unsigned i = 0; i < PAL->size(); ++i) {
466 uint16_t index = PAL->getParamIndex(i);
467 uint16_t attrs = PAL->getParamAttrs(index);
468 Out << typeName << "_PAL->addAttribute(" << index << ", 0";
469 if (attrs & ParamAttr::SExt)
470 Out << " | ParamAttr::SExt";
471 if (attrs & ParamAttr::ZExt)
472 Out << " | ParamAttr::ZExt";
473 if (attrs & ParamAttr::StructRet)
474 Out << " | ParamAttr::StructRet";
475 if (attrs & ParamAttr::InReg)
476 Out << " | ParamAttr::InReg";
477 if (attrs & ParamAttr::NoReturn)
478 Out << " | ParamAttr::NoReturn";
479 if (attrs & ParamAttr::NoUnwind)
480 Out << " | ParamAttr::NoUnwind";
481 Out << ");";
482 }
483 }
Reid Spencer12803f52006-05-31 17:31:38 +0000484 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000485 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000486 Out << "FunctionType* " << typeName << " = FunctionType::get(";
487 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000488 if (isForward)
489 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000490 Out << ",";
491 nl(Out) << "/*Params=*/" << typeName << "_args,";
Reid Spencer6abd3da2007-04-11 09:54:08 +0000492 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") ;
493 nl(Out) << "/*ParamAttrs=/" << typeName << "_PAL" << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000494 out();
495 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000496 break;
497 }
498 case Type::StructTyID: {
499 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000500 Out << "std::vector<const Type*>" << typeName << "_fields;";
501 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000502 StructType::element_iterator EI = ST->element_begin();
503 StructType::element_iterator EE = ST->element_end();
504 for (; EI != EE; ++EI) {
505 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000506 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000507 std::string fieldName(getCppName(fieldTy));
508 Out << typeName << "_fields.push_back(" << fieldName;
509 if (isForward)
510 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000511 Out << ");";
512 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000513 }
514 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000515 << typeName << "_fields);";
516 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000517 break;
518 }
519 case Type::ArrayTyID: {
520 const ArrayType* AT = cast<ArrayType>(Ty);
521 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000522 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000523 std::string elemName(getCppName(ET));
524 Out << "ArrayType* " << typeName << " = ArrayType::get("
525 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000526 << ", " << utostr(AT->getNumElements()) << ");";
527 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000528 break;
529 }
530 case Type::PointerTyID: {
531 const PointerType* PT = cast<PointerType>(Ty);
532 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000533 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000534 std::string elemName(getCppName(ET));
535 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000536 << elemName << (isForward ? "_fwd" : "") << ");";
537 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000538 break;
539 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000540 case Type::VectorTyID: {
541 const VectorType* PT = cast<VectorType>(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000542 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000543 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000544 std::string elemName(getCppName(ET));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000545 Out << "VectorType* " << typeName << " = VectorType::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000546 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000547 << ", " << utostr(PT->getNumElements()) << ");";
548 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000549 break;
550 }
551 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000552 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
553 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000554 break;
555 }
556 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000557 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000558 }
559
Reid Spencer74e032a2006-05-29 02:58:15 +0000560 // If the type had a name, make sure we recreate it.
561 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000562 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer74e032a2006-05-29 02:58:15 +0000563 if (progTypeName)
564 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000565 << typeName << ");";
566 nl(Out);
Reid Spencer74e032a2006-05-29 02:58:15 +0000567
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000568 // Pop us off the type stack
569 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000570
571 // Indicate that this type is now defined.
572 DefinedTypes.insert(Ty);
573
574 // Early resolve as many unresolved types as possible. Search the unresolved
575 // types map for the type we just printed. Now that its definition is complete
576 // we can resolve any previous references to it. This prevents a cascade of
577 // unresolved types.
578 TypeMap::iterator I = UnresolvedTypes.find(Ty);
579 if (I != UnresolvedTypes.end()) {
580 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000581 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
582 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000583 Out << I->second << " = cast<";
584 switch (Ty->getTypeID()) {
585 case Type::FunctionTyID: Out << "FunctionType"; break;
586 case Type::ArrayTyID: Out << "ArrayType"; break;
587 case Type::StructTyID: Out << "StructType"; break;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000588 case Type::VectorTyID: Out << "VectorType"; break;
Reid Spencer15f7e012006-05-30 21:18:23 +0000589 case Type::PointerTyID: Out << "PointerType"; break;
590 case Type::OpaqueTyID: Out << "OpaqueType"; break;
591 default: Out << "NoSuchDerivedType"; break;
592 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000593 Out << ">(" << I->second << "_fwd.get());";
594 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000595 UnresolvedTypes.erase(I);
596 }
597
598 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000599 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000600
601 // We weren't a recursive type
602 return false;
603}
604
Reid Spencer12803f52006-05-31 17:31:38 +0000605// Prints a type definition. Returns true if it could not resolve all the types
606// in the definition but had to use a forward reference.
607void
608CppWriter::printType(const Type* Ty) {
609 assert(TypeStack.empty());
610 TypeStack.clear();
611 printTypeInternal(Ty);
612 assert(TypeStack.empty());
613}
614
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000615void
616CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000617
618 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000619 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
620 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
621 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000622
623 // For primitive types and types already defined, just add a name
624 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Chris Lattner42a75512007-01-15 02:27:26 +0000625 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000626 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000627 Out << "mod->addTypeName(\"";
628 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000629 Out << "\", " << getCppName(TI->second) << ");";
630 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000631 // For everything else, define the type
632 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000633 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000634 }
635 }
636
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000637 // Add all of the global variables to the value table...
638 for (Module::const_global_iterator I = TheModule->global_begin(),
639 E = TheModule->global_end(); I != E; ++I) {
640 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000641 printType(I->getInitializer()->getType());
642 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000643 }
644
645 // Add all the functions to the table
646 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
647 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000648 printType(FI->getReturnType());
649 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000650 // Add all the function arguments
651 for(Function::const_arg_iterator AI = FI->arg_begin(),
652 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000653 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000654 }
655
656 // Add all of the basic blocks and instructions
657 for (Function::const_iterator BB = FI->begin(),
658 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000659 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000660 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
661 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000662 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000663 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000664 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000665 }
666 }
667 }
668}
669
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000670
Reid Spencere0d133f2006-05-29 18:08:06 +0000671// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000672void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000673 // First, if the constant is actually a GlobalValue (variable or function) or
674 // its already in the constant list then we've printed it already and we can
675 // just return.
676 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000677 return;
678
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000679 std::string constName(getCppName(CV));
680 std::string typeName(getCppName(CV->getType()));
681 if (CV->isNullValue()) {
682 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000683 << typeName << ");";
684 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000685 return;
686 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000687 if (isa<GlobalValue>(CV)) {
688 // Skip variables and functions, we emit them elsewhere
689 return;
690 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000691 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnerf6e7bb42007-01-12 18:37:29 +0000692 Out << "ConstantInt* " << constName << " = ConstantInt::get("
Reid Spencer70297252007-03-01 20:55:43 +0000693 << "APInt(cast<IntegerTyp>(" << typeName << ")->getBitWidth(),"
694 << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000695 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000696 Out << "ConstantAggregateZero* " << constName
697 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000698 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000699 Out << "ConstantPointerNull* " << constName
700 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000701 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000702 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000703 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000704 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000705 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000706 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000707 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000708 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000709 // Determine if we want null termination or not.
710 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000711 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000712 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000713 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000714 Out << ");";
715 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000716 Out << "std::vector<Constant*> " << constName << "_elems;";
717 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000718 unsigned N = CA->getNumOperands();
719 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000720 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000721 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000722 << getCppName(CA->getOperand(i)) << ");";
723 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000724 }
725 Out << "Constant* " << constName << " = ConstantArray::get("
726 << typeName << ", " << constName << "_elems);";
727 }
728 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000729 Out << "std::vector<Constant*> " << constName << "_fields;";
730 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000731 unsigned N = CS->getNumOperands();
732 for (unsigned i = 0; i < N; i++) {
733 printConstant(CS->getOperand(i));
734 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000735 << getCppName(CS->getOperand(i)) << ");";
736 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000737 }
738 Out << "Constant* " << constName << " = ConstantStruct::get("
739 << typeName << ", " << constName << "_fields);";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000740 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000741 Out << "std::vector<Constant*> " << constName << "_elems;";
742 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000743 unsigned N = CP->getNumOperands();
744 for (unsigned i = 0; i < N; ++i) {
745 printConstant(CP->getOperand(i));
746 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000747 << getCppName(CP->getOperand(i)) << ");";
748 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000749 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000750 Out << "Constant* " << constName << " = ConstantVector::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000751 << typeName << ", " << constName << "_elems);";
752 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000753 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000754 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000755 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000756 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000757 Out << "std::vector<Constant*> " << constName << "_indices;";
758 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000759 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000760 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000761 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000762 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000763 << getCppName(CE->getOperand(i)) << ");";
764 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000765 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000766 Out << "Constant* " << constName
767 << " = ConstantExpr::getGetElementPtr("
768 << getCppName(CE->getOperand(0)) << ", "
769 << constName << "_indices);";
Reid Spencer3da59db2006-11-27 01:05:10 +0000770 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000771 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000772 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000773 switch (CE->getOpcode()) {
774 default: assert(0 && "Invalid cast opcode");
775 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
776 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
777 case Instruction::SExt: Out << "Instruction::SExt"; break;
778 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
779 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
780 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
781 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
782 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
783 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
784 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
785 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
786 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
787 }
788 Out << ", " << getCppName(CE->getOperand(0)) << ", "
789 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000790 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000791 unsigned N = CE->getNumOperands();
792 for (unsigned i = 0; i < N; ++i ) {
793 printConstant(CE->getOperand(i));
794 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000795 Out << "Constant* " << constName << " = ConstantExpr::";
796 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000797 case Instruction::Add: Out << "getAdd("; break;
798 case Instruction::Sub: Out << "getSub("; break;
799 case Instruction::Mul: Out << "getMul("; break;
800 case Instruction::UDiv: Out << "getUDiv("; break;
801 case Instruction::SDiv: Out << "getSDiv("; break;
802 case Instruction::FDiv: Out << "getFDiv("; break;
803 case Instruction::URem: Out << "getURem("; break;
804 case Instruction::SRem: Out << "getSRem("; break;
805 case Instruction::FRem: Out << "getFRem("; break;
806 case Instruction::And: Out << "getAnd("; break;
807 case Instruction::Or: Out << "getOr("; break;
808 case Instruction::Xor: Out << "getXor("; break;
809 case Instruction::ICmp:
810 Out << "getICmp(ICmpInst::ICMP_";
811 switch (CE->getPredicate()) {
812 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
813 case ICmpInst::ICMP_NE: Out << "NE"; break;
814 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
815 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
816 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
817 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
818 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
819 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
820 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
821 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
822 default: error("Invalid ICmp Predicate");
823 }
824 break;
825 case Instruction::FCmp:
826 Out << "getFCmp(FCmpInst::FCMP_";
827 switch (CE->getPredicate()) {
828 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
829 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
830 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
831 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
832 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
833 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
834 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
835 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
836 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
837 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
838 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
839 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
840 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
841 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
842 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
843 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
844 default: error("Invalid FCmp Predicate");
845 }
846 break;
847 case Instruction::Shl: Out << "getShl("; break;
848 case Instruction::LShr: Out << "getLShr("; break;
849 case Instruction::AShr: Out << "getAShr("; break;
850 case Instruction::Select: Out << "getSelect("; break;
851 case Instruction::ExtractElement: Out << "getExtractElement("; break;
852 case Instruction::InsertElement: Out << "getInsertElement("; break;
853 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000854 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000855 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000856 break;
857 }
858 Out << getCppName(CE->getOperand(0));
859 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
860 Out << ", " << getCppName(CE->getOperand(i));
861 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000862 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000863 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000864 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000865 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000866 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000867 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000868}
869
Reid Spencer12803f52006-05-31 17:31:38 +0000870void
871CppWriter::printConstants(const Module* M) {
872 // Traverse all the global variables looking for constant initializers
873 for (Module::const_global_iterator I = TheModule->global_begin(),
874 E = TheModule->global_end(); I != E; ++I)
875 if (I->hasInitializer())
876 printConstant(I->getInitializer());
877
878 // Traverse the LLVM functions looking for constants
879 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
880 FI != FE; ++FI) {
881 // Add all of the basic blocks and instructions
882 for (Function::const_iterator BB = FI->begin(),
883 E = FI->end(); BB != E; ++BB) {
884 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
885 ++I) {
886 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
887 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
888 printConstant(C);
889 }
890 }
891 }
892 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000893 }
Reid Spencer66c87342006-05-30 03:43:49 +0000894}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000895
Reid Spencer12803f52006-05-31 17:31:38 +0000896void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000897 nl(Out) << "// Type Definitions";
898 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000899 printType(GV->getType());
900 if (GV->hasInitializer()) {
901 Constant* Init = GV->getInitializer();
902 printType(Init->getType());
903 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000904 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000905 printFunctionHead(F);
906 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000907 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000908 printVariableHead(gv);
909 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000910 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000911 printConstant(gv);
912 }
913 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000914 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000915 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000916 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000917 }
Reid Spencer12803f52006-05-31 17:31:38 +0000918}
Reid Spencer15f7e012006-05-30 21:18:23 +0000919
Reid Spencer12803f52006-05-31 17:31:38 +0000920void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000921 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000922 if (is_inline) {
923 Out << " = mod->getGlobalVariable(";
924 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000925 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
926 nl(Out) << "if (!" << getCppName(GV) << ") {";
927 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000928 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000929 Out << " = new GlobalVariable(";
930 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000931 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000932 Out << ",";
933 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
934 Out << ",";
935 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000936 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000937 Out << ",";
938 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000939 if (GV->hasInitializer()) {
940 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000941 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000942 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000943 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000944 Out << "\",";
945 nl(Out) << "mod);";
946 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000947
948 if (GV->hasSection()) {
949 printCppName(GV);
950 Out << "->setSection(\"";
951 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000952 Out << "\");";
953 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000954 }
955 if (GV->getAlignment()) {
956 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000957 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
958 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000959 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000960 if (is_inline) {
961 out(); Out << "}"; nl(Out);
962 }
Reid Spencer12803f52006-05-31 17:31:38 +0000963}
964
965void
966CppWriter::printVariableBody(const GlobalVariable *GV) {
967 if (GV->hasInitializer()) {
968 printCppName(GV);
969 Out << "->setInitializer(";
970 //if (!isa<GlobalValue(GV->getInitializer()))
971 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000972 Out << getCppName(GV->getInitializer()) << ");";
973 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000974 }
975}
976
977std::string
978CppWriter::getOpName(Value* V) {
979 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
980 return getCppName(V);
981
982 // See if its alread in the map of forward references, if so just return the
983 // name we already set up for it
984 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
985 if (I != ForwardRefs.end())
986 return I->second;
987
988 // This is a new forward reference. Generate a unique name for it
989 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
990
991 // Yes, this is a hack. An Argument is the smallest instantiable value that
992 // we can make as a placeholder for the real value. We'll replace these
993 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000994 Out << "Argument* " << result << " = new Argument("
995 << getCppName(V->getType()) << ");";
996 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000997 ForwardRefs[V] = result;
998 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000999}
1000
Reid Spencere0d133f2006-05-29 18:08:06 +00001001// printInstruction - This member is called for each Instruction in a function.
1002void
Reid Spencer12803f52006-05-31 17:31:38 +00001003CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +00001004 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001005
Reid Spencer15f7e012006-05-30 21:18:23 +00001006 // Before we emit this instruction, we need to take care of generating any
1007 // forward references. So, we get the names of all the operands in advance
1008 std::string* opNames = new std::string[I->getNumOperands()];
1009 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1010 opNames[i] = getOpName(I->getOperand(i));
1011 }
1012
Reid Spencere0d133f2006-05-29 18:08:06 +00001013 switch (I->getOpcode()) {
1014 case Instruction::Ret: {
1015 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001016 Out << "ReturnInst* " << iName << " = new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001017 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001018 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001019 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001020 case Instruction::Br: {
1021 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001022 Out << "BranchInst* " << iName << " = new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001023 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001024 Out << opNames[0] << ", "
1025 << opNames[1] << ", "
1026 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001027
Reid Spencere0d133f2006-05-29 18:08:06 +00001028 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001029 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001030 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001031 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001032 }
1033 Out << bbname << ");";
1034 break;
1035 }
Reid Spencer66c87342006-05-30 03:43:49 +00001036 case Instruction::Switch: {
1037 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001038 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001039 << opNames[0] << ", "
1040 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001041 << sw->getNumCases() << ", " << bbname << ");";
1042 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001043 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001044 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001045 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001046 << opNames[i+1] << ");";
1047 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001048 }
1049 break;
1050 }
1051 case Instruction::Invoke: {
1052 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001053 Out << "std::vector<Value*> " << iName << "_params;";
1054 nl(Out);
1055 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1056 Out << iName << "_params.push_back("
1057 << opNames[i] << ");";
1058 nl(Out);
1059 }
1060 Out << "InvokeInst* " << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001061 << opNames[0] << ", "
1062 << opNames[1] << ", "
1063 << opNames[2] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001064 << iName << "_params, \"";
1065 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001066 Out << "\", " << bbname << ");";
1067 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001068 printCallingConv(inv->getCallingConv());
1069 Out << ");";
1070 break;
1071 }
1072 case Instruction::Unwind: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001073 Out << "UnwindInst* " << iName << " = new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001074 << bbname << ");";
1075 break;
1076 }
1077 case Instruction::Unreachable:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001078 Out << "UnreachableInst* " << iName << " = new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001079 << bbname << ");";
1080 break;
1081 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001082 case Instruction::Add:
1083 case Instruction::Sub:
1084 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001085 case Instruction::UDiv:
1086 case Instruction::SDiv:
1087 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001088 case Instruction::URem:
1089 case Instruction::SRem:
1090 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001091 case Instruction::And:
1092 case Instruction::Or:
1093 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001094 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001095 case Instruction::LShr:
1096 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001097 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001098 switch (I->getOpcode()) {
1099 case Instruction::Add: Out << "Instruction::Add"; break;
1100 case Instruction::Sub: Out << "Instruction::Sub"; break;
1101 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001102 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1103 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1104 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001105 case Instruction::URem:Out << "Instruction::URem"; break;
1106 case Instruction::SRem:Out << "Instruction::SRem"; break;
1107 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001108 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001109 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001110 case Instruction::Xor: Out << "Instruction::Xor"; break;
1111 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001112 case Instruction::LShr:Out << "Instruction::LShr"; break;
1113 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001114 default: Out << "Instruction::BadOpCode"; break;
1115 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001116 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001117 printEscapedString(I->getName());
1118 Out << "\", " << bbname << ");";
1119 break;
1120 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001121 case Instruction::FCmp: {
1122 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1123 switch (cast<FCmpInst>(I)->getPredicate()) {
1124 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1125 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1126 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1127 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1128 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1129 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1130 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1131 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1132 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1133 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1134 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1135 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1136 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1137 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1138 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1139 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1140 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1141 }
1142 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1143 printEscapedString(I->getName());
1144 Out << "\", " << bbname << ");";
1145 break;
1146 }
1147 case Instruction::ICmp: {
1148 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1149 switch (cast<ICmpInst>(I)->getPredicate()) {
1150 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1151 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1152 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1153 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1154 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1155 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1156 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1157 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1158 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1159 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1160 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001161 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001162 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001163 printEscapedString(I->getName());
1164 Out << "\", " << bbname << ");";
1165 break;
1166 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001167 case Instruction::Malloc: {
1168 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001169 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001170 << getCppName(mallocI->getAllocatedType()) << ", ";
1171 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001172 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001173 Out << "\"";
1174 printEscapedString(mallocI->getName());
1175 Out << "\", " << bbname << ");";
1176 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001177 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001178 << mallocI->getAlignment() << ");";
1179 break;
1180 }
Reid Spencer66c87342006-05-30 03:43:49 +00001181 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001182 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001183 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1184 break;
1185 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001186 case Instruction::Alloca: {
1187 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001188 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001189 << getCppName(allocaI->getAllocatedType()) << ", ";
1190 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001191 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001192 Out << "\"";
1193 printEscapedString(allocaI->getName());
1194 Out << "\", " << bbname << ");";
1195 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001196 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001197 << allocaI->getAlignment() << ");";
1198 break;
1199 }
Reid Spencer66c87342006-05-30 03:43:49 +00001200 case Instruction::Load:{
1201 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001202 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001203 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001204 printEscapedString(load->getName());
1205 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001206 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001207 break;
1208 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001209 case Instruction::Store: {
1210 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001211 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001212 << opNames[0] << ", "
1213 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001214 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001215 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001216 break;
1217 }
1218 case Instruction::GetElementPtr: {
1219 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1220 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001221 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001222 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001223 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001224 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001225 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001226 Out << "std::vector<Value*> " << iName << "_indices;";
1227 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001228 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001229 Out << iName << "_indices.push_back("
1230 << opNames[i] << ");";
1231 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001232 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001233 Out << "Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001234 << opNames[0] << ", " << iName << "_indices";
Reid Spencere0d133f2006-05-29 18:08:06 +00001235 }
1236 Out << ", \"";
1237 printEscapedString(gep->getName());
1238 Out << "\", " << bbname << ");";
1239 break;
1240 }
Reid Spencer66c87342006-05-30 03:43:49 +00001241 case Instruction::PHI: {
1242 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001243
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001244 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001245 << getCppName(phi->getType()) << ", \"";
1246 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001247 Out << "\", " << bbname << ");";
1248 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001249 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001250 << ");";
1251 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001252 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001253 Out << iName << "->addIncoming("
1254 << opNames[i] << ", " << opNames[i+1] << ");";
1255 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001256 }
Reid Spencer66c87342006-05-30 03:43:49 +00001257 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001258 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001259 case Instruction::Trunc:
1260 case Instruction::ZExt:
1261 case Instruction::SExt:
1262 case Instruction::FPTrunc:
1263 case Instruction::FPExt:
1264 case Instruction::FPToUI:
1265 case Instruction::FPToSI:
1266 case Instruction::UIToFP:
1267 case Instruction::SIToFP:
1268 case Instruction::PtrToInt:
1269 case Instruction::IntToPtr:
1270 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001271 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001272 Out << "CastInst* " << iName << " = new ";
1273 switch (I->getOpcode()) {
Reid Spencer34816572007-02-16 06:34:39 +00001274 case Instruction::Trunc: Out << "TruncInst"; break;
1275 case Instruction::ZExt: Out << "ZExtInst"; break;
1276 case Instruction::SExt: Out << "SExtInst"; break;
1277 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1278 case Instruction::FPExt: Out << "FPExtInst"; break;
1279 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1280 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1281 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1282 case Instruction::SIToFP: Out << "SIToFPInst"; break;
1283 case Instruction::PtrToInt: Out << "PtrToInst"; break;
1284 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1285 case Instruction::BitCast: Out << "BitCastInst"; break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001286 default: assert(!"Unreachable"); break;
1287 }
1288 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001289 << getCppName(cst->getType()) << ", \"";
1290 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001291 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001292 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001293 }
Reid Spencer66c87342006-05-30 03:43:49 +00001294 case Instruction::Call:{
1295 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001296 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001297 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001298 << getCppName(ila->getFunctionType()) << ", \""
1299 << ila->getAsmString() << "\", \""
1300 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001301 << (ila->hasSideEffects() ? "true" : "false") << ");";
1302 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001303 }
Reid Spencer66c87342006-05-30 03:43:49 +00001304 if (call->getNumOperands() > 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001305 Out << "std::vector<Value*> " << iName << "_params;";
1306 nl(Out);
1307 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1308 Out << iName << "_params.push_back(" << opNames[i] << ");";
1309 nl(Out);
1310 }
1311 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001312 << opNames[0] << ", " << iName << "_params, \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001313 } else if (call->getNumOperands() == 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001314 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001315 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001316 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001317 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001318 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001319 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001320 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001321 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001322 }
1323 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001324 Out << "\", " << bbname << ");";
1325 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001326 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001327 Out << ");";
1328 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001329 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001330 Out << ");";
1331 break;
1332 }
1333 case Instruction::Select: {
1334 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001335 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001336 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001337 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001338 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001339 break;
1340 }
1341 case Instruction::UserOp1:
1342 /// FALL THROUGH
1343 case Instruction::UserOp2: {
1344 /// FIXME: What should be done here?
1345 break;
1346 }
1347 case Instruction::VAArg: {
1348 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001349 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001350 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001351 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001352 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001353 break;
1354 }
1355 case Instruction::ExtractElement: {
1356 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001357 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001358 << " = new ExtractElementInst(" << opNames[0]
1359 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001360 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001361 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001362 break;
1363 }
1364 case Instruction::InsertElement: {
1365 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001366 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001367 << " = new InsertElementInst(" << opNames[0]
1368 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001369 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001370 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001371 break;
1372 }
1373 case Instruction::ShuffleVector: {
1374 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001375 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001376 << " = new ShuffleVectorInst(" << opNames[0]
1377 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001378 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001379 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001380 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001381 }
1382 }
Reid Spencer68464b72006-06-15 16:09:59 +00001383 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001384 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001385 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001386}
1387
Reid Spencer12803f52006-05-31 17:31:38 +00001388// Print out the types, constants and declarations needed by one function
1389void CppWriter::printFunctionUses(const Function* F) {
1390
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001391 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001392 if (!is_inline) {
1393 // Print the function's return type
1394 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001395
Reid Spencerf977e7b2006-06-01 23:43:47 +00001396 // Print the function's function type
1397 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001398
Reid Spencerf977e7b2006-06-01 23:43:47 +00001399 // Print the types of each of the function's arguments
1400 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1401 AI != AE; ++AI) {
1402 printType(AI->getType());
1403 }
Reid Spencer12803f52006-05-31 17:31:38 +00001404 }
1405
1406 // Print type definitions for every type referenced by an instruction and
1407 // make a note of any global values or constants that are referenced
1408 std::vector<GlobalValue*> gvs;
1409 std::vector<Constant*> consts;
1410 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1411 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1412 I != E; ++I) {
1413 // Print the type of the instruction itself
1414 printType(I->getType());
1415
1416 // Print the type of each of the instruction's operands
1417 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1418 Value* operand = I->getOperand(i);
1419 printType(operand->getType());
1420 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1421 gvs.push_back(GV);
1422 else if (Constant* C = dyn_cast<Constant>(operand))
1423 consts.push_back(C);
1424 }
1425 }
1426 }
1427
1428 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001429 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001430 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1431 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001432 if (Function* Fun = dyn_cast<Function>(*I)) {
1433 if (!is_inline || Fun != F)
1434 printFunctionHead(Fun);
1435 }
Reid Spencer12803f52006-05-31 17:31:38 +00001436 }
1437
1438 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001439 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001440 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1441 I != E; ++I) {
1442 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1443 printVariableHead(F);
1444 }
1445
1446 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001447 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001448 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1449 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001450 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001451 }
1452
1453 // Process the global variables definitions now that all the constants have
1454 // been emitted. These definitions just couple the gvars with their constant
1455 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001456 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001457 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1458 I != E; ++I) {
1459 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1460 printVariableBody(GV);
1461 }
1462}
1463
1464void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001465 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001466 if (is_inline) {
1467 Out << " = mod->getFunction(\"";
1468 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001469 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1470 nl(Out) << "if (!" << getCppName(F) << ") {";
1471 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001472 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001473 Out<< " = new Function(";
1474 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1475 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001476 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001477 Out << ",";
1478 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001479 printEscapedString(F->getName());
Reid Spencer5cbf9852007-01-30 20:08:39 +00001480 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001481 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001482 printCppName(F);
1483 Out << "->setCallingConv(";
1484 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001485 Out << ");";
1486 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001487 if (F->hasSection()) {
1488 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001489 Out << "->setSection(\"" << F->getSection() << "\");";
1490 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001491 }
1492 if (F->getAlignment()) {
1493 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001494 Out << "->setAlignment(" << F->getAlignment() << ");";
1495 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001496 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001497 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001498 Out << "}";
1499 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001500 }
Reid Spencer12803f52006-05-31 17:31:38 +00001501}
1502
1503void CppWriter::printFunctionBody(const Function *F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001504 if (F->isDeclaration())
Reid Spencer12803f52006-05-31 17:31:38 +00001505 return; // external functions have no bodies.
1506
1507 // Clear the DefinedValues and ForwardRefs maps because we can't have
1508 // cross-function forward refs
1509 ForwardRefs.clear();
1510 DefinedValues.clear();
1511
1512 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001513 if (!is_inline) {
1514 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001515 Out << "Function::arg_iterator args = " << getCppName(F)
1516 << "->arg_begin();";
1517 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001518 }
1519 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1520 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001521 Out << "Value* " << getCppName(AI) << " = args++;";
1522 nl(Out);
1523 if (AI->hasName()) {
1524 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1525 nl(Out);
1526 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001527 }
Reid Spencer12803f52006-05-31 17:31:38 +00001528 }
1529
1530 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001531 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001532 for (Function::const_iterator BI = F->begin(), BE = F->end();
1533 BI != BE; ++BI) {
1534 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001535 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001536 if (BI->hasName())
1537 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001538 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1539 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001540 }
1541
1542 // Output all of its basic blocks... for the function
1543 for (Function::const_iterator BI = F->begin(), BE = F->end();
1544 BI != BE; ++BI) {
1545 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001546 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1547 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001548
1549 // Output all of the instructions in the basic block...
1550 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1551 I != E; ++I) {
1552 printInstruction(I,bbname);
1553 }
1554 }
1555
1556 // Loop over the ForwardRefs and resolve them now that all instructions
1557 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001558 if (!ForwardRefs.empty()) {
1559 nl(Out) << "// Resolve Forward References";
1560 nl(Out);
1561 }
1562
Reid Spencer12803f52006-05-31 17:31:38 +00001563 while (!ForwardRefs.empty()) {
1564 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001565 Out << I->second << "->replaceAllUsesWith("
1566 << getCppName(I->first) << "); delete " << I->second << ";";
1567 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001568 ForwardRefs.erase(I);
1569 }
1570}
1571
Reid Spencerf977e7b2006-06-01 23:43:47 +00001572void CppWriter::printInline(const std::string& fname, const std::string& func) {
Reid Spencer688b0492007-02-05 21:19:13 +00001573 const Function* F = TheModule->getFunction(func);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001574 if (!F) {
1575 error(std::string("Function '") + func + "' not found in input module");
1576 return;
1577 }
Reid Spencer5cbf9852007-01-30 20:08:39 +00001578 if (F->isDeclaration()) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001579 error(std::string("Function '") + func + "' is external!");
1580 return;
1581 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001582 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001583 << getCppName(F);
1584 unsigned arg_count = 1;
1585 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1586 AI != AE; ++AI) {
1587 Out << ", Value* arg_" << arg_count;
1588 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001589 Out << ") {";
1590 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001591 is_inline = true;
1592 printFunctionUses(F);
1593 printFunctionBody(F);
1594 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001595 Out << "return " << getCppName(F->begin()) << ";";
1596 nl(Out) << "}";
1597 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001598}
1599
Reid Spencer12803f52006-05-31 17:31:38 +00001600void CppWriter::printModuleBody() {
1601 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001602 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001603 printTypes(TheModule);
1604
1605 // Functions can call each other and global variables can reference them so
1606 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001607 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001608 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1609 I != E; ++I)
1610 printFunctionHead(I);
1611
1612 // Process the global variables declarations. We can't initialze them until
1613 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001614 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001615 for (Module::const_global_iterator I = TheModule->global_begin(),
1616 E = TheModule->global_end(); I != E; ++I) {
1617 printVariableHead(I);
1618 }
1619
1620 // Print out all the constants definitions. Constants don't recurse except
1621 // through GlobalValues. All GlobalValues have been declared at this point
1622 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001623 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001624 printConstants(TheModule);
1625
1626 // Process the global variables definitions now that all the constants have
1627 // been emitted. These definitions just couple the gvars with their constant
1628 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001629 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001630 for (Module::const_global_iterator I = TheModule->global_begin(),
1631 E = TheModule->global_end(); I != E; ++I) {
1632 printVariableBody(I);
1633 }
1634
1635 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001636 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001637 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1638 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001639 if (!I->isDeclaration()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001640 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1641 << ")";
1642 nl(Out) << "{";
1643 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001644 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001645 nl(Out,-1) << "}";
1646 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001647 }
1648 }
1649}
1650
1651void CppWriter::printProgram(
1652 const std::string& fname,
1653 const std::string& mName
1654) {
1655 Out << "#include <llvm/Module.h>\n";
1656 Out << "#include <llvm/DerivedTypes.h>\n";
1657 Out << "#include <llvm/Constants.h>\n";
1658 Out << "#include <llvm/GlobalVariable.h>\n";
1659 Out << "#include <llvm/Function.h>\n";
1660 Out << "#include <llvm/CallingConv.h>\n";
1661 Out << "#include <llvm/BasicBlock.h>\n";
1662 Out << "#include <llvm/Instructions.h>\n";
1663 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001664 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001665 Out << "#include <llvm/Pass.h>\n";
1666 Out << "#include <llvm/PassManager.h>\n";
1667 Out << "#include <llvm/Analysis/Verifier.h>\n";
1668 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1669 Out << "#include <algorithm>\n";
1670 Out << "#include <iostream>\n\n";
1671 Out << "using namespace llvm;\n\n";
1672 Out << "Module* " << fname << "();\n\n";
1673 Out << "int main(int argc, char**argv) {\n";
1674 Out << " Module* Mod = makeLLVMModule();\n";
1675 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1676 Out << " std::cerr.flush();\n";
1677 Out << " std::cout.flush();\n";
1678 Out << " PassManager PM;\n";
1679 Out << " PM.add(new PrintModulePass(&std::cout));\n";
1680 Out << " PM.run(*Mod);\n";
1681 Out << " return 0;\n";
1682 Out << "}\n\n";
1683 printModule(fname,mName);
1684}
1685
1686void CppWriter::printModule(
1687 const std::string& fname,
1688 const std::string& mName
1689) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001690 nl(Out) << "Module* " << fname << "() {";
1691 nl(Out,1) << "// Module Construction";
1692 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1693 nl(Out) << "mod->setEndianness(";
Reid Spencer12803f52006-05-31 17:31:38 +00001694 switch (TheModule->getEndianness()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001695 case Module::LittleEndian: Out << "Module::LittleEndian);"; break;
1696 case Module::BigEndian: Out << "Module::BigEndian);"; break;
1697 case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001698 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001699 nl(Out) << "mod->setPointerSize(";
Reid Spencer12803f52006-05-31 17:31:38 +00001700 switch (TheModule->getPointerSize()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001701 case Module::Pointer32: Out << "Module::Pointer32);"; break;
1702 case Module::Pointer64: Out << "Module::Pointer64);"; break;
1703 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001704 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001705 nl(Out);
1706 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer12803f52006-05-31 17:31:38 +00001707 Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001708 << "\");";
1709 nl(Out);
1710 }
Reid Spencer12803f52006-05-31 17:31:38 +00001711
1712 if (!TheModule->getModuleInlineAsm().empty()) {
1713 Out << "mod->setModuleInlineAsm(\"";
1714 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001715 Out << "\");";
1716 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001717 }
1718
1719 // Loop over the dependent libraries and emit them.
1720 Module::lib_iterator LI = TheModule->lib_begin();
1721 Module::lib_iterator LE = TheModule->lib_end();
1722 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001723 Out << "mod->addLibrary(\"" << *LI << "\");";
1724 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001725 ++LI;
1726 }
1727 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001728 nl(Out) << "return mod;";
1729 nl(Out,-1) << "}";
1730 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001731}
1732
1733void CppWriter::printContents(
1734 const std::string& fname, // Name of generated function
1735 const std::string& mName // Name of module generated module
1736) {
1737 Out << "\nModule* " << fname << "(Module *mod) {\n";
1738 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001739 printModuleBody();
1740 Out << "\nreturn mod;\n";
1741 Out << "\n}\n";
1742}
1743
1744void CppWriter::printFunction(
1745 const std::string& fname, // Name of generated function
1746 const std::string& funcName // Name of function to generate
1747) {
Reid Spencer688b0492007-02-05 21:19:13 +00001748 const Function* F = TheModule->getFunction(funcName);
Reid Spencer12803f52006-05-31 17:31:38 +00001749 if (!F) {
1750 error(std::string("Function '") + funcName + "' not found in input module");
1751 return;
1752 }
1753 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1754 printFunctionUses(F);
1755 printFunctionHead(F);
1756 printFunctionBody(F);
1757 Out << "return " << getCppName(F) << ";\n";
1758 Out << "}\n";
1759}
1760
1761void CppWriter::printVariable(
1762 const std::string& fname, /// Name of generated function
1763 const std::string& varName // Name of variable to generate
1764) {
1765 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1766
1767 if (!GV) {
1768 error(std::string("Variable '") + varName + "' not found in input module");
1769 return;
1770 }
1771 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1772 printVariableUses(GV);
1773 printVariableHead(GV);
1774 printVariableBody(GV);
1775 Out << "return " << getCppName(GV) << ";\n";
1776 Out << "}\n";
1777}
1778
1779void CppWriter::printType(
1780 const std::string& fname, /// Name of generated function
1781 const std::string& typeName // Name of type to generate
1782) {
1783 const Type* Ty = TheModule->getTypeByName(typeName);
1784 if (!Ty) {
1785 error(std::string("Type '") + typeName + "' not found in input module");
1786 return;
1787 }
1788 Out << "\nType* " << fname << "(Module *mod) {\n";
1789 printType(Ty);
1790 Out << "return " << getCppName(Ty) << ";\n";
1791 Out << "}\n";
1792}
1793
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001794} // end anonymous llvm
1795
1796namespace llvm {
1797
1798void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001799 // Initialize a CppWriter for us to use
1800 CppWriter W(o, mod);
1801
1802 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001803 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001804
1805 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001806 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001807
1808 // Get the name of the thing we are to generate
1809 std::string tgtname = NameToGenerate.getValue();
1810 if (GenerationType == GenModule ||
1811 GenerationType == GenContents ||
1812 GenerationType == GenProgram) {
1813 if (tgtname == "!bad!") {
1814 if (mod->getModuleIdentifier() == "-")
1815 tgtname = "<stdin>";
1816 else
1817 tgtname = mod->getModuleIdentifier();
1818 }
1819 } else if (tgtname == "!bad!") {
1820 W.error("You must use the -for option with -gen-{function,variable,type}");
1821 }
1822
1823 switch (WhatToGenerate(GenerationType)) {
1824 case GenProgram:
1825 if (fname.empty())
1826 fname = "makeLLVMModule";
1827 W.printProgram(fname,tgtname);
1828 break;
1829 case GenModule:
1830 if (fname.empty())
1831 fname = "makeLLVMModule";
1832 W.printModule(fname,tgtname);
1833 break;
1834 case GenContents:
1835 if (fname.empty())
1836 fname = "makeLLVMModuleContents";
1837 W.printContents(fname,tgtname);
1838 break;
1839 case GenFunction:
1840 if (fname.empty())
1841 fname = "makeLLVMFunction";
1842 W.printFunction(fname,tgtname);
1843 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001844 case GenInline:
1845 if (fname.empty())
1846 fname = "makeLLVMInline";
1847 W.printInline(fname,tgtname);
1848 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001849 case GenVariable:
1850 if (fname.empty())
1851 fname = "makeLLVMVariable";
1852 W.printVariable(fname,tgtname);
1853 break;
1854 case GenType:
1855 if (fname.empty())
1856 fname = "makeLLVMType";
1857 W.printType(fname,tgtname);
1858 break;
1859 default:
1860 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001861 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001862}
1863
1864}