blob: fe66575faa0a85b10babac8608295bf120b42fc5 [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"
Chris Lattner2f7c9632001-06-06 20:29:01 +000015#include "llvm/Module.h"
Chris Lattner57698e22002-03-26 18:01:55 +000016#include "llvm/Function.h"
Chris Lattnerda975502001-09-10 07:58:01 +000017#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include "llvm/BasicBlock.h"
Chris Lattnerca142372002-04-28 19:55:58 +000019#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000021#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000022#include "llvm/iPHINode.h"
23#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000024#include "llvm/SymbolTable.h"
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000025#include "llvm/Argument.h"
Chris Lattner5de22042001-11-27 00:03:19 +000026#include "Support/StringExtras.h"
27#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000028#include <algorithm>
Chris Lattner7f74a562002-01-20 22:54:45 +000029using std::string;
30using std::map;
31using std::vector;
32using std::ostream;
Chris Lattner7bfee412001-10-29 16:05:51 +000033
Chris Lattnerd84bb632002-04-16 21:36:08 +000034static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
35 map<const Type *, string> &TypeTable,
36 SlotCalculator *Table);
37
Chris Lattnerb86620e2001-10-29 16:37:48 +000038static const Module *getModuleFromVal(const Value *V) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000039 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000040 return MA->getParent() ? MA->getParent()->getParent() : 0;
41 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
42 return BB->getParent() ? BB->getParent()->getParent() : 0;
43 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000044 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000045 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000046 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000047 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000048 return 0;
49}
50
Chris Lattner7bfee412001-10-29 16:05:51 +000051static SlotCalculator *createSlotCalculator(const Value *V) {
52 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000053 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000054 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000055 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
56 return new SlotCalculator(I->getParent()->getParent(), true);
57 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
58 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000059 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000060 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000061 } else if (const Function *Func = dyn_cast<const Function>(V)) {
62 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000063 }
64 return 0;
65}
Chris Lattner2f7c9632001-06-06 20:29:01 +000066
Chris Lattnerb86620e2001-10-29 16:37:48 +000067
68// If the module has a symbol table, take all global types and stuff their
69// names into the TypeNames map.
70//
71static void fillTypeNameTable(const Module *M,
72 map<const Type *, string> &TypeNames) {
73 if (M && M->hasSymbolTable()) {
74 const SymbolTable *ST = M->getSymbolTable();
75 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
76 if (PI != ST->end()) {
77 SymbolTable::type_const_iterator I = PI->second.begin();
78 for (; I != PI->second.end(); ++I) {
79 // As a heuristic, don't insert pointer to primitive types, because
80 // they are used too often to have a single useful name.
81 //
82 const Type *Ty = cast<const Type>(I->second);
83 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +000084 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +000085 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +000086 }
87 }
88 }
89}
90
91
92
93static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
94 map<const Type *, string> &TypeNames) {
95 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
96
97 // Check to see if the type is named.
98 map<const Type *, string>::iterator I = TypeNames.find(Ty);
99 if (I != TypeNames.end()) return I->second;
100
101 // Check to see if the Type is already on the stack...
102 unsigned Slot = 0, CurSize = TypeStack.size();
103 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
104
105 // This is another base case for the recursion. In this case, we know
106 // that we have looped back to a type that we have previously visited.
107 // Generate the appropriate upreference to handle this.
108 //
109 if (Slot < CurSize)
110 return "\\" + utostr(CurSize-Slot); // Here's the upreference
111
112 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
113
114 string Result;
115 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000116 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000117 const FunctionType *FTy = cast<const FunctionType>(Ty);
118 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000119 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000120 I = FTy->getParamTypes().begin(),
121 E = FTy->getParamTypes().end(); I != E; ++I) {
122 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000123 Result += ", ";
124 Result += calcTypeName(*I, TypeStack, TypeNames);
125 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000126 if (FTy->isVarArg()) {
127 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000128 Result += "...";
129 }
130 Result += ")";
131 break;
132 }
133 case Type::StructTyID: {
134 const StructType *STy = cast<const StructType>(Ty);
135 Result = "{ ";
136 for (StructType::ElementTypes::const_iterator
137 I = STy->getElementTypes().begin(),
138 E = STy->getElementTypes().end(); I != E; ++I) {
139 if (I != STy->getElementTypes().begin())
140 Result += ", ";
141 Result += calcTypeName(*I, TypeStack, TypeNames);
142 }
143 Result += " }";
144 break;
145 }
146 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000147 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000148 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000149 break;
150 case Type::ArrayTyID: {
151 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000152 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000153 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
154 break;
155 }
156 default:
157 assert(0 && "Unhandled case in getTypeProps!");
158 Result = "<error>";
159 }
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 Lattner7f74a562002-01-20 22:54:45 +0000186 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000187 return Out << TypeName;
188}
189
Chris Lattner34b95182001-10-31 04:33:19 +0000190
Chris Lattnerb86620e2001-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 Lattner72f866e2001-10-29 16:40:32 +0000204 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000205 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000206 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000207 }
208}
209
Chris Lattnerd84bb632002-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 Lattner1e194682002-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 Lattnerd84bb632002-04-16 21:36:08 +0000283 Out << "[";
284 if (CA->getNumOperands()) {
285 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000286 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-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 Lattner1e194682002-04-18 18:53:13 +0000291 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-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 Lattner1e194682002-04-18 18:53:13 +0000320 } else if (ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
321 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 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000333 } else {
Chris Lattner1e194682002-04-18 18:53:13 +0000334 assert(0 && "Unrecognized constant value!!!");
Chris Lattnerd84bb632002-04-16 21:36:08 +0000335 }
336}
337
338
339// WriteAsOperand - Write the name of the specified value out to the specified
340// ostream. This can be useful when you just want to print int %reg126, not the
341// whole instruction that generated it.
342//
343static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
344 map<const Type *, string> &TypeTable,
345 SlotCalculator *Table) {
346 Out << " ";
347 if (PrintName && V->hasName()) {
348 Out << "%" << V->getName();
349 } else {
350 if (const Constant *CV = dyn_cast<const Constant>(V)) {
351 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
352 } else {
353 int Slot;
354 if (Table) {
355 Slot = Table->getValSlot(V);
356 } else {
357 if (const Type *Ty = dyn_cast<const Type>(V)) {
358 Out << Ty->getDescription();
359 return;
360 }
361
362 Table = createSlotCalculator(V);
363 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
364
365 Slot = Table->getValSlot(V);
366 delete Table;
367 }
368 if (Slot >= 0) Out << "%" << Slot;
369 else if (PrintName)
370 Out << "<badref>"; // Not embeded into a location?
371 }
372 }
373}
374
375
Chris Lattnerb86620e2001-10-29 16:37:48 +0000376
377// WriteAsOperand - Write the name of the specified value out to the specified
378// ostream. This can be useful when you just want to print int %reg126, not the
379// whole instruction that generated it.
380//
381ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
382 bool PrintName, SlotCalculator *Table) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000383 map<const Type *, string> TypeNames;
384 const Module *M = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000385
Chris Lattnerd84bb632002-04-16 21:36:08 +0000386 if (M && M->hasSymbolTable())
387 fillTypeNameTable(M, TypeNames);
388
389 if (PrintType)
390 printTypeInt(Out, V->getType(), TypeNames);
391
392 WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000393 return Out;
394}
395
396
Chris Lattner2e9fee42001-07-12 23:35:26 +0000397
Chris Lattnerfee714f2001-09-07 16:36:04 +0000398class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000399 ostream &Out;
400 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000401 const Module *TheModule;
402 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000403public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000404 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
405 : Out(o), Table(Tab), TheModule(M) {
406
407 // If the module has a symbol table, take all global types and stuff their
408 // names into the TypeNames map.
409 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000410 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000411 }
412
Chris Lattner7bfee412001-10-29 16:05:51 +0000413 inline void write(const Module *M) { printModule(M); }
414 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000415 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000416 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
417 inline void write(const Instruction *I) { printInstruction(I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000418 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000419 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000420
Chris Lattner1e194682002-04-18 18:53:13 +0000421 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
422
Chris Lattner2f7c9632001-06-06 20:29:01 +0000423private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000424 void printModule(const Module *M);
425 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000426 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000427 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000428 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000429 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000430 void printBasicBlock(const BasicBlock *BB);
431 void printInstruction(const Instruction *I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000432
433 // printType - Go to extreme measures to attempt to print out a short,
434 // symbolic version of a type name.
435 //
436 ostream &printType(const Type *Ty) {
437 return printTypeInt(Out, Ty, TypeNames);
438 }
439
440 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
441 // without considering any symbolic types that we may have equal to it.
442 //
443 ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000444
Chris Lattner862e3382001-10-13 06:42:36 +0000445 // printInfoComment - Print a little comment after the instruction indicating
446 // which slot it occupies.
447 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000448};
449
450
Chris Lattnerd816b532002-04-13 20:53:41 +0000451// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
452// without considering any symbolic types that we may have equal to it.
453//
454ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
455 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
456 printType(FTy->getReturnType()) << " (";
457 for (FunctionType::ParamTypes::const_iterator
458 I = FTy->getParamTypes().begin(),
459 E = FTy->getParamTypes().end(); I != E; ++I) {
460 if (I != FTy->getParamTypes().begin())
461 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000462 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000463 }
464 if (FTy->isVarArg()) {
465 if (!FTy->getParamTypes().empty()) Out << ", ";
466 Out << "...";
467 }
468 Out << ")";
469 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
470 Out << "{ ";
471 for (StructType::ElementTypes::const_iterator
472 I = STy->getElementTypes().begin(),
473 E = STy->getElementTypes().end(); I != E; ++I) {
474 if (I != STy->getElementTypes().begin())
475 Out << ", ";
476 printType(*I);
477 }
478 Out << " }";
479 } else if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
480 printType(PTy->getElementType()) << "*";
481 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
482 Out << "[" << ATy->getNumElements() << " x ";
483 printType(ATy->getElementType()) << "]";
Chris Lattner2904f442002-05-26 20:17:54 +0000484 } else if (OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
485 Out << OTy->getDescription();
Chris Lattnerd816b532002-04-13 20:53:41 +0000486 } else {
487 assert(Ty->isPrimitiveType() && "Unknown derived type!");
488 printType(Ty);
489 }
490 return Out;
491}
492
493
Chris Lattnerfee714f2001-09-07 16:36:04 +0000494void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
495 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000496 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000497 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000498}
499
Chris Lattner2f7c9632001-06-06 20:29:01 +0000500
Chris Lattner7bfee412001-10-29 16:05:51 +0000501void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000502 // Loop over the symbol table, emitting all named constants...
503 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000504 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000505
506 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000507 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000508
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000509 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000510
Chris Lattner6915f8f2002-04-07 22:49:37 +0000511 // Output all of the functions...
Chris Lattner57698e22002-03-26 18:01:55 +0000512 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000513}
514
Chris Lattner7bfee412001-10-29 16:05:51 +0000515void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000516 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000517
Chris Lattner841d8b92001-11-26 18:54:16 +0000518 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000519 if (!GV->hasInitializer()) Out << "uninitialized ";
520
Chris Lattner7bfee412001-10-29 16:05:51 +0000521 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000522 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000523
524 if (GV->hasInitializer())
525 writeOperand(GV->getInitializer(), false, false);
526
Chris Lattner862e3382001-10-13 06:42:36 +0000527 printInfoComment(GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000528 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000529}
530
Chris Lattnerfee714f2001-09-07 16:36:04 +0000531
Chris Lattner7bfee412001-10-29 16:05:51 +0000532// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000533// if a named constant is found, emit it's declaration...
534//
Chris Lattner7bfee412001-10-29 16:05:51 +0000535void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000536 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
537 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
538 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
539
540 for (; I != End; ++I) {
541 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000542 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000543 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000544 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000545 Out << "\t%" << I->first << " = type ";
546
547 // Make sure we print out at least one level of the type structure, so
548 // that we do not get %FILE = type %FILE
549 //
550 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000551 }
552 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000553 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000554}
555
556
Chris Lattner7bfee412001-10-29 16:05:51 +0000557// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000558//
Chris Lattner3462ae32001-12-03 22:26:30 +0000559void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000560 // Don't print out unnamed constants, they will be inlined
561 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000562
Chris Lattneree998be2001-07-26 16:29:38 +0000563 // Print out name...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000564 Out << "\t%" << CPV->getName() << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000565
566 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000567 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000568
Chris Lattner1e194682002-04-18 18:53:13 +0000569 printInfoComment(CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000570 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000571}
572
Chris Lattner6915f8f2002-04-07 22:49:37 +0000573// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000574//
Chris Lattner57698e22002-03-26 18:01:55 +0000575void AssemblyWriter::printFunction(const Function *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000576 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000577 Out << "\n" << (M->isExternal() ? "declare " : "")
578 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner143e5a42002-05-22 22:29:26 +0000579 printType(M->getReturnType()) << " %" << M->getName() << "(";
Chris Lattner6915f8f2002-04-07 22:49:37 +0000580 Table.incorporateFunction(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000581
Chris Lattner7bfee412001-10-29 16:05:51 +0000582 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000583 const FunctionType *MT = M->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000584
Chris Lattner862e3382001-10-13 06:42:36 +0000585 if (!M->isExternal()) {
586 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000587 bind_obj(this, &AssemblyWriter::printArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000588 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000589 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000590 const FunctionType *MT = M->getFunctionType();
591 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
Chris Lattner862e3382001-10-13 06:42:36 +0000592 E = MT->getParamTypes().end(); I != E; ++I) {
593 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000594 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000595 }
596 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000597
598 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000599 if (MT->isVarArg()) {
600 if (MT->getParamTypes().size()) Out << ", ";
601 Out << "..."; // Output varargs portion of signature!
602 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000603 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000604
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000605 if (M->isExternal()) {
606 Out << "\n";
607 } else {
608 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000609
Chris Lattner6915f8f2002-04-07 22:49:37 +0000610 // Output all of its basic blocks... for the function
Chris Lattnerfee714f2001-09-07 16:36:04 +0000611 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000612 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000613
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000614 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000615 }
616
Chris Lattner6915f8f2002-04-07 22:49:37 +0000617 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000618}
619
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000620// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000621// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000622//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000623void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000624 // Insert commas as we go... the first arg doesn't get a comma
625 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
626
627 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000628 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000629
630 // Output name, if available...
631 if (Arg->hasName())
632 Out << " %" << Arg->getName();
633 else if (Table.getValSlot(Arg) < 0)
634 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000635}
636
Chris Lattner7bfee412001-10-29 16:05:51 +0000637// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000638//
Chris Lattner7bfee412001-10-29 16:05:51 +0000639void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000640 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner408dbdb2002-05-14 16:02:05 +0000641 Out << "\n" << BB->getName() << ":\t\t\t\t\t;[#uses="
642 << BB->use_size() << "]"; // Output # uses
643 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000644 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000645 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000646 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000647 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000648 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000649 Out << "<badref>";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000650 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000651 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000652
653 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000654
Chris Lattnerfee714f2001-09-07 16:36:04 +0000655 // Output all of the instructions in the basic block...
656 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000657 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000658}
659
Chris Lattner862e3382001-10-13 06:42:36 +0000660
661// printInfoComment - Print a little comment after the instruction indicating
662// which slot it occupies.
663//
664void AssemblyWriter::printInfoComment(const Value *V) {
665 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000666 Out << "\t\t; <";
667 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000668
669 if (!V->hasName()) {
670 int Slot = Table.getValSlot(V); // Print out the def slot taken...
671 if (Slot >= 0) Out << ":" << Slot;
672 else Out << ":<badref>";
673 }
Chris Lattner3cb3a1f2001-12-14 16:29:12 +0000674 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000675 }
676}
677
Chris Lattner7bfee412001-10-29 16:05:51 +0000678// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000679//
Chris Lattner7bfee412001-10-29 16:05:51 +0000680void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000681 Out << "\t";
682
683 // Print out name if it exists...
684 if (I && I->hasName())
685 Out << "%" << I->getName() << " = ";
686
687 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000688 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000689
690 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000691 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000692
693 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner8d48df22002-04-13 18:34:38 +0000694 if (isa<BranchInst>(I) && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000695 writeOperand(I->getOperand(2), true);
696 Out << ",";
697 writeOperand(Operand, true);
698 Out << ",";
699 writeOperand(I->getOperand(1), true);
700
Chris Lattner8d48df22002-04-13 18:34:38 +0000701 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000702 // Special case switch statement to get formatting nice and correct...
703 writeOperand(Operand , true); Out << ",";
704 writeOperand(I->getOperand(1), true); Out << " [";
705
Chris Lattnera073acb2001-07-07 08:36:50 +0000706 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000707 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000708 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000709 writeOperand(I->getOperand(op+1), true);
710 }
711 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000712 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000713 Out << " ";
Chris Lattner29644b32001-11-06 01:48:45 +0000714 printType(I->getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000715 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000716
Chris Lattner29644b32001-11-06 01:48:45 +0000717 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
718 if (op) Out << ", ";
719 Out << "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000720 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000721 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000722 }
Chris Lattner862e3382001-10-13 06:42:36 +0000723 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000724 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000725 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000726 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000727 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000728 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
729
730 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000731 // only do this if the first argument is a pointer to a nonvararg function,
732 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000733 //
Chris Lattner8d48df22002-04-13 18:34:38 +0000734 if (RetTy && MTy && !MTy->isVarArg() &&
735 (!isa<PointerType>(RetTy) ||
736 !isa<FunctionType>(cast<PointerType>(RetTy)))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000737 Out << " "; printType(RetTy);
738 writeOperand(Operand, false);
739 } else {
740 writeOperand(Operand, true);
741 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000742 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000743 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
744 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000745 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000746 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000747 }
748
749 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000750 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
751 // TODO: Should try to print out short form of the Invoke instruction
752 writeOperand(Operand, true);
753 Out << "(";
754 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
755 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
756 Out << ",";
757 writeOperand(I->getOperand(op), true);
758 }
759
760 Out << " )\n\t\t\tto";
761 writeOperand(II->getNormalDest(), true);
762 Out << " except";
763 writeOperand(II->getExceptionalDest(), true);
764
Chris Lattner8d48df22002-04-13 18:34:38 +0000765 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000766 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000767 printType(AI->getType()->getElementType());
768 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000769 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000770 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000771 }
Chris Lattner862e3382001-10-13 06:42:36 +0000772 } else if (isa<CastInst>(I)) {
Chris Lattner143e5a42002-05-22 22:29:26 +0000773 if (Operand) writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000774 Out << " to ";
775 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000776 } else if (Operand) { // Print the normal way...
777
778 // PrintAllTypes - Instructions who have operands of all the same type
779 // omit the type from all but the first operand. If the instruction has
780 // different type operands (for example br), then they are all printed.
781 bool PrintAllTypes = false;
782 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000783
Chris Lattnera073acb2001-07-07 08:36:50 +0000784 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
785 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000786 if (Operand->getType() != TheType) {
787 PrintAllTypes = true; // We have differing types! Print them all!
788 break;
789 }
790 }
791
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000792 // Shift Left & Right print both types even for Ubyte LHS
793 if (isa<ShiftInst>(I)) PrintAllTypes = true;
794
Chris Lattner7bfee412001-10-29 16:05:51 +0000795 if (!PrintAllTypes) {
796 Out << " ";
797 printType(I->getOperand(0)->getType());
798 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000799
Chris Lattnera073acb2001-07-07 08:36:50 +0000800 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000801 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000802 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000803 }
804 }
805
Chris Lattner862e3382001-10-13 06:42:36 +0000806 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000807 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000808}
809
810
811//===----------------------------------------------------------------------===//
812// External Interface declarations
813//===----------------------------------------------------------------------===//
814
815
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000816void Module::print(std::ostream &o) const {
817 SlotCalculator SlotTable(this, true);
818 AssemblyWriter W(o, SlotTable, this);
819 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000820}
821
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000822void GlobalVariable::print(std::ostream &o) const {
823 SlotCalculator SlotTable(getParent(), true);
824 AssemblyWriter W(o, SlotTable, getParent());
825 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000826}
827
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000828void Function::print(std::ostream &o) const {
829 SlotCalculator SlotTable(getParent(), true);
830 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000831
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000832 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000833}
834
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000835void BasicBlock::print(std::ostream &o) const {
836 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000837 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000838 getParent() ? getParent()->getParent() : 0);
839 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000840}
841
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000842void Instruction::print(std::ostream &o) const {
843 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000844 SlotCalculator SlotTable(F, true);
845 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000846
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000847 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000848}
Chris Lattner7db79582001-11-07 04:21:57 +0000849
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000850void Constant::print(std::ostream &o) const {
851 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner1e194682002-04-18 18:53:13 +0000852 o << " " << getType()->getDescription() << " ";
853
854 map<const Type *, string> TypeTable;
855 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000856}
857
858void Type::print(std::ostream &o) const {
859 if (this == 0)
860 o << "<null Type>";
861 else
862 o << getDescription();
863}
864
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000865void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000866 o << getType() << " " << getName();
867}
868
869void Value::dump() const { print(std::cerr); }
870
871//===----------------------------------------------------------------------===//
872// CachedWriter Class Implementation
873//===----------------------------------------------------------------------===//
874
Chris Lattner7db79582001-11-07 04:21:57 +0000875void CachedWriter::setModule(const Module *M) {
876 delete SC; delete AW;
877 if (M) {
878 SC = new SlotCalculator(M, true);
879 AW = new AssemblyWriter(Out, *SC, M);
880 } else {
881 SC = 0; AW = 0;
882 }
883}
884
885CachedWriter::~CachedWriter() {
886 delete AW;
887 delete SC;
888}
889
890CachedWriter &CachedWriter::operator<<(const Value *V) {
891 assert(AW && SC && "CachedWriter does not have a current module!");
892 switch (V->getValueType()) {
893 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +0000894 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000895 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
896 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
897 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000898 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000899 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000900 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
901 }
902 return *this;
903}