blob: 41c139f9552fcecbd83f4a06acb5e2bdd2fcede1 [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 Lattner3462ae32001-12-03 22:26:30 +000021#include "llvm/ConstantVals.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();
51 else if (const Module *Mod = dyn_cast<const Module>(V))
52 return Mod;
53 return 0;
54}
55
Chris Lattner7bfee412001-10-29 16:05:51 +000056static SlotCalculator *createSlotCalculator(const Value *V) {
57 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000058 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000059 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000060 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
61 return new SlotCalculator(I->getParent()->getParent(), true);
62 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
63 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000064 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000065 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000066 } else if (const Function *Func = dyn_cast<const Function>(V)) {
67 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000068 } else if (const Module *Mod = dyn_cast<const Module>(V)) {
69 return new SlotCalculator(Mod, true);
70 }
71 return 0;
72}
Chris Lattner2f7c9632001-06-06 20:29:01 +000073
Chris Lattnerb86620e2001-10-29 16:37:48 +000074
75// If the module has a symbol table, take all global types and stuff their
76// names into the TypeNames map.
77//
78static void fillTypeNameTable(const Module *M,
79 map<const Type *, string> &TypeNames) {
80 if (M && M->hasSymbolTable()) {
81 const SymbolTable *ST = M->getSymbolTable();
82 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
83 if (PI != ST->end()) {
84 SymbolTable::type_const_iterator I = PI->second.begin();
85 for (; I != PI->second.end(); ++I) {
86 // As a heuristic, don't insert pointer to primitive types, because
87 // they are used too often to have a single useful name.
88 //
89 const Type *Ty = cast<const Type>(I->second);
90 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +000091 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +000092 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +000093 }
94 }
95 }
96}
97
98
99
100static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
101 map<const Type *, string> &TypeNames) {
102 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
103
104 // Check to see if the type is named.
105 map<const Type *, string>::iterator I = TypeNames.find(Ty);
106 if (I != TypeNames.end()) return I->second;
107
108 // Check to see if the Type is already on the stack...
109 unsigned Slot = 0, CurSize = TypeStack.size();
110 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
111
112 // This is another base case for the recursion. In this case, we know
113 // that we have looped back to a type that we have previously visited.
114 // Generate the appropriate upreference to handle this.
115 //
116 if (Slot < CurSize)
117 return "\\" + utostr(CurSize-Slot); // Here's the upreference
118
119 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
120
121 string Result;
122 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000123 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000124 const FunctionType *FTy = cast<const FunctionType>(Ty);
125 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000126 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000127 I = FTy->getParamTypes().begin(),
128 E = FTy->getParamTypes().end(); I != E; ++I) {
129 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000130 Result += ", ";
131 Result += calcTypeName(*I, TypeStack, TypeNames);
132 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000133 if (FTy->isVarArg()) {
134 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000135 Result += "...";
136 }
137 Result += ")";
138 break;
139 }
140 case Type::StructTyID: {
141 const StructType *STy = cast<const StructType>(Ty);
142 Result = "{ ";
143 for (StructType::ElementTypes::const_iterator
144 I = STy->getElementTypes().begin(),
145 E = STy->getElementTypes().end(); I != E; ++I) {
146 if (I != STy->getElementTypes().begin())
147 Result += ", ";
148 Result += calcTypeName(*I, TypeStack, TypeNames);
149 }
150 Result += " }";
151 break;
152 }
153 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000154 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000155 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000156 break;
157 case Type::ArrayTyID: {
158 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000159 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000160 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
161 break;
162 }
163 default:
164 assert(0 && "Unhandled case in getTypeProps!");
165 Result = "<error>";
166 }
167
168 TypeStack.pop_back(); // Remove self from stack...
169 return Result;
170}
171
172
173// printTypeInt - The internal guts of printing out a type that has a
174// potentially named portion.
175//
176static ostream &printTypeInt(ostream &Out, const Type *Ty,
177 map<const Type *, string> &TypeNames) {
178 // Primitive types always print out their description, regardless of whether
179 // they have been named or not.
180 //
181 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
182
183 // Check to see if the type is named.
184 map<const Type *, string>::iterator I = TypeNames.find(Ty);
185 if (I != TypeNames.end()) return Out << I->second;
186
187 // Otherwise we have a type that has not been named but is a derived type.
188 // Carefully recurse the type hierarchy to print out any contained symbolic
189 // names.
190 //
191 vector<const Type *> TypeStack;
192 string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000193 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000194 return Out << TypeName;
195}
196
Chris Lattner34b95182001-10-31 04:33:19 +0000197
Chris Lattnerb86620e2001-10-29 16:37:48 +0000198// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
199// type, iff there is an entry in the modules symbol table for the specified
200// type or one of it's component types. This is slower than a simple x << Type;
201//
202ostream &WriteTypeSymbolic(ostream &Out, const Type *Ty, const Module *M) {
203 Out << " ";
204
205 // If they want us to print out a type, attempt to make it symbolic if there
206 // is a symbol table in the module...
207 if (M && M->hasSymbolTable()) {
208 map<const Type *, string> TypeNames;
209 fillTypeNameTable(M, TypeNames);
210
Chris Lattner72f866e2001-10-29 16:40:32 +0000211 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000212 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000213 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000214 }
215}
216
Chris Lattnerd84bb632002-04-16 21:36:08 +0000217static void WriteConstantInt(ostream &Out, const Constant *CV, bool PrintName,
218 map<const Type *, string> &TypeTable,
219 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000220 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
221 Out << (CB == ConstantBool::True ? "true" : "false");
222 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
223 Out << CI->getValue();
224 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
225 Out << CI->getValue();
226 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
227 // We would like to output the FP constant value in exponential notation,
228 // but we cannot do this if doing so will lose precision. Check here to
229 // make sure that we only output it in exponential format if we can parse
230 // the value back and get the same value.
231 //
232 std::string StrVal = ftostr(CFP->getValue());
233
234 // Check to make sure that the stringized number is not some string like
235 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
236 // the string matches the "[-+]?[0-9]" regex.
237 //
238 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
239 ((StrVal[0] == '-' || StrVal[0] == '+') &&
240 (StrVal[0] >= '0' && StrVal[0] <= '9')))
241 // Reparse stringized version!
242 if (atof(StrVal.c_str()) == CFP->getValue()) {
243 Out << StrVal; return;
244 }
245
246 // Otherwise we could not reparse it to exactly the same value, so we must
247 // output the string in hexadecimal format!
248 //
249 // Behave nicely in the face of C TBAA rules... see:
250 // http://www.nullstone.com/htmls/category/aliastyp.htm
251 //
252 double Val = CFP->getValue();
253 char *Ptr = (char*)&Val;
254 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
255 "assuming that double is 64 bits!");
256 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
257
258 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
259 // As a special case, print the array as a string if it is an array of
260 // ubytes or an array of sbytes with positive values.
261 //
262 const Type *ETy = CA->getType()->getElementType();
263 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
264
265 if (ETy == Type::SByteTy)
266 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
267 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
268 isString = false;
269 break;
270 }
271
272 if (isString) {
273 Out << "c\"";
274 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
275 unsigned char C = (ETy == Type::SByteTy) ?
276 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
277 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
278
279 if (isprint(C)) {
280 Out << C;
281 } else {
282 Out << '\\'
283 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
284 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
285 }
286 }
287 Out << "\"";
288
289 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000290 Out << "[";
291 if (CA->getNumOperands()) {
292 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000293 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000294 WriteAsOperandInternal(Out, CA->getOperand(0),
295 PrintName, TypeTable, Table);
296 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
297 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000298 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000299 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
300 TypeTable, Table);
301 }
302 }
303 Out << " ]";
304 }
305 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
306 Out << "{";
307 if (CS->getNumOperands()) {
308 Out << " ";
309 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
310
311 WriteAsOperandInternal(Out, CS->getOperand(0),
312 PrintName, TypeTable, Table);
313
314 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
315 Out << ", ";
316 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
317
318 WriteAsOperandInternal(Out, CS->getOperand(i),
319 PrintName, TypeTable, Table);
320 }
321 }
322
323 Out << " }";
324 } else if (isa<ConstantPointerNull>(CV)) {
325 Out << "null";
326
Chris Lattner1e194682002-04-18 18:53:13 +0000327 } else if (ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
328 const GlobalValue *V = PR->getValue();
329 if (V->hasName()) {
330 Out << "%" << V->getName();
331 } else if (Table) {
332 int Slot = Table->getValSlot(V);
333 if (Slot >= 0)
334 Out << "%" << Slot;
335 else
336 Out << "<pointer reference badref>";
337 } else {
338 Out << "<pointer reference without context info>";
339 }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000340 } else {
Chris Lattner1e194682002-04-18 18:53:13 +0000341 assert(0 && "Unrecognized constant value!!!");
Chris Lattnerd84bb632002-04-16 21:36:08 +0000342 }
343}
344
345
346// WriteAsOperand - Write the name of the specified value out to the specified
347// ostream. This can be useful when you just want to print int %reg126, not the
348// whole instruction that generated it.
349//
350static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
351 map<const Type *, string> &TypeTable,
352 SlotCalculator *Table) {
353 Out << " ";
354 if (PrintName && V->hasName()) {
355 Out << "%" << V->getName();
356 } else {
357 if (const Constant *CV = dyn_cast<const Constant>(V)) {
358 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
359 } else {
360 int Slot;
361 if (Table) {
362 Slot = Table->getValSlot(V);
363 } else {
364 if (const Type *Ty = dyn_cast<const Type>(V)) {
365 Out << Ty->getDescription();
366 return;
367 }
368
369 Table = createSlotCalculator(V);
370 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
371
372 Slot = Table->getValSlot(V);
373 delete Table;
374 }
375 if (Slot >= 0) Out << "%" << Slot;
376 else if (PrintName)
377 Out << "<badref>"; // Not embeded into a location?
378 }
379 }
380}
381
382
Chris Lattnerb86620e2001-10-29 16:37:48 +0000383
384// WriteAsOperand - Write the name of the specified value out to the specified
385// ostream. This can be useful when you just want to print int %reg126, not the
386// whole instruction that generated it.
387//
388ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
389 bool PrintName, SlotCalculator *Table) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000390 map<const Type *, string> TypeNames;
391 const Module *M = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000392
Chris Lattnerd84bb632002-04-16 21:36:08 +0000393 if (M && M->hasSymbolTable())
394 fillTypeNameTable(M, TypeNames);
395
396 if (PrintType)
397 printTypeInt(Out, V->getType(), TypeNames);
398
399 WriteAsOperandInternal(Out, V, PrintName, TypeNames, Table);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000400 return Out;
401}
402
403
Chris Lattner2e9fee42001-07-12 23:35:26 +0000404
Chris Lattnerfee714f2001-09-07 16:36:04 +0000405class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000406 ostream &Out;
407 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000408 const Module *TheModule;
409 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000410public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000411 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
412 : Out(o), Table(Tab), TheModule(M) {
413
414 // If the module has a symbol table, take all global types and stuff their
415 // names into the TypeNames map.
416 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000417 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000418 }
419
Chris Lattner7bfee412001-10-29 16:05:51 +0000420 inline void write(const Module *M) { printModule(M); }
421 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000422 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000423 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
424 inline void write(const Instruction *I) { printInstruction(I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000425 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000426 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000427
Chris Lattner1e194682002-04-18 18:53:13 +0000428 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
429
Chris Lattner2f7c9632001-06-06 20:29:01 +0000430private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000431 void printModule(const Module *M);
432 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000433 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000434 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000435 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000436 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000437 void printBasicBlock(const BasicBlock *BB);
438 void printInstruction(const Instruction *I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000439
440 // printType - Go to extreme measures to attempt to print out a short,
441 // symbolic version of a type name.
442 //
443 ostream &printType(const Type *Ty) {
444 return printTypeInt(Out, Ty, TypeNames);
445 }
446
447 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
448 // without considering any symbolic types that we may have equal to it.
449 //
450 ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000451
Chris Lattner862e3382001-10-13 06:42:36 +0000452 // printInfoComment - Print a little comment after the instruction indicating
453 // which slot it occupies.
454 void printInfoComment(const Value *V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000455};
456
457
Chris Lattnerd816b532002-04-13 20:53:41 +0000458// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
459// without considering any symbolic types that we may have equal to it.
460//
461ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
462 if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
463 printType(FTy->getReturnType()) << " (";
464 for (FunctionType::ParamTypes::const_iterator
465 I = FTy->getParamTypes().begin(),
466 E = FTy->getParamTypes().end(); I != E; ++I) {
467 if (I != FTy->getParamTypes().begin())
468 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000469 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000470 }
471 if (FTy->isVarArg()) {
472 if (!FTy->getParamTypes().empty()) Out << ", ";
473 Out << "...";
474 }
475 Out << ")";
476 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
477 Out << "{ ";
478 for (StructType::ElementTypes::const_iterator
479 I = STy->getElementTypes().begin(),
480 E = STy->getElementTypes().end(); I != E; ++I) {
481 if (I != STy->getElementTypes().begin())
482 Out << ", ";
483 printType(*I);
484 }
485 Out << " }";
486 } else if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
487 printType(PTy->getElementType()) << "*";
488 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
489 Out << "[" << ATy->getNumElements() << " x ";
490 printType(ATy->getElementType()) << "]";
491 } else {
492 assert(Ty->isPrimitiveType() && "Unknown derived type!");
493 printType(Ty);
494 }
495 return Out;
496}
497
498
Chris Lattnerfee714f2001-09-07 16:36:04 +0000499void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
500 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000501 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000502 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000503}
504
Chris Lattner2f7c9632001-06-06 20:29:01 +0000505
Chris Lattner7bfee412001-10-29 16:05:51 +0000506void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000507 // Loop over the symbol table, emitting all named constants...
508 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000509 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000510
511 for_each(M->gbegin(), M->gend(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000512 bind_obj(this, &AssemblyWriter::printGlobal));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000513
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000514 Out << "implementation\n";
515
Chris Lattner6915f8f2002-04-07 22:49:37 +0000516 // Output all of the functions...
Chris Lattner57698e22002-03-26 18:01:55 +0000517 for_each(M->begin(), M->end(), bind_obj(this,&AssemblyWriter::printFunction));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000518}
519
Chris Lattner7bfee412001-10-29 16:05:51 +0000520void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000521 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000522
Chris Lattner841d8b92001-11-26 18:54:16 +0000523 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattner37798642001-09-18 04:01:05 +0000524 if (!GV->hasInitializer()) Out << "uninitialized ";
525
Chris Lattner7bfee412001-10-29 16:05:51 +0000526 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000527 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000528
529 if (GV->hasInitializer())
530 writeOperand(GV->getInitializer(), false, false);
531
Chris Lattner862e3382001-10-13 06:42:36 +0000532 printInfoComment(GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000533 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000534}
535
Chris Lattnerfee714f2001-09-07 16:36:04 +0000536
Chris Lattner7bfee412001-10-29 16:05:51 +0000537// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000538// if a named constant is found, emit it's declaration...
539//
Chris Lattner7bfee412001-10-29 16:05:51 +0000540void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000541 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
542 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
543 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
544
545 for (; I != End; ++I) {
546 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000547 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000548 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000549 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000550 Out << "\t%" << I->first << " = type ";
551
552 // Make sure we print out at least one level of the type structure, so
553 // that we do not get %FILE = type %FILE
554 //
555 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000556 }
557 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000558 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000559}
560
561
Chris Lattner7bfee412001-10-29 16:05:51 +0000562// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000563//
Chris Lattner3462ae32001-12-03 22:26:30 +0000564void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000565 // Don't print out unnamed constants, they will be inlined
566 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000567
Chris Lattneree998be2001-07-26 16:29:38 +0000568 // Print out name...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000569 Out << "\t%" << CPV->getName() << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000570
571 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000572 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000573
Chris Lattner1e194682002-04-18 18:53:13 +0000574 printInfoComment(CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000575 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000576}
577
Chris Lattner6915f8f2002-04-07 22:49:37 +0000578// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579//
Chris Lattner57698e22002-03-26 18:01:55 +0000580void AssemblyWriter::printFunction(const Function *M) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000581 // Print out the return type and name...
Chris Lattner841d8b92001-11-26 18:54:16 +0000582 Out << "\n" << (M->isExternal() ? "declare " : "")
583 << (M->hasInternalLinkage() ? "internal " : "");
Chris Lattner7bfee412001-10-29 16:05:51 +0000584 printType(M->getReturnType()) << " \"" << M->getName() << "\"(";
Chris Lattner6915f8f2002-04-07 22:49:37 +0000585 Table.incorporateFunction(M);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000586
Chris Lattner7bfee412001-10-29 16:05:51 +0000587 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000588 const FunctionType *MT = M->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000589
Chris Lattner862e3382001-10-13 06:42:36 +0000590 if (!M->isExternal()) {
591 for_each(M->getArgumentList().begin(), M->getArgumentList().end(),
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000592 bind_obj(this, &AssemblyWriter::printArgument));
Chris Lattner862e3382001-10-13 06:42:36 +0000593 } else {
Chris Lattner7bfee412001-10-29 16:05:51 +0000594 // Loop over the arguments, printing them...
Chris Lattner91db5822002-03-29 03:44:36 +0000595 const FunctionType *MT = M->getFunctionType();
596 for (FunctionType::ParamTypes::const_iterator I = MT->getParamTypes().begin(),
Chris Lattner862e3382001-10-13 06:42:36 +0000597 E = MT->getParamTypes().end(); I != E; ++I) {
598 if (I != MT->getParamTypes().begin()) Out << ", ";
Chris Lattner7bfee412001-10-29 16:05:51 +0000599 printType(*I);
Chris Lattner862e3382001-10-13 06:42:36 +0000600 }
601 }
Chris Lattnerfee714f2001-09-07 16:36:04 +0000602
603 // Finish printing arguments...
Chris Lattnerfee714f2001-09-07 16:36:04 +0000604 if (MT->isVarArg()) {
605 if (MT->getParamTypes().size()) Out << ", ";
606 Out << "..."; // Output varargs portion of signature!
607 }
608 Out << ")\n";
609
610 if (!M->isExternal()) {
611 // Loop over the symbol table, emitting all named constants...
612 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000613 printSymbolTable(*M->getSymbolTable());
Chris Lattnerfee714f2001-09-07 16:36:04 +0000614
615 Out << "begin";
616
Chris Lattner6915f8f2002-04-07 22:49:37 +0000617 // Output all of its basic blocks... for the function
Chris Lattnerfee714f2001-09-07 16:36:04 +0000618 for_each(M->begin(), M->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000619 bind_obj(this, &AssemblyWriter::printBasicBlock));
Chris Lattnerfee714f2001-09-07 16:36:04 +0000620
Chris Lattnera7620d92001-07-15 06:35:59 +0000621 Out << "end\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000622 }
623
Chris Lattner6915f8f2002-04-07 22:49:37 +0000624 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000625}
626
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000627// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000628// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000629//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000630void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000631 // Insert commas as we go... the first arg doesn't get a comma
632 if (Arg != Arg->getParent()->getArgumentList().front()) Out << ", ";
633
634 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000635 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000636
637 // Output name, if available...
638 if (Arg->hasName())
639 Out << " %" << Arg->getName();
640 else if (Table.getValSlot(Arg) < 0)
641 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000642}
643
Chris Lattner7bfee412001-10-29 16:05:51 +0000644// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000645//
Chris Lattner7bfee412001-10-29 16:05:51 +0000646void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000647 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnera2f01872001-06-07 16:58:55 +0000648 Out << "\n" << BB->getName() << ":";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000649 } else {
650 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000651 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000652 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000653 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000654 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000655 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000656 }
Chris Lattnera2f01872001-06-07 16:58:55 +0000657 Out << "\t\t\t\t\t;[#uses=" << BB->use_size() << "]\n"; // Output # uses
Chris Lattner2f7c9632001-06-06 20:29:01 +0000658
Chris Lattnerfee714f2001-09-07 16:36:04 +0000659 // Output all of the instructions in the basic block...
660 for_each(BB->begin(), BB->end(),
Chris Lattner7bfee412001-10-29 16:05:51 +0000661 bind_obj(this, &AssemblyWriter::printInstruction));
Chris Lattner2f7c9632001-06-06 20:29:01 +0000662}
663
Chris Lattner862e3382001-10-13 06:42:36 +0000664
665// printInfoComment - Print a little comment after the instruction indicating
666// which slot it occupies.
667//
668void AssemblyWriter::printInfoComment(const Value *V) {
669 if (V->getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000670 Out << "\t\t; <";
671 printType(V->getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000672
673 if (!V->hasName()) {
674 int Slot = Table.getValSlot(V); // Print out the def slot taken...
675 if (Slot >= 0) Out << ":" << Slot;
676 else Out << ":<badref>";
677 }
Chris Lattner3cb3a1f2001-12-14 16:29:12 +0000678 Out << " [#uses=" << V->use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000679 }
680}
681
Chris Lattner7bfee412001-10-29 16:05:51 +0000682// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000683//
Chris Lattner7bfee412001-10-29 16:05:51 +0000684void AssemblyWriter::printInstruction(const Instruction *I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000685 Out << "\t";
686
687 // Print out name if it exists...
688 if (I && I->hasName())
689 Out << "%" << I->getName() << " = ";
690
691 // Print out the opcode...
Chris Lattnerb1ca9cb2001-07-07 19:24:15 +0000692 Out << I->getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000693
694 // Print out the type of the operands...
Chris Lattnera073acb2001-07-07 08:36:50 +0000695 const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000696
697 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner8d48df22002-04-13 18:34:38 +0000698 if (isa<BranchInst>(I) && I->getNumOperands() > 1) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000699 writeOperand(I->getOperand(2), true);
700 Out << ",";
701 writeOperand(Operand, true);
702 Out << ",";
703 writeOperand(I->getOperand(1), true);
704
Chris Lattner8d48df22002-04-13 18:34:38 +0000705 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000706 // Special case switch statement to get formatting nice and correct...
707 writeOperand(Operand , true); Out << ",";
708 writeOperand(I->getOperand(1), true); Out << " [";
709
Chris Lattnera073acb2001-07-07 08:36:50 +0000710 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000711 Out << "\n\t\t";
Chris Lattnera073acb2001-07-07 08:36:50 +0000712 writeOperand(I->getOperand(op ), true); Out << ",";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000713 writeOperand(I->getOperand(op+1), true);
714 }
715 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000716 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000717 Out << " ";
Chris Lattner29644b32001-11-06 01:48:45 +0000718 printType(I->getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000719 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720
Chris Lattner29644b32001-11-06 01:48:45 +0000721 for (unsigned op = 0, Eop = I->getNumOperands(); op < Eop; op += 2) {
722 if (op) Out << ", ";
723 Out << "[";
Chris Lattnera073acb2001-07-07 08:36:50 +0000724 writeOperand(I->getOperand(op ), false); Out << ",";
Chris Lattner4b94e232001-06-21 05:29:56 +0000725 writeOperand(I->getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000726 }
Chris Lattner862e3382001-10-13 06:42:36 +0000727 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000728 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000729 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000730 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000731 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000732 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
733
734 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000735 // only do this if the first argument is a pointer to a nonvararg function,
736 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000737 //
Chris Lattner8d48df22002-04-13 18:34:38 +0000738 if (RetTy && MTy && !MTy->isVarArg() &&
739 (!isa<PointerType>(RetTy) ||
740 !isa<FunctionType>(cast<PointerType>(RetTy)))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000741 Out << " "; printType(RetTy);
742 writeOperand(Operand, false);
743 } else {
744 writeOperand(Operand, true);
745 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000746 Out << "(";
Chris Lattnera073acb2001-07-07 08:36:50 +0000747 if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
748 for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000749 Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000750 writeOperand(I->getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000751 }
752
753 Out << " )";
Chris Lattner862e3382001-10-13 06:42:36 +0000754 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(I)) {
755 // TODO: Should try to print out short form of the Invoke instruction
756 writeOperand(Operand, true);
757 Out << "(";
758 if (I->getNumOperands() > 3) writeOperand(I->getOperand(3), true);
759 for (unsigned op = 4, Eop = I->getNumOperands(); op < Eop; ++op) {
760 Out << ",";
761 writeOperand(I->getOperand(op), true);
762 }
763
764 Out << " )\n\t\t\tto";
765 writeOperand(II->getNormalDest(), true);
766 Out << " except";
767 writeOperand(II->getExceptionalDest(), true);
768
Chris Lattner8d48df22002-04-13 18:34:38 +0000769 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000770 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000771 printType(AI->getType()->getElementType());
772 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000773 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000774 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000775 }
Chris Lattner862e3382001-10-13 06:42:36 +0000776 } else if (isa<CastInst>(I)) {
Chris Lattnera6821822001-07-08 04:57:15 +0000777 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000778 Out << " to ";
779 printType(I->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000780 } else if (Operand) { // Print the normal way...
781
782 // PrintAllTypes - Instructions who have operands of all the same type
783 // omit the type from all but the first operand. If the instruction has
784 // different type operands (for example br), then they are all printed.
785 bool PrintAllTypes = false;
786 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000787
Chris Lattnera073acb2001-07-07 08:36:50 +0000788 for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
789 Operand = I->getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000790 if (Operand->getType() != TheType) {
791 PrintAllTypes = true; // We have differing types! Print them all!
792 break;
793 }
794 }
795
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000796 // Shift Left & Right print both types even for Ubyte LHS
797 if (isa<ShiftInst>(I)) PrintAllTypes = true;
798
Chris Lattner7bfee412001-10-29 16:05:51 +0000799 if (!PrintAllTypes) {
800 Out << " ";
801 printType(I->getOperand(0)->getType());
802 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000803
Chris Lattnera073acb2001-07-07 08:36:50 +0000804 for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000805 if (i) Out << ",";
Chris Lattnera073acb2001-07-07 08:36:50 +0000806 writeOperand(I->getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000807 }
808 }
809
Chris Lattner862e3382001-10-13 06:42:36 +0000810 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000811 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000812}
813
814
815//===----------------------------------------------------------------------===//
816// External Interface declarations
817//===----------------------------------------------------------------------===//
818
819
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000820void Module::print(std::ostream &o) const {
821 SlotCalculator SlotTable(this, true);
822 AssemblyWriter W(o, SlotTable, this);
823 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000824}
825
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000826void GlobalVariable::print(std::ostream &o) const {
827 SlotCalculator SlotTable(getParent(), true);
828 AssemblyWriter W(o, SlotTable, getParent());
829 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000830}
831
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000832void Function::print(std::ostream &o) const {
833 SlotCalculator SlotTable(getParent(), true);
834 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000835
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000836 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000837}
838
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000839void BasicBlock::print(std::ostream &o) const {
840 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000841 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000842 getParent() ? getParent()->getParent() : 0);
843 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000844}
845
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000846void Instruction::print(std::ostream &o) const {
847 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000848 SlotCalculator SlotTable(F, true);
849 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000850
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000851 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000852}
Chris Lattner7db79582001-11-07 04:21:57 +0000853
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000854void Constant::print(std::ostream &o) const {
855 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner1e194682002-04-18 18:53:13 +0000856 o << " " << getType()->getDescription() << " ";
857
858 map<const Type *, string> TypeTable;
859 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000860}
861
862void Type::print(std::ostream &o) const {
863 if (this == 0)
864 o << "<null Type>";
865 else
866 o << getDescription();
867}
868
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000869void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000870 o << getType() << " " << getName();
871}
872
873void Value::dump() const { print(std::cerr); }
874
875//===----------------------------------------------------------------------===//
876// CachedWriter Class Implementation
877//===----------------------------------------------------------------------===//
878
Chris Lattner7db79582001-11-07 04:21:57 +0000879void CachedWriter::setModule(const Module *M) {
880 delete SC; delete AW;
881 if (M) {
882 SC = new SlotCalculator(M, true);
883 AW = new AssemblyWriter(Out, *SC, M);
884 } else {
885 SC = 0; AW = 0;
886 }
887}
888
889CachedWriter::~CachedWriter() {
890 delete AW;
891 delete SC;
892}
893
894CachedWriter &CachedWriter::operator<<(const Value *V) {
895 assert(AW && SC && "CachedWriter does not have a current module!");
896 switch (V->getValueType()) {
897 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +0000898 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000899 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
900 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
901 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000902 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000903 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
904 case Value::ModuleVal: AW->write(cast<Module>(V)); break;
905 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
906 }
907 return *this;
908}