blob: 535b63e30650743f863bf47ea7140774e889f1b6 [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
100// If the module has a symbol table, take all global types and stuff their
101// names into the TypeNames map.
102//
103static 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
384// 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 the
386// 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
Chris Lattnerb86620e2001-10-29 16:37:48 +0000425
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000426/// WriteAsOperand - Write the name of the specified value out to the specified
427/// ostream. This can be useful when you just want to print int %reg126, not
428/// the whole instruction that generated it.
429///
Chris Lattner189d19f2003-11-21 20:23:48 +0000430std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukmanb22d09c2004-03-01 19:48:13 +0000431 bool PrintType, bool PrintName,
432 const Module *Context) {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000433 std::map<const Type *, std::string> TypeNames;
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000434 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +0000435
Chris Lattner98cf1f52002-11-20 18:36:02 +0000436 if (Context)
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000437 fillTypeNameTable(Context, TypeNames);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000438
439 if (PrintType)
440 printTypeInt(Out, V->getType(), TypeNames);
441
Brian Gaekefda1f182003-11-16 23:08:27 +0000442 if (const Type *Ty = dyn_cast<Type> (V))
443 printTypeInt(Out, Ty, TypeNames);
444
Chris Lattner5a9f63e2002-07-10 16:48:17 +0000445 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +0000446 return Out;
447}
448
Chris Lattner189d19f2003-11-21 20:23:48 +0000449namespace llvm {
Chris Lattner2e9fee42001-07-12 23:35:26 +0000450
Chris Lattnerfee714f2001-09-07 16:36:04 +0000451class AssemblyWriter {
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000452 std::ostream &Out;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000453 SlotCalculator &Table;
Chris Lattner7bfee412001-10-29 16:05:51 +0000454 const Module *TheModule;
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000455 std::map<const Type *, std::string> TypeNames;
Chris Lattner8339f7d2003-10-30 23:41:03 +0000456 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000457public:
Chris Lattner8339f7d2003-10-30 23:41:03 +0000458 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M,
459 AssemblyAnnotationWriter *AAW)
460 : Out(o), Table(Tab), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000461
462 // If the module has a symbol table, take all global types and stuff their
463 // names into the TypeNames map.
464 //
Chris Lattnerb86620e2001-10-29 16:37:48 +0000465 fillTypeNameTable(M, TypeNames);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000466 }
467
Chris Lattner7bfee412001-10-29 16:05:51 +0000468 inline void write(const Module *M) { printModule(M); }
469 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner57698e22002-03-26 18:01:55 +0000470 inline void write(const Function *F) { printFunction(F); }
Chris Lattner7bfee412001-10-29 16:05:51 +0000471 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner113f4f42002-06-25 16:13:24 +0000472 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner3462ae32001-12-03 22:26:30 +0000473 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattner7db79582001-11-07 04:21:57 +0000474 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000475
Chris Lattner1e194682002-04-18 18:53:13 +0000476 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
477
Chris Lattner2f7c9632001-06-06 20:29:01 +0000478private :
Chris Lattner7bfee412001-10-29 16:05:51 +0000479 void printModule(const Module *M);
480 void printSymbolTable(const SymbolTable &ST);
Chris Lattner3462ae32001-12-03 22:26:30 +0000481 void printConstant(const Constant *CPV);
Chris Lattner7bfee412001-10-29 16:05:51 +0000482 void printGlobal(const GlobalVariable *GV);
Chris Lattner57698e22002-03-26 18:01:55 +0000483 void printFunction(const Function *F);
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000484 void printArgument(const Argument *FA);
Chris Lattner7bfee412001-10-29 16:05:51 +0000485 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +0000486 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000487
488 // printType - Go to extreme measures to attempt to print out a short,
489 // symbolic version of a type name.
490 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000491 std::ostream &printType(const Type *Ty) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000492 return printTypeInt(Out, Ty, TypeNames);
493 }
494
495 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
496 // without considering any symbolic types that we may have equal to it.
497 //
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000498 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattner7bfee412001-10-29 16:05:51 +0000499
Chris Lattner862e3382001-10-13 06:42:36 +0000500 // printInfoComment - Print a little comment after the instruction indicating
501 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +0000502 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000503};
Chris Lattner189d19f2003-11-21 20:23:48 +0000504} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +0000505
Chris Lattnerd816b532002-04-13 20:53:41 +0000506// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
507// without considering any symbolic types that we may have equal to it.
508//
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000509std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000510 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000511 printType(FTy->getReturnType()) << " (";
Chris Lattnerfa829be2004-02-09 04:14:01 +0000512 for (FunctionType::param_iterator I = FTy->param_begin(),
513 E = FTy->param_end(); I != E; ++I) {
514 if (I != FTy->param_begin())
Chris Lattnerd816b532002-04-13 20:53:41 +0000515 Out << ", ";
Chris Lattnerd84bb632002-04-16 21:36:08 +0000516 printType(*I);
Chris Lattnerd816b532002-04-13 20:53:41 +0000517 }
518 if (FTy->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000519 if (FTy->getNumParams()) Out << ", ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000520 Out << "...";
521 }
522 Out << ")";
Chris Lattner113f4f42002-06-25 16:13:24 +0000523 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000524 Out << "{ ";
Chris Lattnerac6db752004-02-09 04:37:31 +0000525 for (StructType::element_iterator I = STy->element_begin(),
526 E = STy->element_end(); I != E; ++I) {
527 if (I != STy->element_begin())
Chris Lattnerd816b532002-04-13 20:53:41 +0000528 Out << ", ";
529 printType(*I);
530 }
531 Out << " }";
Chris Lattner113f4f42002-06-25 16:13:24 +0000532 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000533 printType(PTy->getElementType()) << "*";
Chris Lattner113f4f42002-06-25 16:13:24 +0000534 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnerd816b532002-04-13 20:53:41 +0000535 Out << "[" << ATy->getNumElements() << " x ";
536 printType(ATy->getElementType()) << "]";
Chris Lattner113f4f42002-06-25 16:13:24 +0000537 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner39adbfc2003-06-01 03:45:51 +0000538 Out << "opaque";
Chris Lattnerd816b532002-04-13 20:53:41 +0000539 } else {
Vikram S. Adveb952b542002-07-14 23:14:45 +0000540 if (!Ty->isPrimitiveType())
541 Out << "<unknown derived type>";
Chris Lattnerd816b532002-04-13 20:53:41 +0000542 printType(Ty);
543 }
544 return Out;
545}
546
547
Chris Lattnerfee714f2001-09-07 16:36:04 +0000548void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
549 bool PrintName) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000550 if (PrintType) { Out << " "; printType(Operand->getType()); }
Chris Lattnerd84bb632002-04-16 21:36:08 +0000551 WriteAsOperandInternal(Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000552}
553
Chris Lattner2f7c9632001-06-06 20:29:01 +0000554
Chris Lattner7bfee412001-10-29 16:05:51 +0000555void AssemblyWriter::printModule(const Module *M) {
Chris Lattner8068e0c2003-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 Lattnerfee714f2001-09-07 16:36:04 +0000567 // Loop over the symbol table, emitting all named constants...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000568 printSymbolTable(M->getSymbolTable());
Chris Lattnerda975502001-09-10 07:58:01 +0000569
Chris Lattner113f4f42002-06-25 16:13:24 +0000570 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
571 printGlobal(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000572
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000573 Out << "\nimplementation ; Functions:\n";
Vikram S. Adve13ba19a2001-09-18 12:48:16 +0000574
Chris Lattner6915f8f2002-04-07 22:49:37 +0000575 // Output all of the functions...
Chris Lattner113f4f42002-06-25 16:13:24 +0000576 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
577 printFunction(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000578}
579
Chris Lattner7bfee412001-10-29 16:05:51 +0000580void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner1c343042003-08-22 05:40:38 +0000581 if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
Chris Lattner37798642001-09-18 04:01:05 +0000582
Chris Lattner379a8d22003-04-16 20:28:45 +0000583 if (!GV->hasInitializer())
584 Out << "external ";
585 else
586 switch (GV->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000587 case GlobalValue::InternalLinkage: Out << "internal "; break;
588 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
589 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000590 case GlobalValue::AppendingLinkage: Out << "appending "; break;
591 case GlobalValue::ExternalLinkage: break;
592 }
Chris Lattner37798642001-09-18 04:01:05 +0000593
Chris Lattner7bfee412001-10-29 16:05:51 +0000594 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner2413b162001-12-04 00:03:30 +0000595 printType(GV->getType()->getElementType());
Chris Lattner37798642001-09-18 04:01:05 +0000596
597 if (GV->hasInitializer())
598 writeOperand(GV->getInitializer(), false, false);
599
Chris Lattner113f4f42002-06-25 16:13:24 +0000600 printInfoComment(*GV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000601 Out << "\n";
Chris Lattnerda975502001-09-10 07:58:01 +0000602}
603
Chris Lattnerfee714f2001-09-07 16:36:04 +0000604
Chris Lattner7bfee412001-10-29 16:05:51 +0000605// printSymbolTable - Run through symbol table looking for named constants
Chris Lattnerfee714f2001-09-07 16:36:04 +0000606// if a named constant is found, emit it's declaration...
607//
Chris Lattner7bfee412001-10-29 16:05:51 +0000608void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000609 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
610 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
611 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
612
613 for (; I != End; ++I) {
614 const Value *V = I->second;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000615 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000616 printConstant(CPV);
Chris Lattnerf26a8ee2003-07-23 15:30:06 +0000617 } else if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattnerda8571b2003-11-09 15:51:07 +0000618 assert(Ty->getType() == Type::TypeTy && TI->first == Type::TypeTy);
Chris Lattner1c343042003-08-22 05:40:38 +0000619 Out << "\t" << getLLVMName(I->first) << " = type ";
Chris Lattnerd816b532002-04-13 20:53:41 +0000620
621 // Make sure we print out at least one level of the type structure, so
622 // that we do not get %FILE = type %FILE
623 //
624 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000625 }
626 }
Chris Lattnera7620d92001-07-15 06:35:59 +0000627 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000628}
629
630
Chris Lattner7bfee412001-10-29 16:05:51 +0000631// printConstant - Print out a constant pool entry...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000632//
Chris Lattner3462ae32001-12-03 22:26:30 +0000633void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattnerfee714f2001-09-07 16:36:04 +0000634 // Don't print out unnamed constants, they will be inlined
635 if (!CPV->hasName()) return;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000636
Chris Lattneree998be2001-07-26 16:29:38 +0000637 // Print out name...
Chris Lattner1c343042003-08-22 05:40:38 +0000638 Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000639
640 // Write the value out now...
Chris Lattnerd84bb632002-04-16 21:36:08 +0000641 writeOperand(CPV, true, false);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000642
Chris Lattner113f4f42002-06-25 16:13:24 +0000643 printInfoComment(*CPV);
Chris Lattner7f74a562002-01-20 22:54:45 +0000644 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000645}
646
Chris Lattner6915f8f2002-04-07 22:49:37 +0000647// printFunction - Print all aspects of a function.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000648//
Chris Lattner113f4f42002-06-25 16:13:24 +0000649void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000650 // Print out the return type and name...
Chris Lattner379a8d22003-04-16 20:28:45 +0000651 Out << "\n";
652
Chris Lattner8339f7d2003-10-30 23:41:03 +0000653 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
654
Chris Lattner379a8d22003-04-16 20:28:45 +0000655 if (F->isExternal())
656 Out << "declare ";
657 else
658 switch (F->getLinkage()) {
Chris Lattner068ad842003-10-16 18:29:00 +0000659 case GlobalValue::InternalLinkage: Out << "internal "; break;
660 case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
661 case GlobalValue::WeakLinkage: Out << "weak "; break;
Chris Lattner379a8d22003-04-16 20:28:45 +0000662 case GlobalValue::AppendingLinkage: Out << "appending "; break;
663 case GlobalValue::ExternalLinkage: break;
664 }
665
Chris Lattner13651f02003-09-03 17:56:43 +0000666 printType(F->getReturnType()) << " ";
Chris Lattner5b337482003-10-18 05:57:43 +0000667 if (!F->getName().empty())
668 Out << getLLVMName(F->getName());
669 else
670 Out << "\"\"";
Chris Lattner13651f02003-09-03 17:56:43 +0000671 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000672 Table.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000673
Chris Lattner7bfee412001-10-29 16:05:51 +0000674 // Loop over the arguments, printing them...
Chris Lattner113f4f42002-06-25 16:13:24 +0000675 const FunctionType *FT = F->getFunctionType();
Chris Lattnerfee714f2001-09-07 16:36:04 +0000676
Chris Lattner149376d2002-10-13 20:57:00 +0000677 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
678 printArgument(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000679
680 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +0000681 if (FT->isVarArg()) {
Chris Lattnerfa829be2004-02-09 04:14:01 +0000682 if (FT->getNumParams()) Out << ", ";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000683 Out << "..."; // Output varargs portion of signature!
684 }
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000685 Out << ")";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000686
Chris Lattner113f4f42002-06-25 16:13:24 +0000687 if (F->isExternal()) {
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000688 Out << "\n";
689 } else {
690 Out << " {";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000691
Chris Lattner6915f8f2002-04-07 22:49:37 +0000692 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +0000693 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
694 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +0000695
Chris Lattnerb2f02e52002-05-06 03:00:40 +0000696 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +0000697 }
698
Chris Lattner6915f8f2002-04-07 22:49:37 +0000699 Table.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000700}
701
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000702// printArgument - This member is called for every argument that
Chris Lattner6915f8f2002-04-07 22:49:37 +0000703// is passed into the function. Simply print it out
Chris Lattner2f7c9632001-06-06 20:29:01 +0000704//
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000705void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000706 // Insert commas as we go... the first arg doesn't get a comma
Chris Lattner113f4f42002-06-25 16:13:24 +0000707 if (Arg != &Arg->getParent()->afront()) Out << ", ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000708
709 // Output type...
Chris Lattner7bfee412001-10-29 16:05:51 +0000710 printType(Arg->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000711
712 // Output name, if available...
713 if (Arg->hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000714 Out << " " << getLLVMName(Arg->getName());
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000715 else if (Table.getSlot(Arg) < 0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000716 Out << "<badref>";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000717}
718
Misha Brukmanfa100532003-10-10 17:54:14 +0000719// printBasicBlock - This member is called for each basic block in a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720//
Chris Lattner7bfee412001-10-29 16:05:51 +0000721void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000722 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner58185f22002-10-02 19:38:55 +0000723 Out << "\n" << BB->getName() << ":";
Chris Lattner408dbdb2002-05-14 16:02:05 +0000724 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000725 int Slot = Table.getSlot(BB);
Chris Lattnera2f01872001-06-07 16:58:55 +0000726 Out << "\n; <label>:";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000727 if (Slot >= 0)
Misha Brukman7fdaab42003-07-14 17:20:40 +0000728 Out << Slot; // Extra newline separates out label's
Chris Lattner2f7c9632001-06-06 20:29:01 +0000729 else
Chris Lattnera2f01872001-06-07 16:58:55 +0000730 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +0000731 }
Chris Lattner2447ef52003-11-20 00:09:43 +0000732
733 if (BB->getParent() == 0)
734 Out << "\t\t; Error: Block without parent!";
735 else {
736 if (BB != &BB->getParent()->front()) { // Not the entry block?
737 // Output predecessors for the block...
738 Out << "\t\t;";
739 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
740
741 if (PI == PE) {
742 Out << " No predecessors!";
743 } else {
744 Out << " preds =";
Chris Lattner00211f12003-11-16 22:59:57 +0000745 writeOperand(*PI, false, true);
Chris Lattner2447ef52003-11-20 00:09:43 +0000746 for (++PI; PI != PE; ++PI) {
747 Out << ",";
748 writeOperand(*PI, false, true);
749 }
Chris Lattner00211f12003-11-16 22:59:57 +0000750 }
Chris Lattner58185f22002-10-02 19:38:55 +0000751 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000752 }
Chris Lattner408dbdb2002-05-14 16:02:05 +0000753
754 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000755
Chris Lattner8339f7d2003-10-30 23:41:03 +0000756 if (AnnotationWriter) AnnotationWriter->emitBasicBlockAnnot(BB, Out);
757
Chris Lattnerfee714f2001-09-07 16:36:04 +0000758 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +0000759 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
760 printInstruction(*I);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000761}
762
Chris Lattner862e3382001-10-13 06:42:36 +0000763
764// printInfoComment - Print a little comment after the instruction indicating
765// which slot it occupies.
766//
Chris Lattner113f4f42002-06-25 16:13:24 +0000767void AssemblyWriter::printInfoComment(const Value &V) {
768 if (V.getType() != Type::VoidTy) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000769 Out << "\t\t; <";
Chris Lattner113f4f42002-06-25 16:13:24 +0000770 printType(V.getType()) << ">";
Chris Lattner862e3382001-10-13 06:42:36 +0000771
Chris Lattner113f4f42002-06-25 16:13:24 +0000772 if (!V.hasName()) {
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000773 int Slot = Table.getSlot(&V); // Print out the def slot taken...
Chris Lattner862e3382001-10-13 06:42:36 +0000774 if (Slot >= 0) Out << ":" << Slot;
775 else Out << ":<badref>";
776 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000777 Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +0000778 }
779}
780
Misha Brukmanfa100532003-10-10 17:54:14 +0000781// printInstruction - This member is called for each Instruction in a method.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000782//
Chris Lattner113f4f42002-06-25 16:13:24 +0000783void AssemblyWriter::printInstruction(const Instruction &I) {
Chris Lattner8339f7d2003-10-30 23:41:03 +0000784 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
785
Chris Lattner2f7c9632001-06-06 20:29:01 +0000786 Out << "\t";
787
788 // Print out name if it exists...
Chris Lattner113f4f42002-06-25 16:13:24 +0000789 if (I.hasName())
Chris Lattner1c343042003-08-22 05:40:38 +0000790 Out << getLLVMName(I.getName()) << " = ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000791
Chris Lattner504f9242003-09-08 17:45:59 +0000792 // If this is a volatile load or store, print out the volatile marker
793 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
794 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
795 Out << "volatile ";
796
Chris Lattner2f7c9632001-06-06 20:29:01 +0000797 // Print out the opcode...
Chris Lattner113f4f42002-06-25 16:13:24 +0000798 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000799
800 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +0000801 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000802
803 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner113f4f42002-06-25 16:13:24 +0000804 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
805 writeOperand(I.getOperand(2), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000806 Out << ",";
807 writeOperand(Operand, true);
808 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000809 writeOperand(I.getOperand(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000810
Chris Lattner8d48df22002-04-13 18:34:38 +0000811 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000812 // Special case switch statement to get formatting nice and correct...
Chris Lattner113f4f42002-06-25 16:13:24 +0000813 writeOperand(Operand , true); Out << ",";
814 writeOperand(I.getOperand(1), true); Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000815
Chris Lattner113f4f42002-06-25 16:13:24 +0000816 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000817 Out << "\n\t\t";
Chris Lattner113f4f42002-06-25 16:13:24 +0000818 writeOperand(I.getOperand(op ), true); Out << ",";
819 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000820 }
821 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +0000822 } else if (isa<PHINode>(I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000823 Out << " ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000824 printType(I.getType());
Chris Lattner9d3292d2001-11-06 08:33:46 +0000825 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000826
Chris Lattner113f4f42002-06-25 16:13:24 +0000827 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Chris Lattner29644b32001-11-06 01:48:45 +0000828 if (op) Out << ", ";
829 Out << "[";
Chris Lattner113f4f42002-06-25 16:13:24 +0000830 writeOperand(I.getOperand(op ), false); Out << ",";
831 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +0000832 }
Chris Lattner862e3382001-10-13 06:42:36 +0000833 } else if (isa<ReturnInst>(I) && !Operand) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000834 Out << " void";
Chris Lattner862e3382001-10-13 06:42:36 +0000835 } else if (isa<CallInst>(I)) {
Chris Lattner463d6a52003-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();
Chris Lattner2f2d9472001-11-06 21:28:12 +0000839
Chris Lattner463d6a52003-08-05 15:34:45 +0000840 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +0000841 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +0000842 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +0000843 //
Chris Lattner463d6a52003-08-05 15:34:45 +0000844 if (!FTy->isVarArg() &&
Chris Lattner8d48df22002-04-13 18:34:38 +0000845 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +0000846 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner2f2d9472001-11-06 21:28:12 +0000847 Out << " "; printType(RetTy);
848 writeOperand(Operand, false);
849 } else {
850 writeOperand(Operand, true);
851 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000852 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000853 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
854 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000855 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000856 writeOperand(I.getOperand(op), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000857 }
858
859 Out << " )";
Chris Lattner113f4f42002-06-25 16:13:24 +0000860 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner463d6a52003-08-05 15:34:45 +0000861 const PointerType *PTy = cast<PointerType>(Operand->getType());
862 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
863 const Type *RetTy = FTy->getReturnType();
864
865 // If possible, print out the short form of the invoke instruction. We can
866 // only do this if the first argument is a pointer to a nonvararg function,
867 // and if the return type is not a pointer to a function.
868 //
869 if (!FTy->isVarArg() &&
870 (!isa<PointerType>(RetTy) ||
871 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
872 Out << " "; printType(RetTy);
873 writeOperand(Operand, false);
874 } else {
875 writeOperand(Operand, true);
876 }
877
Chris Lattner862e3382001-10-13 06:42:36 +0000878 Out << "(";
Chris Lattner113f4f42002-06-25 16:13:24 +0000879 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
880 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Chris Lattner862e3382001-10-13 06:42:36 +0000881 Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000882 writeOperand(I.getOperand(op), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000883 }
884
885 Out << " )\n\t\t\tto";
886 writeOperand(II->getNormalDest(), true);
Chris Lattner85195142004-02-08 21:52:30 +0000887 Out << " unwind";
Chris Lattnerfae8ab32004-02-08 21:44:31 +0000888 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +0000889
Chris Lattner113f4f42002-06-25 16:13:24 +0000890 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Chris Lattner7bfee412001-10-29 16:05:51 +0000891 Out << " ";
Chris Lattner8d48df22002-04-13 18:34:38 +0000892 printType(AI->getType()->getElementType());
893 if (AI->isArrayAllocation()) {
Chris Lattnera073acb2001-07-07 08:36:50 +0000894 Out << ",";
Chris Lattner8d48df22002-04-13 18:34:38 +0000895 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000896 }
Chris Lattner862e3382001-10-13 06:42:36 +0000897 } else if (isa<CastInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000898 if (Operand) writeOperand(Operand, true); // Work with broken code
Chris Lattner7bfee412001-10-29 16:05:51 +0000899 Out << " to ";
Chris Lattner113f4f42002-06-25 16:13:24 +0000900 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000901 } else if (isa<VAArgInst>(I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000902 if (Operand) writeOperand(Operand, true); // Work with broken code
Chris Lattnerf70da102003-05-08 02:44:12 +0000903 Out << ", ";
904 printType(I.getType());
Chris Lattner5b337482003-10-18 05:57:43 +0000905 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattnerc96e96b2003-11-17 01:17:04 +0000906 if (Operand) writeOperand(Operand, true); // Work with broken code
Chris Lattner5b337482003-10-18 05:57:43 +0000907 Out << ", ";
908 printType(VAN->getArgType());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000909 } else if (Operand) { // Print the normal way...
910
911 // PrintAllTypes - Instructions who have operands of all the same type
912 // omit the type from all but the first operand. If the instruction has
913 // different type operands (for example br), then they are all printed.
914 bool PrintAllTypes = false;
915 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000916
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000917 // Shift Left & Right print both types even for Ubyte LHS
918 if (isa<ShiftInst>(I)) {
919 PrintAllTypes = true;
920 } else {
921 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
922 Operand = I.getOperand(i);
923 if (Operand->getType() != TheType) {
924 PrintAllTypes = true; // We have differing types! Print them all!
925 break;
926 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000927 }
928 }
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000929
Chris Lattner7bfee412001-10-29 16:05:51 +0000930 if (!PrintAllTypes) {
931 Out << " ";
Chris Lattnerdeccfaf2003-04-16 20:20:02 +0000932 printType(TheType);
Chris Lattner7bfee412001-10-29 16:05:51 +0000933 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000934
Chris Lattner113f4f42002-06-25 16:13:24 +0000935 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000936 if (i) Out << ",";
Chris Lattner113f4f42002-06-25 16:13:24 +0000937 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000938 }
939 }
940
Chris Lattner862e3382001-10-13 06:42:36 +0000941 printInfoComment(I);
Chris Lattner7f74a562002-01-20 22:54:45 +0000942 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +0000943}
944
945
946//===----------------------------------------------------------------------===//
947// External Interface declarations
948//===----------------------------------------------------------------------===//
949
Chris Lattner8339f7d2003-10-30 23:41:03 +0000950void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000951 SlotCalculator SlotTable(this, false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000952 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000953 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000954}
955
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000956void GlobalVariable::print(std::ostream &o) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000957 SlotCalculator SlotTable(getParent(), false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000958 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000959 W.write(this);
Chris Lattneradfe0d12001-09-10 20:08:19 +0000960}
961
Chris Lattner8339f7d2003-10-30 23:41:03 +0000962void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000963 SlotCalculator SlotTable(getParent(), false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000964 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000965
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000966 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000967}
968
Chris Lattner8339f7d2003-10-30 23:41:03 +0000969void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerf78819d2004-01-18 21:03:06 +0000970 SlotCalculator SlotTable(getParent(), false);
Chris Lattner7bfee412001-10-29 16:05:51 +0000971 AssemblyWriter W(o, SlotTable,
Chris Lattner8339f7d2003-10-30 23:41:03 +0000972 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000973 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000974}
975
Chris Lattner8339f7d2003-10-30 23:41:03 +0000976void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000977 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattnerf78819d2004-01-18 21:03:06 +0000978 SlotCalculator SlotTable(F, false);
Chris Lattner8339f7d2003-10-30 23:41:03 +0000979 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000980
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000981 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000982}
Chris Lattner7db79582001-11-07 04:21:57 +0000983
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000984void Constant::print(std::ostream &o) const {
985 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner7d734802002-09-10 15:53:49 +0000986
987 // Handle CPR's special, because they have context information...
988 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
989 CPR->getValue()->print(o); // Print as a global value, with context info.
990 return;
991 }
992
Chris Lattner1e194682002-04-18 18:53:13 +0000993 o << " " << getType()->getDescription() << " ";
994
Chris Lattnerab7d1ab2003-05-08 02:08:14 +0000995 std::map<const Type *, std::string> TypeTable;
Chris Lattner1e194682002-04-18 18:53:13 +0000996 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +0000997}
998
999void Type::print(std::ostream &o) const {
1000 if (this == 0)
1001 o << "<null Type>";
1002 else
1003 o << getDescription();
1004}
1005
Chris Lattner2e9fa6d2002-04-09 19:48:49 +00001006void Argument::print(std::ostream &o) const {
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001007 o << getType() << " " << getName();
1008}
1009
1010void Value::dump() const { print(std::cerr); }
1011
1012//===----------------------------------------------------------------------===//
1013// CachedWriter Class Implementation
1014//===----------------------------------------------------------------------===//
1015
Chris Lattner7db79582001-11-07 04:21:57 +00001016void CachedWriter::setModule(const Module *M) {
1017 delete SC; delete AW;
1018 if (M) {
Chris Lattner6089bb12004-01-14 02:49:34 +00001019 SC = new SlotCalculator(M, false);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001020 AW = new AssemblyWriter(Out, *SC, M, 0);
Chris Lattner7db79582001-11-07 04:21:57 +00001021 } else {
1022 SC = 0; AW = 0;
1023 }
1024}
1025
1026CachedWriter::~CachedWriter() {
1027 delete AW;
1028 delete SC;
1029}
1030
1031CachedWriter &CachedWriter::operator<<(const Value *V) {
1032 assert(AW && SC && "CachedWriter does not have a current module!");
1033 switch (V->getValueType()) {
1034 case Value::ConstantVal:
Chris Lattner1e194682002-04-18 18:53:13 +00001035 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattnerf26a8ee2003-07-23 15:30:06 +00001036 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001037 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1038 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner57698e22002-03-26 18:01:55 +00001039 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001040 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Chris Lattner7db79582001-11-07 04:21:57 +00001041 default: Out << "<unknown value type: " << V->getValueType() << ">"; break;
1042 }
1043 return *this;
1044}