Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Writer.cpp - Library for Printing VM assembly files ------*- C++ -*--=// |
| 2 | // |
| 3 | // This library implements the functionality defined in llvm/Assembly/Writer.h |
| 4 | // |
| 5 | // This library uses the Analysis library to figure out offsets for |
| 6 | // variables in the method tables... |
| 7 | // |
| 8 | // TODO: print out the type name instead of the full type if a particular type |
| 9 | // is in the symbol table... |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Assembly/Writer.h" |
| 14 | #include "llvm/Analysis/SlotCalculator.h" |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Method.h" |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 18 | #include "llvm/BasicBlock.h" |
| 19 | #include "llvm/ConstPoolVals.h" |
| 20 | #include "llvm/iOther.h" |
| 21 | #include "llvm/iMemory.h" |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 22 | #include "llvm/iTerminators.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 23 | #include "llvm/SymbolTable.h" |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 24 | #include "llvm/Support/STLExtras.h" |
| 25 | #include "llvm/Support/StringExtras.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 27 | #include <map> |
| 28 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 29 | static const Module *getModuleFromVal(const Value *V) { |
| 30 | if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V)) |
| 31 | return MA->getParent() ? MA->getParent()->getParent() : 0; |
| 32 | else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) |
| 33 | return BB->getParent() ? BB->getParent()->getParent() : 0; |
| 34 | else if (const Instruction *I = dyn_cast<const Instruction>(V)) { |
| 35 | const Method *M = I->getParent() ? I->getParent()->getParent() : 0; |
| 36 | return M ? M->getParent() : 0; |
| 37 | } else if (const GlobalValue *GV =dyn_cast<const GlobalValue>(V)) |
| 38 | return GV->getParent(); |
| 39 | else if (const Module *Mod = dyn_cast<const Module>(V)) |
| 40 | return Mod; |
| 41 | return 0; |
| 42 | } |
| 43 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 44 | static SlotCalculator *createSlotCalculator(const Value *V) { |
| 45 | assert(!isa<Type>(V) && "Can't create an SC for a type!"); |
| 46 | if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V)){ |
| 47 | return new SlotCalculator(MA->getParent(), true); |
| 48 | } else if (const Instruction *I = dyn_cast<const Instruction>(V)) { |
| 49 | return new SlotCalculator(I->getParent()->getParent(), true); |
| 50 | } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) { |
| 51 | return new SlotCalculator(BB->getParent(), true); |
| 52 | } else if (const GlobalVariable *GV =dyn_cast<const GlobalVariable>(V)){ |
| 53 | return new SlotCalculator(GV->getParent(), true); |
| 54 | } else if (const Method *Meth = dyn_cast<const Method>(V)) { |
| 55 | return new SlotCalculator(Meth, true); |
| 56 | } else if (const Module *Mod = dyn_cast<const Module>(V)) { |
| 57 | return new SlotCalculator(Mod, true); |
| 58 | } |
| 59 | return 0; |
| 60 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 62 | // WriteAsOperand - Write the name of the specified value out to the specified |
| 63 | // ostream. This can be useful when you just want to print int %reg126, not the |
| 64 | // whole instruction that generated it. |
| 65 | // |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 66 | static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName, |
| 67 | SlotCalculator *Table) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 68 | if (PrintName && V->hasName()) { |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 69 | Out << " %" << V->getName(); |
| 70 | } else { |
Chris Lattner | 4b717c0 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 71 | if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) { |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 72 | Out << " " << CPV->getStrValue(); |
| 73 | } else { |
| 74 | int Slot; |
| 75 | if (Table) { |
| 76 | Slot = Table->getValSlot(V); |
| 77 | } else { |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 78 | if (const Type *Ty = dyn_cast<const Type>(V)) { |
| 79 | Out << " " << Ty->getDescription(); |
| 80 | return; |
| 81 | } |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 82 | |
| 83 | Table = createSlotCalculator(V); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 84 | if (Table == 0) { Out << "BAD VALUE TYPE!"; return; } |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 86 | Slot = Table->getValSlot(V); |
| 87 | delete Table; |
| 88 | } |
| 89 | if (Slot >= 0) Out << " %" << Slot; |
| 90 | else if (PrintName) |
| 91 | Out << "<badref>"; // Not embeded into a location? |
| 92 | } |
| 93 | } |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
| 97 | // If the module has a symbol table, take all global types and stuff their |
| 98 | // names into the TypeNames map. |
| 99 | // |
| 100 | static void fillTypeNameTable(const Module *M, |
| 101 | map<const Type *, string> &TypeNames) { |
| 102 | if (M && M->hasSymbolTable()) { |
| 103 | const SymbolTable *ST = M->getSymbolTable(); |
| 104 | SymbolTable::const_iterator PI = ST->find(Type::TypeTy); |
| 105 | if (PI != ST->end()) { |
| 106 | SymbolTable::type_const_iterator I = PI->second.begin(); |
| 107 | for (; I != PI->second.end(); ++I) { |
| 108 | // As a heuristic, don't insert pointer to primitive types, because |
| 109 | // they are used too often to have a single useful name. |
| 110 | // |
| 111 | const Type *Ty = cast<const Type>(I->second); |
| 112 | if (!isa<PointerType>(Ty) || |
| 113 | !cast<PointerType>(Ty)->getValueType()->isPrimitiveType()) |
| 114 | TypeNames.insert(make_pair(Ty, "%"+I->first)); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack, |
| 123 | map<const Type *, string> &TypeNames) { |
| 124 | if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case |
| 125 | |
| 126 | // Check to see if the type is named. |
| 127 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 128 | if (I != TypeNames.end()) return I->second; |
| 129 | |
| 130 | // Check to see if the Type is already on the stack... |
| 131 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 132 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 133 | |
| 134 | // This is another base case for the recursion. In this case, we know |
| 135 | // that we have looped back to a type that we have previously visited. |
| 136 | // Generate the appropriate upreference to handle this. |
| 137 | // |
| 138 | if (Slot < CurSize) |
| 139 | return "\\" + utostr(CurSize-Slot); // Here's the upreference |
| 140 | |
| 141 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
| 142 | |
| 143 | string Result; |
| 144 | switch (Ty->getPrimitiveID()) { |
| 145 | case Type::MethodTyID: { |
| 146 | const MethodType *MTy = cast<const MethodType>(Ty); |
| 147 | Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " ("; |
| 148 | for (MethodType::ParamTypes::const_iterator |
| 149 | I = MTy->getParamTypes().begin(), |
| 150 | E = MTy->getParamTypes().end(); I != E; ++I) { |
| 151 | if (I != MTy->getParamTypes().begin()) |
| 152 | Result += ", "; |
| 153 | Result += calcTypeName(*I, TypeStack, TypeNames); |
| 154 | } |
| 155 | if (MTy->isVarArg()) { |
| 156 | if (!MTy->getParamTypes().empty()) Result += ", "; |
| 157 | Result += "..."; |
| 158 | } |
| 159 | Result += ")"; |
| 160 | break; |
| 161 | } |
| 162 | case Type::StructTyID: { |
| 163 | const StructType *STy = cast<const StructType>(Ty); |
| 164 | Result = "{ "; |
| 165 | for (StructType::ElementTypes::const_iterator |
| 166 | I = STy->getElementTypes().begin(), |
| 167 | E = STy->getElementTypes().end(); I != E; ++I) { |
| 168 | if (I != STy->getElementTypes().begin()) |
| 169 | Result += ", "; |
| 170 | Result += calcTypeName(*I, TypeStack, TypeNames); |
| 171 | } |
| 172 | Result += " }"; |
| 173 | break; |
| 174 | } |
| 175 | case Type::PointerTyID: |
| 176 | Result = calcTypeName(cast<const PointerType>(Ty)->getValueType(), |
| 177 | TypeStack, TypeNames) + " *"; |
| 178 | break; |
| 179 | case Type::ArrayTyID: { |
| 180 | const ArrayType *ATy = cast<const ArrayType>(Ty); |
| 181 | int NumElements = ATy->getNumElements(); |
| 182 | Result = "["; |
| 183 | if (NumElements != -1) Result += itostr(NumElements) + " x "; |
| 184 | Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]"; |
| 185 | break; |
| 186 | } |
| 187 | default: |
| 188 | assert(0 && "Unhandled case in getTypeProps!"); |
| 189 | Result = "<error>"; |
| 190 | } |
| 191 | |
| 192 | TypeStack.pop_back(); // Remove self from stack... |
| 193 | return Result; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | // printTypeInt - The internal guts of printing out a type that has a |
| 198 | // potentially named portion. |
| 199 | // |
| 200 | static ostream &printTypeInt(ostream &Out, const Type *Ty, |
| 201 | map<const Type *, string> &TypeNames) { |
| 202 | // Primitive types always print out their description, regardless of whether |
| 203 | // they have been named or not. |
| 204 | // |
| 205 | if (Ty->isPrimitiveType()) return Out << Ty->getDescription(); |
| 206 | |
| 207 | // Check to see if the type is named. |
| 208 | map<const Type *, string>::iterator I = TypeNames.find(Ty); |
| 209 | if (I != TypeNames.end()) return Out << I->second; |
| 210 | |
| 211 | // Otherwise we have a type that has not been named but is a derived type. |
| 212 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 213 | // names. |
| 214 | // |
| 215 | vector<const Type *> TypeStack; |
| 216 | string TypeName = calcTypeName(Ty, TypeStack, TypeNames); |
| 217 | TypeNames.insert(make_pair(Ty, TypeName)); // Cache type name for later use |
| 218 | return Out << TypeName; |
| 219 | } |
| 220 | |
| 221 | // WriteTypeSymbolic - This attempts to write the specified type as a symbolic |
| 222 | // type, iff there is an entry in the modules symbol table for the specified |
| 223 | // type or one of it's component types. This is slower than a simple x << Type; |
| 224 | // |
| 225 | ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) { |
| 226 | Out << " "; |
| 227 | |
| 228 | // If they want us to print out a type, attempt to make it symbolic if there |
| 229 | // is a symbol table in the module... |
| 230 | if (M && M->hasSymbolTable()) { |
| 231 | map<const Type *, string> TypeNames; |
| 232 | fillTypeNameTable(M, TypeNames); |
| 233 | |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame^] | 234 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 235 | } else { |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame^] | 236 | return Out << Ty->getDescription(); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | // WriteAsOperand - Write the name of the specified value out to the specified |
| 242 | // ostream. This can be useful when you just want to print int %reg126, not the |
| 243 | // whole instruction that generated it. |
| 244 | // |
| 245 | ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType, |
| 246 | bool PrintName, SlotCalculator *Table) { |
Chris Lattner | 72f866e | 2001-10-29 16:40:32 +0000 | [diff] [blame^] | 247 | if (PrintType) |
| 248 | WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V)); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 249 | |
| 250 | WriteAsOperandInternal(Out, V, PrintName, Table); |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 251 | return Out; |
| 252 | } |
| 253 | |
| 254 | |
Chris Lattner | 2e9fee4 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 255 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 256 | class AssemblyWriter { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 257 | ostream &Out; |
| 258 | SlotCalculator &Table; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 259 | const Module *TheModule; |
| 260 | map<const Type *, string> TypeNames; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 261 | public: |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 262 | inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M) |
| 263 | : Out(o), Table(Tab), TheModule(M) { |
| 264 | |
| 265 | // If the module has a symbol table, take all global types and stuff their |
| 266 | // names into the TypeNames map. |
| 267 | // |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 268 | fillTypeNameTable(M, TypeNames); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 271 | inline void write(const Module *M) { printModule(M); } |
| 272 | inline void write(const GlobalVariable *G) { printGlobal(G); } |
| 273 | inline void write(const Method *M) { printMethod(M); } |
| 274 | inline void write(const BasicBlock *BB) { printBasicBlock(BB); } |
| 275 | inline void write(const Instruction *I) { printInstruction(I); } |
| 276 | inline void write(const ConstPoolVal *CPV) { printConstant(CPV); } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 278 | private : |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 279 | void printModule(const Module *M); |
| 280 | void printSymbolTable(const SymbolTable &ST); |
| 281 | void printConstant(const ConstPoolVal *CPV); |
| 282 | void printGlobal(const GlobalVariable *GV); |
| 283 | void printMethod(const Method *M); |
| 284 | void printMethodArgument(const MethodArgument *MA); |
| 285 | void printBasicBlock(const BasicBlock *BB); |
| 286 | void printInstruction(const Instruction *I); |
| 287 | ostream &printType(const Type *Ty); |
| 288 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 289 | void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 290 | |
| 291 | // printInfoComment - Print a little comment after the instruction indicating |
| 292 | // which slot it occupies. |
| 293 | void printInfoComment(const Value *V); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 297 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, |
| 298 | bool PrintName) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 299 | if (PrintType) { Out << " "; printType(Operand->getType()); } |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 300 | WriteAsOperandInternal(Out, Operand, PrintName, &Table); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 304 | void AssemblyWriter::printModule(const Module *M) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 305 | // Loop over the symbol table, emitting all named constants... |
| 306 | if (M->hasSymbolTable()) |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 307 | printSymbolTable(*M->getSymbolTable()); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 308 | |
| 309 | for_each(M->gbegin(), M->gend(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 310 | bind_obj(this, &AssemblyWriter::printGlobal)); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 311 | |
Vikram S. Adve | 13ba19a | 2001-09-18 12:48:16 +0000 | [diff] [blame] | 312 | Out << "implementation\n"; |
| 313 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 314 | // Output all of the methods... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 315 | for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printMethod)); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 318 | void AssemblyWriter::printGlobal(const GlobalVariable *GV) { |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 319 | if (GV->hasName()) Out << "%" << GV->getName() << " = "; |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 320 | |
| 321 | if (!GV->hasInitializer()) Out << "uninitialized "; |
| 322 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 323 | Out << (GV->isConstant() ? "constant " : "global "); |
| 324 | printType(GV->getType()->getValueType()); |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 325 | |
| 326 | if (GV->hasInitializer()) |
| 327 | writeOperand(GV->getInitializer(), false, false); |
| 328 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 329 | printInfoComment(GV); |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 330 | Out << endl; |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 334 | // printSymbolTable - Run through symbol table looking for named constants |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 335 | // if a named constant is found, emit it's declaration... |
| 336 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 337 | void AssemblyWriter::printSymbolTable(const SymbolTable &ST) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 338 | for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { |
| 339 | SymbolTable::type_const_iterator I = ST.type_begin(TI->first); |
| 340 | SymbolTable::type_const_iterator End = ST.type_end(TI->first); |
| 341 | |
| 342 | for (; I != End; ++I) { |
| 343 | const Value *V = I->second; |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 344 | if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 345 | printConstant(CPV); |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 346 | } else if (const Type *Ty = dyn_cast<const Type>(V)) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 347 | Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl; |
| 348 | } |
| 349 | } |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 354 | // printConstant - Print out a constant pool entry... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 355 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 356 | void AssemblyWriter::printConstant(const ConstPoolVal *CPV) { |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 357 | // Don't print out unnamed constants, they will be inlined |
| 358 | if (!CPV->hasName()) return; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 359 | |
Chris Lattner | ee998be | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 360 | // Print out name... |
| 361 | Out << "\t%" << CPV->getName() << " = "; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 362 | |
Chris Lattner | ee998be | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 363 | // Print out the constant type... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 364 | printType(CPV->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 365 | |
| 366 | // Write the value out now... |
| 367 | writeOperand(CPV, false, false); |
| 368 | |
| 369 | if (!CPV->hasName() && CPV->getType() != Type::VoidTy) { |
| 370 | int Slot = Table.getValSlot(CPV); // Print out the def slot taken... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 371 | Out << "\t\t; <"; |
| 372 | printType(CPV->getType()) << ">:"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 373 | if (Slot >= 0) Out << Slot; |
| 374 | else Out << "<badref>"; |
| 375 | } |
| 376 | |
| 377 | Out << endl; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 380 | // printMethod - Print all aspects of a method. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 381 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 382 | void AssemblyWriter::printMethod(const Method *M) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 383 | // Print out the return type and name... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 384 | Out << "\n" << (M->isExternal() ? "declare " : ""); |
| 385 | printType(M->getReturnType()) << " \"" << M->getName() << "\"("; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 386 | Table.incorporateMethod(M); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 388 | // Loop over the arguments, printing them... |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 389 | const MethodType *MT = cast<const MethodType>(M->getMethodType()); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 390 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 391 | if (!M->isExternal()) { |
| 392 | for_each(M->getArgumentList().begin(), M->getArgumentList().end(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 393 | bind_obj(this, &AssemblyWriter::printMethodArgument)); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 394 | } else { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 395 | // Loop over the arguments, printing them... |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 396 | const MethodType *MT = cast<const MethodType>(M->getMethodType()); |
| 397 | for (MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin(), |
| 398 | E = MT->getParamTypes().end(); I != E; ++I) { |
| 399 | if (I != MT->getParamTypes().begin()) Out << ", "; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 400 | printType(*I); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 403 | |
| 404 | // Finish printing arguments... |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 405 | if (MT->isVarArg()) { |
| 406 | if (MT->getParamTypes().size()) Out << ", "; |
| 407 | Out << "..."; // Output varargs portion of signature! |
| 408 | } |
| 409 | Out << ")\n"; |
| 410 | |
| 411 | if (!M->isExternal()) { |
| 412 | // Loop over the symbol table, emitting all named constants... |
| 413 | if (M->hasSymbolTable()) |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 414 | printSymbolTable(*M->getSymbolTable()); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 415 | |
| 416 | Out << "begin"; |
| 417 | |
| 418 | // Output all of its basic blocks... for the method |
| 419 | for_each(M->begin(), M->end(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 420 | bind_obj(this, &AssemblyWriter::printBasicBlock)); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 421 | |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 422 | Out << "end\n"; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | Table.purgeMethod(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 428 | // printMethodArgument - This member is called for every argument that |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 429 | // is passed into the method. Simply print it out |
| 430 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 431 | void AssemblyWriter::printMethodArgument(const MethodArgument *Arg) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 432 | // Insert commas as we go... the first arg doesn't get a comma |
| 433 | if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; |
| 434 | |
| 435 | // Output type... |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 436 | printType(Arg->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 437 | |
| 438 | // Output name, if available... |
| 439 | if (Arg->hasName()) |
| 440 | Out << " %" << Arg->getName(); |
| 441 | else if (Table.getValSlot(Arg) < 0) |
| 442 | Out << "<badref>"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 445 | // printBasicBlock - This member is called for each basic block in a methd. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 446 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 447 | void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 448 | if (BB->hasName()) { // Print out the label if it exists... |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 449 | Out << "\n" << BB->getName() << ":"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 450 | } else { |
| 451 | int Slot = Table.getValSlot(BB); |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 452 | Out << "\n; <label>:"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 453 | if (Slot >= 0) |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 454 | Out << Slot; // Extra newline seperates out label's |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 455 | else |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 456 | Out << "<badref>"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 458 | Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 459 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 460 | // Output all of the instructions in the basic block... |
| 461 | for_each(BB->begin(), BB->end(), |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 462 | bind_obj(this, &AssemblyWriter::printInstruction)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 465 | |
| 466 | // printInfoComment - Print a little comment after the instruction indicating |
| 467 | // which slot it occupies. |
| 468 | // |
| 469 | void AssemblyWriter::printInfoComment(const Value *V) { |
| 470 | if (V->getType() != Type::VoidTy) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 471 | Out << "\t\t; <"; |
| 472 | printType(V->getType()) << ">"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 473 | |
| 474 | if (!V->hasName()) { |
| 475 | int Slot = Table.getValSlot(V); // Print out the def slot taken... |
| 476 | if (Slot >= 0) Out << ":" << Slot; |
| 477 | else Out << ":<badref>"; |
| 478 | } |
| 479 | Out << "\t[#uses=" << V->use_size() << "]"; // Output # uses |
| 480 | } |
| 481 | } |
| 482 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 483 | // printInstruction - This member is called for each Instruction in a methd. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 484 | // |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 485 | void AssemblyWriter::printInstruction(const Instruction *I) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 486 | Out << "\t"; |
| 487 | |
| 488 | // Print out name if it exists... |
| 489 | if (I && I->hasName()) |
| 490 | Out << "%" << I->getName() << " = "; |
| 491 | |
| 492 | // Print out the opcode... |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 493 | Out << I->getOpcodeName(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 494 | |
| 495 | // Print out the type of the operands... |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 496 | const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 497 | |
| 498 | // Special case conditional branches to swizzle the condition out to the front |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 499 | if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 500 | writeOperand(I->getOperand(2), true); |
| 501 | Out << ","; |
| 502 | writeOperand(Operand, true); |
| 503 | Out << ","; |
| 504 | writeOperand(I->getOperand(1), true); |
| 505 | |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 506 | } else if (I->getOpcode() == Instruction::Switch) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 507 | // Special case switch statement to get formatting nice and correct... |
| 508 | writeOperand(Operand , true); Out << ","; |
| 509 | writeOperand(I->getOperand(1), true); Out << " ["; |
| 510 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 511 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 512 | Out << "\n\t\t"; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 513 | writeOperand(I->getOperand(op ), true); Out << ","; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 514 | writeOperand(I->getOperand(op+1), true); |
| 515 | } |
| 516 | Out << "\n\t]"; |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 517 | } else if (isa<PHINode>(I)) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 518 | Out << " "; |
| 519 | printType(Operand->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 931ef3b | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 521 | Out << " ["; writeOperand(Operand, false); Out << ","; |
Chris Lattner | 4b94e23 | 2001-06-21 05:29:56 +0000 | [diff] [blame] | 522 | writeOperand(I->getOperand(1), false); Out << " ]"; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 523 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) { |
| 524 | Out << ", ["; |
| 525 | writeOperand(I->getOperand(op ), false); Out << ","; |
Chris Lattner | 4b94e23 | 2001-06-21 05:29:56 +0000 | [diff] [blame] | 526 | writeOperand(I->getOperand(op+1), false); Out << " ]"; |
Chris Lattner | 931ef3b | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 527 | } |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 528 | } else if (isa<ReturnInst>(I) && !Operand) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 529 | Out << " void"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 530 | } else if (isa<CallInst>(I)) { |
Chris Lattner | 7fac070 | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 531 | // TODO: Should try to print out short form of the Call instruction |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 532 | writeOperand(Operand, true); |
| 533 | Out << "("; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 534 | if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true); |
| 535 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 536 | Out << ","; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 537 | writeOperand(I->getOperand(op), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | Out << " )"; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 541 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) { |
| 542 | // TODO: Should try to print out short form of the Invoke instruction |
| 543 | writeOperand(Operand, true); |
| 544 | Out << "("; |
| 545 | if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true); |
| 546 | for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) { |
| 547 | Out << ","; |
| 548 | writeOperand(I->getOperand(op), true); |
| 549 | } |
| 550 | |
| 551 | Out << " )\n\t\t\tto"; |
| 552 | writeOperand(II->getNormalDest(), true); |
| 553 | Out << " except"; |
| 554 | writeOperand(II->getExceptionalDest(), true); |
| 555 | |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 556 | } else if (I->getOpcode() == Instruction::Malloc || |
| 557 | I->getOpcode() == Instruction::Alloca) { |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 558 | Out << " "; |
| 559 | printType(cast<const PointerType>(I->getType())->getValueType()); |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 560 | if (I->getNumOperands()) { |
| 561 | Out << ","; |
| 562 | writeOperand(I->getOperand(0), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 563 | } |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 564 | } else if (isa<CastInst>(I)) { |
Chris Lattner | a682182 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 565 | writeOperand(Operand, true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 566 | Out << " to "; |
| 567 | printType(I->getType()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 568 | } else if (Operand) { // Print the normal way... |
| 569 | |
| 570 | // PrintAllTypes - Instructions who have operands of all the same type |
| 571 | // omit the type from all but the first operand. If the instruction has |
| 572 | // different type operands (for example br), then they are all printed. |
| 573 | bool PrintAllTypes = false; |
| 574 | const Type *TheType = Operand->getType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 575 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 576 | for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) { |
| 577 | Operand = I->getOperand(i); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 578 | if (Operand->getType() != TheType) { |
| 579 | PrintAllTypes = true; // We have differing types! Print them all! |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | |
Chris Lattner | b6fe234 | 2001-10-20 09:33:10 +0000 | [diff] [blame] | 584 | // Shift Left & Right print both types even for Ubyte LHS |
| 585 | if (isa<ShiftInst>(I)) PrintAllTypes = true; |
| 586 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 587 | if (!PrintAllTypes) { |
| 588 | Out << " "; |
| 589 | printType(I->getOperand(0)->getType()); |
| 590 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 591 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 592 | for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 593 | if (i) Out << ","; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 594 | writeOperand(I->getOperand(i), PrintAllTypes); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 598 | printInfoComment(I); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 599 | Out << endl; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 603 | // printType - Go to extreme measures to attempt to print out a short, symbolic |
| 604 | // version of a type name. |
| 605 | // |
| 606 | ostream &AssemblyWriter::printType(const Type *Ty) { |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 607 | return printTypeInt(Out, Ty, TypeNames); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 611 | //===----------------------------------------------------------------------===// |
| 612 | // External Interface declarations |
| 613 | //===----------------------------------------------------------------------===// |
| 614 | |
| 615 | |
| 616 | |
| 617 | void WriteToAssembly(const Module *M, ostream &o) { |
| 618 | if (M == 0) { o << "<null> module\n"; return; } |
| 619 | SlotCalculator SlotTable(M, true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 620 | AssemblyWriter W(o, SlotTable, M); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 621 | |
| 622 | W.write(M); |
| 623 | } |
| 624 | |
Chris Lattner | adfe0d1 | 2001-09-10 20:08:19 +0000 | [diff] [blame] | 625 | void WriteToAssembly(const GlobalVariable *G, ostream &o) { |
| 626 | if (G == 0) { o << "<null> global variable\n"; return; } |
| 627 | SlotCalculator SlotTable(G->getParent(), true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 628 | AssemblyWriter W(o, SlotTable, G->getParent()); |
Chris Lattner | adfe0d1 | 2001-09-10 20:08:19 +0000 | [diff] [blame] | 629 | W.write(G); |
| 630 | } |
| 631 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 632 | void WriteToAssembly(const Method *M, ostream &o) { |
| 633 | if (M == 0) { o << "<null> method\n"; return; } |
| 634 | SlotCalculator SlotTable(M->getParent(), true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 635 | AssemblyWriter W(o, SlotTable, M->getParent()); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 636 | |
| 637 | W.write(M); |
| 638 | } |
| 639 | |
| 640 | |
| 641 | void WriteToAssembly(const BasicBlock *BB, ostream &o) { |
| 642 | if (BB == 0) { o << "<null> basic block\n"; return; } |
| 643 | |
| 644 | SlotCalculator SlotTable(BB->getParent(), true); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 645 | AssemblyWriter W(o, SlotTable, |
| 646 | BB->getParent() ? BB->getParent()->getParent() : 0); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 647 | |
| 648 | W.write(BB); |
| 649 | } |
| 650 | |
| 651 | void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) { |
| 652 | if (CPV == 0) { o << "<null> constant pool value\n"; return; } |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 653 | o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void WriteToAssembly(const Instruction *I, ostream &o) { |
| 657 | if (I == 0) { o << "<null> instruction\n"; return; } |
| 658 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 659 | const Method *M = I->getParent() ? I->getParent()->getParent() : 0; |
| 660 | SlotCalculator SlotTable(M, true); |
| 661 | AssemblyWriter W(o, SlotTable, M ? M->getParent() : 0); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 662 | |
| 663 | W.write(I); |
| 664 | } |