blob: 9f826fbff3f5382a1086bca48008b28ad6b292d5 [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 Lattnerca142372002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000018#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000019#include "llvm/iPHINode.h"
20#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000021#include "llvm/SymbolTable.h"
Chris Lattner5de22042001-11-27 00:03:19 +000022#include "Support/StringExtras.h"
23#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000024#include <algorithm>
Chris Lattner7f74a562002-01-20 22:54:45 +000025using std::string;
26using std::map;
27using std::vector;
28using std::ostream;
Chris Lattner7bfee412001-10-29 16:05:51 +000029
Chris Lattnerd84bb632002-04-16 21:36:08 +000030static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
31 map<const Type *, string> &TypeTable,
32 SlotCalculator *Table);
33
Chris Lattnerb86620e2001-10-29 16:37:48 +000034static const Module *getModuleFromVal(const Value *V) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000035 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000036 return MA->getParent() ? MA->getParent()->getParent() : 0;
37 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
38 return BB->getParent() ? BB->getParent()->getParent() : 0;
39 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000040 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000041 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000042 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000043 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000044 return 0;
45}
46
Chris Lattner7bfee412001-10-29 16:05:51 +000047static SlotCalculator *createSlotCalculator(const Value *V) {
48 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000049 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000050 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000051 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
52 return new SlotCalculator(I->getParent()->getParent(), true);
53 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
54 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000055 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000056 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000057 } else if (const Function *Func = dyn_cast<const Function>(V)) {
58 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000059 }
60 return 0;
61}
Chris Lattner2f7c9632001-06-06 20:29:01 +000062
Chris Lattnerb86620e2001-10-29 16:37:48 +000063
64// If the module has a symbol table, take all global types and stuff their
65// names into the TypeNames map.
66//
67static void fillTypeNameTable(const Module *M,
68 map<const Type *, string> &TypeNames) {
69 if (M && M->hasSymbolTable()) {
70 const SymbolTable *ST = M->getSymbolTable();
71 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
72 if (PI != ST->end()) {
73 SymbolTable::type_const_iterator I = PI->second.begin();
74 for (; I != PI->second.end(); ++I) {
75 // As a heuristic, don't insert pointer to primitive types, because
76 // they are used too often to have a single useful name.
77 //
78 const Type *Ty = cast<const Type>(I->second);
79 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +000080 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +000081 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +000082 }
83 }
84 }
85}
86
87
88
89static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
90 map<const Type *, string> &TypeNames) {
91 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
92
93 // Check to see if the type is named.
94 map<const Type *, string>::iterator I = TypeNames.find(Ty);
95 if (I != TypeNames.end()) return I->second;
96
97 // Check to see if the Type is already on the stack...
98 unsigned Slot = 0, CurSize = TypeStack.size();
99 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
100
101 // This is another base case for the recursion. In this case, we know
102 // that we have looped back to a type that we have previously visited.
103 // Generate the appropriate upreference to handle this.
104 //
105 if (Slot < CurSize)
106 return "\\" + utostr(CurSize-Slot); // Here's the upreference
107
108 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
109
110 string Result;
111 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000112 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000113 const FunctionType *FTy = cast<const FunctionType>(Ty);
114 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000115 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000116 I = FTy->getParamTypes().begin(),
117 E = FTy->getParamTypes().end(); I != E; ++I) {
118 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000119 Result += ", ";
120 Result += calcTypeName(*I, TypeStack, TypeNames);
121 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000122 if (FTy->isVarArg()) {
123 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000124 Result += "...";
125 }
126 Result += ")";
127 break;
128 }
129 case Type::StructTyID: {
130 const StructType *STy = cast<const StructType>(Ty);
131 Result = "{ ";
132 for (StructType::ElementTypes::const_iterator
133 I = STy->getElementTypes().begin(),
134 E = STy->getElementTypes().end(); I != E; ++I) {
135 if (I != STy->getElementTypes().begin())
136 Result += ", ";
137 Result += calcTypeName(*I, TypeStack, TypeNames);
138 }
139 Result += " }";
140 break;
141 }
142 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000143 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000144 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000145 break;
146 case Type::ArrayTyID: {
147 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000148 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000149 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
150 break;
151 }
152 default:
153 assert(0 && "Unhandled case in getTypeProps!");
154 Result = "<error>";
155 }
156
157 TypeStack.pop_back(); // Remove self from stack...
158 return Result;
159}
160
161
162// printTypeInt - The internal guts of printing out a type that has a
163// potentially named portion.
164//
165static ostream &printTypeInt(ostream &Out, const Type *Ty,
166 map<const Type *, string> &TypeNames) {
167 // Primitive types always print out their description, regardless of whether
168 // they have been named or not.
169 //
170 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
171
172 // Check to see if the type is named.
173 map<const Type *, string>::iterator I = TypeNames.find(Ty);
174 if (I != TypeNames.end()) return Out << I->second;
175
176 // Otherwise we have a type that has not been named but is a derived type.
177 // Carefully recurse the type hierarchy to print out any contained symbolic
178 // names.
179 //
180 vector<const Type *> TypeStack;
181 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000182 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000183 return Out << TypeName;
184}
185
Chris Lattner34b95182001-10-31 04:33:19 +0000186
Chris Lattnerb86620e2001-10-29 16:37:48 +0000187// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
188// type, iff there is an entry in the modules symbol table for the specified
189// type or one of it's component types. This is slower than a simple x << Type;
190//
191ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
192 Out << " ";
193
194 // If they want us to print out a type, attempt to make it symbolic if there
195 // is a symbol table in the module...
196 if (M && M->hasSymbolTable()) {
197 map<const Type *, string> TypeNames;
198 fillTypeNameTable(M, TypeNames);
199
Chris Lattner72f866e2001-10-29 16:40:32 +0000200 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000201 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000202 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000203 }
204}
205
Chris Lattnerd84bb632002-04-16 21:36:08 +0000206static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
207 map<const Type *, string> &TypeTable,
208 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000209 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
210 Out << (CB == ConstantBool::True ? "true" : "false");
211 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
212 Out << CI->getValue();
213 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
214 Out << CI->getValue();
215 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
216 // We would like to output the FP constant value in exponential notation,
217 // but we cannot do this if doing so will lose precision. Check here to
218 // make sure that we only output it in exponential format if we can parse
219 // the value back and get the same value.
220 //
221 std::string StrVal = ftostr(CFP->getValue());
222
223 // Check to make sure that the stringized number is not some string like
224 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
225 // the string matches the "[-+]?[0-9]" regex.
226 //
227 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
228 ((StrVal[0] == '-' || StrVal[0] == '+') &&
229 (StrVal[0] >= '0' && StrVal[0] <= '9')))
230 // Reparse stringized version!
231 if (atof(StrVal.c_str()) == CFP->getValue()) {
232 Out << StrVal; return;
233 }
234
235 // Otherwise we could not reparse it to exactly the same value, so we must
236 // output the string in hexadecimal format!
237 //
238 // Behave nicely in the face of C TBAA rules... see:
239 // http://www.nullstone.com/htmls/category/aliastyp.htm
240 //
241 double Val = CFP->getValue();
242 char *Ptr = (char*)&Val;
243 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
244 "assuming that double is 64 bits!");
245 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
246
247 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
248 // As a special case, print the array as a string if it is an array of
249 // ubytes or an array of sbytes with positive values.
250 //
251 const Type *ETy = CA->getType()->getElementType();
252 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
253
254 if (ETy == Type::SByteTy)
255 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
256 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
257 isString = false;
258 break;
259 }
260
261 if (isString) {
262 Out << "c\"";
263 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
264 unsigned char C = (ETy == Type::SByteTy) ?
265 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
266 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
267
268 if (isprint(C)) {
269 Out << C;
270 } else {
271 Out << '\\'
272 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
273 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
274 }
275 }
276 Out << "\"";
277
278 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000279 Out << "[";
280 if (CA->getNumOperands()) {
281 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000282 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000283 WriteAsOperandInternal(Out, CA->getOperand(0),
284 PrintName, TypeTable, Table);
285 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
286 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000287 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000288 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
289 TypeTable, Table);
290 }
291 }
292 Out << " ]";
293 }
294 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
295 Out << "{";
296 if (CS->getNumOperands()) {
297 Out << " ";
298 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
299
300 WriteAsOperandInternal(Out, CS->getOperand(0),
301 PrintName, TypeTable, Table);
302
303 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
304 Out << ", ";
305 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
306
307 WriteAsOperandInternal(Out, CS->getOperand(i),
308 PrintName, TypeTable, Table);
309 }
310 }
311
312 Out << " }";
313 } else if (isa<ConstantPointerNull>(CV)) {
314 Out << "null";
315
Chris Lattner113f4f42002-06-25 16:13:24 +0000316 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000317 const GlobalValue *V = PR->getValue();
318 if (V->hasName()) {
319 Out << "%" << V->getName();
320 } else if (Table) {
321 int Slot = Table->getValSlot(V);
322 if (Slot >= 0)
323 Out << "%" << Slot;
324 else
325 Out << "<pointer reference badref>";
326 } else {
327 Out << "<pointer reference without context info>";
328 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000329 } else {
Chris Lattner1e194682002-04-18 18:53:13 +0000330 assert(0 && "Unrecognized constant value!!!");
Chris Lattnerd84bb632002-04-16 21:36:08 +0000331 }
332}
333
334
335// WriteAsOperand - Write the name of the specified value out to the specified
336// ostream. This can be useful when you just want to print int %reg126, not the
337// whole instruction that generated it.
338//
339static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
340 map<const Type *, string> &TypeTable,
341 SlotCalculator *Table) {
342 Out << " ";
343 if (PrintName && V->hasName()) {
344 Out << "%" << V->getName();
345 } else {
346 if (const Constant *CV = dyn_cast<const Constant>(V)) {
347 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
348 } else {
349 int Slot;
350 if (Table) {
351 Slot = Table->getValSlot(V);
352 } else {
353 if (const Type *Ty = dyn_cast<const Type>(V)) {
354 Out << Ty->getDescription();
355 return;
356 }
357
358 Table = createSlotCalculator(V);
359 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
360
361 Slot = Table->getValSlot(V);
362 delete Table;
363 }
364 if (Slot >= 0) Out << "%" << Slot;
365 else if (PrintName)
366 Out << "<badref>"; // Not embeded into a location?
367 }
368 }
369}
370
371
Chris Lattnerb86620e2001-10-29 16:37:48 +0000372
373// WriteAsOperand - Write the name of the specified value out to the specified
374// ostream. This can be useful when you just want to print int %reg126, not the
375// whole instruction that generated it.
376//
377ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
378 bool PrintName, SlotCalculator *Table) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000379 map<const Type *, string> TypeNames;
380 const Module *M = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000381
Chris Lattnerd84bb632002-04-16 21:36:08 +0000382 if (M && M->hasSymbolTable())
383 fillTypeNameTable(M, TypeNames);
384
385 if (PrintType)
386 printTypeInt(Out, V->getType(), TypeNames);
387
388 WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000389 return Out;
390}
391
392
Chris Lattner2e9fee42001-07-12 23:35:26 +0000393
Chris Lattnerfee714f2001-09-07 16:36:04 +0000394class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000395 ostream &Out;
396 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000397 const Module *TheModule;
398 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000399public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000400 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
401 : Out(o), Table(Tab), TheModule(M) {
402
403 // If the module has a symbol table, take all global types and stuff their
404 // names into the TypeNames map.
405 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000406 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000407 }
408
Chris Lattner7bfee412001-10-29 16:05:51 +0000409 inline void write(const Module *M) { printModule(M); }
410 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000411 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000412 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000413 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000414 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000415 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000416
Chris Lattner1e194682002-04-18 18:53:13 +0000417 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
418
Chris Lattner2f7c9632001-06-06 20:29:01 +0000419private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000420 void printModule(const Module *M);
421 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000422 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000423 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000424 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000425 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000426 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000427 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000428
429 // printType - Go to extreme measures to attempt to print out a short,
430 // symbolic version of a type name.
431 //
432 ostream &printType(const Type *Ty) {
433 return printTypeInt(Out, Ty, TypeNames);
434 }
435
436 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
437 // without considering any symbolic types that we may have equal to it.
438 //
439 ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000440
Chris Lattner862e3382001-10-13 06:42:36 +0000441 // printInfoComment - Print a little comment after the instruction indicating
442 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000443 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000444};
445
446
Chris Lattnerd816b532002-04-13 20:53:41 +0000447// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
448// without considering any symbolic types that we may have equal to it.
449//
450ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000451 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000452 printType(FTy->getReturnType()) << " (";
453 for (FunctionType::ParamTypes::const_iterator
454 I = FTy->getParamTypes().begin(),
455 E = FTy->getParamTypes().end(); I != E; ++I) {
456 if (I != FTy->getParamTypes().begin())
457 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000458 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000459 }
460 if (FTy->isVarArg()) {
461 if (!FTy->getParamTypes().empty()) Out << ", ";
462 Out << "...";
463 }
464 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000465 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000466 Out << "{ ";
467 for (StructType::ElementTypes::const_iterator
468 I = STy->getElementTypes().begin(),
469 E = STy->getElementTypes().end(); I != E; ++I) {
470 if (I != STy->getElementTypes().begin())
471 Out << ", ";
472 printType(*I);
473 }
474 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000475 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000476 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000477 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000478 Out << "[" << ATy->getNumElements() << " x ";
479 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000480 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner2904f442002-05-26 20:17:54 +0000481 Out << OTy->getDescription();
Chris Lattnerd816b532002-04-13 20:53:41 +0000482 } else {
483 assert(Ty->isPrimitiveType() && "Unknown derived type!");
484 printType(Ty);
485 }
486 return Out;
487}
488
489
Chris Lattnerfee714f2001-09-07 16:36:04 +0000490void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
491 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000492 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000493 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000494}
495
Chris Lattner2f7c9632001-06-06 20:29:01 +0000496
Chris Lattner7bfee412001-10-29 16:05:51 +0000497void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000498 // Loop over the symbol table, emitting all named constants...
499 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000500 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000501
Chris Lattner113f4f42002-06-25 16:13:24 +0000502 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
503 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000504
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000505 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000506
Chris Lattner6915f8f2002-04-07 22:49:37 +0000507 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000508 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
509 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000510}
511
Chris Lattner7bfee412001-10-29 16:05:51 +0000512void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000513 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000514
Chris Lattner841d8b92001-11-26 18:54:16 +0000515 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000516 if (!GV->hasInitializer()) Out << "uninitialized ";
517
Chris Lattner7bfee412001-10-29 16:05:51 +0000518 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000519 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000520
521 if (GV->hasInitializer())
522 writeOperand(GV->getInitializer(), false, false);
523
Chris Lattner113f4f42002-06-25 16:13:24 +0000524 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000525 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000526}
527
Chris Lattnerfee714f2001-09-07 16:36:04 +0000528
Chris Lattner7bfee412001-10-29 16:05:51 +0000529// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000530// if a named constant is found, emit it's declaration...
531//
Chris Lattner7bfee412001-10-29 16:05:51 +0000532void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000533 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
534 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
535 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
536
537 for (; I != End; ++I) {
538 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000539 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000540 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000541 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000542 Out << "\t%" << I->first << " = type ";
543
544 // Make sure we print out at least one level of the type structure, so
545 // that we do not get %FILE = type %FILE
546 //
547 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000548 }
549 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000550 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000551}
552
553
Chris Lattner7bfee412001-10-29 16:05:51 +0000554// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000555//
Chris Lattner3462ae32001-12-03 22:26:30 +0000556void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000557 // Don't print out unnamed constants, they will be inlined
558 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000559
Chris Lattneree998be2001-07-26 16:29:38 +0000560 // Print out name...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000561 Out << "\t%" << CPV->getName() << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000562
563 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000564 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000565
Chris Lattner113f4f42002-06-25 16:13:24 +0000566 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000567 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000568}
569
Chris Lattner6915f8f2002-04-07 22:49:37 +0000570// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000571//
Chris Lattner113f4f42002-06-25 16:13:24 +0000572void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000573 // Print out the return type and name...
Chris Lattner113f4f42002-06-25 16:13:24 +0000574 Out << "\n" << (F->isExternal() ? "declare " : "")
575 << (F->hasInternalLinkage() ? "internal " : "");
576 printType(F->getReturnType()) << " %" << F->getName() << "(";
577 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000578
Chris Lattner7bfee412001-10-29 16:05:51 +0000579 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000580 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000581
Chris Lattner113f4f42002-06-25 16:13:24 +0000582 if (!F->isExternal()) {
583 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
584 printArgument(I);
Chris Lattner862e3382001-10-13 06:42:36 +0000585 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000586 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000587 for (FunctionType::ParamTypes::const_iterator I = FT->getParamTypes().begin(),
588 E = FT->getParamTypes().end(); I != E; ++I) {
589 if (I != FT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000590 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000591 }
592 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000593
594 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000595 if (FT->isVarArg()) {
596 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000597 Out << "..."; // Output varargs portion of signature!
598 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000599 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000600
Chris Lattner113f4f42002-06-25 16:13:24 +0000601 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000602 Out << "\n";
603 } else {
604 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000605
Chris Lattner6915f8f2002-04-07 22:49:37 +0000606 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000607 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
608 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000609
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000610 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000611 }
612
Chris Lattner6915f8f2002-04-07 22:49:37 +0000613 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000614}
615
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000616// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000617// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000618//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000619void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000620 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000621 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000622
623 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000624 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000625
626 // Output name, if available...
627 if (Arg->hasName())
628 Out << " %" << Arg->getName();
629 else if (Table.getValSlot(Arg) < 0)
630 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000631}
632
Chris Lattner7bfee412001-10-29 16:05:51 +0000633// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000634//
Chris Lattner7bfee412001-10-29 16:05:51 +0000635void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000636 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner408dbdb2002-05-14 16:02:05 +0000637 Out << "\n" << BB->getName() << ":\t\t\t\t\t;[#uses="
638 << BB->use_size() << "]"; // Output # uses
639 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000640 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000641 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000642 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000643 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000644 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000645 Out << "<badref>";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000646 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000647 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000648
649 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000650
Chris Lattnerfee714f2001-09-07 16:36:04 +0000651 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000652 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
653 printInstruction(*I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000654}
655
Chris Lattner862e3382001-10-13 06:42:36 +0000656
657// printInfoComment - Print a little comment after the instruction indicating
658// which slot it occupies.
659//
Chris Lattner113f4f42002-06-25 16:13:24 +0000660void AssemblyWriter::printInfoComment(const Value &V) {
661 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000662 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000663 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000664
Chris Lattner113f4f42002-06-25 16:13:24 +0000665 if (!V.hasName()) {
666 int Slot = Table.getValSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000667 if (Slot >= 0) Out << ":" << Slot;
668 else Out << ":<badref>";
669 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000670 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000671 }
672}
673
Chris Lattner7bfee412001-10-29 16:05:51 +0000674// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000675//
Chris Lattner113f4f42002-06-25 16:13:24 +0000676void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000677 Out << "\t";
678
679 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000680 if (I.hasName())
681 Out << "%" << I.getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000682
683 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000684 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000685
686 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000687 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000688
689 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000690 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
691 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000692 Out << ",";
693 writeOperand(Operand, true);
694 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000695 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000696
Chris Lattner8d48df22002-04-13 18:34:38 +0000697 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000698 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000699 writeOperand(Operand , true); Out << ",";
700 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000701
Chris Lattner113f4f42002-06-25 16:13:24 +0000702 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000703 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000704 writeOperand(I.getOperand(op ), true); Out << ",";
705 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000706 }
707 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000708 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000709 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000710 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000711 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000712
Chris Lattner113f4f42002-06-25 16:13:24 +0000713 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000714 if (op) Out << ", ";
715 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000716 writeOperand(I.getOperand(op ), false); Out << ",";
717 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000718 }
Chris Lattner862e3382001-10-13 06:42:36 +0000719 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000721 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000722 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000723 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000724 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
725
726 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000727 // only do this if the first argument is a pointer to a nonvararg function,
728 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000729 //
Chris Lattner8d48df22002-04-13 18:34:38 +0000730 if (RetTy && MTy && !MTy->isVarArg() &&
731 (!isa<PointerType>(RetTy) ||
732 !isa<FunctionType>(cast<PointerType>(RetTy)))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000733 Out << " "; printType(RetTy);
734 writeOperand(Operand, false);
735 } else {
736 writeOperand(Operand, true);
737 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000738 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000739 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
740 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000741 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000742 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000743 }
744
745 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000746 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner862e3382001-10-13 06:42:36 +0000747 // TODO: Should try to print out short form of the Invoke instruction
748 writeOperand(Operand, true);
749 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000750 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
751 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000752 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000753 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000754 }
755
756 Out << " )\n\t\t\tto";
757 writeOperand(II->getNormalDest(), true);
758 Out << " except";
759 writeOperand(II->getExceptionalDest(), true);
760
Chris Lattner113f4f42002-06-25 16:13:24 +0000761 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000762 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000763 printType(AI->getType()->getElementType());
764 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000765 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000766 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000767 }
Chris Lattner862e3382001-10-13 06:42:36 +0000768 } else if (isa<CastInst>(I)) {
Chris Lattner143e5a42002-05-22 22:29:26 +0000769 if (Operand) writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000770 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000771 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000772 } else if (Operand) { // Print the normal way...
773
774 // PrintAllTypes - Instructions who have operands of all the same type
775 // omit the type from all but the first operand. If the instruction has
776 // different type operands (for example br), then they are all printed.
777 bool PrintAllTypes = false;
778 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000779
Chris Lattner113f4f42002-06-25 16:13:24 +0000780 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
781 Operand = I.getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000782 if (Operand->getType() != TheType) {
783 PrintAllTypes = true; // We have differing types! Print them all!
784 break;
785 }
786 }
787
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000788 // Shift Left & Right print both types even for Ubyte LHS
789 if (isa<ShiftInst>(I)) PrintAllTypes = true;
790
Chris Lattner7bfee412001-10-29 16:05:51 +0000791 if (!PrintAllTypes) {
792 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000793 printType(I.getOperand(0)->getType());
Chris Lattner7bfee412001-10-29 16:05:51 +0000794 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000795
Chris Lattner113f4f42002-06-25 16:13:24 +0000796 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000797 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000798 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000799 }
800 }
801
Chris Lattner862e3382001-10-13 06:42:36 +0000802 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000803 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000804}
805
806
807//===----------------------------------------------------------------------===//
808// External Interface declarations
809//===----------------------------------------------------------------------===//
810
811
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000812void Module::print(std::ostream &o) const {
813 SlotCalculator SlotTable(this, true);
814 AssemblyWriter W(o, SlotTable, this);
815 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000816}
817
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000818void GlobalVariable::print(std::ostream &o) const {
819 SlotCalculator SlotTable(getParent(), true);
820 AssemblyWriter W(o, SlotTable, getParent());
821 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000822}
823
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000824void Function::print(std::ostream &o) const {
825 SlotCalculator SlotTable(getParent(), true);
826 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000827
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000828 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000829}
830
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000831void BasicBlock::print(std::ostream &o) const {
832 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000833 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000834 getParent() ? getParent()->getParent() : 0);
835 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000836}
837
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000838void Instruction::print(std::ostream &o) const {
839 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000840 SlotCalculator SlotTable(F, true);
841 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000842
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000843 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000844}
Chris Lattner7db79582001-11-07 04:21:57 +0000845
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000846void Constant::print(std::ostream &o) const {
847 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner1e194682002-04-18 18:53:13 +0000848 o << " " << getType()->getDescription() << " ";
849
850 map<const Type *, string> TypeTable;
851 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000852}
853
854void Type::print(std::ostream &o) const {
855 if (this == 0)
856 o << "<null Type>";
857 else
858 o << getDescription();
859}
860
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000861void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000862 o << getType() << " " << getName();
863}
864
865void Value::dump() const { print(std::cerr); }
866
867//===----------------------------------------------------------------------===//
868// CachedWriter Class Implementation
869//===----------------------------------------------------------------------===//
870
Chris Lattner7db79582001-11-07 04:21:57 +0000871void CachedWriter::setModule(const Module *M) {
872 delete SC; delete AW;
873 if (M) {
874 SC = new SlotCalculator(M, true);
875 AW = new AssemblyWriter(Out, *SC, M);
876 } else {
877 SC = 0; AW = 0;
878 }
879}
880
881CachedWriter::~CachedWriter() {
882 delete AW;
883 delete SC;
884}
885
886CachedWriter &CachedWriter::operator<<(const Value *V) {
887 assert(AW && SC && "CachedWriter does not have a current module!");
888 switch (V->getValueType()) {
889 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +0000890 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000891 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
892 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
893 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000894 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000895 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000896 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
897 }
898 return *this;
899}