blob: f92fe25fd2528f0f2cbefc6d79c6f774eddee393 [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 Lattner8339f7d2003-10-30 23:41:03 +000020#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattner6915f8f2002-04-07 22:49:37 +000021#include "llvm/SlotCalculator.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000023#include "llvm/Instruction.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000024#include "llvm/Module.h"
Chris Lattnerca142372002-04-28 19:55:58 +000025#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000026#include "llvm/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000027#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000028#include "llvm/iPHINode.h"
29#include "llvm/iOther.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000030#include "llvm/SymbolTable.h"
Chris Lattner58185f22002-10-02 19:38:55 +000031#include "llvm/Support/CFG.h"
Chris Lattner5de22042001-11-27 00:03:19 +000032#include "Support/StringExtras.h"
33#include "Support/STLExtras.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000034#include <algorithm>
Chris Lattner7bfee412001-10-29 16:05:51 +000035
Brian Gaeke960707c2003-11-11 22:41:34 +000036namespace llvm {
37
Chris Lattnerc8b70922002-07-26 21:12:46 +000038static RegisterPass<PrintModulePass>
39X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
40static RegisterPass<PrintFunctionPass>
41Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +000042
Chris Lattnerab7d1ab2003-05-08 02:08:14 +000043static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
44 bool PrintName,
45 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +000046 SlotCalculator *Table);
47
Chris Lattnerb86620e2001-10-29 16:37:48 +000048static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000049 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000050 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000051 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000052 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000053 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000054 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000055 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000056 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000057 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000058 return 0;
59}
60
Chris Lattner7bfee412001-10-29 16:05:51 +000061static SlotCalculator *createSlotCalculator(const Value *V) {
62 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000063 if (const Argument *FA = dyn_cast<Argument>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000064 return new SlotCalculator(FA->getParent(), true);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000065 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +000066 return new SlotCalculator(I->getParent()->getParent(), true);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000067 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +000068 return new SlotCalculator(BB->getParent(), true);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000069 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Chris Lattner7bfee412001-10-29 16:05:51 +000070 return new SlotCalculator(GV->getParent(), true);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000071 } else if (const Function *Func = dyn_cast<Function>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000072 return new SlotCalculator(Func, true);
Chris Lattner7bfee412001-10-29 16:05:51 +000073 }
74 return 0;
75}
Chris Lattner2f7c9632001-06-06 20:29:01 +000076
Chris Lattner1c343042003-08-22 05:40:38 +000077// getLLVMName - Turn the specified string into an 'LLVM name', which is either
78// prefixed with % (if the string only contains simple characters) or is
79// surrounded with ""'s (if it has special chars in it).
80static std::string getLLVMName(const std::string &Name) {
81 assert(!Name.empty() && "Cannot get empty name!");
82
83 // First character cannot start with a number...
84 if (Name[0] >= '0' && Name[0] <= '9')
85 return "\"" + Name + "\"";
86
87 // Scan to see if we have any characters that are not on the "white list"
88 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
89 char C = Name[i];
90 assert(C != '"' && "Illegal character in LLVM value name!");
91 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
92 C != '-' && C != '.' && C != '_')
93 return "\"" + Name + "\"";
94 }
95
96 // If we get here, then the identifier is legal to use as a "VarID".
97 return "%"+Name;
98}
99
Chris Lattnerb86620e2001-10-29 16:37:48 +0000100
101// If the module has a symbol table, take all global types and stuff their
102// names into the TypeNames map.
103//
104static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000105 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000106 if (!M) return;
107 const SymbolTable &ST = M->getSymbolTable();
108 SymbolTable::const_iterator PI = ST.find(Type::TypeTy);
109 if (PI != ST.end()) {
110 SymbolTable::type_const_iterator I = PI->second.begin();
111 for (; I != PI->second.end(); ++I) {
112 // As a heuristic, don't insert pointer to primitive types, because
113 // they are used too often to have a single useful name.
114 //
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000115 const Type *Ty = cast<Type>(I->second);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000116 if (!isa<PointerType>(Ty) ||
Chris Lattnerf14ead92003-10-30 00:22:33 +0000117 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
118 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner1c343042003-08-22 05:40:38 +0000119 TypeNames.insert(std::make_pair(Ty, getLLVMName(I->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000120 }
121 }
122}
123
124
125
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000126static std::string calcTypeName(const Type *Ty,
127 std::vector<const Type *> &TypeStack,
128 std::map<const Type *, std::string> &TypeNames){
Chris Lattnerf14ead92003-10-30 00:22:33 +0000129 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
130 return Ty->getDescription(); // Base case
Chris Lattnerb86620e2001-10-29 16:37:48 +0000131
132 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000133 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000134 if (I != TypeNames.end()) return I->second;
135
Chris Lattnerf14ead92003-10-30 00:22:33 +0000136 if (isa<OpaqueType>(Ty))
137 return "opaque";
138
Chris Lattnerb86620e2001-10-29 16:37:48 +0000139 // Check to see if the Type is already on the stack...
140 unsigned Slot = 0, CurSize = TypeStack.size();
141 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
142
143 // This is another base case for the recursion. In this case, we know
144 // that we have looped back to a type that we have previously visited.
145 // Generate the appropriate upreference to handle this.
146 //
147 if (Slot < CurSize)
148 return "\\" + utostr(CurSize-Slot); // Here's the upreference
149
150 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
151
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000152 std::string Result;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000153 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000154 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000155 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattnerd816b532002-04-13 20:53:41 +0000156 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner91db5822002-03-29 03:44:36 +0000157 for (FunctionType::ParamTypes::const_iterator
Chris Lattnerd816b532002-04-13 20:53:41 +0000158 I = FTy->getParamTypes().begin(),
159 E = FTy->getParamTypes().end(); I != E; ++I) {
160 if (I != FTy->getParamTypes().begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000161 Result += ", ";
162 Result += calcTypeName(*I, TypeStack, TypeNames);
163 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000164 if (FTy->isVarArg()) {
165 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000166 Result += "...";
167 }
168 Result += ")";
169 break;
170 }
171 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000172 const StructType *STy = cast<StructType>(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000173 Result = "{ ";
174 for (StructType::ElementTypes::const_iterator
175 I = STy->getElementTypes().begin(),
176 E = STy->getElementTypes().end(); I != E; ++I) {
177 if (I != STy->getElementTypes().begin())
178 Result += ", ";
179 Result += calcTypeName(*I, TypeStack, TypeNames);
180 }
181 Result += " }";
182 break;
183 }
184 case Type::PointerTyID:
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000185 Result = calcTypeName(cast<PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000186 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000187 break;
188 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000189 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000190 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000191 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
192 break;
193 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000194 case Type::OpaqueTyID:
195 Result = "opaque";
196 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000197 default:
Vikram S. Adveb952b542002-07-14 23:14:45 +0000198 Result = "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000199 }
200
201 TypeStack.pop_back(); // Remove self from stack...
202 return Result;
203}
204
205
206// printTypeInt - The internal guts of printing out a type that has a
207// potentially named portion.
208//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000209static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
210 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000211 // Primitive types always print out their description, regardless of whether
212 // they have been named or not.
213 //
Chris Lattner92d60532003-10-30 00:12:51 +0000214 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
215 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000216
217 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000218 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000219 if (I != TypeNames.end()) return Out << I->second;
220
221 // Otherwise we have a type that has not been named but is a derived type.
222 // Carefully recurse the type hierarchy to print out any contained symbolic
223 // names.
224 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000225 std::vector<const Type *> TypeStack;
226 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000227 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000228 return Out << TypeName;
229}
230
Chris Lattner34b95182001-10-31 04:33:19 +0000231
Chris Lattnerb86620e2001-10-29 16:37:48 +0000232// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
233// type, iff there is an entry in the modules symbol table for the specified
234// type or one of it's component types. This is slower than a simple x << Type;
235//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000236std::ostream &WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
237 const Module *M) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000238 Out << " ";
239
240 // If they want us to print out a type, attempt to make it symbolic if there
241 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000242 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000243 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000244 fillTypeNameTable(M, TypeNames);
245
Chris Lattner72f866e2001-10-29 16:40:32 +0000246 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000247 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000248 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000249 }
250}
251
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000252static void WriteConstantInt(std::ostream &Out, const Constant *CV,
253 bool PrintName,
254 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000255 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000256 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
257 Out << (CB == ConstantBool::True ? "true" : "false");
258 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
259 Out << CI->getValue();
260 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
261 Out << CI->getValue();
262 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
263 // We would like to output the FP constant value in exponential notation,
264 // but we cannot do this if doing so will lose precision. Check here to
265 // make sure that we only output it in exponential format if we can parse
266 // the value back and get the same value.
267 //
268 std::string StrVal = ftostr(CFP->getValue());
269
270 // Check to make sure that the stringized number is not some string like
271 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
272 // the string matches the "[-+]?[0-9]" regex.
273 //
274 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
275 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000276 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000277 // Reparse stringized version!
278 if (atof(StrVal.c_str()) == CFP->getValue()) {
279 Out << StrVal; return;
280 }
281
282 // Otherwise we could not reparse it to exactly the same value, so we must
283 // output the string in hexadecimal format!
284 //
285 // Behave nicely in the face of C TBAA rules... see:
286 // http://www.nullstone.com/htmls/category/aliastyp.htm
287 //
288 double Val = CFP->getValue();
289 char *Ptr = (char*)&Val;
290 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
291 "assuming that double is 64 bits!");
292 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
293
294 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerd401f392003-06-28 20:08:24 +0000295 if (CA->getNumOperands() > 5 && CA->isNullValue()) {
296 Out << "zeroinitializer";
297 return;
298 }
299
Chris Lattner1e194682002-04-18 18:53:13 +0000300 // As a special case, print the array as a string if it is an array of
301 // ubytes or an array of sbytes with positive values.
302 //
303 const Type *ETy = CA->getType()->getElementType();
304 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
305
306 if (ETy == Type::SByteTy)
307 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
308 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
309 isString = false;
310 break;
311 }
312
313 if (isString) {
314 Out << "c\"";
315 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattner6077c312003-07-23 15:22:26 +0000316 unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000317
Chris Lattnere5fd3862002-07-31 23:56:44 +0000318 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000319 Out << C;
320 } else {
321 Out << '\\'
322 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
323 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
324 }
325 }
326 Out << "\"";
327
328 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000329 Out << "[";
330 if (CA->getNumOperands()) {
331 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000332 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000333 WriteAsOperandInternal(Out, CA->getOperand(0),
334 PrintName, TypeTable, Table);
335 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
336 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000337 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000338 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
339 TypeTable, Table);
340 }
341 }
342 Out << " ]";
343 }
344 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Chris Lattnerd401f392003-06-28 20:08:24 +0000345 if (CS->getNumOperands() > 5 && CS->isNullValue()) {
346 Out << "zeroinitializer";
347 return;
348 }
349
Chris Lattnerd84bb632002-04-16 21:36:08 +0000350 Out << "{";
351 if (CS->getNumOperands()) {
352 Out << " ";
353 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
354
355 WriteAsOperandInternal(Out, CS->getOperand(0),
356 PrintName, TypeTable, Table);
357
358 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
359 Out << ", ";
360 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
361
362 WriteAsOperandInternal(Out, CS->getOperand(i),
363 PrintName, TypeTable, Table);
364 }
365 }
366
367 Out << " }";
368 } else if (isa<ConstantPointerNull>(CV)) {
369 Out << "null";
370
Chris Lattner113f4f42002-06-25 16:13:24 +0000371 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000372 const GlobalValue *V = PR->getValue();
373 if (V->hasName()) {
Chris Lattner1c343042003-08-22 05:40:38 +0000374 Out << getLLVMName(V->getName());
Chris Lattner1e194682002-04-18 18:53:13 +0000375 } else if (Table) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000376 int Slot = Table->getSlot(V);
Chris Lattner1e194682002-04-18 18:53:13 +0000377 if (Slot >= 0)
378 Out << "%" << Slot;
379 else
380 Out << "<pointer reference badref>";
381 } else {
382 Out << "<pointer reference without context info>";
383 }
Vikram S. Adveb952b542002-07-14 23:14:45 +0000384
Chris Lattner3cd8c562002-07-30 18:54:25 +0000385 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000386 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000387
388 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
389 printTypeInt(Out, (*OI)->getType(), TypeTable);
390 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
391 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000392 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000393 }
394
Chris Lattnercfe8f532002-08-16 21:17:11 +0000395 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000396 Out << " to ";
397 printTypeInt(Out, CE->getType(), TypeTable);
398 }
Chris Lattnercfe8f532002-08-16 21:17:11 +0000399 Out << ")";
Chris Lattner83b396b2002-08-15 19:37:43 +0000400
Chris Lattnerd84bb632002-04-16 21:36:08 +0000401 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000402 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000403 }
404}
405
406
407// WriteAsOperand - Write the name of the specified value out to the specified
408// ostream. This can be useful when you just want to print int %reg126, not the
409// whole instruction that generated it.
410//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000411static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
412 bool PrintName,
413 std::map<const Type*, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000414 SlotCalculator *Table) {
415 Out << " ";
416 if (PrintName && V->hasName()) {
Chris Lattner1c343042003-08-22 05:40:38 +0000417 Out << getLLVMName(V->getName());
Chris Lattnerd84bb632002-04-16 21:36:08 +0000418 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000419 if (const Constant *CV = dyn_cast<Constant>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000420 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
421 } else {
422 int Slot;
423 if (Table) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000424 Slot = Table->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000425 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000426 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000427 Out << Ty->getDescription();
428 return;
429 }
430
431 Table = createSlotCalculator(V);
432 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
433
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000434 Slot = Table->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000435 delete Table;
436 }
437 if (Slot >= 0) Out << "%" << Slot;
438 else if (PrintName)
Misha Brukmanfa100532003-10-10 17:54:14 +0000439 Out << "<badref>"; // Not embedded into a location?
Chris Lattnerd84bb632002-04-16 21:36:08 +0000440 }
441 }
442}
443
444
Chris Lattnerb86620e2001-10-29 16:37:48 +0000445
446// WriteAsOperand - Write the name of the specified value out to the specified
447// ostream. This can be useful when you just want to print int %reg126, not the
448// whole instruction that generated it.
449//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000450std::ostream &WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
451 bool PrintName, const Module *Context) {
452 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000453 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000454
Chris Lattner98cf1f52002-11-20 18:36:02 +0000455 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000456 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000457
458 if (PrintType)
459 printTypeInt(Out, V->getType(), TypeNames);
460
Brian Gaekefda1f182003-11-16 23:08:27 +0000461 if (const Type *Ty = dyn_cast<Type> (V))
462 printTypeInt(Out, Ty, TypeNames);
463
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000464 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000465 return Out;
466}
467
468
Chris Lattner2e9fee42001-07-12 23:35:26 +0000469
Chris Lattnerfee714f2001-09-07 16:36:04 +0000470class AssemblyWriter {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000471 std::ostream &Out;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000472 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000473 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000474 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000475 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000476public:
Chris Lattner8339f7d2003-10-30 23:41:03 +0000477 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M,
478 AssemblyAnnotationWriter *AAW)
479 : Out(o), Table(Tab), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000480
481 // If the module has a symbol table, take all global types and stuff their
482 // names into the TypeNames map.
483 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000484 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000485 }
486
Chris Lattner7bfee412001-10-29 16:05:51 +0000487 inline void write(const Module *M) { printModule(M); }
488 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000489 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000490 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000491 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000492 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000493 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000494
Chris Lattner1e194682002-04-18 18:53:13 +0000495 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
496
Chris Lattner2f7c9632001-06-06 20:29:01 +0000497private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000498 void printModule(const Module *M);
499 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000500 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000501 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000502 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000503 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000504 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000505 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000506
507 // printType - Go to extreme measures to attempt to print out a short,
508 // symbolic version of a type name.
509 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000510 std::ostream &printType(const Type *Ty) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000511 return printTypeInt(Out, Ty, TypeNames);
512 }
513
514 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
515 // without considering any symbolic types that we may have equal to it.
516 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000517 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000518
Chris Lattner862e3382001-10-13 06:42:36 +0000519 // printInfoComment - Print a little comment after the instruction indicating
520 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000521 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000522};
523
524
Chris Lattnerd816b532002-04-13 20:53:41 +0000525// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
526// without considering any symbolic types that we may have equal to it.
527//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000528std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000529 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000530 printType(FTy->getReturnType()) << " (";
531 for (FunctionType::ParamTypes::const_iterator
532 I = FTy->getParamTypes().begin(),
533 E = FTy->getParamTypes().end(); I != E; ++I) {
534 if (I != FTy->getParamTypes().begin())
535 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000536 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000537 }
538 if (FTy->isVarArg()) {
539 if (!FTy->getParamTypes().empty()) Out << ", ";
540 Out << "...";
541 }
542 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000543 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000544 Out << "{ ";
545 for (StructType::ElementTypes::const_iterator
546 I = STy->getElementTypes().begin(),
547 E = STy->getElementTypes().end(); I != E; ++I) {
548 if (I != STy->getElementTypes().begin())
549 Out << ", ";
550 printType(*I);
551 }
552 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000553 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000554 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000555 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000556 Out << "[" << ATy->getNumElements() << " x ";
557 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000558 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner39adbfc2003-06-01 03:45:51 +0000559 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000560 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000561 if (!Ty->isPrimitiveType())
562 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000563 printType(Ty);
564 }
565 return Out;
566}
567
568
Chris Lattnerfee714f2001-09-07 16:36:04 +0000569void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
570 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000571 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000572 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000573}
574
Chris Lattner2f7c9632001-06-06 20:29:01 +0000575
Chris Lattner7bfee412001-10-29 16:05:51 +0000576void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-08-24 13:48:48 +0000577 switch (M->getEndianness()) {
578 case Module::LittleEndian: Out << "target endian = little\n"; break;
579 case Module::BigEndian: Out << "target endian = big\n"; break;
580 case Module::AnyEndianness: break;
581 }
582 switch (M->getPointerSize()) {
583 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
584 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
585 case Module::AnyPointerSize: break;
586 }
587
Chris Lattnerfee714f2001-09-07 16:36:04 +0000588 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000589 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000590
Chris Lattner113f4f42002-06-25 16:13:24 +0000591 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
592 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000593
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000594 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000595
Chris Lattner6915f8f2002-04-07 22:49:37 +0000596 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000597 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
598 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000599}
600
Chris Lattner7bfee412001-10-29 16:05:51 +0000601void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner1c343042003-08-22 05:40:38 +0000602 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000603
Chris Lattner379a8d22003-04-16 20:28:45 +0000604 if (!GV->hasInitializer())
605 Out << "external ";
606 else
607 switch (GV->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000608 case GlobalValue::InternalLinkage: Out << "internal "; break;
609 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
610 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000611 case GlobalValue::AppendingLinkage: Out << "appending "; break;
612 case GlobalValue::ExternalLinkage: break;
613 }
Chris Lattner37798642001-09-18 04:01:05 +0000614
Chris Lattner7bfee412001-10-29 16:05:51 +0000615 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000616 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000617
618 if (GV->hasInitializer())
619 writeOperand(GV->getInitializer(), false, false);
620
Chris Lattner113f4f42002-06-25 16:13:24 +0000621 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000622 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000623}
624
Chris Lattnerfee714f2001-09-07 16:36:04 +0000625
Chris Lattner7bfee412001-10-29 16:05:51 +0000626// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000627// if a named constant is found, emit it's declaration...
628//
Chris Lattner7bfee412001-10-29 16:05:51 +0000629void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000630 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
631 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
632 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
633
634 for (; I != End; ++I) {
635 const Value *V = I->second;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000636 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000637 printConstant(CPV);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000638 } else if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerda8571b2003-11-09 15:51:07 +0000639 assert(Ty->getType() == Type::TypeTy && TI->first == Type::TypeTy);
Chris Lattner1c343042003-08-22 05:40:38 +0000640 Out << "\t" << getLLVMName(I->first) << " = type ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000641
642 // Make sure we print out at least one level of the type structure, so
643 // that we do not get %FILE = type %FILE
644 //
645 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000646 }
647 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000648 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000649}
650
651
Chris Lattner7bfee412001-10-29 16:05:51 +0000652// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000653//
Chris Lattner3462ae32001-12-03 22:26:30 +0000654void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000655 // Don't print out unnamed constants, they will be inlined
656 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000657
Chris Lattneree998be2001-07-26 16:29:38 +0000658 // Print out name...
Chris Lattner1c343042003-08-22 05:40:38 +0000659 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660
661 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000662 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000663
Chris Lattner113f4f42002-06-25 16:13:24 +0000664 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000665 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000666}
667
Chris Lattner6915f8f2002-04-07 22:49:37 +0000668// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000669//
Chris Lattner113f4f42002-06-25 16:13:24 +0000670void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000671 // Print out the return type and name...
Chris Lattner379a8d22003-04-16 20:28:45 +0000672 Out << "\n";
673
Chris Lattner8339f7d2003-10-30 23:41:03 +0000674 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
675
Chris Lattner379a8d22003-04-16 20:28:45 +0000676 if (F->isExternal())
677 Out << "declare ";
678 else
679 switch (F->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000680 case GlobalValue::InternalLinkage: Out << "internal "; break;
681 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
682 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000683 case GlobalValue::AppendingLinkage: Out << "appending "; break;
684 case GlobalValue::ExternalLinkage: break;
685 }
686
Chris Lattner13651f02003-09-03 17:56:43 +0000687 printType(F->getReturnType()) << " ";
Chris Lattner5b337482003-10-18 05:57:43 +0000688 if (!F->getName().empty())
689 Out << getLLVMName(F->getName());
690 else
691 Out << "\"\"";
Chris Lattner13651f02003-09-03 17:56:43 +0000692 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000693 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000694
Chris Lattner7bfee412001-10-29 16:05:51 +0000695 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000696 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000697
Chris Lattner149376d2002-10-13 20:57:00 +0000698 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
699 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000700
701 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000702 if (FT->isVarArg()) {
703 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000704 Out << "..."; // Output varargs portion of signature!
705 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000706 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000707
Chris Lattner113f4f42002-06-25 16:13:24 +0000708 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000709 Out << "\n";
710 } else {
711 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000712
Chris Lattner6915f8f2002-04-07 22:49:37 +0000713 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000714 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
715 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000716
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000717 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000718 }
719
Chris Lattner6915f8f2002-04-07 22:49:37 +0000720 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000721}
722
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000723// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000724// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000725//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000726void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000727 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000728 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000729
730 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000731 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000732
733 // Output name, if available...
734 if (Arg->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000735 Out << " " << getLLVMName(Arg->getName());
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000736 else if (Table.getSlot(Arg) < 0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000737 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000738}
739
Misha Brukmanfa100532003-10-10 17:54:14 +0000740// printBasicBlock - This member is called for each basic block in a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000741//
Chris Lattner7bfee412001-10-29 16:05:51 +0000742void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000743 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner58185f22002-10-02 19:38:55 +0000744 Out << "\n" << BB->getName() << ":";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000745 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000746 int Slot = Table.getSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000747 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000748 if (Slot >= 0)
Misha Brukman7fdaab42003-07-14 17:20:40 +0000749 Out << Slot; // Extra newline separates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000750 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000751 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000752 }
753
Chris Lattner00211f12003-11-16 22:59:57 +0000754 if (BB != &BB->getParent()->front()) { // Not the entry block?
755 // Output predecessors for the block...
756 Out << "\t\t;";
757 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
758
759 if (PI == PE) {
760 Out << " No predecessors!";
761 } else {
762 Out << " preds =";
Chris Lattner58185f22002-10-02 19:38:55 +0000763 writeOperand(*PI, false, true);
Chris Lattner00211f12003-11-16 22:59:57 +0000764 for (++PI; PI != PE; ++PI) {
765 Out << ",";
766 writeOperand(*PI, false, true);
767 }
Chris Lattner58185f22002-10-02 19:38:55 +0000768 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000769 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000770
771 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000772
Chris Lattner8339f7d2003-10-30 23:41:03 +0000773 if (AnnotationWriter) AnnotationWriter->emitBasicBlockAnnot(BB, Out);
774
Chris Lattnerfee714f2001-09-07 16:36:04 +0000775 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000776 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
777 printInstruction(*I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000778}
779
Chris Lattner862e3382001-10-13 06:42:36 +0000780
781// printInfoComment - Print a little comment after the instruction indicating
782// which slot it occupies.
783//
Chris Lattner113f4f42002-06-25 16:13:24 +0000784void AssemblyWriter::printInfoComment(const Value &V) {
785 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000786 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000787 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000788
Chris Lattner113f4f42002-06-25 16:13:24 +0000789 if (!V.hasName()) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000790 int Slot = Table.getSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000791 if (Slot >= 0) Out << ":" << Slot;
792 else Out << ":<badref>";
793 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000794 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000795 }
796}
797
Misha Brukmanfa100532003-10-10 17:54:14 +0000798// printInstruction - This member is called for each Instruction in a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000799//
Chris Lattner113f4f42002-06-25 16:13:24 +0000800void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner8339f7d2003-10-30 23:41:03 +0000801 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
802
Chris Lattner2f7c9632001-06-06 20:29:01 +0000803 Out << "\t";
804
805 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000806 if (I.hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000807 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000808
Chris Lattner504f9242003-09-08 17:45:59 +0000809 // If this is a volatile load or store, print out the volatile marker
810 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
811 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
812 Out << "volatile ";
813
Chris Lattner2f7c9632001-06-06 20:29:01 +0000814 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000815 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000816
817 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000818 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000819
820 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000821 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
822 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000823 Out << ",";
824 writeOperand(Operand, true);
825 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000826 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000827
Chris Lattner8d48df22002-04-13 18:34:38 +0000828 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000829 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000830 writeOperand(Operand , true); Out << ",";
831 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000832
Chris Lattner113f4f42002-06-25 16:13:24 +0000833 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000834 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000835 writeOperand(I.getOperand(op ), true); Out << ",";
836 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000837 }
838 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000839 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000840 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000841 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000842 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000843
Chris Lattner113f4f42002-06-25 16:13:24 +0000844 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000845 if (op) Out << ", ";
846 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000847 writeOperand(I.getOperand(op ), false); Out << ",";
848 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000849 }
Chris Lattner862e3382001-10-13 06:42:36 +0000850 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000851 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000852 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000853 const PointerType *PTy = cast<PointerType>(Operand->getType());
854 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
855 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +0000856
Chris Lattner463d6a52003-08-05 15:34:45 +0000857 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000858 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +0000859 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000860 //
Chris Lattner463d6a52003-08-05 15:34:45 +0000861 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +0000862 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000863 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000864 Out << " "; printType(RetTy);
865 writeOperand(Operand, false);
866 } else {
867 writeOperand(Operand, true);
868 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000869 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000870 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
871 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000872 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000873 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000874 }
875
876 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000877 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000878 const PointerType *PTy = cast<PointerType>(Operand->getType());
879 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
880 const Type *RetTy = FTy->getReturnType();
881
882 // If possible, print out the short form of the invoke instruction. We can
883 // only do this if the first argument is a pointer to a nonvararg function,
884 // and if the return type is not a pointer to a function.
885 //
886 if (!FTy->isVarArg() &&
887 (!isa<PointerType>(RetTy) ||
888 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
889 Out << " "; printType(RetTy);
890 writeOperand(Operand, false);
891 } else {
892 writeOperand(Operand, true);
893 }
894
Chris Lattner862e3382001-10-13 06:42:36 +0000895 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000896 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
897 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000898 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000899 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000900 }
901
902 Out << " )\n\t\t\tto";
903 writeOperand(II->getNormalDest(), true);
904 Out << " except";
905 writeOperand(II->getExceptionalDest(), true);
906
Chris Lattner113f4f42002-06-25 16:13:24 +0000907 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000908 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000909 printType(AI->getType()->getElementType());
910 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000911 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000912 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000913 }
Chris Lattner862e3382001-10-13 06:42:36 +0000914 } else if (isa<CastInst>(I)) {
Chris Lattnerf70da102003-05-08 02:44:12 +0000915 writeOperand(Operand, true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000916 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000917 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000918 } else if (isa<VAArgInst>(I)) {
Chris Lattnerf70da102003-05-08 02:44:12 +0000919 writeOperand(Operand, true);
920 Out << ", ";
921 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000922 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
923 writeOperand(Operand, true);
924 Out << ", ";
925 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000926 } else if (Operand) { // Print the normal way...
927
928 // PrintAllTypes - Instructions who have operands of all the same type
929 // omit the type from all but the first operand. If the instruction has
930 // different type operands (for example br), then they are all printed.
931 bool PrintAllTypes = false;
932 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000933
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000934 // Shift Left & Right print both types even for Ubyte LHS
935 if (isa<ShiftInst>(I)) {
936 PrintAllTypes = true;
937 } else {
938 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
939 Operand = I.getOperand(i);
940 if (Operand->getType() != TheType) {
941 PrintAllTypes = true; // We have differing types! Print them all!
942 break;
943 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000944 }
945 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000946
Chris Lattner7bfee412001-10-29 16:05:51 +0000947 if (!PrintAllTypes) {
948 Out << " ";
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000949 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +0000950 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000951
Chris Lattner113f4f42002-06-25 16:13:24 +0000952 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000953 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000954 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000955 }
956 }
957
Chris Lattner862e3382001-10-13 06:42:36 +0000958 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000959 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000960}
961
962
963//===----------------------------------------------------------------------===//
964// External Interface declarations
965//===----------------------------------------------------------------------===//
966
Chris Lattner8339f7d2003-10-30 23:41:03 +0000967void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000968 SlotCalculator SlotTable(this, true);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000969 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000970 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000971}
972
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000973void GlobalVariable::print(std::ostream &o) const {
974 SlotCalculator SlotTable(getParent(), true);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000975 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000976 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000977}
978
Chris Lattner8339f7d2003-10-30 23:41:03 +0000979void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000980 SlotCalculator SlotTable(getParent(), true);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000981 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000982
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000983 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000984}
985
Chris Lattner8339f7d2003-10-30 23:41:03 +0000986void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000987 SlotCalculator SlotTable(getParent(), true);
Chris Lattner7bfee412001-10-29 16:05:51 +0000988 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000989 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000990 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000991}
992
Chris Lattner8339f7d2003-10-30 23:41:03 +0000993void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000994 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000995 SlotCalculator SlotTable(F, true);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000996 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000997
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000998 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000999}
Chris Lattner7db79582001-11-07 04:21:57 +00001000
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001001void Constant::print(std::ostream &o) const {
1002 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +00001003
1004 // Handle CPR's special, because they have context information...
1005 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
1006 CPR->getValue()->print(o); // Print as a global value, with context info.
1007 return;
1008 }
1009
Chris Lattner1e194682002-04-18 18:53:13 +00001010 o << " " << getType()->getDescription() << " ";
1011
Chris Lattnerab7d1ab2003-05-08 02:08:14 +00001012 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +00001013 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001014}
1015
1016void Type::print(std::ostream &o) const {
1017 if (this == 0)
1018 o << "<null Type>";
1019 else
1020 o << getDescription();
1021}
1022
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001023void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001024 o << getType() << " " << getName();
1025}
1026
1027void Value::dump() const { print(std::cerr); }
1028
1029//===----------------------------------------------------------------------===//
1030// CachedWriter Class Implementation
1031//===----------------------------------------------------------------------===//
1032
Chris Lattner7db79582001-11-07 04:21:57 +00001033void CachedWriter::setModule(const Module *M) {
1034 delete SC; delete AW;
1035 if (M) {
1036 SC = new SlotCalculator(M, true);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001037 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001038 } else {
1039 SC = 0; AW = 0;
1040 }
1041}
1042
1043CachedWriter::~CachedWriter() {
1044 delete AW;
1045 delete SC;
1046}
1047
1048CachedWriter &CachedWriter::operator<<(const Value *V) {
1049 assert(AW && SC && "CachedWriter does not have a current module!");
1050 switch (V->getValueType()) {
1051 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +00001052 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001053 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001054 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1055 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +00001056 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001057 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001058 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
1059 }
1060 return *this;
1061}
Brian Gaeke960707c2003-11-11 22:41:34 +00001062
1063} // End llvm namespace