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" |
| 17 | #include "llvm/BasicBlock.h" |
| 18 | #include "llvm/ConstPoolVals.h" |
| 19 | #include "llvm/iOther.h" |
| 20 | #include "llvm/iMemory.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 21 | #include "llvm/Support/STLExtras.h" |
| 22 | #include "llvm/SymbolTable.h" |
| 23 | #include <algorithm> |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 2e9fee4 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 25 | void DebugValue(const Value *V) { |
| 26 | cerr << V << endl; |
| 27 | } |
| 28 | |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 29 | // WriteAsOperand - Write the name of the specified value out to the specified |
| 30 | // ostream. This can be useful when you just want to print int %reg126, not the |
| 31 | // whole instruction that generated it. |
| 32 | // |
| 33 | ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType, |
| 34 | bool PrintName, SlotCalculator *Table) { |
| 35 | if (PrintType) |
| 36 | Out << " " << V->getType(); |
| 37 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 38 | if (PrintName && V->hasName()) { |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 39 | Out << " %" << V->getName(); |
| 40 | } else { |
| 41 | if (const ConstPoolVal *CPV = V->castConstant()) { |
| 42 | Out << " " << CPV->getStrValue(); |
| 43 | } else { |
| 44 | int Slot; |
| 45 | if (Table) { |
| 46 | Slot = Table->getValSlot(V); |
| 47 | } else { |
| 48 | if (const Type *Ty = V->castType()) { |
| 49 | return Out << " " << Ty; |
| 50 | } else if (const MethodArgument *MA = V->castMethodArgument()) { |
| 51 | Table = new SlotCalculator(MA->getParent(), true); |
| 52 | } else if (const Instruction *I = V->castInstruction()) { |
| 53 | Table = new SlotCalculator(I->getParent()->getParent(), true); |
| 54 | } else if (const BasicBlock *BB = V->castBasicBlock()) { |
| 55 | Table = new SlotCalculator(BB->getParent(), true); |
| 56 | } else if (const Method *Meth = V->castMethod()) { |
| 57 | Table = new SlotCalculator(Meth, true); |
| 58 | } else if (const Module *Mod = V->castModule()) { |
| 59 | Table = new SlotCalculator(Mod, true); |
| 60 | } else { |
| 61 | return Out << "BAD VALUE TYPE!"; |
| 62 | } |
| 63 | Slot = Table->getValSlot(V); |
| 64 | delete Table; |
| 65 | } |
| 66 | if (Slot >= 0) Out << " %" << Slot; |
| 67 | else if (PrintName) |
| 68 | Out << "<badref>"; // Not embeded into a location? |
| 69 | } |
| 70 | } |
| 71 | return Out; |
| 72 | } |
| 73 | |
| 74 | |
Chris Lattner | 2e9fee4 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 75 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 76 | class AssemblyWriter { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 77 | ostream &Out; |
| 78 | SlotCalculator &Table; |
| 79 | public: |
| 80 | inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) { |
| 81 | } |
| 82 | |
| 83 | inline void write(const Module *M) { processModule(M); } |
| 84 | inline void write(const Method *M) { processMethod(M); } |
| 85 | inline void write(const BasicBlock *BB) { processBasicBlock(BB); } |
| 86 | inline void write(const Instruction *I) { processInstruction(I); } |
| 87 | inline void write(const ConstPoolVal *CPV) { processConstant(CPV); } |
| 88 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 89 | private : |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 90 | void processModule(const Module *M); |
| 91 | void processSymbolTable(const SymbolTable &ST); |
| 92 | void processConstant(const ConstPoolVal *CPV); |
| 93 | void processMethod(const Method *M); |
| 94 | void processMethodArgument(const MethodArgument *MA); |
| 95 | void processBasicBlock(const BasicBlock *BB); |
| 96 | void processInstruction(const Instruction *I); |
| 97 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 98 | void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); |
| 99 | }; |
| 100 | |
| 101 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 102 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, |
| 103 | bool PrintName) { |
| 104 | WriteAsOperand(Out, Operand, PrintType, PrintName, &Table); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 107 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 108 | void AssemblyWriter::processModule(const Module *M) { |
| 109 | // Loop over the symbol table, emitting all named constants... |
| 110 | if (M->hasSymbolTable()) |
| 111 | processSymbolTable(*M->getSymbolTable()); |
| 112 | |
| 113 | Out << "implementation\n"; |
| 114 | |
| 115 | // Output all of the methods... |
| 116 | for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::processMethod)); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | // processSymbolTable - Run through symbol table looking for named constants |
| 121 | // if a named constant is found, emit it's declaration... |
| 122 | // |
| 123 | void AssemblyWriter::processSymbolTable(const SymbolTable &ST) { |
| 124 | for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) { |
| 125 | SymbolTable::type_const_iterator I = ST.type_begin(TI->first); |
| 126 | SymbolTable::type_const_iterator End = ST.type_end(TI->first); |
| 127 | |
| 128 | for (; I != End; ++I) { |
| 129 | const Value *V = I->second; |
| 130 | if (const ConstPoolVal *CPV = V->castConstant()) { |
| 131 | processConstant(CPV); |
| 132 | } else if (const Type *Ty = V->castType()) { |
| 133 | Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl; |
| 134 | } |
| 135 | } |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 136 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | // processConstant - Print out a constant pool entry... |
| 141 | // |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 142 | void AssemblyWriter::processConstant(const ConstPoolVal *CPV) { |
| 143 | // Don't print out unnamed constants, they will be inlined |
| 144 | if (!CPV->hasName()) return; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 145 | |
Chris Lattner | ee998be | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 146 | // Print out name... |
| 147 | Out << "\t%" << CPV->getName() << " = "; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 148 | |
Chris Lattner | ee998be | 2001-07-26 16:29:38 +0000 | [diff] [blame] | 149 | // Print out the constant type... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 150 | Out << CPV->getType(); |
| 151 | |
| 152 | // Write the value out now... |
| 153 | writeOperand(CPV, false, false); |
| 154 | |
| 155 | if (!CPV->hasName() && CPV->getType() != Type::VoidTy) { |
| 156 | int Slot = Table.getValSlot(CPV); // Print out the def slot taken... |
| 157 | Out << "\t\t; <" << CPV->getType() << ">:"; |
| 158 | if (Slot >= 0) Out << Slot; |
| 159 | else Out << "<badref>"; |
| 160 | } |
| 161 | |
| 162 | Out << endl; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // processMethod - Process all aspects of a method. |
| 166 | // |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 167 | void AssemblyWriter::processMethod(const Method *M) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 168 | // Print out the return type and name... |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 169 | Out << "\n" << (M->isExternal() ? "declare " : "") |
| 170 | << M->getReturnType() << " \"" << M->getName() << "\"("; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 171 | Table.incorporateMethod(M); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 172 | |
| 173 | // Loop over the arguments, processing them... |
| 174 | for_each(M->getArgumentList().begin(), M->getArgumentList().end(), |
| 175 | bind_obj(this, &AssemblyWriter::processMethodArgument)); |
| 176 | |
| 177 | |
| 178 | // Finish printing arguments... |
| 179 | const MethodType *MT = (const MethodType*)M->getType(); |
| 180 | if (MT->isVarArg()) { |
| 181 | if (MT->getParamTypes().size()) Out << ", "; |
| 182 | Out << "..."; // Output varargs portion of signature! |
| 183 | } |
| 184 | Out << ")\n"; |
| 185 | |
| 186 | if (!M->isExternal()) { |
| 187 | // Loop over the symbol table, emitting all named constants... |
| 188 | if (M->hasSymbolTable()) |
| 189 | processSymbolTable(*M->getSymbolTable()); |
| 190 | |
| 191 | Out << "begin"; |
| 192 | |
| 193 | // Output all of its basic blocks... for the method |
| 194 | for_each(M->begin(), M->end(), |
| 195 | bind_obj(this, &AssemblyWriter::processBasicBlock)); |
| 196 | |
Chris Lattner | a7620d9 | 2001-07-15 06:35:59 +0000 | [diff] [blame] | 197 | Out << "end\n"; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 198 | } |
| 199 | |
| 200 | Table.purgeMethod(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // processMethodArgument - This member is called for every argument that |
| 204 | // is passed into the method. Simply print it out |
| 205 | // |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 206 | void AssemblyWriter::processMethodArgument(const MethodArgument *Arg) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 207 | // Insert commas as we go... the first arg doesn't get a comma |
| 208 | if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; |
| 209 | |
| 210 | // Output type... |
| 211 | Out << Arg->getType(); |
| 212 | |
| 213 | // Output name, if available... |
| 214 | if (Arg->hasName()) |
| 215 | Out << " %" << Arg->getName(); |
| 216 | else if (Table.getValSlot(Arg) < 0) |
| 217 | Out << "<badref>"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // processBasicBlock - This member is called for each basic block in a methd. |
| 221 | // |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 222 | void AssemblyWriter::processBasicBlock(const BasicBlock *BB) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 223 | if (BB->hasName()) { // Print out the label if it exists... |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 224 | Out << "\n" << BB->getName() << ":"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 225 | } else { |
| 226 | int Slot = Table.getValSlot(BB); |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 227 | Out << "\n; <label>:"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 228 | if (Slot >= 0) |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 229 | Out << Slot; // Extra newline seperates out label's |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 230 | else |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 231 | Out << "<badref>"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 232 | } |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 233 | 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] | 234 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 235 | // Output all of the instructions in the basic block... |
| 236 | for_each(BB->begin(), BB->end(), |
| 237 | bind_obj(this, &AssemblyWriter::processInstruction)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // processInstruction - This member is called for each Instruction in a methd. |
| 241 | // |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame^] | 242 | void AssemblyWriter::processInstruction(const Instruction *I) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 243 | Out << "\t"; |
| 244 | |
| 245 | // Print out name if it exists... |
| 246 | if (I && I->hasName()) |
| 247 | Out << "%" << I->getName() << " = "; |
| 248 | |
| 249 | // Print out the opcode... |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 250 | Out << I->getOpcodeName(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 251 | |
| 252 | // Print out the type of the operands... |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 253 | const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 254 | |
| 255 | // Special case conditional branches to swizzle the condition out to the front |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 256 | if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 257 | writeOperand(I->getOperand(2), true); |
| 258 | Out << ","; |
| 259 | writeOperand(Operand, true); |
| 260 | Out << ","; |
| 261 | writeOperand(I->getOperand(1), true); |
| 262 | |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 263 | } else if (I->getOpcode() == Instruction::Switch) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 264 | // Special case switch statement to get formatting nice and correct... |
| 265 | writeOperand(Operand , true); Out << ","; |
| 266 | writeOperand(I->getOperand(1), true); Out << " ["; |
| 267 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 268 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 269 | Out << "\n\t\t"; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 270 | writeOperand(I->getOperand(op ), true); Out << ","; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 271 | writeOperand(I->getOperand(op+1), true); |
| 272 | } |
| 273 | Out << "\n\t]"; |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 274 | } else if (I->isPHINode()) { |
Chris Lattner | 931ef3b | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 275 | Out << " " << Operand->getType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 931ef3b | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 277 | Out << " ["; writeOperand(Operand, false); Out << ","; |
Chris Lattner | 4b94e23 | 2001-06-21 05:29:56 +0000 | [diff] [blame] | 278 | writeOperand(I->getOperand(1), false); Out << " ]"; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 279 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) { |
| 280 | Out << ", ["; |
| 281 | writeOperand(I->getOperand(op ), false); Out << ","; |
Chris Lattner | 4b94e23 | 2001-06-21 05:29:56 +0000 | [diff] [blame] | 282 | writeOperand(I->getOperand(op+1), false); Out << " ]"; |
Chris Lattner | 931ef3b | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 283 | } |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 284 | } else if (I->getOpcode() == Instruction::Ret && !Operand) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 285 | Out << " void"; |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 286 | } else if (I->getOpcode() == Instruction::Call) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 287 | writeOperand(Operand, true); |
| 288 | Out << "("; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 289 | if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true); |
| 290 | for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 291 | Out << ","; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 292 | writeOperand(I->getOperand(op), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | Out << " )"; |
Chris Lattner | b1ca9cb | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 296 | } else if (I->getOpcode() == Instruction::Malloc || |
| 297 | I->getOpcode() == Instruction::Alloca) { |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 298 | Out << " " << ((const PointerType*)I->getType())->getValueType(); |
| 299 | if (I->getNumOperands()) { |
| 300 | Out << ","; |
| 301 | writeOperand(I->getOperand(0), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 302 | } |
Chris Lattner | a682182 | 2001-07-08 04:57:15 +0000 | [diff] [blame] | 303 | } else if (I->getOpcode() == Instruction::Cast) { |
| 304 | writeOperand(Operand, true); |
| 305 | Out << " to " << I->getType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 306 | } else if (Operand) { // Print the normal way... |
| 307 | |
| 308 | // PrintAllTypes - Instructions who have operands of all the same type |
| 309 | // omit the type from all but the first operand. If the instruction has |
| 310 | // different type operands (for example br), then they are all printed. |
| 311 | bool PrintAllTypes = false; |
| 312 | const Type *TheType = Operand->getType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 313 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 314 | for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) { |
| 315 | Operand = I->getOperand(i); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 316 | if (Operand->getType() != TheType) { |
| 317 | PrintAllTypes = true; // We have differing types! Print them all! |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (!PrintAllTypes) |
| 323 | Out << " " << I->getOperand(0)->getType(); |
| 324 | |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 325 | for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 326 | if (i) Out << ","; |
Chris Lattner | a073acb | 2001-07-07 08:36:50 +0000 | [diff] [blame] | 327 | writeOperand(I->getOperand(i), PrintAllTypes); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | // Print a little comment after the instruction indicating which slot it |
| 332 | // occupies. |
| 333 | // |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 334 | if (I->getType() != Type::VoidTy) { |
| 335 | Out << "\t\t; <" << I->getType() << ">"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 336 | |
Chris Lattner | a2f0187 | 2001-06-07 16:58:55 +0000 | [diff] [blame] | 337 | if (!I->hasName()) { |
| 338 | int Slot = Table.getValSlot(I); // Print out the def slot taken... |
| 339 | if (Slot >= 0) Out << ":" << Slot; |
| 340 | else Out << ":<badref>"; |
| 341 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 342 | Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses |
| 343 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 344 | Out << endl; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
| 348 | //===----------------------------------------------------------------------===// |
| 349 | // External Interface declarations |
| 350 | //===----------------------------------------------------------------------===// |
| 351 | |
| 352 | |
| 353 | |
| 354 | void WriteToAssembly(const Module *M, ostream &o) { |
| 355 | if (M == 0) { o << "<null> module\n"; return; } |
| 356 | SlotCalculator SlotTable(M, true); |
| 357 | AssemblyWriter W(o, SlotTable); |
| 358 | |
| 359 | W.write(M); |
| 360 | } |
| 361 | |
| 362 | void WriteToAssembly(const Method *M, ostream &o) { |
| 363 | if (M == 0) { o << "<null> method\n"; return; } |
| 364 | SlotCalculator SlotTable(M->getParent(), true); |
| 365 | AssemblyWriter W(o, SlotTable); |
| 366 | |
| 367 | W.write(M); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | void WriteToAssembly(const BasicBlock *BB, ostream &o) { |
| 372 | if (BB == 0) { o << "<null> basic block\n"; return; } |
| 373 | |
| 374 | SlotCalculator SlotTable(BB->getParent(), true); |
| 375 | AssemblyWriter W(o, SlotTable); |
| 376 | |
| 377 | W.write(BB); |
| 378 | } |
| 379 | |
| 380 | void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) { |
| 381 | if (CPV == 0) { o << "<null> constant pool value\n"; return; } |
Chris Lattner | 2091efb | 2001-07-28 17:49:02 +0000 | [diff] [blame] | 382 | WriteAsOperand(o, CPV, true, true, 0); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void WriteToAssembly(const Instruction *I, ostream &o) { |
| 386 | if (I == 0) { o << "<null> instruction\n"; return; } |
| 387 | |
| 388 | SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0, |
| 389 | true); |
| 390 | AssemblyWriter W(o, SlotTable); |
| 391 | |
| 392 | W.write(I); |
| 393 | } |