blob: cc62e6007df204972ffa937a899cf36ba7aa3e56 [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...
Chris Lattner90e0d462001-07-25 22:47:55 +0000110 if (isMethod) {
111 if (CP.getParentV()->castMethodAsserting()->getType()->
112 isMethodType()->isVarArg())
113 Out << ", ..."; // Output varargs portion of signature!
114 Out << ")\n";
115 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116
117 ModuleAnalyzer::processConstPool(CP, isMethod);
118
Chris Lattnera7620d92001-07-15 06:35:59 +0000119 if (isMethod) {
120 if (!CP.getParentV()->castMethodAsserting()->isExternal())
121 Out << "begin";
122 } else {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123 Out << "implementation\n";
Chris Lattnera7620d92001-07-15 06:35:59 +0000124 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000125 return false;
126}
127
128
129// processConstant - Print out a constant pool entry...
130//
131bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
132 Out << "\t";
133
134 // Print out name if it exists...
135 if (CPV->hasName())
136 Out << "%" << CPV->getName() << " = ";
137
138 // Print out the opcode...
139 Out << CPV->getType();
140
141 // Write the value out now...
142 writeOperand(CPV, false, false);
143
144 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
145 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
146 Out << "\t\t; <" << CPV->getType() << ">:";
147 if (Slot >= 0) Out << Slot;
148 else Out << "<badref>";
149 }
150
151 Out << endl;
152 return false;
153}
154
155// processMethod - Process all aspects of a method.
156//
157bool AssemblyWriter::processMethod(const Method *M) {
158 // Print out the return type and name...
Chris Lattnera7620d92001-07-15 06:35:59 +0000159 Out << "\n" << (M->isExternal() ? "declare " : "")
160 << M->getReturnType() << " \"" << M->getName() << "\"(";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000161 Table.incorporateMethod(M);
162 ModuleAnalyzer::processMethod(M);
163 Table.purgeMethod();
Chris Lattnera7620d92001-07-15 06:35:59 +0000164 if (!M->isExternal())
165 Out << "end\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000166 return false;
167}
168
169// processMethodArgument - This member is called for every argument that
170// is passed into the method. Simply print it out
171//
172bool AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
173 // Insert commas as we go... the first arg doesn't get a comma
174 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
175
176 // Output type...
177 Out << Arg->getType();
178
179 // Output name, if available...
180 if (Arg->hasName())
181 Out << " %" << Arg->getName();
182 else if (Table.getValSlot(Arg) < 0)
183 Out << "<badref>";
184
185 return false;
186}
187
188// processBasicBlock - This member is called for each basic block in a methd.
189//
190bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
191 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000192 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000193 } else {
194 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000195 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000196 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000197 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000199 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000200 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000201 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202
203 ModuleAnalyzer::processBasicBlock(BB);
204 return false;
205}
206
207// processInstruction - This member is called for each Instruction in a methd.
208//
209bool AssemblyWriter::processInstruction(const Instruction *I) {
210 Out << "\t";
211
212 // Print out name if it exists...
213 if (I && I->hasName())
214 Out << "%" << I->getName() << " = ";
215
216 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000217 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218
219 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000220 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000221
222 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000223 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000224 writeOperand(I->getOperand(2), true);
225 Out << ",";
226 writeOperand(Operand, true);
227 Out << ",";
228 writeOperand(I->getOperand(1), true);
229
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000230 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000231 // Special case switch statement to get formatting nice and correct...
232 writeOperand(Operand , true); Out << ",";
233 writeOperand(I->getOperand(1), true); Out << " [";
234
Chris Lattnera073acb2001-07-07 08:36:50 +0000235 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000236 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000237 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238 writeOperand(I->getOperand(op+1), true);
239 }
240 Out << "\n\t]";
Chris Lattner4cee8d82001-06-27 23:41:11 +0000241 } else if (I->isPHINode()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +0000242 Out << " " << Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243
Chris Lattner931ef3b2001-06-11 15:04:20 +0000244 Out << " ["; writeOperand(Operand, false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000245 writeOperand(I->getOperand(1), false); Out << " ]";
Chris Lattnera073acb2001-07-07 08:36:50 +0000246 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
247 Out << ", [";
248 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000249 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000250 }
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000251 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000252 Out << " void";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000253 } else if (I->getOpcode() == Instruction::Call) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000254 writeOperand(Operand, true);
255 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000256 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
257 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000258 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000259 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000260 }
261
262 Out << " )";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000263 } else if (I->getOpcode() == Instruction::Malloc ||
264 I->getOpcode() == Instruction::Alloca) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000265 Out << " " << ((const PointerType*)I->getType())->getValueType();
266 if (I->getNumOperands()) {
267 Out << ",";
268 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269 }
Chris Lattnera6821822001-07-08 04:57:15 +0000270 } else if (I->getOpcode() == Instruction::Cast) {
271 writeOperand(Operand, true);
272 Out << " to " << I->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273 } else if (Operand) { // Print the normal way...
274
275 // PrintAllTypes - Instructions who have operands of all the same type
276 // omit the type from all but the first operand. If the instruction has
277 // different type operands (for example br), then they are all printed.
278 bool PrintAllTypes = false;
279 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000280
Chris Lattnera073acb2001-07-07 08:36:50 +0000281 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
282 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000283 if (Operand->getType() != TheType) {
284 PrintAllTypes = true; // We have differing types! Print them all!
285 break;
286 }
287 }
288
289 if (!PrintAllTypes)
290 Out << " " << I->getOperand(0)->getType();
291
Chris Lattnera073acb2001-07-07 08:36:50 +0000292 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000293 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000294 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295 }
296 }
297
298 // Print a little comment after the instruction indicating which slot it
299 // occupies.
300 //
Chris Lattnera2f01872001-06-07 16:58:55 +0000301 if (I->getType() != Type::VoidTy) {
302 Out << "\t\t; <" << I->getType() << ">";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000303
Chris Lattnera2f01872001-06-07 16:58:55 +0000304 if (!I->hasName()) {
305 int Slot = Table.getValSlot(I); // Print out the def slot taken...
306 if (Slot >= 0) Out << ":" << Slot;
307 else Out << ":<badref>";
308 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000309 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
310 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311 Out << endl;
312
313 return false;
314}
315
316
317void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
318 bool PrintName) {
Chris Lattner5e5abe32001-07-20 19:15:21 +0000319 WriteAsOperand(Out, Operand, PrintType, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000320}
321
322
323//===----------------------------------------------------------------------===//
324// External Interface declarations
325//===----------------------------------------------------------------------===//
326
327
328
329void WriteToAssembly(const Module *M, ostream &o) {
330 if (M == 0) { o << "<null> module\n"; return; }
331 SlotCalculator SlotTable(M, true);
332 AssemblyWriter W(o, SlotTable);
333
334 W.write(M);
335}
336
337void WriteToAssembly(const Method *M, ostream &o) {
338 if (M == 0) { o << "<null> method\n"; return; }
339 SlotCalculator SlotTable(M->getParent(), true);
340 AssemblyWriter W(o, SlotTable);
341
342 W.write(M);
343}
344
345
346void WriteToAssembly(const BasicBlock *BB, ostream &o) {
347 if (BB == 0) { o << "<null> basic block\n"; return; }
348
349 SlotCalculator SlotTable(BB->getParent(), true);
350 AssemblyWriter W(o, SlotTable);
351
352 W.write(BB);
353}
354
355void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
356 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
357
358 SlotCalculator *SlotTable;
359
360 // A Constant pool value may have a parent that is either a method or a
361 // module. Untangle this now...
362 //
Chris Lattner32e96bc2001-07-14 06:10:33 +0000363 if (const Method *Meth = CPV->getParentV()->castMethod()) {
364 SlotTable = new SlotCalculator(Meth, true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000365 } else {
Chris Lattner4cee8d82001-06-27 23:41:11 +0000366 SlotTable =
Chris Lattner32e96bc2001-07-14 06:10:33 +0000367 new SlotCalculator(CPV->getParentV()->castModuleAsserting(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000368 }
369
370 AssemblyWriter W(o, *SlotTable);
371 W.write(CPV);
372
373 delete SlotTable;
374}
375
376void WriteToAssembly(const Instruction *I, ostream &o) {
377 if (I == 0) { o << "<null> instruction\n"; return; }
378
379 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
380 true);
381 AssemblyWriter W(o, SlotTable);
382
383 W.write(I);
384}