blob: 70f5626af07d503f11c38028a4f4c8dc8fad4911 [file] [log] [blame]
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001//===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Reid Spencere0d133f2006-05-29 18:08:06 +00005// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the writing of the LLVM IR as a set of C++ calls to the
11// LLVM IR interface. The input module is assumed to be verified.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CallingConv.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/InlineAsm.h"
19#include "llvm/Instruction.h"
20#include "llvm/Instructions.h"
21#include "llvm/Module.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000022#include "llvm/TypeSymbolTable.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000023#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/STLExtras.h"
Reid Spencer15f7e012006-05-30 21:18:23 +000025#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000026#include "llvm/Support/CFG.h"
27#include "llvm/Support/ManagedStatic.h"
28#include "llvm/Support/MathExtras.h"
Reid Spencerf977e7b2006-06-01 23:43:47 +000029#include "llvm/Config/config.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000030#include <algorithm>
31#include <iostream>
Reid Spencer66c87342006-05-30 03:43:49 +000032#include <set>
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000033
34using namespace llvm;
35
Reid Spencer15f7e012006-05-30 21:18:23 +000036static cl::opt<std::string>
Reid Spencer15f7e012006-05-30 21:18:23 +000037FuncName("funcname", cl::desc("Specify the name of the generated function"),
38 cl::value_desc("function name"));
39
Reid Spencer12803f52006-05-31 17:31:38 +000040enum WhatToGenerate {
41 GenProgram,
42 GenModule,
43 GenContents,
44 GenFunction,
Reid Spencerf977e7b2006-06-01 23:43:47 +000045 GenInline,
Reid Spencer12803f52006-05-31 17:31:38 +000046 GenVariable,
47 GenType
48};
49
50static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
51 cl::desc("Choose what kind of output to generate"),
52 cl::init(GenProgram),
53 cl::values(
54 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
55 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
56 clEnumValN(GenContents,"gen-contents", "Generate contents of a module"),
57 clEnumValN(GenFunction,"gen-function", "Generate a function definition"),
Reid Spencerf977e7b2006-06-01 23:43:47 +000058 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
Reid Spencer12803f52006-05-31 17:31:38 +000059 clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"),
60 clEnumValN(GenType, "gen-type", "Generate a type definition"),
61 clEnumValEnd
62 )
63);
64
65static cl::opt<std::string> NameToGenerate("for", cl::Optional,
66 cl::desc("Specify the name of the thing to generate"),
67 cl::init("!bad!"));
Reid Spencer15f7e012006-05-30 21:18:23 +000068
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000069namespace {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000070typedef std::vector<const Type*> TypeList;
71typedef std::map<const Type*,std::string> TypeMap;
72typedef std::map<const Value*,std::string> ValueMap;
Reid Spencer66c87342006-05-30 03:43:49 +000073typedef std::set<std::string> NameSet;
Reid Spencer15f7e012006-05-30 21:18:23 +000074typedef std::set<const Type*> TypeSet;
75typedef std::set<const Value*> ValueSet;
76typedef std::map<const Value*,std::string> ForwardRefMap;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000077
Reid Spencere0d133f2006-05-29 18:08:06 +000078class CppWriter {
Reid Spencer12803f52006-05-31 17:31:38 +000079 const char* progname;
Reid Spencere0d133f2006-05-29 18:08:06 +000080 std::ostream &Out;
81 const Module *TheModule;
Andrew Lenharth539d8712006-05-31 20:18:56 +000082 uint64_t uniqueNum;
Reid Spencere0d133f2006-05-29 18:08:06 +000083 TypeMap TypeNames;
84 ValueMap ValueNames;
85 TypeMap UnresolvedTypes;
86 TypeList TypeStack;
Reid Spencer66c87342006-05-30 03:43:49 +000087 NameSet UsedNames;
Reid Spencer15f7e012006-05-30 21:18:23 +000088 TypeSet DefinedTypes;
89 ValueSet DefinedValues;
90 ForwardRefMap ForwardRefs;
Reid Spencerf977e7b2006-06-01 23:43:47 +000091 bool is_inline;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000092
Reid Spencere0d133f2006-05-29 18:08:06 +000093public:
Reid Spencer12803f52006-05-31 17:31:38 +000094 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
95 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
Reid Spencerf977e7b2006-06-01 23:43:47 +000096 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000097
Reid Spencere0d133f2006-05-29 18:08:06 +000098 const Module* getModule() { return TheModule; }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000099
Reid Spencer12803f52006-05-31 17:31:38 +0000100 void printProgram(const std::string& fname, const std::string& modName );
101 void printModule(const std::string& fname, const std::string& modName );
102 void printContents(const std::string& fname, const std::string& modName );
103 void printFunction(const std::string& fname, const std::string& funcName );
Reid Spencerf977e7b2006-06-01 23:43:47 +0000104 void printInline(const std::string& fname, const std::string& funcName );
Reid Spencer12803f52006-05-31 17:31:38 +0000105 void printVariable(const std::string& fname, const std::string& varName );
106 void printType(const std::string& fname, const std::string& typeName );
107
108 void error(const std::string& msg);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000109
Reid Spencere0d133f2006-05-29 18:08:06 +0000110private:
Reid Spencer12803f52006-05-31 17:31:38 +0000111 void printLinkageType(GlobalValue::LinkageTypes LT);
112 void printCallingConv(unsigned cc);
113 void printEscapedString(const std::string& str);
114 void printCFP(const ConstantFP* CFP);
115
116 std::string getCppName(const Type* val);
117 inline void printCppName(const Type* val);
118
119 std::string getCppName(const Value* val);
120 inline void printCppName(const Value* val);
121
122 bool printTypeInternal(const Type* Ty);
123 inline void printType(const Type* Ty);
Reid Spencere0d133f2006-05-29 18:08:06 +0000124 void printTypes(const Module* M);
Reid Spencer12803f52006-05-31 17:31:38 +0000125
Reid Spencere0d133f2006-05-29 18:08:06 +0000126 void printConstant(const Constant *CPV);
Reid Spencer12803f52006-05-31 17:31:38 +0000127 void printConstants(const Module* M);
128
129 void printVariableUses(const GlobalVariable *GV);
130 void printVariableHead(const GlobalVariable *GV);
131 void printVariableBody(const GlobalVariable *GV);
132
133 void printFunctionUses(const Function *F);
Reid Spencer66c87342006-05-30 03:43:49 +0000134 void printFunctionHead(const Function *F);
135 void printFunctionBody(const Function *F);
Reid Spencere0d133f2006-05-29 18:08:06 +0000136 void printInstruction(const Instruction *I, const std::string& bbname);
Reid Spencer15f7e012006-05-30 21:18:23 +0000137 std::string getOpName(Value*);
138
Reid Spencer12803f52006-05-31 17:31:38 +0000139 void printModuleBody();
140
Reid Spencere0d133f2006-05-29 18:08:06 +0000141};
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000142
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000143static unsigned indent_level = 0;
144inline std::ostream& nl(std::ostream& Out, int delta = 0) {
145 Out << "\n";
146 if (delta >= 0 || indent_level >= unsigned(-delta))
147 indent_level += delta;
148 for (unsigned i = 0; i < indent_level; ++i)
149 Out << " ";
150 return Out;
151}
152
153inline void in() { indent_level++; }
154inline void out() { if (indent_level >0) indent_level--; }
155
Reid Spencer12803f52006-05-31 17:31:38 +0000156inline void
157sanitize(std::string& str) {
158 for (size_t i = 0; i < str.length(); ++i)
159 if (!isalnum(str[i]) && str[i] != '_')
160 str[i] = '_';
161}
162
Reid Spencera54b7cb2007-01-12 07:05:14 +0000163inline std::string
Reid Spencer12803f52006-05-31 17:31:38 +0000164getTypePrefix(const Type* Ty ) {
Reid Spencer12803f52006-05-31 17:31:38 +0000165 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000166 case Type::VoidTyID: return "void_";
167 case Type::IntegerTyID:
168 return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
169 "_";
170 case Type::FloatTyID: return "float_";
171 case Type::DoubleTyID: return "double_";
172 case Type::LabelTyID: return "label_";
173 case Type::FunctionTyID: return "func_";
174 case Type::StructTyID: return "struct_";
175 case Type::ArrayTyID: return "array_";
176 case Type::PointerTyID: return "ptr_";
177 case Type::PackedTyID: return "packed_";
178 case Type::OpaqueTyID: return "opaque_";
179 default: return "other_";
Reid Spencer12803f52006-05-31 17:31:38 +0000180 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000181 return "unknown_";
Reid Spencer12803f52006-05-31 17:31:38 +0000182}
183
184// Looks up the type in the symbol table and returns a pointer to its name or
185// a null pointer if it wasn't found. Note that this isn't the same as the
186// Mode::getTypeName function which will return an empty string, not a null
187// pointer if the name is not found.
188inline const std::string*
Reid Spencer78d033e2007-01-06 07:24:44 +0000189findTypeName(const TypeSymbolTable& ST, const Type* Ty)
Reid Spencer12803f52006-05-31 17:31:38 +0000190{
Reid Spencer78d033e2007-01-06 07:24:44 +0000191 TypeSymbolTable::const_iterator TI = ST.begin();
192 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencer12803f52006-05-31 17:31:38 +0000193 for (;TI != TE; ++TI)
194 if (TI->second == Ty)
195 return &(TI->first);
196 return 0;
197}
198
199void
200CppWriter::error(const std::string& msg) {
201 std::cerr << progname << ": " << msg << "\n";
202 exit(2);
203}
204
Reid Spencer15f7e012006-05-30 21:18:23 +0000205// printCFP - Print a floating point constant .. very carefully :)
206// This makes sure that conversion to/from floating yields the same binary
207// result so that we don't lose precision.
208void
209CppWriter::printCFP(const ConstantFP *CFP) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000210 Out << "ConstantFP::get(";
211 if (CFP->getType() == Type::DoubleTy)
212 Out << "Type::DoubleTy, ";
213 else
214 Out << "Type::FloatTy, ";
Reid Spencer15f7e012006-05-30 21:18:23 +0000215#if HAVE_PRINTF_A
216 char Buffer[100];
217 sprintf(Buffer, "%A", CFP->getValue());
218 if ((!strncmp(Buffer, "0x", 2) ||
219 !strncmp(Buffer, "-0x", 3) ||
220 !strncmp(Buffer, "+0x", 3)) &&
221 (atof(Buffer) == CFP->getValue()))
Reid Spencerf977e7b2006-06-01 23:43:47 +0000222 if (CFP->getType() == Type::DoubleTy)
223 Out << "BitsToDouble(" << Buffer << ")";
224 else
225 Out << "BitsToFloat(" << Buffer << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000226 else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000227#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000228 std::string StrVal = ftostr(CFP->getValue());
229
230 while (StrVal[0] == ' ')
231 StrVal.erase(StrVal.begin());
232
233 // Check to make sure that the stringized number is not some string like
234 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
235 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
236 ((StrVal[0] == '-' || StrVal[0] == '+') &&
237 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
238 (atof(StrVal.c_str()) == CFP->getValue()))
239 if (CFP->getType() == Type::DoubleTy)
240 Out << StrVal;
241 else
242 Out << StrVal;
243 else if (CFP->getType() == Type::DoubleTy)
244 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
245 << std::dec << "ULL) /* " << StrVal << " */";
246 else
247 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
248 << std::dec << "U) /* " << StrVal << " */";
Reid Spencer15f7e012006-05-30 21:18:23 +0000249#if HAVE_PRINTF_A
250 }
251#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000252 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000253}
254
Reid Spencer12803f52006-05-31 17:31:38 +0000255void
256CppWriter::printCallingConv(unsigned cc){
257 // Print the calling convention.
258 switch (cc) {
259 case CallingConv::C: Out << "CallingConv::C"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000260 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
261 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
262 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
263 default: Out << cc; break;
264 }
265}
Reid Spencer15f7e012006-05-30 21:18:23 +0000266
Reid Spencer12803f52006-05-31 17:31:38 +0000267void
268CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
269 switch (LT) {
270 case GlobalValue::InternalLinkage:
271 Out << "GlobalValue::InternalLinkage"; break;
272 case GlobalValue::LinkOnceLinkage:
273 Out << "GlobalValue::LinkOnceLinkage "; break;
274 case GlobalValue::WeakLinkage:
275 Out << "GlobalValue::WeakLinkage"; break;
276 case GlobalValue::AppendingLinkage:
277 Out << "GlobalValue::AppendingLinkage"; break;
278 case GlobalValue::ExternalLinkage:
279 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000280 case GlobalValue::DLLImportLinkage:
281 Out << "GlobalValue::DllImportLinkage"; break;
282 case GlobalValue::DLLExportLinkage:
283 Out << "GlobalValue::DllExportLinkage"; break;
284 case GlobalValue::ExternalWeakLinkage:
285 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000286 case GlobalValue::GhostLinkage:
287 Out << "GlobalValue::GhostLinkage"; break;
288 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000289}
290
Reid Spencere0d133f2006-05-29 18:08:06 +0000291// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000292// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000293void
294CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000295 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
296 unsigned char C = Str[i];
297 if (isprint(C) && C != '"' && C != '\\') {
298 Out << C;
299 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000300 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000301 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
302 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
303 }
304 }
305}
306
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000307std::string
308CppWriter::getCppName(const Type* Ty)
309{
310 // First, handle the primitive types .. easy
Chris Lattner42a75512007-01-15 02:27:26 +0000311 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000312 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000313 case Type::VoidTyID: return "Type::VoidTy";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000314 case Type::IntegerTyID: {
315 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
316 return "IntegerType::get(" + utostr(BitWidth) + ")";
317 }
Reid Spencer71d2ec92006-12-31 06:02:26 +0000318 case Type::FloatTyID: return "Type::FloatTy";
319 case Type::DoubleTyID: return "Type::DoubleTy";
320 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000321 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000322 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000323 break;
324 }
325 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
326 }
327
328 // Now, see if we've seen the type before and return that
329 TypeMap::iterator I = TypeNames.find(Ty);
330 if (I != TypeNames.end())
331 return I->second;
332
333 // Okay, let's build a new name for this type. Start with a prefix
334 const char* prefix = 0;
335 switch (Ty->getTypeID()) {
336 case Type::FunctionTyID: prefix = "FuncTy_"; break;
337 case Type::StructTyID: prefix = "StructTy_"; break;
338 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
339 case Type::PointerTyID: prefix = "PointerTy_"; break;
340 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
341 case Type::PackedTyID: prefix = "PackedTy_"; break;
342 default: prefix = "OtherTy_"; break; // prevent breakage
343 }
344
345 // See if the type has a name in the symboltable and build accordingly
Reid Spencer78d033e2007-01-06 07:24:44 +0000346 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000347 std::string name;
348 if (tName)
349 name = std::string(prefix) + *tName;
350 else
351 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000352 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000353
354 // Save the name
355 return TypeNames[Ty] = name;
356}
357
Reid Spencer12803f52006-05-31 17:31:38 +0000358void
359CppWriter::printCppName(const Type* Ty)
360{
361 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000362}
363
Reid Spencer12803f52006-05-31 17:31:38 +0000364std::string
365CppWriter::getCppName(const Value* val) {
366 std::string name;
367 ValueMap::iterator I = ValueNames.find(val);
368 if (I != ValueNames.end() && I->first == val)
369 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000370
Reid Spencer12803f52006-05-31 17:31:38 +0000371 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
372 name = std::string("gvar_") +
373 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000374 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000375 name = std::string("func_");
376 } else if (const Constant* C = dyn_cast<Constant>(val)) {
377 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000378 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
379 if (is_inline) {
380 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
381 Function::const_arg_iterator(Arg)) + 1;
382 name = std::string("arg_") + utostr(argNum);
383 NameSet::iterator NI = UsedNames.find(name);
384 if (NI != UsedNames.end())
385 name += std::string("_") + utostr(uniqueNum++);
386 UsedNames.insert(name);
387 return ValueNames[val] = name;
388 } else {
389 name = getTypePrefix(val->getType());
390 }
Reid Spencer12803f52006-05-31 17:31:38 +0000391 } else {
392 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000393 }
Reid Spencer12803f52006-05-31 17:31:38 +0000394 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
395 sanitize(name);
396 NameSet::iterator NI = UsedNames.find(name);
397 if (NI != UsedNames.end())
398 name += std::string("_") + utostr(uniqueNum++);
399 UsedNames.insert(name);
400 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000401}
402
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000403void
Reid Spencer12803f52006-05-31 17:31:38 +0000404CppWriter::printCppName(const Value* val) {
405 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000406}
407
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000408bool
Reid Spencer12803f52006-05-31 17:31:38 +0000409CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000410 // We don't print definitions for primitive types
Chris Lattner42a75512007-01-15 02:27:26 +0000411 if (Ty->isPrimitiveType() || Ty->isInteger())
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000412 return false;
413
Reid Spencer15f7e012006-05-30 21:18:23 +0000414 // If we already defined this type, we don't need to define it again.
415 if (DefinedTypes.find(Ty) != DefinedTypes.end())
416 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000417
Reid Spencer15f7e012006-05-30 21:18:23 +0000418 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000419 std::string typeName(getCppName(Ty));
420
421 // Search the type stack for recursion. If we find it, then generate this
422 // as an OpaqueType, but make sure not to do this multiple times because
423 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000424 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000425 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000426 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
427 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000428 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
429 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000430 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
431 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000432 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000433 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000434 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000435 }
436
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000437 // We're going to print a derived type which, by definition, contains other
438 // types. So, push this one we're printing onto the type stack to assist with
439 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000440 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000441
442 // Print the type definition
443 switch (Ty->getTypeID()) {
444 case Type::FunctionTyID: {
445 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000446 Out << "std::vector<const Type*>" << typeName << "_args;";
447 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000448 FunctionType::param_iterator PI = FT->param_begin();
449 FunctionType::param_iterator PE = FT->param_end();
450 for (; PI != PE; ++PI) {
451 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000452 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000453 std::string argName(getCppName(argTy));
454 Out << typeName << "_args.push_back(" << argName;
455 if (isForward)
456 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000457 Out << ");";
458 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000459 }
Reid Spencer12803f52006-05-31 17:31:38 +0000460 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000461 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000462 Out << "FunctionType* " << typeName << " = FunctionType::get(";
463 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000464 if (isForward)
465 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000466 Out << ",";
467 nl(Out) << "/*Params=*/" << typeName << "_args,";
468 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
469 out();
470 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000471 break;
472 }
473 case Type::StructTyID: {
474 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000475 Out << "std::vector<const Type*>" << typeName << "_fields;";
476 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000477 StructType::element_iterator EI = ST->element_begin();
478 StructType::element_iterator EE = ST->element_end();
479 for (; EI != EE; ++EI) {
480 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000481 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000482 std::string fieldName(getCppName(fieldTy));
483 Out << typeName << "_fields.push_back(" << fieldName;
484 if (isForward)
485 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000486 Out << ");";
487 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000488 }
489 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000490 << typeName << "_fields);";
491 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000492 break;
493 }
494 case Type::ArrayTyID: {
495 const ArrayType* AT = cast<ArrayType>(Ty);
496 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000497 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000498 std::string elemName(getCppName(ET));
499 Out << "ArrayType* " << typeName << " = ArrayType::get("
500 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000501 << ", " << utostr(AT->getNumElements()) << ");";
502 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000503 break;
504 }
505 case Type::PointerTyID: {
506 const PointerType* PT = cast<PointerType>(Ty);
507 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000508 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000509 std::string elemName(getCppName(ET));
510 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000511 << elemName << (isForward ? "_fwd" : "") << ");";
512 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000513 break;
514 }
515 case Type::PackedTyID: {
516 const PackedType* PT = cast<PackedType>(Ty);
517 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000518 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000519 std::string elemName(getCppName(ET));
520 Out << "PackedType* " << typeName << " = PackedType::get("
521 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000522 << ", " << utostr(PT->getNumElements()) << ");";
523 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000524 break;
525 }
526 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000527 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
528 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000529 break;
530 }
531 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000532 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000533 }
534
Reid Spencer74e032a2006-05-29 02:58:15 +0000535 // If the type had a name, make sure we recreate it.
536 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000537 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer74e032a2006-05-29 02:58:15 +0000538 if (progTypeName)
539 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000540 << typeName << ");";
541 nl(Out);
Reid Spencer74e032a2006-05-29 02:58:15 +0000542
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000543 // Pop us off the type stack
544 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000545
546 // Indicate that this type is now defined.
547 DefinedTypes.insert(Ty);
548
549 // Early resolve as many unresolved types as possible. Search the unresolved
550 // types map for the type we just printed. Now that its definition is complete
551 // we can resolve any previous references to it. This prevents a cascade of
552 // unresolved types.
553 TypeMap::iterator I = UnresolvedTypes.find(Ty);
554 if (I != UnresolvedTypes.end()) {
555 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000556 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
557 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000558 Out << I->second << " = cast<";
559 switch (Ty->getTypeID()) {
560 case Type::FunctionTyID: Out << "FunctionType"; break;
561 case Type::ArrayTyID: Out << "ArrayType"; break;
562 case Type::StructTyID: Out << "StructType"; break;
563 case Type::PackedTyID: Out << "PackedType"; break;
564 case Type::PointerTyID: Out << "PointerType"; break;
565 case Type::OpaqueTyID: Out << "OpaqueType"; break;
566 default: Out << "NoSuchDerivedType"; break;
567 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000568 Out << ">(" << I->second << "_fwd.get());";
569 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000570 UnresolvedTypes.erase(I);
571 }
572
573 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000574 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000575
576 // We weren't a recursive type
577 return false;
578}
579
Reid Spencer12803f52006-05-31 17:31:38 +0000580// Prints a type definition. Returns true if it could not resolve all the types
581// in the definition but had to use a forward reference.
582void
583CppWriter::printType(const Type* Ty) {
584 assert(TypeStack.empty());
585 TypeStack.clear();
586 printTypeInternal(Ty);
587 assert(TypeStack.empty());
588}
589
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000590void
591CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000592
593 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000594 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
595 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
596 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000597
598 // For primitive types and types already defined, just add a name
599 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Chris Lattner42a75512007-01-15 02:27:26 +0000600 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000601 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000602 Out << "mod->addTypeName(\"";
603 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000604 Out << "\", " << getCppName(TI->second) << ");";
605 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000606 // For everything else, define the type
607 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000608 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000609 }
610 }
611
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000612 // Add all of the global variables to the value table...
613 for (Module::const_global_iterator I = TheModule->global_begin(),
614 E = TheModule->global_end(); I != E; ++I) {
615 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000616 printType(I->getInitializer()->getType());
617 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000618 }
619
620 // Add all the functions to the table
621 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
622 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000623 printType(FI->getReturnType());
624 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000625 // Add all the function arguments
626 for(Function::const_arg_iterator AI = FI->arg_begin(),
627 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000628 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000629 }
630
631 // Add all of the basic blocks and instructions
632 for (Function::const_iterator BB = FI->begin(),
633 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000634 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000635 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
636 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000637 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000638 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000639 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000640 }
641 }
642 }
643}
644
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000645
Reid Spencere0d133f2006-05-29 18:08:06 +0000646// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000647void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000648 // First, if the constant is actually a GlobalValue (variable or function) or
649 // its already in the constant list then we've printed it already and we can
650 // just return.
651 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000652 return;
653
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000654 std::string constName(getCppName(CV));
655 std::string typeName(getCppName(CV->getType()));
656 if (CV->isNullValue()) {
657 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000658 << typeName << ");";
659 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000660 return;
661 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000662 if (isa<GlobalValue>(CV)) {
663 // Skip variables and functions, we emit them elsewhere
664 return;
665 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000666 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnerf6e7bb42007-01-12 18:37:29 +0000667 Out << "ConstantInt* " << constName << " = ConstantInt::get("
668 << typeName << ", " << CI->getZExtValue() << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000669 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000670 Out << "ConstantAggregateZero* " << constName
671 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000672 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000673 Out << "ConstantPointerNull* " << constName
674 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000675 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000676 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000677 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000678 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000679 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000680 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000681 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000682 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000683 // Determine if we want null termination or not.
684 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000685 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000686 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000687 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000688 Out << ");";
689 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000690 Out << "std::vector<Constant*> " << constName << "_elems;";
691 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000692 unsigned N = CA->getNumOperands();
693 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000694 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000695 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000696 << getCppName(CA->getOperand(i)) << ");";
697 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000698 }
699 Out << "Constant* " << constName << " = ConstantArray::get("
700 << typeName << ", " << constName << "_elems);";
701 }
702 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000703 Out << "std::vector<Constant*> " << constName << "_fields;";
704 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000705 unsigned N = CS->getNumOperands();
706 for (unsigned i = 0; i < N; i++) {
707 printConstant(CS->getOperand(i));
708 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000709 << getCppName(CS->getOperand(i)) << ");";
710 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000711 }
712 Out << "Constant* " << constName << " = ConstantStruct::get("
713 << typeName << ", " << constName << "_fields);";
714 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000715 Out << "std::vector<Constant*> " << constName << "_elems;";
716 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000717 unsigned N = CP->getNumOperands();
718 for (unsigned i = 0; i < N; ++i) {
719 printConstant(CP->getOperand(i));
720 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000721 << getCppName(CP->getOperand(i)) << ");";
722 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000723 }
724 Out << "Constant* " << constName << " = ConstantPacked::get("
725 << typeName << ", " << constName << "_elems);";
726 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000727 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000728 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000729 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000730 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000731 Out << "std::vector<Constant*> " << constName << "_indices;";
732 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000733 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000734 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000735 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000736 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000737 << getCppName(CE->getOperand(i)) << ");";
738 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000739 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000740 Out << "Constant* " << constName
741 << " = ConstantExpr::getGetElementPtr("
742 << getCppName(CE->getOperand(0)) << ", "
743 << constName << "_indices);";
Reid Spencer3da59db2006-11-27 01:05:10 +0000744 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000745 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000746 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000747 switch (CE->getOpcode()) {
748 default: assert(0 && "Invalid cast opcode");
749 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
750 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
751 case Instruction::SExt: Out << "Instruction::SExt"; break;
752 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
753 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
754 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
755 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
756 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
757 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
758 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
759 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
760 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
761 }
762 Out << ", " << getCppName(CE->getOperand(0)) << ", "
763 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000764 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000765 unsigned N = CE->getNumOperands();
766 for (unsigned i = 0; i < N; ++i ) {
767 printConstant(CE->getOperand(i));
768 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000769 Out << "Constant* " << constName << " = ConstantExpr::";
770 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000771 case Instruction::Add: Out << "getAdd("; break;
772 case Instruction::Sub: Out << "getSub("; break;
773 case Instruction::Mul: Out << "getMul("; break;
774 case Instruction::UDiv: Out << "getUDiv("; break;
775 case Instruction::SDiv: Out << "getSDiv("; break;
776 case Instruction::FDiv: Out << "getFDiv("; break;
777 case Instruction::URem: Out << "getURem("; break;
778 case Instruction::SRem: Out << "getSRem("; break;
779 case Instruction::FRem: Out << "getFRem("; break;
780 case Instruction::And: Out << "getAnd("; break;
781 case Instruction::Or: Out << "getOr("; break;
782 case Instruction::Xor: Out << "getXor("; break;
783 case Instruction::ICmp:
784 Out << "getICmp(ICmpInst::ICMP_";
785 switch (CE->getPredicate()) {
786 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
787 case ICmpInst::ICMP_NE: Out << "NE"; break;
788 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
789 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
790 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
791 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
792 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
793 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
794 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
795 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
796 default: error("Invalid ICmp Predicate");
797 }
798 break;
799 case Instruction::FCmp:
800 Out << "getFCmp(FCmpInst::FCMP_";
801 switch (CE->getPredicate()) {
802 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
803 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
804 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
805 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
806 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
807 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
808 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
809 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
810 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
811 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
812 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
813 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
814 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
815 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
816 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
817 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
818 default: error("Invalid FCmp Predicate");
819 }
820 break;
821 case Instruction::Shl: Out << "getShl("; break;
822 case Instruction::LShr: Out << "getLShr("; break;
823 case Instruction::AShr: Out << "getAShr("; break;
824 case Instruction::Select: Out << "getSelect("; break;
825 case Instruction::ExtractElement: Out << "getExtractElement("; break;
826 case Instruction::InsertElement: Out << "getInsertElement("; break;
827 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000828 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000829 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000830 break;
831 }
832 Out << getCppName(CE->getOperand(0));
833 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
834 Out << ", " << getCppName(CE->getOperand(i));
835 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000836 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000837 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000838 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000839 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000840 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000841 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000842}
843
Reid Spencer12803f52006-05-31 17:31:38 +0000844void
845CppWriter::printConstants(const Module* M) {
846 // Traverse all the global variables looking for constant initializers
847 for (Module::const_global_iterator I = TheModule->global_begin(),
848 E = TheModule->global_end(); I != E; ++I)
849 if (I->hasInitializer())
850 printConstant(I->getInitializer());
851
852 // Traverse the LLVM functions looking for constants
853 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
854 FI != FE; ++FI) {
855 // Add all of the basic blocks and instructions
856 for (Function::const_iterator BB = FI->begin(),
857 E = FI->end(); BB != E; ++BB) {
858 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
859 ++I) {
860 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
861 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
862 printConstant(C);
863 }
864 }
865 }
866 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000867 }
Reid Spencer66c87342006-05-30 03:43:49 +0000868}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000869
Reid Spencer12803f52006-05-31 17:31:38 +0000870void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000871 nl(Out) << "// Type Definitions";
872 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000873 printType(GV->getType());
874 if (GV->hasInitializer()) {
875 Constant* Init = GV->getInitializer();
876 printType(Init->getType());
877 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000878 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000879 printFunctionHead(F);
880 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000881 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000882 printVariableHead(gv);
883 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000884 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000885 printConstant(gv);
886 }
887 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000888 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000889 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000890 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000891 }
Reid Spencer12803f52006-05-31 17:31:38 +0000892}
Reid Spencer15f7e012006-05-30 21:18:23 +0000893
Reid Spencer12803f52006-05-31 17:31:38 +0000894void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000895 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000896 if (is_inline) {
897 Out << " = mod->getGlobalVariable(";
898 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000899 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
900 nl(Out) << "if (!" << getCppName(GV) << ") {";
901 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000902 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000903 Out << " = new GlobalVariable(";
904 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000905 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000906 Out << ",";
907 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
908 Out << ",";
909 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000910 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000911 Out << ",";
912 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000913 if (GV->hasInitializer()) {
914 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000915 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000916 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000917 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000918 Out << "\",";
919 nl(Out) << "mod);";
920 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000921
922 if (GV->hasSection()) {
923 printCppName(GV);
924 Out << "->setSection(\"";
925 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000926 Out << "\");";
927 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000928 }
929 if (GV->getAlignment()) {
930 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000931 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
932 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000933 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000934 if (is_inline) {
935 out(); Out << "}"; nl(Out);
936 }
Reid Spencer12803f52006-05-31 17:31:38 +0000937}
938
939void
940CppWriter::printVariableBody(const GlobalVariable *GV) {
941 if (GV->hasInitializer()) {
942 printCppName(GV);
943 Out << "->setInitializer(";
944 //if (!isa<GlobalValue(GV->getInitializer()))
945 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000946 Out << getCppName(GV->getInitializer()) << ");";
947 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000948 }
949}
950
951std::string
952CppWriter::getOpName(Value* V) {
953 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
954 return getCppName(V);
955
956 // See if its alread in the map of forward references, if so just return the
957 // name we already set up for it
958 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
959 if (I != ForwardRefs.end())
960 return I->second;
961
962 // This is a new forward reference. Generate a unique name for it
963 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
964
965 // Yes, this is a hack. An Argument is the smallest instantiable value that
966 // we can make as a placeholder for the real value. We'll replace these
967 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000968 Out << "Argument* " << result << " = new Argument("
969 << getCppName(V->getType()) << ");";
970 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000971 ForwardRefs[V] = result;
972 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000973}
974
Reid Spencere0d133f2006-05-29 18:08:06 +0000975// printInstruction - This member is called for each Instruction in a function.
976void
Reid Spencer12803f52006-05-31 17:31:38 +0000977CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000978 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000979
Reid Spencer15f7e012006-05-30 21:18:23 +0000980 // Before we emit this instruction, we need to take care of generating any
981 // forward references. So, we get the names of all the operands in advance
982 std::string* opNames = new std::string[I->getNumOperands()];
983 for (unsigned i = 0; i < I->getNumOperands(); i++) {
984 opNames[i] = getOpName(I->getOperand(i));
985 }
986
Reid Spencere0d133f2006-05-29 18:08:06 +0000987 switch (I->getOpcode()) {
988 case Instruction::Ret: {
989 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000990 Out << "ReturnInst* " << iName << " = new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +0000991 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000992 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000993 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000994 case Instruction::Br: {
995 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000996 Out << "BranchInst* " << iName << " = new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +0000997 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +0000998 Out << opNames[0] << ", "
999 << opNames[1] << ", "
1000 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001001
Reid Spencere0d133f2006-05-29 18:08:06 +00001002 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001003 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001004 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001005 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001006 }
1007 Out << bbname << ");";
1008 break;
1009 }
Reid Spencer66c87342006-05-30 03:43:49 +00001010 case Instruction::Switch: {
1011 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001012 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001013 << opNames[0] << ", "
1014 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001015 << sw->getNumCases() << ", " << bbname << ");";
1016 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001017 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001018 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001019 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001020 << opNames[i+1] << ");";
1021 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001022 }
1023 break;
1024 }
1025 case Instruction::Invoke: {
1026 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001027 Out << "std::vector<Value*> " << iName << "_params;";
1028 nl(Out);
1029 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1030 Out << iName << "_params.push_back("
1031 << opNames[i] << ");";
1032 nl(Out);
1033 }
1034 Out << "InvokeInst* " << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001035 << opNames[0] << ", "
1036 << opNames[1] << ", "
1037 << opNames[2] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001038 << iName << "_params, \"";
1039 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001040 Out << "\", " << bbname << ");";
1041 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001042 printCallingConv(inv->getCallingConv());
1043 Out << ");";
1044 break;
1045 }
1046 case Instruction::Unwind: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001047 Out << "UnwindInst* " << iName << " = new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001048 << bbname << ");";
1049 break;
1050 }
1051 case Instruction::Unreachable:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001052 Out << "UnreachableInst* " << iName << " = new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001053 << bbname << ");";
1054 break;
1055 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001056 case Instruction::Add:
1057 case Instruction::Sub:
1058 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001059 case Instruction::UDiv:
1060 case Instruction::SDiv:
1061 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001062 case Instruction::URem:
1063 case Instruction::SRem:
1064 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001065 case Instruction::And:
1066 case Instruction::Or:
1067 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001068 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001069 case Instruction::LShr:
1070 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001071 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001072 switch (I->getOpcode()) {
1073 case Instruction::Add: Out << "Instruction::Add"; break;
1074 case Instruction::Sub: Out << "Instruction::Sub"; break;
1075 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001076 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1077 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1078 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001079 case Instruction::URem:Out << "Instruction::URem"; break;
1080 case Instruction::SRem:Out << "Instruction::SRem"; break;
1081 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001082 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001083 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001084 case Instruction::Xor: Out << "Instruction::Xor"; break;
1085 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001086 case Instruction::LShr:Out << "Instruction::LShr"; break;
1087 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001088 default: Out << "Instruction::BadOpCode"; break;
1089 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001090 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001091 printEscapedString(I->getName());
1092 Out << "\", " << bbname << ");";
1093 break;
1094 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001095 case Instruction::FCmp: {
1096 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1097 switch (cast<FCmpInst>(I)->getPredicate()) {
1098 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1099 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1100 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1101 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1102 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1103 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1104 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1105 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1106 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1107 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1108 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1109 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1110 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1111 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1112 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1113 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1114 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1115 }
1116 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1117 printEscapedString(I->getName());
1118 Out << "\", " << bbname << ");";
1119 break;
1120 }
1121 case Instruction::ICmp: {
1122 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1123 switch (cast<ICmpInst>(I)->getPredicate()) {
1124 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1125 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1126 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1127 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1128 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1129 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1130 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1131 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1132 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1133 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1134 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001135 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001136 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001137 printEscapedString(I->getName());
1138 Out << "\", " << bbname << ");";
1139 break;
1140 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001141 case Instruction::Malloc: {
1142 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001143 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001144 << getCppName(mallocI->getAllocatedType()) << ", ";
1145 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001146 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001147 Out << "\"";
1148 printEscapedString(mallocI->getName());
1149 Out << "\", " << bbname << ");";
1150 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001151 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001152 << mallocI->getAlignment() << ");";
1153 break;
1154 }
Reid Spencer66c87342006-05-30 03:43:49 +00001155 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001156 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001157 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1158 break;
1159 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001160 case Instruction::Alloca: {
1161 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001162 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001163 << getCppName(allocaI->getAllocatedType()) << ", ";
1164 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001165 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001166 Out << "\"";
1167 printEscapedString(allocaI->getName());
1168 Out << "\", " << bbname << ");";
1169 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001170 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001171 << allocaI->getAlignment() << ");";
1172 break;
1173 }
Reid Spencer66c87342006-05-30 03:43:49 +00001174 case Instruction::Load:{
1175 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001176 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001177 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001178 printEscapedString(load->getName());
1179 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001180 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001181 break;
1182 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001183 case Instruction::Store: {
1184 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001185 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001186 << opNames[0] << ", "
1187 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001188 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001189 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001190 break;
1191 }
1192 case Instruction::GetElementPtr: {
1193 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1194 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001195 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001196 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001197 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001198 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001199 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001200 Out << "std::vector<Value*> " << iName << "_indices;";
1201 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001202 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001203 Out << iName << "_indices.push_back("
1204 << opNames[i] << ");";
1205 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001206 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001207 Out << "Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001208 << opNames[0] << ", " << iName << "_indices";
Reid Spencere0d133f2006-05-29 18:08:06 +00001209 }
1210 Out << ", \"";
1211 printEscapedString(gep->getName());
1212 Out << "\", " << bbname << ");";
1213 break;
1214 }
Reid Spencer66c87342006-05-30 03:43:49 +00001215 case Instruction::PHI: {
1216 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001217
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001218 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001219 << getCppName(phi->getType()) << ", \"";
1220 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001221 Out << "\", " << bbname << ");";
1222 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001223 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001224 << ");";
1225 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001226 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001227 Out << iName << "->addIncoming("
1228 << opNames[i] << ", " << opNames[i+1] << ");";
1229 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001230 }
Reid Spencer66c87342006-05-30 03:43:49 +00001231 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001232 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001233 case Instruction::Trunc:
1234 case Instruction::ZExt:
1235 case Instruction::SExt:
1236 case Instruction::FPTrunc:
1237 case Instruction::FPExt:
1238 case Instruction::FPToUI:
1239 case Instruction::FPToSI:
1240 case Instruction::UIToFP:
1241 case Instruction::SIToFP:
1242 case Instruction::PtrToInt:
1243 case Instruction::IntToPtr:
1244 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001245 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001246 Out << "CastInst* " << iName << " = new ";
1247 switch (I->getOpcode()) {
1248 case Instruction::Trunc: Out << "TruncInst";
1249 case Instruction::ZExt: Out << "ZExtInst";
1250 case Instruction::SExt: Out << "SExtInst";
1251 case Instruction::FPTrunc: Out << "FPTruncInst";
1252 case Instruction::FPExt: Out << "FPExtInst";
1253 case Instruction::FPToUI: Out << "FPToUIInst";
1254 case Instruction::FPToSI: Out << "FPToSIInst";
1255 case Instruction::UIToFP: Out << "UIToFPInst";
1256 case Instruction::SIToFP: Out << "SIToFPInst";
1257 case Instruction::PtrToInt: Out << "PtrToInst";
1258 case Instruction::IntToPtr: Out << "IntToPtrInst";
1259 case Instruction::BitCast: Out << "BitCastInst";
1260 default: assert(!"Unreachable"); break;
1261 }
1262 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001263 << getCppName(cst->getType()) << ", \"";
1264 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001265 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001266 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001267 }
Reid Spencer66c87342006-05-30 03:43:49 +00001268 case Instruction::Call:{
1269 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001270 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001271 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001272 << getCppName(ila->getFunctionType()) << ", \""
1273 << ila->getAsmString() << "\", \""
1274 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001275 << (ila->hasSideEffects() ? "true" : "false") << ");";
1276 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001277 }
Reid Spencer66c87342006-05-30 03:43:49 +00001278 if (call->getNumOperands() > 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001279 Out << "std::vector<Value*> " << iName << "_params;";
1280 nl(Out);
1281 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1282 Out << iName << "_params.push_back(" << opNames[i] << ");";
1283 nl(Out);
1284 }
1285 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001286 << opNames[0] << ", " << iName << "_params, \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001287 } else if (call->getNumOperands() == 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001288 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001289 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001290 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001291 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001292 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001293 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001294 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001295 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001296 }
1297 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001298 Out << "\", " << bbname << ");";
1299 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001300 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001301 Out << ");";
1302 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001303 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001304 Out << ");";
1305 break;
1306 }
1307 case Instruction::Select: {
1308 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001309 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001310 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001311 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001312 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001313 break;
1314 }
1315 case Instruction::UserOp1:
1316 /// FALL THROUGH
1317 case Instruction::UserOp2: {
1318 /// FIXME: What should be done here?
1319 break;
1320 }
1321 case Instruction::VAArg: {
1322 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001323 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001324 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001325 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001326 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001327 break;
1328 }
1329 case Instruction::ExtractElement: {
1330 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001331 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001332 << " = new ExtractElementInst(" << opNames[0]
1333 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001334 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001335 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001336 break;
1337 }
1338 case Instruction::InsertElement: {
1339 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001340 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001341 << " = new InsertElementInst(" << opNames[0]
1342 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001343 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001344 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001345 break;
1346 }
1347 case Instruction::ShuffleVector: {
1348 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001349 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001350 << " = new ShuffleVectorInst(" << opNames[0]
1351 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001352 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001353 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001354 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001355 }
1356 }
Reid Spencer68464b72006-06-15 16:09:59 +00001357 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001358 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001359 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001360}
1361
Reid Spencer12803f52006-05-31 17:31:38 +00001362// Print out the types, constants and declarations needed by one function
1363void CppWriter::printFunctionUses(const Function* F) {
1364
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001365 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001366 if (!is_inline) {
1367 // Print the function's return type
1368 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001369
Reid Spencerf977e7b2006-06-01 23:43:47 +00001370 // Print the function's function type
1371 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001372
Reid Spencerf977e7b2006-06-01 23:43:47 +00001373 // Print the types of each of the function's arguments
1374 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1375 AI != AE; ++AI) {
1376 printType(AI->getType());
1377 }
Reid Spencer12803f52006-05-31 17:31:38 +00001378 }
1379
1380 // Print type definitions for every type referenced by an instruction and
1381 // make a note of any global values or constants that are referenced
1382 std::vector<GlobalValue*> gvs;
1383 std::vector<Constant*> consts;
1384 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1385 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1386 I != E; ++I) {
1387 // Print the type of the instruction itself
1388 printType(I->getType());
1389
1390 // Print the type of each of the instruction's operands
1391 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1392 Value* operand = I->getOperand(i);
1393 printType(operand->getType());
1394 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1395 gvs.push_back(GV);
1396 else if (Constant* C = dyn_cast<Constant>(operand))
1397 consts.push_back(C);
1398 }
1399 }
1400 }
1401
1402 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001403 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001404 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1405 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001406 if (Function* Fun = dyn_cast<Function>(*I)) {
1407 if (!is_inline || Fun != F)
1408 printFunctionHead(Fun);
1409 }
Reid Spencer12803f52006-05-31 17:31:38 +00001410 }
1411
1412 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001413 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001414 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1415 I != E; ++I) {
1416 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1417 printVariableHead(F);
1418 }
1419
1420 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001421 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001422 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1423 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001424 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001425 }
1426
1427 // Process the global variables definitions now that all the constants have
1428 // been emitted. These definitions just couple the gvars with their constant
1429 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001430 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001431 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1432 I != E; ++I) {
1433 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1434 printVariableBody(GV);
1435 }
1436}
1437
1438void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001439 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001440 if (is_inline) {
1441 Out << " = mod->getFunction(\"";
1442 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001443 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1444 nl(Out) << "if (!" << getCppName(F) << ") {";
1445 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001446 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001447 Out<< " = new Function(";
1448 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1449 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001450 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001451 Out << ",";
1452 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001453 printEscapedString(F->getName());
Reid Spencer5cbf9852007-01-30 20:08:39 +00001454 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001455 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001456 printCppName(F);
1457 Out << "->setCallingConv(";
1458 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001459 Out << ");";
1460 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001461 if (F->hasSection()) {
1462 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001463 Out << "->setSection(\"" << F->getSection() << "\");";
1464 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001465 }
1466 if (F->getAlignment()) {
1467 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001468 Out << "->setAlignment(" << F->getAlignment() << ");";
1469 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001470 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001471 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001472 Out << "}";
1473 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001474 }
Reid Spencer12803f52006-05-31 17:31:38 +00001475}
1476
1477void CppWriter::printFunctionBody(const Function *F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001478 if (F->isDeclaration())
Reid Spencer12803f52006-05-31 17:31:38 +00001479 return; // external functions have no bodies.
1480
1481 // Clear the DefinedValues and ForwardRefs maps because we can't have
1482 // cross-function forward refs
1483 ForwardRefs.clear();
1484 DefinedValues.clear();
1485
1486 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001487 if (!is_inline) {
1488 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001489 Out << "Function::arg_iterator args = " << getCppName(F)
1490 << "->arg_begin();";
1491 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001492 }
1493 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1494 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001495 Out << "Value* " << getCppName(AI) << " = args++;";
1496 nl(Out);
1497 if (AI->hasName()) {
1498 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1499 nl(Out);
1500 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001501 }
Reid Spencer12803f52006-05-31 17:31:38 +00001502 }
1503
1504 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001505 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001506 for (Function::const_iterator BI = F->begin(), BE = F->end();
1507 BI != BE; ++BI) {
1508 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001509 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001510 if (BI->hasName())
1511 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001512 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1513 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001514 }
1515
1516 // Output all of its basic blocks... for the function
1517 for (Function::const_iterator BI = F->begin(), BE = F->end();
1518 BI != BE; ++BI) {
1519 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001520 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1521 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001522
1523 // Output all of the instructions in the basic block...
1524 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1525 I != E; ++I) {
1526 printInstruction(I,bbname);
1527 }
1528 }
1529
1530 // Loop over the ForwardRefs and resolve them now that all instructions
1531 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001532 if (!ForwardRefs.empty()) {
1533 nl(Out) << "// Resolve Forward References";
1534 nl(Out);
1535 }
1536
Reid Spencer12803f52006-05-31 17:31:38 +00001537 while (!ForwardRefs.empty()) {
1538 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001539 Out << I->second << "->replaceAllUsesWith("
1540 << getCppName(I->first) << "); delete " << I->second << ";";
1541 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001542 ForwardRefs.erase(I);
1543 }
1544}
1545
Reid Spencerf977e7b2006-06-01 23:43:47 +00001546void CppWriter::printInline(const std::string& fname, const std::string& func) {
Reid Spencer688b0492007-02-05 21:19:13 +00001547 const Function* F = TheModule->getFunction(func);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001548 if (!F) {
1549 error(std::string("Function '") + func + "' not found in input module");
1550 return;
1551 }
Reid Spencer5cbf9852007-01-30 20:08:39 +00001552 if (F->isDeclaration()) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001553 error(std::string("Function '") + func + "' is external!");
1554 return;
1555 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001556 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001557 << getCppName(F);
1558 unsigned arg_count = 1;
1559 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1560 AI != AE; ++AI) {
1561 Out << ", Value* arg_" << arg_count;
1562 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001563 Out << ") {";
1564 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001565 is_inline = true;
1566 printFunctionUses(F);
1567 printFunctionBody(F);
1568 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001569 Out << "return " << getCppName(F->begin()) << ";";
1570 nl(Out) << "}";
1571 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001572}
1573
Reid Spencer12803f52006-05-31 17:31:38 +00001574void CppWriter::printModuleBody() {
1575 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001576 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001577 printTypes(TheModule);
1578
1579 // Functions can call each other and global variables can reference them so
1580 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001581 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001582 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1583 I != E; ++I)
1584 printFunctionHead(I);
1585
1586 // Process the global variables declarations. We can't initialze them until
1587 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001588 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001589 for (Module::const_global_iterator I = TheModule->global_begin(),
1590 E = TheModule->global_end(); I != E; ++I) {
1591 printVariableHead(I);
1592 }
1593
1594 // Print out all the constants definitions. Constants don't recurse except
1595 // through GlobalValues. All GlobalValues have been declared at this point
1596 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001597 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001598 printConstants(TheModule);
1599
1600 // Process the global variables definitions now that all the constants have
1601 // been emitted. These definitions just couple the gvars with their constant
1602 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001603 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001604 for (Module::const_global_iterator I = TheModule->global_begin(),
1605 E = TheModule->global_end(); I != E; ++I) {
1606 printVariableBody(I);
1607 }
1608
1609 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001610 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001611 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1612 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001613 if (!I->isDeclaration()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001614 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1615 << ")";
1616 nl(Out) << "{";
1617 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001618 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001619 nl(Out,-1) << "}";
1620 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001621 }
1622 }
1623}
1624
1625void CppWriter::printProgram(
1626 const std::string& fname,
1627 const std::string& mName
1628) {
1629 Out << "#include <llvm/Module.h>\n";
1630 Out << "#include <llvm/DerivedTypes.h>\n";
1631 Out << "#include <llvm/Constants.h>\n";
1632 Out << "#include <llvm/GlobalVariable.h>\n";
1633 Out << "#include <llvm/Function.h>\n";
1634 Out << "#include <llvm/CallingConv.h>\n";
1635 Out << "#include <llvm/BasicBlock.h>\n";
1636 Out << "#include <llvm/Instructions.h>\n";
1637 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001638 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001639 Out << "#include <llvm/Pass.h>\n";
1640 Out << "#include <llvm/PassManager.h>\n";
1641 Out << "#include <llvm/Analysis/Verifier.h>\n";
1642 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1643 Out << "#include <algorithm>\n";
1644 Out << "#include <iostream>\n\n";
1645 Out << "using namespace llvm;\n\n";
1646 Out << "Module* " << fname << "();\n\n";
1647 Out << "int main(int argc, char**argv) {\n";
1648 Out << " Module* Mod = makeLLVMModule();\n";
1649 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1650 Out << " std::cerr.flush();\n";
1651 Out << " std::cout.flush();\n";
1652 Out << " PassManager PM;\n";
1653 Out << " PM.add(new PrintModulePass(&std::cout));\n";
1654 Out << " PM.run(*Mod);\n";
1655 Out << " return 0;\n";
1656 Out << "}\n\n";
1657 printModule(fname,mName);
1658}
1659
1660void CppWriter::printModule(
1661 const std::string& fname,
1662 const std::string& mName
1663) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001664 nl(Out) << "Module* " << fname << "() {";
1665 nl(Out,1) << "// Module Construction";
1666 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1667 nl(Out) << "mod->setEndianness(";
Reid Spencer12803f52006-05-31 17:31:38 +00001668 switch (TheModule->getEndianness()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001669 case Module::LittleEndian: Out << "Module::LittleEndian);"; break;
1670 case Module::BigEndian: Out << "Module::BigEndian);"; break;
1671 case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001672 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001673 nl(Out) << "mod->setPointerSize(";
Reid Spencer12803f52006-05-31 17:31:38 +00001674 switch (TheModule->getPointerSize()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001675 case Module::Pointer32: Out << "Module::Pointer32);"; break;
1676 case Module::Pointer64: Out << "Module::Pointer64);"; break;
1677 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001678 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001679 nl(Out);
1680 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer12803f52006-05-31 17:31:38 +00001681 Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001682 << "\");";
1683 nl(Out);
1684 }
Reid Spencer12803f52006-05-31 17:31:38 +00001685
1686 if (!TheModule->getModuleInlineAsm().empty()) {
1687 Out << "mod->setModuleInlineAsm(\"";
1688 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001689 Out << "\");";
1690 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001691 }
1692
1693 // Loop over the dependent libraries and emit them.
1694 Module::lib_iterator LI = TheModule->lib_begin();
1695 Module::lib_iterator LE = TheModule->lib_end();
1696 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001697 Out << "mod->addLibrary(\"" << *LI << "\");";
1698 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001699 ++LI;
1700 }
1701 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001702 nl(Out) << "return mod;";
1703 nl(Out,-1) << "}";
1704 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001705}
1706
1707void CppWriter::printContents(
1708 const std::string& fname, // Name of generated function
1709 const std::string& mName // Name of module generated module
1710) {
1711 Out << "\nModule* " << fname << "(Module *mod) {\n";
1712 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001713 printModuleBody();
1714 Out << "\nreturn mod;\n";
1715 Out << "\n}\n";
1716}
1717
1718void CppWriter::printFunction(
1719 const std::string& fname, // Name of generated function
1720 const std::string& funcName // Name of function to generate
1721) {
Reid Spencer688b0492007-02-05 21:19:13 +00001722 const Function* F = TheModule->getFunction(funcName);
Reid Spencer12803f52006-05-31 17:31:38 +00001723 if (!F) {
1724 error(std::string("Function '") + funcName + "' not found in input module");
1725 return;
1726 }
1727 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1728 printFunctionUses(F);
1729 printFunctionHead(F);
1730 printFunctionBody(F);
1731 Out << "return " << getCppName(F) << ";\n";
1732 Out << "}\n";
1733}
1734
1735void CppWriter::printVariable(
1736 const std::string& fname, /// Name of generated function
1737 const std::string& varName // Name of variable to generate
1738) {
1739 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1740
1741 if (!GV) {
1742 error(std::string("Variable '") + varName + "' not found in input module");
1743 return;
1744 }
1745 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1746 printVariableUses(GV);
1747 printVariableHead(GV);
1748 printVariableBody(GV);
1749 Out << "return " << getCppName(GV) << ";\n";
1750 Out << "}\n";
1751}
1752
1753void CppWriter::printType(
1754 const std::string& fname, /// Name of generated function
1755 const std::string& typeName // Name of type to generate
1756) {
1757 const Type* Ty = TheModule->getTypeByName(typeName);
1758 if (!Ty) {
1759 error(std::string("Type '") + typeName + "' not found in input module");
1760 return;
1761 }
1762 Out << "\nType* " << fname << "(Module *mod) {\n";
1763 printType(Ty);
1764 Out << "return " << getCppName(Ty) << ";\n";
1765 Out << "}\n";
1766}
1767
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001768} // end anonymous llvm
1769
1770namespace llvm {
1771
1772void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001773 // Initialize a CppWriter for us to use
1774 CppWriter W(o, mod);
1775
1776 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001777 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001778
1779 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001780 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001781
1782 // Get the name of the thing we are to generate
1783 std::string tgtname = NameToGenerate.getValue();
1784 if (GenerationType == GenModule ||
1785 GenerationType == GenContents ||
1786 GenerationType == GenProgram) {
1787 if (tgtname == "!bad!") {
1788 if (mod->getModuleIdentifier() == "-")
1789 tgtname = "<stdin>";
1790 else
1791 tgtname = mod->getModuleIdentifier();
1792 }
1793 } else if (tgtname == "!bad!") {
1794 W.error("You must use the -for option with -gen-{function,variable,type}");
1795 }
1796
1797 switch (WhatToGenerate(GenerationType)) {
1798 case GenProgram:
1799 if (fname.empty())
1800 fname = "makeLLVMModule";
1801 W.printProgram(fname,tgtname);
1802 break;
1803 case GenModule:
1804 if (fname.empty())
1805 fname = "makeLLVMModule";
1806 W.printModule(fname,tgtname);
1807 break;
1808 case GenContents:
1809 if (fname.empty())
1810 fname = "makeLLVMModuleContents";
1811 W.printContents(fname,tgtname);
1812 break;
1813 case GenFunction:
1814 if (fname.empty())
1815 fname = "makeLLVMFunction";
1816 W.printFunction(fname,tgtname);
1817 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001818 case GenInline:
1819 if (fname.empty())
1820 fname = "makeLLVMInline";
1821 W.printInline(fname,tgtname);
1822 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001823 case GenVariable:
1824 if (fname.empty())
1825 fname = "makeLLVMVariable";
1826 W.printVariable(fname,tgtname);
1827 break;
1828 case GenType:
1829 if (fname.empty())
1830 fname = "makeLLVMType";
1831 W.printType(fname,tgtname);
1832 break;
1833 default:
1834 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001835 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001836}
1837
1838}