Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1 | //===-- CppWriter.cpp - Printing LLVM IR as a C++ Source File -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 5 | // 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 Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 7 | // |
| 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" |
| 23 | #include "llvm/Support/CFG.h" |
| 24 | #include "llvm/ADT/StringExtras.h" |
| 25 | #include "llvm/ADT/STLExtras.h" |
| 26 | #include "llvm/Support/MathExtras.h" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 28 | #include "llvm/Config/config.h" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <iostream> |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 31 | #include <set> |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 35 | static cl::opt<std::string> |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 36 | FuncName("funcname", cl::desc("Specify the name of the generated function"), |
| 37 | cl::value_desc("function name")); |
| 38 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 39 | enum WhatToGenerate { |
| 40 | GenProgram, |
| 41 | GenModule, |
| 42 | GenContents, |
| 43 | GenFunction, |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 44 | GenInline, |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 45 | GenVariable, |
| 46 | GenType |
| 47 | }; |
| 48 | |
| 49 | static cl::opt<WhatToGenerate> GenerationType(cl::Optional, |
| 50 | cl::desc("Choose what kind of output to generate"), |
| 51 | cl::init(GenProgram), |
| 52 | cl::values( |
| 53 | clEnumValN(GenProgram, "gen-program", "Generate a complete program"), |
| 54 | clEnumValN(GenModule, "gen-module", "Generate a module definition"), |
| 55 | clEnumValN(GenContents,"gen-contents", "Generate contents of a module"), |
| 56 | clEnumValN(GenFunction,"gen-function", "Generate a function definition"), |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 57 | clEnumValN(GenInline, "gen-inline", "Generate an inline function"), |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 58 | clEnumValN(GenVariable,"gen-variable", "Generate a variable definition"), |
| 59 | clEnumValN(GenType, "gen-type", "Generate a type definition"), |
| 60 | clEnumValEnd |
| 61 | ) |
| 62 | ); |
| 63 | |
| 64 | static cl::opt<std::string> NameToGenerate("for", cl::Optional, |
| 65 | cl::desc("Specify the name of the thing to generate"), |
| 66 | cl::init("!bad!")); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 67 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 68 | namespace { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 69 | typedef std::vector<const Type*> TypeList; |
| 70 | typedef std::map<const Type*,std::string> TypeMap; |
| 71 | typedef std::map<const Value*,std::string> ValueMap; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 72 | typedef std::set<std::string> NameSet; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 73 | typedef std::set<const Type*> TypeSet; |
| 74 | typedef std::set<const Value*> ValueSet; |
| 75 | typedef std::map<const Value*,std::string> ForwardRefMap; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 76 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 77 | class CppWriter { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 78 | const char* progname; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 79 | std::ostream &Out; |
| 80 | const Module *TheModule; |
Andrew Lenharth | 539d871 | 2006-05-31 20:18:56 +0000 | [diff] [blame] | 81 | uint64_t uniqueNum; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 82 | TypeMap TypeNames; |
| 83 | ValueMap ValueNames; |
| 84 | TypeMap UnresolvedTypes; |
| 85 | TypeList TypeStack; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 86 | NameSet UsedNames; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 87 | TypeSet DefinedTypes; |
| 88 | ValueSet DefinedValues; |
| 89 | ForwardRefMap ForwardRefs; |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 90 | bool is_inline; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 91 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 92 | public: |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 93 | inline CppWriter(std::ostream &o, const Module *M, const char* pn="llvm2cpp") |
| 94 | : progname(pn), Out(o), TheModule(M), uniqueNum(0), TypeNames(), |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 95 | ValueNames(), UnresolvedTypes(), TypeStack(), is_inline(false) { } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 96 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 97 | const Module* getModule() { return TheModule; } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 98 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 99 | void printProgram(const std::string& fname, const std::string& modName ); |
| 100 | void printModule(const std::string& fname, const std::string& modName ); |
| 101 | void printContents(const std::string& fname, const std::string& modName ); |
| 102 | void printFunction(const std::string& fname, const std::string& funcName ); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 103 | void printInline(const std::string& fname, const std::string& funcName ); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 104 | void printVariable(const std::string& fname, const std::string& varName ); |
| 105 | void printType(const std::string& fname, const std::string& typeName ); |
| 106 | |
| 107 | void error(const std::string& msg); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 108 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 109 | private: |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 110 | void printLinkageType(GlobalValue::LinkageTypes LT); |
| 111 | void printCallingConv(unsigned cc); |
| 112 | void printEscapedString(const std::string& str); |
| 113 | void printCFP(const ConstantFP* CFP); |
| 114 | |
| 115 | std::string getCppName(const Type* val); |
| 116 | inline void printCppName(const Type* val); |
| 117 | |
| 118 | std::string getCppName(const Value* val); |
| 119 | inline void printCppName(const Value* val); |
| 120 | |
| 121 | bool printTypeInternal(const Type* Ty); |
| 122 | inline void printType(const Type* Ty); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 123 | void printTypes(const Module* M); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 124 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 125 | void printConstant(const Constant *CPV); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 126 | void printConstants(const Module* M); |
| 127 | |
| 128 | void printVariableUses(const GlobalVariable *GV); |
| 129 | void printVariableHead(const GlobalVariable *GV); |
| 130 | void printVariableBody(const GlobalVariable *GV); |
| 131 | |
| 132 | void printFunctionUses(const Function *F); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 133 | void printFunctionHead(const Function *F); |
| 134 | void printFunctionBody(const Function *F); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 135 | void printInstruction(const Instruction *I, const std::string& bbname); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 136 | std::string getOpName(Value*); |
| 137 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 138 | void printModuleBody(); |
| 139 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 140 | }; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 141 | |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 142 | static unsigned indent_level = 0; |
| 143 | inline std::ostream& nl(std::ostream& Out, int delta = 0) { |
| 144 | Out << "\n"; |
| 145 | if (delta >= 0 || indent_level >= unsigned(-delta)) |
| 146 | indent_level += delta; |
| 147 | for (unsigned i = 0; i < indent_level; ++i) |
| 148 | Out << " "; |
| 149 | return Out; |
| 150 | } |
| 151 | |
| 152 | inline void in() { indent_level++; } |
| 153 | inline void out() { if (indent_level >0) indent_level--; } |
| 154 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 155 | inline void |
| 156 | sanitize(std::string& str) { |
| 157 | for (size_t i = 0; i < str.length(); ++i) |
| 158 | if (!isalnum(str[i]) && str[i] != '_') |
| 159 | str[i] = '_'; |
| 160 | } |
| 161 | |
| 162 | inline const char* |
| 163 | getTypePrefix(const Type* Ty ) { |
| 164 | const char* prefix; |
| 165 | switch (Ty->getTypeID()) { |
| 166 | case Type::VoidTyID: prefix = "void_"; break; |
| 167 | case Type::BoolTyID: prefix = "bool_"; break; |
| 168 | case Type::UByteTyID: prefix = "ubyte_"; break; |
| 169 | case Type::SByteTyID: prefix = "sbyte_"; break; |
| 170 | case Type::UShortTyID: prefix = "ushort_"; break; |
| 171 | case Type::ShortTyID: prefix = "short_"; break; |
| 172 | case Type::UIntTyID: prefix = "uint_"; break; |
| 173 | case Type::IntTyID: prefix = "int_"; break; |
| 174 | case Type::ULongTyID: prefix = "ulong_"; break; |
| 175 | case Type::LongTyID: prefix = "long_"; break; |
| 176 | case Type::FloatTyID: prefix = "float_"; break; |
| 177 | case Type::DoubleTyID: prefix = "double_"; break; |
| 178 | case Type::LabelTyID: prefix = "label_"; break; |
| 179 | case Type::FunctionTyID: prefix = "func_"; break; |
| 180 | case Type::StructTyID: prefix = "struct_"; break; |
| 181 | case Type::ArrayTyID: prefix = "array_"; break; |
| 182 | case Type::PointerTyID: prefix = "ptr_"; break; |
| 183 | case Type::PackedTyID: prefix = "packed_"; break; |
| 184 | case Type::OpaqueTyID: prefix = "opaque_"; break; |
| 185 | default: prefix = "other_"; break; |
| 186 | } |
| 187 | return prefix; |
| 188 | } |
| 189 | |
| 190 | // Looks up the type in the symbol table and returns a pointer to its name or |
| 191 | // a null pointer if it wasn't found. Note that this isn't the same as the |
| 192 | // Mode::getTypeName function which will return an empty string, not a null |
| 193 | // pointer if the name is not found. |
| 194 | inline const std::string* |
| 195 | findTypeName(const SymbolTable& ST, const Type* Ty) |
| 196 | { |
| 197 | SymbolTable::type_const_iterator TI = ST.type_begin(); |
| 198 | SymbolTable::type_const_iterator TE = ST.type_end(); |
| 199 | for (;TI != TE; ++TI) |
| 200 | if (TI->second == Ty) |
| 201 | return &(TI->first); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | CppWriter::error(const std::string& msg) { |
| 207 | std::cerr << progname << ": " << msg << "\n"; |
| 208 | exit(2); |
| 209 | } |
| 210 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 211 | // printCFP - Print a floating point constant .. very carefully :) |
| 212 | // This makes sure that conversion to/from floating yields the same binary |
| 213 | // result so that we don't lose precision. |
| 214 | void |
| 215 | CppWriter::printCFP(const ConstantFP *CFP) { |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 216 | Out << "ConstantFP::get("; |
| 217 | if (CFP->getType() == Type::DoubleTy) |
| 218 | Out << "Type::DoubleTy, "; |
| 219 | else |
| 220 | Out << "Type::FloatTy, "; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 221 | #if HAVE_PRINTF_A |
| 222 | char Buffer[100]; |
| 223 | sprintf(Buffer, "%A", CFP->getValue()); |
| 224 | if ((!strncmp(Buffer, "0x", 2) || |
| 225 | !strncmp(Buffer, "-0x", 3) || |
| 226 | !strncmp(Buffer, "+0x", 3)) && |
| 227 | (atof(Buffer) == CFP->getValue())) |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 228 | if (CFP->getType() == Type::DoubleTy) |
| 229 | Out << "BitsToDouble(" << Buffer << ")"; |
| 230 | else |
| 231 | Out << "BitsToFloat(" << Buffer << ")"; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 232 | else { |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 233 | #endif |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 234 | std::string StrVal = ftostr(CFP->getValue()); |
| 235 | |
| 236 | while (StrVal[0] == ' ') |
| 237 | StrVal.erase(StrVal.begin()); |
| 238 | |
| 239 | // Check to make sure that the stringized number is not some string like |
| 240 | // "Inf" or NaN. Check that the string matches the "[-+]?[0-9]" regex. |
| 241 | if (((StrVal[0] >= '0' && StrVal[0] <= '9') || |
| 242 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
| 243 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) && |
| 244 | (atof(StrVal.c_str()) == CFP->getValue())) |
| 245 | if (CFP->getType() == Type::DoubleTy) |
| 246 | Out << StrVal; |
| 247 | else |
| 248 | Out << StrVal; |
| 249 | else if (CFP->getType() == Type::DoubleTy) |
| 250 | Out << "BitsToDouble(0x" << std::hex << DoubleToBits(CFP->getValue()) |
| 251 | << std::dec << "ULL) /* " << StrVal << " */"; |
| 252 | else |
| 253 | Out << "BitsToFloat(0x" << std::hex << FloatToBits(CFP->getValue()) |
| 254 | << std::dec << "U) /* " << StrVal << " */"; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 255 | #if HAVE_PRINTF_A |
| 256 | } |
| 257 | #endif |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 258 | Out << ")"; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 261 | void |
| 262 | CppWriter::printCallingConv(unsigned cc){ |
| 263 | // Print the calling convention. |
| 264 | switch (cc) { |
| 265 | case CallingConv::C: Out << "CallingConv::C"; break; |
| 266 | case CallingConv::CSRet: Out << "CallingConv::CSRet"; break; |
| 267 | case CallingConv::Fast: Out << "CallingConv::Fast"; break; |
| 268 | case CallingConv::Cold: Out << "CallingConv::Cold"; break; |
| 269 | case CallingConv::FirstTargetCC: Out << "CallingConv::FirstTargetCC"; break; |
| 270 | default: Out << cc; break; |
| 271 | } |
| 272 | } |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 273 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 274 | void |
| 275 | CppWriter::printLinkageType(GlobalValue::LinkageTypes LT) { |
| 276 | switch (LT) { |
| 277 | case GlobalValue::InternalLinkage: |
| 278 | Out << "GlobalValue::InternalLinkage"; break; |
| 279 | case GlobalValue::LinkOnceLinkage: |
| 280 | Out << "GlobalValue::LinkOnceLinkage "; break; |
| 281 | case GlobalValue::WeakLinkage: |
| 282 | Out << "GlobalValue::WeakLinkage"; break; |
| 283 | case GlobalValue::AppendingLinkage: |
| 284 | Out << "GlobalValue::AppendingLinkage"; break; |
| 285 | case GlobalValue::ExternalLinkage: |
| 286 | Out << "GlobalValue::ExternalLinkage"; break; |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame^] | 287 | case GlobalValue::DLLImportLinkage: |
| 288 | Out << "GlobalValue::DllImportLinkage"; break; |
| 289 | case GlobalValue::DLLExportLinkage: |
| 290 | Out << "GlobalValue::DllExportLinkage"; break; |
| 291 | case GlobalValue::ExternalWeakLinkage: |
| 292 | Out << "GlobalValue::ExternalWeakLinkage"; break; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 293 | case GlobalValue::GhostLinkage: |
| 294 | Out << "GlobalValue::GhostLinkage"; break; |
| 295 | } |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 298 | // printEscapedString - Print each character of the specified string, escaping |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 299 | // it if it is not printable or if it is an escape char. |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 300 | void |
| 301 | CppWriter::printEscapedString(const std::string &Str) { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 302 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 303 | unsigned char C = Str[i]; |
| 304 | if (isprint(C) && C != '"' && C != '\\') { |
| 305 | Out << C; |
| 306 | } else { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 307 | Out << "\\x" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 308 | << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A')) |
| 309 | << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A')); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 314 | std::string |
| 315 | CppWriter::getCppName(const Type* Ty) |
| 316 | { |
| 317 | // First, handle the primitive types .. easy |
| 318 | if (Ty->isPrimitiveType()) { |
| 319 | switch (Ty->getTypeID()) { |
| 320 | case Type::VoidTyID: return "Type::VoidTy"; |
| 321 | case Type::BoolTyID: return "Type::BoolTy"; |
| 322 | case Type::UByteTyID: return "Type::UByteTy"; |
| 323 | case Type::SByteTyID: return "Type::SByteTy"; |
| 324 | case Type::UShortTyID: return "Type::UShortTy"; |
| 325 | case Type::ShortTyID: return "Type::ShortTy"; |
| 326 | case Type::UIntTyID: return "Type::UIntTy"; |
| 327 | case Type::IntTyID: return "Type::IntTy"; |
| 328 | case Type::ULongTyID: return "Type::ULongTy"; |
| 329 | case Type::LongTyID: return "Type::LongTy"; |
| 330 | case Type::FloatTyID: return "Type::FloatTy"; |
| 331 | case Type::DoubleTyID: return "Type::DoubleTy"; |
| 332 | case Type::LabelTyID: return "Type::LabelTy"; |
| 333 | default: |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 334 | error("Invalid primitive type"); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | return "Type::VoidTy"; // shouldn't be returned, but make it sensible |
| 338 | } |
| 339 | |
| 340 | // Now, see if we've seen the type before and return that |
| 341 | TypeMap::iterator I = TypeNames.find(Ty); |
| 342 | if (I != TypeNames.end()) |
| 343 | return I->second; |
| 344 | |
| 345 | // Okay, let's build a new name for this type. Start with a prefix |
| 346 | const char* prefix = 0; |
| 347 | switch (Ty->getTypeID()) { |
| 348 | case Type::FunctionTyID: prefix = "FuncTy_"; break; |
| 349 | case Type::StructTyID: prefix = "StructTy_"; break; |
| 350 | case Type::ArrayTyID: prefix = "ArrayTy_"; break; |
| 351 | case Type::PointerTyID: prefix = "PointerTy_"; break; |
| 352 | case Type::OpaqueTyID: prefix = "OpaqueTy_"; break; |
| 353 | case Type::PackedTyID: prefix = "PackedTy_"; break; |
| 354 | default: prefix = "OtherTy_"; break; // prevent breakage |
| 355 | } |
| 356 | |
| 357 | // See if the type has a name in the symboltable and build accordingly |
| 358 | const std::string* tName = findTypeName(TheModule->getSymbolTable(), Ty); |
| 359 | std::string name; |
| 360 | if (tName) |
| 361 | name = std::string(prefix) + *tName; |
| 362 | else |
| 363 | name = std::string(prefix) + utostr(uniqueNum++); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 364 | sanitize(name); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 365 | |
| 366 | // Save the name |
| 367 | return TypeNames[Ty] = name; |
| 368 | } |
| 369 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 370 | void |
| 371 | CppWriter::printCppName(const Type* Ty) |
| 372 | { |
| 373 | printEscapedString(getCppName(Ty)); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 376 | std::string |
| 377 | CppWriter::getCppName(const Value* val) { |
| 378 | std::string name; |
| 379 | ValueMap::iterator I = ValueNames.find(val); |
| 380 | if (I != ValueNames.end() && I->first == val) |
| 381 | return I->second; |
Reid Spencer | 25edc35 | 2006-05-31 04:43:19 +0000 | [diff] [blame] | 382 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 383 | if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(val)) { |
| 384 | name = std::string("gvar_") + |
| 385 | getTypePrefix(GV->getType()->getElementType()); |
| 386 | } else if (const Function* F = dyn_cast<Function>(val)) { |
| 387 | name = std::string("func_"); |
| 388 | } else if (const Constant* C = dyn_cast<Constant>(val)) { |
| 389 | name = std::string("const_") + getTypePrefix(C->getType()); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 390 | } else if (const Argument* Arg = dyn_cast<Argument>(val)) { |
| 391 | if (is_inline) { |
| 392 | unsigned argNum = std::distance(Arg->getParent()->arg_begin(), |
| 393 | Function::const_arg_iterator(Arg)) + 1; |
| 394 | name = std::string("arg_") + utostr(argNum); |
| 395 | NameSet::iterator NI = UsedNames.find(name); |
| 396 | if (NI != UsedNames.end()) |
| 397 | name += std::string("_") + utostr(uniqueNum++); |
| 398 | UsedNames.insert(name); |
| 399 | return ValueNames[val] = name; |
| 400 | } else { |
| 401 | name = getTypePrefix(val->getType()); |
| 402 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 403 | } else { |
| 404 | name = getTypePrefix(val->getType()); |
Reid Spencer | 25edc35 | 2006-05-31 04:43:19 +0000 | [diff] [blame] | 405 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 406 | name += (val->hasName() ? val->getName() : utostr(uniqueNum++)); |
| 407 | sanitize(name); |
| 408 | NameSet::iterator NI = UsedNames.find(name); |
| 409 | if (NI != UsedNames.end()) |
| 410 | name += std::string("_") + utostr(uniqueNum++); |
| 411 | UsedNames.insert(name); |
| 412 | return ValueNames[val] = name; |
Reid Spencer | 25edc35 | 2006-05-31 04:43:19 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 415 | void |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 416 | CppWriter::printCppName(const Value* val) { |
| 417 | printEscapedString(getCppName(val)); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 420 | bool |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 421 | CppWriter::printTypeInternal(const Type* Ty) { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 422 | // We don't print definitions for primitive types |
| 423 | if (Ty->isPrimitiveType()) |
| 424 | return false; |
| 425 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 426 | // If we already defined this type, we don't need to define it again. |
| 427 | if (DefinedTypes.find(Ty) != DefinedTypes.end()) |
| 428 | return false; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 429 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 430 | // Everything below needs the name for the type so get it now. |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 431 | std::string typeName(getCppName(Ty)); |
| 432 | |
| 433 | // Search the type stack for recursion. If we find it, then generate this |
| 434 | // as an OpaqueType, but make sure not to do this multiple times because |
| 435 | // the type could appear in multiple places on the stack. Once the opaque |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 436 | // definition is issued, it must not be re-issued. Consequently we have to |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 437 | // check the UnresolvedTypes list as well. |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 438 | TypeList::const_iterator TI = std::find(TypeStack.begin(),TypeStack.end(),Ty); |
| 439 | if (TI != TypeStack.end()) { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 440 | TypeMap::const_iterator I = UnresolvedTypes.find(Ty); |
| 441 | if (I == UnresolvedTypes.end()) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 442 | Out << "PATypeHolder " << typeName << "_fwd = OpaqueType::get();"; |
| 443 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 444 | UnresolvedTypes[Ty] = typeName; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 445 | } |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 446 | return true; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 449 | // We're going to print a derived type which, by definition, contains other |
| 450 | // types. So, push this one we're printing onto the type stack to assist with |
| 451 | // recursive definitions. |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 452 | TypeStack.push_back(Ty); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 453 | |
| 454 | // Print the type definition |
| 455 | switch (Ty->getTypeID()) { |
| 456 | case Type::FunctionTyID: { |
| 457 | const FunctionType* FT = cast<FunctionType>(Ty); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 458 | Out << "std::vector<const Type*>" << typeName << "_args;"; |
| 459 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 460 | FunctionType::param_iterator PI = FT->param_begin(); |
| 461 | FunctionType::param_iterator PE = FT->param_end(); |
| 462 | for (; PI != PE; ++PI) { |
| 463 | const Type* argTy = static_cast<const Type*>(*PI); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 464 | bool isForward = printTypeInternal(argTy); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 465 | std::string argName(getCppName(argTy)); |
| 466 | Out << typeName << "_args.push_back(" << argName; |
| 467 | if (isForward) |
| 468 | Out << "_fwd"; |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 469 | Out << ");"; |
| 470 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 471 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 472 | bool isForward = printTypeInternal(FT->getReturnType()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 473 | std::string retTypeName(getCppName(FT->getReturnType())); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 474 | Out << "FunctionType* " << typeName << " = FunctionType::get("; |
| 475 | in(); nl(Out) << "/*Result=*/" << retTypeName; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 476 | if (isForward) |
| 477 | Out << "_fwd"; |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 478 | Out << ","; |
| 479 | nl(Out) << "/*Params=*/" << typeName << "_args,"; |
| 480 | nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");"; |
| 481 | out(); |
| 482 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 483 | break; |
| 484 | } |
| 485 | case Type::StructTyID: { |
| 486 | const StructType* ST = cast<StructType>(Ty); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 487 | Out << "std::vector<const Type*>" << typeName << "_fields;"; |
| 488 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 489 | StructType::element_iterator EI = ST->element_begin(); |
| 490 | StructType::element_iterator EE = ST->element_end(); |
| 491 | for (; EI != EE; ++EI) { |
| 492 | const Type* fieldTy = static_cast<const Type*>(*EI); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 493 | bool isForward = printTypeInternal(fieldTy); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 494 | std::string fieldName(getCppName(fieldTy)); |
| 495 | Out << typeName << "_fields.push_back(" << fieldName; |
| 496 | if (isForward) |
| 497 | Out << "_fwd"; |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 498 | Out << ");"; |
| 499 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 500 | } |
| 501 | Out << "StructType* " << typeName << " = StructType::get(" |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 502 | << typeName << "_fields);"; |
| 503 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 504 | break; |
| 505 | } |
| 506 | case Type::ArrayTyID: { |
| 507 | const ArrayType* AT = cast<ArrayType>(Ty); |
| 508 | const Type* ET = AT->getElementType(); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 509 | bool isForward = printTypeInternal(ET); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 510 | std::string elemName(getCppName(ET)); |
| 511 | Out << "ArrayType* " << typeName << " = ArrayType::get(" |
| 512 | << elemName << (isForward ? "_fwd" : "") |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 513 | << ", " << utostr(AT->getNumElements()) << ");"; |
| 514 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 515 | break; |
| 516 | } |
| 517 | case Type::PointerTyID: { |
| 518 | const PointerType* PT = cast<PointerType>(Ty); |
| 519 | const Type* ET = PT->getElementType(); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 520 | bool isForward = printTypeInternal(ET); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 521 | std::string elemName(getCppName(ET)); |
| 522 | Out << "PointerType* " << typeName << " = PointerType::get(" |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 523 | << elemName << (isForward ? "_fwd" : "") << ");"; |
| 524 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | case Type::PackedTyID: { |
| 528 | const PackedType* PT = cast<PackedType>(Ty); |
| 529 | const Type* ET = PT->getElementType(); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 530 | bool isForward = printTypeInternal(ET); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 531 | std::string elemName(getCppName(ET)); |
| 532 | Out << "PackedType* " << typeName << " = PackedType::get(" |
| 533 | << elemName << (isForward ? "_fwd" : "") |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 534 | << ", " << utostr(PT->getNumElements()) << ");"; |
| 535 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 536 | break; |
| 537 | } |
| 538 | case Type::OpaqueTyID: { |
| 539 | const OpaqueType* OT = cast<OpaqueType>(Ty); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 540 | Out << "OpaqueType* " << typeName << " = OpaqueType::get();"; |
| 541 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 542 | break; |
| 543 | } |
| 544 | default: |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 545 | error("Invalid TypeID"); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Reid Spencer | 74e032a | 2006-05-29 02:58:15 +0000 | [diff] [blame] | 548 | // If the type had a name, make sure we recreate it. |
| 549 | const std::string* progTypeName = |
| 550 | findTypeName(TheModule->getSymbolTable(),Ty); |
| 551 | if (progTypeName) |
| 552 | Out << "mod->addTypeName(\"" << *progTypeName << "\", " |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 553 | << typeName << ");"; |
| 554 | nl(Out); |
Reid Spencer | 74e032a | 2006-05-29 02:58:15 +0000 | [diff] [blame] | 555 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 556 | // Pop us off the type stack |
| 557 | TypeStack.pop_back(); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 558 | |
| 559 | // Indicate that this type is now defined. |
| 560 | DefinedTypes.insert(Ty); |
| 561 | |
| 562 | // Early resolve as many unresolved types as possible. Search the unresolved |
| 563 | // types map for the type we just printed. Now that its definition is complete |
| 564 | // we can resolve any previous references to it. This prevents a cascade of |
| 565 | // unresolved types. |
| 566 | TypeMap::iterator I = UnresolvedTypes.find(Ty); |
| 567 | if (I != UnresolvedTypes.end()) { |
| 568 | Out << "cast<OpaqueType>(" << I->second |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 569 | << "_fwd.get())->refineAbstractTypeTo(" << I->second << ");"; |
| 570 | nl(Out); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 571 | Out << I->second << " = cast<"; |
| 572 | switch (Ty->getTypeID()) { |
| 573 | case Type::FunctionTyID: Out << "FunctionType"; break; |
| 574 | case Type::ArrayTyID: Out << "ArrayType"; break; |
| 575 | case Type::StructTyID: Out << "StructType"; break; |
| 576 | case Type::PackedTyID: Out << "PackedType"; break; |
| 577 | case Type::PointerTyID: Out << "PointerType"; break; |
| 578 | case Type::OpaqueTyID: Out << "OpaqueType"; break; |
| 579 | default: Out << "NoSuchDerivedType"; break; |
| 580 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 581 | Out << ">(" << I->second << "_fwd.get());"; |
| 582 | nl(Out); nl(Out); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 583 | UnresolvedTypes.erase(I); |
| 584 | } |
| 585 | |
| 586 | // Finally, separate the type definition from other with a newline. |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 587 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 588 | |
| 589 | // We weren't a recursive type |
| 590 | return false; |
| 591 | } |
| 592 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 593 | // Prints a type definition. Returns true if it could not resolve all the types |
| 594 | // in the definition but had to use a forward reference. |
| 595 | void |
| 596 | CppWriter::printType(const Type* Ty) { |
| 597 | assert(TypeStack.empty()); |
| 598 | TypeStack.clear(); |
| 599 | printTypeInternal(Ty); |
| 600 | assert(TypeStack.empty()); |
| 601 | } |
| 602 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 603 | void |
| 604 | CppWriter::printTypes(const Module* M) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 605 | |
| 606 | // Walk the symbol table and print out all its types |
| 607 | const SymbolTable& symtab = M->getSymbolTable(); |
| 608 | for (SymbolTable::type_const_iterator TI = symtab.type_begin(), |
| 609 | TE = symtab.type_end(); TI != TE; ++TI) { |
| 610 | |
| 611 | // For primitive types and types already defined, just add a name |
| 612 | TypeMap::const_iterator TNI = TypeNames.find(TI->second); |
| 613 | if (TI->second->isPrimitiveType() || TNI != TypeNames.end()) { |
| 614 | Out << "mod->addTypeName(\""; |
| 615 | printEscapedString(TI->first); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 616 | Out << "\", " << getCppName(TI->second) << ");"; |
| 617 | nl(Out); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 618 | // For everything else, define the type |
| 619 | } else { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 620 | printType(TI->second); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 624 | // Add all of the global variables to the value table... |
| 625 | for (Module::const_global_iterator I = TheModule->global_begin(), |
| 626 | E = TheModule->global_end(); I != E; ++I) { |
| 627 | if (I->hasInitializer()) |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 628 | printType(I->getInitializer()->getType()); |
| 629 | printType(I->getType()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | // Add all the functions to the table |
| 633 | for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 634 | FI != FE; ++FI) { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 635 | printType(FI->getReturnType()); |
| 636 | printType(FI->getFunctionType()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 637 | // Add all the function arguments |
| 638 | for(Function::const_arg_iterator AI = FI->arg_begin(), |
| 639 | AE = FI->arg_end(); AI != AE; ++AI) { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 640 | printType(AI->getType()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | // Add all of the basic blocks and instructions |
| 644 | for (Function::const_iterator BB = FI->begin(), |
| 645 | E = FI->end(); BB != E; ++BB) { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 646 | printType(BB->getType()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 647 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; |
| 648 | ++I) { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 649 | printType(I->getType()); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 650 | for (unsigned i = 0; i < I->getNumOperands(); ++i) |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 651 | printType(I->getOperand(i)->getType()); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 657 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 658 | // printConstant - Print out a constant pool entry... |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 659 | void CppWriter::printConstant(const Constant *CV) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 660 | // First, if the constant is actually a GlobalValue (variable or function) or |
| 661 | // its already in the constant list then we've printed it already and we can |
| 662 | // just return. |
| 663 | if (isa<GlobalValue>(CV) || ValueNames.find(CV) != ValueNames.end()) |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 664 | return; |
| 665 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 666 | std::string constName(getCppName(CV)); |
| 667 | std::string typeName(getCppName(CV->getType())); |
| 668 | if (CV->isNullValue()) { |
| 669 | Out << "Constant* " << constName << " = Constant::getNullValue(" |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 670 | << typeName << ");"; |
| 671 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 672 | return; |
| 673 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 674 | if (isa<GlobalValue>(CV)) { |
| 675 | // Skip variables and functions, we emit them elsewhere |
| 676 | return; |
| 677 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 678 | if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 679 | Out << "ConstantBool* " << constName << " = ConstantBool::get(" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 680 | << (CB == ConstantBool::True ? "true" : "false") |
| 681 | << ");"; |
| 682 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 683 | Out << "ConstantSInt* " << constName << " = ConstantSInt::get(" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 684 | << typeName << ", " << CI->getValue() << ");"; |
| 685 | } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 686 | Out << "ConstantUInt* " << constName << " = ConstantUInt::get(" |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 687 | << typeName << ", " << CI->getValue() << ");"; |
| 688 | } else if (isa<ConstantAggregateZero>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 689 | Out << "ConstantAggregateZero* " << constName |
| 690 | << " = ConstantAggregateZero::get(" << typeName << ");"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 691 | } else if (isa<ConstantPointerNull>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 692 | Out << "ConstantPointerNull* " << constName |
| 693 | << " = ConstanPointerNull::get(" << typeName << ");"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 694 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 695 | Out << "ConstantFP* " << constName << " = "; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 696 | printCFP(CFP); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 697 | Out << ";"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 698 | } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 699 | if (CA->isString() && CA->getType()->getElementType() == Type::SByteTy) { |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 700 | Out << "Constant* " << constName << " = ConstantArray::get(\""; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 701 | printEscapedString(CA->getAsString()); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 702 | // Determine if we want null termination or not. |
| 703 | if (CA->getType()->getNumElements() <= CA->getAsString().length()) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 704 | Out << "\", false";// No null terminator |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 705 | else |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 706 | Out << "\", true"; // Indicate that the null terminator should be added. |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 707 | Out << ");"; |
| 708 | } else { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 709 | Out << "std::vector<Constant*> " << constName << "_elems;"; |
| 710 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 711 | unsigned N = CA->getNumOperands(); |
| 712 | for (unsigned i = 0; i < N; ++i) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 713 | printConstant(CA->getOperand(i)); // recurse to print operands |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 714 | Out << constName << "_elems.push_back(" |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 715 | << getCppName(CA->getOperand(i)) << ");"; |
| 716 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 717 | } |
| 718 | Out << "Constant* " << constName << " = ConstantArray::get(" |
| 719 | << typeName << ", " << constName << "_elems);"; |
| 720 | } |
| 721 | } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 722 | Out << "std::vector<Constant*> " << constName << "_fields;"; |
| 723 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 724 | unsigned N = CS->getNumOperands(); |
| 725 | for (unsigned i = 0; i < N; i++) { |
| 726 | printConstant(CS->getOperand(i)); |
| 727 | Out << constName << "_fields.push_back(" |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 728 | << getCppName(CS->getOperand(i)) << ");"; |
| 729 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 730 | } |
| 731 | Out << "Constant* " << constName << " = ConstantStruct::get(" |
| 732 | << typeName << ", " << constName << "_fields);"; |
| 733 | } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 734 | Out << "std::vector<Constant*> " << constName << "_elems;"; |
| 735 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 736 | unsigned N = CP->getNumOperands(); |
| 737 | for (unsigned i = 0; i < N; ++i) { |
| 738 | printConstant(CP->getOperand(i)); |
| 739 | Out << constName << "_elems.push_back(" |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 740 | << getCppName(CP->getOperand(i)) << ");"; |
| 741 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 742 | } |
| 743 | Out << "Constant* " << constName << " = ConstantPacked::get(" |
| 744 | << typeName << ", " << constName << "_elems);"; |
| 745 | } else if (isa<UndefValue>(CV)) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 746 | Out << "UndefValue* " << constName << " = UndefValue::get(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 747 | << typeName << ");"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 748 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 749 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 750 | Out << "std::vector<Constant*> " << constName << "_indices;"; |
| 751 | nl(Out); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 752 | printConstant(CE->getOperand(0)); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 753 | for (unsigned i = 1; i < CE->getNumOperands(); ++i ) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 754 | printConstant(CE->getOperand(i)); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 755 | Out << constName << "_indices.push_back(" |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 756 | << getCppName(CE->getOperand(i)) << ");"; |
| 757 | nl(Out); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 758 | } |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 759 | Out << "Constant* " << constName |
| 760 | << " = ConstantExpr::getGetElementPtr(" |
| 761 | << getCppName(CE->getOperand(0)) << ", " |
| 762 | << constName << "_indices);"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 763 | } else if (CE->getOpcode() == Instruction::Cast) { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 764 | printConstant(CE->getOperand(0)); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 765 | Out << "Constant* " << constName << " = ConstantExpr::getCast("; |
| 766 | Out << getCppName(CE->getOperand(0)) << ", " << getCppName(CE->getType()) |
| 767 | << ");"; |
| 768 | } else { |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 769 | unsigned N = CE->getNumOperands(); |
| 770 | for (unsigned i = 0; i < N; ++i ) { |
| 771 | printConstant(CE->getOperand(i)); |
| 772 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 773 | Out << "Constant* " << constName << " = ConstantExpr::"; |
| 774 | switch (CE->getOpcode()) { |
| 775 | case Instruction::Add: Out << "getAdd"; break; |
| 776 | case Instruction::Sub: Out << "getSub"; break; |
| 777 | case Instruction::Mul: Out << "getMul"; break; |
| 778 | case Instruction::Div: Out << "getDiv"; break; |
| 779 | case Instruction::Rem: Out << "getRem"; break; |
| 780 | case Instruction::And: Out << "getAnd"; break; |
| 781 | case Instruction::Or: Out << "getOr"; break; |
| 782 | case Instruction::Xor: Out << "getXor"; break; |
| 783 | case Instruction::SetEQ: Out << "getSetEQ"; break; |
| 784 | case Instruction::SetNE: Out << "getSetNE"; break; |
| 785 | case Instruction::SetLE: Out << "getSetLE"; break; |
| 786 | case Instruction::SetGE: Out << "getSetGE"; break; |
| 787 | case Instruction::SetLT: Out << "getSetLT"; break; |
| 788 | case Instruction::SetGT: Out << "getSetGT"; break; |
| 789 | case Instruction::Shl: Out << "getShl"; break; |
| 790 | case Instruction::Shr: Out << "getShr"; break; |
| 791 | case Instruction::Select: Out << "getSelect"; break; |
| 792 | case Instruction::ExtractElement: Out << "getExtractElement"; break; |
| 793 | case Instruction::InsertElement: Out << "getInsertElement"; break; |
| 794 | case Instruction::ShuffleVector: Out << "getShuffleVector"; break; |
| 795 | default: |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 796 | error("Invalid constant expression"); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | Out << getCppName(CE->getOperand(0)); |
| 800 | for (unsigned i = 1; i < CE->getNumOperands(); ++i) |
| 801 | Out << ", " << getCppName(CE->getOperand(i)); |
| 802 | Out << ");"; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 803 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 804 | } else { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 805 | error("Bad Constant"); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 806 | Out << "Constant* " << constName << " = 0; "; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 807 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 808 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 811 | void |
| 812 | CppWriter::printConstants(const Module* M) { |
| 813 | // Traverse all the global variables looking for constant initializers |
| 814 | for (Module::const_global_iterator I = TheModule->global_begin(), |
| 815 | E = TheModule->global_end(); I != E; ++I) |
| 816 | if (I->hasInitializer()) |
| 817 | printConstant(I->getInitializer()); |
| 818 | |
| 819 | // Traverse the LLVM functions looking for constants |
| 820 | for (Module::const_iterator FI = TheModule->begin(), FE = TheModule->end(); |
| 821 | FI != FE; ++FI) { |
| 822 | // Add all of the basic blocks and instructions |
| 823 | for (Function::const_iterator BB = FI->begin(), |
| 824 | E = FI->end(); BB != E; ++BB) { |
| 825 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; |
| 826 | ++I) { |
| 827 | for (unsigned i = 0; i < I->getNumOperands(); ++i) { |
| 828 | if (Constant* C = dyn_cast<Constant>(I->getOperand(i))) { |
| 829 | printConstant(C); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 834 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 835 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 836 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 837 | void CppWriter::printVariableUses(const GlobalVariable *GV) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 838 | nl(Out) << "// Type Definitions"; |
| 839 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 840 | printType(GV->getType()); |
| 841 | if (GV->hasInitializer()) { |
| 842 | Constant* Init = GV->getInitializer(); |
| 843 | printType(Init->getType()); |
| 844 | if (Function* F = dyn_cast<Function>(Init)) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 845 | nl(Out)<< "/ Function Declarations"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 846 | printFunctionHead(F); |
| 847 | } else if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 848 | nl(Out) << "// Global Variable Declarations"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 849 | printVariableHead(gv); |
| 850 | } else { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 851 | nl(Out) << "// Constant Definitions"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 852 | printConstant(gv); |
| 853 | } |
| 854 | if (GlobalVariable* gv = dyn_cast<GlobalVariable>(Init)) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 855 | nl(Out) << "// Global Variable Definitions"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 856 | printVariableBody(gv); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 857 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 858 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 859 | } |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 860 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 861 | void CppWriter::printVariableHead(const GlobalVariable *GV) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 862 | nl(Out) << "GlobalVariable* " << getCppName(GV); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 863 | if (is_inline) { |
| 864 | Out << " = mod->getGlobalVariable("; |
| 865 | printEscapedString(GV->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 866 | Out << ", " << getCppName(GV->getType()->getElementType()) << ",true)"; |
| 867 | nl(Out) << "if (!" << getCppName(GV) << ") {"; |
| 868 | in(); nl(Out) << getCppName(GV); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 869 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 870 | Out << " = new GlobalVariable("; |
| 871 | nl(Out) << "/*Type=*/"; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 872 | printCppName(GV->getType()->getElementType()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 873 | Out << ","; |
| 874 | nl(Out) << "/*isConstant=*/" << (GV->isConstant()?"true":"false"); |
| 875 | Out << ","; |
| 876 | nl(Out) << "/*Linkage=*/"; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 877 | printLinkageType(GV->getLinkage()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 878 | Out << ","; |
| 879 | nl(Out) << "/*Initializer=*/0, "; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 880 | if (GV->hasInitializer()) { |
| 881 | Out << "// has initializer, specified below"; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 882 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 883 | nl(Out) << "/*Name=*/\""; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 884 | printEscapedString(GV->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 885 | Out << "\","; |
| 886 | nl(Out) << "mod);"; |
| 887 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 888 | |
| 889 | if (GV->hasSection()) { |
| 890 | printCppName(GV); |
| 891 | Out << "->setSection(\""; |
| 892 | printEscapedString(GV->getSection()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 893 | Out << "\");"; |
| 894 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 895 | } |
| 896 | if (GV->getAlignment()) { |
| 897 | printCppName(GV); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 898 | Out << "->setAlignment(" << utostr(GV->getAlignment()) << ");"; |
| 899 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 900 | }; |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 901 | if (is_inline) { |
| 902 | out(); Out << "}"; nl(Out); |
| 903 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | void |
| 907 | CppWriter::printVariableBody(const GlobalVariable *GV) { |
| 908 | if (GV->hasInitializer()) { |
| 909 | printCppName(GV); |
| 910 | Out << "->setInitializer("; |
| 911 | //if (!isa<GlobalValue(GV->getInitializer())) |
| 912 | //else |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 913 | Out << getCppName(GV->getInitializer()) << ");"; |
| 914 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
| 918 | std::string |
| 919 | CppWriter::getOpName(Value* V) { |
| 920 | if (!isa<Instruction>(V) || DefinedValues.find(V) != DefinedValues.end()) |
| 921 | return getCppName(V); |
| 922 | |
| 923 | // See if its alread in the map of forward references, if so just return the |
| 924 | // name we already set up for it |
| 925 | ForwardRefMap::const_iterator I = ForwardRefs.find(V); |
| 926 | if (I != ForwardRefs.end()) |
| 927 | return I->second; |
| 928 | |
| 929 | // This is a new forward reference. Generate a unique name for it |
| 930 | std::string result(std::string("fwdref_") + utostr(uniqueNum++)); |
| 931 | |
| 932 | // Yes, this is a hack. An Argument is the smallest instantiable value that |
| 933 | // we can make as a placeholder for the real value. We'll replace these |
| 934 | // Argument instances later. |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 935 | Out << "Argument* " << result << " = new Argument(" |
| 936 | << getCppName(V->getType()) << ");"; |
| 937 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 938 | ForwardRefs[V] = result; |
| 939 | return result; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 942 | // printInstruction - This member is called for each Instruction in a function. |
| 943 | void |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 944 | CppWriter::printInstruction(const Instruction *I, const std::string& bbname) { |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 945 | std::string iName(getCppName(I)); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 946 | |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 947 | // Before we emit this instruction, we need to take care of generating any |
| 948 | // forward references. So, we get the names of all the operands in advance |
| 949 | std::string* opNames = new std::string[I->getNumOperands()]; |
| 950 | for (unsigned i = 0; i < I->getNumOperands(); i++) { |
| 951 | opNames[i] = getOpName(I->getOperand(i)); |
| 952 | } |
| 953 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 954 | switch (I->getOpcode()) { |
| 955 | case Instruction::Ret: { |
| 956 | const ReturnInst* ret = cast<ReturnInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 957 | Out << "ReturnInst* " << iName << " = new ReturnInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 958 | << (ret->getReturnValue() ? opNames[0] + ", " : "") << bbname << ");"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 959 | break; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 960 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 961 | case Instruction::Br: { |
| 962 | const BranchInst* br = cast<BranchInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 963 | Out << "BranchInst* " << iName << " = new BranchInst(" ; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 964 | if (br->getNumOperands() == 3 ) { |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 965 | Out << opNames[0] << ", " |
| 966 | << opNames[1] << ", " |
| 967 | << opNames[2] << ", "; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 968 | |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 969 | } else if (br->getNumOperands() == 1) { |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 970 | Out << opNames[0] << ", "; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 971 | } else { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 972 | error("Branch with 2 operands?"); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 973 | } |
| 974 | Out << bbname << ");"; |
| 975 | break; |
| 976 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 977 | case Instruction::Switch: { |
| 978 | const SwitchInst* sw = cast<SwitchInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 979 | Out << "SwitchInst* " << iName << " = new SwitchInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 980 | << opNames[0] << ", " |
| 981 | << opNames[1] << ", " |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 982 | << sw->getNumCases() << ", " << bbname << ");"; |
| 983 | nl(Out); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 984 | for (unsigned i = 2; i < sw->getNumOperands(); i += 2 ) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 985 | Out << iName << "->addCase(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 986 | << opNames[i] << ", " |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 987 | << opNames[i+1] << ");"; |
| 988 | nl(Out); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 989 | } |
| 990 | break; |
| 991 | } |
| 992 | case Instruction::Invoke: { |
| 993 | const InvokeInst* inv = cast<InvokeInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 994 | Out << "std::vector<Value*> " << iName << "_params;"; |
| 995 | nl(Out); |
| 996 | for (unsigned i = 3; i < inv->getNumOperands(); ++i) { |
| 997 | Out << iName << "_params.push_back(" |
| 998 | << opNames[i] << ");"; |
| 999 | nl(Out); |
| 1000 | } |
| 1001 | Out << "InvokeInst* " << iName << " = new InvokeInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1002 | << opNames[0] << ", " |
| 1003 | << opNames[1] << ", " |
| 1004 | << opNames[2] << ", " |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1005 | << iName << "_params, \""; |
| 1006 | printEscapedString(inv->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1007 | Out << "\", " << bbname << ");"; |
| 1008 | nl(Out) << iName << "->setCallingConv("; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1009 | printCallingConv(inv->getCallingConv()); |
| 1010 | Out << ");"; |
| 1011 | break; |
| 1012 | } |
| 1013 | case Instruction::Unwind: { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1014 | Out << "UnwindInst* " << iName << " = new UnwindInst(" |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1015 | << bbname << ");"; |
| 1016 | break; |
| 1017 | } |
| 1018 | case Instruction::Unreachable:{ |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1019 | Out << "UnreachableInst* " << iName << " = new UnreachableInst(" |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1020 | << bbname << ");"; |
| 1021 | break; |
| 1022 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1023 | case Instruction::Add: |
| 1024 | case Instruction::Sub: |
| 1025 | case Instruction::Mul: |
| 1026 | case Instruction::Div: |
| 1027 | case Instruction::Rem: |
| 1028 | case Instruction::And: |
| 1029 | case Instruction::Or: |
| 1030 | case Instruction::Xor: |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1031 | case Instruction::Shl: |
| 1032 | case Instruction::Shr:{ |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1033 | Out << "BinaryOperator* " << iName << " = BinaryOperator::create("; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1034 | switch (I->getOpcode()) { |
| 1035 | case Instruction::Add: Out << "Instruction::Add"; break; |
| 1036 | case Instruction::Sub: Out << "Instruction::Sub"; break; |
| 1037 | case Instruction::Mul: Out << "Instruction::Mul"; break; |
| 1038 | case Instruction::Div: Out << "Instruction::Div"; break; |
| 1039 | case Instruction::Rem: Out << "Instruction::Rem"; break; |
| 1040 | case Instruction::And: Out << "Instruction::And"; break; |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1041 | case Instruction::Or: Out << "Instruction::Or"; break; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1042 | case Instruction::Xor: Out << "Instruction::Xor"; break; |
| 1043 | case Instruction::Shl: Out << "Instruction::Shl"; break; |
| 1044 | case Instruction::Shr: Out << "Instruction::Shr"; break; |
| 1045 | default: Out << "Instruction::BadOpCode"; break; |
| 1046 | } |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1047 | Out << ", " << opNames[0] << ", " << opNames[1] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1048 | printEscapedString(I->getName()); |
| 1049 | Out << "\", " << bbname << ");"; |
| 1050 | break; |
| 1051 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1052 | case Instruction::SetEQ: |
| 1053 | case Instruction::SetNE: |
| 1054 | case Instruction::SetLE: |
| 1055 | case Instruction::SetGE: |
| 1056 | case Instruction::SetLT: |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1057 | case Instruction::SetGT: { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1058 | Out << "SetCondInst* " << iName << " = new SetCondInst("; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1059 | switch (I->getOpcode()) { |
| 1060 | case Instruction::SetEQ: Out << "Instruction::SetEQ"; break; |
| 1061 | case Instruction::SetNE: Out << "Instruction::SetNE"; break; |
| 1062 | case Instruction::SetLE: Out << "Instruction::SetLE"; break; |
| 1063 | case Instruction::SetGE: Out << "Instruction::SetGE"; break; |
| 1064 | case Instruction::SetLT: Out << "Instruction::SetLT"; break; |
| 1065 | case Instruction::SetGT: Out << "Instruction::SetGT"; break; |
| 1066 | default: Out << "Instruction::BadOpCode"; break; |
| 1067 | } |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1068 | Out << ", " << opNames[0] << ", " << opNames[1] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1069 | printEscapedString(I->getName()); |
| 1070 | Out << "\", " << bbname << ");"; |
| 1071 | break; |
| 1072 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1073 | case Instruction::Malloc: { |
| 1074 | const MallocInst* mallocI = cast<MallocInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1075 | Out << "MallocInst* " << iName << " = new MallocInst(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1076 | << getCppName(mallocI->getAllocatedType()) << ", "; |
| 1077 | if (mallocI->isArrayAllocation()) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1078 | Out << opNames[0] << ", " ; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1079 | Out << "\""; |
| 1080 | printEscapedString(mallocI->getName()); |
| 1081 | Out << "\", " << bbname << ");"; |
| 1082 | if (mallocI->getAlignment()) |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1083 | nl(Out) << iName << "->setAlignment(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1084 | << mallocI->getAlignment() << ");"; |
| 1085 | break; |
| 1086 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1087 | case Instruction::Free: { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1088 | Out << "FreeInst* " << iName << " = new FreeInst(" |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1089 | << getCppName(I->getOperand(0)) << ", " << bbname << ");"; |
| 1090 | break; |
| 1091 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1092 | case Instruction::Alloca: { |
| 1093 | const AllocaInst* allocaI = cast<AllocaInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1094 | Out << "AllocaInst* " << iName << " = new AllocaInst(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1095 | << getCppName(allocaI->getAllocatedType()) << ", "; |
| 1096 | if (allocaI->isArrayAllocation()) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1097 | Out << opNames[0] << ", "; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1098 | Out << "\""; |
| 1099 | printEscapedString(allocaI->getName()); |
| 1100 | Out << "\", " << bbname << ");"; |
| 1101 | if (allocaI->getAlignment()) |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1102 | nl(Out) << iName << "->setAlignment(" |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1103 | << allocaI->getAlignment() << ");"; |
| 1104 | break; |
| 1105 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1106 | case Instruction::Load:{ |
| 1107 | const LoadInst* load = cast<LoadInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1108 | Out << "LoadInst* " << iName << " = new LoadInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1109 | << opNames[0] << ", \""; |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1110 | printEscapedString(load->getName()); |
| 1111 | Out << "\", " << (load->isVolatile() ? "true" : "false" ) |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1112 | << ", " << bbname << ");"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1113 | break; |
| 1114 | } |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1115 | case Instruction::Store: { |
| 1116 | const StoreInst* store = cast<StoreInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1117 | Out << "StoreInst* " << iName << " = new StoreInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1118 | << opNames[0] << ", " |
| 1119 | << opNames[1] << ", " |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1120 | << (store->isVolatile() ? "true" : "false") |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1121 | << ", " << bbname << ");"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1122 | break; |
| 1123 | } |
| 1124 | case Instruction::GetElementPtr: { |
| 1125 | const GetElementPtrInst* gep = cast<GetElementPtrInst>(I); |
| 1126 | if (gep->getNumOperands() <= 2) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1127 | Out << "GetElementPtrInst* " << iName << " = new GetElementPtrInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1128 | << opNames[0]; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1129 | if (gep->getNumOperands() == 2) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1130 | Out << ", " << opNames[1]; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1131 | } else { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1132 | Out << "std::vector<Value*> " << iName << "_indices;"; |
| 1133 | nl(Out); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1134 | for (unsigned i = 1; i < gep->getNumOperands(); ++i ) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1135 | Out << iName << "_indices.push_back(" |
| 1136 | << opNames[i] << ");"; |
| 1137 | nl(Out); |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1138 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1139 | Out << "Instruction* " << iName << " = new GetElementPtrInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1140 | << opNames[0] << ", " << iName << "_indices"; |
Reid Spencer | e0d133f | 2006-05-29 18:08:06 +0000 | [diff] [blame] | 1141 | } |
| 1142 | Out << ", \""; |
| 1143 | printEscapedString(gep->getName()); |
| 1144 | Out << "\", " << bbname << ");"; |
| 1145 | break; |
| 1146 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1147 | case Instruction::PHI: { |
| 1148 | const PHINode* phi = cast<PHINode>(I); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1149 | |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1150 | Out << "PHINode* " << iName << " = new PHINode(" |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1151 | << getCppName(phi->getType()) << ", \""; |
| 1152 | printEscapedString(phi->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1153 | Out << "\", " << bbname << ");"; |
| 1154 | nl(Out) << iName << "->reserveOperandSpace(" |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1155 | << phi->getNumIncomingValues() |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1156 | << ");"; |
| 1157 | nl(Out); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1158 | for (unsigned i = 0; i < phi->getNumOperands(); i+=2) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1159 | Out << iName << "->addIncoming(" |
| 1160 | << opNames[i] << ", " << opNames[i+1] << ");"; |
| 1161 | nl(Out); |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1162 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1163 | break; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1164 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1165 | case Instruction::Cast: { |
| 1166 | const CastInst* cst = cast<CastInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1167 | Out << "CastInst* " << iName << " = new CastInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1168 | << opNames[0] << ", " |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1169 | << getCppName(cst->getType()) << ", \""; |
| 1170 | printEscapedString(cst->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1171 | Out << "\", " << bbname << ");"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1172 | break; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1173 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1174 | case Instruction::Call:{ |
| 1175 | const CallInst* call = cast<CallInst>(I); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1176 | if (InlineAsm* ila = dyn_cast<InlineAsm>(call->getOperand(0))) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1177 | Out << "InlineAsm* " << getCppName(ila) << " = InlineAsm::get(" |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1178 | << getCppName(ila->getFunctionType()) << ", \"" |
| 1179 | << ila->getAsmString() << "\", \"" |
| 1180 | << ila->getConstraintString() << "\"," |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1181 | << (ila->hasSideEffects() ? "true" : "false") << ");"; |
| 1182 | nl(Out); |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1183 | } |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1184 | if (call->getNumOperands() > 3) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1185 | Out << "std::vector<Value*> " << iName << "_params;"; |
| 1186 | nl(Out); |
| 1187 | for (unsigned i = 1; i < call->getNumOperands(); ++i) { |
| 1188 | Out << iName << "_params.push_back(" << opNames[i] << ");"; |
| 1189 | nl(Out); |
| 1190 | } |
| 1191 | Out << "CallInst* " << iName << " = new CallInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1192 | << opNames[0] << ", " << iName << "_params, \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1193 | } else if (call->getNumOperands() == 3) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1194 | Out << "CallInst* " << iName << " = new CallInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1195 | << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1196 | } else if (call->getNumOperands() == 2) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1197 | Out << "CallInst* " << iName << " = new CallInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1198 | << opNames[0] << ", " << opNames[1] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1199 | } else { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1200 | Out << "CallInst* " << iName << " = new CallInst(" << opNames[0] |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1201 | << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1202 | } |
| 1203 | printEscapedString(call->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1204 | Out << "\", " << bbname << ");"; |
| 1205 | nl(Out) << iName << "->setCallingConv("; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1206 | printCallingConv(call->getCallingConv()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1207 | Out << ");"; |
| 1208 | nl(Out) << iName << "->setTailCall(" |
Reid Spencer | 1a2a0cc | 2006-05-30 10:21:41 +0000 | [diff] [blame] | 1209 | << (call->isTailCall() ? "true":"false"); |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1210 | Out << ");"; |
| 1211 | break; |
| 1212 | } |
| 1213 | case Instruction::Select: { |
| 1214 | const SelectInst* sel = cast<SelectInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1215 | Out << "SelectInst* " << getCppName(sel) << " = new SelectInst("; |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1216 | Out << opNames[0] << ", " << opNames[1] << ", " << opNames[2] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1217 | printEscapedString(sel->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1218 | Out << "\", " << bbname << ");"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1219 | break; |
| 1220 | } |
| 1221 | case Instruction::UserOp1: |
| 1222 | /// FALL THROUGH |
| 1223 | case Instruction::UserOp2: { |
| 1224 | /// FIXME: What should be done here? |
| 1225 | break; |
| 1226 | } |
| 1227 | case Instruction::VAArg: { |
| 1228 | const VAArgInst* va = cast<VAArgInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1229 | Out << "VAArgInst* " << getCppName(va) << " = new VAArgInst(" |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1230 | << opNames[0] << ", " << getCppName(va->getType()) << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1231 | printEscapedString(va->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1232 | Out << "\", " << bbname << ");"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1233 | break; |
| 1234 | } |
| 1235 | case Instruction::ExtractElement: { |
| 1236 | const ExtractElementInst* eei = cast<ExtractElementInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1237 | Out << "ExtractElementInst* " << getCppName(eei) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1238 | << " = new ExtractElementInst(" << opNames[0] |
| 1239 | << ", " << opNames[1] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1240 | printEscapedString(eei->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1241 | Out << "\", " << bbname << ");"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1242 | break; |
| 1243 | } |
| 1244 | case Instruction::InsertElement: { |
| 1245 | const InsertElementInst* iei = cast<InsertElementInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1246 | Out << "InsertElementInst* " << getCppName(iei) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1247 | << " = new InsertElementInst(" << opNames[0] |
| 1248 | << ", " << opNames[1] << ", " << opNames[2] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1249 | printEscapedString(iei->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1250 | Out << "\", " << bbname << ");"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | } |
| 1253 | case Instruction::ShuffleVector: { |
| 1254 | const ShuffleVectorInst* svi = cast<ShuffleVectorInst>(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1255 | Out << "ShuffleVectorInst* " << getCppName(svi) |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1256 | << " = new ShuffleVectorInst(" << opNames[0] |
| 1257 | << ", " << opNames[1] << ", " << opNames[2] << ", \""; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1258 | printEscapedString(svi->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1259 | Out << "\", " << bbname << ");"; |
Reid Spencer | 66c8734 | 2006-05-30 03:43:49 +0000 | [diff] [blame] | 1260 | break; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
Reid Spencer | 68464b7 | 2006-06-15 16:09:59 +0000 | [diff] [blame] | 1263 | DefinedValues.insert(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1264 | nl(Out); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1265 | delete [] opNames; |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1268 | // Print out the types, constants and declarations needed by one function |
| 1269 | void CppWriter::printFunctionUses(const Function* F) { |
| 1270 | |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1271 | nl(Out) << "// Type Definitions"; nl(Out); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1272 | if (!is_inline) { |
| 1273 | // Print the function's return type |
| 1274 | printType(F->getReturnType()); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1275 | |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1276 | // Print the function's function type |
| 1277 | printType(F->getFunctionType()); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1278 | |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1279 | // Print the types of each of the function's arguments |
| 1280 | for(Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 1281 | AI != AE; ++AI) { |
| 1282 | printType(AI->getType()); |
| 1283 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | // Print type definitions for every type referenced by an instruction and |
| 1287 | // make a note of any global values or constants that are referenced |
| 1288 | std::vector<GlobalValue*> gvs; |
| 1289 | std::vector<Constant*> consts; |
| 1290 | for (Function::const_iterator BB = F->begin(), BE = F->end(); BB != BE; ++BB){ |
| 1291 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); |
| 1292 | I != E; ++I) { |
| 1293 | // Print the type of the instruction itself |
| 1294 | printType(I->getType()); |
| 1295 | |
| 1296 | // Print the type of each of the instruction's operands |
| 1297 | for (unsigned i = 0; i < I->getNumOperands(); ++i) { |
| 1298 | Value* operand = I->getOperand(i); |
| 1299 | printType(operand->getType()); |
| 1300 | if (GlobalValue* GV = dyn_cast<GlobalValue>(operand)) |
| 1301 | gvs.push_back(GV); |
| 1302 | else if (Constant* C = dyn_cast<Constant>(operand)) |
| 1303 | consts.push_back(C); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | // Print the function declarations for any functions encountered |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1309 | nl(Out) << "// Function Declarations"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1310 | for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end(); |
| 1311 | I != E; ++I) { |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1312 | if (Function* Fun = dyn_cast<Function>(*I)) { |
| 1313 | if (!is_inline || Fun != F) |
| 1314 | printFunctionHead(Fun); |
| 1315 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | // Print the global variable declarations for any variables encountered |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1319 | nl(Out) << "// Global Variable Declarations"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1320 | for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end(); |
| 1321 | I != E; ++I) { |
| 1322 | if (GlobalVariable* F = dyn_cast<GlobalVariable>(*I)) |
| 1323 | printVariableHead(F); |
| 1324 | } |
| 1325 | |
| 1326 | // Print the constants found |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1327 | nl(Out) << "// Constant Definitions"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1328 | for (std::vector<Constant*>::iterator I = consts.begin(), E = consts.end(); |
| 1329 | I != E; ++I) { |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1330 | printConstant(*I); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | // Process the global variables definitions now that all the constants have |
| 1334 | // been emitted. These definitions just couple the gvars with their constant |
| 1335 | // initializers. |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1336 | nl(Out) << "// Global Variable Definitions"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1337 | for (std::vector<GlobalValue*>::iterator I = gvs.begin(), E = gvs.end(); |
| 1338 | I != E; ++I) { |
| 1339 | if (GlobalVariable* GV = dyn_cast<GlobalVariable>(*I)) |
| 1340 | printVariableBody(GV); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | void CppWriter::printFunctionHead(const Function* F) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1345 | nl(Out) << "Function* " << getCppName(F); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1346 | if (is_inline) { |
| 1347 | Out << " = mod->getFunction(\""; |
| 1348 | printEscapedString(F->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1349 | Out << "\", " << getCppName(F->getFunctionType()) << ");"; |
| 1350 | nl(Out) << "if (!" << getCppName(F) << ") {"; |
| 1351 | nl(Out) << getCppName(F); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1352 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1353 | Out<< " = new Function("; |
| 1354 | nl(Out,1) << "/*Type=*/" << getCppName(F->getFunctionType()) << ","; |
| 1355 | nl(Out) << "/*Linkage=*/"; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1356 | printLinkageType(F->getLinkage()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1357 | Out << ","; |
| 1358 | nl(Out) << "/*Name=*/\""; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1359 | printEscapedString(F->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1360 | Out << "\", mod); " << (F->isExternal()? "// (external, no body)" : ""); |
| 1361 | nl(Out,-1); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1362 | printCppName(F); |
| 1363 | Out << "->setCallingConv("; |
| 1364 | printCallingConv(F->getCallingConv()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1365 | Out << ");"; |
| 1366 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1367 | if (F->hasSection()) { |
| 1368 | printCppName(F); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1369 | Out << "->setSection(\"" << F->getSection() << "\");"; |
| 1370 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1371 | } |
| 1372 | if (F->getAlignment()) { |
| 1373 | printCppName(F); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1374 | Out << "->setAlignment(" << F->getAlignment() << ");"; |
| 1375 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1376 | } |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1377 | if (is_inline) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1378 | Out << "}"; |
| 1379 | nl(Out); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1380 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | void CppWriter::printFunctionBody(const Function *F) { |
| 1384 | if (F->isExternal()) |
| 1385 | return; // external functions have no bodies. |
| 1386 | |
| 1387 | // Clear the DefinedValues and ForwardRefs maps because we can't have |
| 1388 | // cross-function forward refs |
| 1389 | ForwardRefs.clear(); |
| 1390 | DefinedValues.clear(); |
| 1391 | |
| 1392 | // Create all the argument values |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1393 | if (!is_inline) { |
| 1394 | if (!F->arg_empty()) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1395 | Out << "Function::arg_iterator args = " << getCppName(F) |
| 1396 | << "->arg_begin();"; |
| 1397 | nl(Out); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1398 | } |
| 1399 | for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 1400 | AI != AE; ++AI) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1401 | Out << "Value* " << getCppName(AI) << " = args++;"; |
| 1402 | nl(Out); |
| 1403 | if (AI->hasName()) { |
| 1404 | Out << getCppName(AI) << "->setName(\"" << AI->getName() << "\");"; |
| 1405 | nl(Out); |
| 1406 | } |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1407 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | // Create all the basic blocks |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1411 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1412 | for (Function::const_iterator BI = F->begin(), BE = F->end(); |
| 1413 | BI != BE; ++BI) { |
| 1414 | std::string bbname(getCppName(BI)); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1415 | Out << "BasicBlock* " << bbname << " = new BasicBlock(\""; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1416 | if (BI->hasName()) |
| 1417 | printEscapedString(BI->getName()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1418 | Out << "\"," << getCppName(BI->getParent()) << ",0);"; |
| 1419 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | // Output all of its basic blocks... for the function |
| 1423 | for (Function::const_iterator BI = F->begin(), BE = F->end(); |
| 1424 | BI != BE; ++BI) { |
| 1425 | std::string bbname(getCppName(BI)); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1426 | nl(Out) << "// Block " << BI->getName() << " (" << bbname << ")"; |
| 1427 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1428 | |
| 1429 | // Output all of the instructions in the basic block... |
| 1430 | for (BasicBlock::const_iterator I = BI->begin(), E = BI->end(); |
| 1431 | I != E; ++I) { |
| 1432 | printInstruction(I,bbname); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | // Loop over the ForwardRefs and resolve them now that all instructions |
| 1437 | // are generated. |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1438 | if (!ForwardRefs.empty()) { |
| 1439 | nl(Out) << "// Resolve Forward References"; |
| 1440 | nl(Out); |
| 1441 | } |
| 1442 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1443 | while (!ForwardRefs.empty()) { |
| 1444 | ForwardRefMap::iterator I = ForwardRefs.begin(); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1445 | Out << I->second << "->replaceAllUsesWith(" |
| 1446 | << getCppName(I->first) << "); delete " << I->second << ";"; |
| 1447 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1448 | ForwardRefs.erase(I); |
| 1449 | } |
| 1450 | } |
| 1451 | |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1452 | void CppWriter::printInline(const std::string& fname, const std::string& func) { |
| 1453 | const Function* F = TheModule->getNamedFunction(func); |
| 1454 | if (!F) { |
| 1455 | error(std::string("Function '") + func + "' not found in input module"); |
| 1456 | return; |
| 1457 | } |
| 1458 | if (F->isExternal()) { |
| 1459 | error(std::string("Function '") + func + "' is external!"); |
| 1460 | return; |
| 1461 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1462 | nl(Out) << "BasicBlock* " << fname << "(Module* mod, Function *" |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1463 | << getCppName(F); |
| 1464 | unsigned arg_count = 1; |
| 1465 | for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 1466 | AI != AE; ++AI) { |
| 1467 | Out << ", Value* arg_" << arg_count; |
| 1468 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1469 | Out << ") {"; |
| 1470 | nl(Out); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1471 | is_inline = true; |
| 1472 | printFunctionUses(F); |
| 1473 | printFunctionBody(F); |
| 1474 | is_inline = false; |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1475 | Out << "return " << getCppName(F->begin()) << ";"; |
| 1476 | nl(Out) << "}"; |
| 1477 | nl(Out); |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1480 | void CppWriter::printModuleBody() { |
| 1481 | // Print out all the type definitions |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1482 | nl(Out) << "// Type Definitions"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1483 | printTypes(TheModule); |
| 1484 | |
| 1485 | // Functions can call each other and global variables can reference them so |
| 1486 | // define all the functions first before emitting their function bodies. |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1487 | nl(Out) << "// Function Declarations"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1488 | for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); |
| 1489 | I != E; ++I) |
| 1490 | printFunctionHead(I); |
| 1491 | |
| 1492 | // Process the global variables declarations. We can't initialze them until |
| 1493 | // after the constants are printed so just print a header for each global |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1494 | nl(Out) << "// Global Variable Declarations\n"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1495 | for (Module::const_global_iterator I = TheModule->global_begin(), |
| 1496 | E = TheModule->global_end(); I != E; ++I) { |
| 1497 | printVariableHead(I); |
| 1498 | } |
| 1499 | |
| 1500 | // Print out all the constants definitions. Constants don't recurse except |
| 1501 | // through GlobalValues. All GlobalValues have been declared at this point |
| 1502 | // so we can proceed to generate the constants. |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1503 | nl(Out) << "// Constant Definitions"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1504 | printConstants(TheModule); |
| 1505 | |
| 1506 | // Process the global variables definitions now that all the constants have |
| 1507 | // been emitted. These definitions just couple the gvars with their constant |
| 1508 | // initializers. |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1509 | nl(Out) << "// Global Variable Definitions"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1510 | for (Module::const_global_iterator I = TheModule->global_begin(), |
| 1511 | E = TheModule->global_end(); I != E; ++I) { |
| 1512 | printVariableBody(I); |
| 1513 | } |
| 1514 | |
| 1515 | // Finally, we can safely put out all of the function bodies. |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1516 | nl(Out) << "// Function Definitions"; nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1517 | for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); |
| 1518 | I != E; ++I) { |
| 1519 | if (!I->isExternal()) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1520 | nl(Out) << "// Function: " << I->getName() << " (" << getCppName(I) |
| 1521 | << ")"; |
| 1522 | nl(Out) << "{"; |
| 1523 | nl(Out,1); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1524 | printFunctionBody(I); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1525 | nl(Out,-1) << "}"; |
| 1526 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | void CppWriter::printProgram( |
| 1532 | const std::string& fname, |
| 1533 | const std::string& mName |
| 1534 | ) { |
| 1535 | Out << "#include <llvm/Module.h>\n"; |
| 1536 | Out << "#include <llvm/DerivedTypes.h>\n"; |
| 1537 | Out << "#include <llvm/Constants.h>\n"; |
| 1538 | Out << "#include <llvm/GlobalVariable.h>\n"; |
| 1539 | Out << "#include <llvm/Function.h>\n"; |
| 1540 | Out << "#include <llvm/CallingConv.h>\n"; |
| 1541 | Out << "#include <llvm/BasicBlock.h>\n"; |
| 1542 | Out << "#include <llvm/Instructions.h>\n"; |
| 1543 | Out << "#include <llvm/InlineAsm.h>\n"; |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1544 | Out << "#include <llvm/Support/MathExtras.h>\n"; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1545 | Out << "#include <llvm/Pass.h>\n"; |
| 1546 | Out << "#include <llvm/PassManager.h>\n"; |
| 1547 | Out << "#include <llvm/Analysis/Verifier.h>\n"; |
| 1548 | Out << "#include <llvm/Assembly/PrintModulePass.h>\n"; |
| 1549 | Out << "#include <algorithm>\n"; |
| 1550 | Out << "#include <iostream>\n\n"; |
| 1551 | Out << "using namespace llvm;\n\n"; |
| 1552 | Out << "Module* " << fname << "();\n\n"; |
| 1553 | Out << "int main(int argc, char**argv) {\n"; |
| 1554 | Out << " Module* Mod = makeLLVMModule();\n"; |
| 1555 | Out << " verifyModule(*Mod, PrintMessageAction);\n"; |
| 1556 | Out << " std::cerr.flush();\n"; |
| 1557 | Out << " std::cout.flush();\n"; |
| 1558 | Out << " PassManager PM;\n"; |
| 1559 | Out << " PM.add(new PrintModulePass(&std::cout));\n"; |
| 1560 | Out << " PM.run(*Mod);\n"; |
| 1561 | Out << " return 0;\n"; |
| 1562 | Out << "}\n\n"; |
| 1563 | printModule(fname,mName); |
| 1564 | } |
| 1565 | |
| 1566 | void CppWriter::printModule( |
| 1567 | const std::string& fname, |
| 1568 | const std::string& mName |
| 1569 | ) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1570 | nl(Out) << "Module* " << fname << "() {"; |
| 1571 | nl(Out,1) << "// Module Construction"; |
| 1572 | nl(Out) << "Module* mod = new Module(\"" << mName << "\");"; |
| 1573 | nl(Out) << "mod->setEndianness("; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1574 | switch (TheModule->getEndianness()) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1575 | case Module::LittleEndian: Out << "Module::LittleEndian);"; break; |
| 1576 | case Module::BigEndian: Out << "Module::BigEndian);"; break; |
| 1577 | case Module::AnyEndianness:Out << "Module::AnyEndianness);"; break; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1578 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1579 | nl(Out) << "mod->setPointerSize("; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1580 | switch (TheModule->getPointerSize()) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1581 | case Module::Pointer32: Out << "Module::Pointer32);"; break; |
| 1582 | case Module::Pointer64: Out << "Module::Pointer64);"; break; |
| 1583 | case Module::AnyPointerSize: Out << "Module::AnyPointerSize);"; break; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1584 | } |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1585 | nl(Out); |
| 1586 | if (!TheModule->getTargetTriple().empty()) { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1587 | Out << "mod->setTargetTriple(\"" << TheModule->getTargetTriple() |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1588 | << "\");"; |
| 1589 | nl(Out); |
| 1590 | } |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1591 | |
| 1592 | if (!TheModule->getModuleInlineAsm().empty()) { |
| 1593 | Out << "mod->setModuleInlineAsm(\""; |
| 1594 | printEscapedString(TheModule->getModuleInlineAsm()); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1595 | Out << "\");"; |
| 1596 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | // Loop over the dependent libraries and emit them. |
| 1600 | Module::lib_iterator LI = TheModule->lib_begin(); |
| 1601 | Module::lib_iterator LE = TheModule->lib_end(); |
| 1602 | while (LI != LE) { |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1603 | Out << "mod->addLibrary(\"" << *LI << "\");"; |
| 1604 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1605 | ++LI; |
| 1606 | } |
| 1607 | printModuleBody(); |
Reid Spencer | 70bbf9a | 2006-08-14 22:35:15 +0000 | [diff] [blame] | 1608 | nl(Out) << "return mod;"; |
| 1609 | nl(Out,-1) << "}"; |
| 1610 | nl(Out); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | void CppWriter::printContents( |
| 1614 | const std::string& fname, // Name of generated function |
| 1615 | const std::string& mName // Name of module generated module |
| 1616 | ) { |
| 1617 | Out << "\nModule* " << fname << "(Module *mod) {\n"; |
| 1618 | Out << "\nmod->setModuleIdentifier(\"" << mName << "\");\n"; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1619 | printModuleBody(); |
| 1620 | Out << "\nreturn mod;\n"; |
| 1621 | Out << "\n}\n"; |
| 1622 | } |
| 1623 | |
| 1624 | void CppWriter::printFunction( |
| 1625 | const std::string& fname, // Name of generated function |
| 1626 | const std::string& funcName // Name of function to generate |
| 1627 | ) { |
| 1628 | const Function* F = TheModule->getNamedFunction(funcName); |
| 1629 | if (!F) { |
| 1630 | error(std::string("Function '") + funcName + "' not found in input module"); |
| 1631 | return; |
| 1632 | } |
| 1633 | Out << "\nFunction* " << fname << "(Module *mod) {\n"; |
| 1634 | printFunctionUses(F); |
| 1635 | printFunctionHead(F); |
| 1636 | printFunctionBody(F); |
| 1637 | Out << "return " << getCppName(F) << ";\n"; |
| 1638 | Out << "}\n"; |
| 1639 | } |
| 1640 | |
| 1641 | void CppWriter::printVariable( |
| 1642 | const std::string& fname, /// Name of generated function |
| 1643 | const std::string& varName // Name of variable to generate |
| 1644 | ) { |
| 1645 | const GlobalVariable* GV = TheModule->getNamedGlobal(varName); |
| 1646 | |
| 1647 | if (!GV) { |
| 1648 | error(std::string("Variable '") + varName + "' not found in input module"); |
| 1649 | return; |
| 1650 | } |
| 1651 | Out << "\nGlobalVariable* " << fname << "(Module *mod) {\n"; |
| 1652 | printVariableUses(GV); |
| 1653 | printVariableHead(GV); |
| 1654 | printVariableBody(GV); |
| 1655 | Out << "return " << getCppName(GV) << ";\n"; |
| 1656 | Out << "}\n"; |
| 1657 | } |
| 1658 | |
| 1659 | void CppWriter::printType( |
| 1660 | const std::string& fname, /// Name of generated function |
| 1661 | const std::string& typeName // Name of type to generate |
| 1662 | ) { |
| 1663 | const Type* Ty = TheModule->getTypeByName(typeName); |
| 1664 | if (!Ty) { |
| 1665 | error(std::string("Type '") + typeName + "' not found in input module"); |
| 1666 | return; |
| 1667 | } |
| 1668 | Out << "\nType* " << fname << "(Module *mod) {\n"; |
| 1669 | printType(Ty); |
| 1670 | Out << "return " << getCppName(Ty) << ";\n"; |
| 1671 | Out << "}\n"; |
| 1672 | } |
| 1673 | |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1674 | } // end anonymous llvm |
| 1675 | |
| 1676 | namespace llvm { |
| 1677 | |
| 1678 | void WriteModuleToCppFile(Module* mod, std::ostream& o) { |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1679 | // Initialize a CppWriter for us to use |
| 1680 | CppWriter W(o, mod); |
| 1681 | |
| 1682 | // Emit a header |
Reid Spencer | 25edc35 | 2006-05-31 04:43:19 +0000 | [diff] [blame] | 1683 | o << "// Generated by llvm2cpp - DO NOT MODIFY!\n\n"; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1684 | |
| 1685 | // Get the name of the function we're supposed to generate |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1686 | std::string fname = FuncName.getValue(); |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Get the name of the thing we are to generate |
| 1689 | std::string tgtname = NameToGenerate.getValue(); |
| 1690 | if (GenerationType == GenModule || |
| 1691 | GenerationType == GenContents || |
| 1692 | GenerationType == GenProgram) { |
| 1693 | if (tgtname == "!bad!") { |
| 1694 | if (mod->getModuleIdentifier() == "-") |
| 1695 | tgtname = "<stdin>"; |
| 1696 | else |
| 1697 | tgtname = mod->getModuleIdentifier(); |
| 1698 | } |
| 1699 | } else if (tgtname == "!bad!") { |
| 1700 | W.error("You must use the -for option with -gen-{function,variable,type}"); |
| 1701 | } |
| 1702 | |
| 1703 | switch (WhatToGenerate(GenerationType)) { |
| 1704 | case GenProgram: |
| 1705 | if (fname.empty()) |
| 1706 | fname = "makeLLVMModule"; |
| 1707 | W.printProgram(fname,tgtname); |
| 1708 | break; |
| 1709 | case GenModule: |
| 1710 | if (fname.empty()) |
| 1711 | fname = "makeLLVMModule"; |
| 1712 | W.printModule(fname,tgtname); |
| 1713 | break; |
| 1714 | case GenContents: |
| 1715 | if (fname.empty()) |
| 1716 | fname = "makeLLVMModuleContents"; |
| 1717 | W.printContents(fname,tgtname); |
| 1718 | break; |
| 1719 | case GenFunction: |
| 1720 | if (fname.empty()) |
| 1721 | fname = "makeLLVMFunction"; |
| 1722 | W.printFunction(fname,tgtname); |
| 1723 | break; |
Reid Spencer | f977e7b | 2006-06-01 23:43:47 +0000 | [diff] [blame] | 1724 | case GenInline: |
| 1725 | if (fname.empty()) |
| 1726 | fname = "makeLLVMInline"; |
| 1727 | W.printInline(fname,tgtname); |
| 1728 | break; |
Reid Spencer | 12803f5 | 2006-05-31 17:31:38 +0000 | [diff] [blame] | 1729 | case GenVariable: |
| 1730 | if (fname.empty()) |
| 1731 | fname = "makeLLVMVariable"; |
| 1732 | W.printVariable(fname,tgtname); |
| 1733 | break; |
| 1734 | case GenType: |
| 1735 | if (fname.empty()) |
| 1736 | fname = "makeLLVMType"; |
| 1737 | W.printType(fname,tgtname); |
| 1738 | break; |
| 1739 | default: |
| 1740 | W.error("Invalid generation option"); |
Reid Spencer | 15f7e01 | 2006-05-30 21:18:23 +0000 | [diff] [blame] | 1741 | } |
Reid Spencer | fb0c0dc | 2006-05-29 00:57:22 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | } |