e8025441266db98aa406b49e6263cce67a7f3d64
[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
22 void DebugValue(const Value *V) {
23   cerr << V << endl;
24 }
25
26
27 class AssemblyWriter : public ModuleAnalyzer {
28   ostream &Out;
29   SlotCalculator &Table;
30 public:
31   inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) {
32   }
33
34   inline void write(const Module *M)         { processModule(M);      }
35   inline void write(const Method *M)         { processMethod(M);      }
36   inline void write(const BasicBlock *BB)    { processBasicBlock(BB); }
37   inline void write(const Instruction *I)    { processInstruction(I); }
38   inline void write(const ConstPoolVal *CPV) { processConstant(CPV);  }
39
40 protected:
41   virtual bool visitMethod(const Method *M);
42   virtual bool processConstPool(const ConstantPool &CP, bool isMethod);
43   virtual bool processConstant(const ConstPoolVal *CPV);
44   virtual bool processMethod(const Method *M);
45   virtual bool processMethodArgument(const MethodArgument *MA);
46   virtual bool processBasicBlock(const BasicBlock *BB);
47   virtual bool processInstruction(const Instruction *I);
48
49 private :
50   void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
51 };
52
53
54
55 // visitMethod - This member is called after the above two steps, visting each
56 // method, because they are effectively values that go into the constant pool.
57 //
58 bool AssemblyWriter::visitMethod(const Method *M) {
59   return false;
60 }
61
62 bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
63   // Done printing arguments...
64   if (isMethod) Out << ")\n";
65
66   ModuleAnalyzer::processConstPool(CP, isMethod);
67   
68   if (isMethod) {
69     if (!CP.getParentV()->castMethodAsserting()->isExternal())
70       Out << "begin";
71   } else {
72     Out << "implementation\n";
73   }
74   return false;
75 }
76
77
78 // processConstant - Print out a constant pool entry...
79 //
80 bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
81   Out << "\t";
82
83   // Print out name if it exists...
84   if (CPV->hasName())
85     Out << "%" << CPV->getName() << " = ";
86
87   // Print out the opcode...
88   Out << CPV->getType();
89
90   // Write the value out now...
91   writeOperand(CPV, false, false);
92
93   if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
94     int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
95     Out << "\t\t; <" << CPV->getType() << ">:";
96     if (Slot >= 0) Out << Slot;
97     else Out << "<badref>";
98   } 
99
100   Out << endl;
101   return false;
102 }
103
104 // processMethod - Process all aspects of a method.
105 //
106 bool AssemblyWriter::processMethod(const Method *M) {
107   // Print out the return type and name...
108   Out << "\n" << (M->isExternal() ? "declare " : "") 
109       << M->getReturnType() << " \"" << M->getName() << "\"(";
110   Table.incorporateMethod(M);
111   ModuleAnalyzer::processMethod(M);
112   Table.purgeMethod();
113   if (!M->isExternal())
114     Out << "end\n";
115   return false;
116 }
117
118 // processMethodArgument - This member is called for every argument that 
119 // is passed into the method.  Simply print it out
120 //
121 bool AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
122   // Insert commas as we go... the first arg doesn't get a comma
123   if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
124
125   // Output type...
126   Out << Arg->getType();
127   
128   // Output name, if available...
129   if (Arg->hasName())
130     Out << " %" << Arg->getName();
131   else if (Table.getValSlot(Arg) < 0)
132     Out << "<badref>";
133   
134   return false;
135 }
136
137 // processBasicBlock - This member is called for each basic block in a methd.
138 //
139 bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
140   if (BB->hasName()) {              // Print out the label if it exists...
141     Out << "\n" << BB->getName() << ":";
142   } else {
143     int Slot = Table.getValSlot(BB);
144     Out << "\n; <label>:";
145     if (Slot >= 0) 
146       Out << Slot;         // Extra newline seperates out label's
147     else 
148       Out << "<badref>"; 
149   }
150   Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n";  // Output # uses
151
152   ModuleAnalyzer::processBasicBlock(BB);
153   return false;
154 }
155
156 // processInstruction - This member is called for each Instruction in a methd.
157 //
158 bool AssemblyWriter::processInstruction(const Instruction *I) {
159   Out << "\t";
160
161   // Print out name if it exists...
162   if (I && I->hasName())
163     Out << "%" << I->getName() << " = ";
164
165   // Print out the opcode...
166   Out << I->getOpcodeName();
167
168   // Print out the type of the operands...
169   const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
170
171   // Special case conditional branches to swizzle the condition out to the front
172   if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
173     writeOperand(I->getOperand(2), true);
174     Out << ",";
175     writeOperand(Operand, true);
176     Out << ",";
177     writeOperand(I->getOperand(1), true);
178
179   } else if (I->getOpcode() == Instruction::Switch) {
180     // Special case switch statement to get formatting nice and correct...
181     writeOperand(Operand         , true); Out << ",";
182     writeOperand(I->getOperand(1), true); Out << " [";
183
184     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
185       Out << "\n\t\t";
186       writeOperand(I->getOperand(op  ), true); Out << ",";
187       writeOperand(I->getOperand(op+1), true);
188     }
189     Out << "\n\t]";
190   } else if (I->isPHINode()) {
191     Out << " " << Operand->getType();
192
193     Out << " [";  writeOperand(Operand, false); Out << ",";
194     writeOperand(I->getOperand(1), false); Out << " ]";
195     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
196       Out << ", [";  
197       writeOperand(I->getOperand(op  ), false); Out << ",";
198       writeOperand(I->getOperand(op+1), false); Out << " ]";
199     }
200   } else if (I->getOpcode() == Instruction::Ret && !Operand) {
201     Out << " void";
202   } else if (I->getOpcode() == Instruction::Call) {
203     writeOperand(Operand, true);
204     Out << "(";
205     if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
206     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
207       Out << ",";
208       writeOperand(I->getOperand(op), true);
209     }
210
211     Out << " )";
212   } else if (I->getOpcode() == Instruction::Malloc || 
213              I->getOpcode() == Instruction::Alloca) {
214     Out << " " << ((const PointerType*)I->getType())->getValueType();
215     if (I->getNumOperands()) {
216       Out << ",";
217       writeOperand(I->getOperand(0), true);
218     }
219   } else if (I->getOpcode() == Instruction::Cast) {
220     writeOperand(Operand, true);
221     Out << " to " << I->getType();
222   } else if (Operand) {   // Print the normal way...
223
224     // PrintAllTypes - Instructions who have operands of all the same type 
225     // omit the type from all but the first operand.  If the instruction has
226     // different type operands (for example br), then they are all printed.
227     bool PrintAllTypes = false;
228     const Type *TheType = Operand->getType();
229
230     for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
231       Operand = I->getOperand(i);
232       if (Operand->getType() != TheType) {
233         PrintAllTypes = true;       // We have differing types!  Print them all!
234         break;
235       }
236     }
237
238     if (!PrintAllTypes)
239       Out << " " << I->getOperand(0)->getType();
240
241     for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
242       if (i) Out << ",";
243       writeOperand(I->getOperand(i), PrintAllTypes);
244     }
245   }
246
247   // Print a little comment after the instruction indicating which slot it
248   // occupies.
249   //
250   if (I->getType() != Type::VoidTy) {
251     Out << "\t\t; <" << I->getType() << ">";
252
253     if (!I->hasName()) {
254       int Slot = Table.getValSlot(I); // Print out the def slot taken...
255       if (Slot >= 0) Out << ":" << Slot;
256       else Out << ":<badref>";
257     }
258     Out << "\t[#uses=" << I->use_size() << "]";  // Output # uses
259   }
260   Out << endl;
261
262   return false;
263 }
264
265
266 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, 
267                                   bool PrintName) {
268   if (PrintType)
269     Out << " " << Operand->getType();
270   
271   if (Operand->hasName() && PrintName) {
272     Out << " %" << Operand->getName();
273   } else {
274     int Slot = Table.getValSlot(Operand);
275     
276     if (const ConstPoolVal *CPV = Operand->castConstant()) {
277       Out << " " << CPV->getStrValue();
278     } else {
279       if (Slot >= 0)  Out << " %" << Slot;
280       else if (PrintName)
281         Out << "<badref>";     // Not embeded into a location?
282     }
283   }
284 }
285
286
287 //===----------------------------------------------------------------------===//
288 //                       External Interface declarations
289 //===----------------------------------------------------------------------===//
290
291
292
293 void WriteToAssembly(const Module *M, ostream &o) {
294   if (M == 0) { o << "<null> module\n"; return; }
295   SlotCalculator SlotTable(M, true);
296   AssemblyWriter W(o, SlotTable);
297
298   W.write(M);
299 }
300
301 void WriteToAssembly(const Method *M, ostream &o) {
302   if (M == 0) { o << "<null> method\n"; return; }
303   SlotCalculator SlotTable(M->getParent(), true);
304   AssemblyWriter W(o, SlotTable);
305
306   W.write(M);
307 }
308
309
310 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
311   if (BB == 0) { o << "<null> basic block\n"; return; }
312
313   SlotCalculator SlotTable(BB->getParent(), true);
314   AssemblyWriter W(o, SlotTable);
315
316   W.write(BB);
317 }
318
319 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
320   if (CPV == 0) { o << "<null> constant pool value\n"; return; }
321
322   SlotCalculator *SlotTable;
323
324   // A Constant pool value may have a parent that is either a method or a 
325   // module.  Untangle this now...
326   //
327   if (const Method *Meth = CPV->getParentV()->castMethod()) {
328     SlotTable = new SlotCalculator(Meth, true);
329   } else {
330     SlotTable =
331       new SlotCalculator(CPV->getParentV()->castModuleAsserting(), true);
332   }
333
334   AssemblyWriter W(o, *SlotTable);
335   W.write(CPV);
336
337   delete SlotTable;
338 }
339
340 void WriteToAssembly(const Instruction *I, ostream &o) {
341   if (I == 0) { o << "<null> instruction\n"; return; }
342
343   SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0, 
344                            true);
345   AssemblyWriter W(o, SlotTable);
346
347   W.write(I);
348 }