blob: 4acf55bf89c952135d64a102510ecdffc2a2b689 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This library implements the functionality defined in llvm/Assembly/Writer.h
4//
Chris Lattner02b93992002-04-12 18:21:53 +00005// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +00006// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +00007//
Chris Lattner00950542001-06-06 20:29:01 +00008//===----------------------------------------------------------------------===//
9
Chris Lattnerda1fbcc2001-11-07 04:21:57 +000010#include "llvm/Assembly/CachedWriter.h"
Chris Lattner75cf7cf2002-04-08 22:03:40 +000011#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000012#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000013#include "llvm/SlotCalculator.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000014#include "llvm/DerivedTypes.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000015#include "llvm/Instruction.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iMemory.h"
Chris Lattnere02fa852001-10-13 06:42:36 +000019#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000020#include "llvm/iPHINode.h"
21#include "llvm/iOther.h"
Chris Lattner007377f2001-09-07 16:36:04 +000022#include "llvm/SymbolTable.h"
Chris Lattner061269b2002-10-02 19:38:55 +000023#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/StringExtras.h"
25#include "Support/STLExtras.h"
Chris Lattner007377f2001-09-07 16:36:04 +000026#include <algorithm>
Chris Lattnerc1824992001-10-29 16:05:51 +000027
Chris Lattnera6275cc2002-07-26 21:12:46 +000028static RegisterPass<PrintModulePass>
29X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
30static RegisterPass<PrintFunctionPass>
31Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattnerf082b802002-07-23 18:07:49 +000032
Chris Lattner7b13f562003-05-08 02:08:14 +000033static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
34 bool PrintName,
35 std::map<const Type *, std::string> &TypeTable,
Chris Lattner7a716ad2002-04-16 21:36:08 +000036 SlotCalculator *Table);
37
Chris Lattner207b5bc2001-10-29 16:37:48 +000038static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +000039 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000040 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +000041 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000042 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +000043 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +000044 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +000045 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +000046 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000047 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +000048 return 0;
49}
50
Chris Lattnerc1824992001-10-29 16:05:51 +000051static SlotCalculator *createSlotCalculator(const Value *V) {
52 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner949a3622003-07-23 15:30:06 +000053 if (const Argument *FA = dyn_cast<Argument>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +000054 return new SlotCalculator(FA->getParent(), true);
Chris Lattner949a3622003-07-23 15:30:06 +000055 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattnerc1824992001-10-29 16:05:51 +000056 return new SlotCalculator(I->getParent()->getParent(), true);
Chris Lattner949a3622003-07-23 15:30:06 +000057 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Chris Lattnerc1824992001-10-29 16:05:51 +000058 return new SlotCalculator(BB->getParent(), true);
Chris Lattner949a3622003-07-23 15:30:06 +000059 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Chris Lattnerc1824992001-10-29 16:05:51 +000060 return new SlotCalculator(GV->getParent(), true);
Chris Lattner949a3622003-07-23 15:30:06 +000061 } else if (const Function *Func = dyn_cast<Function>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +000062 return new SlotCalculator(Func, true);
Chris Lattnerc1824992001-10-29 16:05:51 +000063 }
64 return 0;
65}
Chris Lattner00950542001-06-06 20:29:01 +000066
Chris Lattner24b8a5d2003-08-22 05:40:38 +000067// getLLVMName - Turn the specified string into an 'LLVM name', which is either
68// prefixed with % (if the string only contains simple characters) or is
69// surrounded with ""'s (if it has special chars in it).
70static std::string getLLVMName(const std::string &Name) {
71 assert(!Name.empty() && "Cannot get empty name!");
72
73 // First character cannot start with a number...
74 if (Name[0] >= '0' && Name[0] <= '9')
75 return "\"" + Name + "\"";
76
77 // Scan to see if we have any characters that are not on the "white list"
78 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
79 char C = Name[i];
80 assert(C != '"' && "Illegal character in LLVM value name!");
81 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
82 C != '-' && C != '.' && C != '_')
83 return "\"" + Name + "\"";
84 }
85
86 // If we get here, then the identifier is legal to use as a "VarID".
87 return "%"+Name;
88}
89
Chris Lattner207b5bc2001-10-29 16:37:48 +000090
91// If the module has a symbol table, take all global types and stuff their
92// names into the TypeNames map.
93//
94static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +000095 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +000096 if (!M) return;
97 const SymbolTable &ST = M->getSymbolTable();
98 SymbolTable::const_iterator PI = ST.find(Type::TypeTy);
99 if (PI != ST.end()) {
100 SymbolTable::type_const_iterator I = PI->second.begin();
101 for (; I != PI->second.end(); ++I) {
102 // As a heuristic, don't insert pointer to primitive types, because
103 // they are used too often to have a single useful name.
104 //
Chris Lattner949a3622003-07-23 15:30:06 +0000105 const Type *Ty = cast<Type>(I->second);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000106 if (!isa<PointerType>(Ty) ||
107 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType())
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000108 TypeNames.insert(std::make_pair(Ty, getLLVMName(I->first)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000109 }
110 }
111}
112
113
114
Chris Lattner7b13f562003-05-08 02:08:14 +0000115static std::string calcTypeName(const Type *Ty,
116 std::vector<const Type *> &TypeStack,
117 std::map<const Type *, std::string> &TypeNames){
Chris Lattner207b5bc2001-10-29 16:37:48 +0000118 if (Ty->isPrimitiveType()) return Ty->getDescription(); // Base case
119
120 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000121 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000122 if (I != TypeNames.end()) return I->second;
123
124 // Check to see if the Type is already on the stack...
125 unsigned Slot = 0, CurSize = TypeStack.size();
126 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
127
128 // This is another base case for the recursion. In this case, we know
129 // that we have looped back to a type that we have previously visited.
130 // Generate the appropriate upreference to handle this.
131 //
132 if (Slot < CurSize)
133 return "\\" + utostr(CurSize-Slot); // Here's the upreference
134
135 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
136
Chris Lattner7b13f562003-05-08 02:08:14 +0000137 std::string Result;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000138 switch (Ty->getPrimitiveID()) {
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000139 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000140 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000141 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000142 for (FunctionType::ParamTypes::const_iterator
Chris Lattner2761e9f2002-04-13 20:53:41 +0000143 I = FTy->getParamTypes().begin(),
144 E = FTy->getParamTypes().end(); I != E; ++I) {
145 if (I != FTy->getParamTypes().begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000146 Result += ", ";
147 Result += calcTypeName(*I, TypeStack, TypeNames);
148 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000149 if (FTy->isVarArg()) {
150 if (!FTy->getParamTypes().empty()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000151 Result += "...";
152 }
153 Result += ")";
154 break;
155 }
156 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000157 const StructType *STy = cast<StructType>(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000158 Result = "{ ";
159 for (StructType::ElementTypes::const_iterator
160 I = STy->getElementTypes().begin(),
161 E = STy->getElementTypes().end(); I != E; ++I) {
162 if (I != STy->getElementTypes().begin())
163 Result += ", ";
164 Result += calcTypeName(*I, TypeStack, TypeNames);
165 }
166 Result += " }";
167 break;
168 }
169 case Type::PointerTyID:
Chris Lattner949a3622003-07-23 15:30:06 +0000170 Result = calcTypeName(cast<PointerType>(Ty)->getElementType(),
Chris Lattner02b93992002-04-12 18:21:53 +0000171 TypeStack, TypeNames) + "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000172 break;
173 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000174 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattnerff5c2962002-04-13 21:11:04 +0000175 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000176 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
177 break;
178 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000179 case Type::OpaqueTyID:
180 Result = "opaque";
181 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000182 default:
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000183 Result = "<unrecognized-type>";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000184 }
185
186 TypeStack.pop_back(); // Remove self from stack...
187 return Result;
188}
189
190
191// printTypeInt - The internal guts of printing out a type that has a
192// potentially named portion.
193//
Chris Lattner7b13f562003-05-08 02:08:14 +0000194static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
195 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000196 // Primitive types always print out their description, regardless of whether
197 // they have been named or not.
198 //
199 if (Ty->isPrimitiveType()) return Out << Ty->getDescription();
200
201 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000202 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000203 if (I != TypeNames.end()) return Out << I->second;
204
205 // Otherwise we have a type that has not been named but is a derived type.
206 // Carefully recurse the type hierarchy to print out any contained symbolic
207 // names.
208 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000209 std::vector<const Type *> TypeStack;
210 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner697954c2002-01-20 22:54:45 +0000211 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattner207b5bc2001-10-29 16:37:48 +0000212 return Out << TypeName;
213}
214
Chris Lattnere51e03b2001-10-31 04:33:19 +0000215
Chris Lattner207b5bc2001-10-29 16:37:48 +0000216// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
217// type, iff there is an entry in the modules symbol table for the specified
218// type or one of it's component types. This is slower than a simple x << Type;
219//
Chris Lattner7b13f562003-05-08 02:08:14 +0000220std::ostream &WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
221 const Module *M) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000222 Out << " ";
223
224 // If they want us to print out a type, attempt to make it symbolic if there
225 // is a symbol table in the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000226 if (M) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000227 std::map<const Type *, std::string> TypeNames;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000228 fillTypeNameTable(M, TypeNames);
229
Chris Lattner7b8660d2001-10-29 16:40:32 +0000230 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000231 } else {
Chris Lattner7b8660d2001-10-29 16:40:32 +0000232 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000233 }
234}
235
Chris Lattner7b13f562003-05-08 02:08:14 +0000236static void WriteConstantInt(std::ostream &Out, const Constant *CV,
237 bool PrintName,
238 std::map<const Type *, std::string> &TypeTable,
Chris Lattner7a716ad2002-04-16 21:36:08 +0000239 SlotCalculator *Table) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000240 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
241 Out << (CB == ConstantBool::True ? "true" : "false");
242 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
243 Out << CI->getValue();
244 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
245 Out << CI->getValue();
246 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
247 // We would like to output the FP constant value in exponential notation,
248 // but we cannot do this if doing so will lose precision. Check here to
249 // make sure that we only output it in exponential format if we can parse
250 // the value back and get the same value.
251 //
252 std::string StrVal = ftostr(CFP->getValue());
253
254 // Check to make sure that the stringized number is not some string like
255 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
256 // the string matches the "[-+]?[0-9]" regex.
257 //
258 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
259 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaekeb471a232003-06-17 23:55:35 +0000260 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-04-18 18:53:13 +0000261 // Reparse stringized version!
262 if (atof(StrVal.c_str()) == CFP->getValue()) {
263 Out << StrVal; return;
264 }
265
266 // Otherwise we could not reparse it to exactly the same value, so we must
267 // output the string in hexadecimal format!
268 //
269 // Behave nicely in the face of C TBAA rules... see:
270 // http://www.nullstone.com/htmls/category/aliastyp.htm
271 //
272 double Val = CFP->getValue();
273 char *Ptr = (char*)&Val;
274 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
275 "assuming that double is 64 bits!");
276 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
277
278 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnere9a64ea2003-06-28 20:08:24 +0000279 if (CA->getNumOperands() > 5 && CA->isNullValue()) {
280 Out << "zeroinitializer";
281 return;
282 }
283
Chris Lattner66e810b2002-04-18 18:53:13 +0000284 // As a special case, print the array as a string if it is an array of
285 // ubytes or an array of sbytes with positive values.
286 //
287 const Type *ETy = CA->getType()->getElementType();
288 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
289
290 if (ETy == Type::SByteTy)
291 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
292 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
293 isString = false;
294 break;
295 }
296
297 if (isString) {
298 Out << "c\"";
299 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000300 unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner66e810b2002-04-18 18:53:13 +0000301
Chris Lattnerfc944462002-07-31 23:56:44 +0000302 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner66e810b2002-04-18 18:53:13 +0000303 Out << C;
304 } else {
305 Out << '\\'
306 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
307 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
308 }
309 }
310 Out << "\"";
311
312 } else { // Cannot output in string format...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000313 Out << "[";
314 if (CA->getNumOperands()) {
315 Out << " ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000316 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000317 WriteAsOperandInternal(Out, CA->getOperand(0),
318 PrintName, TypeTable, Table);
319 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
320 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000321 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000322 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
323 TypeTable, Table);
324 }
325 }
326 Out << " ]";
327 }
328 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Chris Lattnere9a64ea2003-06-28 20:08:24 +0000329 if (CS->getNumOperands() > 5 && CS->isNullValue()) {
330 Out << "zeroinitializer";
331 return;
332 }
333
Chris Lattner7a716ad2002-04-16 21:36:08 +0000334 Out << "{";
335 if (CS->getNumOperands()) {
336 Out << " ";
337 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
338
339 WriteAsOperandInternal(Out, CS->getOperand(0),
340 PrintName, TypeTable, Table);
341
342 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
343 Out << ", ";
344 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
345
346 WriteAsOperandInternal(Out, CS->getOperand(i),
347 PrintName, TypeTable, Table);
348 }
349 }
350
351 Out << " }";
352 } else if (isa<ConstantPointerNull>(CV)) {
353 Out << "null";
354
Chris Lattner7e708292002-06-25 16:13:24 +0000355 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000356 const GlobalValue *V = PR->getValue();
357 if (V->hasName()) {
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000358 Out << getLLVMName(V->getName());
Chris Lattner66e810b2002-04-18 18:53:13 +0000359 } else if (Table) {
360 int Slot = Table->getValSlot(V);
361 if (Slot >= 0)
362 Out << "%" << Slot;
363 else
364 Out << "<pointer reference badref>";
365 } else {
366 Out << "<pointer reference without context info>";
367 }
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000368
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000369 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner38d87732003-03-06 23:23:32 +0000370 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000371
372 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
373 printTypeInt(Out, (*OI)->getType(), TypeTable);
374 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
375 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000376 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000377 }
378
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000379 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner95586b82002-08-15 19:37:43 +0000380 Out << " to ";
381 printTypeInt(Out, CE->getType(), TypeTable);
382 }
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000383 Out << ")";
Chris Lattner95586b82002-08-15 19:37:43 +0000384
Chris Lattner7a716ad2002-04-16 21:36:08 +0000385 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000386 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000387 }
388}
389
390
391// WriteAsOperand - Write the name of the specified value out to the specified
392// ostream. This can be useful when you just want to print int %reg126, not the
393// whole instruction that generated it.
394//
Chris Lattner7b13f562003-05-08 02:08:14 +0000395static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
396 bool PrintName,
397 std::map<const Type*, std::string> &TypeTable,
Chris Lattner7a716ad2002-04-16 21:36:08 +0000398 SlotCalculator *Table) {
399 Out << " ";
400 if (PrintName && V->hasName()) {
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000401 Out << getLLVMName(V->getName());
Chris Lattner7a716ad2002-04-16 21:36:08 +0000402 } else {
Chris Lattner949a3622003-07-23 15:30:06 +0000403 if (const Constant *CV = dyn_cast<Constant>(V)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000404 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
405 } else {
406 int Slot;
407 if (Table) {
408 Slot = Table->getValSlot(V);
409 } else {
Chris Lattner949a3622003-07-23 15:30:06 +0000410 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000411 Out << Ty->getDescription();
412 return;
413 }
414
415 Table = createSlotCalculator(V);
416 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
417
418 Slot = Table->getValSlot(V);
419 delete Table;
420 }
421 if (Slot >= 0) Out << "%" << Slot;
422 else if (PrintName)
423 Out << "<badref>"; // Not embeded into a location?
424 }
425 }
426}
427
428
Chris Lattner207b5bc2001-10-29 16:37:48 +0000429
430// WriteAsOperand - Write the name of the specified value out to the specified
431// ostream. This can be useful when you just want to print int %reg126, not the
432// whole instruction that generated it.
433//
Chris Lattner7b13f562003-05-08 02:08:14 +0000434std::ostream &WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
435 bool PrintName, const Module *Context) {
436 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000437 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000438
Chris Lattner6e6026b2002-11-20 18:36:02 +0000439 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000440 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000441
442 if (PrintType)
443 printTypeInt(Out, V->getType(), TypeNames);
444
Chris Lattner607dc682002-07-10 16:48:17 +0000445 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000446 return Out;
447}
448
449
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000450
Chris Lattner007377f2001-09-07 16:36:04 +0000451class AssemblyWriter {
Chris Lattner7b13f562003-05-08 02:08:14 +0000452 std::ostream &Out;
Chris Lattner00950542001-06-06 20:29:01 +0000453 SlotCalculator &Table;
Chris Lattnerc1824992001-10-29 16:05:51 +0000454 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000455 std::map<const Type *, std::string> TypeNames;
Chris Lattner00950542001-06-06 20:29:01 +0000456public:
Chris Lattner7b13f562003-05-08 02:08:14 +0000457 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M)
Chris Lattnerc1824992001-10-29 16:05:51 +0000458 : Out(o), Table(Tab), TheModule(M) {
459
460 // If the module has a symbol table, take all global types and stuff their
461 // names into the TypeNames map.
462 //
Chris Lattner207b5bc2001-10-29 16:37:48 +0000463 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000464 }
465
Chris Lattnerc1824992001-10-29 16:05:51 +0000466 inline void write(const Module *M) { printModule(M); }
467 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000468 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000469 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000470 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000471 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000472 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000473
Chris Lattner66e810b2002-04-18 18:53:13 +0000474 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
475
Chris Lattner00950542001-06-06 20:29:01 +0000476private :
Chris Lattnerc1824992001-10-29 16:05:51 +0000477 void printModule(const Module *M);
478 void printSymbolTable(const SymbolTable &ST);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000479 void printConstant(const Constant *CPV);
Chris Lattnerc1824992001-10-29 16:05:51 +0000480 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000481 void printFunction(const Function *F);
Chris Lattner73e21422002-04-09 19:48:49 +0000482 void printArgument(const Argument *FA);
Chris Lattnerc1824992001-10-29 16:05:51 +0000483 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000484 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000485
486 // printType - Go to extreme measures to attempt to print out a short,
487 // symbolic version of a type name.
488 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000489 std::ostream &printType(const Type *Ty) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000490 return printTypeInt(Out, Ty, TypeNames);
491 }
492
493 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
494 // without considering any symbolic types that we may have equal to it.
495 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000496 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000497
Chris Lattnere02fa852001-10-13 06:42:36 +0000498 // printInfoComment - Print a little comment after the instruction indicating
499 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000500 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000501};
502
503
Chris Lattner2761e9f2002-04-13 20:53:41 +0000504// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
505// without considering any symbolic types that we may have equal to it.
506//
Chris Lattner7b13f562003-05-08 02:08:14 +0000507std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner7e708292002-06-25 16:13:24 +0000508 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000509 printType(FTy->getReturnType()) << " (";
510 for (FunctionType::ParamTypes::const_iterator
511 I = FTy->getParamTypes().begin(),
512 E = FTy->getParamTypes().end(); I != E; ++I) {
513 if (I != FTy->getParamTypes().begin())
514 Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000515 printType(*I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000516 }
517 if (FTy->isVarArg()) {
518 if (!FTy->getParamTypes().empty()) Out << ", ";
519 Out << "...";
520 }
521 Out << ")";
Chris Lattner7e708292002-06-25 16:13:24 +0000522 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000523 Out << "{ ";
524 for (StructType::ElementTypes::const_iterator
525 I = STy->getElementTypes().begin(),
526 E = STy->getElementTypes().end(); I != E; ++I) {
527 if (I != STy->getElementTypes().begin())
528 Out << ", ";
529 printType(*I);
530 }
531 Out << " }";
Chris Lattner7e708292002-06-25 16:13:24 +0000532 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000533 printType(PTy->getElementType()) << "*";
Chris Lattner7e708292002-06-25 16:13:24 +0000534 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000535 Out << "[" << ATy->getNumElements() << " x ";
536 printType(ATy->getElementType()) << "]";
Chris Lattner7e708292002-06-25 16:13:24 +0000537 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner26c69152003-06-01 03:45:51 +0000538 Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000539 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000540 if (!Ty->isPrimitiveType())
541 Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000542 printType(Ty);
543 }
544 return Out;
545}
546
547
Chris Lattner007377f2001-09-07 16:36:04 +0000548void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
549 bool PrintName) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000550 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattner7a716ad2002-04-16 21:36:08 +0000551 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000552}
553
Chris Lattner00950542001-06-06 20:29:01 +0000554
Chris Lattnerc1824992001-10-29 16:05:51 +0000555void AssemblyWriter::printModule(const Module *M) {
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000556 switch (M->getEndianness()) {
557 case Module::LittleEndian: Out << "target endian = little\n"; break;
558 case Module::BigEndian: Out << "target endian = big\n"; break;
559 case Module::AnyEndianness: break;
560 }
561 switch (M->getPointerSize()) {
562 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
563 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
564 case Module::AnyPointerSize: break;
565 }
566
Chris Lattner007377f2001-09-07 16:36:04 +0000567 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000568 printSymbolTable(M->getSymbolTable());
Chris Lattner70cc3392001-09-10 07:58:01 +0000569
Chris Lattner7e708292002-06-25 16:13:24 +0000570 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
571 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000572
Chris Lattner03e2acb2002-05-06 03:00:40 +0000573 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve5efa3cc2001-09-18 12:48:16 +0000574
Chris Lattnerb5794002002-04-07 22:49:37 +0000575 // Output all of the functions...
Chris Lattner7e708292002-06-25 16:13:24 +0000576 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
577 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000578}
579
Chris Lattnerc1824992001-10-29 16:05:51 +0000580void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000581 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000582
Chris Lattner4ad02e72003-04-16 20:28:45 +0000583 if (!GV->hasInitializer())
584 Out << "external ";
585 else
586 switch (GV->getLinkage()) {
587 case GlobalValue::InternalLinkage: Out << "internal "; break;
588 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
589 case GlobalValue::AppendingLinkage: Out << "appending "; break;
590 case GlobalValue::ExternalLinkage: break;
591 }
Chris Lattnerd70684f2001-09-18 04:01:05 +0000592
Chris Lattnerc1824992001-10-29 16:05:51 +0000593 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000594 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000595
596 if (GV->hasInitializer())
597 writeOperand(GV->getInitializer(), false, false);
598
Chris Lattner7e708292002-06-25 16:13:24 +0000599 printInfoComment(*GV);
Chris Lattner697954c2002-01-20 22:54:45 +0000600 Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000601}
602
Chris Lattner007377f2001-09-07 16:36:04 +0000603
Chris Lattnerc1824992001-10-29 16:05:51 +0000604// printSymbolTable - Run through symbol table looking for named constants
Chris Lattner007377f2001-09-07 16:36:04 +0000605// if a named constant is found, emit it's declaration...
606//
Chris Lattnerc1824992001-10-29 16:05:51 +0000607void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner007377f2001-09-07 16:36:04 +0000608 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
609 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
610 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
611
612 for (; I != End; ++I) {
613 const Value *V = I->second;
Chris Lattner949a3622003-07-23 15:30:06 +0000614 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000615 printConstant(CPV);
Chris Lattner949a3622003-07-23 15:30:06 +0000616 } else if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000617 Out << "\t" << getLLVMName(I->first) << " = type ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000618
619 // Make sure we print out at least one level of the type structure, so
620 // that we do not get %FILE = type %FILE
621 //
622 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000623 }
624 }
Chris Lattner739a56d2001-07-15 06:35:59 +0000625 }
Chris Lattner00950542001-06-06 20:29:01 +0000626}
627
628
Chris Lattnerc1824992001-10-29 16:05:51 +0000629// printConstant - Print out a constant pool entry...
Chris Lattner00950542001-06-06 20:29:01 +0000630//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000631void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattner007377f2001-09-07 16:36:04 +0000632 // Don't print out unnamed constants, they will be inlined
633 if (!CPV->hasName()) return;
Chris Lattner00950542001-06-06 20:29:01 +0000634
Chris Lattner1333ed52001-07-26 16:29:38 +0000635 // Print out name...
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000636 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner00950542001-06-06 20:29:01 +0000637
638 // Write the value out now...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000639 writeOperand(CPV, true, false);
Chris Lattner00950542001-06-06 20:29:01 +0000640
Chris Lattner7e708292002-06-25 16:13:24 +0000641 printInfoComment(*CPV);
Chris Lattner697954c2002-01-20 22:54:45 +0000642 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000643}
644
Chris Lattnerb5794002002-04-07 22:49:37 +0000645// printFunction - Print all aspects of a function.
Chris Lattner00950542001-06-06 20:29:01 +0000646//
Chris Lattner7e708292002-06-25 16:13:24 +0000647void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000648 // Print out the return type and name...
Chris Lattner4ad02e72003-04-16 20:28:45 +0000649 Out << "\n";
650
651 if (F->isExternal())
652 Out << "declare ";
653 else
654 switch (F->getLinkage()) {
655 case GlobalValue::InternalLinkage: Out << "internal "; break;
656 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
657 case GlobalValue::AppendingLinkage: Out << "appending "; break;
658 case GlobalValue::ExternalLinkage: break;
659 }
660
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000661 printType(F->getReturnType()) << " " << getLLVMName(F->getName()) << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000662 Table.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000663
Chris Lattnerc1824992001-10-29 16:05:51 +0000664 // Loop over the arguments, printing them...
Chris Lattner7e708292002-06-25 16:13:24 +0000665 const FunctionType *FT = F->getFunctionType();
Chris Lattner007377f2001-09-07 16:36:04 +0000666
Chris Lattner69da5cf2002-10-13 20:57:00 +0000667 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
668 printArgument(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000669
670 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000671 if (FT->isVarArg()) {
672 if (FT->getParamTypes().size()) Out << ", ";
Chris Lattner007377f2001-09-07 16:36:04 +0000673 Out << "..."; // Output varargs portion of signature!
674 }
Chris Lattner03e2acb2002-05-06 03:00:40 +0000675 Out << ")";
Chris Lattner007377f2001-09-07 16:36:04 +0000676
Chris Lattner7e708292002-06-25 16:13:24 +0000677 if (F->isExternal()) {
Chris Lattner03e2acb2002-05-06 03:00:40 +0000678 Out << "\n";
679 } else {
680 Out << " {";
Chris Lattner007377f2001-09-07 16:36:04 +0000681
Chris Lattnerb5794002002-04-07 22:49:37 +0000682 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +0000683 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
684 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000685
Chris Lattner03e2acb2002-05-06 03:00:40 +0000686 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000687 }
688
Chris Lattnerb5794002002-04-07 22:49:37 +0000689 Table.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000690}
691
Chris Lattner73e21422002-04-09 19:48:49 +0000692// printArgument - This member is called for every argument that
Chris Lattnerb5794002002-04-07 22:49:37 +0000693// is passed into the function. Simply print it out
Chris Lattner00950542001-06-06 20:29:01 +0000694//
Chris Lattner73e21422002-04-09 19:48:49 +0000695void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner00950542001-06-06 20:29:01 +0000696 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner7e708292002-06-25 16:13:24 +0000697 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner00950542001-06-06 20:29:01 +0000698
699 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +0000700 printType(Arg->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000701
702 // Output name, if available...
703 if (Arg->hasName())
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000704 Out << " " << getLLVMName(Arg->getName());
Chris Lattner00950542001-06-06 20:29:01 +0000705 else if (Table.getValSlot(Arg) < 0)
706 Out << "<badref>";
Chris Lattner00950542001-06-06 20:29:01 +0000707}
708
Chris Lattnerc1824992001-10-29 16:05:51 +0000709// printBasicBlock - This member is called for each basic block in a methd.
Chris Lattner00950542001-06-06 20:29:01 +0000710//
Chris Lattnerc1824992001-10-29 16:05:51 +0000711void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000712 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner061269b2002-10-02 19:38:55 +0000713 Out << "\n" << BB->getName() << ":";
Chris Lattnerafc38682002-05-14 16:02:05 +0000714 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner00950542001-06-06 20:29:01 +0000715 int Slot = Table.getValSlot(BB);
Chris Lattnerb9a45782001-06-07 16:58:55 +0000716 Out << "\n; <label>:";
Chris Lattner00950542001-06-06 20:29:01 +0000717 if (Slot >= 0)
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000718 Out << Slot; // Extra newline separates out label's
Chris Lattner00950542001-06-06 20:29:01 +0000719 else
Chris Lattnerb9a45782001-06-07 16:58:55 +0000720 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +0000721 }
722
723 // Output predecessors for the block...
724 Out << "\t\t;";
725 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
726
727 if (PI == PE) {
728 Out << " No predecessors!";
729 } else {
730 Out << " preds =";
731 writeOperand(*PI, false, true);
732 for (++PI; PI != PE; ++PI) {
733 Out << ",";
734 writeOperand(*PI, false, true);
735 }
Chris Lattner00950542001-06-06 20:29:01 +0000736 }
Chris Lattnerafc38682002-05-14 16:02:05 +0000737
738 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000739
Chris Lattner007377f2001-09-07 16:36:04 +0000740 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +0000741 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
742 printInstruction(*I);
Chris Lattner00950542001-06-06 20:29:01 +0000743}
744
Chris Lattnere02fa852001-10-13 06:42:36 +0000745
746// printInfoComment - Print a little comment after the instruction indicating
747// which slot it occupies.
748//
Chris Lattner7e708292002-06-25 16:13:24 +0000749void AssemblyWriter::printInfoComment(const Value &V) {
750 if (V.getType() != Type::VoidTy) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000751 Out << "\t\t; <";
Chris Lattner7e708292002-06-25 16:13:24 +0000752 printType(V.getType()) << ">";
Chris Lattnere02fa852001-10-13 06:42:36 +0000753
Chris Lattner7e708292002-06-25 16:13:24 +0000754 if (!V.hasName()) {
755 int Slot = Table.getValSlot(&V); // Print out the def slot taken...
Chris Lattnere02fa852001-10-13 06:42:36 +0000756 if (Slot >= 0) Out << ":" << Slot;
757 else Out << ":<badref>";
758 }
Chris Lattner7e708292002-06-25 16:13:24 +0000759 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +0000760 }
761}
762
Chris Lattnerc1824992001-10-29 16:05:51 +0000763// printInstruction - This member is called for each Instruction in a methd.
Chris Lattner00950542001-06-06 20:29:01 +0000764//
Chris Lattner7e708292002-06-25 16:13:24 +0000765void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner00950542001-06-06 20:29:01 +0000766 Out << "\t";
767
768 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +0000769 if (I.hasName())
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000770 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +0000771
772 // Print out the opcode...
Chris Lattner7e708292002-06-25 16:13:24 +0000773 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +0000774
775 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +0000776 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000777
778 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +0000779 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
780 writeOperand(I.getOperand(2), true);
Chris Lattner00950542001-06-06 20:29:01 +0000781 Out << ",";
782 writeOperand(Operand, true);
783 Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000784 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000785
Chris Lattner94dc1f22002-04-13 18:34:38 +0000786 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000787 // Special case switch statement to get formatting nice and correct...
Chris Lattner7e708292002-06-25 16:13:24 +0000788 writeOperand(Operand , true); Out << ",";
789 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +0000790
Chris Lattner7e708292002-06-25 16:13:24 +0000791 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner00950542001-06-06 20:29:01 +0000792 Out << "\n\t\t";
Chris Lattner7e708292002-06-25 16:13:24 +0000793 writeOperand(I.getOperand(op ), true); Out << ",";
794 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000795 }
796 Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +0000797 } else if (isa<PHINode>(I)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000798 Out << " ";
Chris Lattner7e708292002-06-25 16:13:24 +0000799 printType(I.getType());
Chris Lattnereed1fc72001-11-06 08:33:46 +0000800 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +0000801
Chris Lattner7e708292002-06-25 16:13:24 +0000802 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner8f410ca2001-11-06 01:48:45 +0000803 if (op) Out << ", ";
804 Out << "[";
Chris Lattner7e708292002-06-25 16:13:24 +0000805 writeOperand(I.getOperand(op ), false); Out << ",";
806 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +0000807 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000808 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner00950542001-06-06 20:29:01 +0000809 Out << " void";
Chris Lattnere02fa852001-10-13 06:42:36 +0000810 } else if (isa<CallInst>(I)) {
Chris Lattner7a012292003-08-05 15:34:45 +0000811 const PointerType *PTy = cast<PointerType>(Operand->getType());
812 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
813 const Type *RetTy = FTy->getReturnType();
Chris Lattner268de042001-11-06 21:28:12 +0000814
Chris Lattner7a012292003-08-05 15:34:45 +0000815 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +0000816 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +0000817 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +0000818 //
Chris Lattner7a012292003-08-05 15:34:45 +0000819 if (!FTy->isVarArg() &&
Chris Lattner94dc1f22002-04-13 18:34:38 +0000820 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +0000821 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner268de042001-11-06 21:28:12 +0000822 Out << " "; printType(RetTy);
823 writeOperand(Operand, false);
824 } else {
825 writeOperand(Operand, true);
826 }
Chris Lattner00950542001-06-06 20:29:01 +0000827 Out << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000828 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
829 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner00950542001-06-06 20:29:01 +0000830 Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000831 writeOperand(I.getOperand(op), true);
Chris Lattner00950542001-06-06 20:29:01 +0000832 }
833
834 Out << " )";
Chris Lattner7e708292002-06-25 16:13:24 +0000835 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner7a012292003-08-05 15:34:45 +0000836 const PointerType *PTy = cast<PointerType>(Operand->getType());
837 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
838 const Type *RetTy = FTy->getReturnType();
839
840 // If possible, print out the short form of the invoke instruction. We can
841 // only do this if the first argument is a pointer to a nonvararg function,
842 // and if the return type is not a pointer to a function.
843 //
844 if (!FTy->isVarArg() &&
845 (!isa<PointerType>(RetTy) ||
846 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
847 Out << " "; printType(RetTy);
848 writeOperand(Operand, false);
849 } else {
850 writeOperand(Operand, true);
851 }
852
Chris Lattnere02fa852001-10-13 06:42:36 +0000853 Out << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000854 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
855 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattnere02fa852001-10-13 06:42:36 +0000856 Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000857 writeOperand(I.getOperand(op), true);
Chris Lattnere02fa852001-10-13 06:42:36 +0000858 }
859
860 Out << " )\n\t\t\tto";
861 writeOperand(II->getNormalDest(), true);
862 Out << " except";
863 writeOperand(II->getExceptionalDest(), true);
864
Chris Lattner7e708292002-06-25 16:13:24 +0000865 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000866 Out << " ";
Chris Lattner94dc1f22002-04-13 18:34:38 +0000867 printType(AI->getType()->getElementType());
868 if (AI->isArrayAllocation()) {
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000869 Out << ",";
Chris Lattner94dc1f22002-04-13 18:34:38 +0000870 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +0000871 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000872 } else if (isa<CastInst>(I)) {
Chris Lattner8f77dae2003-05-08 02:44:12 +0000873 writeOperand(Operand, true);
Chris Lattnerc1824992001-10-29 16:05:51 +0000874 Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +0000875 printType(I.getType());
Chris Lattner8f77dae2003-05-08 02:44:12 +0000876 } else if (isa<VarArgInst>(I)) {
877 writeOperand(Operand, true);
878 Out << ", ";
879 printType(I.getType());
Chris Lattner00950542001-06-06 20:29:01 +0000880 } else if (Operand) { // Print the normal way...
881
882 // PrintAllTypes - Instructions who have operands of all the same type
883 // omit the type from all but the first operand. If the instruction has
884 // different type operands (for example br), then they are all printed.
885 bool PrintAllTypes = false;
886 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +0000887
Chris Lattnerffd9bf42003-04-16 20:20:02 +0000888 // Shift Left & Right print both types even for Ubyte LHS
889 if (isa<ShiftInst>(I)) {
890 PrintAllTypes = true;
891 } else {
892 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
893 Operand = I.getOperand(i);
894 if (Operand->getType() != TheType) {
895 PrintAllTypes = true; // We have differing types! Print them all!
896 break;
897 }
Chris Lattner00950542001-06-06 20:29:01 +0000898 }
899 }
Chris Lattnerffd9bf42003-04-16 20:20:02 +0000900
Chris Lattnerc1824992001-10-29 16:05:51 +0000901 if (!PrintAllTypes) {
902 Out << " ";
Chris Lattnerffd9bf42003-04-16 20:20:02 +0000903 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +0000904 }
Chris Lattner00950542001-06-06 20:29:01 +0000905
Chris Lattner7e708292002-06-25 16:13:24 +0000906 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000907 if (i) Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000908 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000909 }
910 }
911
Chris Lattnere02fa852001-10-13 06:42:36 +0000912 printInfoComment(I);
Chris Lattner697954c2002-01-20 22:54:45 +0000913 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000914}
915
916
917//===----------------------------------------------------------------------===//
918// External Interface declarations
919//===----------------------------------------------------------------------===//
920
921
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000922void Module::print(std::ostream &o) const {
923 SlotCalculator SlotTable(this, true);
924 AssemblyWriter W(o, SlotTable, this);
925 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000926}
927
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000928void GlobalVariable::print(std::ostream &o) const {
929 SlotCalculator SlotTable(getParent(), true);
930 AssemblyWriter W(o, SlotTable, getParent());
931 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +0000932}
933
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000934void Function::print(std::ostream &o) const {
935 SlotCalculator SlotTable(getParent(), true);
936 AssemblyWriter W(o, SlotTable, getParent());
Chris Lattner00950542001-06-06 20:29:01 +0000937
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000938 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000939}
940
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000941void BasicBlock::print(std::ostream &o) const {
942 SlotCalculator SlotTable(getParent(), true);
Chris Lattnerc1824992001-10-29 16:05:51 +0000943 AssemblyWriter W(o, SlotTable,
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000944 getParent() ? getParent()->getParent() : 0);
945 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000946}
947
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000948void Instruction::print(std::ostream &o) const {
949 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000950 SlotCalculator SlotTable(F, true);
951 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0);
Chris Lattner00950542001-06-06 20:29:01 +0000952
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000953 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000954}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000955
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000956void Constant::print(std::ostream &o) const {
957 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +0000958
959 // Handle CPR's special, because they have context information...
960 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
961 CPR->getValue()->print(o); // Print as a global value, with context info.
962 return;
963 }
964
Chris Lattner66e810b2002-04-18 18:53:13 +0000965 o << " " << getType()->getDescription() << " ";
966
Chris Lattner7b13f562003-05-08 02:08:14 +0000967 std::map<const Type *, std::string> TypeTable;
Chris Lattner66e810b2002-04-18 18:53:13 +0000968 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000969}
970
971void Type::print(std::ostream &o) const {
972 if (this == 0)
973 o << "<null Type>";
974 else
975 o << getDescription();
976}
977
Chris Lattner73e21422002-04-09 19:48:49 +0000978void Argument::print(std::ostream &o) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000979 o << getType() << " " << getName();
980}
981
982void Value::dump() const { print(std::cerr); }
983
984//===----------------------------------------------------------------------===//
985// CachedWriter Class Implementation
986//===----------------------------------------------------------------------===//
987
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000988void CachedWriter::setModule(const Module *M) {
989 delete SC; delete AW;
990 if (M) {
991 SC = new SlotCalculator(M, true);
992 AW = new AssemblyWriter(Out, *SC, M);
993 } else {
994 SC = 0; AW = 0;
995 }
996}
997
998CachedWriter::~CachedWriter() {
999 delete AW;
1000 delete SC;
1001}
1002
1003CachedWriter &CachedWriter::operator<<(const Value *V) {
1004 assert(AW && SC && "CachedWriter does not have a current module!");
1005 switch (V->getValueType()) {
1006 case Value::ConstantVal:
Chris Lattner66e810b2002-04-18 18:53:13 +00001007 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner949a3622003-07-23 15:30:06 +00001008 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001009 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1010 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner79df7c02002-03-26 18:01:55 +00001011 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001012 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001013 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
1014 }
1015 return *this;
1016}