blob: 82997cebcc5057b935802584663c8def9bffbeef [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"
21#include "llvm/Module.h"
22#include "llvm/SymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000023#include "llvm/TypeSymbolTable.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000024#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/STLExtras.h"
Reid Spencer15f7e012006-05-30 21:18:23 +000026#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000027#include "llvm/Support/CFG.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/MathExtras.h"
Reid Spencerf977e7b2006-06-01 23:43:47 +000030#include "llvm/Config/config.h"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000031#include <algorithm>
32#include <iostream>
Reid Spencer66c87342006-05-30 03:43:49 +000033#include <set>
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000034
35using namespace llvm;
36
Reid Spencer15f7e012006-05-30 21:18:23 +000037static cl::opt<std::string>
Reid Spencer15f7e012006-05-30 21:18:23 +000038FuncName("funcname", cl::desc("Specify the name of the generated function"),
39 cl::value_desc("function name"));
40
Reid Spencer12803f52006-05-31 17:31:38 +000041enum WhatToGenerate {
42 GenProgram,
43 GenModule,
44 GenContents,
45 GenFunction,
Reid Spencerf977e7b2006-06-01 23:43:47 +000046 GenInline,
Reid Spencer12803f52006-05-31 17:31:38 +000047 GenVariable,
48 GenType
49};
50
51static cl::opt<WhatToGenerate> GenerationType(cl::Optional,
52 cl::desc("Choose what kind of output to generate"),
53 cl::init(GenProgram),
54 cl::values(
55 clEnumValN(GenProgram, "gen-program", "Generate a complete program"),
56 clEnumValN(GenModule, "gen-module", "Generate a module definition"),
57 clEnumValN(GenContents,"gen-contents", "Generate contents of a module"),
58 clEnumValN(GenFunction,"gen-function", "Generate a function definition"),
Reid Spencerf977e7b2006-06-01 23:43:47 +000059 clEnumValN(GenInline, "gen-inline", "Generate an inline function"),
Reid Spencer12803f52006-05-31 17:31:38 +000060 clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"),
61 clEnumValN(GenType, "gen-type", "Generate a type definition"),
62 clEnumValEnd
63 )
64);
65
66static cl::opt<std::string> NameToGenerate("for", cl::Optional,
67 cl::desc("Specify the name of the thing to generate"),
68 cl::init("!bad!"));
Reid Spencer15f7e012006-05-30 21:18:23 +000069
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000070namespace {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000071typedef std::vector<const Type*> TypeList;
72typedef std::map<const Type*,std::string> TypeMap;
73typedef std::map<const Value*,std::string> ValueMap;
Reid Spencer66c87342006-05-30 03:43:49 +000074typedef std::set<std::string> NameSet;
Reid Spencer15f7e012006-05-30 21:18:23 +000075typedef std::set<const Type*> TypeSet;
76typedef std::set<const Value*> ValueSet;
77typedef std::map<const Value*,std::string> ForwardRefMap;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000078
Reid Spencere0d133f2006-05-29 18:08:06 +000079class CppWriter {
Reid Spencer12803f52006-05-31 17:31:38 +000080 const char* progname;
Reid Spencere0d133f2006-05-29 18:08:06 +000081 std::ostream &Out;
82 const Module *TheModule;
Andrew Lenharth539d8712006-05-31 20:18:56 +000083 uint64_t uniqueNum;
Reid Spencere0d133f2006-05-29 18:08:06 +000084 TypeMap TypeNames;
85 ValueMap ValueNames;
86 TypeMap UnresolvedTypes;
87 TypeList TypeStack;
Reid Spencer66c87342006-05-30 03:43:49 +000088 NameSet UsedNames;
Reid Spencer15f7e012006-05-30 21:18:23 +000089 TypeSet DefinedTypes;
90 ValueSet DefinedValues;
91 ForwardRefMap ForwardRefs;
Reid Spencerf977e7b2006-06-01 23:43:47 +000092 bool is_inline;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000093
Reid Spencere0d133f2006-05-29 18:08:06 +000094public:
Reid Spencer12803f52006-05-31 17:31:38 +000095 inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp")
96 : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(),
Reid Spencerf977e7b2006-06-01 23:43:47 +000097 ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +000098
Reid Spencere0d133f2006-05-29 18:08:06 +000099 const Module* getModule() { return TheModule; }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000100
Reid Spencer12803f52006-05-31 17:31:38 +0000101 void printProgram(const std::string& fname, const std::string& modName );
102 void printModule(const std::string& fname, const std::string& modName );
103 void printContents(const std::string& fname, const std::string& modName );
104 void printFunction(const std::string& fname, const std::string& funcName );
Reid Spencerf977e7b2006-06-01 23:43:47 +0000105 void printInline(const std::string& fname, const std::string& funcName );
Reid Spencer12803f52006-05-31 17:31:38 +0000106 void printVariable(const std::string& fname, const std::string& varName );
107 void printType(const std::string& fname, const std::string& typeName );
108
109 void error(const std::string& msg);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000110
Reid Spencere0d133f2006-05-29 18:08:06 +0000111private:
Reid Spencer12803f52006-05-31 17:31:38 +0000112 void printLinkageType(GlobalValue::LinkageTypes LT);
113 void printCallingConv(unsigned cc);
114 void printEscapedString(const std::string& str);
115 void printCFP(const ConstantFP* CFP);
116
117 std::string getCppName(const Type* val);
118 inline void printCppName(const Type* val);
119
120 std::string getCppName(const Value* val);
121 inline void printCppName(const Value* val);
122
123 bool printTypeInternal(const Type* Ty);
124 inline void printType(const Type* Ty);
Reid Spencere0d133f2006-05-29 18:08:06 +0000125 void printTypes(const Module* M);
Reid Spencer12803f52006-05-31 17:31:38 +0000126
Reid Spencere0d133f2006-05-29 18:08:06 +0000127 void printConstant(const Constant *CPV);
Reid Spencer12803f52006-05-31 17:31:38 +0000128 void printConstants(const Module* M);
129
130 void printVariableUses(const GlobalVariable *GV);
131 void printVariableHead(const GlobalVariable *GV);
132 void printVariableBody(const GlobalVariable *GV);
133
134 void printFunctionUses(const Function *F);
Reid Spencer66c87342006-05-30 03:43:49 +0000135 void printFunctionHead(const Function *F);
136 void printFunctionBody(const Function *F);
Reid Spencere0d133f2006-05-29 18:08:06 +0000137 void printInstruction(const Instruction *I, const std::string& bbname);
Reid Spencer15f7e012006-05-30 21:18:23 +0000138 std::string getOpName(Value*);
139
Reid Spencer12803f52006-05-31 17:31:38 +0000140 void printModuleBody();
141
Reid Spencere0d133f2006-05-29 18:08:06 +0000142};
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000143
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000144static unsigned indent_level = 0;
145inline std::ostream& nl(std::ostream& Out, int delta = 0) {
146 Out << "\n";
147 if (delta >= 0 || indent_level >= unsigned(-delta))
148 indent_level += delta;
149 for (unsigned i = 0; i < indent_level; ++i)
150 Out << " ";
151 return Out;
152}
153
154inline void in() { indent_level++; }
155inline void out() { if (indent_level >0) indent_level--; }
156
Reid Spencer12803f52006-05-31 17:31:38 +0000157inline void
158sanitize(std::string& str) {
159 for (size_t i = 0; i < str.length(); ++i)
160 if (!isalnum(str[i]) && str[i] != '_')
161 str[i] = '_';
162}
163
Reid Spencera54b7cb2007-01-12 07:05:14 +0000164inline std::string
Reid Spencer12803f52006-05-31 17:31:38 +0000165getTypePrefix(const Type* Ty ) {
Reid Spencer12803f52006-05-31 17:31:38 +0000166 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000167 case Type::VoidTyID: return "void_";
168 case Type::IntegerTyID:
169 return std::string("int") + utostr(cast<IntegerType>(Ty)->getBitWidth()) +
170 "_";
171 case Type::FloatTyID: return "float_";
172 case Type::DoubleTyID: return "double_";
173 case Type::LabelTyID: return "label_";
174 case Type::FunctionTyID: return "func_";
175 case Type::StructTyID: return "struct_";
176 case Type::ArrayTyID: return "array_";
177 case Type::PointerTyID: return "ptr_";
178 case Type::PackedTyID: return "packed_";
179 case Type::OpaqueTyID: return "opaque_";
180 default: return "other_";
Reid Spencer12803f52006-05-31 17:31:38 +0000181 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000182 return "unknown_";
Reid Spencer12803f52006-05-31 17:31:38 +0000183}
184
185// Looks up the type in the symbol table and returns a pointer to its name or
186// a null pointer if it wasn't found. Note that this isn't the same as the
187// Mode::getTypeName function which will return an empty string, not a null
188// pointer if the name is not found.
189inline const std::string*
Reid Spencer78d033e2007-01-06 07:24:44 +0000190findTypeName(const TypeSymbolTable& ST, const Type* Ty)
Reid Spencer12803f52006-05-31 17:31:38 +0000191{
Reid Spencer78d033e2007-01-06 07:24:44 +0000192 TypeSymbolTable::const_iterator TI = ST.begin();
193 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencer12803f52006-05-31 17:31:38 +0000194 for (;TI != TE; ++TI)
195 if (TI->second == Ty)
196 return &(TI->first);
197 return 0;
198}
199
200void
201CppWriter::error(const std::string& msg) {
202 std::cerr << progname << ": " << msg << "\n";
203 exit(2);
204}
205
Reid Spencer15f7e012006-05-30 21:18:23 +0000206// printCFP - Print a floating point constant .. very carefully :)
207// This makes sure that conversion to/from floating yields the same binary
208// result so that we don't lose precision.
209void
210CppWriter::printCFP(const ConstantFP *CFP) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000211 Out << "ConstantFP::get(";
212 if (CFP->getType() == Type::DoubleTy)
213 Out << "Type::DoubleTy, ";
214 else
215 Out << "Type::FloatTy, ";
Reid Spencer15f7e012006-05-30 21:18:23 +0000216#if HAVE_PRINTF_A
217 char Buffer[100];
218 sprintf(Buffer, "%A", CFP->getValue());
219 if ((!strncmp(Buffer, "0x", 2) ||
220 !strncmp(Buffer, "-0x", 3) ||
221 !strncmp(Buffer, "+0x", 3)) &&
222 (atof(Buffer) == CFP->getValue()))
Reid Spencerf977e7b2006-06-01 23:43:47 +0000223 if (CFP->getType() == Type::DoubleTy)
224 Out << "BitsToDouble(" << Buffer << ")";
225 else
226 Out << "BitsToFloat(" << Buffer << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000227 else {
Reid Spencer15f7e012006-05-30 21:18:23 +0000228#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000229 std::string StrVal = ftostr(CFP->getValue());
230
231 while (StrVal[0] == ' ')
232 StrVal.erase(StrVal.begin());
233
234 // Check to make sure that the stringized number is not some string like
235 // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex.
236 if (((StrVal[0] >= '0' && StrVal[0] <= '9') ||
237 ((StrVal[0] == '-' || StrVal[0] == '+') &&
238 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
239 (atof(StrVal.c_str()) == CFP->getValue()))
240 if (CFP->getType() == Type::DoubleTy)
241 Out << StrVal;
242 else
243 Out << StrVal;
244 else if (CFP->getType() == Type::DoubleTy)
245 Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue())
246 << std::dec << "ULL) /* " << StrVal << " */";
247 else
248 Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue())
249 << std::dec << "U) /* " << StrVal << " */";
Reid Spencer15f7e012006-05-30 21:18:23 +0000250#if HAVE_PRINTF_A
251 }
252#endif
Reid Spencerf977e7b2006-06-01 23:43:47 +0000253 Out << ")";
Reid Spencer15f7e012006-05-30 21:18:23 +0000254}
255
Reid Spencer12803f52006-05-31 17:31:38 +0000256void
257CppWriter::printCallingConv(unsigned cc){
258 // Print the calling convention.
259 switch (cc) {
260 case CallingConv::C: Out << "CallingConv::C"; break;
261 case CallingConv::CSRet: Out << "CallingConv::CSRet"; break;
262 case CallingConv::Fast: Out << "CallingConv::Fast"; break;
263 case CallingConv::Cold: Out << "CallingConv::Cold"; break;
264 case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break;
265 default: Out << cc; break;
266 }
267}
Reid Spencer15f7e012006-05-30 21:18:23 +0000268
Reid Spencer12803f52006-05-31 17:31:38 +0000269void
270CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) {
271 switch (LT) {
272 case GlobalValue::InternalLinkage:
273 Out << "GlobalValue::InternalLinkage"; break;
274 case GlobalValue::LinkOnceLinkage:
275 Out << "GlobalValue::LinkOnceLinkage "; break;
276 case GlobalValue::WeakLinkage:
277 Out << "GlobalValue::WeakLinkage"; break;
278 case GlobalValue::AppendingLinkage:
279 Out << "GlobalValue::AppendingLinkage"; break;
280 case GlobalValue::ExternalLinkage:
281 Out << "GlobalValue::ExternalLinkage"; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000282 case GlobalValue::DLLImportLinkage:
283 Out << "GlobalValue::DllImportLinkage"; break;
284 case GlobalValue::DLLExportLinkage:
285 Out << "GlobalValue::DllExportLinkage"; break;
286 case GlobalValue::ExternalWeakLinkage:
287 Out << "GlobalValue::ExternalWeakLinkage"; break;
Reid Spencer12803f52006-05-31 17:31:38 +0000288 case GlobalValue::GhostLinkage:
289 Out << "GlobalValue::GhostLinkage"; break;
290 }
Reid Spencer15f7e012006-05-30 21:18:23 +0000291}
292
Reid Spencere0d133f2006-05-29 18:08:06 +0000293// printEscapedString - Print each character of the specified string, escaping
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000294// it if it is not printable or if it is an escape char.
Reid Spencere0d133f2006-05-29 18:08:06 +0000295void
296CppWriter::printEscapedString(const std::string &Str) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000297 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
298 unsigned char C = Str[i];
299 if (isprint(C) && C != '"' && C != '\\') {
300 Out << C;
301 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000302 Out << "\\x"
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000303 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
304 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
305 }
306 }
307}
308
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000309std::string
310CppWriter::getCppName(const Type* Ty)
311{
312 // First, handle the primitive types .. easy
Reid Spencera54b7cb2007-01-12 07:05:14 +0000313 if (Ty->isPrimitiveType() || Ty->isIntegral()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000314 switch (Ty->getTypeID()) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000315 case Type::VoidTyID: return "Type::VoidTy";
Reid Spencera54b7cb2007-01-12 07:05:14 +0000316 case Type::IntegerTyID: {
317 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
318 return "IntegerType::get(" + utostr(BitWidth) + ")";
319 }
Reid Spencer71d2ec92006-12-31 06:02:26 +0000320 case Type::FloatTyID: return "Type::FloatTy";
321 case Type::DoubleTyID: return "Type::DoubleTy";
322 case Type::LabelTyID: return "Type::LabelTy";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000323 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000324 error("Invalid primitive type");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000325 break;
326 }
327 return "Type::VoidTy"; // shouldn't be returned, but make it sensible
328 }
329
330 // Now, see if we've seen the type before and return that
331 TypeMap::iterator I = TypeNames.find(Ty);
332 if (I != TypeNames.end())
333 return I->second;
334
335 // Okay, let's build a new name for this type. Start with a prefix
336 const char* prefix = 0;
337 switch (Ty->getTypeID()) {
338 case Type::FunctionTyID: prefix = "FuncTy_"; break;
339 case Type::StructTyID: prefix = "StructTy_"; break;
340 case Type::ArrayTyID: prefix = "ArrayTy_"; break;
341 case Type::PointerTyID: prefix = "PointerTy_"; break;
342 case Type::OpaqueTyID: prefix = "OpaqueTy_"; break;
343 case Type::PackedTyID: prefix = "PackedTy_"; break;
344 default: prefix = "OtherTy_"; break; // prevent breakage
345 }
346
347 // See if the type has a name in the symboltable and build accordingly
Reid Spencer78d033e2007-01-06 07:24:44 +0000348 const std::string* tName = findTypeName(TheModule->getTypeSymbolTable(), Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000349 std::string name;
350 if (tName)
351 name = std::string(prefix) + *tName;
352 else
353 name = std::string(prefix) + utostr(uniqueNum++);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000354 sanitize(name);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000355
356 // Save the name
357 return TypeNames[Ty] = name;
358}
359
Reid Spencer12803f52006-05-31 17:31:38 +0000360void
361CppWriter::printCppName(const Type* Ty)
362{
363 printEscapedString(getCppName(Ty));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000364}
365
Reid Spencer12803f52006-05-31 17:31:38 +0000366std::string
367CppWriter::getCppName(const Value* val) {
368 std::string name;
369 ValueMap::iterator I = ValueNames.find(val);
370 if (I != ValueNames.end() && I->first == val)
371 return I->second;
Reid Spencer25edc352006-05-31 04:43:19 +0000372
Reid Spencer12803f52006-05-31 17:31:38 +0000373 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) {
374 name = std::string("gvar_") +
375 getTypePrefix(GV->getType()->getElementType());
Reid Spencer3ed469c2006-11-02 20:25:50 +0000376 } else if (isa<Function>(val)) {
Reid Spencer12803f52006-05-31 17:31:38 +0000377 name = std::string("func_");
378 } else if (const Constant* C = dyn_cast<Constant>(val)) {
379 name = std::string("const_") + getTypePrefix(C->getType());
Reid Spencerf977e7b2006-06-01 23:43:47 +0000380 } else if (const Argument* Arg = dyn_cast<Argument>(val)) {
381 if (is_inline) {
382 unsigned argNum = std::distance(Arg->getParent()->arg_begin(),
383 Function::const_arg_iterator(Arg)) + 1;
384 name = std::string("arg_") + utostr(argNum);
385 NameSet::iterator NI = UsedNames.find(name);
386 if (NI != UsedNames.end())
387 name += std::string("_") + utostr(uniqueNum++);
388 UsedNames.insert(name);
389 return ValueNames[val] = name;
390 } else {
391 name = getTypePrefix(val->getType());
392 }
Reid Spencer12803f52006-05-31 17:31:38 +0000393 } else {
394 name = getTypePrefix(val->getType());
Reid Spencer25edc352006-05-31 04:43:19 +0000395 }
Reid Spencer12803f52006-05-31 17:31:38 +0000396 name += (val->hasName() ? val->getName() : utostr(uniqueNum++));
397 sanitize(name);
398 NameSet::iterator NI = UsedNames.find(name);
399 if (NI != UsedNames.end())
400 name += std::string("_") + utostr(uniqueNum++);
401 UsedNames.insert(name);
402 return ValueNames[val] = name;
Reid Spencer25edc352006-05-31 04:43:19 +0000403}
404
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000405void
Reid Spencer12803f52006-05-31 17:31:38 +0000406CppWriter::printCppName(const Value* val) {
407 printEscapedString(getCppName(val));
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000408}
409
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000410bool
Reid Spencer12803f52006-05-31 17:31:38 +0000411CppWriter::printTypeInternal(const Type* Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000412 // We don't print definitions for primitive types
Reid Spencera54b7cb2007-01-12 07:05:14 +0000413 if (Ty->isPrimitiveType() || Ty->isIntegral())
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000414 return false;
415
Reid Spencer15f7e012006-05-30 21:18:23 +0000416 // If we already defined this type, we don't need to define it again.
417 if (DefinedTypes.find(Ty) != DefinedTypes.end())
418 return false;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000419
Reid Spencer15f7e012006-05-30 21:18:23 +0000420 // Everything below needs the name for the type so get it now.
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000421 std::string typeName(getCppName(Ty));
422
423 // Search the type stack for recursion. If we find it, then generate this
424 // as an OpaqueType, but make sure not to do this multiple times because
425 // the type could appear in multiple places on the stack. Once the opaque
Reid Spencer15f7e012006-05-30 21:18:23 +0000426 // definition is issued, it must not be re-issued. Consequently we have to
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000427 // check the UnresolvedTypes list as well.
Reid Spencer12803f52006-05-31 17:31:38 +0000428 TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty);
429 if (TI != TypeStack.end()) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000430 TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
431 if (I == UnresolvedTypes.end()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000432 Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();";
433 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000434 UnresolvedTypes[Ty] = typeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000435 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000436 return true;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000437 }
438
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000439 // We're going to print a derived type which, by definition, contains other
440 // types. So, push this one we're printing onto the type stack to assist with
441 // recursive definitions.
Reid Spencer15f7e012006-05-30 21:18:23 +0000442 TypeStack.push_back(Ty);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000443
444 // Print the type definition
445 switch (Ty->getTypeID()) {
446 case Type::FunctionTyID: {
447 const FunctionType* FT = cast<FunctionType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000448 Out << "std::vector<const Type*>" << typeName << "_args;";
449 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000450 FunctionType::param_iterator PI = FT->param_begin();
451 FunctionType::param_iterator PE = FT->param_end();
452 for (; PI != PE; ++PI) {
453 const Type* argTy = static_cast<const Type*>(*PI);
Reid Spencer12803f52006-05-31 17:31:38 +0000454 bool isForward = printTypeInternal(argTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000455 std::string argName(getCppName(argTy));
456 Out << typeName << "_args.push_back(" << argName;
457 if (isForward)
458 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000459 Out << ");";
460 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000461 }
Reid Spencer12803f52006-05-31 17:31:38 +0000462 bool isForward = printTypeInternal(FT->getReturnType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000463 std::string retTypeName(getCppName(FT->getReturnType()));
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000464 Out << "FunctionType* " << typeName << " = FunctionType::get(";
465 in(); nl(Out) << "/*Result=*/" << retTypeName;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000466 if (isForward)
467 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000468 Out << ",";
469 nl(Out) << "/*Params=*/" << typeName << "_args,";
470 nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
471 out();
472 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000473 break;
474 }
475 case Type::StructTyID: {
476 const StructType* ST = cast<StructType>(Ty);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000477 Out << "std::vector<const Type*>" << typeName << "_fields;";
478 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000479 StructType::element_iterator EI = ST->element_begin();
480 StructType::element_iterator EE = ST->element_end();
481 for (; EI != EE; ++EI) {
482 const Type* fieldTy = static_cast<const Type*>(*EI);
Reid Spencer12803f52006-05-31 17:31:38 +0000483 bool isForward = printTypeInternal(fieldTy);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000484 std::string fieldName(getCppName(fieldTy));
485 Out << typeName << "_fields.push_back(" << fieldName;
486 if (isForward)
487 Out << "_fwd";
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000488 Out << ");";
489 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000490 }
491 Out << "StructType* " << typeName << " = StructType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000492 << typeName << "_fields);";
493 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000494 break;
495 }
496 case Type::ArrayTyID: {
497 const ArrayType* AT = cast<ArrayType>(Ty);
498 const Type* ET = AT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000499 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000500 std::string elemName(getCppName(ET));
501 Out << "ArrayType* " << typeName << " = ArrayType::get("
502 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000503 << ", " << utostr(AT->getNumElements()) << ");";
504 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000505 break;
506 }
507 case Type::PointerTyID: {
508 const PointerType* PT = cast<PointerType>(Ty);
509 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000510 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000511 std::string elemName(getCppName(ET));
512 Out << "PointerType* " << typeName << " = PointerType::get("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000513 << elemName << (isForward ? "_fwd" : "") << ");";
514 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000515 break;
516 }
517 case Type::PackedTyID: {
518 const PackedType* PT = cast<PackedType>(Ty);
519 const Type* ET = PT->getElementType();
Reid Spencer12803f52006-05-31 17:31:38 +0000520 bool isForward = printTypeInternal(ET);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000521 std::string elemName(getCppName(ET));
522 Out << "PackedType* " << typeName << " = PackedType::get("
523 << elemName << (isForward ? "_fwd" : "")
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000524 << ", " << utostr(PT->getNumElements()) << ");";
525 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000526 break;
527 }
528 case Type::OpaqueTyID: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000529 Out << "OpaqueType* " << typeName << " = OpaqueType::get();";
530 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000531 break;
532 }
533 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000534 error("Invalid TypeID");
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000535 }
536
Reid Spencer74e032a2006-05-29 02:58:15 +0000537 // If the type had a name, make sure we recreate it.
538 const std::string* progTypeName =
Reid Spencer78d033e2007-01-06 07:24:44 +0000539 findTypeName(TheModule->getTypeSymbolTable(),Ty);
Reid Spencer74e032a2006-05-29 02:58:15 +0000540 if (progTypeName)
541 Out << "mod->addTypeName(\"" << *progTypeName << "\", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000542 << typeName << ");";
543 nl(Out);
Reid Spencer74e032a2006-05-29 02:58:15 +0000544
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000545 // Pop us off the type stack
546 TypeStack.pop_back();
Reid Spencer15f7e012006-05-30 21:18:23 +0000547
548 // Indicate that this type is now defined.
549 DefinedTypes.insert(Ty);
550
551 // Early resolve as many unresolved types as possible. Search the unresolved
552 // types map for the type we just printed. Now that its definition is complete
553 // we can resolve any previous references to it. This prevents a cascade of
554 // unresolved types.
555 TypeMap::iterator I = UnresolvedTypes.find(Ty);
556 if (I != UnresolvedTypes.end()) {
557 Out << "cast<OpaqueType>(" << I->second
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000558 << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
559 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000560 Out << I->second << " = cast<";
561 switch (Ty->getTypeID()) {
562 case Type::FunctionTyID: Out << "FunctionType"; break;
563 case Type::ArrayTyID: Out << "ArrayType"; break;
564 case Type::StructTyID: Out << "StructType"; break;
565 case Type::PackedTyID: Out << "PackedType"; break;
566 case Type::PointerTyID: Out << "PointerType"; break;
567 case Type::OpaqueTyID: Out << "OpaqueType"; break;
568 default: Out << "NoSuchDerivedType"; break;
569 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000570 Out << ">(" << I->second << "_fwd.get());";
571 nl(Out); nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +0000572 UnresolvedTypes.erase(I);
573 }
574
575 // Finally, separate the type definition from other with a newline.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000576 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000577
578 // We weren't a recursive type
579 return false;
580}
581
Reid Spencer12803f52006-05-31 17:31:38 +0000582// Prints a type definition. Returns true if it could not resolve all the types
583// in the definition but had to use a forward reference.
584void
585CppWriter::printType(const Type* Ty) {
586 assert(TypeStack.empty());
587 TypeStack.clear();
588 printTypeInternal(Ty);
589 assert(TypeStack.empty());
590}
591
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000592void
593CppWriter::printTypes(const Module* M) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000594
595 // Walk the symbol table and print out all its types
Reid Spencer78d033e2007-01-06 07:24:44 +0000596 const TypeSymbolTable& symtab = M->getTypeSymbolTable();
597 for (TypeSymbolTable::const_iterator TI = symtab.begin(), TE = symtab.end();
598 TI != TE; ++TI) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000599
600 // For primitive types and types already defined, just add a name
601 TypeMap::const_iterator TNI = TypeNames.find(TI->second);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000602 if (TI->second->isIntegral() || TI->second->isPrimitiveType() ||
603 TNI != TypeNames.end()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000604 Out << "mod->addTypeName(\"";
605 printEscapedString(TI->first);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000606 Out << "\", " << getCppName(TI->second) << ");";
607 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000608 // For everything else, define the type
609 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000610 printType(TI->second);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000611 }
612 }
613
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000614 // Add all of the global variables to the value table...
615 for (Module::const_global_iterator I = TheModule->global_begin(),
616 E = TheModule->global_end(); I != E; ++I) {
617 if (I->hasInitializer())
Reid Spencer12803f52006-05-31 17:31:38 +0000618 printType(I->getInitializer()->getType());
619 printType(I->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000620 }
621
622 // Add all the functions to the table
623 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
624 FI != FE; ++FI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000625 printType(FI->getReturnType());
626 printType(FI->getFunctionType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000627 // Add all the function arguments
628 for(Function::const_arg_iterator AI = FI->arg_begin(),
629 AE = FI->arg_end(); AI != AE; ++AI) {
Reid Spencer12803f52006-05-31 17:31:38 +0000630 printType(AI->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000631 }
632
633 // Add all of the basic blocks and instructions
634 for (Function::const_iterator BB = FI->begin(),
635 E = FI->end(); BB != E; ++BB) {
Reid Spencer12803f52006-05-31 17:31:38 +0000636 printType(BB->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000637 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
638 ++I) {
Reid Spencer12803f52006-05-31 17:31:38 +0000639 printType(I->getType());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000640 for (unsigned i = 0; i < I->getNumOperands(); ++i)
Reid Spencer12803f52006-05-31 17:31:38 +0000641 printType(I->getOperand(i)->getType());
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000642 }
643 }
644 }
645}
646
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000647
Reid Spencere0d133f2006-05-29 18:08:06 +0000648// printConstant - Print out a constant pool entry...
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000649void CppWriter::printConstant(const Constant *CV) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000650 // First, if the constant is actually a GlobalValue (variable or function) or
651 // its already in the constant list then we've printed it already and we can
652 // just return.
653 if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end())
Reid Spencere0d133f2006-05-29 18:08:06 +0000654 return;
655
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000656 std::string constName(getCppName(CV));
657 std::string typeName(getCppName(CV->getType()));
658 if (CV->isNullValue()) {
659 Out << "Constant* " << constName << " = Constant::getNullValue("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000660 << typeName << ");";
661 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000662 return;
663 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000664 if (isa<GlobalValue>(CV)) {
665 // Skip variables and functions, we emit them elsewhere
666 return;
667 }
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000668 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000669 if (CI->getType() == Type::Int1Ty)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000670 Out << "ConstantInt* " << constName << " = ConstantInt::get("
Reid Spencer579dca12007-01-12 04:24:46 +0000671 << (CI->getZExtValue() ? "true" : "false") << ");";
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000672 else
673 Out << "ConstantInt* " << constName << " = ConstantInt::get("
674 << typeName << ", " << CI->getZExtValue() << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000675 } else if (isa<ConstantAggregateZero>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000676 Out << "ConstantAggregateZero* " << constName
677 << " = ConstantAggregateZero::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000678 } else if (isa<ConstantPointerNull>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000679 Out << "ConstantPointerNull* " << constName
680 << " = ConstanPointerNull::get(" << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000681 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Reid Spencerf977e7b2006-06-01 23:43:47 +0000682 Out << "ConstantFP* " << constName << " = ";
Reid Spencer12803f52006-05-31 17:31:38 +0000683 printCFP(CFP);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000684 Out << ";";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000685 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Reid Spencer71d2ec92006-12-31 06:02:26 +0000686 if (CA->isString() && CA->getType()->getElementType() == Type::Int8Ty) {
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000687 Out << "Constant* " << constName << " = ConstantArray::get(\"";
Reid Spencere0d133f2006-05-29 18:08:06 +0000688 printEscapedString(CA->getAsString());
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000689 // Determine if we want null termination or not.
690 if (CA->getType()->getNumElements() <= CA->getAsString().length())
Reid Spencer15f7e012006-05-30 21:18:23 +0000691 Out << "\", false";// No null terminator
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000692 else
Reid Spencer15f7e012006-05-30 21:18:23 +0000693 Out << "\", true"; // Indicate that the null terminator should be added.
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000694 Out << ");";
695 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000696 Out << "std::vector<Constant*> " << constName << "_elems;";
697 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000698 unsigned N = CA->getNumOperands();
699 for (unsigned i = 0; i < N; ++i) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000700 printConstant(CA->getOperand(i)); // recurse to print operands
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000701 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000702 << getCppName(CA->getOperand(i)) << ");";
703 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000704 }
705 Out << "Constant* " << constName << " = ConstantArray::get("
706 << typeName << ", " << constName << "_elems);";
707 }
708 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000709 Out << "std::vector<Constant*> " << constName << "_fields;";
710 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000711 unsigned N = CS->getNumOperands();
712 for (unsigned i = 0; i < N; i++) {
713 printConstant(CS->getOperand(i));
714 Out << constName << "_fields.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000715 << getCppName(CS->getOperand(i)) << ");";
716 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000717 }
718 Out << "Constant* " << constName << " = ConstantStruct::get("
719 << typeName << ", " << constName << "_fields);";
720 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000721 Out << "std::vector<Constant*> " << constName << "_elems;";
722 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000723 unsigned N = CP->getNumOperands();
724 for (unsigned i = 0; i < N; ++i) {
725 printConstant(CP->getOperand(i));
726 Out << constName << "_elems.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000727 << getCppName(CP->getOperand(i)) << ");";
728 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000729 }
730 Out << "Constant* " << constName << " = ConstantPacked::get("
731 << typeName << ", " << constName << "_elems);";
732 } else if (isa<UndefValue>(CV)) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000733 Out << "UndefValue* " << constName << " = UndefValue::get("
Reid Spencere0d133f2006-05-29 18:08:06 +0000734 << typeName << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000735 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000736 if (CE->getOpcode() == Instruction::GetElementPtr) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000737 Out << "std::vector<Constant*> " << constName << "_indices;";
738 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000739 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000740 for (unsigned i = 1; i < CE->getNumOperands(); ++i ) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000741 printConstant(CE->getOperand(i));
Reid Spencere0d133f2006-05-29 18:08:06 +0000742 Out << constName << "_indices.push_back("
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000743 << getCppName(CE->getOperand(i)) << ");";
744 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +0000745 }
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000746 Out << "Constant* " << constName
747 << " = ConstantExpr::getGetElementPtr("
748 << getCppName(CE->getOperand(0)) << ", "
749 << constName << "_indices);";
Reid Spencer3da59db2006-11-27 01:05:10 +0000750 } else if (CE->isCast()) {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000751 printConstant(CE->getOperand(0));
Reid Spencere0d133f2006-05-29 18:08:06 +0000752 Out << "Constant* " << constName << " = ConstantExpr::getCast(";
Reid Spencerb0e9f722006-12-12 01:31:37 +0000753 switch (CE->getOpcode()) {
754 default: assert(0 && "Invalid cast opcode");
755 case Instruction::Trunc: Out << "Instruction::Trunc"; break;
756 case Instruction::ZExt: Out << "Instruction::ZExt"; break;
757 case Instruction::SExt: Out << "Instruction::SExt"; break;
758 case Instruction::FPTrunc: Out << "Instruction::FPTrunc"; break;
759 case Instruction::FPExt: Out << "Instruction::FPExt"; break;
760 case Instruction::FPToUI: Out << "Instruction::FPToUI"; break;
761 case Instruction::FPToSI: Out << "Instruction::FPToSI"; break;
762 case Instruction::UIToFP: Out << "Instruction::UIToFP"; break;
763 case Instruction::SIToFP: Out << "Instruction::SIToFP"; break;
764 case Instruction::PtrToInt: Out << "Instruction::PtrToInt"; break;
765 case Instruction::IntToPtr: Out << "Instruction::IntToPtr"; break;
766 case Instruction::BitCast: Out << "Instruction::BitCast"; break;
767 }
768 Out << ", " << getCppName(CE->getOperand(0)) << ", "
769 << getCppName(CE->getType()) << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000770 } else {
Reid Spencer1a2a0cc2006-05-30 10:21:41 +0000771 unsigned N = CE->getNumOperands();
772 for (unsigned i = 0; i < N; ++i ) {
773 printConstant(CE->getOperand(i));
774 }
Reid Spencere0d133f2006-05-29 18:08:06 +0000775 Out << "Constant* " << constName << " = ConstantExpr::";
776 switch (CE->getOpcode()) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000777 case Instruction::Add: Out << "getAdd("; break;
778 case Instruction::Sub: Out << "getSub("; break;
779 case Instruction::Mul: Out << "getMul("; break;
780 case Instruction::UDiv: Out << "getUDiv("; break;
781 case Instruction::SDiv: Out << "getSDiv("; break;
782 case Instruction::FDiv: Out << "getFDiv("; break;
783 case Instruction::URem: Out << "getURem("; break;
784 case Instruction::SRem: Out << "getSRem("; break;
785 case Instruction::FRem: Out << "getFRem("; break;
786 case Instruction::And: Out << "getAnd("; break;
787 case Instruction::Or: Out << "getOr("; break;
788 case Instruction::Xor: Out << "getXor("; break;
789 case Instruction::ICmp:
790 Out << "getICmp(ICmpInst::ICMP_";
791 switch (CE->getPredicate()) {
792 case ICmpInst::ICMP_EQ: Out << "EQ"; break;
793 case ICmpInst::ICMP_NE: Out << "NE"; break;
794 case ICmpInst::ICMP_SLT: Out << "SLT"; break;
795 case ICmpInst::ICMP_ULT: Out << "ULT"; break;
796 case ICmpInst::ICMP_SGT: Out << "SGT"; break;
797 case ICmpInst::ICMP_UGT: Out << "UGT"; break;
798 case ICmpInst::ICMP_SLE: Out << "SLE"; break;
799 case ICmpInst::ICMP_ULE: Out << "ULE"; break;
800 case ICmpInst::ICMP_SGE: Out << "SGE"; break;
801 case ICmpInst::ICMP_UGE: Out << "UGE"; break;
802 default: error("Invalid ICmp Predicate");
803 }
804 break;
805 case Instruction::FCmp:
806 Out << "getFCmp(FCmpInst::FCMP_";
807 switch (CE->getPredicate()) {
808 case FCmpInst::FCMP_FALSE: Out << "FALSE"; break;
809 case FCmpInst::FCMP_ORD: Out << "ORD"; break;
810 case FCmpInst::FCMP_UNO: Out << "UNO"; break;
811 case FCmpInst::FCMP_OEQ: Out << "OEQ"; break;
812 case FCmpInst::FCMP_UEQ: Out << "UEQ"; break;
813 case FCmpInst::FCMP_ONE: Out << "ONE"; break;
814 case FCmpInst::FCMP_UNE: Out << "UNE"; break;
815 case FCmpInst::FCMP_OLT: Out << "OLT"; break;
816 case FCmpInst::FCMP_ULT: Out << "ULT"; break;
817 case FCmpInst::FCMP_OGT: Out << "OGT"; break;
818 case FCmpInst::FCMP_UGT: Out << "UGT"; break;
819 case FCmpInst::FCMP_OLE: Out << "OLE"; break;
820 case FCmpInst::FCMP_ULE: Out << "ULE"; break;
821 case FCmpInst::FCMP_OGE: Out << "OGE"; break;
822 case FCmpInst::FCMP_UGE: Out << "UGE"; break;
823 case FCmpInst::FCMP_TRUE: Out << "TRUE"; break;
824 default: error("Invalid FCmp Predicate");
825 }
826 break;
827 case Instruction::Shl: Out << "getShl("; break;
828 case Instruction::LShr: Out << "getLShr("; break;
829 case Instruction::AShr: Out << "getAShr("; break;
830 case Instruction::Select: Out << "getSelect("; break;
831 case Instruction::ExtractElement: Out << "getExtractElement("; break;
832 case Instruction::InsertElement: Out << "getInsertElement("; break;
833 case Instruction::ShuffleVector: Out << "getShuffleVector("; break;
Reid Spencere0d133f2006-05-29 18:08:06 +0000834 default:
Reid Spencer12803f52006-05-31 17:31:38 +0000835 error("Invalid constant expression");
Reid Spencere0d133f2006-05-29 18:08:06 +0000836 break;
837 }
838 Out << getCppName(CE->getOperand(0));
839 for (unsigned i = 1; i < CE->getNumOperands(); ++i)
840 Out << ", " << getCppName(CE->getOperand(i));
841 Out << ");";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000842 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000843 } else {
Reid Spencer12803f52006-05-31 17:31:38 +0000844 error("Bad Constant");
Reid Spencere0d133f2006-05-29 18:08:06 +0000845 Out << "Constant* " << constName << " = 0; ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000846 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000847 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000848}
849
Reid Spencer12803f52006-05-31 17:31:38 +0000850void
851CppWriter::printConstants(const Module* M) {
852 // Traverse all the global variables looking for constant initializers
853 for (Module::const_global_iterator I = TheModule->global_begin(),
854 E = TheModule->global_end(); I != E; ++I)
855 if (I->hasInitializer())
856 printConstant(I->getInitializer());
857
858 // Traverse the LLVM functions looking for constants
859 for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end();
860 FI != FE; ++FI) {
861 // Add all of the basic blocks and instructions
862 for (Function::const_iterator BB = FI->begin(),
863 E = FI->end(); BB != E; ++BB) {
864 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;
865 ++I) {
866 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
867 if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) {
868 printConstant(C);
869 }
870 }
871 }
872 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000873 }
Reid Spencer66c87342006-05-30 03:43:49 +0000874}
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000875
Reid Spencer12803f52006-05-31 17:31:38 +0000876void CppWriter::printVariableUses(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000877 nl(Out) << "// Type Definitions";
878 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000879 printType(GV->getType());
880 if (GV->hasInitializer()) {
881 Constant* Init = GV->getInitializer();
882 printType(Init->getType());
883 if (Function* F = dyn_cast<Function>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000884 nl(Out)<< "/ Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000885 printFunctionHead(F);
886 } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000887 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000888 printVariableHead(gv);
889 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000890 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000891 printConstant(gv);
892 }
893 if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000894 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000895 printVariableBody(gv);
Reid Spencere0d133f2006-05-29 18:08:06 +0000896 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000897 }
Reid Spencer12803f52006-05-31 17:31:38 +0000898}
Reid Spencer15f7e012006-05-30 21:18:23 +0000899
Reid Spencer12803f52006-05-31 17:31:38 +0000900void CppWriter::printVariableHead(const GlobalVariable *GV) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000901 nl(Out) << "GlobalVariable* " << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000902 if (is_inline) {
903 Out << " = mod->getGlobalVariable(";
904 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000905 Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)";
906 nl(Out) << "if (!" << getCppName(GV) << ") {";
907 in(); nl(Out) << getCppName(GV);
Reid Spencerf977e7b2006-06-01 23:43:47 +0000908 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000909 Out << " = new GlobalVariable(";
910 nl(Out) << "/*Type=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000911 printCppName(GV->getType()->getElementType());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000912 Out << ",";
913 nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false");
914 Out << ",";
915 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +0000916 printLinkageType(GV->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000917 Out << ",";
918 nl(Out) << "/*Initializer=*/0, ";
Reid Spencer12803f52006-05-31 17:31:38 +0000919 if (GV->hasInitializer()) {
920 Out << "// has initializer, specified below";
Reid Spencer15f7e012006-05-30 21:18:23 +0000921 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000922 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +0000923 printEscapedString(GV->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000924 Out << "\",";
925 nl(Out) << "mod);";
926 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000927
928 if (GV->hasSection()) {
929 printCppName(GV);
930 Out << "->setSection(\"";
931 printEscapedString(GV->getSection());
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000932 Out << "\");";
933 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000934 }
935 if (GV->getAlignment()) {
936 printCppName(GV);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000937 Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");";
938 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000939 };
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000940 if (is_inline) {
941 out(); Out << "}"; nl(Out);
942 }
Reid Spencer12803f52006-05-31 17:31:38 +0000943}
944
945void
946CppWriter::printVariableBody(const GlobalVariable *GV) {
947 if (GV->hasInitializer()) {
948 printCppName(GV);
949 Out << "->setInitializer(";
950 //if (!isa<GlobalValue(GV->getInitializer()))
951 //else
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000952 Out << getCppName(GV->getInitializer()) << ");";
953 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000954 }
955}
956
957std::string
958CppWriter::getOpName(Value* V) {
959 if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end())
960 return getCppName(V);
961
962 // See if its alread in the map of forward references, if so just return the
963 // name we already set up for it
964 ForwardRefMap::const_iterator I = ForwardRefs.find(V);
965 if (I != ForwardRefs.end())
966 return I->second;
967
968 // This is a new forward reference. Generate a unique name for it
969 std::string result(std::string("fwdref_") + utostr(uniqueNum++));
970
971 // Yes, this is a hack. An Argument is the smallest instantiable value that
972 // we can make as a placeholder for the real value. We'll replace these
973 // Argument instances later.
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000974 Out << "Argument* " << result << " = new Argument("
975 << getCppName(V->getType()) << ");";
976 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +0000977 ForwardRefs[V] = result;
978 return result;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000979}
980
Reid Spencere0d133f2006-05-29 18:08:06 +0000981// printInstruction - This member is called for each Instruction in a function.
982void
Reid Spencer12803f52006-05-31 17:31:38 +0000983CppWriter::printInstruction(const Instruction *I, const std::string& bbname) {
Reid Spencere0d133f2006-05-29 18:08:06 +0000984 std::string iName(getCppName(I));
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000985
Reid Spencer15f7e012006-05-30 21:18:23 +0000986 // Before we emit this instruction, we need to take care of generating any
987 // forward references. So, we get the names of all the operands in advance
988 std::string* opNames = new std::string[I->getNumOperands()];
989 for (unsigned i = 0; i < I->getNumOperands(); i++) {
990 opNames[i] = getOpName(I->getOperand(i));
991 }
992
Reid Spencere0d133f2006-05-29 18:08:06 +0000993 switch (I->getOpcode()) {
994 case Instruction::Ret: {
995 const ReturnInst* ret = cast<ReturnInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +0000996 Out << "ReturnInst* " << iName << " = new ReturnInst("
Reid Spencer15f7e012006-05-30 21:18:23 +0000997 << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +0000998 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +0000999 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001000 case Instruction::Br: {
1001 const BranchInst* br = cast<BranchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001002 Out << "BranchInst* " << iName << " = new BranchInst(" ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001003 if (br->getNumOperands() == 3 ) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001004 Out << opNames[0] << ", "
1005 << opNames[1] << ", "
1006 << opNames[2] << ", ";
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001007
Reid Spencere0d133f2006-05-29 18:08:06 +00001008 } else if (br->getNumOperands() == 1) {
Reid Spencer15f7e012006-05-30 21:18:23 +00001009 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001010 } else {
Reid Spencer12803f52006-05-31 17:31:38 +00001011 error("Branch with 2 operands?");
Reid Spencere0d133f2006-05-29 18:08:06 +00001012 }
1013 Out << bbname << ");";
1014 break;
1015 }
Reid Spencer66c87342006-05-30 03:43:49 +00001016 case Instruction::Switch: {
1017 const SwitchInst* sw = cast<SwitchInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001018 Out << "SwitchInst* " << iName << " = new SwitchInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001019 << opNames[0] << ", "
1020 << opNames[1] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001021 << sw->getNumCases() << ", " << bbname << ");";
1022 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001023 for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001024 Out << iName << "->addCase("
Reid Spencer15f7e012006-05-30 21:18:23 +00001025 << opNames[i] << ", "
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001026 << opNames[i+1] << ");";
1027 nl(Out);
Reid Spencer66c87342006-05-30 03:43:49 +00001028 }
1029 break;
1030 }
1031 case Instruction::Invoke: {
1032 const InvokeInst* inv = cast<InvokeInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001033 Out << "std::vector<Value*> " << iName << "_params;";
1034 nl(Out);
1035 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
1036 Out << iName << "_params.push_back("
1037 << opNames[i] << ");";
1038 nl(Out);
1039 }
1040 Out << "InvokeInst* " << iName << " = new InvokeInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001041 << opNames[0] << ", "
1042 << opNames[1] << ", "
1043 << opNames[2] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001044 << iName << "_params, \"";
1045 printEscapedString(inv->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001046 Out << "\", " << bbname << ");";
1047 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001048 printCallingConv(inv->getCallingConv());
1049 Out << ");";
1050 break;
1051 }
1052 case Instruction::Unwind: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001053 Out << "UnwindInst* " << iName << " = new UnwindInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001054 << bbname << ");";
1055 break;
1056 }
1057 case Instruction::Unreachable:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001058 Out << "UnreachableInst* " << iName << " = new UnreachableInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001059 << bbname << ");";
1060 break;
1061 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001062 case Instruction::Add:
1063 case Instruction::Sub:
1064 case Instruction::Mul:
Reid Spencer1628cec2006-10-26 06:15:43 +00001065 case Instruction::UDiv:
1066 case Instruction::SDiv:
1067 case Instruction::FDiv:
Reid Spencer0a783f72006-11-02 01:53:59 +00001068 case Instruction::URem:
1069 case Instruction::SRem:
1070 case Instruction::FRem:
Reid Spencere0d133f2006-05-29 18:08:06 +00001071 case Instruction::And:
1072 case Instruction::Or:
1073 case Instruction::Xor:
Reid Spencer66c87342006-05-30 03:43:49 +00001074 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001075 case Instruction::LShr:
1076 case Instruction::AShr:{
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001077 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
Reid Spencer66c87342006-05-30 03:43:49 +00001078 switch (I->getOpcode()) {
1079 case Instruction::Add: Out << "Instruction::Add"; break;
1080 case Instruction::Sub: Out << "Instruction::Sub"; break;
1081 case Instruction::Mul: Out << "Instruction::Mul"; break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001082 case Instruction::UDiv:Out << "Instruction::UDiv"; break;
1083 case Instruction::SDiv:Out << "Instruction::SDiv"; break;
1084 case Instruction::FDiv:Out << "Instruction::FDiv"; break;
Reid Spencer0a783f72006-11-02 01:53:59 +00001085 case Instruction::URem:Out << "Instruction::URem"; break;
1086 case Instruction::SRem:Out << "Instruction::SRem"; break;
1087 case Instruction::FRem:Out << "Instruction::FRem"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001088 case Instruction::And: Out << "Instruction::And"; break;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001089 case Instruction::Or: Out << "Instruction::Or"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001090 case Instruction::Xor: Out << "Instruction::Xor"; break;
1091 case Instruction::Shl: Out << "Instruction::Shl"; break;
Reid Spencer3822ff52006-11-08 06:47:33 +00001092 case Instruction::LShr:Out << "Instruction::LShr"; break;
1093 case Instruction::AShr:Out << "Instruction::AShr"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001094 default: Out << "Instruction::BadOpCode"; break;
1095 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001096 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001097 printEscapedString(I->getName());
1098 Out << "\", " << bbname << ");";
1099 break;
1100 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001101 case Instruction::FCmp: {
1102 Out << "FCmpInst* " << iName << " = new FCmpInst(";
1103 switch (cast<FCmpInst>(I)->getPredicate()) {
1104 case FCmpInst::FCMP_FALSE: Out << "FCmpInst::FCMP_FALSE"; break;
1105 case FCmpInst::FCMP_OEQ : Out << "FCmpInst::FCMP_OEQ"; break;
1106 case FCmpInst::FCMP_OGT : Out << "FCmpInst::FCMP_OGT"; break;
1107 case FCmpInst::FCMP_OGE : Out << "FCmpInst::FCMP_OGE"; break;
1108 case FCmpInst::FCMP_OLT : Out << "FCmpInst::FCMP_OLT"; break;
1109 case FCmpInst::FCMP_OLE : Out << "FCmpInst::FCMP_OLE"; break;
1110 case FCmpInst::FCMP_ONE : Out << "FCmpInst::FCMP_ONE"; break;
1111 case FCmpInst::FCMP_ORD : Out << "FCmpInst::FCMP_ORD"; break;
1112 case FCmpInst::FCMP_UNO : Out << "FCmpInst::FCMP_UNO"; break;
1113 case FCmpInst::FCMP_UEQ : Out << "FCmpInst::FCMP_UEQ"; break;
1114 case FCmpInst::FCMP_UGT : Out << "FCmpInst::FCMP_UGT"; break;
1115 case FCmpInst::FCMP_UGE : Out << "FCmpInst::FCMP_UGE"; break;
1116 case FCmpInst::FCMP_ULT : Out << "FCmpInst::FCMP_ULT"; break;
1117 case FCmpInst::FCMP_ULE : Out << "FCmpInst::FCMP_ULE"; break;
1118 case FCmpInst::FCMP_UNE : Out << "FCmpInst::FCMP_UNE"; break;
1119 case FCmpInst::FCMP_TRUE : Out << "FCmpInst::FCMP_TRUE"; break;
1120 default: Out << "FCmpInst::BAD_ICMP_PREDICATE"; break;
1121 }
1122 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
1123 printEscapedString(I->getName());
1124 Out << "\", " << bbname << ");";
1125 break;
1126 }
1127 case Instruction::ICmp: {
1128 Out << "ICmpInst* " << iName << " = new ICmpInst(";
1129 switch (cast<ICmpInst>(I)->getPredicate()) {
1130 case ICmpInst::ICMP_EQ: Out << "ICmpInst::ICMP_EQ"; break;
1131 case ICmpInst::ICMP_NE: Out << "ICmpInst::ICMP_NE"; break;
1132 case ICmpInst::ICMP_ULE: Out << "ICmpInst::ICMP_ULE"; break;
1133 case ICmpInst::ICMP_SLE: Out << "ICmpInst::ICMP_SLE"; break;
1134 case ICmpInst::ICMP_UGE: Out << "ICmpInst::ICMP_UGE"; break;
1135 case ICmpInst::ICMP_SGE: Out << "ICmpInst::ICMP_SGE"; break;
1136 case ICmpInst::ICMP_ULT: Out << "ICmpInst::ICMP_ULT"; break;
1137 case ICmpInst::ICMP_SLT: Out << "ICmpInst::ICMP_SLT"; break;
1138 case ICmpInst::ICMP_UGT: Out << "ICmpInst::ICMP_UGT"; break;
1139 case ICmpInst::ICMP_SGT: Out << "ICmpInst::ICMP_SGT"; break;
1140 default: Out << "ICmpInst::BAD_ICMP_PREDICATE"; break;
Reid Spencer66c87342006-05-30 03:43:49 +00001141 }
Reid Spencer15f7e012006-05-30 21:18:23 +00001142 Out << ", " << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001143 printEscapedString(I->getName());
1144 Out << "\", " << bbname << ");";
1145 break;
1146 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001147 case Instruction::Malloc: {
1148 const MallocInst* mallocI = cast<MallocInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001149 Out << "MallocInst* " << iName << " = new MallocInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001150 << getCppName(mallocI->getAllocatedType()) << ", ";
1151 if (mallocI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001152 Out << opNames[0] << ", " ;
Reid Spencere0d133f2006-05-29 18:08:06 +00001153 Out << "\"";
1154 printEscapedString(mallocI->getName());
1155 Out << "\", " << bbname << ");";
1156 if (mallocI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001157 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001158 << mallocI->getAlignment() << ");";
1159 break;
1160 }
Reid Spencer66c87342006-05-30 03:43:49 +00001161 case Instruction::Free: {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001162 Out << "FreeInst* " << iName << " = new FreeInst("
Reid Spencer66c87342006-05-30 03:43:49 +00001163 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
1164 break;
1165 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001166 case Instruction::Alloca: {
1167 const AllocaInst* allocaI = cast<AllocaInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001168 Out << "AllocaInst* " << iName << " = new AllocaInst("
Reid Spencere0d133f2006-05-29 18:08:06 +00001169 << getCppName(allocaI->getAllocatedType()) << ", ";
1170 if (allocaI->isArrayAllocation())
Reid Spencer15f7e012006-05-30 21:18:23 +00001171 Out << opNames[0] << ", ";
Reid Spencere0d133f2006-05-29 18:08:06 +00001172 Out << "\"";
1173 printEscapedString(allocaI->getName());
1174 Out << "\", " << bbname << ");";
1175 if (allocaI->getAlignment())
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001176 nl(Out) << iName << "->setAlignment("
Reid Spencere0d133f2006-05-29 18:08:06 +00001177 << allocaI->getAlignment() << ");";
1178 break;
1179 }
Reid Spencer66c87342006-05-30 03:43:49 +00001180 case Instruction::Load:{
1181 const LoadInst* load = cast<LoadInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001182 Out << "LoadInst* " << iName << " = new LoadInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001183 << opNames[0] << ", \"";
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001184 printEscapedString(load->getName());
1185 Out << "\", " << (load->isVolatile() ? "true" : "false" )
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001186 << ", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001187 break;
1188 }
Reid Spencere0d133f2006-05-29 18:08:06 +00001189 case Instruction::Store: {
1190 const StoreInst* store = cast<StoreInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001191 Out << "StoreInst* " << iName << " = new StoreInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001192 << opNames[0] << ", "
1193 << opNames[1] << ", "
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001194 << (store->isVolatile() ? "true" : "false")
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001195 << ", " << bbname << ");";
Reid Spencere0d133f2006-05-29 18:08:06 +00001196 break;
1197 }
1198 case Instruction::GetElementPtr: {
1199 const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
1200 if (gep->getNumOperands() <= 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001201 Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001202 << opNames[0];
Reid Spencere0d133f2006-05-29 18:08:06 +00001203 if (gep->getNumOperands() == 2)
Reid Spencer15f7e012006-05-30 21:18:23 +00001204 Out << ", " << opNames[1];
Reid Spencere0d133f2006-05-29 18:08:06 +00001205 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001206 Out << "std::vector<Value*> " << iName << "_indices;";
1207 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001208 for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001209 Out << iName << "_indices.push_back("
1210 << opNames[i] << ");";
1211 nl(Out);
Reid Spencere0d133f2006-05-29 18:08:06 +00001212 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001213 Out << "Instruction* " << iName << " = new GetElementPtrInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001214 << opNames[0] << ", " << iName << "_indices";
Reid Spencere0d133f2006-05-29 18:08:06 +00001215 }
1216 Out << ", \"";
1217 printEscapedString(gep->getName());
1218 Out << "\", " << bbname << ");";
1219 break;
1220 }
Reid Spencer66c87342006-05-30 03:43:49 +00001221 case Instruction::PHI: {
1222 const PHINode* phi = cast<PHINode>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001223
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001224 Out << "PHINode* " << iName << " = new PHINode("
Reid Spencer66c87342006-05-30 03:43:49 +00001225 << getCppName(phi->getType()) << ", \"";
1226 printEscapedString(phi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001227 Out << "\", " << bbname << ");";
1228 nl(Out) << iName << "->reserveOperandSpace("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001229 << phi->getNumIncomingValues()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001230 << ");";
1231 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001232 for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001233 Out << iName << "->addIncoming("
1234 << opNames[i] << ", " << opNames[i+1] << ");";
1235 nl(Out);
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001236 }
Reid Spencer66c87342006-05-30 03:43:49 +00001237 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001238 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001239 case Instruction::Trunc:
1240 case Instruction::ZExt:
1241 case Instruction::SExt:
1242 case Instruction::FPTrunc:
1243 case Instruction::FPExt:
1244 case Instruction::FPToUI:
1245 case Instruction::FPToSI:
1246 case Instruction::UIToFP:
1247 case Instruction::SIToFP:
1248 case Instruction::PtrToInt:
1249 case Instruction::IntToPtr:
1250 case Instruction::BitCast: {
Reid Spencer66c87342006-05-30 03:43:49 +00001251 const CastInst* cst = cast<CastInst>(I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001252 Out << "CastInst* " << iName << " = new ";
1253 switch (I->getOpcode()) {
1254 case Instruction::Trunc: Out << "TruncInst";
1255 case Instruction::ZExt: Out << "ZExtInst";
1256 case Instruction::SExt: Out << "SExtInst";
1257 case Instruction::FPTrunc: Out << "FPTruncInst";
1258 case Instruction::FPExt: Out << "FPExtInst";
1259 case Instruction::FPToUI: Out << "FPToUIInst";
1260 case Instruction::FPToSI: Out << "FPToSIInst";
1261 case Instruction::UIToFP: Out << "UIToFPInst";
1262 case Instruction::SIToFP: Out << "SIToFPInst";
1263 case Instruction::PtrToInt: Out << "PtrToInst";
1264 case Instruction::IntToPtr: Out << "IntToPtrInst";
1265 case Instruction::BitCast: Out << "BitCastInst";
1266 default: assert(!"Unreachable"); break;
1267 }
1268 Out << "(" << opNames[0] << ", "
Reid Spencer66c87342006-05-30 03:43:49 +00001269 << getCppName(cst->getType()) << ", \"";
1270 printEscapedString(cst->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001271 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001272 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001273 }
Reid Spencer66c87342006-05-30 03:43:49 +00001274 case Instruction::Call:{
1275 const CallInst* call = cast<CallInst>(I);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001276 if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001277 Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001278 << getCppName(ila->getFunctionType()) << ", \""
1279 << ila->getAsmString() << "\", \""
1280 << ila->getConstraintString() << "\","
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001281 << (ila->hasSideEffects() ? "true" : "false") << ");";
1282 nl(Out);
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001283 }
Reid Spencer66c87342006-05-30 03:43:49 +00001284 if (call->getNumOperands() > 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001285 Out << "std::vector<Value*> " << iName << "_params;";
1286 nl(Out);
1287 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
1288 Out << iName << "_params.push_back(" << opNames[i] << ");";
1289 nl(Out);
1290 }
1291 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001292 << opNames[0] << ", " << iName << "_params, \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001293 } else if (call->getNumOperands() == 3) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001294 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001295 << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001296 } else if (call->getNumOperands() == 2) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001297 Out << "CallInst* " << iName << " = new CallInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001298 << opNames[0] << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001299 } else {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001300 Out << "CallInst* " << iName << " = new CallInst(" << opNames[0]
Reid Spencer15f7e012006-05-30 21:18:23 +00001301 << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001302 }
1303 printEscapedString(call->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001304 Out << "\", " << bbname << ");";
1305 nl(Out) << iName << "->setCallingConv(";
Reid Spencer66c87342006-05-30 03:43:49 +00001306 printCallingConv(call->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001307 Out << ");";
1308 nl(Out) << iName << "->setTailCall("
Reid Spencer1a2a0cc2006-05-30 10:21:41 +00001309 << (call->isTailCall() ? "true":"false");
Reid Spencer66c87342006-05-30 03:43:49 +00001310 Out << ");";
1311 break;
1312 }
1313 case Instruction::Select: {
1314 const SelectInst* sel = cast<SelectInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001315 Out << "SelectInst* " << getCppName(sel) << " = new SelectInst(";
Reid Spencer15f7e012006-05-30 21:18:23 +00001316 Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001317 printEscapedString(sel->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001318 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001319 break;
1320 }
1321 case Instruction::UserOp1:
1322 /// FALL THROUGH
1323 case Instruction::UserOp2: {
1324 /// FIXME: What should be done here?
1325 break;
1326 }
1327 case Instruction::VAArg: {
1328 const VAArgInst* va = cast<VAArgInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001329 Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst("
Reid Spencer15f7e012006-05-30 21:18:23 +00001330 << opNames[0] << ", " << getCppName(va->getType()) << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001331 printEscapedString(va->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001332 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001333 break;
1334 }
1335 case Instruction::ExtractElement: {
1336 const ExtractElementInst* eei = cast<ExtractElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001337 Out << "ExtractElementInst* " << getCppName(eei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001338 << " = new ExtractElementInst(" << opNames[0]
1339 << ", " << opNames[1] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001340 printEscapedString(eei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001341 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001342 break;
1343 }
1344 case Instruction::InsertElement: {
1345 const InsertElementInst* iei = cast<InsertElementInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001346 Out << "InsertElementInst* " << getCppName(iei)
Reid Spencer15f7e012006-05-30 21:18:23 +00001347 << " = new InsertElementInst(" << opNames[0]
1348 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001349 printEscapedString(iei->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001350 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001351 break;
1352 }
1353 case Instruction::ShuffleVector: {
1354 const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001355 Out << "ShuffleVectorInst* " << getCppName(svi)
Reid Spencer15f7e012006-05-30 21:18:23 +00001356 << " = new ShuffleVectorInst(" << opNames[0]
1357 << ", " << opNames[1] << ", " << opNames[2] << ", \"";
Reid Spencer66c87342006-05-30 03:43:49 +00001358 printEscapedString(svi->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001359 Out << "\", " << bbname << ");";
Reid Spencer66c87342006-05-30 03:43:49 +00001360 break;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001361 }
1362 }
Reid Spencer68464b72006-06-15 16:09:59 +00001363 DefinedValues.insert(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001364 nl(Out);
Reid Spencer15f7e012006-05-30 21:18:23 +00001365 delete [] opNames;
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001366}
1367
Reid Spencer12803f52006-05-31 17:31:38 +00001368// Print out the types, constants and declarations needed by one function
1369void CppWriter::printFunctionUses(const Function* F) {
1370
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001371 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001372 if (!is_inline) {
1373 // Print the function's return type
1374 printType(F->getReturnType());
Reid Spencer12803f52006-05-31 17:31:38 +00001375
Reid Spencerf977e7b2006-06-01 23:43:47 +00001376 // Print the function's function type
1377 printType(F->getFunctionType());
Reid Spencer12803f52006-05-31 17:31:38 +00001378
Reid Spencerf977e7b2006-06-01 23:43:47 +00001379 // Print the types of each of the function's arguments
1380 for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1381 AI != AE; ++AI) {
1382 printType(AI->getType());
1383 }
Reid Spencer12803f52006-05-31 17:31:38 +00001384 }
1385
1386 // Print type definitions for every type referenced by an instruction and
1387 // make a note of any global values or constants that are referenced
1388 std::vector<GlobalValue*> gvs;
1389 std::vector<Constant*> consts;
1390 for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){
1391 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1392 I != E; ++I) {
1393 // Print the type of the instruction itself
1394 printType(I->getType());
1395
1396 // Print the type of each of the instruction's operands
1397 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
1398 Value* operand = I->getOperand(i);
1399 printType(operand->getType());
1400 if (GlobalValue* GV = dyn_cast<GlobalValue>(operand))
1401 gvs.push_back(GV);
1402 else if (Constant* C = dyn_cast<Constant>(operand))
1403 consts.push_back(C);
1404 }
1405 }
1406 }
1407
1408 // Print the function declarations for any functions encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001409 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001410 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1411 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001412 if (Function* Fun = dyn_cast<Function>(*I)) {
1413 if (!is_inline || Fun != F)
1414 printFunctionHead(Fun);
1415 }
Reid Spencer12803f52006-05-31 17:31:38 +00001416 }
1417
1418 // Print the global variable declarations for any variables encountered
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001419 nl(Out) << "// Global Variable Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001420 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1421 I != E; ++I) {
1422 if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I))
1423 printVariableHead(F);
1424 }
1425
1426 // Print the constants found
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001427 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001428 for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end();
1429 I != E; ++I) {
Reid Spencerf977e7b2006-06-01 23:43:47 +00001430 printConstant(*I);
Reid Spencer12803f52006-05-31 17:31:38 +00001431 }
1432
1433 // Process the global variables definitions now that all the constants have
1434 // been emitted. These definitions just couple the gvars with their constant
1435 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001436 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001437 for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end();
1438 I != E; ++I) {
1439 if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I))
1440 printVariableBody(GV);
1441 }
1442}
1443
1444void CppWriter::printFunctionHead(const Function* F) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001445 nl(Out) << "Function* " << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001446 if (is_inline) {
1447 Out << " = mod->getFunction(\"";
1448 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001449 Out << "\", " << getCppName(F->getFunctionType()) << ");";
1450 nl(Out) << "if (!" << getCppName(F) << ") {";
1451 nl(Out) << getCppName(F);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001452 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001453 Out<< " = new Function(";
1454 nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ",";
1455 nl(Out) << "/*Linkage=*/";
Reid Spencer12803f52006-05-31 17:31:38 +00001456 printLinkageType(F->getLinkage());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001457 Out << ",";
1458 nl(Out) << "/*Name=*/\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001459 printEscapedString(F->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001460 Out << "\", mod); " << (F->isExternal()? "// (external, no body)" : "");
1461 nl(Out,-1);
Reid Spencer12803f52006-05-31 17:31:38 +00001462 printCppName(F);
1463 Out << "->setCallingConv(";
1464 printCallingConv(F->getCallingConv());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001465 Out << ");";
1466 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001467 if (F->hasSection()) {
1468 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001469 Out << "->setSection(\"" << F->getSection() << "\");";
1470 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001471 }
1472 if (F->getAlignment()) {
1473 printCppName(F);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001474 Out << "->setAlignment(" << F->getAlignment() << ");";
1475 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001476 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001477 if (is_inline) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001478 Out << "}";
1479 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001480 }
Reid Spencer12803f52006-05-31 17:31:38 +00001481}
1482
1483void CppWriter::printFunctionBody(const Function *F) {
1484 if (F->isExternal())
1485 return; // external functions have no bodies.
1486
1487 // Clear the DefinedValues and ForwardRefs maps because we can't have
1488 // cross-function forward refs
1489 ForwardRefs.clear();
1490 DefinedValues.clear();
1491
1492 // Create all the argument values
Reid Spencerf977e7b2006-06-01 23:43:47 +00001493 if (!is_inline) {
1494 if (!F->arg_empty()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001495 Out << "Function::arg_iterator args = " << getCppName(F)
1496 << "->arg_begin();";
1497 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001498 }
1499 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1500 AI != AE; ++AI) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001501 Out << "Value* " << getCppName(AI) << " = args++;";
1502 nl(Out);
1503 if (AI->hasName()) {
1504 Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");";
1505 nl(Out);
1506 }
Reid Spencerf977e7b2006-06-01 23:43:47 +00001507 }
Reid Spencer12803f52006-05-31 17:31:38 +00001508 }
1509
1510 // Create all the basic blocks
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001511 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001512 for (Function::const_iterator BI = F->begin(), BE = F->end();
1513 BI != BE; ++BI) {
1514 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001515 Out << "BasicBlock* " << bbname << " = new BasicBlock(\"";
Reid Spencer12803f52006-05-31 17:31:38 +00001516 if (BI->hasName())
1517 printEscapedString(BI->getName());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001518 Out << "\"," << getCppName(BI->getParent()) << ",0);";
1519 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001520 }
1521
1522 // Output all of its basic blocks... for the function
1523 for (Function::const_iterator BI = F->begin(), BE = F->end();
1524 BI != BE; ++BI) {
1525 std::string bbname(getCppName(BI));
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001526 nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")";
1527 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001528
1529 // Output all of the instructions in the basic block...
1530 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end();
1531 I != E; ++I) {
1532 printInstruction(I,bbname);
1533 }
1534 }
1535
1536 // Loop over the ForwardRefs and resolve them now that all instructions
1537 // are generated.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001538 if (!ForwardRefs.empty()) {
1539 nl(Out) << "// Resolve Forward References";
1540 nl(Out);
1541 }
1542
Reid Spencer12803f52006-05-31 17:31:38 +00001543 while (!ForwardRefs.empty()) {
1544 ForwardRefMap::iterator I = ForwardRefs.begin();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001545 Out << I->second << "->replaceAllUsesWith("
1546 << getCppName(I->first) << "); delete " << I->second << ";";
1547 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001548 ForwardRefs.erase(I);
1549 }
1550}
1551
Reid Spencerf977e7b2006-06-01 23:43:47 +00001552void CppWriter::printInline(const std::string& fname, const std::string& func) {
1553 const Function* F = TheModule->getNamedFunction(func);
1554 if (!F) {
1555 error(std::string("Function '") + func + "' not found in input module");
1556 return;
1557 }
1558 if (F->isExternal()) {
1559 error(std::string("Function '") + func + "' is external!");
1560 return;
1561 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001562 nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *"
Reid Spencerf977e7b2006-06-01 23:43:47 +00001563 << getCppName(F);
1564 unsigned arg_count = 1;
1565 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1566 AI != AE; ++AI) {
1567 Out << ", Value* arg_" << arg_count;
1568 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001569 Out << ") {";
1570 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001571 is_inline = true;
1572 printFunctionUses(F);
1573 printFunctionBody(F);
1574 is_inline = false;
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001575 Out << "return " << getCppName(F->begin()) << ";";
1576 nl(Out) << "}";
1577 nl(Out);
Reid Spencerf977e7b2006-06-01 23:43:47 +00001578}
1579
Reid Spencer12803f52006-05-31 17:31:38 +00001580void CppWriter::printModuleBody() {
1581 // Print out all the type definitions
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001582 nl(Out) << "// Type Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001583 printTypes(TheModule);
1584
1585 // Functions can call each other and global variables can reference them so
1586 // define all the functions first before emitting their function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001587 nl(Out) << "// Function Declarations"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001588 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1589 I != E; ++I)
1590 printFunctionHead(I);
1591
1592 // Process the global variables declarations. We can't initialze them until
1593 // after the constants are printed so just print a header for each global
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001594 nl(Out) << "// Global Variable Declarations\n"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001595 for (Module::const_global_iterator I = TheModule->global_begin(),
1596 E = TheModule->global_end(); I != E; ++I) {
1597 printVariableHead(I);
1598 }
1599
1600 // Print out all the constants definitions. Constants don't recurse except
1601 // through GlobalValues. All GlobalValues have been declared at this point
1602 // so we can proceed to generate the constants.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001603 nl(Out) << "// Constant Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001604 printConstants(TheModule);
1605
1606 // Process the global variables definitions now that all the constants have
1607 // been emitted. These definitions just couple the gvars with their constant
1608 // initializers.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001609 nl(Out) << "// Global Variable Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001610 for (Module::const_global_iterator I = TheModule->global_begin(),
1611 E = TheModule->global_end(); I != E; ++I) {
1612 printVariableBody(I);
1613 }
1614
1615 // Finally, we can safely put out all of the function bodies.
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001616 nl(Out) << "// Function Definitions"; nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001617 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
1618 I != E; ++I) {
1619 if (!I->isExternal()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001620 nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I)
1621 << ")";
1622 nl(Out) << "{";
1623 nl(Out,1);
Reid Spencer12803f52006-05-31 17:31:38 +00001624 printFunctionBody(I);
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001625 nl(Out,-1) << "}";
1626 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001627 }
1628 }
1629}
1630
1631void CppWriter::printProgram(
1632 const std::string& fname,
1633 const std::string& mName
1634) {
1635 Out << "#include <llvm/Module.h>\n";
1636 Out << "#include <llvm/DerivedTypes.h>\n";
1637 Out << "#include <llvm/Constants.h>\n";
1638 Out << "#include <llvm/GlobalVariable.h>\n";
1639 Out << "#include <llvm/Function.h>\n";
1640 Out << "#include <llvm/CallingConv.h>\n";
1641 Out << "#include <llvm/BasicBlock.h>\n";
1642 Out << "#include <llvm/Instructions.h>\n";
1643 Out << "#include <llvm/InlineAsm.h>\n";
Reid Spencerf977e7b2006-06-01 23:43:47 +00001644 Out << "#include <llvm/Support/MathExtras.h>\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001645 Out << "#include <llvm/Pass.h>\n";
1646 Out << "#include <llvm/PassManager.h>\n";
1647 Out << "#include <llvm/Analysis/Verifier.h>\n";
1648 Out << "#include <llvm/Assembly/PrintModulePass.h>\n";
1649 Out << "#include <algorithm>\n";
1650 Out << "#include <iostream>\n\n";
1651 Out << "using namespace llvm;\n\n";
1652 Out << "Module* " << fname << "();\n\n";
1653 Out << "int main(int argc, char**argv) {\n";
1654 Out << " Module* Mod = makeLLVMModule();\n";
1655 Out << " verifyModule(*Mod, PrintMessageAction);\n";
1656 Out << " std::cerr.flush();\n";
1657 Out << " std::cout.flush();\n";
1658 Out << " PassManager PM;\n";
1659 Out << " PM.add(new PrintModulePass(&std::cout));\n";
1660 Out << " PM.run(*Mod);\n";
1661 Out << " return 0;\n";
1662 Out << "}\n\n";
1663 printModule(fname,mName);
1664}
1665
1666void CppWriter::printModule(
1667 const std::string& fname,
1668 const std::string& mName
1669) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001670 nl(Out) << "Module* " << fname << "() {";
1671 nl(Out,1) << "// Module Construction";
1672 nl(Out) << "Module* mod = new Module(\"" << mName << "\");";
1673 nl(Out) << "mod->setEndianness(";
Reid Spencer12803f52006-05-31 17:31:38 +00001674 switch (TheModule->getEndianness()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001675 case Module::LittleEndian: Out << "Module::LittleEndian);"; break;
1676 case Module::BigEndian: Out << "Module::BigEndian);"; break;
1677 case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001678 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001679 nl(Out) << "mod->setPointerSize(";
Reid Spencer12803f52006-05-31 17:31:38 +00001680 switch (TheModule->getPointerSize()) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001681 case Module::Pointer32: Out << "Module::Pointer32);"; break;
1682 case Module::Pointer64: Out << "Module::Pointer64);"; break;
1683 case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break;
Reid Spencer12803f52006-05-31 17:31:38 +00001684 }
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001685 nl(Out);
1686 if (!TheModule->getTargetTriple().empty()) {
Reid Spencer12803f52006-05-31 17:31:38 +00001687 Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple()
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001688 << "\");";
1689 nl(Out);
1690 }
Reid Spencer12803f52006-05-31 17:31:38 +00001691
1692 if (!TheModule->getModuleInlineAsm().empty()) {
1693 Out << "mod->setModuleInlineAsm(\"";
1694 printEscapedString(TheModule->getModuleInlineAsm());
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001695 Out << "\");";
1696 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001697 }
1698
1699 // Loop over the dependent libraries and emit them.
1700 Module::lib_iterator LI = TheModule->lib_begin();
1701 Module::lib_iterator LE = TheModule->lib_end();
1702 while (LI != LE) {
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001703 Out << "mod->addLibrary(\"" << *LI << "\");";
1704 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001705 ++LI;
1706 }
1707 printModuleBody();
Reid Spencer70bbf9a2006-08-14 22:35:15 +00001708 nl(Out) << "return mod;";
1709 nl(Out,-1) << "}";
1710 nl(Out);
Reid Spencer12803f52006-05-31 17:31:38 +00001711}
1712
1713void CppWriter::printContents(
1714 const std::string& fname, // Name of generated function
1715 const std::string& mName // Name of module generated module
1716) {
1717 Out << "\nModule* " << fname << "(Module *mod) {\n";
1718 Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001719 printModuleBody();
1720 Out << "\nreturn mod;\n";
1721 Out << "\n}\n";
1722}
1723
1724void CppWriter::printFunction(
1725 const std::string& fname, // Name of generated function
1726 const std::string& funcName // Name of function to generate
1727) {
1728 const Function* F = TheModule->getNamedFunction(funcName);
1729 if (!F) {
1730 error(std::string("Function '") + funcName + "' not found in input module");
1731 return;
1732 }
1733 Out << "\nFunction* " << fname << "(Module *mod) {\n";
1734 printFunctionUses(F);
1735 printFunctionHead(F);
1736 printFunctionBody(F);
1737 Out << "return " << getCppName(F) << ";\n";
1738 Out << "}\n";
1739}
1740
1741void CppWriter::printVariable(
1742 const std::string& fname, /// Name of generated function
1743 const std::string& varName // Name of variable to generate
1744) {
1745 const GlobalVariable* GV = TheModule->getNamedGlobal(varName);
1746
1747 if (!GV) {
1748 error(std::string("Variable '") + varName + "' not found in input module");
1749 return;
1750 }
1751 Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n";
1752 printVariableUses(GV);
1753 printVariableHead(GV);
1754 printVariableBody(GV);
1755 Out << "return " << getCppName(GV) << ";\n";
1756 Out << "}\n";
1757}
1758
1759void CppWriter::printType(
1760 const std::string& fname, /// Name of generated function
1761 const std::string& typeName // Name of type to generate
1762) {
1763 const Type* Ty = TheModule->getTypeByName(typeName);
1764 if (!Ty) {
1765 error(std::string("Type '") + typeName + "' not found in input module");
1766 return;
1767 }
1768 Out << "\nType* " << fname << "(Module *mod) {\n";
1769 printType(Ty);
1770 Out << "return " << getCppName(Ty) << ";\n";
1771 Out << "}\n";
1772}
1773
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001774} // end anonymous llvm
1775
1776namespace llvm {
1777
1778void WriteModuleToCppFile(Module* mod, std::ostream& o) {
Reid Spencer12803f52006-05-31 17:31:38 +00001779 // Initialize a CppWriter for us to use
1780 CppWriter W(o, mod);
1781
1782 // Emit a header
Reid Spencer25edc352006-05-31 04:43:19 +00001783 o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n";
Reid Spencer12803f52006-05-31 17:31:38 +00001784
1785 // Get the name of the function we're supposed to generate
Reid Spencer15f7e012006-05-30 21:18:23 +00001786 std::string fname = FuncName.getValue();
Reid Spencer12803f52006-05-31 17:31:38 +00001787
1788 // Get the name of the thing we are to generate
1789 std::string tgtname = NameToGenerate.getValue();
1790 if (GenerationType == GenModule ||
1791 GenerationType == GenContents ||
1792 GenerationType == GenProgram) {
1793 if (tgtname == "!bad!") {
1794 if (mod->getModuleIdentifier() == "-")
1795 tgtname = "<stdin>";
1796 else
1797 tgtname = mod->getModuleIdentifier();
1798 }
1799 } else if (tgtname == "!bad!") {
1800 W.error("You must use the -for option with -gen-{function,variable,type}");
1801 }
1802
1803 switch (WhatToGenerate(GenerationType)) {
1804 case GenProgram:
1805 if (fname.empty())
1806 fname = "makeLLVMModule";
1807 W.printProgram(fname,tgtname);
1808 break;
1809 case GenModule:
1810 if (fname.empty())
1811 fname = "makeLLVMModule";
1812 W.printModule(fname,tgtname);
1813 break;
1814 case GenContents:
1815 if (fname.empty())
1816 fname = "makeLLVMModuleContents";
1817 W.printContents(fname,tgtname);
1818 break;
1819 case GenFunction:
1820 if (fname.empty())
1821 fname = "makeLLVMFunction";
1822 W.printFunction(fname,tgtname);
1823 break;
Reid Spencerf977e7b2006-06-01 23:43:47 +00001824 case GenInline:
1825 if (fname.empty())
1826 fname = "makeLLVMInline";
1827 W.printInline(fname,tgtname);
1828 break;
Reid Spencer12803f52006-05-31 17:31:38 +00001829 case GenVariable:
1830 if (fname.empty())
1831 fname = "makeLLVMVariable";
1832 W.printVariable(fname,tgtname);
1833 break;
1834 case GenType:
1835 if (fname.empty())
1836 fname = "makeLLVMType";
1837 W.printType(fname,tgtname);
1838 break;
1839 default:
1840 W.error("Invalid generation option");
Reid Spencer15f7e012006-05-30 21:18:23 +00001841 }
Reid Spencerfb0c0dc2006-05-29 00:57:22 +00001842}
1843
1844}