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" |
| 21 | |
| 22 | class AssemblyWriter : public ModuleAnalyzer { |
| 23 | ostream &Out; |
| 24 | SlotCalculator &Table; |
| 25 | public: |
| 26 | inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) { |
| 27 | } |
| 28 | |
| 29 | inline void write(const Module *M) { processModule(M); } |
| 30 | inline void write(const Method *M) { processMethod(M); } |
| 31 | inline void write(const BasicBlock *BB) { processBasicBlock(BB); } |
| 32 | inline void write(const Instruction *I) { processInstruction(I); } |
| 33 | inline void write(const ConstPoolVal *CPV) { processConstant(CPV); } |
| 34 | |
| 35 | protected: |
| 36 | virtual bool visitMethod(const Method *M); |
| 37 | virtual bool processConstPool(const ConstantPool &CP, bool isMethod); |
| 38 | virtual bool processConstant(const ConstPoolVal *CPV); |
| 39 | virtual bool processMethod(const Method *M); |
| 40 | virtual bool processMethodArgument(const MethodArgument *MA); |
| 41 | virtual bool processBasicBlock(const BasicBlock *BB); |
| 42 | virtual bool processInstruction(const Instruction *I); |
| 43 | |
| 44 | private : |
| 45 | void writeOperand(const Value *Op, bool PrintType, bool PrintName = true); |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | |
| 50 | // visitMethod - This member is called after the above two steps, visting each |
| 51 | // method, because they are effectively values that go into the constant pool. |
| 52 | // |
| 53 | bool AssemblyWriter::visitMethod(const Method *M) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) { |
| 58 | // Done printing arguments... |
| 59 | if (isMethod) Out << ")\n"; |
| 60 | |
| 61 | ModuleAnalyzer::processConstPool(CP, isMethod); |
| 62 | |
| 63 | if (isMethod) |
| 64 | Out << "begin"; |
| 65 | else |
| 66 | Out << "implementation\n"; |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | // processConstant - Print out a constant pool entry... |
| 72 | // |
| 73 | bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) { |
| 74 | Out << "\t"; |
| 75 | |
| 76 | // Print out name if it exists... |
| 77 | if (CPV->hasName()) |
| 78 | Out << "%" << CPV->getName() << " = "; |
| 79 | |
| 80 | // Print out the opcode... |
| 81 | Out << CPV->getType(); |
| 82 | |
| 83 | // Write the value out now... |
| 84 | writeOperand(CPV, false, false); |
| 85 | |
| 86 | if (!CPV->hasName() && CPV->getType() != Type::VoidTy) { |
| 87 | int Slot = Table.getValSlot(CPV); // Print out the def slot taken... |
| 88 | Out << "\t\t; <" << CPV->getType() << ">:"; |
| 89 | if (Slot >= 0) Out << Slot; |
| 90 | else Out << "<badref>"; |
| 91 | } |
| 92 | |
| 93 | Out << endl; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | // processMethod - Process all aspects of a method. |
| 98 | // |
| 99 | bool AssemblyWriter::processMethod(const Method *M) { |
| 100 | // Print out the return type and name... |
| 101 | Out << "\n" << M->getReturnType() << " \"" << M->getName() << "\"("; |
| 102 | Table.incorporateMethod(M); |
| 103 | ModuleAnalyzer::processMethod(M); |
| 104 | Table.purgeMethod(); |
| 105 | Out << "end\n"; |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | // processMethodArgument - This member is called for every argument that |
| 110 | // is passed into the method. Simply print it out |
| 111 | // |
| 112 | bool AssemblyWriter::processMethodArgument(const MethodArgument *Arg) { |
| 113 | // Insert commas as we go... the first arg doesn't get a comma |
| 114 | if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", "; |
| 115 | |
| 116 | // Output type... |
| 117 | Out << Arg->getType(); |
| 118 | |
| 119 | // Output name, if available... |
| 120 | if (Arg->hasName()) |
| 121 | Out << " %" << Arg->getName(); |
| 122 | else if (Table.getValSlot(Arg) < 0) |
| 123 | Out << "<badref>"; |
| 124 | |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | // processBasicBlock - This member is called for each basic block in a methd. |
| 129 | // |
| 130 | bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) { |
| 131 | if (BB->hasName()) { // Print out the label if it exists... |
| 132 | Out << "\n" << BB->getName() << ":\n"; |
| 133 | } else { |
| 134 | int Slot = Table.getValSlot(BB); |
| 135 | Out << "\t\t\t\t; <label>:"; |
| 136 | if (Slot >= 0) |
| 137 | Out << Slot << endl; // Extra newline seperates out label's |
| 138 | else |
| 139 | Out << "<badref>\n"; |
| 140 | } |
| 141 | |
| 142 | ModuleAnalyzer::processBasicBlock(BB); |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | // processInstruction - This member is called for each Instruction in a methd. |
| 147 | // |
| 148 | bool AssemblyWriter::processInstruction(const Instruction *I) { |
| 149 | Out << "\t"; |
| 150 | |
| 151 | // Print out name if it exists... |
| 152 | if (I && I->hasName()) |
| 153 | Out << "%" << I->getName() << " = "; |
| 154 | |
| 155 | // Print out the opcode... |
| 156 | Out << I->getOpcode(); |
| 157 | |
| 158 | // Print out the type of the operands... |
| 159 | const Value *Operand = I->getOperand(0); |
| 160 | |
| 161 | // Special case conditional branches to swizzle the condition out to the front |
| 162 | if (I->getInstType() == Instruction::Br && I->getOperand(1)) { |
| 163 | writeOperand(I->getOperand(2), true); |
| 164 | Out << ","; |
| 165 | writeOperand(Operand, true); |
| 166 | Out << ","; |
| 167 | writeOperand(I->getOperand(1), true); |
| 168 | |
| 169 | } else if (I->getInstType() == Instruction::Switch) { |
| 170 | // Special case switch statement to get formatting nice and correct... |
| 171 | writeOperand(Operand , true); Out << ","; |
| 172 | writeOperand(I->getOperand(1), true); Out << " ["; |
| 173 | |
| 174 | for (unsigned op = 2; (Operand = I->getOperand(op)); op += 2) { |
| 175 | Out << "\n\t\t"; |
| 176 | writeOperand(Operand, true); Out << ","; |
| 177 | writeOperand(I->getOperand(op+1), true); |
| 178 | } |
| 179 | Out << "\n\t]"; |
| 180 | |
| 181 | } else if (I->getInstType() == Instruction::Ret && !Operand) { |
| 182 | Out << " void"; |
| 183 | } else if (I->getInstType() == Instruction::Call) { |
| 184 | writeOperand(Operand, true); |
| 185 | Out << "("; |
| 186 | Operand = I->getOperand(1); |
| 187 | if (Operand) writeOperand(Operand, true); |
| 188 | for (unsigned op = 2; (Operand = I->getOperand(op)); ++op) { |
| 189 | Out << ","; |
| 190 | writeOperand(Operand, true); |
| 191 | } |
| 192 | |
| 193 | Out << " )"; |
| 194 | } else if (I->getInstType() == Instruction::Malloc || |
| 195 | I->getInstType() == Instruction::Alloca) { |
| 196 | Out << " " << ((const PointerType*)((ConstPoolType*)Operand) |
| 197 | ->getValue())->getValueType(); |
| 198 | if ((Operand = I->getOperand(1))) { |
| 199 | Out << ","; writeOperand(Operand, true); |
| 200 | } |
| 201 | |
| 202 | } else if (Operand) { // Print the normal way... |
| 203 | |
| 204 | // PrintAllTypes - Instructions who have operands of all the same type |
| 205 | // omit the type from all but the first operand. If the instruction has |
| 206 | // different type operands (for example br), then they are all printed. |
| 207 | bool PrintAllTypes = false; |
| 208 | const Type *TheType = Operand->getType(); |
| 209 | unsigned i; |
| 210 | |
| 211 | for (i = 1; (Operand = I->getOperand(i)); i++) { |
| 212 | if (Operand->getType() != TheType) { |
| 213 | PrintAllTypes = true; // We have differing types! Print them all! |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (!PrintAllTypes) |
| 219 | Out << " " << I->getOperand(0)->getType(); |
| 220 | |
| 221 | for (unsigned i = 0; (Operand = I->getOperand(i)); i++) { |
| 222 | if (i) Out << ","; |
| 223 | writeOperand(Operand, PrintAllTypes); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Print a little comment after the instruction indicating which slot it |
| 228 | // occupies. |
| 229 | // |
| 230 | if (!I->hasName() && I->getType() != Type::VoidTy) { |
| 231 | int Slot = Table.getValSlot(I); // Print out the def slot taken... |
| 232 | Out << "\t\t; <" << I->getType() << ">:"; |
| 233 | if (Slot >= 0) Out << Slot; |
| 234 | else Out << "<badref>"; |
| 235 | |
| 236 | Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses |
| 237 | } |
| 238 | |
| 239 | Out << endl; |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, |
| 246 | bool PrintName) { |
| 247 | if (PrintType) |
| 248 | Out << " " << Operand->getType(); |
| 249 | |
| 250 | if (Operand->hasName() && PrintName) { |
| 251 | Out << " %" << Operand->getName(); |
| 252 | } else { |
| 253 | int Slot = Table.getValSlot(Operand); |
| 254 | |
| 255 | if (Operand->getValueType() == Value::ConstantVal) { |
| 256 | Out << " " << ((ConstPoolVal*)Operand)->getStrValue(); |
| 257 | } else { |
| 258 | if (Slot >= 0) Out << " %" << Slot; |
| 259 | else if (PrintName) |
| 260 | Out << "<badref>"; // Not embeded into a location? |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | //===----------------------------------------------------------------------===// |
| 267 | // External Interface declarations |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | |
| 270 | |
| 271 | |
| 272 | void WriteToAssembly(const Module *M, ostream &o) { |
| 273 | if (M == 0) { o << "<null> module\n"; return; } |
| 274 | SlotCalculator SlotTable(M, true); |
| 275 | AssemblyWriter W(o, SlotTable); |
| 276 | |
| 277 | W.write(M); |
| 278 | } |
| 279 | |
| 280 | void WriteToAssembly(const Method *M, ostream &o) { |
| 281 | if (M == 0) { o << "<null> method\n"; return; } |
| 282 | SlotCalculator SlotTable(M->getParent(), true); |
| 283 | AssemblyWriter W(o, SlotTable); |
| 284 | |
| 285 | W.write(M); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | void WriteToAssembly(const BasicBlock *BB, ostream &o) { |
| 290 | if (BB == 0) { o << "<null> basic block\n"; return; } |
| 291 | |
| 292 | SlotCalculator SlotTable(BB->getParent(), true); |
| 293 | AssemblyWriter W(o, SlotTable); |
| 294 | |
| 295 | W.write(BB); |
| 296 | } |
| 297 | |
| 298 | void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) { |
| 299 | if (CPV == 0) { o << "<null> constant pool value\n"; return; } |
| 300 | |
| 301 | SlotCalculator *SlotTable; |
| 302 | |
| 303 | // A Constant pool value may have a parent that is either a method or a |
| 304 | // module. Untangle this now... |
| 305 | // |
| 306 | if (CPV->getParent() == 0 || |
| 307 | CPV->getParent()->getValueType() == Value::MethodVal) { |
| 308 | SlotTable = new SlotCalculator((Method*)CPV->getParent(), true); |
| 309 | } else { |
| 310 | assert(CPV->getParent()->getValueType() == Value::ModuleVal); |
| 311 | SlotTable = new SlotCalculator((Module*)CPV->getParent(), true); |
| 312 | } |
| 313 | |
| 314 | AssemblyWriter W(o, *SlotTable); |
| 315 | W.write(CPV); |
| 316 | |
| 317 | delete SlotTable; |
| 318 | } |
| 319 | |
| 320 | void WriteToAssembly(const Instruction *I, ostream &o) { |
| 321 | if (I == 0) { o << "<null> instruction\n"; return; } |
| 322 | |
| 323 | SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0, |
| 324 | true); |
| 325 | AssemblyWriter W(o, SlotTable); |
| 326 | |
| 327 | W.write(I); |
| 328 | } |