blob: 7aefe32a22b8dfa99beeff5689549bacbde09e97 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This library implements the functionality defined in llvm/Assembly/Writer.h
4//
Chris Lattner02b93992002-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 Lattner00950542001-06-06 20:29:01 +00009//===----------------------------------------------------------------------===//
10
Chris Lattnerda1fbcc2001-11-07 04:21:57 +000011#include "llvm/Assembly/CachedWriter.h"
Chris Lattner75cf7cf2002-04-08 22:03:40 +000012#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000013#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000014#include "llvm/SlotCalculator.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000015#include "llvm/DerivedTypes.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000016#include "llvm/Instruction.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/iMemory.h"
Chris Lattnere02fa852001-10-13 06:42:36 +000020#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
22#include "llvm/iOther.h"
Chris Lattner007377f2001-09-07 16:36:04 +000023#include "llvm/SymbolTable.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/StringExtras.h"
25#include "Support/STLExtras.h"
Chris Lattner007377f2001-09-07 16:36:04 +000026#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::string;
28using std::map;
29using std::vector;
30using std::ostream;
Chris Lattnerc1824992001-10-29 16:05:51 +000031
Chris Lattnerf082b802002-07-23 18:07:49 +000032static RegisterPass<PrintModulePass> X("printm", "Print module to stderr");
33static RegisterPass<PrintFunctionPass> Y("print", "Print function to stderr");
34
Chris Lattner7a716ad2002-04-16 21:36:08 +000035static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
36 map<const Type *, string> &TypeTable,
37 SlotCalculator *Table);
38
Chris Lattner207b5bc2001-10-29 16:37:48 +000039static const Module *getModuleFromVal(const Value *V) {
Chris Lattner73e21422002-04-09 19:48:49 +000040 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000041 return MA->getParent() ? MA->getParent()->getParent() : 0;
42 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
43 return BB->getParent() ? BB->getParent()->getParent() : 0;
44 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +000045 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +000046 return M ? M->getParent() : 0;
Chris Lattner79df7c02002-03-26 18:01:55 +000047 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000048 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +000049 return 0;
50}
51
Chris Lattnerc1824992001-10-29 16:05:51 +000052static SlotCalculator *createSlotCalculator(const Value *V) {
53 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner73e21422002-04-09 19:48:49 +000054 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +000055 return new SlotCalculator(FA->getParent(), true);
Chris Lattnerc1824992001-10-29 16:05:51 +000056 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
57 return new SlotCalculator(I->getParent()->getParent(), true);
58 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
59 return new SlotCalculator(BB->getParent(), true);
Chris Lattner79df7c02002-03-26 18:01:55 +000060 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattnerc1824992001-10-29 16:05:51 +000061 return new SlotCalculator(GV->getParent(), true);
Chris Lattner79df7c02002-03-26 18:01:55 +000062 } else if (const Function *Func = dyn_cast<const Function>(V)) {
63 return new SlotCalculator(Func, true);
Chris Lattnerc1824992001-10-29 16:05:51 +000064 }
65 return 0;
66}
Chris Lattner00950542001-06-06 20:29:01 +000067
Chris Lattner207b5bc2001-10-29 16:37:48 +000068
69// If the module has a symbol table, take all global types and stuff their
70// names into the TypeNames map.
71//
72static void fillTypeNameTable(const Module *M,
73 map<const Type *, string> &TypeNames) {
74 if (M && M->hasSymbolTable()) {
75 const SymbolTable *ST = M->getSymbolTable();
76 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
77 if (PI != ST->end()) {
78 SymbolTable::type_const_iterator I = PI->second.begin();
79 for (; I != PI->second.end(); ++I) {
80 // As a heuristic, don't insert pointer to primitive types, because
81 // they are used too often to have a single useful name.
82 //
83 const Type *Ty = cast<const Type>(I->second);
84 if (!isa<PointerType>(Ty) ||
Chris Lattner7a176752001-12-04 00:03:30 +000085 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner697954c2002-01-20 22:54:45 +000086 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattner207b5bc2001-10-29 16:37:48 +000087 }
88 }
89 }
90}
91
92
93
94static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
95 map<const Type *, string> &TypeNames) {
96 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
97
98 // Check to see if the type is named.
99 map<const Type *, string>::iterator I = TypeNames.find(Ty);
100 if (I != TypeNames.end()) return I->second;
101
102 // Check to see if the Type is already on the stack...
103 unsigned Slot = 0, CurSize = TypeStack.size();
104 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
105
106 // This is another base case for the recursion. In this case, we know
107 // that we have looped back to a type that we have previously visited.
108 // Generate the appropriate upreference to handle this.
109 //
110 if (Slot < CurSize)
111 return "\\" + utostr(CurSize-Slot); // Here's the upreference
112
113 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
114
115 string Result;
116 switch (Ty->getPrimitiveID()) {
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000117 case Type::FunctionTyID: {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000118 const FunctionType *FTy = cast<const FunctionType>(Ty);
119 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000120 for (FunctionType::ParamTypes::const_iterator
Chris Lattner2761e9f2002-04-13 20:53:41 +0000121 I = FTy->getParamTypes().begin(),
122 E = FTy->getParamTypes().end(); I != E; ++I) {
123 if (I != FTy->getParamTypes().begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000124 Result += ", ";
125 Result += calcTypeName(*I, TypeStack, TypeNames);
126 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000127 if (FTy->isVarArg()) {
128 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000129 Result += "...";
130 }
131 Result += ")";
132 break;
133 }
134 case Type::StructTyID: {
135 const StructType *STy = cast<const StructType>(Ty);
136 Result = "{ ";
137 for (StructType::ElementTypes::const_iterator
138 I = STy->getElementTypes().begin(),
139 E = STy->getElementTypes().end(); I != E; ++I) {
140 if (I != STy->getElementTypes().begin())
141 Result += ", ";
142 Result += calcTypeName(*I, TypeStack, TypeNames);
143 }
144 Result += " }";
145 break;
146 }
147 case Type::PointerTyID:
Chris Lattner7a176752001-12-04 00:03:30 +0000148 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner02b93992002-04-12 18:21:53 +0000149 TypeStack, TypeNames) + "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000150 break;
151 case Type::ArrayTyID: {
152 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattnerff5c2962002-04-13 21:11:04 +0000153 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000154 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
155 break;
156 }
157 default:
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000158 Result = "<unrecognized-type>";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000159 }
160
161 TypeStack.pop_back(); // Remove self from stack...
162 return Result;
163}
164
165
166// printTypeInt - The internal guts of printing out a type that has a
167// potentially named portion.
168//
169static ostream &printTypeInt(ostream &Out, const Type *Ty,
170 map<const Type *, string> &TypeNames) {
171 // Primitive types always print out their description, regardless of whether
172 // they have been named or not.
173 //
174 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
175
176 // Check to see if the type is named.
177 map<const Type *, string>::iterator I = TypeNames.find(Ty);
178 if (I != TypeNames.end()) return Out << I->second;
179
180 // Otherwise we have a type that has not been named but is a derived type.
181 // Carefully recurse the type hierarchy to print out any contained symbolic
182 // names.
183 //
184 vector<const Type *> TypeStack;
185 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner697954c2002-01-20 22:54:45 +0000186 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattner207b5bc2001-10-29 16:37:48 +0000187 return Out << TypeName;
188}
189
Chris Lattnere51e03b2001-10-31 04:33:19 +0000190
Chris Lattner207b5bc2001-10-29 16:37:48 +0000191// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
192// type, iff there is an entry in the modules symbol table for the specified
193// type or one of it's component types. This is slower than a simple x << Type;
194//
195ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
196 Out << " ";
197
198 // If they want us to print out a type, attempt to make it symbolic if there
199 // is a symbol table in the module...
200 if (M && M->hasSymbolTable()) {
201 map<const Type *, string> TypeNames;
202 fillTypeNameTable(M, TypeNames);
203
Chris Lattner7b8660d2001-10-29 16:40:32 +0000204 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000205 } else {
Chris Lattner7b8660d2001-10-29 16:40:32 +0000206 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000207 }
208}
209
Chris Lattner7a716ad2002-04-16 21:36:08 +0000210static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
211 map<const Type *, string> &TypeTable,
212 SlotCalculator *Table) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000213 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
214 Out << (CB == ConstantBool::True ? "true" : "false");
215 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
216 Out << CI->getValue();
217 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
218 Out << CI->getValue();
219 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
220 // We would like to output the FP constant value in exponential notation,
221 // but we cannot do this if doing so will lose precision. Check here to
222 // make sure that we only output it in exponential format if we can parse
223 // the value back and get the same value.
224 //
225 std::string StrVal = ftostr(CFP->getValue());
226
227 // Check to make sure that the stringized number is not some string like
228 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
229 // the string matches the "[-+]?[0-9]" regex.
230 //
231 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
232 ((StrVal[0] == '-' || StrVal[0] == '+') &&
233 (StrVal[0] >= '0' && StrVal[0] <= '9')))
234 // Reparse stringized version!
235 if (atof(StrVal.c_str()) == CFP->getValue()) {
236 Out << StrVal; return;
237 }
238
239 // Otherwise we could not reparse it to exactly the same value, so we must
240 // output the string in hexadecimal format!
241 //
242 // Behave nicely in the face of C TBAA rules... see:
243 // http://www.nullstone.com/htmls/category/aliastyp.htm
244 //
245 double Val = CFP->getValue();
246 char *Ptr = (char*)&Val;
247 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
248 "assuming that double is 64 bits!");
249 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
250
251 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
252 // As a special case, print the array as a string if it is an array of
253 // ubytes or an array of sbytes with positive values.
254 //
255 const Type *ETy = CA->getType()->getElementType();
256 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
257
258 if (ETy == Type::SByteTy)
259 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
260 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
261 isString = false;
262 break;
263 }
264
265 if (isString) {
266 Out << "c\"";
267 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
268 unsigned char C = (ETy == Type::SByteTy) ?
269 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
270 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
271
272 if (isprint(C)) {
273 Out << C;
274 } else {
275 Out << '\\'
276 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
277 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
278 }
279 }
280 Out << "\"";
281
282 } else { // Cannot output in string format...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000283 Out << "[";
284 if (CA->getNumOperands()) {
285 Out << " ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000286 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000287 WriteAsOperandInternal(Out, CA->getOperand(0),
288 PrintName, TypeTable, Table);
289 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
290 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000291 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000292 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
293 TypeTable, Table);
294 }
295 }
296 Out << " ]";
297 }
298 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
299 Out << "{";
300 if (CS->getNumOperands()) {
301 Out << " ";
302 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
303
304 WriteAsOperandInternal(Out, CS->getOperand(0),
305 PrintName, TypeTable, Table);
306
307 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
308 Out << ", ";
309 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
310
311 WriteAsOperandInternal(Out, CS->getOperand(i),
312 PrintName, TypeTable, Table);
313 }
314 }
315
316 Out << " }";
317 } else if (isa<ConstantPointerNull>(CV)) {
318 Out << "null";
319
Chris Lattner7e708292002-06-25 16:13:24 +0000320 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000321 const GlobalValue *V = PR->getValue();
322 if (V->hasName()) {
323 Out << "%" << V->getName();
324 } else if (Table) {
325 int Slot = Table->getValSlot(V);
326 if (Slot >= 0)
327 Out << "%" << Slot;
328 else
329 Out << "<pointer reference badref>";
330 } else {
331 Out << "<pointer reference without context info>";
332 }
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000333
334 } else if (const ConstantExpr *CE=dyn_cast<ConstantExpr>(CV)) {
335 Out << CE->getOpcodeName();
336
337 bool isGEP = CE->getOpcode() == Instruction::GetElementPtr;
338 Out << (isGEP? " (" : " ");
339
340 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
341 printTypeInt(Out, (*OI)->getType(), TypeTable);
342 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
343 if (OI+1 != CE->op_end())
344 Out << ", "; // ((isGEP && OI == CE->op_begin())? " " : ", ");
345 }
346
347 if (isGEP)
348 Out << ")";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000349 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000350 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000351 }
352}
353
354
355// WriteAsOperand - Write the name of the specified value out to the specified
356// ostream. This can be useful when you just want to print int %reg126, not the
357// whole instruction that generated it.
358//
359static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
360 map<const Type *, string> &TypeTable,
361 SlotCalculator *Table) {
362 Out << " ";
363 if (PrintName && V->hasName()) {
364 Out << "%" << V->getName();
365 } else {
366 if (const Constant *CV = dyn_cast<const Constant>(V)) {
367 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
368 } else {
369 int Slot;
370 if (Table) {
371 Slot = Table->getValSlot(V);
372 } else {
373 if (const Type *Ty = dyn_cast<const Type>(V)) {
374 Out << Ty->getDescription();
375 return;
376 }
377
378 Table = createSlotCalculator(V);
379 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
380
381 Slot = Table->getValSlot(V);
382 delete Table;
383 }
384 if (Slot >= 0) Out << "%" << Slot;
385 else if (PrintName)
386 Out << "<badref>"; // Not embeded into a location?
387 }
388 }
389}
390
391
Chris Lattner207b5bc2001-10-29 16:37:48 +0000392
393// WriteAsOperand - Write the name of the specified value out to the specified
394// ostream. This can be useful when you just want to print int %reg126, not the
395// whole instruction that generated it.
396//
397ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
Chris Lattner607dc682002-07-10 16:48:17 +0000398 bool PrintName, const Module *Context) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000399 map<const Type *, string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000400 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000401
Chris Lattner607dc682002-07-10 16:48:17 +0000402 if (Context && Context->hasSymbolTable())
403 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000404
405 if (PrintType)
406 printTypeInt(Out, V->getType(), TypeNames);
407
Chris Lattner607dc682002-07-10 16:48:17 +0000408 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000409 return Out;
410}
411
412
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000413
Chris Lattner007377f2001-09-07 16:36:04 +0000414class AssemblyWriter {
Chris Lattner00950542001-06-06 20:29:01 +0000415 ostream &Out;
416 SlotCalculator &Table;
Chris Lattnerc1824992001-10-29 16:05:51 +0000417 const Module *TheModule;
418 map<const Type *, string> TypeNames;
Chris Lattner00950542001-06-06 20:29:01 +0000419public:
Chris Lattnerc1824992001-10-29 16:05:51 +0000420 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
421 : Out(o), Table(Tab), TheModule(M) {
422
423 // If the module has a symbol table, take all global types and stuff their
424 // names into the TypeNames map.
425 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000426 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000427 }
428
Chris Lattnerc1824992001-10-29 16:05:51 +0000429 inline void write(const Module *M) { printModule(M); }
430 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000431 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000432 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000433 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000434 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000435 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000436
Chris Lattner66e810b2002-04-18 18:53:13 +0000437 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
438
Chris Lattner00950542001-06-06 20:29:01 +0000439private :
Chris Lattnerc1824992001-10-29 16:05:51 +0000440 void printModule(const Module *M);
441 void printSymbolTable(const SymbolTable &ST);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000442 void printConstant(const Constant *CPV);
Chris Lattnerc1824992001-10-29 16:05:51 +0000443 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000444 void printFunction(const Function *F);
Chris Lattner73e21422002-04-09 19:48:49 +0000445 void printArgument(const Argument *FA);
Chris Lattnerc1824992001-10-29 16:05:51 +0000446 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000447 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000448
449 // printType - Go to extreme measures to attempt to print out a short,
450 // symbolic version of a type name.
451 //
452 ostream &printType(const Type *Ty) {
453 return printTypeInt(Out, Ty, TypeNames);
454 }
455
456 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
457 // without considering any symbolic types that we may have equal to it.
458 //
459 ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000460
Chris Lattnere02fa852001-10-13 06:42:36 +0000461 // printInfoComment - Print a little comment after the instruction indicating
462 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000463 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000464};
465
466
Chris Lattner2761e9f2002-04-13 20:53:41 +0000467// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
468// without considering any symbolic types that we may have equal to it.
469//
470ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner7e708292002-06-25 16:13:24 +0000471 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000472 printType(FTy->getReturnType()) << " (";
473 for (FunctionType::ParamTypes::const_iterator
474 I = FTy->getParamTypes().begin(),
475 E = FTy->getParamTypes().end(); I != E; ++I) {
476 if (I != FTy->getParamTypes().begin())
477 Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000478 printType(*I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000479 }
480 if (FTy->isVarArg()) {
481 if (!FTy->getParamTypes().empty()) Out << ", ";
482 Out << "...";
483 }
484 Out << ")";
Chris Lattner7e708292002-06-25 16:13:24 +0000485 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000486 Out << "{ ";
487 for (StructType::ElementTypes::const_iterator
488 I = STy->getElementTypes().begin(),
489 E = STy->getElementTypes().end(); I != E; ++I) {
490 if (I != STy->getElementTypes().begin())
491 Out << ", ";
492 printType(*I);
493 }
494 Out << " }";
Chris Lattner7e708292002-06-25 16:13:24 +0000495 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000496 printType(PTy->getElementType()) << "*";
Chris Lattner7e708292002-06-25 16:13:24 +0000497 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000498 Out << "[" << ATy->getNumElements() << " x ";
499 printType(ATy->getElementType()) << "]";
Chris Lattner7e708292002-06-25 16:13:24 +0000500 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner9e77f772002-05-26 20:17:54 +0000501 Out << OTy->getDescription();
Chris Lattner2761e9f2002-04-13 20:53:41 +0000502 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000503 if (!Ty->isPrimitiveType())
504 Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000505 printType(Ty);
506 }
507 return Out;
508}
509
510
Chris Lattner007377f2001-09-07 16:36:04 +0000511void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
512 bool PrintName) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000513 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000514 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000515}
516
Chris Lattner00950542001-06-06 20:29:01 +0000517
Chris Lattnerc1824992001-10-29 16:05:51 +0000518void AssemblyWriter::printModule(const Module *M) {
Chris Lattner007377f2001-09-07 16:36:04 +0000519 // Loop over the symbol table, emitting all named constants...
520 if (M->hasSymbolTable())
Chris Lattnerc1824992001-10-29 16:05:51 +0000521 printSymbolTable(*M->getSymbolTable());
Chris Lattner70cc3392001-09-10 07:58:01 +0000522
Chris Lattner7e708292002-06-25 16:13:24 +0000523 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
524 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000525
Chris Lattner03e2acb2002-05-06 03:00:40 +0000526 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve5efa3cc2001-09-18 12:48:16 +0000527
Chris Lattnerb5794002002-04-07 22:49:37 +0000528 // Output all of the functions...
Chris Lattner7e708292002-06-25 16:13:24 +0000529 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
530 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000531}
532
Chris Lattnerc1824992001-10-29 16:05:51 +0000533void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner70cc3392001-09-10 07:58:01 +0000534 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000535
Chris Lattnerdda71962001-11-26 18:54:16 +0000536 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000537 if (!GV->hasInitializer()) Out << "uninitialized ";
538
Chris Lattnerc1824992001-10-29 16:05:51 +0000539 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000540 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000541
542 if (GV->hasInitializer())
543 writeOperand(GV->getInitializer(), false, false);
544
Chris Lattner7e708292002-06-25 16:13:24 +0000545 printInfoComment(*GV);
Chris Lattner697954c2002-01-20 22:54:45 +0000546 Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000547}
548
Chris Lattner007377f2001-09-07 16:36:04 +0000549
Chris Lattnerc1824992001-10-29 16:05:51 +0000550// printSymbolTable - Run through symbol table looking for named constants
Chris Lattner007377f2001-09-07 16:36:04 +0000551// if a named constant is found, emit it's declaration...
552//
Chris Lattnerc1824992001-10-29 16:05:51 +0000553void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner007377f2001-09-07 16:36:04 +0000554 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
555 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
556 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
557
558 for (; I != End; ++I) {
559 const Value *V = I->second;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000560 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000561 printConstant(CPV);
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000562 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000563 Out << "\t%" << I->first << " = type ";
564
565 // Make sure we print out at least one level of the type structure, so
566 // that we do not get %FILE = type %FILE
567 //
568 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000569 }
570 }
Chris Lattner739a56d2001-07-15 06:35:59 +0000571 }
Chris Lattner00950542001-06-06 20:29:01 +0000572}
573
574
Chris Lattnerc1824992001-10-29 16:05:51 +0000575// printConstant - Print out a constant pool entry...
Chris Lattner00950542001-06-06 20:29:01 +0000576//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000577void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattner007377f2001-09-07 16:36:04 +0000578 // Don't print out unnamed constants, they will be inlined
579 if (!CPV->hasName()) return;
Chris Lattner00950542001-06-06 20:29:01 +0000580
Chris Lattner1333ed52001-07-26 16:29:38 +0000581 // Print out name...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000582 Out << "\t%" << CPV->getName() << " =";
Chris Lattner00950542001-06-06 20:29:01 +0000583
584 // Write the value out now...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000585 writeOperand(CPV, true, false);
Chris Lattner00950542001-06-06 20:29:01 +0000586
Chris Lattner7e708292002-06-25 16:13:24 +0000587 printInfoComment(*CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000588 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000589}
590
Chris Lattnerb5794002002-04-07 22:49:37 +0000591// printFunction - Print all aspects of a function.
Chris Lattner00950542001-06-06 20:29:01 +0000592//
Chris Lattner7e708292002-06-25 16:13:24 +0000593void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000594 // Print out the return type and name...
Chris Lattner7e708292002-06-25 16:13:24 +0000595 Out << "\n" << (F->isExternal() ? "declare " : "")
596 << (F->hasInternalLinkage() ? "internal " : "");
597 printType(F->getReturnType()) << " %" << F->getName() << "(";
598 Table.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000599
Chris Lattnerc1824992001-10-29 16:05:51 +0000600 // Loop over the arguments, printing them...
Chris Lattner7e708292002-06-25 16:13:24 +0000601 const FunctionType *FT = F->getFunctionType();
Chris Lattner007377f2001-09-07 16:36:04 +0000602
Chris Lattner7e708292002-06-25 16:13:24 +0000603 if (!F->isExternal()) {
604 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
605 printArgument(I);
Chris Lattnere02fa852001-10-13 06:42:36 +0000606 } else {
Chris Lattnerc1824992001-10-29 16:05:51 +0000607 // Loop over the arguments, printing them...
Chris Lattner7e708292002-06-25 16:13:24 +0000608 for (FunctionType::ParamTypes::const_iterator I = FT->getParamTypes().begin(),
609 E = FT->getParamTypes().end(); I != E; ++I) {
610 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattnerc1824992001-10-29 16:05:51 +0000611 printType(*I);
Chris Lattnere02fa852001-10-13 06:42:36 +0000612 }
613 }
Chris Lattner007377f2001-09-07 16:36:04 +0000614
615 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000616 if (FT->isVarArg()) {
617 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattner007377f2001-09-07 16:36:04 +0000618 Out << "..."; // Output varargs portion of signature!
619 }
Chris Lattner03e2acb2002-05-06 03:00:40 +0000620 Out << ")";
Chris Lattner007377f2001-09-07 16:36:04 +0000621
Chris Lattner7e708292002-06-25 16:13:24 +0000622 if (F->isExternal()) {
Chris Lattner03e2acb2002-05-06 03:00:40 +0000623 Out << "\n";
624 } else {
625 Out << " {";
Chris Lattner007377f2001-09-07 16:36:04 +0000626
Chris Lattnerb5794002002-04-07 22:49:37 +0000627 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +0000628 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
629 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000630
Chris Lattner03e2acb2002-05-06 03:00:40 +0000631 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000632 }
633
Chris Lattnerb5794002002-04-07 22:49:37 +0000634 Table.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000635}
636
Chris Lattner73e21422002-04-09 19:48:49 +0000637// printArgument - This member is called for every argument that
Chris Lattnerb5794002002-04-07 22:49:37 +0000638// is passed into the function. Simply print it out
Chris Lattner00950542001-06-06 20:29:01 +0000639//
Chris Lattner73e21422002-04-09 19:48:49 +0000640void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner00950542001-06-06 20:29:01 +0000641 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner7e708292002-06-25 16:13:24 +0000642 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner00950542001-06-06 20:29:01 +0000643
644 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +0000645 printType(Arg->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000646
647 // Output name, if available...
648 if (Arg->hasName())
649 Out << " %" << Arg->getName();
650 else if (Table.getValSlot(Arg) < 0)
651 Out << "<badref>";
Chris Lattner00950542001-06-06 20:29:01 +0000652}
653
Chris Lattnerc1824992001-10-29 16:05:51 +0000654// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner00950542001-06-06 20:29:01 +0000655//
Chris Lattnerc1824992001-10-29 16:05:51 +0000656void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000657 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerafc38682002-05-14 16:02:05 +0000658 Out << "\n" << BB->getName() << ":\t\t\t\t\t;[#uses="
659 << BB->use_size() << "]"; // Output # uses
660 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner00950542001-06-06 20:29:01 +0000661 int Slot = Table.getValSlot(BB);
Chris Lattnerb9a45782001-06-07 16:58:55 +0000662 Out << "\n; <label>:";
Chris Lattner00950542001-06-06 20:29:01 +0000663 if (Slot >= 0)
Chris Lattnerb9a45782001-06-07 16:58:55 +0000664 Out << Slot; // Extra newline seperates out label's
Chris Lattner00950542001-06-06 20:29:01 +0000665 else
Chris Lattnerb9a45782001-06-07 16:58:55 +0000666 Out << "<badref>";
Chris Lattnerafc38682002-05-14 16:02:05 +0000667 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]"; // Output # uses
Chris Lattner00950542001-06-06 20:29:01 +0000668 }
Chris Lattnerafc38682002-05-14 16:02:05 +0000669
670 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000671
Chris Lattner007377f2001-09-07 16:36:04 +0000672 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +0000673 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
674 printInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000675}
676
Chris Lattnere02fa852001-10-13 06:42:36 +0000677
678// printInfoComment - Print a little comment after the instruction indicating
679// which slot it occupies.
680//
Chris Lattner7e708292002-06-25 16:13:24 +0000681void AssemblyWriter::printInfoComment(const Value &V) {
682 if (V.getType() != Type::VoidTy) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000683 Out << "\t\t; <";
Chris Lattner7e708292002-06-25 16:13:24 +0000684 printType(V.getType()) << ">";
Chris Lattnere02fa852001-10-13 06:42:36 +0000685
Chris Lattner7e708292002-06-25 16:13:24 +0000686 if (!V.hasName()) {
687 int Slot = Table.getValSlot(&V); // Print out the def slot taken...
Chris Lattnere02fa852001-10-13 06:42:36 +0000688 if (Slot >= 0) Out << ":" << Slot;
689 else Out << ":<badref>";
690 }
Chris Lattner7e708292002-06-25 16:13:24 +0000691 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +0000692 }
693}
694
Chris Lattnerc1824992001-10-29 16:05:51 +0000695// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner00950542001-06-06 20:29:01 +0000696//
Chris Lattner7e708292002-06-25 16:13:24 +0000697void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner00950542001-06-06 20:29:01 +0000698 Out << "\t";
699
700 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +0000701 if (I.hasName())
702 Out << "%" << I.getName() << " = ";
Chris Lattner00950542001-06-06 20:29:01 +0000703
704 // Print out the opcode...
Chris Lattner7e708292002-06-25 16:13:24 +0000705 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +0000706
707 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +0000708 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000709
710 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +0000711 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
712 writeOperand(I.getOperand(2), true);
Chris Lattner00950542001-06-06 20:29:01 +0000713 Out << ",";
714 writeOperand(Operand, true);
715 Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000716 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000717
Chris Lattner94dc1f22002-04-13 18:34:38 +0000718 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000719 // Special case switch statement to get formatting nice and correct...
Chris Lattner7e708292002-06-25 16:13:24 +0000720 writeOperand(Operand , true); Out << ",";
721 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +0000722
Chris Lattner7e708292002-06-25 16:13:24 +0000723 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner00950542001-06-06 20:29:01 +0000724 Out << "\n\t\t";
Chris Lattner7e708292002-06-25 16:13:24 +0000725 writeOperand(I.getOperand(op ), true); Out << ",";
726 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000727 }
728 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +0000729 } else if (isa<PHINode>(I)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000730 Out << " ";
Chris Lattner7e708292002-06-25 16:13:24 +0000731 printType(I.getType());
Chris Lattnereed1fc72001-11-06 08:33:46 +0000732 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +0000733
Chris Lattner7e708292002-06-25 16:13:24 +0000734 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner8f410ca2001-11-06 01:48:45 +0000735 if (op) Out << ", ";
736 Out << "[";
Chris Lattner7e708292002-06-25 16:13:24 +0000737 writeOperand(I.getOperand(op ), false); Out << ",";
738 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +0000739 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000740 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner00950542001-06-06 20:29:01 +0000741 Out << " void";
Chris Lattnere02fa852001-10-13 06:42:36 +0000742 } else if (isa<CallInst>(I)) {
Chris Lattner268de042001-11-06 21:28:12 +0000743 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000744 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner268de042001-11-06 21:28:12 +0000745 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
746
747 // If possible, print out the short form of the call instruction, but we can
Chris Lattnerb5794002002-04-07 22:49:37 +0000748 // only do this if the first argument is a pointer to a nonvararg function,
749 // and if the value returned is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +0000750 //
Chris Lattner94dc1f22002-04-13 18:34:38 +0000751 if (RetTy && MTy && !MTy->isVarArg() &&
752 (!isa<PointerType>(RetTy) ||
753 !isa<FunctionType>(cast<PointerType>(RetTy)))) {
Chris Lattner268de042001-11-06 21:28:12 +0000754 Out << " "; printType(RetTy);
755 writeOperand(Operand, false);
756 } else {
757 writeOperand(Operand, true);
758 }
Chris Lattner00950542001-06-06 20:29:01 +0000759 Out << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000760 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
761 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner00950542001-06-06 20:29:01 +0000762 Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000763 writeOperand(I.getOperand(op), true);
Chris Lattner00950542001-06-06 20:29:01 +0000764 }
765
766 Out << " )";
Chris Lattner7e708292002-06-25 16:13:24 +0000767 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattnere02fa852001-10-13 06:42:36 +0000768 // TODO: Should try to print out short form of the Invoke instruction
769 writeOperand(Operand, true);
770 Out << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000771 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
772 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattnere02fa852001-10-13 06:42:36 +0000773 Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000774 writeOperand(I.getOperand(op), true);
Chris Lattnere02fa852001-10-13 06:42:36 +0000775 }
776
777 Out << " )\n\t\t\tto";
778 writeOperand(II->getNormalDest(), true);
779 Out << " except";
780 writeOperand(II->getExceptionalDest(), true);
781
Chris Lattner7e708292002-06-25 16:13:24 +0000782 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000783 Out << " ";
Chris Lattner94dc1f22002-04-13 18:34:38 +0000784 printType(AI->getType()->getElementType());
785 if (AI->isArrayAllocation()) {
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000786 Out << ",";
Chris Lattner94dc1f22002-04-13 18:34:38 +0000787 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +0000788 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000789 } else if (isa<CastInst>(I)) {
Chris Lattner68e02782002-05-22 22:29:26 +0000790 if (Operand) writeOperand(Operand, true);
Chris Lattnerc1824992001-10-29 16:05:51 +0000791 Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +0000792 printType(I.getType());
Chris Lattner00950542001-06-06 20:29:01 +0000793 } else if (Operand) { // Print the normal way...
794
795 // PrintAllTypes - Instructions who have operands of all the same type
796 // omit the type from all but the first operand. If the instruction has
797 // different type operands (for example br), then they are all printed.
798 bool PrintAllTypes = false;
799 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +0000800
Chris Lattner7e708292002-06-25 16:13:24 +0000801 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
802 Operand = I.getOperand(i);
Chris Lattner00950542001-06-06 20:29:01 +0000803 if (Operand->getType() != TheType) {
804 PrintAllTypes = true; // We have differing types! Print them all!
805 break;
806 }
807 }
808
Chris Lattnerf434cd12001-10-20 09:33:10 +0000809 // Shift Left & Right print both types even for Ubyte LHS
810 if (isa<ShiftInst>(I)) PrintAllTypes = true;
811
Chris Lattnerc1824992001-10-29 16:05:51 +0000812 if (!PrintAllTypes) {
813 Out << " ";
Chris Lattner7e708292002-06-25 16:13:24 +0000814 printType(I.getOperand(0)->getType());
Chris Lattnerc1824992001-10-29 16:05:51 +0000815 }
Chris Lattner00950542001-06-06 20:29:01 +0000816
Chris Lattner7e708292002-06-25 16:13:24 +0000817 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000818 if (i) Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000819 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000820 }
821 }
822
Chris Lattnere02fa852001-10-13 06:42:36 +0000823 printInfoComment(I);
Chris Lattner697954c2002-01-20 22:54:45 +0000824 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000825}
826
827
828//===----------------------------------------------------------------------===//
829// External Interface declarations
830//===----------------------------------------------------------------------===//
831
832
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000833void Module::print(std::ostream &o) const {
834 SlotCalculator SlotTable(this, true);
835 AssemblyWriter W(o, SlotTable, this);
836 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000837}
838
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000839void GlobalVariable::print(std::ostream &o) const {
840 SlotCalculator SlotTable(getParent(), true);
841 AssemblyWriter W(o, SlotTable, getParent());
842 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +0000843}
844
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000845void Function::print(std::ostream &o) const {
846 SlotCalculator SlotTable(getParent(), true);
847 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner00950542001-06-06 20:29:01 +0000848
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000849 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000850}
851
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000852void BasicBlock::print(std::ostream &o) const {
853 SlotCalculator SlotTable(getParent(), true);
Chris Lattnerc1824992001-10-29 16:05:51 +0000854 AssemblyWriter W(o, SlotTable,
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000855 getParent() ? getParent()->getParent() : 0);
856 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000857}
858
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000859void Instruction::print(std::ostream &o) const {
860 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000861 SlotCalculator SlotTable(F, true);
862 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner00950542001-06-06 20:29:01 +0000863
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000864 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000865}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000866
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000867void Constant::print(std::ostream &o) const {
868 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner66e810b2002-04-18 18:53:13 +0000869 o << " " << getType()->getDescription() << " ";
870
871 map<const Type *, string> TypeTable;
872 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000873}
874
875void Type::print(std::ostream &o) const {
876 if (this == 0)
877 o << "<null Type>";
878 else
879 o << getDescription();
880}
881
Chris Lattner73e21422002-04-09 19:48:49 +0000882void Argument::print(std::ostream &o) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000883 o << getType() << " " << getName();
884}
885
886void Value::dump() const { print(std::cerr); }
887
888//===----------------------------------------------------------------------===//
889// CachedWriter Class Implementation
890//===----------------------------------------------------------------------===//
891
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000892void CachedWriter::setModule(const Module *M) {
893 delete SC; delete AW;
894 if (M) {
895 SC = new SlotCalculator(M, true);
896 AW = new AssemblyWriter(Out, *SC, M);
897 } else {
898 SC = 0; AW = 0;
899 }
900}
901
902CachedWriter::~CachedWriter() {
903 delete AW;
904 delete SC;
905}
906
907CachedWriter &CachedWriter::operator<<(const Value *V) {
908 assert(AW && SC && "CachedWriter does not have a current module!");
909 switch (V->getValueType()) {
910 case Value::ConstantVal:
Chris Lattner66e810b2002-04-18 18:53:13 +0000911 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000912 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
913 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
914 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner79df7c02002-03-26 18:01:55 +0000915 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000916 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000917 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
918 }
919 return *this;
920}