blob: f5e0fcfa922031a2e7f9b1dfac95a5df8c209fa6 [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: {
154 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000155 Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000156 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerb86620e2001-10-29 16:37:48 +0000157 I = MTy->getParamTypes().begin(),
158 E = MTy->getParamTypes().end(); I != E; ++I) {
159 if (I != MTy->getParamTypes().begin())
160 Result += ", ";
161 Result += calcTypeName(*I, TypeStack, TypeNames);
162 }
163 if (MTy->isVarArg()) {
164 if (!MTy->getParamTypes().empty()) Result += ", ";
165 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);
189 int NumElements = ATy->getNumElements();
190 Result = "[";
191 if (NumElements != -1) Result += itostr(NumElements) + " x ";
192 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
193 break;
194 }
195 default:
196 assert(0 && "Unhandled case in getTypeProps!");
197 Result = "<error>";
198 }
199
200 TypeStack.pop_back(); // Remove self from stack...
201 return Result;
202}
203
204
205// printTypeInt - The internal guts of printing out a type that has a
206// potentially named portion.
207//
208static ostream &printTypeInt(ostream &Out, const Type *Ty,
209 map<const Type *, string> &TypeNames) {
210 // Primitive types always print out their description, regardless of whether
211 // they have been named or not.
212 //
213 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
214
215 // Check to see if the type is named.
216 map<const Type *, string>::iterator I = TypeNames.find(Ty);
217 if (I != TypeNames.end()) return Out << I->second;
218
219 // Otherwise we have a type that has not been named but is a derived type.
220 // Carefully recurse the type hierarchy to print out any contained symbolic
221 // names.
222 //
223 vector<const Type *> TypeStack;
224 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000225 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000226 return Out << TypeName;
227}
228
Chris Lattner34b95182001-10-31 04:33:19 +0000229
Chris Lattnerb86620e2001-10-29 16:37:48 +0000230// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
231// type, iff there is an entry in the modules symbol table for the specified
232// type or one of it's component types. This is slower than a simple x << Type;
233//
234ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
235 Out << " ";
236
237 // If they want us to print out a type, attempt to make it symbolic if there
238 // is a symbol table in the module...
239 if (M && M->hasSymbolTable()) {
240 map<const Type *, string> TypeNames;
241 fillTypeNameTable(M, TypeNames);
242
Chris Lattner72f866e2001-10-29 16:40:32 +0000243 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000244 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000245 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000246 }
247}
248
249
250// WriteAsOperand - Write the name of the specified value out to the specified
251// ostream. This can be useful when you just want to print int %reg126, not the
252// whole instruction that generated it.
253//
254ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
255 bool PrintName, SlotCalculator *Table) {
Chris Lattner72f866e2001-10-29 16:40:32 +0000256 if (PrintType)
257 WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000258
259 WriteAsOperandInternal(Out, V, PrintName, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000260 return Out;
261}
262
263
Chris Lattner2e9fee42001-07-12 23:35:26 +0000264
Chris Lattnerfee714f2001-09-07 16:36:04 +0000265class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000266 ostream &Out;
267 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000268 const Module *TheModule;
269 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000270public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000271 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
272 : Out(o), Table(Tab), TheModule(M) {
273
274 // If the module has a symbol table, take all global types and stuff their
275 // names into the TypeNames map.
276 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000277 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000278 }
279
Chris Lattner7bfee412001-10-29 16:05:51 +0000280 inline void write(const Module *M) { printModule(M); }
281 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000282 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000283 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
284 inline void write(const Instruction *I) { printInstruction(I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000285 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000286 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000287
Chris Lattner2f7c9632001-06-06 20:29:01 +0000288private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000289 void printModule(const Module *M);
290 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000291 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000292 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000293 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000294 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000295 void printBasicBlock(const BasicBlock *BB);
296 void printInstruction(const Instruction *I);
297 ostream &printType(const Type *Ty);
298
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
Chris Lattner862e3382001-10-13 06:42:36 +0000300
301 // printInfoComment - Print a little comment after the instruction indicating
302 // which slot it occupies.
303 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000304};
305
306
Chris Lattnerfee714f2001-09-07 16:36:04 +0000307void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
308 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000309 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000310 WriteAsOperandInternal(Out, Operand, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311}
312
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313
Chris Lattner7bfee412001-10-29 16:05:51 +0000314void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000315 // Loop over the symbol table, emitting all named constants...
316 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000317 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000318
319 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000320 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000321
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000322 Out << "implementation\n";
323
Chris Lattner6915f8f2002-04-07 22:49:37 +0000324 // Output all of the functions...
Chris Lattner57698e22002-03-26 18:01:55 +0000325 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000326}
327
Chris Lattner7bfee412001-10-29 16:05:51 +0000328void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000329 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000330
Chris Lattner841d8b92001-11-26 18:54:16 +0000331 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000332 if (!GV->hasInitializer()) Out << "uninitialized ";
333
Chris Lattner7bfee412001-10-29 16:05:51 +0000334 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000335 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000336
337 if (GV->hasInitializer())
338 writeOperand(GV->getInitializer(), false, false);
339
Chris Lattner862e3382001-10-13 06:42:36 +0000340 printInfoComment(GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000341 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000342}
343
Chris Lattnerfee714f2001-09-07 16:36:04 +0000344
Chris Lattner7bfee412001-10-29 16:05:51 +0000345// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000346// if a named constant is found, emit it's declaration...
347//
Chris Lattner7bfee412001-10-29 16:05:51 +0000348void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000349 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
350 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
351 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
352
353 for (; I != End; ++I) {
354 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000355 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000356 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000357 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000358 Out << "\t%" << I->first << " = type " << Ty->getDescription() << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000359 }
360 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000361 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000362}
363
364
Chris Lattner7bfee412001-10-29 16:05:51 +0000365// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000366//
Chris Lattner3462ae32001-12-03 22:26:30 +0000367void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000368 // Don't print out unnamed constants, they will be inlined
369 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000370
Chris Lattneree998be2001-07-26 16:29:38 +0000371 // Print out name...
372 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000373
Chris Lattneree998be2001-07-26 16:29:38 +0000374 // Print out the constant type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000375 printType(CPV->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000376
377 // Write the value out now...
378 writeOperand(CPV, false, false);
379
380 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
381 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
Chris Lattner7bfee412001-10-29 16:05:51 +0000382 Out << "\t\t; <";
383 printType(CPV->getType()) << ">:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000384 if (Slot >= 0) Out << Slot;
385 else Out << "<badref>";
386 }
387
Chris Lattner7f74a562002-01-20 22:54:45 +0000388 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000389}
390
Chris Lattner6915f8f2002-04-07 22:49:37 +0000391// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000392//
Chris Lattner57698e22002-03-26 18:01:55 +0000393void AssemblyWriter::printFunction(const Function *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000394 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000395 Out << "\n" << (M->isExternal() ? "declare " : "")
396 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner7bfee412001-10-29 16:05:51 +0000397 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner6915f8f2002-04-07 22:49:37 +0000398 Table.incorporateFunction(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000399
Chris Lattner7bfee412001-10-29 16:05:51 +0000400 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000401 const FunctionType *MT = M->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000402
Chris Lattner862e3382001-10-13 06:42:36 +0000403 if (!M->isExternal()) {
404 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000405 bind_obj(this, &AssemblyWriter::printArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000406 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000407 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000408 const FunctionType *MT = M->getFunctionType();
409 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
Chris Lattner862e3382001-10-13 06:42:36 +0000410 E = MT->getParamTypes().end(); I != E; ++I) {
411 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000412 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000413 }
414 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000415
416 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000417 if (MT->isVarArg()) {
418 if (MT->getParamTypes().size()) Out << ", ";
419 Out << "..."; // Output varargs portion of signature!
420 }
421 Out << ")\n";
422
423 if (!M->isExternal()) {
424 // Loop over the symbol table, emitting all named constants...
425 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000426 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000427
428 Out << "begin";
429
Chris Lattner6915f8f2002-04-07 22:49:37 +0000430 // Output all of its basic blocks... for the function
Chris Lattnerfee714f2001-09-07 16:36:04 +0000431 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000432 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000433
Chris Lattnera7620d92001-07-15 06:35:59 +0000434 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000435 }
436
Chris Lattner6915f8f2002-04-07 22:49:37 +0000437 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000438}
439
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000440// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000441// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000442//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000443void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444 // Insert commas as we go... the first arg doesn't get a comma
445 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
446
447 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000448 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449
450 // Output name, if available...
451 if (Arg->hasName())
452 Out << " %" << Arg->getName();
453 else if (Table.getValSlot(Arg) < 0)
454 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000455}
456
Chris Lattner7bfee412001-10-29 16:05:51 +0000457// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000458//
Chris Lattner7bfee412001-10-29 16:05:51 +0000459void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000460 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000461 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000462 } else {
463 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000464 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000465 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000466 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000467 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000468 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000469 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000470 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000471
Chris Lattnerfee714f2001-09-07 16:36:04 +0000472 // Output all of the instructions in the basic block...
473 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000474 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000475}
476
Chris Lattner862e3382001-10-13 06:42:36 +0000477
478// printInfoComment - Print a little comment after the instruction indicating
479// which slot it occupies.
480//
481void AssemblyWriter::printInfoComment(const Value *V) {
482 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000483 Out << "\t\t; <";
484 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000485
486 if (!V->hasName()) {
487 int Slot = Table.getValSlot(V); // Print out the def slot taken...
488 if (Slot >= 0) Out << ":" << Slot;
489 else Out << ":<badref>";
490 }
Chris Lattner3cb3a1f2001-12-14 16:29:12 +0000491 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000492 }
493}
494
Chris Lattner7bfee412001-10-29 16:05:51 +0000495// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000496//
Chris Lattner7bfee412001-10-29 16:05:51 +0000497void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000498 Out << "\t";
499
500 // Print out name if it exists...
501 if (I && I->hasName())
502 Out << "%" << I->getName() << " = ";
503
504 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000505 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000506
507 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000508 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000509
510 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000511 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000512 writeOperand(I->getOperand(2), true);
513 Out << ",";
514 writeOperand(Operand, true);
515 Out << ",";
516 writeOperand(I->getOperand(1), true);
517
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000518 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000519 // Special case switch statement to get formatting nice and correct...
520 writeOperand(Operand , true); Out << ",";
521 writeOperand(I->getOperand(1), true); Out << " [";
522
Chris Lattnera073acb2001-07-07 08:36:50 +0000523 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000524 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000525 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000526 writeOperand(I->getOperand(op+1), true);
527 }
528 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000529 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000530 Out << " ";
Chris Lattner29644b32001-11-06 01:48:45 +0000531 printType(I->getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000532 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000533
Chris Lattner29644b32001-11-06 01:48:45 +0000534 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
535 if (op) Out << ", ";
536 Out << "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000537 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000538 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000539 }
Chris Lattner862e3382001-10-13 06:42:36 +0000540 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000541 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000542 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000543 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000544 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000545 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
546
547 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000548 // only do this if the first argument is a pointer to a nonvararg function,
549 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000550 //
551 if (RetTy && !MTy->isVarArg() &&
Chris Lattner91db5822002-03-29 03:44:36 +0000552 (!isa<PointerType>(RetTy)||!isa<FunctionType>(cast<PointerType>(RetTy)))){
Chris Lattner2f2d9472001-11-06 21:28:12 +0000553 Out << " "; printType(RetTy);
554 writeOperand(Operand, false);
555 } else {
556 writeOperand(Operand, true);
557 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000558 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000559 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
560 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000561 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000562 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000563 }
564
565 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000566 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
567 // TODO: Should try to print out short form of the Invoke instruction
568 writeOperand(Operand, true);
569 Out << "(";
570 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
571 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
572 Out << ",";
573 writeOperand(I->getOperand(op), true);
574 }
575
576 Out << " )\n\t\t\tto";
577 writeOperand(II->getNormalDest(), true);
578 Out << " except";
579 writeOperand(II->getExceptionalDest(), true);
580
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000581 } else if (I->getOpcode() == Instruction::Malloc ||
582 I->getOpcode() == Instruction::Alloca) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000583 Out << " ";
Chris Lattner2413b162001-12-04 00:03:30 +0000584 printType(cast<const PointerType>(I->getType())->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000585 if (I->getNumOperands()) {
586 Out << ",";
587 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588 }
Chris Lattner862e3382001-10-13 06:42:36 +0000589 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000590 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000591 Out << " to ";
592 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000593 } else if (Operand) { // Print the normal way...
594
595 // PrintAllTypes - Instructions who have operands of all the same type
596 // omit the type from all but the first operand. If the instruction has
597 // different type operands (for example br), then they are all printed.
598 bool PrintAllTypes = false;
599 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000600
Chris Lattnera073acb2001-07-07 08:36:50 +0000601 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
602 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000603 if (Operand->getType() != TheType) {
604 PrintAllTypes = true; // We have differing types! Print them all!
605 break;
606 }
607 }
608
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000609 // Shift Left & Right print both types even for Ubyte LHS
610 if (isa<ShiftInst>(I)) PrintAllTypes = true;
611
Chris Lattner7bfee412001-10-29 16:05:51 +0000612 if (!PrintAllTypes) {
613 Out << " ";
614 printType(I->getOperand(0)->getType());
615 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000616
Chris Lattnera073acb2001-07-07 08:36:50 +0000617 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000618 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000619 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000620 }
621 }
622
Chris Lattner862e3382001-10-13 06:42:36 +0000623 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000624 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000625}
626
627
Chris Lattner7bfee412001-10-29 16:05:51 +0000628// printType - Go to extreme measures to attempt to print out a short, symbolic
629// version of a type name.
630//
631ostream &AssemblyWriter::printType(const Type *Ty) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000632 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner7bfee412001-10-29 16:05:51 +0000633}
634
635
Chris Lattner2f7c9632001-06-06 20:29:01 +0000636//===----------------------------------------------------------------------===//
637// External Interface declarations
638//===----------------------------------------------------------------------===//
639
640
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000641void Module::print(std::ostream &o) const {
642 SlotCalculator SlotTable(this, true);
643 AssemblyWriter W(o, SlotTable, this);
644 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000645}
646
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000647void GlobalVariable::print(std::ostream &o) const {
648 SlotCalculator SlotTable(getParent(), true);
649 AssemblyWriter W(o, SlotTable, getParent());
650 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000651}
652
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000653void Function::print(std::ostream &o) const {
654 SlotCalculator SlotTable(getParent(), true);
655 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000656
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000657 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000658}
659
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000660void BasicBlock::print(std::ostream &o) const {
661 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000662 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000663 getParent() ? getParent()->getParent() : 0);
664 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000665}
666
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000667void Instruction::print(std::ostream &o) const {
668 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000669 SlotCalculator SlotTable(F, true);
670 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000671
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000672 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000673}
Chris Lattner7db79582001-11-07 04:21:57 +0000674
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000675void Constant::print(std::ostream &o) const {
676 if (this == 0) { o << "<null> constant value\n"; return; }
677 o << " " << getType()->getDescription() << " " << getStrValue();
678}
679
680void Type::print(std::ostream &o) const {
681 if (this == 0)
682 o << "<null Type>";
683 else
684 o << getDescription();
685}
686
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000687void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000688 o << getType() << " " << getName();
689}
690
691void Value::dump() const { print(std::cerr); }
692
693//===----------------------------------------------------------------------===//
694// CachedWriter Class Implementation
695//===----------------------------------------------------------------------===//
696
Chris Lattner7db79582001-11-07 04:21:57 +0000697void CachedWriter::setModule(const Module *M) {
698 delete SC; delete AW;
699 if (M) {
700 SC = new SlotCalculator(M, true);
701 AW = new AssemblyWriter(Out, *SC, M);
702 } else {
703 SC = 0; AW = 0;
704 }
705}
706
707CachedWriter::~CachedWriter() {
708 delete AW;
709 delete SC;
710}
711
712CachedWriter &CachedWriter::operator<<(const Value *V) {
713 assert(AW && SC && "CachedWriter does not have a current module!");
714 switch (V->getValueType()) {
715 case Value::ConstantVal:
716 Out << " "; AW->write(V->getType());
Chris Lattner3462ae32001-12-03 22:26:30 +0000717 Out << " " << cast<Constant>(V)->getStrValue(); break;
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000718 case Value::ArgumentVal:
Chris Lattner7db79582001-11-07 04:21:57 +0000719 AW->write(V->getType()); Out << " " << V->getName(); break;
720 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
721 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
722 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000723 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000724 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
725 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
726 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
727 }
728 return *this;
729}