blob: 9523c9428a190f42a1960b8b3ab7073966d081c2 [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
Chris Lattner7db79582001-11-07 04:21:57 +000013#include "llvm/Assembly/CachedWriter.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#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"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000021#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000022#include "llvm/iPHINode.h"
23#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000024#include "llvm/SymbolTable.h"
Chris Lattner5de22042001-11-27 00:03:19 +000025#include "Support/StringExtras.h"
26#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000027#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000028#include <map>
29
Chris Lattnerb86620e2001-10-29 16:37:48 +000030static const Module *getModuleFromVal(const Value *V) {
31 if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V))
32 return MA->getParent() ? MA->getParent()->getParent() : 0;
33 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
34 return BB->getParent() ? BB->getParent()->getParent() : 0;
35 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
36 const Method *M = I->getParent() ? I->getParent()->getParent() : 0;
37 return M ? M->getParent() : 0;
38 } else if (const GlobalValue *GV =dyn_cast<const GlobalValue>(V))
39 return GV->getParent();
40 else if (const Module *Mod = dyn_cast<const Module>(V))
41 return Mod;
42 return 0;
43}
44
Chris Lattner7bfee412001-10-29 16:05:51 +000045static SlotCalculator *createSlotCalculator(const Value *V) {
46 assert(!isa<Type>(V) && "Can't create an SC for a type!");
47 if (const MethodArgument *MA =dyn_cast<const MethodArgument>(V)){
48 return new SlotCalculator(MA->getParent(), true);
49 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
50 return new SlotCalculator(I->getParent()->getParent(), true);
51 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
52 return new SlotCalculator(BB->getParent(), true);
53 } else if (const GlobalVariable *GV =dyn_cast<const GlobalVariable>(V)){
54 return new SlotCalculator(GV->getParent(), true);
55 } else if (const Method *Meth = dyn_cast<const Method>(V)) {
56 return new SlotCalculator(Meth, true);
57 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
58 return new SlotCalculator(Mod, true);
59 }
60 return 0;
61}
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattner5e5abe32001-07-20 19:15:21 +000063// WriteAsOperand - Write the name of the specified value out to the specified
64// ostream. This can be useful when you just want to print int %reg126, not the
65// whole instruction that generated it.
66//
Chris Lattnerb86620e2001-10-29 16:37:48 +000067static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
68 SlotCalculator *Table) {
Chris Lattnerfee714f2001-09-07 16:36:04 +000069 if (PrintName && V->hasName()) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000070 Out << " %" << V->getName();
71 } else {
Chris Lattner4b717c02001-10-01 16:18:37 +000072 if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000073 Out << " " << CPV->getStrValue();
74 } else {
75 int Slot;
76 if (Table) {
77 Slot = Table->getValSlot(V);
78 } else {
Chris Lattnerb86620e2001-10-29 16:37:48 +000079 if (const Type *Ty = dyn_cast<const Type>(V)) {
80 Out << " " << Ty->getDescription();
81 return;
82 }
Chris Lattner7bfee412001-10-29 16:05:51 +000083
84 Table = createSlotCalculator(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +000085 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +000086
Chris Lattner5e5abe32001-07-20 19:15:21 +000087 Slot = Table->getValSlot(V);
88 delete Table;
89 }
90 if (Slot >= 0) Out << " %" << Slot;
91 else if (PrintName)
92 Out << "<badref>"; // Not embeded into a location?
93 }
94 }
Chris Lattnerb86620e2001-10-29 16:37:48 +000095}
96
97
98// If the module has a symbol table, take all global types and stuff their
99// names into the TypeNames map.
100//
101static void fillTypeNameTable(const Module *M,
102 map<const Type *, string> &TypeNames) {
103 if (M && M->hasSymbolTable()) {
104 const SymbolTable *ST = M->getSymbolTable();
105 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
106 if (PI != ST->end()) {
107 SymbolTable::type_const_iterator I = PI->second.begin();
108 for (; I != PI->second.end(); ++I) {
109 // As a heuristic, don't insert pointer to primitive types, because
110 // they are used too often to have a single useful name.
111 //
112 const Type *Ty = cast<const Type>(I->second);
113 if (!isa<PointerType>(Ty) ||
114 !cast<PointerType>(Ty)->getValueType()->isPrimitiveType())
115 TypeNames.insert(make_pair(Ty, "%"+I->first));
116 }
117 }
118 }
119}
120
121
122
123static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
124 map<const Type *, string> &TypeNames) {
125 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
126
127 // Check to see if the type is named.
128 map<const Type *, string>::iterator I = TypeNames.find(Ty);
129 if (I != TypeNames.end()) return I->second;
130
131 // Check to see if the Type is already on the stack...
132 unsigned Slot = 0, CurSize = TypeStack.size();
133 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
134
135 // This is another base case for the recursion. In this case, we know
136 // that we have looped back to a type that we have previously visited.
137 // Generate the appropriate upreference to handle this.
138 //
139 if (Slot < CurSize)
140 return "\\" + utostr(CurSize-Slot); // Here's the upreference
141
142 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
143
144 string Result;
145 switch (Ty->getPrimitiveID()) {
146 case Type::MethodTyID: {
147 const MethodType *MTy = cast<const MethodType>(Ty);
148 Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " (";
149 for (MethodType::ParamTypes::const_iterator
150 I = MTy->getParamTypes().begin(),
151 E = MTy->getParamTypes().end(); I != E; ++I) {
152 if (I != MTy->getParamTypes().begin())
153 Result += ", ";
154 Result += calcTypeName(*I, TypeStack, TypeNames);
155 }
156 if (MTy->isVarArg()) {
157 if (!MTy->getParamTypes().empty()) Result += ", ";
158 Result += "...";
159 }
160 Result += ")";
161 break;
162 }
163 case Type::StructTyID: {
164 const StructType *STy = cast<const StructType>(Ty);
165 Result = "{ ";
166 for (StructType::ElementTypes::const_iterator
167 I = STy->getElementTypes().begin(),
168 E = STy->getElementTypes().end(); I != E; ++I) {
169 if (I != STy->getElementTypes().begin())
170 Result += ", ";
171 Result += calcTypeName(*I, TypeStack, TypeNames);
172 }
173 Result += " }";
174 break;
175 }
176 case Type::PointerTyID:
177 Result = calcTypeName(cast<const PointerType>(Ty)->getValueType(),
178 TypeStack, TypeNames) + " *";
179 break;
180 case Type::ArrayTyID: {
181 const ArrayType *ATy = cast<const ArrayType>(Ty);
182 int NumElements = ATy->getNumElements();
183 Result = "[";
184 if (NumElements != -1) Result += itostr(NumElements) + " x ";
185 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
186 break;
187 }
188 default:
189 assert(0 && "Unhandled case in getTypeProps!");
190 Result = "<error>";
191 }
192
193 TypeStack.pop_back(); // Remove self from stack...
194 return Result;
195}
196
197
198// printTypeInt - The internal guts of printing out a type that has a
199// potentially named portion.
200//
201static ostream &printTypeInt(ostream &Out, const Type *Ty,
202 map<const Type *, string> &TypeNames) {
203 // Primitive types always print out their description, regardless of whether
204 // they have been named or not.
205 //
206 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
207
208 // Check to see if the type is named.
209 map<const Type *, string>::iterator I = TypeNames.find(Ty);
210 if (I != TypeNames.end()) return Out << I->second;
211
212 // Otherwise we have a type that has not been named but is a derived type.
213 // Carefully recurse the type hierarchy to print out any contained symbolic
214 // names.
215 //
216 vector<const Type *> TypeStack;
217 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
218 TypeNames.insert(make_pair(Ty, TypeName)); // Cache type name for later use
219 return Out << TypeName;
220}
221
Chris Lattner34b95182001-10-31 04:33:19 +0000222
Chris Lattnerb86620e2001-10-29 16:37:48 +0000223// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
224// type, iff there is an entry in the modules symbol table for the specified
225// type or one of it's component types. This is slower than a simple x << Type;
226//
227ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
228 Out << " ";
229
230 // If they want us to print out a type, attempt to make it symbolic if there
231 // is a symbol table in the module...
232 if (M && M->hasSymbolTable()) {
233 map<const Type *, string> TypeNames;
234 fillTypeNameTable(M, TypeNames);
235
Chris Lattner72f866e2001-10-29 16:40:32 +0000236 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000237 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000238 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000239 }
240}
241
242
243// WriteAsOperand - Write the name of the specified value out to the specified
244// ostream. This can be useful when you just want to print int %reg126, not the
245// whole instruction that generated it.
246//
247ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
248 bool PrintName, SlotCalculator *Table) {
Chris Lattner72f866e2001-10-29 16:40:32 +0000249 if (PrintType)
250 WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000251
252 WriteAsOperandInternal(Out, V, PrintName, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000253 return Out;
254}
255
256
Chris Lattner2e9fee42001-07-12 23:35:26 +0000257
Chris Lattnerfee714f2001-09-07 16:36:04 +0000258class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000259 ostream &Out;
260 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000261 const Module *TheModule;
262 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000263public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000264 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
265 : Out(o), Table(Tab), TheModule(M) {
266
267 // If the module has a symbol table, take all global types and stuff their
268 // names into the TypeNames map.
269 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000270 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000271 }
272
Chris Lattner7bfee412001-10-29 16:05:51 +0000273 inline void write(const Module *M) { printModule(M); }
274 inline void write(const GlobalVariable *G) { printGlobal(G); }
275 inline void write(const Method *M) { printMethod(M); }
276 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
277 inline void write(const Instruction *I) { printInstruction(I); }
278 inline void write(const ConstPoolVal *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000279 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000280
Chris Lattner2f7c9632001-06-06 20:29:01 +0000281private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000282 void printModule(const Module *M);
283 void printSymbolTable(const SymbolTable &ST);
284 void printConstant(const ConstPoolVal *CPV);
285 void printGlobal(const GlobalVariable *GV);
286 void printMethod(const Method *M);
287 void printMethodArgument(const MethodArgument *MA);
288 void printBasicBlock(const BasicBlock *BB);
289 void printInstruction(const Instruction *I);
290 ostream &printType(const Type *Ty);
291
Chris Lattner2f7c9632001-06-06 20:29:01 +0000292 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
Chris Lattner862e3382001-10-13 06:42:36 +0000293
294 // printInfoComment - Print a little comment after the instruction indicating
295 // which slot it occupies.
296 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000297};
298
299
Chris Lattnerfee714f2001-09-07 16:36:04 +0000300void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
301 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000302 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000303 WriteAsOperandInternal(Out, Operand, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000304}
305
Chris Lattner2f7c9632001-06-06 20:29:01 +0000306
Chris Lattner7bfee412001-10-29 16:05:51 +0000307void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000308 // Loop over the symbol table, emitting all named constants...
309 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000310 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000311
312 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000313 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000314
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000315 Out << "implementation\n";
316
Chris Lattnerfee714f2001-09-07 16:36:04 +0000317 // Output all of the methods...
Chris Lattner7bfee412001-10-29 16:05:51 +0000318 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printMethod));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000319}
320
Chris Lattner7bfee412001-10-29 16:05:51 +0000321void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000322 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000323
Chris Lattner841d8b92001-11-26 18:54:16 +0000324 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000325 if (!GV->hasInitializer()) Out << "uninitialized ";
326
Chris Lattner7bfee412001-10-29 16:05:51 +0000327 Out << (GV->isConstant() ? "constant " : "global ");
328 printType(GV->getType()->getValueType());
Chris Lattner37798642001-09-18 04:01:05 +0000329
330 if (GV->hasInitializer())
331 writeOperand(GV->getInitializer(), false, false);
332
Chris Lattner862e3382001-10-13 06:42:36 +0000333 printInfoComment(GV);
Chris Lattner37798642001-09-18 04:01:05 +0000334 Out << endl;
Chris Lattnerda975502001-09-10 07:58:01 +0000335}
336
Chris Lattnerfee714f2001-09-07 16:36:04 +0000337
Chris Lattner7bfee412001-10-29 16:05:51 +0000338// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000339// if a named constant is found, emit it's declaration...
340//
Chris Lattner7bfee412001-10-29 16:05:51 +0000341void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000342 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
343 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
344 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
345
346 for (; I != End; ++I) {
347 const Value *V = I->second;
Chris Lattner38569342001-10-01 20:11:19 +0000348 if (const ConstPoolVal *CPV = dyn_cast<const ConstPoolVal>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000349 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000350 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000351 Out << "\t%" << I->first << " = type " << Ty->getDescription() << endl;
352 }
353 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000354 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000355}
356
357
Chris Lattner7bfee412001-10-29 16:05:51 +0000358// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000359//
Chris Lattner7bfee412001-10-29 16:05:51 +0000360void AssemblyWriter::printConstant(const ConstPoolVal *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000361 // Don't print out unnamed constants, they will be inlined
362 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000363
Chris Lattneree998be2001-07-26 16:29:38 +0000364 // Print out name...
365 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000366
Chris Lattneree998be2001-07-26 16:29:38 +0000367 // Print out the constant type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000368 printType(CPV->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000369
370 // Write the value out now...
371 writeOperand(CPV, false, false);
372
373 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
374 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
Chris Lattner7bfee412001-10-29 16:05:51 +0000375 Out << "\t\t; <";
376 printType(CPV->getType()) << ">:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000377 if (Slot >= 0) Out << Slot;
378 else Out << "<badref>";
379 }
380
381 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000382}
383
Chris Lattner7bfee412001-10-29 16:05:51 +0000384// printMethod - Print all aspects of a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000385//
Chris Lattner7bfee412001-10-29 16:05:51 +0000386void AssemblyWriter::printMethod(const Method *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000387 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000388 Out << "\n" << (M->isExternal() ? "declare " : "")
389 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner7bfee412001-10-29 16:05:51 +0000390 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000391 Table.incorporateMethod(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000392
Chris Lattner7bfee412001-10-29 16:05:51 +0000393 // Loop over the arguments, printing them...
Chris Lattner862e3382001-10-13 06:42:36 +0000394 const MethodType *MT = cast<const MethodType>(M->getMethodType());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000395
Chris Lattner862e3382001-10-13 06:42:36 +0000396 if (!M->isExternal()) {
397 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000398 bind_obj(this, &AssemblyWriter::printMethodArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000399 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000400 // Loop over the arguments, printing them...
Chris Lattner862e3382001-10-13 06:42:36 +0000401 const MethodType *MT = cast<const MethodType>(M->getMethodType());
402 for (MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
403 E = MT->getParamTypes().end(); I != E; ++I) {
404 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000405 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000406 }
407 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000408
409 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000410 if (MT->isVarArg()) {
411 if (MT->getParamTypes().size()) Out << ", ";
412 Out << "..."; // Output varargs portion of signature!
413 }
414 Out << ")\n";
415
416 if (!M->isExternal()) {
417 // Loop over the symbol table, emitting all named constants...
418 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000419 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000420
421 Out << "begin";
422
423 // Output all of its basic blocks... for the method
424 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000425 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000426
Chris Lattnera7620d92001-07-15 06:35:59 +0000427 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000428 }
429
430 Table.purgeMethod();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000431}
432
Chris Lattner7bfee412001-10-29 16:05:51 +0000433// printMethodArgument - This member is called for every argument that
Chris Lattner2f7c9632001-06-06 20:29:01 +0000434// is passed into the method. Simply print it out
435//
Chris Lattner7bfee412001-10-29 16:05:51 +0000436void AssemblyWriter::printMethodArgument(const MethodArgument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000437 // Insert commas as we go... the first arg doesn't get a comma
438 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
439
440 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000441 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000442
443 // Output name, if available...
444 if (Arg->hasName())
445 Out << " %" << Arg->getName();
446 else if (Table.getValSlot(Arg) < 0)
447 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000448}
449
Chris Lattner7bfee412001-10-29 16:05:51 +0000450// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000451//
Chris Lattner7bfee412001-10-29 16:05:51 +0000452void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000453 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000454 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000455 } else {
456 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000457 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000458 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000459 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000460 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000461 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000462 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000463 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000464
Chris Lattnerfee714f2001-09-07 16:36:04 +0000465 // Output all of the instructions in the basic block...
466 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000467 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000468}
469
Chris Lattner862e3382001-10-13 06:42:36 +0000470
471// printInfoComment - Print a little comment after the instruction indicating
472// which slot it occupies.
473//
474void AssemblyWriter::printInfoComment(const Value *V) {
475 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000476 Out << "\t\t; <";
477 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000478
479 if (!V->hasName()) {
480 int Slot = Table.getValSlot(V); // Print out the def slot taken...
481 if (Slot >= 0) Out << ":" << Slot;
482 else Out << ":<badref>";
483 }
484 Out << "\t[#uses=" << V->use_size() << "]"; // Output # uses
485 }
486}
487
Chris Lattner7bfee412001-10-29 16:05:51 +0000488// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000489//
Chris Lattner7bfee412001-10-29 16:05:51 +0000490void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000491 Out << "\t";
492
493 // Print out name if it exists...
494 if (I && I->hasName())
495 Out << "%" << I->getName() << " = ";
496
497 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000498 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000499
500 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000501 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000502
503 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000504 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000505 writeOperand(I->getOperand(2), true);
506 Out << ",";
507 writeOperand(Operand, true);
508 Out << ",";
509 writeOperand(I->getOperand(1), true);
510
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000511 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000512 // Special case switch statement to get formatting nice and correct...
513 writeOperand(Operand , true); Out << ",";
514 writeOperand(I->getOperand(1), true); Out << " [";
515
Chris Lattnera073acb2001-07-07 08:36:50 +0000516 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000517 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000518 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000519 writeOperand(I->getOperand(op+1), true);
520 }
521 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000522 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000523 Out << " ";
Chris Lattner29644b32001-11-06 01:48:45 +0000524 printType(I->getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000525 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000526
Chris Lattner29644b32001-11-06 01:48:45 +0000527 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
528 if (op) Out << ", ";
529 Out << "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000530 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000531 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000532 }
Chris Lattner862e3382001-10-13 06:42:36 +0000533 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000534 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000535 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000536 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
537 const MethodType *MTy = PTy ? dyn_cast<MethodType>(PTy->getValueType()) :0;
538 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
539
540 // If possible, print out the short form of the call instruction, but we can
541 // only do this if the first argument is a pointer to a nonvararg method,
542 // and if the value returned is not a pointer to a method.
543 //
544 if (RetTy && !MTy->isVarArg() &&
545 (!isa<PointerType>(RetTy)||!isa<MethodType>(cast<PointerType>(RetTy)))){
546 Out << " "; printType(RetTy);
547 writeOperand(Operand, false);
548 } else {
549 writeOperand(Operand, true);
550 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000551 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000552 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
553 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000554 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000555 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000556 }
557
558 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000559 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
560 // TODO: Should try to print out short form of the Invoke instruction
561 writeOperand(Operand, true);
562 Out << "(";
563 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
564 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
565 Out << ",";
566 writeOperand(I->getOperand(op), true);
567 }
568
569 Out << " )\n\t\t\tto";
570 writeOperand(II->getNormalDest(), true);
571 Out << " except";
572 writeOperand(II->getExceptionalDest(), true);
573
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000574 } else if (I->getOpcode() == Instruction::Malloc ||
575 I->getOpcode() == Instruction::Alloca) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000576 Out << " ";
577 printType(cast<const PointerType>(I->getType())->getValueType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000578 if (I->getNumOperands()) {
579 Out << ",";
580 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000581 }
Chris Lattner862e3382001-10-13 06:42:36 +0000582 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000583 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000584 Out << " to ";
585 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000586 } else if (Operand) { // Print the normal way...
587
588 // PrintAllTypes - Instructions who have operands of all the same type
589 // omit the type from all but the first operand. If the instruction has
590 // different type operands (for example br), then they are all printed.
591 bool PrintAllTypes = false;
592 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000593
Chris Lattnera073acb2001-07-07 08:36:50 +0000594 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
595 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596 if (Operand->getType() != TheType) {
597 PrintAllTypes = true; // We have differing types! Print them all!
598 break;
599 }
600 }
601
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000602 // Shift Left & Right print both types even for Ubyte LHS
603 if (isa<ShiftInst>(I)) PrintAllTypes = true;
604
Chris Lattner7bfee412001-10-29 16:05:51 +0000605 if (!PrintAllTypes) {
606 Out << " ";
607 printType(I->getOperand(0)->getType());
608 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000609
Chris Lattnera073acb2001-07-07 08:36:50 +0000610 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000611 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000612 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000613 }
614 }
615
Chris Lattner862e3382001-10-13 06:42:36 +0000616 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000617 Out << endl;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000618}
619
620
Chris Lattner7bfee412001-10-29 16:05:51 +0000621// printType - Go to extreme measures to attempt to print out a short, symbolic
622// version of a type name.
623//
624ostream &AssemblyWriter::printType(const Type *Ty) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000625 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner7bfee412001-10-29 16:05:51 +0000626}
627
628
Chris Lattner2f7c9632001-06-06 20:29:01 +0000629//===----------------------------------------------------------------------===//
630// External Interface declarations
631//===----------------------------------------------------------------------===//
632
633
634
635void WriteToAssembly(const Module *M, ostream &o) {
636 if (M == 0) { o << "<null> module\n"; return; }
637 SlotCalculator SlotTable(M, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000638 AssemblyWriter W(o, SlotTable, M);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000639
640 W.write(M);
641}
642
Chris Lattneradfe0d12001-09-10 20:08:19 +0000643void WriteToAssembly(const GlobalVariable *G, ostream &o) {
644 if (G == 0) { o << "<null> global variable\n"; return; }
645 SlotCalculator SlotTable(G->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000646 AssemblyWriter W(o, SlotTable, G->getParent());
Chris Lattneradfe0d12001-09-10 20:08:19 +0000647 W.write(G);
648}
649
Chris Lattner2f7c9632001-06-06 20:29:01 +0000650void WriteToAssembly(const Method *M, ostream &o) {
651 if (M == 0) { o << "<null> method\n"; return; }
652 SlotCalculator SlotTable(M->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000653 AssemblyWriter W(o, SlotTable, M->getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000654
655 W.write(M);
656}
657
658
659void WriteToAssembly(const BasicBlock *BB, ostream &o) {
660 if (BB == 0) { o << "<null> basic block\n"; return; }
661
662 SlotCalculator SlotTable(BB->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000663 AssemblyWriter W(o, SlotTable,
664 BB->getParent() ? BB->getParent()->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000665
666 W.write(BB);
667}
668
669void WriteToAssembly(const ConstPoolVal *CPV, ostream &o) {
670 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +0000671 o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000672}
673
674void WriteToAssembly(const Instruction *I, ostream &o) {
675 if (I == 0) { o << "<null> instruction\n"; return; }
676
Chris Lattner7bfee412001-10-29 16:05:51 +0000677 const Method *M = I->getParent() ? I->getParent()->getParent() : 0;
678 SlotCalculator SlotTable(M, true);
679 AssemblyWriter W(o, SlotTable, M ? M->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000680
681 W.write(I);
682}
Chris Lattner7db79582001-11-07 04:21:57 +0000683
684void CachedWriter::setModule(const Module *M) {
685 delete SC; delete AW;
686 if (M) {
687 SC = new SlotCalculator(M, true);
688 AW = new AssemblyWriter(Out, *SC, M);
689 } else {
690 SC = 0; AW = 0;
691 }
692}
693
694CachedWriter::~CachedWriter() {
695 delete AW;
696 delete SC;
697}
698
699CachedWriter &CachedWriter::operator<<(const Value *V) {
700 assert(AW && SC && "CachedWriter does not have a current module!");
701 switch (V->getValueType()) {
702 case Value::ConstantVal:
703 Out << " "; AW->write(V->getType());
704 Out << " " << cast<ConstPoolVal>(V)->getStrValue(); break;
705 case Value::MethodArgumentVal:
706 AW->write(V->getType()); Out << " " << V->getName(); break;
707 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
708 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
709 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
710 case Value::MethodVal: AW->write(cast<Method>(V)); break;
711 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
712 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
713 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
714 }
715 return *this;
716}