blob: e82b3d95621845a74ac779e5fa58589cac6785e7 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000020#include "llvm/CallingConv.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"
Chris Lattner863517a2006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Chris Lattner3990b122009-12-28 23:41:32 +000024#include "llvm/IntrinsicInst.h"
Chris Lattnerf3523592009-10-27 17:08:31 +000025#include "llvm/LLVMContext.h"
Dan Gohman1224c382009-07-20 21:19:07 +000026#include "llvm/Operator.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000027#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000028#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000029#include "llvm/TypeSymbolTable.h"
Chris Lattner413fd232009-03-01 00:03:38 +000030#include "llvm/ADT/DenseSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000033#include "llvm/Support/CFG.h"
Devang Patel2d5988d2009-09-30 20:16:54 +000034#include "llvm/Support/Dwarf.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000035#include "llvm/Support/ErrorHandling.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000036#include "llvm/Support/MathExtras.h"
Dan Gohman683e9222009-08-12 17:23:50 +000037#include "llvm/Support/FormattedStream.h"
Chris Lattner007377f2001-09-07 16:36:04 +000038#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000039#include <cctype>
Devang Patel923078c2009-07-01 19:21:12 +000040#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000041using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000042
Reid Spenceredd5d9e2005-05-15 16:13:11 +000043// Make virtual table appear in this compilation unit.
44AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
45
Chris Lattner6ab910b2008-08-19 04:36:02 +000046//===----------------------------------------------------------------------===//
47// Helper Functions
48//===----------------------------------------------------------------------===//
49
50static const Module *getModuleFromVal(const Value *V) {
51 if (const Argument *MA = dyn_cast<Argument>(V))
52 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000053
Chris Lattner6ab910b2008-08-19 04:36:02 +000054 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
55 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000056
Chris Lattner6ab910b2008-08-19 04:36:02 +000057 if (const Instruction *I = dyn_cast<Instruction>(V)) {
58 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
59 return M ? M->getParent() : 0;
60 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000061
Chris Lattner6ab910b2008-08-19 04:36:02 +000062 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
63 return GV->getParent();
64 return 0;
65}
66
Daniel Dunbare9da1332008-10-28 19:33:02 +000067// PrintEscapedString - Print each character of the specified string, escaping
68// it if it is not printable or if it is an escape char.
Dan Gohman683e9222009-08-12 17:23:50 +000069static void PrintEscapedString(const StringRef &Name,
Dan Gohman1220e102009-08-12 20:56:03 +000070 raw_ostream &Out) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000071 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
72 unsigned char C = Name[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000073 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000074 Out << C;
75 else
76 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
77 }
78}
79
Chris Lattner6ab910b2008-08-19 04:36:02 +000080enum PrefixType {
81 GlobalPrefix,
82 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +000083 LocalPrefix,
84 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +000085};
86
87/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
88/// prefixed with % (if the string only contains simple characters) or is
89/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +000090static void PrintLLVMName(raw_ostream &OS, const StringRef &Name,
Daniel Dunbar03d76512009-07-25 23:55:21 +000091 PrefixType Prefix) {
92 assert(Name.data() && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +000093 switch (Prefix) {
Torok Edwinc23197a2009-07-14 16:55:14 +000094 default: llvm_unreachable("Bad prefix!");
Daniel Dunbarcad35802008-10-14 23:28:09 +000095 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +000096 case GlobalPrefix: OS << '@'; break;
97 case LabelPrefix: break;
98 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +000099 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000100
Chris Lattner6ab910b2008-08-19 04:36:02 +0000101 // Scan the name to see if it needs quotes first.
Daniel Dunbar03d76512009-07-25 23:55:21 +0000102 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000103 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000104 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
105 char C = Name[i];
Chris Lattner6ab910b2008-08-19 04:36:02 +0000106 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
107 NeedsQuotes = true;
108 break;
109 }
110 }
111 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000112
Chris Lattner6ab910b2008-08-19 04:36:02 +0000113 // If we didn't need any quotes, just write out the name in one blast.
114 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000115 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000116 return;
117 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000118
Chris Lattner6ab910b2008-08-19 04:36:02 +0000119 // Okay, we need quotes. Output the quotes and escape any scary characters as
120 // needed.
121 OS << '"';
Daniel Dunbar03d76512009-07-25 23:55:21 +0000122 PrintEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000123 OS << '"';
124}
125
126/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
127/// prefixed with % (if the string only contains simple characters) or is
128/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000129static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000130 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000131 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
132}
133
Chris Lattner9cc34462009-02-28 20:25:14 +0000134//===----------------------------------------------------------------------===//
135// TypePrinting Class: Type printing machinery
136//===----------------------------------------------------------------------===//
137
Chris Lattner87185e82009-02-28 23:03:55 +0000138static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) {
139 return *static_cast<DenseMap<const Type *, std::string>*>(M);
Chris Lattnerd8030a72009-02-28 22:34:45 +0000140}
141
142void TypePrinting::clear() {
143 getTypeNamesMap(TypeNames).clear();
144}
Chris Lattner9cc34462009-02-28 20:25:14 +0000145
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000146bool TypePrinting::hasTypeName(const Type *Ty) const {
147 return getTypeNamesMap(TypeNames).count(Ty);
148}
149
150void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
151 getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
152}
153
154
155TypePrinting::TypePrinting() {
Chris Lattner87185e82009-02-28 23:03:55 +0000156 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner9cc34462009-02-28 20:25:14 +0000157}
158
Chris Lattnerd8030a72009-02-28 22:34:45 +0000159TypePrinting::~TypePrinting() {
160 delete &getTypeNamesMap(TypeNames);
161}
162
Chris Lattner534361e2009-02-28 20:49:40 +0000163/// CalcTypeName - Write the specified type to the specified raw_ostream, making
164/// use of type names or up references to shorten the type name where possible.
165void TypePrinting::CalcTypeName(const Type *Ty,
Chris Lattnerb840cac2009-02-28 20:34:19 +0000166 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000167 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000168 // Check to see if the type is named.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000169 if (!IgnoreTopLevelName) {
Nick Lewycky04234832009-03-19 06:31:22 +0000170 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000171 DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
172 if (I != TM.end()) {
173 OS << I->second;
174 return;
175 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000176 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000177
Chris Lattner9cc34462009-02-28 20:25:14 +0000178 // Check to see if the Type is already on the stack...
179 unsigned Slot = 0, CurSize = TypeStack.size();
180 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
Daniel Dunbara279bc32009-09-20 02:20:51 +0000181
Chris Lattner9cc34462009-02-28 20:25:14 +0000182 // This is another base case for the recursion. In this case, we know
183 // that we have looped back to a type that we have previously visited.
184 // Generate the appropriate upreference to handle this.
185 if (Slot < CurSize) {
Chris Lattner30794262009-02-28 21:27:31 +0000186 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner9cc34462009-02-28 20:25:14 +0000187 return;
188 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000189
Chris Lattner9cc34462009-02-28 20:25:14 +0000190 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Daniel Dunbara279bc32009-09-20 02:20:51 +0000191
Chris Lattner9cc34462009-02-28 20:25:14 +0000192 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000193 case Type::VoidTyID: OS << "void"; break;
194 case Type::FloatTyID: OS << "float"; break;
195 case Type::DoubleTyID: OS << "double"; break;
196 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
197 case Type::FP128TyID: OS << "fp128"; break;
198 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
199 case Type::LabelTyID: OS << "label"; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000200 case Type::MetadataTyID: OS << "metadata"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000201 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000202 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner583ffd82009-02-28 21:18:43 +0000203 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000204
Chris Lattner36942d72009-02-28 20:35:42 +0000205 case Type::FunctionTyID: {
206 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000207 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
208 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000209 for (FunctionType::param_iterator I = FTy->param_begin(),
210 E = FTy->param_end(); I != E; ++I) {
211 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000212 OS << ", ";
213 CalcTypeName(*I, TypeStack, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000214 }
Chris Lattner36942d72009-02-28 20:35:42 +0000215 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000216 if (FTy->getNumParams()) OS << ", ";
217 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000218 }
Chris Lattner30794262009-02-28 21:27:31 +0000219 OS << ')';
Chris Lattner36942d72009-02-28 20:35:42 +0000220 break;
221 }
222 case Type::StructTyID: {
223 const StructType *STy = cast<StructType>(Ty);
224 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000225 OS << '<';
226 OS << "{ ";
Chris Lattner36942d72009-02-28 20:35:42 +0000227 for (StructType::element_iterator I = STy->element_begin(),
228 E = STy->element_end(); I != E; ++I) {
Chris Lattner30794262009-02-28 21:27:31 +0000229 CalcTypeName(*I, TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000230 if (next(I) != STy->element_end())
Chris Lattner30794262009-02-28 21:27:31 +0000231 OS << ',';
232 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000233 }
Chris Lattner30794262009-02-28 21:27:31 +0000234 OS << '}';
Chris Lattner36942d72009-02-28 20:35:42 +0000235 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000236 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000237 break;
238 }
239 case Type::PointerTyID: {
240 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000241 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000242 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000243 OS << " addrspace(" << AddressSpace << ')';
244 OS << '*';
Chris Lattner36942d72009-02-28 20:35:42 +0000245 break;
246 }
247 case Type::ArrayTyID: {
248 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000249 OS << '[' << ATy->getNumElements() << " x ";
250 CalcTypeName(ATy->getElementType(), TypeStack, OS);
251 OS << ']';
Chris Lattner36942d72009-02-28 20:35:42 +0000252 break;
253 }
254 case Type::VectorTyID: {
255 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000256 OS << "<" << PTy->getNumElements() << " x ";
257 CalcTypeName(PTy->getElementType(), TypeStack, OS);
258 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000259 break;
260 }
261 case Type::OpaqueTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000262 OS << "opaque";
Chris Lattner36942d72009-02-28 20:35:42 +0000263 break;
264 default:
Chris Lattner30794262009-02-28 21:27:31 +0000265 OS << "<unrecognized-type>";
Chris Lattner36942d72009-02-28 20:35:42 +0000266 break;
Chris Lattner9cc34462009-02-28 20:25:14 +0000267 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000268
Chris Lattner534361e2009-02-28 20:49:40 +0000269 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner9cc34462009-02-28 20:25:14 +0000270}
271
272/// printTypeInt - The internal guts of printing out a type that has a
273/// potentially named portion.
274///
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000275void TypePrinting::print(const Type *Ty, raw_ostream &OS,
276 bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000277 // Check to see if the type is named.
Chris Lattner87185e82009-02-28 23:03:55 +0000278 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000279 if (!IgnoreTopLevelName) {
280 DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
281 if (I != TM.end()) {
282 OS << I->second;
283 return;
284 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000285 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000286
Chris Lattner9cc34462009-02-28 20:25:14 +0000287 // Otherwise we have a type that has not been named but is a derived type.
288 // Carefully recurse the type hierarchy to print out any contained symbolic
289 // names.
Chris Lattnerb840cac2009-02-28 20:34:19 +0000290 SmallVector<const Type *, 16> TypeStack;
Chris Lattner9cc34462009-02-28 20:25:14 +0000291 std::string TypeName;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000292
Chris Lattner534361e2009-02-28 20:49:40 +0000293 raw_string_ostream TypeOS(TypeName);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000294 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner534361e2009-02-28 20:49:40 +0000295 OS << TypeOS.str();
296
297 // Cache type name for later use.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000298 if (!IgnoreTopLevelName)
299 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner9cc34462009-02-28 20:25:14 +0000300}
301
Chris Lattner413fd232009-03-01 00:03:38 +0000302namespace {
303 class TypeFinder {
304 // To avoid walking constant expressions multiple times and other IR
305 // objects, we keep several helper maps.
306 DenseSet<const Value*> VisitedConstants;
307 DenseSet<const Type*> VisitedTypes;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000308
Chris Lattner413fd232009-03-01 00:03:38 +0000309 TypePrinting &TP;
310 std::vector<const Type*> &NumberedTypes;
311 public:
312 TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
313 : TP(tp), NumberedTypes(numberedTypes) {}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000314
Chris Lattner413fd232009-03-01 00:03:38 +0000315 void Run(const Module &M) {
Chris Lattner88485862009-03-01 00:32:33 +0000316 // Get types from the type symbol table. This gets opaque types referened
317 // only through derived named types.
318 const TypeSymbolTable &ST = M.getTypeSymbolTable();
319 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
320 TI != E; ++TI)
321 IncorporateType(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000322
Chris Lattner413fd232009-03-01 00:03:38 +0000323 // Get types from global variables.
324 for (Module::const_global_iterator I = M.global_begin(),
325 E = M.global_end(); I != E; ++I) {
326 IncorporateType(I->getType());
327 if (I->hasInitializer())
328 IncorporateValue(I->getInitializer());
329 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000330
Chris Lattner413fd232009-03-01 00:03:38 +0000331 // Get types from aliases.
332 for (Module::const_alias_iterator I = M.alias_begin(),
333 E = M.alias_end(); I != E; ++I) {
334 IncorporateType(I->getType());
335 IncorporateValue(I->getAliasee());
336 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000337
Chris Lattner413fd232009-03-01 00:03:38 +0000338 // Get types from functions.
339 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
340 IncorporateType(FI->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000341
Chris Lattner413fd232009-03-01 00:03:38 +0000342 for (Function::const_iterator BB = FI->begin(), E = FI->end();
343 BB != E;++BB)
344 for (BasicBlock::const_iterator II = BB->begin(),
345 E = BB->end(); II != E; ++II) {
346 const Instruction &I = *II;
347 // Incorporate the type of the instruction and all its operands.
348 IncorporateType(I.getType());
349 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
350 OI != OE; ++OI)
351 IncorporateValue(*OI);
352 }
353 }
354 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000355
Chris Lattner413fd232009-03-01 00:03:38 +0000356 private:
357 void IncorporateType(const Type *Ty) {
358 // Check to see if we're already visited this type.
Chris Lattner88485862009-03-01 00:32:33 +0000359 if (!VisitedTypes.insert(Ty).second)
Chris Lattner413fd232009-03-01 00:03:38 +0000360 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000361
Chris Lattner413fd232009-03-01 00:03:38 +0000362 // If this is a structure or opaque type, add a name for the type.
Nick Lewycky21cc4462009-04-04 07:22:01 +0000363 if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
364 || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
Chris Lattner413fd232009-03-01 00:03:38 +0000365 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
366 NumberedTypes.push_back(Ty);
367 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000368
Chris Lattner413fd232009-03-01 00:03:38 +0000369 // Recursively walk all contained types.
370 for (Type::subtype_iterator I = Ty->subtype_begin(),
371 E = Ty->subtype_end(); I != E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000372 IncorporateType(*I);
Chris Lattner413fd232009-03-01 00:03:38 +0000373 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000374
Chris Lattner413fd232009-03-01 00:03:38 +0000375 /// IncorporateValue - This method is used to walk operand lists finding
376 /// types hiding in constant expressions and other operands that won't be
377 /// walked in other ways. GlobalValues, basic blocks, instructions, and
378 /// inst operands are all explicitly enumerated.
379 void IncorporateValue(const Value *V) {
380 if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000381
Chris Lattner413fd232009-03-01 00:03:38 +0000382 // Already visited?
383 if (!VisitedConstants.insert(V).second)
384 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000385
Chris Lattner413fd232009-03-01 00:03:38 +0000386 // Check this type.
387 IncorporateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000388
Chris Lattner413fd232009-03-01 00:03:38 +0000389 // Look in operands for types.
390 const Constant *C = cast<Constant>(V);
391 for (Constant::const_op_iterator I = C->op_begin(),
392 E = C->op_end(); I != E;++I)
393 IncorporateValue(*I);
394 }
395 };
396} // end anonymous namespace
397
398
399/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
400/// the specified module to the TypePrinter and all numbered types to it and the
401/// NumberedTypes table.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000402static void AddModuleTypesToPrinter(TypePrinting &TP,
Chris Lattner413fd232009-03-01 00:03:38 +0000403 std::vector<const Type*> &NumberedTypes,
404 const Module *M) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000405 if (M == 0) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000406
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000407 // If the module has a symbol table, take all global types and stuff their
408 // names into the TypeNames map.
409 const TypeSymbolTable &ST = M->getTypeSymbolTable();
410 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
411 TI != E; ++TI) {
412 const Type *Ty = cast<Type>(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000413
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000414 // As a heuristic, don't insert pointer to primitive types, because
415 // they are used too often to have a single useful name.
416 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
417 const Type *PETy = PTy->getElementType();
418 if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
419 !isa<OpaqueType>(PETy))
420 continue;
421 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000422
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000423 // Likewise don't insert primitives either.
424 if (Ty->isInteger() || Ty->isPrimitiveType())
425 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000426
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000427 // Get the name as a string and insert it into TypeNames.
428 std::string NameStr;
Dan Gohman683e9222009-08-12 17:23:50 +0000429 raw_string_ostream NameROS(NameStr);
430 formatted_raw_ostream NameOS(NameROS);
Daniel Dunbar03d76512009-07-25 23:55:21 +0000431 PrintLLVMName(NameOS, TI->first, LocalPrefix);
Dan Gohman683e9222009-08-12 17:23:50 +0000432 NameOS.flush();
433 TP.addTypeName(Ty, NameStr);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000434 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000435
Chris Lattner413fd232009-03-01 00:03:38 +0000436 // Walk the entire module to find references to unnamed structure and opaque
437 // types. This is required for correctness by opaque types (because multiple
438 // uses of an unnamed opaque type needs to be referred to by the same ID) and
439 // it shrinks complex recursive structure types substantially in some cases.
440 TypeFinder(TP, NumberedTypes).Run(*M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000441}
442
Chris Lattner9cc34462009-02-28 20:25:14 +0000443
Chris Lattner9cc34462009-02-28 20:25:14 +0000444/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
445/// type, iff there is an entry in the modules symbol table for the specified
Chris Lattnerc2871372009-02-28 21:05:51 +0000446/// type or one of it's component types.
Chris Lattner9cc34462009-02-28 20:25:14 +0000447///
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000448void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
449 TypePrinting Printer;
Chris Lattner413fd232009-03-01 00:03:38 +0000450 std::vector<const Type*> NumberedTypes;
451 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000452 Printer.print(Ty, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000453}
454
Chris Lattner6ab910b2008-08-19 04:36:02 +0000455//===----------------------------------------------------------------------===//
456// SlotTracker Class: Enumerate slot numbers for unnamed values
457//===----------------------------------------------------------------------===//
458
Chris Lattnerb64871a2008-08-19 04:28:07 +0000459namespace {
460
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000461/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000462///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000463class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000464public:
Devang Patel320671d2009-07-08 21:44:25 +0000465 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattner92255072008-08-17 17:25:25 +0000466 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000467
468private:
Devang Patel320671d2009-07-08 21:44:25 +0000469 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000470 const Module* TheModule;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000471
Devang Patel320671d2009-07-08 21:44:25 +0000472 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000473 const Function* TheFunction;
474 bool FunctionProcessed;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000475
Devang Patel320671d2009-07-08 21:44:25 +0000476 /// TheMDNode - The MDNode for which we are holding slot numbers.
477 const MDNode *TheMDNode;
478
Devang Patelc29d5b32009-07-30 00:02:57 +0000479 /// TheNamedMDNode - The MDNode for which we are holding slot numbers.
480 const NamedMDNode *TheNamedMDNode;
481
Devang Patel320671d2009-07-08 21:44:25 +0000482 /// mMap - The TypePlanes map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000483 ValueMap mMap;
484 unsigned mNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000485
Devang Patel320671d2009-07-08 21:44:25 +0000486 /// fMap - The TypePlanes map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000487 ValueMap fMap;
488 unsigned fNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000489
Devang Patel320671d2009-07-08 21:44:25 +0000490 /// mdnMap - Map for MDNodes.
491 ValueMap mdnMap;
492 unsigned mdnNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000493public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000494 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000495 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000496 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000497 explicit SlotTracker(const Function *F);
Devang Patel320671d2009-07-08 21:44:25 +0000498 /// Construct from a mdnode.
499 explicit SlotTracker(const MDNode *N);
Devang Patelc29d5b32009-07-30 00:02:57 +0000500 /// Construct from a named mdnode.
501 explicit SlotTracker(const NamedMDNode *N);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000502
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000503 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000504 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000505 int getLocalSlot(const Value *V);
506 int getGlobalSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000507 int getMetadataSlot(const MDNode *N);
Reid Spencerfc621e22004-06-09 15:26:53 +0000508
Misha Brukmanfd939082005-04-21 23:48:37 +0000509 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000510 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000511 void incorporateFunction(const Function *F) {
512 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000513 FunctionProcessed = false;
514 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000515
Misha Brukmanfd939082005-04-21 23:48:37 +0000516 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000517 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000518 /// will reset the state of the machine back to just the module contents.
519 void purgeFunction();
520
Devang Patel320671d2009-07-08 21:44:25 +0000521 /// MDNode map iterators.
522 ValueMap::iterator mdnBegin() { return mdnMap.begin(); }
523 ValueMap::iterator mdnEnd() { return mdnMap.end(); }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +0000524 unsigned mdnSize() const { return mdnMap.size(); }
525 bool mdnEmpty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000526
Reid Spencerb03de0c2004-05-26 21:56:09 +0000527 /// This function does the actual initialization.
528 inline void initialize();
529
Devang Patel320671d2009-07-08 21:44:25 +0000530 // Implementation Details
531private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000532 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
533 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000534
535 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
536 void CreateMetadataSlot(const MDNode *N);
537
Chris Lattner9446bbe2007-01-09 07:55:49 +0000538 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
539 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000540
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000541 /// Add all of the module level global variables (and their initializers)
542 /// and function declarations, but not the contents of those functions.
543 void processModule();
544
Devang Patel320671d2009-07-08 21:44:25 +0000545 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000546 void processFunction();
547
Devang Patel320671d2009-07-08 21:44:25 +0000548 /// Add all MDNode operands.
549 void processMDNode();
550
Devang Patelc29d5b32009-07-30 00:02:57 +0000551 /// Add all MDNode operands.
552 void processNamedMDNode();
553
Chris Lattner0d9574a2008-08-19 04:26:57 +0000554 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
555 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000556};
557
Chris Lattnerb64871a2008-08-19 04:28:07 +0000558} // end anonymous namespace
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000559
Chris Lattner6ab910b2008-08-19 04:36:02 +0000560
561static SlotTracker *createSlotTracker(const Value *V) {
562 if (const Argument *FA = dyn_cast<Argument>(V))
563 return new SlotTracker(FA->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000564
Chris Lattner6ab910b2008-08-19 04:36:02 +0000565 if (const Instruction *I = dyn_cast<Instruction>(V))
566 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000567
Chris Lattner6ab910b2008-08-19 04:36:02 +0000568 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
569 return new SlotTracker(BB->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000570
Chris Lattner6ab910b2008-08-19 04:36:02 +0000571 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
572 return new SlotTracker(GV->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000573
Chris Lattner6ab910b2008-08-19 04:36:02 +0000574 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbara279bc32009-09-20 02:20:51 +0000575 return new SlotTracker(GA->getParent());
576
Chris Lattner6ab910b2008-08-19 04:36:02 +0000577 if (const Function *Func = dyn_cast<Function>(V))
578 return new SlotTracker(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000579
Chris Lattner6ab910b2008-08-19 04:36:02 +0000580 return 0;
581}
582
583#if 0
Dan Gohmanfd87a542009-07-25 01:43:01 +0000584#define ST_DEBUG(X) errs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000585#else
Chris Lattner24233032008-08-19 04:47:09 +0000586#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000587#endif
588
589// Module level constructor. Causes the contents of the Module (sans functions)
590// to be added to the slot table.
591SlotTracker::SlotTracker(const Module *M)
Devang Patel320671d2009-07-08 21:44:25 +0000592 : TheModule(M), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
Devang Patelc29d5b32009-07-30 00:02:57 +0000593 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000594}
595
596// Function level constructor. Causes the contents of the Module and the one
597// function provided to be added to the slot table.
598SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000599 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Devang Patelc29d5b32009-07-30 00:02:57 +0000600 TheMDNode(0), TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Devang Patel320671d2009-07-08 21:44:25 +0000601}
602
603// Constructor to handle single MDNode.
604SlotTracker::SlotTracker(const MDNode *C)
605 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(C),
Devang Patelc29d5b32009-07-30 00:02:57 +0000606 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
607}
608
609// Constructor to handle single NamedMDNode.
610SlotTracker::SlotTracker(const NamedMDNode *N)
611 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
612 TheNamedMDNode(N), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000613}
614
615inline void SlotTracker::initialize() {
616 if (TheModule) {
617 processModule();
618 TheModule = 0; ///< Prevent re-processing next time we're called.
619 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000620
Chris Lattner6ab910b2008-08-19 04:36:02 +0000621 if (TheFunction && !FunctionProcessed)
622 processFunction();
Devang Patel320671d2009-07-08 21:44:25 +0000623
624 if (TheMDNode)
625 processMDNode();
Devang Patelc29d5b32009-07-30 00:02:57 +0000626
627 if (TheNamedMDNode)
628 processNamedMDNode();
Chris Lattner6ab910b2008-08-19 04:36:02 +0000629}
630
631// Iterate through all the global variables, functions, and global
632// variable initializers and create slots for them.
633void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000634 ST_DEBUG("begin processModule!\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000635
Chris Lattner6ab910b2008-08-19 04:36:02 +0000636 // Add all of the unnamed global variables to the value table.
637 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel320671d2009-07-08 21:44:25 +0000638 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000639 if (!I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000640 CreateModuleSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000641 if (I->hasInitializer()) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000642 if (MDNode *N = dyn_cast<MDNode>(I->getInitializer()))
Devang Patel320671d2009-07-08 21:44:25 +0000643 CreateMetadataSlot(N);
644 }
645 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000646
Devang Patel37c4a2d2009-07-29 22:04:47 +0000647 // Add metadata used by named metadata.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000648 for (Module::const_named_metadata_iterator
Devang Patel37c4a2d2009-07-29 22:04:47 +0000649 I = TheModule->named_metadata_begin(),
650 E = TheModule->named_metadata_end(); I != E; ++I) {
651 const NamedMDNode *NMD = I;
652 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
653 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
Devang Patele8861b82009-07-30 01:02:04 +0000654 if (MD)
655 CreateMetadataSlot(MD);
Devang Patel37c4a2d2009-07-29 22:04:47 +0000656 }
657 }
658
Chris Lattner6ab910b2008-08-19 04:36:02 +0000659 // Add all the unnamed functions to the table.
660 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
661 I != E; ++I)
662 if (!I->hasName())
663 CreateModuleSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000664
Chris Lattner24233032008-08-19 04:47:09 +0000665 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000666}
667
Chris Lattner6ab910b2008-08-19 04:36:02 +0000668// Process the arguments, basic blocks, and instructions of a function.
669void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000670 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000671 fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000672
Chris Lattner6ab910b2008-08-19 04:36:02 +0000673 // Add all the function arguments with no names.
674 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
675 AE = TheFunction->arg_end(); AI != AE; ++AI)
676 if (!AI->hasName())
677 CreateFunctionSlot(AI);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000678
Chris Lattner24233032008-08-19 04:47:09 +0000679 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000680
Chris Lattner3990b122009-12-28 23:41:32 +0000681 SmallVector<std::pair<unsigned, MDNode*>, 2> MDForInst;
Devang Patel43215782009-09-16 20:21:17 +0000682
Chris Lattner6ab910b2008-08-19 04:36:02 +0000683 // Add all of the basic blocks and instructions with no names.
684 for (Function::const_iterator BB = TheFunction->begin(),
685 E = TheFunction->end(); BB != E; ++BB) {
686 if (!BB->hasName())
687 CreateFunctionSlot(BB);
Chris Lattner3990b122009-12-28 23:41:32 +0000688
Daniel Dunbara279bc32009-09-20 02:20:51 +0000689 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel320671d2009-07-08 21:44:25 +0000690 ++I) {
Chris Lattner3990b122009-12-28 23:41:32 +0000691 if (!I->getType()->isVoidTy() && !I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000692 CreateFunctionSlot(I);
Chris Lattner3990b122009-12-28 23:41:32 +0000693
694 // Intrinsics can directly use metadata.
695 if (isa<IntrinsicInst>(I))
696 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
697 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
698 CreateMetadataSlot(N);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000699
Devang Patel43215782009-09-16 20:21:17 +0000700 // Process metadata attached with this instruction.
Chris Lattner3990b122009-12-28 23:41:32 +0000701 MDForInst.clear();
702 I->getAllMetadata(MDForInst);
703 for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
704 CreateMetadataSlot(MDForInst[i].second);
Devang Patel320671d2009-07-08 21:44:25 +0000705 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000706 }
Devang Patel43215782009-09-16 20:21:17 +0000707
Chris Lattner6ab910b2008-08-19 04:36:02 +0000708 FunctionProcessed = true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000709
Chris Lattner24233032008-08-19 04:47:09 +0000710 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000711}
712
Devang Patel320671d2009-07-08 21:44:25 +0000713/// processMDNode - Process TheMDNode.
714void SlotTracker::processMDNode() {
715 ST_DEBUG("begin processMDNode!\n");
716 mdnNext = 0;
717 CreateMetadataSlot(TheMDNode);
718 TheMDNode = 0;
719 ST_DEBUG("end processMDNode!\n");
720}
721
Devang Patelc29d5b32009-07-30 00:02:57 +0000722/// processNamedMDNode - Process TheNamedMDNode.
723void SlotTracker::processNamedMDNode() {
724 ST_DEBUG("begin processNamedMDNode!\n");
725 mdnNext = 0;
726 for (unsigned i = 0, e = TheNamedMDNode->getNumElements(); i != e; ++i) {
727 MDNode *MD = dyn_cast_or_null<MDNode>(TheNamedMDNode->getElement(i));
728 if (MD)
729 CreateMetadataSlot(MD);
730 }
731 TheNamedMDNode = 0;
732 ST_DEBUG("end processNamedMDNode!\n");
733}
734
Chris Lattner6ab910b2008-08-19 04:36:02 +0000735/// Clean up after incorporating a function. This is the only way to get out of
736/// the function incorporation state that affects get*Slot/Create*Slot. Function
737/// incorporation state is indicated by TheFunction != 0.
738void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000739 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000740 fMap.clear(); // Simply discard the function level map
741 TheFunction = 0;
742 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000743 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000744}
745
746/// getGlobalSlot - Get the slot number of a global value.
747int SlotTracker::getGlobalSlot(const GlobalValue *V) {
748 // Check for uninitialized state and do lazy initialization.
749 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000750
Chris Lattner6ab910b2008-08-19 04:36:02 +0000751 // Find the type plane in the module map
752 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000753 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000754}
755
Devang Patel320671d2009-07-08 21:44:25 +0000756/// getGlobalSlot - Get the slot number of a MDNode.
757int SlotTracker::getMetadataSlot(const MDNode *N) {
758 // Check for uninitialized state and do lazy initialization.
759 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000760
Devang Patel320671d2009-07-08 21:44:25 +0000761 // Find the type plane in the module map
762 ValueMap::iterator MI = mdnMap.find(N);
763 return MI == mdnMap.end() ? -1 : (int)MI->second;
764}
765
Chris Lattner6ab910b2008-08-19 04:36:02 +0000766
767/// getLocalSlot - Get the slot number for a value that is local to a function.
768int SlotTracker::getLocalSlot(const Value *V) {
769 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000770
Chris Lattner6ab910b2008-08-19 04:36:02 +0000771 // Check for uninitialized state and do lazy initialization.
772 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000773
Chris Lattner6ab910b2008-08-19 04:36:02 +0000774 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000775 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000776}
777
778
779/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
780void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
781 assert(V && "Can't insert a null Value into SlotTracker!");
Chris Lattner4ee93c42009-12-29 07:25:48 +0000782 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000783 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000784
Chris Lattner6ab910b2008-08-19 04:36:02 +0000785 unsigned DestSlot = mNext++;
786 mMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000787
Chris Lattner24233032008-08-19 04:47:09 +0000788 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000789 DestSlot << " [");
790 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000791 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000792 (isa<Function>(V) ? 'F' :
793 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
794}
795
Chris Lattner6ab910b2008-08-19 04:36:02 +0000796/// CreateSlot - Create a new slot for the specified value if it has no name.
797void SlotTracker::CreateFunctionSlot(const Value *V) {
Chris Lattner4ee93c42009-12-29 07:25:48 +0000798 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000799
Chris Lattner6ab910b2008-08-19 04:36:02 +0000800 unsigned DestSlot = fNext++;
801 fMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000802
Chris Lattner6ab910b2008-08-19 04:36:02 +0000803 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000804 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000805 DestSlot << " [o]\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000806}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000807
Devang Patel320671d2009-07-08 21:44:25 +0000808/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
809void SlotTracker::CreateMetadataSlot(const MDNode *N) {
810 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000811
Victor Hernandez5d301622009-12-18 20:09:14 +0000812 // Don't insert if N is a function-local metadata.
813 if (N->isFunctionLocal())
814 return;
Victor Hernandezff7707e2009-12-04 20:07:10 +0000815
Devang Patel320671d2009-07-08 21:44:25 +0000816 ValueMap::iterator I = mdnMap.find(N);
817 if (I != mdnMap.end())
818 return;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000819
Devang Patel320671d2009-07-08 21:44:25 +0000820 unsigned DestSlot = mdnNext++;
821 mdnMap[N] = DestSlot;
822
Devang Patel028fa772009-10-21 21:25:09 +0000823 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
824 const Value *TV = N->getElement(i);
Devang Patel320671d2009-07-08 21:44:25 +0000825 if (TV)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000826 if (const MDNode *N2 = dyn_cast<MDNode>(TV))
Devang Patel320671d2009-07-08 21:44:25 +0000827 CreateMetadataSlot(N2);
828 }
829}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000830
831//===----------------------------------------------------------------------===//
832// AsmWriter Implementation
833//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000834
Dan Gohman1220e102009-08-12 20:56:03 +0000835static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000836 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +0000837 SlotTracker *Machine);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000838
Chris Lattnerc97536e2008-08-17 04:40:13 +0000839
Chris Lattner207b5bc2001-10-29 16:37:48 +0000840
Chris Lattner82c4bc72006-12-06 06:40:49 +0000841static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000842 const char * pred = "unknown";
843 switch (predicate) {
844 case FCmpInst::FCMP_FALSE: pred = "false"; break;
845 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
846 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
847 case FCmpInst::FCMP_OGE: pred = "oge"; break;
848 case FCmpInst::FCMP_OLT: pred = "olt"; break;
849 case FCmpInst::FCMP_OLE: pred = "ole"; break;
850 case FCmpInst::FCMP_ONE: pred = "one"; break;
851 case FCmpInst::FCMP_ORD: pred = "ord"; break;
852 case FCmpInst::FCMP_UNO: pred = "uno"; break;
853 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
854 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
855 case FCmpInst::FCMP_UGE: pred = "uge"; break;
856 case FCmpInst::FCMP_ULT: pred = "ult"; break;
857 case FCmpInst::FCMP_ULE: pred = "ule"; break;
858 case FCmpInst::FCMP_UNE: pred = "une"; break;
859 case FCmpInst::FCMP_TRUE: pred = "true"; break;
860 case ICmpInst::ICMP_EQ: pred = "eq"; break;
861 case ICmpInst::ICMP_NE: pred = "ne"; break;
862 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
863 case ICmpInst::ICMP_SGE: pred = "sge"; break;
864 case ICmpInst::ICMP_SLT: pred = "slt"; break;
865 case ICmpInst::ICMP_SLE: pred = "sle"; break;
866 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
867 case ICmpInst::ICMP_UGE: pred = "uge"; break;
868 case ICmpInst::ICMP_ULT: pred = "ult"; break;
869 case ICmpInst::ICMP_ULE: pred = "ule"; break;
870 }
871 return pred;
872}
873
Devang Patel2d5988d2009-09-30 20:16:54 +0000874static void WriteMDNodeComment(const MDNode *Node,
875 formatted_raw_ostream &Out) {
876 if (Node->getNumElements() < 1)
877 return;
Devang Patel4d7a2062009-09-30 21:26:51 +0000878 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Node->getElement(0));
Devang Patel2d5988d2009-09-30 20:16:54 +0000879 if (!CI) return;
880 unsigned Val = CI->getZExtValue();
881 unsigned Tag = Val & ~LLVMDebugVersionMask;
Chris Lattner4ee93c42009-12-29 07:25:48 +0000882 if (Val < LLVMDebugVersion)
883 return;
884
885 Out.PadToColumn(50);
886 if (Tag == dwarf::DW_TAG_auto_variable)
887 Out << "; [ DW_TAG_auto_variable ]";
888 else if (Tag == dwarf::DW_TAG_arg_variable)
889 Out << "; [ DW_TAG_arg_variable ]";
890 else if (Tag == dwarf::DW_TAG_return_variable)
891 Out << "; [ DW_TAG_return_variable ]";
892 else if (Tag == dwarf::DW_TAG_vector_type)
893 Out << "; [ DW_TAG_vector_type ]";
894 else if (Tag == dwarf::DW_TAG_user_base)
895 Out << "; [ DW_TAG_user_base ]";
896 else
897 Out << "; [ " << dwarf::TagString(Tag) << " ]";
Devang Patel2d5988d2009-09-30 20:16:54 +0000898}
899
Dan Gohman683e9222009-08-12 17:23:50 +0000900static void WriteMDNodes(formatted_raw_ostream &Out, TypePrinting &TypePrinter,
Devang Patel320671d2009-07-08 21:44:25 +0000901 SlotTracker &Machine) {
902 SmallVector<const MDNode *, 16> Nodes;
903 Nodes.resize(Machine.mdnSize());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000904 for (SlotTracker::ValueMap::iterator I =
905 Machine.mdnBegin(), E = Machine.mdnEnd(); I != E; ++I)
Devang Patel320671d2009-07-08 21:44:25 +0000906 Nodes[I->second] = cast<MDNode>(I->first);
907
908 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Devang Patel2214c942009-07-08 21:57:07 +0000909 Out << '!' << i << " = metadata ";
Devang Patel320671d2009-07-08 21:44:25 +0000910 const MDNode *Node = Nodes[i];
911 Out << "!{";
Devang Patel028fa772009-10-21 21:25:09 +0000912 for (unsigned mi = 0, me = Node->getNumElements(); mi != me; ++mi) {
913 const Value *V = Node->getElement(mi);
Devang Patel320671d2009-07-08 21:44:25 +0000914 if (!V)
915 Out << "null";
916 else if (const MDNode *N = dyn_cast<MDNode>(V)) {
917 Out << "metadata ";
918 Out << '!' << Machine.getMetadataSlot(N);
919 }
920 else {
Devang Patel028fa772009-10-21 21:25:09 +0000921 TypePrinter.print(V->getType(), Out);
Devang Patel320671d2009-07-08 21:44:25 +0000922 Out << ' ';
Devang Patel028fa772009-10-21 21:25:09 +0000923 WriteAsOperandInternal(Out, Node->getElement(mi),
924 &TypePrinter, &Machine);
Devang Patel320671d2009-07-08 21:44:25 +0000925 }
Devang Patel028fa772009-10-21 21:25:09 +0000926 if (mi + 1 != me)
Devang Patel320671d2009-07-08 21:44:25 +0000927 Out << ", ";
928 }
Devang Patel2d5988d2009-09-30 20:16:54 +0000929
930 Out << "}";
931 WriteMDNodeComment(Node, Out);
932 Out << "\n";
Devang Patel320671d2009-07-08 21:44:25 +0000933 }
934}
935
Dan Gohman1220e102009-08-12 20:56:03 +0000936static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman1224c382009-07-20 21:19:07 +0000937 if (const OverflowingBinaryOperator *OBO =
938 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000939 if (OBO->hasNoUnsignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000940 Out << " nuw";
Dan Gohman5078f842009-08-20 17:11:38 +0000941 if (OBO->hasNoSignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000942 Out << " nsw";
Dan Gohman1224c382009-07-20 21:19:07 +0000943 } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) {
944 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +0000945 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +0000946 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
947 if (GEP->isInBounds())
948 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +0000949 }
950}
951
Dan Gohman1220e102009-08-12 20:56:03 +0000952static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattner9cc34462009-02-28 20:25:14 +0000953 TypePrinting &TypePrinter, SlotTracker *Machine) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000954 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000955 if (CI->getType() == Type::getInt1Ty(CV->getContext())) {
Reid Spencer579dca12007-01-12 04:24:46 +0000956 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000957 return;
958 }
959 Out << CI->getValue();
960 return;
961 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000962
Chris Lattnerfad86b02008-08-17 07:19:36 +0000963 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000964 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
965 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
966 // We would like to output the FP constant value in exponential notation,
967 // but we cannot do this if doing so will lose precision. Check here to
968 // make sure that we only output it in exponential format if we can parse
969 // the value back and get the same value.
970 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000971 bool ignored;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000972 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattnerfad86b02008-08-17 07:19:36 +0000973 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
974 CFP->getValueAPF().convertToFloat();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000975 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner66e810b2002-04-18 18:53:13 +0000976
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000977 // Check to make sure that the stringized number is not some string like
978 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
979 // that the string matches the "[-+]?[0-9]" regex.
980 //
981 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
982 ((StrVal[0] == '-' || StrVal[0] == '+') &&
983 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
984 // Reparse stringized version!
985 if (atof(StrVal.c_str()) == Val) {
986 Out << StrVal;
987 return;
988 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000989 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000990 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000991 // output the string in hexadecimal format! Note that loading and storing
992 // floating point types changes the bits of NaNs on some hosts, notably
993 // x86, so we must not use these types.
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000994 assert(sizeof(double) == sizeof(uint64_t) &&
995 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000996 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000997 APFloat apf = CFP->getValueAPF();
998 // Floats are represented in ASCII IR as double, convert.
999 if (!isDouble)
Daniel Dunbara279bc32009-09-20 02:20:51 +00001000 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen541ed9f2009-01-21 20:32:55 +00001001 &ignored);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001002 Out << "0x" <<
1003 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen541ed9f2009-01-21 20:32:55 +00001004 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001005 return;
1006 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001007
Chris Lattnercfb5a202008-08-19 05:06:27 +00001008 // Some form of long double. These appear as a magic letter identifying
1009 // the type, then a fixed number of hex digits.
1010 Out << "0x";
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001011 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001012 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001013 // api needed to prevent premature destruction
1014 APInt api = CFP->getValueAPF().bitcastToAPInt();
1015 const uint64_t* p = api.getRawData();
1016 uint64_t word = p[1];
1017 int shiftcount=12;
1018 int width = api.getBitWidth();
1019 for (int j=0; j<width; j+=4, shiftcount-=4) {
1020 unsigned int nibble = (word>>shiftcount) & 15;
1021 if (nibble < 10)
1022 Out << (unsigned char)(nibble + '0');
1023 else
1024 Out << (unsigned char)(nibble - 10 + 'A');
1025 if (shiftcount == 0 && j+4 < width) {
1026 word = *p;
1027 shiftcount = 64;
1028 if (width-j-4 < 64)
1029 shiftcount = width-j-4;
1030 }
1031 }
1032 return;
1033 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattnercfb5a202008-08-19 05:06:27 +00001034 Out << 'L';
1035 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1036 Out << 'M';
1037 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001038 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001039 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +00001040 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +00001041 const uint64_t* p = api.getRawData();
1042 uint64_t word = *p;
1043 int shiftcount=60;
1044 int width = api.getBitWidth();
1045 for (int j=0; j<width; j+=4, shiftcount-=4) {
1046 unsigned int nibble = (word>>shiftcount) & 15;
1047 if (nibble < 10)
1048 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001049 else
Chris Lattnercfb5a202008-08-19 05:06:27 +00001050 Out << (unsigned char)(nibble - 10 + 'A');
1051 if (shiftcount == 0 && j+4 < width) {
1052 word = *(++p);
1053 shiftcount = 64;
1054 if (width-j-4 < 64)
1055 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001056 }
1057 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001058 return;
1059 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001060
Chris Lattnercfb5a202008-08-19 05:06:27 +00001061 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +00001062 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001063 return;
1064 }
Chris Lattner73050e12009-10-28 03:38:12 +00001065
1066 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1067 Out << "blockaddress(";
1068 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine);
1069 Out << ", ";
Chris Lattnercdfc9402009-11-01 01:27:45 +00001070 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine);
Chris Lattner73050e12009-10-28 03:38:12 +00001071 Out << ")";
1072 return;
1073 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001074
Chris Lattnercfb5a202008-08-19 05:06:27 +00001075 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +00001076 // As a special case, print the array as a string if it is an array of
Dan Gohman9b38d7d2008-05-12 16:34:30 +00001077 // i8 with ConstantInt values.
Misha Brukmanfd939082005-04-21 23:48:37 +00001078 //
Chris Lattner66e810b2002-04-18 18:53:13 +00001079 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +00001080 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +00001081 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +00001082 PrintEscapedString(CA->getAsString(), Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001083 Out << '"';
Chris Lattner66e810b2002-04-18 18:53:13 +00001084 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +00001085 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001086 if (CA->getNumOperands()) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001087 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001088 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001089 WriteAsOperandInternal(Out, CA->getOperand(0),
Dan Gohmand6c0f652009-08-13 15:27:57 +00001090 &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001091 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1092 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001093 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001094 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001095 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001096 }
1097 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001098 Out << ']';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001099 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001100 return;
1101 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001102
Chris Lattnercfb5a202008-08-19 05:06:27 +00001103 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001104 if (CS->getType()->isPacked())
1105 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +00001106 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +00001107 unsigned N = CS->getNumOperands();
1108 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +00001109 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001110 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001111 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001112
Dan Gohmand6c0f652009-08-13 15:27:57 +00001113 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001114
Jim Laskeya3f332b2006-02-25 12:27:03 +00001115 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001116 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001117 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001118 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001119
Dan Gohmand6c0f652009-08-13 15:27:57 +00001120 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001121 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001122 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001123 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001124
Dan Gohman8dae1382008-09-14 17:21:12 +00001125 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001126 if (CS->getType()->isPacked())
1127 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001128 return;
1129 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001130
Chris Lattnercfb5a202008-08-19 05:06:27 +00001131 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1132 const Type *ETy = CP->getType()->getElementType();
1133 assert(CP->getNumOperands() > 0 &&
1134 "Number of operands for a PackedConst must be > 0");
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001135 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001136 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001137 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001138 WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001139 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner4667b712008-08-19 05:26:17 +00001140 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001141 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001142 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001143 WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001144 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001145 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001146 return;
1147 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001148
Chris Lattnercfb5a202008-08-19 05:06:27 +00001149 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001150 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001151 return;
1152 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001153
Chris Lattnercfb5a202008-08-19 05:06:27 +00001154 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +00001155 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001156 return;
1157 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001158
Devang Patel320671d2009-07-08 21:44:25 +00001159 if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
1160 Out << "!" << Machine->getMetadataSlot(Node);
1161 return;
1162 }
1163
Chris Lattnercfb5a202008-08-19 05:06:27 +00001164 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001165 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +00001166 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +00001167 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +00001168 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +00001169 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +00001170
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001171 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001172 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001173 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001174 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001175 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001176 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001177 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001178
Dan Gohman995be7d2008-05-31 19:12:39 +00001179 if (CE->hasIndices()) {
1180 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
1181 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1182 Out << ", " << Indices[i];
1183 }
1184
Reid Spencer3da59db2006-11-27 01:05:10 +00001185 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +00001186 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001187 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +00001188 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001189
Misha Brukman40c732c2004-06-04 21:11:51 +00001190 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001191 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001192 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001193
Chris Lattnercfb5a202008-08-19 05:06:27 +00001194 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001195}
1196
1197
Misha Brukmanab5c6002004-03-02 00:22:19 +00001198/// WriteAsOperand - Write the name of the specified value out to the specified
1199/// ostream. This can be useful when you just want to print int %reg126, not
1200/// the whole instruction that generated it.
1201///
Dan Gohman1220e102009-08-12 20:56:03 +00001202static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00001203 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +00001204 SlotTracker *Machine) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001205 if (V->hasName()) {
1206 PrintLLVMName(Out, V);
1207 return;
1208 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001209
Chris Lattnerc97536e2008-08-17 04:40:13 +00001210 const Constant *CV = dyn_cast<Constant>(V);
1211 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001212 assert(TypePrinter && "Constants require TypePrinting!");
1213 WriteConstantInt(Out, CV, *TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001214 return;
1215 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001216
Chris Lattnercfb5a202008-08-19 05:06:27 +00001217 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001218 Out << "asm ";
1219 if (IA->hasSideEffects())
1220 Out << "sideeffect ";
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001221 if (IA->isAlignStack())
1222 Out << "alignstack ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001223 Out << '"';
1224 PrintEscapedString(IA->getAsmString(), Out);
1225 Out << "\", \"";
1226 PrintEscapedString(IA->getConstraintString(), Out);
1227 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001228 return;
1229 }
Devang Patele54abc92009-07-22 17:43:22 +00001230
Devang Patel104cf9e2009-07-23 01:07:34 +00001231 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Victor Hernandez5d301622009-12-18 20:09:14 +00001232 if (N->isFunctionLocal()) {
Victor Hernandez97e24502009-12-04 01:35:02 +00001233 // Print metadata inline, not via slot reference number.
1234 Out << "!{";
1235 for (unsigned mi = 0, me = N->getNumElements(); mi != me; ++mi) {
1236 const Value *Val = N->getElement(mi);
1237 if (!Val)
1238 Out << "null";
1239 else {
1240 TypePrinter->print(N->getElement(0)->getType(), Out);
1241 Out << ' ';
1242 WriteAsOperandInternal(Out, N->getElement(0), TypePrinter, Machine);
1243 }
1244 if (mi + 1 != me)
1245 Out << ", ";
1246 }
1247 Out << '}';
1248 return;
1249 }
1250
Devang Patel104cf9e2009-07-23 01:07:34 +00001251 Out << '!' << Machine->getMetadataSlot(N);
1252 return;
1253 }
1254
Devang Patele54abc92009-07-22 17:43:22 +00001255 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +00001256 Out << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001257 PrintEscapedString(MDS->getString(), Out);
Devang Patele54abc92009-07-22 17:43:22 +00001258 Out << '"';
1259 return;
1260 }
1261
Evan Cheng746d5462009-11-16 07:10:36 +00001262 if (V->getValueID() == Value::PseudoSourceValueVal ||
1263 V->getValueID() == Value::FixedStackPseudoSourceValueVal) {
Dan Gohmancd26ec52009-09-23 01:33:16 +00001264 V->print(Out);
1265 return;
1266 }
1267
Chris Lattnercfb5a202008-08-19 05:06:27 +00001268 char Prefix = '%';
1269 int Slot;
1270 if (Machine) {
1271 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1272 Slot = Machine->getGlobalSlot(GV);
1273 Prefix = '@';
1274 } else {
1275 Slot = Machine->getLocalSlot(V);
1276 }
Chris Lattnerc97536e2008-08-17 04:40:13 +00001277 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001278 Machine = createSlotTracker(V);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001279 if (Machine) {
1280 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1281 Slot = Machine->getGlobalSlot(GV);
1282 Prefix = '@';
1283 } else {
1284 Slot = Machine->getLocalSlot(V);
1285 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001286 delete Machine;
Chris Lattner80cd1152006-01-25 22:26:05 +00001287 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001288 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001289 }
1290 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001291
Chris Lattnercfb5a202008-08-19 05:06:27 +00001292 if (Slot != -1)
1293 Out << Prefix << Slot;
1294 else
1295 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001296}
1297
Dan Gohman1220e102009-08-12 20:56:03 +00001298void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1299 bool PrintType, const Module *Context) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001300
1301 // Fast path: Don't construct and populate a TypePrinting object if we
1302 // won't be needing any types printed.
Dan Gohman009fc9e2009-08-13 23:07:11 +00001303 if (!PrintType &&
1304 (!isa<Constant>(V) || V->hasName() || isa<GlobalValue>(V))) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001305 WriteAsOperandInternal(Out, V, 0, 0);
1306 return;
1307 }
1308
Chris Lattner607dc682002-07-10 16:48:17 +00001309 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001310
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001311 TypePrinting TypePrinter;
Chris Lattner413fd232009-03-01 00:03:38 +00001312 std::vector<const Type*> NumberedTypes;
1313 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001314 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001315 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001316 Out << ' ';
1317 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001318
Dan Gohmand6c0f652009-08-13 15:27:57 +00001319 WriteAsOperandInternal(Out, V, &TypePrinter, 0);
Chris Lattner622f7402001-07-20 19:15:21 +00001320}
1321
Chris Lattnercfb5a202008-08-19 05:06:27 +00001322namespace {
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001323
Chris Lattner007377f2001-09-07 16:36:04 +00001324class AssemblyWriter {
Dan Gohman683e9222009-08-12 17:23:50 +00001325 formatted_raw_ostream &Out;
Chris Lattner0d9574a2008-08-19 04:26:57 +00001326 SlotTracker &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +00001327 const Module *TheModule;
Chris Lattner9cc34462009-02-28 20:25:14 +00001328 TypePrinting TypePrinter;
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001329 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner413fd232009-03-01 00:03:38 +00001330 std::vector<const Type*> NumberedTypes;
Chris Lattner7d05c462009-12-28 20:10:43 +00001331 SmallVector<StringRef, 8> MDNames;
1332
Chris Lattner00950542001-06-06 20:29:01 +00001333public:
Dan Gohman683e9222009-08-12 17:23:50 +00001334 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1335 const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001336 AssemblyAnnotationWriter *AAW)
Devang Patel3168b792009-09-28 18:31:56 +00001337 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner413fd232009-03-01 00:03:38 +00001338 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Devang Patel18f0c262009-09-28 20:56:00 +00001339 // FIXME: Provide MDPrinter
Chris Lattner7d05c462009-12-28 20:10:43 +00001340 if (M)
1341 M->getContext().getMetadata().getMDKindNames(MDNames);
Chris Lattner00950542001-06-06 20:29:01 +00001342 }
1343
Chris Lattner413fd232009-03-01 00:03:38 +00001344 void write(const Module *M) { printModule(M); }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001345
Chris Lattner944fac72008-08-23 22:23:09 +00001346 void write(const GlobalValue *G) {
1347 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
1348 printGlobal(GV);
1349 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
1350 printAlias(GA);
1351 else if (const Function *F = dyn_cast<Function>(G))
1352 printFunction(F);
1353 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001354 llvm_unreachable("Unknown global");
Chris Lattner944fac72008-08-23 22:23:09 +00001355 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001356
Chris Lattnercfb5a202008-08-19 05:06:27 +00001357 void write(const BasicBlock *BB) { printBasicBlock(BB); }
1358 void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner00950542001-06-06 20:29:01 +00001359
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001360 void writeOperand(const Value *Op, bool PrintType);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001361 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner66e810b2002-04-18 18:53:13 +00001362
Misha Brukmanf771bea2004-11-15 19:30:05 +00001363private:
Chris Lattnerc1824992001-10-29 16:05:51 +00001364 void printModule(const Module *M);
Reid Spencer78d033e2007-01-06 07:24:44 +00001365 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +00001366 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001367 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +00001368 void printFunction(const Function *F);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001369 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattnerc1824992001-10-29 16:05:51 +00001370 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +00001371 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +00001372
Chris Lattnere02fa852001-10-13 06:42:36 +00001373 // printInfoComment - Print a little comment after the instruction indicating
1374 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +00001375 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +00001376};
Chris Lattner413fd232009-03-01 00:03:38 +00001377} // end of anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +00001378
Chris Lattner2761e9f2002-04-13 20:53:41 +00001379
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001380void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1381 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001382 Out << "<null operand!>";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001383 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001384 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001385 TypePrinter.print(Operand->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001386 Out << ' ';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001387 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001388 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Chris Lattneraab18202005-02-24 16:58:29 +00001389 }
Chris Lattner00950542001-06-06 20:29:01 +00001390}
1391
Daniel Dunbara279bc32009-09-20 02:20:51 +00001392void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001393 Attributes Attrs) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001394 if (Operand == 0) {
1395 Out << "<null operand!>";
1396 } else {
Duncan Sandsdc024672007-11-27 13:23:08 +00001397 // Print the type
Chris Lattner0f7364b2009-02-28 21:26:53 +00001398 TypePrinter.print(Operand->getType(), Out);
Duncan Sandsdc024672007-11-27 13:23:08 +00001399 // Print parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001400 if (Attrs != Attribute::None)
1401 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman8dae1382008-09-14 17:21:12 +00001402 Out << ' ';
Duncan Sandsdc024672007-11-27 13:23:08 +00001403 // Print the operand
Dan Gohmand6c0f652009-08-13 15:27:57 +00001404 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Duncan Sandsdc024672007-11-27 13:23:08 +00001405 }
1406}
Chris Lattner00950542001-06-06 20:29:01 +00001407
Chris Lattnerc1824992001-10-29 16:05:51 +00001408void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +00001409 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001410 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001411 // require a comment char before it).
1412 M->getModuleIdentifier().find('\n') == std::string::npos)
1413 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1414
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001415 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001416 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001417 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001418 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001419
Chris Lattnercc041ba2006-01-24 04:13:11 +00001420 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001421 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001422 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001423 size_t CurPos = 0;
1424 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001425 Out << '\n';
Chris Lattner42a162e2006-01-24 00:45:30 +00001426 while (NewLine != std::string::npos) {
1427 // We found a newline, print the portion of the asm string from the
1428 // last newline up to this newline.
1429 Out << "module asm \"";
1430 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1431 Out);
1432 Out << "\"\n";
1433 CurPos = NewLine+1;
1434 NewLine = Asm.find_first_of('\n', CurPos);
1435 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001436 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +00001437 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +00001438 Out << "\"\n";
1439 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001440
Chris Lattner44da7d72004-09-14 05:06:58 +00001441 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +00001442 Module::lib_iterator LI = M->lib_begin();
1443 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +00001444 if (LI != LE) {
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001445 Out << '\n';
Chris Lattnercfe97b72004-09-14 04:51:44 +00001446 Out << "deplibs = [ ";
1447 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +00001448 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001449 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +00001450 if (LI != LE)
1451 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001452 }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001453 Out << " ]";
Reid Spencer83f6a772004-07-25 18:08:18 +00001454 }
Reid Spencere59eaf42004-09-13 23:44:23 +00001455
Chris Lattner413fd232009-03-01 00:03:38 +00001456 // Loop over the symbol table, emitting all id'd types.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001457 if (!M->getTypeSymbolTable().empty() || !NumberedTypes.empty()) Out << '\n';
Reid Spencer78d033e2007-01-06 07:24:44 +00001458 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +00001459
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001460 // Output all globals.
1461 if (!M->global_empty()) Out << '\n';
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001462 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1463 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +00001464 printGlobal(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001465
Chris Lattner69dacfc2007-04-26 02:24:10 +00001466 // Output all aliases.
1467 if (!M->alias_empty()) Out << "\n";
1468 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1469 I != E; ++I)
1470 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001471
Chris Lattner44da7d72004-09-14 05:06:58 +00001472 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001473 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1474 printFunction(I);
Devang Patel320671d2009-07-08 21:44:25 +00001475
Devang Patel37c4a2d2009-07-29 22:04:47 +00001476 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001477 if (!M->named_metadata_empty()) Out << '\n';
Devang Patel37c4a2d2009-07-29 22:04:47 +00001478 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
1479 E = M->named_metadata_end(); I != E; ++I) {
1480 const NamedMDNode *NMD = I;
1481 Out << "!" << NMD->getName() << " = !{";
1482 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1483 if (i) Out << ", ";
Devang Patele8861b82009-07-30 01:02:04 +00001484 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
Devang Patel37c4a2d2009-07-29 22:04:47 +00001485 Out << '!' << Machine.getMetadataSlot(MD);
1486 }
1487 Out << "}\n";
1488 }
1489
1490 // Output metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001491 if (!Machine.mdnEmpty()) Out << '\n';
Devang Patel320671d2009-07-08 21:44:25 +00001492 WriteMDNodes(Out, TypePrinter, Machine);
Chris Lattner007377f2001-09-07 16:36:04 +00001493}
1494
Dan Gohman683e9222009-08-12 17:23:50 +00001495static void PrintLinkage(GlobalValue::LinkageTypes LT,
1496 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001497 switch (LT) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001498 case GlobalValue::ExternalLinkage: break;
1499 case GlobalValue::PrivateLinkage: Out << "private "; break;
1500 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
1501 case GlobalValue::InternalLinkage: Out << "internal "; break;
1502 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1503 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1504 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1505 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1506 case GlobalValue::CommonLinkage: Out << "common "; break;
1507 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1508 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1509 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1510 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001511 case GlobalValue::AvailableExternallyLinkage:
1512 Out << "available_externally ";
1513 break;
Eric Christopher04458e42009-11-13 23:00:14 +00001514 // This is invalid syntax and just a debugging aid.
1515 case GlobalValue::GhostLinkage: Out << "ghost "; break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001516 }
1517}
Duncan Sands667d4b82009-03-07 15:45:40 +00001518
Chris Lattnercfb5a202008-08-19 05:06:27 +00001519
1520static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00001521 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001522 switch (Vis) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001523 default: llvm_unreachable("Invalid visibility style!");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001524 case GlobalValue::DefaultVisibility: break;
1525 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1526 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1527 }
1528}
1529
Chris Lattnerc1824992001-10-29 16:05:51 +00001530void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001531 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine);
Dan Gohman3845e502009-08-12 23:32:33 +00001532 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00001533
Chris Lattner52b26de2008-08-19 05:16:28 +00001534 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1535 Out << "external ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001536
Chris Lattner52b26de2008-08-19 05:16:28 +00001537 PrintLinkage(GV->getLinkage(), Out);
1538 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001539
1540 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerdf986172009-01-02 07:01:27 +00001541 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1542 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001543 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001544 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001545
Dan Gohman8dae1382008-09-14 17:21:12 +00001546 if (GV->hasInitializer()) {
1547 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00001548 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001549 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001550
Chris Lattner60962db2005-11-12 00:10:19 +00001551 if (GV->hasSection())
1552 Out << ", section \"" << GV->getSection() << '"';
1553 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001554 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001555
Chris Lattner7e708292002-06-25 16:13:24 +00001556 printInfoComment(*GV);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001557 Out << '\n';
Chris Lattner70cc3392001-09-10 07:58:01 +00001558}
1559
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001560void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001561 // Don't crash when dumping partially built GA
1562 if (!GA->hasName())
1563 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001564 else {
1565 PrintLLVMName(Out, GA);
1566 Out << " = ";
1567 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001568 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001569
1570 Out << "alias ";
1571
Chris Lattnercfb5a202008-08-19 05:06:27 +00001572 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001573
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001574 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001575
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001576 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001577 TypePrinter.print(GV->getType(), Out);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001578 Out << ' ';
1579 PrintLLVMName(Out, GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001580 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001581 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001582 Out << "* ";
1583
Dan Gohmand6c0f652009-08-13 15:27:57 +00001584 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Anton Korobeynikov591858a2008-03-22 08:17:17 +00001585 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001586 TypePrinter.print(GA->getType(), Out);
1587 Out << ' ';
Chris Lattnerc97536e2008-08-17 04:40:13 +00001588 PrintLLVMName(Out, GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001589 } else {
Chris Lattnera2165ed2009-04-25 21:23:19 +00001590 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1591 // The only valid GEP is an all zero GEP.
1592 assert((CE->getOpcode() == Instruction::BitCast ||
1593 CE->getOpcode() == Instruction::GetElementPtr) &&
1594 "Unsupported aliasee");
1595 writeOperand(CE, false);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001596 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001597
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001598 printInfoComment(*GA);
Chris Lattner52b26de2008-08-19 05:16:28 +00001599 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001600}
1601
Reid Spencer78d033e2007-01-06 07:24:44 +00001602void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner413fd232009-03-01 00:03:38 +00001603 // Emit all numbered types.
1604 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman3845e502009-08-12 23:32:33 +00001605 Out << '%' << i << " = type ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001606
Chris Lattner413fd232009-03-01 00:03:38 +00001607 // Make sure we print out at least one level of the type structure, so
1608 // that we do not get %2 = type %2
1609 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001610 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00001611 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001612
Chris Lattner413fd232009-03-01 00:03:38 +00001613 // Print the named types.
Reid Spencer78d033e2007-01-06 07:24:44 +00001614 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1615 TI != TE; ++TI) {
Daniel Dunbar03d76512009-07-25 23:55:21 +00001616 PrintLLVMName(Out, TI->first, LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00001617 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001618
1619 // Make sure we print out at least one level of the type structure, so
1620 // that we do not get %FILE = type %FILE
Chris Lattner0f7364b2009-02-28 21:26:53 +00001621 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001622 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001623 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001624}
1625
Misha Brukmanab5c6002004-03-02 00:22:19 +00001626/// printFunction - Print all aspects of a function.
1627///
Chris Lattner7e708292002-06-25 16:13:24 +00001628void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001629 // Print out the return type and name.
1630 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001631
Misha Brukman0313e0b2004-06-21 21:53:56 +00001632 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001633
Reid Spencer5cbf9852007-01-30 20:08:39 +00001634 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001635 Out << "declare ";
1636 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001637 Out << "define ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001638
Chris Lattnercfb5a202008-08-19 05:06:27 +00001639 PrintLinkage(F->getLinkage(), Out);
1640 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001641
Chris Lattnerd5118982005-05-06 20:26:43 +00001642 // Print the calling convention.
1643 switch (F->getCallingConv()) {
1644 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001645 case CallingConv::Fast: Out << "fastcc "; break;
1646 case CallingConv::Cold: Out << "coldcc "; break;
1647 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001648 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1649 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1650 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1651 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001652 case CallingConv::MSP430_INTR: Out << "msp430_intrcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001653 default: Out << "cc" << F->getCallingConv() << " "; break;
1654 }
1655
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001656 const FunctionType *FT = F->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00001657 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel652203f2008-09-29 20:49:50 +00001658 Attributes RetAttrs = Attrs.getRetAttributes();
1659 if (RetAttrs != Attribute::None)
1660 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001661 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001662 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001663 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001664 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001665 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001666
Chris Lattnerc1824992001-10-29 16:05:51 +00001667 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001668
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001669 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001670 if (!F->isDeclaration()) {
1671 // If this isn't a declaration, print the argument names as well.
1672 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1673 I != E; ++I) {
1674 // Insert commas as we go... the first arg doesn't get a comma
1675 if (I != F->arg_begin()) Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001676 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001677 Idx++;
1678 }
1679 } else {
1680 // Otherwise, print the types from the function type.
1681 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1682 // Insert commas as we go... the first arg doesn't get a comma
1683 if (i) Out << ", ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001684
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001685 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001686 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001687
Devang Patel19c87462008-09-26 22:53:05 +00001688 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel05988662008-09-25 21:00:45 +00001689 if (ArgAttrs != Attribute::None)
1690 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001691 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001692 }
Chris Lattner007377f2001-09-07 16:36:04 +00001693
1694 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001695 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001696 if (FT->getNumParams()) Out << ", ";
1697 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001698 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001699 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001700 Attributes FnAttrs = Attrs.getFnAttributes();
1701 if (FnAttrs != Attribute::None)
1702 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner60962db2005-11-12 00:10:19 +00001703 if (F->hasSection())
1704 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +00001705 if (F->getAlignment())
1706 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001707 if (F->hasGC())
1708 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001709 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001710 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001711 } else {
Chris Lattnera1b58582008-04-21 06:12:55 +00001712 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001713
Chris Lattnerb5794002002-04-07 22:49:37 +00001714 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001715 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1716 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001717
Misha Brukman0313e0b2004-06-21 21:53:56 +00001718 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001719 }
1720
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001721 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001722}
1723
Misha Brukmanab5c6002004-03-02 00:22:19 +00001724/// printArgument - This member is called for every argument that is passed into
1725/// the function. Simply print it out
1726///
Daniel Dunbara279bc32009-09-20 02:20:51 +00001727void AssemblyWriter::printArgument(const Argument *Arg,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001728 Attributes Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001729 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001730 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001731
Duncan Sandsdc024672007-11-27 13:23:08 +00001732 // Output parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001733 if (Attrs != Attribute::None)
1734 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001735
Chris Lattner00950542001-06-06 20:29:01 +00001736 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001737 if (Arg->hasName()) {
1738 Out << ' ';
1739 PrintLLVMName(Out, Arg);
1740 }
Chris Lattner00950542001-06-06 20:29:01 +00001741}
1742
Misha Brukmanab5c6002004-03-02 00:22:19 +00001743/// printBasicBlock - This member is called for each basic block in a method.
1744///
Chris Lattnerc1824992001-10-29 16:05:51 +00001745void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001746 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001747 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001748 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001749 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001750 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner96c5b2f2008-04-21 04:20:33 +00001751 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001752 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001753 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001754 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001755 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001756 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001757 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001758
Dan Gohman683e9222009-08-12 17:23:50 +00001759 if (BB->getParent() == 0) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001760 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001761 Out << "; Error: Block without parent!";
1762 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnereb411292008-04-22 02:45:44 +00001763 // Output predecessors for the block...
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001764 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001765 Out << ";";
Chris Lattnereb411292008-04-22 02:45:44 +00001766 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001767
Chris Lattnereb411292008-04-22 02:45:44 +00001768 if (PI == PE) {
1769 Out << " No predecessors!";
1770 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001771 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001772 writeOperand(*PI, false);
1773 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001774 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001775 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001776 }
Chris Lattner061269b2002-10-02 19:38:55 +00001777 }
Chris Lattner00950542001-06-06 20:29:01 +00001778 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001779
Chris Lattnereb411292008-04-22 02:45:44 +00001780 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001781
Misha Brukman0313e0b2004-06-21 21:53:56 +00001782 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001783
Chris Lattner007377f2001-09-07 16:36:04 +00001784 // Output all of the instructions in the basic block...
Dan Gohmanbeca6892009-07-13 18:27:59 +00001785 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner7e708292002-06-25 16:13:24 +00001786 printInstruction(*I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00001787 Out << '\n';
1788 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00001789
Misha Brukman0313e0b2004-06-21 21:53:56 +00001790 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001791}
1792
Chris Lattnere02fa852001-10-13 06:42:36 +00001793
Misha Brukmanab5c6002004-03-02 00:22:19 +00001794/// printInfoComment - Print a little comment after the instruction indicating
1795/// which slot it occupies.
1796///
Chris Lattner7e708292002-06-25 16:13:24 +00001797void AssemblyWriter::printInfoComment(const Value &V) {
Chris Lattner4ee93c42009-12-29 07:25:48 +00001798 if (V.getType()->isVoidTy()) return;
1799
1800 Out.PadToColumn(50);
1801 Out << "; <";
1802 TypePrinter.print(V.getType(), Out);
1803 Out << "> [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001804}
1805
Reid Spencer3a9ec242006-08-28 01:02:49 +00001806// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001807void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001808 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001809
Dan Gohman3845e502009-08-12 23:32:33 +00001810 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00001811 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00001812
1813 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001814 if (I.hasName()) {
1815 PrintLLVMName(Out, &I);
1816 Out << " = ";
Chris Lattner4ee93c42009-12-29 07:25:48 +00001817 } else if (!I.getType()->isVoidTy()) {
Chris Lattner828db8a2008-08-29 17:19:30 +00001818 // Print out the def slot taken.
1819 int SlotNum = Machine.getLocalSlot(&I);
1820 if (SlotNum == -1)
1821 Out << "<badref> = ";
1822 else
1823 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001824 }
Chris Lattner00950542001-06-06 20:29:01 +00001825
Chris Lattnerddb6db42005-05-06 05:51:46 +00001826 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001827 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001828 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001829 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001830 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1831 // If this is a call, check if it's a tail call.
1832 Out << "tail ";
1833 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001834
Chris Lattner00950542001-06-06 20:29:01 +00001835 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001836 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001837
Dan Gohman59858cf2009-07-27 16:11:46 +00001838 // Print out optimization information.
1839 WriteOptimizationInfo(Out, &I);
1840
Reid Spencer74f16422006-12-03 06:27:29 +00001841 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001842 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001843 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001844
Chris Lattner00950542001-06-06 20:29:01 +00001845 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001846 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001847
1848 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001849 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1850 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001851 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001852 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001853 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001854 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001855 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001856 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001857
Chris Lattner94dc1f22002-04-13 18:34:38 +00001858 } else if (isa<SwitchInst>(I)) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001859 // Special case switch instruction to get formatting nice and correct.
Dan Gohman8dae1382008-09-14 17:21:12 +00001860 Out << ' ';
Chris Lattnerab49ee72008-08-23 22:52:27 +00001861 writeOperand(Operand , true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001862 Out << ", ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001863 writeOperand(I.getOperand(1), true);
1864 Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001865
Chris Lattner7e708292002-06-25 16:13:24 +00001866 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Dan Gohman01889ca2009-08-13 01:41:52 +00001867 Out << "\n ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001868 writeOperand(I.getOperand(op ), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001869 Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001870 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001871 }
Dan Gohman01889ca2009-08-13 01:41:52 +00001872 Out << "\n ]";
Chris Lattnerab21db72009-10-28 00:19:10 +00001873 } else if (isa<IndirectBrInst>(I)) {
1874 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001875 Out << ' ';
1876 writeOperand(Operand, true);
Dan Gohman0ed1f422009-10-30 02:01:10 +00001877 Out << ", [";
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001878
1879 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1880 if (i != 1)
1881 Out << ", ";
1882 writeOperand(I.getOperand(i), true);
1883 }
1884 Out << ']';
Chris Lattnerb00c5822001-10-02 03:41:24 +00001885 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001886 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001887 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001888 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001889
Chris Lattner7e708292002-06-25 16:13:24 +00001890 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001891 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001892 Out << "[ ";
1893 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001894 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001895 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001896 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001897 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001898 writeOperand(I.getOperand(0), true);
1899 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1900 Out << ", " << *i;
1901 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001902 Out << ' ';
1903 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001904 writeOperand(I.getOperand(1), true);
1905 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1906 Out << ", " << *i;
Devang Patel57ef4f42008-02-23 00:35:18 +00001907 } else if (isa<ReturnInst>(I) && !Operand) {
1908 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001909 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1910 // Print the calling convention being used.
1911 switch (CI->getCallingConv()) {
1912 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001913 case CallingConv::Fast: Out << " fastcc"; break;
1914 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerb28a6dc2007-11-18 18:32:16 +00001915 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001916 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
1917 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1918 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1919 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001920 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001921 default: Out << " cc" << CI->getCallingConv(); break;
1922 }
1923
Reid Spencerb138a062007-04-09 06:10:42 +00001924 const PointerType *PTy = cast<PointerType>(Operand->getType());
1925 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1926 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001927 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001928
Devang Patel652203f2008-09-29 20:49:50 +00001929 if (PAL.getRetAttributes() != Attribute::None)
1930 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1931
Chris Lattner7a012292003-08-05 15:34:45 +00001932 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001933 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001934 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001935 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001936 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001937 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001938 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001939 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001940 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001941 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001942 writeOperand(Operand, false);
1943 } else {
1944 writeOperand(Operand, true);
1945 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001946 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001947 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1948 if (op > 1)
Dan Gohman8dae1382008-09-14 17:21:12 +00001949 Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001950 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner00950542001-06-06 20:29:01 +00001951 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001952 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001953 if (PAL.getFnAttributes() != Attribute::None)
1954 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001955 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencerb138a062007-04-09 06:10:42 +00001956 const PointerType *PTy = cast<PointerType>(Operand->getType());
1957 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1958 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001959 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001960
Chris Lattnerd5118982005-05-06 20:26:43 +00001961 // Print the calling convention being used.
1962 switch (II->getCallingConv()) {
1963 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001964 case CallingConv::Fast: Out << " fastcc"; break;
1965 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman8dae1382008-09-14 17:21:12 +00001966 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1967 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001968 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1969 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1970 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001971 case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001972 default: Out << " cc" << II->getCallingConv(); break;
1973 }
1974
Devang Patel652203f2008-09-29 20:49:50 +00001975 if (PAL.getRetAttributes() != Attribute::None)
1976 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1977
Chris Lattner7a012292003-08-05 15:34:45 +00001978 // If possible, print out the short form of the invoke instruction. We can
1979 // only do this if the first argument is a pointer to a nonvararg function,
1980 // and if the return type is not a pointer to a function.
1981 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001982 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001983 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001984 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001985 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001986 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001987 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001988 writeOperand(Operand, false);
1989 } else {
1990 writeOperand(Operand, true);
1991 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001992 Out << '(';
Gabor Greif03a5f132009-09-03 02:02:59 +00001993 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1994 if (op > 3)
Dan Gohman8dae1382008-09-14 17:21:12 +00001995 Out << ", ";
Gabor Greif03a5f132009-09-03 02:02:59 +00001996 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattnere02fa852001-10-13 06:42:36 +00001997 }
1998
Dan Gohman8dae1382008-09-14 17:21:12 +00001999 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00002000 if (PAL.getFnAttributes() != Attribute::None)
2001 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
2002
Dan Gohman01889ca2009-08-13 01:41:52 +00002003 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00002004 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00002005 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00002006 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00002007
Victor Hernandez7b929da2009-10-23 21:09:37 +00002008 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00002009 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00002010 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohman69bff072009-07-31 18:23:24 +00002011 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002012 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00002013 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00002014 }
Nate Begeman14b05292005-11-05 09:21:28 +00002015 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00002016 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00002017 }
Chris Lattnere02fa852001-10-13 06:42:36 +00002018 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002019 if (Operand) {
2020 Out << ' ';
2021 writeOperand(Operand, true); // Work with broken code
2022 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00002023 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00002024 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00002025 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002026 if (Operand) {
2027 Out << ' ';
2028 writeOperand(Operand, true); // Work with broken code
2029 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00002030 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00002031 TypePrinter.print(I.getType(), Out);
2032 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00002033
Misha Brukmanfd939082005-04-21 23:48:37 +00002034 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00002035 // omit the type from all but the first operand. If the instruction has
2036 // different type operands (for example br), then they are all printed.
2037 bool PrintAllTypes = false;
2038 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00002039
Reid Spencerebe57e32007-02-02 13:54:55 +00002040 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00002041 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
2042 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00002043 PrintAllTypes = true;
2044 } else {
2045 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
2046 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00002047 // note that Operand shouldn't be null, but the test helps make dump()
2048 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00002049 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00002050 PrintAllTypes = true; // We have differing types! Print them all!
2051 break;
2052 }
Chris Lattner00950542001-06-06 20:29:01 +00002053 }
2054 }
Misha Brukmanfd939082005-04-21 23:48:37 +00002055
Chris Lattnerc1824992001-10-29 16:05:51 +00002056 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00002057 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00002058 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00002059 }
Chris Lattner00950542001-06-06 20:29:01 +00002060
Dan Gohman8dae1382008-09-14 17:21:12 +00002061 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00002062 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002063 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00002064 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00002065 }
2066 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002067
Chris Lattner7d05c462009-12-28 20:10:43 +00002068 // Print post operand alignment for load/store.
Christopher Lamb43c7f372007-04-22 19:24:39 +00002069 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
2070 Out << ", align " << cast<LoadInst>(I).getAlignment();
2071 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
2072 Out << ", align " << cast<StoreInst>(I).getAlignment();
2073 }
Chris Lattner00950542001-06-06 20:29:01 +00002074
Chris Lattner7d05c462009-12-28 20:10:43 +00002075 // Print Metadata info.
Devang Patel7f93f4d2009-10-07 16:37:55 +00002076 if (!MDNames.empty()) {
Chris Lattner3990b122009-12-28 23:41:32 +00002077 SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD;
2078 I.getAllMetadata(InstMD);
2079 for (unsigned i = 0, e = InstMD.size(); i != e; ++i)
2080 Out << ", !" << MDNames[InstMD[i].first]
2081 << " !" << Machine.getMetadataSlot(InstMD[i].second);
Devang Patel7f93f4d2009-10-07 16:37:55 +00002082 }
Chris Lattnere02fa852001-10-13 06:42:36 +00002083 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00002084}
2085
2086
2087//===----------------------------------------------------------------------===//
2088// External Interface declarations
2089//===----------------------------------------------------------------------===//
2090
Dan Gohman683e9222009-08-12 17:23:50 +00002091void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00002092 SlotTracker SlotTable(this);
Dan Gohman683e9222009-08-12 17:23:50 +00002093 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002094 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002095 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00002096}
2097
Chris Lattner6d4306e2009-02-28 21:11:05 +00002098void Type::print(raw_ostream &OS) const {
2099 if (this == 0) {
2100 OS << "<null Type>";
2101 return;
2102 }
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002103 TypePrinting().print(this, OS);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002104}
2105
Dan Gohman683e9222009-08-12 17:23:50 +00002106void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002107 if (this == 0) {
Dan Gohman1220e102009-08-12 20:56:03 +00002108 ROS << "printing a <null> value\n";
Chris Lattner944fac72008-08-23 22:23:09 +00002109 return;
2110 }
Dan Gohman1220e102009-08-12 20:56:03 +00002111 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002112 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2113 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2114 SlotTracker SlotTable(F);
2115 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
2116 W.write(I);
2117 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2118 SlotTracker SlotTable(BB->getParent());
2119 AssemblyWriter W(OS, SlotTable,
2120 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
2121 W.write(BB);
2122 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2123 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00002124 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattner944fac72008-08-23 22:23:09 +00002125 W.write(GV);
Devang Patele54abc92009-07-22 17:43:22 +00002126 } else if (const MDString *MDS = dyn_cast<MDString>(this)) {
2127 TypePrinting TypePrinter;
2128 TypePrinter.print(MDS->getType(), OS);
2129 OS << ' ';
2130 OS << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00002131 PrintEscapedString(MDS->getString(), OS);
Devang Patele54abc92009-07-22 17:43:22 +00002132 OS << '"';
Devang Patelfcd65ae2009-07-01 20:59:15 +00002133 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Devang Patel320671d2009-07-08 21:44:25 +00002134 SlotTracker SlotTable(N);
Devang Patelfcd65ae2009-07-01 20:59:15 +00002135 TypePrinting TypePrinter;
Devang Patel320671d2009-07-08 21:44:25 +00002136 SlotTable.initialize();
2137 WriteMDNodes(OS, TypePrinter, SlotTable);
Devang Patelc29d5b32009-07-30 00:02:57 +00002138 } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) {
2139 SlotTracker SlotTable(N);
2140 TypePrinting TypePrinter;
2141 SlotTable.initialize();
2142 OS << "!" << N->getName() << " = !{";
2143 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
2144 if (i) OS << ", ";
Devang Patele8861b82009-07-30 01:02:04 +00002145 MDNode *MD = dyn_cast_or_null<MDNode>(N->getElement(i));
2146 if (MD)
2147 OS << '!' << SlotTable.getMetadataSlot(MD);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002148 else
Devang Patele8861b82009-07-30 01:02:04 +00002149 OS << "null";
Devang Patelc29d5b32009-07-30 00:02:57 +00002150 }
2151 OS << "}\n";
2152 WriteMDNodes(OS, TypePrinter, SlotTable);
Chris Lattner944fac72008-08-23 22:23:09 +00002153 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002154 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00002155 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00002156 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +00002157 WriteConstantInt(OS, C, TypePrinter, 0);
Chris Lattner944fac72008-08-23 22:23:09 +00002158 } else if (const Argument *A = dyn_cast<Argument>(this)) {
2159 WriteAsOperand(OS, this, true,
2160 A->getParent() ? A->getParent()->getParent() : 0);
2161 } else if (isa<InlineAsm>(this)) {
2162 WriteAsOperand(OS, this, true, 0);
2163 } else {
Dan Gohmancd26ec52009-09-23 01:33:16 +00002164 // Otherwise we don't know what it is. Call the virtual function to
2165 // allow a subclass to print itself.
2166 printCustom(OS);
Chris Lattner944fac72008-08-23 22:23:09 +00002167 }
2168}
2169
Dan Gohmancd26ec52009-09-23 01:33:16 +00002170// Value::printCustom - subclasses should override this to implement printing.
2171void Value::printCustom(raw_ostream &OS) const {
2172 llvm_unreachable("Unknown value to print out!");
2173}
2174
Chris Lattner7059e532008-08-25 17:03:15 +00002175// Value::dump - allow easy printing of Values from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00002176void Value::dump() const { print(errs()); errs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00002177
Chris Lattner7059e532008-08-25 17:03:15 +00002178// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner795daec2008-10-01 20:16:19 +00002179// This one uses type names from the given context module
2180void Type::dump(const Module *Context) const {
2181 WriteTypeSymbolic(errs(), this, Context);
2182 errs() << '\n';
Chris Lattner795daec2008-10-01 20:16:19 +00002183}
2184
Chris Lattnerc2871372009-02-28 21:05:51 +00002185// Type::dump - allow easy printing of Types from the debugger.
2186void Type::dump() const { dump(0); }
2187
Chris Lattner7059e532008-08-25 17:03:15 +00002188// Module::dump() - Allow printing of Modules from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00002189void Module::dump() const { print(errs(), 0); }