1 //===-- Writer.cpp - Library for Printing VM assembly files ------*- C++ -*--=//
3 // This library implements the functionality defined in llvm/Assembly/Writer.h
5 // This library uses the Analysis library to figure out offsets for
6 // variables in the method tables...
8 // TODO: print out the type name instead of the full type if a particular type
9 // is in the symbol table...
11 //===----------------------------------------------------------------------===//
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"
22 void DebugValue(const Value *V) {
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.
30 ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
31 bool PrintName, SlotCalculator *Table) {
33 Out << " " << V->getType();
35 if (V->hasName() && PrintName) {
36 Out << " %" << V->getName();
38 if (const ConstPoolVal *CPV = V->castConstant()) {
39 Out << " " << CPV->getStrValue();
43 Slot = Table->getValSlot(V);
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);
58 return Out << "BAD VALUE TYPE!";
60 Slot = Table->getValSlot(V);
63 if (Slot >= 0) Out << " %" << Slot;
65 Out << "<badref>"; // Not embeded into a location?
73 class AssemblyWriter : public ModuleAnalyzer {
75 SlotCalculator &Table;
77 inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) {
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); }
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);
96 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
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.
104 bool AssemblyWriter::visitMethod(const Method *M) {
108 bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
109 // Done printing arguments...
111 if (CP.getParentV()->castMethodAsserting()->getType()->
112 isMethodType()->isVarArg())
113 Out << ", ..."; // Output varargs portion of signature!
117 ModuleAnalyzer::processConstPool(CP, isMethod);
120 if (!CP.getParentV()->castMethodAsserting()->isExternal())
123 Out << "implementation\n";
129 // processConstant - Print out a constant pool entry...
131 bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
133 return false; // Don't print out unnamed constants, they will be inlined
136 Out << "\t%" << CPV->getName() << " = ";
138 // Print out the constant type...
139 Out << CPV->getType();
141 // Write the value out now...
142 writeOperand(CPV, false, false);
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>";
155 // processMethod - Process all aspects of a method.
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);
164 if (!M->isExternal())
169 // processMethodArgument - This member is called for every argument that
170 // is passed into the method. Simply print it out
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 << ", ";
177 Out << Arg->getType();
179 // Output name, if available...
181 Out << " %" << Arg->getName();
182 else if (Table.getValSlot(Arg) < 0)
188 // processBasicBlock - This member is called for each basic block in a methd.
190 bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
191 if (BB->hasName()) { // Print out the label if it exists...
192 Out << "\n" << BB->getName() << ":";
194 int Slot = Table.getValSlot(BB);
195 Out << "\n; <label>:";
197 Out << Slot; // Extra newline seperates out label's
201 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
203 ModuleAnalyzer::processBasicBlock(BB);
207 // processInstruction - This member is called for each Instruction in a methd.
209 bool AssemblyWriter::processInstruction(const Instruction *I) {
212 // Print out name if it exists...
213 if (I && I->hasName())
214 Out << "%" << I->getName() << " = ";
216 // Print out the opcode...
217 Out << I->getOpcodeName();
219 // Print out the type of the operands...
220 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
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);
226 writeOperand(Operand, true);
228 writeOperand(I->getOperand(1), true);
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 << " [";
235 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
237 writeOperand(I->getOperand(op ), true); Out << ",";
238 writeOperand(I->getOperand(op+1), true);
241 } else if (I->isPHINode()) {
242 Out << " " << Operand->getType();
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) {
248 writeOperand(I->getOperand(op ), false); Out << ",";
249 writeOperand(I->getOperand(op+1), false); Out << " ]";
251 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
253 } else if (I->getOpcode() == Instruction::Call) {
254 writeOperand(Operand, true);
256 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
257 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
259 writeOperand(I->getOperand(op), true);
263 } else if (I->getOpcode() == Instruction::Malloc ||
264 I->getOpcode() == Instruction::Alloca) {
265 Out << " " << ((const PointerType*)I->getType())->getValueType();
266 if (I->getNumOperands()) {
268 writeOperand(I->getOperand(0), true);
270 } else if (I->getOpcode() == Instruction::Cast) {
271 writeOperand(Operand, true);
272 Out << " to " << I->getType();
273 } else if (Operand) { // Print the normal way...
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();
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!
290 Out << " " << I->getOperand(0)->getType();
292 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
294 writeOperand(I->getOperand(i), PrintAllTypes);
298 // Print a little comment after the instruction indicating which slot it
301 if (I->getType() != Type::VoidTy) {
302 Out << "\t\t; <" << I->getType() << ">";
305 int Slot = Table.getValSlot(I); // Print out the def slot taken...
306 if (Slot >= 0) Out << ":" << Slot;
307 else Out << ":<badref>";
309 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
317 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
319 WriteAsOperand(Out, Operand, PrintType, PrintName, &Table);
323 //===----------------------------------------------------------------------===//
324 // External Interface declarations
325 //===----------------------------------------------------------------------===//
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);
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);
346 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
347 if (BB == 0) { o << "<null> basic block\n"; return; }
349 SlotCalculator SlotTable(BB->getParent(), true);
350 AssemblyWriter W(o, SlotTable);
355 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
356 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
358 SlotCalculator *SlotTable;
360 // A Constant pool value may have a parent that is either a method or a
361 // module. Untangle this now...
363 if (const Method *Meth = CPV->getParentV()->castMethod()) {
364 SlotTable = new SlotCalculator(Meth, true);
367 new SlotCalculator(CPV->getParentV()->castModuleAsserting(), true);
370 AssemblyWriter W(o, *SlotTable);
376 void WriteToAssembly(const Instruction *I, ostream &o) {
377 if (I == 0) { o << "<null> instruction\n"; return; }
379 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
381 AssemblyWriter W(o, SlotTable);