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->getOpcode();
159 // Print out the type of the operands...
160 const Value *Operand = I->getOperand(0);
162 // Special case conditional branches to swizzle the condition out to the front
163 if (I->getInstType() == Instruction::Br && I->getOperand(1)) {
164 writeOperand(I->getOperand(2), true);
166 writeOperand(Operand, true);
168 writeOperand(I->getOperand(1), true);
170 } else if (I->getInstType() == 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; (Operand = I->getOperand(op)); op += 2) {
177 writeOperand(Operand, 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; (Operand = I->getOperand(op)); op += 2) {
187 Out << ", ["; writeOperand(Operand, false); Out << ",";
188 writeOperand(I->getOperand(op+1), false); Out << " ]";
190 } else if (I->getInstType() == Instruction::Ret && !Operand) {
192 } else if (I->getInstType() == Instruction::Call) {
193 writeOperand(Operand, true);
195 Operand = I->getOperand(1);
196 if (Operand) writeOperand(Operand, true);
197 for (unsigned op = 2; (Operand = I->getOperand(op)); ++op) {
199 writeOperand(Operand, true);
203 } else if (I->getInstType() == Instruction::Malloc ||
204 I->getInstType() == Instruction::Alloca) {
205 Out << " " << ((const PointerType*)((ConstPoolType*)Operand)
206 ->getValue())->getValueType();
207 if ((Operand = I->getOperand(1))) {
208 Out << ","; writeOperand(Operand, true);
211 } else if (Operand) { // Print the normal way...
213 // PrintAllTypes - Instructions who have operands of all the same type
214 // omit the type from all but the first operand. If the instruction has
215 // different type operands (for example br), then they are all printed.
216 bool PrintAllTypes = false;
217 const Type *TheType = Operand->getType();
220 for (i = 1; (Operand = I->getOperand(i)); i++) {
221 if (Operand->getType() != TheType) {
222 PrintAllTypes = true; // We have differing types! Print them all!
228 Out << " " << I->getOperand(0)->getType();
230 for (unsigned i = 0; (Operand = I->getOperand(i)); i++) {
232 writeOperand(Operand, PrintAllTypes);
236 // Print a little comment after the instruction indicating which slot it
239 if (I->getType() != Type::VoidTy) {
240 Out << "\t\t; <" << I->getType() << ">";
243 int Slot = Table.getValSlot(I); // Print out the def slot taken...
244 if (Slot >= 0) Out << ":" << Slot;
245 else Out << ":<badref>";
247 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
255 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
258 Out << " " << Operand->getType();
260 if (Operand->hasName() && PrintName) {
261 Out << " %" << Operand->getName();
263 int Slot = Table.getValSlot(Operand);
265 if (Operand->isConstant()) {
266 Out << " " << ((ConstPoolVal*)Operand)->getStrValue();
268 if (Slot >= 0) Out << " %" << Slot;
270 Out << "<badref>"; // Not embeded into a location?
276 //===----------------------------------------------------------------------===//
277 // External Interface declarations
278 //===----------------------------------------------------------------------===//
282 void WriteToAssembly(const Module *M, ostream &o) {
283 if (M == 0) { o << "<null> module\n"; return; }
284 SlotCalculator SlotTable(M, true);
285 AssemblyWriter W(o, SlotTable);
290 void WriteToAssembly(const Method *M, ostream &o) {
291 if (M == 0) { o << "<null> method\n"; return; }
292 SlotCalculator SlotTable(M->getParent(), true);
293 AssemblyWriter W(o, SlotTable);
299 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
300 if (BB == 0) { o << "<null> basic block\n"; return; }
302 SlotCalculator SlotTable(BB->getParent(), true);
303 AssemblyWriter W(o, SlotTable);
308 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
309 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
311 SlotCalculator *SlotTable;
313 // A Constant pool value may have a parent that is either a method or a
314 // module. Untangle this now...
316 if (CPV->getParent() == 0 || CPV->getParent()->isMethod()) {
317 SlotTable = new SlotCalculator((Method*)CPV->getParent(), true);
320 new SlotCalculator(CPV->getParent()->castModuleAsserting(), true);
323 AssemblyWriter W(o, *SlotTable);
329 void WriteToAssembly(const Instruction *I, ostream &o) {
330 if (I == 0) { o << "<null> instruction\n"; return; }
332 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
334 AssemblyWriter W(o, SlotTable);