blob: 1719bc532f881f252fb811d010fe7aaf00a6a4ae [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
Chris Lattner5e5abe32001-07-20 19:15:21 +000026// WriteAsOperand - Write the name of the specified value out to the specified
27// ostream. This can be useful when you just want to print int %reg126, not the
28// whole instruction that generated it.
29//
30ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
31 bool PrintName, SlotCalculator *Table) {
32 if (PrintType)
33 Out << " " << V->getType();
34
35 if (V->hasName() && PrintName) {
36 Out << " %" << V->getName();
37 } else {
38 if (const ConstPoolVal *CPV = V->castConstant()) {
39 Out << " " << CPV->getStrValue();
40 } else {
41 int Slot;
42 if (Table) {
43 Slot = Table->getValSlot(V);
44 } else {
45 if (const Type *Ty = V->castType()) {
46 return Out << " " << Ty;
47 } else if (const MethodArgument *MA = V->castMethodArgument()) {
48 Table = new SlotCalculator(MA->getParent(), true);
49 } else if (const Instruction *I = V->castInstruction()) {
50 Table = new SlotCalculator(I->getParent()->getParent(), true);
51 } else if (const BasicBlock *BB = V->castBasicBlock()) {
52 Table = new SlotCalculator(BB->getParent(), true);
53 } else if (const Method *Meth = V->castMethod()) {
54 Table = new SlotCalculator(Meth, true);
55 } else if (const Module *Mod = V->castModule()) {
56 Table = new SlotCalculator(Mod, true);
57 } else {
58 return Out << "BAD VALUE TYPE!";
59 }
60 Slot = Table->getValSlot(V);
61 delete Table;
62 }
63 if (Slot >= 0) Out << " %" << Slot;
64 else if (PrintName)
65 Out << "<badref>"; // Not embeded into a location?
66 }
67 }
68 return Out;
69}
70
71
Chris Lattner2e9fee42001-07-12 23:35:26 +000072
Chris Lattner2f7c9632001-06-06 20:29:01 +000073class AssemblyWriter : public ModuleAnalyzer {
74 ostream &Out;
75 SlotCalculator &Table;
76public:
77 inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) {
78 }
79
80 inline void write(const Module *M) { processModule(M); }
81 inline void write(const Method *M) { processMethod(M); }
82 inline void write(const BasicBlock *BB) { processBasicBlock(BB); }
83 inline void write(const Instruction *I) { processInstruction(I); }
84 inline void write(const ConstPoolVal *CPV) { processConstant(CPV); }
85
86protected:
87 virtual bool visitMethod(const Method *M);
88 virtual bool processConstPool(const ConstantPool &CP, bool isMethod);
89 virtual bool processConstant(const ConstPoolVal *CPV);
90 virtual bool processMethod(const Method *M);
91 virtual bool processMethodArgument(const MethodArgument *MA);
92 virtual bool processBasicBlock(const BasicBlock *BB);
93 virtual bool processInstruction(const Instruction *I);
94
95private :
96 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
97};
98
99
100
101// visitMethod - This member is called after the above two steps, visting each
102// method, because they are effectively values that go into the constant pool.
103//
104bool AssemblyWriter::visitMethod(const Method *M) {
105 return false;
106}
107
108bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
109 // Done printing arguments...
110 if (isMethod) Out << ")\n";
111
112 ModuleAnalyzer::processConstPool(CP, isMethod);
113
Chris Lattnera7620d92001-07-15 06:35:59 +0000114 if (isMethod) {
115 if (!CP.getParentV()->castMethodAsserting()->isExternal())
116 Out << "begin";
117 } else {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000118 Out << "implementation\n";
Chris Lattnera7620d92001-07-15 06:35:59 +0000119 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120 return false;
121}
122
123
124// processConstant - Print out a constant pool entry...
125//
126bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
127 Out << "\t";
128
129 // Print out name if it exists...
130 if (CPV->hasName())
131 Out << "%" << CPV->getName() << " = ";
132
133 // Print out the opcode...
134 Out << CPV->getType();
135
136 // Write the value out now...
137 writeOperand(CPV, false, false);
138
139 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
140 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
141 Out << "\t\t; <" << CPV->getType() << ">:";
142 if (Slot >= 0) Out << Slot;
143 else Out << "<badref>";
144 }
145
146 Out << endl;
147 return false;
148}
149
150// processMethod - Process all aspects of a method.
151//
152bool AssemblyWriter::processMethod(const Method *M) {
153 // Print out the return type and name...
Chris Lattnera7620d92001-07-15 06:35:59 +0000154 Out << "\n" << (M->isExternal() ? "declare " : "")
155 << M->getReturnType() << " \"" << M->getName() << "\"(";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000156 Table.incorporateMethod(M);
157 ModuleAnalyzer::processMethod(M);
158 Table.purgeMethod();
Chris Lattnera7620d92001-07-15 06:35:59 +0000159 if (!M->isExternal())
160 Out << "end\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161 return false;
162}
163
164// processMethodArgument - This member is called for every argument that
165// is passed into the method. Simply print it out
166//
167bool AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
168 // Insert commas as we go... the first arg doesn't get a comma
169 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
170
171 // Output type...
172 Out << Arg->getType();
173
174 // Output name, if available...
175 if (Arg->hasName())
176 Out << " %" << Arg->getName();
177 else if (Table.getValSlot(Arg) < 0)
178 Out << "<badref>";
179
180 return false;
181}
182
183// processBasicBlock - This member is called for each basic block in a methd.
184//
185bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
186 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000187 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000188 } else {
189 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000190 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000192 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000194 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000195 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000196 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000197
198 ModuleAnalyzer::processBasicBlock(BB);
199 return false;
200}
201
202// processInstruction - This member is called for each Instruction in a methd.
203//
204bool AssemblyWriter::processInstruction(const Instruction *I) {
205 Out << "\t";
206
207 // Print out name if it exists...
208 if (I && I->hasName())
209 Out << "%" << I->getName() << " = ";
210
211 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000212 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213
214 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000215 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000216
217 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000218 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219 writeOperand(I->getOperand(2), true);
220 Out << ",";
221 writeOperand(Operand, true);
222 Out << ",";
223 writeOperand(I->getOperand(1), true);
224
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000225 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000226 // Special case switch statement to get formatting nice and correct...
227 writeOperand(Operand , true); Out << ",";
228 writeOperand(I->getOperand(1), true); Out << " [";
229
Chris Lattnera073acb2001-07-07 08:36:50 +0000230 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000232 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233 writeOperand(I->getOperand(op+1), true);
234 }
235 Out << "\n\t]";
Chris Lattner4cee8d82001-06-27 23:41:11 +0000236 } else if (I->isPHINode()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +0000237 Out << " " << Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238
Chris Lattner931ef3b2001-06-11 15:04:20 +0000239 Out << " ["; writeOperand(Operand, false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000240 writeOperand(I->getOperand(1), false); Out << " ]";
Chris Lattnera073acb2001-07-07 08:36:50 +0000241 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
242 Out << ", [";
243 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000244 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000245 }
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000246 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000247 Out << " void";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000248 } else if (I->getOpcode() == Instruction::Call) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000249 writeOperand(Operand, true);
250 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000251 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
252 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000253 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000254 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000255 }
256
257 Out << " )";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000258 } else if (I->getOpcode() == Instruction::Malloc ||
259 I->getOpcode() == Instruction::Alloca) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000260 Out << " " << ((const PointerType*)I->getType())->getValueType();
261 if (I->getNumOperands()) {
262 Out << ",";
263 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000264 }
Chris Lattnera6821822001-07-08 04:57:15 +0000265 } else if (I->getOpcode() == Instruction::Cast) {
266 writeOperand(Operand, true);
267 Out << " to " << I->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268 } else if (Operand) { // Print the normal way...
269
270 // PrintAllTypes - Instructions who have operands of all the same type
271 // omit the type from all but the first operand. If the instruction has
272 // different type operands (for example br), then they are all printed.
273 bool PrintAllTypes = false;
274 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000275
Chris Lattnera073acb2001-07-07 08:36:50 +0000276 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
277 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000278 if (Operand->getType() != TheType) {
279 PrintAllTypes = true; // We have differing types! Print them all!
280 break;
281 }
282 }
283
284 if (!PrintAllTypes)
285 Out << " " << I->getOperand(0)->getType();
286
Chris Lattnera073acb2001-07-07 08:36:50 +0000287 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000288 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000289 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000290 }
291 }
292
293 // Print a little comment after the instruction indicating which slot it
294 // occupies.
295 //
Chris Lattnera2f01872001-06-07 16:58:55 +0000296 if (I->getType() != Type::VoidTy) {
297 Out << "\t\t; <" << I->getType() << ">";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000298
Chris Lattnera2f01872001-06-07 16:58:55 +0000299 if (!I->hasName()) {
300 int Slot = Table.getValSlot(I); // Print out the def slot taken...
301 if (Slot >= 0) Out << ":" << Slot;
302 else Out << ":<badref>";
303 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000304 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
305 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000306 Out << endl;
307
308 return false;
309}
310
311
312void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
313 bool PrintName) {
Chris Lattner5e5abe32001-07-20 19:15:21 +0000314 WriteAsOperand(Out, Operand, PrintType, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315}
316
317
318//===----------------------------------------------------------------------===//
319// External Interface declarations
320//===----------------------------------------------------------------------===//
321
322
323
324void WriteToAssembly(const Module *M, ostream &o) {
325 if (M == 0) { o << "<null> module\n"; return; }
326 SlotCalculator SlotTable(M, true);
327 AssemblyWriter W(o, SlotTable);
328
329 W.write(M);
330}
331
332void WriteToAssembly(const Method *M, ostream &o) {
333 if (M == 0) { o << "<null> method\n"; return; }
334 SlotCalculator SlotTable(M->getParent(), true);
335 AssemblyWriter W(o, SlotTable);
336
337 W.write(M);
338}
339
340
341void WriteToAssembly(const BasicBlock *BB, ostream &o) {
342 if (BB == 0) { o << "<null> basic block\n"; return; }
343
344 SlotCalculator SlotTable(BB->getParent(), true);
345 AssemblyWriter W(o, SlotTable);
346
347 W.write(BB);
348}
349
350void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
351 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
352
353 SlotCalculator *SlotTable;
354
355 // A Constant pool value may have a parent that is either a method or a
356 // module. Untangle this now...
357 //
Chris Lattner32e96bc2001-07-14 06:10:33 +0000358 if (const Method *Meth = CPV->getParentV()->castMethod()) {
359 SlotTable = new SlotCalculator(Meth, true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000360 } else {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000361 SlotTable =
Chris Lattner32e96bc2001-07-14 06:10:33 +0000362 new SlotCalculator(CPV->getParentV()->castModuleAsserting(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000363 }
364
365 AssemblyWriter W(o, *SlotTable);
366 W.write(CPV);
367
368 delete SlotTable;
369}
370
371void WriteToAssembly(const Instruction *I, ostream &o) {
372 if (I == 0) { o << "<null> instruction\n"; return; }
373
374 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
375 true);
376 AssemblyWriter W(o, SlotTable);
377
378 W.write(I);
379}