blob: 2e5f75d0b4b8ab0a19957849bf2cd8f9bfe179bc [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 Lattner189088e2002-04-12 18:21:53 +00005// Note that these routines must be extremely tolerant of various errors in the
6// LLVM code, because of of the primary uses of it is for debugging
7// transformations.
8//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009// TODO: print out the type name instead of the full type if a particular type
Chris Lattner189088e2002-04-12 18:21:53 +000010// is in the symbol table...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner7db79582001-11-07 04:21:57 +000014#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000015#include "llvm/Assembly/Writer.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000016#include "llvm/SlotCalculator.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/Module.h"
Chris Lattner57698e22002-03-26 18:01:55 +000018#include "llvm/Function.h"
Chris Lattnerda975502001-09-10 07:58:01 +000019#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/BasicBlock.h"
Chris Lattner3462ae32001-12-03 22:26:30 +000021#include "llvm/ConstantVals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000023#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000024#include "llvm/iPHINode.h"
25#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000026#include "llvm/SymbolTable.h"
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000027#include "llvm/Argument.h"
Chris Lattner5de22042001-11-27 00:03:19 +000028#include "Support/StringExtras.h"
29#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000030#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000031#include <map>
Chris Lattner7f74a562002-01-20 22:54:45 +000032using std::string;
33using std::map;
34using std::vector;
35using std::ostream;
Chris Lattner7bfee412001-10-29 16:05:51 +000036
Chris Lattnerb86620e2001-10-29 16:37:48 +000037static const Module *getModuleFromVal(const Value *V) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000038 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000039 return MA->getParent() ? MA->getParent()->getParent() : 0;
40 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
41 return BB->getParent() ? BB->getParent()->getParent() : 0;
42 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000043 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000044 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000045 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000046 return GV->getParent();
47 else if (const Module *Mod = dyn_cast<const Module>(V))
48 return Mod;
49 return 0;
50}
51
Chris Lattner7bfee412001-10-29 16:05:51 +000052static SlotCalculator *createSlotCalculator(const Value *V) {
53 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000054 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000055 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000056 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
57 return new SlotCalculator(I->getParent()->getParent(), true);
58 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
59 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000060 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000061 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000062 } else if (const Function *Func = dyn_cast<const Function>(V)) {
63 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000064 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
65 return new SlotCalculator(Mod, true);
66 }
67 return 0;
68}
Chris Lattner2f7c9632001-06-06 20:29:01 +000069
Chris Lattner5e5abe32001-07-20 19:15:21 +000070// WriteAsOperand - Write the name of the specified value out to the specified
71// ostream. This can be useful when you just want to print int %reg126, not the
72// whole instruction that generated it.
73//
Chris Lattnerb86620e2001-10-29 16:37:48 +000074static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
75 SlotCalculator *Table) {
Chris Lattnerfee714f2001-09-07 16:36:04 +000076 if (PrintName && V->hasName()) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000077 Out << " %" << V->getName();
78 } else {
Chris Lattner3462ae32001-12-03 22:26:30 +000079 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000080 Out << " " << CPV->getStrValue();
81 } else {
82 int Slot;
83 if (Table) {
84 Slot = Table->getValSlot(V);
85 } else {
Chris Lattnerb86620e2001-10-29 16:37:48 +000086 if (const Type *Ty = dyn_cast<const Type>(V)) {
87 Out << " " << Ty->getDescription();
88 return;
89 }
Chris Lattner7bfee412001-10-29 16:05:51 +000090
91 Table = createSlotCalculator(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +000092 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +000093
Chris Lattner5e5abe32001-07-20 19:15:21 +000094 Slot = Table->getValSlot(V);
95 delete Table;
96 }
97 if (Slot >= 0) Out << " %" << Slot;
98 else if (PrintName)
99 Out << "<badref>"; // Not embeded into a location?
100 }
101 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000102}
103
104
105// If the module has a symbol table, take all global types and stuff their
106// names into the TypeNames map.
107//
108static void fillTypeNameTable(const Module *M,
109 map<const Type *, string> &TypeNames) {
110 if (M && M->hasSymbolTable()) {
111 const SymbolTable *ST = M->getSymbolTable();
112 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
113 if (PI != ST->end()) {
114 SymbolTable::type_const_iterator I = PI->second.begin();
115 for (; I != PI->second.end(); ++I) {
116 // As a heuristic, don't insert pointer to primitive types, because
117 // they are used too often to have a single useful name.
118 //
119 const Type *Ty = cast<const Type>(I->second);
120 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +0000121 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +0000122 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000123 }
124 }
125 }
126}
127
128
129
130static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
131 map<const Type *, string> &TypeNames) {
132 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
133
134 // Check to see if the type is named.
135 map<const Type *, string>::iterator I = TypeNames.find(Ty);
136 if (I != TypeNames.end()) return I->second;
137
138 // Check to see if the Type is already on the stack...
139 unsigned Slot = 0, CurSize = TypeStack.size();
140 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
141
142 // This is another base case for the recursion. In this case, we know
143 // that we have looped back to a type that we have previously visited.
144 // Generate the appropriate upreference to handle this.
145 //
146 if (Slot < CurSize)
147 return "\\" + utostr(CurSize-Slot); // Here's the upreference
148
149 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
150
151 string Result;
152 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000153 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000154 const FunctionType *FTy = cast<const FunctionType>(Ty);
155 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000156 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000157 I = FTy->getParamTypes().begin(),
158 E = FTy->getParamTypes().end(); I != E; ++I) {
159 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000160 Result += ", ";
161 Result += calcTypeName(*I, TypeStack, TypeNames);
162 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000163 if (FTy->isVarArg()) {
164 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000165 Result += "...";
166 }
167 Result += ")";
168 break;
169 }
170 case Type::StructTyID: {
171 const StructType *STy = cast<const StructType>(Ty);
172 Result = "{ ";
173 for (StructType::ElementTypes::const_iterator
174 I = STy->getElementTypes().begin(),
175 E = STy->getElementTypes().end(); I != E; ++I) {
176 if (I != STy->getElementTypes().begin())
177 Result += ", ";
178 Result += calcTypeName(*I, TypeStack, TypeNames);
179 }
180 Result += " }";
181 break;
182 }
183 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000184 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000185 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000186 break;
187 case Type::ArrayTyID: {
188 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000189 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000190 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
191 break;
192 }
193 default:
194 assert(0 && "Unhandled case in getTypeProps!");
195 Result = "<error>";
196 }
197
198 TypeStack.pop_back(); // Remove self from stack...
199 return Result;
200}
201
202
203// printTypeInt - The internal guts of printing out a type that has a
204// potentially named portion.
205//
206static ostream &printTypeInt(ostream &Out, const Type *Ty,
207 map<const Type *, string> &TypeNames) {
208 // Primitive types always print out their description, regardless of whether
209 // they have been named or not.
210 //
211 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
212
213 // Check to see if the type is named.
214 map<const Type *, string>::iterator I = TypeNames.find(Ty);
215 if (I != TypeNames.end()) return Out << I->second;
216
217 // Otherwise we have a type that has not been named but is a derived type.
218 // Carefully recurse the type hierarchy to print out any contained symbolic
219 // names.
220 //
221 vector<const Type *> TypeStack;
222 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000223 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000224 return Out << TypeName;
225}
226
Chris Lattner34b95182001-10-31 04:33:19 +0000227
Chris Lattnerb86620e2001-10-29 16:37:48 +0000228// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
229// type, iff there is an entry in the modules symbol table for the specified
230// type or one of it's component types. This is slower than a simple x << Type;
231//
232ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
233 Out << " ";
234
235 // If they want us to print out a type, attempt to make it symbolic if there
236 // is a symbol table in the module...
237 if (M && M->hasSymbolTable()) {
238 map<const Type *, string> TypeNames;
239 fillTypeNameTable(M, TypeNames);
240
Chris Lattner72f866e2001-10-29 16:40:32 +0000241 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000242 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000243 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000244 }
245}
246
247
248// WriteAsOperand - Write the name of the specified value out to the specified
249// ostream. This can be useful when you just want to print int %reg126, not the
250// whole instruction that generated it.
251//
252ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
253 bool PrintName, SlotCalculator *Table) {
Chris Lattner72f866e2001-10-29 16:40:32 +0000254 if (PrintType)
255 WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000256
257 WriteAsOperandInternal(Out, V, PrintName, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000258 return Out;
259}
260
261
Chris Lattner2e9fee42001-07-12 23:35:26 +0000262
Chris Lattnerfee714f2001-09-07 16:36:04 +0000263class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000264 ostream &Out;
265 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000266 const Module *TheModule;
267 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000268public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000269 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
270 : Out(o), Table(Tab), TheModule(M) {
271
272 // If the module has a symbol table, take all global types and stuff their
273 // names into the TypeNames map.
274 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000275 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000276 }
277
Chris Lattner7bfee412001-10-29 16:05:51 +0000278 inline void write(const Module *M) { printModule(M); }
279 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000280 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000281 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
282 inline void write(const Instruction *I) { printInstruction(I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000283 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000284 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000285
Chris Lattner2f7c9632001-06-06 20:29:01 +0000286private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000287 void printModule(const Module *M);
288 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000289 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000290 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000291 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000292 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000293 void printBasicBlock(const BasicBlock *BB);
294 void printInstruction(const Instruction *I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000295
296 // printType - Go to extreme measures to attempt to print out a short,
297 // symbolic version of a type name.
298 //
299 ostream &printType(const Type *Ty) {
300 return printTypeInt(Out, Ty, TypeNames);
301 }
302
303 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
304 // without considering any symbolic types that we may have equal to it.
305 //
306 ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000307
Chris Lattner2f7c9632001-06-06 20:29:01 +0000308 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
Chris Lattner862e3382001-10-13 06:42:36 +0000309
310 // printInfoComment - Print a little comment after the instruction indicating
311 // which slot it occupies.
312 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313};
314
315
Chris Lattnerd816b532002-04-13 20:53:41 +0000316// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
317// without considering any symbolic types that we may have equal to it.
318//
319ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
320 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
321 printType(FTy->getReturnType()) << " (";
322 for (FunctionType::ParamTypes::const_iterator
323 I = FTy->getParamTypes().begin(),
324 E = FTy->getParamTypes().end(); I != E; ++I) {
325 if (I != FTy->getParamTypes().begin())
326 Out << ", ";
327 Out << printType(*I);
328 }
329 if (FTy->isVarArg()) {
330 if (!FTy->getParamTypes().empty()) Out << ", ";
331 Out << "...";
332 }
333 Out << ")";
334 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
335 Out << "{ ";
336 for (StructType::ElementTypes::const_iterator
337 I = STy->getElementTypes().begin(),
338 E = STy->getElementTypes().end(); I != E; ++I) {
339 if (I != STy->getElementTypes().begin())
340 Out << ", ";
341 printType(*I);
342 }
343 Out << " }";
344 } else if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
345 printType(PTy->getElementType()) << "*";
346 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
347 Out << "[" << ATy->getNumElements() << " x ";
348 printType(ATy->getElementType()) << "]";
349 } else {
350 assert(Ty->isPrimitiveType() && "Unknown derived type!");
351 printType(Ty);
352 }
353 return Out;
354}
355
356
Chris Lattnerfee714f2001-09-07 16:36:04 +0000357void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
358 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000359 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000360 WriteAsOperandInternal(Out, Operand, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000361}
362
Chris Lattner2f7c9632001-06-06 20:29:01 +0000363
Chris Lattner7bfee412001-10-29 16:05:51 +0000364void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000365 // Loop over the symbol table, emitting all named constants...
366 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000367 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000368
369 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000370 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000371
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000372 Out << "implementation\n";
373
Chris Lattner6915f8f2002-04-07 22:49:37 +0000374 // Output all of the functions...
Chris Lattner57698e22002-03-26 18:01:55 +0000375 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000376}
377
Chris Lattner7bfee412001-10-29 16:05:51 +0000378void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000379 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000380
Chris Lattner841d8b92001-11-26 18:54:16 +0000381 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000382 if (!GV->hasInitializer()) Out << "uninitialized ";
383
Chris Lattner7bfee412001-10-29 16:05:51 +0000384 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000385 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000386
387 if (GV->hasInitializer())
388 writeOperand(GV->getInitializer(), false, false);
389
Chris Lattner862e3382001-10-13 06:42:36 +0000390 printInfoComment(GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000391 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000392}
393
Chris Lattnerfee714f2001-09-07 16:36:04 +0000394
Chris Lattner7bfee412001-10-29 16:05:51 +0000395// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000396// if a named constant is found, emit it's declaration...
397//
Chris Lattner7bfee412001-10-29 16:05:51 +0000398void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000399 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
400 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
401 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
402
403 for (; I != End; ++I) {
404 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000405 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000406 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000407 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000408 Out << "\t%" << I->first << " = type ";
409
410 // Make sure we print out at least one level of the type structure, so
411 // that we do not get %FILE = type %FILE
412 //
413 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000414 }
415 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000416 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000417}
418
419
Chris Lattner7bfee412001-10-29 16:05:51 +0000420// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000421//
Chris Lattner3462ae32001-12-03 22:26:30 +0000422void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000423 // Don't print out unnamed constants, they will be inlined
424 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000425
Chris Lattneree998be2001-07-26 16:29:38 +0000426 // Print out name...
427 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000428
Chris Lattneree998be2001-07-26 16:29:38 +0000429 // Print out the constant type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000430 printType(CPV->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000431
432 // Write the value out now...
433 writeOperand(CPV, false, false);
434
435 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
436 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
Chris Lattner7bfee412001-10-29 16:05:51 +0000437 Out << "\t\t; <";
438 printType(CPV->getType()) << ">:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000439 if (Slot >= 0) Out << Slot;
440 else Out << "<badref>";
441 }
442
Chris Lattner7f74a562002-01-20 22:54:45 +0000443 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444}
445
Chris Lattner6915f8f2002-04-07 22:49:37 +0000446// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000447//
Chris Lattner57698e22002-03-26 18:01:55 +0000448void AssemblyWriter::printFunction(const Function *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000450 Out << "\n" << (M->isExternal() ? "declare " : "")
451 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner7bfee412001-10-29 16:05:51 +0000452 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner6915f8f2002-04-07 22:49:37 +0000453 Table.incorporateFunction(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000454
Chris Lattner7bfee412001-10-29 16:05:51 +0000455 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000456 const FunctionType *MT = M->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000457
Chris Lattner862e3382001-10-13 06:42:36 +0000458 if (!M->isExternal()) {
459 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000460 bind_obj(this, &AssemblyWriter::printArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000461 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000462 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000463 const FunctionType *MT = M->getFunctionType();
464 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
Chris Lattner862e3382001-10-13 06:42:36 +0000465 E = MT->getParamTypes().end(); I != E; ++I) {
466 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000467 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000468 }
469 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000470
471 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000472 if (MT->isVarArg()) {
473 if (MT->getParamTypes().size()) Out << ", ";
474 Out << "..."; // Output varargs portion of signature!
475 }
476 Out << ")\n";
477
478 if (!M->isExternal()) {
479 // Loop over the symbol table, emitting all named constants...
480 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000481 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000482
483 Out << "begin";
484
Chris Lattner6915f8f2002-04-07 22:49:37 +0000485 // Output all of its basic blocks... for the function
Chris Lattnerfee714f2001-09-07 16:36:04 +0000486 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000487 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000488
Chris Lattnera7620d92001-07-15 06:35:59 +0000489 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000490 }
491
Chris Lattner6915f8f2002-04-07 22:49:37 +0000492 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000493}
494
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000495// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000496// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000497//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000498void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000499 // Insert commas as we go... the first arg doesn't get a comma
500 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
501
502 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000503 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000504
505 // Output name, if available...
506 if (Arg->hasName())
507 Out << " %" << Arg->getName();
508 else if (Table.getValSlot(Arg) < 0)
509 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000510}
511
Chris Lattner7bfee412001-10-29 16:05:51 +0000512// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000513//
Chris Lattner7bfee412001-10-29 16:05:51 +0000514void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000515 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000516 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000517 } else {
518 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000519 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000520 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000521 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000522 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000523 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000524 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000525 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000526
Chris Lattnerfee714f2001-09-07 16:36:04 +0000527 // Output all of the instructions in the basic block...
528 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000529 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000530}
531
Chris Lattner862e3382001-10-13 06:42:36 +0000532
533// printInfoComment - Print a little comment after the instruction indicating
534// which slot it occupies.
535//
536void AssemblyWriter::printInfoComment(const Value *V) {
537 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000538 Out << "\t\t; <";
539 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000540
541 if (!V->hasName()) {
542 int Slot = Table.getValSlot(V); // Print out the def slot taken...
543 if (Slot >= 0) Out << ":" << Slot;
544 else Out << ":<badref>";
545 }
Chris Lattner3cb3a1f2001-12-14 16:29:12 +0000546 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000547 }
548}
549
Chris Lattner7bfee412001-10-29 16:05:51 +0000550// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000551//
Chris Lattner7bfee412001-10-29 16:05:51 +0000552void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000553 Out << "\t";
554
555 // Print out name if it exists...
556 if (I && I->hasName())
557 Out << "%" << I->getName() << " = ";
558
559 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000560 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000561
562 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000563 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000564
565 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner8d48df22002-04-13 18:34:38 +0000566 if (isa<BranchInst>(I) && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000567 writeOperand(I->getOperand(2), true);
568 Out << ",";
569 writeOperand(Operand, true);
570 Out << ",";
571 writeOperand(I->getOperand(1), true);
572
Chris Lattner8d48df22002-04-13 18:34:38 +0000573 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000574 // Special case switch statement to get formatting nice and correct...
575 writeOperand(Operand , true); Out << ",";
576 writeOperand(I->getOperand(1), true); Out << " [";
577
Chris Lattnera073acb2001-07-07 08:36:50 +0000578 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000580 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000581 writeOperand(I->getOperand(op+1), true);
582 }
583 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000584 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000585 Out << " ";
Chris Lattner29644b32001-11-06 01:48:45 +0000586 printType(I->getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000587 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588
Chris Lattner29644b32001-11-06 01:48:45 +0000589 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
590 if (op) Out << ", ";
591 Out << "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000592 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000593 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000594 }
Chris Lattner862e3382001-10-13 06:42:36 +0000595 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000597 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000598 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000599 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000600 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
601
602 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000603 // only do this if the first argument is a pointer to a nonvararg function,
604 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000605 //
Chris Lattner8d48df22002-04-13 18:34:38 +0000606 if (RetTy && MTy && !MTy->isVarArg() &&
607 (!isa<PointerType>(RetTy) ||
608 !isa<FunctionType>(cast<PointerType>(RetTy)))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000609 Out << " "; printType(RetTy);
610 writeOperand(Operand, false);
611 } else {
612 writeOperand(Operand, true);
613 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000614 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000615 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
616 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000617 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000618 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000619 }
620
621 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000622 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
623 // TODO: Should try to print out short form of the Invoke instruction
624 writeOperand(Operand, true);
625 Out << "(";
626 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
627 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
628 Out << ",";
629 writeOperand(I->getOperand(op), true);
630 }
631
632 Out << " )\n\t\t\tto";
633 writeOperand(II->getNormalDest(), true);
634 Out << " except";
635 writeOperand(II->getExceptionalDest(), true);
636
Chris Lattner8d48df22002-04-13 18:34:38 +0000637 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000638 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000639 printType(AI->getType()->getElementType());
640 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000641 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000642 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000643 }
Chris Lattner862e3382001-10-13 06:42:36 +0000644 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000645 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000646 Out << " to ";
647 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000648 } else if (Operand) { // Print the normal way...
649
650 // PrintAllTypes - Instructions who have operands of all the same type
651 // omit the type from all but the first operand. If the instruction has
652 // different type operands (for example br), then they are all printed.
653 bool PrintAllTypes = false;
654 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000655
Chris Lattnera073acb2001-07-07 08:36:50 +0000656 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
657 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000658 if (Operand->getType() != TheType) {
659 PrintAllTypes = true; // We have differing types! Print them all!
660 break;
661 }
662 }
663
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000664 // Shift Left & Right print both types even for Ubyte LHS
665 if (isa<ShiftInst>(I)) PrintAllTypes = true;
666
Chris Lattner7bfee412001-10-29 16:05:51 +0000667 if (!PrintAllTypes) {
668 Out << " ";
669 printType(I->getOperand(0)->getType());
670 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000671
Chris Lattnera073acb2001-07-07 08:36:50 +0000672 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000673 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000674 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000675 }
676 }
677
Chris Lattner862e3382001-10-13 06:42:36 +0000678 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000679 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000680}
681
682
683//===----------------------------------------------------------------------===//
684// External Interface declarations
685//===----------------------------------------------------------------------===//
686
687
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000688void Module::print(std::ostream &o) const {
689 SlotCalculator SlotTable(this, true);
690 AssemblyWriter W(o, SlotTable, this);
691 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000692}
693
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000694void GlobalVariable::print(std::ostream &o) const {
695 SlotCalculator SlotTable(getParent(), true);
696 AssemblyWriter W(o, SlotTable, getParent());
697 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000698}
699
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000700void Function::print(std::ostream &o) const {
701 SlotCalculator SlotTable(getParent(), true);
702 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000703
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000704 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000705}
706
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000707void BasicBlock::print(std::ostream &o) const {
708 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000709 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000710 getParent() ? getParent()->getParent() : 0);
711 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000712}
713
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000714void Instruction::print(std::ostream &o) const {
715 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000716 SlotCalculator SlotTable(F, true);
717 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000718
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000719 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720}
Chris Lattner7db79582001-11-07 04:21:57 +0000721
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000722void Constant::print(std::ostream &o) const {
723 if (this == 0) { o << "<null> constant value\n"; return; }
724 o << " " << getType()->getDescription() << " " << getStrValue();
725}
726
727void Type::print(std::ostream &o) const {
728 if (this == 0)
729 o << "<null Type>";
730 else
731 o << getDescription();
732}
733
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000734void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000735 o << getType() << " " << getName();
736}
737
738void Value::dump() const { print(std::cerr); }
739
740//===----------------------------------------------------------------------===//
741// CachedWriter Class Implementation
742//===----------------------------------------------------------------------===//
743
Chris Lattner7db79582001-11-07 04:21:57 +0000744void CachedWriter::setModule(const Module *M) {
745 delete SC; delete AW;
746 if (M) {
747 SC = new SlotCalculator(M, true);
748 AW = new AssemblyWriter(Out, *SC, M);
749 } else {
750 SC = 0; AW = 0;
751 }
752}
753
754CachedWriter::~CachedWriter() {
755 delete AW;
756 delete SC;
757}
758
759CachedWriter &CachedWriter::operator<<(const Value *V) {
760 assert(AW && SC && "CachedWriter does not have a current module!");
761 switch (V->getValueType()) {
762 case Value::ConstantVal:
763 Out << " "; AW->write(V->getType());
Chris Lattner3462ae32001-12-03 22:26:30 +0000764 Out << " " << cast<Constant>(V)->getStrValue(); break;
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000765 case Value::ArgumentVal:
Chris Lattner7db79582001-11-07 04:21:57 +0000766 AW->write(V->getType()); Out << " " << V->getName(); break;
767 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
768 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
769 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000770 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000771 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
772 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
773 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
774 }
775 return *this;
776}