Don't write out constants that do not have a name, they will be inlined.
[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 // 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 //
30 ostream &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
72
73 class AssemblyWriter : public ModuleAnalyzer {
74   ostream &Out;
75   SlotCalculator &Table;
76 public:
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
86 protected:
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
95 private :
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 //
104 bool AssemblyWriter::visitMethod(const Method *M) {
105   return false;
106 }
107
108 bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
109   // Done printing arguments...
110   if (isMethod) {
111     if (CP.getParentV()->castMethodAsserting()->getType()->
112         isMethodType()->isVarArg())
113       Out << ", ...";  // Output varargs portion of signature!
114     Out << ")\n";
115   }
116
117   ModuleAnalyzer::processConstPool(CP, isMethod);
118   
119   if (isMethod) {
120     if (!CP.getParentV()->castMethodAsserting()->isExternal())
121       Out << "begin";
122   } else {
123     Out << "implementation\n";
124   }
125   return false;
126 }
127
128
129 // processConstant - Print out a constant pool entry...
130 //
131 bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
132   if (!CPV->hasName())
133     return false;    // Don't print out unnamed constants, they will be inlined
134
135   // Print out name...
136   Out << "\t%" << CPV->getName() << " = ";
137
138   // Print out the constant type...
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 //
157 bool AssemblyWriter::processMethod(const Method *M) {
158   // Print out the return type and name...
159   Out << "\n" << (M->isExternal() ? "declare " : "") 
160       << M->getReturnType() << " \"" << M->getName() << "\"(";
161   Table.incorporateMethod(M);
162   ModuleAnalyzer::processMethod(M);
163   Table.purgeMethod();
164   if (!M->isExternal())
165     Out << "end\n";
166   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 //
172 bool 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 //
190 bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
191   if (BB->hasName()) {              // Print out the label if it exists...
192     Out << "\n" << BB->getName() << ":";
193   } else {
194     int Slot = Table.getValSlot(BB);
195     Out << "\n; <label>:";
196     if (Slot >= 0) 
197       Out << Slot;         // Extra newline seperates out label's
198     else 
199       Out << "<badref>"; 
200   }
201   Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n";  // Output # uses
202
203   ModuleAnalyzer::processBasicBlock(BB);
204   return false;
205 }
206
207 // processInstruction - This member is called for each Instruction in a methd.
208 //
209 bool 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...
217   Out << I->getOpcodeName();
218
219   // Print out the type of the operands...
220   const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
221
222   // Special case conditional branches to swizzle the condition out to the front
223   if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
224     writeOperand(I->getOperand(2), true);
225     Out << ",";
226     writeOperand(Operand, true);
227     Out << ",";
228     writeOperand(I->getOperand(1), true);
229
230   } else if (I->getOpcode() == Instruction::Switch) {
231     // Special case switch statement to get formatting nice and correct...
232     writeOperand(Operand         , true); Out << ",";
233     writeOperand(I->getOperand(1), true); Out << " [";
234
235     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
236       Out << "\n\t\t";
237       writeOperand(I->getOperand(op  ), true); Out << ",";
238       writeOperand(I->getOperand(op+1), true);
239     }
240     Out << "\n\t]";
241   } else if (I->isPHINode()) {
242     Out << " " << Operand->getType();
243
244     Out << " [";  writeOperand(Operand, false); Out << ",";
245     writeOperand(I->getOperand(1), false); Out << " ]";
246     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
247       Out << ", [";  
248       writeOperand(I->getOperand(op  ), false); Out << ",";
249       writeOperand(I->getOperand(op+1), false); Out << " ]";
250     }
251   } else if (I->getOpcode() == Instruction::Ret && !Operand) {
252     Out << " void";
253   } else if (I->getOpcode() == Instruction::Call) {
254     writeOperand(Operand, true);
255     Out << "(";
256     if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
257     for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
258       Out << ",";
259       writeOperand(I->getOperand(op), true);
260     }
261
262     Out << " )";
263   } else if (I->getOpcode() == Instruction::Malloc || 
264              I->getOpcode() == Instruction::Alloca) {
265     Out << " " << ((const PointerType*)I->getType())->getValueType();
266     if (I->getNumOperands()) {
267       Out << ",";
268       writeOperand(I->getOperand(0), true);
269     }
270   } else if (I->getOpcode() == Instruction::Cast) {
271     writeOperand(Operand, true);
272     Out << " to " << I->getType();
273   } 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();
280
281     for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
282       Operand = I->getOperand(i);
283       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
292     for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
293       if (i) Out << ",";
294       writeOperand(I->getOperand(i), PrintAllTypes);
295     }
296   }
297
298   // Print a little comment after the instruction indicating which slot it
299   // occupies.
300   //
301   if (I->getType() != Type::VoidTy) {
302     Out << "\t\t; <" << I->getType() << ">";
303
304     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     }
309     Out << "\t[#uses=" << I->use_size() << "]";  // Output # uses
310   }
311   Out << endl;
312
313   return false;
314 }
315
316
317 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType, 
318                                   bool PrintName) {
319   WriteAsOperand(Out, Operand, PrintType, PrintName, &Table);
320 }
321
322
323 //===----------------------------------------------------------------------===//
324 //                       External Interface declarations
325 //===----------------------------------------------------------------------===//
326
327
328
329 void 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
337 void 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
346 void 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
355 void 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   //
363   if (const Method *Meth = CPV->getParentV()->castMethod()) {
364     SlotTable = new SlotCalculator(Meth, true);
365   } else {
366     SlotTable =
367       new SlotCalculator(CPV->getParentV()->castModuleAsserting(), true);
368   }
369
370   AssemblyWriter W(o, *SlotTable);
371   W.write(CPV);
372
373   delete SlotTable;
374 }
375
376 void 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 }