blob: c68cd796f1a89146b2371df574fdc804c15631cd [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...
157 Out << I->getOpcode();
158
159 // Print out the type of the operands...
160 const Value *Operand = I->getOperand(0);
161
162 // Special case conditional branches to swizzle the condition out to the front
163 if (I->getInstType() == Instruction::Br && I->getOperand(1)) {
164 writeOperand(I->getOperand(2), true);
165 Out << ",";
166 writeOperand(Operand, true);
167 Out << ",";
168 writeOperand(I->getOperand(1), true);
169
170 } else if (I->getInstType() == Instruction::Switch) {
171 // Special case switch statement to get formatting nice and correct...
172 writeOperand(Operand , true); Out << ",";
173 writeOperand(I->getOperand(1), true); Out << " [";
174
175 for (unsigned op = 2; (Operand = I->getOperand(op)); op += 2) {
176 Out << "\n\t\t";
177 writeOperand(Operand, true); Out << ",";
178 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 Lattner931ef3b2001-06-11 15:04:20 +0000186 for (unsigned op = 2; (Operand = I->getOperand(op)); op += 2) {
187 Out << ", ["; writeOperand(Operand, false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000188 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000189 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000190 } else if (I->getInstType() == Instruction::Ret && !Operand) {
191 Out << " void";
192 } else if (I->getInstType() == Instruction::Call) {
193 writeOperand(Operand, true);
194 Out << "(";
195 Operand = I->getOperand(1);
196 if (Operand) writeOperand(Operand, true);
197 for (unsigned op = 2; (Operand = I->getOperand(op)); ++op) {
198 Out << ",";
199 writeOperand(Operand, true);
200 }
201
202 Out << " )";
203 } else if (I->getInstType() == Instruction::Malloc ||
204 I->getInstType() == Instruction::Alloca) {
205 Out << " " << ((const PointerType*)((ConstPoolType*)Operand)
206 ->getValue())->getValueType();
207 if ((Operand = I->getOperand(1))) {
208 Out << ","; writeOperand(Operand, true);
209 }
210
211 } else if (Operand) { // Print the normal way...
212
213 // PrintAllTypes - Instructions who have operands of all the same type
214 // omit the type from all but the first operand. If the instruction has
215 // different type operands (for example br), then they are all printed.
216 bool PrintAllTypes = false;
217 const Type *TheType = Operand->getType();
218 unsigned i;
219
220 for (i = 1; (Operand = I->getOperand(i)); i++) {
221 if (Operand->getType() != TheType) {
222 PrintAllTypes = true; // We have differing types! Print them all!
223 break;
224 }
225 }
226
227 if (!PrintAllTypes)
228 Out << " " << I->getOperand(0)->getType();
229
230 for (unsigned i = 0; (Operand = I->getOperand(i)); i++) {
231 if (i) Out << ",";
232 writeOperand(Operand, PrintAllTypes);
233 }
234 }
235
236 // Print a little comment after the instruction indicating which slot it
237 // occupies.
238 //
Chris Lattnera2f01872001-06-07 16:58:55 +0000239 if (I->getType() != Type::VoidTy) {
240 Out << "\t\t; <" << I->getType() << ">";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000241
Chris Lattnera2f01872001-06-07 16:58:55 +0000242 if (!I->hasName()) {
243 int Slot = Table.getValSlot(I); // Print out the def slot taken...
244 if (Slot >= 0) Out << ":" << Slot;
245 else Out << ":<badref>";
246 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000247 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
248 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 Out << endl;
250
251 return false;
252}
253
254
255void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
256 bool PrintName) {
257 if (PrintType)
258 Out << " " << Operand->getType();
259
260 if (Operand->hasName() && PrintName) {
261 Out << " %" << Operand->getName();
262 } else {
263 int Slot = Table.getValSlot(Operand);
264
Chris Lattner4cee8d82001-06-27 23:41:11 +0000265 if (Operand->isConstant()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000266 Out << " " << ((ConstPoolVal*)Operand)->getStrValue();
267 } else {
268 if (Slot >= 0) Out << " %" << Slot;
269 else if (PrintName)
270 Out << "<badref>"; // Not embeded into a location?
271 }
272 }
273}
274
275
276//===----------------------------------------------------------------------===//
277// External Interface declarations
278//===----------------------------------------------------------------------===//
279
280
281
282void WriteToAssembly(const Module *M, ostream &o) {
283 if (M == 0) { o << "<null> module\n"; return; }
284 SlotCalculator SlotTable(M, true);
285 AssemblyWriter W(o, SlotTable);
286
287 W.write(M);
288}
289
290void WriteToAssembly(const Method *M, ostream &o) {
291 if (M == 0) { o << "<null> method\n"; return; }
292 SlotCalculator SlotTable(M->getParent(), true);
293 AssemblyWriter W(o, SlotTable);
294
295 W.write(M);
296}
297
298
299void WriteToAssembly(const BasicBlock *BB, ostream &o) {
300 if (BB == 0) { o << "<null> basic block\n"; return; }
301
302 SlotCalculator SlotTable(BB->getParent(), true);
303 AssemblyWriter W(o, SlotTable);
304
305 W.write(BB);
306}
307
308void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
309 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
310
311 SlotCalculator *SlotTable;
312
313 // A Constant pool value may have a parent that is either a method or a
314 // module. Untangle this now...
315 //
Chris Lattner4cee8d82001-06-27 23:41:11 +0000316 if (CPV->getParent() == 0 || CPV->getParent()->isMethod()) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000317 SlotTable = new SlotCalculator((Method*)CPV->getParent(), true);
318 } else {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000319 SlotTable =
320 new SlotCalculator(CPV->getParent()->castModuleAsserting(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000321 }
322
323 AssemblyWriter W(o, *SlotTable);
324 W.write(CPV);
325
326 delete SlotTable;
327}
328
329void WriteToAssembly(const Instruction *I, ostream &o) {
330 if (I == 0) { o << "<null> instruction\n"; return; }
331
332 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
333 true);
334 AssemblyWriter W(o, SlotTable);
335
336 W.write(I);
337}