blob: 1551dc3be780b35c1a5e6e0bb555af5686429196 [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) {
Dale Johannesen43421b32007-09-06 18:13:44 +0000212 APFloat APF = APFloat(CFP->getValueAPF()); // copy
213 if (CFP->getType() == Type::FloatTy)
214 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000215 Out << "ConstantFP::get(";
216 if (CFP->getType() == Type::DoubleTy)
217 Out << "Type::DoubleTy, ";
218 else
219 Out << "Type::FloatTy, ";
Dale Johannesen43421b32007-09-06 18:13:44 +0000220 Out << "APFloat(";
Reid Spencer15f7e012006-05-30 21:18:23 +0000221#if HAVE_PRINTF_A
222 char Buffer[100];
Dale Johannesen43421b32007-09-06 18:13:44 +0000223 sprintf(Buffer, "%A", APF.convertToDouble());
Reid Spencer15f7e012006-05-30 21:18:23 +0000224 if ((!strncmp(Buffer, "0x", 2) ||
225 !strncmp(Buffer, "-0x", 3) ||
226 !strncmp(Buffer, "+0x", 3)) &&
Dale Johannesen43421b32007-09-06 18:13:44 +0000227 APF.bitwiseIsEqual(APFloat(atof(Buffer)))) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000228 if (CFP->getType() == Type::DoubleTy)
229 Out << "BitsToDouble(" << Buffer << ")";
230 else
Dale Johannesen43421b32007-09-06 18:13:44 +0000231 Out << "BitsToFloat((float)" << Buffer << ")";
232 Out << ")";
233 } else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000234#endif
Dale Johannesen43421b32007-09-06 18:13:44 +0000235 std::string StrVal = ftostr(CFP->getValueAPF());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000236
237 while (StrVal[0] == ' ')
238 StrVal.erase(StrVal.begin());
239
240 // Check to make sure that the stringized number is not some string like
241 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
242 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
243 ((StrVal[0] == '-' || StrVal[0] == '+') &&
244 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
Dale Johannesen43421b32007-09-06 18:13:44 +0000245 (CFP->isExactlyValue(atof(StrVal.c_str())))) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000246 if (CFP->getType() == Type::DoubleTy)
247 Out << StrVal;
248 else
Dale Johannesen43421b32007-09-06 18:13:44 +0000249 Out << StrVal << "f";
250 }
Reid Spencerf977e7b2006-06-01 23:43:47 +0000251 else if (CFP->getType() == Type::DoubleTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000252 Out << "BitsToDouble(0x" << std::hex
253 << DoubleToBits(CFP->getValueAPF().convertToDouble())
Reid Spencerf977e7b2006-06-01 23:43:47 +0000254 << std::dec << "ULL) /* " << StrVal << " */";
255 else
Dale Johannesen43421b32007-09-06 18:13:44 +0000256 Out << "BitsToFloat(0x" << std::hex
257 << FloatToBits(CFP->getValueAPF().convertToFloat())
Reid Spencerf977e7b2006-06-01 23:43:47 +0000258 << std::dec << "U) /* " << StrVal << " */";
Dale Johannesen43421b32007-09-06 18:13:44 +0000259 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000260#if HAVE_PRINTF_A
261 }
262#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000263 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000264}
265
Reid Spencer12803f52006-05-31 17:31:38 +0000266void
267CppWriter::printCallingConv(unsigned cc){
268 // Print the calling convention.
269 switch (cc) {
270 case CallingConv::C: Out << "CallingConv::C"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000271 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
272 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
273 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
274 default: Out << cc; break;
275 }
276}
Reid Spencer15f7e012006-05-30 21:18:23 +0000277
Reid Spencer12803f52006-05-31 17:31:38 +0000278void
279CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
280 switch (LT) {
281 case GlobalValue::InternalLinkage:
282 Out << "GlobalValue::InternalLinkage"; break;
283 case GlobalValue::LinkOnceLinkage:
284 Out << "GlobalValue::LinkOnceLinkage "; break;
285 case GlobalValue::WeakLinkage:
286 Out << "GlobalValue::WeakLinkage"; break;
287 case GlobalValue::AppendingLinkage:
288 Out << "GlobalValue::AppendingLinkage"; break;
289 case GlobalValue::ExternalLinkage:
290 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000291 case GlobalValue::DLLImportLinkage:
Anton Korobeynikov02e21522007-07-11 19:51:06 +0000292 Out << "GlobalValue::DLLImportLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000293 case GlobalValue::DLLExportLinkage:
Anton Korobeynikov02e21522007-07-11 19:51:06 +0000294 Out << "GlobalValue::DLLExportLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000295 case GlobalValue::ExternalWeakLinkage:
296 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000297 case GlobalValue::GhostLinkage:
298 Out << "GlobalValue::GhostLinkage"; break;
299 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000300}
301
Reid Spencere0d133f2006-05-29 18:08:06 +0000302// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000303// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000304void
305CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000306 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
307 unsigned char C = Str[i];
308 if (isprint(C) && C != '"' && C != '\\') {
309 Out << C;
310 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000311 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000312 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
313 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
314 }
315 }
316}
317
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000318std::string
319CppWriter::getCppName(const Type* Ty)
320{
321 // First, handle the primitive types .. easy
Chris Lattner42a75512007-01-15 02:27:26 +0000322 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000323 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000324 case Type::VoidTyID: return "Type::VoidTy";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000325 case Type::IntegerTyID: {
326 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
327 return "IntegerType::get(" + utostr(BitWidth) + ")";
328 }
Reid Spencer71d2ec92006-12-31 06:02:26 +0000329 case Type::FloatTyID: return "Type::FloatTy";
330 case Type::DoubleTyID: return "Type::DoubleTy";
331 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000332 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000333 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000334 break;
335 }
336 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
337 }
338
339 // Now, see if we've seen the type before and return that
340 TypeMap::iterator I = TypeNames.find(Ty);
341 if (I != TypeNames.end())
342 return I->second;
343
344 // Okay, let's build a new name for this type. Start with a prefix
345 const char* prefix = 0;
346 switch (Ty->getTypeID()) {
347 case Type::FunctionTyID: prefix = "FuncTy_"; break;
348 case Type::StructTyID: prefix = "StructTy_"; break;
349 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
350 case Type::PointerTyID: prefix = "PointerTy_"; break;
351 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
Reid Spencerac9dcb92007-02-15 03:39:18 +0000352 case Type::VectorTyID: prefix = "VectorTy_"; break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000353 default: prefix = "OtherTy_"; break; // prevent breakage
354 }
355
356 // See if the type has a name in the symboltable and build accordingly
Reid Spencer78d033e2007-01-06 07:24:44 +0000357 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000358 std::string name;
359 if (tName)
360 name = std::string(prefix) + *tName;
361 else
362 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000363 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000364
365 // Save the name
366 return TypeNames[Ty] = name;
367}
368
Reid Spencer12803f52006-05-31 17:31:38 +0000369void
370CppWriter::printCppName(const Type* Ty)
371{
372 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000373}
374
Reid Spencer12803f52006-05-31 17:31:38 +0000375std::string
376CppWriter::getCppName(const Value* val) {
377 std::string name;
378 ValueMap::iterator I = ValueNames.find(val);
379 if (I != ValueNames.end() && I->first == val)
380 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000381
Reid Spencer12803f52006-05-31 17:31:38 +0000382 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
383 name = std::string("gvar_") +
384 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000385 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000386 name = std::string("func_");
387 } else if (const Constant* C = dyn_cast<Constant>(val)) {
388 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000389 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
390 if (is_inline) {
391 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
392 Function::const_arg_iterator(Arg)) + 1;
393 name = std::string("arg_") + utostr(argNum);
394 NameSet::iterator NI = UsedNames.find(name);
395 if (NI != UsedNames.end())
396 name += std::string("_") + utostr(uniqueNum++);
397 UsedNames.insert(name);
398 return ValueNames[val] = name;
399 } else {
400 name = getTypePrefix(val->getType());
401 }
Reid Spencer12803f52006-05-31 17:31:38 +0000402 } else {
403 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000404 }
Reid Spencer12803f52006-05-31 17:31:38 +0000405 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
406 sanitize(name);
407 NameSet::iterator NI = UsedNames.find(name);
408 if (NI != UsedNames.end())
409 name += std::string("_") + utostr(uniqueNum++);
410 UsedNames.insert(name);
411 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000412}
413
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000414void
Reid Spencer12803f52006-05-31 17:31:38 +0000415CppWriter::printCppName(const Value* val) {
416 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000417}
418
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000419bool
Reid Spencer12803f52006-05-31 17:31:38 +0000420CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000421 // We don't print definitions for primitive types
Chris Lattner42a75512007-01-15 02:27:26 +0000422 if (Ty->isPrimitiveType() || Ty->isInteger())
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000423 return false;
424
Reid Spencer15f7e012006-05-30 21:18:23 +0000425 // If we already defined this type, we don't need to define it again.
426 if (DefinedTypes.find(Ty) != DefinedTypes.end())
427 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000428
Reid Spencer15f7e012006-05-30 21:18:23 +0000429 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000430 std::string typeName(getCppName(Ty));
431
432 // Search the type stack for recursion. If we find it, then generate this
433 // as an OpaqueType, but make sure not to do this multiple times because
434 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000435 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000436 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000437 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
438 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000439 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
440 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000441 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
442 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000443 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000444 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000445 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000446 }
447
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000448 // We're going to print a derived type which, by definition, contains other
449 // types. So, push this one we're printing onto the type stack to assist with
450 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000451 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000452
453 // Print the type definition
454 switch (Ty->getTypeID()) {
455 case Type::FunctionTyID: {
456 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000457 Out << "std::vector<const Type*>" << typeName << "_args;";
458 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000459 FunctionType::param_iterator PI = FT->param_begin();
460 FunctionType::param_iterator PE = FT->param_end();
461 for (; PI != PE; ++PI) {
462 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000463 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000464 std::string argName(getCppName(argTy));
465 Out << typeName << "_args.push_back(" << argName;
466 if (isForward)
467 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000468 Out << ");";
469 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000470 }
Reid Spencer6abd3da2007-04-11 09:54:08 +0000471 const ParamAttrsList *PAL = FT->getParamAttrs();
472 Out << "ParamAttrsList *" << typeName << "_PAL = 0;";
Reid Spencera9297b12007-04-11 10:01:32 +0000473 nl(Out);
Reid Spencer4f859aa2007-04-22 05:46:44 +0000474 if (PAL) {
475 Out << '{'; in(); nl(Out);
476 Out << "ParamAttrsVector Attrs;"; nl(Out);
477 Out << "ParamAttrsWithIndex PAWI;"; nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000478 for (unsigned i = 0; i < PAL->size(); ++i) {
479 uint16_t index = PAL->getParamIndex(i);
480 uint16_t attrs = PAL->getParamAttrs(index);
Reid Spencer4f859aa2007-04-22 05:46:44 +0000481 Out << "PAWI.index = " << index << "; PAWI.attrs = 0 ";
Reid Spencer6abd3da2007-04-11 09:54:08 +0000482 if (attrs & ParamAttr::SExt)
483 Out << " | ParamAttr::SExt";
484 if (attrs & ParamAttr::ZExt)
485 Out << " | ParamAttr::ZExt";
Zhou Shengfebca342007-06-05 05:28:26 +0000486 if (attrs & ParamAttr::NoAlias)
487 Out << " | ParamAttr::NoAlias";
Reid Spencer6abd3da2007-04-11 09:54:08 +0000488 if (attrs & ParamAttr::StructRet)
489 Out << " | ParamAttr::StructRet";
490 if (attrs & ParamAttr::InReg)
491 Out << " | ParamAttr::InReg";
492 if (attrs & ParamAttr::NoReturn)
493 Out << " | ParamAttr::NoReturn";
494 if (attrs & ParamAttr::NoUnwind)
495 Out << " | ParamAttr::NoUnwind";
Reid Spencer4f859aa2007-04-22 05:46:44 +0000496 Out << ";";
497 nl(Out);
498 Out << "Attrs.push_back(PAWI);";
Reid Spencera9297b12007-04-11 10:01:32 +0000499 nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000500 }
Reid Spencer4f859aa2007-04-22 05:46:44 +0000501 Out << typeName << "_PAL = ParamAttrsList::get(Attrs);";
502 nl(Out);
503 out(); nl(Out);
504 Out << '}'; nl(Out);
Reid Spencer6abd3da2007-04-11 09:54:08 +0000505 }
Reid Spencer12803f52006-05-31 17:31:38 +0000506 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000507 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000508 Out << "FunctionType* " << typeName << " = FunctionType::get(";
509 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000510 if (isForward)
511 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000512 Out << ",";
513 nl(Out) << "/*Params=*/" << typeName << "_args,";
Reid Spencer07441662007-04-11 12:28:56 +0000514 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true," : "false,") ;
Reid Spencera9297b12007-04-11 10:01:32 +0000515 nl(Out) << "/*ParamAttrs=*/" << typeName << "_PAL" << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000516 out();
517 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000518 break;
519 }
520 case Type::StructTyID: {
521 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000522 Out << "std::vector<const Type*>" << typeName << "_fields;";
523 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000524 StructType::element_iterator EI = ST->element_begin();
525 StructType::element_iterator EE = ST->element_end();
526 for (; EI != EE; ++EI) {
527 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000528 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000529 std::string fieldName(getCppName(fieldTy));
530 Out << typeName << "_fields.push_back(" << fieldName;
531 if (isForward)
532 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000533 Out << ");";
534 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000535 }
536 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer46fea102007-04-11 12:41:49 +0000537 << typeName << "_fields, /*isPacked=*/"
538 << (ST->isPacked() ? "true" : "false") << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000539 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000540 break;
541 }
542 case Type::ArrayTyID: {
543 const ArrayType* AT = cast<ArrayType>(Ty);
544 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000545 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000546 std::string elemName(getCppName(ET));
547 Out << "ArrayType* " << typeName << " = ArrayType::get("
548 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000549 << ", " << utostr(AT->getNumElements()) << ");";
550 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000551 break;
552 }
553 case Type::PointerTyID: {
554 const PointerType* PT = cast<PointerType>(Ty);
555 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000556 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000557 std::string elemName(getCppName(ET));
558 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000559 << elemName << (isForward ? "_fwd" : "") << ");";
560 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000561 break;
562 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000563 case Type::VectorTyID: {
564 const VectorType* PT = cast<VectorType>(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000565 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000566 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000567 std::string elemName(getCppName(ET));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000568 Out << "VectorType* " << typeName << " = VectorType::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000569 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000570 << ", " << utostr(PT->getNumElements()) << ");";
571 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000572 break;
573 }
574 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000575 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
576 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000577 break;
578 }
579 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000580 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000581 }
582
Reid Spencer74e032a2006-05-29 02:58:15 +0000583 // If the type had a name, make sure we recreate it.
584 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000585 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer07441662007-04-11 12:28:56 +0000586 if (progTypeName) {
Reid Spencer74e032a2006-05-29 02:58:15 +0000587 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000588 << typeName << ");";
589 nl(Out);
Reid Spencer07441662007-04-11 12:28:56 +0000590 }
Reid Spencer74e032a2006-05-29 02:58:15 +0000591
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000592 // Pop us off the type stack
593 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000594
595 // Indicate that this type is now defined.
596 DefinedTypes.insert(Ty);
597
598 // Early resolve as many unresolved types as possible. Search the unresolved
599 // types map for the type we just printed. Now that its definition is complete
600 // we can resolve any previous references to it. This prevents a cascade of
601 // unresolved types.
602 TypeMap::iterator I = UnresolvedTypes.find(Ty);
603 if (I != UnresolvedTypes.end()) {
604 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000605 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
606 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000607 Out << I->second << " = cast<";
608 switch (Ty->getTypeID()) {
609 case Type::FunctionTyID: Out << "FunctionType"; break;
610 case Type::ArrayTyID: Out << "ArrayType"; break;
611 case Type::StructTyID: Out << "StructType"; break;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000612 case Type::VectorTyID: Out << "VectorType"; break;
Reid Spencer15f7e012006-05-30 21:18:23 +0000613 case Type::PointerTyID: Out << "PointerType"; break;
614 case Type::OpaqueTyID: Out << "OpaqueType"; break;
615 default: Out << "NoSuchDerivedType"; break;
616 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000617 Out << ">(" << I->second << "_fwd.get());";
618 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000619 UnresolvedTypes.erase(I);
620 }
621
622 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000623 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000624
625 // We weren't a recursive type
626 return false;
627}
628
Reid Spencer12803f52006-05-31 17:31:38 +0000629// Prints a type definition. Returns true if it could not resolve all the types
630// in the definition but had to use a forward reference.
631void
632CppWriter::printType(const Type* Ty) {
633 assert(TypeStack.empty());
634 TypeStack.clear();
635 printTypeInternal(Ty);
636 assert(TypeStack.empty());
637}
638
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000639void
640CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000641
642 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000643 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
644 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
645 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000646
647 // For primitive types and types already defined, just add a name
648 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Chris Lattner42a75512007-01-15 02:27:26 +0000649 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000650 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000651 Out << "mod->addTypeName(\"";
652 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000653 Out << "\", " << getCppName(TI->second) << ");";
654 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000655 // For everything else, define the type
656 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000657 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000658 }
659 }
660
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000661 // Add all of the global variables to the value table...
662 for (Module::const_global_iterator I = TheModule->global_begin(),
663 E = TheModule->global_end(); I != E; ++I) {
664 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000665 printType(I->getInitializer()->getType());
666 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000667 }
668
669 // Add all the functions to the table
670 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
671 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000672 printType(FI->getReturnType());
673 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000674 // Add all the function arguments
675 for(Function::const_arg_iterator AI = FI->arg_begin(),
676 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000677 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000678 }
679
680 // Add all of the basic blocks and instructions
681 for (Function::const_iterator BB = FI->begin(),
682 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000683 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000684 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
685 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000686 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000687 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000688 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000689 }
690 }
691 }
692}
693
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000694
Reid Spencere0d133f2006-05-29 18:08:06 +0000695// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000696void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000697 // First, if the constant is actually a GlobalValue (variable or function) or
698 // its already in the constant list then we've printed it already and we can
699 // just return.
700 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000701 return;
702
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000703 std::string constName(getCppName(CV));
704 std::string typeName(getCppName(CV->getType()));
705 if (CV->isNullValue()) {
706 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000707 << typeName << ");";
708 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000709 return;
710 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000711 if (isa<GlobalValue>(CV)) {
712 // Skip variables and functions, we emit them elsewhere
713 return;
714 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000715 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4d26a062007-04-11 13:02:56 +0000716 Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
717 << cast<IntegerType>(CI->getType())->getBitWidth() << ", "
Reid Spencer70297252007-03-01 20:55:43 +0000718 << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000719 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000720 Out << "ConstantAggregateZero* " << constName
721 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000722 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000723 Out << "ConstantPointerNull* " << constName
724 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000725 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000726 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000727 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000728 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000729 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000730 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000731 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencerc38adbb2007-06-25 16:45:54 +0000732 std::string tmp = CA->getAsString();
733 bool nullTerminate = false;
734 if (tmp[tmp.length()-1] == 0) {
735 tmp.erase(tmp.length()-1);
736 nullTerminate = true;
737 }
738 printEscapedString(tmp);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000739 // Determine if we want null termination or not.
Reid Spencerc38adbb2007-06-25 16:45:54 +0000740 if (nullTerminate)
Reid Spencer15f7e012006-05-30 21:18:23 +0000741 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencerc38adbb2007-06-25 16:45:54 +0000742 else
743 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000744 Out << ");";
745 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000746 Out << "std::vector<Constant*> " << constName << "_elems;";
747 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000748 unsigned N = CA->getNumOperands();
749 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000750 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000751 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000752 << getCppName(CA->getOperand(i)) << ");";
753 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000754 }
755 Out << "Constant* " << constName << " = ConstantArray::get("
756 << typeName << ", " << constName << "_elems);";
757 }
758 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000759 Out << "std::vector<Constant*> " << constName << "_fields;";
760 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000761 unsigned N = CS->getNumOperands();
762 for (unsigned i = 0; i < N; i++) {
763 printConstant(CS->getOperand(i));
764 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000765 << getCppName(CS->getOperand(i)) << ");";
766 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000767 }
768 Out << "Constant* " << constName << " = ConstantStruct::get("
769 << typeName << ", " << constName << "_fields);";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000770 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000771 Out << "std::vector<Constant*> " << constName << "_elems;";
772 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000773 unsigned N = CP->getNumOperands();
774 for (unsigned i = 0; i < N; ++i) {
775 printConstant(CP->getOperand(i));
776 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000777 << getCppName(CP->getOperand(i)) << ");";
778 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000779 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000780 Out << "Constant* " << constName << " = ConstantVector::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000781 << typeName << ", " << constName << "_elems);";
782 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000783 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000784 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000785 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000786 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000787 Out << "std::vector<Constant*> " << constName << "_indices;";
788 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000789 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000790 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000791 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000792 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000793 << getCppName(CE->getOperand(i)) << ");";
794 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000795 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000796 Out << "Constant* " << constName
797 << " = ConstantExpr::getGetElementPtr("
798 << getCppName(CE->getOperand(0)) << ", "
David Greene418a04e2007-09-04 17:15:07 +0000799 << "&" << constName << "_indices[0], "
800 << constName << "_indices.size()"
Reid Spencer07441662007-04-11 12:28:56 +0000801 << " );";
Reid Spencer3da59db2006-11-27 01:05:10 +0000802 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000803 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000804 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000805 switch (CE->getOpcode()) {
806 default: assert(0 && "Invalid cast opcode");
807 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
808 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
809 case Instruction::SExt: Out << "Instruction::SExt"; break;
810 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
811 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
812 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
813 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
814 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
815 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
816 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
817 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
818 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
819 }
820 Out << ", " << getCppName(CE->getOperand(0)) << ", "
821 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000822 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000823 unsigned N = CE->getNumOperands();
824 for (unsigned i = 0; i < N; ++i ) {
825 printConstant(CE->getOperand(i));
826 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000827 Out << "Constant* " << constName << " = ConstantExpr::";
828 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000829 case Instruction::Add: Out << "getAdd("; break;
830 case Instruction::Sub: Out << "getSub("; break;
831 case Instruction::Mul: Out << "getMul("; break;
832 case Instruction::UDiv: Out << "getUDiv("; break;
833 case Instruction::SDiv: Out << "getSDiv("; break;
834 case Instruction::FDiv: Out << "getFDiv("; break;
835 case Instruction::URem: Out << "getURem("; break;
836 case Instruction::SRem: Out << "getSRem("; break;
837 case Instruction::FRem: Out << "getFRem("; break;
838 case Instruction::And: Out << "getAnd("; break;
839 case Instruction::Or: Out << "getOr("; break;
840 case Instruction::Xor: Out << "getXor("; break;
841 case Instruction::ICmp:
842 Out << "getICmp(ICmpInst::ICMP_";
843 switch (CE->getPredicate()) {
844 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
845 case ICmpInst::ICMP_NE: Out << "NE"; break;
846 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
847 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
848 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
849 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
850 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
851 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
852 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
853 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
854 default: error("Invalid ICmp Predicate");
855 }
856 break;
857 case Instruction::FCmp:
858 Out << "getFCmp(FCmpInst::FCMP_";
859 switch (CE->getPredicate()) {
860 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
861 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
862 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
863 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
864 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
865 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
866 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
867 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
868 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
869 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
870 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
871 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
872 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
873 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
874 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
875 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
876 default: error("Invalid FCmp Predicate");
877 }
878 break;
879 case Instruction::Shl: Out << "getShl("; break;
880 case Instruction::LShr: Out << "getLShr("; break;
881 case Instruction::AShr: Out << "getAShr("; break;
882 case Instruction::Select: Out << "getSelect("; break;
883 case Instruction::ExtractElement: Out << "getExtractElement("; break;
884 case Instruction::InsertElement: Out << "getInsertElement("; break;
885 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000886 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000887 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000888 break;
889 }
890 Out << getCppName(CE->getOperand(0));
891 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
892 Out << ", " << getCppName(CE->getOperand(i));
893 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000894 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000895 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000896 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000897 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000898 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000899 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000900}
901
Reid Spencer12803f52006-05-31 17:31:38 +0000902void
903CppWriter::printConstants(const Module* M) {
904 // Traverse all the global variables looking for constant initializers
905 for (Module::const_global_iterator I = TheModule->global_begin(),
906 E = TheModule->global_end(); I != E; ++I)
907 if (I->hasInitializer())
908 printConstant(I->getInitializer());
909
910 // Traverse the LLVM functions looking for constants
911 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
912 FI != FE; ++FI) {
913 // Add all of the basic blocks and instructions
914 for (Function::const_iterator BB = FI->begin(),
915 E = FI->end(); BB != E; ++BB) {
916 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
917 ++I) {
918 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
919 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
920 printConstant(C);
921 }
922 }
923 }
924 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000925 }
Reid Spencer66c87342006-05-30 03:43:49 +0000926}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000927
Reid Spencer12803f52006-05-31 17:31:38 +0000928void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000929 nl(Out) << "// Type Definitions";
930 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000931 printType(GV->getType());
932 if (GV->hasInitializer()) {
933 Constant* Init = GV->getInitializer();
934 printType(Init->getType());
935 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000936 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000937 printFunctionHead(F);
938 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000939 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000940 printVariableHead(gv);
941 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000942 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000943 printConstant(gv);
944 }
945 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000946 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000947 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000948 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000949 }
Reid Spencer12803f52006-05-31 17:31:38 +0000950}
Reid Spencer15f7e012006-05-30 21:18:23 +0000951
Reid Spencer12803f52006-05-31 17:31:38 +0000952void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000953 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000954 if (is_inline) {
955 Out << " = mod->getGlobalVariable(";
956 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000957 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
958 nl(Out) << "if (!" << getCppName(GV) << ") {";
959 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000960 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000961 Out << " = new GlobalVariable(";
962 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000963 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000964 Out << ",";
965 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
966 Out << ",";
967 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000968 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000969 Out << ",";
970 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000971 if (GV->hasInitializer()) {
972 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000973 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000974 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000975 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000976 Out << "\",";
977 nl(Out) << "mod);";
978 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000979
980 if (GV->hasSection()) {
981 printCppName(GV);
982 Out << "->setSection(\"";
983 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000984 Out << "\");";
985 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000986 }
987 if (GV->getAlignment()) {
988 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000989 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
990 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000991 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000992 if (is_inline) {
993 out(); Out << "}"; nl(Out);
994 }
Reid Spencer12803f52006-05-31 17:31:38 +0000995}
996
997void
998CppWriter::printVariableBody(const GlobalVariable *GV) {
999 if (GV->hasInitializer()) {
1000 printCppName(GV);
1001 Out << "->setInitializer(";
1002 //if (!isa<GlobalValue(GV->getInitializer()))
1003 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001004 Out << getCppName(GV->getInitializer()) << ");";
1005 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001006 }
1007}
1008
1009std::string
1010CppWriter::getOpName(Value* V) {
1011 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
1012 return getCppName(V);
1013
1014 // See if its alread in the map of forward references, if so just return the
1015 // name we already set up for it
1016 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
1017 if (I != ForwardRefs.end())
1018 return I->second;
1019
1020 // This is a new forward reference. Generate a unique name for it
1021 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
1022
1023 // Yes, this is a hack. An Argument is the smallest instantiable value that
1024 // we can make as a placeholder for the real value. We'll replace these
1025 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001026 Out << "Argument* " << result << " = new Argument("
1027 << getCppName(V->getType()) << ");";
1028 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001029 ForwardRefs[V] = result;
1030 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001031}
1032
Reid Spencere0d133f2006-05-29 18:08:06 +00001033// printInstruction - This member is called for each Instruction in a function.
1034void
Reid Spencer12803f52006-05-31 17:31:38 +00001035CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +00001036 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001037
Reid Spencer15f7e012006-05-30 21:18:23 +00001038 // Before we emit this instruction, we need to take care of generating any
1039 // forward references. So, we get the names of all the operands in advance
1040 std::string* opNames = new std::string[I->getNumOperands()];
1041 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1042 opNames[i] = getOpName(I->getOperand(i));
1043 }
1044
Reid Spencere0d133f2006-05-29 18:08:06 +00001045 switch (I->getOpcode()) {
1046 case Instruction::Ret: {
1047 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001048 Out << "new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001049 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001050 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001051 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001052 case Instruction::Br: {
1053 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001054 Out << "new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001055 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001056 Out << opNames[0] << ", "
1057 << opNames[1] << ", "
1058 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001059
Reid Spencere0d133f2006-05-29 18:08:06 +00001060 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001061 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001062 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001063 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001064 }
1065 Out << bbname << ");";
1066 break;
1067 }
Reid Spencer66c87342006-05-30 03:43:49 +00001068 case Instruction::Switch: {
1069 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001070 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001071 << opNames[0] << ", "
1072 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001073 << sw->getNumCases() << ", " << bbname << ");";
1074 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001075 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001076 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001077 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001078 << opNames[i+1] << ");";
1079 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001080 }
1081 break;
1082 }
1083 case Instruction::Invoke: {
1084 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001085 Out << "std::vector<Value*> " << iName << "_params;";
1086 nl(Out);
1087 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1088 Out << iName << "_params.push_back("
1089 << opNames[i] << ");";
1090 nl(Out);
1091 }
Reid Spencer07441662007-04-11 12:28:56 +00001092 Out << "InvokeInst *" << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001093 << opNames[0] << ", "
1094 << opNames[1] << ", "
1095 << opNames[2] << ", "
David Greenef1355a52007-08-27 19:04:21 +00001096 << iName << "_params.begin(), " << iName << "_params.end(), \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001097 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001098 Out << "\", " << bbname << ");";
1099 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001100 printCallingConv(inv->getCallingConv());
1101 Out << ");";
1102 break;
1103 }
1104 case Instruction::Unwind: {
Reid Spencer07441662007-04-11 12:28:56 +00001105 Out << "new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001106 << bbname << ");";
1107 break;
1108 }
1109 case Instruction::Unreachable:{
Reid Spencer07441662007-04-11 12:28:56 +00001110 Out << "new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001111 << bbname << ");";
1112 break;
1113 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001114 case Instruction::Add:
1115 case Instruction::Sub:
1116 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001117 case Instruction::UDiv:
1118 case Instruction::SDiv:
1119 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001120 case Instruction::URem:
1121 case Instruction::SRem:
1122 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001123 case Instruction::And:
1124 case Instruction::Or:
1125 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001126 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001127 case Instruction::LShr:
1128 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001129 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001130 switch (I->getOpcode()) {
1131 case Instruction::Add: Out << "Instruction::Add"; break;
1132 case Instruction::Sub: Out << "Instruction::Sub"; break;
1133 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001134 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1135 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1136 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001137 case Instruction::URem:Out << "Instruction::URem"; break;
1138 case Instruction::SRem:Out << "Instruction::SRem"; break;
1139 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001140 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001141 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001142 case Instruction::Xor: Out << "Instruction::Xor"; break;
1143 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001144 case Instruction::LShr:Out << "Instruction::LShr"; break;
1145 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001146 default: Out << "Instruction::BadOpCode"; break;
1147 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001148 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001149 printEscapedString(I->getName());
1150 Out << "\", " << bbname << ");";
1151 break;
1152 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001153 case Instruction::FCmp: {
1154 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1155 switch (cast<FCmpInst>(I)->getPredicate()) {
1156 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1157 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1158 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1159 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1160 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1161 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1162 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1163 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1164 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1165 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1166 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1167 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1168 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1169 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1170 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1171 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1172 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1173 }
1174 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1175 printEscapedString(I->getName());
1176 Out << "\", " << bbname << ");";
1177 break;
1178 }
1179 case Instruction::ICmp: {
1180 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1181 switch (cast<ICmpInst>(I)->getPredicate()) {
1182 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1183 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1184 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1185 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1186 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1187 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1188 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1189 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1190 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1191 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1192 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001193 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001194 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001195 printEscapedString(I->getName());
1196 Out << "\", " << bbname << ");";
1197 break;
1198 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001199 case Instruction::Malloc: {
1200 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001201 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001202 << getCppName(mallocI->getAllocatedType()) << ", ";
1203 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001204 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001205 Out << "\"";
1206 printEscapedString(mallocI->getName());
1207 Out << "\", " << bbname << ");";
1208 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001209 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001210 << mallocI->getAlignment() << ");";
1211 break;
1212 }
Reid Spencer66c87342006-05-30 03:43:49 +00001213 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001214 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001215 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1216 break;
1217 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001218 case Instruction::Alloca: {
1219 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001220 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001221 << getCppName(allocaI->getAllocatedType()) << ", ";
1222 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001223 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001224 Out << "\"";
1225 printEscapedString(allocaI->getName());
1226 Out << "\", " << bbname << ");";
1227 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001228 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001229 << allocaI->getAlignment() << ");";
1230 break;
1231 }
Reid Spencer66c87342006-05-30 03:43:49 +00001232 case Instruction::Load:{
1233 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001234 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001235 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001236 printEscapedString(load->getName());
1237 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001238 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001239 break;
1240 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001241 case Instruction::Store: {
1242 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001243 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001244 << opNames[0] << ", "
1245 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001246 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001247 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001248 break;
1249 }
1250 case Instruction::GetElementPtr: {
1251 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1252 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001253 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001254 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001255 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001256 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001257 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001258 Out << "std::vector<Value*> " << iName << "_indices;";
1259 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001260 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001261 Out << iName << "_indices.push_back("
1262 << opNames[i] << ");";
1263 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001264 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001265 Out << "Instruction* " << iName << " = new GetElementPtrInst("
David Greeneb8f74792007-09-04 15:46:09 +00001266 << opNames[0] << ", " << iName << "_indices.begin(), "
1267 << iName << "_indices.end()";
Reid Spencere0d133f2006-05-29 18:08:06 +00001268 }
1269 Out << ", \"";
1270 printEscapedString(gep->getName());
1271 Out << "\", " << bbname << ");";
1272 break;
1273 }
Reid Spencer66c87342006-05-30 03:43:49 +00001274 case Instruction::PHI: {
1275 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001276
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001277 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001278 << getCppName(phi->getType()) << ", \"";
1279 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001280 Out << "\", " << bbname << ");";
1281 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001282 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001283 << ");";
1284 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001285 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001286 Out << iName << "->addIncoming("
1287 << opNames[i] << ", " << opNames[i+1] << ");";
1288 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001289 }
Reid Spencer66c87342006-05-30 03:43:49 +00001290 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001291 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001292 case Instruction::Trunc:
1293 case Instruction::ZExt:
1294 case Instruction::SExt:
1295 case Instruction::FPTrunc:
1296 case Instruction::FPExt:
1297 case Instruction::FPToUI:
1298 case Instruction::FPToSI:
1299 case Instruction::UIToFP:
1300 case Instruction::SIToFP:
1301 case Instruction::PtrToInt:
1302 case Instruction::IntToPtr:
1303 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001304 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001305 Out << "CastInst* " << iName << " = new ";
1306 switch (I->getOpcode()) {
Reid Spencer34816572007-02-16 06:34:39 +00001307 case Instruction::Trunc: Out << "TruncInst"; break;
1308 case Instruction::ZExt: Out << "ZExtInst"; break;
1309 case Instruction::SExt: Out << "SExtInst"; break;
1310 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1311 case Instruction::FPExt: Out << "FPExtInst"; break;
1312 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1313 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1314 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1315 case Instruction::SIToFP: Out << "SIToFPInst"; break;
Reid Spencer07441662007-04-11 12:28:56 +00001316 case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
Reid Spencer34816572007-02-16 06:34:39 +00001317 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1318 case Instruction::BitCast: Out << "BitCastInst"; break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001319 default: assert(!"Unreachable"); break;
1320 }
1321 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001322 << getCppName(cst->getType()) << ", \"";
1323 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001324 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001325 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001326 }
Reid Spencer66c87342006-05-30 03:43:49 +00001327 case Instruction::Call:{
1328 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001329 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001330 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001331 << getCppName(ila->getFunctionType()) << ", \""
1332 << ila->getAsmString() << "\", \""
1333 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001334 << (ila->hasSideEffects() ? "true" : "false") << ");";
1335 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001336 }
Reid Spencer22256f42007-08-02 03:30:26 +00001337 if (call->getNumOperands() > 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001338 Out << "std::vector<Value*> " << iName << "_params;";
1339 nl(Out);
1340 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1341 Out << iName << "_params.push_back(" << opNames[i] << ");";
1342 nl(Out);
1343 }
1344 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer22256f42007-08-02 03:30:26 +00001345 << opNames[0] << ", " << iName << "_params.begin(), "
1346 << iName << "_params.end(), \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001347 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001348 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001349 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001350 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001351 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001352 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001353 }
1354 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001355 Out << "\", " << bbname << ");";
1356 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001357 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001358 Out << ");";
1359 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001360 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001361 Out << ");";
1362 break;
1363 }
1364 case Instruction::Select: {
1365 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001366 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001367 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001368 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001369 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001370 break;
1371 }
1372 case Instruction::UserOp1:
1373 /// FALL THROUGH
1374 case Instruction::UserOp2: {
1375 /// FIXME: What should be done here?
1376 break;
1377 }
1378 case Instruction::VAArg: {
1379 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001380 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001381 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001382 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001383 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001384 break;
1385 }
1386 case Instruction::ExtractElement: {
1387 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001388 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001389 << " = new ExtractElementInst(" << opNames[0]
1390 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001391 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001392 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001393 break;
1394 }
1395 case Instruction::InsertElement: {
1396 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001397 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001398 << " = new InsertElementInst(" << opNames[0]
1399 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001400 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001401 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001402 break;
1403 }
1404 case Instruction::ShuffleVector: {
1405 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001406 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001407 << " = new ShuffleVectorInst(" << opNames[0]
1408 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001409 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001410 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001411 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001412 }
1413 }
Reid Spencer68464b72006-06-15 16:09:59 +00001414 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001415 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001416 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001417}
1418
Reid Spencer12803f52006-05-31 17:31:38 +00001419// Print out the types, constants and declarations needed by one function
1420void CppWriter::printFunctionUses(const Function* F) {
1421
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001422 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001423 if (!is_inline) {
1424 // Print the function's return type
1425 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001426
Reid Spencerf977e7b2006-06-01 23:43:47 +00001427 // Print the function's function type
1428 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001429
Reid Spencerf977e7b2006-06-01 23:43:47 +00001430 // Print the types of each of the function's arguments
1431 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1432 AI != AE; ++AI) {
1433 printType(AI->getType());
1434 }
Reid Spencer12803f52006-05-31 17:31:38 +00001435 }
1436
1437 // Print type definitions for every type referenced by an instruction and
1438 // make a note of any global values or constants that are referenced
Reid Spencera4d71f02007-06-16 20:48:27 +00001439 SmallPtrSet<GlobalValue*,64> gvs;
1440 SmallPtrSet<Constant*,64> consts;
Reid Spencer12803f52006-05-31 17:31:38 +00001441 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1442 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1443 I != E; ++I) {
1444 // Print the type of the instruction itself
1445 printType(I->getType());
1446
1447 // Print the type of each of the instruction's operands
1448 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1449 Value* operand = I->getOperand(i);
1450 printType(operand->getType());
Reid Spencerab24b4c2007-06-16 20:33:24 +00001451
1452 // If the operand references a GVal or Constant, make a note of it
1453 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
Reid Spencera4d71f02007-06-16 20:48:27 +00001454 gvs.insert(GV);
Reid Spencerab24b4c2007-06-16 20:33:24 +00001455 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1456 if (GVar->hasInitializer())
Reid Spencera4d71f02007-06-16 20:48:27 +00001457 consts.insert(GVar->getInitializer());
Reid Spencerab24b4c2007-06-16 20:33:24 +00001458 } else if (Constant* C = dyn_cast<Constant>(operand))
Reid Spencera4d71f02007-06-16 20:48:27 +00001459 consts.insert(C);
Reid Spencer12803f52006-05-31 17:31:38 +00001460 }
1461 }
1462 }
1463
1464 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001465 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001466 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001467 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001468 if (Function* Fun = dyn_cast<Function>(*I)) {
1469 if (!is_inline || Fun != F)
1470 printFunctionHead(Fun);
1471 }
Reid Spencer12803f52006-05-31 17:31:38 +00001472 }
1473
1474 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001475 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001476 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001477 I != E; ++I) {
1478 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1479 printVariableHead(F);
1480 }
1481
1482 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001483 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001484 for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(), E = consts.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001485 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001486 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001487 }
1488
1489 // Process the global variables definitions now that all the constants have
1490 // been emitted. These definitions just couple the gvars with their constant
1491 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001492 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001493 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001494 I != E; ++I) {
1495 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1496 printVariableBody(GV);
1497 }
1498}
1499
1500void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001501 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001502 if (is_inline) {
1503 Out << " = mod->getFunction(\"";
1504 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001505 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1506 nl(Out) << "if (!" << getCppName(F) << ") {";
1507 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001508 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001509 Out<< " = new Function(";
1510 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1511 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001512 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001513 Out << ",";
1514 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001515 printEscapedString(F->getName());
Reid Spencer5cbf9852007-01-30 20:08:39 +00001516 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001517 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001518 printCppName(F);
1519 Out << "->setCallingConv(";
1520 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001521 Out << ");";
1522 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001523 if (F->hasSection()) {
1524 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001525 Out << "->setSection(\"" << F->getSection() << "\");";
1526 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001527 }
1528 if (F->getAlignment()) {
1529 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001530 Out << "->setAlignment(" << F->getAlignment() << ");";
1531 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001532 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001533 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001534 Out << "}";
1535 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001536 }
Reid Spencer12803f52006-05-31 17:31:38 +00001537}
1538
1539void CppWriter::printFunctionBody(const Function *F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001540 if (F->isDeclaration())
Reid Spencer12803f52006-05-31 17:31:38 +00001541 return; // external functions have no bodies.
1542
1543 // Clear the DefinedValues and ForwardRefs maps because we can't have
1544 // cross-function forward refs
1545 ForwardRefs.clear();
1546 DefinedValues.clear();
1547
1548 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001549 if (!is_inline) {
1550 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001551 Out << "Function::arg_iterator args = " << getCppName(F)
1552 << "->arg_begin();";
1553 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001554 }
1555 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1556 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001557 Out << "Value* " << getCppName(AI) << " = args++;";
1558 nl(Out);
1559 if (AI->hasName()) {
1560 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1561 nl(Out);
1562 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001563 }
Reid Spencer12803f52006-05-31 17:31:38 +00001564 }
1565
1566 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001567 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001568 for (Function::const_iterator BI = F->begin(), BE = F->end();
1569 BI != BE; ++BI) {
1570 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001571 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001572 if (BI->hasName())
1573 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001574 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1575 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001576 }
1577
1578 // Output all of its basic blocks... for the function
1579 for (Function::const_iterator BI = F->begin(), BE = F->end();
1580 BI != BE; ++BI) {
1581 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001582 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1583 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001584
1585 // Output all of the instructions in the basic block...
1586 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1587 I != E; ++I) {
1588 printInstruction(I,bbname);
1589 }
1590 }
1591
1592 // Loop over the ForwardRefs and resolve them now that all instructions
1593 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001594 if (!ForwardRefs.empty()) {
1595 nl(Out) << "// Resolve Forward References";
1596 nl(Out);
1597 }
1598
Reid Spencer12803f52006-05-31 17:31:38 +00001599 while (!ForwardRefs.empty()) {
1600 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001601 Out << I->second << "->replaceAllUsesWith("
1602 << getCppName(I->first) << "); delete " << I->second << ";";
1603 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001604 ForwardRefs.erase(I);
1605 }
1606}
1607
Reid Spencerf977e7b2006-06-01 23:43:47 +00001608void CppWriter::printInline(const std::string& fname, const std::string& func) {
Reid Spencer688b0492007-02-05 21:19:13 +00001609 const Function* F = TheModule->getFunction(func);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001610 if (!F) {
1611 error(std::string("Function '") + func + "' not found in input module");
1612 return;
1613 }
Reid Spencer5cbf9852007-01-30 20:08:39 +00001614 if (F->isDeclaration()) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001615 error(std::string("Function '") + func + "' is external!");
1616 return;
1617 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001618 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001619 << getCppName(F);
1620 unsigned arg_count = 1;
1621 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1622 AI != AE; ++AI) {
1623 Out << ", Value* arg_" << arg_count;
1624 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001625 Out << ") {";
1626 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001627 is_inline = true;
1628 printFunctionUses(F);
1629 printFunctionBody(F);
1630 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001631 Out << "return " << getCppName(F->begin()) << ";";
1632 nl(Out) << "}";
1633 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001634}
1635
Reid Spencer12803f52006-05-31 17:31:38 +00001636void CppWriter::printModuleBody() {
1637 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001638 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001639 printTypes(TheModule);
1640
1641 // Functions can call each other and global variables can reference them so
1642 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001643 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001644 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1645 I != E; ++I)
1646 printFunctionHead(I);
1647
1648 // Process the global variables declarations. We can't initialze them until
1649 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001650 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001651 for (Module::const_global_iterator I = TheModule->global_begin(),
1652 E = TheModule->global_end(); I != E; ++I) {
1653 printVariableHead(I);
1654 }
1655
1656 // Print out all the constants definitions. Constants don't recurse except
1657 // through GlobalValues. All GlobalValues have been declared at this point
1658 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001659 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001660 printConstants(TheModule);
1661
1662 // Process the global variables definitions now that all the constants have
1663 // been emitted. These definitions just couple the gvars with their constant
1664 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001665 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001666 for (Module::const_global_iterator I = TheModule->global_begin(),
1667 E = TheModule->global_end(); I != E; ++I) {
1668 printVariableBody(I);
1669 }
1670
1671 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001672 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001673 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1674 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001675 if (!I->isDeclaration()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001676 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1677 << ")";
1678 nl(Out) << "{";
1679 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001680 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001681 nl(Out,-1) << "}";
1682 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001683 }
1684 }
1685}
1686
1687void CppWriter::printProgram(
1688 const std::string& fname,
1689 const std::string& mName
1690) {
1691 Out << "#include <llvm/Module.h>\n";
1692 Out << "#include <llvm/DerivedTypes.h>\n";
1693 Out << "#include <llvm/Constants.h>\n";
1694 Out << "#include <llvm/GlobalVariable.h>\n";
1695 Out << "#include <llvm/Function.h>\n";
1696 Out << "#include <llvm/CallingConv.h>\n";
1697 Out << "#include <llvm/BasicBlock.h>\n";
1698 Out << "#include <llvm/Instructions.h>\n";
1699 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencer07441662007-04-11 12:28:56 +00001700 Out << "#include <llvm/ParameterAttributes.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001701 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001702 Out << "#include <llvm/Pass.h>\n";
1703 Out << "#include <llvm/PassManager.h>\n";
1704 Out << "#include <llvm/Analysis/Verifier.h>\n";
1705 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1706 Out << "#include <algorithm>\n";
1707 Out << "#include <iostream>\n\n";
1708 Out << "using namespace llvm;\n\n";
1709 Out << "Module* " << fname << "();\n\n";
1710 Out << "int main(int argc, char**argv) {\n";
Nick Lewycky8946fe12007-06-16 16:17:35 +00001711 Out << " Module* Mod = " << fname << "();\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001712 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1713 Out << " std::cerr.flush();\n";
1714 Out << " std::cout.flush();\n";
1715 Out << " PassManager PM;\n";
Reid Spencer07441662007-04-11 12:28:56 +00001716 Out << " PM.add(new PrintModulePass(&llvm::cout));\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001717 Out << " PM.run(*Mod);\n";
1718 Out << " return 0;\n";
1719 Out << "}\n\n";
1720 printModule(fname,mName);
1721}
1722
1723void CppWriter::printModule(
1724 const std::string& fname,
1725 const std::string& mName
1726) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001727 nl(Out) << "Module* " << fname << "() {";
1728 nl(Out,1) << "// Module Construction";
1729 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001730 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001731 nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
1732 }
1733 if (!TheModule->getTargetTriple().empty()) {
1734 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1735 << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001736 }
Reid Spencer12803f52006-05-31 17:31:38 +00001737
1738 if (!TheModule->getModuleInlineAsm().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001739 nl(Out) << "mod->setModuleInlineAsm(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001740 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001741 Out << "\");";
Reid Spencer12803f52006-05-31 17:31:38 +00001742 }
Reid Spencer07441662007-04-11 12:28:56 +00001743 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001744
1745 // Loop over the dependent libraries and emit them.
1746 Module::lib_iterator LI = TheModule->lib_begin();
1747 Module::lib_iterator LE = TheModule->lib_end();
1748 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001749 Out << "mod->addLibrary(\"" << *LI << "\");";
1750 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001751 ++LI;
1752 }
1753 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001754 nl(Out) << "return mod;";
1755 nl(Out,-1) << "}";
1756 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001757}
1758
1759void CppWriter::printContents(
1760 const std::string& fname, // Name of generated function
1761 const std::string& mName // Name of module generated module
1762) {
1763 Out << "\nModule* " << fname << "(Module *mod) {\n";
1764 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001765 printModuleBody();
1766 Out << "\nreturn mod;\n";
1767 Out << "\n}\n";
1768}
1769
1770void CppWriter::printFunction(
1771 const std::string& fname, // Name of generated function
1772 const std::string& funcName // Name of function to generate
1773) {
Reid Spencer688b0492007-02-05 21:19:13 +00001774 const Function* F = TheModule->getFunction(funcName);
Reid Spencer12803f52006-05-31 17:31:38 +00001775 if (!F) {
1776 error(std::string("Function '") + funcName + "' not found in input module");
1777 return;
1778 }
1779 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1780 printFunctionUses(F);
1781 printFunctionHead(F);
1782 printFunctionBody(F);
1783 Out << "return " << getCppName(F) << ";\n";
1784 Out << "}\n";
1785}
1786
1787void CppWriter::printVariable(
1788 const std::string& fname, /// Name of generated function
1789 const std::string& varName // Name of variable to generate
1790) {
1791 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1792
1793 if (!GV) {
1794 error(std::string("Variable '") + varName + "' not found in input module");
1795 return;
1796 }
1797 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1798 printVariableUses(GV);
1799 printVariableHead(GV);
1800 printVariableBody(GV);
1801 Out << "return " << getCppName(GV) << ";\n";
1802 Out << "}\n";
1803}
1804
1805void CppWriter::printType(
1806 const std::string& fname, /// Name of generated function
1807 const std::string& typeName // Name of type to generate
1808) {
1809 const Type* Ty = TheModule->getTypeByName(typeName);
1810 if (!Ty) {
1811 error(std::string("Type '") + typeName + "' not found in input module");
1812 return;
1813 }
1814 Out << "\nType* " << fname << "(Module *mod) {\n";
1815 printType(Ty);
1816 Out << "return " << getCppName(Ty) << ";\n";
1817 Out << "}\n";
1818}
1819
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001820} // end anonymous llvm
1821
1822namespace llvm {
1823
1824void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001825 // Initialize a CppWriter for us to use
1826 CppWriter W(o, mod);
1827
1828 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001829 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001830
1831 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001832 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001833
1834 // Get the name of the thing we are to generate
1835 std::string tgtname = NameToGenerate.getValue();
1836 if (GenerationType == GenModule ||
1837 GenerationType == GenContents ||
1838 GenerationType == GenProgram) {
1839 if (tgtname == "!bad!") {
1840 if (mod->getModuleIdentifier() == "-")
1841 tgtname = "<stdin>";
1842 else
1843 tgtname = mod->getModuleIdentifier();
1844 }
1845 } else if (tgtname == "!bad!") {
1846 W.error("You must use the -for option with -gen-{function,variable,type}");
1847 }
1848
1849 switch (WhatToGenerate(GenerationType)) {
1850 case GenProgram:
1851 if (fname.empty())
1852 fname = "makeLLVMModule";
1853 W.printProgram(fname,tgtname);
1854 break;
1855 case GenModule:
1856 if (fname.empty())
1857 fname = "makeLLVMModule";
1858 W.printModule(fname,tgtname);
1859 break;
1860 case GenContents:
1861 if (fname.empty())
1862 fname = "makeLLVMModuleContents";
1863 W.printContents(fname,tgtname);
1864 break;
1865 case GenFunction:
1866 if (fname.empty())
1867 fname = "makeLLVMFunction";
1868 W.printFunction(fname,tgtname);
1869 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001870 case GenInline:
1871 if (fname.empty())
1872 fname = "makeLLVMInline";
1873 W.printInline(fname,tgtname);
1874 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001875 case GenVariable:
1876 if (fname.empty())
1877 fname = "makeLLVMVariable";
1878 W.printVariable(fname,tgtname);
1879 break;
1880 case GenType:
1881 if (fname.empty())
1882 fname = "makeLLVMType";
1883 W.printType(fname,tgtname);
1884 break;
1885 default:
1886 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001887 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001888}
1889
1890}