blob: 1076b59c02112375b209202b47ebc9fedec1f94f [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";
Zhou Shengfebca342007-06-05 05:28:26 +0000476 if (attrs & ParamAttr::NoAlias)
477 Out << " | ParamAttr::NoAlias";
Reid Spencer6abd3da2007-04-11 09:54:08 +0000478 if (attrs & ParamAttr::StructRet)
479 Out << " | ParamAttr::StructRet";
480 if (attrs & ParamAttr::InReg)
481 Out << " | ParamAttr::InReg";
482 if (attrs & ParamAttr::NoReturn)
483 Out << " | ParamAttr::NoReturn";
484 if (attrs & ParamAttr::NoUnwind)
485 Out << " | ParamAttr::NoUnwind";
Reid Spencer4f859aa2007-04-22 05:46:44 +0000486 Out << ";";
487 nl(Out);
488 Out << "Attrs.push_back(PAWI);";
Reid Spencera9297b12007-04-11 10:01:32 +0000489 nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000490 }
Reid Spencer4f859aa2007-04-22 05:46:44 +0000491 Out << typeName << "_PAL = ParamAttrsList::get(Attrs);";
492 nl(Out);
493 out(); nl(Out);
494 Out << '}'; nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000495 }
Reid Spencer12803f52006-05-31 17:31:38 +0000496 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000497 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000498 Out << "FunctionType* " << typeName << " = FunctionType::get(";
499 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000500 if (isForward)
501 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000502 Out << ",";
503 nl(Out) << "/*Params=*/" << typeName << "_args,";
Reid Spencer07441662007-04-11 12:28:56 +0000504 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true," : "false,") ;
Reid Spencera9297b12007-04-11 10:01:32 +0000505 nl(Out) << "/*ParamAttrs=*/" << typeName << "_PAL" << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000506 out();
507 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000508 break;
509 }
510 case Type::StructTyID: {
511 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000512 Out << "std::vector<const Type*>" << typeName << "_fields;";
513 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000514 StructType::element_iterator EI = ST->element_begin();
515 StructType::element_iterator EE = ST->element_end();
516 for (; EI != EE; ++EI) {
517 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000518 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000519 std::string fieldName(getCppName(fieldTy));
520 Out << typeName << "_fields.push_back(" << fieldName;
521 if (isForward)
522 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000523 Out << ");";
524 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000525 }
526 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer46fea102007-04-11 12:41:49 +0000527 << typeName << "_fields, /*isPacked=*/"
528 << (ST->isPacked() ? "true" : "false") << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000529 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000530 break;
531 }
532 case Type::ArrayTyID: {
533 const ArrayType* AT = cast<ArrayType>(Ty);
534 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000535 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000536 std::string elemName(getCppName(ET));
537 Out << "ArrayType* " << typeName << " = ArrayType::get("
538 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000539 << ", " << utostr(AT->getNumElements()) << ");";
540 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000541 break;
542 }
543 case Type::PointerTyID: {
544 const PointerType* PT = cast<PointerType>(Ty);
545 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000546 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000547 std::string elemName(getCppName(ET));
548 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000549 << elemName << (isForward ? "_fwd" : "") << ");";
550 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000551 break;
552 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000553 case Type::VectorTyID: {
554 const VectorType* PT = cast<VectorType>(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000555 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000556 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000557 std::string elemName(getCppName(ET));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000558 Out << "VectorType* " << typeName << " = VectorType::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000559 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000560 << ", " << utostr(PT->getNumElements()) << ");";
561 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000562 break;
563 }
564 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000565 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
566 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000567 break;
568 }
569 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000570 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000571 }
572
Reid Spencer74e032a2006-05-29 02:58:15 +0000573 // If the type had a name, make sure we recreate it.
574 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000575 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer07441662007-04-11 12:28:56 +0000576 if (progTypeName) {
Reid Spencer74e032a2006-05-29 02:58:15 +0000577 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000578 << typeName << ");";
579 nl(Out);
Reid Spencer07441662007-04-11 12:28:56 +0000580 }
Reid Spencer74e032a2006-05-29 02:58:15 +0000581
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000582 // Pop us off the type stack
583 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000584
585 // Indicate that this type is now defined.
586 DefinedTypes.insert(Ty);
587
588 // Early resolve as many unresolved types as possible. Search the unresolved
589 // types map for the type we just printed. Now that its definition is complete
590 // we can resolve any previous references to it. This prevents a cascade of
591 // unresolved types.
592 TypeMap::iterator I = UnresolvedTypes.find(Ty);
593 if (I != UnresolvedTypes.end()) {
594 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000595 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
596 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000597 Out << I->second << " = cast<";
598 switch (Ty->getTypeID()) {
599 case Type::FunctionTyID: Out << "FunctionType"; break;
600 case Type::ArrayTyID: Out << "ArrayType"; break;
601 case Type::StructTyID: Out << "StructType"; break;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000602 case Type::VectorTyID: Out << "VectorType"; break;
Reid Spencer15f7e012006-05-30 21:18:23 +0000603 case Type::PointerTyID: Out << "PointerType"; break;
604 case Type::OpaqueTyID: Out << "OpaqueType"; break;
605 default: Out << "NoSuchDerivedType"; break;
606 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000607 Out << ">(" << I->second << "_fwd.get());";
608 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000609 UnresolvedTypes.erase(I);
610 }
611
612 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000613 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000614
615 // We weren't a recursive type
616 return false;
617}
618
Reid Spencer12803f52006-05-31 17:31:38 +0000619// Prints a type definition. Returns true if it could not resolve all the types
620// in the definition but had to use a forward reference.
621void
622CppWriter::printType(const Type* Ty) {
623 assert(TypeStack.empty());
624 TypeStack.clear();
625 printTypeInternal(Ty);
626 assert(TypeStack.empty());
627}
628
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000629void
630CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000631
632 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000633 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
634 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
635 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000636
637 // For primitive types and types already defined, just add a name
638 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Chris Lattner42a75512007-01-15 02:27:26 +0000639 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000640 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000641 Out << "mod->addTypeName(\"";
642 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000643 Out << "\", " << getCppName(TI->second) << ");";
644 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000645 // For everything else, define the type
646 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000647 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000648 }
649 }
650
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000651 // Add all of the global variables to the value table...
652 for (Module::const_global_iterator I = TheModule->global_begin(),
653 E = TheModule->global_end(); I != E; ++I) {
654 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000655 printType(I->getInitializer()->getType());
656 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000657 }
658
659 // Add all the functions to the table
660 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
661 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000662 printType(FI->getReturnType());
663 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000664 // Add all the function arguments
665 for(Function::const_arg_iterator AI = FI->arg_begin(),
666 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000667 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000668 }
669
670 // Add all of the basic blocks and instructions
671 for (Function::const_iterator BB = FI->begin(),
672 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000673 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000674 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
675 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000676 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000677 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000678 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000679 }
680 }
681 }
682}
683
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000684
Reid Spencere0d133f2006-05-29 18:08:06 +0000685// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000686void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000687 // First, if the constant is actually a GlobalValue (variable or function) or
688 // its already in the constant list then we've printed it already and we can
689 // just return.
690 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000691 return;
692
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000693 std::string constName(getCppName(CV));
694 std::string typeName(getCppName(CV->getType()));
695 if (CV->isNullValue()) {
696 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000697 << typeName << ");";
698 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000699 return;
700 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000701 if (isa<GlobalValue>(CV)) {
702 // Skip variables and functions, we emit them elsewhere
703 return;
704 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000705 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4d26a062007-04-11 13:02:56 +0000706 Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
707 << cast<IntegerType>(CI->getType())->getBitWidth() << ", "
Reid Spencer70297252007-03-01 20:55:43 +0000708 << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000709 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000710 Out << "ConstantAggregateZero* " << constName
711 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000712 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000713 Out << "ConstantPointerNull* " << constName
714 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000715 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000716 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000717 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000718 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000719 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000720 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000721 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000722 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000723 // Determine if we want null termination or not.
724 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000725 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000726 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000727 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000728 Out << ");";
729 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000730 Out << "std::vector<Constant*> " << constName << "_elems;";
731 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000732 unsigned N = CA->getNumOperands();
733 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000734 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000735 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000736 << getCppName(CA->getOperand(i)) << ");";
737 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000738 }
739 Out << "Constant* " << constName << " = ConstantArray::get("
740 << typeName << ", " << constName << "_elems);";
741 }
742 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000743 Out << "std::vector<Constant*> " << constName << "_fields;";
744 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000745 unsigned N = CS->getNumOperands();
746 for (unsigned i = 0; i < N; i++) {
747 printConstant(CS->getOperand(i));
748 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000749 << getCppName(CS->getOperand(i)) << ");";
750 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000751 }
752 Out << "Constant* " << constName << " = ConstantStruct::get("
753 << typeName << ", " << constName << "_fields);";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000754 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000755 Out << "std::vector<Constant*> " << constName << "_elems;";
756 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000757 unsigned N = CP->getNumOperands();
758 for (unsigned i = 0; i < N; ++i) {
759 printConstant(CP->getOperand(i));
760 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000761 << getCppName(CP->getOperand(i)) << ");";
762 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000763 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000764 Out << "Constant* " << constName << " = ConstantVector::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000765 << typeName << ", " << constName << "_elems);";
766 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000767 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000768 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000769 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000770 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000771 Out << "std::vector<Constant*> " << constName << "_indices;";
772 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000773 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000774 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000775 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000776 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000777 << getCppName(CE->getOperand(i)) << ");";
778 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000779 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000780 Out << "Constant* " << constName
781 << " = ConstantExpr::getGetElementPtr("
782 << getCppName(CE->getOperand(0)) << ", "
Reid Spencer07441662007-04-11 12:28:56 +0000783 << "&" << constName << "_indices[0], " << CE->getNumOperands() - 1
784 << " );";
Reid Spencer3da59db2006-11-27 01:05:10 +0000785 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000786 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000787 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000788 switch (CE->getOpcode()) {
789 default: assert(0 && "Invalid cast opcode");
790 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
791 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
792 case Instruction::SExt: Out << "Instruction::SExt"; break;
793 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
794 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
795 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
796 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
797 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
798 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
799 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
800 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
801 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
802 }
803 Out << ", " << getCppName(CE->getOperand(0)) << ", "
804 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000805 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000806 unsigned N = CE->getNumOperands();
807 for (unsigned i = 0; i < N; ++i ) {
808 printConstant(CE->getOperand(i));
809 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000810 Out << "Constant* " << constName << " = ConstantExpr::";
811 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000812 case Instruction::Add: Out << "getAdd("; break;
813 case Instruction::Sub: Out << "getSub("; break;
814 case Instruction::Mul: Out << "getMul("; break;
815 case Instruction::UDiv: Out << "getUDiv("; break;
816 case Instruction::SDiv: Out << "getSDiv("; break;
817 case Instruction::FDiv: Out << "getFDiv("; break;
818 case Instruction::URem: Out << "getURem("; break;
819 case Instruction::SRem: Out << "getSRem("; break;
820 case Instruction::FRem: Out << "getFRem("; break;
821 case Instruction::And: Out << "getAnd("; break;
822 case Instruction::Or: Out << "getOr("; break;
823 case Instruction::Xor: Out << "getXor("; break;
824 case Instruction::ICmp:
825 Out << "getICmp(ICmpInst::ICMP_";
826 switch (CE->getPredicate()) {
827 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
828 case ICmpInst::ICMP_NE: Out << "NE"; break;
829 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
830 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
831 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
832 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
833 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
834 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
835 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
836 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
837 default: error("Invalid ICmp Predicate");
838 }
839 break;
840 case Instruction::FCmp:
841 Out << "getFCmp(FCmpInst::FCMP_";
842 switch (CE->getPredicate()) {
843 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
844 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
845 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
846 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
847 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
848 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
849 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
850 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
851 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
852 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
853 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
854 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
855 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
856 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
857 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
858 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
859 default: error("Invalid FCmp Predicate");
860 }
861 break;
862 case Instruction::Shl: Out << "getShl("; break;
863 case Instruction::LShr: Out << "getLShr("; break;
864 case Instruction::AShr: Out << "getAShr("; break;
865 case Instruction::Select: Out << "getSelect("; break;
866 case Instruction::ExtractElement: Out << "getExtractElement("; break;
867 case Instruction::InsertElement: Out << "getInsertElement("; break;
868 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000869 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000870 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000871 break;
872 }
873 Out << getCppName(CE->getOperand(0));
874 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
875 Out << ", " << getCppName(CE->getOperand(i));
876 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000877 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000878 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000879 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000880 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000881 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000882 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000883}
884
Reid Spencer12803f52006-05-31 17:31:38 +0000885void
886CppWriter::printConstants(const Module* M) {
887 // Traverse all the global variables looking for constant initializers
888 for (Module::const_global_iterator I = TheModule->global_begin(),
889 E = TheModule->global_end(); I != E; ++I)
890 if (I->hasInitializer())
891 printConstant(I->getInitializer());
892
893 // Traverse the LLVM functions looking for constants
894 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
895 FI != FE; ++FI) {
896 // Add all of the basic blocks and instructions
897 for (Function::const_iterator BB = FI->begin(),
898 E = FI->end(); BB != E; ++BB) {
899 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
900 ++I) {
901 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
902 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
903 printConstant(C);
904 }
905 }
906 }
907 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000908 }
Reid Spencer66c87342006-05-30 03:43:49 +0000909}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000910
Reid Spencer12803f52006-05-31 17:31:38 +0000911void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000912 nl(Out) << "// Type Definitions";
913 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000914 printType(GV->getType());
915 if (GV->hasInitializer()) {
916 Constant* Init = GV->getInitializer();
917 printType(Init->getType());
918 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000919 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000920 printFunctionHead(F);
921 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000922 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000923 printVariableHead(gv);
924 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000925 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000926 printConstant(gv);
927 }
928 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000929 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000930 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000931 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000932 }
Reid Spencer12803f52006-05-31 17:31:38 +0000933}
Reid Spencer15f7e012006-05-30 21:18:23 +0000934
Reid Spencer12803f52006-05-31 17:31:38 +0000935void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000936 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000937 if (is_inline) {
938 Out << " = mod->getGlobalVariable(";
939 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000940 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
941 nl(Out) << "if (!" << getCppName(GV) << ") {";
942 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000943 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000944 Out << " = new GlobalVariable(";
945 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000946 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000947 Out << ",";
948 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
949 Out << ",";
950 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000951 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000952 Out << ",";
953 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000954 if (GV->hasInitializer()) {
955 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000956 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000957 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000958 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000959 Out << "\",";
960 nl(Out) << "mod);";
961 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000962
963 if (GV->hasSection()) {
964 printCppName(GV);
965 Out << "->setSection(\"";
966 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000967 Out << "\");";
968 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000969 }
970 if (GV->getAlignment()) {
971 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000972 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
973 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000974 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000975 if (is_inline) {
976 out(); Out << "}"; nl(Out);
977 }
Reid Spencer12803f52006-05-31 17:31:38 +0000978}
979
980void
981CppWriter::printVariableBody(const GlobalVariable *GV) {
982 if (GV->hasInitializer()) {
983 printCppName(GV);
984 Out << "->setInitializer(";
985 //if (!isa<GlobalValue(GV->getInitializer()))
986 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000987 Out << getCppName(GV->getInitializer()) << ");";
988 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000989 }
990}
991
992std::string
993CppWriter::getOpName(Value* V) {
994 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
995 return getCppName(V);
996
997 // See if its alread in the map of forward references, if so just return the
998 // name we already set up for it
999 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
1000 if (I != ForwardRefs.end())
1001 return I->second;
1002
1003 // This is a new forward reference. Generate a unique name for it
1004 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
1005
1006 // Yes, this is a hack. An Argument is the smallest instantiable value that
1007 // we can make as a placeholder for the real value. We'll replace these
1008 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001009 Out << "Argument* " << result << " = new Argument("
1010 << getCppName(V->getType()) << ");";
1011 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001012 ForwardRefs[V] = result;
1013 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001014}
1015
Reid Spencere0d133f2006-05-29 18:08:06 +00001016// printInstruction - This member is called for each Instruction in a function.
1017void
Reid Spencer12803f52006-05-31 17:31:38 +00001018CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +00001019 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001020
Reid Spencer15f7e012006-05-30 21:18:23 +00001021 // Before we emit this instruction, we need to take care of generating any
1022 // forward references. So, we get the names of all the operands in advance
1023 std::string* opNames = new std::string[I->getNumOperands()];
1024 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1025 opNames[i] = getOpName(I->getOperand(i));
1026 }
1027
Reid Spencere0d133f2006-05-29 18:08:06 +00001028 switch (I->getOpcode()) {
1029 case Instruction::Ret: {
1030 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001031 Out << "new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001032 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001033 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001034 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001035 case Instruction::Br: {
1036 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001037 Out << "new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001038 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001039 Out << opNames[0] << ", "
1040 << opNames[1] << ", "
1041 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001042
Reid Spencere0d133f2006-05-29 18:08:06 +00001043 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001044 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001045 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001046 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001047 }
1048 Out << bbname << ");";
1049 break;
1050 }
Reid Spencer66c87342006-05-30 03:43:49 +00001051 case Instruction::Switch: {
1052 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001053 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001054 << opNames[0] << ", "
1055 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001056 << sw->getNumCases() << ", " << bbname << ");";
1057 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001058 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001059 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001060 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001061 << opNames[i+1] << ");";
1062 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001063 }
1064 break;
1065 }
1066 case Instruction::Invoke: {
1067 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001068 Out << "std::vector<Value*> " << iName << "_params;";
1069 nl(Out);
1070 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1071 Out << iName << "_params.push_back("
1072 << opNames[i] << ");";
1073 nl(Out);
1074 }
Reid Spencer07441662007-04-11 12:28:56 +00001075 Out << "InvokeInst *" << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001076 << opNames[0] << ", "
1077 << opNames[1] << ", "
1078 << opNames[2] << ", "
Reid Spencer07441662007-04-11 12:28:56 +00001079 << "&" << iName << "_params[0], " << inv->getNumOperands() - 3
1080 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001081 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001082 Out << "\", " << bbname << ");";
1083 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001084 printCallingConv(inv->getCallingConv());
1085 Out << ");";
1086 break;
1087 }
1088 case Instruction::Unwind: {
Reid Spencer07441662007-04-11 12:28:56 +00001089 Out << "new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001090 << bbname << ");";
1091 break;
1092 }
1093 case Instruction::Unreachable:{
Reid Spencer07441662007-04-11 12:28:56 +00001094 Out << "new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001095 << bbname << ");";
1096 break;
1097 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001098 case Instruction::Add:
1099 case Instruction::Sub:
1100 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001101 case Instruction::UDiv:
1102 case Instruction::SDiv:
1103 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001104 case Instruction::URem:
1105 case Instruction::SRem:
1106 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001107 case Instruction::And:
1108 case Instruction::Or:
1109 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001110 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001111 case Instruction::LShr:
1112 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001113 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001114 switch (I->getOpcode()) {
1115 case Instruction::Add: Out << "Instruction::Add"; break;
1116 case Instruction::Sub: Out << "Instruction::Sub"; break;
1117 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001118 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1119 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1120 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001121 case Instruction::URem:Out << "Instruction::URem"; break;
1122 case Instruction::SRem:Out << "Instruction::SRem"; break;
1123 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001124 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001125 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001126 case Instruction::Xor: Out << "Instruction::Xor"; break;
1127 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001128 case Instruction::LShr:Out << "Instruction::LShr"; break;
1129 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001130 default: Out << "Instruction::BadOpCode"; break;
1131 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001132 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001133 printEscapedString(I->getName());
1134 Out << "\", " << bbname << ");";
1135 break;
1136 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001137 case Instruction::FCmp: {
1138 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1139 switch (cast<FCmpInst>(I)->getPredicate()) {
1140 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1141 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1142 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1143 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1144 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1145 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1146 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1147 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1148 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1149 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1150 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1151 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1152 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1153 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1154 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1155 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1156 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1157 }
1158 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1159 printEscapedString(I->getName());
1160 Out << "\", " << bbname << ");";
1161 break;
1162 }
1163 case Instruction::ICmp: {
1164 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1165 switch (cast<ICmpInst>(I)->getPredicate()) {
1166 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1167 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1168 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1169 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1170 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1171 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1172 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1173 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1174 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1175 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1176 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001177 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001178 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001179 printEscapedString(I->getName());
1180 Out << "\", " << bbname << ");";
1181 break;
1182 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001183 case Instruction::Malloc: {
1184 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001185 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001186 << getCppName(mallocI->getAllocatedType()) << ", ";
1187 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001188 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001189 Out << "\"";
1190 printEscapedString(mallocI->getName());
1191 Out << "\", " << bbname << ");";
1192 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001193 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001194 << mallocI->getAlignment() << ");";
1195 break;
1196 }
Reid Spencer66c87342006-05-30 03:43:49 +00001197 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001198 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001199 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1200 break;
1201 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001202 case Instruction::Alloca: {
1203 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001204 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001205 << getCppName(allocaI->getAllocatedType()) << ", ";
1206 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001207 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001208 Out << "\"";
1209 printEscapedString(allocaI->getName());
1210 Out << "\", " << bbname << ");";
1211 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001212 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001213 << allocaI->getAlignment() << ");";
1214 break;
1215 }
Reid Spencer66c87342006-05-30 03:43:49 +00001216 case Instruction::Load:{
1217 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001218 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001219 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001220 printEscapedString(load->getName());
1221 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001222 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001223 break;
1224 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001225 case Instruction::Store: {
1226 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001227 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001228 << opNames[0] << ", "
1229 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001230 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001231 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001232 break;
1233 }
1234 case Instruction::GetElementPtr: {
1235 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1236 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001237 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001238 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001239 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001240 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001241 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001242 Out << "std::vector<Value*> " << iName << "_indices;";
1243 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001244 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001245 Out << iName << "_indices.push_back("
1246 << opNames[i] << ");";
1247 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001248 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001249 Out << "Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer07441662007-04-11 12:28:56 +00001250 << opNames[0] << ", &" << iName << "_indices[0], "
1251 << gep->getNumOperands() - 1;
Reid Spencere0d133f2006-05-29 18:08:06 +00001252 }
1253 Out << ", \"";
1254 printEscapedString(gep->getName());
1255 Out << "\", " << bbname << ");";
1256 break;
1257 }
Reid Spencer66c87342006-05-30 03:43:49 +00001258 case Instruction::PHI: {
1259 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001260
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001261 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001262 << getCppName(phi->getType()) << ", \"";
1263 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001264 Out << "\", " << bbname << ");";
1265 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001266 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001267 << ");";
1268 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001269 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001270 Out << iName << "->addIncoming("
1271 << opNames[i] << ", " << opNames[i+1] << ");";
1272 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001273 }
Reid Spencer66c87342006-05-30 03:43:49 +00001274 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001275 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001276 case Instruction::Trunc:
1277 case Instruction::ZExt:
1278 case Instruction::SExt:
1279 case Instruction::FPTrunc:
1280 case Instruction::FPExt:
1281 case Instruction::FPToUI:
1282 case Instruction::FPToSI:
1283 case Instruction::UIToFP:
1284 case Instruction::SIToFP:
1285 case Instruction::PtrToInt:
1286 case Instruction::IntToPtr:
1287 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001288 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001289 Out << "CastInst* " << iName << " = new ";
1290 switch (I->getOpcode()) {
Reid Spencer34816572007-02-16 06:34:39 +00001291 case Instruction::Trunc: Out << "TruncInst"; break;
1292 case Instruction::ZExt: Out << "ZExtInst"; break;
1293 case Instruction::SExt: Out << "SExtInst"; break;
1294 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1295 case Instruction::FPExt: Out << "FPExtInst"; break;
1296 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1297 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1298 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1299 case Instruction::SIToFP: Out << "SIToFPInst"; break;
Reid Spencer07441662007-04-11 12:28:56 +00001300 case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
Reid Spencer34816572007-02-16 06:34:39 +00001301 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1302 case Instruction::BitCast: Out << "BitCastInst"; break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001303 default: assert(!"Unreachable"); break;
1304 }
1305 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001306 << getCppName(cst->getType()) << ", \"";
1307 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001308 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001309 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001310 }
Reid Spencer66c87342006-05-30 03:43:49 +00001311 case Instruction::Call:{
1312 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001313 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001314 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001315 << getCppName(ila->getFunctionType()) << ", \""
1316 << ila->getAsmString() << "\", \""
1317 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001318 << (ila->hasSideEffects() ? "true" : "false") << ");";
1319 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001320 }
Reid Spencer66c87342006-05-30 03:43:49 +00001321 if (call->getNumOperands() > 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001322 Out << "std::vector<Value*> " << iName << "_params;";
1323 nl(Out);
1324 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1325 Out << iName << "_params.push_back(" << opNames[i] << ");";
1326 nl(Out);
1327 }
1328 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer07441662007-04-11 12:28:56 +00001329 << opNames[0] << ", &" << iName << "_params[0], "
1330 << call->getNumOperands() - 1 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001331 } else if (call->getNumOperands() == 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001332 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001333 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001334 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001335 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001336 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001337 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001338 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001339 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001340 }
1341 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001342 Out << "\", " << bbname << ");";
1343 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001344 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001345 Out << ");";
1346 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001347 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001348 Out << ");";
1349 break;
1350 }
1351 case Instruction::Select: {
1352 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001353 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001354 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001355 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001356 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001357 break;
1358 }
1359 case Instruction::UserOp1:
1360 /// FALL THROUGH
1361 case Instruction::UserOp2: {
1362 /// FIXME: What should be done here?
1363 break;
1364 }
1365 case Instruction::VAArg: {
1366 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001367 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001368 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001369 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001370 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001371 break;
1372 }
1373 case Instruction::ExtractElement: {
1374 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001375 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001376 << " = new ExtractElementInst(" << opNames[0]
1377 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001378 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001379 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001380 break;
1381 }
1382 case Instruction::InsertElement: {
1383 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001384 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001385 << " = new InsertElementInst(" << opNames[0]
1386 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001387 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001388 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001389 break;
1390 }
1391 case Instruction::ShuffleVector: {
1392 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001393 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001394 << " = new ShuffleVectorInst(" << opNames[0]
1395 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001396 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001397 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001398 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001399 }
1400 }
Reid Spencer68464b72006-06-15 16:09:59 +00001401 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001402 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001403 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001404}
1405
Reid Spencer12803f52006-05-31 17:31:38 +00001406// Print out the types, constants and declarations needed by one function
1407void CppWriter::printFunctionUses(const Function* F) {
1408
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001409 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001410 if (!is_inline) {
1411 // Print the function's return type
1412 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001413
Reid Spencerf977e7b2006-06-01 23:43:47 +00001414 // Print the function's function type
1415 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001416
Reid Spencerf977e7b2006-06-01 23:43:47 +00001417 // Print the types of each of the function's arguments
1418 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1419 AI != AE; ++AI) {
1420 printType(AI->getType());
1421 }
Reid Spencer12803f52006-05-31 17:31:38 +00001422 }
1423
1424 // Print type definitions for every type referenced by an instruction and
1425 // make a note of any global values or constants that are referenced
1426 std::vector<GlobalValue*> gvs;
1427 std::vector<Constant*> consts;
1428 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1429 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1430 I != E; ++I) {
1431 // Print the type of the instruction itself
1432 printType(I->getType());
1433
1434 // Print the type of each of the instruction's operands
1435 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1436 Value* operand = I->getOperand(i);
1437 printType(operand->getType());
1438 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1439 gvs.push_back(GV);
1440 else if (Constant* C = dyn_cast<Constant>(operand))
1441 consts.push_back(C);
1442 }
1443 }
1444 }
1445
1446 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001447 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001448 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1449 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001450 if (Function* Fun = dyn_cast<Function>(*I)) {
1451 if (!is_inline || Fun != F)
1452 printFunctionHead(Fun);
1453 }
Reid Spencer12803f52006-05-31 17:31:38 +00001454 }
1455
1456 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001457 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001458 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1459 I != E; ++I) {
1460 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1461 printVariableHead(F);
1462 }
1463
1464 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001465 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001466 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1467 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001468 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001469 }
1470
1471 // Process the global variables definitions now that all the constants have
1472 // been emitted. These definitions just couple the gvars with their constant
1473 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001474 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001475 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1476 I != E; ++I) {
1477 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1478 printVariableBody(GV);
1479 }
1480}
1481
1482void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001483 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001484 if (is_inline) {
1485 Out << " = mod->getFunction(\"";
1486 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001487 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1488 nl(Out) << "if (!" << getCppName(F) << ") {";
1489 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001490 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001491 Out<< " = new Function(";
1492 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1493 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001494 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001495 Out << ",";
1496 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001497 printEscapedString(F->getName());
Reid Spencer5cbf9852007-01-30 20:08:39 +00001498 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001499 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001500 printCppName(F);
1501 Out << "->setCallingConv(";
1502 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001503 Out << ");";
1504 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001505 if (F->hasSection()) {
1506 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001507 Out << "->setSection(\"" << F->getSection() << "\");";
1508 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001509 }
1510 if (F->getAlignment()) {
1511 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001512 Out << "->setAlignment(" << F->getAlignment() << ");";
1513 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001514 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001515 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001516 Out << "}";
1517 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001518 }
Reid Spencer12803f52006-05-31 17:31:38 +00001519}
1520
1521void CppWriter::printFunctionBody(const Function *F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001522 if (F->isDeclaration())
Reid Spencer12803f52006-05-31 17:31:38 +00001523 return; // external functions have no bodies.
1524
1525 // Clear the DefinedValues and ForwardRefs maps because we can't have
1526 // cross-function forward refs
1527 ForwardRefs.clear();
1528 DefinedValues.clear();
1529
1530 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001531 if (!is_inline) {
1532 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001533 Out << "Function::arg_iterator args = " << getCppName(F)
1534 << "->arg_begin();";
1535 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001536 }
1537 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1538 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001539 Out << "Value* " << getCppName(AI) << " = args++;";
1540 nl(Out);
1541 if (AI->hasName()) {
1542 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1543 nl(Out);
1544 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001545 }
Reid Spencer12803f52006-05-31 17:31:38 +00001546 }
1547
1548 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001549 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001550 for (Function::const_iterator BI = F->begin(), BE = F->end();
1551 BI != BE; ++BI) {
1552 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001553 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001554 if (BI->hasName())
1555 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001556 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1557 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001558 }
1559
1560 // Output all of its basic blocks... for the function
1561 for (Function::const_iterator BI = F->begin(), BE = F->end();
1562 BI != BE; ++BI) {
1563 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001564 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1565 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001566
1567 // Output all of the instructions in the basic block...
1568 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1569 I != E; ++I) {
1570 printInstruction(I,bbname);
1571 }
1572 }
1573
1574 // Loop over the ForwardRefs and resolve them now that all instructions
1575 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001576 if (!ForwardRefs.empty()) {
1577 nl(Out) << "// Resolve Forward References";
1578 nl(Out);
1579 }
1580
Reid Spencer12803f52006-05-31 17:31:38 +00001581 while (!ForwardRefs.empty()) {
1582 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001583 Out << I->second << "->replaceAllUsesWith("
1584 << getCppName(I->first) << "); delete " << I->second << ";";
1585 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001586 ForwardRefs.erase(I);
1587 }
1588}
1589
Reid Spencerf977e7b2006-06-01 23:43:47 +00001590void CppWriter::printInline(const std::string& fname, const std::string& func) {
Reid Spencer688b0492007-02-05 21:19:13 +00001591 const Function* F = TheModule->getFunction(func);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001592 if (!F) {
1593 error(std::string("Function '") + func + "' not found in input module");
1594 return;
1595 }
Reid Spencer5cbf9852007-01-30 20:08:39 +00001596 if (F->isDeclaration()) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001597 error(std::string("Function '") + func + "' is external!");
1598 return;
1599 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001600 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001601 << getCppName(F);
1602 unsigned arg_count = 1;
1603 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1604 AI != AE; ++AI) {
1605 Out << ", Value* arg_" << arg_count;
1606 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001607 Out << ") {";
1608 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001609 is_inline = true;
1610 printFunctionUses(F);
1611 printFunctionBody(F);
1612 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001613 Out << "return " << getCppName(F->begin()) << ";";
1614 nl(Out) << "}";
1615 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001616}
1617
Reid Spencer12803f52006-05-31 17:31:38 +00001618void CppWriter::printModuleBody() {
1619 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001620 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001621 printTypes(TheModule);
1622
1623 // Functions can call each other and global variables can reference them so
1624 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001625 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001626 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1627 I != E; ++I)
1628 printFunctionHead(I);
1629
1630 // Process the global variables declarations. We can't initialze them until
1631 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001632 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001633 for (Module::const_global_iterator I = TheModule->global_begin(),
1634 E = TheModule->global_end(); I != E; ++I) {
1635 printVariableHead(I);
1636 }
1637
1638 // Print out all the constants definitions. Constants don't recurse except
1639 // through GlobalValues. All GlobalValues have been declared at this point
1640 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001641 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001642 printConstants(TheModule);
1643
1644 // Process the global variables definitions now that all the constants have
1645 // been emitted. These definitions just couple the gvars with their constant
1646 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001647 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001648 for (Module::const_global_iterator I = TheModule->global_begin(),
1649 E = TheModule->global_end(); I != E; ++I) {
1650 printVariableBody(I);
1651 }
1652
1653 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001654 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001655 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1656 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001657 if (!I->isDeclaration()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001658 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1659 << ")";
1660 nl(Out) << "{";
1661 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001662 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001663 nl(Out,-1) << "}";
1664 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001665 }
1666 }
1667}
1668
1669void CppWriter::printProgram(
1670 const std::string& fname,
1671 const std::string& mName
1672) {
1673 Out << "#include <llvm/Module.h>\n";
1674 Out << "#include <llvm/DerivedTypes.h>\n";
1675 Out << "#include <llvm/Constants.h>\n";
1676 Out << "#include <llvm/GlobalVariable.h>\n";
1677 Out << "#include <llvm/Function.h>\n";
1678 Out << "#include <llvm/CallingConv.h>\n";
1679 Out << "#include <llvm/BasicBlock.h>\n";
1680 Out << "#include <llvm/Instructions.h>\n";
1681 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencer07441662007-04-11 12:28:56 +00001682 Out << "#include <llvm/ParameterAttributes.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001683 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001684 Out << "#include <llvm/Pass.h>\n";
1685 Out << "#include <llvm/PassManager.h>\n";
1686 Out << "#include <llvm/Analysis/Verifier.h>\n";
1687 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1688 Out << "#include <algorithm>\n";
1689 Out << "#include <iostream>\n\n";
1690 Out << "using namespace llvm;\n\n";
1691 Out << "Module* " << fname << "();\n\n";
1692 Out << "int main(int argc, char**argv) {\n";
Nick Lewycky8946fe12007-06-16 16:17:35 +00001693 Out << " Module* Mod = " << fname << "();\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001694 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1695 Out << " std::cerr.flush();\n";
1696 Out << " std::cout.flush();\n";
1697 Out << " PassManager PM;\n";
Reid Spencer07441662007-04-11 12:28:56 +00001698 Out << " PM.add(new PrintModulePass(&llvm::cout));\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001699 Out << " PM.run(*Mod);\n";
1700 Out << " return 0;\n";
1701 Out << "}\n\n";
1702 printModule(fname,mName);
1703}
1704
1705void CppWriter::printModule(
1706 const std::string& fname,
1707 const std::string& mName
1708) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001709 nl(Out) << "Module* " << fname << "() {";
1710 nl(Out,1) << "// Module Construction";
1711 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001712 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001713 nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
1714 }
1715 if (!TheModule->getTargetTriple().empty()) {
1716 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1717 << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001718 }
Reid Spencer12803f52006-05-31 17:31:38 +00001719
1720 if (!TheModule->getModuleInlineAsm().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001721 nl(Out) << "mod->setModuleInlineAsm(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001722 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001723 Out << "\");";
Reid Spencer12803f52006-05-31 17:31:38 +00001724 }
Reid Spencer07441662007-04-11 12:28:56 +00001725 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001726
1727 // Loop over the dependent libraries and emit them.
1728 Module::lib_iterator LI = TheModule->lib_begin();
1729 Module::lib_iterator LE = TheModule->lib_end();
1730 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001731 Out << "mod->addLibrary(\"" << *LI << "\");";
1732 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001733 ++LI;
1734 }
1735 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001736 nl(Out) << "return mod;";
1737 nl(Out,-1) << "}";
1738 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001739}
1740
1741void CppWriter::printContents(
1742 const std::string& fname, // Name of generated function
1743 const std::string& mName // Name of module generated module
1744) {
1745 Out << "\nModule* " << fname << "(Module *mod) {\n";
1746 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001747 printModuleBody();
1748 Out << "\nreturn mod;\n";
1749 Out << "\n}\n";
1750}
1751
1752void CppWriter::printFunction(
1753 const std::string& fname, // Name of generated function
1754 const std::string& funcName // Name of function to generate
1755) {
Reid Spencer688b0492007-02-05 21:19:13 +00001756 const Function* F = TheModule->getFunction(funcName);
Reid Spencer12803f52006-05-31 17:31:38 +00001757 if (!F) {
1758 error(std::string("Function '") + funcName + "' not found in input module");
1759 return;
1760 }
1761 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1762 printFunctionUses(F);
1763 printFunctionHead(F);
1764 printFunctionBody(F);
1765 Out << "return " << getCppName(F) << ";\n";
1766 Out << "}\n";
1767}
1768
1769void CppWriter::printVariable(
1770 const std::string& fname, /// Name of generated function
1771 const std::string& varName // Name of variable to generate
1772) {
1773 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1774
1775 if (!GV) {
1776 error(std::string("Variable '") + varName + "' not found in input module");
1777 return;
1778 }
1779 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1780 printVariableUses(GV);
1781 printVariableHead(GV);
1782 printVariableBody(GV);
1783 Out << "return " << getCppName(GV) << ";\n";
1784 Out << "}\n";
1785}
1786
1787void CppWriter::printType(
1788 const std::string& fname, /// Name of generated function
1789 const std::string& typeName // Name of type to generate
1790) {
1791 const Type* Ty = TheModule->getTypeByName(typeName);
1792 if (!Ty) {
1793 error(std::string("Type '") + typeName + "' not found in input module");
1794 return;
1795 }
1796 Out << "\nType* " << fname << "(Module *mod) {\n";
1797 printType(Ty);
1798 Out << "return " << getCppName(Ty) << ";\n";
1799 Out << "}\n";
1800}
1801
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001802} // end anonymous llvm
1803
1804namespace llvm {
1805
1806void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001807 // Initialize a CppWriter for us to use
1808 CppWriter W(o, mod);
1809
1810 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001811 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001812
1813 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001814 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001815
1816 // Get the name of the thing we are to generate
1817 std::string tgtname = NameToGenerate.getValue();
1818 if (GenerationType == GenModule ||
1819 GenerationType == GenContents ||
1820 GenerationType == GenProgram) {
1821 if (tgtname == "!bad!") {
1822 if (mod->getModuleIdentifier() == "-")
1823 tgtname = "<stdin>";
1824 else
1825 tgtname = mod->getModuleIdentifier();
1826 }
1827 } else if (tgtname == "!bad!") {
1828 W.error("You must use the -for option with -gen-{function,variable,type}");
1829 }
1830
1831 switch (WhatToGenerate(GenerationType)) {
1832 case GenProgram:
1833 if (fname.empty())
1834 fname = "makeLLVMModule";
1835 W.printProgram(fname,tgtname);
1836 break;
1837 case GenModule:
1838 if (fname.empty())
1839 fname = "makeLLVMModule";
1840 W.printModule(fname,tgtname);
1841 break;
1842 case GenContents:
1843 if (fname.empty())
1844 fname = "makeLLVMModuleContents";
1845 W.printContents(fname,tgtname);
1846 break;
1847 case GenFunction:
1848 if (fname.empty())
1849 fname = "makeLLVMFunction";
1850 W.printFunction(fname,tgtname);
1851 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001852 case GenInline:
1853 if (fname.empty())
1854 fname = "makeLLVMInline";
1855 W.printInline(fname,tgtname);
1856 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001857 case GenVariable:
1858 if (fname.empty())
1859 fname = "makeLLVMVariable";
1860 W.printVariable(fname,tgtname);
1861 break;
1862 case GenType:
1863 if (fname.empty())
1864 fname = "makeLLVMType";
1865 W.printType(fname,tgtname);
1866 break;
1867 default:
1868 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001869 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001870}
1871
1872}