blob: e5f9114c8e1f7ece9b26ecf80cafb8393635ff0c [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
3// This library implements the functionality defined in llvm/Assembly/Writer.h
4//
Chris Lattner2f7c9632001-06-06 20:29:01 +00005// TODO: print out the type name instead of the full type if a particular type
6// is in the symbol table...
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner7db79582001-11-07 04:21:57 +000010#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000011#include "llvm/Assembly/Writer.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000012#include "llvm/SlotCalculator.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000013#include "llvm/Module.h"
Chris Lattner57698e22002-03-26 18:01:55 +000014#include "llvm/Function.h"
Chris Lattnerda975502001-09-10 07:58:01 +000015#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/BasicBlock.h"
Chris Lattner3462ae32001-12-03 22:26:30 +000017#include "llvm/ConstantVals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000019#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000020#include "llvm/iPHINode.h"
21#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000022#include "llvm/SymbolTable.h"
Chris Lattner5de22042001-11-27 00:03:19 +000023#include "Support/StringExtras.h"
24#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000025#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000026#include <map>
Chris Lattner7f74a562002-01-20 22:54:45 +000027using std::string;
28using std::map;
29using std::vector;
30using std::ostream;
Chris Lattner7bfee412001-10-29 16:05:51 +000031
Chris Lattnerb86620e2001-10-29 16:37:48 +000032static const Module *getModuleFromVal(const Value *V) {
Chris Lattner57698e22002-03-26 18:01:55 +000033 if (const FunctionArgument *MA = dyn_cast<const FunctionArgument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000034 return MA->getParent() ? MA->getParent()->getParent() : 0;
35 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
36 return BB->getParent() ? BB->getParent()->getParent() : 0;
37 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000038 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000039 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000040 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000041 return GV->getParent();
42 else if (const Module *Mod = dyn_cast<const Module>(V))
43 return Mod;
44 return 0;
45}
46
Chris Lattner7bfee412001-10-29 16:05:51 +000047static SlotCalculator *createSlotCalculator(const Value *V) {
48 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner57698e22002-03-26 18:01:55 +000049 if (const FunctionArgument *FA = dyn_cast<const FunctionArgument>(V)) {
50 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000051 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
52 return new SlotCalculator(I->getParent()->getParent(), true);
53 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
54 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000055 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000056 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000057 } else if (const Function *Func = dyn_cast<const Function>(V)) {
58 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000059 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
60 return new SlotCalculator(Mod, true);
61 }
62 return 0;
63}
Chris Lattner2f7c9632001-06-06 20:29:01 +000064
Chris Lattner5e5abe32001-07-20 19:15:21 +000065// WriteAsOperand - Write the name of the specified value out to the specified
66// ostream. This can be useful when you just want to print int %reg126, not the
67// whole instruction that generated it.
68//
Chris Lattnerb86620e2001-10-29 16:37:48 +000069static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
70 SlotCalculator *Table) {
Chris Lattnerfee714f2001-09-07 16:36:04 +000071 if (PrintName && V->hasName()) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000072 Out << " %" << V->getName();
73 } else {
Chris Lattner3462ae32001-12-03 22:26:30 +000074 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000075 Out << " " << CPV->getStrValue();
76 } else {
77 int Slot;
78 if (Table) {
79 Slot = Table->getValSlot(V);
80 } else {
Chris Lattnerb86620e2001-10-29 16:37:48 +000081 if (const Type *Ty = dyn_cast<const Type>(V)) {
82 Out << " " << Ty->getDescription();
83 return;
84 }
Chris Lattner7bfee412001-10-29 16:05:51 +000085
86 Table = createSlotCalculator(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +000087 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +000088
Chris Lattner5e5abe32001-07-20 19:15:21 +000089 Slot = Table->getValSlot(V);
90 delete Table;
91 }
92 if (Slot >= 0) Out << " %" << Slot;
93 else if (PrintName)
94 Out << "<badref>"; // Not embeded into a location?
95 }
96 }
Chris Lattnerb86620e2001-10-29 16:37:48 +000097}
98
99
100// If the module has a symbol table, take all global types and stuff their
101// names into the TypeNames map.
102//
103static void fillTypeNameTable(const Module *M,
104 map<const Type *, string> &TypeNames) {
105 if (M && M->hasSymbolTable()) {
106 const SymbolTable *ST = M->getSymbolTable();
107 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
108 if (PI != ST->end()) {
109 SymbolTable::type_const_iterator I = PI->second.begin();
110 for (; I != PI->second.end(); ++I) {
111 // As a heuristic, don't insert pointer to primitive types, because
112 // they are used too often to have a single useful name.
113 //
114 const Type *Ty = cast<const Type>(I->second);
115 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +0000116 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +0000117 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000118 }
119 }
120 }
121}
122
123
124
125static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
126 map<const Type *, string> &TypeNames) {
127 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
128
129 // Check to see if the type is named.
130 map<const Type *, string>::iterator I = TypeNames.find(Ty);
131 if (I != TypeNames.end()) return I->second;
132
133 // Check to see if the Type is already on the stack...
134 unsigned Slot = 0, CurSize = TypeStack.size();
135 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
136
137 // This is another base case for the recursion. In this case, we know
138 // that we have looped back to a type that we have previously visited.
139 // Generate the appropriate upreference to handle this.
140 //
141 if (Slot < CurSize)
142 return "\\" + utostr(CurSize-Slot); // Here's the upreference
143
144 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
145
146 string Result;
147 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000148 case Type::FunctionTyID: {
149 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000150 Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000151 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerb86620e2001-10-29 16:37:48 +0000152 I = MTy->getParamTypes().begin(),
153 E = MTy->getParamTypes().end(); I != E; ++I) {
154 if (I != MTy->getParamTypes().begin())
155 Result += ", ";
156 Result += calcTypeName(*I, TypeStack, TypeNames);
157 }
158 if (MTy->isVarArg()) {
159 if (!MTy->getParamTypes().empty()) Result += ", ";
160 Result += "...";
161 }
162 Result += ")";
163 break;
164 }
165 case Type::StructTyID: {
166 const StructType *STy = cast<const StructType>(Ty);
167 Result = "{ ";
168 for (StructType::ElementTypes::const_iterator
169 I = STy->getElementTypes().begin(),
170 E = STy->getElementTypes().end(); I != E; ++I) {
171 if (I != STy->getElementTypes().begin())
172 Result += ", ";
173 Result += calcTypeName(*I, TypeStack, TypeNames);
174 }
175 Result += " }";
176 break;
177 }
178 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000179 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattnerb86620e2001-10-29 16:37:48 +0000180 TypeStack, TypeNames) + " *";
181 break;
182 case Type::ArrayTyID: {
183 const ArrayType *ATy = cast<const ArrayType>(Ty);
184 int NumElements = ATy->getNumElements();
185 Result = "[";
186 if (NumElements != -1) Result += itostr(NumElements) + " x ";
187 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
188 break;
189 }
190 default:
191 assert(0 && "Unhandled case in getTypeProps!");
192 Result = "<error>";
193 }
194
195 TypeStack.pop_back(); // Remove self from stack...
196 return Result;
197}
198
199
200// printTypeInt - The internal guts of printing out a type that has a
201// potentially named portion.
202//
203static ostream &printTypeInt(ostream &Out, const Type *Ty,
204 map<const Type *, string> &TypeNames) {
205 // Primitive types always print out their description, regardless of whether
206 // they have been named or not.
207 //
208 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
209
210 // Check to see if the type is named.
211 map<const Type *, string>::iterator I = TypeNames.find(Ty);
212 if (I != TypeNames.end()) return Out << I->second;
213
214 // Otherwise we have a type that has not been named but is a derived type.
215 // Carefully recurse the type hierarchy to print out any contained symbolic
216 // names.
217 //
218 vector<const Type *> TypeStack;
219 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000220 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000221 return Out << TypeName;
222}
223
Chris Lattner34b95182001-10-31 04:33:19 +0000224
Chris Lattnerb86620e2001-10-29 16:37:48 +0000225// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
226// type, iff there is an entry in the modules symbol table for the specified
227// type or one of it's component types. This is slower than a simple x << Type;
228//
229ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
230 Out << " ";
231
232 // If they want us to print out a type, attempt to make it symbolic if there
233 // is a symbol table in the module...
234 if (M && M->hasSymbolTable()) {
235 map<const Type *, string> TypeNames;
236 fillTypeNameTable(M, TypeNames);
237
Chris Lattner72f866e2001-10-29 16:40:32 +0000238 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000239 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000240 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000241 }
242}
243
244
245// WriteAsOperand - Write the name of the specified value out to the specified
246// ostream. This can be useful when you just want to print int %reg126, not the
247// whole instruction that generated it.
248//
249ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
250 bool PrintName, SlotCalculator *Table) {
Chris Lattner72f866e2001-10-29 16:40:32 +0000251 if (PrintType)
252 WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000253
254 WriteAsOperandInternal(Out, V, PrintName, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000255 return Out;
256}
257
258
Chris Lattner2e9fee42001-07-12 23:35:26 +0000259
Chris Lattnerfee714f2001-09-07 16:36:04 +0000260class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000261 ostream &Out;
262 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000263 const Module *TheModule;
264 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000265public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000266 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
267 : Out(o), Table(Tab), TheModule(M) {
268
269 // If the module has a symbol table, take all global types and stuff their
270 // names into the TypeNames map.
271 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000272 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273 }
274
Chris Lattner7bfee412001-10-29 16:05:51 +0000275 inline void write(const Module *M) { printModule(M); }
276 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000277 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000278 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
279 inline void write(const Instruction *I) { printInstruction(I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000280 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000281 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000282
Chris Lattner2f7c9632001-06-06 20:29:01 +0000283private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000284 void printModule(const Module *M);
285 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000286 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000287 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000288 void printFunction(const Function *F);
289 void printFunctionArgument(const FunctionArgument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000290 void printBasicBlock(const BasicBlock *BB);
291 void printInstruction(const Instruction *I);
292 ostream &printType(const Type *Ty);
293
Chris Lattner2f7c9632001-06-06 20:29:01 +0000294 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
Chris Lattner862e3382001-10-13 06:42:36 +0000295
296 // printInfoComment - Print a little comment after the instruction indicating
297 // which slot it occupies.
298 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299};
300
301
Chris Lattnerfee714f2001-09-07 16:36:04 +0000302void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
303 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000304 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000305 WriteAsOperandInternal(Out, Operand, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000306}
307
Chris Lattner2f7c9632001-06-06 20:29:01 +0000308
Chris Lattner7bfee412001-10-29 16:05:51 +0000309void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000310 // Loop over the symbol table, emitting all named constants...
311 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000312 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000313
314 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000315 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000316
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000317 Out << "implementation\n";
318
Chris Lattner6915f8f2002-04-07 22:49:37 +0000319 // Output all of the functions...
Chris Lattner57698e22002-03-26 18:01:55 +0000320 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000321}
322
Chris Lattner7bfee412001-10-29 16:05:51 +0000323void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000324 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000325
Chris Lattner841d8b92001-11-26 18:54:16 +0000326 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000327 if (!GV->hasInitializer()) Out << "uninitialized ";
328
Chris Lattner7bfee412001-10-29 16:05:51 +0000329 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000330 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000331
332 if (GV->hasInitializer())
333 writeOperand(GV->getInitializer(), false, false);
334
Chris Lattner862e3382001-10-13 06:42:36 +0000335 printInfoComment(GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000336 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000337}
338
Chris Lattnerfee714f2001-09-07 16:36:04 +0000339
Chris Lattner7bfee412001-10-29 16:05:51 +0000340// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000341// if a named constant is found, emit it's declaration...
342//
Chris Lattner7bfee412001-10-29 16:05:51 +0000343void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000344 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
345 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
346 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
347
348 for (; I != End; ++I) {
349 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000350 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000351 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000352 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000353 Out << "\t%" << I->first << " = type " << Ty->getDescription() << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000354 }
355 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000356 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000357}
358
359
Chris Lattner7bfee412001-10-29 16:05:51 +0000360// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000361//
Chris Lattner3462ae32001-12-03 22:26:30 +0000362void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000363 // Don't print out unnamed constants, they will be inlined
364 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000365
Chris Lattneree998be2001-07-26 16:29:38 +0000366 // Print out name...
367 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000368
Chris Lattneree998be2001-07-26 16:29:38 +0000369 // Print out the constant type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000370 printType(CPV->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000371
372 // Write the value out now...
373 writeOperand(CPV, false, false);
374
375 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
376 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
Chris Lattner7bfee412001-10-29 16:05:51 +0000377 Out << "\t\t; <";
378 printType(CPV->getType()) << ">:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000379 if (Slot >= 0) Out << Slot;
380 else Out << "<badref>";
381 }
382
Chris Lattner7f74a562002-01-20 22:54:45 +0000383 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000384}
385
Chris Lattner6915f8f2002-04-07 22:49:37 +0000386// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000387//
Chris Lattner57698e22002-03-26 18:01:55 +0000388void AssemblyWriter::printFunction(const Function *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000389 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000390 Out << "\n" << (M->isExternal() ? "declare " : "")
391 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner7bfee412001-10-29 16:05:51 +0000392 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner6915f8f2002-04-07 22:49:37 +0000393 Table.incorporateFunction(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000394
Chris Lattner7bfee412001-10-29 16:05:51 +0000395 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000396 const FunctionType *MT = M->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000397
Chris Lattner862e3382001-10-13 06:42:36 +0000398 if (!M->isExternal()) {
399 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner57698e22002-03-26 18:01:55 +0000400 bind_obj(this, &AssemblyWriter::printFunctionArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000401 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000402 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000403 const FunctionType *MT = M->getFunctionType();
404 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
Chris Lattner862e3382001-10-13 06:42:36 +0000405 E = MT->getParamTypes().end(); I != E; ++I) {
406 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000407 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000408 }
409 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000410
411 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000412 if (MT->isVarArg()) {
413 if (MT->getParamTypes().size()) Out << ", ";
414 Out << "..."; // Output varargs portion of signature!
415 }
416 Out << ")\n";
417
418 if (!M->isExternal()) {
419 // Loop over the symbol table, emitting all named constants...
420 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000421 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000422
423 Out << "begin";
424
Chris Lattner6915f8f2002-04-07 22:49:37 +0000425 // Output all of its basic blocks... for the function
Chris Lattnerfee714f2001-09-07 16:36:04 +0000426 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000427 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000428
Chris Lattnera7620d92001-07-15 06:35:59 +0000429 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000430 }
431
Chris Lattner6915f8f2002-04-07 22:49:37 +0000432 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000433}
434
Chris Lattner57698e22002-03-26 18:01:55 +0000435// printFunctionArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000436// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000437//
Chris Lattner57698e22002-03-26 18:01:55 +0000438void AssemblyWriter::printFunctionArgument(const FunctionArgument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000439 // Insert commas as we go... the first arg doesn't get a comma
440 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
441
442 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000443 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444
445 // Output name, if available...
446 if (Arg->hasName())
447 Out << " %" << Arg->getName();
448 else if (Table.getValSlot(Arg) < 0)
449 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000450}
451
Chris Lattner7bfee412001-10-29 16:05:51 +0000452// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000453//
Chris Lattner7bfee412001-10-29 16:05:51 +0000454void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000455 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000456 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000457 } else {
458 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000459 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000460 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000461 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000462 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000463 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000464 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000465 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000466
Chris Lattnerfee714f2001-09-07 16:36:04 +0000467 // Output all of the instructions in the basic block...
468 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000469 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000470}
471
Chris Lattner862e3382001-10-13 06:42:36 +0000472
473// printInfoComment - Print a little comment after the instruction indicating
474// which slot it occupies.
475//
476void AssemblyWriter::printInfoComment(const Value *V) {
477 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000478 Out << "\t\t; <";
479 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000480
481 if (!V->hasName()) {
482 int Slot = Table.getValSlot(V); // Print out the def slot taken...
483 if (Slot >= 0) Out << ":" << Slot;
484 else Out << ":<badref>";
485 }
Chris Lattner3cb3a1f2001-12-14 16:29:12 +0000486 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000487 }
488}
489
Chris Lattner7bfee412001-10-29 16:05:51 +0000490// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000491//
Chris Lattner7bfee412001-10-29 16:05:51 +0000492void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000493 Out << "\t";
494
495 // Print out name if it exists...
496 if (I && I->hasName())
497 Out << "%" << I->getName() << " = ";
498
499 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000500 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000501
502 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000503 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000504
505 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000506 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000507 writeOperand(I->getOperand(2), true);
508 Out << ",";
509 writeOperand(Operand, true);
510 Out << ",";
511 writeOperand(I->getOperand(1), true);
512
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000513 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000514 // Special case switch statement to get formatting nice and correct...
515 writeOperand(Operand , true); Out << ",";
516 writeOperand(I->getOperand(1), true); Out << " [";
517
Chris Lattnera073acb2001-07-07 08:36:50 +0000518 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000519 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000520 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000521 writeOperand(I->getOperand(op+1), true);
522 }
523 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000524 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000525 Out << " ";
Chris Lattner29644b32001-11-06 01:48:45 +0000526 printType(I->getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000527 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000528
Chris Lattner29644b32001-11-06 01:48:45 +0000529 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
530 if (op) Out << ", ";
531 Out << "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000532 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000533 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000534 }
Chris Lattner862e3382001-10-13 06:42:36 +0000535 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000536 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000537 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000538 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000539 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000540 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
541
542 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000543 // only do this if the first argument is a pointer to a nonvararg function,
544 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000545 //
546 if (RetTy && !MTy->isVarArg() &&
Chris Lattner91db5822002-03-29 03:44:36 +0000547 (!isa<PointerType>(RetTy)||!isa<FunctionType>(cast<PointerType>(RetTy)))){
Chris Lattner2f2d9472001-11-06 21:28:12 +0000548 Out << " "; printType(RetTy);
549 writeOperand(Operand, false);
550 } else {
551 writeOperand(Operand, true);
552 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000553 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000554 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
555 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000556 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000557 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000558 }
559
560 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000561 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
562 // TODO: Should try to print out short form of the Invoke instruction
563 writeOperand(Operand, true);
564 Out << "(";
565 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
566 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
567 Out << ",";
568 writeOperand(I->getOperand(op), true);
569 }
570
571 Out << " )\n\t\t\tto";
572 writeOperand(II->getNormalDest(), true);
573 Out << " except";
574 writeOperand(II->getExceptionalDest(), true);
575
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000576 } else if (I->getOpcode() == Instruction::Malloc ||
577 I->getOpcode() == Instruction::Alloca) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000578 Out << " ";
Chris Lattner2413b162001-12-04 00:03:30 +0000579 printType(cast<const PointerType>(I->getType())->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000580 if (I->getNumOperands()) {
581 Out << ",";
582 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000583 }
Chris Lattner862e3382001-10-13 06:42:36 +0000584 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000585 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000586 Out << " to ";
587 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588 } else if (Operand) { // Print the normal way...
589
590 // PrintAllTypes - Instructions who have operands of all the same type
591 // omit the type from all but the first operand. If the instruction has
592 // different type operands (for example br), then they are all printed.
593 bool PrintAllTypes = false;
594 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595
Chris Lattnera073acb2001-07-07 08:36:50 +0000596 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
597 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000598 if (Operand->getType() != TheType) {
599 PrintAllTypes = true; // We have differing types! Print them all!
600 break;
601 }
602 }
603
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000604 // Shift Left & Right print both types even for Ubyte LHS
605 if (isa<ShiftInst>(I)) PrintAllTypes = true;
606
Chris Lattner7bfee412001-10-29 16:05:51 +0000607 if (!PrintAllTypes) {
608 Out << " ";
609 printType(I->getOperand(0)->getType());
610 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000611
Chris Lattnera073acb2001-07-07 08:36:50 +0000612 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000613 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000614 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000615 }
616 }
617
Chris Lattner862e3382001-10-13 06:42:36 +0000618 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000619 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000620}
621
622
Chris Lattner7bfee412001-10-29 16:05:51 +0000623// printType - Go to extreme measures to attempt to print out a short, symbolic
624// version of a type name.
625//
626ostream &AssemblyWriter::printType(const Type *Ty) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000627 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner7bfee412001-10-29 16:05:51 +0000628}
629
630
Chris Lattner2f7c9632001-06-06 20:29:01 +0000631//===----------------------------------------------------------------------===//
632// External Interface declarations
633//===----------------------------------------------------------------------===//
634
635
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000636void Module::print(std::ostream &o) const {
637 SlotCalculator SlotTable(this, true);
638 AssemblyWriter W(o, SlotTable, this);
639 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000640}
641
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000642void GlobalVariable::print(std::ostream &o) const {
643 SlotCalculator SlotTable(getParent(), true);
644 AssemblyWriter W(o, SlotTable, getParent());
645 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000646}
647
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000648void Function::print(std::ostream &o) const {
649 SlotCalculator SlotTable(getParent(), true);
650 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000651
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000652 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000653}
654
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000655void BasicBlock::print(std::ostream &o) const {
656 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000657 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000658 getParent() ? getParent()->getParent() : 0);
659 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660}
661
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000662void Instruction::print(std::ostream &o) const {
663 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000664 SlotCalculator SlotTable(F, true);
665 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000666
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000667 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000668}
Chris Lattner7db79582001-11-07 04:21:57 +0000669
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000670void Constant::print(std::ostream &o) const {
671 if (this == 0) { o << "<null> constant value\n"; return; }
672 o << " " << getType()->getDescription() << " " << getStrValue();
673}
674
675void Type::print(std::ostream &o) const {
676 if (this == 0)
677 o << "<null Type>";
678 else
679 o << getDescription();
680}
681
682void FunctionArgument::print(std::ostream &o) const {
683 o << getType() << " " << getName();
684}
685
686void Value::dump() const { print(std::cerr); }
687
688//===----------------------------------------------------------------------===//
689// CachedWriter Class Implementation
690//===----------------------------------------------------------------------===//
691
Chris Lattner7db79582001-11-07 04:21:57 +0000692void CachedWriter::setModule(const Module *M) {
693 delete SC; delete AW;
694 if (M) {
695 SC = new SlotCalculator(M, true);
696 AW = new AssemblyWriter(Out, *SC, M);
697 } else {
698 SC = 0; AW = 0;
699 }
700}
701
702CachedWriter::~CachedWriter() {
703 delete AW;
704 delete SC;
705}
706
707CachedWriter &CachedWriter::operator<<(const Value *V) {
708 assert(AW && SC && "CachedWriter does not have a current module!");
709 switch (V->getValueType()) {
710 case Value::ConstantVal:
711 Out << " "; AW->write(V->getType());
Chris Lattner3462ae32001-12-03 22:26:30 +0000712 Out << " " << cast<Constant>(V)->getStrValue(); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000713 case Value::FunctionArgumentVal:
Chris Lattner7db79582001-11-07 04:21:57 +0000714 AW->write(V->getType()); Out << " " << V->getName(); break;
715 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
716 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
717 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000718 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000719 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
720 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
721 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
722 }
723 return *this;
724}