blob: 18bdc1b6668dd43b98ef9342f7fc7bd2d8572cae [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,
Chris Lattner120119d2007-11-13 18:22:33 +000047 GenFunctions,
Reid Spencerf977e7b2006-06-01 23:43:47 +000048 GenInline,
Reid Spencer12803f52006-05-31 17:31:38 +000049 GenVariable,
50 GenType
51};
52
53static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
54 cl::desc("Choose what kind of output to generate"),
55 cl::init(GenProgram),
56 cl::values(
Chris Lattner120119d2007-11-13 18:22:33 +000057 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
58 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
59 clEnumValN(GenContents, "gen-contents", "Generate contents of a module"),
60 clEnumValN(GenFunction, "gen-function", "Generate a function definition"),
61 clEnumValN(GenFunctions,"gen-functions", "Generate all function definitions"),
62 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
63 clEnumValN(GenVariable, "gen-variable", "Generate a variable definition"),
64 clEnumValN(GenType, "gen-type", "Generate a type definition"),
Reid Spencer12803f52006-05-31 17:31:38 +000065 clEnumValEnd
66 )
67);
68
69static cl::opt<std::string> NameToGenerate("for", cl::Optional,
70 cl::desc("Specify the name of the thing to generate"),
71 cl::init("!bad!"));
Reid Spencer15f7e012006-05-30 21:18:23 +000072
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000073namespace {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000074typedef std::vector<const Type*> TypeList;
75typedef std::map<const Type*,std::string> TypeMap;
76typedef std::map<const Value*,std::string> ValueMap;
Reid Spencer66c87342006-05-30 03:43:49 +000077typedef std::set<std::string> NameSet;
Reid Spencer15f7e012006-05-30 21:18:23 +000078typedef std::set<const Type*> TypeSet;
79typedef std::set<const Value*> ValueSet;
80typedef std::map<const Value*,std::string> ForwardRefMap;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000081
Reid Spencere0d133f2006-05-29 18:08:06 +000082class CppWriter {
Reid Spencer12803f52006-05-31 17:31:38 +000083 const char* progname;
Reid Spencere0d133f2006-05-29 18:08:06 +000084 std::ostream &Out;
85 const Module *TheModule;
Andrew Lenharth539d8712006-05-31 20:18:56 +000086 uint64_t uniqueNum;
Reid Spencere0d133f2006-05-29 18:08:06 +000087 TypeMap TypeNames;
88 ValueMap ValueNames;
89 TypeMap UnresolvedTypes;
90 TypeList TypeStack;
Reid Spencer66c87342006-05-30 03:43:49 +000091 NameSet UsedNames;
Reid Spencer15f7e012006-05-30 21:18:23 +000092 TypeSet DefinedTypes;
93 ValueSet DefinedValues;
94 ForwardRefMap ForwardRefs;
Reid Spencerf977e7b2006-06-01 23:43:47 +000095 bool is_inline;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000096
Reid Spencere0d133f2006-05-29 18:08:06 +000097public:
Reid Spencer12803f52006-05-31 17:31:38 +000098 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
99 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
Reid Spencerf977e7b2006-06-01 23:43:47 +0000100 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000101
Reid Spencere0d133f2006-05-29 18:08:06 +0000102 const Module* getModule() { return TheModule; }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000103
Reid Spencer12803f52006-05-31 17:31:38 +0000104 void printProgram(const std::string& fname, const std::string& modName );
105 void printModule(const std::string& fname, const std::string& modName );
106 void printContents(const std::string& fname, const std::string& modName );
107 void printFunction(const std::string& fname, const std::string& funcName );
Chris Lattner120119d2007-11-13 18:22:33 +0000108 void printFunctions();
Reid Spencerf977e7b2006-06-01 23:43:47 +0000109 void printInline(const std::string& fname, const std::string& funcName );
Reid Spencer12803f52006-05-31 17:31:38 +0000110 void printVariable(const std::string& fname, const std::string& varName );
111 void printType(const std::string& fname, const std::string& typeName );
112
113 void error(const std::string& msg);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000114
Reid Spencere0d133f2006-05-29 18:08:06 +0000115private:
Reid Spencer12803f52006-05-31 17:31:38 +0000116 void printLinkageType(GlobalValue::LinkageTypes LT);
Duncan Sandsdc024672007-11-27 13:23:08 +0000117 void printVisibilityType(GlobalValue::VisibilityTypes VisTypes);
Reid Spencer12803f52006-05-31 17:31:38 +0000118 void printCallingConv(unsigned cc);
119 void printEscapedString(const std::string& str);
120 void printCFP(const ConstantFP* CFP);
121
122 std::string getCppName(const Type* val);
123 inline void printCppName(const Type* val);
124
125 std::string getCppName(const Value* val);
126 inline void printCppName(const Value* val);
127
Duncan Sandsdc024672007-11-27 13:23:08 +0000128 void printParamAttrs(const ParamAttrsList* PAL, const std::string &name);
Reid Spencer12803f52006-05-31 17:31:38 +0000129 bool printTypeInternal(const Type* Ty);
130 inline void printType(const Type* Ty);
Reid Spencere0d133f2006-05-29 18:08:06 +0000131 void printTypes(const Module* M);
Reid Spencer12803f52006-05-31 17:31:38 +0000132
Reid Spencere0d133f2006-05-29 18:08:06 +0000133 void printConstant(const Constant *CPV);
Reid Spencer12803f52006-05-31 17:31:38 +0000134 void printConstants(const Module* M);
135
136 void printVariableUses(const GlobalVariable *GV);
137 void printVariableHead(const GlobalVariable *GV);
138 void printVariableBody(const GlobalVariable *GV);
139
140 void printFunctionUses(const Function *F);
Reid Spencer66c87342006-05-30 03:43:49 +0000141 void printFunctionHead(const Function *F);
142 void printFunctionBody(const Function *F);
Reid Spencere0d133f2006-05-29 18:08:06 +0000143 void printInstruction(const Instruction *I, const std::string& bbname);
Reid Spencer15f7e012006-05-30 21:18:23 +0000144 std::string getOpName(Value*);
145
Reid Spencer12803f52006-05-31 17:31:38 +0000146 void printModuleBody();
147
Reid Spencere0d133f2006-05-29 18:08:06 +0000148};
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000149
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000150static unsigned indent_level = 0;
151inline std::ostream& nl(std::ostream& Out, int delta = 0) {
152 Out << "\n";
153 if (delta >= 0 || indent_level >= unsigned(-delta))
154 indent_level += delta;
155 for (unsigned i = 0; i < indent_level; ++i)
156 Out << " ";
157 return Out;
158}
159
160inline void in() { indent_level++; }
161inline void out() { if (indent_level >0) indent_level--; }
162
Reid Spencer12803f52006-05-31 17:31:38 +0000163inline void
164sanitize(std::string& str) {
165 for (size_t i = 0; i < str.length(); ++i)
166 if (!isalnum(str[i]) && str[i] != '_')
167 str[i] = '_';
168}
169
Reid Spencera54b7cb2007-01-12 07:05:14 +0000170inline std::string
Reid Spencer12803f52006-05-31 17:31:38 +0000171getTypePrefix(const Type* Ty ) {
Reid Spencer12803f52006-05-31 17:31:38 +0000172 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000173 case Type::VoidTyID: return "void_";
174 case Type::IntegerTyID:
175 return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
176 "_";
177 case Type::FloatTyID: return "float_";
178 case Type::DoubleTyID: return "double_";
179 case Type::LabelTyID: return "label_";
180 case Type::FunctionTyID: return "func_";
181 case Type::StructTyID: return "struct_";
182 case Type::ArrayTyID: return "array_";
183 case Type::PointerTyID: return "ptr_";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000184 case Type::VectorTyID: return "packed_";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000185 case Type::OpaqueTyID: return "opaque_";
186 default: return "other_";
Reid Spencer12803f52006-05-31 17:31:38 +0000187 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000188 return "unknown_";
Reid Spencer12803f52006-05-31 17:31:38 +0000189}
190
191// Looks up the type in the symbol table and returns a pointer to its name or
192// a null pointer if it wasn't found. Note that this isn't the same as the
193// Mode::getTypeName function which will return an empty string, not a null
194// pointer if the name is not found.
195inline const std::string*
Reid Spencer78d033e2007-01-06 07:24:44 +0000196findTypeName(const TypeSymbolTable& ST, const Type* Ty)
Reid Spencer12803f52006-05-31 17:31:38 +0000197{
Reid Spencer78d033e2007-01-06 07:24:44 +0000198 TypeSymbolTable::const_iterator TI = ST.begin();
199 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencer12803f52006-05-31 17:31:38 +0000200 for (;TI != TE; ++TI)
201 if (TI->second == Ty)
202 return &(TI->first);
203 return 0;
204}
205
206void
207CppWriter::error(const std::string& msg) {
208 std::cerr << progname << ": " << msg << "\n";
209 exit(2);
210}
211
Reid Spencer15f7e012006-05-30 21:18:23 +0000212// printCFP - Print a floating point constant .. very carefully :)
213// This makes sure that conversion to/from floating yields the same binary
214// result so that we don't lose precision.
215void
216CppWriter::printCFP(const ConstantFP *CFP) {
Dale Johannesen43421b32007-09-06 18:13:44 +0000217 APFloat APF = APFloat(CFP->getValueAPF()); // copy
218 if (CFP->getType() == Type::FloatTy)
219 APF.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000220 Out << "ConstantFP::get(";
221 if (CFP->getType() == Type::DoubleTy)
222 Out << "Type::DoubleTy, ";
223 else
224 Out << "Type::FloatTy, ";
Dale Johannesen43421b32007-09-06 18:13:44 +0000225 Out << "APFloat(";
Reid Spencer15f7e012006-05-30 21:18:23 +0000226#if HAVE_PRINTF_A
227 char Buffer[100];
Dale Johannesen43421b32007-09-06 18:13:44 +0000228 sprintf(Buffer, "%A", APF.convertToDouble());
Reid Spencer15f7e012006-05-30 21:18:23 +0000229 if ((!strncmp(Buffer, "0x", 2) ||
230 !strncmp(Buffer, "-0x", 3) ||
231 !strncmp(Buffer, "+0x", 3)) &&
Dale Johannesen43421b32007-09-06 18:13:44 +0000232 APF.bitwiseIsEqual(APFloat(atof(Buffer)))) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000233 if (CFP->getType() == Type::DoubleTy)
234 Out << "BitsToDouble(" << Buffer << ")";
235 else
Dale Johannesen43421b32007-09-06 18:13:44 +0000236 Out << "BitsToFloat((float)" << Buffer << ")";
237 Out << ")";
238 } else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000239#endif
Dale Johannesen43421b32007-09-06 18:13:44 +0000240 std::string StrVal = ftostr(CFP->getValueAPF());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000241
242 while (StrVal[0] == ' ')
243 StrVal.erase(StrVal.begin());
244
245 // Check to make sure that the stringized number is not some string like
246 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
247 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
248 ((StrVal[0] == '-' || StrVal[0] == '+') &&
249 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
Dale Johannesen43421b32007-09-06 18:13:44 +0000250 (CFP->isExactlyValue(atof(StrVal.c_str())))) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000251 if (CFP->getType() == Type::DoubleTy)
252 Out << StrVal;
253 else
Dale Johannesen43421b32007-09-06 18:13:44 +0000254 Out << StrVal << "f";
255 }
Reid Spencerf977e7b2006-06-01 23:43:47 +0000256 else if (CFP->getType() == Type::DoubleTy)
Dale Johannesen43421b32007-09-06 18:13:44 +0000257 Out << "BitsToDouble(0x" << std::hex
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000258 << CFP->getValueAPF().convertToAPInt().getZExtValue()
Reid Spencerf977e7b2006-06-01 23:43:47 +0000259 << std::dec << "ULL) /* " << StrVal << " */";
260 else
Dale Johannesen43421b32007-09-06 18:13:44 +0000261 Out << "BitsToFloat(0x" << std::hex
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000262 << (uint32_t)CFP->getValueAPF().convertToAPInt().getZExtValue()
Reid Spencerf977e7b2006-06-01 23:43:47 +0000263 << std::dec << "U) /* " << StrVal << " */";
Dale Johannesen43421b32007-09-06 18:13:44 +0000264 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000265#if HAVE_PRINTF_A
266 }
267#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000268 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000269}
270
Reid Spencer12803f52006-05-31 17:31:38 +0000271void
272CppWriter::printCallingConv(unsigned cc){
273 // Print the calling convention.
274 switch (cc) {
275 case CallingConv::C: Out << "CallingConv::C"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000276 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
277 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
278 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
279 default: Out << cc; break;
280 }
281}
Reid Spencer15f7e012006-05-30 21:18:23 +0000282
Reid Spencer12803f52006-05-31 17:31:38 +0000283void
284CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
285 switch (LT) {
286 case GlobalValue::InternalLinkage:
287 Out << "GlobalValue::InternalLinkage"; break;
288 case GlobalValue::LinkOnceLinkage:
289 Out << "GlobalValue::LinkOnceLinkage "; break;
290 case GlobalValue::WeakLinkage:
291 Out << "GlobalValue::WeakLinkage"; break;
292 case GlobalValue::AppendingLinkage:
293 Out << "GlobalValue::AppendingLinkage"; break;
294 case GlobalValue::ExternalLinkage:
295 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000296 case GlobalValue::DLLImportLinkage:
Anton Korobeynikov02e21522007-07-11 19:51:06 +0000297 Out << "GlobalValue::DLLImportLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000298 case GlobalValue::DLLExportLinkage:
Anton Korobeynikov02e21522007-07-11 19:51:06 +0000299 Out << "GlobalValue::DLLExportLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000300 case GlobalValue::ExternalWeakLinkage:
301 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000302 case GlobalValue::GhostLinkage:
303 Out << "GlobalValue::GhostLinkage"; break;
304 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000305}
306
Duncan Sandsdc024672007-11-27 13:23:08 +0000307void
308CppWriter::printVisibilityType(GlobalValue::VisibilityTypes VisType) {
309 switch (VisType) {
310 default: assert(0 && "Unknown GVar visibility");
311 case GlobalValue::DefaultVisibility:
312 Out << "GlobalValue::DefaultVisibility";
313 break;
314 case GlobalValue::HiddenVisibility:
315 Out << "GlobalValue::HiddenVisibility";
316 break;
317 case GlobalValue::ProtectedVisibility:
318 Out << "GlobalValue::ProtectedVisibility";
319 break;
320 }
321}
322
Reid Spencere0d133f2006-05-29 18:08:06 +0000323// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000324// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000325void
326CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000327 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
328 unsigned char C = Str[i];
329 if (isprint(C) && C != '"' && C != '\\') {
330 Out << C;
331 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000332 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000333 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
334 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
335 }
336 }
337}
338
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000339std::string
340CppWriter::getCppName(const Type* Ty)
341{
342 // First, handle the primitive types .. easy
Chris Lattner42a75512007-01-15 02:27:26 +0000343 if (Ty->isPrimitiveType() || Ty->isInteger()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000344 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000345 case Type::VoidTyID: return "Type::VoidTy";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000346 case Type::IntegerTyID: {
347 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
348 return "IntegerType::get(" + utostr(BitWidth) + ")";
349 }
Reid Spencer71d2ec92006-12-31 06:02:26 +0000350 case Type::FloatTyID: return "Type::FloatTy";
351 case Type::DoubleTyID: return "Type::DoubleTy";
352 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000353 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000354 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000355 break;
356 }
357 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
358 }
359
360 // Now, see if we've seen the type before and return that
361 TypeMap::iterator I = TypeNames.find(Ty);
362 if (I != TypeNames.end())
363 return I->second;
364
365 // Okay, let's build a new name for this type. Start with a prefix
366 const char* prefix = 0;
367 switch (Ty->getTypeID()) {
368 case Type::FunctionTyID: prefix = "FuncTy_"; break;
369 case Type::StructTyID: prefix = "StructTy_"; break;
370 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
371 case Type::PointerTyID: prefix = "PointerTy_"; break;
372 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
Reid Spencerac9dcb92007-02-15 03:39:18 +0000373 case Type::VectorTyID: prefix = "VectorTy_"; break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000374 default: prefix = "OtherTy_"; break; // prevent breakage
375 }
376
377 // See if the type has a name in the symboltable and build accordingly
Reid Spencer78d033e2007-01-06 07:24:44 +0000378 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000379 std::string name;
380 if (tName)
381 name = std::string(prefix) + *tName;
382 else
383 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000384 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000385
386 // Save the name
387 return TypeNames[Ty] = name;
388}
389
Reid Spencer12803f52006-05-31 17:31:38 +0000390void
391CppWriter::printCppName(const Type* Ty)
392{
393 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000394}
395
Reid Spencer12803f52006-05-31 17:31:38 +0000396std::string
397CppWriter::getCppName(const Value* val) {
398 std::string name;
399 ValueMap::iterator I = ValueNames.find(val);
400 if (I != ValueNames.end() && I->first == val)
401 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000402
Reid Spencer12803f52006-05-31 17:31:38 +0000403 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
404 name = std::string("gvar_") +
405 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000406 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000407 name = std::string("func_");
408 } else if (const Constant* C = dyn_cast<Constant>(val)) {
409 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000410 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
411 if (is_inline) {
412 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
413 Function::const_arg_iterator(Arg)) + 1;
414 name = std::string("arg_") + utostr(argNum);
415 NameSet::iterator NI = UsedNames.find(name);
416 if (NI != UsedNames.end())
417 name += std::string("_") + utostr(uniqueNum++);
418 UsedNames.insert(name);
419 return ValueNames[val] = name;
420 } else {
421 name = getTypePrefix(val->getType());
422 }
Reid Spencer12803f52006-05-31 17:31:38 +0000423 } else {
424 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000425 }
Reid Spencer12803f52006-05-31 17:31:38 +0000426 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
427 sanitize(name);
428 NameSet::iterator NI = UsedNames.find(name);
429 if (NI != UsedNames.end())
430 name += std::string("_") + utostr(uniqueNum++);
431 UsedNames.insert(name);
432 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000433}
434
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000435void
Reid Spencer12803f52006-05-31 17:31:38 +0000436CppWriter::printCppName(const Value* val) {
437 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000438}
439
Duncan Sandsdc024672007-11-27 13:23:08 +0000440void
441CppWriter::printParamAttrs(const ParamAttrsList* PAL, const std::string &name) {
442 Out << "ParamAttrsList *" << name << "_PAL = 0;";
443 nl(Out);
444 if (PAL) {
445 Out << '{'; in(); nl(Out);
446 Out << "ParamAttrsVector Attrs;"; nl(Out);
447 Out << "ParamAttrsWithIndex PAWI;"; nl(Out);
448 for (unsigned i = 0; i < PAL->size(); ++i) {
449 uint16_t index = PAL->getParamIndex(i);
450 uint16_t attrs = PAL->getParamAttrs(index);
451 Out << "PAWI.index = " << index << "; PAWI.attrs = 0 ";
452 if (attrs & ParamAttr::SExt)
453 Out << " | ParamAttr::SExt";
454 if (attrs & ParamAttr::ZExt)
455 Out << " | ParamAttr::ZExt";
456 if (attrs & ParamAttr::StructRet)
457 Out << " | ParamAttr::StructRet";
458 if (attrs & ParamAttr::InReg)
459 Out << " | ParamAttr::InReg";
460 if (attrs & ParamAttr::NoReturn)
461 Out << " | ParamAttr::NoReturn";
462 if (attrs & ParamAttr::NoUnwind)
463 Out << " | ParamAttr::NoUnwind";
464 Out << ";";
465 nl(Out);
466 Out << "Attrs.push_back(PAWI);";
467 nl(Out);
468 }
469 Out << name << "_PAL = ParamAttrsList::get(Attrs);";
470 nl(Out);
471 out(); nl(Out);
472 Out << '}'; nl(Out);
473 }
474}
475
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000476bool
Reid Spencer12803f52006-05-31 17:31:38 +0000477CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000478 // We don't print definitions for primitive types
Chris Lattner42a75512007-01-15 02:27:26 +0000479 if (Ty->isPrimitiveType() || Ty->isInteger())
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000480 return false;
481
Reid Spencer15f7e012006-05-30 21:18:23 +0000482 // If we already defined this type, we don't need to define it again.
483 if (DefinedTypes.find(Ty) != DefinedTypes.end())
484 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000485
Reid Spencer15f7e012006-05-30 21:18:23 +0000486 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000487 std::string typeName(getCppName(Ty));
488
489 // Search the type stack for recursion. If we find it, then generate this
490 // as an OpaqueType, but make sure not to do this multiple times because
491 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000492 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000493 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000494 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
495 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000496 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
497 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000498 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
499 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000500 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000501 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000502 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000503 }
504
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000505 // We're going to print a derived type which, by definition, contains other
506 // types. So, push this one we're printing onto the type stack to assist with
507 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000508 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000509
510 // Print the type definition
511 switch (Ty->getTypeID()) {
512 case Type::FunctionTyID: {
513 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000514 Out << "std::vector<const Type*>" << typeName << "_args;";
515 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000516 FunctionType::param_iterator PI = FT->param_begin();
517 FunctionType::param_iterator PE = FT->param_end();
518 for (; PI != PE; ++PI) {
519 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000520 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000521 std::string argName(getCppName(argTy));
522 Out << typeName << "_args.push_back(" << argName;
523 if (isForward)
524 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000525 Out << ");";
526 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000527 }
Reid Spencer12803f52006-05-31 17:31:38 +0000528 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000529 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000530 Out << "FunctionType* " << typeName << " = FunctionType::get(";
531 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000532 if (isForward)
533 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000534 Out << ",";
535 nl(Out) << "/*Params=*/" << typeName << "_args,";
Duncan Sandsdc024672007-11-27 13:23:08 +0000536 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000537 out();
538 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000539 break;
540 }
541 case Type::StructTyID: {
542 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000543 Out << "std::vector<const Type*>" << typeName << "_fields;";
544 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000545 StructType::element_iterator EI = ST->element_begin();
546 StructType::element_iterator EE = ST->element_end();
547 for (; EI != EE; ++EI) {
548 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000549 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000550 std::string fieldName(getCppName(fieldTy));
551 Out << typeName << "_fields.push_back(" << fieldName;
552 if (isForward)
553 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000554 Out << ");";
555 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000556 }
557 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer46fea102007-04-11 12:41:49 +0000558 << typeName << "_fields, /*isPacked=*/"
559 << (ST->isPacked() ? "true" : "false") << ");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000560 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000561 break;
562 }
563 case Type::ArrayTyID: {
564 const ArrayType* AT = cast<ArrayType>(Ty);
565 const Type* ET = AT->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));
568 Out << "ArrayType* " << typeName << " = ArrayType::get("
569 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000570 << ", " << utostr(AT->getNumElements()) << ");";
571 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000572 break;
573 }
574 case Type::PointerTyID: {
575 const PointerType* PT = cast<PointerType>(Ty);
576 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000577 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000578 std::string elemName(getCppName(ET));
579 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000580 << elemName << (isForward ? "_fwd" : "") << ");";
581 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000582 break;
583 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000584 case Type::VectorTyID: {
585 const VectorType* PT = cast<VectorType>(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000586 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000587 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000588 std::string elemName(getCppName(ET));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000589 Out << "VectorType* " << typeName << " = VectorType::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000590 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000591 << ", " << utostr(PT->getNumElements()) << ");";
592 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000593 break;
594 }
595 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000596 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
597 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000598 break;
599 }
600 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000601 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000602 }
603
Reid Spencer74e032a2006-05-29 02:58:15 +0000604 // If the type had a name, make sure we recreate it.
605 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000606 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer07441662007-04-11 12:28:56 +0000607 if (progTypeName) {
Reid Spencer74e032a2006-05-29 02:58:15 +0000608 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000609 << typeName << ");";
610 nl(Out);
Reid Spencer07441662007-04-11 12:28:56 +0000611 }
Reid Spencer74e032a2006-05-29 02:58:15 +0000612
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000613 // Pop us off the type stack
614 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000615
616 // Indicate that this type is now defined.
617 DefinedTypes.insert(Ty);
618
619 // Early resolve as many unresolved types as possible. Search the unresolved
620 // types map for the type we just printed. Now that its definition is complete
621 // we can resolve any previous references to it. This prevents a cascade of
622 // unresolved types.
623 TypeMap::iterator I = UnresolvedTypes.find(Ty);
624 if (I != UnresolvedTypes.end()) {
625 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000626 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
627 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000628 Out << I->second << " = cast<";
629 switch (Ty->getTypeID()) {
630 case Type::FunctionTyID: Out << "FunctionType"; break;
631 case Type::ArrayTyID: Out << "ArrayType"; break;
632 case Type::StructTyID: Out << "StructType"; break;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000633 case Type::VectorTyID: Out << "VectorType"; break;
Reid Spencer15f7e012006-05-30 21:18:23 +0000634 case Type::PointerTyID: Out << "PointerType"; break;
635 case Type::OpaqueTyID: Out << "OpaqueType"; break;
636 default: Out << "NoSuchDerivedType"; break;
637 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000638 Out << ">(" << I->second << "_fwd.get());";
639 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000640 UnresolvedTypes.erase(I);
641 }
642
643 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000644 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000645
646 // We weren't a recursive type
647 return false;
648}
649
Reid Spencer12803f52006-05-31 17:31:38 +0000650// Prints a type definition. Returns true if it could not resolve all the types
651// in the definition but had to use a forward reference.
652void
653CppWriter::printType(const Type* Ty) {
654 assert(TypeStack.empty());
655 TypeStack.clear();
656 printTypeInternal(Ty);
657 assert(TypeStack.empty());
658}
659
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000660void
661CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000662
663 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000664 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
665 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
666 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000667
668 // For primitive types and types already defined, just add a name
669 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Chris Lattner42a75512007-01-15 02:27:26 +0000670 if (TI->second->isInteger() || TI->second->isPrimitiveType() ||
Reid Spencera54b7cb2007-01-12 07:05:14 +0000671 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000672 Out << "mod->addTypeName(\"";
673 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000674 Out << "\", " << getCppName(TI->second) << ");";
675 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000676 // For everything else, define the type
677 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000678 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000679 }
680 }
681
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000682 // Add all of the global variables to the value table...
683 for (Module::const_global_iterator I = TheModule->global_begin(),
684 E = TheModule->global_end(); I != E; ++I) {
685 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000686 printType(I->getInitializer()->getType());
687 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000688 }
689
690 // Add all the functions to the table
691 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
692 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000693 printType(FI->getReturnType());
694 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000695 // Add all the function arguments
696 for(Function::const_arg_iterator AI = FI->arg_begin(),
697 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000698 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000699 }
700
701 // Add all of the basic blocks and instructions
702 for (Function::const_iterator BB = FI->begin(),
703 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000704 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000705 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
706 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000707 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000708 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000709 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000710 }
711 }
712 }
713}
714
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000715
Reid Spencere0d133f2006-05-29 18:08:06 +0000716// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000717void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000718 // First, if the constant is actually a GlobalValue (variable or function) or
719 // its already in the constant list then we've printed it already and we can
720 // just return.
721 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000722 return;
723
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000724 std::string constName(getCppName(CV));
725 std::string typeName(getCppName(CV->getType()));
726 if (CV->isNullValue()) {
727 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000728 << typeName << ");";
729 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000730 return;
731 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000732 if (isa<GlobalValue>(CV)) {
733 // Skip variables and functions, we emit them elsewhere
734 return;
735 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000736 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4d26a062007-04-11 13:02:56 +0000737 Out << "ConstantInt* " << constName << " = ConstantInt::get(APInt("
738 << cast<IntegerType>(CI->getType())->getBitWidth() << ", "
Reid Spencer70297252007-03-01 20:55:43 +0000739 << " \"" << CI->getValue().toStringSigned(10) << "\", 10));";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000740 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000741 Out << "ConstantAggregateZero* " << constName
742 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000743 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000744 Out << "ConstantPointerNull* " << constName
745 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000746 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000747 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000748 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000749 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000750 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000751 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000752 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencerc38adbb2007-06-25 16:45:54 +0000753 std::string tmp = CA->getAsString();
754 bool nullTerminate = false;
755 if (tmp[tmp.length()-1] == 0) {
756 tmp.erase(tmp.length()-1);
757 nullTerminate = true;
758 }
759 printEscapedString(tmp);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000760 // Determine if we want null termination or not.
Reid Spencerc38adbb2007-06-25 16:45:54 +0000761 if (nullTerminate)
Reid Spencer15f7e012006-05-30 21:18:23 +0000762 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencerc38adbb2007-06-25 16:45:54 +0000763 else
764 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000765 Out << ");";
766 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000767 Out << "std::vector<Constant*> " << constName << "_elems;";
768 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000769 unsigned N = CA->getNumOperands();
770 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000771 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000772 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000773 << getCppName(CA->getOperand(i)) << ");";
774 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000775 }
776 Out << "Constant* " << constName << " = ConstantArray::get("
777 << typeName << ", " << constName << "_elems);";
778 }
779 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000780 Out << "std::vector<Constant*> " << constName << "_fields;";
781 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000782 unsigned N = CS->getNumOperands();
783 for (unsigned i = 0; i < N; i++) {
784 printConstant(CS->getOperand(i));
785 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000786 << getCppName(CS->getOperand(i)) << ");";
787 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000788 }
789 Out << "Constant* " << constName << " = ConstantStruct::get("
790 << typeName << ", " << constName << "_fields);";
Reid Spencer9d6565a2007-02-15 02:26:10 +0000791 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000792 Out << "std::vector<Constant*> " << constName << "_elems;";
793 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000794 unsigned N = CP->getNumOperands();
795 for (unsigned i = 0; i < N; ++i) {
796 printConstant(CP->getOperand(i));
797 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000798 << getCppName(CP->getOperand(i)) << ");";
799 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000800 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000801 Out << "Constant* " << constName << " = ConstantVector::get("
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000802 << typeName << ", " << constName << "_elems);";
803 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000804 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000805 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000806 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000807 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000808 Out << "std::vector<Constant*> " << constName << "_indices;";
809 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000810 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000811 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000812 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000813 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000814 << getCppName(CE->getOperand(i)) << ");";
815 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000816 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000817 Out << "Constant* " << constName
818 << " = ConstantExpr::getGetElementPtr("
819 << getCppName(CE->getOperand(0)) << ", "
David Greene418a04e2007-09-04 17:15:07 +0000820 << "&" << constName << "_indices[0], "
821 << constName << "_indices.size()"
Reid Spencer07441662007-04-11 12:28:56 +0000822 << " );";
Reid Spencer3da59db2006-11-27 01:05:10 +0000823 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000824 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000825 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000826 switch (CE->getOpcode()) {
827 default: assert(0 && "Invalid cast opcode");
828 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
829 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
830 case Instruction::SExt: Out << "Instruction::SExt"; break;
831 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
832 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
833 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
834 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
835 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
836 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
837 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
838 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
839 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
840 }
841 Out << ", " << getCppName(CE->getOperand(0)) << ", "
842 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000843 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000844 unsigned N = CE->getNumOperands();
845 for (unsigned i = 0; i < N; ++i ) {
846 printConstant(CE->getOperand(i));
847 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000848 Out << "Constant* " << constName << " = ConstantExpr::";
849 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000850 case Instruction::Add: Out << "getAdd("; break;
851 case Instruction::Sub: Out << "getSub("; break;
852 case Instruction::Mul: Out << "getMul("; break;
853 case Instruction::UDiv: Out << "getUDiv("; break;
854 case Instruction::SDiv: Out << "getSDiv("; break;
855 case Instruction::FDiv: Out << "getFDiv("; break;
856 case Instruction::URem: Out << "getURem("; break;
857 case Instruction::SRem: Out << "getSRem("; break;
858 case Instruction::FRem: Out << "getFRem("; break;
859 case Instruction::And: Out << "getAnd("; break;
860 case Instruction::Or: Out << "getOr("; break;
861 case Instruction::Xor: Out << "getXor("; break;
862 case Instruction::ICmp:
863 Out << "getICmp(ICmpInst::ICMP_";
864 switch (CE->getPredicate()) {
865 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
866 case ICmpInst::ICMP_NE: Out << "NE"; break;
867 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
868 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
869 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
870 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
871 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
872 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
873 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
874 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
875 default: error("Invalid ICmp Predicate");
876 }
877 break;
878 case Instruction::FCmp:
879 Out << "getFCmp(FCmpInst::FCMP_";
880 switch (CE->getPredicate()) {
881 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
882 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
883 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
884 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
885 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
886 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
887 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
888 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
889 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
890 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
891 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
892 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
893 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
894 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
895 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
896 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
897 default: error("Invalid FCmp Predicate");
898 }
899 break;
900 case Instruction::Shl: Out << "getShl("; break;
901 case Instruction::LShr: Out << "getLShr("; break;
902 case Instruction::AShr: Out << "getAShr("; break;
903 case Instruction::Select: Out << "getSelect("; break;
904 case Instruction::ExtractElement: Out << "getExtractElement("; break;
905 case Instruction::InsertElement: Out << "getInsertElement("; break;
906 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000907 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000908 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000909 break;
910 }
911 Out << getCppName(CE->getOperand(0));
912 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
913 Out << ", " << getCppName(CE->getOperand(i));
914 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000915 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000916 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000917 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000918 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000919 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000920 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000921}
922
Reid Spencer12803f52006-05-31 17:31:38 +0000923void
924CppWriter::printConstants(const Module* M) {
925 // Traverse all the global variables looking for constant initializers
926 for (Module::const_global_iterator I = TheModule->global_begin(),
927 E = TheModule->global_end(); I != E; ++I)
928 if (I->hasInitializer())
929 printConstant(I->getInitializer());
930
931 // Traverse the LLVM functions looking for constants
932 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
933 FI != FE; ++FI) {
934 // Add all of the basic blocks and instructions
935 for (Function::const_iterator BB = FI->begin(),
936 E = FI->end(); BB != E; ++BB) {
937 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
938 ++I) {
939 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
940 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
941 printConstant(C);
942 }
943 }
944 }
945 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000946 }
Reid Spencer66c87342006-05-30 03:43:49 +0000947}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000948
Reid Spencer12803f52006-05-31 17:31:38 +0000949void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000950 nl(Out) << "// Type Definitions";
951 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000952 printType(GV->getType());
953 if (GV->hasInitializer()) {
954 Constant* Init = GV->getInitializer();
955 printType(Init->getType());
956 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000957 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000958 printFunctionHead(F);
959 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000960 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000961 printVariableHead(gv);
962 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000963 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000964 printConstant(gv);
965 }
966 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000967 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000968 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000969 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000970 }
Reid Spencer12803f52006-05-31 17:31:38 +0000971}
Reid Spencer15f7e012006-05-30 21:18:23 +0000972
Reid Spencer12803f52006-05-31 17:31:38 +0000973void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000974 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000975 if (is_inline) {
976 Out << " = mod->getGlobalVariable(";
977 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000978 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
979 nl(Out) << "if (!" << getCppName(GV) << ") {";
980 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000981 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000982 Out << " = new GlobalVariable(";
983 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000984 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000985 Out << ",";
986 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
987 Out << ",";
988 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000989 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000990 Out << ",";
991 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000992 if (GV->hasInitializer()) {
993 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000994 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000995 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000996 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000997 Out << "\",";
998 nl(Out) << "mod);";
999 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001000
1001 if (GV->hasSection()) {
1002 printCppName(GV);
1003 Out << "->setSection(\"";
1004 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001005 Out << "\");";
1006 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001007 }
1008 if (GV->getAlignment()) {
1009 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001010 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
1011 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001012 };
Duncan Sandsdc024672007-11-27 13:23:08 +00001013 if (GV->getVisibility() != GlobalValue::DefaultVisibility) {
1014 printCppName(GV);
1015 Out << "->setVisibility(";
1016 printVisibilityType(GV->getVisibility());
1017 Out << ");";
1018 nl(Out);
1019 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001020 if (is_inline) {
1021 out(); Out << "}"; nl(Out);
1022 }
Reid Spencer12803f52006-05-31 17:31:38 +00001023}
1024
1025void
1026CppWriter::printVariableBody(const GlobalVariable *GV) {
1027 if (GV->hasInitializer()) {
1028 printCppName(GV);
1029 Out << "->setInitializer(";
1030 //if (!isa<GlobalValue(GV->getInitializer()))
1031 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001032 Out << getCppName(GV->getInitializer()) << ");";
1033 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001034 }
1035}
1036
1037std::string
1038CppWriter::getOpName(Value* V) {
1039 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
1040 return getCppName(V);
1041
1042 // See if its alread in the map of forward references, if so just return the
1043 // name we already set up for it
1044 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
1045 if (I != ForwardRefs.end())
1046 return I->second;
1047
1048 // This is a new forward reference. Generate a unique name for it
1049 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
1050
1051 // Yes, this is a hack. An Argument is the smallest instantiable value that
1052 // we can make as a placeholder for the real value. We'll replace these
1053 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001054 Out << "Argument* " << result << " = new Argument("
1055 << getCppName(V->getType()) << ");";
1056 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001057 ForwardRefs[V] = result;
1058 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001059}
1060
Reid Spencere0d133f2006-05-29 18:08:06 +00001061// printInstruction - This member is called for each Instruction in a function.
1062void
Reid Spencer12803f52006-05-31 17:31:38 +00001063CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +00001064 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001065
Reid Spencer15f7e012006-05-30 21:18:23 +00001066 // Before we emit this instruction, we need to take care of generating any
1067 // forward references. So, we get the names of all the operands in advance
1068 std::string* opNames = new std::string[I->getNumOperands()];
1069 for (unsigned i = 0; i < I->getNumOperands(); i++) {
1070 opNames[i] = getOpName(I->getOperand(i));
1071 }
1072
Reid Spencere0d133f2006-05-29 18:08:06 +00001073 switch (I->getOpcode()) {
1074 case Instruction::Ret: {
1075 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001076 Out << "new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001077 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001078 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001079 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001080 case Instruction::Br: {
1081 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer07441662007-04-11 12:28:56 +00001082 Out << "new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001083 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001084 Out << opNames[0] << ", "
1085 << opNames[1] << ", "
1086 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001087
Reid Spencere0d133f2006-05-29 18:08:06 +00001088 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001089 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001090 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001091 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001092 }
1093 Out << bbname << ");";
1094 break;
1095 }
Reid Spencer66c87342006-05-30 03:43:49 +00001096 case Instruction::Switch: {
1097 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001098 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001099 << opNames[0] << ", "
1100 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001101 << sw->getNumCases() << ", " << bbname << ");";
1102 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001103 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001104 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001105 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001106 << opNames[i+1] << ");";
1107 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001108 }
1109 break;
1110 }
1111 case Instruction::Invoke: {
1112 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001113 Out << "std::vector<Value*> " << iName << "_params;";
1114 nl(Out);
1115 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1116 Out << iName << "_params.push_back("
1117 << opNames[i] << ");";
1118 nl(Out);
1119 }
Reid Spencer07441662007-04-11 12:28:56 +00001120 Out << "InvokeInst *" << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001121 << opNames[0] << ", "
1122 << opNames[1] << ", "
1123 << opNames[2] << ", "
David Greenef1355a52007-08-27 19:04:21 +00001124 << iName << "_params.begin(), " << iName << "_params.end(), \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001125 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001126 Out << "\", " << bbname << ");";
1127 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001128 printCallingConv(inv->getCallingConv());
1129 Out << ");";
Duncan Sandsdc024672007-11-27 13:23:08 +00001130 printParamAttrs(inv->getParamAttrs(), iName);
1131 Out << iName << "->setParamAttrs(" << iName << "_PAL);";
1132 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001133 break;
1134 }
1135 case Instruction::Unwind: {
Reid Spencer07441662007-04-11 12:28:56 +00001136 Out << "new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001137 << bbname << ");";
1138 break;
1139 }
1140 case Instruction::Unreachable:{
Reid Spencer07441662007-04-11 12:28:56 +00001141 Out << "new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001142 << bbname << ");";
1143 break;
1144 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001145 case Instruction::Add:
1146 case Instruction::Sub:
1147 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001148 case Instruction::UDiv:
1149 case Instruction::SDiv:
1150 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001151 case Instruction::URem:
1152 case Instruction::SRem:
1153 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001154 case Instruction::And:
1155 case Instruction::Or:
1156 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001157 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001158 case Instruction::LShr:
1159 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001160 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001161 switch (I->getOpcode()) {
1162 case Instruction::Add: Out << "Instruction::Add"; break;
1163 case Instruction::Sub: Out << "Instruction::Sub"; break;
1164 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001165 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1166 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1167 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001168 case Instruction::URem:Out << "Instruction::URem"; break;
1169 case Instruction::SRem:Out << "Instruction::SRem"; break;
1170 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001171 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001172 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001173 case Instruction::Xor: Out << "Instruction::Xor"; break;
1174 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001175 case Instruction::LShr:Out << "Instruction::LShr"; break;
1176 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001177 default: Out << "Instruction::BadOpCode"; break;
1178 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001179 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001180 printEscapedString(I->getName());
1181 Out << "\", " << bbname << ");";
1182 break;
1183 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001184 case Instruction::FCmp: {
1185 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1186 switch (cast<FCmpInst>(I)->getPredicate()) {
1187 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1188 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1189 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1190 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1191 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1192 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1193 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1194 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1195 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1196 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1197 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1198 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1199 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1200 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1201 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1202 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1203 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1204 }
1205 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1206 printEscapedString(I->getName());
1207 Out << "\", " << bbname << ");";
1208 break;
1209 }
1210 case Instruction::ICmp: {
1211 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1212 switch (cast<ICmpInst>(I)->getPredicate()) {
1213 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1214 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1215 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1216 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1217 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1218 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1219 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1220 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1221 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1222 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1223 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001224 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001225 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001226 printEscapedString(I->getName());
1227 Out << "\", " << bbname << ");";
1228 break;
1229 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001230 case Instruction::Malloc: {
1231 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001232 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001233 << getCppName(mallocI->getAllocatedType()) << ", ";
1234 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001235 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001236 Out << "\"";
1237 printEscapedString(mallocI->getName());
1238 Out << "\", " << bbname << ");";
1239 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001240 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001241 << mallocI->getAlignment() << ");";
1242 break;
1243 }
Reid Spencer66c87342006-05-30 03:43:49 +00001244 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001245 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001246 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1247 break;
1248 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001249 case Instruction::Alloca: {
1250 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001251 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001252 << getCppName(allocaI->getAllocatedType()) << ", ";
1253 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001254 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001255 Out << "\"";
1256 printEscapedString(allocaI->getName());
1257 Out << "\", " << bbname << ");";
1258 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001259 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001260 << allocaI->getAlignment() << ");";
1261 break;
1262 }
Reid Spencer66c87342006-05-30 03:43:49 +00001263 case Instruction::Load:{
1264 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001265 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001266 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001267 printEscapedString(load->getName());
1268 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001269 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001270 break;
1271 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001272 case Instruction::Store: {
1273 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001274 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001275 << opNames[0] << ", "
1276 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001277 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001278 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001279 break;
1280 }
1281 case Instruction::GetElementPtr: {
1282 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1283 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001284 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001285 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001286 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001287 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001288 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001289 Out << "std::vector<Value*> " << iName << "_indices;";
1290 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001291 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001292 Out << iName << "_indices.push_back("
1293 << opNames[i] << ");";
1294 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001295 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001296 Out << "Instruction* " << iName << " = new GetElementPtrInst("
David Greeneb8f74792007-09-04 15:46:09 +00001297 << opNames[0] << ", " << iName << "_indices.begin(), "
1298 << iName << "_indices.end()";
Reid Spencere0d133f2006-05-29 18:08:06 +00001299 }
1300 Out << ", \"";
1301 printEscapedString(gep->getName());
1302 Out << "\", " << bbname << ");";
1303 break;
1304 }
Reid Spencer66c87342006-05-30 03:43:49 +00001305 case Instruction::PHI: {
1306 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001307
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001308 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001309 << getCppName(phi->getType()) << ", \"";
1310 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001311 Out << "\", " << bbname << ");";
1312 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001313 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001314 << ");";
1315 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001316 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001317 Out << iName << "->addIncoming("
1318 << opNames[i] << ", " << opNames[i+1] << ");";
1319 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001320 }
Reid Spencer66c87342006-05-30 03:43:49 +00001321 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001322 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001323 case Instruction::Trunc:
1324 case Instruction::ZExt:
1325 case Instruction::SExt:
1326 case Instruction::FPTrunc:
1327 case Instruction::FPExt:
1328 case Instruction::FPToUI:
1329 case Instruction::FPToSI:
1330 case Instruction::UIToFP:
1331 case Instruction::SIToFP:
1332 case Instruction::PtrToInt:
1333 case Instruction::IntToPtr:
1334 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001335 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001336 Out << "CastInst* " << iName << " = new ";
1337 switch (I->getOpcode()) {
Reid Spencer34816572007-02-16 06:34:39 +00001338 case Instruction::Trunc: Out << "TruncInst"; break;
1339 case Instruction::ZExt: Out << "ZExtInst"; break;
1340 case Instruction::SExt: Out << "SExtInst"; break;
1341 case Instruction::FPTrunc: Out << "FPTruncInst"; break;
1342 case Instruction::FPExt: Out << "FPExtInst"; break;
1343 case Instruction::FPToUI: Out << "FPToUIInst"; break;
1344 case Instruction::FPToSI: Out << "FPToSIInst"; break;
1345 case Instruction::UIToFP: Out << "UIToFPInst"; break;
1346 case Instruction::SIToFP: Out << "SIToFPInst"; break;
Reid Spencer07441662007-04-11 12:28:56 +00001347 case Instruction::PtrToInt: Out << "PtrToIntInst"; break;
Reid Spencer34816572007-02-16 06:34:39 +00001348 case Instruction::IntToPtr: Out << "IntToPtrInst"; break;
1349 case Instruction::BitCast: Out << "BitCastInst"; break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001350 default: assert(!"Unreachable"); break;
1351 }
1352 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001353 << getCppName(cst->getType()) << ", \"";
1354 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001355 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001356 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001357 }
Reid Spencer66c87342006-05-30 03:43:49 +00001358 case Instruction::Call:{
1359 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001360 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001361 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001362 << getCppName(ila->getFunctionType()) << ", \""
1363 << ila->getAsmString() << "\", \""
1364 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001365 << (ila->hasSideEffects() ? "true" : "false") << ");";
1366 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001367 }
Reid Spencer22256f42007-08-02 03:30:26 +00001368 if (call->getNumOperands() > 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001369 Out << "std::vector<Value*> " << iName << "_params;";
1370 nl(Out);
1371 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1372 Out << iName << "_params.push_back(" << opNames[i] << ");";
1373 nl(Out);
1374 }
1375 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer22256f42007-08-02 03:30:26 +00001376 << opNames[0] << ", " << iName << "_params.begin(), "
1377 << iName << "_params.end(), \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001378 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001379 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001380 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001381 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001382 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001383 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001384 }
1385 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001386 Out << "\", " << bbname << ");";
1387 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001388 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001389 Out << ");";
1390 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001391 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001392 Out << ");";
Duncan Sandsdc024672007-11-27 13:23:08 +00001393 printParamAttrs(call->getParamAttrs(), iName);
1394 Out << iName << "->setParamAttrs(" << iName << "_PAL);";
1395 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001396 break;
1397 }
1398 case Instruction::Select: {
1399 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001400 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001401 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001402 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001403 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001404 break;
1405 }
1406 case Instruction::UserOp1:
1407 /// FALL THROUGH
1408 case Instruction::UserOp2: {
1409 /// FIXME: What should be done here?
1410 break;
1411 }
1412 case Instruction::VAArg: {
1413 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001414 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001415 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001416 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001417 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001418 break;
1419 }
1420 case Instruction::ExtractElement: {
1421 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001422 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001423 << " = new ExtractElementInst(" << opNames[0]
1424 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001425 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001426 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001427 break;
1428 }
1429 case Instruction::InsertElement: {
1430 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001431 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001432 << " = new InsertElementInst(" << opNames[0]
1433 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001434 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001435 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001436 break;
1437 }
1438 case Instruction::ShuffleVector: {
1439 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001440 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001441 << " = new ShuffleVectorInst(" << opNames[0]
1442 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001443 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001444 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001445 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001446 }
1447 }
Reid Spencer68464b72006-06-15 16:09:59 +00001448 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001449 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001450 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001451}
1452
Reid Spencer12803f52006-05-31 17:31:38 +00001453// Print out the types, constants and declarations needed by one function
1454void CppWriter::printFunctionUses(const Function* F) {
1455
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001456 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001457 if (!is_inline) {
1458 // Print the function's return type
1459 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001460
Reid Spencerf977e7b2006-06-01 23:43:47 +00001461 // Print the function's function type
1462 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001463
Reid Spencerf977e7b2006-06-01 23:43:47 +00001464 // Print the types of each of the function's arguments
1465 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1466 AI != AE; ++AI) {
1467 printType(AI->getType());
1468 }
Reid Spencer12803f52006-05-31 17:31:38 +00001469 }
1470
1471 // Print type definitions for every type referenced by an instruction and
1472 // make a note of any global values or constants that are referenced
Reid Spencera4d71f02007-06-16 20:48:27 +00001473 SmallPtrSet<GlobalValue*,64> gvs;
1474 SmallPtrSet<Constant*,64> consts;
Reid Spencer12803f52006-05-31 17:31:38 +00001475 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1476 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1477 I != E; ++I) {
1478 // Print the type of the instruction itself
1479 printType(I->getType());
1480
1481 // Print the type of each of the instruction's operands
1482 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1483 Value* operand = I->getOperand(i);
1484 printType(operand->getType());
Reid Spencerab24b4c2007-06-16 20:33:24 +00001485
1486 // If the operand references a GVal or Constant, make a note of it
1487 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) {
Reid Spencera4d71f02007-06-16 20:48:27 +00001488 gvs.insert(GV);
Reid Spencerab24b4c2007-06-16 20:33:24 +00001489 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
1490 if (GVar->hasInitializer())
Reid Spencera4d71f02007-06-16 20:48:27 +00001491 consts.insert(GVar->getInitializer());
Reid Spencerab24b4c2007-06-16 20:33:24 +00001492 } else if (Constant* C = dyn_cast<Constant>(operand))
Reid Spencera4d71f02007-06-16 20:48:27 +00001493 consts.insert(C);
Reid Spencer12803f52006-05-31 17:31:38 +00001494 }
1495 }
1496 }
1497
1498 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001499 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001500 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001501 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001502 if (Function* Fun = dyn_cast<Function>(*I)) {
1503 if (!is_inline || Fun != F)
1504 printFunctionHead(Fun);
1505 }
Reid Spencer12803f52006-05-31 17:31:38 +00001506 }
1507
1508 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001509 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001510 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001511 I != E; ++I) {
1512 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1513 printVariableHead(F);
1514 }
1515
1516 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001517 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001518 for (SmallPtrSet<Constant*,64>::iterator I = consts.begin(), E = consts.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001519 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001520 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001521 }
1522
1523 // Process the global variables definitions now that all the constants have
1524 // been emitted. These definitions just couple the gvars with their constant
1525 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001526 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencera4d71f02007-06-16 20:48:27 +00001527 for (SmallPtrSet<GlobalValue*,64>::iterator I = gvs.begin(), E = gvs.end();
Reid Spencer12803f52006-05-31 17:31:38 +00001528 I != E; ++I) {
1529 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1530 printVariableBody(GV);
1531 }
1532}
1533
1534void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001535 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001536 if (is_inline) {
1537 Out << " = mod->getFunction(\"";
1538 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001539 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1540 nl(Out) << "if (!" << getCppName(F) << ") {";
1541 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001542 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001543 Out<< " = new Function(";
1544 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1545 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001546 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001547 Out << ",";
1548 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001549 printEscapedString(F->getName());
Reid Spencer5cbf9852007-01-30 20:08:39 +00001550 Out << "\", mod); " << (F->isDeclaration()? "// (external, no body)" : "");
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001551 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001552 printCppName(F);
1553 Out << "->setCallingConv(";
1554 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001555 Out << ");";
1556 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001557 if (F->hasSection()) {
1558 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001559 Out << "->setSection(\"" << F->getSection() << "\");";
1560 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001561 }
1562 if (F->getAlignment()) {
1563 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001564 Out << "->setAlignment(" << F->getAlignment() << ");";
1565 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001566 }
Duncan Sandsdc024672007-11-27 13:23:08 +00001567 if (F->getVisibility() != GlobalValue::DefaultVisibility) {
1568 printCppName(F);
1569 Out << "->setVisibility(";
1570 printVisibilityType(F->getVisibility());
1571 Out << ");";
1572 nl(Out);
1573 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001574 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001575 Out << "}";
1576 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001577 }
Duncan Sandsdc024672007-11-27 13:23:08 +00001578 printParamAttrs(F->getParamAttrs(), getCppName(F));
1579 printCppName(F);
1580 Out << "->setParamAttrs(" << getCppName(F) << "_PAL);";
1581 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001582}
1583
1584void CppWriter::printFunctionBody(const Function *F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001585 if (F->isDeclaration())
Reid Spencer12803f52006-05-31 17:31:38 +00001586 return; // external functions have no bodies.
1587
1588 // Clear the DefinedValues and ForwardRefs maps because we can't have
1589 // cross-function forward refs
1590 ForwardRefs.clear();
1591 DefinedValues.clear();
1592
1593 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001594 if (!is_inline) {
1595 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001596 Out << "Function::arg_iterator args = " << getCppName(F)
1597 << "->arg_begin();";
1598 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001599 }
1600 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1601 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001602 Out << "Value* " << getCppName(AI) << " = args++;";
1603 nl(Out);
1604 if (AI->hasName()) {
1605 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1606 nl(Out);
1607 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001608 }
Reid Spencer12803f52006-05-31 17:31:38 +00001609 }
1610
1611 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001612 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001613 for (Function::const_iterator BI = F->begin(), BE = F->end();
1614 BI != BE; ++BI) {
1615 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001616 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001617 if (BI->hasName())
1618 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001619 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1620 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001621 }
1622
1623 // Output all of its basic blocks... for the function
1624 for (Function::const_iterator BI = F->begin(), BE = F->end();
1625 BI != BE; ++BI) {
1626 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001627 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1628 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001629
1630 // Output all of the instructions in the basic block...
1631 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1632 I != E; ++I) {
1633 printInstruction(I,bbname);
1634 }
1635 }
1636
1637 // Loop over the ForwardRefs and resolve them now that all instructions
1638 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001639 if (!ForwardRefs.empty()) {
1640 nl(Out) << "// Resolve Forward References";
1641 nl(Out);
1642 }
1643
Reid Spencer12803f52006-05-31 17:31:38 +00001644 while (!ForwardRefs.empty()) {
1645 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001646 Out << I->second << "->replaceAllUsesWith("
1647 << getCppName(I->first) << "); delete " << I->second << ";";
1648 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001649 ForwardRefs.erase(I);
1650 }
1651}
1652
Reid Spencerf977e7b2006-06-01 23:43:47 +00001653void CppWriter::printInline(const std::string& fname, const std::string& func) {
Reid Spencer688b0492007-02-05 21:19:13 +00001654 const Function* F = TheModule->getFunction(func);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001655 if (!F) {
1656 error(std::string("Function '") + func + "' not found in input module");
1657 return;
1658 }
Reid Spencer5cbf9852007-01-30 20:08:39 +00001659 if (F->isDeclaration()) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001660 error(std::string("Function '") + func + "' is external!");
1661 return;
1662 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001663 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001664 << getCppName(F);
1665 unsigned arg_count = 1;
1666 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1667 AI != AE; ++AI) {
1668 Out << ", Value* arg_" << arg_count;
1669 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001670 Out << ") {";
1671 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001672 is_inline = true;
1673 printFunctionUses(F);
1674 printFunctionBody(F);
1675 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001676 Out << "return " << getCppName(F->begin()) << ";";
1677 nl(Out) << "}";
1678 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001679}
1680
Reid Spencer12803f52006-05-31 17:31:38 +00001681void CppWriter::printModuleBody() {
1682 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001683 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001684 printTypes(TheModule);
1685
1686 // Functions can call each other and global variables can reference them so
1687 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001688 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001689 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1690 I != E; ++I)
1691 printFunctionHead(I);
1692
1693 // Process the global variables declarations. We can't initialze them until
1694 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001695 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001696 for (Module::const_global_iterator I = TheModule->global_begin(),
1697 E = TheModule->global_end(); I != E; ++I) {
1698 printVariableHead(I);
1699 }
1700
1701 // Print out all the constants definitions. Constants don't recurse except
1702 // through GlobalValues. All GlobalValues have been declared at this point
1703 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001704 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001705 printConstants(TheModule);
1706
1707 // Process the global variables definitions now that all the constants have
1708 // been emitted. These definitions just couple the gvars with their constant
1709 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001710 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001711 for (Module::const_global_iterator I = TheModule->global_begin(),
1712 E = TheModule->global_end(); I != E; ++I) {
1713 printVariableBody(I);
1714 }
1715
1716 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001717 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001718 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1719 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001720 if (!I->isDeclaration()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001721 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1722 << ")";
1723 nl(Out) << "{";
1724 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001725 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001726 nl(Out,-1) << "}";
1727 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001728 }
1729 }
1730}
1731
1732void CppWriter::printProgram(
1733 const std::string& fname,
1734 const std::string& mName
1735) {
1736 Out << "#include <llvm/Module.h>\n";
1737 Out << "#include <llvm/DerivedTypes.h>\n";
1738 Out << "#include <llvm/Constants.h>\n";
1739 Out << "#include <llvm/GlobalVariable.h>\n";
1740 Out << "#include <llvm/Function.h>\n";
1741 Out << "#include <llvm/CallingConv.h>\n";
1742 Out << "#include <llvm/BasicBlock.h>\n";
1743 Out << "#include <llvm/Instructions.h>\n";
1744 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencer07441662007-04-11 12:28:56 +00001745 Out << "#include <llvm/ParameterAttributes.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001746 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001747 Out << "#include <llvm/Pass.h>\n";
1748 Out << "#include <llvm/PassManager.h>\n";
1749 Out << "#include <llvm/Analysis/Verifier.h>\n";
1750 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1751 Out << "#include <algorithm>\n";
1752 Out << "#include <iostream>\n\n";
1753 Out << "using namespace llvm;\n\n";
1754 Out << "Module* " << fname << "();\n\n";
1755 Out << "int main(int argc, char**argv) {\n";
Nick Lewycky8946fe12007-06-16 16:17:35 +00001756 Out << " Module* Mod = " << fname << "();\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001757 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1758 Out << " std::cerr.flush();\n";
1759 Out << " std::cout.flush();\n";
1760 Out << " PassManager PM;\n";
Reid Spencer07441662007-04-11 12:28:56 +00001761 Out << " PM.add(new PrintModulePass(&llvm::cout));\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001762 Out << " PM.run(*Mod);\n";
1763 Out << " return 0;\n";
1764 Out << "}\n\n";
1765 printModule(fname,mName);
1766}
1767
1768void CppWriter::printModule(
1769 const std::string& fname,
1770 const std::string& mName
1771) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001772 nl(Out) << "Module* " << fname << "() {";
1773 nl(Out,1) << "// Module Construction";
1774 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001775 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001776 nl(Out) << "mod->setDataLayout(\"" << TheModule->getDataLayout() << "\");";
1777 }
1778 if (!TheModule->getTargetTriple().empty()) {
1779 nl(Out) << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
1780 << "\");";
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001781 }
Reid Spencer12803f52006-05-31 17:31:38 +00001782
1783 if (!TheModule->getModuleInlineAsm().empty()) {
Reid Spencer07441662007-04-11 12:28:56 +00001784 nl(Out) << "mod->setModuleInlineAsm(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001785 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001786 Out << "\");";
Reid Spencer12803f52006-05-31 17:31:38 +00001787 }
Reid Spencer07441662007-04-11 12:28:56 +00001788 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001789
1790 // Loop over the dependent libraries and emit them.
1791 Module::lib_iterator LI = TheModule->lib_begin();
1792 Module::lib_iterator LE = TheModule->lib_end();
1793 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001794 Out << "mod->addLibrary(\"" << *LI << "\");";
1795 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001796 ++LI;
1797 }
1798 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001799 nl(Out) << "return mod;";
1800 nl(Out,-1) << "}";
1801 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001802}
1803
1804void CppWriter::printContents(
1805 const std::string& fname, // Name of generated function
1806 const std::string& mName // Name of module generated module
1807) {
1808 Out << "\nModule* " << fname << "(Module *mod) {\n";
1809 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001810 printModuleBody();
1811 Out << "\nreturn mod;\n";
1812 Out << "\n}\n";
1813}
1814
1815void CppWriter::printFunction(
1816 const std::string& fname, // Name of generated function
1817 const std::string& funcName // Name of function to generate
1818) {
Reid Spencer688b0492007-02-05 21:19:13 +00001819 const Function* F = TheModule->getFunction(funcName);
Reid Spencer12803f52006-05-31 17:31:38 +00001820 if (!F) {
1821 error(std::string("Function '") + funcName + "' not found in input module");
1822 return;
1823 }
1824 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1825 printFunctionUses(F);
1826 printFunctionHead(F);
1827 printFunctionBody(F);
1828 Out << "return " << getCppName(F) << ";\n";
1829 Out << "}\n";
1830}
1831
Chris Lattner120119d2007-11-13 18:22:33 +00001832void CppWriter::printFunctions() {
1833 const Module::FunctionListType &funcs = TheModule->getFunctionList();
1834 Module::const_iterator I = funcs.begin();
1835 Module::const_iterator IE = funcs.end();
1836
1837 for (; I != IE; ++I) {
1838 const Function &func = *I;
1839 if (!func.isDeclaration()) {
1840 std::string name("define_");
1841 name += func.getName();
1842 printFunction(name, func.getName());
1843 }
1844 }
1845}
1846
Reid Spencer12803f52006-05-31 17:31:38 +00001847void CppWriter::printVariable(
1848 const std::string& fname, /// Name of generated function
1849 const std::string& varName // Name of variable to generate
1850) {
1851 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1852
1853 if (!GV) {
1854 error(std::string("Variable '") + varName + "' not found in input module");
1855 return;
1856 }
1857 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1858 printVariableUses(GV);
1859 printVariableHead(GV);
1860 printVariableBody(GV);
1861 Out << "return " << getCppName(GV) << ";\n";
1862 Out << "}\n";
1863}
1864
1865void CppWriter::printType(
1866 const std::string& fname, /// Name of generated function
1867 const std::string& typeName // Name of type to generate
1868) {
1869 const Type* Ty = TheModule->getTypeByName(typeName);
1870 if (!Ty) {
1871 error(std::string("Type '") + typeName + "' not found in input module");
1872 return;
1873 }
1874 Out << "\nType* " << fname << "(Module *mod) {\n";
1875 printType(Ty);
1876 Out << "return " << getCppName(Ty) << ";\n";
1877 Out << "}\n";
1878}
1879
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001880} // end anonymous llvm
1881
1882namespace llvm {
1883
1884void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001885 // Initialize a CppWriter for us to use
1886 CppWriter W(o, mod);
1887
1888 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001889 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001890
1891 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001892 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001893
1894 // Get the name of the thing we are to generate
1895 std::string tgtname = NameToGenerate.getValue();
1896 if (GenerationType == GenModule ||
1897 GenerationType == GenContents ||
Chris Lattner120119d2007-11-13 18:22:33 +00001898 GenerationType == GenProgram ||
1899 GenerationType == GenFunctions) {
Reid Spencer12803f52006-05-31 17:31:38 +00001900 if (tgtname == "!bad!") {
1901 if (mod->getModuleIdentifier() == "-")
1902 tgtname = "<stdin>";
1903 else
1904 tgtname = mod->getModuleIdentifier();
1905 }
1906 } else if (tgtname == "!bad!") {
1907 W.error("You must use the -for option with -gen-{function,variable,type}");
1908 }
1909
1910 switch (WhatToGenerate(GenerationType)) {
1911 case GenProgram:
1912 if (fname.empty())
1913 fname = "makeLLVMModule";
1914 W.printProgram(fname,tgtname);
1915 break;
1916 case GenModule:
1917 if (fname.empty())
1918 fname = "makeLLVMModule";
1919 W.printModule(fname,tgtname);
1920 break;
1921 case GenContents:
1922 if (fname.empty())
1923 fname = "makeLLVMModuleContents";
1924 W.printContents(fname,tgtname);
1925 break;
1926 case GenFunction:
1927 if (fname.empty())
1928 fname = "makeLLVMFunction";
1929 W.printFunction(fname,tgtname);
1930 break;
Chris Lattner120119d2007-11-13 18:22:33 +00001931 case GenFunctions:
1932 W.printFunctions();
1933 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001934 case GenInline:
1935 if (fname.empty())
1936 fname = "makeLLVMInline";
1937 W.printInline(fname,tgtname);
1938 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001939 case GenVariable:
1940 if (fname.empty())
1941 fname = "makeLLVMVariable";
1942 W.printVariable(fname,tgtname);
1943 break;
1944 case GenType:
1945 if (fname.empty())
1946 fname = "makeLLVMType";
1947 W.printType(fname,tgtname);
1948 break;
1949 default:
1950 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001951 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001952}
1953
1954}