* Assembly writer is not a module analyzer anymore
[oota-llvm.git] / lib / VMCore / AsmWriter.cpp
1 //===-- 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 #include "llvm/Support/STLExtras.h"
22 #include "llvm/SymbolTable.h"
23 #include <algorithm>
24
25 void DebugValue(const Value *V) {
26   cerr << V << endl;
27 }
28
29 // 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 //
33 ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType, 
34                         bool PrintName, SlotCalculator *Table) {
35   if (PrintType)
36     Out << " " << V->getType();
37   
38   if (PrintName && V->hasName()) {
39     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
75
76 class AssemblyWriter {
77   ostream &Out;
78   SlotCalculator &Table;
79 public:
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
89 private :
90   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
98   void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
99 };
100
101
102 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, 
103                                   bool PrintName) {
104   WriteAsOperand(Out, Operand, PrintType, PrintName, &Table);
105 }
106
107
108 void 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 //
123 void 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     }
136   }
137 }
138
139
140 // processConstant - Print out a constant pool entry...
141 //
142 void AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
143   // Don't print out unnamed constants, they will be inlined
144   if (!CPV->hasName()) return;
145
146   // Print out name...
147   Out << "\t%" << CPV->getName() << " = ";
148
149   // Print out the constant type...
150   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;
163 }
164
165 // processMethod - Process all aspects of a method.
166 //
167 void AssemblyWriter::processMethod(const Method *M) {
168   // Print out the return type and name...
169   Out << "\n" << (M->isExternal() ? "declare " : "") 
170       << M->getReturnType() << " \"" << M->getName() << "\"(";
171   Table.incorporateMethod(M);
172
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
197     Out << "end\n";
198   }
199
200   Table.purgeMethod();
201 }
202
203 // processMethodArgument - This member is called for every argument that 
204 // is passed into the method.  Simply print it out
205 //
206 void AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
207   // 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>";
218 }
219
220 // processBasicBlock - This member is called for each basic block in a methd.
221 //
222 void AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
223   if (BB->hasName()) {              // Print out the label if it exists...
224     Out << "\n" << BB->getName() << ":";
225   } else {
226     int Slot = Table.getValSlot(BB);
227     Out << "\n; <label>:";
228     if (Slot >= 0) 
229       Out << Slot;         // Extra newline seperates out label's
230     else 
231       Out << "<badref>"; 
232   }
233   Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n";  // Output # uses
234
235   // Output all of the instructions in the basic block...
236   for_each(BB->begin(), BB->end(),
237            bind_obj(this, &AssemblyWriter::processInstruction));
238 }
239
240 // processInstruction - This member is called for each Instruction in a methd.
241 //
242 void AssemblyWriter::processInstruction(const Instruction *I) {
243   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...
250   Out << I->getOpcodeName();
251
252   // Print out the type of the operands...
253   const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
254
255   // Special case conditional branches to swizzle the condition out to the front
256   if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
257     writeOperand(I->getOperand(2), true);
258     Out << ",";
259     writeOperand(Operand, true);
260     Out << ",";
261     writeOperand(I->getOperand(1), true);
262
263   } else if (I->getOpcode() == Instruction::Switch) {
264     // Special case switch statement to get formatting nice and correct...
265     writeOperand(Operand         , true); Out << ",";
266     writeOperand(I->getOperand(1), true); Out << " [";
267
268     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
269       Out << "\n\t\t";
270       writeOperand(I->getOperand(op  ), true); Out << ",";
271       writeOperand(I->getOperand(op+1), true);
272     }
273     Out << "\n\t]";
274   } else if (I->isPHINode()) {
275     Out << " " << Operand->getType();
276
277     Out << " [";  writeOperand(Operand, false); Out << ",";
278     writeOperand(I->getOperand(1), false); Out << " ]";
279     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
280       Out << ", [";  
281       writeOperand(I->getOperand(op  ), false); Out << ",";
282       writeOperand(I->getOperand(op+1), false); Out << " ]";
283     }
284   } else if (I->getOpcode() == Instruction::Ret && !Operand) {
285     Out << " void";
286   } else if (I->getOpcode() == Instruction::Call) {
287     writeOperand(Operand, true);
288     Out << "(";
289     if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
290     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
291       Out << ",";
292       writeOperand(I->getOperand(op), true);
293     }
294
295     Out << " )";
296   } else if (I->getOpcode() == Instruction::Malloc || 
297              I->getOpcode() == Instruction::Alloca) {
298     Out << " " << ((const PointerType*)I->getType())->getValueType();
299     if (I->getNumOperands()) {
300       Out << ",";
301       writeOperand(I->getOperand(0), true);
302     }
303   } else if (I->getOpcode() == Instruction::Cast) {
304     writeOperand(Operand, true);
305     Out << " to " << I->getType();
306   } 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();
313
314     for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
315       Operand = I->getOperand(i);
316       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
325     for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
326       if (i) Out << ",";
327       writeOperand(I->getOperand(i), PrintAllTypes);
328     }
329   }
330
331   // Print a little comment after the instruction indicating which slot it
332   // occupies.
333   //
334   if (I->getType() != Type::VoidTy) {
335     Out << "\t\t; <" << I->getType() << ">";
336
337     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     }
342     Out << "\t[#uses=" << I->use_size() << "]";  // Output # uses
343   }
344   Out << endl;
345 }
346
347
348 //===----------------------------------------------------------------------===//
349 //                       External Interface declarations
350 //===----------------------------------------------------------------------===//
351
352
353
354 void 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
362 void 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
371 void 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
380 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
381   if (CPV == 0) { o << "<null> constant pool value\n"; return; }
382   WriteAsOperand(o, CPV, true, true, 0);
383 }
384
385 void 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 }