blob: 703bebe41c6e2cd12afeb89bb6b6acd05bb2bb1f [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
Chris Lattnerb86620e2001-10-29 16:37:48 +000029static const Module *getModuleFromVal(const Value *V) {
30 if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V))
31 return MA->getParent() ? MA->getParent()->getParent() : 0;
32 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
33 return BB->getParent() ? BB->getParent()->getParent() : 0;
34 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
35 const Method *M = I->getParent() ? I->getParent()->getParent() : 0;
36 return M ? M->getParent() : 0;
37 } else if (const GlobalValue *GV =dyn_cast<const GlobalValue>(V))
38 return GV->getParent();
39 else if (const Module *Mod = dyn_cast<const Module>(V))
40 return Mod;
41 return 0;
42}
43
Chris Lattner7bfee412001-10-29 16:05:51 +000044static SlotCalculator *createSlotCalculator(const Value *V) {
45 assert(!isa<Type>(V) && "Can't create an SC for a type!");
46 if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V)){
47 return new SlotCalculator(MA->getParent(), true);
48 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
49 return new SlotCalculator(I->getParent()->getParent(), true);
50 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
51 return new SlotCalculator(BB->getParent(), true);
52 } else if (const GlobalVariable *GV =dyn_cast<const GlobalVariable>(V)){
53 return new SlotCalculator(GV->getParent(), true);
54 } else if (const Method *Meth = dyn_cast<const Method>(V)) {
55 return new SlotCalculator(Meth, true);
56 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
57 return new SlotCalculator(Mod, true);
58 }
59 return 0;
60}
Chris Lattner2f7c9632001-06-06 20:29:01 +000061
Chris Lattner5e5abe32001-07-20 19:15:21 +000062// WriteAsOperand - Write the name of the specified value out to the specified
63// ostream. This can be useful when you just want to print int %reg126, not the
64// whole instruction that generated it.
65//
Chris Lattnerb86620e2001-10-29 16:37:48 +000066static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
67 SlotCalculator *Table) {
Chris Lattnerfee714f2001-09-07 16:36:04 +000068 if (PrintName && V->hasName()) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000069 Out << " %" << V->getName();
70 } else {
Chris Lattner4b717c02001-10-01 16:18:37 +000071 if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000072 Out << " " << CPV->getStrValue();
73 } else {
74 int Slot;
75 if (Table) {
76 Slot = Table->getValSlot(V);
77 } else {
Chris Lattnerb86620e2001-10-29 16:37:48 +000078 if (const Type *Ty = dyn_cast<const Type>(V)) {
79 Out << " " << Ty->getDescription();
80 return;
81 }
Chris Lattner7bfee412001-10-29 16:05:51 +000082
83 Table = createSlotCalculator(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +000084 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +000085
Chris Lattner5e5abe32001-07-20 19:15:21 +000086 Slot = Table->getValSlot(V);
87 delete Table;
88 }
89 if (Slot >= 0) Out << " %" << Slot;
90 else if (PrintName)
91 Out << "<badref>"; // Not embeded into a location?
92 }
93 }
Chris Lattnerb86620e2001-10-29 16:37:48 +000094}
95
96
97// If the module has a symbol table, take all global types and stuff their
98// names into the TypeNames map.
99//
100static void fillTypeNameTable(const Module *M,
101 map<const Type *, string> &TypeNames) {
102 if (M && M->hasSymbolTable()) {
103 const SymbolTable *ST = M->getSymbolTable();
104 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
105 if (PI != ST->end()) {
106 SymbolTable::type_const_iterator I = PI->second.begin();
107 for (; I != PI->second.end(); ++I) {
108 // As a heuristic, don't insert pointer to primitive types, because
109 // they are used too often to have a single useful name.
110 //
111 const Type *Ty = cast<const Type>(I->second);
112 if (!isa<PointerType>(Ty) ||
113 !cast<PointerType>(Ty)->getValueType()->isPrimitiveType())
114 TypeNames.insert(make_pair(Ty, "%"+I->first));
115 }
116 }
117 }
118}
119
120
121
122static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
123 map<const Type *, string> &TypeNames) {
124 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
125
126 // Check to see if the type is named.
127 map<const Type *, string>::iterator I = TypeNames.find(Ty);
128 if (I != TypeNames.end()) return I->second;
129
130 // Check to see if the Type is already on the stack...
131 unsigned Slot = 0, CurSize = TypeStack.size();
132 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
133
134 // This is another base case for the recursion. In this case, we know
135 // that we have looped back to a type that we have previously visited.
136 // Generate the appropriate upreference to handle this.
137 //
138 if (Slot < CurSize)
139 return "\\" + utostr(CurSize-Slot); // Here's the upreference
140
141 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
142
143 string Result;
144 switch (Ty->getPrimitiveID()) {
145 case Type::MethodTyID: {
146 const MethodType *MTy = cast<const MethodType>(Ty);
147 Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " (";
148 for (MethodType::ParamTypes::const_iterator
149 I = MTy->getParamTypes().begin(),
150 E = MTy->getParamTypes().end(); I != E; ++I) {
151 if (I != MTy->getParamTypes().begin())
152 Result += ", ";
153 Result += calcTypeName(*I, TypeStack, TypeNames);
154 }
155 if (MTy->isVarArg()) {
156 if (!MTy->getParamTypes().empty()) Result += ", ";
157 Result += "...";
158 }
159 Result += ")";
160 break;
161 }
162 case Type::StructTyID: {
163 const StructType *STy = cast<const StructType>(Ty);
164 Result = "{ ";
165 for (StructType::ElementTypes::const_iterator
166 I = STy->getElementTypes().begin(),
167 E = STy->getElementTypes().end(); I != E; ++I) {
168 if (I != STy->getElementTypes().begin())
169 Result += ", ";
170 Result += calcTypeName(*I, TypeStack, TypeNames);
171 }
172 Result += " }";
173 break;
174 }
175 case Type::PointerTyID:
176 Result = calcTypeName(cast<const PointerType>(Ty)->getValueType(),
177 TypeStack, TypeNames) + " *";
178 break;
179 case Type::ArrayTyID: {
180 const ArrayType *ATy = cast<const ArrayType>(Ty);
181 int NumElements = ATy->getNumElements();
182 Result = "[";
183 if (NumElements != -1) Result += itostr(NumElements) + " x ";
184 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
185 break;
186 }
187 default:
188 assert(0 && "Unhandled case in getTypeProps!");
189 Result = "<error>";
190 }
191
192 TypeStack.pop_back(); // Remove self from stack...
193 return Result;
194}
195
196
197// printTypeInt - The internal guts of printing out a type that has a
198// potentially named portion.
199//
200static ostream &printTypeInt(ostream &Out, const Type *Ty,
201 map<const Type *, string> &TypeNames) {
202 // Primitive types always print out their description, regardless of whether
203 // they have been named or not.
204 //
205 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
206
207 // Check to see if the type is named.
208 map<const Type *, string>::iterator I = TypeNames.find(Ty);
209 if (I != TypeNames.end()) return Out << I->second;
210
211 // Otherwise we have a type that has not been named but is a derived type.
212 // Carefully recurse the type hierarchy to print out any contained symbolic
213 // names.
214 //
215 vector<const Type *> TypeStack;
216 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
217 TypeNames.insert(make_pair(Ty, TypeName)); // Cache type name for later use
218 return Out << TypeName;
219}
220
221// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
222// type, iff there is an entry in the modules symbol table for the specified
223// type or one of it's component types. This is slower than a simple x << Type;
224//
225ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
226 Out << " ";
227
228 // If they want us to print out a type, attempt to make it symbolic if there
229 // is a symbol table in the module...
230 if (M && M->hasSymbolTable()) {
231 map<const Type *, string> TypeNames;
232 fillTypeNameTable(M, TypeNames);
233
234 return printTypeInt(Out, V->getType(), TypeNames);
235 } else {
236 return Out << V->getType()->getDescription();
237 }
238}
239
240
241// WriteAsOperand - Write the name of the specified value out to the specified
242// ostream. This can be useful when you just want to print int %reg126, not the
243// whole instruction that generated it.
244//
245ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
246 bool PrintName, SlotCalculator *Table) {
247 if (PrintType) {
248 WriteTypeSymbolic(Ty, getModuleFromVal(V));
249 }
250
251 WriteAsOperandInternal(Out, V, PrintName, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000252 return Out;
253}
254
255
Chris Lattner2e9fee42001-07-12 23:35:26 +0000256
Chris Lattnerfee714f2001-09-07 16:36:04 +0000257class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000258 ostream &Out;
259 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000260 const Module *TheModule;
261 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000262public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000263 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
264 : Out(o), Table(Tab), TheModule(M) {
265
266 // If the module has a symbol table, take all global types and stuff their
267 // names into the TypeNames map.
268 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000269 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000270 }
271
Chris Lattner7bfee412001-10-29 16:05:51 +0000272 inline void write(const Module *M) { printModule(M); }
273 inline void write(const GlobalVariable *G) { printGlobal(G); }
274 inline void write(const Method *M) { printMethod(M); }
275 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
276 inline void write(const Instruction *I) { printInstruction(I); }
277 inline void write(const ConstPoolVal *CPV) { printConstant(CPV); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000278
Chris Lattner2f7c9632001-06-06 20:29:01 +0000279private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000280 void printModule(const Module *M);
281 void printSymbolTable(const SymbolTable &ST);
282 void printConstant(const ConstPoolVal *CPV);
283 void printGlobal(const GlobalVariable *GV);
284 void printMethod(const Method *M);
285 void printMethodArgument(const MethodArgument *MA);
286 void printBasicBlock(const BasicBlock *BB);
287 void printInstruction(const Instruction *I);
288 ostream &printType(const Type *Ty);
289
Chris Lattner2f7c9632001-06-06 20:29:01 +0000290 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
Chris Lattner862e3382001-10-13 06:42:36 +0000291
292 // printInfoComment - Print a little comment after the instruction indicating
293 // which slot it occupies.
294 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000295};
296
297
Chris Lattnerfee714f2001-09-07 16:36:04 +0000298void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
299 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000300 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000301 WriteAsOperandInternal(Out, Operand, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000302}
303
Chris Lattner2f7c9632001-06-06 20:29:01 +0000304
Chris Lattner7bfee412001-10-29 16:05:51 +0000305void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000306 // Loop over the symbol table, emitting all named constants...
307 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000308 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000309
310 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000311 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000312
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000313 Out << "implementation\n";
314
Chris Lattnerfee714f2001-09-07 16:36:04 +0000315 // Output all of the methods...
Chris Lattner7bfee412001-10-29 16:05:51 +0000316 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printMethod));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000317}
318
Chris Lattner7bfee412001-10-29 16:05:51 +0000319void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000320 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000321
322 if (!GV->hasInitializer()) Out << "uninitialized ";
323
Chris Lattner7bfee412001-10-29 16:05:51 +0000324 Out << (GV->isConstant() ? "constant " : "global ");
325 printType(GV->getType()->getValueType());
Chris Lattner37798642001-09-18 04:01:05 +0000326
327 if (GV->hasInitializer())
328 writeOperand(GV->getInitializer(), false, false);
329
Chris Lattner862e3382001-10-13 06:42:36 +0000330 printInfoComment(GV);
Chris Lattner37798642001-09-18 04:01:05 +0000331 Out << endl;
Chris Lattnerda975502001-09-10 07:58:01 +0000332}
333
Chris Lattnerfee714f2001-09-07 16:36:04 +0000334
Chris Lattner7bfee412001-10-29 16:05:51 +0000335// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000336// if a named constant is found, emit it's declaration...
337//
Chris Lattner7bfee412001-10-29 16:05:51 +0000338void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000339 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
340 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
341 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
342
343 for (; I != End; ++I) {
344 const Value *V = I->second;
Chris Lattner38569342001-10-01 20:11:19 +0000345 if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000346 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000347 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000348 Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
349 }
350 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000351 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000352}
353
354
Chris Lattner7bfee412001-10-29 16:05:51 +0000355// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000356//
Chris Lattner7bfee412001-10-29 16:05:51 +0000357void AssemblyWriter::printConstant(const ConstPoolVal *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000358 // Don't print out unnamed constants, they will be inlined
359 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000360
Chris Lattneree998be2001-07-26 16:29:38 +0000361 // Print out name...
362 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000363
Chris Lattneree998be2001-07-26 16:29:38 +0000364 // Print out the constant type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000365 printType(CPV->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000366
367 // Write the value out now...
368 writeOperand(CPV, false, false);
369
370 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
371 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
Chris Lattner7bfee412001-10-29 16:05:51 +0000372 Out << "\t\t; <";
373 printType(CPV->getType()) << ">:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000374 if (Slot >= 0) Out << Slot;
375 else Out << "<badref>";
376 }
377
378 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000379}
380
Chris Lattner7bfee412001-10-29 16:05:51 +0000381// printMethod - Print all aspects of a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000382//
Chris Lattner7bfee412001-10-29 16:05:51 +0000383void AssemblyWriter::printMethod(const Method *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000384 // Print out the return type and name...
Chris Lattner7bfee412001-10-29 16:05:51 +0000385 Out << "\n" << (M->isExternal() ? "declare " : "");
386 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000387 Table.incorporateMethod(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000388
Chris Lattner7bfee412001-10-29 16:05:51 +0000389 // Loop over the arguments, printing them...
Chris Lattner862e3382001-10-13 06:42:36 +0000390 const MethodType *MT = cast<const MethodType>(M->getMethodType());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000391
Chris Lattner862e3382001-10-13 06:42:36 +0000392 if (!M->isExternal()) {
393 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000394 bind_obj(this, &AssemblyWriter::printMethodArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000395 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000396 // Loop over the arguments, printing them...
Chris Lattner862e3382001-10-13 06:42:36 +0000397 const MethodType *MT = cast<const MethodType>(M->getMethodType());
398 for (MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
399 E = MT->getParamTypes().end(); I != E; ++I) {
400 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000401 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000402 }
403 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000404
405 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000406 if (MT->isVarArg()) {
407 if (MT->getParamTypes().size()) Out << ", ";
408 Out << "..."; // Output varargs portion of signature!
409 }
410 Out << ")\n";
411
412 if (!M->isExternal()) {
413 // Loop over the symbol table, emitting all named constants...
414 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000415 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000416
417 Out << "begin";
418
419 // Output all of its basic blocks... for the method
420 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000421 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000422
Chris Lattnera7620d92001-07-15 06:35:59 +0000423 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000424 }
425
426 Table.purgeMethod();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000427}
428
Chris Lattner7bfee412001-10-29 16:05:51 +0000429// printMethodArgument - This member is called for every argument that
Chris Lattner2f7c9632001-06-06 20:29:01 +0000430// is passed into the method. Simply print it out
431//
Chris Lattner7bfee412001-10-29 16:05:51 +0000432void AssemblyWriter::printMethodArgument(const MethodArgument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000433 // Insert commas as we go... the first arg doesn't get a comma
434 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
435
436 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000437 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000438
439 // Output name, if available...
440 if (Arg->hasName())
441 Out << " %" << Arg->getName();
442 else if (Table.getValSlot(Arg) < 0)
443 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444}
445
Chris Lattner7bfee412001-10-29 16:05:51 +0000446// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000447//
Chris Lattner7bfee412001-10-29 16:05:51 +0000448void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000450 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000451 } else {
452 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000453 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000454 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000455 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000456 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000457 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000458 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000459 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000460
Chris Lattnerfee714f2001-09-07 16:36:04 +0000461 // Output all of the instructions in the basic block...
462 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000463 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000464}
465
Chris Lattner862e3382001-10-13 06:42:36 +0000466
467// printInfoComment - Print a little comment after the instruction indicating
468// which slot it occupies.
469//
470void AssemblyWriter::printInfoComment(const Value *V) {
471 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000472 Out << "\t\t; <";
473 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000474
475 if (!V->hasName()) {
476 int Slot = Table.getValSlot(V); // Print out the def slot taken...
477 if (Slot >= 0) Out << ":" << Slot;
478 else Out << ":<badref>";
479 }
480 Out << "\t[#uses=" << V->use_size() << "]"; // Output # uses
481 }
482}
483
Chris Lattner7bfee412001-10-29 16:05:51 +0000484// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000485//
Chris Lattner7bfee412001-10-29 16:05:51 +0000486void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000487 Out << "\t";
488
489 // Print out name if it exists...
490 if (I && I->hasName())
491 Out << "%" << I->getName() << " = ";
492
493 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000494 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000495
496 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000497 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000498
499 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000500 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000501 writeOperand(I->getOperand(2), true);
502 Out << ",";
503 writeOperand(Operand, true);
504 Out << ",";
505 writeOperand(I->getOperand(1), true);
506
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000507 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000508 // Special case switch statement to get formatting nice and correct...
509 writeOperand(Operand , true); Out << ",";
510 writeOperand(I->getOperand(1), true); Out << " [";
511
Chris Lattnera073acb2001-07-07 08:36:50 +0000512 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000513 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000514 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000515 writeOperand(I->getOperand(op+1), true);
516 }
517 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000518 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000519 Out << " ";
520 printType(Operand->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000521
Chris Lattner931ef3b2001-06-11 15:04:20 +0000522 Out << " ["; writeOperand(Operand, false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000523 writeOperand(I->getOperand(1), false); Out << " ]";
Chris Lattnera073acb2001-07-07 08:36:50 +0000524 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
525 Out << ", [";
526 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000527 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000528 }
Chris Lattner862e3382001-10-13 06:42:36 +0000529 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000530 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000531 } else if (isa<CallInst>(I)) {
Chris Lattner7fac0702001-10-03 14:53:21 +0000532 // TODO: Should try to print out short form of the Call instruction
Chris Lattner2f7c9632001-06-06 20:29:01 +0000533 writeOperand(Operand, true);
534 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000535 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
536 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000537 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000538 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000539 }
540
541 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000542 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
543 // TODO: Should try to print out short form of the Invoke instruction
544 writeOperand(Operand, true);
545 Out << "(";
546 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
547 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
548 Out << ",";
549 writeOperand(I->getOperand(op), true);
550 }
551
552 Out << " )\n\t\t\tto";
553 writeOperand(II->getNormalDest(), true);
554 Out << " except";
555 writeOperand(II->getExceptionalDest(), true);
556
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000557 } else if (I->getOpcode() == Instruction::Malloc ||
558 I->getOpcode() == Instruction::Alloca) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000559 Out << " ";
560 printType(cast<const PointerType>(I->getType())->getValueType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000561 if (I->getNumOperands()) {
562 Out << ",";
563 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000564 }
Chris Lattner862e3382001-10-13 06:42:36 +0000565 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000566 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000567 Out << " to ";
568 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000569 } else if (Operand) { // Print the normal way...
570
571 // PrintAllTypes - Instructions who have operands of all the same type
572 // omit the type from all but the first operand. If the instruction has
573 // different type operands (for example br), then they are all printed.
574 bool PrintAllTypes = false;
575 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000576
Chris Lattnera073acb2001-07-07 08:36:50 +0000577 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
578 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579 if (Operand->getType() != TheType) {
580 PrintAllTypes = true; // We have differing types! Print them all!
581 break;
582 }
583 }
584
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000585 // Shift Left & Right print both types even for Ubyte LHS
586 if (isa<ShiftInst>(I)) PrintAllTypes = true;
587
Chris Lattner7bfee412001-10-29 16:05:51 +0000588 if (!PrintAllTypes) {
589 Out << " ";
590 printType(I->getOperand(0)->getType());
591 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000592
Chris Lattnera073acb2001-07-07 08:36:50 +0000593 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000594 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000595 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596 }
597 }
598
Chris Lattner862e3382001-10-13 06:42:36 +0000599 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000600 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000601}
602
603
Chris Lattner7bfee412001-10-29 16:05:51 +0000604// printType - Go to extreme measures to attempt to print out a short, symbolic
605// version of a type name.
606//
607ostream &AssemblyWriter::printType(const Type *Ty) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000608 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner7bfee412001-10-29 16:05:51 +0000609}
610
611
Chris Lattner2f7c9632001-06-06 20:29:01 +0000612//===----------------------------------------------------------------------===//
613// External Interface declarations
614//===----------------------------------------------------------------------===//
615
616
617
618void WriteToAssembly(const Module *M, ostream &o) {
619 if (M == 0) { o << "<null> module\n"; return; }
620 SlotCalculator SlotTable(M, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000621 AssemblyWriter W(o, SlotTable, M);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000622
623 W.write(M);
624}
625
Chris Lattneradfe0d12001-09-10 20:08:19 +0000626void WriteToAssembly(const GlobalVariable *G, ostream &o) {
627 if (G == 0) { o << "<null> global variable\n"; return; }
628 SlotCalculator SlotTable(G->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000629 AssemblyWriter W(o, SlotTable, G->getParent());
Chris Lattneradfe0d12001-09-10 20:08:19 +0000630 W.write(G);
631}
632
Chris Lattner2f7c9632001-06-06 20:29:01 +0000633void WriteToAssembly(const Method *M, ostream &o) {
634 if (M == 0) { o << "<null> method\n"; return; }
635 SlotCalculator SlotTable(M->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000636 AssemblyWriter W(o, SlotTable, M->getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000637
638 W.write(M);
639}
640
641
642void WriteToAssembly(const BasicBlock *BB, ostream &o) {
643 if (BB == 0) { o << "<null> basic block\n"; return; }
644
645 SlotCalculator SlotTable(BB->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000646 AssemblyWriter W(o, SlotTable,
647 BB->getParent() ? BB->getParent()->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000648
649 W.write(BB);
650}
651
652void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
653 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +0000654 o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000655}
656
657void WriteToAssembly(const Instruction *I, ostream &o) {
658 if (I == 0) { o << "<null> instruction\n"; return; }
659
Chris Lattner7bfee412001-10-29 16:05:51 +0000660 const Method *M = I->getParent() ? I->getParent()->getParent() : 0;
661 SlotCalculator SlotTable(M, true);
662 AssemblyWriter W(o, SlotTable, M ? M->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000663
664 W.write(I);
665}