blob: d84cba0752bb525778e3a907726e93ff6930f8ea [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- 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
22class AssemblyWriter : public ModuleAnalyzer {
23 ostream &Out;
24 SlotCalculator &Table;
25public:
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
35protected:
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
44private :
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//
53bool AssemblyWriter::visitMethod(const Method *M) {
54 return false;
55}
56
57bool 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//
73bool 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//
99bool 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//
112bool 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//
130bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
131 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000132 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133 } else {
134 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000135 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000137 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000139 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000140 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000141 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000142
143 ModuleAnalyzer::processBasicBlock(BB);
144 return false;
145}
146
147// processInstruction - This member is called for each Instruction in a methd.
148//
149bool AssemblyWriter::processInstruction(const Instruction *I) {
150 Out << "\t";
151
152 // Print out name if it exists...
153 if (I && I->hasName())
154 Out << "%" << I->getName() << " = ";
155
156 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000157 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158
159 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000160 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161
162 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000163 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000164 writeOperand(I->getOperand(2), true);
165 Out << ",";
166 writeOperand(Operand, true);
167 Out << ",";
168 writeOperand(I->getOperand(1), true);
169
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000170 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171 // Special case switch statement to get formatting nice and correct...
172 writeOperand(Operand , true); Out << ",";
173 writeOperand(I->getOperand(1), true); Out << " [";
174
Chris Lattnera073acb2001-07-07 08:36:50 +0000175 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000176 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000177 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000178 writeOperand(I->getOperand(op+1), true);
179 }
180 Out << "\n\t]";
Chris Lattner4cee8d82001-06-27 23:41:11 +0000181 } else if (I->isPHINode()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +0000182 Out << " " << Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000183
Chris Lattner931ef3b2001-06-11 15:04:20 +0000184 Out << " ["; writeOperand(Operand, false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000185 writeOperand(I->getOperand(1), false); Out << " ]";
Chris Lattnera073acb2001-07-07 08:36:50 +0000186 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
187 Out << ", [";
188 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000189 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000190 }
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000191 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000192 Out << " void";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000193 } else if (I->getOpcode() == Instruction::Call) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000194 writeOperand(Operand, true);
195 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000196 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
197 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000199 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000200 }
201
202 Out << " )";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000203 } else if (I->getOpcode() == Instruction::Malloc ||
204 I->getOpcode() == Instruction::Alloca) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000205 Out << " " << ((const PointerType*)I->getType())->getValueType();
206 if (I->getNumOperands()) {
207 Out << ",";
208 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209 }
Chris Lattnera6821822001-07-08 04:57:15 +0000210 } else if (I->getOpcode() == Instruction::Cast) {
211 writeOperand(Operand, true);
212 Out << " to " << I->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213 } else if (Operand) { // Print the normal way...
214
215 // PrintAllTypes - Instructions who have operands of all the same type
216 // omit the type from all but the first operand. If the instruction has
217 // different type operands (for example br), then they are all printed.
218 bool PrintAllTypes = false;
219 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220
Chris Lattnera073acb2001-07-07 08:36:50 +0000221 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
222 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000223 if (Operand->getType() != TheType) {
224 PrintAllTypes = true; // We have differing types! Print them all!
225 break;
226 }
227 }
228
229 if (!PrintAllTypes)
230 Out << " " << I->getOperand(0)->getType();
231
Chris Lattnera073acb2001-07-07 08:36:50 +0000232 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000234 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235 }
236 }
237
238 // Print a little comment after the instruction indicating which slot it
239 // occupies.
240 //
Chris Lattnera2f01872001-06-07 16:58:55 +0000241 if (I->getType() != Type::VoidTy) {
242 Out << "\t\t; <" << I->getType() << ">";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243
Chris Lattnera2f01872001-06-07 16:58:55 +0000244 if (!I->hasName()) {
245 int Slot = Table.getValSlot(I); // Print out the def slot taken...
246 if (Slot >= 0) Out << ":" << Slot;
247 else Out << ":<badref>";
248 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
250 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000251 Out << endl;
252
253 return false;
254}
255
256
257void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
258 bool PrintName) {
259 if (PrintType)
260 Out << " " << Operand->getType();
261
262 if (Operand->hasName() && PrintName) {
263 Out << " %" << Operand->getName();
264 } else {
265 int Slot = Table.getValSlot(Operand);
266
Chris Lattnera073acb2001-07-07 08:36:50 +0000267 if (const ConstPoolVal *CPV = Operand->castConstant()) {
268 Out << " " << CPV->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269 } else {
270 if (Slot >= 0) Out << " %" << Slot;
271 else if (PrintName)
272 Out << "<badref>"; // Not embeded into a location?
273 }
274 }
275}
276
277
278//===----------------------------------------------------------------------===//
279// External Interface declarations
280//===----------------------------------------------------------------------===//
281
282
283
284void WriteToAssembly(const Module *M, ostream &o) {
285 if (M == 0) { o << "<null> module\n"; return; }
286 SlotCalculator SlotTable(M, true);
287 AssemblyWriter W(o, SlotTable);
288
289 W.write(M);
290}
291
292void WriteToAssembly(const Method *M, ostream &o) {
293 if (M == 0) { o << "<null> method\n"; return; }
294 SlotCalculator SlotTable(M->getParent(), true);
295 AssemblyWriter W(o, SlotTable);
296
297 W.write(M);
298}
299
300
301void WriteToAssembly(const BasicBlock *BB, ostream &o) {
302 if (BB == 0) { o << "<null> basic block\n"; return; }
303
304 SlotCalculator SlotTable(BB->getParent(), true);
305 AssemblyWriter W(o, SlotTable);
306
307 W.write(BB);
308}
309
310void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
311 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
312
313 SlotCalculator *SlotTable;
314
315 // A Constant pool value may have a parent that is either a method or a
316 // module. Untangle this now...
317 //
Chris Lattner4cee8d82001-06-27 23:41:11 +0000318 if (CPV->getParent() == 0 || CPV->getParent()->isMethod()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000319 SlotTable = new SlotCalculator((Method*)CPV->getParent(), true);
320 } else {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000321 SlotTable =
322 new SlotCalculator(CPV->getParent()->castModuleAsserting(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000323 }
324
325 AssemblyWriter W(o, *SlotTable);
326 W.write(CPV);
327
328 delete SlotTable;
329}
330
331void WriteToAssembly(const Instruction *I, ostream &o) {
332 if (I == 0) { o << "<null> instruction\n"; return; }
333
334 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
335 true);
336 AssemblyWriter W(o, SlotTable);
337
338 W.write(I);
339}