blob: 842bc0912aa0ce0410bbf0af31d2d4460a91e63d [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//===----------------------------------------------------------------------===//
10
Chris Lattner7db79582001-11-07 04:21:57 +000011#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000012#include "llvm/Assembly/Writer.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000013#include "llvm/SlotCalculator.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000014#include "llvm/DerivedTypes.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000015#include "llvm/Instruction.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000016#include "llvm/Module.h"
Chris Lattnerca142372002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000019#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000020#include "llvm/iPHINode.h"
21#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000022#include "llvm/SymbolTable.h"
Chris Lattner5de22042001-11-27 00:03:19 +000023#include "Support/StringExtras.h"
24#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000025#include <algorithm>
Chris 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 Lattnerd84bb632002-04-16 21:36:08 +000031static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
32 map<const Type *, string> &TypeTable,
33 SlotCalculator *Table);
34
Chris Lattnerb86620e2001-10-29 16:37:48 +000035static const Module *getModuleFromVal(const Value *V) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000036 if (const Argument *MA = dyn_cast<const Argument>(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();
Chris Lattnerb86620e2001-10-29 16:37:48 +000045 return 0;
46}
47
Chris Lattner7bfee412001-10-29 16:05:51 +000048static SlotCalculator *createSlotCalculator(const Value *V) {
49 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000050 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000051 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000052 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
53 return new SlotCalculator(I->getParent()->getParent(), true);
54 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
55 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000056 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000057 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000058 } else if (const Function *Func = dyn_cast<const Function>(V)) {
59 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000060 }
61 return 0;
62}
Chris Lattner2f7c9632001-06-06 20:29:01 +000063
Chris Lattnerb86620e2001-10-29 16:37:48 +000064
65// If the module has a symbol table, take all global types and stuff their
66// names into the TypeNames map.
67//
68static void fillTypeNameTable(const Module *M,
69 map<const Type *, string> &TypeNames) {
70 if (M && M->hasSymbolTable()) {
71 const SymbolTable *ST = M->getSymbolTable();
72 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
73 if (PI != ST->end()) {
74 SymbolTable::type_const_iterator I = PI->second.begin();
75 for (; I != PI->second.end(); ++I) {
76 // As a heuristic, don't insert pointer to primitive types, because
77 // they are used too often to have a single useful name.
78 //
79 const Type *Ty = cast<const Type>(I->second);
80 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +000081 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +000082 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +000083 }
84 }
85 }
86}
87
88
89
90static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
91 map<const Type *, string> &TypeNames) {
92 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
93
94 // Check to see if the type is named.
95 map<const Type *, string>::iterator I = TypeNames.find(Ty);
96 if (I != TypeNames.end()) return I->second;
97
98 // Check to see if the Type is already on the stack...
99 unsigned Slot = 0, CurSize = TypeStack.size();
100 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
101
102 // This is another base case for the recursion. In this case, we know
103 // that we have looped back to a type that we have previously visited.
104 // Generate the appropriate upreference to handle this.
105 //
106 if (Slot < CurSize)
107 return "\\" + utostr(CurSize-Slot); // Here's the upreference
108
109 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
110
111 string Result;
112 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000113 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000114 const FunctionType *FTy = cast<const FunctionType>(Ty);
115 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000116 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000117 I = FTy->getParamTypes().begin(),
118 E = FTy->getParamTypes().end(); I != E; ++I) {
119 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000120 Result += ", ";
121 Result += calcTypeName(*I, TypeStack, TypeNames);
122 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000123 if (FTy->isVarArg()) {
124 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000125 Result += "...";
126 }
127 Result += ")";
128 break;
129 }
130 case Type::StructTyID: {
131 const StructType *STy = cast<const StructType>(Ty);
132 Result = "{ ";
133 for (StructType::ElementTypes::const_iterator
134 I = STy->getElementTypes().begin(),
135 E = STy->getElementTypes().end(); I != E; ++I) {
136 if (I != STy->getElementTypes().begin())
137 Result += ", ";
138 Result += calcTypeName(*I, TypeStack, TypeNames);
139 }
140 Result += " }";
141 break;
142 }
143 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000144 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000145 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000146 break;
147 case Type::ArrayTyID: {
148 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000149 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000150 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
151 break;
152 }
153 default:
Vikram S. Adveb952b542002-07-14 23:14:45 +0000154 Result = "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000155 }
156
157 TypeStack.pop_back(); // Remove self from stack...
158 return Result;
159}
160
161
162// printTypeInt - The internal guts of printing out a type that has a
163// potentially named portion.
164//
165static ostream &printTypeInt(ostream &Out, const Type *Ty,
166 map<const Type *, string> &TypeNames) {
167 // Primitive types always print out their description, regardless of whether
168 // they have been named or not.
169 //
170 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
171
172 // Check to see if the type is named.
173 map<const Type *, string>::iterator I = TypeNames.find(Ty);
174 if (I != TypeNames.end()) return Out << I->second;
175
176 // Otherwise we have a type that has not been named but is a derived type.
177 // Carefully recurse the type hierarchy to print out any contained symbolic
178 // names.
179 //
180 vector<const Type *> TypeStack;
181 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000182 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000183 return Out << TypeName;
184}
185
Chris Lattner34b95182001-10-31 04:33:19 +0000186
Chris Lattnerb86620e2001-10-29 16:37:48 +0000187// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
188// type, iff there is an entry in the modules symbol table for the specified
189// type or one of it's component types. This is slower than a simple x << Type;
190//
191ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
192 Out << " ";
193
194 // If they want us to print out a type, attempt to make it symbolic if there
195 // is a symbol table in the module...
196 if (M && M->hasSymbolTable()) {
197 map<const Type *, string> TypeNames;
198 fillTypeNameTable(M, TypeNames);
199
Chris Lattner72f866e2001-10-29 16:40:32 +0000200 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000201 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000202 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000203 }
204}
205
Chris Lattnerd84bb632002-04-16 21:36:08 +0000206static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
207 map<const Type *, string> &TypeTable,
208 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000209 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
210 Out << (CB == ConstantBool::True ? "true" : "false");
211 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
212 Out << CI->getValue();
213 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
214 Out << CI->getValue();
215 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
216 // We would like to output the FP constant value in exponential notation,
217 // but we cannot do this if doing so will lose precision. Check here to
218 // make sure that we only output it in exponential format if we can parse
219 // the value back and get the same value.
220 //
221 std::string StrVal = ftostr(CFP->getValue());
222
223 // Check to make sure that the stringized number is not some string like
224 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
225 // the string matches the "[-+]?[0-9]" regex.
226 //
227 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
228 ((StrVal[0] == '-' || StrVal[0] == '+') &&
229 (StrVal[0] >= '0' && StrVal[0] <= '9')))
230 // Reparse stringized version!
231 if (atof(StrVal.c_str()) == CFP->getValue()) {
232 Out << StrVal; return;
233 }
234
235 // Otherwise we could not reparse it to exactly the same value, so we must
236 // output the string in hexadecimal format!
237 //
238 // Behave nicely in the face of C TBAA rules... see:
239 // http://www.nullstone.com/htmls/category/aliastyp.htm
240 //
241 double Val = CFP->getValue();
242 char *Ptr = (char*)&Val;
243 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
244 "assuming that double is 64 bits!");
245 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
246
247 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
248 // As a special case, print the array as a string if it is an array of
249 // ubytes or an array of sbytes with positive values.
250 //
251 const Type *ETy = CA->getType()->getElementType();
252 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
253
254 if (ETy == Type::SByteTy)
255 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
256 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
257 isString = false;
258 break;
259 }
260
261 if (isString) {
262 Out << "c\"";
263 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
264 unsigned char C = (ETy == Type::SByteTy) ?
265 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
266 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
267
268 if (isprint(C)) {
269 Out << C;
270 } else {
271 Out << '\\'
272 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
273 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
274 }
275 }
276 Out << "\"";
277
278 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000279 Out << "[";
280 if (CA->getNumOperands()) {
281 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000282 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000283 WriteAsOperandInternal(Out, CA->getOperand(0),
284 PrintName, TypeTable, Table);
285 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
286 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000287 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000288 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
289 TypeTable, Table);
290 }
291 }
292 Out << " ]";
293 }
294 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
295 Out << "{";
296 if (CS->getNumOperands()) {
297 Out << " ";
298 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
299
300 WriteAsOperandInternal(Out, CS->getOperand(0),
301 PrintName, TypeTable, Table);
302
303 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
304 Out << ", ";
305 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
306
307 WriteAsOperandInternal(Out, CS->getOperand(i),
308 PrintName, TypeTable, Table);
309 }
310 }
311
312 Out << " }";
313 } else if (isa<ConstantPointerNull>(CV)) {
314 Out << "null";
315
Chris Lattner113f4f42002-06-25 16:13:24 +0000316 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000317 const GlobalValue *V = PR->getValue();
318 if (V->hasName()) {
319 Out << "%" << V->getName();
320 } else if (Table) {
321 int Slot = Table->getValSlot(V);
322 if (Slot >= 0)
323 Out << "%" << Slot;
324 else
325 Out << "<pointer reference badref>";
326 } else {
327 Out << "<pointer reference without context info>";
328 }
Vikram S. Adveb952b542002-07-14 23:14:45 +0000329
330 } else if (const ConstantExpr *CE=dyn_cast<ConstantExpr>(CV)) {
331 Out << CE->getOpcodeName();
332
333 bool isGEP = CE->getOpcode() == Instruction::GetElementPtr;
334 Out << (isGEP? " (" : " ");
335
336 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
337 printTypeInt(Out, (*OI)->getType(), TypeTable);
338 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
339 if (OI+1 != CE->op_end())
340 Out << ", "; // ((isGEP && OI == CE->op_begin())? " " : ", ");
341 }
342
343 if (isGEP)
344 Out << ")";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000345 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000346 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000347 }
348}
349
350
351// WriteAsOperand - Write the name of the specified value out to the specified
352// ostream. This can be useful when you just want to print int %reg126, not the
353// whole instruction that generated it.
354//
355static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
356 map<const Type *, string> &TypeTable,
357 SlotCalculator *Table) {
358 Out << " ";
359 if (PrintName && V->hasName()) {
360 Out << "%" << V->getName();
361 } else {
362 if (const Constant *CV = dyn_cast<const Constant>(V)) {
363 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
364 } else {
365 int Slot;
366 if (Table) {
367 Slot = Table->getValSlot(V);
368 } else {
369 if (const Type *Ty = dyn_cast<const Type>(V)) {
370 Out << Ty->getDescription();
371 return;
372 }
373
374 Table = createSlotCalculator(V);
375 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
376
377 Slot = Table->getValSlot(V);
378 delete Table;
379 }
380 if (Slot >= 0) Out << "%" << Slot;
381 else if (PrintName)
382 Out << "<badref>"; // Not embeded into a location?
383 }
384 }
385}
386
387
Chris Lattnerb86620e2001-10-29 16:37:48 +0000388
389// WriteAsOperand - Write the name of the specified value out to the specified
390// ostream. This can be useful when you just want to print int %reg126, not the
391// whole instruction that generated it.
392//
393ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000394 bool PrintName, const Module *Context) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000395 map<const Type *, string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000396 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000397
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000398 if (Context && Context->hasSymbolTable())
399 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000400
401 if (PrintType)
402 printTypeInt(Out, V->getType(), TypeNames);
403
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000404 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000405 return Out;
406}
407
408
Chris Lattner2e9fee42001-07-12 23:35:26 +0000409
Chris Lattnerfee714f2001-09-07 16:36:04 +0000410class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000411 ostream &Out;
412 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000413 const Module *TheModule;
414 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000415public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000416 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
417 : Out(o), Table(Tab), TheModule(M) {
418
419 // If the module has a symbol table, take all global types and stuff their
420 // names into the TypeNames map.
421 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000422 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000423 }
424
Chris Lattner7bfee412001-10-29 16:05:51 +0000425 inline void write(const Module *M) { printModule(M); }
426 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000427 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000428 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000429 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000430 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000431 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000432
Chris Lattner1e194682002-04-18 18:53:13 +0000433 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
434
Chris Lattner2f7c9632001-06-06 20:29:01 +0000435private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000436 void printModule(const Module *M);
437 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000438 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000439 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000440 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000441 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000442 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000443 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000444
445 // printType - Go to extreme measures to attempt to print out a short,
446 // symbolic version of a type name.
447 //
448 ostream &printType(const Type *Ty) {
449 return printTypeInt(Out, Ty, TypeNames);
450 }
451
452 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
453 // without considering any symbolic types that we may have equal to it.
454 //
455 ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000456
Chris Lattner862e3382001-10-13 06:42:36 +0000457 // printInfoComment - Print a little comment after the instruction indicating
458 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000459 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000460};
461
462
Chris Lattnerd816b532002-04-13 20:53:41 +0000463// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
464// without considering any symbolic types that we may have equal to it.
465//
466ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000467 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000468 printType(FTy->getReturnType()) << " (";
469 for (FunctionType::ParamTypes::const_iterator
470 I = FTy->getParamTypes().begin(),
471 E = FTy->getParamTypes().end(); I != E; ++I) {
472 if (I != FTy->getParamTypes().begin())
473 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000474 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000475 }
476 if (FTy->isVarArg()) {
477 if (!FTy->getParamTypes().empty()) Out << ", ";
478 Out << "...";
479 }
480 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000481 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000482 Out << "{ ";
483 for (StructType::ElementTypes::const_iterator
484 I = STy->getElementTypes().begin(),
485 E = STy->getElementTypes().end(); I != E; ++I) {
486 if (I != STy->getElementTypes().begin())
487 Out << ", ";
488 printType(*I);
489 }
490 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000491 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000492 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000493 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000494 Out << "[" << ATy->getNumElements() << " x ";
495 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000496 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner2904f442002-05-26 20:17:54 +0000497 Out << OTy->getDescription();
Chris Lattnerd816b532002-04-13 20:53:41 +0000498 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000499 if (!Ty->isPrimitiveType())
500 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000501 printType(Ty);
502 }
503 return Out;
504}
505
506
Chris Lattnerfee714f2001-09-07 16:36:04 +0000507void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
508 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000509 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000510 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000511}
512
Chris Lattner2f7c9632001-06-06 20:29:01 +0000513
Chris Lattner7bfee412001-10-29 16:05:51 +0000514void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000515 // Loop over the symbol table, emitting all named constants...
516 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000517 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000518
Chris Lattner113f4f42002-06-25 16:13:24 +0000519 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
520 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000521
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000522 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000523
Chris Lattner6915f8f2002-04-07 22:49:37 +0000524 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000525 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
526 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000527}
528
Chris Lattner7bfee412001-10-29 16:05:51 +0000529void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000530 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000531
Chris Lattner841d8b92001-11-26 18:54:16 +0000532 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000533 if (!GV->hasInitializer()) Out << "uninitialized ";
534
Chris Lattner7bfee412001-10-29 16:05:51 +0000535 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000536 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000537
538 if (GV->hasInitializer())
539 writeOperand(GV->getInitializer(), false, false);
540
Chris Lattner113f4f42002-06-25 16:13:24 +0000541 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000542 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000543}
544
Chris Lattnerfee714f2001-09-07 16:36:04 +0000545
Chris Lattner7bfee412001-10-29 16:05:51 +0000546// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000547// if a named constant is found, emit it's declaration...
548//
Chris Lattner7bfee412001-10-29 16:05:51 +0000549void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000550 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
551 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
552 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
553
554 for (; I != End; ++I) {
555 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000556 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000557 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000558 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000559 Out << "\t%" << I->first << " = type ";
560
561 // Make sure we print out at least one level of the type structure, so
562 // that we do not get %FILE = type %FILE
563 //
564 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000565 }
566 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000567 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000568}
569
570
Chris Lattner7bfee412001-10-29 16:05:51 +0000571// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000572//
Chris Lattner3462ae32001-12-03 22:26:30 +0000573void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000574 // Don't print out unnamed constants, they will be inlined
575 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000576
Chris Lattneree998be2001-07-26 16:29:38 +0000577 // Print out name...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000578 Out << "\t%" << CPV->getName() << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579
580 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000581 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000582
Chris Lattner113f4f42002-06-25 16:13:24 +0000583 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000584 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000585}
586
Chris Lattner6915f8f2002-04-07 22:49:37 +0000587// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588//
Chris Lattner113f4f42002-06-25 16:13:24 +0000589void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000590 // Print out the return type and name...
Chris Lattner113f4f42002-06-25 16:13:24 +0000591 Out << "\n" << (F->isExternal() ? "declare " : "")
592 << (F->hasInternalLinkage() ? "internal " : "");
593 printType(F->getReturnType()) << " %" << F->getName() << "(";
594 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000595
Chris Lattner7bfee412001-10-29 16:05:51 +0000596 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000597 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000598
Chris Lattner113f4f42002-06-25 16:13:24 +0000599 if (!F->isExternal()) {
600 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
601 printArgument(I);
Chris Lattner862e3382001-10-13 06:42:36 +0000602 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000603 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000604 for (FunctionType::ParamTypes::const_iterator I = FT->getParamTypes().begin(),
605 E = FT->getParamTypes().end(); I != E; ++I) {
606 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000607 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000608 }
609 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000610
611 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000612 if (FT->isVarArg()) {
613 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000614 Out << "..."; // Output varargs portion of signature!
615 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000616 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000617
Chris Lattner113f4f42002-06-25 16:13:24 +0000618 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000619 Out << "\n";
620 } else {
621 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000622
Chris Lattner6915f8f2002-04-07 22:49:37 +0000623 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000624 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
625 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000626
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000627 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000628 }
629
Chris Lattner6915f8f2002-04-07 22:49:37 +0000630 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000631}
632
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000633// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000634// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000635//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000636void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000637 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000638 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000639
640 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000641 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000642
643 // Output name, if available...
644 if (Arg->hasName())
645 Out << " %" << Arg->getName();
646 else if (Table.getValSlot(Arg) < 0)
647 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000648}
649
Chris Lattner7bfee412001-10-29 16:05:51 +0000650// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000651//
Chris Lattner7bfee412001-10-29 16:05:51 +0000652void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000653 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner408dbdb2002-05-14 16:02:05 +0000654 Out << "\n" << BB->getName() << ":\t\t\t\t\t;[#uses="
655 << BB->use_size() << "]"; // Output # uses
656 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000657 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000658 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000659 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000660 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000661 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000662 Out << "<badref>";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000663 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000664 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000665
666 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000667
Chris Lattnerfee714f2001-09-07 16:36:04 +0000668 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000669 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
670 printInstruction(*I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000671}
672
Chris Lattner862e3382001-10-13 06:42:36 +0000673
674// printInfoComment - Print a little comment after the instruction indicating
675// which slot it occupies.
676//
Chris Lattner113f4f42002-06-25 16:13:24 +0000677void AssemblyWriter::printInfoComment(const Value &V) {
678 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000679 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000680 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000681
Chris Lattner113f4f42002-06-25 16:13:24 +0000682 if (!V.hasName()) {
683 int Slot = Table.getValSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000684 if (Slot >= 0) Out << ":" << Slot;
685 else Out << ":<badref>";
686 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000687 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000688 }
689}
690
Chris Lattner7bfee412001-10-29 16:05:51 +0000691// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000692//
Chris Lattner113f4f42002-06-25 16:13:24 +0000693void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000694 Out << "\t";
695
696 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000697 if (I.hasName())
698 Out << "%" << I.getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000699
700 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000701 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000702
703 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000704 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000705
706 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000707 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
708 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000709 Out << ",";
710 writeOperand(Operand, true);
711 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000712 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000713
Chris Lattner8d48df22002-04-13 18:34:38 +0000714 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000715 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000716 writeOperand(Operand , true); Out << ",";
717 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000718
Chris Lattner113f4f42002-06-25 16:13:24 +0000719 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000721 writeOperand(I.getOperand(op ), true); Out << ",";
722 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000723 }
724 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000725 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000726 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000727 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000728 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000729
Chris Lattner113f4f42002-06-25 16:13:24 +0000730 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000731 if (op) Out << ", ";
732 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000733 writeOperand(I.getOperand(op ), false); Out << ",";
734 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000735 }
Chris Lattner862e3382001-10-13 06:42:36 +0000736 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000737 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000738 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000739 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000740 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000741 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
742
743 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000744 // only do this if the first argument is a pointer to a nonvararg function,
745 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000746 //
Chris Lattner8d48df22002-04-13 18:34:38 +0000747 if (RetTy && MTy && !MTy->isVarArg() &&
748 (!isa<PointerType>(RetTy) ||
749 !isa<FunctionType>(cast<PointerType>(RetTy)))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000750 Out << " "; printType(RetTy);
751 writeOperand(Operand, false);
752 } else {
753 writeOperand(Operand, true);
754 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000755 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000756 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
757 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000758 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000759 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000760 }
761
762 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000763 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner862e3382001-10-13 06:42:36 +0000764 // TODO: Should try to print out short form of the Invoke instruction
765 writeOperand(Operand, true);
766 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000767 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
768 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000769 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000770 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000771 }
772
773 Out << " )\n\t\t\tto";
774 writeOperand(II->getNormalDest(), true);
775 Out << " except";
776 writeOperand(II->getExceptionalDest(), true);
777
Chris Lattner113f4f42002-06-25 16:13:24 +0000778 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000779 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000780 printType(AI->getType()->getElementType());
781 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000782 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000783 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000784 }
Chris Lattner862e3382001-10-13 06:42:36 +0000785 } else if (isa<CastInst>(I)) {
Chris Lattner143e5a42002-05-22 22:29:26 +0000786 if (Operand) writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000787 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000788 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000789 } else if (Operand) { // Print the normal way...
790
791 // PrintAllTypes - Instructions who have operands of all the same type
792 // omit the type from all but the first operand. If the instruction has
793 // different type operands (for example br), then they are all printed.
794 bool PrintAllTypes = false;
795 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000796
Chris Lattner113f4f42002-06-25 16:13:24 +0000797 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
798 Operand = I.getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000799 if (Operand->getType() != TheType) {
800 PrintAllTypes = true; // We have differing types! Print them all!
801 break;
802 }
803 }
804
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000805 // Shift Left & Right print both types even for Ubyte LHS
806 if (isa<ShiftInst>(I)) PrintAllTypes = true;
807
Chris Lattner7bfee412001-10-29 16:05:51 +0000808 if (!PrintAllTypes) {
809 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000810 printType(I.getOperand(0)->getType());
Chris Lattner7bfee412001-10-29 16:05:51 +0000811 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000812
Chris Lattner113f4f42002-06-25 16:13:24 +0000813 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000814 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000815 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000816 }
817 }
818
Chris Lattner862e3382001-10-13 06:42:36 +0000819 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000820 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000821}
822
823
824//===----------------------------------------------------------------------===//
825// External Interface declarations
826//===----------------------------------------------------------------------===//
827
828
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000829void Module::print(std::ostream &o) const {
830 SlotCalculator SlotTable(this, true);
831 AssemblyWriter W(o, SlotTable, this);
832 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000833}
834
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000835void GlobalVariable::print(std::ostream &o) const {
836 SlotCalculator SlotTable(getParent(), true);
837 AssemblyWriter W(o, SlotTable, getParent());
838 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000839}
840
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000841void Function::print(std::ostream &o) const {
842 SlotCalculator SlotTable(getParent(), true);
843 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000844
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000845 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000846}
847
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000848void BasicBlock::print(std::ostream &o) const {
849 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000850 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000851 getParent() ? getParent()->getParent() : 0);
852 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000853}
854
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000855void Instruction::print(std::ostream &o) const {
856 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000857 SlotCalculator SlotTable(F, true);
858 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000859
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000860 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000861}
Chris Lattner7db79582001-11-07 04:21:57 +0000862
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000863void Constant::print(std::ostream &o) const {
864 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner1e194682002-04-18 18:53:13 +0000865 o << " " << getType()->getDescription() << " ";
866
867 map<const Type *, string> TypeTable;
868 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000869}
870
871void Type::print(std::ostream &o) const {
872 if (this == 0)
873 o << "<null Type>";
874 else
875 o << getDescription();
876}
877
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000878void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000879 o << getType() << " " << getName();
880}
881
882void Value::dump() const { print(std::cerr); }
883
884//===----------------------------------------------------------------------===//
885// CachedWriter Class Implementation
886//===----------------------------------------------------------------------===//
887
Chris Lattner7db79582001-11-07 04:21:57 +0000888void CachedWriter::setModule(const Module *M) {
889 delete SC; delete AW;
890 if (M) {
891 SC = new SlotCalculator(M, true);
892 AW = new AssemblyWriter(Out, *SC, M);
893 } else {
894 SC = 0; AW = 0;
895 }
896}
897
898CachedWriter::~CachedWriter() {
899 delete AW;
900 delete SC;
901}
902
903CachedWriter &CachedWriter::operator<<(const Value *V) {
904 assert(AW && SC && "CachedWriter does not have a current module!");
905 switch (V->getValueType()) {
906 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +0000907 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000908 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
909 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
910 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000911 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000912 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000913 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
914 }
915 return *this;
916}