blob: a8951e61d1e7bf0e848fefe98ff4bdecb4d9dcc4 [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"
Chris Lattnerfee714f2001-09-07 16:36:04 +000021#include "llvm/Support/STLExtras.h"
22#include "llvm/SymbolTable.h"
23#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000024
Chris Lattner2e9fee42001-07-12 23:35:26 +000025void DebugValue(const Value *V) {
26 cerr << V << endl;
27}
28
Chris Lattner5e5abe32001-07-20 19:15:21 +000029// WriteAsOperand - Write the name of the specified value out to the specified
30// ostream. This can be useful when you just want to print int %reg126, not the
31// whole instruction that generated it.
32//
33ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
34 bool PrintName, SlotCalculator *Table) {
35 if (PrintType)
36 Out << " " << V->getType();
37
Chris Lattnerfee714f2001-09-07 16:36:04 +000038 if (PrintName && V->hasName()) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000039 Out << " %" << V->getName();
40 } else {
41 if (const ConstPoolVal *CPV = V->castConstant()) {
42 Out << " " << CPV->getStrValue();
43 } else {
44 int Slot;
45 if (Table) {
46 Slot = Table->getValSlot(V);
47 } else {
48 if (const Type *Ty = V->castType()) {
49 return Out << " " << Ty;
50 } else if (const MethodArgument *MA = V->castMethodArgument()) {
51 Table = new SlotCalculator(MA->getParent(), true);
52 } else if (const Instruction *I = V->castInstruction()) {
53 Table = new SlotCalculator(I->getParent()->getParent(), true);
54 } else if (const BasicBlock *BB = V->castBasicBlock()) {
55 Table = new SlotCalculator(BB->getParent(), true);
56 } else if (const Method *Meth = V->castMethod()) {
57 Table = new SlotCalculator(Meth, true);
58 } else if (const Module *Mod = V->castModule()) {
59 Table = new SlotCalculator(Mod, true);
60 } else {
61 return Out << "BAD VALUE TYPE!";
62 }
63 Slot = Table->getValSlot(V);
64 delete Table;
65 }
66 if (Slot >= 0) Out << " %" << Slot;
67 else if (PrintName)
68 Out << "<badref>"; // Not embeded into a location?
69 }
70 }
71 return Out;
72}
73
74
Chris Lattner2e9fee42001-07-12 23:35:26 +000075
Chris Lattnerfee714f2001-09-07 16:36:04 +000076class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +000077 ostream &Out;
78 SlotCalculator &Table;
79public:
80 inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) {
81 }
82
83 inline void write(const Module *M) { processModule(M); }
84 inline void write(const Method *M) { processMethod(M); }
85 inline void write(const BasicBlock *BB) { processBasicBlock(BB); }
86 inline void write(const Instruction *I) { processInstruction(I); }
87 inline void write(const ConstPoolVal *CPV) { processConstant(CPV); }
88
Chris Lattner2f7c9632001-06-06 20:29:01 +000089private :
Chris Lattnerfee714f2001-09-07 16:36:04 +000090 void processModule(const Module *M);
91 void processSymbolTable(const SymbolTable &ST);
92 void processConstant(const ConstPoolVal *CPV);
93 void processMethod(const Method *M);
94 void processMethodArgument(const MethodArgument *MA);
95 void processBasicBlock(const BasicBlock *BB);
96 void processInstruction(const Instruction *I);
97
Chris Lattner2f7c9632001-06-06 20:29:01 +000098 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
99};
100
101
Chris Lattnerfee714f2001-09-07 16:36:04 +0000102void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
103 bool PrintName) {
104 WriteAsOperand(Out, Operand, PrintType, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105}
106
Chris Lattner2f7c9632001-06-06 20:29:01 +0000107
Chris Lattnerfee714f2001-09-07 16:36:04 +0000108void AssemblyWriter::processModule(const Module *M) {
109 // Loop over the symbol table, emitting all named constants...
110 if (M->hasSymbolTable())
111 processSymbolTable(*M->getSymbolTable());
112
113 Out << "implementation\n";
114
115 // Output all of the methods...
116 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::processMethod));
117}
118
119
120// processSymbolTable - Run through symbol table looking for named constants
121// if a named constant is found, emit it's declaration...
122//
123void AssemblyWriter::processSymbolTable(const SymbolTable &ST) {
124 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
125 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
126 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
127
128 for (; I != End; ++I) {
129 const Value *V = I->second;
130 if (const ConstPoolVal *CPV = V->castConstant()) {
131 processConstant(CPV);
132 } else if (const Type *Ty = V->castType()) {
133 Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
134 }
135 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000136 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000137}
138
139
140// processConstant - Print out a constant pool entry...
141//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000142void AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
143 // Don't print out unnamed constants, they will be inlined
144 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000145
Chris Lattneree998be2001-07-26 16:29:38 +0000146 // Print out name...
147 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000148
Chris Lattneree998be2001-07-26 16:29:38 +0000149 // Print out the constant type...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000150 Out << CPV->getType();
151
152 // Write the value out now...
153 writeOperand(CPV, false, false);
154
155 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
156 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
157 Out << "\t\t; <" << CPV->getType() << ">:";
158 if (Slot >= 0) Out << Slot;
159 else Out << "<badref>";
160 }
161
162 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163}
164
165// processMethod - Process all aspects of a method.
166//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000167void AssemblyWriter::processMethod(const Method *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168 // Print out the return type and name...
Chris Lattnera7620d92001-07-15 06:35:59 +0000169 Out << "\n" << (M->isExternal() ? "declare " : "")
170 << M->getReturnType() << " \"" << M->getName() << "\"(";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000171 Table.incorporateMethod(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000172
173 // Loop over the arguments, processing them...
174 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
175 bind_obj(this, &AssemblyWriter::processMethodArgument));
176
177
178 // Finish printing arguments...
179 const MethodType *MT = (const MethodType*)M->getType();
180 if (MT->isVarArg()) {
181 if (MT->getParamTypes().size()) Out << ", ";
182 Out << "..."; // Output varargs portion of signature!
183 }
184 Out << ")\n";
185
186 if (!M->isExternal()) {
187 // Loop over the symbol table, emitting all named constants...
188 if (M->hasSymbolTable())
189 processSymbolTable(*M->getSymbolTable());
190
191 Out << "begin";
192
193 // Output all of its basic blocks... for the method
194 for_each(M->begin(), M->end(),
195 bind_obj(this, &AssemblyWriter::processBasicBlock));
196
Chris Lattnera7620d92001-07-15 06:35:59 +0000197 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000198 }
199
200 Table.purgeMethod();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000201}
202
203// processMethodArgument - This member is called for every argument that
204// is passed into the method. Simply print it out
205//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000206void AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000207 // Insert commas as we go... the first arg doesn't get a comma
208 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
209
210 // Output type...
211 Out << Arg->getType();
212
213 // Output name, if available...
214 if (Arg->hasName())
215 Out << " %" << Arg->getName();
216 else if (Table.getValSlot(Arg) < 0)
217 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000218}
219
220// processBasicBlock - This member is called for each basic block in a methd.
221//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000222void AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000223 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000224 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225 } else {
226 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000227 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000229 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000231 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000232 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000233 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000234
Chris Lattnerfee714f2001-09-07 16:36:04 +0000235 // Output all of the instructions in the basic block...
236 for_each(BB->begin(), BB->end(),
237 bind_obj(this, &AssemblyWriter::processInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000238}
239
240// processInstruction - This member is called for each Instruction in a methd.
241//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000242void AssemblyWriter::processInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000243 Out << "\t";
244
245 // Print out name if it exists...
246 if (I && I->hasName())
247 Out << "%" << I->getName() << " = ";
248
249 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000250 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000251
252 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000253 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000254
255 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000256 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000257 writeOperand(I->getOperand(2), true);
258 Out << ",";
259 writeOperand(Operand, true);
260 Out << ",";
261 writeOperand(I->getOperand(1), true);
262
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000263 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000264 // Special case switch statement to get formatting nice and correct...
265 writeOperand(Operand , true); Out << ",";
266 writeOperand(I->getOperand(1), true); Out << " [";
267
Chris Lattnera073acb2001-07-07 08:36:50 +0000268 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000270 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000271 writeOperand(I->getOperand(op+1), true);
272 }
273 Out << "\n\t]";
Chris Lattner4cee8d82001-06-27 23:41:11 +0000274 } else if (I->isPHINode()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +0000275 Out << " " << Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000276
Chris Lattner931ef3b2001-06-11 15:04:20 +0000277 Out << " ["; writeOperand(Operand, false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000278 writeOperand(I->getOperand(1), false); Out << " ]";
Chris Lattnera073acb2001-07-07 08:36:50 +0000279 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
280 Out << ", [";
281 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000282 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000283 }
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000284 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000285 Out << " void";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000286 } else if (I->getOpcode() == Instruction::Call) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000287 writeOperand(Operand, true);
288 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000289 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
290 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000291 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000292 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000293 }
294
295 Out << " )";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000296 } else if (I->getOpcode() == Instruction::Malloc ||
297 I->getOpcode() == Instruction::Alloca) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000298 Out << " " << ((const PointerType*)I->getType())->getValueType();
299 if (I->getNumOperands()) {
300 Out << ",";
301 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000302 }
Chris Lattnera6821822001-07-08 04:57:15 +0000303 } else if (I->getOpcode() == Instruction::Cast) {
304 writeOperand(Operand, true);
305 Out << " to " << I->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000306 } else if (Operand) { // Print the normal way...
307
308 // PrintAllTypes - Instructions who have operands of all the same type
309 // omit the type from all but the first operand. If the instruction has
310 // different type operands (for example br), then they are all printed.
311 bool PrintAllTypes = false;
312 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313
Chris Lattnera073acb2001-07-07 08:36:50 +0000314 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
315 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000316 if (Operand->getType() != TheType) {
317 PrintAllTypes = true; // We have differing types! Print them all!
318 break;
319 }
320 }
321
322 if (!PrintAllTypes)
323 Out << " " << I->getOperand(0)->getType();
324
Chris Lattnera073acb2001-07-07 08:36:50 +0000325 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000326 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000327 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000328 }
329 }
330
331 // Print a little comment after the instruction indicating which slot it
332 // occupies.
333 //
Chris Lattnera2f01872001-06-07 16:58:55 +0000334 if (I->getType() != Type::VoidTy) {
335 Out << "\t\t; <" << I->getType() << ">";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000336
Chris Lattnera2f01872001-06-07 16:58:55 +0000337 if (!I->hasName()) {
338 int Slot = Table.getValSlot(I); // Print out the def slot taken...
339 if (Slot >= 0) Out << ":" << Slot;
340 else Out << ":<badref>";
341 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000342 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
343 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000345}
346
347
348//===----------------------------------------------------------------------===//
349// External Interface declarations
350//===----------------------------------------------------------------------===//
351
352
353
354void WriteToAssembly(const Module *M, ostream &o) {
355 if (M == 0) { o << "<null> module\n"; return; }
356 SlotCalculator SlotTable(M, true);
357 AssemblyWriter W(o, SlotTable);
358
359 W.write(M);
360}
361
362void WriteToAssembly(const Method *M, ostream &o) {
363 if (M == 0) { o << "<null> method\n"; return; }
364 SlotCalculator SlotTable(M->getParent(), true);
365 AssemblyWriter W(o, SlotTable);
366
367 W.write(M);
368}
369
370
371void WriteToAssembly(const BasicBlock *BB, ostream &o) {
372 if (BB == 0) { o << "<null> basic block\n"; return; }
373
374 SlotCalculator SlotTable(BB->getParent(), true);
375 AssemblyWriter W(o, SlotTable);
376
377 W.write(BB);
378}
379
380void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
381 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
Chris Lattner2091efb2001-07-28 17:49:02 +0000382 WriteAsOperand(o, CPV, true, true, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000383}
384
385void WriteToAssembly(const Instruction *I, ostream &o) {
386 if (I == 0) { o << "<null> instruction\n"; return; }
387
388 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
389 true);
390 AssemblyWriter W(o, SlotTable);
391
392 W.write(I);
393}