blob: c060a8df20af3c52fa0a475ce5157d6106460a99 [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 Lattner7bfee412001-10-29 16:05:51 +000028
Chris Lattnerc8b70922002-07-26 21:12:46 +000029static RegisterPass<PrintModulePass>
30X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
31static RegisterPass<PrintFunctionPass>
32Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +000033
Chris Lattnerab7d1ab2003-05-08 02:08:14 +000034static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
35 bool PrintName,
36 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +000037 SlotCalculator *Table);
38
Chris Lattnerb86620e2001-10-29 16:37:48 +000039static const Module *getModuleFromVal(const Value *V) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000040 if (const Argument *MA = dyn_cast<const Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000041 return MA->getParent() ? MA->getParent()->getParent() : 0;
42 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V))
43 return BB->getParent() ? BB->getParent()->getParent() : 0;
44 else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000045 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000046 return M ? M->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +000047 } else if (const GlobalValue *GV = dyn_cast<const GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000048 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000049 return 0;
50}
51
Chris Lattner7bfee412001-10-29 16:05:51 +000052static SlotCalculator *createSlotCalculator(const Value *V) {
53 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner2e9fa6d2002-04-09 19:48:49 +000054 if (const Argument *FA = dyn_cast<const Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000055 return new SlotCalculator(FA->getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +000056 } else if (const Instruction *I = dyn_cast<const Instruction>(V)) {
57 return new SlotCalculator(I->getParent()->getParent(), true);
58 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(V)) {
59 return new SlotCalculator(BB->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000060 } else if (const GlobalVariable *GV = dyn_cast<const GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000061 return new SlotCalculator(GV->getParent(), true);
Chris Lattner57698e22002-03-26 18:01:55 +000062 } else if (const Function *Func = dyn_cast<const Function>(V)) {
63 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000064 }
65 return 0;
66}
Chris Lattner2f7c9632001-06-06 20:29:01 +000067
Chris Lattnerb86620e2001-10-29 16:37:48 +000068
69// If the module has a symbol table, take all global types and stuff their
70// names into the TypeNames map.
71//
72static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +000073 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +000074 if (!M) return;
75 const SymbolTable &ST = M->getSymbolTable();
76 SymbolTable::const_iterator PI = ST.find(Type::TypeTy);
77 if (PI != ST.end()) {
78 SymbolTable::type_const_iterator I = PI->second.begin();
79 for (; I != PI->second.end(); ++I) {
80 // As a heuristic, don't insert pointer to primitive types, because
81 // they are used too often to have a single useful name.
82 //
83 const Type *Ty = cast<const Type>(I->second);
84 if (!isa<PointerType>(Ty) ||
85 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
86 TypeNames.insert(std::make_pair(Ty, "%"+I->first));
Chris Lattnerb86620e2001-10-29 16:37:48 +000087 }
88 }
89}
90
91
92
Chris Lattnerab7d1ab2003-05-08 02:08:14 +000093static std::string calcTypeName(const Type *Ty,
94 std::vector<const Type *> &TypeStack,
95 std::map<const Type *, std::string> &TypeNames){
Chris Lattnerb86620e2001-10-29 16:37:48 +000096 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
97
98 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +000099 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000100 if (I != TypeNames.end()) return I->second;
101
102 // Check to see if the Type is already on the stack...
103 unsigned Slot = 0, CurSize = TypeStack.size();
104 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
105
106 // This is another base case for the recursion. In this case, we know
107 // that we have looped back to a type that we have previously visited.
108 // Generate the appropriate upreference to handle this.
109 //
110 if (Slot < CurSize)
111 return "\\" + utostr(CurSize-Slot); // Here's the upreference
112
113 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
114
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000115 std::string Result;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000116 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000117 case Type::FunctionTyID: {
Chris Lattnerd816b532002-04-13 20:53:41 +0000118 const FunctionType *FTy = cast<const FunctionType>(Ty);
119 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000120 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000121 I = FTy->getParamTypes().begin(),
122 E = FTy->getParamTypes().end(); I != E; ++I) {
123 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000124 Result += ", ";
125 Result += calcTypeName(*I, TypeStack, TypeNames);
126 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000127 if (FTy->isVarArg()) {
128 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000129 Result += "...";
130 }
131 Result += ")";
132 break;
133 }
134 case Type::StructTyID: {
135 const StructType *STy = cast<const StructType>(Ty);
136 Result = "{ ";
137 for (StructType::ElementTypes::const_iterator
138 I = STy->getElementTypes().begin(),
139 E = STy->getElementTypes().end(); I != E; ++I) {
140 if (I != STy->getElementTypes().begin())
141 Result += ", ";
142 Result += calcTypeName(*I, TypeStack, TypeNames);
143 }
144 Result += " }";
145 break;
146 }
147 case Type::PointerTyID:
Chris Lattner2413b162001-12-04 00:03:30 +0000148 Result = calcTypeName(cast<const PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000149 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000150 break;
151 case Type::ArrayTyID: {
152 const ArrayType *ATy = cast<const ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000153 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000154 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
155 break;
156 }
157 default:
Vikram S. Adveb952b542002-07-14 23:14:45 +0000158 Result = "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000159 }
160
161 TypeStack.pop_back(); // Remove self from stack...
162 return Result;
163}
164
165
166// printTypeInt - The internal guts of printing out a type that has a
167// potentially named portion.
168//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000169static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
170 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000171 // Primitive types always print out their description, regardless of whether
172 // they have been named or not.
173 //
174 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
175
176 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000177 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000178 if (I != TypeNames.end()) return Out << I->second;
179
180 // Otherwise we have a type that has not been named but is a derived type.
181 // Carefully recurse the type hierarchy to print out any contained symbolic
182 // names.
183 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000184 std::vector<const Type *> TypeStack;
185 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000186 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000187 return Out << TypeName;
188}
189
Chris Lattner34b95182001-10-31 04:33:19 +0000190
Chris Lattnerb86620e2001-10-29 16:37:48 +0000191// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
192// type, iff there is an entry in the modules symbol table for the specified
193// type or one of it's component types. This is slower than a simple x << Type;
194//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000195std::ostream &WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
196 const Module *M) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000197 Out << " ";
198
199 // If they want us to print out a type, attempt to make it symbolic if there
200 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000201 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000202 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000203 fillTypeNameTable(M, TypeNames);
204
Chris Lattner72f866e2001-10-29 16:40:32 +0000205 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000206 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000207 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000208 }
209}
210
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000211static void WriteConstantInt(std::ostream &Out, const Constant *CV,
212 bool PrintName,
213 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000214 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000215 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
216 Out << (CB == ConstantBool::True ? "true" : "false");
217 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
218 Out << CI->getValue();
219 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
220 Out << CI->getValue();
221 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
222 // We would like to output the FP constant value in exponential notation,
223 // but we cannot do this if doing so will lose precision. Check here to
224 // make sure that we only output it in exponential format if we can parse
225 // the value back and get the same value.
226 //
227 std::string StrVal = ftostr(CFP->getValue());
228
229 // Check to make sure that the stringized number is not some string like
230 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
231 // the string matches the "[-+]?[0-9]" regex.
232 //
233 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
234 ((StrVal[0] == '-' || StrVal[0] == '+') &&
235 (StrVal[0] >= '0' && StrVal[0] <= '9')))
236 // Reparse stringized version!
237 if (atof(StrVal.c_str()) == CFP->getValue()) {
238 Out << StrVal; return;
239 }
240
241 // Otherwise we could not reparse it to exactly the same value, so we must
242 // output the string in hexadecimal format!
243 //
244 // Behave nicely in the face of C TBAA rules... see:
245 // http://www.nullstone.com/htmls/category/aliastyp.htm
246 //
247 double Val = CFP->getValue();
248 char *Ptr = (char*)&Val;
249 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
250 "assuming that double is 64 bits!");
251 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
252
253 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
254 // As a special case, print the array as a string if it is an array of
255 // ubytes or an array of sbytes with positive values.
256 //
257 const Type *ETy = CA->getType()->getElementType();
258 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
259
260 if (ETy == Type::SByteTy)
261 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
262 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
263 isString = false;
264 break;
265 }
266
267 if (isString) {
268 Out << "c\"";
269 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
270 unsigned char C = (ETy == Type::SByteTy) ?
271 (unsigned char)cast<ConstantSInt>(CA->getOperand(i))->getValue() :
272 (unsigned char)cast<ConstantUInt>(CA->getOperand(i))->getValue();
273
Chris Lattnere5fd3862002-07-31 23:56:44 +0000274 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000275 Out << C;
276 } else {
277 Out << '\\'
278 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
279 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
280 }
281 }
282 Out << "\"";
283
284 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000285 Out << "[";
286 if (CA->getNumOperands()) {
287 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000288 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000289 WriteAsOperandInternal(Out, CA->getOperand(0),
290 PrintName, TypeTable, Table);
291 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
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(i), PrintName,
295 TypeTable, Table);
296 }
297 }
298 Out << " ]";
299 }
300 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
301 Out << "{";
302 if (CS->getNumOperands()) {
303 Out << " ";
304 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
305
306 WriteAsOperandInternal(Out, CS->getOperand(0),
307 PrintName, TypeTable, Table);
308
309 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
310 Out << ", ";
311 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
312
313 WriteAsOperandInternal(Out, CS->getOperand(i),
314 PrintName, TypeTable, Table);
315 }
316 }
317
318 Out << " }";
319 } else if (isa<ConstantPointerNull>(CV)) {
320 Out << "null";
321
Chris Lattner113f4f42002-06-25 16:13:24 +0000322 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000323 const GlobalValue *V = PR->getValue();
324 if (V->hasName()) {
325 Out << "%" << V->getName();
326 } else if (Table) {
327 int Slot = Table->getValSlot(V);
328 if (Slot >= 0)
329 Out << "%" << Slot;
330 else
331 Out << "<pointer reference badref>";
332 } else {
333 Out << "<pointer reference without context info>";
334 }
Vikram S. Adveb952b542002-07-14 23:14:45 +0000335
Chris Lattner3cd8c562002-07-30 18:54:25 +0000336 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000337 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000338
339 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
340 printTypeInt(Out, (*OI)->getType(), TypeTable);
341 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
342 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000343 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000344 }
345
Chris Lattnercfe8f532002-08-16 21:17:11 +0000346 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000347 Out << " to ";
348 printTypeInt(Out, CE->getType(), TypeTable);
349 }
Chris Lattnercfe8f532002-08-16 21:17:11 +0000350 Out << ")";
Chris Lattner83b396b2002-08-15 19:37:43 +0000351
Chris Lattnerd84bb632002-04-16 21:36:08 +0000352 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000353 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000354 }
355}
356
357
358// WriteAsOperand - Write the name of the specified value out to the specified
359// ostream. This can be useful when you just want to print int %reg126, not the
360// whole instruction that generated it.
361//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000362static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
363 bool PrintName,
364 std::map<const Type*, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000365 SlotCalculator *Table) {
366 Out << " ";
367 if (PrintName && V->hasName()) {
368 Out << "%" << V->getName();
369 } else {
370 if (const Constant *CV = dyn_cast<const Constant>(V)) {
371 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
372 } else {
373 int Slot;
374 if (Table) {
375 Slot = Table->getValSlot(V);
376 } else {
377 if (const Type *Ty = dyn_cast<const Type>(V)) {
378 Out << Ty->getDescription();
379 return;
380 }
381
382 Table = createSlotCalculator(V);
383 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
384
385 Slot = Table->getValSlot(V);
386 delete Table;
387 }
388 if (Slot >= 0) Out << "%" << Slot;
389 else if (PrintName)
390 Out << "<badref>"; // Not embeded into a location?
391 }
392 }
393}
394
395
Chris Lattnerb86620e2001-10-29 16:37:48 +0000396
397// WriteAsOperand - Write the name of the specified value out to the specified
398// ostream. This can be useful when you just want to print int %reg126, not the
399// whole instruction that generated it.
400//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000401std::ostream &WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
402 bool PrintName, const Module *Context) {
403 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000404 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000405
Chris Lattner98cf1f52002-11-20 18:36:02 +0000406 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000407 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000408
409 if (PrintType)
410 printTypeInt(Out, V->getType(), TypeNames);
411
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000412 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000413 return Out;
414}
415
416
Chris Lattner2e9fee42001-07-12 23:35:26 +0000417
Chris Lattnerfee714f2001-09-07 16:36:04 +0000418class AssemblyWriter {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000419 std::ostream &Out;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000420 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000421 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000422 std::map<const Type *, std::string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000423public:
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000424 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M)
Chris Lattner7bfee412001-10-29 16:05:51 +0000425 : Out(o), Table(Tab), TheModule(M) {
426
427 // If the module has a symbol table, take all global types and stuff their
428 // names into the TypeNames map.
429 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000430 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000431 }
432
Chris Lattner7bfee412001-10-29 16:05:51 +0000433 inline void write(const Module *M) { printModule(M); }
434 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000435 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000436 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000437 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000438 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000439 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000440
Chris Lattner1e194682002-04-18 18:53:13 +0000441 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
442
Chris Lattner2f7c9632001-06-06 20:29:01 +0000443private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000444 void printModule(const Module *M);
445 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000446 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000447 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000448 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000449 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000450 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000451 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000452
453 // printType - Go to extreme measures to attempt to print out a short,
454 // symbolic version of a type name.
455 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000456 std::ostream &printType(const Type *Ty) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000457 return printTypeInt(Out, Ty, TypeNames);
458 }
459
460 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
461 // without considering any symbolic types that we may have equal to it.
462 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000463 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000464
Chris Lattner862e3382001-10-13 06:42:36 +0000465 // printInfoComment - Print a little comment after the instruction indicating
466 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000467 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000468};
469
470
Chris Lattnerd816b532002-04-13 20:53:41 +0000471// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
472// without considering any symbolic types that we may have equal to it.
473//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000474std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000475 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000476 printType(FTy->getReturnType()) << " (";
477 for (FunctionType::ParamTypes::const_iterator
478 I = FTy->getParamTypes().begin(),
479 E = FTy->getParamTypes().end(); I != E; ++I) {
480 if (I != FTy->getParamTypes().begin())
481 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000482 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000483 }
484 if (FTy->isVarArg()) {
485 if (!FTy->getParamTypes().empty()) Out << ", ";
486 Out << "...";
487 }
488 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000489 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000490 Out << "{ ";
491 for (StructType::ElementTypes::const_iterator
492 I = STy->getElementTypes().begin(),
493 E = STy->getElementTypes().end(); I != E; ++I) {
494 if (I != STy->getElementTypes().begin())
495 Out << ", ";
496 printType(*I);
497 }
498 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000499 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000500 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000501 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000502 Out << "[" << ATy->getNumElements() << " x ";
503 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000504 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner2904f442002-05-26 20:17:54 +0000505 Out << OTy->getDescription();
Chris Lattnerd816b532002-04-13 20:53:41 +0000506 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000507 if (!Ty->isPrimitiveType())
508 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000509 printType(Ty);
510 }
511 return Out;
512}
513
514
Chris Lattnerfee714f2001-09-07 16:36:04 +0000515void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
516 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000517 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000518 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000519}
520
Chris Lattner2f7c9632001-06-06 20:29:01 +0000521
Chris Lattner7bfee412001-10-29 16:05:51 +0000522void AssemblyWriter::printModule(const Module *M) {
Chris Lattnerc840aa62003-04-22 19:07:19 +0000523 Out << "target endian = " << (M->isLittleEndian() ? "little" : "big") << "\n";
524 Out << "target pointersize = " << (M->has32BitPointers() ? 32 : 64) << "\n";
525
Chris Lattnerfee714f2001-09-07 16:36:04 +0000526 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000527 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000528
Chris Lattner113f4f42002-06-25 16:13:24 +0000529 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
530 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000531
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000532 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000533
Chris Lattner6915f8f2002-04-07 22:49:37 +0000534 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000535 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
536 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000537}
538
Chris Lattner7bfee412001-10-29 16:05:51 +0000539void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattnerda975502001-09-10 07:58:01 +0000540 if (GV->hasName()) Out << "%" << GV->getName() << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000541
Chris Lattner379a8d22003-04-16 20:28:45 +0000542 if (!GV->hasInitializer())
543 Out << "external ";
544 else
545 switch (GV->getLinkage()) {
546 case GlobalValue::InternalLinkage: Out << "internal "; break;
547 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
548 case GlobalValue::AppendingLinkage: Out << "appending "; break;
549 case GlobalValue::ExternalLinkage: break;
550 }
Chris Lattner37798642001-09-18 04:01:05 +0000551
Chris Lattner7bfee412001-10-29 16:05:51 +0000552 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000553 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000554
555 if (GV->hasInitializer())
556 writeOperand(GV->getInitializer(), false, false);
557
Chris Lattner113f4f42002-06-25 16:13:24 +0000558 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000559 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000560}
561
Chris Lattnerfee714f2001-09-07 16:36:04 +0000562
Chris Lattner7bfee412001-10-29 16:05:51 +0000563// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000564// if a named constant is found, emit it's declaration...
565//
Chris Lattner7bfee412001-10-29 16:05:51 +0000566void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000567 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
568 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
569 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
570
571 for (; I != End; ++I) {
572 const Value *V = I->second;
Chris Lattner3462ae32001-12-03 22:26:30 +0000573 if (const Constant *CPV = dyn_cast<const Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000574 printConstant(CPV);
Chris Lattner38569342001-10-01 20:11:19 +0000575 } else if (const Type *Ty = dyn_cast<const Type>(V)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000576 Out << "\t%" << I->first << " = type ";
577
578 // Make sure we print out at least one level of the type structure, so
579 // that we do not get %FILE = type %FILE
580 //
581 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000582 }
583 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000584 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000585}
586
587
Chris Lattner7bfee412001-10-29 16:05:51 +0000588// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000589//
Chris Lattner3462ae32001-12-03 22:26:30 +0000590void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000591 // Don't print out unnamed constants, they will be inlined
592 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000593
Chris Lattneree998be2001-07-26 16:29:38 +0000594 // Print out name...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000595 Out << "\t%" << CPV->getName() << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000596
597 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000598 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000599
Chris Lattner113f4f42002-06-25 16:13:24 +0000600 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000601 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000602}
603
Chris Lattner6915f8f2002-04-07 22:49:37 +0000604// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000605//
Chris Lattner113f4f42002-06-25 16:13:24 +0000606void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000607 // Print out the return type and name...
Chris Lattner379a8d22003-04-16 20:28:45 +0000608 Out << "\n";
609
610 if (F->isExternal())
611 Out << "declare ";
612 else
613 switch (F->getLinkage()) {
614 case GlobalValue::InternalLinkage: Out << "internal "; break;
615 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
616 case GlobalValue::AppendingLinkage: Out << "appending "; break;
617 case GlobalValue::ExternalLinkage: break;
618 }
619
Chris Lattner113f4f42002-06-25 16:13:24 +0000620 printType(F->getReturnType()) << " %" << F->getName() << "(";
621 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000622
Chris Lattner7bfee412001-10-29 16:05:51 +0000623 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000624 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000625
Chris Lattner149376d2002-10-13 20:57:00 +0000626 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
627 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000628
629 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000630 if (FT->isVarArg()) {
631 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000632 Out << "..."; // Output varargs portion of signature!
633 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000634 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000635
Chris Lattner113f4f42002-06-25 16:13:24 +0000636 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000637 Out << "\n";
638 } else {
639 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000640
Chris Lattner6915f8f2002-04-07 22:49:37 +0000641 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000642 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
643 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000644
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000645 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000646 }
647
Chris Lattner6915f8f2002-04-07 22:49:37 +0000648 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000649}
650
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000651// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000652// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000653//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000654void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000655 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000656 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000657
658 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000659 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660
661 // Output name, if available...
662 if (Arg->hasName())
663 Out << " %" << Arg->getName();
664 else if (Table.getValSlot(Arg) < 0)
665 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000666}
667
Chris Lattner7bfee412001-10-29 16:05:51 +0000668// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000669//
Chris Lattner7bfee412001-10-29 16:05:51 +0000670void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000671 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner58185f22002-10-02 19:38:55 +0000672 Out << "\n" << BB->getName() << ":";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000673 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000674 int Slot = Table.getValSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000675 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000676 if (Slot >= 0)
Chris Lattnera2f01872001-06-07 16:58:55 +0000677 Out << Slot; // Extra newline seperates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000678 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000679 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000680 }
681
682 // Output predecessors for the block...
683 Out << "\t\t;";
684 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
685
686 if (PI == PE) {
687 Out << " No predecessors!";
688 } else {
689 Out << " preds =";
690 writeOperand(*PI, false, true);
691 for (++PI; PI != PE; ++PI) {
692 Out << ",";
693 writeOperand(*PI, false, true);
694 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000695 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000696
697 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000698
Chris Lattnerfee714f2001-09-07 16:36:04 +0000699 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000700 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
701 printInstruction(*I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000702}
703
Chris Lattner862e3382001-10-13 06:42:36 +0000704
705// printInfoComment - Print a little comment after the instruction indicating
706// which slot it occupies.
707//
Chris Lattner113f4f42002-06-25 16:13:24 +0000708void AssemblyWriter::printInfoComment(const Value &V) {
709 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000710 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000711 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000712
Chris Lattner113f4f42002-06-25 16:13:24 +0000713 if (!V.hasName()) {
714 int Slot = Table.getValSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000715 if (Slot >= 0) Out << ":" << Slot;
716 else Out << ":<badref>";
717 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000718 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000719 }
720}
721
Chris Lattner7bfee412001-10-29 16:05:51 +0000722// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000723//
Chris Lattner113f4f42002-06-25 16:13:24 +0000724void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000725 Out << "\t";
726
727 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000728 if (I.hasName())
729 Out << "%" << I.getName() << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000730
731 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000732 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000733
734 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000735 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000736
737 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000738 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
739 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000740 Out << ",";
741 writeOperand(Operand, true);
742 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000743 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000744
Chris Lattner8d48df22002-04-13 18:34:38 +0000745 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000746 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000747 writeOperand(Operand , true); Out << ",";
748 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000749
Chris Lattner113f4f42002-06-25 16:13:24 +0000750 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000751 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000752 writeOperand(I.getOperand(op ), true); Out << ",";
753 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000754 }
755 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000756 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000757 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000758 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000759 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000760
Chris Lattner113f4f42002-06-25 16:13:24 +0000761 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000762 if (op) Out << ", ";
763 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000764 writeOperand(I.getOperand(op ), false); Out << ",";
765 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000766 }
Chris Lattner862e3382001-10-13 06:42:36 +0000767 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000768 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000769 } else if (isa<CallInst>(I)) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000770 const PointerType *PTy = dyn_cast<PointerType>(Operand->getType());
Chris Lattner91db5822002-03-29 03:44:36 +0000771 const FunctionType*MTy = PTy ? dyn_cast<FunctionType>(PTy->getElementType()):0;
Chris Lattner2f2d9472001-11-06 21:28:12 +0000772 const Type *RetTy = MTy ? MTy->getReturnType() : 0;
773
774 // If possible, print out the short form of the call instruction, but we can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000775 // only do this if the first argument is a pointer to a nonvararg function,
776 // and if the value returned is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000777 //
Chris Lattner8d48df22002-04-13 18:34:38 +0000778 if (RetTy && MTy && !MTy->isVarArg() &&
779 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000780 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000781 Out << " "; printType(RetTy);
782 writeOperand(Operand, false);
783 } else {
784 writeOperand(Operand, true);
785 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000786 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000787 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
788 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000789 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000790 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000791 }
792
793 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000794 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner862e3382001-10-13 06:42:36 +0000795 // TODO: Should try to print out short form of the Invoke instruction
796 writeOperand(Operand, true);
797 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000798 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
799 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000800 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000801 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000802 }
803
804 Out << " )\n\t\t\tto";
805 writeOperand(II->getNormalDest(), true);
806 Out << " except";
807 writeOperand(II->getExceptionalDest(), true);
808
Chris Lattner113f4f42002-06-25 16:13:24 +0000809 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000810 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000811 printType(AI->getType()->getElementType());
812 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000813 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000814 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000815 }
Chris Lattner862e3382001-10-13 06:42:36 +0000816 } else if (isa<CastInst>(I)) {
Chris Lattner143e5a42002-05-22 22:29:26 +0000817 if (Operand) writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000818 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000819 printType(I.getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000820 } else if (Operand) { // Print the normal way...
821
822 // PrintAllTypes - Instructions who have operands of all the same type
823 // omit the type from all but the first operand. If the instruction has
824 // different type operands (for example br), then they are all printed.
825 bool PrintAllTypes = false;
826 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000827
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000828 // Shift Left & Right print both types even for Ubyte LHS
829 if (isa<ShiftInst>(I)) {
830 PrintAllTypes = true;
831 } else {
832 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
833 Operand = I.getOperand(i);
834 if (Operand->getType() != TheType) {
835 PrintAllTypes = true; // We have differing types! Print them all!
836 break;
837 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000838 }
839 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000840
Chris Lattner7bfee412001-10-29 16:05:51 +0000841 if (!PrintAllTypes) {
842 Out << " ";
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000843 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +0000844 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000845
Chris Lattner113f4f42002-06-25 16:13:24 +0000846 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000847 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000848 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000849 }
850 }
851
Chris Lattner862e3382001-10-13 06:42:36 +0000852 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000853 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000854}
855
856
857//===----------------------------------------------------------------------===//
858// External Interface declarations
859//===----------------------------------------------------------------------===//
860
861
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000862void Module::print(std::ostream &o) const {
863 SlotCalculator SlotTable(this, true);
864 AssemblyWriter W(o, SlotTable, this);
865 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000866}
867
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000868void GlobalVariable::print(std::ostream &o) const {
869 SlotCalculator SlotTable(getParent(), true);
870 AssemblyWriter W(o, SlotTable, getParent());
871 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000872}
873
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000874void Function::print(std::ostream &o) const {
875 SlotCalculator SlotTable(getParent(), true);
876 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000877
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000878 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000879}
880
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000881void BasicBlock::print(std::ostream &o) const {
882 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000883 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000884 getParent() ? getParent()->getParent() : 0);
885 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000886}
887
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000888void Instruction::print(std::ostream &o) const {
889 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000890 SlotCalculator SlotTable(F, true);
891 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000892
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000893 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000894}
Chris Lattner7db79582001-11-07 04:21:57 +0000895
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000896void Constant::print(std::ostream &o) const {
897 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +0000898
899 // Handle CPR's special, because they have context information...
900 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
901 CPR->getValue()->print(o); // Print as a global value, with context info.
902 return;
903 }
904
Chris Lattner1e194682002-04-18 18:53:13 +0000905 o << " " << getType()->getDescription() << " ";
906
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000907 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +0000908 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000909}
910
911void Type::print(std::ostream &o) const {
912 if (this == 0)
913 o << "<null Type>";
914 else
915 o << getDescription();
916}
917
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000918void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000919 o << getType() << " " << getName();
920}
921
922void Value::dump() const { print(std::cerr); }
923
924//===----------------------------------------------------------------------===//
925// CachedWriter Class Implementation
926//===----------------------------------------------------------------------===//
927
Chris Lattner7db79582001-11-07 04:21:57 +0000928void CachedWriter::setModule(const Module *M) {
929 delete SC; delete AW;
930 if (M) {
931 SC = new SlotCalculator(M, true);
932 AW = new AssemblyWriter(Out, *SC, M);
933 } else {
934 SC = 0; AW = 0;
935 }
936}
937
938CachedWriter::~CachedWriter() {
939 delete AW;
940 delete SC;
941}
942
943CachedWriter &CachedWriter::operator<<(const Value *V) {
944 assert(AW && SC && "CachedWriter does not have a current module!");
945 switch (V->getValueType()) {
946 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +0000947 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000948 case Value::TypeVal: AW->write(cast<const Type>(V)); break;
949 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
950 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +0000951 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000952 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +0000953 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
954 }
955 return *this;
956}