blob: 0b7b0eea0025f8a726ce18d4d79893d24a407c2f [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 Spencera4d71f02007-06-16 20:48:27 +000026#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer15f7e012006-05-30 21:18:23 +000027#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000028#include "llvm/Support/CFG.h"
29#include "llvm/Support/ManagedStatic.h"
30#include "llvm/Support/MathExtras.h"
Reid Spencerf977e7b2006-06-01 23:43:47 +000031#include "llvm/Config/config.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000032#include <algorithm>
33#include <iostream>
Reid Spencer66c87342006-05-30 03:43:49 +000034#include <set>
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000035
36using namespace llvm;
37
Reid Spencer15f7e012006-05-30 21:18:23 +000038static cl::opt<std::string>
Reid Spencer15f7e012006-05-30 21:18:23 +000039FuncName("funcname", cl::desc("Specify the name of the generated function"),
40 cl::value_desc("function name"));
41
Reid Spencer12803f52006-05-31 17:31:38 +000042enum WhatToGenerate {
43 GenProgram,
44 GenModule,
45 GenContents,
46 GenFunction,
Reid Spencerf977e7b2006-06-01 23:43:47 +000047 GenInline,
Reid Spencer12803f52006-05-31 17:31:38 +000048 GenVariable,
49 GenType
50};
51
52static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
53 cl::desc("Choose what kind of output to generate"),
54 cl::init(GenProgram),
55 cl::values(
56 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
57 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
58 clEnumValN(GenContents,"gen-contents", "Generate contents of a module"),
59 clEnumValN(GenFunction,"gen-function", "Generate a function definition"),
Reid Spencerf977e7b2006-06-01 23:43:47 +000060 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
Reid Spencer12803f52006-05-31 17:31:38 +000061 clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"),
62 clEnumValN(GenType, "gen-type", "Generate a type definition"),
63 clEnumValEnd
64 )
65);
66
67static cl::opt<std::string> NameToGenerate("for", cl::Optional,
68 cl::desc("Specify the name of the thing to generate"),
69 cl::init("!bad!"));
Reid Spencer15f7e012006-05-30 21:18:23 +000070
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000071namespace {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000072typedef std::vector<const Type*> TypeList;
73typedef std::map<const Type*,std::string> TypeMap;
74typedef std::map<const Value*,std::string> ValueMap;
Reid Spencer66c87342006-05-30 03:43:49 +000075typedef std::set<std::string> NameSet;
Reid Spencer15f7e012006-05-30 21:18:23 +000076typedef std::set<const Type*> TypeSet;
77typedef std::set<const Value*> ValueSet;
78typedef std::map<const Value*,std::string> ForwardRefMap;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000079
Reid Spencere0d133f2006-05-29 18:08:06 +000080class CppWriter {
Reid Spencer12803f52006-05-31 17:31:38 +000081 const char* progname;
Reid Spencere0d133f2006-05-29 18:08:06 +000082 std::ostream &Out;
83 const Module *TheModule;
Andrew Lenharth539d8712006-05-31 20:18:56 +000084 uint64_t uniqueNum;
Reid Spencere0d133f2006-05-29 18:08:06 +000085 TypeMap TypeNames;
86 ValueMap ValueNames;
87 TypeMap UnresolvedTypes;
88 TypeList TypeStack;
Reid Spencer66c87342006-05-30 03:43:49 +000089 NameSet UsedNames;
Reid Spencer15f7e012006-05-30 21:18:23 +000090 TypeSet DefinedTypes;
91 ValueSet DefinedValues;
92 ForwardRefMap ForwardRefs;
Reid Spencerf977e7b2006-06-01 23:43:47 +000093 bool is_inline;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000094
Reid Spencere0d133f2006-05-29 18:08:06 +000095public:
Reid Spencer12803f52006-05-31 17:31:38 +000096 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
97 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
Reid Spencerf977e7b2006-06-01 23:43:47 +000098 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000099
Reid Spencere0d133f2006-05-29 18:08:06 +0000100 const Module* getModule() { return TheModule; }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000101
Reid Spencer12803f52006-05-31 17:31:38 +0000102 void printProgram(const std::string& fname, const std::string& modName );
103 void printModule(const std::string& fname, const std::string& modName );
104 void printContents(const std::string& fname, const std::string& modName );
105 void printFunction(const std::string& fname, const std::string& funcName );
Reid Spencerf977e7b2006-06-01 23:43:47 +0000106 void printInline(const std::string& fname, const std::string& funcName );
Reid Spencer12803f52006-05-31 17:31:38 +0000107 void printVariable(const std::string& fname, const std::string& varName );
108 void printType(const std::string& fname, const std::string& typeName );
109
110 void error(const std::string& msg);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000111
Reid Spencere0d133f2006-05-29 18:08:06 +0000112private:
Reid Spencer12803f52006-05-31 17:31:38 +0000113 void printLinkageType(GlobalValue::LinkageTypes LT);
114 void printCallingConv(unsigned cc);
115 void printEscapedString(const std::string& str);
116 void printCFP(const ConstantFP* CFP);
117
118 std::string getCppName(const Type* val);
119 inline void printCppName(const Type* val);
120
121 std::string getCppName(const Value* val);
122 inline void printCppName(const Value* val);
123
124 bool printTypeInternal(const Type* Ty);
125 inline void printType(const Type* Ty);
Reid Spencere0d133f2006-05-29 18:08:06 +0000126 void printTypes(const Module* M);
Reid Spencer12803f52006-05-31 17:31:38 +0000127
Reid Spencere0d133f2006-05-29 18:08:06 +0000128 void printConstant(const Constant *CPV);
Reid Spencer12803f52006-05-31 17:31:38 +0000129 void printConstants(const Module* M);
130
131 void printVariableUses(const GlobalVariable *GV);
132 void printVariableHead(const GlobalVariable *GV);
133 void printVariableBody(const GlobalVariable *GV);
134
135 void printFunctionUses(const Function *F);
Reid Spencer66c87342006-05-30 03:43:49 +0000136 void printFunctionHead(const Function *F);
137 void printFunctionBody(const Function *F);
Reid Spencere0d133f2006-05-29 18:08:06 +0000138 void printInstruction(const Instruction *I, const std::string& bbname);
Reid Spencer15f7e012006-05-30 21:18:23 +0000139 std::string getOpName(Value*);
140
Reid Spencer12803f52006-05-31 17:31:38 +0000141 void printModuleBody();
142
Reid Spencere0d133f2006-05-29 18:08:06 +0000143};
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000144
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000145static unsigned indent_level = 0;
146inline std::ostream& nl(std::ostream& Out, int delta = 0) {
147 Out << "\n";
148 if (delta >= 0 || indent_level >= unsigned(-delta))
149 indent_level += delta;
150 for (unsigned i = 0; i < indent_level; ++i)
151 Out << " ";
152 return Out;
153}
154
155inline void in() { indent_level++; }
156inline void out() { if (indent_level >0) indent_level--; }
157
Reid Spencer12803f52006-05-31 17:31:38 +0000158inline void
159sanitize(std::string& str) {
160 for (size_t i = 0; i < str.length(); ++i)
161 if (!isalnum(str[i]) && str[i] != '_')
162 str[i] = '_';
163}
164
Reid Spencera54b7cb2007-01-12 07:05:14 +0000165inline std::string
Reid Spencer12803f52006-05-31 17:31:38 +0000166getTypePrefix(const Type* Ty ) {
Reid Spencer12803f52006-05-31 17:31:38 +0000167 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000168 case Type::VoidTyID: return "void_";
169 case Type::IntegerTyID:
170 return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
171 "_";
172 case Type::FloatTyID: return "float_";
173 case Type::DoubleTyID: return "double_";
174 case Type::LabelTyID: return "label_";
175 case Type::FunctionTyID: return "func_";
176 case Type::StructTyID: return "struct_";
177 case Type::ArrayTyID: return "array_";
178 case Type::PointerTyID: return "ptr_";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000179 case Type::VectorTyID: return "packed_";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000180 case Type::OpaqueTyID: return "opaque_";
181 default: return "other_";
Reid Spencer12803f52006-05-31 17:31:38 +0000182 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000183 return "unknown_";
Reid Spencer12803f52006-05-31 17:31:38 +0000184}
185
186// Looks up the type in the symbol table and returns a pointer to its name or
187// a null pointer if it wasn't found. Note that this isn't the same as the
188// Mode::getTypeName function which will return an empty string, not a null
189// pointer if the name is not found.
190inline const std::string*
Reid Spencer78d033e2007-01-06 07:24:44 +0000191findTypeName(const TypeSymbolTable& ST, const Type* Ty)
Reid Spencer12803f52006-05-31 17:31:38 +0000192{
Reid Spencer78d033e2007-01-06 07:24:44 +0000193 TypeSymbolTable::const_iterator TI = ST.begin();
194 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencer12803f52006-05-31 17:31:38 +0000195 for (;TI != TE; ++TI)
196 if (TI->second == Ty)
197 return &(TI->first);
198 return 0;
199}
200
201void
202CppWriter::error(const std::string& msg) {
203 std::cerr << progname << ": " << msg << "\n";
204 exit(2);
205}
206
Reid Spencer15f7e012006-05-30 21:18:23 +0000207// printCFP - Print a floating point constant .. very carefully :)
208// This makes sure that conversion to/from floating yields the same binary
209// result so that we don't lose precision.
210void
211CppWriter::printCFP(const ConstantFP *CFP) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000212 Out << "ConstantFP::get(";
213 if (CFP->getType() == Type::DoubleTy)
214 Out << "Type::DoubleTy, ";
215 else
216 Out << "Type::FloatTy, ";
Reid Spencer15f7e012006-05-30 21:18:23 +0000217#if HAVE_PRINTF_A
218 char Buffer[100];
219 sprintf(Buffer, "%A", CFP->getValue());
220 if ((!strncmp(Buffer, "0x", 2) ||
221 !strncmp(Buffer, "-0x", 3) ||
222 !strncmp(Buffer, "+0x", 3)) &&
223 (atof(Buffer) == CFP->getValue()))
Reid Spencerf977e7b2006-06-01 23:43:47 +0000224 if (CFP->getType() == Type::DoubleTy)
225 Out << "BitsToDouble(" << Buffer << ")";
226 else
227 Out << "BitsToFloat(" << Buffer << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000228 else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000229#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000230 std::string StrVal = ftostr(CFP->getValue());
231
232 while (StrVal[0] == ' ')
233 StrVal.erase(StrVal.begin());
234
235 // Check to make sure that the stringized number is not some string like
236 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
237 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
238 ((StrVal[0] == '-' || StrVal[0] == '+') &&
239 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
240 (atof(StrVal.c_str()) == CFP->getValue()))
241 if (CFP->getType() == Type::DoubleTy)
242 Out << StrVal;
243 else
244 Out << StrVal;
245 else if (CFP->getType() == Type::DoubleTy)
246 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
247 << std::dec << "ULL) /* " << StrVal << " */";
248 else
249 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
250 << std::dec << "U) /* " << StrVal << " */";
Reid Spencer15f7e012006-05-30 21:18:23 +0000251#if HAVE_PRINTF_A
252 }
253#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000254 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000255}
256
Reid Spencer12803f52006-05-31 17:31:38 +0000257void
258CppWriter::printCallingConv(unsigned cc){
259 // Print the calling convention.
260 switch (cc) {
261 case CallingConv::C: Out << "CallingConv::C"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000262 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
263 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
264 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
265 default: Out << cc; break;
266 }
267}
Reid Spencer15f7e012006-05-30 21:18:23 +0000268
Reid Spencer12803f52006-05-31 17:31:38 +0000269void
270CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
271 switch (LT) {
272 case GlobalValue::InternalLinkage:
273 Out << "GlobalValue::InternalLinkage"; break;
274 case GlobalValue::LinkOnceLinkage:
275 Out << "GlobalValue::LinkOnceLinkage "; break;
276 case GlobalValue::WeakLinkage:
277 Out << "GlobalValue::WeakLinkage"; break;
278 case GlobalValue::AppendingLinkage:
279 Out << "GlobalValue::AppendingLinkage"; break;
280 case GlobalValue::ExternalLinkage:
281 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000282 case GlobalValue::DLLImportLinkage:
Anton Korobeynikov02e21522007-07-11 19:51:06 +0000283 Out << "GlobalValue::DLLImportLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000284 case GlobalValue::DLLExportLinkage:
Anton Korobeynikov02e21522007-07-11 19:51:06 +0000285 Out << "GlobalValue::DLLExportLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000286 case GlobalValue::ExternalWeakLinkage:
287 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000288 case GlobalValue::GhostLinkage:
289 Out << "GlobalValue::GhostLinkage"; break;
290 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000291}
292
Reid Spencere0d133f2006-05-29 18:08:06 +0000293// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000294// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000295void
296CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000297 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
298 unsigned char C = Str[i];
299 if (isprint(C) && C != '"' && C != '\\') {
300 Out << C;
301 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000302 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000303 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
304 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
305 }
306 }
307}
308
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000309std::string
310CppWriter::getCppName(const Type* Ty)
311{
312 // First, handle the primitive types .. easy
Chris Lattner42a75512007-01-15 02:27:26 +0000313 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000314 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000315 case Type::VoidTyID: return "Type::VoidTy";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000316 case Type::IntegerTyID: {
317 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
318 return "IntegerType::get(" + utostr(BitWidth) + ")";
319 }
Reid Spencer71d2ec92006-12-31 06:02:26 +0000320 case Type::FloatTyID: return "Type::FloatTy";
321 case Type::DoubleTyID: return "Type::DoubleTy";
322 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000323 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000324 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000325 break;
326 }
327 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
328 }
329
330 // Now, see if we've seen the type before and return that
331 TypeMap::iterator I = TypeNames.find(Ty);
332 if (I != TypeNames.end())
333 return I->second;
334
335 // Okay, let's build a new name for this type. Start with a prefix
336 const char* prefix = 0;
337 switch (Ty->getTypeID()) {
338 case Type::FunctionTyID: prefix = "FuncTy_"; break;
339 case Type::StructTyID: prefix = "StructTy_"; break;
340 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
341 case Type::PointerTyID: prefix = "PointerTy_"; break;
342 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
Reid Spencerac9dcb92007-02-15 03:39:18 +0000343 case Type::VectorTyID: prefix = "VectorTy_"; break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000344 default: prefix = "OtherTy_"; break; // prevent breakage
345 }
346
347 // See if the type has a name in the symboltable and build accordingly
Reid Spencer78d033e2007-01-06 07:24:44 +0000348 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000349 std::string name;
350 if (tName)
351 name = std::string(prefix) + *tName;
352 else
353 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000354 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000355
356 // Save the name
357 return TypeNames[Ty] = name;
358}
359
Reid Spencer12803f52006-05-31 17:31:38 +0000360void
361CppWriter::printCppName(const Type* Ty)
362{
363 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000364}
365
Reid Spencer12803f52006-05-31 17:31:38 +0000366std::string
367CppWriter::getCppName(const Value* val) {
368 std::string name;
369 ValueMap::iterator I = ValueNames.find(val);
370 if (I != ValueNames.end() && I->first == val)
371 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000372
Reid Spencer12803f52006-05-31 17:31:38 +0000373 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
374 name = std::string("gvar_") +
375 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000376 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000377 name = std::string("func_");
378 } else if (const Constant* C = dyn_cast<Constant>(val)) {
379 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000380 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
381 if (is_inline) {
382 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
383 Function::const_arg_iterator(Arg)) + 1;
384 name = std::string("arg_") + utostr(argNum);
385 NameSet::iterator NI = UsedNames.find(name);
386 if (NI != UsedNames.end())
387 name += std::string("_") + utostr(uniqueNum++);
388 UsedNames.insert(name);
389 return ValueNames[val] = name;
390 } else {
391 name = getTypePrefix(val->getType());
392 }
Reid Spencer12803f52006-05-31 17:31:38 +0000393 } else {
394 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000395 }
Reid Spencer12803f52006-05-31 17:31:38 +0000396 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
397 sanitize(name);
398 NameSet::iterator NI = UsedNames.find(name);
399 if (NI != UsedNames.end())
400 name += std::string("_") + utostr(uniqueNum++);
401 UsedNames.insert(name);
402 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000403}
404
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000405void
Reid Spencer12803f52006-05-31 17:31:38 +0000406CppWriter::printCppName(const Value* val) {
407 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000408}
409
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000410bool
Reid Spencer12803f52006-05-31 17:31:38 +0000411CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000412 // We don't print definitions for primitive types
Chris Lattner42a75512007-01-15 02:27:26 +0000413 if (Ty->isPrimitiveType() || Ty->isInteger())
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000414 return false;
415
Reid Spencer15f7e012006-05-30 21:18:23 +0000416 // If we already defined this type, we don't need to define it again.
417 if (DefinedTypes.find(Ty) != DefinedTypes.end())
418 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000419
Reid Spencer15f7e012006-05-30 21:18:23 +0000420 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000421 std::string typeName(getCppName(Ty));
422
423 // Search the type stack for recursion. If we find it, then generate this
424 // as an OpaqueType, but make sure not to do this multiple times because
425 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000426 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000427 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000428 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
429 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000430 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
431 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000432 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
433 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000434 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000435 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000436 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000437 }
438
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000439 // We're going to print a derived type which, by definition, contains other
440 // types. So, push this one we're printing onto the type stack to assist with
441 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000442 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000443
444 // Print the type definition
445 switch (Ty->getTypeID()) {
446 case Type::FunctionTyID: {
447 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000448 Out << "std::vector<const Type*>" << typeName << "_args;";
449 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000450 FunctionType::param_iterator PI = FT->param_begin();
451 FunctionType::param_iterator PE = FT->param_end();
452 for (; PI != PE; ++PI) {
453 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000454 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000455 std::string argName(getCppName(argTy));
456 Out << typeName << "_args.push_back(" << argName;
457 if (isForward)
458 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000459 Out << ");";
460 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000461 }
Reid Spencer6abd3da2007-04-11 09:54:08 +0000462 const ParamAttrsList *PAL = FT->getParamAttrs();
463 Out << "ParamAttrsList *" << typeName << "_PAL = 0;";
Reid Spencera9297b12007-04-11 10:01:32 +0000464 nl(Out);
Reid Spencer4f859aa2007-04-22 05:46:44 +0000465 if (PAL) {
466 Out << '{'; in(); nl(Out);
467 Out << "ParamAttrsVector Attrs;"; nl(Out);
468 Out << "ParamAttrsWithIndex PAWI;"; nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000469 for (unsigned i = 0; i < PAL->size(); ++i) {
470 uint16_t index = PAL->getParamIndex(i);
471 uint16_t attrs = PAL->getParamAttrs(index);
Reid Spencer4f859aa2007-04-22 05:46:44 +0000472 Out << "PAWI.index = " << index << "; PAWI.attrs = 0 ";
Reid Spencer6abd3da2007-04-11 09:54:08 +0000473 if (attrs & ParamAttr::SExt)
474 Out << " | ParamAttr::SExt";
475 if (attrs & ParamAttr::ZExt)
476 Out << " | ParamAttr::ZExt";
Zhou Shengfebca342007-06-05 05:28:26 +0000477 if (attrs & ParamAttr::NoAlias)
478 Out << " | ParamAttr::NoAlias";
Reid Spencer6abd3da2007-04-11 09:54:08 +0000479 if (attrs & ParamAttr::StructRet)
480 Out << " | ParamAttr::StructRet";
481 if (attrs & ParamAttr::InReg)
482 Out << " | ParamAttr::InReg";
483 if (attrs & ParamAttr::NoReturn)
484 Out << " | ParamAttr::NoReturn";
485 if (attrs & ParamAttr::NoUnwind)
486 Out << " | ParamAttr::NoUnwind";
Reid Spencer4f859aa2007-04-22 05:46:44 +0000487 Out << ";";
488 nl(Out);
489 Out << "Attrs.push_back(PAWI);";
Reid Spencera9297b12007-04-11 10:01:32 +0000490 nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000491 }
Reid Spencer4f859aa2007-04-22 05:46:44 +0000492 Out << typeName << "_PAL = ParamAttrsList::get(Attrs);";
493 nl(Out);
494 out(); nl(Out);
495 Out << '}'; nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000496 }
Reid Spencer12803f52006-05-31 17:31:38 +0000497 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000498 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000499 Out << "FunctionType* " << typeName << " = FunctionType::get(";
500 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000501 if (isForward)
502 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000503 Out << ",";
504 nl(Out) << "/*Params=*/" << typeName << "_args,";
Reid Spencer07441662007-04-11 12:28:56 +0000505 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true," : "false,") ;
Reid Spencera9297b12007-04-11 10:01:32 +0000506 nl(Out) << "/*ParamAttrs=*/" << typeName << "_PAL" << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000507 out();
508 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000509 break;
510 }
511 case Type::StructTyID: {
512 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000513 Out << "std::vector<const Type*>" << typeName << "_fields;";
514 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000515 StructType::element_iterator EI = ST->element_begin();
516 StructType::element_iterator EE = ST->element_end();
517 for (; EI != EE; ++EI) {
518 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000519 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000520 std::string fieldName(getCppName(fieldTy));
521 Out << typeName << "_fields.push_back(" << fieldName;
522 if (isForward)
523 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000524 Out << ");";
525 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000526 }
527 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer46fea102007-04-11 12:41:49 +0000528 << typeName << "_fields, /*isPacked=*/"
529 << (ST->isPacked() ? "true" : "false") << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000530 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000531 break;
532 }
533 case Type::ArrayTyID: {
534 const ArrayType* AT = cast<ArrayType>(Ty);
535 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000536 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000537 std::string elemName(getCppName(ET));
538 Out << "ArrayType* " << typeName << " = ArrayType::get("
539 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000540 << ", " << utostr(AT->getNumElements()) << ");";
541 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000542 break;
543 }
544 case Type::PointerTyID: {
545 const PointerType* PT = cast<PointerType>(Ty);
546 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000547 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000548 std::string elemName(getCppName(ET));
549 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000550 << elemName << (isForward ? "_fwd" : "") << ");";
551 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000552 break;
553 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000554 case Type::VectorTyID: {
555 const VectorType* PT = cast<VectorType>(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000556 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000557 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000558 std::string elemName(getCppName(ET));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000559 Out << "VectorType* " << typeName << " = VectorType::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000560 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000561 << ", " << utostr(PT->getNumElements()) << ");";
562 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000563 break;
564 }
565 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000566 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
567 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000568 break;
569 }
570 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000571 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000572 }
573
Reid Spencer74e032a2006-05-29 02:58:15 +0000574 // If the type had a name, make sure we recreate it.
575 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000576 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer07441662007-04-11 12:28:56 +0000577 if (progTypeName) {
Reid Spencer74e032a2006-05-29 02:58:15 +0000578 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000579 << typeName << ");";
580 nl(Out);
Reid Spencer07441662007-04-11 12:28:56 +0000581 }
Reid Spencer74e032a2006-05-29 02:58:15 +0000582
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000583 // Pop us off the type stack
584 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000585
586 // Indicate that this type is now defined.
587 DefinedTypes.insert(Ty);
588
589 // Early resolve as many unresolved types as possible. Search the unresolved
590 // types map for the type we just printed. Now that its definition is complete
591 // we can resolve any previous references to it. This prevents a cascade of
592 // unresolved types.
593 TypeMap::iterator I = UnresolvedTypes.find(Ty);
594 if (I != UnresolvedTypes.end()) {
595 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000596 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
597 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000598 Out << I->second << " = cast<";
599 switch (Ty->getTypeID()) {
600 case Type::FunctionTyID: Out << "FunctionType"; break;
601 case Type::ArrayTyID: Out << "ArrayType"; break;
602 case Type::StructTyID: Out << "StructType"; break;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000603 case Type::VectorTyID: Out << "VectorType"; break;
Reid Spencer15f7e012006-05-30 21:18:23 +0000604 case Type::PointerTyID: Out << "PointerType"; break;
605 case Type::OpaqueTyID: Out << "OpaqueType"; break;
606 default: Out << "NoSuchDerivedType"; break;
607 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000608 Out << ">(" << I->second << "_fwd.get());";
609 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000610 UnresolvedTypes.erase(I);
611 }
612
613 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000614 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000615
616 // We weren't a recursive type
617 return false;
618}
619
Reid Spencer12803f52006-05-31 17:31:38 +0000620// Prints a type definition. Returns true if it could not resolve all the types
621// in the definition but had to use a forward reference.
622void
623CppWriter::printType(const Type* Ty) {
624 assert(TypeStack.empty());
625 TypeStack.clear();
626 printTypeInternal(Ty);
627 assert(TypeStack.empty());
628}
629
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000630void
631CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000632
633 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000634 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
635 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
636 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000637
638 // For primitive types and types already defined, just add a name
639 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Chris Lattner42a75512007-01-15 02:27:26 +0000640 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000641 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000642 Out << "mod->addTypeName(\"";
643 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000644 Out << "\", " << getCppName(TI->second) << ");";
645 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000646 // For everything else, define the type
647 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000648 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000649 }
650 }
651
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000652 // Add all of the global variables to the value table...
653 for (Module::const_global_iterator I = TheModule->global_begin(),
654 E = TheModule->global_end(); I != E; ++I) {
655 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000656 printType(I->getInitializer()->getType());
657 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000658 }
659
660 // Add all the functions to the table
661 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
662 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000663 printType(FI->getReturnType());
664 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000665 // Add all the function arguments
666 for(Function::const_arg_iterator AI = FI->arg_begin(),
667 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000668 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000669 }
670
671 // Add all of the basic blocks and instructions
672 for (Function::const_iterator BB = FI->begin(),
673 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000674 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000675 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
676 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000677 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000678 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000679 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000680 }
681 }
682 }
683}
684
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000685
Reid Spencere0d133f2006-05-29 18:08:06 +0000686// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000687void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000688 // First, if the constant is actually a GlobalValue (variable or function) or
689 // its already in the constant list then we've printed it already and we can
690 // just return.
691 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000692 return;
693
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000694 std::string constName(getCppName(CV));
695 std::string typeName(getCppName(CV->getType()));
696 if (CV->isNullValue()) {
697 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000698 << typeName << ");";
699 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000700 return;
701 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000702 if (isa<GlobalValue>(CV)) {
703 // Skip variables and functions, we emit them elsewhere
704 return;
705 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000706 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4d26a062007-04-11 13:02:56 +0000707 Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
708 << cast<IntegerType>(CI->getType())->getBitWidth() << ", "
Reid Spencer70297252007-03-01 20:55:43 +0000709 << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000710 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000711 Out << "ConstantAggregateZero* " << constName
712 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000713 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000714 Out << "ConstantPointerNull* " << constName
715 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000716 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000717 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000718 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000719 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000720 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000721 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000722 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencerc38adbb2007-06-25 16:45:54 +0000723 std::string tmp = CA->getAsString();
724 bool nullTerminate = false;
725 if (tmp[tmp.length()-1] == 0) {
726 tmp.erase(tmp.length()-1);
727 nullTerminate = true;
728 }
729 printEscapedString(tmp);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000730 // Determine if we want null termination or not.
Reid Spencerc38adbb2007-06-25 16:45:54 +0000731 if (nullTerminate)
Reid Spencer15f7e012006-05-30 21:18:23 +0000732 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencerc38adbb2007-06-25 16:45:54 +0000733 else
734 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000735 Out << ");";
736 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000737 Out << "std::vector<Constant*> " << constName << "_elems;";
738 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000739 unsigned N = CA->getNumOperands();
740 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000741 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000742 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000743 << getCppName(CA->getOperand(i)) << ");";
744 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000745 }
746 Out << "Constant* " << constName << " = ConstantArray::get("
747 << typeName << ", " << constName << "_elems);";
748 }
749 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000750 Out << "std::vector<Constant*> " << constName << "_fields;";
751 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000752 unsigned N = CS->getNumOperands();
753 for (unsigned i = 0; i < N; i++) {
754 printConstant(CS->getOperand(i));
755 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000756 << getCppName(CS->getOperand(i)) << ");";
757 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000758 }
759 Out << "Constant* " << constName << " = ConstantStruct::get("
760 << typeName << ", " << constName << "_fields);";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000761 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000762 Out << "std::vector<Constant*> " << constName << "_elems;";
763 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000764 unsigned N = CP->getNumOperands();
765 for (unsigned i = 0; i < N; ++i) {
766 printConstant(CP->getOperand(i));
767 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000768 << getCppName(CP->getOperand(i)) << ");";
769 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000770 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000771 Out << "Constant* " << constName << " = ConstantVector::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000772 << typeName << ", " << constName << "_elems);";
773 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000774 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000775 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000776 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000777 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000778 Out << "std::vector<Constant*> " << constName << "_indices;";
779 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000780 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000781 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000782 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000783 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000784 << getCppName(CE->getOperand(i)) << ");";
785 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000786 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000787 Out << "Constant* " << constName
788 << " = ConstantExpr::getGetElementPtr("
789 << getCppName(CE->getOperand(0)) << ", "
David Greene418a04e2007-09-04 17:15:07 +0000790 << "&" << constName << "_indices[0], "
791 << constName << "_indices.size()"
Reid Spencer07441662007-04-11 12:28:56 +0000792 << " );";
Reid Spencer3da59db2006-11-27 01:05:10 +0000793 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000794 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000795 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000796 switch (CE->getOpcode()) {
797 default: assert(0 && "Invalid cast opcode");
798 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
799 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
800 case Instruction::SExt: Out << "Instruction::SExt"; break;
801 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
802 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
803 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
804 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
805 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
806 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
807 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
808 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
809 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
810 }
811 Out << ", " << getCppName(CE->getOperand(0)) << ", "
812 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000813 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000814 unsigned N = CE->getNumOperands();
815 for (unsigned i = 0; i < N; ++i ) {
816 printConstant(CE->getOperand(i));
817 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000818 Out << "Constant* " << constName << " = ConstantExpr::";
819 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000820 case Instruction::Add: Out << "getAdd("; break;
821 case Instruction::Sub: Out << "getSub("; break;
822 case Instruction::Mul: Out << "getMul("; break;
823 case Instruction::UDiv: Out << "getUDiv("; break;
824 case Instruction::SDiv: Out << "getSDiv("; break;
825 case Instruction::FDiv: Out << "getFDiv("; break;
826 case Instruction::URem: Out << "getURem("; break;
827 case Instruction::SRem: Out << "getSRem("; break;
828 case Instruction::FRem: Out << "getFRem("; break;
829 case Instruction::And: Out << "getAnd("; break;
830 case Instruction::Or: Out << "getOr("; break;
831 case Instruction::Xor: Out << "getXor("; break;
832 case Instruction::ICmp:
833 Out << "getICmp(ICmpInst::ICMP_";
834 switch (CE->getPredicate()) {
835 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
836 case ICmpInst::ICMP_NE: Out << "NE"; break;
837 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
838 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
839 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
840 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
841 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
842 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
843 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
844 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
845 default: error("Invalid ICmp Predicate");
846 }
847 break;
848 case Instruction::FCmp:
849 Out << "getFCmp(FCmpInst::FCMP_";
850 switch (CE->getPredicate()) {
851 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
852 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
853 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
854 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
855 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
856 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
857 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
858 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
859 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
860 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
861 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
862 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
863 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
864 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
865 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
866 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
867 default: error("Invalid FCmp Predicate");
868 }
869 break;
870 case Instruction::Shl: Out << "getShl("; break;
871 case Instruction::LShr: Out << "getLShr("; break;
872 case Instruction::AShr: Out << "getAShr("; break;
873 case Instruction::Select: Out << "getSelect("; break;
874 case Instruction::ExtractElement: Out << "getExtractElement("; break;
875 case Instruction::InsertElement: Out << "getInsertElement("; break;
876 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000877 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000878 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000879 break;
880 }
881 Out << getCppName(CE->getOperand(0));
882 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
883 Out << ", " << getCppName(CE->getOperand(i));
884 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000885 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000886 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000887 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000888 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000889 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000890 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000891}
892
Reid Spencer12803f52006-05-31 17:31:38 +0000893void
894CppWriter::printConstants(const Module* M) {
895 // Traverse all the global variables looking for constant initializers
896 for (Module::const_global_iterator I = TheModule->global_begin(),
897 E = TheModule->global_end(); I != E; ++I)
898 if (I->hasInitializer())
899 printConstant(I->getInitializer());
900
901 // Traverse the LLVM functions looking for constants
902 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
903 FI != FE; ++FI) {
904 // Add all of the basic blocks and instructions
905 for (Function::const_iterator BB = FI->begin(),
906 E = FI->end(); BB != E; ++BB) {
907 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
908 ++I) {
909 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
910 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
911 printConstant(C);
912 }
913 }
914 }
915 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000916 }
Reid Spencer66c87342006-05-30 03:43:49 +0000917}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000918
Reid Spencer12803f52006-05-31 17:31:38 +0000919void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000920 nl(Out) << "// Type Definitions";
921 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000922 printType(GV->getType());
923 if (GV->hasInitializer()) {
924 Constant* Init = GV->getInitializer();
925 printType(Init->getType());
926 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000927 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000928 printFunctionHead(F);
929 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000930 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000931 printVariableHead(gv);
932 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000933 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000934 printConstant(gv);
935 }
936 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000937 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000938 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000939 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000940 }
Reid Spencer12803f52006-05-31 17:31:38 +0000941}
Reid Spencer15f7e012006-05-30 21:18:23 +0000942
Reid Spencer12803f52006-05-31 17:31:38 +0000943void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000944 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000945 if (is_inline) {
946 Out << " = mod->getGlobalVariable(";
947 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000948 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
949 nl(Out) << "if (!" << getCppName(GV) << ") {";
950 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000951 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000952 Out << " = new GlobalVariable(";
953 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000954 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000955 Out << ",";
956 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
957 Out << ",";
958 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000959 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000960 Out << ",";
961 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000962 if (GV->hasInitializer()) {
963 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000964 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000965 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000966 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000967 Out << "\",";
968 nl(Out) << "mod);";
969 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000970
971 if (GV->hasSection()) {
972 printCppName(GV);
973 Out << "->setSection(\"";
974 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000975 Out << "\");";
976 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000977 }
978 if (GV->getAlignment()) {
979 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000980 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
981 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000982 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000983 if (is_inline) {
984 out(); Out << "}"; nl(Out);
985 }
Reid Spencer12803f52006-05-31 17:31:38 +0000986}
987
988void
989CppWriter::printVariableBody(const GlobalVariable *GV) {
990 if (GV->hasInitializer()) {
991 printCppName(GV);
992 Out << "->setInitializer(";
993 //if (!isa<GlobalValue(GV->getInitializer()))
994 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000995 Out << getCppName(GV->getInitializer()) << ");";
996 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000997 }
998}
999
1000std::string
1001CppWriter::getOpName(Value* V) {
1002 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
1003 return getCppName(V);
1004
1005 // See if its alread in the map of forward references, if so just return the
1006 // name we already set up for it
1007 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
1008 if (I != ForwardRefs.end())
1009 return I->second;
1010
1011 // This is a new forward reference. Generate a unique name for it
1012 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
1013
1014 // Yes, this is a hack. An Argument is the smallest instantiable value that
1015 // we can make as a placeholder for the real value. We'll replace these
1016 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001017 Out << "Argument* " << result << " = new Argument("
1018 << getCppName(V->getType()) << ");";
1019 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001020 ForwardRefs[V] = result;
1021 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001022}
1023
Reid Spencere0d133f2006-05-29 18:08:06 +00001024// printInstruction - This member is called for each Instruction in a function.
1025void
Reid Spencer12803f52006-05-31 17:31:38 +00001026CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +00001027 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001028
Reid Spencer15f7e012006-05-30 21:18:23 +00001029 // Before we emit this instruction, we need to take care of generating any
1030 // forward references. So, we get the names of all the operands in advance
1031 std::string* opNames = new std::string[I->getNumOperands()];
1032 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1033 opNames[i] = getOpName(I->getOperand(i));
1034 }
1035
Reid Spencere0d133f2006-05-29 18:08:06 +00001036 switch (I->getOpcode()) {
1037 case Instruction::Ret: {
1038 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001039 Out << "new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001040 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001041 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001042 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001043 case Instruction::Br: {
1044 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001045 Out << "new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001046 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001047 Out << opNames[0] << ", "
1048 << opNames[1] << ", "
1049 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001050
Reid Spencere0d133f2006-05-29 18:08:06 +00001051 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001052 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001053 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001054 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001055 }
1056 Out << bbname << ");";
1057 break;
1058 }
Reid Spencer66c87342006-05-30 03:43:49 +00001059 case Instruction::Switch: {
1060 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001061 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001062 << opNames[0] << ", "
1063 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001064 << sw->getNumCases() << ", " << bbname << ");";
1065 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001066 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001067 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001068 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001069 << opNames[i+1] << ");";
1070 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001071 }
1072 break;
1073 }
1074 case Instruction::Invoke: {
1075 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001076 Out << "std::vector<Value*> " << iName << "_params;";
1077 nl(Out);
1078 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1079 Out << iName << "_params.push_back("
1080 << opNames[i] << ");";
1081 nl(Out);
1082 }
Reid Spencer07441662007-04-11 12:28:56 +00001083 Out << "InvokeInst *" << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001084 << opNames[0] << ", "
1085 << opNames[1] << ", "
1086 << opNames[2] << ", "
David Greenef1355a52007-08-27 19:04:21 +00001087 << iName << "_params.begin(), " << iName << "_params.end(), \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001088 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001089 Out << "\", " << bbname << ");";
1090 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001091 printCallingConv(inv->getCallingConv());
1092 Out << ");";
1093 break;
1094 }
1095 case Instruction::Unwind: {
Reid Spencer07441662007-04-11 12:28:56 +00001096 Out << "new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001097 << bbname << ");";
1098 break;
1099 }
1100 case Instruction::Unreachable:{
Reid Spencer07441662007-04-11 12:28:56 +00001101 Out << "new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001102 << bbname << ");";
1103 break;
1104 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001105 case Instruction::Add:
1106 case Instruction::Sub:
1107 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001108 case Instruction::UDiv:
1109 case Instruction::SDiv:
1110 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001111 case Instruction::URem:
1112 case Instruction::SRem:
1113 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001114 case Instruction::And:
1115 case Instruction::Or:
1116 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001117 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001118 case Instruction::LShr:
1119 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001120 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001121 switch (I->getOpcode()) {
1122 case Instruction::Add: Out << "Instruction::Add"; break;
1123 case Instruction::Sub: Out << "Instruction::Sub"; break;
1124 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001125 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1126 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1127 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001128 case Instruction::URem:Out << "Instruction::URem"; break;
1129 case Instruction::SRem:Out << "Instruction::SRem"; break;
1130 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001131 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001132 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001133 case Instruction::Xor: Out << "Instruction::Xor"; break;
1134 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001135 case Instruction::LShr:Out << "Instruction::LShr"; break;
1136 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001137 default: Out << "Instruction::BadOpCode"; break;
1138 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001139 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001140 printEscapedString(I->getName());
1141 Out << "\", " << bbname << ");";
1142 break;
1143 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001144 case Instruction::FCmp: {
1145 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1146 switch (cast<FCmpInst>(I)->getPredicate()) {
1147 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1148 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1149 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1150 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1151 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1152 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1153 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1154 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1155 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1156 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1157 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1158 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1159 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1160 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1161 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1162 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1163 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1164 }
1165 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1166 printEscapedString(I->getName());
1167 Out << "\", " << bbname << ");";
1168 break;
1169 }
1170 case Instruction::ICmp: {
1171 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1172 switch (cast<ICmpInst>(I)->getPredicate()) {
1173 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1174 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1175 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1176 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1177 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1178 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1179 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1180 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1181 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1182 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1183 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001184 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001185 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001186 printEscapedString(I->getName());
1187 Out << "\", " << bbname << ");";
1188 break;
1189 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001190 case Instruction::Malloc: {
1191 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001192 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001193 << getCppName(mallocI->getAllocatedType()) << ", ";
1194 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001195 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001196 Out << "\"";
1197 printEscapedString(mallocI->getName());
1198 Out << "\", " << bbname << ");";
1199 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001200 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001201 << mallocI->getAlignment() << ");";
1202 break;
1203 }
Reid Spencer66c87342006-05-30 03:43:49 +00001204 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001205 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001206 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1207 break;
1208 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001209 case Instruction::Alloca: {
1210 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001211 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001212 << getCppName(allocaI->getAllocatedType()) << ", ";
1213 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001214 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001215 Out << "\"";
1216 printEscapedString(allocaI->getName());
1217 Out << "\", " << bbname << ");";
1218 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001219 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001220 << allocaI->getAlignment() << ");";
1221 break;
1222 }
Reid Spencer66c87342006-05-30 03:43:49 +00001223 case Instruction::Load:{
1224 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001225 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001226 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001227 printEscapedString(load->getName());
1228 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001229 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001230 break;
1231 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001232 case Instruction::Store: {
1233 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001234 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001235 << opNames[0] << ", "
1236 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001237 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001238 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001239 break;
1240 }
1241 case Instruction::GetElementPtr: {
1242 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1243 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001244 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001245 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001246 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001247 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001248 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001249 Out << "std::vector<Value*> " << iName << "_indices;";
1250 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001251 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001252 Out << iName << "_indices.push_back("
1253 << opNames[i] << ");";
1254 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001255 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001256 Out << "Instruction* " << iName << " = new GetElementPtrInst("
David Greeneb8f74792007-09-04 15:46:09 +00001257 << opNames[0] << ", " << iName << "_indices.begin(), "
1258 << iName << "_indices.end()";
Reid Spencere0d133f2006-05-29 18:08:06 +00001259 }
1260 Out << ", \"";
1261 printEscapedString(gep->getName());
1262 Out << "\", " << bbname << ");";
1263 break;
1264 }
Reid Spencer66c87342006-05-30 03:43:49 +00001265 case Instruction::PHI: {
1266 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001267
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001268 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001269 << getCppName(phi->getType()) << ", \"";
1270 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001271 Out << "\", " << bbname << ");";
1272 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001273 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001274 << ");";
1275 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001276 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001277 Out << iName << "->addIncoming("
1278 << opNames[i] << ", " << opNames[i+1] << ");";
1279 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001280 }
Reid Spencer66c87342006-05-30 03:43:49 +00001281 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001282 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001283 case Instruction::Trunc:
1284 case Instruction::ZExt:
1285 case Instruction::SExt:
1286 case Instruction::FPTrunc:
1287 case Instruction::FPExt:
1288 case Instruction::FPToUI:
1289 case Instruction::FPToSI:
1290 case Instruction::UIToFP:
1291 case Instruction::SIToFP:
1292 case Instruction::PtrToInt:
1293 case Instruction::IntToPtr:
1294 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001295 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001296 Out << "CastInst* " << iName << " = new ";
1297 switch (I->getOpcode()) {
Reid Spencer34816572007-02-16 06:34:39 +00001298 case Instruction::Trunc: Out << "TruncInst"; break;
1299 case Instruction::ZExt: Out << "ZExtInst"; break;
1300 case Instruction::SExt: Out << "SExtInst"; break;
1301 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1302 case Instruction::FPExt: Out << "FPExtInst"; break;
1303 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1304 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1305 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1306 case Instruction::SIToFP: Out << "SIToFPInst"; break;
Reid Spencer07441662007-04-11 12:28:56 +00001307 case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
Reid Spencer34816572007-02-16 06:34:39 +00001308 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1309 case Instruction::BitCast: Out << "BitCastInst"; break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001310 default: assert(!"Unreachable"); break;
1311 }
1312 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001313 << getCppName(cst->getType()) << ", \"";
1314 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001315 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001316 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001317 }
Reid Spencer66c87342006-05-30 03:43:49 +00001318 case Instruction::Call:{
1319 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001320 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001321 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001322 << getCppName(ila->getFunctionType()) << ", \""
1323 << ila->getAsmString() << "\", \""
1324 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001325 << (ila->hasSideEffects() ? "true" : "false") << ");";
1326 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001327 }
Reid Spencer22256f42007-08-02 03:30:26 +00001328 if (call->getNumOperands() > 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001329 Out << "std::vector<Value*> " << iName << "_params;";
1330 nl(Out);
1331 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1332 Out << iName << "_params.push_back(" << opNames[i] << ");";
1333 nl(Out);
1334 }
1335 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer22256f42007-08-02 03:30:26 +00001336 << opNames[0] << ", " << iName << "_params.begin(), "
1337 << iName << "_params.end(), \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001338 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001339 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001340 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001341 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001342 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001343 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001344 }
1345 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001346 Out << "\", " << bbname << ");";
1347 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001348 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001349 Out << ");";
1350 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001351 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001352 Out << ");";
1353 break;
1354 }
1355 case Instruction::Select: {
1356 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001357 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001358 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001359 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001360 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001361 break;
1362 }
1363 case Instruction::UserOp1:
1364 /// FALL THROUGH
1365 case Instruction::UserOp2: {
1366 /// FIXME: What should be done here?
1367 break;
1368 }
1369 case Instruction::VAArg: {
1370 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001371 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001372 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001373 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001374 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001375 break;
1376 }
1377 case Instruction::ExtractElement: {
1378 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001379 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001380 << " = new ExtractElementInst(" << opNames[0]
1381 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001382 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001383 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001384 break;
1385 }
1386 case Instruction::InsertElement: {
1387 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001388 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001389 << " = new InsertElementInst(" << opNames[0]
1390 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001391 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001392 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001393 break;
1394 }
1395 case Instruction::ShuffleVector: {
1396 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001397 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001398 << " = new ShuffleVectorInst(" << opNames[0]
1399 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001400 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001401 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001402 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001403 }
1404 }
Reid Spencer68464b72006-06-15 16:09:59 +00001405 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001406 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001407 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001408}
1409
Reid Spencer12803f52006-05-31 17:31:38 +00001410// Print out the types, constants and declarations needed by one function
1411void CppWriter::printFunctionUses(const Function* F) {
1412
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001413 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001414 if (!is_inline) {
1415 // Print the function's return type
1416 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001417
Reid Spencerf977e7b2006-06-01 23:43:47 +00001418 // Print the function's function type
1419 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001420
Reid Spencerf977e7b2006-06-01 23:43:47 +00001421 // Print the types of each of the function's arguments
1422 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1423 AI != AE; ++AI) {
1424 printType(AI->getType());
1425 }
Reid Spencer12803f52006-05-31 17:31:38 +00001426 }
1427
1428 // Print type definitions for every type referenced by an instruction and
1429 // make a note of any global values or constants that are referenced
Reid Spencera4d71f02007-06-16 20:48:27 +00001430 SmallPtrSet<GlobalValue*,64> gvs;
1431 SmallPtrSet<Constant*,64> consts;
Reid Spencer12803f52006-05-31 17:31:38 +00001432 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1433 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1434 I != E; ++I) {
1435 // Print the type of the instruction itself
1436 printType(I->getType());
1437
1438 // Print the type of each of the instruction's operands
1439 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1440 Value* operand = I->getOperand(i);
1441 printType(operand->getType());
Reid Spencerab24b4c2007-06-16 20:33:24 +00001442
1443 // If the operand references a GVal or Constant, make a note of it
1444 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
Reid Spencera4d71f02007-06-16 20:48:27 +00001445 gvs.insert(GV);
Reid Spencerab24b4c2007-06-16 20:33:24 +00001446 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1447 if (GVar->hasInitializer())
Reid Spencera4d71f02007-06-16 20:48:27 +00001448 consts.insert(GVar->getInitializer());
Reid Spencerab24b4c2007-06-16 20:33:24 +00001449 } else if (Constant* C = dyn_cast<Constant>(operand))
Reid Spencera4d71f02007-06-16 20:48:27 +00001450 consts.insert(C);
Reid Spencer12803f52006-05-31 17:31:38 +00001451 }
1452 }
1453 }
1454
1455 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001456 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001457 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001458 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001459 if (Function* Fun = dyn_cast<Function>(*I)) {
1460 if (!is_inline || Fun != F)
1461 printFunctionHead(Fun);
1462 }
Reid Spencer12803f52006-05-31 17:31:38 +00001463 }
1464
1465 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001466 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001467 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001468 I != E; ++I) {
1469 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1470 printVariableHead(F);
1471 }
1472
1473 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001474 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001475 for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(), E = consts.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001476 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001477 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001478 }
1479
1480 // Process the global variables definitions now that all the constants have
1481 // been emitted. These definitions just couple the gvars with their constant
1482 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001483 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001484 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001485 I != E; ++I) {
1486 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1487 printVariableBody(GV);
1488 }
1489}
1490
1491void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001492 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001493 if (is_inline) {
1494 Out << " = mod->getFunction(\"";
1495 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001496 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1497 nl(Out) << "if (!" << getCppName(F) << ") {";
1498 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001499 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001500 Out<< " = new Function(";
1501 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1502 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001503 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001504 Out << ",";
1505 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001506 printEscapedString(F->getName());
Reid Spencer5cbf9852007-01-30 20:08:39 +00001507 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001508 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001509 printCppName(F);
1510 Out << "->setCallingConv(";
1511 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001512 Out << ");";
1513 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001514 if (F->hasSection()) {
1515 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001516 Out << "->setSection(\"" << F->getSection() << "\");";
1517 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001518 }
1519 if (F->getAlignment()) {
1520 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001521 Out << "->setAlignment(" << F->getAlignment() << ");";
1522 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001523 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001524 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001525 Out << "}";
1526 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001527 }
Reid Spencer12803f52006-05-31 17:31:38 +00001528}
1529
1530void CppWriter::printFunctionBody(const Function *F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001531 if (F->isDeclaration())
Reid Spencer12803f52006-05-31 17:31:38 +00001532 return; // external functions have no bodies.
1533
1534 // Clear the DefinedValues and ForwardRefs maps because we can't have
1535 // cross-function forward refs
1536 ForwardRefs.clear();
1537 DefinedValues.clear();
1538
1539 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001540 if (!is_inline) {
1541 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001542 Out << "Function::arg_iterator args = " << getCppName(F)
1543 << "->arg_begin();";
1544 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001545 }
1546 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1547 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001548 Out << "Value* " << getCppName(AI) << " = args++;";
1549 nl(Out);
1550 if (AI->hasName()) {
1551 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1552 nl(Out);
1553 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001554 }
Reid Spencer12803f52006-05-31 17:31:38 +00001555 }
1556
1557 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001558 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001559 for (Function::const_iterator BI = F->begin(), BE = F->end();
1560 BI != BE; ++BI) {
1561 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001562 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001563 if (BI->hasName())
1564 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001565 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1566 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001567 }
1568
1569 // Output all of its basic blocks... for the function
1570 for (Function::const_iterator BI = F->begin(), BE = F->end();
1571 BI != BE; ++BI) {
1572 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001573 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1574 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001575
1576 // Output all of the instructions in the basic block...
1577 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1578 I != E; ++I) {
1579 printInstruction(I,bbname);
1580 }
1581 }
1582
1583 // Loop over the ForwardRefs and resolve them now that all instructions
1584 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001585 if (!ForwardRefs.empty()) {
1586 nl(Out) << "// Resolve Forward References";
1587 nl(Out);
1588 }
1589
Reid Spencer12803f52006-05-31 17:31:38 +00001590 while (!ForwardRefs.empty()) {
1591 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001592 Out << I->second << "->replaceAllUsesWith("
1593 << getCppName(I->first) << "); delete " << I->second << ";";
1594 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001595 ForwardRefs.erase(I);
1596 }
1597}
1598
Reid Spencerf977e7b2006-06-01 23:43:47 +00001599void CppWriter::printInline(const std::string& fname, const std::string& func) {
Reid Spencer688b0492007-02-05 21:19:13 +00001600 const Function* F = TheModule->getFunction(func);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001601 if (!F) {
1602 error(std::string("Function '") + func + "' not found in input module");
1603 return;
1604 }
Reid Spencer5cbf9852007-01-30 20:08:39 +00001605 if (F->isDeclaration()) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001606 error(std::string("Function '") + func + "' is external!");
1607 return;
1608 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001609 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001610 << getCppName(F);
1611 unsigned arg_count = 1;
1612 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1613 AI != AE; ++AI) {
1614 Out << ", Value* arg_" << arg_count;
1615 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001616 Out << ") {";
1617 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001618 is_inline = true;
1619 printFunctionUses(F);
1620 printFunctionBody(F);
1621 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001622 Out << "return " << getCppName(F->begin()) << ";";
1623 nl(Out) << "}";
1624 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001625}
1626
Reid Spencer12803f52006-05-31 17:31:38 +00001627void CppWriter::printModuleBody() {
1628 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001629 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001630 printTypes(TheModule);
1631
1632 // Functions can call each other and global variables can reference them so
1633 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001634 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001635 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1636 I != E; ++I)
1637 printFunctionHead(I);
1638
1639 // Process the global variables declarations. We can't initialze them until
1640 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001641 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001642 for (Module::const_global_iterator I = TheModule->global_begin(),
1643 E = TheModule->global_end(); I != E; ++I) {
1644 printVariableHead(I);
1645 }
1646
1647 // Print out all the constants definitions. Constants don't recurse except
1648 // through GlobalValues. All GlobalValues have been declared at this point
1649 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001650 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001651 printConstants(TheModule);
1652
1653 // Process the global variables definitions now that all the constants have
1654 // been emitted. These definitions just couple the gvars with their constant
1655 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001656 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001657 for (Module::const_global_iterator I = TheModule->global_begin(),
1658 E = TheModule->global_end(); I != E; ++I) {
1659 printVariableBody(I);
1660 }
1661
1662 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001663 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001664 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1665 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001666 if (!I->isDeclaration()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001667 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1668 << ")";
1669 nl(Out) << "{";
1670 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001671 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001672 nl(Out,-1) << "}";
1673 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001674 }
1675 }
1676}
1677
1678void CppWriter::printProgram(
1679 const std::string& fname,
1680 const std::string& mName
1681) {
1682 Out << "#include <llvm/Module.h>\n";
1683 Out << "#include <llvm/DerivedTypes.h>\n";
1684 Out << "#include <llvm/Constants.h>\n";
1685 Out << "#include <llvm/GlobalVariable.h>\n";
1686 Out << "#include <llvm/Function.h>\n";
1687 Out << "#include <llvm/CallingConv.h>\n";
1688 Out << "#include <llvm/BasicBlock.h>\n";
1689 Out << "#include <llvm/Instructions.h>\n";
1690 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencer07441662007-04-11 12:28:56 +00001691 Out << "#include <llvm/ParameterAttributes.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001692 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001693 Out << "#include <llvm/Pass.h>\n";
1694 Out << "#include <llvm/PassManager.h>\n";
1695 Out << "#include <llvm/Analysis/Verifier.h>\n";
1696 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1697 Out << "#include <algorithm>\n";
1698 Out << "#include <iostream>\n\n";
1699 Out << "using namespace llvm;\n\n";
1700 Out << "Module* " << fname << "();\n\n";
1701 Out << "int main(int argc, char**argv) {\n";
Nick Lewycky8946fe12007-06-16 16:17:35 +00001702 Out << " Module* Mod = " << fname << "();\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001703 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1704 Out << " std::cerr.flush();\n";
1705 Out << " std::cout.flush();\n";
1706 Out << " PassManager PM;\n";
Reid Spencer07441662007-04-11 12:28:56 +00001707 Out << " PM.add(new PrintModulePass(&llvm::cout));\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001708 Out << " PM.run(*Mod);\n";
1709 Out << " return 0;\n";
1710 Out << "}\n\n";
1711 printModule(fname,mName);
1712}
1713
1714void CppWriter::printModule(
1715 const std::string& fname,
1716 const std::string& mName
1717) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001718 nl(Out) << "Module* " << fname << "() {";
1719 nl(Out,1) << "// Module Construction";
1720 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001721 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001722 nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
1723 }
1724 if (!TheModule->getTargetTriple().empty()) {
1725 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1726 << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001727 }
Reid Spencer12803f52006-05-31 17:31:38 +00001728
1729 if (!TheModule->getModuleInlineAsm().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001730 nl(Out) << "mod->setModuleInlineAsm(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001731 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001732 Out << "\");";
Reid Spencer12803f52006-05-31 17:31:38 +00001733 }
Reid Spencer07441662007-04-11 12:28:56 +00001734 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001735
1736 // Loop over the dependent libraries and emit them.
1737 Module::lib_iterator LI = TheModule->lib_begin();
1738 Module::lib_iterator LE = TheModule->lib_end();
1739 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001740 Out << "mod->addLibrary(\"" << *LI << "\");";
1741 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001742 ++LI;
1743 }
1744 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001745 nl(Out) << "return mod;";
1746 nl(Out,-1) << "}";
1747 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001748}
1749
1750void CppWriter::printContents(
1751 const std::string& fname, // Name of generated function
1752 const std::string& mName // Name of module generated module
1753) {
1754 Out << "\nModule* " << fname << "(Module *mod) {\n";
1755 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001756 printModuleBody();
1757 Out << "\nreturn mod;\n";
1758 Out << "\n}\n";
1759}
1760
1761void CppWriter::printFunction(
1762 const std::string& fname, // Name of generated function
1763 const std::string& funcName // Name of function to generate
1764) {
Reid Spencer688b0492007-02-05 21:19:13 +00001765 const Function* F = TheModule->getFunction(funcName);
Reid Spencer12803f52006-05-31 17:31:38 +00001766 if (!F) {
1767 error(std::string("Function '") + funcName + "' not found in input module");
1768 return;
1769 }
1770 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1771 printFunctionUses(F);
1772 printFunctionHead(F);
1773 printFunctionBody(F);
1774 Out << "return " << getCppName(F) << ";\n";
1775 Out << "}\n";
1776}
1777
1778void CppWriter::printVariable(
1779 const std::string& fname, /// Name of generated function
1780 const std::string& varName // Name of variable to generate
1781) {
1782 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1783
1784 if (!GV) {
1785 error(std::string("Variable '") + varName + "' not found in input module");
1786 return;
1787 }
1788 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1789 printVariableUses(GV);
1790 printVariableHead(GV);
1791 printVariableBody(GV);
1792 Out << "return " << getCppName(GV) << ";\n";
1793 Out << "}\n";
1794}
1795
1796void CppWriter::printType(
1797 const std::string& fname, /// Name of generated function
1798 const std::string& typeName // Name of type to generate
1799) {
1800 const Type* Ty = TheModule->getTypeByName(typeName);
1801 if (!Ty) {
1802 error(std::string("Type '") + typeName + "' not found in input module");
1803 return;
1804 }
1805 Out << "\nType* " << fname << "(Module *mod) {\n";
1806 printType(Ty);
1807 Out << "return " << getCppName(Ty) << ";\n";
1808 Out << "}\n";
1809}
1810
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001811} // end anonymous llvm
1812
1813namespace llvm {
1814
1815void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001816 // Initialize a CppWriter for us to use
1817 CppWriter W(o, mod);
1818
1819 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001820 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001821
1822 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001823 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001824
1825 // Get the name of the thing we are to generate
1826 std::string tgtname = NameToGenerate.getValue();
1827 if (GenerationType == GenModule ||
1828 GenerationType == GenContents ||
1829 GenerationType == GenProgram) {
1830 if (tgtname == "!bad!") {
1831 if (mod->getModuleIdentifier() == "-")
1832 tgtname = "<stdin>";
1833 else
1834 tgtname = mod->getModuleIdentifier();
1835 }
1836 } else if (tgtname == "!bad!") {
1837 W.error("You must use the -for option with -gen-{function,variable,type}");
1838 }
1839
1840 switch (WhatToGenerate(GenerationType)) {
1841 case GenProgram:
1842 if (fname.empty())
1843 fname = "makeLLVMModule";
1844 W.printProgram(fname,tgtname);
1845 break;
1846 case GenModule:
1847 if (fname.empty())
1848 fname = "makeLLVMModule";
1849 W.printModule(fname,tgtname);
1850 break;
1851 case GenContents:
1852 if (fname.empty())
1853 fname = "makeLLVMModuleContents";
1854 W.printContents(fname,tgtname);
1855 break;
1856 case GenFunction:
1857 if (fname.empty())
1858 fname = "makeLLVMFunction";
1859 W.printFunction(fname,tgtname);
1860 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001861 case GenInline:
1862 if (fname.empty())
1863 fname = "makeLLVMInline";
1864 W.printInline(fname,tgtname);
1865 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001866 case GenVariable:
1867 if (fname.empty())
1868 fname = "makeLLVMVariable";
1869 W.printVariable(fname,tgtname);
1870 break;
1871 case GenType:
1872 if (fname.empty())
1873 fname = "makeLLVMType";
1874 W.printType(fname,tgtname);
1875 break;
1876 default:
1877 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001878 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001879}
1880
1881}