blob: 316baf124fac50898782a4bdd9ff49d50e491c93 [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// TODO: print out the type name instead of the full type if a particular type
Chris Lattner189088e2002-04-12 18:21:53 +000010// is in the symbol table...
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner7db79582001-11-07 04:21:57 +000014#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000015#include "llvm/Assembly/Writer.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000016#include "llvm/SlotCalculator.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/Module.h"
Chris Lattner57698e22002-03-26 18:01:55 +000018#include "llvm/Function.h"
Chris Lattnerda975502001-09-10 07:58:01 +000019#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000020#include "llvm/BasicBlock.h"
Chris Lattnerca142372002-04-28 19:55:58 +000021#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000022#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000023#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000024#include "llvm/iPHINode.h"
25#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000026#include "llvm/SymbolTable.h"
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000027#include "llvm/Argument.h"
Chris Lattner5de22042001-11-27 00:03:19 +000028#include "Support/StringExtras.h"
29#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000030#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000031#include <map>
Chris Lattner7f74a562002-01-20 22:54:45 +000032using std::string;
33using std::map;
34using std::vector;
35using std::ostream;
Chris Lattner7bfee412001-10-29 16:05:51 +000036
Chris Lattnerd84bb632002-04-16 21:36:08 +000037static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
38 map<const Type *, string> &TypeTable,
39 SlotCalculator *Table);
40
Chris Lattnerb86620e2001-10-29 16:37:48 +000041static const Module *getModuleFromVal(const Value *V) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000042 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000043 return MA->getParent() ? MA->getParent()->getParent() : 0;
44 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
45 return BB->getParent() ? BB->getParent()->getParent() : 0;
46 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000047 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000048 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000049 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000050 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000051 return 0;
52}
53
Chris Lattner7bfee412001-10-29 16:05:51 +000054static SlotCalculator *createSlotCalculator(const Value *V) {
55 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000056 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000057 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000058 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
59 return new SlotCalculator(I->getParent()->getParent(), true);
60 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
61 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000062 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000063 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000064 } else if (const Function *Func = dyn_cast<const Function>(V)) {
65 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000066 }
67 return 0;
68}
Chris Lattner2f7c9632001-06-06 20:29:01 +000069
Chris Lattnerb86620e2001-10-29 16:37:48 +000070
71// If the module has a symbol table, take all global types and stuff their
72// names into the TypeNames map.
73//
74static void fillTypeNameTable(const Module *M,
75 map<const Type *, string> &TypeNames) {
76 if (M && M->hasSymbolTable()) {
77 const SymbolTable *ST = M->getSymbolTable();
78 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
79 if (PI != ST->end()) {
80 SymbolTable::type_const_iterator I = PI->second.begin();
81 for (; I != PI->second.end(); ++I) {
82 // As a heuristic, don't insert pointer to primitive types, because
83 // they are used too often to have a single useful name.
84 //
85 const Type *Ty = cast<const Type>(I->second);
86 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +000087 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +000088 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +000089 }
90 }
91 }
92}
93
94
95
96static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
97 map<const Type *, string> &TypeNames) {
98 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
99
100 // Check to see if the type is named.
101 map<const Type *, string>::iterator I = TypeNames.find(Ty);
102 if (I != TypeNames.end()) return I->second;
103
104 // Check to see if the Type is already on the stack...
105 unsigned Slot = 0, CurSize = TypeStack.size();
106 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
107
108 // This is another base case for the recursion. In this case, we know
109 // that we have looped back to a type that we have previously visited.
110 // Generate the appropriate upreference to handle this.
111 //
112 if (Slot < CurSize)
113 return "\\" + utostr(CurSize-Slot); // Here's the upreference
114
115 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
116
117 string Result;
118 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000119 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000120 const FunctionType *FTy = cast<const FunctionType>(Ty);
121 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000122 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000123 I = FTy->getParamTypes().begin(),
124 E = FTy->getParamTypes().end(); I != E; ++I) {
125 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000126 Result += ", ";
127 Result += calcTypeName(*I, TypeStack, TypeNames);
128 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000129 if (FTy->isVarArg()) {
130 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000131 Result += "...";
132 }
133 Result += ")";
134 break;
135 }
136 case Type::StructTyID: {
137 const StructType *STy = cast<const StructType>(Ty);
138 Result = "{ ";
139 for (StructType::ElementTypes::const_iterator
140 I = STy->getElementTypes().begin(),
141 E = STy->getElementTypes().end(); I != E; ++I) {
142 if (I != STy->getElementTypes().begin())
143 Result += ", ";
144 Result += calcTypeName(*I, TypeStack, TypeNames);
145 }
146 Result += " }";
147 break;
148 }
149 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000150 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000151 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000152 break;
153 case Type::ArrayTyID: {
154 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000155 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000156 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
157 break;
158 }
159 default:
160 assert(0 && "Unhandled case in getTypeProps!");
161 Result = "<error>";
162 }
163
164 TypeStack.pop_back(); // Remove self from stack...
165 return Result;
166}
167
168
169// printTypeInt - The internal guts of printing out a type that has a
170// potentially named portion.
171//
172static ostream &printTypeInt(ostream &Out, const Type *Ty,
173 map<const Type *, string> &TypeNames) {
174 // Primitive types always print out their description, regardless of whether
175 // they have been named or not.
176 //
177 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
178
179 // Check to see if the type is named.
180 map<const Type *, string>::iterator I = TypeNames.find(Ty);
181 if (I != TypeNames.end()) return Out << I->second;
182
183 // Otherwise we have a type that has not been named but is a derived type.
184 // Carefully recurse the type hierarchy to print out any contained symbolic
185 // names.
186 //
187 vector<const Type *> TypeStack;
188 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000189 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000190 return Out << TypeName;
191}
192
Chris Lattner34b95182001-10-31 04:33:19 +0000193
Chris Lattnerb86620e2001-10-29 16:37:48 +0000194// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
195// type, iff there is an entry in the modules symbol table for the specified
196// type or one of it's component types. This is slower than a simple x << Type;
197//
198ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
199 Out << " ";
200
201 // If they want us to print out a type, attempt to make it symbolic if there
202 // is a symbol table in the module...
203 if (M && M->hasSymbolTable()) {
204 map<const Type *, string> TypeNames;
205 fillTypeNameTable(M, TypeNames);
206
Chris Lattner72f866e2001-10-29 16:40:32 +0000207 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000208 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000209 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000210 }
211}
212
Chris Lattnerd84bb632002-04-16 21:36:08 +0000213static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
214 map<const Type *, string> &TypeTable,
215 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000216 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
217 Out << (CB == ConstantBool::True ? "true" : "false");
218 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
219 Out << CI->getValue();
220 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
221 Out << CI->getValue();
222 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
223 // We would like to output the FP constant value in exponential notation,
224 // but we cannot do this if doing so will lose precision. Check here to
225 // make sure that we only output it in exponential format if we can parse
226 // the value back and get the same value.
227 //
228 std::string StrVal = ftostr(CFP->getValue());
229
230 // Check to make sure that the stringized number is not some string like
231 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
232 // the string matches the "[-+]?[0-9]" regex.
233 //
234 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
235 ((StrVal[0] == '-' || StrVal[0] == '+') &&
236 (StrVal[0] >= '0' && StrVal[0] <= '9')))
237 // Reparse stringized version!
238 if (atof(StrVal.c_str()) == CFP->getValue()) {
239 Out << StrVal; return;
240 }
241
242 // Otherwise we could not reparse it to exactly the same value, so we must
243 // output the string in hexadecimal format!
244 //
245 // Behave nicely in the face of C TBAA rules... see:
246 // http://www.nullstone.com/htmls/category/aliastyp.htm
247 //
248 double Val = CFP->getValue();
249 char *Ptr = (char*)&Val;
250 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
251 "assuming that double is 64 bits!");
252 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
253
254 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
255 // As a special case, print the array as a string if it is an array of
256 // ubytes or an array of sbytes with positive values.
257 //
258 const Type *ETy = CA->getType()->getElementType();
259 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
260
261 if (ETy == Type::SByteTy)
262 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
263 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
264 isString = false;
265 break;
266 }
267
268 if (isString) {
269 Out << "c\"";
270 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
271 unsigned char C = (ETy == Type::SByteTy) ?
272 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
273 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
274
275 if (isprint(C)) {
276 Out << C;
277 } else {
278 Out << '\\'
279 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
280 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
281 }
282 }
283 Out << "\"";
284
285 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000286 Out << "[";
287 if (CA->getNumOperands()) {
288 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000289 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000290 WriteAsOperandInternal(Out, CA->getOperand(0),
291 PrintName, TypeTable, Table);
292 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
293 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000294 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000295 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
296 TypeTable, Table);
297 }
298 }
299 Out << " ]";
300 }
301 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
302 Out << "{";
303 if (CS->getNumOperands()) {
304 Out << " ";
305 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
306
307 WriteAsOperandInternal(Out, CS->getOperand(0),
308 PrintName, TypeTable, Table);
309
310 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
311 Out << ", ";
312 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
313
314 WriteAsOperandInternal(Out, CS->getOperand(i),
315 PrintName, TypeTable, Table);
316 }
317 }
318
319 Out << " }";
320 } else if (isa<ConstantPointerNull>(CV)) {
321 Out << "null";
322
Chris Lattner1e194682002-04-18 18:53:13 +0000323 } else if (ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
324 const GlobalValue *V = PR->getValue();
325 if (V->hasName()) {
326 Out << "%" << V->getName();
327 } else if (Table) {
328 int Slot = Table->getValSlot(V);
329 if (Slot >= 0)
330 Out << "%" << Slot;
331 else
332 Out << "<pointer reference badref>";
333 } else {
334 Out << "<pointer reference without context info>";
335 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000336 } else {
Chris Lattner1e194682002-04-18 18:53:13 +0000337 assert(0 && "Unrecognized constant value!!!");
Chris Lattnerd84bb632002-04-16 21:36:08 +0000338 }
339}
340
341
342// WriteAsOperand - Write the name of the specified value out to the specified
343// ostream. This can be useful when you just want to print int %reg126, not the
344// whole instruction that generated it.
345//
346static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
347 map<const Type *, string> &TypeTable,
348 SlotCalculator *Table) {
349 Out << " ";
350 if (PrintName && V->hasName()) {
351 Out << "%" << V->getName();
352 } else {
353 if (const Constant *CV = dyn_cast<const Constant>(V)) {
354 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
355 } else {
356 int Slot;
357 if (Table) {
358 Slot = Table->getValSlot(V);
359 } else {
360 if (const Type *Ty = dyn_cast<const Type>(V)) {
361 Out << Ty->getDescription();
362 return;
363 }
364
365 Table = createSlotCalculator(V);
366 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
367
368 Slot = Table->getValSlot(V);
369 delete Table;
370 }
371 if (Slot >= 0) Out << "%" << Slot;
372 else if (PrintName)
373 Out << "<badref>"; // Not embeded into a location?
374 }
375 }
376}
377
378
Chris Lattnerb86620e2001-10-29 16:37:48 +0000379
380// WriteAsOperand - Write the name of the specified value out to the specified
381// ostream. This can be useful when you just want to print int %reg126, not the
382// whole instruction that generated it.
383//
384ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
385 bool PrintName, SlotCalculator *Table) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000386 map<const Type *, string> TypeNames;
387 const Module *M = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000388
Chris Lattnerd84bb632002-04-16 21:36:08 +0000389 if (M && M->hasSymbolTable())
390 fillTypeNameTable(M, TypeNames);
391
392 if (PrintType)
393 printTypeInt(Out, V->getType(), TypeNames);
394
395 WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000396 return Out;
397}
398
399
Chris Lattner2e9fee42001-07-12 23:35:26 +0000400
Chris Lattnerfee714f2001-09-07 16:36:04 +0000401class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000402 ostream &Out;
403 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000404 const Module *TheModule;
405 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000406public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000407 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
408 : Out(o), Table(Tab), TheModule(M) {
409
410 // If the module has a symbol table, take all global types and stuff their
411 // names into the TypeNames map.
412 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000413 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000414 }
415
Chris Lattner7bfee412001-10-29 16:05:51 +0000416 inline void write(const Module *M) { printModule(M); }
417 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000418 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000419 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
420 inline void write(const Instruction *I) { printInstruction(I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000421 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000422 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000423
Chris Lattner1e194682002-04-18 18:53:13 +0000424 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
425
Chris Lattner2f7c9632001-06-06 20:29:01 +0000426private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000427 void printModule(const Module *M);
428 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000429 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000430 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000431 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000432 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000433 void printBasicBlock(const BasicBlock *BB);
434 void printInstruction(const Instruction *I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000435
436 // printType - Go to extreme measures to attempt to print out a short,
437 // symbolic version of a type name.
438 //
439 ostream &printType(const Type *Ty) {
440 return printTypeInt(Out, Ty, TypeNames);
441 }
442
443 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
444 // without considering any symbolic types that we may have equal to it.
445 //
446 ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000447
Chris Lattner862e3382001-10-13 06:42:36 +0000448 // printInfoComment - Print a little comment after the instruction indicating
449 // which slot it occupies.
450 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000451};
452
453
Chris Lattnerd816b532002-04-13 20:53:41 +0000454// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
455// without considering any symbolic types that we may have equal to it.
456//
457ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
458 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
459 printType(FTy->getReturnType()) << " (";
460 for (FunctionType::ParamTypes::const_iterator
461 I = FTy->getParamTypes().begin(),
462 E = FTy->getParamTypes().end(); I != E; ++I) {
463 if (I != FTy->getParamTypes().begin())
464 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000465 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000466 }
467 if (FTy->isVarArg()) {
468 if (!FTy->getParamTypes().empty()) Out << ", ";
469 Out << "...";
470 }
471 Out << ")";
472 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
473 Out << "{ ";
474 for (StructType::ElementTypes::const_iterator
475 I = STy->getElementTypes().begin(),
476 E = STy->getElementTypes().end(); I != E; ++I) {
477 if (I != STy->getElementTypes().begin())
478 Out << ", ";
479 printType(*I);
480 }
481 Out << " }";
482 } else if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
483 printType(PTy->getElementType()) << "*";
484 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
485 Out << "[" << ATy->getNumElements() << " x ";
486 printType(ATy->getElementType()) << "]";
487 } else {
488 assert(Ty->isPrimitiveType() && "Unknown derived type!");
489 printType(Ty);
490 }
491 return Out;
492}
493
494
Chris Lattnerfee714f2001-09-07 16:36:04 +0000495void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
496 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000497 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000498 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000499}
500
Chris Lattner2f7c9632001-06-06 20:29:01 +0000501
Chris Lattner7bfee412001-10-29 16:05:51 +0000502void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000503 // Loop over the symbol table, emitting all named constants...
504 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000505 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000506
507 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000508 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000509
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000510 Out << "implementation\n";
511
Chris Lattner6915f8f2002-04-07 22:49:37 +0000512 // Output all of the functions...
Chris Lattner57698e22002-03-26 18:01:55 +0000513 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000514}
515
Chris Lattner7bfee412001-10-29 16:05:51 +0000516void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000517 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000518
Chris Lattner841d8b92001-11-26 18:54:16 +0000519 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000520 if (!GV->hasInitializer()) Out << "uninitialized ";
521
Chris Lattner7bfee412001-10-29 16:05:51 +0000522 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000523 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000524
525 if (GV->hasInitializer())
526 writeOperand(GV->getInitializer(), false, false);
527
Chris Lattner862e3382001-10-13 06:42:36 +0000528 printInfoComment(GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000529 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000530}
531
Chris Lattnerfee714f2001-09-07 16:36:04 +0000532
Chris Lattner7bfee412001-10-29 16:05:51 +0000533// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000534// if a named constant is found, emit it's declaration...
535//
Chris Lattner7bfee412001-10-29 16:05:51 +0000536void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000537 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
538 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
539 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
540
541 for (; I != End; ++I) {
542 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000543 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000544 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000545 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000546 Out << "\t%" << I->first << " = type ";
547
548 // Make sure we print out at least one level of the type structure, so
549 // that we do not get %FILE = type %FILE
550 //
551 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000552 }
553 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000554 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000555}
556
557
Chris Lattner7bfee412001-10-29 16:05:51 +0000558// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000559//
Chris Lattner3462ae32001-12-03 22:26:30 +0000560void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000561 // Don't print out unnamed constants, they will be inlined
562 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000563
Chris Lattneree998be2001-07-26 16:29:38 +0000564 // Print out name...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000565 Out << "\t%" << CPV->getName() << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000566
567 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000568 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000569
Chris Lattner1e194682002-04-18 18:53:13 +0000570 printInfoComment(CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000571 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000572}
573
Chris Lattner6915f8f2002-04-07 22:49:37 +0000574// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000575//
Chris Lattner57698e22002-03-26 18:01:55 +0000576void AssemblyWriter::printFunction(const Function *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000577 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000578 Out << "\n" << (M->isExternal() ? "declare " : "")
579 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner7bfee412001-10-29 16:05:51 +0000580 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner6915f8f2002-04-07 22:49:37 +0000581 Table.incorporateFunction(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000582
Chris Lattner7bfee412001-10-29 16:05:51 +0000583 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000584 const FunctionType *MT = M->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000585
Chris Lattner862e3382001-10-13 06:42:36 +0000586 if (!M->isExternal()) {
587 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000588 bind_obj(this, &AssemblyWriter::printArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000589 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000590 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000591 const FunctionType *MT = M->getFunctionType();
592 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
Chris Lattner862e3382001-10-13 06:42:36 +0000593 E = MT->getParamTypes().end(); I != E; ++I) {
594 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000595 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000596 }
597 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000598
599 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000600 if (MT->isVarArg()) {
601 if (MT->getParamTypes().size()) Out << ", ";
602 Out << "..."; // Output varargs portion of signature!
603 }
604 Out << ")\n";
605
606 if (!M->isExternal()) {
607 // Loop over the symbol table, emitting all named constants...
608 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000609 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000610
611 Out << "begin";
612
Chris Lattner6915f8f2002-04-07 22:49:37 +0000613 // Output all of its basic blocks... for the function
Chris Lattnerfee714f2001-09-07 16:36:04 +0000614 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000615 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000616
Chris Lattnera7620d92001-07-15 06:35:59 +0000617 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000618 }
619
Chris Lattner6915f8f2002-04-07 22:49:37 +0000620 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000621}
622
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000623// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000624// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000625//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000626void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000627 // Insert commas as we go... the first arg doesn't get a comma
628 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
629
630 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000631 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000632
633 // Output name, if available...
634 if (Arg->hasName())
635 Out << " %" << Arg->getName();
636 else if (Table.getValSlot(Arg) < 0)
637 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000638}
639
Chris Lattner7bfee412001-10-29 16:05:51 +0000640// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000641//
Chris Lattner7bfee412001-10-29 16:05:51 +0000642void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000643 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000644 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000645 } else {
646 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000647 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000648 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000649 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000650 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000651 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000652 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000653 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
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 Lattnera6821822001-07-08 04:57:15 +0000773 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}