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