blob: d507d9a516a89044e1d59636bbdcd831e0b61861 [file] [log] [blame]
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001//===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Reid Spencere0d133f2006-05-29 18:08:06 +00005// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the writing of the LLVM IR as a set of C++ calls to the
11// LLVM IR interface. The input module is assumed to be verified.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CallingConv.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/InlineAsm.h"
19#include "llvm/Instruction.h"
20#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22#include "llvm/SymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000023#include "llvm/TypeSymbolTable.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/STLExtras.h"
Reid Spencer15f7e012006-05-30 21:18:23 +000026#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000027#include "llvm/Support/CFG.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/MathExtras.h"
Reid Spencerf977e7b2006-06-01 23:43:47 +000030#include "llvm/Config/config.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000031#include <algorithm>
32#include <iostream>
Reid Spencer66c87342006-05-30 03:43:49 +000033#include <set>
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000034
35using namespace llvm;
36
Reid Spencer15f7e012006-05-30 21:18:23 +000037static cl::opt<std::string>
Reid Spencer15f7e012006-05-30 21:18:23 +000038FuncName("funcname", cl::desc("Specify the name of the generated function"),
39 cl::value_desc("function name"));
40
Reid Spencer12803f52006-05-31 17:31:38 +000041enum WhatToGenerate {
42 GenProgram,
43 GenModule,
44 GenContents,
45 GenFunction,
Reid Spencerf977e7b2006-06-01 23:43:47 +000046 GenInline,
Reid Spencer12803f52006-05-31 17:31:38 +000047 GenVariable,
48 GenType
49};
50
51static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
52 cl::desc("Choose what kind of output to generate"),
53 cl::init(GenProgram),
54 cl::values(
55 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
56 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
57 clEnumValN(GenContents,"gen-contents", "Generate contents of a module"),
58 clEnumValN(GenFunction,"gen-function", "Generate a function definition"),
Reid Spencerf977e7b2006-06-01 23:43:47 +000059 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
Reid Spencer12803f52006-05-31 17:31:38 +000060 clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"),
61 clEnumValN(GenType, "gen-type", "Generate a type definition"),
62 clEnumValEnd
63 )
64);
65
66static cl::opt<std::string> NameToGenerate("for", cl::Optional,
67 cl::desc("Specify the name of the thing to generate"),
68 cl::init("!bad!"));
Reid Spencer15f7e012006-05-30 21:18:23 +000069
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000070namespace {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000071typedef std::vector<const Type*> TypeList;
72typedef std::map<const Type*,std::string> TypeMap;
73typedef std::map<const Value*,std::string> ValueMap;
Reid Spencer66c87342006-05-30 03:43:49 +000074typedef std::set<std::string> NameSet;
Reid Spencer15f7e012006-05-30 21:18:23 +000075typedef std::set<const Type*> TypeSet;
76typedef std::set<const Value*> ValueSet;
77typedef std::map<const Value*,std::string> ForwardRefMap;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000078
Reid Spencere0d133f2006-05-29 18:08:06 +000079class CppWriter {
Reid Spencer12803f52006-05-31 17:31:38 +000080 const char* progname;
Reid Spencere0d133f2006-05-29 18:08:06 +000081 std::ostream &Out;
82 const Module *TheModule;
Andrew Lenharth539d8712006-05-31 20:18:56 +000083 uint64_t uniqueNum;
Reid Spencere0d133f2006-05-29 18:08:06 +000084 TypeMap TypeNames;
85 ValueMap ValueNames;
86 TypeMap UnresolvedTypes;
87 TypeList TypeStack;
Reid Spencer66c87342006-05-30 03:43:49 +000088 NameSet UsedNames;
Reid Spencer15f7e012006-05-30 21:18:23 +000089 TypeSet DefinedTypes;
90 ValueSet DefinedValues;
91 ForwardRefMap ForwardRefs;
Reid Spencerf977e7b2006-06-01 23:43:47 +000092 bool is_inline;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000093
Reid Spencere0d133f2006-05-29 18:08:06 +000094public:
Reid Spencer12803f52006-05-31 17:31:38 +000095 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
96 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
Reid Spencerf977e7b2006-06-01 23:43:47 +000097 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000098
Reid Spencere0d133f2006-05-29 18:08:06 +000099 const Module* getModule() { return TheModule; }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000100
Reid Spencer12803f52006-05-31 17:31:38 +0000101 void printProgram(const std::string& fname, const std::string& modName );
102 void printModule(const std::string& fname, const std::string& modName );
103 void printContents(const std::string& fname, const std::string& modName );
104 void printFunction(const std::string& fname, const std::string& funcName );
Reid Spencerf977e7b2006-06-01 23:43:47 +0000105 void printInline(const std::string& fname, const std::string& funcName );
Reid Spencer12803f52006-05-31 17:31:38 +0000106 void printVariable(const std::string& fname, const std::string& varName );
107 void printType(const std::string& fname, const std::string& typeName );
108
109 void error(const std::string& msg);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000110
Reid Spencere0d133f2006-05-29 18:08:06 +0000111private:
Reid Spencer12803f52006-05-31 17:31:38 +0000112 void printLinkageType(GlobalValue::LinkageTypes LT);
113 void printCallingConv(unsigned cc);
114 void printEscapedString(const std::string& str);
115 void printCFP(const ConstantFP* CFP);
116
117 std::string getCppName(const Type* val);
118 inline void printCppName(const Type* val);
119
120 std::string getCppName(const Value* val);
121 inline void printCppName(const Value* val);
122
123 bool printTypeInternal(const Type* Ty);
124 inline void printType(const Type* Ty);
Reid Spencere0d133f2006-05-29 18:08:06 +0000125 void printTypes(const Module* M);
Reid Spencer12803f52006-05-31 17:31:38 +0000126
Reid Spencere0d133f2006-05-29 18:08:06 +0000127 void printConstant(const Constant *CPV);
Reid Spencer12803f52006-05-31 17:31:38 +0000128 void printConstants(const Module* M);
129
130 void printVariableUses(const GlobalVariable *GV);
131 void printVariableHead(const GlobalVariable *GV);
132 void printVariableBody(const GlobalVariable *GV);
133
134 void printFunctionUses(const Function *F);
Reid Spencer66c87342006-05-30 03:43:49 +0000135 void printFunctionHead(const Function *F);
136 void printFunctionBody(const Function *F);
Reid Spencere0d133f2006-05-29 18:08:06 +0000137 void printInstruction(const Instruction *I, const std::string& bbname);
Reid Spencer15f7e012006-05-30 21:18:23 +0000138 std::string getOpName(Value*);
139
Reid Spencer12803f52006-05-31 17:31:38 +0000140 void printModuleBody();
141
Reid Spencere0d133f2006-05-29 18:08:06 +0000142};
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000143
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000144static unsigned indent_level = 0;
145inline std::ostream& nl(std::ostream& Out, int delta = 0) {
146 Out << "\n";
147 if (delta >= 0 || indent_level >= unsigned(-delta))
148 indent_level += delta;
149 for (unsigned i = 0; i < indent_level; ++i)
150 Out << " ";
151 return Out;
152}
153
154inline void in() { indent_level++; }
155inline void out() { if (indent_level >0) indent_level--; }
156
Reid Spencer12803f52006-05-31 17:31:38 +0000157inline void
158sanitize(std::string& str) {
159 for (size_t i = 0; i < str.length(); ++i)
160 if (!isalnum(str[i]) && str[i] != '_')
161 str[i] = '_';
162}
163
Reid Spencera54b7cb2007-01-12 07:05:14 +0000164inline std::string
Reid Spencer12803f52006-05-31 17:31:38 +0000165getTypePrefix(const Type* Ty ) {
Reid Spencer12803f52006-05-31 17:31:38 +0000166 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000167 case Type::VoidTyID: return "void_";
168 case Type::IntegerTyID:
169 return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
170 "_";
171 case Type::FloatTyID: return "float_";
172 case Type::DoubleTyID: return "double_";
173 case Type::LabelTyID: return "label_";
174 case Type::FunctionTyID: return "func_";
175 case Type::StructTyID: return "struct_";
176 case Type::ArrayTyID: return "array_";
177 case Type::PointerTyID: return "ptr_";
178 case Type::PackedTyID: return "packed_";
179 case Type::OpaqueTyID: return "opaque_";
180 default: return "other_";
Reid Spencer12803f52006-05-31 17:31:38 +0000181 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000182 return "unknown_";
Reid Spencer12803f52006-05-31 17:31:38 +0000183}
184
185// Looks up the type in the symbol table and returns a pointer to its name or
186// a null pointer if it wasn't found. Note that this isn't the same as the
187// Mode::getTypeName function which will return an empty string, not a null
188// pointer if the name is not found.
189inline const std::string*
Reid Spencer78d033e2007-01-06 07:24:44 +0000190findTypeName(const TypeSymbolTable& ST, const Type* Ty)
Reid Spencer12803f52006-05-31 17:31:38 +0000191{
Reid Spencer78d033e2007-01-06 07:24:44 +0000192 TypeSymbolTable::const_iterator TI = ST.begin();
193 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencer12803f52006-05-31 17:31:38 +0000194 for (;TI != TE; ++TI)
195 if (TI->second == Ty)
196 return &(TI->first);
197 return 0;
198}
199
200void
201CppWriter::error(const std::string& msg) {
202 std::cerr << progname << ": " << msg << "\n";
203 exit(2);
204}
205
Reid Spencer15f7e012006-05-30 21:18:23 +0000206// printCFP - Print a floating point constant .. very carefully :)
207// This makes sure that conversion to/from floating yields the same binary
208// result so that we don't lose precision.
209void
210CppWriter::printCFP(const ConstantFP *CFP) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000211 Out << "ConstantFP::get(";
212 if (CFP->getType() == Type::DoubleTy)
213 Out << "Type::DoubleTy, ";
214 else
215 Out << "Type::FloatTy, ";
Reid Spencer15f7e012006-05-30 21:18:23 +0000216#if HAVE_PRINTF_A
217 char Buffer[100];
218 sprintf(Buffer, "%A", CFP->getValue());
219 if ((!strncmp(Buffer, "0x", 2) ||
220 !strncmp(Buffer, "-0x", 3) ||
221 !strncmp(Buffer, "+0x", 3)) &&
222 (atof(Buffer) == CFP->getValue()))
Reid Spencerf977e7b2006-06-01 23:43:47 +0000223 if (CFP->getType() == Type::DoubleTy)
224 Out << "BitsToDouble(" << Buffer << ")";
225 else
226 Out << "BitsToFloat(" << Buffer << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000227 else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000228#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000229 std::string StrVal = ftostr(CFP->getValue());
230
231 while (StrVal[0] == ' ')
232 StrVal.erase(StrVal.begin());
233
234 // Check to make sure that the stringized number is not some string like
235 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
236 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
237 ((StrVal[0] == '-' || StrVal[0] == '+') &&
238 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
239 (atof(StrVal.c_str()) == CFP->getValue()))
240 if (CFP->getType() == Type::DoubleTy)
241 Out << StrVal;
242 else
243 Out << StrVal;
244 else if (CFP->getType() == Type::DoubleTy)
245 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
246 << std::dec << "ULL) /* " << StrVal << " */";
247 else
248 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
249 << std::dec << "U) /* " << StrVal << " */";
Reid Spencer15f7e012006-05-30 21:18:23 +0000250#if HAVE_PRINTF_A
251 }
252#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000253 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000254}
255
Reid Spencer12803f52006-05-31 17:31:38 +0000256void
257CppWriter::printCallingConv(unsigned cc){
258 // Print the calling convention.
259 switch (cc) {
260 case CallingConv::C: Out << "CallingConv::C"; break;
261 case CallingConv::CSRet: Out << "CallingConv::CSRet"; break;
262 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
263 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
264 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
265 default: Out << cc; break;
266 }
267}
Reid Spencer15f7e012006-05-30 21:18:23 +0000268
Reid Spencer12803f52006-05-31 17:31:38 +0000269void
270CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
271 switch (LT) {
272 case GlobalValue::InternalLinkage:
273 Out << "GlobalValue::InternalLinkage"; break;
274 case GlobalValue::LinkOnceLinkage:
275 Out << "GlobalValue::LinkOnceLinkage "; break;
276 case GlobalValue::WeakLinkage:
277 Out << "GlobalValue::WeakLinkage"; break;
278 case GlobalValue::AppendingLinkage:
279 Out << "GlobalValue::AppendingLinkage"; break;
280 case GlobalValue::ExternalLinkage:
281 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000282 case GlobalValue::DLLImportLinkage:
283 Out << "GlobalValue::DllImportLinkage"; break;
284 case GlobalValue::DLLExportLinkage:
285 Out << "GlobalValue::DllExportLinkage"; break;
286 case GlobalValue::ExternalWeakLinkage:
287 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000288 case GlobalValue::GhostLinkage:
289 Out << "GlobalValue::GhostLinkage"; break;
290 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000291}
292
Reid Spencere0d133f2006-05-29 18:08:06 +0000293// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000294// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000295void
296CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000297 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
298 unsigned char C = Str[i];
299 if (isprint(C) && C != '"' && C != '\\') {
300 Out << C;
301 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000302 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000303 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
304 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
305 }
306 }
307}
308
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000309std::string
310CppWriter::getCppName(const Type* Ty)
311{
312 // First, handle the primitive types .. easy
Chris Lattner42a75512007-01-15 02:27:26 +0000313 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000314 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000315 case Type::VoidTyID: return "Type::VoidTy";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000316 case Type::IntegerTyID: {
317 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
318 return "IntegerType::get(" + utostr(BitWidth) + ")";
319 }
Reid Spencer71d2ec92006-12-31 06:02:26 +0000320 case Type::FloatTyID: return "Type::FloatTy";
321 case Type::DoubleTyID: return "Type::DoubleTy";
322 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000323 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000324 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000325 break;
326 }
327 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
328 }
329
330 // Now, see if we've seen the type before and return that
331 TypeMap::iterator I = TypeNames.find(Ty);
332 if (I != TypeNames.end())
333 return I->second;
334
335 // Okay, let's build a new name for this type. Start with a prefix
336 const char* prefix = 0;
337 switch (Ty->getTypeID()) {
338 case Type::FunctionTyID: prefix = "FuncTy_"; break;
339 case Type::StructTyID: prefix = "StructTy_"; break;
340 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
341 case Type::PointerTyID: prefix = "PointerTy_"; break;
342 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
343 case Type::PackedTyID: prefix = "PackedTy_"; break;
344 default: prefix = "OtherTy_"; break; // prevent breakage
345 }
346
347 // See if the type has a name in the symboltable and build accordingly
Reid Spencer78d033e2007-01-06 07:24:44 +0000348 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000349 std::string name;
350 if (tName)
351 name = std::string(prefix) + *tName;
352 else
353 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000354 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000355
356 // Save the name
357 return TypeNames[Ty] = name;
358}
359
Reid Spencer12803f52006-05-31 17:31:38 +0000360void
361CppWriter::printCppName(const Type* Ty)
362{
363 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000364}
365
Reid Spencer12803f52006-05-31 17:31:38 +0000366std::string
367CppWriter::getCppName(const Value* val) {
368 std::string name;
369 ValueMap::iterator I = ValueNames.find(val);
370 if (I != ValueNames.end() && I->first == val)
371 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000372
Reid Spencer12803f52006-05-31 17:31:38 +0000373 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
374 name = std::string("gvar_") +
375 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000376 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000377 name = std::string("func_");
378 } else if (const Constant* C = dyn_cast<Constant>(val)) {
379 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000380 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
381 if (is_inline) {
382 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
383 Function::const_arg_iterator(Arg)) + 1;
384 name = std::string("arg_") + utostr(argNum);
385 NameSet::iterator NI = UsedNames.find(name);
386 if (NI != UsedNames.end())
387 name += std::string("_") + utostr(uniqueNum++);
388 UsedNames.insert(name);
389 return ValueNames[val] = name;
390 } else {
391 name = getTypePrefix(val->getType());
392 }
Reid Spencer12803f52006-05-31 17:31:38 +0000393 } else {
394 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000395 }
Reid Spencer12803f52006-05-31 17:31:38 +0000396 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
397 sanitize(name);
398 NameSet::iterator NI = UsedNames.find(name);
399 if (NI != UsedNames.end())
400 name += std::string("_") + utostr(uniqueNum++);
401 UsedNames.insert(name);
402 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000403}
404
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000405void
Reid Spencer12803f52006-05-31 17:31:38 +0000406CppWriter::printCppName(const Value* val) {
407 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000408}
409
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000410bool
Reid Spencer12803f52006-05-31 17:31:38 +0000411CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000412 // We don't print definitions for primitive types
Chris Lattner42a75512007-01-15 02:27:26 +0000413 if (Ty->isPrimitiveType() || Ty->isInteger())
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000414 return false;
415
Reid Spencer15f7e012006-05-30 21:18:23 +0000416 // If we already defined this type, we don't need to define it again.
417 if (DefinedTypes.find(Ty) != DefinedTypes.end())
418 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000419
Reid Spencer15f7e012006-05-30 21:18:23 +0000420 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000421 std::string typeName(getCppName(Ty));
422
423 // Search the type stack for recursion. If we find it, then generate this
424 // as an OpaqueType, but make sure not to do this multiple times because
425 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000426 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000427 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000428 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
429 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000430 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
431 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000432 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
433 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000434 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000435 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000436 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000437 }
438
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000439 // We're going to print a derived type which, by definition, contains other
440 // types. So, push this one we're printing onto the type stack to assist with
441 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000442 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000443
444 // Print the type definition
445 switch (Ty->getTypeID()) {
446 case Type::FunctionTyID: {
447 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000448 Out << "std::vector<const Type*>" << typeName << "_args;";
449 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000450 FunctionType::param_iterator PI = FT->param_begin();
451 FunctionType::param_iterator PE = FT->param_end();
452 for (; PI != PE; ++PI) {
453 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000454 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000455 std::string argName(getCppName(argTy));
456 Out << typeName << "_args.push_back(" << argName;
457 if (isForward)
458 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000459 Out << ");";
460 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000461 }
Reid Spencer12803f52006-05-31 17:31:38 +0000462 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000463 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000464 Out << "FunctionType* " << typeName << " = FunctionType::get(";
465 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000466 if (isForward)
467 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000468 Out << ",";
469 nl(Out) << "/*Params=*/" << typeName << "_args,";
470 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
471 out();
472 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000473 break;
474 }
475 case Type::StructTyID: {
476 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000477 Out << "std::vector<const Type*>" << typeName << "_fields;";
478 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000479 StructType::element_iterator EI = ST->element_begin();
480 StructType::element_iterator EE = ST->element_end();
481 for (; EI != EE; ++EI) {
482 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000483 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000484 std::string fieldName(getCppName(fieldTy));
485 Out << typeName << "_fields.push_back(" << fieldName;
486 if (isForward)
487 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000488 Out << ");";
489 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000490 }
491 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000492 << typeName << "_fields);";
493 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000494 break;
495 }
496 case Type::ArrayTyID: {
497 const ArrayType* AT = cast<ArrayType>(Ty);
498 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000499 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000500 std::string elemName(getCppName(ET));
501 Out << "ArrayType* " << typeName << " = ArrayType::get("
502 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000503 << ", " << utostr(AT->getNumElements()) << ");";
504 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000505 break;
506 }
507 case Type::PointerTyID: {
508 const PointerType* PT = cast<PointerType>(Ty);
509 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000510 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000511 std::string elemName(getCppName(ET));
512 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000513 << elemName << (isForward ? "_fwd" : "") << ");";
514 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000515 break;
516 }
517 case Type::PackedTyID: {
518 const PackedType* PT = cast<PackedType>(Ty);
519 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000520 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000521 std::string elemName(getCppName(ET));
522 Out << "PackedType* " << typeName << " = PackedType::get("
523 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000524 << ", " << utostr(PT->getNumElements()) << ");";
525 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000526 break;
527 }
528 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000529 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
530 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000531 break;
532 }
533 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000534 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000535 }
536
Reid Spencer74e032a2006-05-29 02:58:15 +0000537 // If the type had a name, make sure we recreate it.
538 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000539 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer74e032a2006-05-29 02:58:15 +0000540 if (progTypeName)
541 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000542 << typeName << ");";
543 nl(Out);
Reid Spencer74e032a2006-05-29 02:58:15 +0000544
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000545 // Pop us off the type stack
546 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000547
548 // Indicate that this type is now defined.
549 DefinedTypes.insert(Ty);
550
551 // Early resolve as many unresolved types as possible. Search the unresolved
552 // types map for the type we just printed. Now that its definition is complete
553 // we can resolve any previous references to it. This prevents a cascade of
554 // unresolved types.
555 TypeMap::iterator I = UnresolvedTypes.find(Ty);
556 if (I != UnresolvedTypes.end()) {
557 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000558 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
559 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000560 Out << I->second << " = cast<";
561 switch (Ty->getTypeID()) {
562 case Type::FunctionTyID: Out << "FunctionType"; break;
563 case Type::ArrayTyID: Out << "ArrayType"; break;
564 case Type::StructTyID: Out << "StructType"; break;
565 case Type::PackedTyID: Out << "PackedType"; break;
566 case Type::PointerTyID: Out << "PointerType"; break;
567 case Type::OpaqueTyID: Out << "OpaqueType"; break;
568 default: Out << "NoSuchDerivedType"; break;
569 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000570 Out << ">(" << I->second << "_fwd.get());";
571 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000572 UnresolvedTypes.erase(I);
573 }
574
575 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000576 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000577
578 // We weren't a recursive type
579 return false;
580}
581
Reid Spencer12803f52006-05-31 17:31:38 +0000582// Prints a type definition. Returns true if it could not resolve all the types
583// in the definition but had to use a forward reference.
584void
585CppWriter::printType(const Type* Ty) {
586 assert(TypeStack.empty());
587 TypeStack.clear();
588 printTypeInternal(Ty);
589 assert(TypeStack.empty());
590}
591
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000592void
593CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000594
595 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000596 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
597 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
598 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000599
600 // For primitive types and types already defined, just add a name
601 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Chris Lattner42a75512007-01-15 02:27:26 +0000602 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000603 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000604 Out << "mod->addTypeName(\"";
605 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000606 Out << "\", " << getCppName(TI->second) << ");";
607 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000608 // For everything else, define the type
609 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000610 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000611 }
612 }
613
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000614 // Add all of the global variables to the value table...
615 for (Module::const_global_iterator I = TheModule->global_begin(),
616 E = TheModule->global_end(); I != E; ++I) {
617 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000618 printType(I->getInitializer()->getType());
619 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000620 }
621
622 // Add all the functions to the table
623 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
624 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000625 printType(FI->getReturnType());
626 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000627 // Add all the function arguments
628 for(Function::const_arg_iterator AI = FI->arg_begin(),
629 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000630 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000631 }
632
633 // Add all of the basic blocks and instructions
634 for (Function::const_iterator BB = FI->begin(),
635 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000636 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000637 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
638 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000639 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000640 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000641 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000642 }
643 }
644 }
645}
646
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000647
Reid Spencere0d133f2006-05-29 18:08:06 +0000648// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000649void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000650 // First, if the constant is actually a GlobalValue (variable or function) or
651 // its already in the constant list then we've printed it already and we can
652 // just return.
653 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000654 return;
655
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000656 std::string constName(getCppName(CV));
657 std::string typeName(getCppName(CV->getType()));
658 if (CV->isNullValue()) {
659 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000660 << typeName << ");";
661 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000662 return;
663 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000664 if (isa<GlobalValue>(CV)) {
665 // Skip variables and functions, we emit them elsewhere
666 return;
667 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000668 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnerf6e7bb42007-01-12 18:37:29 +0000669 Out << "ConstantInt* " << constName << " = ConstantInt::get("
670 << typeName << ", " << CI->getZExtValue() << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000671 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000672 Out << "ConstantAggregateZero* " << constName
673 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000674 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000675 Out << "ConstantPointerNull* " << constName
676 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000677 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000678 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000679 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000680 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000681 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000682 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000683 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000684 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000685 // Determine if we want null termination or not.
686 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000687 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000688 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000689 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000690 Out << ");";
691 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000692 Out << "std::vector<Constant*> " << constName << "_elems;";
693 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000694 unsigned N = CA->getNumOperands();
695 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000696 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000697 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000698 << getCppName(CA->getOperand(i)) << ");";
699 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000700 }
701 Out << "Constant* " << constName << " = ConstantArray::get("
702 << typeName << ", " << constName << "_elems);";
703 }
704 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000705 Out << "std::vector<Constant*> " << constName << "_fields;";
706 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000707 unsigned N = CS->getNumOperands();
708 for (unsigned i = 0; i < N; i++) {
709 printConstant(CS->getOperand(i));
710 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000711 << getCppName(CS->getOperand(i)) << ");";
712 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000713 }
714 Out << "Constant* " << constName << " = ConstantStruct::get("
715 << typeName << ", " << constName << "_fields);";
716 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000717 Out << "std::vector<Constant*> " << constName << "_elems;";
718 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000719 unsigned N = CP->getNumOperands();
720 for (unsigned i = 0; i < N; ++i) {
721 printConstant(CP->getOperand(i));
722 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000723 << getCppName(CP->getOperand(i)) << ");";
724 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000725 }
726 Out << "Constant* " << constName << " = ConstantPacked::get("
727 << typeName << ", " << constName << "_elems);";
728 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000729 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000730 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000731 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000732 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000733 Out << "std::vector<Constant*> " << constName << "_indices;";
734 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000735 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000736 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000737 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000738 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000739 << getCppName(CE->getOperand(i)) << ");";
740 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000741 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000742 Out << "Constant* " << constName
743 << " = ConstantExpr::getGetElementPtr("
744 << getCppName(CE->getOperand(0)) << ", "
745 << constName << "_indices);";
Reid Spencer3da59db2006-11-27 01:05:10 +0000746 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000747 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000748 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000749 switch (CE->getOpcode()) {
750 default: assert(0 && "Invalid cast opcode");
751 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
752 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
753 case Instruction::SExt: Out << "Instruction::SExt"; break;
754 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
755 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
756 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
757 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
758 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
759 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
760 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
761 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
762 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
763 }
764 Out << ", " << getCppName(CE->getOperand(0)) << ", "
765 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000766 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000767 unsigned N = CE->getNumOperands();
768 for (unsigned i = 0; i < N; ++i ) {
769 printConstant(CE->getOperand(i));
770 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000771 Out << "Constant* " << constName << " = ConstantExpr::";
772 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000773 case Instruction::Add: Out << "getAdd("; break;
774 case Instruction::Sub: Out << "getSub("; break;
775 case Instruction::Mul: Out << "getMul("; break;
776 case Instruction::UDiv: Out << "getUDiv("; break;
777 case Instruction::SDiv: Out << "getSDiv("; break;
778 case Instruction::FDiv: Out << "getFDiv("; break;
779 case Instruction::URem: Out << "getURem("; break;
780 case Instruction::SRem: Out << "getSRem("; break;
781 case Instruction::FRem: Out << "getFRem("; break;
782 case Instruction::And: Out << "getAnd("; break;
783 case Instruction::Or: Out << "getOr("; break;
784 case Instruction::Xor: Out << "getXor("; break;
785 case Instruction::ICmp:
786 Out << "getICmp(ICmpInst::ICMP_";
787 switch (CE->getPredicate()) {
788 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
789 case ICmpInst::ICMP_NE: Out << "NE"; break;
790 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
791 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
792 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
793 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
794 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
795 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
796 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
797 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
798 default: error("Invalid ICmp Predicate");
799 }
800 break;
801 case Instruction::FCmp:
802 Out << "getFCmp(FCmpInst::FCMP_";
803 switch (CE->getPredicate()) {
804 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
805 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
806 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
807 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
808 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
809 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
810 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
811 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
812 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
813 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
814 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
815 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
816 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
817 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
818 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
819 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
820 default: error("Invalid FCmp Predicate");
821 }
822 break;
823 case Instruction::Shl: Out << "getShl("; break;
824 case Instruction::LShr: Out << "getLShr("; break;
825 case Instruction::AShr: Out << "getAShr("; break;
826 case Instruction::Select: Out << "getSelect("; break;
827 case Instruction::ExtractElement: Out << "getExtractElement("; break;
828 case Instruction::InsertElement: Out << "getInsertElement("; break;
829 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000830 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000831 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000832 break;
833 }
834 Out << getCppName(CE->getOperand(0));
835 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
836 Out << ", " << getCppName(CE->getOperand(i));
837 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000838 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000839 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000840 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000841 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000842 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000843 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000844}
845
Reid Spencer12803f52006-05-31 17:31:38 +0000846void
847CppWriter::printConstants(const Module* M) {
848 // Traverse all the global variables looking for constant initializers
849 for (Module::const_global_iterator I = TheModule->global_begin(),
850 E = TheModule->global_end(); I != E; ++I)
851 if (I->hasInitializer())
852 printConstant(I->getInitializer());
853
854 // Traverse the LLVM functions looking for constants
855 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
856 FI != FE; ++FI) {
857 // Add all of the basic blocks and instructions
858 for (Function::const_iterator BB = FI->begin(),
859 E = FI->end(); BB != E; ++BB) {
860 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
861 ++I) {
862 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
863 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
864 printConstant(C);
865 }
866 }
867 }
868 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000869 }
Reid Spencer66c87342006-05-30 03:43:49 +0000870}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000871
Reid Spencer12803f52006-05-31 17:31:38 +0000872void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000873 nl(Out) << "// Type Definitions";
874 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000875 printType(GV->getType());
876 if (GV->hasInitializer()) {
877 Constant* Init = GV->getInitializer();
878 printType(Init->getType());
879 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000880 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000881 printFunctionHead(F);
882 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000883 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000884 printVariableHead(gv);
885 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000886 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000887 printConstant(gv);
888 }
889 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000890 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000891 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000892 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000893 }
Reid Spencer12803f52006-05-31 17:31:38 +0000894}
Reid Spencer15f7e012006-05-30 21:18:23 +0000895
Reid Spencer12803f52006-05-31 17:31:38 +0000896void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000897 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000898 if (is_inline) {
899 Out << " = mod->getGlobalVariable(";
900 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000901 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
902 nl(Out) << "if (!" << getCppName(GV) << ") {";
903 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000904 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000905 Out << " = new GlobalVariable(";
906 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000907 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000908 Out << ",";
909 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
910 Out << ",";
911 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000912 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000913 Out << ",";
914 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000915 if (GV->hasInitializer()) {
916 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000917 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000918 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000919 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000920 Out << "\",";
921 nl(Out) << "mod);";
922 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000923
924 if (GV->hasSection()) {
925 printCppName(GV);
926 Out << "->setSection(\"";
927 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000928 Out << "\");";
929 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000930 }
931 if (GV->getAlignment()) {
932 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000933 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
934 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000935 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000936 if (is_inline) {
937 out(); Out << "}"; nl(Out);
938 }
Reid Spencer12803f52006-05-31 17:31:38 +0000939}
940
941void
942CppWriter::printVariableBody(const GlobalVariable *GV) {
943 if (GV->hasInitializer()) {
944 printCppName(GV);
945 Out << "->setInitializer(";
946 //if (!isa<GlobalValue(GV->getInitializer()))
947 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000948 Out << getCppName(GV->getInitializer()) << ");";
949 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000950 }
951}
952
953std::string
954CppWriter::getOpName(Value* V) {
955 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
956 return getCppName(V);
957
958 // See if its alread in the map of forward references, if so just return the
959 // name we already set up for it
960 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
961 if (I != ForwardRefs.end())
962 return I->second;
963
964 // This is a new forward reference. Generate a unique name for it
965 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
966
967 // Yes, this is a hack. An Argument is the smallest instantiable value that
968 // we can make as a placeholder for the real value. We'll replace these
969 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000970 Out << "Argument* " << result << " = new Argument("
971 << getCppName(V->getType()) << ");";
972 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000973 ForwardRefs[V] = result;
974 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000975}
976
Reid Spencere0d133f2006-05-29 18:08:06 +0000977// printInstruction - This member is called for each Instruction in a function.
978void
Reid Spencer12803f52006-05-31 17:31:38 +0000979CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000980 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000981
Reid Spencer15f7e012006-05-30 21:18:23 +0000982 // Before we emit this instruction, we need to take care of generating any
983 // forward references. So, we get the names of all the operands in advance
984 std::string* opNames = new std::string[I->getNumOperands()];
985 for (unsigned i = 0; i < I->getNumOperands(); i++) {
986 opNames[i] = getOpName(I->getOperand(i));
987 }
988
Reid Spencere0d133f2006-05-29 18:08:06 +0000989 switch (I->getOpcode()) {
990 case Instruction::Ret: {
991 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000992 Out << "ReturnInst* " << iName << " = new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +0000993 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000994 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000995 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000996 case Instruction::Br: {
997 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000998 Out << "BranchInst* " << iName << " = new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +0000999 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001000 Out << opNames[0] << ", "
1001 << opNames[1] << ", "
1002 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001003
Reid Spencere0d133f2006-05-29 18:08:06 +00001004 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001005 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001006 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001007 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001008 }
1009 Out << bbname << ");";
1010 break;
1011 }
Reid Spencer66c87342006-05-30 03:43:49 +00001012 case Instruction::Switch: {
1013 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001014 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001015 << opNames[0] << ", "
1016 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001017 << sw->getNumCases() << ", " << bbname << ");";
1018 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001019 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001020 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001021 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001022 << opNames[i+1] << ");";
1023 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001024 }
1025 break;
1026 }
1027 case Instruction::Invoke: {
1028 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001029 Out << "std::vector<Value*> " << iName << "_params;";
1030 nl(Out);
1031 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1032 Out << iName << "_params.push_back("
1033 << opNames[i] << ");";
1034 nl(Out);
1035 }
1036 Out << "InvokeInst* " << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001037 << opNames[0] << ", "
1038 << opNames[1] << ", "
1039 << opNames[2] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001040 << iName << "_params, \"";
1041 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001042 Out << "\", " << bbname << ");";
1043 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001044 printCallingConv(inv->getCallingConv());
1045 Out << ");";
1046 break;
1047 }
1048 case Instruction::Unwind: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001049 Out << "UnwindInst* " << iName << " = new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001050 << bbname << ");";
1051 break;
1052 }
1053 case Instruction::Unreachable:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001054 Out << "UnreachableInst* " << iName << " = new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001055 << bbname << ");";
1056 break;
1057 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001058 case Instruction::Add:
1059 case Instruction::Sub:
1060 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001061 case Instruction::UDiv:
1062 case Instruction::SDiv:
1063 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001064 case Instruction::URem:
1065 case Instruction::SRem:
1066 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001067 case Instruction::And:
1068 case Instruction::Or:
1069 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001070 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001071 case Instruction::LShr:
1072 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001073 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001074 switch (I->getOpcode()) {
1075 case Instruction::Add: Out << "Instruction::Add"; break;
1076 case Instruction::Sub: Out << "Instruction::Sub"; break;
1077 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001078 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1079 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1080 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001081 case Instruction::URem:Out << "Instruction::URem"; break;
1082 case Instruction::SRem:Out << "Instruction::SRem"; break;
1083 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001084 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001085 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001086 case Instruction::Xor: Out << "Instruction::Xor"; break;
1087 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001088 case Instruction::LShr:Out << "Instruction::LShr"; break;
1089 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001090 default: Out << "Instruction::BadOpCode"; break;
1091 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001092 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001093 printEscapedString(I->getName());
1094 Out << "\", " << bbname << ");";
1095 break;
1096 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001097 case Instruction::FCmp: {
1098 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1099 switch (cast<FCmpInst>(I)->getPredicate()) {
1100 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1101 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1102 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1103 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1104 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1105 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1106 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1107 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1108 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1109 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1110 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1111 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1112 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1113 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1114 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1115 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1116 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1117 }
1118 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1119 printEscapedString(I->getName());
1120 Out << "\", " << bbname << ");";
1121 break;
1122 }
1123 case Instruction::ICmp: {
1124 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1125 switch (cast<ICmpInst>(I)->getPredicate()) {
1126 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1127 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1128 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1129 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1130 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1131 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1132 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1133 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1134 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1135 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1136 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001137 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001138 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001139 printEscapedString(I->getName());
1140 Out << "\", " << bbname << ");";
1141 break;
1142 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001143 case Instruction::Malloc: {
1144 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001145 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001146 << getCppName(mallocI->getAllocatedType()) << ", ";
1147 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001148 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001149 Out << "\"";
1150 printEscapedString(mallocI->getName());
1151 Out << "\", " << bbname << ");";
1152 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001153 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001154 << mallocI->getAlignment() << ");";
1155 break;
1156 }
Reid Spencer66c87342006-05-30 03:43:49 +00001157 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001158 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001159 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1160 break;
1161 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001162 case Instruction::Alloca: {
1163 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001164 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001165 << getCppName(allocaI->getAllocatedType()) << ", ";
1166 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001167 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001168 Out << "\"";
1169 printEscapedString(allocaI->getName());
1170 Out << "\", " << bbname << ");";
1171 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001172 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001173 << allocaI->getAlignment() << ");";
1174 break;
1175 }
Reid Spencer66c87342006-05-30 03:43:49 +00001176 case Instruction::Load:{
1177 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001178 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001179 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001180 printEscapedString(load->getName());
1181 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001182 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001183 break;
1184 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001185 case Instruction::Store: {
1186 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001187 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001188 << opNames[0] << ", "
1189 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001190 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001191 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001192 break;
1193 }
1194 case Instruction::GetElementPtr: {
1195 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1196 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001197 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001198 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001199 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001200 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001201 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001202 Out << "std::vector<Value*> " << iName << "_indices;";
1203 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001204 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001205 Out << iName << "_indices.push_back("
1206 << opNames[i] << ");";
1207 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001208 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001209 Out << "Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001210 << opNames[0] << ", " << iName << "_indices";
Reid Spencere0d133f2006-05-29 18:08:06 +00001211 }
1212 Out << ", \"";
1213 printEscapedString(gep->getName());
1214 Out << "\", " << bbname << ");";
1215 break;
1216 }
Reid Spencer66c87342006-05-30 03:43:49 +00001217 case Instruction::PHI: {
1218 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001219
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001220 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001221 << getCppName(phi->getType()) << ", \"";
1222 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001223 Out << "\", " << bbname << ");";
1224 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001225 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001226 << ");";
1227 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001228 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001229 Out << iName << "->addIncoming("
1230 << opNames[i] << ", " << opNames[i+1] << ");";
1231 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001232 }
Reid Spencer66c87342006-05-30 03:43:49 +00001233 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001234 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001235 case Instruction::Trunc:
1236 case Instruction::ZExt:
1237 case Instruction::SExt:
1238 case Instruction::FPTrunc:
1239 case Instruction::FPExt:
1240 case Instruction::FPToUI:
1241 case Instruction::FPToSI:
1242 case Instruction::UIToFP:
1243 case Instruction::SIToFP:
1244 case Instruction::PtrToInt:
1245 case Instruction::IntToPtr:
1246 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001247 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001248 Out << "CastInst* " << iName << " = new ";
1249 switch (I->getOpcode()) {
1250 case Instruction::Trunc: Out << "TruncInst";
1251 case Instruction::ZExt: Out << "ZExtInst";
1252 case Instruction::SExt: Out << "SExtInst";
1253 case Instruction::FPTrunc: Out << "FPTruncInst";
1254 case Instruction::FPExt: Out << "FPExtInst";
1255 case Instruction::FPToUI: Out << "FPToUIInst";
1256 case Instruction::FPToSI: Out << "FPToSIInst";
1257 case Instruction::UIToFP: Out << "UIToFPInst";
1258 case Instruction::SIToFP: Out << "SIToFPInst";
1259 case Instruction::PtrToInt: Out << "PtrToInst";
1260 case Instruction::IntToPtr: Out << "IntToPtrInst";
1261 case Instruction::BitCast: Out << "BitCastInst";
1262 default: assert(!"Unreachable"); break;
1263 }
1264 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001265 << getCppName(cst->getType()) << ", \"";
1266 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001267 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001268 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001269 }
Reid Spencer66c87342006-05-30 03:43:49 +00001270 case Instruction::Call:{
1271 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001272 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001273 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001274 << getCppName(ila->getFunctionType()) << ", \""
1275 << ila->getAsmString() << "\", \""
1276 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001277 << (ila->hasSideEffects() ? "true" : "false") << ");";
1278 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001279 }
Reid Spencer66c87342006-05-30 03:43:49 +00001280 if (call->getNumOperands() > 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001281 Out << "std::vector<Value*> " << iName << "_params;";
1282 nl(Out);
1283 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1284 Out << iName << "_params.push_back(" << opNames[i] << ");";
1285 nl(Out);
1286 }
1287 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001288 << opNames[0] << ", " << iName << "_params, \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001289 } else if (call->getNumOperands() == 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001290 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001291 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001292 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001293 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001294 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001295 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001296 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001297 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001298 }
1299 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001300 Out << "\", " << bbname << ");";
1301 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001302 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001303 Out << ");";
1304 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001305 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001306 Out << ");";
1307 break;
1308 }
1309 case Instruction::Select: {
1310 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001311 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001312 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001313 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001314 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001315 break;
1316 }
1317 case Instruction::UserOp1:
1318 /// FALL THROUGH
1319 case Instruction::UserOp2: {
1320 /// FIXME: What should be done here?
1321 break;
1322 }
1323 case Instruction::VAArg: {
1324 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001325 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001326 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001327 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001328 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001329 break;
1330 }
1331 case Instruction::ExtractElement: {
1332 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001333 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001334 << " = new ExtractElementInst(" << opNames[0]
1335 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001336 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001337 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001338 break;
1339 }
1340 case Instruction::InsertElement: {
1341 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001342 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001343 << " = new InsertElementInst(" << opNames[0]
1344 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001345 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001346 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001347 break;
1348 }
1349 case Instruction::ShuffleVector: {
1350 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001351 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001352 << " = new ShuffleVectorInst(" << opNames[0]
1353 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001354 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001355 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001356 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001357 }
1358 }
Reid Spencer68464b72006-06-15 16:09:59 +00001359 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001360 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001361 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001362}
1363
Reid Spencer12803f52006-05-31 17:31:38 +00001364// Print out the types, constants and declarations needed by one function
1365void CppWriter::printFunctionUses(const Function* F) {
1366
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001367 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001368 if (!is_inline) {
1369 // Print the function's return type
1370 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001371
Reid Spencerf977e7b2006-06-01 23:43:47 +00001372 // Print the function's function type
1373 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001374
Reid Spencerf977e7b2006-06-01 23:43:47 +00001375 // Print the types of each of the function's arguments
1376 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1377 AI != AE; ++AI) {
1378 printType(AI->getType());
1379 }
Reid Spencer12803f52006-05-31 17:31:38 +00001380 }
1381
1382 // Print type definitions for every type referenced by an instruction and
1383 // make a note of any global values or constants that are referenced
1384 std::vector<GlobalValue*> gvs;
1385 std::vector<Constant*> consts;
1386 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1387 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1388 I != E; ++I) {
1389 // Print the type of the instruction itself
1390 printType(I->getType());
1391
1392 // Print the type of each of the instruction's operands
1393 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1394 Value* operand = I->getOperand(i);
1395 printType(operand->getType());
1396 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1397 gvs.push_back(GV);
1398 else if (Constant* C = dyn_cast<Constant>(operand))
1399 consts.push_back(C);
1400 }
1401 }
1402 }
1403
1404 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001405 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001406 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1407 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001408 if (Function* Fun = dyn_cast<Function>(*I)) {
1409 if (!is_inline || Fun != F)
1410 printFunctionHead(Fun);
1411 }
Reid Spencer12803f52006-05-31 17:31:38 +00001412 }
1413
1414 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001415 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001416 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1417 I != E; ++I) {
1418 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1419 printVariableHead(F);
1420 }
1421
1422 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001423 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001424 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1425 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001426 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001427 }
1428
1429 // Process the global variables definitions now that all the constants have
1430 // been emitted. These definitions just couple the gvars with their constant
1431 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001432 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001433 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1434 I != E; ++I) {
1435 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1436 printVariableBody(GV);
1437 }
1438}
1439
1440void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001441 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001442 if (is_inline) {
1443 Out << " = mod->getFunction(\"";
1444 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001445 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1446 nl(Out) << "if (!" << getCppName(F) << ") {";
1447 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001448 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001449 Out<< " = new Function(";
1450 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1451 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001452 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001453 Out << ",";
1454 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001455 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001456 Out << "\", mod); " << (F->isExternal()? "// (external, no body)" : "");
1457 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001458 printCppName(F);
1459 Out << "->setCallingConv(";
1460 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001461 Out << ");";
1462 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001463 if (F->hasSection()) {
1464 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001465 Out << "->setSection(\"" << F->getSection() << "\");";
1466 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001467 }
1468 if (F->getAlignment()) {
1469 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001470 Out << "->setAlignment(" << F->getAlignment() << ");";
1471 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001472 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001473 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001474 Out << "}";
1475 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001476 }
Reid Spencer12803f52006-05-31 17:31:38 +00001477}
1478
1479void CppWriter::printFunctionBody(const Function *F) {
1480 if (F->isExternal())
1481 return; // external functions have no bodies.
1482
1483 // Clear the DefinedValues and ForwardRefs maps because we can't have
1484 // cross-function forward refs
1485 ForwardRefs.clear();
1486 DefinedValues.clear();
1487
1488 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001489 if (!is_inline) {
1490 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001491 Out << "Function::arg_iterator args = " << getCppName(F)
1492 << "->arg_begin();";
1493 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001494 }
1495 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1496 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001497 Out << "Value* " << getCppName(AI) << " = args++;";
1498 nl(Out);
1499 if (AI->hasName()) {
1500 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1501 nl(Out);
1502 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001503 }
Reid Spencer12803f52006-05-31 17:31:38 +00001504 }
1505
1506 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001507 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001508 for (Function::const_iterator BI = F->begin(), BE = F->end();
1509 BI != BE; ++BI) {
1510 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001511 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001512 if (BI->hasName())
1513 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001514 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1515 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001516 }
1517
1518 // Output all of its basic blocks... for the function
1519 for (Function::const_iterator BI = F->begin(), BE = F->end();
1520 BI != BE; ++BI) {
1521 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001522 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1523 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001524
1525 // Output all of the instructions in the basic block...
1526 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1527 I != E; ++I) {
1528 printInstruction(I,bbname);
1529 }
1530 }
1531
1532 // Loop over the ForwardRefs and resolve them now that all instructions
1533 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001534 if (!ForwardRefs.empty()) {
1535 nl(Out) << "// Resolve Forward References";
1536 nl(Out);
1537 }
1538
Reid Spencer12803f52006-05-31 17:31:38 +00001539 while (!ForwardRefs.empty()) {
1540 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001541 Out << I->second << "->replaceAllUsesWith("
1542 << getCppName(I->first) << "); delete " << I->second << ";";
1543 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001544 ForwardRefs.erase(I);
1545 }
1546}
1547
Reid Spencerf977e7b2006-06-01 23:43:47 +00001548void CppWriter::printInline(const std::string& fname, const std::string& func) {
1549 const Function* F = TheModule->getNamedFunction(func);
1550 if (!F) {
1551 error(std::string("Function '") + func + "' not found in input module");
1552 return;
1553 }
1554 if (F->isExternal()) {
1555 error(std::string("Function '") + func + "' is external!");
1556 return;
1557 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001558 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001559 << getCppName(F);
1560 unsigned arg_count = 1;
1561 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1562 AI != AE; ++AI) {
1563 Out << ", Value* arg_" << arg_count;
1564 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001565 Out << ") {";
1566 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001567 is_inline = true;
1568 printFunctionUses(F);
1569 printFunctionBody(F);
1570 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001571 Out << "return " << getCppName(F->begin()) << ";";
1572 nl(Out) << "}";
1573 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001574}
1575
Reid Spencer12803f52006-05-31 17:31:38 +00001576void CppWriter::printModuleBody() {
1577 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001578 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001579 printTypes(TheModule);
1580
1581 // Functions can call each other and global variables can reference them so
1582 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001583 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001584 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1585 I != E; ++I)
1586 printFunctionHead(I);
1587
1588 // Process the global variables declarations. We can't initialze them until
1589 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001590 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001591 for (Module::const_global_iterator I = TheModule->global_begin(),
1592 E = TheModule->global_end(); I != E; ++I) {
1593 printVariableHead(I);
1594 }
1595
1596 // Print out all the constants definitions. Constants don't recurse except
1597 // through GlobalValues. All GlobalValues have been declared at this point
1598 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001599 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001600 printConstants(TheModule);
1601
1602 // Process the global variables definitions now that all the constants have
1603 // been emitted. These definitions just couple the gvars with their constant
1604 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001605 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001606 for (Module::const_global_iterator I = TheModule->global_begin(),
1607 E = TheModule->global_end(); I != E; ++I) {
1608 printVariableBody(I);
1609 }
1610
1611 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001612 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001613 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1614 I != E; ++I) {
1615 if (!I->isExternal()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001616 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1617 << ")";
1618 nl(Out) << "{";
1619 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001620 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001621 nl(Out,-1) << "}";
1622 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001623 }
1624 }
1625}
1626
1627void CppWriter::printProgram(
1628 const std::string& fname,
1629 const std::string& mName
1630) {
1631 Out << "#include <llvm/Module.h>\n";
1632 Out << "#include <llvm/DerivedTypes.h>\n";
1633 Out << "#include <llvm/Constants.h>\n";
1634 Out << "#include <llvm/GlobalVariable.h>\n";
1635 Out << "#include <llvm/Function.h>\n";
1636 Out << "#include <llvm/CallingConv.h>\n";
1637 Out << "#include <llvm/BasicBlock.h>\n";
1638 Out << "#include <llvm/Instructions.h>\n";
1639 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001640 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001641 Out << "#include <llvm/Pass.h>\n";
1642 Out << "#include <llvm/PassManager.h>\n";
1643 Out << "#include <llvm/Analysis/Verifier.h>\n";
1644 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1645 Out << "#include <algorithm>\n";
1646 Out << "#include <iostream>\n\n";
1647 Out << "using namespace llvm;\n\n";
1648 Out << "Module* " << fname << "();\n\n";
1649 Out << "int main(int argc, char**argv) {\n";
1650 Out << " Module* Mod = makeLLVMModule();\n";
1651 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1652 Out << " std::cerr.flush();\n";
1653 Out << " std::cout.flush();\n";
1654 Out << " PassManager PM;\n";
1655 Out << " PM.add(new PrintModulePass(&std::cout));\n";
1656 Out << " PM.run(*Mod);\n";
1657 Out << " return 0;\n";
1658 Out << "}\n\n";
1659 printModule(fname,mName);
1660}
1661
1662void CppWriter::printModule(
1663 const std::string& fname,
1664 const std::string& mName
1665) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001666 nl(Out) << "Module* " << fname << "() {";
1667 nl(Out,1) << "// Module Construction";
1668 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1669 nl(Out) << "mod->setEndianness(";
Reid Spencer12803f52006-05-31 17:31:38 +00001670 switch (TheModule->getEndianness()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001671 case Module::LittleEndian: Out << "Module::LittleEndian);"; break;
1672 case Module::BigEndian: Out << "Module::BigEndian);"; break;
1673 case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001674 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001675 nl(Out) << "mod->setPointerSize(";
Reid Spencer12803f52006-05-31 17:31:38 +00001676 switch (TheModule->getPointerSize()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001677 case Module::Pointer32: Out << "Module::Pointer32);"; break;
1678 case Module::Pointer64: Out << "Module::Pointer64);"; break;
1679 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001680 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001681 nl(Out);
1682 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer12803f52006-05-31 17:31:38 +00001683 Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001684 << "\");";
1685 nl(Out);
1686 }
Reid Spencer12803f52006-05-31 17:31:38 +00001687
1688 if (!TheModule->getModuleInlineAsm().empty()) {
1689 Out << "mod->setModuleInlineAsm(\"";
1690 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001691 Out << "\");";
1692 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001693 }
1694
1695 // Loop over the dependent libraries and emit them.
1696 Module::lib_iterator LI = TheModule->lib_begin();
1697 Module::lib_iterator LE = TheModule->lib_end();
1698 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001699 Out << "mod->addLibrary(\"" << *LI << "\");";
1700 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001701 ++LI;
1702 }
1703 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001704 nl(Out) << "return mod;";
1705 nl(Out,-1) << "}";
1706 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001707}
1708
1709void CppWriter::printContents(
1710 const std::string& fname, // Name of generated function
1711 const std::string& mName // Name of module generated module
1712) {
1713 Out << "\nModule* " << fname << "(Module *mod) {\n";
1714 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001715 printModuleBody();
1716 Out << "\nreturn mod;\n";
1717 Out << "\n}\n";
1718}
1719
1720void CppWriter::printFunction(
1721 const std::string& fname, // Name of generated function
1722 const std::string& funcName // Name of function to generate
1723) {
1724 const Function* F = TheModule->getNamedFunction(funcName);
1725 if (!F) {
1726 error(std::string("Function '") + funcName + "' not found in input module");
1727 return;
1728 }
1729 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1730 printFunctionUses(F);
1731 printFunctionHead(F);
1732 printFunctionBody(F);
1733 Out << "return " << getCppName(F) << ";\n";
1734 Out << "}\n";
1735}
1736
1737void CppWriter::printVariable(
1738 const std::string& fname, /// Name of generated function
1739 const std::string& varName // Name of variable to generate
1740) {
1741 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1742
1743 if (!GV) {
1744 error(std::string("Variable '") + varName + "' not found in input module");
1745 return;
1746 }
1747 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1748 printVariableUses(GV);
1749 printVariableHead(GV);
1750 printVariableBody(GV);
1751 Out << "return " << getCppName(GV) << ";\n";
1752 Out << "}\n";
1753}
1754
1755void CppWriter::printType(
1756 const std::string& fname, /// Name of generated function
1757 const std::string& typeName // Name of type to generate
1758) {
1759 const Type* Ty = TheModule->getTypeByName(typeName);
1760 if (!Ty) {
1761 error(std::string("Type '") + typeName + "' not found in input module");
1762 return;
1763 }
1764 Out << "\nType* " << fname << "(Module *mod) {\n";
1765 printType(Ty);
1766 Out << "return " << getCppName(Ty) << ";\n";
1767 Out << "}\n";
1768}
1769
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001770} // end anonymous llvm
1771
1772namespace llvm {
1773
1774void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001775 // Initialize a CppWriter for us to use
1776 CppWriter W(o, mod);
1777
1778 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001779 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001780
1781 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001782 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001783
1784 // Get the name of the thing we are to generate
1785 std::string tgtname = NameToGenerate.getValue();
1786 if (GenerationType == GenModule ||
1787 GenerationType == GenContents ||
1788 GenerationType == GenProgram) {
1789 if (tgtname == "!bad!") {
1790 if (mod->getModuleIdentifier() == "-")
1791 tgtname = "<stdin>";
1792 else
1793 tgtname = mod->getModuleIdentifier();
1794 }
1795 } else if (tgtname == "!bad!") {
1796 W.error("You must use the -for option with -gen-{function,variable,type}");
1797 }
1798
1799 switch (WhatToGenerate(GenerationType)) {
1800 case GenProgram:
1801 if (fname.empty())
1802 fname = "makeLLVMModule";
1803 W.printProgram(fname,tgtname);
1804 break;
1805 case GenModule:
1806 if (fname.empty())
1807 fname = "makeLLVMModule";
1808 W.printModule(fname,tgtname);
1809 break;
1810 case GenContents:
1811 if (fname.empty())
1812 fname = "makeLLVMModuleContents";
1813 W.printContents(fname,tgtname);
1814 break;
1815 case GenFunction:
1816 if (fname.empty())
1817 fname = "makeLLVMFunction";
1818 W.printFunction(fname,tgtname);
1819 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001820 case GenInline:
1821 if (fname.empty())
1822 fname = "makeLLVMInline";
1823 W.printInline(fname,tgtname);
1824 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001825 case GenVariable:
1826 if (fname.empty())
1827 fname = "makeLLVMVariable";
1828 W.printVariable(fname,tgtname);
1829 break;
1830 case GenType:
1831 if (fname.empty())
1832 fname = "makeLLVMType";
1833 W.printType(fname,tgtname);
1834 break;
1835 default:
1836 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001837 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001838}
1839
1840}