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...
110 if (isMethod) Out << ")\n";
112 ModuleAnalyzer::processConstPool(CP, isMethod);
115 if (!CP.getParentV()->castMethodAsserting()->isExternal())
118 Out << "implementation\n";
124 // processConstant - Print out a constant pool entry...
126 bool AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
129 // Print out name if it exists...
131 Out << "%" << CPV->getName() << " = ";
133 // Print out the opcode...
134 Out << CPV->getType();
136 // Write the value out now...
137 writeOperand(CPV, false, false);
139 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
140 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
141 Out << "\t\t; <" << CPV->getType() << ">:";
142 if (Slot >= 0) Out << Slot;
143 else Out << "<badref>";
150 // processMethod - Process all aspects of a method.
152 bool AssemblyWriter::processMethod(const Method *M) {
153 // Print out the return type and name...
154 Out << "\n" << (M->isExternal() ? "declare " : "")
155 << M->getReturnType() << " \"" << M->getName() << "\"(";
156 Table.incorporateMethod(M);
157 ModuleAnalyzer::processMethod(M);
159 if (!M->isExternal())
164 // processMethodArgument - This member is called for every argument that
165 // is passed into the method. Simply print it out
167 bool AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
168 // Insert commas as we go... the first arg doesn't get a comma
169 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
172 Out << Arg->getType();
174 // Output name, if available...
176 Out << " %" << Arg->getName();
177 else if (Table.getValSlot(Arg) < 0)
183 // processBasicBlock - This member is called for each basic block in a methd.
185 bool AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
186 if (BB->hasName()) { // Print out the label if it exists...
187 Out << "\n" << BB->getName() << ":";
189 int Slot = Table.getValSlot(BB);
190 Out << "\n; <label>:";
192 Out << Slot; // Extra newline seperates out label's
196 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
198 ModuleAnalyzer::processBasicBlock(BB);
202 // processInstruction - This member is called for each Instruction in a methd.
204 bool AssemblyWriter::processInstruction(const Instruction *I) {
207 // Print out name if it exists...
208 if (I && I->hasName())
209 Out << "%" << I->getName() << " = ";
211 // Print out the opcode...
212 Out << I->getOpcodeName();
214 // Print out the type of the operands...
215 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
217 // Special case conditional branches to swizzle the condition out to the front
218 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
219 writeOperand(I->getOperand(2), true);
221 writeOperand(Operand, true);
223 writeOperand(I->getOperand(1), true);
225 } else if (I->getOpcode() == Instruction::Switch) {
226 // Special case switch statement to get formatting nice and correct...
227 writeOperand(Operand , true); Out << ",";
228 writeOperand(I->getOperand(1), true); Out << " [";
230 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
232 writeOperand(I->getOperand(op ), true); Out << ",";
233 writeOperand(I->getOperand(op+1), true);
236 } else if (I->isPHINode()) {
237 Out << " " << Operand->getType();
239 Out << " ["; writeOperand(Operand, false); Out << ",";
240 writeOperand(I->getOperand(1), false); Out << " ]";
241 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
243 writeOperand(I->getOperand(op ), false); Out << ",";
244 writeOperand(I->getOperand(op+1), false); Out << " ]";
246 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
248 } else if (I->getOpcode() == Instruction::Call) {
249 writeOperand(Operand, true);
251 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
252 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
254 writeOperand(I->getOperand(op), true);
258 } else if (I->getOpcode() == Instruction::Malloc ||
259 I->getOpcode() == Instruction::Alloca) {
260 Out << " " << ((const PointerType*)I->getType())->getValueType();
261 if (I->getNumOperands()) {
263 writeOperand(I->getOperand(0), true);
265 } else if (I->getOpcode() == Instruction::Cast) {
266 writeOperand(Operand, true);
267 Out << " to " << I->getType();
268 } else if (Operand) { // Print the normal way...
270 // PrintAllTypes - Instructions who have operands of all the same type
271 // omit the type from all but the first operand. If the instruction has
272 // different type operands (for example br), then they are all printed.
273 bool PrintAllTypes = false;
274 const Type *TheType = Operand->getType();
276 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
277 Operand = I->getOperand(i);
278 if (Operand->getType() != TheType) {
279 PrintAllTypes = true; // We have differing types! Print them all!
285 Out << " " << I->getOperand(0)->getType();
287 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
289 writeOperand(I->getOperand(i), PrintAllTypes);
293 // Print a little comment after the instruction indicating which slot it
296 if (I->getType() != Type::VoidTy) {
297 Out << "\t\t; <" << I->getType() << ">";
300 int Slot = Table.getValSlot(I); // Print out the def slot taken...
301 if (Slot >= 0) Out << ":" << Slot;
302 else Out << ":<badref>";
304 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
312 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
314 WriteAsOperand(Out, Operand, PrintType, PrintName, &Table);
318 //===----------------------------------------------------------------------===//
319 // External Interface declarations
320 //===----------------------------------------------------------------------===//
324 void WriteToAssembly(const Module *M, ostream &o) {
325 if (M == 0) { o << "<null> module\n"; return; }
326 SlotCalculator SlotTable(M, true);
327 AssemblyWriter W(o, SlotTable);
332 void WriteToAssembly(const Method *M, ostream &o) {
333 if (M == 0) { o << "<null> method\n"; return; }
334 SlotCalculator SlotTable(M->getParent(), true);
335 AssemblyWriter W(o, SlotTable);
341 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
342 if (BB == 0) { o << "<null> basic block\n"; return; }
344 SlotCalculator SlotTable(BB->getParent(), true);
345 AssemblyWriter W(o, SlotTable);
350 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
351 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
353 SlotCalculator *SlotTable;
355 // A Constant pool value may have a parent that is either a method or a
356 // module. Untangle this now...
358 if (const Method *Meth = CPV->getParentV()->castMethod()) {
359 SlotTable = new SlotCalculator(Meth, true);
362 new SlotCalculator(CPV->getParentV()->castModuleAsserting(), true);
365 AssemblyWriter W(o, *SlotTable);
371 void WriteToAssembly(const Instruction *I, ostream &o) {
372 if (I == 0) { o << "<null> instruction\n"; return; }
374 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
376 AssemblyWriter W(o, SlotTable);