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