blob: 533274a6d837cca21727697ce05c35fd44a52324 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
John Criswellb576c942003-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 Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerda1fbcc2001-11-07 04:21:57 +000017#include "llvm/Assembly/CachedWriter.h"
Chris Lattner75cf7cf2002-04-08 22:03:40 +000018#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000020#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000023#include "llvm/Instruction.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include "llvm/iMemory.h"
Chris Lattnere02fa852001-10-13 06:42:36 +000025#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
27#include "llvm/iOther.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000028#include "llvm/Module.h"
Chris Lattner007377f2001-09-07 16:36:04 +000029#include "llvm/SymbolTable.h"
Misha Brukman5cf1acf2004-04-28 15:31:21 +000030#include "llvm/Analysis/SlotCalculator.h"
31#include "llvm/Assembly/Writer.h"
Chris Lattner061269b2002-10-02 19:38:55 +000032#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000033#include "Support/StringExtras.h"
34#include "Support/STLExtras.h"
Chris Lattner007377f2001-09-07 16:36:04 +000035#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattnera6275cc2002-07-26 21:12:46 +000038static RegisterPass<PrintModulePass>
39X("printm", "Print module to stderr",PassInfo::Analysis|PassInfo::Optimization);
40static RegisterPass<PrintFunctionPass>
41Y("print","Print function to stderr",PassInfo::Analysis|PassInfo::Optimization);
Chris Lattnerf082b802002-07-23 18:07:49 +000042
Chris Lattner7b13f562003-05-08 02:08:14 +000043static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
44 bool PrintName,
45 std::map<const Type *, std::string> &TypeTable,
Chris Lattner7a716ad2002-04-16 21:36:08 +000046 SlotCalculator *Table);
47
Chris Lattner207b5bc2001-10-29 16:37:48 +000048static const Module *getModuleFromVal(const Value *V) {
Chris Lattner949a3622003-07-23 15:30:06 +000049 if (const Argument *MA = dyn_cast<Argument>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000050 return MA->getParent() ? MA->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +000051 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000052 return BB->getParent() ? BB->getParent()->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +000053 else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner79df7c02002-03-26 18:01:55 +000054 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
Chris Lattner207b5bc2001-10-29 16:37:48 +000055 return M ? M->getParent() : 0;
Chris Lattner949a3622003-07-23 15:30:06 +000056 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
Chris Lattner207b5bc2001-10-29 16:37:48 +000057 return GV->getParent();
Chris Lattner207b5bc2001-10-29 16:37:48 +000058 return 0;
59}
60
Chris Lattnerc1824992001-10-29 16:05:51 +000061static SlotCalculator *createSlotCalculator(const Value *V) {
62 assert(!isa<Type>(V) && "Can't create an SC for a type!");
Chris Lattner949a3622003-07-23 15:30:06 +000063 if (const Argument *FA = dyn_cast<Argument>(V)) {
Chris Lattner8ce75012004-01-14 02:49:34 +000064 return new SlotCalculator(FA->getParent(), false);
Chris Lattner949a3622003-07-23 15:30:06 +000065 } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner8ce75012004-01-14 02:49:34 +000066 return new SlotCalculator(I->getParent()->getParent(), false);
Chris Lattner949a3622003-07-23 15:30:06 +000067 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Chris Lattner8ce75012004-01-14 02:49:34 +000068 return new SlotCalculator(BB->getParent(), false);
Chris Lattner949a3622003-07-23 15:30:06 +000069 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
Chris Lattner8ce75012004-01-14 02:49:34 +000070 return new SlotCalculator(GV->getParent(), false);
Chris Lattner949a3622003-07-23 15:30:06 +000071 } else if (const Function *Func = dyn_cast<Function>(V)) {
Chris Lattner8ce75012004-01-14 02:49:34 +000072 return new SlotCalculator(Func, false);
Chris Lattnerc1824992001-10-29 16:05:51 +000073 }
74 return 0;
75}
Chris Lattner00950542001-06-06 20:29:01 +000076
Chris Lattner24b8a5d2003-08-22 05:40:38 +000077// getLLVMName - Turn the specified string into an 'LLVM name', which is either
78// prefixed with % (if the string only contains simple characters) or is
79// surrounded with ""'s (if it has special chars in it).
80static std::string getLLVMName(const std::string &Name) {
81 assert(!Name.empty() && "Cannot get empty name!");
82
83 // First character cannot start with a number...
84 if (Name[0] >= '0' && Name[0] <= '9')
85 return "\"" + Name + "\"";
86
87 // Scan to see if we have any characters that are not on the "white list"
88 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
89 char C = Name[i];
90 assert(C != '"' && "Illegal character in LLVM value name!");
91 if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
92 C != '-' && C != '.' && C != '_')
93 return "\"" + Name + "\"";
94 }
95
96 // If we get here, then the identifier is legal to use as a "VarID".
97 return "%"+Name;
98}
99
Chris Lattner207b5bc2001-10-29 16:37:48 +0000100
Misha Brukmanab5c6002004-03-02 00:22:19 +0000101/// fillTypeNameTable - If the module has a symbol table, take all global types
102/// and stuff their names into the TypeNames map.
103///
Chris Lattner207b5bc2001-10-29 16:37:48 +0000104static void fillTypeNameTable(const Module *M,
Chris Lattner7b13f562003-05-08 02:08:14 +0000105 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000106 if (!M) return;
107 const SymbolTable &ST = M->getSymbolTable();
108 SymbolTable::const_iterator PI = ST.find(Type::TypeTy);
109 if (PI != ST.end()) {
110 SymbolTable::type_const_iterator I = PI->second.begin();
111 for (; I != PI->second.end(); ++I) {
112 // As a heuristic, don't insert pointer to primitive types, because
113 // they are used too often to have a single useful name.
114 //
Chris Lattner949a3622003-07-23 15:30:06 +0000115 const Type *Ty = cast<Type>(I->second);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000116 if (!isa<PointerType>(Ty) ||
Chris Lattner88c17382003-10-30 00:22:33 +0000117 !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
118 isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000119 TypeNames.insert(std::make_pair(Ty, getLLVMName(I->first)));
Chris Lattner207b5bc2001-10-29 16:37:48 +0000120 }
121 }
122}
123
124
125
Chris Lattner7b13f562003-05-08 02:08:14 +0000126static std::string calcTypeName(const Type *Ty,
127 std::vector<const Type *> &TypeStack,
128 std::map<const Type *, std::string> &TypeNames){
Chris Lattner88c17382003-10-30 00:22:33 +0000129 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
130 return Ty->getDescription(); // Base case
Chris Lattner207b5bc2001-10-29 16:37:48 +0000131
132 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000133 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000134 if (I != TypeNames.end()) return I->second;
135
Chris Lattner88c17382003-10-30 00:22:33 +0000136 if (isa<OpaqueType>(Ty))
137 return "opaque";
138
Chris Lattner207b5bc2001-10-29 16:37:48 +0000139 // Check to see if the Type is already on the stack...
140 unsigned Slot = 0, CurSize = TypeStack.size();
141 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
142
143 // This is another base case for the recursion. In this case, we know
144 // that we have looped back to a type that we have previously visited.
145 // Generate the appropriate upreference to handle this.
Chris Lattner207b5bc2001-10-29 16:37:48 +0000146 if (Slot < CurSize)
147 return "\\" + utostr(CurSize-Slot); // Here's the upreference
148
149 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
150
Chris Lattner7b13f562003-05-08 02:08:14 +0000151 std::string Result;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000152 switch (Ty->getPrimitiveID()) {
Chris Lattner6bfd6a52002-03-29 03:44:36 +0000153 case Type::FunctionTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000154 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000155 Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000156 for (FunctionType::param_iterator I = FTy->param_begin(),
157 E = FTy->param_end(); I != E; ++I) {
158 if (I != FTy->param_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000159 Result += ", ";
160 Result += calcTypeName(*I, TypeStack, TypeNames);
161 }
Chris Lattner2761e9f2002-04-13 20:53:41 +0000162 if (FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000163 if (FTy->getNumParams()) Result += ", ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000164 Result += "...";
165 }
166 Result += ")";
167 break;
168 }
169 case Type::StructTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000170 const StructType *STy = cast<StructType>(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000171 Result = "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000172 for (StructType::element_iterator I = STy->element_begin(),
173 E = STy->element_end(); I != E; ++I) {
174 if (I != STy->element_begin())
Chris Lattner207b5bc2001-10-29 16:37:48 +0000175 Result += ", ";
176 Result += calcTypeName(*I, TypeStack, TypeNames);
177 }
178 Result += " }";
179 break;
180 }
181 case Type::PointerTyID:
Chris Lattner949a3622003-07-23 15:30:06 +0000182 Result = calcTypeName(cast<PointerType>(Ty)->getElementType(),
Chris Lattner02b93992002-04-12 18:21:53 +0000183 TypeStack, TypeNames) + "*";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000184 break;
185 case Type::ArrayTyID: {
Chris Lattner949a3622003-07-23 15:30:06 +0000186 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattnerff5c2962002-04-13 21:11:04 +0000187 Result = "[" + utostr(ATy->getNumElements()) + " x ";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000188 Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]";
189 break;
190 }
Chris Lattner9e094c42003-05-14 17:50:47 +0000191 case Type::OpaqueTyID:
192 Result = "opaque";
193 break;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000194 default:
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000195 Result = "<unrecognized-type>";
Chris Lattner207b5bc2001-10-29 16:37:48 +0000196 }
197
198 TypeStack.pop_back(); // Remove self from stack...
199 return Result;
200}
201
202
Misha Brukman9d0802e2004-03-01 19:48:13 +0000203/// printTypeInt - The internal guts of printing out a type that has a
204/// potentially named portion.
205///
Chris Lattner7b13f562003-05-08 02:08:14 +0000206static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty,
207 std::map<const Type *, std::string> &TypeNames) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000208 // Primitive types always print out their description, regardless of whether
209 // they have been named or not.
210 //
Chris Lattnerdaf2a492003-10-30 00:12:51 +0000211 if (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))
212 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000213
214 // Check to see if the type is named.
Chris Lattner7b13f562003-05-08 02:08:14 +0000215 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000216 if (I != TypeNames.end()) return Out << I->second;
217
218 // Otherwise we have a type that has not been named but is a derived type.
219 // Carefully recurse the type hierarchy to print out any contained symbolic
220 // names.
221 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000222 std::vector<const Type *> TypeStack;
223 std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames);
Chris Lattner697954c2002-01-20 22:54:45 +0000224 TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
Chris Lattner207b5bc2001-10-29 16:37:48 +0000225 return Out << TypeName;
226}
227
Chris Lattnere51e03b2001-10-31 04:33:19 +0000228
Misha Brukman9d0802e2004-03-01 19:48:13 +0000229/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
230/// type, iff there is an entry in the modules symbol table for the specified
231/// type or one of it's component types. This is slower than a simple x << Type
232///
Chris Lattner31f84992003-11-21 20:23:48 +0000233std::ostream &llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
234 const Module *M) {
Chris Lattner207b5bc2001-10-29 16:37:48 +0000235 Out << " ";
236
237 // If they want us to print out a type, attempt to make it symbolic if there
238 // is a symbol table in the module...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000239 if (M) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000240 std::map<const Type *, std::string> TypeNames;
Chris Lattner207b5bc2001-10-29 16:37:48 +0000241 fillTypeNameTable(M, TypeNames);
242
Chris Lattner7b8660d2001-10-29 16:40:32 +0000243 return printTypeInt(Out, Ty, TypeNames);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000244 } else {
Chris Lattner7b8660d2001-10-29 16:40:32 +0000245 return Out << Ty->getDescription();
Chris Lattner207b5bc2001-10-29 16:37:48 +0000246 }
247}
248
Chris Lattner7b13f562003-05-08 02:08:14 +0000249static void WriteConstantInt(std::ostream &Out, const Constant *CV,
250 bool PrintName,
251 std::map<const Type *, std::string> &TypeTable,
Chris Lattner7a716ad2002-04-16 21:36:08 +0000252 SlotCalculator *Table) {
Chris Lattner66e810b2002-04-18 18:53:13 +0000253 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
254 Out << (CB == ConstantBool::True ? "true" : "false");
255 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) {
256 Out << CI->getValue();
257 } else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) {
258 Out << CI->getValue();
259 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
260 // We would like to output the FP constant value in exponential notation,
261 // but we cannot do this if doing so will lose precision. Check here to
262 // make sure that we only output it in exponential format if we can parse
263 // the value back and get the same value.
264 //
265 std::string StrVal = ftostr(CFP->getValue());
266
267 // Check to make sure that the stringized number is not some string like
268 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
269 // the string matches the "[-+]?[0-9]" regex.
270 //
271 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
272 ((StrVal[0] == '-' || StrVal[0] == '+') &&
Brian Gaekeb471a232003-06-17 23:55:35 +0000273 (StrVal[1] >= '0' && StrVal[1] <= '9')))
Chris Lattner66e810b2002-04-18 18:53:13 +0000274 // Reparse stringized version!
275 if (atof(StrVal.c_str()) == CFP->getValue()) {
276 Out << StrVal; return;
277 }
278
279 // Otherwise we could not reparse it to exactly the same value, so we must
280 // output the string in hexadecimal format!
281 //
282 // Behave nicely in the face of C TBAA rules... see:
283 // http://www.nullstone.com/htmls/category/aliastyp.htm
284 //
285 double Val = CFP->getValue();
286 char *Ptr = (char*)&Val;
287 assert(sizeof(double) == sizeof(uint64_t) && sizeof(double) == 8 &&
288 "assuming that double is 64 bits!");
289 Out << "0x" << utohexstr(*(uint64_t*)Ptr);
290
Chris Lattnerde512b52004-02-15 05:55:15 +0000291 } else if (isa<ConstantAggregateZero>(CV)) {
292 Out << "zeroinitializer";
Chris Lattner66e810b2002-04-18 18:53:13 +0000293 } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
294 // As a special case, print the array as a string if it is an array of
295 // ubytes or an array of sbytes with positive values.
296 //
297 const Type *ETy = CA->getType()->getElementType();
298 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
299
300 if (ETy == Type::SByteTy)
301 for (unsigned i = 0; i < CA->getNumOperands(); ++i)
302 if (cast<ConstantSInt>(CA->getOperand(i))->getValue() < 0) {
303 isString = false;
304 break;
305 }
306
307 if (isString) {
308 Out << "c\"";
309 for (unsigned i = 0; i < CA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000310 unsigned char C = cast<ConstantInt>(CA->getOperand(i))->getRawValue();
Chris Lattner66e810b2002-04-18 18:53:13 +0000311
Chris Lattnerfc944462002-07-31 23:56:44 +0000312 if (isprint(C) && C != '"' && C != '\\') {
Chris Lattner66e810b2002-04-18 18:53:13 +0000313 Out << C;
314 } else {
315 Out << '\\'
316 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
317 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
318 }
319 }
320 Out << "\"";
321
322 } else { // Cannot output in string format...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000323 Out << "[";
324 if (CA->getNumOperands()) {
325 Out << " ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000326 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000327 WriteAsOperandInternal(Out, CA->getOperand(0),
328 PrintName, TypeTable, Table);
329 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
330 Out << ", ";
Chris Lattner66e810b2002-04-18 18:53:13 +0000331 printTypeInt(Out, ETy, TypeTable);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000332 WriteAsOperandInternal(Out, CA->getOperand(i), PrintName,
333 TypeTable, Table);
334 }
335 }
336 Out << " ]";
337 }
338 } else if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
339 Out << "{";
340 if (CS->getNumOperands()) {
341 Out << " ";
342 printTypeInt(Out, CS->getOperand(0)->getType(), TypeTable);
343
344 WriteAsOperandInternal(Out, CS->getOperand(0),
345 PrintName, TypeTable, Table);
346
347 for (unsigned i = 1; i < CS->getNumOperands(); i++) {
348 Out << ", ";
349 printTypeInt(Out, CS->getOperand(i)->getType(), TypeTable);
350
351 WriteAsOperandInternal(Out, CS->getOperand(i),
352 PrintName, TypeTable, Table);
353 }
354 }
355
356 Out << " }";
357 } else if (isa<ConstantPointerNull>(CV)) {
358 Out << "null";
359
Chris Lattner7e708292002-06-25 16:13:24 +0000360 } else if (const ConstantPointerRef *PR = dyn_cast<ConstantPointerRef>(CV)) {
Chris Lattner2ff95b62004-01-18 21:03:06 +0000361 WriteAsOperandInternal(Out, PR->getValue(), true, TypeTable, Table);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000362
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000363 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Chris Lattner38d87732003-03-06 23:23:32 +0000364 Out << CE->getOpcodeName() << " (";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000365
366 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
367 printTypeInt(Out, (*OI)->getType(), TypeTable);
368 WriteAsOperandInternal(Out, *OI, PrintName, TypeTable, Table);
369 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +0000370 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000371 }
372
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000373 if (CE->getOpcode() == Instruction::Cast) {
Chris Lattner95586b82002-08-15 19:37:43 +0000374 Out << " to ";
375 printTypeInt(Out, CE->getType(), TypeTable);
376 }
Chris Lattner1c93e5b2002-08-16 21:17:11 +0000377 Out << ")";
Chris Lattner95586b82002-08-15 19:37:43 +0000378
Chris Lattner7a716ad2002-04-16 21:36:08 +0000379 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000380 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000381 }
382}
383
384
Misha Brukmanab5c6002004-03-02 00:22:19 +0000385/// WriteAsOperand - Write the name of the specified value out to the specified
386/// ostream. This can be useful when you just want to print int %reg126, not
387/// the whole instruction that generated it.
388///
Chris Lattner7b13f562003-05-08 02:08:14 +0000389static void WriteAsOperandInternal(std::ostream &Out, const Value *V,
390 bool PrintName,
391 std::map<const Type*, std::string> &TypeTable,
Chris Lattner7a716ad2002-04-16 21:36:08 +0000392 SlotCalculator *Table) {
393 Out << " ";
394 if (PrintName && V->hasName()) {
Chris Lattner24b8a5d2003-08-22 05:40:38 +0000395 Out << getLLVMName(V->getName());
Chris Lattner7a716ad2002-04-16 21:36:08 +0000396 } else {
Chris Lattner949a3622003-07-23 15:30:06 +0000397 if (const Constant *CV = dyn_cast<Constant>(V)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000398 WriteConstantInt(Out, CV, PrintName, TypeTable, Table);
399 } else {
400 int Slot;
401 if (Table) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000402 Slot = Table->getSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000403 } else {
Chris Lattner949a3622003-07-23 15:30:06 +0000404 if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +0000405 Out << Ty->getDescription();
406 return;
407 }
408
409 Table = createSlotCalculator(V);
410 if (Table == 0) { Out << "BAD VALUE TYPE!"; return; }
411
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000412 Slot = Table->getSlot(V);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000413 delete Table;
414 }
415 if (Slot >= 0) Out << "%" << Slot;
416 else if (PrintName)
Misha Brukman9d0802e2004-03-01 19:48:13 +0000417 if (V->hasName())
418 Out << "<badref: " << getLLVMName(V->getName()) << ">";
419 else
420 Out << "<badref>"; // Not embedded into a location?
Chris Lattner7a716ad2002-04-16 21:36:08 +0000421 }
422 }
423}
424
425
Misha Brukman9d0802e2004-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 Lattner31f84992003-11-21 20:23:48 +0000430std::ostream &llvm::WriteAsOperand(std::ostream &Out, const Value *V,
Misha Brukman9d0802e2004-03-01 19:48:13 +0000431 bool PrintType, bool PrintName,
432 const Module *Context) {
Chris Lattner7b13f562003-05-08 02:08:14 +0000433 std::map<const Type *, std::string> TypeNames;
Chris Lattner607dc682002-07-10 16:48:17 +0000434 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +0000435
Chris Lattner6e6026b2002-11-20 18:36:02 +0000436 if (Context)
Chris Lattner607dc682002-07-10 16:48:17 +0000437 fillTypeNameTable(Context, TypeNames);
Chris Lattner7a716ad2002-04-16 21:36:08 +0000438
439 if (PrintType)
440 printTypeInt(Out, V->getType(), TypeNames);
441
Brian Gaekecd4a3982003-11-16 23:08:27 +0000442 if (const Type *Ty = dyn_cast<Type> (V))
443 printTypeInt(Out, Ty, TypeNames);
444
Chris Lattner607dc682002-07-10 16:48:17 +0000445 WriteAsOperandInternal(Out, V, PrintName, TypeNames, 0);
Chris Lattner622f7402001-07-20 19:15:21 +0000446 return Out;
447}
448
Chris Lattner31f84992003-11-21 20:23:48 +0000449namespace llvm {
Chris Lattnerd8c2e422001-07-12 23:35:26 +0000450
Chris Lattner007377f2001-09-07 16:36:04 +0000451class AssemblyWriter {
Misha Brukmane5242de2004-04-28 19:24:28 +0000452 std::ostream *Out;
Chris Lattner00950542001-06-06 20:29:01 +0000453 SlotCalculator &Table;
Chris Lattnerc1824992001-10-29 16:05:51 +0000454 const Module *TheModule;
Chris Lattner7b13f562003-05-08 02:08:14 +0000455 std::map<const Type *, std::string> TypeNames;
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000456 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner00950542001-06-06 20:29:01 +0000457public:
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000458 inline AssemblyWriter(std::ostream &o, SlotCalculator &Tab, const Module *M,
459 AssemblyAnnotationWriter *AAW)
Misha Brukmane5242de2004-04-28 19:24:28 +0000460 : Out(&o), Table(Tab), TheModule(M), AnnotationWriter(AAW) {
Chris Lattnerc1824992001-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 Lattner207b5bc2001-10-29 16:37:48 +0000465 fillTypeNameTable(M, TypeNames);
Chris Lattner00950542001-06-06 20:29:01 +0000466 }
467
Chris Lattnerc1824992001-10-29 16:05:51 +0000468 inline void write(const Module *M) { printModule(M); }
469 inline void write(const GlobalVariable *G) { printGlobal(G); }
Chris Lattner79df7c02002-03-26 18:01:55 +0000470 inline void write(const Function *F) { printFunction(F); }
Chris Lattnerc1824992001-10-29 16:05:51 +0000471 inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
Chris Lattner7e708292002-06-25 16:13:24 +0000472 inline void write(const Instruction *I) { printInstruction(*I); }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000473 inline void write(const Constant *CPV) { printConstant(CPV); }
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000474 inline void write(const Type *Ty) { printType(Ty); }
Chris Lattner00950542001-06-06 20:29:01 +0000475
Chris Lattner66e810b2002-04-18 18:53:13 +0000476 void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
477
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000478 const Module* getModule() { return TheModule; }
Misha Brukmane5242de2004-04-28 19:24:28 +0000479 void setStream(std::ostream &os) { Out = &os; }
Misha Brukman5cf1acf2004-04-28 15:31:21 +0000480
Chris Lattner00950542001-06-06 20:29:01 +0000481private :
Chris Lattnerc1824992001-10-29 16:05:51 +0000482 void printModule(const Module *M);
483 void printSymbolTable(const SymbolTable &ST);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000484 void printConstant(const Constant *CPV);
Chris Lattnerc1824992001-10-29 16:05:51 +0000485 void printGlobal(const GlobalVariable *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +0000486 void printFunction(const Function *F);
Chris Lattner73e21422002-04-09 19:48:49 +0000487 void printArgument(const Argument *FA);
Chris Lattnerc1824992001-10-29 16:05:51 +0000488 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +0000489 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000490
491 // printType - Go to extreme measures to attempt to print out a short,
492 // symbolic version of a type name.
493 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000494 std::ostream &printType(const Type *Ty) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000495 return printTypeInt(*Out, Ty, TypeNames);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000496 }
497
498 // printTypeAtLeastOneLevel - Print out one level of the possibly complex type
499 // without considering any symbolic types that we may have equal to it.
500 //
Chris Lattner7b13f562003-05-08 02:08:14 +0000501 std::ostream &printTypeAtLeastOneLevel(const Type *Ty);
Chris Lattnerc1824992001-10-29 16:05:51 +0000502
Chris Lattnere02fa852001-10-13 06:42:36 +0000503 // printInfoComment - Print a little comment after the instruction indicating
504 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +0000505 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +0000506};
Chris Lattner31f84992003-11-21 20:23:48 +0000507} // end of anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +0000508
Misha Brukmanab5c6002004-03-02 00:22:19 +0000509/// printTypeAtLeastOneLevel - Print out one level of the possibly complex type
510/// without considering any symbolic types that we may have equal to it.
511///
Chris Lattner7b13f562003-05-08 02:08:14 +0000512std::ostream &AssemblyWriter::printTypeAtLeastOneLevel(const Type *Ty) {
Chris Lattner7e708292002-06-25 16:13:24 +0000513 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000514 printType(FTy->getReturnType()) << " (";
Chris Lattnerd5d89962004-02-09 04:14:01 +0000515 for (FunctionType::param_iterator I = FTy->param_begin(),
516 E = FTy->param_end(); I != E; ++I) {
517 if (I != FTy->param_begin())
Misha Brukmane5242de2004-04-28 19:24:28 +0000518 *Out << ", ";
Chris Lattner7a716ad2002-04-16 21:36:08 +0000519 printType(*I);
Chris Lattner2761e9f2002-04-13 20:53:41 +0000520 }
521 if (FTy->isVarArg()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000522 if (FTy->getNumParams()) *Out << ", ";
523 *Out << "...";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000524 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000525 *Out << ")";
Chris Lattner7e708292002-06-25 16:13:24 +0000526 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000527 *Out << "{ ";
Chris Lattnerd21cd802004-02-09 04:37:31 +0000528 for (StructType::element_iterator I = STy->element_begin(),
529 E = STy->element_end(); I != E; ++I) {
530 if (I != STy->element_begin())
Misha Brukmane5242de2004-04-28 19:24:28 +0000531 *Out << ", ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000532 printType(*I);
533 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000534 *Out << " }";
Chris Lattner7e708292002-06-25 16:13:24 +0000535 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
Chris Lattner2761e9f2002-04-13 20:53:41 +0000536 printType(PTy->getElementType()) << "*";
Chris Lattner7e708292002-06-25 16:13:24 +0000537 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000538 *Out << "[" << ATy->getNumElements() << " x ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000539 printType(ATy->getElementType()) << "]";
Chris Lattner7e708292002-06-25 16:13:24 +0000540 } else if (const OpaqueType *OTy = dyn_cast<OpaqueType>(Ty)) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000541 *Out << "opaque";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000542 } else {
Vikram S. Adveb4dbb442002-07-14 23:14:45 +0000543 if (!Ty->isPrimitiveType())
Misha Brukmane5242de2004-04-28 19:24:28 +0000544 *Out << "<unknown derived type>";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000545 printType(Ty);
546 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000547 return *Out;
Chris Lattner2761e9f2002-04-13 20:53:41 +0000548}
549
550
Chris Lattner007377f2001-09-07 16:36:04 +0000551void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType,
552 bool PrintName) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000553 if (PrintType) { *Out << " "; printType(Operand->getType()); }
554 WriteAsOperandInternal(*Out, Operand, PrintName, TypeNames, &Table);
Chris Lattner00950542001-06-06 20:29:01 +0000555}
556
Chris Lattner00950542001-06-06 20:29:01 +0000557
Chris Lattnerc1824992001-10-29 16:05:51 +0000558void AssemblyWriter::printModule(const Module *M) {
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000559 switch (M->getEndianness()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000560 case Module::LittleEndian: *Out << "target endian = little\n"; break;
561 case Module::BigEndian: *Out << "target endian = big\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000562 case Module::AnyEndianness: break;
563 }
564 switch (M->getPointerSize()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000565 case Module::Pointer32: *Out << "target pointersize = 32\n"; break;
566 case Module::Pointer64: *Out << "target pointersize = 64\n"; break;
Chris Lattnereb5d3a12003-08-24 13:48:48 +0000567 case Module::AnyPointerSize: break;
568 }
569
Chris Lattner007377f2001-09-07 16:36:04 +0000570 // Loop over the symbol table, emitting all named constants...
Chris Lattner6e6026b2002-11-20 18:36:02 +0000571 printSymbolTable(M->getSymbolTable());
Chris Lattner70cc3392001-09-10 07:58:01 +0000572
Chris Lattner7e708292002-06-25 16:13:24 +0000573 for (Module::const_giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
574 printGlobal(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000575
Misha Brukmane5242de2004-04-28 19:24:28 +0000576 *Out << "\nimplementation ; Functions:\n";
Vikram S. Adve5efa3cc2001-09-18 12:48:16 +0000577
Chris Lattnerb5794002002-04-07 22:49:37 +0000578 // Output all of the functions...
Chris Lattner7e708292002-06-25 16:13:24 +0000579 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
580 printFunction(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000581}
582
Chris Lattnerc1824992001-10-29 16:05:51 +0000583void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000584 if (GV->hasName()) *Out << getLLVMName(GV->getName()) << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +0000585
Chris Lattner4ad02e72003-04-16 20:28:45 +0000586 if (!GV->hasInitializer())
Misha Brukmane5242de2004-04-28 19:24:28 +0000587 *Out << "external ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000588 else
589 switch (GV->getLinkage()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000590 case GlobalValue::InternalLinkage: *Out << "internal "; break;
591 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
592 case GlobalValue::WeakLinkage: *Out << "weak "; break;
593 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000594 case GlobalValue::ExternalLinkage: break;
595 }
Chris Lattnerd70684f2001-09-18 04:01:05 +0000596
Misha Brukmane5242de2004-04-28 19:24:28 +0000597 *Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner7a176752001-12-04 00:03:30 +0000598 printType(GV->getType()->getElementType());
Chris Lattnerd70684f2001-09-18 04:01:05 +0000599
600 if (GV->hasInitializer())
601 writeOperand(GV->getInitializer(), false, false);
602
Chris Lattner7e708292002-06-25 16:13:24 +0000603 printInfoComment(*GV);
Misha Brukmane5242de2004-04-28 19:24:28 +0000604 *Out << "\n";
Chris Lattner70cc3392001-09-10 07:58:01 +0000605}
606
Chris Lattner007377f2001-09-07 16:36:04 +0000607
Misha Brukmanab5c6002004-03-02 00:22:19 +0000608/// printSymbolTable - Run through symbol table looking for named constants
609/// if a named constant is found, emit it's declaration...
610///
Chris Lattnerc1824992001-10-29 16:05:51 +0000611void AssemblyWriter::printSymbolTable(const SymbolTable &ST) {
Chris Lattner007377f2001-09-07 16:36:04 +0000612 for (SymbolTable::const_iterator TI = ST.begin(); TI != ST.end(); ++TI) {
613 SymbolTable::type_const_iterator I = ST.type_begin(TI->first);
614 SymbolTable::type_const_iterator End = ST.type_end(TI->first);
615
616 for (; I != End; ++I) {
617 const Value *V = I->second;
Chris Lattner949a3622003-07-23 15:30:06 +0000618 if (const Constant *CPV = dyn_cast<Constant>(V)) {
Chris Lattnerc1824992001-10-29 16:05:51 +0000619 printConstant(CPV);
Chris Lattner949a3622003-07-23 15:30:06 +0000620 } else if (const Type *Ty = dyn_cast<Type>(V)) {
Chris Lattner54baafd2003-11-09 15:51:07 +0000621 assert(Ty->getType() == Type::TypeTy && TI->first == Type::TypeTy);
Misha Brukmane5242de2004-04-28 19:24:28 +0000622 *Out << "\t" << getLLVMName(I->first) << " = type ";
Chris Lattner2761e9f2002-04-13 20:53:41 +0000623
624 // Make sure we print out at least one level of the type structure, so
625 // that we do not get %FILE = type %FILE
626 //
627 printTypeAtLeastOneLevel(Ty) << "\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000628 }
629 }
Chris Lattner739a56d2001-07-15 06:35:59 +0000630 }
Chris Lattner00950542001-06-06 20:29:01 +0000631}
632
633
Misha Brukmanab5c6002004-03-02 00:22:19 +0000634/// printConstant - Print out a constant pool entry...
635///
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000636void AssemblyWriter::printConstant(const Constant *CPV) {
Chris Lattner007377f2001-09-07 16:36:04 +0000637 // Don't print out unnamed constants, they will be inlined
638 if (!CPV->hasName()) return;
Chris Lattner00950542001-06-06 20:29:01 +0000639
Chris Lattner1333ed52001-07-26 16:29:38 +0000640 // Print out name...
Misha Brukmane5242de2004-04-28 19:24:28 +0000641 *Out << "\t" << getLLVMName(CPV->getName()) << " =";
Chris Lattner00950542001-06-06 20:29:01 +0000642
643 // Write the value out now...
Chris Lattner7a716ad2002-04-16 21:36:08 +0000644 writeOperand(CPV, true, false);
Chris Lattner00950542001-06-06 20:29:01 +0000645
Chris Lattner7e708292002-06-25 16:13:24 +0000646 printInfoComment(*CPV);
Misha Brukmane5242de2004-04-28 19:24:28 +0000647 *Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000648}
649
Misha Brukmanab5c6002004-03-02 00:22:19 +0000650/// printFunction - Print all aspects of a function.
651///
Chris Lattner7e708292002-06-25 16:13:24 +0000652void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000653 // Print out the return type and name...
Misha Brukmane5242de2004-04-28 19:24:28 +0000654 *Out << "\n";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000655
Misha Brukmane5242de2004-04-28 19:24:28 +0000656 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, *Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000657
Chris Lattner4ad02e72003-04-16 20:28:45 +0000658 if (F->isExternal())
Misha Brukmane5242de2004-04-28 19:24:28 +0000659 *Out << "declare ";
Chris Lattner4ad02e72003-04-16 20:28:45 +0000660 else
661 switch (F->getLinkage()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000662 case GlobalValue::InternalLinkage: *Out << "internal "; break;
663 case GlobalValue::LinkOnceLinkage: *Out << "linkonce "; break;
664 case GlobalValue::WeakLinkage: *Out << "weak "; break;
665 case GlobalValue::AppendingLinkage: *Out << "appending "; break;
Chris Lattner4ad02e72003-04-16 20:28:45 +0000666 case GlobalValue::ExternalLinkage: break;
667 }
668
Chris Lattnerb8565e32003-09-03 17:56:43 +0000669 printType(F->getReturnType()) << " ";
Chris Lattner4d45bd02003-10-18 05:57:43 +0000670 if (!F->getName().empty())
Misha Brukmane5242de2004-04-28 19:24:28 +0000671 *Out << getLLVMName(F->getName());
Chris Lattner4d45bd02003-10-18 05:57:43 +0000672 else
Misha Brukmane5242de2004-04-28 19:24:28 +0000673 *Out << "\"\"";
674 *Out << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000675 Table.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +0000676
Chris Lattnerc1824992001-10-29 16:05:51 +0000677 // Loop over the arguments, printing them...
Chris Lattner7e708292002-06-25 16:13:24 +0000678 const FunctionType *FT = F->getFunctionType();
Chris Lattner007377f2001-09-07 16:36:04 +0000679
Chris Lattner69da5cf2002-10-13 20:57:00 +0000680 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
681 printArgument(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000682
683 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +0000684 if (FT->isVarArg()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000685 if (FT->getNumParams()) *Out << ", ";
686 *Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +0000687 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000688 *Out << ")";
Chris Lattner007377f2001-09-07 16:36:04 +0000689
Chris Lattner7e708292002-06-25 16:13:24 +0000690 if (F->isExternal()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000691 *Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +0000692 } else {
Misha Brukmane5242de2004-04-28 19:24:28 +0000693 *Out << " {";
Chris Lattner007377f2001-09-07 16:36:04 +0000694
Chris Lattnerb5794002002-04-07 22:49:37 +0000695 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +0000696 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
697 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +0000698
Misha Brukmane5242de2004-04-28 19:24:28 +0000699 *Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +0000700 }
701
Chris Lattnerb5794002002-04-07 22:49:37 +0000702 Table.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000703}
704
Misha Brukmanab5c6002004-03-02 00:22:19 +0000705/// printArgument - This member is called for every argument that is passed into
706/// the function. Simply print it out
707///
Chris Lattner73e21422002-04-09 19:48:49 +0000708void AssemblyWriter::printArgument(const Argument *Arg) {
Chris Lattner00950542001-06-06 20:29:01 +0000709 // Insert commas as we go... the first arg doesn't get a comma
Misha Brukmane5242de2004-04-28 19:24:28 +0000710 if (Arg != &Arg->getParent()->afront()) *Out << ", ";
Chris Lattner00950542001-06-06 20:29:01 +0000711
712 // Output type...
Chris Lattnerc1824992001-10-29 16:05:51 +0000713 printType(Arg->getType());
Chris Lattner00950542001-06-06 20:29:01 +0000714
715 // Output name, if available...
716 if (Arg->hasName())
Misha Brukmane5242de2004-04-28 19:24:28 +0000717 *Out << " " << getLLVMName(Arg->getName());
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000718 else if (Table.getSlot(Arg) < 0)
Misha Brukmane5242de2004-04-28 19:24:28 +0000719 *Out << "<badref>";
Chris Lattner00950542001-06-06 20:29:01 +0000720}
721
Misha Brukmanab5c6002004-03-02 00:22:19 +0000722/// printBasicBlock - This member is called for each basic block in a method.
723///
Chris Lattnerc1824992001-10-29 16:05:51 +0000724void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Chris Lattner00950542001-06-06 20:29:01 +0000725 if (BB->hasName()) { // Print out the label if it exists...
Misha Brukmane5242de2004-04-28 19:24:28 +0000726 *Out << "\n" << BB->getName() << ":";
Chris Lattnerafc38682002-05-14 16:02:05 +0000727 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000728 int Slot = Table.getSlot(BB);
Misha Brukmane5242de2004-04-28 19:24:28 +0000729 *Out << "\n; <label>:";
Chris Lattner00950542001-06-06 20:29:01 +0000730 if (Slot >= 0)
Misha Brukmane5242de2004-04-28 19:24:28 +0000731 *Out << Slot; // Extra newline separates out label's
Chris Lattner00950542001-06-06 20:29:01 +0000732 else
Misha Brukmane5242de2004-04-28 19:24:28 +0000733 *Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +0000734 }
Chris Lattner4e4d8622003-11-20 00:09:43 +0000735
736 if (BB->getParent() == 0)
Misha Brukmane5242de2004-04-28 19:24:28 +0000737 *Out << "\t\t; Error: Block without parent!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000738 else {
739 if (BB != &BB->getParent()->front()) { // Not the entry block?
740 // Output predecessors for the block...
Misha Brukmane5242de2004-04-28 19:24:28 +0000741 *Out << "\t\t;";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000742 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
743
744 if (PI == PE) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000745 *Out << " No predecessors!";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000746 } else {
Misha Brukmane5242de2004-04-28 19:24:28 +0000747 *Out << " preds =";
Chris Lattner40efcec2003-11-16 22:59:57 +0000748 writeOperand(*PI, false, true);
Chris Lattner4e4d8622003-11-20 00:09:43 +0000749 for (++PI; PI != PE; ++PI) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000750 *Out << ",";
Chris Lattner4e4d8622003-11-20 00:09:43 +0000751 writeOperand(*PI, false, true);
752 }
Chris Lattner40efcec2003-11-16 22:59:57 +0000753 }
Chris Lattner061269b2002-10-02 19:38:55 +0000754 }
Chris Lattner00950542001-06-06 20:29:01 +0000755 }
Chris Lattnerafc38682002-05-14 16:02:05 +0000756
Misha Brukmane5242de2004-04-28 19:24:28 +0000757 *Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000758
Misha Brukmane5242de2004-04-28 19:24:28 +0000759 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, *Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000760
Chris Lattner007377f2001-09-07 16:36:04 +0000761 // Output all of the instructions in the basic block...
Chris Lattner7e708292002-06-25 16:13:24 +0000762 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
763 printInstruction(*I);
Chris Lattner9f717ef2004-03-08 18:51:45 +0000764
Misha Brukmane5242de2004-04-28 19:24:28 +0000765 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, *Out);
Chris Lattner00950542001-06-06 20:29:01 +0000766}
767
Chris Lattnere02fa852001-10-13 06:42:36 +0000768
Misha Brukmanab5c6002004-03-02 00:22:19 +0000769/// printInfoComment - Print a little comment after the instruction indicating
770/// which slot it occupies.
771///
Chris Lattner7e708292002-06-25 16:13:24 +0000772void AssemblyWriter::printInfoComment(const Value &V) {
773 if (V.getType() != Type::VoidTy) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000774 *Out << "\t\t; <";
Chris Lattner7e708292002-06-25 16:13:24 +0000775 printType(V.getType()) << ">";
Chris Lattnere02fa852001-10-13 06:42:36 +0000776
Chris Lattner7e708292002-06-25 16:13:24 +0000777 if (!V.hasName()) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000778 int Slot = Table.getSlot(&V); // Print out the def slot taken...
Misha Brukmane5242de2004-04-28 19:24:28 +0000779 if (Slot >= 0) *Out << ":" << Slot;
780 else *Out << ":<badref>";
Chris Lattnere02fa852001-10-13 06:42:36 +0000781 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000782 *Out << " [#uses=" << V.use_size() << "]"; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +0000783 }
784}
785
Misha Brukmanab5c6002004-03-02 00:22:19 +0000786/// printInstruction - This member is called for each Instruction in a method.
787///
Chris Lattner7e708292002-06-25 16:13:24 +0000788void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000789 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, *Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000790
Misha Brukmane5242de2004-04-28 19:24:28 +0000791 *Out << "\t";
Chris Lattner00950542001-06-06 20:29:01 +0000792
793 // Print out name if it exists...
Chris Lattner7e708292002-06-25 16:13:24 +0000794 if (I.hasName())
Misha Brukmane5242de2004-04-28 19:24:28 +0000795 *Out << getLLVMName(I.getName()) << " = ";
Chris Lattner00950542001-06-06 20:29:01 +0000796
Chris Lattnere5e475e2003-09-08 17:45:59 +0000797 // If this is a volatile load or store, print out the volatile marker
798 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
799 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()))
Misha Brukmane5242de2004-04-28 19:24:28 +0000800 *Out << "volatile ";
Chris Lattnere5e475e2003-09-08 17:45:59 +0000801
Chris Lattner00950542001-06-06 20:29:01 +0000802 // Print out the opcode...
Misha Brukmane5242de2004-04-28 19:24:28 +0000803 *Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +0000804
805 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +0000806 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000807
808 // Special case conditional branches to swizzle the condition out to the front
Chris Lattner7e708292002-06-25 16:13:24 +0000809 if (isa<BranchInst>(I) && I.getNumOperands() > 1) {
810 writeOperand(I.getOperand(2), true);
Misha Brukmane5242de2004-04-28 19:24:28 +0000811 *Out << ",";
Chris Lattner00950542001-06-06 20:29:01 +0000812 writeOperand(Operand, true);
Misha Brukmane5242de2004-04-28 19:24:28 +0000813 *Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000814 writeOperand(I.getOperand(1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000815
Chris Lattner94dc1f22002-04-13 18:34:38 +0000816 } else if (isa<SwitchInst>(I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000817 // Special case switch statement to get formatting nice and correct...
Misha Brukmane5242de2004-04-28 19:24:28 +0000818 writeOperand(Operand , true); *Out << ",";
819 writeOperand(I.getOperand(1), true); *Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +0000820
Chris Lattner7e708292002-06-25 16:13:24 +0000821 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000822 *Out << "\n\t\t";
823 writeOperand(I.getOperand(op ), true); *Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000824 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +0000825 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000826 *Out << "\n\t]";
Chris Lattnerb00c5822001-10-02 03:41:24 +0000827 } else if (isa<PHINode>(I)) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000828 *Out << " ";
Chris Lattner7e708292002-06-25 16:13:24 +0000829 printType(I.getType());
Misha Brukmane5242de2004-04-28 19:24:28 +0000830 *Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +0000831
Chris Lattner7e708292002-06-25 16:13:24 +0000832 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000833 if (op) *Out << ", ";
834 *Out << "[";
835 writeOperand(I.getOperand(op ), false); *Out << ",";
836 writeOperand(I.getOperand(op+1), false); *Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +0000837 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000838 } else if (isa<ReturnInst>(I) && !Operand) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000839 *Out << " void";
Chris Lattnere02fa852001-10-13 06:42:36 +0000840 } else if (isa<CallInst>(I)) {
Chris Lattner7a012292003-08-05 15:34:45 +0000841 const PointerType *PTy = cast<PointerType>(Operand->getType());
842 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
843 const Type *RetTy = FTy->getReturnType();
Chris Lattner268de042001-11-06 21:28:12 +0000844
Chris Lattner7a012292003-08-05 15:34:45 +0000845 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +0000846 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +0000847 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +0000848 //
Chris Lattner7a012292003-08-05 15:34:45 +0000849 if (!FTy->isVarArg() &&
Chris Lattner94dc1f22002-04-13 18:34:38 +0000850 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +0000851 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000852 *Out << " "; printType(RetTy);
Chris Lattner268de042001-11-06 21:28:12 +0000853 writeOperand(Operand, false);
854 } else {
855 writeOperand(Operand, true);
856 }
Misha Brukmane5242de2004-04-28 19:24:28 +0000857 *Out << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000858 if (I.getNumOperands() > 1) writeOperand(I.getOperand(1), true);
859 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000860 *Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000861 writeOperand(I.getOperand(op), true);
Chris Lattner00950542001-06-06 20:29:01 +0000862 }
863
Misha Brukmane5242de2004-04-28 19:24:28 +0000864 *Out << " )";
Chris Lattner7e708292002-06-25 16:13:24 +0000865 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner7a012292003-08-05 15:34:45 +0000866 const PointerType *PTy = cast<PointerType>(Operand->getType());
867 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
868 const Type *RetTy = FTy->getReturnType();
869
870 // If possible, print out the short form of the invoke instruction. We can
871 // only do this if the first argument is a pointer to a nonvararg function,
872 // and if the return type is not a pointer to a function.
873 //
874 if (!FTy->isVarArg() &&
875 (!isa<PointerType>(RetTy) ||
876 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000877 *Out << " "; printType(RetTy);
Chris Lattner7a012292003-08-05 15:34:45 +0000878 writeOperand(Operand, false);
879 } else {
880 writeOperand(Operand, true);
881 }
882
Misha Brukmane5242de2004-04-28 19:24:28 +0000883 *Out << "(";
Chris Lattner7e708292002-06-25 16:13:24 +0000884 if (I.getNumOperands() > 3) writeOperand(I.getOperand(3), true);
885 for (unsigned op = 4, Eop = I.getNumOperands(); op < Eop; ++op) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000886 *Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000887 writeOperand(I.getOperand(op), true);
Chris Lattnere02fa852001-10-13 06:42:36 +0000888 }
889
Misha Brukmane5242de2004-04-28 19:24:28 +0000890 *Out << " )\n\t\t\tto";
Chris Lattnere02fa852001-10-13 06:42:36 +0000891 writeOperand(II->getNormalDest(), true);
Misha Brukmane5242de2004-04-28 19:24:28 +0000892 *Out << " unwind";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +0000893 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +0000894
Chris Lattner7e708292002-06-25 16:13:24 +0000895 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000896 *Out << " ";
Chris Lattner94dc1f22002-04-13 18:34:38 +0000897 printType(AI->getType()->getElementType());
898 if (AI->isArrayAllocation()) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000899 *Out << ",";
Chris Lattner94dc1f22002-04-13 18:34:38 +0000900 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +0000901 }
Chris Lattnere02fa852001-10-13 06:42:36 +0000902 } else if (isa<CastInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +0000903 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmane5242de2004-04-28 19:24:28 +0000904 *Out << " to ";
Chris Lattner7e708292002-06-25 16:13:24 +0000905 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +0000906 } else if (isa<VAArgInst>(I)) {
Chris Lattner41495a22003-11-17 01:17:04 +0000907 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmane5242de2004-04-28 19:24:28 +0000908 *Out << ", ";
Chris Lattner8f77dae2003-05-08 02:44:12 +0000909 printType(I.getType());
Chris Lattner4d45bd02003-10-18 05:57:43 +0000910 } else if (const VANextInst *VAN = dyn_cast<VANextInst>(&I)) {
Chris Lattner41495a22003-11-17 01:17:04 +0000911 if (Operand) writeOperand(Operand, true); // Work with broken code
Misha Brukmane5242de2004-04-28 19:24:28 +0000912 *Out << ", ";
Chris Lattner4d45bd02003-10-18 05:57:43 +0000913 printType(VAN->getArgType());
Chris Lattner00950542001-06-06 20:29:01 +0000914 } else if (Operand) { // Print the normal way...
915
916 // PrintAllTypes - Instructions who have operands of all the same type
917 // omit the type from all but the first operand. If the instruction has
918 // different type operands (for example br), then they are all printed.
919 bool PrintAllTypes = false;
920 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +0000921
Chris Lattnercfdd1482004-03-12 05:53:14 +0000922 // Shift Left & Right print both types even for Ubyte LHS, and select prints
923 // types even if all operands are bools.
924 if (isa<ShiftInst>(I) || isa<SelectInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +0000925 PrintAllTypes = true;
926 } else {
927 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
928 Operand = I.getOperand(i);
929 if (Operand->getType() != TheType) {
930 PrintAllTypes = true; // We have differing types! Print them all!
931 break;
932 }
Chris Lattner00950542001-06-06 20:29:01 +0000933 }
934 }
Chris Lattnerffd9bf42003-04-16 20:20:02 +0000935
Chris Lattnerc1824992001-10-29 16:05:51 +0000936 if (!PrintAllTypes) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000937 *Out << " ";
Chris Lattnerffd9bf42003-04-16 20:20:02 +0000938 printType(TheType);
Chris Lattnerc1824992001-10-29 16:05:51 +0000939 }
Chris Lattner00950542001-06-06 20:29:01 +0000940
Chris Lattner7e708292002-06-25 16:13:24 +0000941 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Misha Brukmane5242de2004-04-28 19:24:28 +0000942 if (i) *Out << ",";
Chris Lattner7e708292002-06-25 16:13:24 +0000943 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000944 }
945 }
946
Chris Lattnere02fa852001-10-13 06:42:36 +0000947 printInfoComment(I);
Misha Brukmane5242de2004-04-28 19:24:28 +0000948 *Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +0000949}
950
951
952//===----------------------------------------------------------------------===//
953// External Interface declarations
954//===----------------------------------------------------------------------===//
955
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000956void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner2ff95b62004-01-18 21:03:06 +0000957 SlotCalculator SlotTable(this, false);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000958 AssemblyWriter W(o, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000959 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000960}
961
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000962void GlobalVariable::print(std::ostream &o) const {
Chris Lattner2ff95b62004-01-18 21:03:06 +0000963 SlotCalculator SlotTable(getParent(), false);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000964 AssemblyWriter W(o, SlotTable, getParent(), 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000965 W.write(this);
Chris Lattnerb0e45232001-09-10 20:08:19 +0000966}
967
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000968void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner2ff95b62004-01-18 21:03:06 +0000969 SlotCalculator SlotTable(getParent(), false);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000970 AssemblyWriter W(o, SlotTable, getParent(), AAW);
Chris Lattner00950542001-06-06 20:29:01 +0000971
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000972 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000973}
974
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000975void BasicBlock::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner2ff95b62004-01-18 21:03:06 +0000976 SlotCalculator SlotTable(getParent(), false);
Chris Lattnerc1824992001-10-29 16:05:51 +0000977 AssemblyWriter W(o, SlotTable,
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000978 getParent() ? getParent()->getParent() : 0, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000979 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000980}
981
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000982void Instruction::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000983 const Function *F = getParent() ? getParent()->getParent() : 0;
Chris Lattner2ff95b62004-01-18 21:03:06 +0000984 SlotCalculator SlotTable(F, false);
Chris Lattner95e5a2c2003-10-30 23:41:03 +0000985 AssemblyWriter W(o, SlotTable, F ? F->getParent() : 0, AAW);
Chris Lattner00950542001-06-06 20:29:01 +0000986
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000987 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +0000988}
Chris Lattnerda1fbcc2001-11-07 04:21:57 +0000989
Chris Lattner75cf7cf2002-04-08 22:03:40 +0000990void Constant::print(std::ostream &o) const {
991 if (this == 0) { o << "<null> constant value\n"; return; }
Chris Lattner3bc06b32002-09-10 15:53:49 +0000992
993 // Handle CPR's special, because they have context information...
994 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
995 CPR->getValue()->print(o); // Print as a global value, with context info.
996 return;
997 }
998
Chris Lattner66e810b2002-04-18 18:53:13 +0000999 o << " " << getType()->getDescription() << " ";
1000
Chris Lattner7b13f562003-05-08 02:08:14 +00001001 std::map<const Type *, std::string> TypeTable;
Chris Lattner66e810b2002-04-18 18:53:13 +00001002 WriteConstantInt(o, this, false, TypeTable, 0);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001003}
1004
1005void Type::print(std::ostream &o) const {
1006 if (this == 0)
1007 o << "<null Type>";
1008 else
1009 o << getDescription();
1010}
1011
Chris Lattner73e21422002-04-09 19:48:49 +00001012void Argument::print(std::ostream &o) const {
Chris Lattner75cf7cf2002-04-08 22:03:40 +00001013 o << getType() << " " << getName();
1014}
1015
1016void Value::dump() const { print(std::cerr); }
1017
1018//===----------------------------------------------------------------------===//
1019// CachedWriter Class Implementation
1020//===----------------------------------------------------------------------===//
1021
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001022void CachedWriter::setModule(const Module *M) {
1023 delete SC; delete AW;
1024 if (M) {
Chris Lattner8ce75012004-01-14 02:49:34 +00001025 SC = new SlotCalculator(M, false);
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001026 AW = new AssemblyWriter(*Out, *SC, M, 0);
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001027 } else {
1028 SC = 0; AW = 0;
1029 }
1030}
1031
1032CachedWriter::~CachedWriter() {
1033 delete AW;
1034 delete SC;
1035}
1036
1037CachedWriter &CachedWriter::operator<<(const Value *V) {
1038 assert(AW && SC && "CachedWriter does not have a current module!");
1039 switch (V->getValueType()) {
1040 case Value::ConstantVal:
Chris Lattner66e810b2002-04-18 18:53:13 +00001041 case Value::ArgumentVal: AW->writeOperand(V, true, true); break;
Chris Lattner949a3622003-07-23 15:30:06 +00001042 case Value::TypeVal: AW->write(cast<Type>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001043 case Value::InstructionVal: AW->write(cast<Instruction>(V)); break;
1044 case Value::BasicBlockVal: AW->write(cast<BasicBlock>(V)); break;
Chris Lattner79df7c02002-03-26 18:01:55 +00001045 case Value::FunctionVal: AW->write(cast<Function>(V)); break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001046 case Value::GlobalVariableVal: AW->write(cast<GlobalVariable>(V)); break;
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001047 default: *Out << "<unknown value type: " << V->getValueType() << ">"; break;
Chris Lattnerda1fbcc2001-11-07 04:21:57 +00001048 }
1049 return *this;
1050}
Misha Brukman5cf1acf2004-04-28 15:31:21 +00001051
1052CachedWriter& CachedWriter::operator<<(const Type *X) {
1053 if (SymbolicTypes) {
1054 const Module *M = AW->getModule();
1055 if (M) WriteTypeSymbolic(*Out, X, M);
1056 return *this;
1057 } else
1058 return *this << (const Value*)X;
1059}
Misha Brukmane5242de2004-04-28 19:24:28 +00001060
1061void CachedWriter::setStream(std::ostream &os) {
1062 Out = &os;
1063 if (AW) AW->setStream(os);
1064}