blob: f92fac512add6add91757b7041bce2ff85d6a5b4 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Writer.cpp - Library for Printing VM assembly files ------*- C++ -*--=//
2//
3// This library implements the functionality defined in llvm/Assembly/Writer.h
4//
5// This library uses the Analysis library to figure out offsets for
6// variables in the method tables...
7//
8// TODO: print out the type name instead of the full type if a particular type
9// is in the symbol table...
10//
11//===----------------------------------------------------------------------===//
12
Chris Lattner7db79582001-11-07 04:21:57 +000013#include "llvm/Assembly/CachedWriter.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "llvm/Analysis/SlotCalculator.h"
15#include "llvm/Module.h"
Chris Lattner57698e22002-03-26 18:01:55 +000016#include "llvm/Function.h"
Chris Lattnerda975502001-09-10 07:58:01 +000017#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/BasicBlock.h"
Chris Lattner3462ae32001-12-03 22:26:30 +000019#include "llvm/ConstantVals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000021#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000022#include "llvm/iPHINode.h"
23#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000024#include "llvm/SymbolTable.h"
Chris Lattner5de22042001-11-27 00:03:19 +000025#include "Support/StringExtras.h"
26#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000027#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000028#include <map>
Chris Lattner7f74a562002-01-20 22:54:45 +000029using std::string;
30using std::map;
31using std::vector;
32using std::ostream;
Chris Lattner7bfee412001-10-29 16:05:51 +000033
Chris Lattnerb86620e2001-10-29 16:37:48 +000034static const Module *getModuleFromVal(const Value *V) {
Chris Lattner57698e22002-03-26 18:01:55 +000035 if (const FunctionArgument *MA = dyn_cast<const FunctionArgument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000036 return MA->getParent() ? MA->getParent()->getParent() : 0;
37 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
38 return BB->getParent() ? BB->getParent()->getParent() : 0;
39 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000040 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000041 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000042 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000043 return GV->getParent();
44 else if (const Module *Mod = dyn_cast<const Module>(V))
45 return Mod;
46 return 0;
47}
48
Chris Lattner7bfee412001-10-29 16:05:51 +000049static SlotCalculator *createSlotCalculator(const Value *V) {
50 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner57698e22002-03-26 18:01:55 +000051 if (const FunctionArgument *FA = dyn_cast<const FunctionArgument>(V)) {
52 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000053 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
54 return new SlotCalculator(I->getParent()->getParent(), true);
55 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
56 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000057 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000058 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000059 } else if (const Function *Func = dyn_cast<const Function>(V)) {
60 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000061 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
62 return new SlotCalculator(Mod, true);
63 }
64 return 0;
65}
Chris Lattner2f7c9632001-06-06 20:29:01 +000066
Chris Lattner5e5abe32001-07-20 19:15:21 +000067// WriteAsOperand - Write the name of the specified value out to the specified
68// ostream. This can be useful when you just want to print int %reg126, not the
69// whole instruction that generated it.
70//
Chris Lattnerb86620e2001-10-29 16:37:48 +000071static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
72 SlotCalculator *Table) {
Chris Lattnerfee714f2001-09-07 16:36:04 +000073 if (PrintName && V->hasName()) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000074 Out << " %" << V->getName();
75 } else {
Chris Lattner3462ae32001-12-03 22:26:30 +000076 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner5e5abe32001-07-20 19:15:21 +000077 Out << " " << CPV->getStrValue();
78 } else {
79 int Slot;
80 if (Table) {
81 Slot = Table->getValSlot(V);
82 } else {
Chris Lattnerb86620e2001-10-29 16:37:48 +000083 if (const Type *Ty = dyn_cast<const Type>(V)) {
84 Out << " " << Ty->getDescription();
85 return;
86 }
Chris Lattner7bfee412001-10-29 16:05:51 +000087
88 Table = createSlotCalculator(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +000089 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +000090
Chris Lattner5e5abe32001-07-20 19:15:21 +000091 Slot = Table->getValSlot(V);
92 delete Table;
93 }
94 if (Slot >= 0) Out << " %" << Slot;
95 else if (PrintName)
96 Out << "<badref>"; // Not embeded into a location?
97 }
98 }
Chris Lattnerb86620e2001-10-29 16:37:48 +000099}
100
101
102// If the module has a symbol table, take all global types and stuff their
103// names into the TypeNames map.
104//
105static void fillTypeNameTable(const Module *M,
106 map<const Type *, string> &TypeNames) {
107 if (M && M->hasSymbolTable()) {
108 const SymbolTable *ST = M->getSymbolTable();
109 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
110 if (PI != ST->end()) {
111 SymbolTable::type_const_iterator I = PI->second.begin();
112 for (; I != PI->second.end(); ++I) {
113 // As a heuristic, don't insert pointer to primitive types, because
114 // they are used too often to have a single useful name.
115 //
116 const Type *Ty = cast<const Type>(I->second);
117 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +0000118 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +0000119 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000120 }
121 }
122 }
123}
124
125
126
127static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
128 map<const Type *, string> &TypeNames) {
129 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
130
131 // Check to see if the type is named.
132 map<const Type *, string>::iterator I = TypeNames.find(Ty);
133 if (I != TypeNames.end()) return I->second;
134
135 // Check to see if the Type is already on the stack...
136 unsigned Slot = 0, CurSize = TypeStack.size();
137 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
138
139 // This is another base case for the recursion. In this case, we know
140 // that we have looped back to a type that we have previously visited.
141 // Generate the appropriate upreference to handle this.
142 //
143 if (Slot < CurSize)
144 return "\\" + utostr(CurSize-Slot); // Here's the upreference
145
146 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
147
148 string Result;
149 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000150 case Type::FunctionTyID: {
151 const FunctionType *MTy = cast<const FunctionType>(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000152 Result = calcTypeName(MTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000153 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerb86620e2001-10-29 16:37:48 +0000154 I = MTy->getParamTypes().begin(),
155 E = MTy->getParamTypes().end(); I != E; ++I) {
156 if (I != MTy->getParamTypes().begin())
157 Result += ", ";
158 Result += calcTypeName(*I, TypeStack, TypeNames);
159 }
160 if (MTy->isVarArg()) {
161 if (!MTy->getParamTypes().empty()) Result += ", ";
162 Result += "...";
163 }
164 Result += ")";
165 break;
166 }
167 case Type::StructTyID: {
168 const StructType *STy = cast<const StructType>(Ty);
169 Result = "{ ";
170 for (StructType::ElementTypes::const_iterator
171 I = STy->getElementTypes().begin(),
172 E = STy->getElementTypes().end(); I != E; ++I) {
173 if (I != STy->getElementTypes().begin())
174 Result += ", ";
175 Result += calcTypeName(*I, TypeStack, TypeNames);
176 }
177 Result += " }";
178 break;
179 }
180 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000181 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattnerb86620e2001-10-29 16:37:48 +0000182 TypeStack, TypeNames) + " *";
183 break;
184 case Type::ArrayTyID: {
185 const ArrayType *ATy = cast<const ArrayType>(Ty);
186 int NumElements = ATy->getNumElements();
187 Result = "[";
188 if (NumElements != -1) Result += itostr(NumElements) + " x ";
189 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
190 break;
191 }
192 default:
193 assert(0 && "Unhandled case in getTypeProps!");
194 Result = "<error>";
195 }
196
197 TypeStack.pop_back(); // Remove self from stack...
198 return Result;
199}
200
201
202// printTypeInt - The internal guts of printing out a type that has a
203// potentially named portion.
204//
205static ostream &printTypeInt(ostream &Out, const Type *Ty,
206 map<const Type *, string> &TypeNames) {
207 // Primitive types always print out their description, regardless of whether
208 // they have been named or not.
209 //
210 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
211
212 // Check to see if the type is named.
213 map<const Type *, string>::iterator I = TypeNames.find(Ty);
214 if (I != TypeNames.end()) return Out << I->second;
215
216 // Otherwise we have a type that has not been named but is a derived type.
217 // Carefully recurse the type hierarchy to print out any contained symbolic
218 // names.
219 //
220 vector<const Type *> TypeStack;
221 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000222 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000223 return Out << TypeName;
224}
225
Chris Lattner34b95182001-10-31 04:33:19 +0000226
Chris Lattnerb86620e2001-10-29 16:37:48 +0000227// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
228// type, iff there is an entry in the modules symbol table for the specified
229// type or one of it's component types. This is slower than a simple x << Type;
230//
231ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
232 Out << " ";
233
234 // If they want us to print out a type, attempt to make it symbolic if there
235 // is a symbol table in the module...
236 if (M && M->hasSymbolTable()) {
237 map<const Type *, string> TypeNames;
238 fillTypeNameTable(M, TypeNames);
239
Chris Lattner72f866e2001-10-29 16:40:32 +0000240 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000241 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000242 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000243 }
244}
245
246
247// WriteAsOperand - Write the name of the specified value out to the specified
248// ostream. This can be useful when you just want to print int %reg126, not the
249// whole instruction that generated it.
250//
251ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
252 bool PrintName, SlotCalculator *Table) {
Chris Lattner72f866e2001-10-29 16:40:32 +0000253 if (PrintType)
254 WriteTypeSymbolic(Out, V->getType(), getModuleFromVal(V));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000255
256 WriteAsOperandInternal(Out, V, PrintName, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000257 return Out;
258}
259
260
Chris Lattner2e9fee42001-07-12 23:35:26 +0000261
Chris Lattnerfee714f2001-09-07 16:36:04 +0000262class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000263 ostream &Out;
264 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000265 const Module *TheModule;
266 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000267public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000268 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
269 : Out(o), Table(Tab), TheModule(M) {
270
271 // If the module has a symbol table, take all global types and stuff their
272 // names into the TypeNames map.
273 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000274 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000275 }
276
Chris Lattner7bfee412001-10-29 16:05:51 +0000277 inline void write(const Module *M) { printModule(M); }
278 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000279 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000280 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
281 inline void write(const Instruction *I) { printInstruction(I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000282 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000283 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000284
Chris Lattner2f7c9632001-06-06 20:29:01 +0000285private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000286 void printModule(const Module *M);
287 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000288 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000289 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000290 void printFunction(const Function *F);
291 void printFunctionArgument(const FunctionArgument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000292 void printBasicBlock(const BasicBlock *BB);
293 void printInstruction(const Instruction *I);
294 ostream &printType(const Type *Ty);
295
Chris Lattner2f7c9632001-06-06 20:29:01 +0000296 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
Chris Lattner862e3382001-10-13 06:42:36 +0000297
298 // printInfoComment - Print a little comment after the instruction indicating
299 // which slot it occupies.
300 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000301};
302
303
Chris Lattnerfee714f2001-09-07 16:36:04 +0000304void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
305 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000306 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerb86620e2001-10-29 16:37:48 +0000307 WriteAsOperandInternal(Out, Operand, PrintName, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000308}
309
Chris Lattner2f7c9632001-06-06 20:29:01 +0000310
Chris Lattner7bfee412001-10-29 16:05:51 +0000311void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000312 // Loop over the symbol table, emitting all named constants...
313 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000314 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000315
316 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000317 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000318
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000319 Out << "implementation\n";
320
Chris Lattnerfee714f2001-09-07 16:36:04 +0000321 // Output all of the methods...
Chris Lattner57698e22002-03-26 18:01:55 +0000322 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000323}
324
Chris Lattner7bfee412001-10-29 16:05:51 +0000325void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000326 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000327
Chris Lattner841d8b92001-11-26 18:54:16 +0000328 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000329 if (!GV->hasInitializer()) Out << "uninitialized ";
330
Chris Lattner7bfee412001-10-29 16:05:51 +0000331 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000332 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000333
334 if (GV->hasInitializer())
335 writeOperand(GV->getInitializer(), false, false);
336
Chris Lattner862e3382001-10-13 06:42:36 +0000337 printInfoComment(GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000338 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000339}
340
Chris Lattnerfee714f2001-09-07 16:36:04 +0000341
Chris Lattner7bfee412001-10-29 16:05:51 +0000342// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000343// if a named constant is found, emit it's declaration...
344//
Chris Lattner7bfee412001-10-29 16:05:51 +0000345void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000346 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
347 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
348 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
349
350 for (; I != End; ++I) {
351 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000352 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000353 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000354 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000355 Out << "\t%" << I->first << " = type " << Ty->getDescription() << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000356 }
357 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000358 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000359}
360
361
Chris Lattner7bfee412001-10-29 16:05:51 +0000362// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000363//
Chris Lattner3462ae32001-12-03 22:26:30 +0000364void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000365 // Don't print out unnamed constants, they will be inlined
366 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000367
Chris Lattneree998be2001-07-26 16:29:38 +0000368 // Print out name...
369 Out << "\t%" << CPV->getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000370
Chris Lattneree998be2001-07-26 16:29:38 +0000371 // Print out the constant type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000372 printType(CPV->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000373
374 // Write the value out now...
375 writeOperand(CPV, false, false);
376
377 if (!CPV->hasName() && CPV->getType() != Type::VoidTy) {
378 int Slot = Table.getValSlot(CPV); // Print out the def slot taken...
Chris Lattner7bfee412001-10-29 16:05:51 +0000379 Out << "\t\t; <";
380 printType(CPV->getType()) << ">:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000381 if (Slot >= 0) Out << Slot;
382 else Out << "<badref>";
383 }
384
Chris Lattner7f74a562002-01-20 22:54:45 +0000385 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000386}
387
Chris Lattner57698e22002-03-26 18:01:55 +0000388// printFunction - Print all aspects of a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000389//
Chris Lattner57698e22002-03-26 18:01:55 +0000390void AssemblyWriter::printFunction(const Function *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000391 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000392 Out << "\n" << (M->isExternal() ? "declare " : "")
393 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner7bfee412001-10-29 16:05:51 +0000394 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000395 Table.incorporateMethod(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000396
Chris Lattner7bfee412001-10-29 16:05:51 +0000397 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000398 const FunctionType *MT = M->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000399
Chris Lattner862e3382001-10-13 06:42:36 +0000400 if (!M->isExternal()) {
401 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner57698e22002-03-26 18:01:55 +0000402 bind_obj(this, &AssemblyWriter::printFunctionArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000403 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000404 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000405 const FunctionType *MT = M->getFunctionType();
406 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
Chris Lattner862e3382001-10-13 06:42:36 +0000407 E = MT->getParamTypes().end(); I != E; ++I) {
408 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000409 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000410 }
411 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000412
413 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000414 if (MT->isVarArg()) {
415 if (MT->getParamTypes().size()) Out << ", ";
416 Out << "..."; // Output varargs portion of signature!
417 }
418 Out << ")\n";
419
420 if (!M->isExternal()) {
421 // Loop over the symbol table, emitting all named constants...
422 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000423 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000424
425 Out << "begin";
426
427 // Output all of its basic blocks... for the method
428 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000429 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000430
Chris Lattnera7620d92001-07-15 06:35:59 +0000431 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000432 }
433
434 Table.purgeMethod();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000435}
436
Chris Lattner57698e22002-03-26 18:01:55 +0000437// printFunctionArgument - This member is called for every argument that
Chris Lattner2f7c9632001-06-06 20:29:01 +0000438// is passed into the method. Simply print it out
439//
Chris Lattner57698e22002-03-26 18:01:55 +0000440void AssemblyWriter::printFunctionArgument(const FunctionArgument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000441 // Insert commas as we go... the first arg doesn't get a comma
442 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
443
444 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000445 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000446
447 // Output name, if available...
448 if (Arg->hasName())
449 Out << " %" << Arg->getName();
450 else if (Table.getValSlot(Arg) < 0)
451 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000452}
453
Chris Lattner7bfee412001-10-29 16:05:51 +0000454// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000455//
Chris Lattner7bfee412001-10-29 16:05:51 +0000456void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000457 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000458 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000459 } else {
460 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000461 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000462 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000463 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000464 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000465 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000466 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000467 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000468
Chris Lattnerfee714f2001-09-07 16:36:04 +0000469 // Output all of the instructions in the basic block...
470 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000471 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000472}
473
Chris Lattner862e3382001-10-13 06:42:36 +0000474
475// printInfoComment - Print a little comment after the instruction indicating
476// which slot it occupies.
477//
478void AssemblyWriter::printInfoComment(const Value *V) {
479 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000480 Out << "\t\t; <";
481 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000482
483 if (!V->hasName()) {
484 int Slot = Table.getValSlot(V); // Print out the def slot taken...
485 if (Slot >= 0) Out << ":" << Slot;
486 else Out << ":<badref>";
487 }
Chris Lattner3cb3a1f2001-12-14 16:29:12 +0000488 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000489 }
490}
491
Chris Lattner7bfee412001-10-29 16:05:51 +0000492// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000493//
Chris Lattner7bfee412001-10-29 16:05:51 +0000494void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000495 Out << "\t";
496
497 // Print out name if it exists...
498 if (I && I->hasName())
499 Out << "%" << I->getName() << " = ";
500
501 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000502 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000503
504 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000505 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000506
507 // Special case conditional branches to swizzle the condition out to the front
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000508 if (I->getOpcode() == Instruction::Br && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000509 writeOperand(I->getOperand(2), true);
510 Out << ",";
511 writeOperand(Operand, true);
512 Out << ",";
513 writeOperand(I->getOperand(1), true);
514
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000515 } else if (I->getOpcode() == Instruction::Switch) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000516 // Special case switch statement to get formatting nice and correct...
517 writeOperand(Operand , true); Out << ",";
518 writeOperand(I->getOperand(1), true); Out << " [";
519
Chris Lattnera073acb2001-07-07 08:36:50 +0000520 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000521 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000522 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000523 writeOperand(I->getOperand(op+1), true);
524 }
525 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000526 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000527 Out << " ";
Chris Lattner29644b32001-11-06 01:48:45 +0000528 printType(I->getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000529 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000530
Chris Lattner29644b32001-11-06 01:48:45 +0000531 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
532 if (op) Out << ", ";
533 Out << "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000534 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000535 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000536 }
Chris Lattner862e3382001-10-13 06:42:36 +0000537 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000538 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000539 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000540 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000541 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000542 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
543
544 // If possible, print out the short form of the call instruction, but we can
545 // only do this if the first argument is a pointer to a nonvararg method,
546 // and if the value returned is not a pointer to a method.
547 //
548 if (RetTy && !MTy->isVarArg() &&
Chris Lattner91db5822002-03-29 03:44:36 +0000549 (!isa<PointerType>(RetTy)||!isa<FunctionType>(cast<PointerType>(RetTy)))){
Chris Lattner2f2d9472001-11-06 21:28:12 +0000550 Out << " "; printType(RetTy);
551 writeOperand(Operand, false);
552 } else {
553 writeOperand(Operand, true);
554 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000555 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000556 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
557 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000558 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000559 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000560 }
561
562 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000563 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
564 // TODO: Should try to print out short form of the Invoke instruction
565 writeOperand(Operand, true);
566 Out << "(";
567 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
568 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
569 Out << ",";
570 writeOperand(I->getOperand(op), true);
571 }
572
573 Out << " )\n\t\t\tto";
574 writeOperand(II->getNormalDest(), true);
575 Out << " except";
576 writeOperand(II->getExceptionalDest(), true);
577
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000578 } else if (I->getOpcode() == Instruction::Malloc ||
579 I->getOpcode() == Instruction::Alloca) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000580 Out << " ";
Chris Lattner2413b162001-12-04 00:03:30 +0000581 printType(cast<const PointerType>(I->getType())->getElementType());
Chris Lattnera073acb2001-07-07 08:36:50 +0000582 if (I->getNumOperands()) {
583 Out << ",";
584 writeOperand(I->getOperand(0), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000585 }
Chris Lattner862e3382001-10-13 06:42:36 +0000586 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000587 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000588 Out << " to ";
589 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000590 } else if (Operand) { // Print the normal way...
591
592 // PrintAllTypes - Instructions who have operands of all the same type
593 // omit the type from all but the first operand. If the instruction has
594 // different type operands (for example br), then they are all printed.
595 bool PrintAllTypes = false;
596 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000597
Chris Lattnera073acb2001-07-07 08:36:50 +0000598 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
599 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000600 if (Operand->getType() != TheType) {
601 PrintAllTypes = true; // We have differing types! Print them all!
602 break;
603 }
604 }
605
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000606 // Shift Left & Right print both types even for Ubyte LHS
607 if (isa<ShiftInst>(I)) PrintAllTypes = true;
608
Chris Lattner7bfee412001-10-29 16:05:51 +0000609 if (!PrintAllTypes) {
610 Out << " ";
611 printType(I->getOperand(0)->getType());
612 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000613
Chris Lattnera073acb2001-07-07 08:36:50 +0000614 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000615 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000616 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000617 }
618 }
619
Chris Lattner862e3382001-10-13 06:42:36 +0000620 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000621 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000622}
623
624
Chris Lattner7bfee412001-10-29 16:05:51 +0000625// printType - Go to extreme measures to attempt to print out a short, symbolic
626// version of a type name.
627//
628ostream &AssemblyWriter::printType(const Type *Ty) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000629 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner7bfee412001-10-29 16:05:51 +0000630}
631
632
Chris Lattner2f7c9632001-06-06 20:29:01 +0000633//===----------------------------------------------------------------------===//
634// External Interface declarations
635//===----------------------------------------------------------------------===//
636
637
638
639void WriteToAssembly(const Module *M, ostream &o) {
640 if (M == 0) { o << "<null> module\n"; return; }
641 SlotCalculator SlotTable(M, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000642 AssemblyWriter W(o, SlotTable, M);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000643
644 W.write(M);
645}
646
Chris Lattneradfe0d12001-09-10 20:08:19 +0000647void WriteToAssembly(const GlobalVariable *G, ostream &o) {
648 if (G == 0) { o << "<null> global variable\n"; return; }
649 SlotCalculator SlotTable(G->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000650 AssemblyWriter W(o, SlotTable, G->getParent());
Chris Lattneradfe0d12001-09-10 20:08:19 +0000651 W.write(G);
652}
653
Chris Lattner57698e22002-03-26 18:01:55 +0000654void WriteToAssembly(const Function *F, ostream &o) {
655 if (F == 0) { o << "<null> function\n"; return; }
656 SlotCalculator SlotTable(F->getParent(), true);
657 AssemblyWriter W(o, SlotTable, F->getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000658
Chris Lattner57698e22002-03-26 18:01:55 +0000659 W.write(F);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660}
661
662
663void WriteToAssembly(const BasicBlock *BB, ostream &o) {
664 if (BB == 0) { o << "<null> basic block\n"; return; }
665
666 SlotCalculator SlotTable(BB->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000667 AssemblyWriter W(o, SlotTable,
668 BB->getParent() ? BB->getParent()->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000669
670 W.write(BB);
671}
672
Chris Lattner3462ae32001-12-03 22:26:30 +0000673void WriteToAssembly(const Constant *CPV, ostream &o) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000674 if (CPV == 0) { o << "<null> constant pool value\n"; return; }
Chris Lattner7bfee412001-10-29 16:05:51 +0000675 o << " " << CPV->getType()->getDescription() << " " << CPV->getStrValue();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000676}
677
678void WriteToAssembly(const Instruction *I, ostream &o) {
679 if (I == 0) { o << "<null> instruction\n"; return; }
680
Chris Lattner57698e22002-03-26 18:01:55 +0000681 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
682 SlotCalculator SlotTable(F, true);
683 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000684
685 W.write(I);
686}
Chris Lattner7db79582001-11-07 04:21:57 +0000687
688void CachedWriter::setModule(const Module *M) {
689 delete SC; delete AW;
690 if (M) {
691 SC = new SlotCalculator(M, true);
692 AW = new AssemblyWriter(Out, *SC, M);
693 } else {
694 SC = 0; AW = 0;
695 }
696}
697
698CachedWriter::~CachedWriter() {
699 delete AW;
700 delete SC;
701}
702
703CachedWriter &CachedWriter::operator<<(const Value *V) {
704 assert(AW && SC && "CachedWriter does not have a current module!");
705 switch (V->getValueType()) {
706 case Value::ConstantVal:
707 Out << " "; AW->write(V->getType());
Chris Lattner3462ae32001-12-03 22:26:30 +0000708 Out << " " << cast<Constant>(V)->getStrValue(); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000709 case Value::FunctionArgumentVal:
Chris Lattner7db79582001-11-07 04:21:57 +0000710 AW->write(V->getType()); Out << " " << V->getName(); break;
711 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
712 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
713 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000714 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000715 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
716 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
717 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
718 }
719 return *this;
720}