blob: 7fde12203593f1ee432917b57d55da8e240289d4 [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner7db79582001-11-07 04:21:57 +000017#include "llvm/Assembly/CachedWriter.h"
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000018#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000020#include "llvm/SlotCalculator.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000021#include "llvm/DerivedTypes.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000022#include "llvm/Instruction.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000023#include "llvm/Module.h"
Chris Lattnerca142372002-04-28 19:55:58 +000024#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000025#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000026#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000027#include "llvm/iPHINode.h"
28#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000029#include "llvm/SymbolTable.h"
Chris Lattner58185f22002-10-02 19:38:55 +000030#include "llvm/Support/CFG.h"
Chris Lattner5de22042001-11-27 00:03:19 +000031#include "Support/StringExtras.h"
32#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000033#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000034
Chris Lattnerc8b70922002-07-26 21:12:46 +000035static RegisterPass<PrintModulePass>
36X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
37static RegisterPass<PrintFunctionPass>
38Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +000039
Chris Lattnerab7d1ab2003-05-08 02:08:14 +000040static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
41 bool PrintName,
42 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +000043 SlotCalculator *Table);
44
Chris Lattnerb86620e2001-10-29 16:37:48 +000045static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000046 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000047 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000048 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000049 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000050 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000051 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000052 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000053 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000054 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000055 return 0;
56}
57
Chris Lattner7bfee412001-10-29 16:05:51 +000058static SlotCalculator *createSlotCalculator(const Value *V) {
59 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000060 if (const Argument *FA = dyn_cast<Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000061 return new SlotCalculator(FA->getParent(), true);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000062 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +000063 return new SlotCalculator(I->getParent()->getParent(), true);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000064 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +000065 return new SlotCalculator(BB->getParent(), true);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000066 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000067 return new SlotCalculator(GV->getParent(), true);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000068 } else if (const Function *Func = dyn_cast<Function>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000069 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000070 }
71 return 0;
72}
Chris Lattner2f7c9632001-06-06 20:29:01 +000073
Chris Lattner1c343042003-08-22 05:40:38 +000074// getLLVMName - Turn the specified string into an 'LLVM name', which is either
75// prefixed with % (if the string only contains simple characters) or is
76// surrounded with ""'s (if it has special chars in it).
77static std::string getLLVMName(const std::string &Name) {
78 assert(!Name.empty() && "Cannot get empty name!");
79
80 // First character cannot start with a number...
81 if (Name[0] >= '0' && Name[0] <= '9')
82 return "\"" + Name + "\"";
83
84 // Scan to see if we have any characters that are not on the "white list"
85 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
86 char C = Name[i];
87 assert(C != '"' && "Illegal character in LLVM value name!");
88 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
89 C != '-' && C != '.' && C != '_')
90 return "\"" + Name + "\"";
91 }
92
93 // If we get here, then the identifier is legal to use as a "VarID".
94 return "%"+Name;
95}
96
Chris Lattnerb86620e2001-10-29 16:37:48 +000097
98// If the module has a symbol table, take all global types and stuff their
99// names into the TypeNames map.
100//
101static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000102 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000103 if (!M) return;
104 const SymbolTable &ST = M->getSymbolTable();
105 SymbolTable::const_iterator PI = ST.find(Type::TypeTy);
106 if (PI != ST.end()) {
107 SymbolTable::type_const_iterator I = PI->second.begin();
108 for (; I != PI->second.end(); ++I) {
109 // As a heuristic, don't insert pointer to primitive types, because
110 // they are used too often to have a single useful name.
111 //
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000112 const Type *Ty = cast<Type>(I->second);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000113 if (!isa<PointerType>(Ty) ||
114 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner1c343042003-08-22 05:40:38 +0000115 TypeNames.insert(std::make_pair(Ty, getLLVMName(I->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000116 }
117 }
118}
119
120
121
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000122static std::string calcTypeName(const Type *Ty,
123 std::vector<const Type *> &TypeStack,
124 std::map<const Type *, std::string> &TypeNames){
Chris Lattnerb86620e2001-10-29 16:37:48 +0000125 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
126
127 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000128 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000129 if (I != TypeNames.end()) return I->second;
130
131 // Check to see if the Type is already on the stack...
132 unsigned Slot = 0, CurSize = TypeStack.size();
133 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
134
135 // This is another base case for the recursion. In this case, we know
136 // that we have looped back to a type that we have previously visited.
137 // Generate the appropriate upreference to handle this.
138 //
139 if (Slot < CurSize)
140 return "\\" + utostr(CurSize-Slot); // Here's the upreference
141
142 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
143
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000144 std::string Result;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000145 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000146 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000147 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattnerd816b532002-04-13 20:53:41 +0000148 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000149 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000150 I = FTy->getParamTypes().begin(),
151 E = FTy->getParamTypes().end(); I != E; ++I) {
152 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000153 Result += ", ";
154 Result += calcTypeName(*I, TypeStack, TypeNames);
155 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000156 if (FTy->isVarArg()) {
157 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000158 Result += "...";
159 }
160 Result += ")";
161 break;
162 }
163 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000164 const StructType *STy = cast<StructType>(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000165 Result = "{ ";
166 for (StructType::ElementTypes::const_iterator
167 I = STy->getElementTypes().begin(),
168 E = STy->getElementTypes().end(); I != E; ++I) {
169 if (I != STy->getElementTypes().begin())
170 Result += ", ";
171 Result += calcTypeName(*I, TypeStack, TypeNames);
172 }
173 Result += " }";
174 break;
175 }
176 case Type::PointerTyID:
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000177 Result = calcTypeName(cast<PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000178 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000179 break;
180 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000181 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000182 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000183 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
184 break;
185 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000186 case Type::OpaqueTyID:
187 Result = "opaque";
188 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000189 default:
Vikram S. Adveb952b542002-07-14 23:14:45 +0000190 Result = "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000191 }
192
193 TypeStack.pop_back(); // Remove self from stack...
194 return Result;
195}
196
197
198// printTypeInt - The internal guts of printing out a type that has a
199// potentially named portion.
200//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000201static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
202 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000203 // Primitive types always print out their description, regardless of whether
204 // they have been named or not.
205 //
206 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
207
208 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000209 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000210 if (I != TypeNames.end()) return Out << I->second;
211
212 // Otherwise we have a type that has not been named but is a derived type.
213 // Carefully recurse the type hierarchy to print out any contained symbolic
214 // names.
215 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000216 std::vector<const Type *> TypeStack;
217 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000218 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000219 return Out << TypeName;
220}
221
Chris Lattner34b95182001-10-31 04:33:19 +0000222
Chris Lattnerb86620e2001-10-29 16:37:48 +0000223// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
224// type, iff there is an entry in the modules symbol table for the specified
225// type or one of it's component types. This is slower than a simple x << Type;
226//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000227std::ostream &WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
228 const Module *M) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000229 Out << " ";
230
231 // If they want us to print out a type, attempt to make it symbolic if there
232 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000233 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000234 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000235 fillTypeNameTable(M, TypeNames);
236
Chris Lattner72f866e2001-10-29 16:40:32 +0000237 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000238 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000239 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000240 }
241}
242
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000243static void WriteConstantInt(std::ostream &Out, const Constant *CV,
244 bool PrintName,
245 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000246 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000247 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
248 Out << (CB == ConstantBool::True ? "true" : "false");
249 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
250 Out << CI->getValue();
251 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
252 Out << CI->getValue();
253 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
254 // We would like to output the FP constant value in exponential notation,
255 // but we cannot do this if doing so will lose precision. Check here to
256 // make sure that we only output it in exponential format if we can parse
257 // the value back and get the same value.
258 //
259 std::string StrVal = ftostr(CFP->getValue());
260
261 // Check to make sure that the stringized number is not some string like
262 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
263 // the string matches the "[-+]?[0-9]" regex.
264 //
265 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
266 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000267 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000268 // Reparse stringized version!
269 if (atof(StrVal.c_str()) == CFP->getValue()) {
270 Out << StrVal; return;
271 }
272
273 // Otherwise we could not reparse it to exactly the same value, so we must
274 // output the string in hexadecimal format!
275 //
276 // Behave nicely in the face of C TBAA rules... see:
277 // http://www.nullstone.com/htmls/category/aliastyp.htm
278 //
279 double Val = CFP->getValue();
280 char *Ptr = (char*)&Val;
281 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
282 "assuming that double is 64 bits!");
283 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
284
285 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerd401f392003-06-28 20:08:24 +0000286 if (CA->getNumOperands() > 5 && CA->isNullValue()) {
287 Out << "zeroinitializer";
288 return;
289 }
290
Chris Lattner1e194682002-04-18 18:53:13 +0000291 // As a special case, print the array as a string if it is an array of
292 // ubytes or an array of sbytes with positive values.
293 //
294 const Type *ETy = CA->getType()->getElementType();
295 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
296
297 if (ETy == Type::SByteTy)
298 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
299 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
300 isString = false;
301 break;
302 }
303
304 if (isString) {
305 Out << "c\"";
306 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattner6077c312003-07-23 15:22:26 +0000307 unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000308
Chris Lattnere5fd3862002-07-31 23:56:44 +0000309 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000310 Out << C;
311 } else {
312 Out << '\\'
313 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
314 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
315 }
316 }
317 Out << "\"";
318
319 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000320 Out << "[";
321 if (CA->getNumOperands()) {
322 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000323 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000324 WriteAsOperandInternal(Out, CA->getOperand(0),
325 PrintName, TypeTable, Table);
326 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
327 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000328 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000329 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
330 TypeTable, Table);
331 }
332 }
333 Out << " ]";
334 }
335 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Chris Lattnerd401f392003-06-28 20:08:24 +0000336 if (CS->getNumOperands() > 5 && CS->isNullValue()) {
337 Out << "zeroinitializer";
338 return;
339 }
340
Chris Lattnerd84bb632002-04-16 21:36:08 +0000341 Out << "{";
342 if (CS->getNumOperands()) {
343 Out << " ";
344 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
345
346 WriteAsOperandInternal(Out, CS->getOperand(0),
347 PrintName, TypeTable, Table);
348
349 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
350 Out << ", ";
351 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
352
353 WriteAsOperandInternal(Out, CS->getOperand(i),
354 PrintName, TypeTable, Table);
355 }
356 }
357
358 Out << " }";
359 } else if (isa<ConstantPointerNull>(CV)) {
360 Out << "null";
361
Chris Lattner113f4f42002-06-25 16:13:24 +0000362 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000363 const GlobalValue *V = PR->getValue();
364 if (V->hasName()) {
Chris Lattner1c343042003-08-22 05:40:38 +0000365 Out << getLLVMName(V->getName());
Chris Lattner1e194682002-04-18 18:53:13 +0000366 } else if (Table) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000367 int Slot = Table->getSlot(V);
Chris Lattner1e194682002-04-18 18:53:13 +0000368 if (Slot >= 0)
369 Out << "%" << Slot;
370 else
371 Out << "<pointer reference badref>";
372 } else {
373 Out << "<pointer reference without context info>";
374 }
Vikram S. Adveb952b542002-07-14 23:14:45 +0000375
Chris Lattner3cd8c562002-07-30 18:54:25 +0000376 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000377 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000378
379 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
380 printTypeInt(Out, (*OI)->getType(), TypeTable);
381 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
382 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000383 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000384 }
385
Chris Lattnercfe8f532002-08-16 21:17:11 +0000386 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000387 Out << " to ";
388 printTypeInt(Out, CE->getType(), TypeTable);
389 }
Chris Lattnercfe8f532002-08-16 21:17:11 +0000390 Out << ")";
Chris Lattner83b396b2002-08-15 19:37:43 +0000391
Chris Lattnerd84bb632002-04-16 21:36:08 +0000392 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000393 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000394 }
395}
396
397
398// WriteAsOperand - Write the name of the specified value out to the specified
399// ostream. This can be useful when you just want to print int %reg126, not the
400// whole instruction that generated it.
401//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000402static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
403 bool PrintName,
404 std::map<const Type*, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000405 SlotCalculator *Table) {
406 Out << " ";
407 if (PrintName && V->hasName()) {
Chris Lattner1c343042003-08-22 05:40:38 +0000408 Out << getLLVMName(V->getName());
Chris Lattnerd84bb632002-04-16 21:36:08 +0000409 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000410 if (const Constant *CV = dyn_cast<Constant>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000411 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
412 } else {
413 int Slot;
414 if (Table) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000415 Slot = Table->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000416 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000417 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000418 Out << Ty->getDescription();
419 return;
420 }
421
422 Table = createSlotCalculator(V);
423 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
424
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000425 Slot = Table->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000426 delete Table;
427 }
428 if (Slot >= 0) Out << "%" << Slot;
429 else if (PrintName)
Misha Brukmanfa100532003-10-10 17:54:14 +0000430 Out << "<badref>"; // Not embedded into a location?
Chris Lattnerd84bb632002-04-16 21:36:08 +0000431 }
432 }
433}
434
435
Chris Lattnerb86620e2001-10-29 16:37:48 +0000436
437// WriteAsOperand - Write the name of the specified value out to the specified
438// ostream. This can be useful when you just want to print int %reg126, not the
439// whole instruction that generated it.
440//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000441std::ostream &WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
442 bool PrintName, const Module *Context) {
443 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000444 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000445
Chris Lattner98cf1f52002-11-20 18:36:02 +0000446 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000447 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000448
449 if (PrintType)
450 printTypeInt(Out, V->getType(), TypeNames);
451
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000452 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000453 return Out;
454}
455
456
Chris Lattner2e9fee42001-07-12 23:35:26 +0000457
Chris Lattnerfee714f2001-09-07 16:36:04 +0000458class AssemblyWriter {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000459 std::ostream &Out;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000460 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000461 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000462 std::map<const Type *, std::string> TypeNames;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000463public:
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000464 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M)
Chris Lattner7bfee412001-10-29 16:05:51 +0000465 : Out(o), Table(Tab), TheModule(M) {
466
467 // If the module has a symbol table, take all global types and stuff their
468 // names into the TypeNames map.
469 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000470 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000471 }
472
Chris Lattner7bfee412001-10-29 16:05:51 +0000473 inline void write(const Module *M) { printModule(M); }
474 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000475 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000476 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000477 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000478 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000479 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000480
Chris Lattner1e194682002-04-18 18:53:13 +0000481 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
482
Chris Lattner2f7c9632001-06-06 20:29:01 +0000483private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000484 void printModule(const Module *M);
485 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000486 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000487 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000488 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000489 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000490 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000491 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000492
493 // printType - Go to extreme measures to attempt to print out a short,
494 // symbolic version of a type name.
495 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000496 std::ostream &printType(const Type *Ty) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000497 return printTypeInt(Out, Ty, TypeNames);
498 }
499
500 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
501 // without considering any symbolic types that we may have equal to it.
502 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000503 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000504
Chris Lattner862e3382001-10-13 06:42:36 +0000505 // printInfoComment - Print a little comment after the instruction indicating
506 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000507 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000508};
509
510
Chris Lattnerd816b532002-04-13 20:53:41 +0000511// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
512// without considering any symbolic types that we may have equal to it.
513//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000514std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000515 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000516 printType(FTy->getReturnType()) << " (";
517 for (FunctionType::ParamTypes::const_iterator
518 I = FTy->getParamTypes().begin(),
519 E = FTy->getParamTypes().end(); I != E; ++I) {
520 if (I != FTy->getParamTypes().begin())
521 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000522 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000523 }
524 if (FTy->isVarArg()) {
525 if (!FTy->getParamTypes().empty()) Out << ", ";
526 Out << "...";
527 }
528 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000529 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000530 Out << "{ ";
531 for (StructType::ElementTypes::const_iterator
532 I = STy->getElementTypes().begin(),
533 E = STy->getElementTypes().end(); I != E; ++I) {
534 if (I != STy->getElementTypes().begin())
535 Out << ", ";
536 printType(*I);
537 }
538 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000539 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000540 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000541 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000542 Out << "[" << ATy->getNumElements() << " x ";
543 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000544 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner39adbfc2003-06-01 03:45:51 +0000545 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000546 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000547 if (!Ty->isPrimitiveType())
548 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000549 printType(Ty);
550 }
551 return Out;
552}
553
554
Chris Lattnerfee714f2001-09-07 16:36:04 +0000555void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
556 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000557 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000558 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000559}
560
Chris Lattner2f7c9632001-06-06 20:29:01 +0000561
Chris Lattner7bfee412001-10-29 16:05:51 +0000562void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-08-24 13:48:48 +0000563 switch (M->getEndianness()) {
564 case Module::LittleEndian: Out << "target endian = little\n"; break;
565 case Module::BigEndian: Out << "target endian = big\n"; break;
566 case Module::AnyEndianness: break;
567 }
568 switch (M->getPointerSize()) {
569 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
570 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
571 case Module::AnyPointerSize: break;
572 }
573
Chris Lattnerfee714f2001-09-07 16:36:04 +0000574 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000575 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000576
Chris Lattner113f4f42002-06-25 16:13:24 +0000577 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
578 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000579
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000580 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000581
Chris Lattner6915f8f2002-04-07 22:49:37 +0000582 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000583 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
584 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000585}
586
Chris Lattner7bfee412001-10-29 16:05:51 +0000587void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner1c343042003-08-22 05:40:38 +0000588 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000589
Chris Lattner379a8d22003-04-16 20:28:45 +0000590 if (!GV->hasInitializer())
591 Out << "external ";
592 else
593 switch (GV->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000594 case GlobalValue::InternalLinkage: Out << "internal "; break;
595 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
596 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000597 case GlobalValue::AppendingLinkage: Out << "appending "; break;
598 case GlobalValue::ExternalLinkage: break;
599 }
Chris Lattner37798642001-09-18 04:01:05 +0000600
Chris Lattner7bfee412001-10-29 16:05:51 +0000601 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000602 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000603
604 if (GV->hasInitializer())
605 writeOperand(GV->getInitializer(), false, false);
606
Chris Lattner113f4f42002-06-25 16:13:24 +0000607 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000608 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000609}
610
Chris Lattnerfee714f2001-09-07 16:36:04 +0000611
Chris Lattner7bfee412001-10-29 16:05:51 +0000612// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000613// if a named constant is found, emit it's declaration...
614//
Chris Lattner7bfee412001-10-29 16:05:51 +0000615void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000616 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
617 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
618 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
619
620 for (; I != End; ++I) {
621 const Value *V = I->second;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000622 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000623 printConstant(CPV);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000624 } else if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattner1c343042003-08-22 05:40:38 +0000625 Out << "\t" << getLLVMName(I->first) << " = type ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000626
627 // Make sure we print out at least one level of the type structure, so
628 // that we do not get %FILE = type %FILE
629 //
630 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000631 }
632 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000633 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000634}
635
636
Chris Lattner7bfee412001-10-29 16:05:51 +0000637// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000638//
Chris Lattner3462ae32001-12-03 22:26:30 +0000639void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000640 // Don't print out unnamed constants, they will be inlined
641 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000642
Chris Lattneree998be2001-07-26 16:29:38 +0000643 // Print out name...
Chris Lattner1c343042003-08-22 05:40:38 +0000644 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000645
646 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000647 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000648
Chris Lattner113f4f42002-06-25 16:13:24 +0000649 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000650 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000651}
652
Chris Lattner6915f8f2002-04-07 22:49:37 +0000653// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000654//
Chris Lattner113f4f42002-06-25 16:13:24 +0000655void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000656 // Print out the return type and name...
Chris Lattner379a8d22003-04-16 20:28:45 +0000657 Out << "\n";
658
659 if (F->isExternal())
660 Out << "declare ";
661 else
662 switch (F->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000663 case GlobalValue::InternalLinkage: Out << "internal "; break;
664 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
665 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000666 case GlobalValue::AppendingLinkage: Out << "appending "; break;
667 case GlobalValue::ExternalLinkage: break;
668 }
669
Chris Lattner13651f02003-09-03 17:56:43 +0000670 printType(F->getReturnType()) << " ";
Chris Lattner5b337482003-10-18 05:57:43 +0000671 if (!F->getName().empty())
672 Out << getLLVMName(F->getName());
673 else
674 Out << "\"\"";
Chris Lattner13651f02003-09-03 17:56:43 +0000675 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000676 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000677
Chris Lattner7bfee412001-10-29 16:05:51 +0000678 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000679 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000680
Chris Lattner149376d2002-10-13 20:57:00 +0000681 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
682 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000683
684 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000685 if (FT->isVarArg()) {
686 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000687 Out << "..."; // Output varargs portion of signature!
688 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000689 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000690
Chris Lattner113f4f42002-06-25 16:13:24 +0000691 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000692 Out << "\n";
693 } else {
694 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000695
Chris Lattner6915f8f2002-04-07 22:49:37 +0000696 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000697 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
698 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000699
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000700 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000701 }
702
Chris Lattner6915f8f2002-04-07 22:49:37 +0000703 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000704}
705
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000706// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000707// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000708//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000709void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000710 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000711 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000712
713 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000714 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000715
716 // Output name, if available...
717 if (Arg->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000718 Out << " " << getLLVMName(Arg->getName());
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000719 else if (Table.getSlot(Arg) < 0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000721}
722
Misha Brukmanfa100532003-10-10 17:54:14 +0000723// printBasicBlock - This member is called for each basic block in a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000724//
Chris Lattner7bfee412001-10-29 16:05:51 +0000725void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000726 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner58185f22002-10-02 19:38:55 +0000727 Out << "\n" << BB->getName() << ":";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000728 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000729 int Slot = Table.getSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000730 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000731 if (Slot >= 0)
Misha Brukman7fdaab42003-07-14 17:20:40 +0000732 Out << Slot; // Extra newline separates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000733 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000734 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000735 }
736
737 // Output predecessors for the block...
738 Out << "\t\t;";
739 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
740
741 if (PI == PE) {
742 Out << " No predecessors!";
743 } else {
744 Out << " preds =";
745 writeOperand(*PI, false, true);
746 for (++PI; PI != PE; ++PI) {
747 Out << ",";
748 writeOperand(*PI, false, true);
749 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000750 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000751
752 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000753
Chris Lattnerfee714f2001-09-07 16:36:04 +0000754 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000755 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
756 printInstruction(*I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000757}
758
Chris Lattner862e3382001-10-13 06:42:36 +0000759
760// printInfoComment - Print a little comment after the instruction indicating
761// which slot it occupies.
762//
Chris Lattner113f4f42002-06-25 16:13:24 +0000763void AssemblyWriter::printInfoComment(const Value &V) {
764 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000765 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000766 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000767
Chris Lattner113f4f42002-06-25 16:13:24 +0000768 if (!V.hasName()) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000769 int Slot = Table.getSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000770 if (Slot >= 0) Out << ":" << Slot;
771 else Out << ":<badref>";
772 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000773 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000774 }
775}
776
Misha Brukmanfa100532003-10-10 17:54:14 +0000777// printInstruction - This member is called for each Instruction in a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000778//
Chris Lattner113f4f42002-06-25 16:13:24 +0000779void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000780 Out << "\t";
781
782 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000783 if (I.hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000784 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000785
Chris Lattner504f9242003-09-08 17:45:59 +0000786 // If this is a volatile load or store, print out the volatile marker
787 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
788 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
789 Out << "volatile ";
790
Chris Lattner2f7c9632001-06-06 20:29:01 +0000791 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000792 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000793
794 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000795 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000796
797 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000798 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
799 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000800 Out << ",";
801 writeOperand(Operand, true);
802 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000803 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000804
Chris Lattner8d48df22002-04-13 18:34:38 +0000805 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000806 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000807 writeOperand(Operand , true); Out << ",";
808 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000809
Chris Lattner113f4f42002-06-25 16:13:24 +0000810 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000811 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000812 writeOperand(I.getOperand(op ), true); Out << ",";
813 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000814 }
815 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000816 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000817 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000818 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000819 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000820
Chris Lattner113f4f42002-06-25 16:13:24 +0000821 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000822 if (op) Out << ", ";
823 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000824 writeOperand(I.getOperand(op ), false); Out << ",";
825 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000826 }
Chris Lattner862e3382001-10-13 06:42:36 +0000827 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000828 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000829 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000830 const PointerType *PTy = cast<PointerType>(Operand->getType());
831 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
832 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +0000833
Chris Lattner463d6a52003-08-05 15:34:45 +0000834 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000835 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +0000836 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000837 //
Chris Lattner463d6a52003-08-05 15:34:45 +0000838 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +0000839 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000840 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000841 Out << " "; printType(RetTy);
842 writeOperand(Operand, false);
843 } else {
844 writeOperand(Operand, true);
845 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000846 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000847 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
848 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000849 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000850 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000851 }
852
853 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000854 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000855 const PointerType *PTy = cast<PointerType>(Operand->getType());
856 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
857 const Type *RetTy = FTy->getReturnType();
858
859 // If possible, print out the short form of the invoke instruction. We can
860 // only do this if the first argument is a pointer to a nonvararg function,
861 // and if the return type is not a pointer to a function.
862 //
863 if (!FTy->isVarArg() &&
864 (!isa<PointerType>(RetTy) ||
865 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
866 Out << " "; printType(RetTy);
867 writeOperand(Operand, false);
868 } else {
869 writeOperand(Operand, true);
870 }
871
Chris Lattner862e3382001-10-13 06:42:36 +0000872 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000873 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
874 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000875 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000876 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000877 }
878
879 Out << " )\n\t\t\tto";
880 writeOperand(II->getNormalDest(), true);
881 Out << " except";
882 writeOperand(II->getExceptionalDest(), true);
883
Chris Lattner113f4f42002-06-25 16:13:24 +0000884 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000885 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000886 printType(AI->getType()->getElementType());
887 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000888 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000889 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000890 }
Chris Lattner862e3382001-10-13 06:42:36 +0000891 } else if (isa<CastInst>(I)) {
Chris Lattnerf70da102003-05-08 02:44:12 +0000892 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000893 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000894 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000895 } else if (isa<VAArgInst>(I)) {
Chris Lattnerf70da102003-05-08 02:44:12 +0000896 writeOperand(Operand, true);
897 Out << ", ";
898 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000899 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
900 writeOperand(Operand, true);
901 Out << ", ";
902 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000903 } else if (Operand) { // Print the normal way...
904
905 // PrintAllTypes - Instructions who have operands of all the same type
906 // omit the type from all but the first operand. If the instruction has
907 // different type operands (for example br), then they are all printed.
908 bool PrintAllTypes = false;
909 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000910
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000911 // Shift Left & Right print both types even for Ubyte LHS
912 if (isa<ShiftInst>(I)) {
913 PrintAllTypes = true;
914 } else {
915 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
916 Operand = I.getOperand(i);
917 if (Operand->getType() != TheType) {
918 PrintAllTypes = true; // We have differing types! Print them all!
919 break;
920 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000921 }
922 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000923
Chris Lattner7bfee412001-10-29 16:05:51 +0000924 if (!PrintAllTypes) {
925 Out << " ";
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000926 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +0000927 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000928
Chris Lattner113f4f42002-06-25 16:13:24 +0000929 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000930 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000931 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000932 }
933 }
934
Chris Lattner862e3382001-10-13 06:42:36 +0000935 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000936 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000937}
938
939
940//===----------------------------------------------------------------------===//
941// External Interface declarations
942//===----------------------------------------------------------------------===//
943
944
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000945void Module::print(std::ostream &o) const {
946 SlotCalculator SlotTable(this, true);
947 AssemblyWriter W(o, SlotTable, this);
948 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000949}
950
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000951void GlobalVariable::print(std::ostream &o) const {
952 SlotCalculator SlotTable(getParent(), true);
953 AssemblyWriter W(o, SlotTable, getParent());
954 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000955}
956
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000957void Function::print(std::ostream &o) const {
958 SlotCalculator SlotTable(getParent(), true);
959 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000960
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000961 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000962}
963
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000964void BasicBlock::print(std::ostream &o) const {
965 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000966 AssemblyWriter W(o, SlotTable,
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000967 getParent() ? getParent()->getParent() : 0);
968 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000969}
970
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000971void Instruction::print(std::ostream &o) const {
972 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000973 SlotCalculator SlotTable(F, true);
974 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000975
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000976 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000977}
Chris Lattner7db79582001-11-07 04:21:57 +0000978
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000979void Constant::print(std::ostream &o) const {
980 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +0000981
982 // Handle CPR's special, because they have context information...
983 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
984 CPR->getValue()->print(o); // Print as a global value, with context info.
985 return;
986 }
987
Chris Lattner1e194682002-04-18 18:53:13 +0000988 o << " " << getType()->getDescription() << " ";
989
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000990 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +0000991 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000992}
993
994void Type::print(std::ostream &o) const {
995 if (this == 0)
996 o << "<null Type>";
997 else
998 o << getDescription();
999}
1000
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001001void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001002 o << getType() << " " << getName();
1003}
1004
1005void Value::dump() const { print(std::cerr); }
1006
1007//===----------------------------------------------------------------------===//
1008// CachedWriter Class Implementation
1009//===----------------------------------------------------------------------===//
1010
Chris Lattner7db79582001-11-07 04:21:57 +00001011void CachedWriter::setModule(const Module *M) {
1012 delete SC; delete AW;
1013 if (M) {
1014 SC = new SlotCalculator(M, true);
1015 AW = new AssemblyWriter(Out, *SC, M);
1016 } else {
1017 SC = 0; AW = 0;
1018 }
1019}
1020
1021CachedWriter::~CachedWriter() {
1022 delete AW;
1023 delete SC;
1024}
1025
1026CachedWriter &CachedWriter::operator<<(const Value *V) {
1027 assert(AW && SC && "CachedWriter does not have a current module!");
1028 switch (V->getValueType()) {
1029 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +00001030 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001031 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001032 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1033 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +00001034 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001035 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001036 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
1037 }
1038 return *this;
1039}