blob: e8025441266db98aa406b49e6263cce67a7f3d64 [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
Chris Lattner2e9fee42001-07-12 23:35:26 +000022void DebugValue(const Value *V) {
23 cerr << V << endl;
24}
25
26
Chris Lattner2f7c9632001-06-06 20:29:01 +000027class AssemblyWriter : public ModuleAnalyzer {
28 ostream &Out;
29 SlotCalculator &Table;
30public:
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
40protected:
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
49private :
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//
58bool AssemblyWriter::visitMethod(const Method *M) {
59 return false;
60}
61
62bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
63 // Done printing arguments...
64 if (isMethod) Out << ")\n";
65
66 ModuleAnalyzer::processConstPool(CP, isMethod);
67
Chris Lattnera7620d92001-07-15 06:35:59 +000068 if (isMethod) {
69 if (!CP.getParentV()->castMethodAsserting()->isExternal())
70 Out << "begin";
71 } else {
Chris Lattner2f7c9632001-06-06 20:29:01 +000072 Out << "implementation\n";
Chris Lattnera7620d92001-07-15 06:35:59 +000073 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000074 return false;
75}
76
77
78// processConstant - Print out a constant pool entry...
79//
80bool 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//
106bool AssemblyWriter::processMethod(const Method *M) {
107 // Print out the return type and name...
Chris Lattnera7620d92001-07-15 06:35:59 +0000108 Out << "\n" << (M->isExternal() ? "declare " : "")
109 << M->getReturnType() << " \"" << M->getName() << "\"(";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110 Table.incorporateMethod(M);
111 ModuleAnalyzer::processMethod(M);
112 Table.purgeMethod();
Chris Lattnera7620d92001-07-15 06:35:59 +0000113 if (!M->isExternal())
114 Out << "end\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000115 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//
121bool 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//
139bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
140 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000141 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000142 } else {
143 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000144 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000146 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000148 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000150 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151
152 ModuleAnalyzer::processBasicBlock(BB);
153 return false;
154}
155
156// processInstruction - This member is called for each Instruction in a methd.
157//
158bool 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 Lattnerb1ca9cb2001-07-07 19:24:15 +0000166 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000167
168 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000169 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000170
171 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000172 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000173 writeOperand(I->getOperand(2), true);
174 Out << ",";
175 writeOperand(Operand, true);
176 Out << ",";
177 writeOperand(I->getOperand(1), true);
178
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000179 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180 // Special case switch statement to get formatting nice and correct...
181 writeOperand(Operand , true); Out << ",";
182 writeOperand(I->getOperand(1), true); Out << " [";
183
Chris Lattnera073acb2001-07-07 08:36:50 +0000184 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000185 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000186 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187 writeOperand(I->getOperand(op+1), true);
188 }
189 Out << "\n\t]";
Chris Lattner4cee8d82001-06-27 23:41:11 +0000190 } else if (I->isPHINode()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +0000191 Out << " " << Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000192
Chris Lattner931ef3b2001-06-11 15:04:20 +0000193 Out << " ["; writeOperand(Operand, false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000194 writeOperand(I->getOperand(1), false); Out << " ]";
Chris Lattnera073acb2001-07-07 08:36:50 +0000195 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
196 Out << ", [";
197 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000198 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000199 }
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000200 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000201 Out << " void";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000202 } else if (I->getOpcode() == Instruction::Call) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203 writeOperand(Operand, true);
204 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000205 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
206 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000208 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209 }
210
211 Out << " )";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000212 } else if (I->getOpcode() == Instruction::Malloc ||
213 I->getOpcode() == Instruction::Alloca) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000214 Out << " " << ((const PointerType*)I->getType())->getValueType();
215 if (I->getNumOperands()) {
216 Out << ",";
217 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218 }
Chris Lattnera6821822001-07-08 04:57:15 +0000219 } else if (I->getOpcode() == Instruction::Cast) {
220 writeOperand(Operand, true);
221 Out << " to " << I->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000222 } 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 Lattner2f7c9632001-06-06 20:29:01 +0000229
Chris Lattnera073acb2001-07-07 08:36:50 +0000230 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
231 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000232 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 Lattnera073acb2001-07-07 08:36:50 +0000241 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000243 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244 }
245 }
246
247 // Print a little comment after the instruction indicating which slot it
248 // occupies.
249 //
Chris Lattnera2f01872001-06-07 16:58:55 +0000250 if (I->getType() != Type::VoidTy) {
251 Out << "\t\t; <" << I->getType() << ">";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000252
Chris Lattnera2f01872001-06-07 16:58:55 +0000253 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 Lattner2f7c9632001-06-06 20:29:01 +0000258 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
259 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000260 Out << endl;
261
262 return false;
263}
264
265
266void 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 Lattnera073acb2001-07-07 08:36:50 +0000276 if (const ConstPoolVal *CPV = Operand->castConstant()) {
277 Out << " " << CPV->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000278 } 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
293void 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
301void 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
310void 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
319void 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 Lattner32e96bc2001-07-14 06:10:33 +0000327 if (const Method *Meth = CPV->getParentV()->castMethod()) {
328 SlotTable = new SlotCalculator(Meth, true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000329 } else {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000330 SlotTable =
Chris Lattner32e96bc2001-07-14 06:10:33 +0000331 new SlotCalculator(CPV->getParentV()->castModuleAsserting(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000332 }
333
334 AssemblyWriter W(o, *SlotTable);
335 W.write(CPV);
336
337 delete SlotTable;
338}
339
340void 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}