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);
69 if (!CP.getParentV()->castMethodAsserting()->isExternal())
72 Out << "implementation\n";
78 // processConstant - Print out a constant pool entry...
80 bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
83 // Print out name if it exists...
85 Out << "%" << CPV->getName() << " = ";
87 // Print out the opcode...
88 Out << CPV->getType();
90 // Write the value out now...
91 writeOperand(CPV, false, false);
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>";
104 // processMethod - Process all aspects of a method.
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);
113 if (!M->isExternal())
118 // processMethodArgument - This member is called for every argument that
119 // is passed into the method. Simply print it out
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 << ", ";
126 Out << Arg->getType();
128 // Output name, if available...
130 Out << " %" << Arg->getName();
131 else if (Table.getValSlot(Arg) < 0)
137 // processBasicBlock - This member is called for each basic block in a methd.
139 bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
140 if (BB->hasName()) { // Print out the label if it exists...
141 Out << "\n" << BB->getName() << ":";
143 int Slot = Table.getValSlot(BB);
144 Out << "\n; <label>:";
146 Out << Slot; // Extra newline seperates out label's
150 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
152 ModuleAnalyzer::processBasicBlock(BB);
156 // processInstruction - This member is called for each Instruction in a methd.
158 bool AssemblyWriter::processInstruction(const Instruction *I) {
161 // Print out name if it exists...
162 if (I && I->hasName())
163 Out << "%" << I->getName() << " = ";
165 // Print out the opcode...
166 Out << I->getOpcodeName();
168 // Print out the type of the operands...
169 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
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);
175 writeOperand(Operand, true);
177 writeOperand(I->getOperand(1), true);
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 << " [";
184 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
186 writeOperand(I->getOperand(op ), true); Out << ",";
187 writeOperand(I->getOperand(op+1), true);
190 } else if (I->isPHINode()) {
191 Out << " " << Operand->getType();
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) {
197 writeOperand(I->getOperand(op ), false); Out << ",";
198 writeOperand(I->getOperand(op+1), false); Out << " ]";
200 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
202 } else if (I->getOpcode() == Instruction::Call) {
203 writeOperand(Operand, true);
205 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
206 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
208 writeOperand(I->getOperand(op), true);
212 } else if (I->getOpcode() == Instruction::Malloc ||
213 I->getOpcode() == Instruction::Alloca) {
214 Out << " " << ((const PointerType*)I->getType())->getValueType();
215 if (I->getNumOperands()) {
217 writeOperand(I->getOperand(0), true);
219 } else if (I->getOpcode() == Instruction::Cast) {
220 writeOperand(Operand, true);
221 Out << " to " << I->getType();
222 } else if (Operand) { // Print the normal way...
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();
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!
239 Out << " " << I->getOperand(0)->getType();
241 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
243 writeOperand(I->getOperand(i), PrintAllTypes);
247 // Print a little comment after the instruction indicating which slot it
250 if (I->getType() != Type::VoidTy) {
251 Out << "\t\t; <" << I->getType() << ">";
254 int Slot = Table.getValSlot(I); // Print out the def slot taken...
255 if (Slot >= 0) Out << ":" << Slot;
256 else Out << ":<badref>";
258 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
266 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
269 Out << " " << Operand->getType();
271 if (Operand->hasName() && PrintName) {
272 Out << " %" << Operand->getName();
274 int Slot = Table.getValSlot(Operand);
276 if (const ConstPoolVal *CPV = Operand->castConstant()) {
277 Out << " " << CPV->getStrValue();
279 if (Slot >= 0) Out << " %" << Slot;
281 Out << "<badref>"; // Not embeded into a location?
287 //===----------------------------------------------------------------------===//
288 // External Interface declarations
289 //===----------------------------------------------------------------------===//
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);
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);
310 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
311 if (BB == 0) { o << "<null> basic block\n"; return; }
313 SlotCalculator SlotTable(BB->getParent(), true);
314 AssemblyWriter W(o, SlotTable);
319 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
320 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
322 SlotCalculator *SlotTable;
324 // A Constant pool value may have a parent that is either a method or a
325 // module. Untangle this now...
327 if (const Method *Meth = CPV->getParentV()->castMethod()) {
328 SlotTable = new SlotCalculator(Meth, true);
331 new SlotCalculator(CPV->getParentV()->castModuleAsserting(), true);
334 AssemblyWriter W(o, *SlotTable);
340 void WriteToAssembly(const Instruction *I, ostream &o) {
341 if (I == 0) { o << "<null> instruction\n"; return; }
343 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
345 AssemblyWriter W(o, SlotTable);