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) {
27 class AssemblyWriter : public ModuleAnalyzer {
29 SlotCalculator &Table;
31 inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) {
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); }
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);
50 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
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.
58 bool AssemblyWriter::visitMethod(const Method *M) {
62 bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
63 // Done printing arguments...
64 if (isMethod) Out << ")\n";
66 ModuleAnalyzer::processConstPool(CP, isMethod);
71 Out << "implementation\n";
76 // processConstant - Print out a constant pool entry...
78 bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
81 // Print out name if it exists...
83 Out << "%" << CPV->getName() << " = ";
85 // Print out the opcode...
86 Out << CPV->getType();
88 // Write the value out now...
89 writeOperand(CPV, false, false);
91 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
92 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
93 Out << "\t\t; <" << CPV->getType() << ">:";
94 if (Slot >= 0) Out << Slot;
95 else Out << "<badref>";
102 // processMethod - Process all aspects of a method.
104 bool AssemblyWriter::processMethod(const Method *M) {
105 // Print out the return type and name...
106 Out << "\n" << M->getReturnType() << " \"" << M->getName() << "\"(";
107 Table.incorporateMethod(M);
108 ModuleAnalyzer::processMethod(M);
114 // processMethodArgument - This member is called for every argument that
115 // is passed into the method. Simply print it out
117 bool AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
118 // Insert commas as we go... the first arg doesn't get a comma
119 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
122 Out << Arg->getType();
124 // Output name, if available...
126 Out << " %" << Arg->getName();
127 else if (Table.getValSlot(Arg) < 0)
133 // processBasicBlock - This member is called for each basic block in a methd.
135 bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
136 if (BB->hasName()) { // Print out the label if it exists...
137 Out << "\n" << BB->getName() << ":";
139 int Slot = Table.getValSlot(BB);
140 Out << "\n; <label>:";
142 Out << Slot; // Extra newline seperates out label's
146 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
148 ModuleAnalyzer::processBasicBlock(BB);
152 // processInstruction - This member is called for each Instruction in a methd.
154 bool AssemblyWriter::processInstruction(const Instruction *I) {
157 // Print out name if it exists...
158 if (I && I->hasName())
159 Out << "%" << I->getName() << " = ";
161 // Print out the opcode...
162 Out << I->getOpcodeName();
164 // Print out the type of the operands...
165 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
167 // Special case conditional branches to swizzle the condition out to the front
168 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
169 writeOperand(I->getOperand(2), true);
171 writeOperand(Operand, true);
173 writeOperand(I->getOperand(1), true);
175 } else if (I->getOpcode() == Instruction::Switch) {
176 // Special case switch statement to get formatting nice and correct...
177 writeOperand(Operand , true); Out << ",";
178 writeOperand(I->getOperand(1), true); Out << " [";
180 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
182 writeOperand(I->getOperand(op ), true); Out << ",";
183 writeOperand(I->getOperand(op+1), true);
186 } else if (I->isPHINode()) {
187 Out << " " << Operand->getType();
189 Out << " ["; writeOperand(Operand, false); Out << ",";
190 writeOperand(I->getOperand(1), false); Out << " ]";
191 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
193 writeOperand(I->getOperand(op ), false); Out << ",";
194 writeOperand(I->getOperand(op+1), false); Out << " ]";
196 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
198 } else if (I->getOpcode() == Instruction::Call) {
199 writeOperand(Operand, true);
201 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
202 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
204 writeOperand(I->getOperand(op), true);
208 } else if (I->getOpcode() == Instruction::Malloc ||
209 I->getOpcode() == Instruction::Alloca) {
210 Out << " " << ((const PointerType*)I->getType())->getValueType();
211 if (I->getNumOperands()) {
213 writeOperand(I->getOperand(0), true);
215 } else if (I->getOpcode() == Instruction::Cast) {
216 writeOperand(Operand, true);
217 Out << " to " << I->getType();
218 } else if (Operand) { // Print the normal way...
220 // PrintAllTypes - Instructions who have operands of all the same type
221 // omit the type from all but the first operand. If the instruction has
222 // different type operands (for example br), then they are all printed.
223 bool PrintAllTypes = false;
224 const Type *TheType = Operand->getType();
226 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
227 Operand = I->getOperand(i);
228 if (Operand->getType() != TheType) {
229 PrintAllTypes = true; // We have differing types! Print them all!
235 Out << " " << I->getOperand(0)->getType();
237 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
239 writeOperand(I->getOperand(i), PrintAllTypes);
243 // Print a little comment after the instruction indicating which slot it
246 if (I->getType() != Type::VoidTy) {
247 Out << "\t\t; <" << I->getType() << ">";
250 int Slot = Table.getValSlot(I); // Print out the def slot taken...
251 if (Slot >= 0) Out << ":" << Slot;
252 else Out << ":<badref>";
254 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
262 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
265 Out << " " << Operand->getType();
267 if (Operand->hasName() && PrintName) {
268 Out << " %" << Operand->getName();
270 int Slot = Table.getValSlot(Operand);
272 if (const ConstPoolVal *CPV = Operand->castConstant()) {
273 Out << " " << CPV->getStrValue();
275 if (Slot >= 0) Out << " %" << Slot;
277 Out << "<badref>"; // Not embeded into a location?
283 //===----------------------------------------------------------------------===//
284 // External Interface declarations
285 //===----------------------------------------------------------------------===//
289 void WriteToAssembly(const Module *M, ostream &o) {
290 if (M == 0) { o << "<null> module\n"; return; }
291 SlotCalculator SlotTable(M, true);
292 AssemblyWriter W(o, SlotTable);
297 void WriteToAssembly(const Method *M, ostream &o) {
298 if (M == 0) { o << "<null> method\n"; return; }
299 SlotCalculator SlotTable(M->getParent(), true);
300 AssemblyWriter W(o, SlotTable);
306 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
307 if (BB == 0) { o << "<null> basic block\n"; return; }
309 SlotCalculator SlotTable(BB->getParent(), true);
310 AssemblyWriter W(o, SlotTable);
315 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
316 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
318 SlotCalculator *SlotTable;
320 // A Constant pool value may have a parent that is either a method or a
321 // module. Untangle this now...
323 if (CPV->getParent() == 0 || CPV->getParent()->isMethod()) {
324 SlotTable = new SlotCalculator((Method*)CPV->getParent(), true);
327 new SlotCalculator(CPV->getParent()->castModuleAsserting(), true);
330 AssemblyWriter W(o, *SlotTable);
336 void WriteToAssembly(const Instruction *I, ostream &o) {
337 if (I == 0) { o << "<null> instruction\n"; return; }
339 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
341 AssemblyWriter W(o, SlotTable);