blob: d954ccb948572e336c75e78ba59f58e94d7180d8 [file] [log] [blame]
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001//===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Reid Spencere0d133f2006-05-29 18:08:06 +00005// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the writing of the LLVM IR as a set of C++ calls to the
11// LLVM IR interface. The input module is assumed to be verified.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CallingConv.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/InlineAsm.h"
19#include "llvm/Instruction.h"
20#include "llvm/Instructions.h"
Reid Spencer6abd3da2007-04-11 09:54:08 +000021#include "llvm/ParameterAttributes.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000022#include "llvm/Module.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000023#include "llvm/TypeSymbolTable.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/STLExtras.h"
Reid Spencer15f7e012006-05-30 21:18:23 +000026#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000027#include "llvm/Support/CFG.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/MathExtras.h"
Reid Spencerf977e7b2006-06-01 23:43:47 +000030#include "llvm/Config/config.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000031#include <algorithm>
32#include <iostream>
Reid Spencer66c87342006-05-30 03:43:49 +000033#include <set>
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000034
35using namespace llvm;
36
Reid Spencer15f7e012006-05-30 21:18:23 +000037static cl::opt<std::string>
Reid Spencer15f7e012006-05-30 21:18:23 +000038FuncName("funcname", cl::desc("Specify the name of the generated function"),
39 cl::value_desc("function name"));
40
Reid Spencer12803f52006-05-31 17:31:38 +000041enum WhatToGenerate {
42 GenProgram,
43 GenModule,
44 GenContents,
45 GenFunction,
Reid Spencerf977e7b2006-06-01 23:43:47 +000046 GenInline,
Reid Spencer12803f52006-05-31 17:31:38 +000047 GenVariable,
48 GenType
49};
50
51static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
52 cl::desc("Choose what kind of output to generate"),
53 cl::init(GenProgram),
54 cl::values(
55 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
56 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
57 clEnumValN(GenContents,"gen-contents", "Generate contents of a module"),
58 clEnumValN(GenFunction,"gen-function", "Generate a function definition"),
Reid Spencerf977e7b2006-06-01 23:43:47 +000059 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
Reid Spencer12803f52006-05-31 17:31:38 +000060 clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"),
61 clEnumValN(GenType, "gen-type", "Generate a type definition"),
62 clEnumValEnd
63 )
64);
65
66static cl::opt<std::string> NameToGenerate("for", cl::Optional,
67 cl::desc("Specify the name of the thing to generate"),
68 cl::init("!bad!"));
Reid Spencer15f7e012006-05-30 21:18:23 +000069
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000070namespace {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000071typedef std::vector<const Type*> TypeList;
72typedef std::map<const Type*,std::string> TypeMap;
73typedef std::map<const Value*,std::string> ValueMap;
Reid Spencer66c87342006-05-30 03:43:49 +000074typedef std::set<std::string> NameSet;
Reid Spencer15f7e012006-05-30 21:18:23 +000075typedef std::set<const Type*> TypeSet;
76typedef std::set<const Value*> ValueSet;
77typedef std::map<const Value*,std::string> ForwardRefMap;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000078
Reid Spencere0d133f2006-05-29 18:08:06 +000079class CppWriter {
Reid Spencer12803f52006-05-31 17:31:38 +000080 const char* progname;
Reid Spencere0d133f2006-05-29 18:08:06 +000081 std::ostream &Out;
82 const Module *TheModule;
Andrew Lenharth539d8712006-05-31 20:18:56 +000083 uint64_t uniqueNum;
Reid Spencere0d133f2006-05-29 18:08:06 +000084 TypeMap TypeNames;
85 ValueMap ValueNames;
86 TypeMap UnresolvedTypes;
87 TypeList TypeStack;
Reid Spencer66c87342006-05-30 03:43:49 +000088 NameSet UsedNames;
Reid Spencer15f7e012006-05-30 21:18:23 +000089 TypeSet DefinedTypes;
90 ValueSet DefinedValues;
91 ForwardRefMap ForwardRefs;
Reid Spencerf977e7b2006-06-01 23:43:47 +000092 bool is_inline;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000093
Reid Spencere0d133f2006-05-29 18:08:06 +000094public:
Reid Spencer12803f52006-05-31 17:31:38 +000095 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
96 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
Reid Spencerf977e7b2006-06-01 23:43:47 +000097 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000098
Reid Spencere0d133f2006-05-29 18:08:06 +000099 const Module* getModule() { return TheModule; }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000100
Reid Spencer12803f52006-05-31 17:31:38 +0000101 void printProgram(const std::string& fname, const std::string& modName );
102 void printModule(const std::string& fname, const std::string& modName );
103 void printContents(const std::string& fname, const std::string& modName );
104 void printFunction(const std::string& fname, const std::string& funcName );
Reid Spencerf977e7b2006-06-01 23:43:47 +0000105 void printInline(const std::string& fname, const std::string& funcName );
Reid Spencer12803f52006-05-31 17:31:38 +0000106 void printVariable(const std::string& fname, const std::string& varName );
107 void printType(const std::string& fname, const std::string& typeName );
108
109 void error(const std::string& msg);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000110
Reid Spencere0d133f2006-05-29 18:08:06 +0000111private:
Reid Spencer12803f52006-05-31 17:31:38 +0000112 void printLinkageType(GlobalValue::LinkageTypes LT);
113 void printCallingConv(unsigned cc);
114 void printEscapedString(const std::string& str);
115 void printCFP(const ConstantFP* CFP);
116
117 std::string getCppName(const Type* val);
118 inline void printCppName(const Type* val);
119
120 std::string getCppName(const Value* val);
121 inline void printCppName(const Value* val);
122
123 bool printTypeInternal(const Type* Ty);
124 inline void printType(const Type* Ty);
Reid Spencere0d133f2006-05-29 18:08:06 +0000125 void printTypes(const Module* M);
Reid Spencer12803f52006-05-31 17:31:38 +0000126
Reid Spencere0d133f2006-05-29 18:08:06 +0000127 void printConstant(const Constant *CPV);
Reid Spencer12803f52006-05-31 17:31:38 +0000128 void printConstants(const Module* M);
129
130 void printVariableUses(const GlobalVariable *GV);
131 void printVariableHead(const GlobalVariable *GV);
132 void printVariableBody(const GlobalVariable *GV);
133
134 void printFunctionUses(const Function *F);
Reid Spencer66c87342006-05-30 03:43:49 +0000135 void printFunctionHead(const Function *F);
136 void printFunctionBody(const Function *F);
Reid Spencere0d133f2006-05-29 18:08:06 +0000137 void printInstruction(const Instruction *I, const std::string& bbname);
Reid Spencer15f7e012006-05-30 21:18:23 +0000138 std::string getOpName(Value*);
139
Reid Spencer12803f52006-05-31 17:31:38 +0000140 void printModuleBody();
141
Reid Spencere0d133f2006-05-29 18:08:06 +0000142};
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000143
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000144static unsigned indent_level = 0;
145inline std::ostream& nl(std::ostream& Out, int delta = 0) {
146 Out << "\n";
147 if (delta >= 0 || indent_level >= unsigned(-delta))
148 indent_level += delta;
149 for (unsigned i = 0; i < indent_level; ++i)
150 Out << " ";
151 return Out;
152}
153
154inline void in() { indent_level++; }
155inline void out() { if (indent_level >0) indent_level--; }
156
Reid Spencer12803f52006-05-31 17:31:38 +0000157inline void
158sanitize(std::string& str) {
159 for (size_t i = 0; i < str.length(); ++i)
160 if (!isalnum(str[i]) && str[i] != '_')
161 str[i] = '_';
162}
163
Reid Spencera54b7cb2007-01-12 07:05:14 +0000164inline std::string
Reid Spencer12803f52006-05-31 17:31:38 +0000165getTypePrefix(const Type* Ty ) {
Reid Spencer12803f52006-05-31 17:31:38 +0000166 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000167 case Type::VoidTyID: return "void_";
168 case Type::IntegerTyID:
169 return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
170 "_";
171 case Type::FloatTyID: return "float_";
172 case Type::DoubleTyID: return "double_";
173 case Type::LabelTyID: return "label_";
174 case Type::FunctionTyID: return "func_";
175 case Type::StructTyID: return "struct_";
176 case Type::ArrayTyID: return "array_";
177 case Type::PointerTyID: return "ptr_";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000178 case Type::VectorTyID: return "packed_";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000179 case Type::OpaqueTyID: return "opaque_";
180 default: return "other_";
Reid Spencer12803f52006-05-31 17:31:38 +0000181 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000182 return "unknown_";
Reid Spencer12803f52006-05-31 17:31:38 +0000183}
184
185// Looks up the type in the symbol table and returns a pointer to its name or
186// a null pointer if it wasn't found. Note that this isn't the same as the
187// Mode::getTypeName function which will return an empty string, not a null
188// pointer if the name is not found.
189inline const std::string*
Reid Spencer78d033e2007-01-06 07:24:44 +0000190findTypeName(const TypeSymbolTable& ST, const Type* Ty)
Reid Spencer12803f52006-05-31 17:31:38 +0000191{
Reid Spencer78d033e2007-01-06 07:24:44 +0000192 TypeSymbolTable::const_iterator TI = ST.begin();
193 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencer12803f52006-05-31 17:31:38 +0000194 for (;TI != TE; ++TI)
195 if (TI->second == Ty)
196 return &(TI->first);
197 return 0;
198}
199
200void
201CppWriter::error(const std::string& msg) {
202 std::cerr << progname << ": " << msg << "\n";
203 exit(2);
204}
205
Reid Spencer15f7e012006-05-30 21:18:23 +0000206// printCFP - Print a floating point constant .. very carefully :)
207// This makes sure that conversion to/from floating yields the same binary
208// result so that we don't lose precision.
209void
210CppWriter::printCFP(const ConstantFP *CFP) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000211 Out << "ConstantFP::get(";
212 if (CFP->getType() == Type::DoubleTy)
213 Out << "Type::DoubleTy, ";
214 else
215 Out << "Type::FloatTy, ";
Reid Spencer15f7e012006-05-30 21:18:23 +0000216#if HAVE_PRINTF_A
217 char Buffer[100];
218 sprintf(Buffer, "%A", CFP->getValue());
219 if ((!strncmp(Buffer, "0x", 2) ||
220 !strncmp(Buffer, "-0x", 3) ||
221 !strncmp(Buffer, "+0x", 3)) &&
222 (atof(Buffer) == CFP->getValue()))
Reid Spencerf977e7b2006-06-01 23:43:47 +0000223 if (CFP->getType() == Type::DoubleTy)
224 Out << "BitsToDouble(" << Buffer << ")";
225 else
226 Out << "BitsToFloat(" << Buffer << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000227 else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000228#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000229 std::string StrVal = ftostr(CFP->getValue());
230
231 while (StrVal[0] == ' ')
232 StrVal.erase(StrVal.begin());
233
234 // Check to make sure that the stringized number is not some string like
235 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
236 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
237 ((StrVal[0] == '-' || StrVal[0] == '+') &&
238 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
239 (atof(StrVal.c_str()) == CFP->getValue()))
240 if (CFP->getType() == Type::DoubleTy)
241 Out << StrVal;
242 else
243 Out << StrVal;
244 else if (CFP->getType() == Type::DoubleTy)
245 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
246 << std::dec << "ULL) /* " << StrVal << " */";
247 else
248 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
249 << std::dec << "U) /* " << StrVal << " */";
Reid Spencer15f7e012006-05-30 21:18:23 +0000250#if HAVE_PRINTF_A
251 }
252#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000253 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000254}
255
Reid Spencer12803f52006-05-31 17:31:38 +0000256void
257CppWriter::printCallingConv(unsigned cc){
258 // Print the calling convention.
259 switch (cc) {
260 case CallingConv::C: Out << "CallingConv::C"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000261 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
262 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
263 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
264 default: Out << cc; break;
265 }
266}
Reid Spencer15f7e012006-05-30 21:18:23 +0000267
Reid Spencer12803f52006-05-31 17:31:38 +0000268void
269CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
270 switch (LT) {
271 case GlobalValue::InternalLinkage:
272 Out << "GlobalValue::InternalLinkage"; break;
273 case GlobalValue::LinkOnceLinkage:
274 Out << "GlobalValue::LinkOnceLinkage "; break;
275 case GlobalValue::WeakLinkage:
276 Out << "GlobalValue::WeakLinkage"; break;
277 case GlobalValue::AppendingLinkage:
278 Out << "GlobalValue::AppendingLinkage"; break;
279 case GlobalValue::ExternalLinkage:
280 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000281 case GlobalValue::DLLImportLinkage:
282 Out << "GlobalValue::DllImportLinkage"; break;
283 case GlobalValue::DLLExportLinkage:
284 Out << "GlobalValue::DllExportLinkage"; break;
285 case GlobalValue::ExternalWeakLinkage:
286 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000287 case GlobalValue::GhostLinkage:
288 Out << "GlobalValue::GhostLinkage"; break;
289 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000290}
291
Reid Spencere0d133f2006-05-29 18:08:06 +0000292// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000293// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000294void
295CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000296 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
297 unsigned char C = Str[i];
298 if (isprint(C) && C != '"' && C != '\\') {
299 Out << C;
300 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000301 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000302 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
303 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
304 }
305 }
306}
307
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000308std::string
309CppWriter::getCppName(const Type* Ty)
310{
311 // First, handle the primitive types .. easy
Chris Lattner42a75512007-01-15 02:27:26 +0000312 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000313 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000314 case Type::VoidTyID: return "Type::VoidTy";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000315 case Type::IntegerTyID: {
316 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
317 return "IntegerType::get(" + utostr(BitWidth) + ")";
318 }
Reid Spencer71d2ec92006-12-31 06:02:26 +0000319 case Type::FloatTyID: return "Type::FloatTy";
320 case Type::DoubleTyID: return "Type::DoubleTy";
321 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000322 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000323 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000324 break;
325 }
326 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
327 }
328
329 // Now, see if we've seen the type before and return that
330 TypeMap::iterator I = TypeNames.find(Ty);
331 if (I != TypeNames.end())
332 return I->second;
333
334 // Okay, let's build a new name for this type. Start with a prefix
335 const char* prefix = 0;
336 switch (Ty->getTypeID()) {
337 case Type::FunctionTyID: prefix = "FuncTy_"; break;
338 case Type::StructTyID: prefix = "StructTy_"; break;
339 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
340 case Type::PointerTyID: prefix = "PointerTy_"; break;
341 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
Reid Spencerac9dcb92007-02-15 03:39:18 +0000342 case Type::VectorTyID: prefix = "VectorTy_"; break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000343 default: prefix = "OtherTy_"; break; // prevent breakage
344 }
345
346 // See if the type has a name in the symboltable and build accordingly
Reid Spencer78d033e2007-01-06 07:24:44 +0000347 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000348 std::string name;
349 if (tName)
350 name = std::string(prefix) + *tName;
351 else
352 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000353 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000354
355 // Save the name
356 return TypeNames[Ty] = name;
357}
358
Reid Spencer12803f52006-05-31 17:31:38 +0000359void
360CppWriter::printCppName(const Type* Ty)
361{
362 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000363}
364
Reid Spencer12803f52006-05-31 17:31:38 +0000365std::string
366CppWriter::getCppName(const Value* val) {
367 std::string name;
368 ValueMap::iterator I = ValueNames.find(val);
369 if (I != ValueNames.end() && I->first == val)
370 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000371
Reid Spencer12803f52006-05-31 17:31:38 +0000372 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
373 name = std::string("gvar_") +
374 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000375 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000376 name = std::string("func_");
377 } else if (const Constant* C = dyn_cast<Constant>(val)) {
378 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000379 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
380 if (is_inline) {
381 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
382 Function::const_arg_iterator(Arg)) + 1;
383 name = std::string("arg_") + utostr(argNum);
384 NameSet::iterator NI = UsedNames.find(name);
385 if (NI != UsedNames.end())
386 name += std::string("_") + utostr(uniqueNum++);
387 UsedNames.insert(name);
388 return ValueNames[val] = name;
389 } else {
390 name = getTypePrefix(val->getType());
391 }
Reid Spencer12803f52006-05-31 17:31:38 +0000392 } else {
393 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000394 }
Reid Spencer12803f52006-05-31 17:31:38 +0000395 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
396 sanitize(name);
397 NameSet::iterator NI = UsedNames.find(name);
398 if (NI != UsedNames.end())
399 name += std::string("_") + utostr(uniqueNum++);
400 UsedNames.insert(name);
401 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000402}
403
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000404void
Reid Spencer12803f52006-05-31 17:31:38 +0000405CppWriter::printCppName(const Value* val) {
406 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000407}
408
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000409bool
Reid Spencer12803f52006-05-31 17:31:38 +0000410CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000411 // We don't print definitions for primitive types
Chris Lattner42a75512007-01-15 02:27:26 +0000412 if (Ty->isPrimitiveType() || Ty->isInteger())
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000413 return false;
414
Reid Spencer15f7e012006-05-30 21:18:23 +0000415 // If we already defined this type, we don't need to define it again.
416 if (DefinedTypes.find(Ty) != DefinedTypes.end())
417 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000418
Reid Spencer15f7e012006-05-30 21:18:23 +0000419 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000420 std::string typeName(getCppName(Ty));
421
422 // Search the type stack for recursion. If we find it, then generate this
423 // as an OpaqueType, but make sure not to do this multiple times because
424 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000425 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000426 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000427 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
428 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000429 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
430 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000431 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
432 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000433 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000434 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000435 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000436 }
437
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000438 // We're going to print a derived type which, by definition, contains other
439 // types. So, push this one we're printing onto the type stack to assist with
440 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000441 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000442
443 // Print the type definition
444 switch (Ty->getTypeID()) {
445 case Type::FunctionTyID: {
446 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000447 Out << "std::vector<const Type*>" << typeName << "_args;";
448 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000449 FunctionType::param_iterator PI = FT->param_begin();
450 FunctionType::param_iterator PE = FT->param_end();
451 for (; PI != PE; ++PI) {
452 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000453 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000454 std::string argName(getCppName(argTy));
455 Out << typeName << "_args.push_back(" << argName;
456 if (isForward)
457 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000458 Out << ");";
459 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000460 }
Reid Spencer6abd3da2007-04-11 09:54:08 +0000461 const ParamAttrsList *PAL = FT->getParamAttrs();
462 Out << "ParamAttrsList *" << typeName << "_PAL = 0;";
Reid Spencera9297b12007-04-11 10:01:32 +0000463 nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000464 if (PAL && !PAL->empty()) {
465 Out << typeName << "_PAL = new ParamAttrsList();";
Reid Spencera9297b12007-04-11 10:01:32 +0000466 nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000467 for (unsigned i = 0; i < PAL->size(); ++i) {
468 uint16_t index = PAL->getParamIndex(i);
469 uint16_t attrs = PAL->getParamAttrs(index);
470 Out << typeName << "_PAL->addAttribute(" << index << ", 0";
471 if (attrs & ParamAttr::SExt)
472 Out << " | ParamAttr::SExt";
473 if (attrs & ParamAttr::ZExt)
474 Out << " | ParamAttr::ZExt";
475 if (attrs & ParamAttr::StructRet)
476 Out << " | ParamAttr::StructRet";
477 if (attrs & ParamAttr::InReg)
478 Out << " | ParamAttr::InReg";
479 if (attrs & ParamAttr::NoReturn)
480 Out << " | ParamAttr::NoReturn";
481 if (attrs & ParamAttr::NoUnwind)
482 Out << " | ParamAttr::NoUnwind";
483 Out << ");";
Reid Spencera9297b12007-04-11 10:01:32 +0000484 nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000485 }
486 }
Reid Spencer12803f52006-05-31 17:31:38 +0000487 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000488 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000489 Out << "FunctionType* " << typeName << " = FunctionType::get(";
490 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000491 if (isForward)
492 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000493 Out << ",";
494 nl(Out) << "/*Params=*/" << typeName << "_args,";
Reid Spencer6abd3da2007-04-11 09:54:08 +0000495 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") ;
Reid Spencera9297b12007-04-11 10:01:32 +0000496 nl(Out) << "/*ParamAttrs=*/" << typeName << "_PAL" << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000497 out();
498 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000499 break;
500 }
501 case Type::StructTyID: {
502 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000503 Out << "std::vector<const Type*>" << typeName << "_fields;";
504 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000505 StructType::element_iterator EI = ST->element_begin();
506 StructType::element_iterator EE = ST->element_end();
507 for (; EI != EE; ++EI) {
508 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000509 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000510 std::string fieldName(getCppName(fieldTy));
511 Out << typeName << "_fields.push_back(" << fieldName;
512 if (isForward)
513 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000514 Out << ");";
515 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000516 }
517 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000518 << typeName << "_fields);";
519 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000520 break;
521 }
522 case Type::ArrayTyID: {
523 const ArrayType* AT = cast<ArrayType>(Ty);
524 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000525 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000526 std::string elemName(getCppName(ET));
527 Out << "ArrayType* " << typeName << " = ArrayType::get("
528 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000529 << ", " << utostr(AT->getNumElements()) << ");";
530 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000531 break;
532 }
533 case Type::PointerTyID: {
534 const PointerType* PT = cast<PointerType>(Ty);
535 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000536 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000537 std::string elemName(getCppName(ET));
538 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000539 << elemName << (isForward ? "_fwd" : "") << ");";
540 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000541 break;
542 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000543 case Type::VectorTyID: {
544 const VectorType* PT = cast<VectorType>(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000545 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000546 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000547 std::string elemName(getCppName(ET));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000548 Out << "VectorType* " << typeName << " = VectorType::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000549 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000550 << ", " << utostr(PT->getNumElements()) << ");";
551 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000552 break;
553 }
554 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000555 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
556 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000557 break;
558 }
559 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000560 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000561 }
562
Reid Spencer74e032a2006-05-29 02:58:15 +0000563 // If the type had a name, make sure we recreate it.
564 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000565 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer74e032a2006-05-29 02:58:15 +0000566 if (progTypeName)
567 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000568 << typeName << ");";
569 nl(Out);
Reid Spencer74e032a2006-05-29 02:58:15 +0000570
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000571 // Pop us off the type stack
572 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000573
574 // Indicate that this type is now defined.
575 DefinedTypes.insert(Ty);
576
577 // Early resolve as many unresolved types as possible. Search the unresolved
578 // types map for the type we just printed. Now that its definition is complete
579 // we can resolve any previous references to it. This prevents a cascade of
580 // unresolved types.
581 TypeMap::iterator I = UnresolvedTypes.find(Ty);
582 if (I != UnresolvedTypes.end()) {
583 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000584 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
585 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000586 Out << I->second << " = cast<";
587 switch (Ty->getTypeID()) {
588 case Type::FunctionTyID: Out << "FunctionType"; break;
589 case Type::ArrayTyID: Out << "ArrayType"; break;
590 case Type::StructTyID: Out << "StructType"; break;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000591 case Type::VectorTyID: Out << "VectorType"; break;
Reid Spencer15f7e012006-05-30 21:18:23 +0000592 case Type::PointerTyID: Out << "PointerType"; break;
593 case Type::OpaqueTyID: Out << "OpaqueType"; break;
594 default: Out << "NoSuchDerivedType"; break;
595 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000596 Out << ">(" << I->second << "_fwd.get());";
597 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000598 UnresolvedTypes.erase(I);
599 }
600
601 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000602 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000603
604 // We weren't a recursive type
605 return false;
606}
607
Reid Spencer12803f52006-05-31 17:31:38 +0000608// Prints a type definition. Returns true if it could not resolve all the types
609// in the definition but had to use a forward reference.
610void
611CppWriter::printType(const Type* Ty) {
612 assert(TypeStack.empty());
613 TypeStack.clear();
614 printTypeInternal(Ty);
615 assert(TypeStack.empty());
616}
617
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000618void
619CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000620
621 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000622 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
623 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
624 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000625
626 // For primitive types and types already defined, just add a name
627 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Chris Lattner42a75512007-01-15 02:27:26 +0000628 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000629 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000630 Out << "mod->addTypeName(\"";
631 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000632 Out << "\", " << getCppName(TI->second) << ");";
633 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000634 // For everything else, define the type
635 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000636 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000637 }
638 }
639
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000640 // Add all of the global variables to the value table...
641 for (Module::const_global_iterator I = TheModule->global_begin(),
642 E = TheModule->global_end(); I != E; ++I) {
643 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000644 printType(I->getInitializer()->getType());
645 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000646 }
647
648 // Add all the functions to the table
649 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
650 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000651 printType(FI->getReturnType());
652 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000653 // Add all the function arguments
654 for(Function::const_arg_iterator AI = FI->arg_begin(),
655 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000656 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000657 }
658
659 // Add all of the basic blocks and instructions
660 for (Function::const_iterator BB = FI->begin(),
661 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000662 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000663 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
664 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000665 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000666 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000667 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000668 }
669 }
670 }
671}
672
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000673
Reid Spencere0d133f2006-05-29 18:08:06 +0000674// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000675void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000676 // First, if the constant is actually a GlobalValue (variable or function) or
677 // its already in the constant list then we've printed it already and we can
678 // just return.
679 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000680 return;
681
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000682 std::string constName(getCppName(CV));
683 std::string typeName(getCppName(CV->getType()));
684 if (CV->isNullValue()) {
685 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000686 << typeName << ");";
687 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000688 return;
689 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000690 if (isa<GlobalValue>(CV)) {
691 // Skip variables and functions, we emit them elsewhere
692 return;
693 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000694 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattnerf6e7bb42007-01-12 18:37:29 +0000695 Out << "ConstantInt* " << constName << " = ConstantInt::get("
Reid Spencer70297252007-03-01 20:55:43 +0000696 << "APInt(cast<IntegerTyp>(" << typeName << ")->getBitWidth(),"
697 << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000698 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000699 Out << "ConstantAggregateZero* " << constName
700 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000701 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000702 Out << "ConstantPointerNull* " << constName
703 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000704 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000705 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000706 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000707 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000708 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000709 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000710 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000711 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000712 // Determine if we want null termination or not.
713 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000714 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000715 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000716 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000717 Out << ");";
718 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000719 Out << "std::vector<Constant*> " << constName << "_elems;";
720 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000721 unsigned N = CA->getNumOperands();
722 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000723 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000724 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000725 << getCppName(CA->getOperand(i)) << ");";
726 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000727 }
728 Out << "Constant* " << constName << " = ConstantArray::get("
729 << typeName << ", " << constName << "_elems);";
730 }
731 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000732 Out << "std::vector<Constant*> " << constName << "_fields;";
733 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000734 unsigned N = CS->getNumOperands();
735 for (unsigned i = 0; i < N; i++) {
736 printConstant(CS->getOperand(i));
737 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000738 << getCppName(CS->getOperand(i)) << ");";
739 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000740 }
741 Out << "Constant* " << constName << " = ConstantStruct::get("
742 << typeName << ", " << constName << "_fields);";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000743 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000744 Out << "std::vector<Constant*> " << constName << "_elems;";
745 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000746 unsigned N = CP->getNumOperands();
747 for (unsigned i = 0; i < N; ++i) {
748 printConstant(CP->getOperand(i));
749 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000750 << getCppName(CP->getOperand(i)) << ");";
751 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000752 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000753 Out << "Constant* " << constName << " = ConstantVector::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000754 << typeName << ", " << constName << "_elems);";
755 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000756 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000757 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000758 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000759 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000760 Out << "std::vector<Constant*> " << constName << "_indices;";
761 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000762 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000763 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000764 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000765 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000766 << getCppName(CE->getOperand(i)) << ");";
767 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000768 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000769 Out << "Constant* " << constName
770 << " = ConstantExpr::getGetElementPtr("
771 << getCppName(CE->getOperand(0)) << ", "
772 << constName << "_indices);";
Reid Spencer3da59db2006-11-27 01:05:10 +0000773 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000774 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000775 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000776 switch (CE->getOpcode()) {
777 default: assert(0 && "Invalid cast opcode");
778 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
779 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
780 case Instruction::SExt: Out << "Instruction::SExt"; break;
781 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
782 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
783 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
784 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
785 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
786 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
787 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
788 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
789 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
790 }
791 Out << ", " << getCppName(CE->getOperand(0)) << ", "
792 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000793 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000794 unsigned N = CE->getNumOperands();
795 for (unsigned i = 0; i < N; ++i ) {
796 printConstant(CE->getOperand(i));
797 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000798 Out << "Constant* " << constName << " = ConstantExpr::";
799 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000800 case Instruction::Add: Out << "getAdd("; break;
801 case Instruction::Sub: Out << "getSub("; break;
802 case Instruction::Mul: Out << "getMul("; break;
803 case Instruction::UDiv: Out << "getUDiv("; break;
804 case Instruction::SDiv: Out << "getSDiv("; break;
805 case Instruction::FDiv: Out << "getFDiv("; break;
806 case Instruction::URem: Out << "getURem("; break;
807 case Instruction::SRem: Out << "getSRem("; break;
808 case Instruction::FRem: Out << "getFRem("; break;
809 case Instruction::And: Out << "getAnd("; break;
810 case Instruction::Or: Out << "getOr("; break;
811 case Instruction::Xor: Out << "getXor("; break;
812 case Instruction::ICmp:
813 Out << "getICmp(ICmpInst::ICMP_";
814 switch (CE->getPredicate()) {
815 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
816 case ICmpInst::ICMP_NE: Out << "NE"; break;
817 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
818 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
819 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
820 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
821 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
822 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
823 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
824 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
825 default: error("Invalid ICmp Predicate");
826 }
827 break;
828 case Instruction::FCmp:
829 Out << "getFCmp(FCmpInst::FCMP_";
830 switch (CE->getPredicate()) {
831 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
832 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
833 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
834 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
835 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
836 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
837 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
838 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
839 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
840 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
841 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
842 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
843 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
844 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
845 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
846 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
847 default: error("Invalid FCmp Predicate");
848 }
849 break;
850 case Instruction::Shl: Out << "getShl("; break;
851 case Instruction::LShr: Out << "getLShr("; break;
852 case Instruction::AShr: Out << "getAShr("; break;
853 case Instruction::Select: Out << "getSelect("; break;
854 case Instruction::ExtractElement: Out << "getExtractElement("; break;
855 case Instruction::InsertElement: Out << "getInsertElement("; break;
856 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000857 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000858 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000859 break;
860 }
861 Out << getCppName(CE->getOperand(0));
862 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
863 Out << ", " << getCppName(CE->getOperand(i));
864 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000865 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000866 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000867 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000868 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000869 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000870 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000871}
872
Reid Spencer12803f52006-05-31 17:31:38 +0000873void
874CppWriter::printConstants(const Module* M) {
875 // Traverse all the global variables looking for constant initializers
876 for (Module::const_global_iterator I = TheModule->global_begin(),
877 E = TheModule->global_end(); I != E; ++I)
878 if (I->hasInitializer())
879 printConstant(I->getInitializer());
880
881 // Traverse the LLVM functions looking for constants
882 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
883 FI != FE; ++FI) {
884 // Add all of the basic blocks and instructions
885 for (Function::const_iterator BB = FI->begin(),
886 E = FI->end(); BB != E; ++BB) {
887 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
888 ++I) {
889 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
890 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
891 printConstant(C);
892 }
893 }
894 }
895 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000896 }
Reid Spencer66c87342006-05-30 03:43:49 +0000897}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000898
Reid Spencer12803f52006-05-31 17:31:38 +0000899void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000900 nl(Out) << "// Type Definitions";
901 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000902 printType(GV->getType());
903 if (GV->hasInitializer()) {
904 Constant* Init = GV->getInitializer();
905 printType(Init->getType());
906 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000907 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000908 printFunctionHead(F);
909 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000910 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000911 printVariableHead(gv);
912 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000913 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000914 printConstant(gv);
915 }
916 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000917 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000918 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000919 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000920 }
Reid Spencer12803f52006-05-31 17:31:38 +0000921}
Reid Spencer15f7e012006-05-30 21:18:23 +0000922
Reid Spencer12803f52006-05-31 17:31:38 +0000923void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000924 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000925 if (is_inline) {
926 Out << " = mod->getGlobalVariable(";
927 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000928 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
929 nl(Out) << "if (!" << getCppName(GV) << ") {";
930 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000931 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000932 Out << " = new GlobalVariable(";
933 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000934 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000935 Out << ",";
936 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
937 Out << ",";
938 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000939 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000940 Out << ",";
941 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000942 if (GV->hasInitializer()) {
943 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000944 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000945 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000946 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000947 Out << "\",";
948 nl(Out) << "mod);";
949 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000950
951 if (GV->hasSection()) {
952 printCppName(GV);
953 Out << "->setSection(\"";
954 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000955 Out << "\");";
956 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000957 }
958 if (GV->getAlignment()) {
959 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000960 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
961 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000962 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000963 if (is_inline) {
964 out(); Out << "}"; nl(Out);
965 }
Reid Spencer12803f52006-05-31 17:31:38 +0000966}
967
968void
969CppWriter::printVariableBody(const GlobalVariable *GV) {
970 if (GV->hasInitializer()) {
971 printCppName(GV);
972 Out << "->setInitializer(";
973 //if (!isa<GlobalValue(GV->getInitializer()))
974 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000975 Out << getCppName(GV->getInitializer()) << ");";
976 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000977 }
978}
979
980std::string
981CppWriter::getOpName(Value* V) {
982 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
983 return getCppName(V);
984
985 // See if its alread in the map of forward references, if so just return the
986 // name we already set up for it
987 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
988 if (I != ForwardRefs.end())
989 return I->second;
990
991 // This is a new forward reference. Generate a unique name for it
992 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
993
994 // Yes, this is a hack. An Argument is the smallest instantiable value that
995 // we can make as a placeholder for the real value. We'll replace these
996 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000997 Out << "Argument* " << result << " = new Argument("
998 << getCppName(V->getType()) << ");";
999 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001000 ForwardRefs[V] = result;
1001 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001002}
1003
Reid Spencere0d133f2006-05-29 18:08:06 +00001004// printInstruction - This member is called for each Instruction in a function.
1005void
Reid Spencer12803f52006-05-31 17:31:38 +00001006CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +00001007 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001008
Reid Spencer15f7e012006-05-30 21:18:23 +00001009 // Before we emit this instruction, we need to take care of generating any
1010 // forward references. So, we get the names of all the operands in advance
1011 std::string* opNames = new std::string[I->getNumOperands()];
1012 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1013 opNames[i] = getOpName(I->getOperand(i));
1014 }
1015
Reid Spencere0d133f2006-05-29 18:08:06 +00001016 switch (I->getOpcode()) {
1017 case Instruction::Ret: {
1018 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001019 Out << "ReturnInst* " << iName << " = new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001020 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001021 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001022 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001023 case Instruction::Br: {
1024 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001025 Out << "BranchInst* " << iName << " = new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001026 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001027 Out << opNames[0] << ", "
1028 << opNames[1] << ", "
1029 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001030
Reid Spencere0d133f2006-05-29 18:08:06 +00001031 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001032 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001033 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001034 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001035 }
1036 Out << bbname << ");";
1037 break;
1038 }
Reid Spencer66c87342006-05-30 03:43:49 +00001039 case Instruction::Switch: {
1040 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001041 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001042 << opNames[0] << ", "
1043 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001044 << sw->getNumCases() << ", " << bbname << ");";
1045 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001046 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001047 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001048 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001049 << opNames[i+1] << ");";
1050 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001051 }
1052 break;
1053 }
1054 case Instruction::Invoke: {
1055 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001056 Out << "std::vector<Value*> " << iName << "_params;";
1057 nl(Out);
1058 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1059 Out << iName << "_params.push_back("
1060 << opNames[i] << ");";
1061 nl(Out);
1062 }
1063 Out << "InvokeInst* " << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001064 << opNames[0] << ", "
1065 << opNames[1] << ", "
1066 << opNames[2] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001067 << iName << "_params, \"";
1068 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001069 Out << "\", " << bbname << ");";
1070 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001071 printCallingConv(inv->getCallingConv());
1072 Out << ");";
1073 break;
1074 }
1075 case Instruction::Unwind: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001076 Out << "UnwindInst* " << iName << " = new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001077 << bbname << ");";
1078 break;
1079 }
1080 case Instruction::Unreachable:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001081 Out << "UnreachableInst* " << iName << " = new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001082 << bbname << ");";
1083 break;
1084 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001085 case Instruction::Add:
1086 case Instruction::Sub:
1087 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001088 case Instruction::UDiv:
1089 case Instruction::SDiv:
1090 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001091 case Instruction::URem:
1092 case Instruction::SRem:
1093 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001094 case Instruction::And:
1095 case Instruction::Or:
1096 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001097 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001098 case Instruction::LShr:
1099 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001100 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001101 switch (I->getOpcode()) {
1102 case Instruction::Add: Out << "Instruction::Add"; break;
1103 case Instruction::Sub: Out << "Instruction::Sub"; break;
1104 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001105 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1106 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1107 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001108 case Instruction::URem:Out << "Instruction::URem"; break;
1109 case Instruction::SRem:Out << "Instruction::SRem"; break;
1110 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001111 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001112 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001113 case Instruction::Xor: Out << "Instruction::Xor"; break;
1114 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001115 case Instruction::LShr:Out << "Instruction::LShr"; break;
1116 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001117 default: Out << "Instruction::BadOpCode"; break;
1118 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001119 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001120 printEscapedString(I->getName());
1121 Out << "\", " << bbname << ");";
1122 break;
1123 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001124 case Instruction::FCmp: {
1125 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1126 switch (cast<FCmpInst>(I)->getPredicate()) {
1127 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1128 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1129 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1130 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1131 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1132 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1133 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1134 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1135 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1136 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1137 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1138 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1139 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1140 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1141 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1142 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1143 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1144 }
1145 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1146 printEscapedString(I->getName());
1147 Out << "\", " << bbname << ");";
1148 break;
1149 }
1150 case Instruction::ICmp: {
1151 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1152 switch (cast<ICmpInst>(I)->getPredicate()) {
1153 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1154 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1155 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1156 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1157 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1158 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1159 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1160 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1161 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1162 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1163 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001164 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001165 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001166 printEscapedString(I->getName());
1167 Out << "\", " << bbname << ");";
1168 break;
1169 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001170 case Instruction::Malloc: {
1171 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001172 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001173 << getCppName(mallocI->getAllocatedType()) << ", ";
1174 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001175 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001176 Out << "\"";
1177 printEscapedString(mallocI->getName());
1178 Out << "\", " << bbname << ");";
1179 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001180 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001181 << mallocI->getAlignment() << ");";
1182 break;
1183 }
Reid Spencer66c87342006-05-30 03:43:49 +00001184 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001185 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001186 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1187 break;
1188 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001189 case Instruction::Alloca: {
1190 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001191 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001192 << getCppName(allocaI->getAllocatedType()) << ", ";
1193 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001194 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001195 Out << "\"";
1196 printEscapedString(allocaI->getName());
1197 Out << "\", " << bbname << ");";
1198 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001199 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001200 << allocaI->getAlignment() << ");";
1201 break;
1202 }
Reid Spencer66c87342006-05-30 03:43:49 +00001203 case Instruction::Load:{
1204 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001205 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001206 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001207 printEscapedString(load->getName());
1208 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001209 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001210 break;
1211 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001212 case Instruction::Store: {
1213 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001214 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001215 << opNames[0] << ", "
1216 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001217 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001218 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001219 break;
1220 }
1221 case Instruction::GetElementPtr: {
1222 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1223 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001224 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001225 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001226 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001227 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001228 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001229 Out << "std::vector<Value*> " << iName << "_indices;";
1230 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001231 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001232 Out << iName << "_indices.push_back("
1233 << opNames[i] << ");";
1234 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001235 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001236 Out << "Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001237 << opNames[0] << ", " << iName << "_indices";
Reid Spencere0d133f2006-05-29 18:08:06 +00001238 }
1239 Out << ", \"";
1240 printEscapedString(gep->getName());
1241 Out << "\", " << bbname << ");";
1242 break;
1243 }
Reid Spencer66c87342006-05-30 03:43:49 +00001244 case Instruction::PHI: {
1245 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001246
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001247 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001248 << getCppName(phi->getType()) << ", \"";
1249 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001250 Out << "\", " << bbname << ");";
1251 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001252 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001253 << ");";
1254 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001255 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001256 Out << iName << "->addIncoming("
1257 << opNames[i] << ", " << opNames[i+1] << ");";
1258 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001259 }
Reid Spencer66c87342006-05-30 03:43:49 +00001260 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001261 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001262 case Instruction::Trunc:
1263 case Instruction::ZExt:
1264 case Instruction::SExt:
1265 case Instruction::FPTrunc:
1266 case Instruction::FPExt:
1267 case Instruction::FPToUI:
1268 case Instruction::FPToSI:
1269 case Instruction::UIToFP:
1270 case Instruction::SIToFP:
1271 case Instruction::PtrToInt:
1272 case Instruction::IntToPtr:
1273 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001274 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001275 Out << "CastInst* " << iName << " = new ";
1276 switch (I->getOpcode()) {
Reid Spencer34816572007-02-16 06:34:39 +00001277 case Instruction::Trunc: Out << "TruncInst"; break;
1278 case Instruction::ZExt: Out << "ZExtInst"; break;
1279 case Instruction::SExt: Out << "SExtInst"; break;
1280 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1281 case Instruction::FPExt: Out << "FPExtInst"; break;
1282 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1283 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1284 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1285 case Instruction::SIToFP: Out << "SIToFPInst"; break;
1286 case Instruction::PtrToInt: Out << "PtrToInst"; break;
1287 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1288 case Instruction::BitCast: Out << "BitCastInst"; break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001289 default: assert(!"Unreachable"); break;
1290 }
1291 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001292 << getCppName(cst->getType()) << ", \"";
1293 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001294 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001295 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001296 }
Reid Spencer66c87342006-05-30 03:43:49 +00001297 case Instruction::Call:{
1298 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001299 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001300 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001301 << getCppName(ila->getFunctionType()) << ", \""
1302 << ila->getAsmString() << "\", \""
1303 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001304 << (ila->hasSideEffects() ? "true" : "false") << ");";
1305 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001306 }
Reid Spencer66c87342006-05-30 03:43:49 +00001307 if (call->getNumOperands() > 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001308 Out << "std::vector<Value*> " << iName << "_params;";
1309 nl(Out);
1310 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1311 Out << iName << "_params.push_back(" << opNames[i] << ");";
1312 nl(Out);
1313 }
1314 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001315 << opNames[0] << ", " << iName << "_params, \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001316 } else if (call->getNumOperands() == 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001317 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001318 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001319 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001320 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001321 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001322 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001323 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001324 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001325 }
1326 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001327 Out << "\", " << bbname << ");";
1328 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001329 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001330 Out << ");";
1331 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001332 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001333 Out << ");";
1334 break;
1335 }
1336 case Instruction::Select: {
1337 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001338 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001339 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001340 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001341 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001342 break;
1343 }
1344 case Instruction::UserOp1:
1345 /// FALL THROUGH
1346 case Instruction::UserOp2: {
1347 /// FIXME: What should be done here?
1348 break;
1349 }
1350 case Instruction::VAArg: {
1351 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001352 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001353 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001354 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001355 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001356 break;
1357 }
1358 case Instruction::ExtractElement: {
1359 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001360 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001361 << " = new ExtractElementInst(" << opNames[0]
1362 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001363 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001364 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001365 break;
1366 }
1367 case Instruction::InsertElement: {
1368 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001369 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001370 << " = new InsertElementInst(" << opNames[0]
1371 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001372 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001373 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001374 break;
1375 }
1376 case Instruction::ShuffleVector: {
1377 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001378 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001379 << " = new ShuffleVectorInst(" << opNames[0]
1380 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001381 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001382 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001383 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001384 }
1385 }
Reid Spencer68464b72006-06-15 16:09:59 +00001386 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001387 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001388 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001389}
1390
Reid Spencer12803f52006-05-31 17:31:38 +00001391// Print out the types, constants and declarations needed by one function
1392void CppWriter::printFunctionUses(const Function* F) {
1393
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001394 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001395 if (!is_inline) {
1396 // Print the function's return type
1397 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001398
Reid Spencerf977e7b2006-06-01 23:43:47 +00001399 // Print the function's function type
1400 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001401
Reid Spencerf977e7b2006-06-01 23:43:47 +00001402 // Print the types of each of the function's arguments
1403 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1404 AI != AE; ++AI) {
1405 printType(AI->getType());
1406 }
Reid Spencer12803f52006-05-31 17:31:38 +00001407 }
1408
1409 // Print type definitions for every type referenced by an instruction and
1410 // make a note of any global values or constants that are referenced
1411 std::vector<GlobalValue*> gvs;
1412 std::vector<Constant*> consts;
1413 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1414 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1415 I != E; ++I) {
1416 // Print the type of the instruction itself
1417 printType(I->getType());
1418
1419 // Print the type of each of the instruction's operands
1420 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1421 Value* operand = I->getOperand(i);
1422 printType(operand->getType());
1423 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1424 gvs.push_back(GV);
1425 else if (Constant* C = dyn_cast<Constant>(operand))
1426 consts.push_back(C);
1427 }
1428 }
1429 }
1430
1431 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001432 nl(Out) << "// Function Declarations"; 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) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001435 if (Function* Fun = dyn_cast<Function>(*I)) {
1436 if (!is_inline || Fun != F)
1437 printFunctionHead(Fun);
1438 }
Reid Spencer12803f52006-05-31 17:31:38 +00001439 }
1440
1441 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001442 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001443 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1444 I != E; ++I) {
1445 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1446 printVariableHead(F);
1447 }
1448
1449 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001450 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001451 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1452 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001453 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001454 }
1455
1456 // Process the global variables definitions now that all the constants have
1457 // been emitted. These definitions just couple the gvars with their constant
1458 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001459 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001460 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1461 I != E; ++I) {
1462 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1463 printVariableBody(GV);
1464 }
1465}
1466
1467void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001468 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001469 if (is_inline) {
1470 Out << " = mod->getFunction(\"";
1471 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001472 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1473 nl(Out) << "if (!" << getCppName(F) << ") {";
1474 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001475 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001476 Out<< " = new Function(";
1477 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1478 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001479 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001480 Out << ",";
1481 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001482 printEscapedString(F->getName());
Reid Spencer5cbf9852007-01-30 20:08:39 +00001483 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001484 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001485 printCppName(F);
1486 Out << "->setCallingConv(";
1487 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001488 Out << ");";
1489 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001490 if (F->hasSection()) {
1491 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001492 Out << "->setSection(\"" << F->getSection() << "\");";
1493 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001494 }
1495 if (F->getAlignment()) {
1496 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001497 Out << "->setAlignment(" << F->getAlignment() << ");";
1498 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001499 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001500 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001501 Out << "}";
1502 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001503 }
Reid Spencer12803f52006-05-31 17:31:38 +00001504}
1505
1506void CppWriter::printFunctionBody(const Function *F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001507 if (F->isDeclaration())
Reid Spencer12803f52006-05-31 17:31:38 +00001508 return; // external functions have no bodies.
1509
1510 // Clear the DefinedValues and ForwardRefs maps because we can't have
1511 // cross-function forward refs
1512 ForwardRefs.clear();
1513 DefinedValues.clear();
1514
1515 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001516 if (!is_inline) {
1517 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001518 Out << "Function::arg_iterator args = " << getCppName(F)
1519 << "->arg_begin();";
1520 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001521 }
1522 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1523 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001524 Out << "Value* " << getCppName(AI) << " = args++;";
1525 nl(Out);
1526 if (AI->hasName()) {
1527 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1528 nl(Out);
1529 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001530 }
Reid Spencer12803f52006-05-31 17:31:38 +00001531 }
1532
1533 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001534 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001535 for (Function::const_iterator BI = F->begin(), BE = F->end();
1536 BI != BE; ++BI) {
1537 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001538 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001539 if (BI->hasName())
1540 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001541 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1542 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001543 }
1544
1545 // Output all of its basic blocks... for the function
1546 for (Function::const_iterator BI = F->begin(), BE = F->end();
1547 BI != BE; ++BI) {
1548 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001549 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1550 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001551
1552 // Output all of the instructions in the basic block...
1553 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1554 I != E; ++I) {
1555 printInstruction(I,bbname);
1556 }
1557 }
1558
1559 // Loop over the ForwardRefs and resolve them now that all instructions
1560 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001561 if (!ForwardRefs.empty()) {
1562 nl(Out) << "// Resolve Forward References";
1563 nl(Out);
1564 }
1565
Reid Spencer12803f52006-05-31 17:31:38 +00001566 while (!ForwardRefs.empty()) {
1567 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001568 Out << I->second << "->replaceAllUsesWith("
1569 << getCppName(I->first) << "); delete " << I->second << ";";
1570 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001571 ForwardRefs.erase(I);
1572 }
1573}
1574
Reid Spencerf977e7b2006-06-01 23:43:47 +00001575void CppWriter::printInline(const std::string& fname, const std::string& func) {
Reid Spencer688b0492007-02-05 21:19:13 +00001576 const Function* F = TheModule->getFunction(func);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001577 if (!F) {
1578 error(std::string("Function '") + func + "' not found in input module");
1579 return;
1580 }
Reid Spencer5cbf9852007-01-30 20:08:39 +00001581 if (F->isDeclaration()) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001582 error(std::string("Function '") + func + "' is external!");
1583 return;
1584 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001585 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001586 << getCppName(F);
1587 unsigned arg_count = 1;
1588 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1589 AI != AE; ++AI) {
1590 Out << ", Value* arg_" << arg_count;
1591 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001592 Out << ") {";
1593 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001594 is_inline = true;
1595 printFunctionUses(F);
1596 printFunctionBody(F);
1597 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001598 Out << "return " << getCppName(F->begin()) << ";";
1599 nl(Out) << "}";
1600 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001601}
1602
Reid Spencer12803f52006-05-31 17:31:38 +00001603void CppWriter::printModuleBody() {
1604 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001605 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001606 printTypes(TheModule);
1607
1608 // Functions can call each other and global variables can reference them so
1609 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001610 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001611 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1612 I != E; ++I)
1613 printFunctionHead(I);
1614
1615 // Process the global variables declarations. We can't initialze them until
1616 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001617 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001618 for (Module::const_global_iterator I = TheModule->global_begin(),
1619 E = TheModule->global_end(); I != E; ++I) {
1620 printVariableHead(I);
1621 }
1622
1623 // Print out all the constants definitions. Constants don't recurse except
1624 // through GlobalValues. All GlobalValues have been declared at this point
1625 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001626 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001627 printConstants(TheModule);
1628
1629 // Process the global variables definitions now that all the constants have
1630 // been emitted. These definitions just couple the gvars with their constant
1631 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001632 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001633 for (Module::const_global_iterator I = TheModule->global_begin(),
1634 E = TheModule->global_end(); I != E; ++I) {
1635 printVariableBody(I);
1636 }
1637
1638 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001639 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001640 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1641 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001642 if (!I->isDeclaration()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001643 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1644 << ")";
1645 nl(Out) << "{";
1646 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001647 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001648 nl(Out,-1) << "}";
1649 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001650 }
1651 }
1652}
1653
1654void CppWriter::printProgram(
1655 const std::string& fname,
1656 const std::string& mName
1657) {
1658 Out << "#include <llvm/Module.h>\n";
1659 Out << "#include <llvm/DerivedTypes.h>\n";
1660 Out << "#include <llvm/Constants.h>\n";
1661 Out << "#include <llvm/GlobalVariable.h>\n";
1662 Out << "#include <llvm/Function.h>\n";
1663 Out << "#include <llvm/CallingConv.h>\n";
1664 Out << "#include <llvm/BasicBlock.h>\n";
1665 Out << "#include <llvm/Instructions.h>\n";
1666 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001667 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001668 Out << "#include <llvm/Pass.h>\n";
1669 Out << "#include <llvm/PassManager.h>\n";
1670 Out << "#include <llvm/Analysis/Verifier.h>\n";
1671 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1672 Out << "#include <algorithm>\n";
1673 Out << "#include <iostream>\n\n";
1674 Out << "using namespace llvm;\n\n";
1675 Out << "Module* " << fname << "();\n\n";
1676 Out << "int main(int argc, char**argv) {\n";
1677 Out << " Module* Mod = makeLLVMModule();\n";
1678 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1679 Out << " std::cerr.flush();\n";
1680 Out << " std::cout.flush();\n";
1681 Out << " PassManager PM;\n";
1682 Out << " PM.add(new PrintModulePass(&std::cout));\n";
1683 Out << " PM.run(*Mod);\n";
1684 Out << " return 0;\n";
1685 Out << "}\n\n";
1686 printModule(fname,mName);
1687}
1688
1689void CppWriter::printModule(
1690 const std::string& fname,
1691 const std::string& mName
1692) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001693 nl(Out) << "Module* " << fname << "() {";
1694 nl(Out,1) << "// Module Construction";
1695 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1696 nl(Out) << "mod->setEndianness(";
Reid Spencer12803f52006-05-31 17:31:38 +00001697 switch (TheModule->getEndianness()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001698 case Module::LittleEndian: Out << "Module::LittleEndian);"; break;
1699 case Module::BigEndian: Out << "Module::BigEndian);"; break;
1700 case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001701 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001702 nl(Out) << "mod->setPointerSize(";
Reid Spencer12803f52006-05-31 17:31:38 +00001703 switch (TheModule->getPointerSize()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001704 case Module::Pointer32: Out << "Module::Pointer32);"; break;
1705 case Module::Pointer64: Out << "Module::Pointer64);"; break;
1706 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001707 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001708 nl(Out);
1709 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer12803f52006-05-31 17:31:38 +00001710 Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001711 << "\");";
1712 nl(Out);
1713 }
Reid Spencer12803f52006-05-31 17:31:38 +00001714
1715 if (!TheModule->getModuleInlineAsm().empty()) {
1716 Out << "mod->setModuleInlineAsm(\"";
1717 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001718 Out << "\");";
1719 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001720 }
1721
1722 // Loop over the dependent libraries and emit them.
1723 Module::lib_iterator LI = TheModule->lib_begin();
1724 Module::lib_iterator LE = TheModule->lib_end();
1725 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001726 Out << "mod->addLibrary(\"" << *LI << "\");";
1727 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001728 ++LI;
1729 }
1730 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001731 nl(Out) << "return mod;";
1732 nl(Out,-1) << "}";
1733 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001734}
1735
1736void CppWriter::printContents(
1737 const std::string& fname, // Name of generated function
1738 const std::string& mName // Name of module generated module
1739) {
1740 Out << "\nModule* " << fname << "(Module *mod) {\n";
1741 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001742 printModuleBody();
1743 Out << "\nreturn mod;\n";
1744 Out << "\n}\n";
1745}
1746
1747void CppWriter::printFunction(
1748 const std::string& fname, // Name of generated function
1749 const std::string& funcName // Name of function to generate
1750) {
Reid Spencer688b0492007-02-05 21:19:13 +00001751 const Function* F = TheModule->getFunction(funcName);
Reid Spencer12803f52006-05-31 17:31:38 +00001752 if (!F) {
1753 error(std::string("Function '") + funcName + "' not found in input module");
1754 return;
1755 }
1756 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1757 printFunctionUses(F);
1758 printFunctionHead(F);
1759 printFunctionBody(F);
1760 Out << "return " << getCppName(F) << ";\n";
1761 Out << "}\n";
1762}
1763
1764void CppWriter::printVariable(
1765 const std::string& fname, /// Name of generated function
1766 const std::string& varName // Name of variable to generate
1767) {
1768 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1769
1770 if (!GV) {
1771 error(std::string("Variable '") + varName + "' not found in input module");
1772 return;
1773 }
1774 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1775 printVariableUses(GV);
1776 printVariableHead(GV);
1777 printVariableBody(GV);
1778 Out << "return " << getCppName(GV) << ";\n";
1779 Out << "}\n";
1780}
1781
1782void CppWriter::printType(
1783 const std::string& fname, /// Name of generated function
1784 const std::string& typeName // Name of type to generate
1785) {
1786 const Type* Ty = TheModule->getTypeByName(typeName);
1787 if (!Ty) {
1788 error(std::string("Type '") + typeName + "' not found in input module");
1789 return;
1790 }
1791 Out << "\nType* " << fname << "(Module *mod) {\n";
1792 printType(Ty);
1793 Out << "return " << getCppName(Ty) << ";\n";
1794 Out << "}\n";
1795}
1796
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001797} // end anonymous llvm
1798
1799namespace llvm {
1800
1801void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001802 // Initialize a CppWriter for us to use
1803 CppWriter W(o, mod);
1804
1805 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001806 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001807
1808 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001809 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001810
1811 // Get the name of the thing we are to generate
1812 std::string tgtname = NameToGenerate.getValue();
1813 if (GenerationType == GenModule ||
1814 GenerationType == GenContents ||
1815 GenerationType == GenProgram) {
1816 if (tgtname == "!bad!") {
1817 if (mod->getModuleIdentifier() == "-")
1818 tgtname = "<stdin>";
1819 else
1820 tgtname = mod->getModuleIdentifier();
1821 }
1822 } else if (tgtname == "!bad!") {
1823 W.error("You must use the -for option with -gen-{function,variable,type}");
1824 }
1825
1826 switch (WhatToGenerate(GenerationType)) {
1827 case GenProgram:
1828 if (fname.empty())
1829 fname = "makeLLVMModule";
1830 W.printProgram(fname,tgtname);
1831 break;
1832 case GenModule:
1833 if (fname.empty())
1834 fname = "makeLLVMModule";
1835 W.printModule(fname,tgtname);
1836 break;
1837 case GenContents:
1838 if (fname.empty())
1839 fname = "makeLLVMModuleContents";
1840 W.printContents(fname,tgtname);
1841 break;
1842 case GenFunction:
1843 if (fname.empty())
1844 fname = "makeLLVMFunction";
1845 W.printFunction(fname,tgtname);
1846 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001847 case GenInline:
1848 if (fname.empty())
1849 fname = "makeLLVMInline";
1850 W.printInline(fname,tgtname);
1851 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001852 case GenVariable:
1853 if (fname.empty())
1854 fname = "makeLLVMVariable";
1855 W.printVariable(fname,tgtname);
1856 break;
1857 case GenType:
1858 if (fname.empty())
1859 fname = "makeLLVMType";
1860 W.printType(fname,tgtname);
1861 break;
1862 default:
1863 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001864 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001865}
1866
1867}