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