blob: e27bd26ba27e7e621547245dffbf27ee7edb1071 [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 Lattner7f8845a2002-07-23 18:07:49 +000013#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000014#include "llvm/SlotCalculator.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000015#include "llvm/DerivedTypes.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000016#include "llvm/Instruction.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include "llvm/Module.h"
Chris Lattnerca142372002-04-28 19:55:58 +000018#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000020#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
22#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000023#include "llvm/SymbolTable.h"
Chris Lattner58185f22002-10-02 19:38:55 +000024#include "llvm/Support/CFG.h"
Chris Lattner5de22042001-11-27 00:03:19 +000025#include "Support/StringExtras.h"
26#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000027#include <algorithm>
Chris Lattner7f74a562002-01-20 22:54:45 +000028using std::string;
29using std::map;
30using std::vector;
31using std::ostream;
Chris Lattner7bfee412001-10-29 16:05:51 +000032
Chris Lattnerc8b70922002-07-26 21:12:46 +000033static RegisterPass<PrintModulePass>
34X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
35static RegisterPass<PrintFunctionPass>
36Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +000037
Chris Lattnerd84bb632002-04-16 21:36:08 +000038static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
39 map<const Type *, string> &TypeTable,
40 SlotCalculator *Table);
41
Chris Lattnerb86620e2001-10-29 16:37:48 +000042static const Module *getModuleFromVal(const Value *V) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000043 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000044 return MA->getParent() ? MA->getParent()->getParent() : 0;
45 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
46 return BB->getParent() ? BB->getParent()->getParent() : 0;
47 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000048 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000049 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000050 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000051 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000052 return 0;
53}
54
Chris Lattner7bfee412001-10-29 16:05:51 +000055static SlotCalculator *createSlotCalculator(const Value *V) {
56 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000057 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000058 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000059 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
60 return new SlotCalculator(I->getParent()->getParent(), true);
61 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
62 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000063 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000064 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000065 } else if (const Function *Func = dyn_cast<const Function>(V)) {
66 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000067 }
68 return 0;
69}
Chris Lattner2f7c9632001-06-06 20:29:01 +000070
Chris Lattnerb86620e2001-10-29 16:37:48 +000071
72// If the module has a symbol table, take all global types and stuff their
73// names into the TypeNames map.
74//
75static void fillTypeNameTable(const Module *M,
76 map<const Type *, string> &TypeNames) {
77 if (M && M->hasSymbolTable()) {
78 const SymbolTable *ST = M->getSymbolTable();
79 SymbolTable::const_iterator PI = ST->find(Type::TypeTy);
80 if (PI != ST->end()) {
81 SymbolTable::type_const_iterator I = PI->second.begin();
82 for (; I != PI->second.end(); ++I) {
83 // As a heuristic, don't insert pointer to primitive types, because
84 // they are used too often to have a single useful name.
85 //
86 const Type *Ty = cast<const Type>(I->second);
87 if (!isa<PointerType>(Ty) ||
Chris Lattner2413b162001-12-04 00:03:30 +000088 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner7f74a562002-01-20 22:54:45 +000089 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +000090 }
91 }
92 }
93}
94
95
96
97static string calcTypeName(const Type *Ty, vector<const Type *> &TypeStack,
98 map<const Type *, string> &TypeNames) {
99 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
100
101 // Check to see if the type is named.
102 map<const Type *, string>::iterator I = TypeNames.find(Ty);
103 if (I != TypeNames.end()) return I->second;
104
105 // Check to see if the Type is already on the stack...
106 unsigned Slot = 0, CurSize = TypeStack.size();
107 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
108
109 // This is another base case for the recursion. In this case, we know
110 // that we have looped back to a type that we have previously visited.
111 // Generate the appropriate upreference to handle this.
112 //
113 if (Slot < CurSize)
114 return "\\" + utostr(CurSize-Slot); // Here's the upreference
115
116 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
117
118 string Result;
119 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000120 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000121 const FunctionType *FTy = cast<const FunctionType>(Ty);
122 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000123 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000124 I = FTy->getParamTypes().begin(),
125 E = FTy->getParamTypes().end(); I != E; ++I) {
126 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000127 Result += ", ";
128 Result += calcTypeName(*I, TypeStack, TypeNames);
129 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000130 if (FTy->isVarArg()) {
131 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000132 Result += "...";
133 }
134 Result += ")";
135 break;
136 }
137 case Type::StructTyID: {
138 const StructType *STy = cast<const StructType>(Ty);
139 Result = "{ ";
140 for (StructType::ElementTypes::const_iterator
141 I = STy->getElementTypes().begin(),
142 E = STy->getElementTypes().end(); I != E; ++I) {
143 if (I != STy->getElementTypes().begin())
144 Result += ", ";
145 Result += calcTypeName(*I, TypeStack, TypeNames);
146 }
147 Result += " }";
148 break;
149 }
150 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000151 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000152 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000153 break;
154 case Type::ArrayTyID: {
155 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000156 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000157 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
158 break;
159 }
160 default:
Vikram S. Adveb952b542002-07-14 23:14:45 +0000161 Result = "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000162 }
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
Chris Lattnere5fd3862002-07-31 23:56:44 +0000275 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000276 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 Lattner113f4f42002-06-25 16:13:24 +0000323 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000324 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 }
Vikram S. Adveb952b542002-07-14 23:14:45 +0000336
Chris Lattner3cd8c562002-07-30 18:54:25 +0000337 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000338 Out << CE->getOpcodeName();
339
340 bool isGEP = CE->getOpcode() == Instruction::GetElementPtr;
Chris Lattnercfe8f532002-08-16 21:17:11 +0000341 Out << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000342
343 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
344 printTypeInt(Out, (*OI)->getType(), TypeTable);
345 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
346 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000347 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000348 }
349
Chris Lattnercfe8f532002-08-16 21:17:11 +0000350 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000351 Out << " to ";
352 printTypeInt(Out, CE->getType(), TypeTable);
353 }
Chris Lattnercfe8f532002-08-16 21:17:11 +0000354 Out << ")";
Chris Lattner83b396b2002-08-15 19:37:43 +0000355
Chris Lattnerd84bb632002-04-16 21:36:08 +0000356 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000357 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000358 }
359}
360
361
362// WriteAsOperand - Write the name of the specified value out to the specified
363// ostream. This can be useful when you just want to print int %reg126, not the
364// whole instruction that generated it.
365//
366static void WriteAsOperandInternal(ostream &Out, const Value *V, bool PrintName,
367 map<const Type *, string> &TypeTable,
368 SlotCalculator *Table) {
369 Out << " ";
370 if (PrintName && V->hasName()) {
371 Out << "%" << V->getName();
372 } else {
373 if (const Constant *CV = dyn_cast<const Constant>(V)) {
374 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
375 } else {
376 int Slot;
377 if (Table) {
378 Slot = Table->getValSlot(V);
379 } else {
380 if (const Type *Ty = dyn_cast<const Type>(V)) {
381 Out << Ty->getDescription();
382 return;
383 }
384
385 Table = createSlotCalculator(V);
386 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
387
388 Slot = Table->getValSlot(V);
389 delete Table;
390 }
391 if (Slot >= 0) Out << "%" << Slot;
392 else if (PrintName)
393 Out << "<badref>"; // Not embeded into a location?
394 }
395 }
396}
397
398
Chris Lattnerb86620e2001-10-29 16:37:48 +0000399
400// WriteAsOperand - Write the name of the specified value out to the specified
401// ostream. This can be useful when you just want to print int %reg126, not the
402// whole instruction that generated it.
403//
404ostream &WriteAsOperand(ostream &Out, const Value *V, bool PrintType,
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000405 bool PrintName, const Module *Context) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000406 map<const Type *, string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000407 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000408
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000409 if (Context && Context->hasSymbolTable())
410 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000411
412 if (PrintType)
413 printTypeInt(Out, V->getType(), TypeNames);
414
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000415 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000416 return Out;
417}
418
419
Chris Lattner2e9fee42001-07-12 23:35:26 +0000420
Chris Lattnerfee714f2001-09-07 16:36:04 +0000421class AssemblyWriter {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000422 ostream &Out;
423 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000424 const Module *TheModule;
425 map<const Type *, string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000426public:
Chris Lattner7bfee412001-10-29 16:05:51 +0000427 inline AssemblyWriter(ostream &o, SlotCalculator &Tab, const Module *M)
428 : Out(o), Table(Tab), TheModule(M) {
429
430 // If the module has a symbol table, take all global types and stuff their
431 // names into the TypeNames map.
432 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000433 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000434 }
435
Chris Lattner7bfee412001-10-29 16:05:51 +0000436 inline void write(const Module *M) { printModule(M); }
437 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000438 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000439 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000440 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000441 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000442 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000443
Chris Lattner1e194682002-04-18 18:53:13 +0000444 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
445
Chris Lattner2f7c9632001-06-06 20:29:01 +0000446private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000447 void printModule(const Module *M);
448 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000449 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000450 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000451 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000452 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000453 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000454 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000455
456 // printType - Go to extreme measures to attempt to print out a short,
457 // symbolic version of a type name.
458 //
459 ostream &printType(const Type *Ty) {
460 return printTypeInt(Out, Ty, TypeNames);
461 }
462
463 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
464 // without considering any symbolic types that we may have equal to it.
465 //
466 ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000467
Chris Lattner862e3382001-10-13 06:42:36 +0000468 // printInfoComment - Print a little comment after the instruction indicating
469 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000470 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000471};
472
473
Chris Lattnerd816b532002-04-13 20:53:41 +0000474// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
475// without considering any symbolic types that we may have equal to it.
476//
477ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000478 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000479 printType(FTy->getReturnType()) << " (";
480 for (FunctionType::ParamTypes::const_iterator
481 I = FTy->getParamTypes().begin(),
482 E = FTy->getParamTypes().end(); I != E; ++I) {
483 if (I != FTy->getParamTypes().begin())
484 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000485 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000486 }
487 if (FTy->isVarArg()) {
488 if (!FTy->getParamTypes().empty()) Out << ", ";
489 Out << "...";
490 }
491 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000492 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000493 Out << "{ ";
494 for (StructType::ElementTypes::const_iterator
495 I = STy->getElementTypes().begin(),
496 E = STy->getElementTypes().end(); I != E; ++I) {
497 if (I != STy->getElementTypes().begin())
498 Out << ", ";
499 printType(*I);
500 }
501 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000502 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000503 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000504 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000505 Out << "[" << ATy->getNumElements() << " x ";
506 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000507 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner2904f442002-05-26 20:17:54 +0000508 Out << OTy->getDescription();
Chris Lattnerd816b532002-04-13 20:53:41 +0000509 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000510 if (!Ty->isPrimitiveType())
511 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000512 printType(Ty);
513 }
514 return Out;
515}
516
517
Chris Lattnerfee714f2001-09-07 16:36:04 +0000518void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
519 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000520 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000521 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000522}
523
Chris Lattner2f7c9632001-06-06 20:29:01 +0000524
Chris Lattner7bfee412001-10-29 16:05:51 +0000525void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000526 // Loop over the symbol table, emitting all named constants...
527 if (M->hasSymbolTable())
Chris Lattner7bfee412001-10-29 16:05:51 +0000528 printSymbolTable(*M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000529
Chris Lattner113f4f42002-06-25 16:13:24 +0000530 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
531 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000532
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000533 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000534
Chris Lattner6915f8f2002-04-07 22:49:37 +0000535 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000536 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
537 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000538}
539
Chris Lattner7bfee412001-10-29 16:05:51 +0000540void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000541 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000542
Chris Lattner841d8b92001-11-26 18:54:16 +0000543 if (GV->hasInternalLinkage()) Out << "internal ";
Chris Lattnerd1319862002-10-06 22:48:09 +0000544 if (!GV->hasInitializer()) Out << "external ";
Chris Lattner37798642001-09-18 04:01:05 +0000545
Chris Lattner7bfee412001-10-29 16:05:51 +0000546 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000547 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000548
549 if (GV->hasInitializer())
550 writeOperand(GV->getInitializer(), false, false);
551
Chris Lattner113f4f42002-06-25 16:13:24 +0000552 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000553 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000554}
555
Chris Lattnerfee714f2001-09-07 16:36:04 +0000556
Chris Lattner7bfee412001-10-29 16:05:51 +0000557// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000558// if a named constant is found, emit it's declaration...
559//
Chris Lattner7bfee412001-10-29 16:05:51 +0000560void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000561 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
562 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
563 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
564
565 for (; I != End; ++I) {
566 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000567 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000568 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000569 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000570 Out << "\t%" << I->first << " = type ";
571
572 // Make sure we print out at least one level of the type structure, so
573 // that we do not get %FILE = type %FILE
574 //
575 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000576 }
577 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000578 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000579}
580
581
Chris Lattner7bfee412001-10-29 16:05:51 +0000582// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000583//
Chris Lattner3462ae32001-12-03 22:26:30 +0000584void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000585 // Don't print out unnamed constants, they will be inlined
586 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000587
Chris Lattneree998be2001-07-26 16:29:38 +0000588 // Print out name...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000589 Out << "\t%" << CPV->getName() << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000590
591 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000592 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000593
Chris Lattner113f4f42002-06-25 16:13:24 +0000594 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000595 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596}
597
Chris Lattner6915f8f2002-04-07 22:49:37 +0000598// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000599//
Chris Lattner113f4f42002-06-25 16:13:24 +0000600void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000601 // Print out the return type and name...
Chris Lattner113f4f42002-06-25 16:13:24 +0000602 Out << "\n" << (F->isExternal() ? "declare " : "")
603 << (F->hasInternalLinkage() ? "internal " : "");
604 printType(F->getReturnType()) << " %" << F->getName() << "(";
605 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000606
Chris Lattner7bfee412001-10-29 16:05:51 +0000607 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000608 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000609
Chris Lattner149376d2002-10-13 20:57:00 +0000610 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
611 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000612
613 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000614 if (FT->isVarArg()) {
615 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000616 Out << "..."; // Output varargs portion of signature!
617 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000618 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000619
Chris Lattner113f4f42002-06-25 16:13:24 +0000620 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000621 Out << "\n";
622 } else {
623 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000624
Chris Lattner6915f8f2002-04-07 22:49:37 +0000625 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000626 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
627 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000628
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000629 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000630 }
631
Chris Lattner6915f8f2002-04-07 22:49:37 +0000632 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000633}
634
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000635// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000636// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000637//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000638void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000639 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000640 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000641
642 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000643 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000644
645 // Output name, if available...
646 if (Arg->hasName())
647 Out << " %" << Arg->getName();
648 else if (Table.getValSlot(Arg) < 0)
649 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000650}
651
Chris Lattner7bfee412001-10-29 16:05:51 +0000652// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000653//
Chris Lattner7bfee412001-10-29 16:05:51 +0000654void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000655 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner58185f22002-10-02 19:38:55 +0000656 Out << "\n" << BB->getName() << ":";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000657 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000658 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000659 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000661 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000662 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000663 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000664 }
665
666 // Output predecessors for the block...
667 Out << "\t\t;";
668 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
669
670 if (PI == PE) {
671 Out << " No predecessors!";
672 } else {
673 Out << " preds =";
674 writeOperand(*PI, false, true);
675 for (++PI; PI != PE; ++PI) {
676 Out << ",";
677 writeOperand(*PI, false, true);
678 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000679 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000680
681 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000682
Chris Lattnerfee714f2001-09-07 16:36:04 +0000683 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000684 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
685 printInstruction(*I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000686}
687
Chris Lattner862e3382001-10-13 06:42:36 +0000688
689// printInfoComment - Print a little comment after the instruction indicating
690// which slot it occupies.
691//
Chris Lattner113f4f42002-06-25 16:13:24 +0000692void AssemblyWriter::printInfoComment(const Value &V) {
693 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000694 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000695 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000696
Chris Lattner113f4f42002-06-25 16:13:24 +0000697 if (!V.hasName()) {
698 int Slot = Table.getValSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000699 if (Slot >= 0) Out << ":" << Slot;
700 else Out << ":<badref>";
701 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000702 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000703 }
704}
705
Chris Lattner7bfee412001-10-29 16:05:51 +0000706// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000707//
Chris Lattner113f4f42002-06-25 16:13:24 +0000708void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000709 Out << "\t";
710
711 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000712 if (I.hasName())
713 Out << "%" << I.getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000714
715 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000716 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000717
718 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000719 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720
721 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000722 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
723 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000724 Out << ",";
725 writeOperand(Operand, true);
726 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000727 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000728
Chris Lattner8d48df22002-04-13 18:34:38 +0000729 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000730 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000731 writeOperand(Operand , true); Out << ",";
732 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000733
Chris Lattner113f4f42002-06-25 16:13:24 +0000734 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000735 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000736 writeOperand(I.getOperand(op ), true); Out << ",";
737 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000738 }
739 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000740 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000741 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000742 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000743 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000744
Chris Lattner113f4f42002-06-25 16:13:24 +0000745 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000746 if (op) Out << ", ";
747 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000748 writeOperand(I.getOperand(op ), false); Out << ",";
749 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000750 }
Chris Lattner862e3382001-10-13 06:42:36 +0000751 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000752 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000753 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000754 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000755 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000756 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
757
758 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000759 // only do this if the first argument is a pointer to a nonvararg function,
760 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000761 //
Chris Lattner8d48df22002-04-13 18:34:38 +0000762 if (RetTy && MTy && !MTy->isVarArg() &&
763 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000764 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000765 Out << " "; printType(RetTy);
766 writeOperand(Operand, false);
767 } else {
768 writeOperand(Operand, true);
769 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000770 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000771 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
772 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000773 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000774 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000775 }
776
777 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000778 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner862e3382001-10-13 06:42:36 +0000779 // TODO: Should try to print out short form of the Invoke instruction
780 writeOperand(Operand, true);
781 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000782 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
783 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000784 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000785 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000786 }
787
788 Out << " )\n\t\t\tto";
789 writeOperand(II->getNormalDest(), true);
790 Out << " except";
791 writeOperand(II->getExceptionalDest(), true);
792
Chris Lattner113f4f42002-06-25 16:13:24 +0000793 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000794 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000795 printType(AI->getType()->getElementType());
796 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000797 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000798 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000799 }
Chris Lattner862e3382001-10-13 06:42:36 +0000800 } else if (isa<CastInst>(I)) {
Chris Lattner143e5a42002-05-22 22:29:26 +0000801 if (Operand) writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000802 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000803 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000804 } else if (Operand) { // Print the normal way...
805
806 // PrintAllTypes - Instructions who have operands of all the same type
807 // omit the type from all but the first operand. If the instruction has
808 // different type operands (for example br), then they are all printed.
809 bool PrintAllTypes = false;
810 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000811
Chris Lattner113f4f42002-06-25 16:13:24 +0000812 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
813 Operand = I.getOperand(i);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000814 if (Operand->getType() != TheType) {
815 PrintAllTypes = true; // We have differing types! Print them all!
816 break;
817 }
818 }
819
Chris Lattnerb6fe2342001-10-20 09:33:10 +0000820 // Shift Left & Right print both types even for Ubyte LHS
821 if (isa<ShiftInst>(I)) PrintAllTypes = true;
822
Chris Lattner7bfee412001-10-29 16:05:51 +0000823 if (!PrintAllTypes) {
824 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000825 printType(I.getOperand(0)->getType());
Chris Lattner7bfee412001-10-29 16:05:51 +0000826 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000827
Chris Lattner113f4f42002-06-25 16:13:24 +0000828 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000829 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000830 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000831 }
832 }
833
Chris Lattner862e3382001-10-13 06:42:36 +0000834 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000835 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000836}
837
838
839//===----------------------------------------------------------------------===//
840// External Interface declarations
841//===----------------------------------------------------------------------===//
842
843
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000844void Module::print(std::ostream &o) const {
845 SlotCalculator SlotTable(this, true);
846 AssemblyWriter W(o, SlotTable, this);
847 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000848}
849
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000850void GlobalVariable::print(std::ostream &o) const {
851 SlotCalculator SlotTable(getParent(), true);
852 AssemblyWriter W(o, SlotTable, getParent());
853 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000854}
855
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000856void Function::print(std::ostream &o) const {
857 SlotCalculator SlotTable(getParent(), true);
858 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000859
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000860 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000861}
862
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000863void BasicBlock::print(std::ostream &o) const {
864 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000865 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000866 getParent() ? getParent()->getParent() : 0);
867 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000868}
869
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000870void Instruction::print(std::ostream &o) const {
871 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000872 SlotCalculator SlotTable(F, true);
873 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000874
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000875 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000876}
Chris Lattner7db79582001-11-07 04:21:57 +0000877
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000878void Constant::print(std::ostream &o) const {
879 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +0000880
881 // Handle CPR's special, because they have context information...
882 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
883 CPR->getValue()->print(o); // Print as a global value, with context info.
884 return;
885 }
886
Chris Lattner1e194682002-04-18 18:53:13 +0000887 o << " " << getType()->getDescription() << " ";
888
889 map<const Type *, string> TypeTable;
890 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000891}
892
893void Type::print(std::ostream &o) const {
894 if (this == 0)
895 o << "<null Type>";
896 else
897 o << getDescription();
898}
899
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000900void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000901 o << getType() << " " << getName();
902}
903
904void Value::dump() const { print(std::cerr); }
905
906//===----------------------------------------------------------------------===//
907// CachedWriter Class Implementation
908//===----------------------------------------------------------------------===//
909
Chris Lattner7db79582001-11-07 04:21:57 +0000910void CachedWriter::setModule(const Module *M) {
911 delete SC; delete AW;
912 if (M) {
913 SC = new SlotCalculator(M, true);
914 AW = new AssemblyWriter(Out, *SC, M);
915 } else {
916 SC = 0; AW = 0;
917 }
918}
919
920CachedWriter::~CachedWriter() {
921 delete AW;
922 delete SC;
923}
924
925CachedWriter &CachedWriter::operator<<(const Value *V) {
926 assert(AW && SC && "CachedWriter does not have a current module!");
927 switch (V->getValueType()) {
928 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +0000929 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000930 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
931 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
932 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000933 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000934 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000935 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
936 }
937 return *this;
938}