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"
21 #include "llvm/Support/STLExtras.h"
22 #include "llvm/SymbolTable.h"
25 void DebugValue(const Value *V) {
29 // WriteAsOperand - Write the name of the specified value out to the specified
30 // ostream. This can be useful when you just want to print int %reg126, not the
31 // whole instruction that generated it.
33 ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
34 bool PrintName, SlotCalculator *Table) {
36 Out << " " << V->getType();
38 if (PrintName && V->hasName()) {
39 Out << " %" << V->getName();
41 if (const ConstPoolVal *CPV = V->castConstant()) {
42 Out << " " << CPV->getStrValue();
46 Slot = Table->getValSlot(V);
48 if (const Type *Ty = V->castType()) {
49 return Out << " " << Ty;
50 } else if (const MethodArgument *MA = V->castMethodArgument()) {
51 Table = new SlotCalculator(MA->getParent(), true);
52 } else if (const Instruction *I = V->castInstruction()) {
53 Table = new SlotCalculator(I->getParent()->getParent(), true);
54 } else if (const BasicBlock *BB = V->castBasicBlock()) {
55 Table = new SlotCalculator(BB->getParent(), true);
56 } else if (const Method *Meth = V->castMethod()) {
57 Table = new SlotCalculator(Meth, true);
58 } else if (const Module *Mod = V->castModule()) {
59 Table = new SlotCalculator(Mod, true);
61 return Out << "BAD VALUE TYPE!";
63 Slot = Table->getValSlot(V);
66 if (Slot >= 0) Out << " %" << Slot;
68 Out << "<badref>"; // Not embeded into a location?
76 class AssemblyWriter {
78 SlotCalculator &Table;
80 inline AssemblyWriter(ostream &o, SlotCalculator &Tab) : Out(o), Table(Tab) {
83 inline void write(const Module *M) { processModule(M); }
84 inline void write(const Method *M) { processMethod(M); }
85 inline void write(const BasicBlock *BB) { processBasicBlock(BB); }
86 inline void write(const Instruction *I) { processInstruction(I); }
87 inline void write(const ConstPoolVal *CPV) { processConstant(CPV); }
90 void processModule(const Module *M);
91 void processSymbolTable(const SymbolTable &ST);
92 void processConstant(const ConstPoolVal *CPV);
93 void processMethod(const Method *M);
94 void processMethodArgument(const MethodArgument *MA);
95 void processBasicBlock(const BasicBlock *BB);
96 void processInstruction(const Instruction *I);
98 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
102 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
104 WriteAsOperand(Out, Operand, PrintType, PrintName, &Table);
108 void AssemblyWriter::processModule(const Module *M) {
109 // Loop over the symbol table, emitting all named constants...
110 if (M->hasSymbolTable())
111 processSymbolTable(*M->getSymbolTable());
113 Out << "implementation\n";
115 // Output all of the methods...
116 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::processMethod));
120 // processSymbolTable - Run through symbol table looking for named constants
121 // if a named constant is found, emit it's declaration...
123 void AssemblyWriter::processSymbolTable(const SymbolTable &ST) {
124 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
125 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
126 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
128 for (; I != End; ++I) {
129 const Value *V = I->second;
130 if (const ConstPoolVal *CPV = V->castConstant()) {
131 processConstant(CPV);
132 } else if (const Type *Ty = V->castType()) {
133 Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
140 // processConstant - Print out a constant pool entry...
142 void AssemblyWriter::processConstant(const ConstPoolVal *CPV) {
143 // Don't print out unnamed constants, they will be inlined
144 if (!CPV->hasName()) return;
147 Out << "\t%" << CPV->getName() << " = ";
149 // Print out the constant type...
150 Out << CPV->getType();
152 // Write the value out now...
153 writeOperand(CPV, false, false);
155 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
156 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
157 Out << "\t\t; <" << CPV->getType() << ">:";
158 if (Slot >= 0) Out << Slot;
159 else Out << "<badref>";
165 // processMethod - Process all aspects of a method.
167 void AssemblyWriter::processMethod(const Method *M) {
168 // Print out the return type and name...
169 Out << "\n" << (M->isExternal() ? "declare " : "")
170 << M->getReturnType() << " \"" << M->getName() << "\"(";
171 Table.incorporateMethod(M);
173 // Loop over the arguments, processing them...
174 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
175 bind_obj(this, &AssemblyWriter::processMethodArgument));
178 // Finish printing arguments...
179 const MethodType *MT = (const MethodType*)M->getType();
180 if (MT->isVarArg()) {
181 if (MT->getParamTypes().size()) Out << ", ";
182 Out << "..."; // Output varargs portion of signature!
186 if (!M->isExternal()) {
187 // Loop over the symbol table, emitting all named constants...
188 if (M->hasSymbolTable())
189 processSymbolTable(*M->getSymbolTable());
193 // Output all of its basic blocks... for the method
194 for_each(M->begin(), M->end(),
195 bind_obj(this, &AssemblyWriter::processBasicBlock));
203 // processMethodArgument - This member is called for every argument that
204 // is passed into the method. Simply print it out
206 void AssemblyWriter::processMethodArgument(const MethodArgument *Arg) {
207 // Insert commas as we go... the first arg doesn't get a comma
208 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
211 Out << Arg->getType();
213 // Output name, if available...
215 Out << " %" << Arg->getName();
216 else if (Table.getValSlot(Arg) < 0)
220 // processBasicBlock - This member is called for each basic block in a methd.
222 void AssemblyWriter::processBasicBlock(const BasicBlock *BB) {
223 if (BB->hasName()) { // Print out the label if it exists...
224 Out << "\n" << BB->getName() << ":";
226 int Slot = Table.getValSlot(BB);
227 Out << "\n; <label>:";
229 Out << Slot; // Extra newline seperates out label's
233 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
235 // Output all of the instructions in the basic block...
236 for_each(BB->begin(), BB->end(),
237 bind_obj(this, &AssemblyWriter::processInstruction));
240 // processInstruction - This member is called for each Instruction in a methd.
242 void AssemblyWriter::processInstruction(const Instruction *I) {
245 // Print out name if it exists...
246 if (I && I->hasName())
247 Out << "%" << I->getName() << " = ";
249 // Print out the opcode...
250 Out << I->getOpcodeName();
252 // Print out the type of the operands...
253 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
255 // Special case conditional branches to swizzle the condition out to the front
256 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
257 writeOperand(I->getOperand(2), true);
259 writeOperand(Operand, true);
261 writeOperand(I->getOperand(1), true);
263 } else if (I->getOpcode() == Instruction::Switch) {
264 // Special case switch statement to get formatting nice and correct...
265 writeOperand(Operand , true); Out << ",";
266 writeOperand(I->getOperand(1), true); Out << " [";
268 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
270 writeOperand(I->getOperand(op ), true); Out << ",";
271 writeOperand(I->getOperand(op+1), true);
274 } else if (I->isPHINode()) {
275 Out << " " << Operand->getType();
277 Out << " ["; writeOperand(Operand, false); Out << ",";
278 writeOperand(I->getOperand(1), false); Out << " ]";
279 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
281 writeOperand(I->getOperand(op ), false); Out << ",";
282 writeOperand(I->getOperand(op+1), false); Out << " ]";
284 } else if (I->getOpcode() == Instruction::Ret && !Operand) {
286 } else if (I->getOpcode() == Instruction::Call) {
287 writeOperand(Operand, true);
289 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
290 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
292 writeOperand(I->getOperand(op), true);
296 } else if (I->getOpcode() == Instruction::Malloc ||
297 I->getOpcode() == Instruction::Alloca) {
298 Out << " " << ((const PointerType*)I->getType())->getValueType();
299 if (I->getNumOperands()) {
301 writeOperand(I->getOperand(0), true);
303 } else if (I->getOpcode() == Instruction::Cast) {
304 writeOperand(Operand, true);
305 Out << " to " << I->getType();
306 } else if (Operand) { // Print the normal way...
308 // PrintAllTypes - Instructions who have operands of all the same type
309 // omit the type from all but the first operand. If the instruction has
310 // different type operands (for example br), then they are all printed.
311 bool PrintAllTypes = false;
312 const Type *TheType = Operand->getType();
314 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
315 Operand = I->getOperand(i);
316 if (Operand->getType() != TheType) {
317 PrintAllTypes = true; // We have differing types! Print them all!
323 Out << " " << I->getOperand(0)->getType();
325 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
327 writeOperand(I->getOperand(i), PrintAllTypes);
331 // Print a little comment after the instruction indicating which slot it
334 if (I->getType() != Type::VoidTy) {
335 Out << "\t\t; <" << I->getType() << ">";
338 int Slot = Table.getValSlot(I); // Print out the def slot taken...
339 if (Slot >= 0) Out << ":" << Slot;
340 else Out << ":<badref>";
342 Out << "\t[#uses=" << I->use_size() << "]"; // Output # uses
348 //===----------------------------------------------------------------------===//
349 // External Interface declarations
350 //===----------------------------------------------------------------------===//
354 void WriteToAssembly(const Module *M, ostream &o) {
355 if (M == 0) { o << "<null> module\n"; return; }
356 SlotCalculator SlotTable(M, true);
357 AssemblyWriter W(o, SlotTable);
362 void WriteToAssembly(const Method *M, ostream &o) {
363 if (M == 0) { o << "<null> method\n"; return; }
364 SlotCalculator SlotTable(M->getParent(), true);
365 AssemblyWriter W(o, SlotTable);
371 void WriteToAssembly(const BasicBlock *BB, ostream &o) {
372 if (BB == 0) { o << "<null> basic block\n"; return; }
374 SlotCalculator SlotTable(BB->getParent(), true);
375 AssemblyWriter W(o, SlotTable);
380 void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
381 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
382 WriteAsOperand(o, CPV, true, true, 0);
385 void WriteToAssembly(const Instruction *I, ostream &o) {
386 if (I == 0) { o << "<null> instruction\n"; return; }
388 SlotCalculator SlotTable(I->getParent() ? I->getParent()->getParent() : 0,
390 AssemblyWriter W(o, SlotTable);