blob: 7b0bd7a7063dd43a07fd6b5ee013345f7b771582 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Writer.cpp - Library for Printing VM assembly files ------*- C++ -*--=//
2//
3// This library implements the functionality defined in llvm/Assembly/Writer.h
4//
5// This library uses the Analysis library to figure out offsets for
6// variables in the method tables...
7//
8// TODO: print out the type name instead of the full type if a particular type
9// is in the symbol table...
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Assembly/Writer.h"
14#include "llvm/Analysis/SlotCalculator.h"
15#include "llvm/Module.h"
16#include "llvm/Method.h"
Chris Lattnerda975502001-09-10 07:58:01 +000017#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/BasicBlock.h"
19#include "llvm/ConstPoolVals.h"
20#include "llvm/iOther.h"
21#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000022#include "llvm/iTerminators.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000023#include "llvm/SymbolTable.h"
Chris Lattner7bfee412001-10-29 16:05:51 +000024#include "llvm/Support/STLExtras.h"
25#include "llvm/Support/StringExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000026#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000027#include <map>
28
29static SlotCalculator *createSlotCalculator(const Value *V) {
30 assert(!isa<Type>(V) && "Can't create an SC for a type!");
31 if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V)){
32 return new SlotCalculator(MA->getParent(), true);
33 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
34 return new SlotCalculator(I->getParent()->getParent(), true);
35 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
36 return new SlotCalculator(BB->getParent(), true);
37 } else if (const GlobalVariable *GV =dyn_cast<const GlobalVariable>(V)){
38 return new SlotCalculator(GV->getParent(), true);
39 } else if (const Method *Meth = dyn_cast<const Method>(V)) {
40 return new SlotCalculator(Meth, true);
41 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
42 return new SlotCalculator(Mod, true);
43 }
44 return 0;
45}
Chris Lattner2f7c9632001-06-06 20:29:01 +000046
Chris Lattner5e5abe32001-07-20 19:15:21 +000047// WriteAsOperand - Write the name of the specified value out to the specified
48// ostream. This can be useful when you just want to print int %reg126, not the
49// whole instruction that generated it.
50//
51ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
52 bool PrintName, SlotCalculator *Table) {
53 if (PrintType)
Chris Lattner7bfee412001-10-29 16:05:51 +000054 Out << " " << V->getType()->getDescription();
Chris Lattner5e5abe32001-07-20 19:15:21 +000055
Chris Lattnerfee714f2001-09-07 16:36:04 +000056 if (PrintName && V->hasName()) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000057 Out << " %" << V->getName();
58 } else {
Chris Lattner4b717c02001-10-01 16:18:37 +000059 if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000060 Out << " " << CPV->getStrValue();
61 } else {
62 int Slot;
63 if (Table) {
64 Slot = Table->getValSlot(V);
65 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +000066 if (const Type *Ty = dyn_cast<const Type>(V))
67 return Out << " " << Ty;
68
69 Table = createSlotCalculator(V);
70 if (Table == 0) return Out << "BAD VALUE TYPE!";
71
Chris Lattner5e5abe32001-07-20 19:15:21 +000072 Slot = Table->getValSlot(V);
73 delete Table;
74 }
75 if (Slot >= 0) Out << " %" << Slot;
76 else if (PrintName)
77 Out << "<badref>"; // Not embeded into a location?
78 }
79 }
80 return Out;
81}
82
83
Chris Lattner2e9fee42001-07-12 23:35:26 +000084
Chris Lattnerfee714f2001-09-07 16:36:04 +000085class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +000086 ostream &Out;
87 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +000088 const Module *TheModule;
89 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +000090public:
Chris Lattner7bfee412001-10-29 16:05:51 +000091 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
92 : Out(o), Table(Tab), TheModule(M) {
93
94 // If the module has a symbol table, take all global types and stuff their
95 // names into the TypeNames map.
96 //
97 if (M && M->hasSymbolTable()) {
98 const SymbolTable *ST = M->getSymbolTable();
99 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
100 if (PI != ST->end()) {
101 SymbolTable::type_const_iterator I = PI->second.begin();
102 for (; I != PI->second.end(); ++I) {
103 // As a heuristic, don't insert pointer to primitive types, because
104 // they are used too often to have a single useful name.
105 //
106 const Type *Ty = cast<const Type>(I->second);
107 if (!isa<PointerType>(Ty) ||
108 !cast<PointerType>(Ty)->getValueType()->isPrimitiveType())
109 TypeNames.insert(make_pair(Ty, "%"+I->first));
110 }
111 }
112 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000113 }
114
Chris Lattner7bfee412001-10-29 16:05:51 +0000115 inline void write(const Module *M) { printModule(M); }
116 inline void write(const GlobalVariable *G) { printGlobal(G); }
117 inline void write(const Method *M) { printMethod(M); }
118 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
119 inline void write(const Instruction *I) { printInstruction(I); }
120 inline void write(const ConstPoolVal *CPV) { printConstant(CPV); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121
Chris Lattner2f7c9632001-06-06 20:29:01 +0000122private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000123 void printModule(const Module *M);
124 void printSymbolTable(const SymbolTable &ST);
125 void printConstant(const ConstPoolVal *CPV);
126 void printGlobal(const GlobalVariable *GV);
127 void printMethod(const Method *M);
128 void printMethodArgument(const MethodArgument *MA);
129 void printBasicBlock(const BasicBlock *BB);
130 void printInstruction(const Instruction *I);
131 ostream &printType(const Type *Ty);
132
Chris Lattner2f7c9632001-06-06 20:29:01 +0000133 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
Chris Lattner862e3382001-10-13 06:42:36 +0000134
135 // printInfoComment - Print a little comment after the instruction indicating
136 // which slot it occupies.
137 void printInfoComment(const Value *V);
138
Chris Lattner7bfee412001-10-29 16:05:51 +0000139
140 string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000141};
142
143
Chris Lattnerfee714f2001-09-07 16:36:04 +0000144void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
145 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000146 if (PrintType) { Out << " "; printType(Operand->getType()); }
147 WriteAsOperand(Out, Operand, false, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000148}
149
Chris Lattner2f7c9632001-06-06 20:29:01 +0000150
Chris Lattner7bfee412001-10-29 16:05:51 +0000151void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000152 // Loop over the symbol table, emitting all named constants...
153 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000154 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000155
156 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000157 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000158
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000159 Out << "implementation\n";
160
Chris Lattnerfee714f2001-09-07 16:36:04 +0000161 // Output all of the methods...
Chris Lattner7bfee412001-10-29 16:05:51 +0000162 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printMethod));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000163}
164
Chris Lattner7bfee412001-10-29 16:05:51 +0000165void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000166 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000167
168 if (!GV->hasInitializer()) Out << "uninitialized ";
169
Chris Lattner7bfee412001-10-29 16:05:51 +0000170 Out << (GV->isConstant() ? "constant " : "global ");
171 printType(GV->getType()->getValueType());
Chris Lattner37798642001-09-18 04:01:05 +0000172
173 if (GV->hasInitializer())
174 writeOperand(GV->getInitializer(), false, false);
175
Chris Lattner862e3382001-10-13 06:42:36 +0000176 printInfoComment(GV);
Chris Lattner37798642001-09-18 04:01:05 +0000177 Out << endl;
Chris Lattnerda975502001-09-10 07:58:01 +0000178}
179
Chris Lattnerfee714f2001-09-07 16:36:04 +0000180
Chris Lattner7bfee412001-10-29 16:05:51 +0000181// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000182// if a named constant is found, emit it's declaration...
183//
Chris Lattner7bfee412001-10-29 16:05:51 +0000184void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000185 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
186 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
187 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
188
189 for (; I != End; ++I) {
190 const Value *V = I->second;
Chris Lattner38569342001-10-01 20:11:19 +0000191 if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000192 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000193 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000194 Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
195 }
196 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000197 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000198}
199
200
Chris Lattner7bfee412001-10-29 16:05:51 +0000201// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000202//
Chris Lattner7bfee412001-10-29 16:05:51 +0000203void AssemblyWriter::printConstant(const ConstPoolVal *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000204 // Don't print out unnamed constants, they will be inlined
205 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000206
Chris Lattneree998be2001-07-26 16:29:38 +0000207 // Print out name...
208 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000209
Chris Lattneree998be2001-07-26 16:29:38 +0000210 // Print out the constant type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000211 printType(CPV->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000212
213 // Write the value out now...
214 writeOperand(CPV, false, false);
215
216 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
217 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
Chris Lattner7bfee412001-10-29 16:05:51 +0000218 Out << "\t\t; <";
219 printType(CPV->getType()) << ">:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000220 if (Slot >= 0) Out << Slot;
221 else Out << "<badref>";
222 }
223
224 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000225}
226
Chris Lattner7bfee412001-10-29 16:05:51 +0000227// printMethod - Print all aspects of a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000228//
Chris Lattner7bfee412001-10-29 16:05:51 +0000229void AssemblyWriter::printMethod(const Method *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000230 // Print out the return type and name...
Chris Lattner7bfee412001-10-29 16:05:51 +0000231 Out << "\n" << (M->isExternal() ? "declare " : "");
232 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000233 Table.incorporateMethod(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000234
Chris Lattner7bfee412001-10-29 16:05:51 +0000235 // Loop over the arguments, printing them...
Chris Lattner862e3382001-10-13 06:42:36 +0000236 const MethodType *MT = cast<const MethodType>(M->getMethodType());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000237
Chris Lattner862e3382001-10-13 06:42:36 +0000238 if (!M->isExternal()) {
239 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000240 bind_obj(this, &AssemblyWriter::printMethodArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000241 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000242 // Loop over the arguments, printing them...
Chris Lattner862e3382001-10-13 06:42:36 +0000243 const MethodType *MT = cast<const MethodType>(M->getMethodType());
244 for (MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
245 E = MT->getParamTypes().end(); I != E; ++I) {
246 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000247 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000248 }
249 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000250
251 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000252 if (MT->isVarArg()) {
253 if (MT->getParamTypes().size()) Out << ", ";
254 Out << "..."; // Output varargs portion of signature!
255 }
256 Out << ")\n";
257
258 if (!M->isExternal()) {
259 // Loop over the symbol table, emitting all named constants...
260 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000261 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000262
263 Out << "begin";
264
265 // Output all of its basic blocks... for the method
266 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000267 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000268
Chris Lattnera7620d92001-07-15 06:35:59 +0000269 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000270 }
271
272 Table.purgeMethod();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273}
274
Chris Lattner7bfee412001-10-29 16:05:51 +0000275// printMethodArgument - This member is called for every argument that
Chris Lattner2f7c9632001-06-06 20:29:01 +0000276// is passed into the method. Simply print it out
277//
Chris Lattner7bfee412001-10-29 16:05:51 +0000278void AssemblyWriter::printMethodArgument(const MethodArgument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000279 // Insert commas as we go... the first arg doesn't get a comma
280 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
281
282 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000283 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000284
285 // Output name, if available...
286 if (Arg->hasName())
287 Out << " %" << Arg->getName();
288 else if (Table.getValSlot(Arg) < 0)
289 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000290}
291
Chris Lattner7bfee412001-10-29 16:05:51 +0000292// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000293//
Chris Lattner7bfee412001-10-29 16:05:51 +0000294void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000296 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000297 } else {
298 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000299 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000300 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000301 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000302 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000303 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000304 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000305 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000306
Chris Lattnerfee714f2001-09-07 16:36:04 +0000307 // Output all of the instructions in the basic block...
308 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000309 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000310}
311
Chris Lattner862e3382001-10-13 06:42:36 +0000312
313// printInfoComment - Print a little comment after the instruction indicating
314// which slot it occupies.
315//
316void AssemblyWriter::printInfoComment(const Value *V) {
317 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000318 Out << "\t\t; <";
319 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000320
321 if (!V->hasName()) {
322 int Slot = Table.getValSlot(V); // Print out the def slot taken...
323 if (Slot >= 0) Out << ":" << Slot;
324 else Out << ":<badref>";
325 }
326 Out << "\t[#uses=" << V->use_size() << "]"; // Output # uses
327 }
328}
329
Chris Lattner7bfee412001-10-29 16:05:51 +0000330// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000331//
Chris Lattner7bfee412001-10-29 16:05:51 +0000332void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000333 Out << "\t";
334
335 // Print out name if it exists...
336 if (I && I->hasName())
337 Out << "%" << I->getName() << " = ";
338
339 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000340 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000341
342 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000343 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000344
345 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000346 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000347 writeOperand(I->getOperand(2), true);
348 Out << ",";
349 writeOperand(Operand, true);
350 Out << ",";
351 writeOperand(I->getOperand(1), true);
352
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000353 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000354 // Special case switch statement to get formatting nice and correct...
355 writeOperand(Operand , true); Out << ",";
356 writeOperand(I->getOperand(1), true); Out << " [";
357
Chris Lattnera073acb2001-07-07 08:36:50 +0000358 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000359 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000360 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000361 writeOperand(I->getOperand(op+1), true);
362 }
363 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000364 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000365 Out << " ";
366 printType(Operand->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000367
Chris Lattner931ef3b2001-06-11 15:04:20 +0000368 Out << " ["; writeOperand(Operand, false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000369 writeOperand(I->getOperand(1), false); Out << " ]";
Chris Lattnera073acb2001-07-07 08:36:50 +0000370 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
371 Out << ", [";
372 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000373 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000374 }
Chris Lattner862e3382001-10-13 06:42:36 +0000375 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000376 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000377 } else if (isa<CallInst>(I)) {
Chris Lattner7fac0702001-10-03 14:53:21 +0000378 // TODO: Should try to print out short form of the Call instruction
Chris Lattner2f7c9632001-06-06 20:29:01 +0000379 writeOperand(Operand, true);
380 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000381 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
382 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000383 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000384 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000385 }
386
387 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000388 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
389 // TODO: Should try to print out short form of the Invoke instruction
390 writeOperand(Operand, true);
391 Out << "(";
392 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
393 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
394 Out << ",";
395 writeOperand(I->getOperand(op), true);
396 }
397
398 Out << " )\n\t\t\tto";
399 writeOperand(II->getNormalDest(), true);
400 Out << " except";
401 writeOperand(II->getExceptionalDest(), true);
402
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000403 } else if (I->getOpcode() == Instruction::Malloc ||
404 I->getOpcode() == Instruction::Alloca) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000405 Out << " ";
406 printType(cast<const PointerType>(I->getType())->getValueType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000407 if (I->getNumOperands()) {
408 Out << ",";
409 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000410 }
Chris Lattner862e3382001-10-13 06:42:36 +0000411 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000412 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000413 Out << " to ";
414 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000415 } else if (Operand) { // Print the normal way...
416
417 // PrintAllTypes - Instructions who have operands of all the same type
418 // omit the type from all but the first operand. If the instruction has
419 // different type operands (for example br), then they are all printed.
420 bool PrintAllTypes = false;
421 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000422
Chris Lattnera073acb2001-07-07 08:36:50 +0000423 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
424 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000425 if (Operand->getType() != TheType) {
426 PrintAllTypes = true; // We have differing types! Print them all!
427 break;
428 }
429 }
430
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000431 // Shift Left & Right print both types even for Ubyte LHS
432 if (isa<ShiftInst>(I)) PrintAllTypes = true;
433
Chris Lattner7bfee412001-10-29 16:05:51 +0000434 if (!PrintAllTypes) {
435 Out << " ";
436 printType(I->getOperand(0)->getType());
437 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000438
Chris Lattnera073acb2001-07-07 08:36:50 +0000439 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000440 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000441 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000442 }
443 }
444
Chris Lattner862e3382001-10-13 06:42:36 +0000445 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000446 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000447}
448
449
Chris Lattner7bfee412001-10-29 16:05:51 +0000450string AssemblyWriter::calcTypeName(const Type *Ty,
451 vector<const Type *> &TypeStack) {
452 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
453
454 // Check to see if the type is named.
455 map<const Type *, string>::iterator I = TypeNames.find(Ty);
456 if (I != TypeNames.end()) return I->second;
457
458 // Check to see if the Type is already on the stack...
459 unsigned Slot = 0, CurSize = TypeStack.size();
460 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
461
462 // This is another base case for the recursion. In this case, we know
463 // that we have looped back to a type that we have previously visited.
464 // Generate the appropriate upreference to handle this.
465 //
466 if (Slot < CurSize)
467 return "\\" + utostr(CurSize-Slot); // Here's the upreference
468
469 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
470
471 string Result;
472 switch (Ty->getPrimitiveID()) {
473 case Type::MethodTyID: {
474 const MethodType *MTy = cast<const MethodType>(Ty);
475 Result = calcTypeName(MTy->getReturnType(), TypeStack)+" (";
476 for (MethodType::ParamTypes::const_iterator
477 I = MTy->getParamTypes().begin(),
478 E = MTy->getParamTypes().end(); I != E; ++I) {
479 if (I != MTy->getParamTypes().begin())
480 Result += ", ";
481 Result += calcTypeName(*I, TypeStack);
482 }
483 if (MTy->isVarArg()) {
484 if (!MTy->getParamTypes().empty()) Result += ", ";
485 Result += "...";
486 }
487 Result += ")";
488 break;
489 }
490 case Type::StructTyID: {
491 const StructType *STy = cast<const StructType>(Ty);
492 Result = "{ ";
493 for (StructType::ElementTypes::const_iterator
494 I = STy->getElementTypes().begin(),
495 E = STy->getElementTypes().end(); I != E; ++I) {
496 if (I != STy->getElementTypes().begin())
497 Result += ", ";
498 Result += calcTypeName(*I, TypeStack);
499 }
500 Result += " }";
501 break;
502 }
503 case Type::PointerTyID:
504 Result = calcTypeName(cast<const PointerType>(Ty)->getValueType(),
505 TypeStack) + " *";
506 break;
507 case Type::ArrayTyID: {
508 const ArrayType *ATy = cast<const ArrayType>(Ty);
509 int NumElements = ATy->getNumElements();
510 Result = "[";
511 if (NumElements != -1) Result += itostr(NumElements) + " x ";
512 Result += calcTypeName(ATy->getElementType(), TypeStack) + "]";
513 break;
514 }
515 default:
516 assert(0 && "Unhandled case in getTypeProps!");
517 Result = "<error>";
518 }
519
520 TypeStack.pop_back(); // Remove self from stack...
521 return Result;
522}
523
524// printType - Go to extreme measures to attempt to print out a short, symbolic
525// version of a type name.
526//
527ostream &AssemblyWriter::printType(const Type *Ty) {
528 // Primitive types always print out their description, regardless of whether
529 // they have been named or not.
530 //
531 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
532
533 // Check to see if the type is named.
534 map<const Type *, string>::iterator I = TypeNames.find(Ty);
535 if (I != TypeNames.end()) return Out << I->second;
536
537 // Otherwise we have a type that has not been named but is a derived type.
538 // Carefully recurse the type hierarchy to print out any contained symbolic
539 // names.
540 //
541 vector<const Type *> TypeStack;
542 string TypeName = calcTypeName(Ty, TypeStack);
543 TypeNames.insert(make_pair(Ty, TypeName)); // Cache type name for later use
544 return Out << TypeName;
545}
546
547
Chris Lattner2f7c9632001-06-06 20:29:01 +0000548//===----------------------------------------------------------------------===//
549// External Interface declarations
550//===----------------------------------------------------------------------===//
551
552
553
554void WriteToAssembly(const Module *M, ostream &o) {
555 if (M == 0) { o << "<null> module\n"; return; }
556 SlotCalculator SlotTable(M, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000557 AssemblyWriter W(o, SlotTable, M);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000558
559 W.write(M);
560}
561
Chris Lattneradfe0d12001-09-10 20:08:19 +0000562void WriteToAssembly(const GlobalVariable *G, ostream &o) {
563 if (G == 0) { o << "<null> global variable\n"; return; }
564 SlotCalculator SlotTable(G->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000565 AssemblyWriter W(o, SlotTable, G->getParent());
Chris Lattneradfe0d12001-09-10 20:08:19 +0000566 W.write(G);
567}
568
Chris Lattner2f7c9632001-06-06 20:29:01 +0000569void WriteToAssembly(const Method *M, ostream &o) {
570 if (M == 0) { o << "<null> method\n"; return; }
571 SlotCalculator SlotTable(M->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000572 AssemblyWriter W(o, SlotTable, M->getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000573
574 W.write(M);
575}
576
577
578void WriteToAssembly(const BasicBlock *BB, ostream &o) {
579 if (BB == 0) { o << "<null> basic block\n"; return; }
580
581 SlotCalculator SlotTable(BB->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000582 AssemblyWriter W(o, SlotTable,
583 BB->getParent() ? BB->getParent()->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000584
585 W.write(BB);
586}
587
588void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
589 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +0000590 o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000591}
592
593void WriteToAssembly(const Instruction *I, ostream &o) {
594 if (I == 0) { o << "<null> instruction\n"; return; }
595
Chris Lattner7bfee412001-10-29 16:05:51 +0000596 const Method *M = I->getParent() ? I->getParent()->getParent() : 0;
597 SlotCalculator SlotTable(M, true);
598 AssemblyWriter W(o, SlotTable, M ? M->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000599
600 W.write(I);
601}