blob: b90a5b2857796b8936bf6334924b9ba3de074ed6 [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 Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.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/iMemory.h"
Chris Lattner862e3382001-10-13 06:42:36 +000025#include "llvm/iTerminators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
27#include "llvm/iOther.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000028#include "llvm/Module.h"
29#include "llvm/Analysis/SlotCalculator.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 Lattner189d19f2003-11-21 20:23:48 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chris Lattnerc8b70922002-07-26 21:12:46 +000037static RegisterPass<PrintModulePass>
38X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
39static RegisterPass<PrintFunctionPass>
40Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattner7f8845a2002-07-23 18:07:49 +000041
Chris Lattnerab7d1ab2003-05-08 02:08:14 +000042static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
43 bool PrintName,
44 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +000045 SlotCalculator *Table);
46
Chris Lattnerb86620e2001-10-29 16:37:48 +000047static const Module *getModuleFromVal(const Value *V) {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000048 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000049 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000050 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000051 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000052 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner57698e22002-03-26 18:01:55 +000053 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattnerb86620e2001-10-29 16:37:48 +000054 return M ? M->getParent() : 0;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000055 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattnerb86620e2001-10-29 16:37:48 +000056 return GV->getParent();
Chris Lattnerb86620e2001-10-29 16:37:48 +000057 return 0;
58}
59
Chris Lattner7bfee412001-10-29 16:05:51 +000060static SlotCalculator *createSlotCalculator(const Value *V) {
61 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000062 if (const Argument *FA = dyn_cast<Argument>(V)) {
Chris Lattner6089bb12004-01-14 02:49:34 +000063 return new SlotCalculator(FA->getParent(), false);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000064 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner6089bb12004-01-14 02:49:34 +000065 return new SlotCalculator(I->getParent()->getParent(), false);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000066 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Chris Lattner6089bb12004-01-14 02:49:34 +000067 return new SlotCalculator(BB->getParent(), false);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000068 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Chris Lattner6089bb12004-01-14 02:49:34 +000069 return new SlotCalculator(GV->getParent(), false);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +000070 } else if (const Function *Func = dyn_cast<Function>(V)) {
Chris Lattner6089bb12004-01-14 02:49:34 +000071 return new SlotCalculator(Func, false);
Chris Lattner7bfee412001-10-29 16:05:51 +000072 }
73 return 0;
74}
Chris Lattner2f7c9632001-06-06 20:29:01 +000075
Chris Lattner1c343042003-08-22 05:40:38 +000076// getLLVMName - Turn the specified string into an 'LLVM name', which is either
77// prefixed with % (if the string only contains simple characters) or is
78// surrounded with ""'s (if it has special chars in it).
79static std::string getLLVMName(const std::string &Name) {
80 assert(!Name.empty() && "Cannot get empty name!");
81
82 // First character cannot start with a number...
83 if (Name[0] >= '0' && Name[0] <= '9')
84 return "\"" + Name + "\"";
85
86 // Scan to see if we have any characters that are not on the "white list"
87 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
88 char C = Name[i];
89 assert(C != '"' && "Illegal character in LLVM value name!");
90 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
91 C != '-' && C != '.' && C != '_')
92 return "\"" + Name + "\"";
93 }
94
95 // If we get here, then the identifier is legal to use as a "VarID".
96 return "%"+Name;
97}
98
Chris Lattnerb86620e2001-10-29 16:37:48 +000099
Misha Brukmanc566ca362004-03-02 00:22:19 +0000100/// fillTypeNameTable - If the module has a symbol table, take all global types
101/// and stuff their names into the TypeNames map.
102///
Chris Lattnerb86620e2001-10-29 16:37:48 +0000103static void fillTypeNameTable(const Module *M,
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000104 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000105 if (!M) return;
106 const SymbolTable &ST = M->getSymbolTable();
107 SymbolTable::const_iterator PI = ST.find(Type::TypeTy);
108 if (PI != ST.end()) {
109 SymbolTable::type_const_iterator I = PI->second.begin();
110 for (; I != PI->second.end(); ++I) {
111 // As a heuristic, don't insert pointer to primitive types, because
112 // they are used too often to have a single useful name.
113 //
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000114 const Type *Ty = cast<Type>(I->second);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000115 if (!isa<PointerType>(Ty) ||
Chris Lattnerf14ead92003-10-30 00:22:33 +0000116 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
117 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner1c343042003-08-22 05:40:38 +0000118 TypeNames.insert(std::make_pair(Ty, getLLVMName(I->first)));
Chris Lattnerb86620e2001-10-29 16:37:48 +0000119 }
120 }
121}
122
123
124
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000125static std::string calcTypeName(const Type *Ty,
126 std::vector<const Type *> &TypeStack,
127 std::map<const Type *, std::string> &TypeNames){
Chris Lattnerf14ead92003-10-30 00:22:33 +0000128 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
129 return Ty->getDescription(); // Base case
Chris Lattnerb86620e2001-10-29 16:37:48 +0000130
131 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000132 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000133 if (I != TypeNames.end()) return I->second;
134
Chris Lattnerf14ead92003-10-30 00:22:33 +0000135 if (isa<OpaqueType>(Ty))
136 return "opaque";
137
Chris Lattnerb86620e2001-10-29 16:37:48 +0000138 // Check to see if the Type is already on the stack...
139 unsigned Slot = 0, CurSize = TypeStack.size();
140 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
141
142 // This is another base case for the recursion. In this case, we know
143 // that we have looped back to a type that we have previously visited.
144 // Generate the appropriate upreference to handle this.
Chris Lattnerb86620e2001-10-29 16:37:48 +0000145 if (Slot < CurSize)
146 return "\\" + utostr(CurSize-Slot); // Here's the upreference
147
148 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
149
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000150 std::string Result;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000151 switch (Ty->getPrimitiveID()) {
Chris Lattner91db5822002-03-29 03:44:36 +0000152 case Type::FunctionTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000153 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattnerd816b532002-04-13 20:53:41 +0000154 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000155 for (FunctionType::param_iterator I = FTy->param_begin(),
156 E = FTy->param_end(); I != E; ++I) {
157 if (I != FTy->param_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000158 Result += ", ";
159 Result += calcTypeName(*I, TypeStack, TypeNames);
160 }
Chris Lattnerd816b532002-04-13 20:53:41 +0000161 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000162 if (FTy->getNumParams()) Result += ", ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000163 Result += "...";
164 }
165 Result += ")";
166 break;
167 }
168 case Type::StructTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000169 const StructType *STy = cast<StructType>(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000170 Result = "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000171 for (StructType::element_iterator I = STy->element_begin(),
172 E = STy->element_end(); I != E; ++I) {
173 if (I != STy->element_begin())
Chris Lattnerb86620e2001-10-29 16:37:48 +0000174 Result += ", ";
175 Result += calcTypeName(*I, TypeStack, TypeNames);
176 }
177 Result += " }";
178 break;
179 }
180 case Type::PointerTyID:
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000181 Result = calcTypeName(cast<PointerType>(Ty)->getElementType(),
Chris Lattner189088e2002-04-12 18:21:53 +0000182 TypeStack, TypeNames) + "*";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000183 break;
184 case Type::ArrayTyID: {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000185 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner8a939c92002-04-13 21:11:04 +0000186 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000187 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
188 break;
189 }
Chris Lattner15285ab2003-05-14 17:50:47 +0000190 case Type::OpaqueTyID:
191 Result = "opaque";
192 break;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000193 default:
Vikram S. Adveb952b542002-07-14 23:14:45 +0000194 Result = "<unrecognized-type>";
Chris Lattnerb86620e2001-10-29 16:37:48 +0000195 }
196
197 TypeStack.pop_back(); // Remove self from stack...
198 return Result;
199}
200
201
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000202/// printTypeInt - The internal guts of printing out a type that has a
203/// potentially named portion.
204///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000205static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
206 std::map<const Type *, std::string> &TypeNames) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000207 // Primitive types always print out their description, regardless of whether
208 // they have been named or not.
209 //
Chris Lattner92d60532003-10-30 00:12:51 +0000210 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
211 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000212
213 // Check to see if the type is named.
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000214 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000215 if (I != TypeNames.end()) return Out << I->second;
216
217 // Otherwise we have a type that has not been named but is a derived type.
218 // Carefully recurse the type hierarchy to print out any contained symbolic
219 // names.
220 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000221 std::vector<const Type *> TypeStack;
222 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner7f74a562002-01-20 22:54:45 +0000223 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattnerb86620e2001-10-29 16:37:48 +0000224 return Out << TypeName;
225}
226
Chris Lattner34b95182001-10-31 04:33:19 +0000227
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000228/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
229/// type, iff there is an entry in the modules symbol table for the specified
230/// type or one of it's component types. This is slower than a simple x << Type
231///
Chris Lattner189d19f2003-11-21 20:23:48 +0000232std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
233 const Module *M) {
Chris Lattnerb86620e2001-10-29 16:37:48 +0000234 Out << " ";
235
236 // If they want us to print out a type, attempt to make it symbolic if there
237 // is a symbol table in the module...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000238 if (M) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000239 std::map<const Type *, std::string> TypeNames;
Chris Lattnerb86620e2001-10-29 16:37:48 +0000240 fillTypeNameTable(M, TypeNames);
241
Chris Lattner72f866e2001-10-29 16:40:32 +0000242 return printTypeInt(Out, Ty, TypeNames);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000243 } else {
Chris Lattner72f866e2001-10-29 16:40:32 +0000244 return Out << Ty->getDescription();
Chris Lattnerb86620e2001-10-29 16:37:48 +0000245 }
246}
247
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000248static void WriteConstantInt(std::ostream &Out, const Constant *CV,
249 bool PrintName,
250 std::map<const Type *, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000251 SlotCalculator *Table) {
Chris Lattner1e194682002-04-18 18:53:13 +0000252 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
253 Out << (CB == ConstantBool::True ? "true" : "false");
254 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
255 Out << CI->getValue();
256 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
257 Out << CI->getValue();
258 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
259 // We would like to output the FP constant value in exponential notation,
260 // but we cannot do this if doing so will lose precision. Check here to
261 // make sure that we only output it in exponential format if we can parse
262 // the value back and get the same value.
263 //
264 std::string StrVal = ftostr(CFP->getValue());
265
266 // Check to make sure that the stringized number is not some string like
267 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
268 // the string matches the "[-+]?[0-9]" regex.
269 //
270 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
271 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaeke87b4f072003-06-17 23:55:35 +0000272 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner1e194682002-04-18 18:53:13 +0000273 // Reparse stringized version!
274 if (atof(StrVal.c_str()) == CFP->getValue()) {
275 Out << StrVal; return;
276 }
277
278 // Otherwise we could not reparse it to exactly the same value, so we must
279 // output the string in hexadecimal format!
280 //
281 // Behave nicely in the face of C TBAA rules... see:
282 // http://www.nullstone.com/htmls/category/aliastyp.htm
283 //
284 double Val = CFP->getValue();
285 char *Ptr = (char*)&Val;
286 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
287 "assuming that double is 64 bits!");
288 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
289
Chris Lattner76b2ff42004-02-15 05:55:15 +0000290 } else if (isa<ConstantAggregateZero>(CV)) {
291 Out << "zeroinitializer";
Chris Lattner1e194682002-04-18 18:53:13 +0000292 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
293 // As a special case, print the array as a string if it is an array of
294 // ubytes or an array of sbytes with positive values.
295 //
296 const Type *ETy = CA->getType()->getElementType();
297 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
298
299 if (ETy == Type::SByteTy)
300 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
301 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
302 isString = false;
303 break;
304 }
305
306 if (isString) {
307 Out << "c\"";
308 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattner6077c312003-07-23 15:22:26 +0000309 unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner1e194682002-04-18 18:53:13 +0000310
Chris Lattnere5fd3862002-07-31 23:56:44 +0000311 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner1e194682002-04-18 18:53:13 +0000312 Out << C;
313 } else {
314 Out << '\\'
315 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
316 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
317 }
318 }
319 Out << "\"";
320
321 } else { // Cannot output in string format...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000322 Out << "[";
323 if (CA->getNumOperands()) {
324 Out << " ";
Chris Lattner1e194682002-04-18 18:53:13 +0000325 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000326 WriteAsOperandInternal(Out, CA->getOperand(0),
327 PrintName, TypeTable, Table);
328 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
329 Out << ", ";
Chris Lattner1e194682002-04-18 18:53:13 +0000330 printTypeInt(Out, ETy, TypeTable);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000331 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
332 TypeTable, Table);
333 }
334 }
335 Out << " ]";
336 }
337 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
338 Out << "{";
339 if (CS->getNumOperands()) {
340 Out << " ";
341 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
342
343 WriteAsOperandInternal(Out, CS->getOperand(0),
344 PrintName, TypeTable, Table);
345
346 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
347 Out << ", ";
348 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
349
350 WriteAsOperandInternal(Out, CS->getOperand(i),
351 PrintName, TypeTable, Table);
352 }
353 }
354
355 Out << " }";
356 } else if (isa<ConstantPointerNull>(CV)) {
357 Out << "null";
358
Chris Lattner113f4f42002-06-25 16:13:24 +0000359 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000360 WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Table);
Vikram S. Adveb952b542002-07-14 23:14:45 +0000361
Chris Lattner3cd8c562002-07-30 18:54:25 +0000362 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattnerb8257222003-03-06 23:23:32 +0000363 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000364
365 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
366 printTypeInt(Out, (*OI)->getType(), TypeTable);
367 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
368 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +0000369 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +0000370 }
371
Chris Lattnercfe8f532002-08-16 21:17:11 +0000372 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner83b396b2002-08-15 19:37:43 +0000373 Out << " to ";
374 printTypeInt(Out, CE->getType(), TypeTable);
375 }
Chris Lattnercfe8f532002-08-16 21:17:11 +0000376 Out << ")";
Chris Lattner83b396b2002-08-15 19:37:43 +0000377
Chris Lattnerd84bb632002-04-16 21:36:08 +0000378 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000379 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000380 }
381}
382
383
Misha Brukmanc566ca362004-03-02 00:22:19 +0000384/// WriteAsOperand - Write the name of the specified value out to the specified
385/// ostream. This can be useful when you just want to print int %reg126, not
386/// the whole instruction that generated it.
387///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000388static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
389 bool PrintName,
390 std::map<const Type*, std::string> &TypeTable,
Chris Lattnerd84bb632002-04-16 21:36:08 +0000391 SlotCalculator *Table) {
392 Out << " ";
393 if (PrintName && V->hasName()) {
Chris Lattner1c343042003-08-22 05:40:38 +0000394 Out << getLLVMName(V->getName());
Chris Lattnerd84bb632002-04-16 21:36:08 +0000395 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000396 if (const Constant *CV = dyn_cast<Constant>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000397 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
398 } else {
399 int Slot;
400 if (Table) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000401 Slot = Table->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000402 } else {
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000403 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +0000404 Out << Ty->getDescription();
405 return;
406 }
407
408 Table = createSlotCalculator(V);
409 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
410
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000411 Slot = Table->getSlot(V);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000412 delete Table;
413 }
414 if (Slot >= 0) Out << "%" << Slot;
415 else if (PrintName)
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000416 if (V->hasName())
417 Out << "<badref: " << getLLVMName(V->getName()) << ">";
418 else
419 Out << "<badref>"; // Not embedded into a location?
Chris Lattnerd84bb632002-04-16 21:36:08 +0000420 }
421 }
422}
423
424
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000425/// WriteAsOperand - Write the name of the specified value out to the specified
426/// ostream. This can be useful when you just want to print int %reg126, not
427/// the whole instruction that generated it.
428///
Chris Lattner189d19f2003-11-21 20:23:48 +0000429std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000430 bool PrintType, bool PrintName,
431 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000432 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000433 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000434
Chris Lattner98cf1f52002-11-20 18:36:02 +0000435 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000436 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000437
438 if (PrintType)
439 printTypeInt(Out, V->getType(), TypeNames);
440
Brian Gaekefda1f182003-11-16 23:08:27 +0000441 if (const Type *Ty = dyn_cast<Type> (V))
442 printTypeInt(Out, Ty, TypeNames);
443
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000444 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000445 return Out;
446}
447
Chris Lattner189d19f2003-11-21 20:23:48 +0000448namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000449
Chris Lattnerfee714f2001-09-07 16:36:04 +0000450class AssemblyWriter {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000451 std::ostream &Out;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000452 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000453 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000454 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000455 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000456public:
Chris Lattner8339f7d2003-10-30 23:41:03 +0000457 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M,
458 AssemblyAnnotationWriter *AAW)
459 : Out(o), Table(Tab), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000460
461 // If the module has a symbol table, take all global types and stuff their
462 // names into the TypeNames map.
463 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000464 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000465 }
466
Chris Lattner7bfee412001-10-29 16:05:51 +0000467 inline void write(const Module *M) { printModule(M); }
468 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000469 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000470 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000471 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000472 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000473 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000474
Chris Lattner1e194682002-04-18 18:53:13 +0000475 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
476
Chris Lattner2f7c9632001-06-06 20:29:01 +0000477private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000478 void printModule(const Module *M);
479 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000480 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000481 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000482 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000483 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000484 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000485 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000486
487 // printType - Go to extreme measures to attempt to print out a short,
488 // symbolic version of a type name.
489 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000490 std::ostream &printType(const Type *Ty) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000491 return printTypeInt(Out, Ty, TypeNames);
492 }
493
494 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
495 // without considering any symbolic types that we may have equal to it.
496 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000497 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000498
Chris Lattner862e3382001-10-13 06:42:36 +0000499 // printInfoComment - Print a little comment after the instruction indicating
500 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000501 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000502};
Chris Lattner189d19f2003-11-21 20:23:48 +0000503} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000504
Misha Brukmanc566ca362004-03-02 00:22:19 +0000505/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
506/// without considering any symbolic types that we may have equal to it.
507///
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000508std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000509 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000510 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000511 for (FunctionType::param_iterator I = FTy->param_begin(),
512 E = FTy->param_end(); I != E; ++I) {
513 if (I != FTy->param_begin())
Chris Lattnerd816b532002-04-13 20:53:41 +0000514 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000515 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000516 }
517 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000518 if (FTy->getNumParams()) Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000519 Out << "...";
520 }
521 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000522 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000523 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000524 for (StructType::element_iterator I = STy->element_begin(),
525 E = STy->element_end(); I != E; ++I) {
526 if (I != STy->element_begin())
Chris Lattnerd816b532002-04-13 20:53:41 +0000527 Out << ", ";
528 printType(*I);
529 }
530 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000531 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000532 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000533 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000534 Out << "[" << ATy->getNumElements() << " x ";
535 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000536 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner39adbfc2003-06-01 03:45:51 +0000537 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000538 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000539 if (!Ty->isPrimitiveType())
540 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000541 printType(Ty);
542 }
543 return Out;
544}
545
546
Chris Lattnerfee714f2001-09-07 16:36:04 +0000547void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
548 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000549 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000550 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000551}
552
Chris Lattner2f7c9632001-06-06 20:29:01 +0000553
Chris Lattner7bfee412001-10-29 16:05:51 +0000554void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-08-24 13:48:48 +0000555 switch (M->getEndianness()) {
556 case Module::LittleEndian: Out << "target endian = little\n"; break;
557 case Module::BigEndian: Out << "target endian = big\n"; break;
558 case Module::AnyEndianness: break;
559 }
560 switch (M->getPointerSize()) {
561 case Module::Pointer32: Out << "target pointersize = 32\n"; break;
562 case Module::Pointer64: Out << "target pointersize = 64\n"; break;
563 case Module::AnyPointerSize: break;
564 }
565
Chris Lattnerfee714f2001-09-07 16:36:04 +0000566 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000567 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000568
Chris Lattner113f4f42002-06-25 16:13:24 +0000569 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
570 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000571
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000572 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000573
Chris Lattner6915f8f2002-04-07 22:49:37 +0000574 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000575 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
576 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000577}
578
Chris Lattner7bfee412001-10-29 16:05:51 +0000579void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner1c343042003-08-22 05:40:38 +0000580 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000581
Chris Lattner379a8d22003-04-16 20:28:45 +0000582 if (!GV->hasInitializer())
583 Out << "external ";
584 else
585 switch (GV->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000586 case GlobalValue::InternalLinkage: Out << "internal "; break;
587 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
588 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000589 case GlobalValue::AppendingLinkage: Out << "appending "; break;
590 case GlobalValue::ExternalLinkage: break;
591 }
Chris Lattner37798642001-09-18 04:01:05 +0000592
Chris Lattner7bfee412001-10-29 16:05:51 +0000593 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000594 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000595
596 if (GV->hasInitializer())
597 writeOperand(GV->getInitializer(), false, false);
598
Chris Lattner113f4f42002-06-25 16:13:24 +0000599 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000600 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000601}
602
Chris Lattnerfee714f2001-09-07 16:36:04 +0000603
Misha Brukmanc566ca362004-03-02 00:22:19 +0000604/// printSymbolTable - Run through symbol table looking for named constants
605/// if a named constant is found, emit it's declaration...
606///
Chris Lattner7bfee412001-10-29 16:05:51 +0000607void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-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 Lattnerf26a8ee2003-07-23 15:30:06 +0000614 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000615 printConstant(CPV);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000616 } else if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerda8571b2003-11-09 15:51:07 +0000617 assert(Ty->getType() == Type::TypeTy && TI->first == Type::TypeTy);
Chris Lattner1c343042003-08-22 05:40:38 +0000618 Out << "\t" << getLLVMName(I->first) << " = type ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000619
620 // Make sure we print out at least one level of the type structure, so
621 // that we do not get %FILE = type %FILE
622 //
623 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000624 }
625 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000626 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000627}
628
629
Misha Brukmanc566ca362004-03-02 00:22:19 +0000630/// printConstant - Print out a constant pool entry...
631///
Chris Lattner3462ae32001-12-03 22:26:30 +0000632void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000633 // Don't print out unnamed constants, they will be inlined
634 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000635
Chris Lattneree998be2001-07-26 16:29:38 +0000636 // Print out name...
Chris Lattner1c343042003-08-22 05:40:38 +0000637 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000638
639 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000640 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000641
Chris Lattner113f4f42002-06-25 16:13:24 +0000642 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000643 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000644}
645
Misha Brukmanc566ca362004-03-02 00:22:19 +0000646/// printFunction - Print all aspects of a function.
647///
Chris Lattner113f4f42002-06-25 16:13:24 +0000648void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000649 // Print out the return type and name...
Chris Lattner379a8d22003-04-16 20:28:45 +0000650 Out << "\n";
651
Chris Lattner8339f7d2003-10-30 23:41:03 +0000652 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
653
Chris Lattner379a8d22003-04-16 20:28:45 +0000654 if (F->isExternal())
655 Out << "declare ";
656 else
657 switch (F->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000658 case GlobalValue::InternalLinkage: Out << "internal "; break;
659 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
660 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000661 case GlobalValue::AppendingLinkage: Out << "appending "; break;
662 case GlobalValue::ExternalLinkage: break;
663 }
664
Chris Lattner13651f02003-09-03 17:56:43 +0000665 printType(F->getReturnType()) << " ";
Chris Lattner5b337482003-10-18 05:57:43 +0000666 if (!F->getName().empty())
667 Out << getLLVMName(F->getName());
668 else
669 Out << "\"\"";
Chris Lattner13651f02003-09-03 17:56:43 +0000670 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000671 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000672
Chris Lattner7bfee412001-10-29 16:05:51 +0000673 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000674 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000675
Chris Lattner149376d2002-10-13 20:57:00 +0000676 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
677 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000678
679 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000680 if (FT->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000681 if (FT->getNumParams()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000682 Out << "..."; // Output varargs portion of signature!
683 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000684 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000685
Chris Lattner113f4f42002-06-25 16:13:24 +0000686 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000687 Out << "\n";
688 } else {
689 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000690
Chris Lattner6915f8f2002-04-07 22:49:37 +0000691 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000692 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
693 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000694
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000695 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000696 }
697
Chris Lattner6915f8f2002-04-07 22:49:37 +0000698 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000699}
700
Misha Brukmanc566ca362004-03-02 00:22:19 +0000701/// printArgument - This member is called for every argument that is passed into
702/// the function. Simply print it out
703///
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000704void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000705 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000706 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000707
708 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000709 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000710
711 // Output name, if available...
712 if (Arg->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000713 Out << " " << getLLVMName(Arg->getName());
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000714 else if (Table.getSlot(Arg) < 0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000715 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000716}
717
Misha Brukmanc566ca362004-03-02 00:22:19 +0000718/// printBasicBlock - This member is called for each basic block in a method.
719///
Chris Lattner7bfee412001-10-29 16:05:51 +0000720void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000721 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner58185f22002-10-02 19:38:55 +0000722 Out << "\n" << BB->getName() << ":";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000723 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000724 int Slot = Table.getSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000725 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000726 if (Slot >= 0)
Misha Brukman7fdaab42003-07-14 17:20:40 +0000727 Out << Slot; // Extra newline separates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000728 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000729 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000730 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000731
732 if (BB->getParent() == 0)
733 Out << "\t\t; Error: Block without parent!";
734 else {
735 if (BB != &BB->getParent()->front()) { // Not the entry block?
736 // Output predecessors for the block...
737 Out << "\t\t;";
738 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
739
740 if (PI == PE) {
741 Out << " No predecessors!";
742 } else {
743 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +0000744 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +0000745 for (++PI; PI != PE; ++PI) {
746 Out << ",";
747 writeOperand(*PI, false, true);
748 }
Chris Lattner00211f12003-11-16 22:59:57 +0000749 }
Chris Lattner58185f22002-10-02 19:38:55 +0000750 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000751 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000752
753 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000754
Chris Lattner96cdd272004-03-08 18:51:45 +0000755 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000756
Chris Lattnerfee714f2001-09-07 16:36:04 +0000757 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000758 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
759 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +0000760
761 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000762}
763
Chris Lattner862e3382001-10-13 06:42:36 +0000764
Misha Brukmanc566ca362004-03-02 00:22:19 +0000765/// printInfoComment - Print a little comment after the instruction indicating
766/// which slot it occupies.
767///
Chris Lattner113f4f42002-06-25 16:13:24 +0000768void AssemblyWriter::printInfoComment(const Value &V) {
769 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000770 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000771 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000772
Chris Lattner113f4f42002-06-25 16:13:24 +0000773 if (!V.hasName()) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000774 int Slot = Table.getSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000775 if (Slot >= 0) Out << ":" << Slot;
776 else Out << ":<badref>";
777 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000778 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000779 }
780}
781
Misha Brukmanc566ca362004-03-02 00:22:19 +0000782/// printInstruction - This member is called for each Instruction in a method.
783///
Chris Lattner113f4f42002-06-25 16:13:24 +0000784void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner8339f7d2003-10-30 23:41:03 +0000785 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
786
Chris Lattner2f7c9632001-06-06 20:29:01 +0000787 Out << "\t";
788
789 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000790 if (I.hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000791 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000792
Chris Lattner504f9242003-09-08 17:45:59 +0000793 // If this is a volatile load or store, print out the volatile marker
794 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
795 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
796 Out << "volatile ";
797
Chris Lattner2f7c9632001-06-06 20:29:01 +0000798 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000799 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000800
801 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000802 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000803
804 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000805 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
806 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000807 Out << ",";
808 writeOperand(Operand, true);
809 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000810 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000811
Chris Lattner8d48df22002-04-13 18:34:38 +0000812 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000813 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000814 writeOperand(Operand , true); Out << ",";
815 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000816
Chris Lattner113f4f42002-06-25 16:13:24 +0000817 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000818 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000819 writeOperand(I.getOperand(op ), true); Out << ",";
820 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000821 }
822 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000823 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000824 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000825 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000826 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000827
Chris Lattner113f4f42002-06-25 16:13:24 +0000828 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000829 if (op) Out << ", ";
830 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000831 writeOperand(I.getOperand(op ), false); Out << ",";
832 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000833 }
Chris Lattner862e3382001-10-13 06:42:36 +0000834 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000835 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000836 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000837 const PointerType *PTy = cast<PointerType>(Operand->getType());
838 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
839 const Type *RetTy = FTy->getReturnType();
Chris Lattner2f2d9472001-11-06 21:28:12 +0000840
Chris Lattner463d6a52003-08-05 15:34:45 +0000841 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000842 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +0000843 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000844 //
Chris Lattner463d6a52003-08-05 15:34:45 +0000845 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +0000846 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000847 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000848 Out << " "; printType(RetTy);
849 writeOperand(Operand, false);
850 } else {
851 writeOperand(Operand, true);
852 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000853 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000854 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
855 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000856 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000857 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000858 }
859
860 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000861 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000862 const PointerType *PTy = cast<PointerType>(Operand->getType());
863 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
864 const Type *RetTy = FTy->getReturnType();
865
866 // If possible, print out the short form of the invoke instruction. We can
867 // only do this if the first argument is a pointer to a nonvararg function,
868 // and if the return type is not a pointer to a function.
869 //
870 if (!FTy->isVarArg() &&
871 (!isa<PointerType>(RetTy) ||
872 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
873 Out << " "; printType(RetTy);
874 writeOperand(Operand, false);
875 } else {
876 writeOperand(Operand, true);
877 }
878
Chris Lattner862e3382001-10-13 06:42:36 +0000879 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000880 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
881 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000882 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000883 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000884 }
885
886 Out << " )\n\t\t\tto";
887 writeOperand(II->getNormalDest(), true);
Chris Lattner85195142004-02-08 21:52:30 +0000888 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +0000889 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000890
Chris Lattner113f4f42002-06-25 16:13:24 +0000891 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000892 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000893 printType(AI->getType()->getElementType());
894 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000895 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000896 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000897 }
Chris Lattner862e3382001-10-13 06:42:36 +0000898 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000899 if (Operand) writeOperand(Operand, true); // Work with broken code
Chris Lattner7bfee412001-10-29 16:05:51 +0000900 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000901 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000902 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000903 if (Operand) writeOperand(Operand, true); // Work with broken code
Chris Lattnerf70da102003-05-08 02:44:12 +0000904 Out << ", ";
905 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000906 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000907 if (Operand) writeOperand(Operand, true); // Work with broken code
Chris Lattner5b337482003-10-18 05:57:43 +0000908 Out << ", ";
909 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000910 } else if (Operand) { // Print the normal way...
911
912 // PrintAllTypes - Instructions who have operands of all the same type
913 // omit the type from all but the first operand. If the instruction has
914 // different type operands (for example br), then they are all printed.
915 bool PrintAllTypes = false;
916 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000917
Chris Lattner52bd5cb92004-03-12 05:53:14 +0000918 // Shift Left & Right print both types even for Ubyte LHS, and select prints
919 // types even if all operands are bools.
920 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000921 PrintAllTypes = true;
922 } else {
923 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
924 Operand = I.getOperand(i);
925 if (Operand->getType() != TheType) {
926 PrintAllTypes = true; // We have differing types! Print them all!
927 break;
928 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000929 }
930 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000931
Chris Lattner7bfee412001-10-29 16:05:51 +0000932 if (!PrintAllTypes) {
933 Out << " ";
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000934 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +0000935 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000936
Chris Lattner113f4f42002-06-25 16:13:24 +0000937 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000938 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000939 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000940 }
941 }
942
Chris Lattner862e3382001-10-13 06:42:36 +0000943 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000944 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000945}
946
947
948//===----------------------------------------------------------------------===//
949// External Interface declarations
950//===----------------------------------------------------------------------===//
951
Chris Lattner8339f7d2003-10-30 23:41:03 +0000952void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000953 SlotCalculator SlotTable(this, false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000954 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000955 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000956}
957
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000958void GlobalVariable::print(std::ostream &o) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000959 SlotCalculator SlotTable(getParent(), false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000960 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000961 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000962}
963
Chris Lattner8339f7d2003-10-30 23:41:03 +0000964void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000965 SlotCalculator SlotTable(getParent(), false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000966 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000967
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000968 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000969}
970
Chris Lattner8339f7d2003-10-30 23:41:03 +0000971void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000972 SlotCalculator SlotTable(getParent(), false);
Chris Lattner7bfee412001-10-29 16:05:51 +0000973 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000974 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000975 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000976}
977
Chris Lattner8339f7d2003-10-30 23:41:03 +0000978void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000979 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattnerf78819d2004-01-18 21:03:06 +0000980 SlotCalculator SlotTable(F, false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000981 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, 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}
Chris Lattner7db79582001-11-07 04:21:57 +0000985
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000986void Constant::print(std::ostream &o) const {
987 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +0000988
989 // Handle CPR's special, because they have context information...
990 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
991 CPR->getValue()->print(o); // Print as a global value, with context info.
992 return;
993 }
994
Chris Lattner1e194682002-04-18 18:53:13 +0000995 o << " " << getType()->getDescription() << " ";
996
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000997 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +0000998 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000999}
1000
1001void Type::print(std::ostream &o) const {
1002 if (this == 0)
1003 o << "<null Type>";
1004 else
1005 o << getDescription();
1006}
1007
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001008void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001009 o << getType() << " " << getName();
1010}
1011
1012void Value::dump() const { print(std::cerr); }
1013
1014//===----------------------------------------------------------------------===//
1015// CachedWriter Class Implementation
1016//===----------------------------------------------------------------------===//
1017
Chris Lattner7db79582001-11-07 04:21:57 +00001018void CachedWriter::setModule(const Module *M) {
1019 delete SC; delete AW;
1020 if (M) {
Chris Lattner6089bb12004-01-14 02:49:34 +00001021 SC = new SlotCalculator(M, false);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001022 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001023 } else {
1024 SC = 0; AW = 0;
1025 }
1026}
1027
1028CachedWriter::~CachedWriter() {
1029 delete AW;
1030 delete SC;
1031}
1032
1033CachedWriter &CachedWriter::operator<<(const Value *V) {
1034 assert(AW && SC && "CachedWriter does not have a current module!");
1035 switch (V->getValueType()) {
1036 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +00001037 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001038 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001039 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1040 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +00001041 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001042 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001043 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
1044 }
1045 return *this;
1046}