blob: 0af79bea5688d45edce3f0f928005a4ef2c1ec3d [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 Lattner6915f8f2002-04-07 22:49:37 +000011#include "llvm/SlotCalculator.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000012#include "llvm/Module.h"
Chris Lattner57698e22002-03-26 18:01:55 +000013#include "llvm/Function.h"
Chris Lattnerda975502001-09-10 07:58:01 +000014#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include "llvm/BasicBlock.h"
Chris Lattner3462ae32001-12-03 22:26:30 +000016#include "llvm/ConstantVals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000018#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000019#include "llvm/iPHINode.h"
20#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000021#include "llvm/SymbolTable.h"
Chris Lattner5de22042001-11-27 00:03:19 +000022#include "Support/StringExtras.h"
23#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000024#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000025#include <map>
Chris Lattner7f74a562002-01-20 22:54:45 +000026using std::string;
27using std::map;
28using std::vector;
29using std::ostream;
Chris Lattner7bfee412001-10-29 16:05:51 +000030
Chris Lattnerf7e79482002-04-07 22:31:46 +000031void Value::dump() const {
32 std::cerr << this;
33}
34
Chris Lattnerb86620e2001-10-29 16:37:48 +000035static const Module *getModuleFromVal(const Value *V) {
Chris Lattner57698e22002-03-26 18:01:55 +000036 if (const FunctionArgument *MA = dyn_cast<const FunctionArgument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000037 return MA->getParent() ? MA->getParent()->getParent() : 0;
38 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
39 return BB->getParent() ? BB->getParent()->getParent() : 0;
40 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000041 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000042 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000043 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000044 return GV->getParent();
45 else if (const Module *Mod = dyn_cast<const Module>(V))
46 return Mod;
47 return 0;
48}
49
Chris Lattner7bfee412001-10-29 16:05:51 +000050static SlotCalculator *createSlotCalculator(const Value *V) {
51 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner57698e22002-03-26 18:01:55 +000052 if (const FunctionArgument *FA = dyn_cast<const FunctionArgument>(V)) {
53 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000054 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
55 return new SlotCalculator(I->getParent()->getParent(), true);
56 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
57 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000058 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000059 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000060 } else if (const Function *Func = dyn_cast<const Function>(V)) {
61 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000062 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
63 return new SlotCalculator(Mod, true);
64 }
65 return 0;
66}
Chris Lattner2f7c9632001-06-06 20:29:01 +000067
Chris Lattner5e5abe32001-07-20 19:15:21 +000068// WriteAsOperand - Write the name of the specified value out to the specified
69// ostream. This can be useful when you just want to print int %reg126, not the
70// whole instruction that generated it.
71//
Chris Lattnerb86620e2001-10-29 16:37:48 +000072static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
73 SlotCalculator *Table) {
Chris Lattnerfee714f2001-09-07 16:36:04 +000074 if (PrintName && V->hasName()) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000075 Out << " %" << V->getName();
76 } else {
Chris Lattner3462ae32001-12-03 22:26:30 +000077 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000078 Out << " " << CPV->getStrValue();
79 } else {
80 int Slot;
81 if (Table) {
82 Slot = Table->getValSlot(V);
83 } else {
Chris Lattnerb86620e2001-10-29 16:37:48 +000084 if (const Type *Ty = dyn_cast<const Type>(V)) {
85 Out << " " << Ty->getDescription();
86 return;
87 }
Chris Lattner7bfee412001-10-29 16:05:51 +000088
89 Table = createSlotCalculator(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +000090 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +000091
Chris Lattner5e5abe32001-07-20 19:15:21 +000092 Slot = Table->getValSlot(V);
93 delete Table;
94 }
95 if (Slot >= 0) Out << " %" << Slot;
96 else if (PrintName)
97 Out << "<badref>"; // Not embeded into a location?
98 }
99 }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000100}
101
102
103// If the module has a symbol table, take all global types and stuff their
104// names into the TypeNames map.
105//
106static void fillTypeNameTable(const Module *M,
107 map<const Type *, string> &TypeNames) {
108 if (M && M->hasSymbolTable()) {
109 const SymbolTable *ST = M->getSymbolTable();
110 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
111 if (PI != ST->end()) {
112 SymbolTable::type_const_iterator I = PI->second.begin();
113 for (; I != PI->second.end(); ++I) {
114 // As a heuristic, don't insert pointer to primitive types, because
115 // they are used too often to have a single useful name.
116 //
117 const Type *Ty = cast<const Type>(I->second);
118 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +0000119 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +0000120 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000121 }
122 }
123 }
124}
125
126
127
128static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
129 map<const Type *, string> &TypeNames) {
130 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
131
132 // Check to see if the type is named.
133 map<const Type *, string>::iterator I = TypeNames.find(Ty);
134 if (I != TypeNames.end()) return I->second;
135
136 // Check to see if the Type is already on the stack...
137 unsigned Slot = 0, CurSize = TypeStack.size();
138 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
139
140 // This is another base case for the recursion. In this case, we know
141 // that we have looped back to a type that we have previously visited.
142 // Generate the appropriate upreference to handle this.
143 //
144 if (Slot < CurSize)
145 return "\\" + utostr(CurSize-Slot); // Here's the upreference
146
147 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
148
149 string Result;
150 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000151 case Type::FunctionTyID: {
152 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000153 Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000154 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerb86620e2001-10-29 16:37:48 +0000155 I = MTy->getParamTypes().begin(),
156 E = MTy->getParamTypes().end(); I != E; ++I) {
157 if (I != MTy->getParamTypes().begin())
158 Result += ", ";
159 Result += calcTypeName(*I, TypeStack, TypeNames);
160 }
161 if (MTy->isVarArg()) {
162 if (!MTy->getParamTypes().empty()) Result += ", ";
163 Result += "...";
164 }
165 Result += ")";
166 break;
167 }
168 case Type::StructTyID: {
169 const StructType *STy = cast<const StructType>(Ty);
170 Result = "{ ";
171 for (StructType::ElementTypes::const_iterator
172 I = STy->getElementTypes().begin(),
173 E = STy->getElementTypes().end(); I != E; ++I) {
174 if (I != STy->getElementTypes().begin())
175 Result += ", ";
176 Result += calcTypeName(*I, TypeStack, TypeNames);
177 }
178 Result += " }";
179 break;
180 }
181 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000182 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattnerb86620e2001-10-29 16:37:48 +0000183 TypeStack, TypeNames) + " *";
184 break;
185 case Type::ArrayTyID: {
186 const ArrayType *ATy = cast<const ArrayType>(Ty);
187 int NumElements = ATy->getNumElements();
188 Result = "[";
189 if (NumElements != -1) Result += itostr(NumElements) + " x ";
190 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);
292 void printFunctionArgument(const FunctionArgument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000293 void printBasicBlock(const BasicBlock *BB);
294 void printInstruction(const Instruction *I);
295 ostream &printType(const Type *Ty);
296
Chris Lattner2f7c9632001-06-06 20:29:01 +0000297 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
Chris Lattner862e3382001-10-13 06:42:36 +0000298
299 // printInfoComment - Print a little comment after the instruction indicating
300 // which slot it occupies.
301 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000302};
303
304
Chris Lattnerfee714f2001-09-07 16:36:04 +0000305void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
306 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000307 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000308 WriteAsOperandInternal(Out, Operand, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000309}
310
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311
Chris Lattner7bfee412001-10-29 16:05:51 +0000312void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000313 // Loop over the symbol table, emitting all named constants...
314 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000315 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000316
317 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000318 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000319
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000320 Out << "implementation\n";
321
Chris Lattner6915f8f2002-04-07 22:49:37 +0000322 // Output all of the functions...
Chris Lattner57698e22002-03-26 18:01:55 +0000323 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000324}
325
Chris Lattner7bfee412001-10-29 16:05:51 +0000326void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000327 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000328
Chris Lattner841d8b92001-11-26 18:54:16 +0000329 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000330 if (!GV->hasInitializer()) Out << "uninitialized ";
331
Chris Lattner7bfee412001-10-29 16:05:51 +0000332 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000333 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000334
335 if (GV->hasInitializer())
336 writeOperand(GV->getInitializer(), false, false);
337
Chris Lattner862e3382001-10-13 06:42:36 +0000338 printInfoComment(GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000339 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000340}
341
Chris Lattnerfee714f2001-09-07 16:36:04 +0000342
Chris Lattner7bfee412001-10-29 16:05:51 +0000343// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000344// if a named constant is found, emit it's declaration...
345//
Chris Lattner7bfee412001-10-29 16:05:51 +0000346void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000347 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
348 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
349 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
350
351 for (; I != End; ++I) {
352 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000353 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000354 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000355 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000356 Out << "\t%" << I->first << " = type " << Ty->getDescription() << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000357 }
358 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000359 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000360}
361
362
Chris Lattner7bfee412001-10-29 16:05:51 +0000363// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000364//
Chris Lattner3462ae32001-12-03 22:26:30 +0000365void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000366 // Don't print out unnamed constants, they will be inlined
367 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000368
Chris Lattneree998be2001-07-26 16:29:38 +0000369 // Print out name...
370 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000371
Chris Lattneree998be2001-07-26 16:29:38 +0000372 // Print out the constant type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000373 printType(CPV->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000374
375 // Write the value out now...
376 writeOperand(CPV, false, false);
377
378 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
379 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
Chris Lattner7bfee412001-10-29 16:05:51 +0000380 Out << "\t\t; <";
381 printType(CPV->getType()) << ">:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000382 if (Slot >= 0) Out << Slot;
383 else Out << "<badref>";
384 }
385
Chris Lattner7f74a562002-01-20 22:54:45 +0000386 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000387}
388
Chris Lattner6915f8f2002-04-07 22:49:37 +0000389// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000390//
Chris Lattner57698e22002-03-26 18:01:55 +0000391void AssemblyWriter::printFunction(const Function *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000392 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000393 Out << "\n" << (M->isExternal() ? "declare " : "")
394 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner7bfee412001-10-29 16:05:51 +0000395 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner6915f8f2002-04-07 22:49:37 +0000396 Table.incorporateFunction(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000397
Chris Lattner7bfee412001-10-29 16:05:51 +0000398 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000399 const FunctionType *MT = M->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000400
Chris Lattner862e3382001-10-13 06:42:36 +0000401 if (!M->isExternal()) {
402 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner57698e22002-03-26 18:01:55 +0000403 bind_obj(this, &AssemblyWriter::printFunctionArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000404 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000405 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000406 const FunctionType *MT = M->getFunctionType();
407 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
Chris Lattner862e3382001-10-13 06:42:36 +0000408 E = MT->getParamTypes().end(); I != E; ++I) {
409 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000410 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000411 }
412 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000413
414 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000415 if (MT->isVarArg()) {
416 if (MT->getParamTypes().size()) Out << ", ";
417 Out << "..."; // Output varargs portion of signature!
418 }
419 Out << ")\n";
420
421 if (!M->isExternal()) {
422 // Loop over the symbol table, emitting all named constants...
423 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000424 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000425
426 Out << "begin";
427
Chris Lattner6915f8f2002-04-07 22:49:37 +0000428 // Output all of its basic blocks... for the function
Chris Lattnerfee714f2001-09-07 16:36:04 +0000429 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000430 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000431
Chris Lattnera7620d92001-07-15 06:35:59 +0000432 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000433 }
434
Chris Lattner6915f8f2002-04-07 22:49:37 +0000435 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000436}
437
Chris Lattner57698e22002-03-26 18:01:55 +0000438// printFunctionArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000439// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000440//
Chris Lattner57698e22002-03-26 18:01:55 +0000441void AssemblyWriter::printFunctionArgument(const FunctionArgument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000442 // Insert commas as we go... the first arg doesn't get a comma
443 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
444
445 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000446 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000447
448 // Output name, if available...
449 if (Arg->hasName())
450 Out << " %" << Arg->getName();
451 else if (Table.getValSlot(Arg) < 0)
452 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000453}
454
Chris Lattner7bfee412001-10-29 16:05:51 +0000455// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000456//
Chris Lattner7bfee412001-10-29 16:05:51 +0000457void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000458 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000459 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000460 } else {
461 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000462 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000463 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000464 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000465 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000466 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000467 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000468 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000469
Chris Lattnerfee714f2001-09-07 16:36:04 +0000470 // Output all of the instructions in the basic block...
471 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000472 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000473}
474
Chris Lattner862e3382001-10-13 06:42:36 +0000475
476// printInfoComment - Print a little comment after the instruction indicating
477// which slot it occupies.
478//
479void AssemblyWriter::printInfoComment(const Value *V) {
480 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000481 Out << "\t\t; <";
482 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000483
484 if (!V->hasName()) {
485 int Slot = Table.getValSlot(V); // Print out the def slot taken...
486 if (Slot >= 0) Out << ":" << Slot;
487 else Out << ":<badref>";
488 }
Chris Lattner3cb3a1f2001-12-14 16:29:12 +0000489 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000490 }
491}
492
Chris Lattner7bfee412001-10-29 16:05:51 +0000493// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000494//
Chris Lattner7bfee412001-10-29 16:05:51 +0000495void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000496 Out << "\t";
497
498 // Print out name if it exists...
499 if (I && I->hasName())
500 Out << "%" << I->getName() << " = ";
501
502 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000503 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000504
505 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000506 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000507
508 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000509 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000510 writeOperand(I->getOperand(2), true);
511 Out << ",";
512 writeOperand(Operand, true);
513 Out << ",";
514 writeOperand(I->getOperand(1), true);
515
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000516 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000517 // Special case switch statement to get formatting nice and correct...
518 writeOperand(Operand , true); Out << ",";
519 writeOperand(I->getOperand(1), true); Out << " [";
520
Chris Lattnera073acb2001-07-07 08:36:50 +0000521 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000522 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000523 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000524 writeOperand(I->getOperand(op+1), true);
525 }
526 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000527 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000528 Out << " ";
Chris Lattner29644b32001-11-06 01:48:45 +0000529 printType(I->getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000530 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000531
Chris Lattner29644b32001-11-06 01:48:45 +0000532 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
533 if (op) Out << ", ";
534 Out << "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000535 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000536 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000537 }
Chris Lattner862e3382001-10-13 06:42:36 +0000538 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000539 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000540 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000541 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000542 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000543 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
544
545 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000546 // only do this if the first argument is a pointer to a nonvararg function,
547 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000548 //
549 if (RetTy && !MTy->isVarArg() &&
Chris Lattner91db5822002-03-29 03:44:36 +0000550 (!isa<PointerType>(RetTy)||!isa<FunctionType>(cast<PointerType>(RetTy)))){
Chris Lattner2f2d9472001-11-06 21:28:12 +0000551 Out << " "; printType(RetTy);
552 writeOperand(Operand, false);
553 } else {
554 writeOperand(Operand, true);
555 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000556 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000557 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
558 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000559 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000560 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000561 }
562
563 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000564 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
565 // TODO: Should try to print out short form of the Invoke instruction
566 writeOperand(Operand, true);
567 Out << "(";
568 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
569 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
570 Out << ",";
571 writeOperand(I->getOperand(op), true);
572 }
573
574 Out << " )\n\t\t\tto";
575 writeOperand(II->getNormalDest(), true);
576 Out << " except";
577 writeOperand(II->getExceptionalDest(), true);
578
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000579 } else if (I->getOpcode() == Instruction::Malloc ||
580 I->getOpcode() == Instruction::Alloca) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000581 Out << " ";
Chris Lattner2413b162001-12-04 00:03:30 +0000582 printType(cast<const PointerType>(I->getType())->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000583 if (I->getNumOperands()) {
584 Out << ",";
585 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000586 }
Chris Lattner862e3382001-10-13 06:42:36 +0000587 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000588 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000589 Out << " to ";
590 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000591 } else if (Operand) { // Print the normal way...
592
593 // PrintAllTypes - Instructions who have operands of all the same type
594 // omit the type from all but the first operand. If the instruction has
595 // different type operands (for example br), then they are all printed.
596 bool PrintAllTypes = false;
597 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000598
Chris Lattnera073acb2001-07-07 08:36:50 +0000599 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
600 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000601 if (Operand->getType() != TheType) {
602 PrintAllTypes = true; // We have differing types! Print them all!
603 break;
604 }
605 }
606
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000607 // Shift Left & Right print both types even for Ubyte LHS
608 if (isa<ShiftInst>(I)) PrintAllTypes = true;
609
Chris Lattner7bfee412001-10-29 16:05:51 +0000610 if (!PrintAllTypes) {
611 Out << " ";
612 printType(I->getOperand(0)->getType());
613 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000614
Chris Lattnera073acb2001-07-07 08:36:50 +0000615 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000616 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000617 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000618 }
619 }
620
Chris Lattner862e3382001-10-13 06:42:36 +0000621 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000622 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000623}
624
625
Chris Lattner7bfee412001-10-29 16:05:51 +0000626// printType - Go to extreme measures to attempt to print out a short, symbolic
627// version of a type name.
628//
629ostream &AssemblyWriter::printType(const Type *Ty) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000630 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner7bfee412001-10-29 16:05:51 +0000631}
632
633
Chris Lattner2f7c9632001-06-06 20:29:01 +0000634//===----------------------------------------------------------------------===//
635// External Interface declarations
636//===----------------------------------------------------------------------===//
637
638
639
640void WriteToAssembly(const Module *M, ostream &o) {
641 if (M == 0) { o << "<null> module\n"; return; }
642 SlotCalculator SlotTable(M, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000643 AssemblyWriter W(o, SlotTable, M);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000644
645 W.write(M);
646}
647
Chris Lattneradfe0d12001-09-10 20:08:19 +0000648void WriteToAssembly(const GlobalVariable *G, ostream &o) {
649 if (G == 0) { o << "<null> global variable\n"; return; }
650 SlotCalculator SlotTable(G->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000651 AssemblyWriter W(o, SlotTable, G->getParent());
Chris Lattneradfe0d12001-09-10 20:08:19 +0000652 W.write(G);
653}
654
Chris Lattner57698e22002-03-26 18:01:55 +0000655void WriteToAssembly(const Function *F, ostream &o) {
656 if (F == 0) { o << "<null> function\n"; return; }
657 SlotCalculator SlotTable(F->getParent(), true);
658 AssemblyWriter W(o, SlotTable, F->getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000659
Chris Lattner57698e22002-03-26 18:01:55 +0000660 W.write(F);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000661}
662
663
664void WriteToAssembly(const BasicBlock *BB, ostream &o) {
665 if (BB == 0) { o << "<null> basic block\n"; return; }
666
667 SlotCalculator SlotTable(BB->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000668 AssemblyWriter W(o, SlotTable,
669 BB->getParent() ? BB->getParent()->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000670
671 W.write(BB);
672}
673
Chris Lattner3462ae32001-12-03 22:26:30 +0000674void WriteToAssembly(const Constant *CPV, ostream &o) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000675 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +0000676 o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000677}
678
679void WriteToAssembly(const Instruction *I, ostream &o) {
680 if (I == 0) { o << "<null> instruction\n"; return; }
681
Chris Lattner57698e22002-03-26 18:01:55 +0000682 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
683 SlotCalculator SlotTable(F, true);
684 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000685
686 W.write(I);
687}
Chris Lattner7db79582001-11-07 04:21:57 +0000688
689void CachedWriter::setModule(const Module *M) {
690 delete SC; delete AW;
691 if (M) {
692 SC = new SlotCalculator(M, true);
693 AW = new AssemblyWriter(Out, *SC, M);
694 } else {
695 SC = 0; AW = 0;
696 }
697}
698
699CachedWriter::~CachedWriter() {
700 delete AW;
701 delete SC;
702}
703
704CachedWriter &CachedWriter::operator<<(const Value *V) {
705 assert(AW && SC && "CachedWriter does not have a current module!");
706 switch (V->getValueType()) {
707 case Value::ConstantVal:
708 Out << " "; AW->write(V->getType());
Chris Lattner3462ae32001-12-03 22:26:30 +0000709 Out << " " << cast<Constant>(V)->getStrValue(); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000710 case Value::FunctionArgumentVal:
Chris Lattner7db79582001-11-07 04:21:57 +0000711 AW->write(V->getType()); Out << " " << V->getName(); break;
712 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
713 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
714 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000715 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000716 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
717 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
718 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
719 }
720 return *this;
721}