blob: 926e61160c99608100cb54742f37f333dbdab75f [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"
Chris Lattnerda975502001-09-10 07:58:01 +000017#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/BasicBlock.h"
19#include "llvm/ConstPoolVals.h"
20#include "llvm/iOther.h"
21#include "llvm/iMemory.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000022#include "llvm/Support/STLExtras.h"
23#include "llvm/SymbolTable.h"
24#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000025
Chris Lattner2e9fee42001-07-12 23:35:26 +000026void DebugValue(const Value *V) {
27 cerr << V << endl;
28}
29
Chris Lattner5e5abe32001-07-20 19:15:21 +000030// WriteAsOperand - Write the name of the specified value out to the specified
31// ostream. This can be useful when you just want to print int %reg126, not the
32// whole instruction that generated it.
33//
34ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
35 bool PrintName, SlotCalculator *Table) {
36 if (PrintType)
37 Out << " " << V->getType();
38
Chris Lattnerfee714f2001-09-07 16:36:04 +000039 if (PrintName && V->hasName()) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000040 Out << " %" << V->getName();
41 } else {
42 if (const ConstPoolVal *CPV = V->castConstant()) {
43 Out << " " << CPV->getStrValue();
44 } else {
45 int Slot;
46 if (Table) {
47 Slot = Table->getValSlot(V);
48 } else {
49 if (const Type *Ty = V->castType()) {
50 return Out << " " << Ty;
51 } else if (const MethodArgument *MA = V->castMethodArgument()) {
52 Table = new SlotCalculator(MA->getParent(), true);
53 } else if (const Instruction *I = V->castInstruction()) {
54 Table = new SlotCalculator(I->getParent()->getParent(), true);
55 } else if (const BasicBlock *BB = V->castBasicBlock()) {
56 Table = new SlotCalculator(BB->getParent(), true);
57 } else if (const Method *Meth = V->castMethod()) {
58 Table = new SlotCalculator(Meth, true);
59 } else if (const Module *Mod = V->castModule()) {
60 Table = new SlotCalculator(Mod, true);
61 } else {
62 return Out << "BAD VALUE TYPE!";
63 }
64 Slot = Table->getValSlot(V);
65 delete Table;
66 }
67 if (Slot >= 0) Out << " %" << Slot;
68 else if (PrintName)
69 Out << "<badref>"; // Not embeded into a location?
70 }
71 }
72 return Out;
73}
74
75
Chris Lattner2e9fee42001-07-12 23:35:26 +000076
Chris Lattnerfee714f2001-09-07 16:36:04 +000077class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +000078 ostream &Out;
79 SlotCalculator &Table;
80public:
81 inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) {
82 }
83
84 inline void write(const Module *M) { processModule(M); }
Chris Lattnerda975502001-09-10 07:58:01 +000085 inline void write(const GlobalVariable *G) { processGlobal(G); }
Chris Lattner2f7c9632001-06-06 20:29:01 +000086 inline void write(const Method *M) { processMethod(M); }
87 inline void write(const BasicBlock *BB) { processBasicBlock(BB); }
88 inline void write(const Instruction *I) { processInstruction(I); }
89 inline void write(const ConstPoolVal *CPV) { processConstant(CPV); }
90
Chris Lattner2f7c9632001-06-06 20:29:01 +000091private :
Chris Lattnerfee714f2001-09-07 16:36:04 +000092 void processModule(const Module *M);
93 void processSymbolTable(const SymbolTable &ST);
94 void processConstant(const ConstPoolVal *CPV);
Chris Lattnerda975502001-09-10 07:58:01 +000095 void processGlobal(const GlobalVariable *GV);
Chris Lattnerfee714f2001-09-07 16:36:04 +000096 void processMethod(const Method *M);
97 void processMethodArgument(const MethodArgument *MA);
98 void processBasicBlock(const BasicBlock *BB);
99 void processInstruction(const Instruction *I);
Chris Lattnerda975502001-09-10 07:58:01 +0000100
Chris Lattner2f7c9632001-06-06 20:29:01 +0000101 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
102};
103
104
Chris Lattnerfee714f2001-09-07 16:36:04 +0000105void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
106 bool PrintName) {
107 WriteAsOperand(Out, Operand, PrintType, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000108}
109
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110
Chris Lattnerfee714f2001-09-07 16:36:04 +0000111void AssemblyWriter::processModule(const Module *M) {
112 // Loop over the symbol table, emitting all named constants...
113 if (M->hasSymbolTable())
114 processSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000115
116 for_each(M->gbegin(), M->gend(),
117 bind_obj(this, &AssemblyWriter::processGlobal));
118
Chris Lattnerfee714f2001-09-07 16:36:04 +0000119 Out << "implementation\n";
120
121 // Output all of the methods...
122 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::processMethod));
123}
124
Chris Lattnerda975502001-09-10 07:58:01 +0000125void AssemblyWriter::processGlobal(const GlobalVariable *GV) {
126 Out << "global ";
127 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
128 Out << GV->getType()->getDescription() << endl;
129}
130
Chris Lattnerfee714f2001-09-07 16:36:04 +0000131
132// processSymbolTable - Run through symbol table looking for named constants
133// if a named constant is found, emit it's declaration...
134//
135void AssemblyWriter::processSymbolTable(const SymbolTable &ST) {
136 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
137 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
138 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
139
140 for (; I != End; ++I) {
141 const Value *V = I->second;
142 if (const ConstPoolVal *CPV = V->castConstant()) {
143 processConstant(CPV);
144 } else if (const Type *Ty = V->castType()) {
145 Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
146 }
147 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000148 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149}
150
151
152// processConstant - Print out a constant pool entry...
153//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000154void AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
155 // Don't print out unnamed constants, they will be inlined
156 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000157
Chris Lattneree998be2001-07-26 16:29:38 +0000158 // Print out name...
159 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000160
Chris Lattneree998be2001-07-26 16:29:38 +0000161 // Print out the constant type...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000162 Out << CPV->getType();
163
164 // Write the value out now...
165 writeOperand(CPV, false, false);
166
167 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
168 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
169 Out << "\t\t; <" << CPV->getType() << ">:";
170 if (Slot >= 0) Out << Slot;
171 else Out << "<badref>";
172 }
173
174 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000175}
176
177// processMethod - Process all aspects of a method.
178//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000179void AssemblyWriter::processMethod(const Method *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000180 // Print out the return type and name...
Chris Lattnera7620d92001-07-15 06:35:59 +0000181 Out << "\n" << (M->isExternal() ? "declare " : "")
182 << M->getReturnType() << " \"" << M->getName() << "\"(";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000183 Table.incorporateMethod(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000184
185 // Loop over the arguments, processing them...
186 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
187 bind_obj(this, &AssemblyWriter::processMethodArgument));
188
189
190 // Finish printing arguments...
191 const MethodType *MT = (const MethodType*)M->getType();
192 if (MT->isVarArg()) {
193 if (MT->getParamTypes().size()) Out << ", ";
194 Out << "..."; // Output varargs portion of signature!
195 }
196 Out << ")\n";
197
198 if (!M->isExternal()) {
199 // Loop over the symbol table, emitting all named constants...
200 if (M->hasSymbolTable())
201 processSymbolTable(*M->getSymbolTable());
202
203 Out << "begin";
204
205 // Output all of its basic blocks... for the method
206 for_each(M->begin(), M->end(),
207 bind_obj(this, &AssemblyWriter::processBasicBlock));
208
Chris Lattnera7620d92001-07-15 06:35:59 +0000209 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000210 }
211
212 Table.purgeMethod();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000213}
214
215// processMethodArgument - This member is called for every argument that
216// is passed into the method. Simply print it out
217//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000218void AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000219 // Insert commas as we go... the first arg doesn't get a comma
220 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
221
222 // Output type...
223 Out << Arg->getType();
224
225 // Output name, if available...
226 if (Arg->hasName())
227 Out << " %" << Arg->getName();
228 else if (Table.getValSlot(Arg) < 0)
229 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230}
231
232// processBasicBlock - This member is called for each basic block in a methd.
233//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000234void AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000235 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000236 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000237 } else {
238 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000239 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000240 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000241 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000243 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000244 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000245 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000246
Chris Lattnerfee714f2001-09-07 16:36:04 +0000247 // Output all of the instructions in the basic block...
248 for_each(BB->begin(), BB->end(),
249 bind_obj(this, &AssemblyWriter::processInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000250}
251
252// processInstruction - This member is called for each Instruction in a methd.
253//
Chris Lattnerfee714f2001-09-07 16:36:04 +0000254void AssemblyWriter::processInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000255 Out << "\t";
256
257 // Print out name if it exists...
258 if (I && I->hasName())
259 Out << "%" << I->getName() << " = ";
260
261 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000262 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000263
264 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000265 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000266
267 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000268 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000269 writeOperand(I->getOperand(2), true);
270 Out << ",";
271 writeOperand(Operand, true);
272 Out << ",";
273 writeOperand(I->getOperand(1), true);
274
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000275 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000276 // Special case switch statement to get formatting nice and correct...
277 writeOperand(Operand , true); Out << ",";
278 writeOperand(I->getOperand(1), true); Out << " [";
279
Chris Lattnera073acb2001-07-07 08:36:50 +0000280 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000281 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000282 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000283 writeOperand(I->getOperand(op+1), true);
284 }
285 Out << "\n\t]";
Chris Lattner4cee8d82001-06-27 23:41:11 +0000286 } else if (I->isPHINode()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +0000287 Out << " " << Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000288
Chris Lattner931ef3b2001-06-11 15:04:20 +0000289 Out << " ["; writeOperand(Operand, false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000290 writeOperand(I->getOperand(1), false); Out << " ]";
Chris Lattnera073acb2001-07-07 08:36:50 +0000291 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
292 Out << ", [";
293 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000294 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000295 }
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000296 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000297 Out << " void";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000298 } else if (I->getOpcode() == Instruction::Call) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299 writeOperand(Operand, true);
300 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000301 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
302 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000303 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000304 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000305 }
306
307 Out << " )";
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000308 } else if (I->getOpcode() == Instruction::Malloc ||
309 I->getOpcode() == Instruction::Alloca) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000310 Out << " " << ((const PointerType*)I->getType())->getValueType();
311 if (I->getNumOperands()) {
312 Out << ",";
313 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000314 }
Chris Lattnera6821822001-07-08 04:57:15 +0000315 } else if (I->getOpcode() == Instruction::Cast) {
316 writeOperand(Operand, true);
317 Out << " to " << I->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000318 } else if (Operand) { // Print the normal way...
319
320 // PrintAllTypes - Instructions who have operands of all the same type
321 // omit the type from all but the first operand. If the instruction has
322 // different type operands (for example br), then they are all printed.
323 bool PrintAllTypes = false;
324 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000325
Chris Lattnera073acb2001-07-07 08:36:50 +0000326 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
327 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000328 if (Operand->getType() != TheType) {
329 PrintAllTypes = true; // We have differing types! Print them all!
330 break;
331 }
332 }
333
334 if (!PrintAllTypes)
335 Out << " " << I->getOperand(0)->getType();
336
Chris Lattnera073acb2001-07-07 08:36:50 +0000337 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000339 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000340 }
341 }
342
343 // Print a little comment after the instruction indicating which slot it
344 // occupies.
345 //
Chris Lattnera2f01872001-06-07 16:58:55 +0000346 if (I->getType() != Type::VoidTy) {
347 Out << "\t\t; <" << I->getType() << ">";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000348
Chris Lattnera2f01872001-06-07 16:58:55 +0000349 if (!I->hasName()) {
350 int Slot = Table.getValSlot(I); // Print out the def slot taken...
351 if (Slot >= 0) Out << ":" << Slot;
352 else Out << ":<badref>";
353 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000354 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
355 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000356 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000357}
358
359
360//===----------------------------------------------------------------------===//
361// External Interface declarations
362//===----------------------------------------------------------------------===//
363
364
365
366void WriteToAssembly(const Module *M, ostream &o) {
367 if (M == 0) { o << "<null> module\n"; return; }
368 SlotCalculator SlotTable(M, true);
369 AssemblyWriter W(o, SlotTable);
370
371 W.write(M);
372}
373
Chris Lattneradfe0d12001-09-10 20:08:19 +0000374void WriteToAssembly(const GlobalVariable *G, ostream &o) {
375 if (G == 0) { o << "<null> global variable\n"; return; }
376 SlotCalculator SlotTable(G->getParent(), true);
377 AssemblyWriter W(o, SlotTable);
378 W.write(G);
379}
380
Chris Lattner2f7c9632001-06-06 20:29:01 +0000381void WriteToAssembly(const Method *M, ostream &o) {
382 if (M == 0) { o << "<null> method\n"; return; }
383 SlotCalculator SlotTable(M->getParent(), true);
384 AssemblyWriter W(o, SlotTable);
385
386 W.write(M);
387}
388
389
390void WriteToAssembly(const BasicBlock *BB, ostream &o) {
391 if (BB == 0) { o << "<null> basic block\n"; return; }
392
393 SlotCalculator SlotTable(BB->getParent(), true);
394 AssemblyWriter W(o, SlotTable);
395
396 W.write(BB);
397}
398
399void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
400 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
Chris Lattner2091efb2001-07-28 17:49:02 +0000401 WriteAsOperand(o, CPV, true, true, 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000402}
403
404void WriteToAssembly(const Instruction *I, ostream &o) {
405 if (I == 0) { o << "<null> instruction\n"; return; }
406
407 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
408 true);
409 AssemblyWriter W(o, SlotTable);
410
411 W.write(I);
412}