blob: 58c67b716e3758dd9d120a015a604d54bd1ed28d [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 Spencer4f859aa2007-04-22 05:46:44 +0000464 if (PAL) {
465 Out << '{'; in(); nl(Out);
466 Out << "ParamAttrsVector Attrs;"; nl(Out);
467 Out << "ParamAttrsWithIndex PAWI;"; nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000468 for (unsigned i = 0; i < PAL->size(); ++i) {
469 uint16_t index = PAL->getParamIndex(i);
470 uint16_t attrs = PAL->getParamAttrs(index);
Reid Spencer4f859aa2007-04-22 05:46:44 +0000471 Out << "PAWI.index = " << index << "; PAWI.attrs = 0 ";
Reid Spencer6abd3da2007-04-11 09:54:08 +0000472 if (attrs & ParamAttr::SExt)
473 Out << " | ParamAttr::SExt";
474 if (attrs & ParamAttr::ZExt)
475 Out << " | ParamAttr::ZExt";
476 if (attrs & ParamAttr::StructRet)
477 Out << " | ParamAttr::StructRet";
478 if (attrs & ParamAttr::InReg)
479 Out << " | ParamAttr::InReg";
480 if (attrs & ParamAttr::NoReturn)
481 Out << " | ParamAttr::NoReturn";
482 if (attrs & ParamAttr::NoUnwind)
483 Out << " | ParamAttr::NoUnwind";
Reid Spencer4f859aa2007-04-22 05:46:44 +0000484 Out << ";";
485 nl(Out);
486 Out << "Attrs.push_back(PAWI);";
Reid Spencera9297b12007-04-11 10:01:32 +0000487 nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000488 }
Reid Spencer4f859aa2007-04-22 05:46:44 +0000489 Out << typeName << "_PAL = ParamAttrsList::get(Attrs);";
490 nl(Out);
491 out(); nl(Out);
492 Out << '}'; nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000493 }
Reid Spencer12803f52006-05-31 17:31:38 +0000494 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000495 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000496 Out << "FunctionType* " << typeName << " = FunctionType::get(";
497 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000498 if (isForward)
499 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000500 Out << ",";
501 nl(Out) << "/*Params=*/" << typeName << "_args,";
Reid Spencer07441662007-04-11 12:28:56 +0000502 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true," : "false,") ;
Reid Spencera9297b12007-04-11 10:01:32 +0000503 nl(Out) << "/*ParamAttrs=*/" << typeName << "_PAL" << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000504 out();
505 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000506 break;
507 }
508 case Type::StructTyID: {
509 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000510 Out << "std::vector<const Type*>" << typeName << "_fields;";
511 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000512 StructType::element_iterator EI = ST->element_begin();
513 StructType::element_iterator EE = ST->element_end();
514 for (; EI != EE; ++EI) {
515 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000516 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000517 std::string fieldName(getCppName(fieldTy));
518 Out << typeName << "_fields.push_back(" << fieldName;
519 if (isForward)
520 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000521 Out << ");";
522 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000523 }
524 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer46fea102007-04-11 12:41:49 +0000525 << typeName << "_fields, /*isPacked=*/"
526 << (ST->isPacked() ? "true" : "false") << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000527 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000528 break;
529 }
530 case Type::ArrayTyID: {
531 const ArrayType* AT = cast<ArrayType>(Ty);
532 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000533 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000534 std::string elemName(getCppName(ET));
535 Out << "ArrayType* " << typeName << " = ArrayType::get("
536 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000537 << ", " << utostr(AT->getNumElements()) << ");";
538 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000539 break;
540 }
541 case Type::PointerTyID: {
542 const PointerType* PT = cast<PointerType>(Ty);
543 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000544 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000545 std::string elemName(getCppName(ET));
546 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000547 << elemName << (isForward ? "_fwd" : "") << ");";
548 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000549 break;
550 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000551 case Type::VectorTyID: {
552 const VectorType* PT = cast<VectorType>(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000553 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000554 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000555 std::string elemName(getCppName(ET));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000556 Out << "VectorType* " << typeName << " = VectorType::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000557 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000558 << ", " << utostr(PT->getNumElements()) << ");";
559 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000560 break;
561 }
562 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000563 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
564 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000565 break;
566 }
567 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000568 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000569 }
570
Reid Spencer74e032a2006-05-29 02:58:15 +0000571 // If the type had a name, make sure we recreate it.
572 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000573 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer07441662007-04-11 12:28:56 +0000574 if (progTypeName) {
Reid Spencer74e032a2006-05-29 02:58:15 +0000575 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000576 << typeName << ");";
577 nl(Out);
Reid Spencer07441662007-04-11 12:28:56 +0000578 }
Reid Spencer74e032a2006-05-29 02:58:15 +0000579
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000580 // Pop us off the type stack
581 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000582
583 // Indicate that this type is now defined.
584 DefinedTypes.insert(Ty);
585
586 // Early resolve as many unresolved types as possible. Search the unresolved
587 // types map for the type we just printed. Now that its definition is complete
588 // we can resolve any previous references to it. This prevents a cascade of
589 // unresolved types.
590 TypeMap::iterator I = UnresolvedTypes.find(Ty);
591 if (I != UnresolvedTypes.end()) {
592 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000593 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
594 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000595 Out << I->second << " = cast<";
596 switch (Ty->getTypeID()) {
597 case Type::FunctionTyID: Out << "FunctionType"; break;
598 case Type::ArrayTyID: Out << "ArrayType"; break;
599 case Type::StructTyID: Out << "StructType"; break;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000600 case Type::VectorTyID: Out << "VectorType"; break;
Reid Spencer15f7e012006-05-30 21:18:23 +0000601 case Type::PointerTyID: Out << "PointerType"; break;
602 case Type::OpaqueTyID: Out << "OpaqueType"; break;
603 default: Out << "NoSuchDerivedType"; break;
604 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000605 Out << ">(" << I->second << "_fwd.get());";
606 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000607 UnresolvedTypes.erase(I);
608 }
609
610 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000611 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000612
613 // We weren't a recursive type
614 return false;
615}
616
Reid Spencer12803f52006-05-31 17:31:38 +0000617// Prints a type definition. Returns true if it could not resolve all the types
618// in the definition but had to use a forward reference.
619void
620CppWriter::printType(const Type* Ty) {
621 assert(TypeStack.empty());
622 TypeStack.clear();
623 printTypeInternal(Ty);
624 assert(TypeStack.empty());
625}
626
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000627void
628CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000629
630 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000631 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
632 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
633 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000634
635 // For primitive types and types already defined, just add a name
636 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Chris Lattner42a75512007-01-15 02:27:26 +0000637 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000638 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000639 Out << "mod->addTypeName(\"";
640 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000641 Out << "\", " << getCppName(TI->second) << ");";
642 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000643 // For everything else, define the type
644 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000645 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000646 }
647 }
648
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000649 // Add all of the global variables to the value table...
650 for (Module::const_global_iterator I = TheModule->global_begin(),
651 E = TheModule->global_end(); I != E; ++I) {
652 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000653 printType(I->getInitializer()->getType());
654 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000655 }
656
657 // Add all the functions to the table
658 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
659 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000660 printType(FI->getReturnType());
661 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000662 // Add all the function arguments
663 for(Function::const_arg_iterator AI = FI->arg_begin(),
664 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000665 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000666 }
667
668 // Add all of the basic blocks and instructions
669 for (Function::const_iterator BB = FI->begin(),
670 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000671 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000672 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
673 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000674 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000675 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000676 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000677 }
678 }
679 }
680}
681
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000682
Reid Spencere0d133f2006-05-29 18:08:06 +0000683// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000684void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000685 // First, if the constant is actually a GlobalValue (variable or function) or
686 // its already in the constant list then we've printed it already and we can
687 // just return.
688 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000689 return;
690
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000691 std::string constName(getCppName(CV));
692 std::string typeName(getCppName(CV->getType()));
693 if (CV->isNullValue()) {
694 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000695 << typeName << ");";
696 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000697 return;
698 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000699 if (isa<GlobalValue>(CV)) {
700 // Skip variables and functions, we emit them elsewhere
701 return;
702 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000703 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4d26a062007-04-11 13:02:56 +0000704 Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
705 << cast<IntegerType>(CI->getType())->getBitWidth() << ", "
Reid Spencer70297252007-03-01 20:55:43 +0000706 << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000707 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000708 Out << "ConstantAggregateZero* " << constName
709 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000710 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000711 Out << "ConstantPointerNull* " << constName
712 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000713 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000714 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000715 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000716 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000717 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000718 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000719 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000720 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000721 // Determine if we want null termination or not.
722 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000723 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000724 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000725 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000726 Out << ");";
727 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000728 Out << "std::vector<Constant*> " << constName << "_elems;";
729 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000730 unsigned N = CA->getNumOperands();
731 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000732 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000733 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000734 << getCppName(CA->getOperand(i)) << ");";
735 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000736 }
737 Out << "Constant* " << constName << " = ConstantArray::get("
738 << typeName << ", " << constName << "_elems);";
739 }
740 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000741 Out << "std::vector<Constant*> " << constName << "_fields;";
742 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000743 unsigned N = CS->getNumOperands();
744 for (unsigned i = 0; i < N; i++) {
745 printConstant(CS->getOperand(i));
746 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000747 << getCppName(CS->getOperand(i)) << ");";
748 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000749 }
750 Out << "Constant* " << constName << " = ConstantStruct::get("
751 << typeName << ", " << constName << "_fields);";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000752 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000753 Out << "std::vector<Constant*> " << constName << "_elems;";
754 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000755 unsigned N = CP->getNumOperands();
756 for (unsigned i = 0; i < N; ++i) {
757 printConstant(CP->getOperand(i));
758 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000759 << getCppName(CP->getOperand(i)) << ");";
760 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000761 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000762 Out << "Constant* " << constName << " = ConstantVector::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000763 << typeName << ", " << constName << "_elems);";
764 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000765 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000766 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000767 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000768 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000769 Out << "std::vector<Constant*> " << constName << "_indices;";
770 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000771 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000772 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000773 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000774 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000775 << getCppName(CE->getOperand(i)) << ");";
776 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000777 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000778 Out << "Constant* " << constName
779 << " = ConstantExpr::getGetElementPtr("
780 << getCppName(CE->getOperand(0)) << ", "
Reid Spencer07441662007-04-11 12:28:56 +0000781 << "&" << constName << "_indices[0], " << CE->getNumOperands() - 1
782 << " );";
Reid Spencer3da59db2006-11-27 01:05:10 +0000783 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000784 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000785 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000786 switch (CE->getOpcode()) {
787 default: assert(0 && "Invalid cast opcode");
788 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
789 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
790 case Instruction::SExt: Out << "Instruction::SExt"; break;
791 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
792 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
793 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
794 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
795 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
796 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
797 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
798 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
799 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
800 }
801 Out << ", " << getCppName(CE->getOperand(0)) << ", "
802 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000803 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000804 unsigned N = CE->getNumOperands();
805 for (unsigned i = 0; i < N; ++i ) {
806 printConstant(CE->getOperand(i));
807 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000808 Out << "Constant* " << constName << " = ConstantExpr::";
809 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000810 case Instruction::Add: Out << "getAdd("; break;
811 case Instruction::Sub: Out << "getSub("; break;
812 case Instruction::Mul: Out << "getMul("; break;
813 case Instruction::UDiv: Out << "getUDiv("; break;
814 case Instruction::SDiv: Out << "getSDiv("; break;
815 case Instruction::FDiv: Out << "getFDiv("; break;
816 case Instruction::URem: Out << "getURem("; break;
817 case Instruction::SRem: Out << "getSRem("; break;
818 case Instruction::FRem: Out << "getFRem("; break;
819 case Instruction::And: Out << "getAnd("; break;
820 case Instruction::Or: Out << "getOr("; break;
821 case Instruction::Xor: Out << "getXor("; break;
822 case Instruction::ICmp:
823 Out << "getICmp(ICmpInst::ICMP_";
824 switch (CE->getPredicate()) {
825 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
826 case ICmpInst::ICMP_NE: Out << "NE"; break;
827 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
828 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
829 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
830 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
831 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
832 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
833 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
834 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
835 default: error("Invalid ICmp Predicate");
836 }
837 break;
838 case Instruction::FCmp:
839 Out << "getFCmp(FCmpInst::FCMP_";
840 switch (CE->getPredicate()) {
841 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
842 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
843 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
844 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
845 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
846 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
847 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
848 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
849 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
850 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
851 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
852 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
853 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
854 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
855 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
856 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
857 default: error("Invalid FCmp Predicate");
858 }
859 break;
860 case Instruction::Shl: Out << "getShl("; break;
861 case Instruction::LShr: Out << "getLShr("; break;
862 case Instruction::AShr: Out << "getAShr("; break;
863 case Instruction::Select: Out << "getSelect("; break;
864 case Instruction::ExtractElement: Out << "getExtractElement("; break;
865 case Instruction::InsertElement: Out << "getInsertElement("; break;
866 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000867 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000868 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000869 break;
870 }
871 Out << getCppName(CE->getOperand(0));
872 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
873 Out << ", " << getCppName(CE->getOperand(i));
874 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000875 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000876 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000877 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000878 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000879 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000880 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000881}
882
Reid Spencer12803f52006-05-31 17:31:38 +0000883void
884CppWriter::printConstants(const Module* M) {
885 // Traverse all the global variables looking for constant initializers
886 for (Module::const_global_iterator I = TheModule->global_begin(),
887 E = TheModule->global_end(); I != E; ++I)
888 if (I->hasInitializer())
889 printConstant(I->getInitializer());
890
891 // Traverse the LLVM functions looking for constants
892 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
893 FI != FE; ++FI) {
894 // Add all of the basic blocks and instructions
895 for (Function::const_iterator BB = FI->begin(),
896 E = FI->end(); BB != E; ++BB) {
897 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
898 ++I) {
899 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
900 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
901 printConstant(C);
902 }
903 }
904 }
905 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000906 }
Reid Spencer66c87342006-05-30 03:43:49 +0000907}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000908
Reid Spencer12803f52006-05-31 17:31:38 +0000909void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000910 nl(Out) << "// Type Definitions";
911 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000912 printType(GV->getType());
913 if (GV->hasInitializer()) {
914 Constant* Init = GV->getInitializer();
915 printType(Init->getType());
916 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000917 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000918 printFunctionHead(F);
919 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000920 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000921 printVariableHead(gv);
922 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000923 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000924 printConstant(gv);
925 }
926 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000927 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000928 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000929 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000930 }
Reid Spencer12803f52006-05-31 17:31:38 +0000931}
Reid Spencer15f7e012006-05-30 21:18:23 +0000932
Reid Spencer12803f52006-05-31 17:31:38 +0000933void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000934 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000935 if (is_inline) {
936 Out << " = mod->getGlobalVariable(";
937 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000938 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
939 nl(Out) << "if (!" << getCppName(GV) << ") {";
940 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000941 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000942 Out << " = new GlobalVariable(";
943 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000944 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000945 Out << ",";
946 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
947 Out << ",";
948 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000949 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000950 Out << ",";
951 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000952 if (GV->hasInitializer()) {
953 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000954 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000955 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000956 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000957 Out << "\",";
958 nl(Out) << "mod);";
959 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000960
961 if (GV->hasSection()) {
962 printCppName(GV);
963 Out << "->setSection(\"";
964 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000965 Out << "\");";
966 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000967 }
968 if (GV->getAlignment()) {
969 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000970 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
971 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000972 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000973 if (is_inline) {
974 out(); Out << "}"; nl(Out);
975 }
Reid Spencer12803f52006-05-31 17:31:38 +0000976}
977
978void
979CppWriter::printVariableBody(const GlobalVariable *GV) {
980 if (GV->hasInitializer()) {
981 printCppName(GV);
982 Out << "->setInitializer(";
983 //if (!isa<GlobalValue(GV->getInitializer()))
984 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000985 Out << getCppName(GV->getInitializer()) << ");";
986 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000987 }
988}
989
990std::string
991CppWriter::getOpName(Value* V) {
992 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
993 return getCppName(V);
994
995 // See if its alread in the map of forward references, if so just return the
996 // name we already set up for it
997 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
998 if (I != ForwardRefs.end())
999 return I->second;
1000
1001 // This is a new forward reference. Generate a unique name for it
1002 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
1003
1004 // Yes, this is a hack. An Argument is the smallest instantiable value that
1005 // we can make as a placeholder for the real value. We'll replace these
1006 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001007 Out << "Argument* " << result << " = new Argument("
1008 << getCppName(V->getType()) << ");";
1009 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001010 ForwardRefs[V] = result;
1011 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001012}
1013
Reid Spencere0d133f2006-05-29 18:08:06 +00001014// printInstruction - This member is called for each Instruction in a function.
1015void
Reid Spencer12803f52006-05-31 17:31:38 +00001016CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +00001017 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001018
Reid Spencer15f7e012006-05-30 21:18:23 +00001019 // Before we emit this instruction, we need to take care of generating any
1020 // forward references. So, we get the names of all the operands in advance
1021 std::string* opNames = new std::string[I->getNumOperands()];
1022 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1023 opNames[i] = getOpName(I->getOperand(i));
1024 }
1025
Reid Spencere0d133f2006-05-29 18:08:06 +00001026 switch (I->getOpcode()) {
1027 case Instruction::Ret: {
1028 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001029 Out << "new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001030 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001031 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001032 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001033 case Instruction::Br: {
1034 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001035 Out << "new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001036 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001037 Out << opNames[0] << ", "
1038 << opNames[1] << ", "
1039 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001040
Reid Spencere0d133f2006-05-29 18:08:06 +00001041 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001042 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001043 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001044 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001045 }
1046 Out << bbname << ");";
1047 break;
1048 }
Reid Spencer66c87342006-05-30 03:43:49 +00001049 case Instruction::Switch: {
1050 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001051 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001052 << opNames[0] << ", "
1053 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001054 << sw->getNumCases() << ", " << bbname << ");";
1055 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001056 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001057 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001058 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001059 << opNames[i+1] << ");";
1060 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001061 }
1062 break;
1063 }
1064 case Instruction::Invoke: {
1065 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001066 Out << "std::vector<Value*> " << iName << "_params;";
1067 nl(Out);
1068 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1069 Out << iName << "_params.push_back("
1070 << opNames[i] << ");";
1071 nl(Out);
1072 }
Reid Spencer07441662007-04-11 12:28:56 +00001073 Out << "InvokeInst *" << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001074 << opNames[0] << ", "
1075 << opNames[1] << ", "
1076 << opNames[2] << ", "
Reid Spencer07441662007-04-11 12:28:56 +00001077 << "&" << iName << "_params[0], " << inv->getNumOperands() - 3
1078 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001079 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001080 Out << "\", " << bbname << ");";
1081 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001082 printCallingConv(inv->getCallingConv());
1083 Out << ");";
1084 break;
1085 }
1086 case Instruction::Unwind: {
Reid Spencer07441662007-04-11 12:28:56 +00001087 Out << "new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001088 << bbname << ");";
1089 break;
1090 }
1091 case Instruction::Unreachable:{
Reid Spencer07441662007-04-11 12:28:56 +00001092 Out << "new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001093 << bbname << ");";
1094 break;
1095 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001096 case Instruction::Add:
1097 case Instruction::Sub:
1098 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001099 case Instruction::UDiv:
1100 case Instruction::SDiv:
1101 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001102 case Instruction::URem:
1103 case Instruction::SRem:
1104 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001105 case Instruction::And:
1106 case Instruction::Or:
1107 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001108 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001109 case Instruction::LShr:
1110 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001111 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001112 switch (I->getOpcode()) {
1113 case Instruction::Add: Out << "Instruction::Add"; break;
1114 case Instruction::Sub: Out << "Instruction::Sub"; break;
1115 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001116 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1117 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1118 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001119 case Instruction::URem:Out << "Instruction::URem"; break;
1120 case Instruction::SRem:Out << "Instruction::SRem"; break;
1121 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001122 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001123 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001124 case Instruction::Xor: Out << "Instruction::Xor"; break;
1125 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001126 case Instruction::LShr:Out << "Instruction::LShr"; break;
1127 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001128 default: Out << "Instruction::BadOpCode"; break;
1129 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001130 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001131 printEscapedString(I->getName());
1132 Out << "\", " << bbname << ");";
1133 break;
1134 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001135 case Instruction::FCmp: {
1136 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1137 switch (cast<FCmpInst>(I)->getPredicate()) {
1138 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1139 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1140 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1141 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1142 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1143 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1144 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1145 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1146 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1147 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1148 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1149 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1150 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1151 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1152 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1153 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1154 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1155 }
1156 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1157 printEscapedString(I->getName());
1158 Out << "\", " << bbname << ");";
1159 break;
1160 }
1161 case Instruction::ICmp: {
1162 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1163 switch (cast<ICmpInst>(I)->getPredicate()) {
1164 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1165 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1166 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1167 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1168 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1169 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1170 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1171 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1172 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1173 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1174 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001175 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001176 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001177 printEscapedString(I->getName());
1178 Out << "\", " << bbname << ");";
1179 break;
1180 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001181 case Instruction::Malloc: {
1182 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001183 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001184 << getCppName(mallocI->getAllocatedType()) << ", ";
1185 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001186 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001187 Out << "\"";
1188 printEscapedString(mallocI->getName());
1189 Out << "\", " << bbname << ");";
1190 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001191 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001192 << mallocI->getAlignment() << ");";
1193 break;
1194 }
Reid Spencer66c87342006-05-30 03:43:49 +00001195 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001196 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001197 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1198 break;
1199 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001200 case Instruction::Alloca: {
1201 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001202 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001203 << getCppName(allocaI->getAllocatedType()) << ", ";
1204 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001205 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001206 Out << "\"";
1207 printEscapedString(allocaI->getName());
1208 Out << "\", " << bbname << ");";
1209 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001210 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001211 << allocaI->getAlignment() << ");";
1212 break;
1213 }
Reid Spencer66c87342006-05-30 03:43:49 +00001214 case Instruction::Load:{
1215 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001216 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001217 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001218 printEscapedString(load->getName());
1219 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001220 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001221 break;
1222 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001223 case Instruction::Store: {
1224 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001225 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001226 << opNames[0] << ", "
1227 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001228 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001229 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001230 break;
1231 }
1232 case Instruction::GetElementPtr: {
1233 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1234 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001235 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001236 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001237 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001238 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001239 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001240 Out << "std::vector<Value*> " << iName << "_indices;";
1241 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001242 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001243 Out << iName << "_indices.push_back("
1244 << opNames[i] << ");";
1245 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001246 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001247 Out << "Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer07441662007-04-11 12:28:56 +00001248 << opNames[0] << ", &" << iName << "_indices[0], "
1249 << gep->getNumOperands() - 1;
Reid Spencere0d133f2006-05-29 18:08:06 +00001250 }
1251 Out << ", \"";
1252 printEscapedString(gep->getName());
1253 Out << "\", " << bbname << ");";
1254 break;
1255 }
Reid Spencer66c87342006-05-30 03:43:49 +00001256 case Instruction::PHI: {
1257 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001258
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001259 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001260 << getCppName(phi->getType()) << ", \"";
1261 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001262 Out << "\", " << bbname << ");";
1263 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001264 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001265 << ");";
1266 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001267 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001268 Out << iName << "->addIncoming("
1269 << opNames[i] << ", " << opNames[i+1] << ");";
1270 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001271 }
Reid Spencer66c87342006-05-30 03:43:49 +00001272 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001273 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001274 case Instruction::Trunc:
1275 case Instruction::ZExt:
1276 case Instruction::SExt:
1277 case Instruction::FPTrunc:
1278 case Instruction::FPExt:
1279 case Instruction::FPToUI:
1280 case Instruction::FPToSI:
1281 case Instruction::UIToFP:
1282 case Instruction::SIToFP:
1283 case Instruction::PtrToInt:
1284 case Instruction::IntToPtr:
1285 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001286 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001287 Out << "CastInst* " << iName << " = new ";
1288 switch (I->getOpcode()) {
Reid Spencer34816572007-02-16 06:34:39 +00001289 case Instruction::Trunc: Out << "TruncInst"; break;
1290 case Instruction::ZExt: Out << "ZExtInst"; break;
1291 case Instruction::SExt: Out << "SExtInst"; break;
1292 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1293 case Instruction::FPExt: Out << "FPExtInst"; break;
1294 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1295 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1296 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1297 case Instruction::SIToFP: Out << "SIToFPInst"; break;
Reid Spencer07441662007-04-11 12:28:56 +00001298 case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
Reid Spencer34816572007-02-16 06:34:39 +00001299 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1300 case Instruction::BitCast: Out << "BitCastInst"; break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001301 default: assert(!"Unreachable"); break;
1302 }
1303 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001304 << getCppName(cst->getType()) << ", \"";
1305 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001306 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001307 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001308 }
Reid Spencer66c87342006-05-30 03:43:49 +00001309 case Instruction::Call:{
1310 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001311 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001312 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001313 << getCppName(ila->getFunctionType()) << ", \""
1314 << ila->getAsmString() << "\", \""
1315 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001316 << (ila->hasSideEffects() ? "true" : "false") << ");";
1317 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001318 }
Reid Spencer66c87342006-05-30 03:43:49 +00001319 if (call->getNumOperands() > 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001320 Out << "std::vector<Value*> " << iName << "_params;";
1321 nl(Out);
1322 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1323 Out << iName << "_params.push_back(" << opNames[i] << ");";
1324 nl(Out);
1325 }
1326 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer07441662007-04-11 12:28:56 +00001327 << opNames[0] << ", &" << iName << "_params[0], "
1328 << call->getNumOperands() - 1 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001329 } else if (call->getNumOperands() == 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001330 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001331 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001332 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001333 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001334 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001335 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001336 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001337 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001338 }
1339 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001340 Out << "\", " << bbname << ");";
1341 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001342 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001343 Out << ");";
1344 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001345 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001346 Out << ");";
1347 break;
1348 }
1349 case Instruction::Select: {
1350 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001351 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001352 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001353 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001354 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001355 break;
1356 }
1357 case Instruction::UserOp1:
1358 /// FALL THROUGH
1359 case Instruction::UserOp2: {
1360 /// FIXME: What should be done here?
1361 break;
1362 }
1363 case Instruction::VAArg: {
1364 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001365 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001366 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001367 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001368 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001369 break;
1370 }
1371 case Instruction::ExtractElement: {
1372 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001373 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001374 << " = new ExtractElementInst(" << opNames[0]
1375 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001376 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001377 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001378 break;
1379 }
1380 case Instruction::InsertElement: {
1381 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001382 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001383 << " = new InsertElementInst(" << opNames[0]
1384 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001385 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001386 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001387 break;
1388 }
1389 case Instruction::ShuffleVector: {
1390 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001391 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001392 << " = new ShuffleVectorInst(" << opNames[0]
1393 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001394 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001395 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001396 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001397 }
1398 }
Reid Spencer68464b72006-06-15 16:09:59 +00001399 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001400 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001401 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001402}
1403
Reid Spencer12803f52006-05-31 17:31:38 +00001404// Print out the types, constants and declarations needed by one function
1405void CppWriter::printFunctionUses(const Function* F) {
1406
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001407 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001408 if (!is_inline) {
1409 // Print the function's return type
1410 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001411
Reid Spencerf977e7b2006-06-01 23:43:47 +00001412 // Print the function's function type
1413 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001414
Reid Spencerf977e7b2006-06-01 23:43:47 +00001415 // Print the types of each of the function's arguments
1416 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1417 AI != AE; ++AI) {
1418 printType(AI->getType());
1419 }
Reid Spencer12803f52006-05-31 17:31:38 +00001420 }
1421
1422 // Print type definitions for every type referenced by an instruction and
1423 // make a note of any global values or constants that are referenced
1424 std::vector<GlobalValue*> gvs;
1425 std::vector<Constant*> consts;
1426 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1427 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1428 I != E; ++I) {
1429 // Print the type of the instruction itself
1430 printType(I->getType());
1431
1432 // Print the type of each of the instruction's operands
1433 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1434 Value* operand = I->getOperand(i);
1435 printType(operand->getType());
1436 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1437 gvs.push_back(GV);
1438 else if (Constant* C = dyn_cast<Constant>(operand))
1439 consts.push_back(C);
1440 }
1441 }
1442 }
1443
1444 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001445 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001446 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1447 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001448 if (Function* Fun = dyn_cast<Function>(*I)) {
1449 if (!is_inline || Fun != F)
1450 printFunctionHead(Fun);
1451 }
Reid Spencer12803f52006-05-31 17:31:38 +00001452 }
1453
1454 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001455 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001456 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1457 I != E; ++I) {
1458 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1459 printVariableHead(F);
1460 }
1461
1462 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001463 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001464 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1465 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001466 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001467 }
1468
1469 // Process the global variables definitions now that all the constants have
1470 // been emitted. These definitions just couple the gvars with their constant
1471 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001472 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001473 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1474 I != E; ++I) {
1475 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1476 printVariableBody(GV);
1477 }
1478}
1479
1480void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001481 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001482 if (is_inline) {
1483 Out << " = mod->getFunction(\"";
1484 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001485 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1486 nl(Out) << "if (!" << getCppName(F) << ") {";
1487 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001488 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001489 Out<< " = new Function(";
1490 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1491 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001492 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001493 Out << ",";
1494 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001495 printEscapedString(F->getName());
Reid Spencer5cbf9852007-01-30 20:08:39 +00001496 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001497 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001498 printCppName(F);
1499 Out << "->setCallingConv(";
1500 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001501 Out << ");";
1502 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001503 if (F->hasSection()) {
1504 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001505 Out << "->setSection(\"" << F->getSection() << "\");";
1506 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001507 }
1508 if (F->getAlignment()) {
1509 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001510 Out << "->setAlignment(" << F->getAlignment() << ");";
1511 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001512 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001513 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001514 Out << "}";
1515 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001516 }
Reid Spencer12803f52006-05-31 17:31:38 +00001517}
1518
1519void CppWriter::printFunctionBody(const Function *F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001520 if (F->isDeclaration())
Reid Spencer12803f52006-05-31 17:31:38 +00001521 return; // external functions have no bodies.
1522
1523 // Clear the DefinedValues and ForwardRefs maps because we can't have
1524 // cross-function forward refs
1525 ForwardRefs.clear();
1526 DefinedValues.clear();
1527
1528 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001529 if (!is_inline) {
1530 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001531 Out << "Function::arg_iterator args = " << getCppName(F)
1532 << "->arg_begin();";
1533 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001534 }
1535 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1536 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001537 Out << "Value* " << getCppName(AI) << " = args++;";
1538 nl(Out);
1539 if (AI->hasName()) {
1540 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1541 nl(Out);
1542 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001543 }
Reid Spencer12803f52006-05-31 17:31:38 +00001544 }
1545
1546 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001547 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001548 for (Function::const_iterator BI = F->begin(), BE = F->end();
1549 BI != BE; ++BI) {
1550 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001551 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001552 if (BI->hasName())
1553 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001554 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1555 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001556 }
1557
1558 // Output all of its basic blocks... for the function
1559 for (Function::const_iterator BI = F->begin(), BE = F->end();
1560 BI != BE; ++BI) {
1561 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001562 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1563 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001564
1565 // Output all of the instructions in the basic block...
1566 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1567 I != E; ++I) {
1568 printInstruction(I,bbname);
1569 }
1570 }
1571
1572 // Loop over the ForwardRefs and resolve them now that all instructions
1573 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001574 if (!ForwardRefs.empty()) {
1575 nl(Out) << "// Resolve Forward References";
1576 nl(Out);
1577 }
1578
Reid Spencer12803f52006-05-31 17:31:38 +00001579 while (!ForwardRefs.empty()) {
1580 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001581 Out << I->second << "->replaceAllUsesWith("
1582 << getCppName(I->first) << "); delete " << I->second << ";";
1583 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001584 ForwardRefs.erase(I);
1585 }
1586}
1587
Reid Spencerf977e7b2006-06-01 23:43:47 +00001588void CppWriter::printInline(const std::string& fname, const std::string& func) {
Reid Spencer688b0492007-02-05 21:19:13 +00001589 const Function* F = TheModule->getFunction(func);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001590 if (!F) {
1591 error(std::string("Function '") + func + "' not found in input module");
1592 return;
1593 }
Reid Spencer5cbf9852007-01-30 20:08:39 +00001594 if (F->isDeclaration()) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001595 error(std::string("Function '") + func + "' is external!");
1596 return;
1597 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001598 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001599 << getCppName(F);
1600 unsigned arg_count = 1;
1601 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1602 AI != AE; ++AI) {
1603 Out << ", Value* arg_" << arg_count;
1604 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001605 Out << ") {";
1606 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001607 is_inline = true;
1608 printFunctionUses(F);
1609 printFunctionBody(F);
1610 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001611 Out << "return " << getCppName(F->begin()) << ";";
1612 nl(Out) << "}";
1613 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001614}
1615
Reid Spencer12803f52006-05-31 17:31:38 +00001616void CppWriter::printModuleBody() {
1617 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001618 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001619 printTypes(TheModule);
1620
1621 // Functions can call each other and global variables can reference them so
1622 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001623 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001624 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1625 I != E; ++I)
1626 printFunctionHead(I);
1627
1628 // Process the global variables declarations. We can't initialze them until
1629 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001630 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001631 for (Module::const_global_iterator I = TheModule->global_begin(),
1632 E = TheModule->global_end(); I != E; ++I) {
1633 printVariableHead(I);
1634 }
1635
1636 // Print out all the constants definitions. Constants don't recurse except
1637 // through GlobalValues. All GlobalValues have been declared at this point
1638 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001639 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001640 printConstants(TheModule);
1641
1642 // Process the global variables definitions now that all the constants have
1643 // been emitted. These definitions just couple the gvars with their constant
1644 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001645 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001646 for (Module::const_global_iterator I = TheModule->global_begin(),
1647 E = TheModule->global_end(); I != E; ++I) {
1648 printVariableBody(I);
1649 }
1650
1651 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001652 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001653 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1654 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001655 if (!I->isDeclaration()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001656 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1657 << ")";
1658 nl(Out) << "{";
1659 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001660 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001661 nl(Out,-1) << "}";
1662 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001663 }
1664 }
1665}
1666
1667void CppWriter::printProgram(
1668 const std::string& fname,
1669 const std::string& mName
1670) {
1671 Out << "#include <llvm/Module.h>\n";
1672 Out << "#include <llvm/DerivedTypes.h>\n";
1673 Out << "#include <llvm/Constants.h>\n";
1674 Out << "#include <llvm/GlobalVariable.h>\n";
1675 Out << "#include <llvm/Function.h>\n";
1676 Out << "#include <llvm/CallingConv.h>\n";
1677 Out << "#include <llvm/BasicBlock.h>\n";
1678 Out << "#include <llvm/Instructions.h>\n";
1679 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencer07441662007-04-11 12:28:56 +00001680 Out << "#include <llvm/ParameterAttributes.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001681 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001682 Out << "#include <llvm/Pass.h>\n";
1683 Out << "#include <llvm/PassManager.h>\n";
1684 Out << "#include <llvm/Analysis/Verifier.h>\n";
1685 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1686 Out << "#include <algorithm>\n";
1687 Out << "#include <iostream>\n\n";
1688 Out << "using namespace llvm;\n\n";
1689 Out << "Module* " << fname << "();\n\n";
1690 Out << "int main(int argc, char**argv) {\n";
1691 Out << " Module* Mod = makeLLVMModule();\n";
1692 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1693 Out << " std::cerr.flush();\n";
1694 Out << " std::cout.flush();\n";
1695 Out << " PassManager PM;\n";
Reid Spencer07441662007-04-11 12:28:56 +00001696 Out << " PM.add(new PrintModulePass(&llvm::cout));\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001697 Out << " PM.run(*Mod);\n";
1698 Out << " return 0;\n";
1699 Out << "}\n\n";
1700 printModule(fname,mName);
1701}
1702
1703void CppWriter::printModule(
1704 const std::string& fname,
1705 const std::string& mName
1706) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001707 nl(Out) << "Module* " << fname << "() {";
1708 nl(Out,1) << "// Module Construction";
1709 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001710 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001711 nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
1712 }
1713 if (!TheModule->getTargetTriple().empty()) {
1714 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1715 << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001716 }
Reid Spencer12803f52006-05-31 17:31:38 +00001717
1718 if (!TheModule->getModuleInlineAsm().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001719 nl(Out) << "mod->setModuleInlineAsm(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001720 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001721 Out << "\");";
Reid Spencer12803f52006-05-31 17:31:38 +00001722 }
Reid Spencer07441662007-04-11 12:28:56 +00001723 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001724
1725 // Loop over the dependent libraries and emit them.
1726 Module::lib_iterator LI = TheModule->lib_begin();
1727 Module::lib_iterator LE = TheModule->lib_end();
1728 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001729 Out << "mod->addLibrary(\"" << *LI << "\");";
1730 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001731 ++LI;
1732 }
1733 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001734 nl(Out) << "return mod;";
1735 nl(Out,-1) << "}";
1736 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001737}
1738
1739void CppWriter::printContents(
1740 const std::string& fname, // Name of generated function
1741 const std::string& mName // Name of module generated module
1742) {
1743 Out << "\nModule* " << fname << "(Module *mod) {\n";
1744 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001745 printModuleBody();
1746 Out << "\nreturn mod;\n";
1747 Out << "\n}\n";
1748}
1749
1750void CppWriter::printFunction(
1751 const std::string& fname, // Name of generated function
1752 const std::string& funcName // Name of function to generate
1753) {
Reid Spencer688b0492007-02-05 21:19:13 +00001754 const Function* F = TheModule->getFunction(funcName);
Reid Spencer12803f52006-05-31 17:31:38 +00001755 if (!F) {
1756 error(std::string("Function '") + funcName + "' not found in input module");
1757 return;
1758 }
1759 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1760 printFunctionUses(F);
1761 printFunctionHead(F);
1762 printFunctionBody(F);
1763 Out << "return " << getCppName(F) << ";\n";
1764 Out << "}\n";
1765}
1766
1767void CppWriter::printVariable(
1768 const std::string& fname, /// Name of generated function
1769 const std::string& varName // Name of variable to generate
1770) {
1771 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1772
1773 if (!GV) {
1774 error(std::string("Variable '") + varName + "' not found in input module");
1775 return;
1776 }
1777 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1778 printVariableUses(GV);
1779 printVariableHead(GV);
1780 printVariableBody(GV);
1781 Out << "return " << getCppName(GV) << ";\n";
1782 Out << "}\n";
1783}
1784
1785void CppWriter::printType(
1786 const std::string& fname, /// Name of generated function
1787 const std::string& typeName // Name of type to generate
1788) {
1789 const Type* Ty = TheModule->getTypeByName(typeName);
1790 if (!Ty) {
1791 error(std::string("Type '") + typeName + "' not found in input module");
1792 return;
1793 }
1794 Out << "\nType* " << fname << "(Module *mod) {\n";
1795 printType(Ty);
1796 Out << "return " << getCppName(Ty) << ";\n";
1797 Out << "}\n";
1798}
1799
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001800} // end anonymous llvm
1801
1802namespace llvm {
1803
1804void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001805 // Initialize a CppWriter for us to use
1806 CppWriter W(o, mod);
1807
1808 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001809 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001810
1811 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001812 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001813
1814 // Get the name of the thing we are to generate
1815 std::string tgtname = NameToGenerate.getValue();
1816 if (GenerationType == GenModule ||
1817 GenerationType == GenContents ||
1818 GenerationType == GenProgram) {
1819 if (tgtname == "!bad!") {
1820 if (mod->getModuleIdentifier() == "-")
1821 tgtname = "<stdin>";
1822 else
1823 tgtname = mod->getModuleIdentifier();
1824 }
1825 } else if (tgtname == "!bad!") {
1826 W.error("You must use the -for option with -gen-{function,variable,type}");
1827 }
1828
1829 switch (WhatToGenerate(GenerationType)) {
1830 case GenProgram:
1831 if (fname.empty())
1832 fname = "makeLLVMModule";
1833 W.printProgram(fname,tgtname);
1834 break;
1835 case GenModule:
1836 if (fname.empty())
1837 fname = "makeLLVMModule";
1838 W.printModule(fname,tgtname);
1839 break;
1840 case GenContents:
1841 if (fname.empty())
1842 fname = "makeLLVMModuleContents";
1843 W.printContents(fname,tgtname);
1844 break;
1845 case GenFunction:
1846 if (fname.empty())
1847 fname = "makeLLVMFunction";
1848 W.printFunction(fname,tgtname);
1849 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001850 case GenInline:
1851 if (fname.empty())
1852 fname = "makeLLVMInline";
1853 W.printInline(fname,tgtname);
1854 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001855 case GenVariable:
1856 if (fname.empty())
1857 fname = "makeLLVMVariable";
1858 W.printVariable(fname,tgtname);
1859 break;
1860 case GenType:
1861 if (fname.empty())
1862 fname = "makeLLVMType";
1863 W.printType(fname,tgtname);
1864 break;
1865 default:
1866 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001867 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001868}
1869
1870}