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 class AssemblyWriter : public ModuleAnalyzer {
24 SlotCalculator &Table;
26 inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) {
29 inline void write(const Module *M) { processModule(M); }
30 inline void write(const Method *M) { processMethod(M); }
31 inline void write(const BasicBlock *BB) { processBasicBlock(BB); }
32 inline void write(const Instruction *I) { processInstruction(I); }
33 inline void write(const ConstPoolVal *CPV) { processConstant(CPV); }
36 virtual bool visitMethod(const Method *M);
37 virtual bool processConstPool(const ConstantPool &CP, bool isMethod);
38 virtual bool processConstant(const ConstPoolVal *CPV);
39 virtual bool processMethod(const Method *M);
40 virtual bool processMethodArgument(const MethodArgument *MA);
41 virtual bool processBasicBlock(const BasicBlock *BB);
42 virtual bool processInstruction(const Instruction *I);
45 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
50 // visitMethod - This member is called after the above two steps, visting each
51 // method, because they are effectively values that go into the constant pool.
53 bool AssemblyWriter::visitMethod(const Method *M) {
57 bool AssemblyWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
58 // Done printing arguments...
59 if (isMethod) Out << ")\n";
61 ModuleAnalyzer::processConstPool(CP, isMethod);
66 Out << "implementation\n";
71 // processConstant - Print out a constant pool entry...
73 bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
76 // Print out name if it exists...
78 Out << "%" << CPV->getName() << " = ";
80 // Print out the opcode...
81 Out << CPV->getType();
83 // Write the value out now...
84 writeOperand(CPV, false, false);
86 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
87 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
88 Out << "\t\t; <" << CPV->getType() << ">:";
89 if (Slot >= 0) Out << Slot;
90 else Out << "<badref>";
97 // processMethod - Process all aspects of a method.
99 bool AssemblyWriter::processMethod(const Method *M) {
100 // Print out the return type and name...
101 Out << "\n" << M->getReturnType() << " \"" << M->getName() << "\"(";
102 Table.incorporateMethod(M);
103 ModuleAnalyzer::processMethod(M);
109 // processMethodArgument - This member is called for every argument that
110 // is passed into the method. Simply print it out
112 bool AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
113 // Insert commas as we go... the first arg doesn't get a comma
114 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
117 Out << Arg->getType();
119 // Output name, if available...
121 Out << " %" << Arg->getName();
122 else if (Table.getValSlot(Arg) < 0)
128 // processBasicBlock - This member is called for each basic block in a methd.
130 bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
131 if (BB->hasName()) { // Print out the label if it exists...
132 Out << "\n" << BB->getName() << ":";
134 int Slot = Table.getValSlot(BB);
135 Out << "\n; <label>:";
137 Out << Slot; // Extra newline seperates out label's
141 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
143 ModuleAnalyzer::processBasicBlock(BB);
147 // processInstruction - This member is called for each Instruction in a methd.
149 bool AssemblyWriter::processInstruction(const Instruction *I) {
152 // Print out name if it exists...
153 if (I && I->hasName())
154 Out << "%" << I->getName() << " = ";
156 // Print out the opcode...
157 Out << I->getOpcodeName();
159 // Print out the type of the operands...
160 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
162 // Special case conditional branches to swizzle the condition out to the front
163 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
164 writeOperand(I->getOperand(2), true);
166 writeOperand(Operand, true);
168 writeOperand(I->getOperand(1), true);
170 } else if (I->getOpcode() == Instruction::Switch) {
171 // Special case switch statement to get formatting nice and correct...
172 writeOperand(Operand , true); Out << ",";
173 writeOperand(I->getOperand(1), true); Out << " [";
175 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
177 writeOperand(I->getOperand(op ), true); Out << ",";
178 writeOperand(I->getOperand(op+1), true);
181 } else if (I->isPHINode()) {
182 Out << " " << Operand->getType();
184 Out << " ["; writeOperand(Operand, false); Out << ",";
185 writeOperand(I->getOperand(1), false); Out << " ]";
186 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
188 writeOperand(I->getOperand(op ), false); Out << ",";
189 writeOperand(I->getOperand(op+1), false); Out << " ]";
191 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
193 } else if (I->getOpcode() == Instruction::Call) {
194 writeOperand(Operand, true);
196 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
197 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
199 writeOperand(I->getOperand(op), true);
203 } else if (I->getOpcode() == Instruction::Malloc ||
204 I->getOpcode() == Instruction::Alloca) {
205 Out << " " << ((const PointerType*)I->getType())->getValueType();
206 if (I->getNumOperands()) {
208 writeOperand(I->getOperand(0), true);
210 } else if (I->getOpcode() == Instruction::Cast) {
211 writeOperand(Operand, true);
212 Out << " to " << I->getType();
213 } else if (Operand) { // Print the normal way...
215 // PrintAllTypes - Instructions who have operands of all the same type
216 // omit the type from all but the first operand. If the instruction has
217 // different type operands (for example br), then they are all printed.
218 bool PrintAllTypes = false;
219 const Type *TheType = Operand->getType();
221 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
222 Operand = I->getOperand(i);
223 if (Operand->getType() != TheType) {
224 PrintAllTypes = true; // We have differing types! Print them all!
230 Out << " " << I->getOperand(0)->getType();
232 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
234 writeOperand(I->getOperand(i), PrintAllTypes);
238 // Print a little comment after the instruction indicating which slot it
241 if (I->getType() != Type::VoidTy) {
242 Out << "\t\t; <" << I->getType() << ">";
245 int Slot = Table.getValSlot(I); // Print out the def slot taken...
246 if (Slot >= 0) Out << ":" << Slot;
247 else Out << ":<badref>";
249 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
257 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
260 Out << " " << Operand->getType();
262 if (Operand->hasName() && PrintName) {
263 Out << " %" << Operand->getName();
265 int Slot = Table.getValSlot(Operand);
267 if (const ConstPoolVal *CPV = Operand->castConstant()) {
268 Out << " " << CPV->getStrValue();
270 if (Slot >= 0) Out << " %" << Slot;
272 Out << "<badref>"; // Not embeded into a location?
278 //===----------------------------------------------------------------------===//
279 // External Interface declarations
280 //===----------------------------------------------------------------------===//
284 void WriteToAssembly(const Module *M, ostream &o) {
285 if (M == 0) { o << "<null> module\n"; return; }
286 SlotCalculator SlotTable(M, true);
287 AssemblyWriter W(o, SlotTable);
292 void WriteToAssembly(const Method *M, ostream &o) {
293 if (M == 0) { o << "<null> method\n"; return; }
294 SlotCalculator SlotTable(M->getParent(), true);
295 AssemblyWriter W(o, SlotTable);
301 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
302 if (BB == 0) { o << "<null> basic block\n"; return; }
304 SlotCalculator SlotTable(BB->getParent(), true);
305 AssemblyWriter W(o, SlotTable);
310 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
311 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
313 SlotCalculator *SlotTable;
315 // A Constant pool value may have a parent that is either a method or a
316 // module. Untangle this now...
318 if (CPV->getParent() == 0 || CPV->getParent()->isMethod()) {
319 SlotTable = new SlotCalculator((Method*)CPV->getParent(), true);
322 new SlotCalculator(CPV->getParent()->castModuleAsserting(), true);
325 AssemblyWriter W(o, *SlotTable);
331 void WriteToAssembly(const Instruction *I, ostream &o) {
332 if (I == 0) { o << "<null> instruction\n"; return; }
334 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
336 AssemblyWriter W(o, SlotTable);