blob: 219fe0974d54fe1985faf38aadd9af846e85ba2f [file] [log] [blame]
Chris Lattnerf7e79482002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner189088e2002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattnerf70da102003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner189088e2002-04-12 18:21:53 +000014//
Chris Lattner2f7c9632001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner7f8845a2002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner8339f7d2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerf7b6d312005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner913d18f2002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner8bbcda22006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb952b542002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman2d3fa9e2004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Dan Gohman0ebd6962009-07-20 21:19:07 +000026#include "llvm/Operator.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000027#include "llvm/Metadata.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000028#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000029#include "llvm/ValueSymbolTable.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000030#include "llvm/TypeSymbolTable.h"
Chris Lattner3243ea12009-03-01 00:03:38 +000031#include "llvm/ADT/DenseSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000034#include "llvm/Support/CFG.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000035#include "llvm/Support/ErrorHandling.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000036#include "llvm/Support/MathExtras.h"
Dan Gohmane2745262009-08-12 17:23:50 +000037#include "llvm/Support/FormattedStream.h"
Chris Lattnerfee714f2001-09-07 16:36:04 +000038#include <algorithm>
Reid Spencerbdf03b42007-05-22 19:27:35 +000039#include <cctype>
Devang Patel39e64d42009-07-01 19:21:12 +000040#include <map>
Chris Lattner189d19f2003-11-21 20:23:48 +000041using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000042
Reid Spencer294715b2005-05-15 16:13:11 +000043// Make virtual table appear in this compilation unit.
44AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
45
Chris Lattner3eee99c2008-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 Dunbar7d6781b2009-09-20 02:20:51 +000053
Chris Lattner3eee99c2008-08-19 04:36:02 +000054 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
55 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000056
Chris Lattner3eee99c2008-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 Dunbar7d6781b2009-09-20 02:20:51 +000061
Chris Lattner3eee99c2008-08-19 04:36:02 +000062 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
63 return GV->getParent();
64 return 0;
65}
66
Daniel Dunbardb0b70a2008-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 Gohmane2745262009-08-12 17:23:50 +000069static void PrintEscapedString(const StringRef &Name,
Dan Gohman12dad632009-08-12 20:56:03 +000070 raw_ostream &Out) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +000071 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
72 unsigned char C = Name[i];
Nick Lewycky5aa592a2009-03-15 06:39:52 +000073 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbardb0b70a2008-10-28 19:33:02 +000074 Out << C;
75 else
76 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
77 }
78}
79
Chris Lattner3eee99c2008-08-19 04:36:02 +000080enum PrefixType {
81 GlobalPrefix,
82 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +000083 LocalPrefix,
84 NoPrefix
Chris Lattner3eee99c2008-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 Gohman12dad632009-08-12 20:56:03 +000090static void PrintLLVMName(raw_ostream &OS, const StringRef &Name,
Daniel Dunbare03eecb2009-07-25 23:55:21 +000091 PrefixType Prefix) {
92 assert(Name.data() && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +000093 switch (Prefix) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000094 default: llvm_unreachable("Bad prefix!");
Daniel Dunbar389529a2008-10-14 23:28:09 +000095 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +000096 case GlobalPrefix: OS << '@'; break;
97 case LabelPrefix: break;
98 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +000099 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000100
Chris Lattner3eee99c2008-08-19 04:36:02 +0000101 // Scan the name to see if it needs quotes first.
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000102 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000103 if (!NeedsQuotes) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000104 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
105 char C = Name[i];
Chris Lattner3eee99c2008-08-19 04:36:02 +0000106 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
107 NeedsQuotes = true;
108 break;
109 }
110 }
111 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000112
Chris Lattner3eee99c2008-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 Dunbare03eecb2009-07-25 23:55:21 +0000115 OS << Name;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000116 return;
117 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000118
Chris Lattner3eee99c2008-08-19 04:36:02 +0000119 // Okay, we need quotes. Output the quotes and escape any scary characters as
120 // needed.
121 OS << '"';
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000122 PrintEscapedString(Name, OS);
Chris Lattner3eee99c2008-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 Gohman12dad632009-08-12 20:56:03 +0000129static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000130 PrintLLVMName(OS, V->getName(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000131 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
132}
133
Chris Lattner0f578952009-02-28 20:25:14 +0000134//===----------------------------------------------------------------------===//
135// TypePrinting Class: Type printing machinery
136//===----------------------------------------------------------------------===//
137
Chris Lattner3339d7f2009-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 Lattner3ad46822009-02-28 22:34:45 +0000140}
141
142void TypePrinting::clear() {
143 getTypeNamesMap(TypeNames).clear();
144}
Chris Lattner0f578952009-02-28 20:25:14 +0000145
Chris Lattner92c5c122009-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 Lattner3339d7f2009-02-28 23:03:55 +0000156 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner0f578952009-02-28 20:25:14 +0000157}
158
Chris Lattner3ad46822009-02-28 22:34:45 +0000159TypePrinting::~TypePrinting() {
160 delete &getTypeNamesMap(TypeNames);
161}
162
Chris Lattner40959d02009-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 Lattner765338d2009-02-28 20:34:19 +0000166 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattner242d91a2009-03-01 01:16:21 +0000167 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner0f578952009-02-28 20:25:14 +0000168 // Check to see if the type is named.
Chris Lattner242d91a2009-03-01 01:16:21 +0000169 if (!IgnoreTopLevelName) {
Nick Lewycky063699a2009-03-19 06:31:22 +0000170 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattner242d91a2009-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 Lattner0f578952009-02-28 20:25:14 +0000176 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000177
Chris Lattner0f578952009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000181
Chris Lattner0f578952009-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 Lattner06a23212009-02-28 21:27:31 +0000186 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner0f578952009-02-28 20:25:14 +0000187 return;
188 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000189
Chris Lattner0f578952009-02-28 20:25:14 +0000190 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000191
Chris Lattner0f578952009-02-28 20:25:14 +0000192 switch (Ty->getTypeID()) {
Chris Lattner06a23212009-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 Lewyckyadbc2842009-05-30 05:06:04 +0000200 case Type::MetadataTyID: OS << "metadata"; break;
Chris Lattner68318b12009-02-28 21:18:43 +0000201 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000202 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner68318b12009-02-28 21:18:43 +0000203 break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000204
Chris Lattner07d88292009-02-28 20:35:42 +0000205 case Type::FunctionTyID: {
206 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000207 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
208 OS << " (";
Chris Lattner07d88292009-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 Lattner06a23212009-02-28 21:27:31 +0000212 OS << ", ";
213 CalcTypeName(*I, TypeStack, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000214 }
Chris Lattner07d88292009-02-28 20:35:42 +0000215 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000216 if (FTy->getNumParams()) OS << ", ";
217 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000218 }
Chris Lattner06a23212009-02-28 21:27:31 +0000219 OS << ')';
Chris Lattner07d88292009-02-28 20:35:42 +0000220 break;
221 }
222 case Type::StructTyID: {
223 const StructType *STy = cast<StructType>(Ty);
224 if (STy->isPacked())
Chris Lattner06a23212009-02-28 21:27:31 +0000225 OS << '<';
226 OS << "{ ";
Chris Lattner07d88292009-02-28 20:35:42 +0000227 for (StructType::element_iterator I = STy->element_begin(),
228 E = STy->element_end(); I != E; ++I) {
Chris Lattner06a23212009-02-28 21:27:31 +0000229 CalcTypeName(*I, TypeStack, OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000230 if (next(I) != STy->element_end())
Chris Lattner06a23212009-02-28 21:27:31 +0000231 OS << ',';
232 OS << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +0000233 }
Chris Lattner06a23212009-02-28 21:27:31 +0000234 OS << '}';
Chris Lattner07d88292009-02-28 20:35:42 +0000235 if (STy->isPacked())
Chris Lattner06a23212009-02-28 21:27:31 +0000236 OS << '>';
Chris Lattner07d88292009-02-28 20:35:42 +0000237 break;
238 }
239 case Type::PointerTyID: {
240 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000241 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000242 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000243 OS << " addrspace(" << AddressSpace << ')';
244 OS << '*';
Chris Lattner07d88292009-02-28 20:35:42 +0000245 break;
246 }
247 case Type::ArrayTyID: {
248 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000249 OS << '[' << ATy->getNumElements() << " x ";
250 CalcTypeName(ATy->getElementType(), TypeStack, OS);
251 OS << ']';
Chris Lattner07d88292009-02-28 20:35:42 +0000252 break;
253 }
254 case Type::VectorTyID: {
255 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000256 OS << "<" << PTy->getNumElements() << " x ";
257 CalcTypeName(PTy->getElementType(), TypeStack, OS);
258 OS << '>';
Chris Lattner07d88292009-02-28 20:35:42 +0000259 break;
260 }
261 case Type::OpaqueTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000262 OS << "opaque";
Chris Lattner07d88292009-02-28 20:35:42 +0000263 break;
264 default:
Chris Lattner06a23212009-02-28 21:27:31 +0000265 OS << "<unrecognized-type>";
Chris Lattner07d88292009-02-28 20:35:42 +0000266 break;
Chris Lattner0f578952009-02-28 20:25:14 +0000267 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000268
Chris Lattner40959d02009-02-28 20:49:40 +0000269 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner0f578952009-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 Lattner242d91a2009-03-01 01:16:21 +0000275void TypePrinting::print(const Type *Ty, raw_ostream &OS,
276 bool IgnoreTopLevelName) {
Chris Lattner0f578952009-02-28 20:25:14 +0000277 // Check to see if the type is named.
Chris Lattner3339d7f2009-02-28 23:03:55 +0000278 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattner242d91a2009-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 Lattner0f578952009-02-28 20:25:14 +0000285 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000286
Chris Lattner0f578952009-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 Lattner765338d2009-02-28 20:34:19 +0000290 SmallVector<const Type *, 16> TypeStack;
Chris Lattner0f578952009-02-28 20:25:14 +0000291 std::string TypeName;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000292
Chris Lattner40959d02009-02-28 20:49:40 +0000293 raw_string_ostream TypeOS(TypeName);
Chris Lattner242d91a2009-03-01 01:16:21 +0000294 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner40959d02009-02-28 20:49:40 +0000295 OS << TypeOS.str();
296
297 // Cache type name for later use.
Chris Lattner242d91a2009-03-01 01:16:21 +0000298 if (!IgnoreTopLevelName)
299 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner0f578952009-02-28 20:25:14 +0000300}
301
Chris Lattner3243ea12009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000308
Chris Lattner3243ea12009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000314
Chris Lattner3243ea12009-03-01 00:03:38 +0000315 void Run(const Module &M) {
Chris Lattner84516482009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000322
Chris Lattner3243ea12009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000330
Chris Lattner3243ea12009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000337
Chris Lattner3243ea12009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000341
Chris Lattner3243ea12009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000355
Chris Lattner3243ea12009-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 Lattner84516482009-03-01 00:32:33 +0000359 if (!VisitedTypes.insert(Ty).second)
Chris Lattner3243ea12009-03-01 00:03:38 +0000360 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000361
Chris Lattner3243ea12009-03-01 00:03:38 +0000362 // If this is a structure or opaque type, add a name for the type.
Nick Lewycky49f89192009-04-04 07:22:01 +0000363 if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
364 || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
Chris Lattner3243ea12009-03-01 00:03:38 +0000365 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
366 NumberedTypes.push_back(Ty);
367 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000368
Chris Lattner3243ea12009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000372 IncorporateType(*I);
Chris Lattner3243ea12009-03-01 00:03:38 +0000373 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000374
Chris Lattner3243ea12009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000381
Chris Lattner3243ea12009-03-01 00:03:38 +0000382 // Already visited?
383 if (!VisitedConstants.insert(V).second)
384 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000385
Chris Lattner3243ea12009-03-01 00:03:38 +0000386 // Check this type.
387 IncorporateType(V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000388
Chris Lattner3243ea12009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000402static void AddModuleTypesToPrinter(TypePrinting &TP,
Chris Lattner3243ea12009-03-01 00:03:38 +0000403 std::vector<const Type*> &NumberedTypes,
404 const Module *M) {
Chris Lattner92c5c122009-02-28 23:20:19 +0000405 if (M == 0) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000406
Chris Lattner92c5c122009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000413
Chris Lattner92c5c122009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000422
Chris Lattner92c5c122009-02-28 23:20:19 +0000423 // Likewise don't insert primitives either.
424 if (Ty->isInteger() || Ty->isPrimitiveType())
425 continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000426
Chris Lattner92c5c122009-02-28 23:20:19 +0000427 // Get the name as a string and insert it into TypeNames.
428 std::string NameStr;
Dan Gohmane2745262009-08-12 17:23:50 +0000429 raw_string_ostream NameROS(NameStr);
430 formatted_raw_ostream NameOS(NameROS);
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000431 PrintLLVMName(NameOS, TI->first, LocalPrefix);
Dan Gohmane2745262009-08-12 17:23:50 +0000432 NameOS.flush();
433 TP.addTypeName(Ty, NameStr);
Chris Lattner92c5c122009-02-28 23:20:19 +0000434 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000435
Chris Lattner3243ea12009-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 Lattner92c5c122009-02-28 23:20:19 +0000441}
442
Chris Lattner0f578952009-02-28 20:25:14 +0000443
Chris Lattner0f578952009-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 Lattner24025262009-02-28 21:05:51 +0000446/// type or one of it's component types.
Chris Lattner0f578952009-02-28 20:25:14 +0000447///
Chris Lattner92c5c122009-02-28 23:20:19 +0000448void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
449 TypePrinting Printer;
Chris Lattner3243ea12009-03-01 00:03:38 +0000450 std::vector<const Type*> NumberedTypes;
451 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattner92c5c122009-02-28 23:20:19 +0000452 Printer.print(Ty, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000453}
454
Chris Lattner3eee99c2008-08-19 04:36:02 +0000455//===----------------------------------------------------------------------===//
456// SlotTracker Class: Enumerate slot numbers for unnamed values
457//===----------------------------------------------------------------------===//
458
Chris Lattner3ee58762008-08-19 04:28:07 +0000459namespace {
460
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000461/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000462///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000463class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000464public:
Devang Patel983c6b12009-07-08 21:44:25 +0000465 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000466 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000467
468private:
Devang Patel983c6b12009-07-08 21:44:25 +0000469 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000470 const Module* TheModule;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000471
Devang Patel983c6b12009-07-08 21:44:25 +0000472 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000473 const Function* TheFunction;
474 bool FunctionProcessed;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000475
Devang Patel983c6b12009-07-08 21:44:25 +0000476 /// TheMDNode - The MDNode for which we are holding slot numbers.
477 const MDNode *TheMDNode;
478
Devang Patelb4a4e772009-07-30 00:02:57 +0000479 /// TheNamedMDNode - The MDNode for which we are holding slot numbers.
480 const NamedMDNode *TheNamedMDNode;
481
Devang Patel983c6b12009-07-08 21:44:25 +0000482 /// mMap - The TypePlanes map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000483 ValueMap mMap;
484 unsigned mNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000485
Devang Patel983c6b12009-07-08 21:44:25 +0000486 /// fMap - The TypePlanes map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000487 ValueMap fMap;
488 unsigned fNext;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000489
Devang Patel983c6b12009-07-08 21:44:25 +0000490 /// mdnMap - Map for MDNodes.
491 ValueMap mdnMap;
492 unsigned mdnNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000493public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000494 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000495 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000496 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000497 explicit SlotTracker(const Function *F);
Devang Patel983c6b12009-07-08 21:44:25 +0000498 /// Construct from a mdnode.
499 explicit SlotTracker(const MDNode *N);
Devang Patelb4a4e772009-07-30 00:02:57 +0000500 /// Construct from a named mdnode.
501 explicit SlotTracker(const NamedMDNode *N);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000502
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000503 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000504 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000505 int getLocalSlot(const Value *V);
506 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000507 int getMetadataSlot(const MDNode *N);
Reid Spencer8beac692004-06-09 15:26:53 +0000508
Misha Brukmanb1c93172005-04-21 23:48:37 +0000509 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000510 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000511 void incorporateFunction(const Function *F) {
512 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000513 FunctionProcessed = false;
514 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000515
Misha Brukmanb1c93172005-04-21 23:48:37 +0000516 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000517 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000518 /// will reset the state of the machine back to just the module contents.
519 void purgeFunction();
520
Devang Patel983c6b12009-07-08 21:44:25 +0000521 /// MDNode map iterators.
522 ValueMap::iterator mdnBegin() { return mdnMap.begin(); }
523 ValueMap::iterator mdnEnd() { return mdnMap.end(); }
Dan Gohman69273e62009-08-12 23:54:22 +0000524 unsigned mdnSize() const { return mdnMap.size(); }
525 bool mdnEmpty() const { return mdnMap.empty(); }
Devang Patel983c6b12009-07-08 21:44:25 +0000526
Reid Spencer56010e42004-05-26 21:56:09 +0000527 /// This function does the actual initialization.
528 inline void initialize();
529
Devang Patel983c6b12009-07-08 21:44:25 +0000530 // Implementation Details
531private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000532 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
533 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000534
535 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
536 void CreateMetadataSlot(const MDNode *N);
537
Chris Lattnerea862a32007-01-09 07:55:49 +0000538 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
539 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000540
Reid Spencer16f2f7f2004-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 Patel983c6b12009-07-08 21:44:25 +0000545 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000546 void processFunction();
547
Devang Patel983c6b12009-07-08 21:44:25 +0000548 /// Add all MDNode operands.
549 void processMDNode();
550
Devang Patelb4a4e772009-07-30 00:02:57 +0000551 /// Add all MDNode operands.
552 void processNamedMDNode();
553
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000554 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
555 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000556};
557
Chris Lattner3ee58762008-08-19 04:28:07 +0000558} // end anonymous namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000559
Chris Lattner3eee99c2008-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 Dunbar7d6781b2009-09-20 02:20:51 +0000564
Chris Lattner3eee99c2008-08-19 04:36:02 +0000565 if (const Instruction *I = dyn_cast<Instruction>(V))
566 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000567
Chris Lattner3eee99c2008-08-19 04:36:02 +0000568 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
569 return new SlotTracker(BB->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000570
Chris Lattner3eee99c2008-08-19 04:36:02 +0000571 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
572 return new SlotTracker(GV->getParent());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000573
Chris Lattner3eee99c2008-08-19 04:36:02 +0000574 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000575 return new SlotTracker(GA->getParent());
576
Chris Lattner3eee99c2008-08-19 04:36:02 +0000577 if (const Function *Func = dyn_cast<Function>(V))
578 return new SlotTracker(Func);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000579
Chris Lattner3eee99c2008-08-19 04:36:02 +0000580 return 0;
581}
582
583#if 0
Dan Gohman1ddf98a2009-07-25 01:43:01 +0000584#define ST_DEBUG(X) errs() << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000585#else
Chris Lattner604e3512008-08-19 04:47:09 +0000586#define ST_DEBUG(X)
Chris Lattner3eee99c2008-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 Patel983c6b12009-07-08 21:44:25 +0000592 : TheModule(M), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
Devang Patelb4a4e772009-07-30 00:02:57 +0000593 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-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 Lattner3a36d2c2008-08-19 05:06:27 +0000599 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Devang Patelb4a4e772009-07-30 00:02:57 +0000600 TheMDNode(0), TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Devang Patel983c6b12009-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 Patelb4a4e772009-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 Lattner3eee99c2008-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 Dunbar7d6781b2009-09-20 02:20:51 +0000620
Chris Lattner3eee99c2008-08-19 04:36:02 +0000621 if (TheFunction && !FunctionProcessed)
622 processFunction();
Devang Patel983c6b12009-07-08 21:44:25 +0000623
624 if (TheMDNode)
625 processMDNode();
Devang Patelb4a4e772009-07-30 00:02:57 +0000626
627 if (TheNamedMDNode)
628 processNamedMDNode();
Chris Lattner3eee99c2008-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 Lattner604e3512008-08-19 04:47:09 +0000634 ST_DEBUG("begin processModule!\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000635
Chris Lattner3eee99c2008-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 Patel983c6b12009-07-08 21:44:25 +0000638 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000639 if (!I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000640 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000641 if (I->hasInitializer()) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000642 if (MDNode *N = dyn_cast<MDNode>(I->getInitializer()))
Devang Patel983c6b12009-07-08 21:44:25 +0000643 CreateMetadataSlot(N);
644 }
645 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000646
Devang Patel23e68302009-07-29 22:04:47 +0000647 // Add metadata used by named metadata.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000648 for (Module::const_named_metadata_iterator
Devang Patel23e68302009-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 Patel847fcac2009-07-30 01:02:04 +0000654 if (MD)
655 CreateMetadataSlot(MD);
Devang Patel23e68302009-07-29 22:04:47 +0000656 }
657 }
658
Chris Lattner3eee99c2008-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 Dunbar7d6781b2009-09-20 02:20:51 +0000664
Chris Lattner604e3512008-08-19 04:47:09 +0000665 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000666}
667
Chris Lattner3eee99c2008-08-19 04:36:02 +0000668// Process the arguments, basic blocks, and instructions of a function.
669void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000670 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000671 fNext = 0;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000672
Chris Lattner3eee99c2008-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 Dunbar7d6781b2009-09-20 02:20:51 +0000678
Chris Lattner604e3512008-08-19 04:47:09 +0000679 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000680
Devang Pateldec23fd2009-09-16 20:21:17 +0000681 Metadata &TheMetadata = TheFunction->getContext().getMetadata();
682
Chris Lattner3eee99c2008-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);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000688 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel983c6b12009-07-08 21:44:25 +0000689 ++I) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000690 if (I->getType() != Type::getVoidTy(TheFunction->getContext()) &&
691 !I->hasName())
Chris Lattner3eee99c2008-08-19 04:36:02 +0000692 CreateFunctionSlot(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000693 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dan Gohmand04f9bf2009-08-17 15:28:08 +0000694 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
Devang Patel983c6b12009-07-08 21:44:25 +0000695 CreateMetadataSlot(N);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000696
Devang Pateldec23fd2009-09-16 20:21:17 +0000697 // Process metadata attached with this instruction.
698 const Metadata::MDMapTy *MDs = TheMetadata.getMDs(I);
699 if (MDs)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000700 for (Metadata::MDMapTy::const_iterator MI = MDs->begin(),
701 ME = MDs->end(); MI != ME; ++MI)
702 if (MDNode *MDN = dyn_cast_or_null<MDNode>(MI->second))
703 CreateMetadataSlot(MDN);
Devang Patel983c6b12009-07-08 21:44:25 +0000704 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000705 }
Devang Pateldec23fd2009-09-16 20:21:17 +0000706
Chris Lattner3eee99c2008-08-19 04:36:02 +0000707 FunctionProcessed = true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000708
Chris Lattner604e3512008-08-19 04:47:09 +0000709 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000710}
711
Devang Patel983c6b12009-07-08 21:44:25 +0000712/// processMDNode - Process TheMDNode.
713void SlotTracker::processMDNode() {
714 ST_DEBUG("begin processMDNode!\n");
715 mdnNext = 0;
716 CreateMetadataSlot(TheMDNode);
717 TheMDNode = 0;
718 ST_DEBUG("end processMDNode!\n");
719}
720
Devang Patelb4a4e772009-07-30 00:02:57 +0000721/// processNamedMDNode - Process TheNamedMDNode.
722void SlotTracker::processNamedMDNode() {
723 ST_DEBUG("begin processNamedMDNode!\n");
724 mdnNext = 0;
725 for (unsigned i = 0, e = TheNamedMDNode->getNumElements(); i != e; ++i) {
726 MDNode *MD = dyn_cast_or_null<MDNode>(TheNamedMDNode->getElement(i));
727 if (MD)
728 CreateMetadataSlot(MD);
729 }
730 TheNamedMDNode = 0;
731 ST_DEBUG("end processNamedMDNode!\n");
732}
733
Chris Lattner3eee99c2008-08-19 04:36:02 +0000734/// Clean up after incorporating a function. This is the only way to get out of
735/// the function incorporation state that affects get*Slot/Create*Slot. Function
736/// incorporation state is indicated by TheFunction != 0.
737void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000738 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000739 fMap.clear(); // Simply discard the function level map
740 TheFunction = 0;
741 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000742 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000743}
744
745/// getGlobalSlot - Get the slot number of a global value.
746int SlotTracker::getGlobalSlot(const GlobalValue *V) {
747 // Check for uninitialized state and do lazy initialization.
748 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000749
Chris Lattner3eee99c2008-08-19 04:36:02 +0000750 // Find the type plane in the module map
751 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000752 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000753}
754
Devang Patel983c6b12009-07-08 21:44:25 +0000755/// getGlobalSlot - Get the slot number of a MDNode.
756int SlotTracker::getMetadataSlot(const MDNode *N) {
757 // Check for uninitialized state and do lazy initialization.
758 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000759
Devang Patel983c6b12009-07-08 21:44:25 +0000760 // Find the type plane in the module map
761 ValueMap::iterator MI = mdnMap.find(N);
762 return MI == mdnMap.end() ? -1 : (int)MI->second;
763}
764
Chris Lattner3eee99c2008-08-19 04:36:02 +0000765
766/// getLocalSlot - Get the slot number for a value that is local to a function.
767int SlotTracker::getLocalSlot(const Value *V) {
768 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000769
Chris Lattner3eee99c2008-08-19 04:36:02 +0000770 // Check for uninitialized state and do lazy initialization.
771 initialize();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000772
Chris Lattner3eee99c2008-08-19 04:36:02 +0000773 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000774 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000775}
776
777
778/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
779void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
780 assert(V && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000781 assert(V->getType() != Type::getVoidTy(V->getContext()) &&
Owen Anderson55f1c092009-08-13 21:58:54 +0000782 "Doesn't need a slot!");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000783 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000784
Chris Lattner3eee99c2008-08-19 04:36:02 +0000785 unsigned DestSlot = mNext++;
786 mMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000787
Chris Lattner604e3512008-08-19 04:47:09 +0000788 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000789 DestSlot << " [");
790 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000791 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000792 (isa<Function>(V) ? 'F' :
793 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
794}
795
Chris Lattner3eee99c2008-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) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000798 assert(V->getType() != Type::getVoidTy(TheFunction->getContext()) &&
Owen Anderson55f1c092009-08-13 21:58:54 +0000799 !V->hasName() && "Doesn't need a slot!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000800
Chris Lattner3eee99c2008-08-19 04:36:02 +0000801 unsigned DestSlot = fNext++;
802 fMap[V] = DestSlot;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000803
Chris Lattner3eee99c2008-08-19 04:36:02 +0000804 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000805 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000806 DestSlot << " [o]\n");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000807}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000808
Devang Patel983c6b12009-07-08 21:44:25 +0000809/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
810void SlotTracker::CreateMetadataSlot(const MDNode *N) {
811 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000812
Devang Patel983c6b12009-07-08 21:44:25 +0000813 ValueMap::iterator I = mdnMap.find(N);
814 if (I != mdnMap.end())
815 return;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000816
Devang Patel983c6b12009-07-08 21:44:25 +0000817 unsigned DestSlot = mdnNext++;
818 mdnMap[N] = DestSlot;
819
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000820 for (MDNode::const_elem_iterator MDI = N->elem_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000821 MDE = N->elem_end(); MDI != MDE; ++MDI) {
822 const Value *TV = *MDI;
823 if (TV)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000824 if (const MDNode *N2 = dyn_cast<MDNode>(TV))
Devang Patel983c6b12009-07-08 21:44:25 +0000825 CreateMetadataSlot(N2);
826 }
827}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000828
829//===----------------------------------------------------------------------===//
830// AsmWriter Implementation
831//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000832
Dan Gohman12dad632009-08-12 20:56:03 +0000833static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +0000834 TypePrinting *TypePrinter,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000835 SlotTracker *Machine);
Reid Spencer58d30f22004-07-04 11:50:43 +0000836
Chris Lattner033935d2008-08-17 04:40:13 +0000837
Chris Lattnerb86620e2001-10-29 16:37:48 +0000838
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000839static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000840 const char * pred = "unknown";
841 switch (predicate) {
842 case FCmpInst::FCMP_FALSE: pred = "false"; break;
843 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
844 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
845 case FCmpInst::FCMP_OGE: pred = "oge"; break;
846 case FCmpInst::FCMP_OLT: pred = "olt"; break;
847 case FCmpInst::FCMP_OLE: pred = "ole"; break;
848 case FCmpInst::FCMP_ONE: pred = "one"; break;
849 case FCmpInst::FCMP_ORD: pred = "ord"; break;
850 case FCmpInst::FCMP_UNO: pred = "uno"; break;
851 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
852 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
853 case FCmpInst::FCMP_UGE: pred = "uge"; break;
854 case FCmpInst::FCMP_ULT: pred = "ult"; break;
855 case FCmpInst::FCMP_ULE: pred = "ule"; break;
856 case FCmpInst::FCMP_UNE: pred = "une"; break;
857 case FCmpInst::FCMP_TRUE: pred = "true"; break;
858 case ICmpInst::ICMP_EQ: pred = "eq"; break;
859 case ICmpInst::ICMP_NE: pred = "ne"; break;
860 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
861 case ICmpInst::ICMP_SGE: pred = "sge"; break;
862 case ICmpInst::ICMP_SLT: pred = "slt"; break;
863 case ICmpInst::ICMP_SLE: pred = "sle"; break;
864 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
865 case ICmpInst::ICMP_UGE: pred = "uge"; break;
866 case ICmpInst::ICMP_ULT: pred = "ult"; break;
867 case ICmpInst::ICMP_ULE: pred = "ule"; break;
868 }
869 return pred;
870}
871
Dan Gohmane2745262009-08-12 17:23:50 +0000872static void WriteMDNodes(formatted_raw_ostream &Out, TypePrinting &TypePrinter,
Devang Patel983c6b12009-07-08 21:44:25 +0000873 SlotTracker &Machine) {
874 SmallVector<const MDNode *, 16> Nodes;
875 Nodes.resize(Machine.mdnSize());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000876 for (SlotTracker::ValueMap::iterator I =
877 Machine.mdnBegin(), E = Machine.mdnEnd(); I != E; ++I)
Devang Patel983c6b12009-07-08 21:44:25 +0000878 Nodes[I->second] = cast<MDNode>(I->first);
879
880 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Devang Patelf04d63a2009-07-08 21:57:07 +0000881 Out << '!' << i << " = metadata ";
Devang Patel983c6b12009-07-08 21:44:25 +0000882 const MDNode *Node = Nodes[i];
883 Out << "!{";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000884 for (MDNode::const_elem_iterator NI = Node->elem_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000885 NE = Node->elem_end(); NI != NE;) {
886 const Value *V = *NI;
887 if (!V)
888 Out << "null";
889 else if (const MDNode *N = dyn_cast<MDNode>(V)) {
890 Out << "metadata ";
891 Out << '!' << Machine.getMetadataSlot(N);
892 }
893 else {
894 TypePrinter.print((*NI)->getType(), Out);
895 Out << ' ';
Dan Gohman8061d9e2009-08-13 15:27:57 +0000896 WriteAsOperandInternal(Out, *NI, &TypePrinter, &Machine);
Devang Patel983c6b12009-07-08 21:44:25 +0000897 }
898 if (++NI != NE)
899 Out << ", ";
900 }
901 Out << "}\n";
902 }
903}
904
Dan Gohman12dad632009-08-12 20:56:03 +0000905static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman0ebd6962009-07-20 21:19:07 +0000906 if (const OverflowingBinaryOperator *OBO =
907 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman16f54152009-08-20 17:11:38 +0000908 if (OBO->hasNoUnsignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000909 Out << " nuw";
Dan Gohman16f54152009-08-20 17:11:38 +0000910 if (OBO->hasNoSignedWrap())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000911 Out << " nsw";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000912 } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) {
913 if (Div->isExact())
Dan Gohman9c7f8082009-07-27 16:11:46 +0000914 Out << " exact";
Dan Gohman1639c392009-07-27 21:53:46 +0000915 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
916 if (GEP->isInBounds())
917 Out << " inbounds";
Dan Gohman0ebd6962009-07-20 21:19:07 +0000918 }
919}
920
Dan Gohman12dad632009-08-12 20:56:03 +0000921static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattner0f578952009-02-28 20:25:14 +0000922 TypePrinting &TypePrinter, SlotTracker *Machine) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000923 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000924 if (CI->getType() == Type::getInt1Ty(CV->getContext())) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000925 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000926 return;
927 }
928 Out << CI->getValue();
929 return;
930 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000931
Chris Lattner17f71652008-08-17 07:19:36 +0000932 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000933 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
934 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
935 // We would like to output the FP constant value in exponential notation,
936 // but we cannot do this if doing so will lose precision. Check here to
937 // make sure that we only output it in exponential format if we can parse
938 // the value back and get the same value.
939 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000940 bool ignored;
Dale Johannesen028084e2007-09-12 03:30:33 +0000941 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattner17f71652008-08-17 07:19:36 +0000942 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
943 CFP->getValueAPF().convertToFloat();
Dale Johannesen028084e2007-09-12 03:30:33 +0000944 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner1e194682002-04-18 18:53:13 +0000945
Dale Johannesen028084e2007-09-12 03:30:33 +0000946 // Check to make sure that the stringized number is not some string like
947 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
948 // that the string matches the "[-+]?[0-9]" regex.
949 //
950 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
951 ((StrVal[0] == '-' || StrVal[0] == '+') &&
952 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
953 // Reparse stringized version!
954 if (atof(StrVal.c_str()) == Val) {
955 Out << StrVal;
956 return;
957 }
Chris Lattner1e194682002-04-18 18:53:13 +0000958 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000959 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000960 // output the string in hexadecimal format! Note that loading and storing
961 // floating point types changes the bits of NaNs on some hosts, notably
962 // x86, so we must not use these types.
Dale Johannesen028084e2007-09-12 03:30:33 +0000963 assert(sizeof(double) == sizeof(uint64_t) &&
964 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000965 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000966 APFloat apf = CFP->getValueAPF();
967 // Floats are represented in ASCII IR as double, convert.
968 if (!isDouble)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000969 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen1f864982009-01-21 20:32:55 +0000970 &ignored);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000971 Out << "0x" <<
972 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen1f864982009-01-21 20:32:55 +0000973 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000974 return;
975 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000976
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000977 // Some form of long double. These appear as a magic letter identifying
978 // the type, then a fixed number of hex digits.
979 Out << "0x";
Dale Johannesen93eefa02009-03-23 21:16:53 +0000980 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000981 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +0000982 // api needed to prevent premature destruction
983 APInt api = CFP->getValueAPF().bitcastToAPInt();
984 const uint64_t* p = api.getRawData();
985 uint64_t word = p[1];
986 int shiftcount=12;
987 int width = api.getBitWidth();
988 for (int j=0; j<width; j+=4, shiftcount-=4) {
989 unsigned int nibble = (word>>shiftcount) & 15;
990 if (nibble < 10)
991 Out << (unsigned char)(nibble + '0');
992 else
993 Out << (unsigned char)(nibble - 10 + 'A');
994 if (shiftcount == 0 && j+4 < width) {
995 word = *p;
996 shiftcount = 64;
997 if (width-j-4 < 64)
998 shiftcount = width-j-4;
999 }
1000 }
1001 return;
1002 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001003 Out << 'L';
1004 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1005 Out << 'M';
1006 else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001007 llvm_unreachable("Unsupported floating point type");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001008 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +00001009 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001010 const uint64_t* p = api.getRawData();
1011 uint64_t word = *p;
1012 int shiftcount=60;
1013 int width = api.getBitWidth();
1014 for (int j=0; j<width; j+=4, shiftcount-=4) {
1015 unsigned int nibble = (word>>shiftcount) & 15;
1016 if (nibble < 10)
1017 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +00001018 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001019 Out << (unsigned char)(nibble - 10 + 'A');
1020 if (shiftcount == 0 && j+4 < width) {
1021 word = *(++p);
1022 shiftcount = 64;
1023 if (width-j-4 < 64)
1024 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +00001025 }
1026 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001027 return;
1028 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001029
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001030 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +00001031 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001032 return;
1033 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001034
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001035 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +00001036 // As a special case, print the array as a string if it is an array of
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001037 // i8 with ConstantInt values.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001038 //
Chris Lattner1e194682002-04-18 18:53:13 +00001039 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001040 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +00001041 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001042 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001043 Out << '"';
Chris Lattner1e194682002-04-18 18:53:13 +00001044 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +00001045 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001046 if (CA->getNumOperands()) {
Chris Lattnere101c442009-02-28 21:26:53 +00001047 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001048 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001049 WriteAsOperandInternal(Out, CA->getOperand(0),
Dan Gohman8061d9e2009-08-13 15:27:57 +00001050 &TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001051 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1052 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001053 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001054 Out << ' ';
Dan Gohman8061d9e2009-08-13 15:27:57 +00001055 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001056 }
1057 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001058 Out << ']';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001059 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001060 return;
1061 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001062
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001063 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001064 if (CS->getType()->isPacked())
1065 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +00001066 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +00001067 unsigned N = CS->getNumOperands();
1068 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +00001069 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001070 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001071 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001072
Dan Gohman8061d9e2009-08-13 15:27:57 +00001073 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001074
Jim Laskey3bb78742006-02-25 12:27:03 +00001075 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001076 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001077 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001078 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001079
Dan Gohman8061d9e2009-08-13 15:27:57 +00001080 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001081 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001082 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001083 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001084
Dan Gohman81313fd2008-09-14 17:21:12 +00001085 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001086 if (CS->getType()->isPacked())
1087 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001088 return;
1089 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001090
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001091 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1092 const Type *ETy = CP->getType()->getElementType();
1093 assert(CP->getNumOperands() > 0 &&
1094 "Number of operands for a PackedConst must be > 0");
Dan Gohman1262a252009-02-11 00:25:25 +00001095 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +00001096 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001097 Out << ' ';
Dan Gohman8061d9e2009-08-13 15:27:57 +00001098 WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001099 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner585297e82008-08-19 05:26:17 +00001100 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001101 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001102 Out << ' ';
Dan Gohman8061d9e2009-08-13 15:27:57 +00001103 WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001104 }
Dan Gohman1262a252009-02-11 00:25:25 +00001105 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001106 return;
1107 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001108
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001109 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001110 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001111 return;
1112 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001113
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001114 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001115 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001116 return;
1117 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001118
Devang Patel983c6b12009-07-08 21:44:25 +00001119 if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
1120 Out << "!" << Machine->getMetadataSlot(Node);
1121 return;
1122 }
1123
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001124 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001125 Out << CE->getOpcodeName();
Dan Gohman9c7f8082009-07-27 16:11:46 +00001126 WriteOptimizationInfo(Out, CE);
Reid Spencer812a1be2006-12-04 05:19:18 +00001127 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001128 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +00001129 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001130
Vikram S. Adveb952b542002-07-14 23:14:45 +00001131 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +00001132 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001133 Out << ' ';
Dan Gohman8061d9e2009-08-13 15:27:57 +00001134 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001135 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001136 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001137 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001138
Dan Gohmana76f0f72008-05-31 19:12:39 +00001139 if (CE->hasIndices()) {
1140 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
1141 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1142 Out << ", " << Indices[i];
1143 }
1144
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001145 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001146 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001147 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001148 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001149
Misha Brukman21bbdb92004-06-04 21:11:51 +00001150 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001151 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001152 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001153
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001154 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001155}
1156
1157
Misha Brukmanc566ca362004-03-02 00:22:19 +00001158/// WriteAsOperand - Write the name of the specified value out to the specified
1159/// ostream. This can be useful when you just want to print int %reg126, not
1160/// the whole instruction that generated it.
1161///
Dan Gohman12dad632009-08-12 20:56:03 +00001162static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohman8061d9e2009-08-13 15:27:57 +00001163 TypePrinting *TypePrinter,
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001164 SlotTracker *Machine) {
Chris Lattner033935d2008-08-17 04:40:13 +00001165 if (V->hasName()) {
1166 PrintLLVMName(Out, V);
1167 return;
1168 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001169
Chris Lattner033935d2008-08-17 04:40:13 +00001170 const Constant *CV = dyn_cast<Constant>(V);
1171 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001172 assert(TypePrinter && "Constants require TypePrinting!");
1173 WriteConstantInt(Out, CV, *TypePrinter, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001174 return;
1175 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001176
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001177 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001178 Out << "asm ";
1179 if (IA->hasSideEffects())
1180 Out << "sideeffect ";
1181 Out << '"';
1182 PrintEscapedString(IA->getAsmString(), Out);
1183 Out << "\", \"";
1184 PrintEscapedString(IA->getConstraintString(), Out);
1185 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001186 return;
1187 }
Devang Patel7428d8a2009-07-22 17:43:22 +00001188
Devang Patele059ba6e2009-07-23 01:07:34 +00001189 if (const MDNode *N = dyn_cast<MDNode>(V)) {
1190 Out << '!' << Machine->getMetadataSlot(N);
1191 return;
1192 }
1193
Devang Patel7428d8a2009-07-22 17:43:22 +00001194 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patel7428d8a2009-07-22 17:43:22 +00001195 Out << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001196 PrintEscapedString(MDS->getString(), Out);
Devang Patel7428d8a2009-07-22 17:43:22 +00001197 Out << '"';
1198 return;
1199 }
1200
Dan Gohmanc0353bf2009-09-23 01:33:16 +00001201 if (V->getValueID() == Value::PseudoSourceValueVal) {
1202 V->print(Out);
1203 return;
1204 }
1205
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001206 char Prefix = '%';
1207 int Slot;
1208 if (Machine) {
1209 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1210 Slot = Machine->getGlobalSlot(GV);
1211 Prefix = '@';
1212 } else {
1213 Slot = Machine->getLocalSlot(V);
1214 }
Chris Lattner033935d2008-08-17 04:40:13 +00001215 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001216 Machine = createSlotTracker(V);
Chris Lattner033935d2008-08-17 04:40:13 +00001217 if (Machine) {
1218 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1219 Slot = Machine->getGlobalSlot(GV);
1220 Prefix = '@';
1221 } else {
1222 Slot = Machine->getLocalSlot(V);
1223 }
Dan Gohman8061d9e2009-08-13 15:27:57 +00001224 delete Machine;
Chris Lattnera2d810d2006-01-25 22:26:05 +00001225 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001226 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001227 }
1228 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001229
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001230 if (Slot != -1)
1231 Out << Prefix << Slot;
1232 else
1233 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001234}
1235
Dan Gohman12dad632009-08-12 20:56:03 +00001236void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1237 bool PrintType, const Module *Context) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001238
1239 // Fast path: Don't construct and populate a TypePrinting object if we
1240 // won't be needing any types printed.
Dan Gohman08097122009-08-13 23:07:11 +00001241 if (!PrintType &&
1242 (!isa<Constant>(V) || V->hasName() || isa<GlobalValue>(V))) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001243 WriteAsOperandInternal(Out, V, 0, 0);
1244 return;
1245 }
1246
Chris Lattner5a9f63e2002-07-10 16:48:17 +00001247 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +00001248
Chris Lattner92c5c122009-02-28 23:20:19 +00001249 TypePrinting TypePrinter;
Chris Lattner3243ea12009-03-01 00:03:38 +00001250 std::vector<const Type*> NumberedTypes;
1251 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman81313fd2008-09-14 17:21:12 +00001252 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001253 TypePrinter.print(V->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001254 Out << ' ';
1255 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001256
Dan Gohman8061d9e2009-08-13 15:27:57 +00001257 WriteAsOperandInternal(Out, V, &TypePrinter, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +00001258}
1259
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001260namespace {
Chris Lattner2e9fee42001-07-12 23:35:26 +00001261
Chris Lattnerfee714f2001-09-07 16:36:04 +00001262class AssemblyWriter {
Dan Gohmane2745262009-08-12 17:23:50 +00001263 formatted_raw_ostream &Out;
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001264 SlotTracker &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +00001265 const Module *TheModule;
Chris Lattner0f578952009-02-28 20:25:14 +00001266 TypePrinting TypePrinter;
Chris Lattner8339f7d2003-10-30 23:41:03 +00001267 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner3243ea12009-03-01 00:03:38 +00001268 std::vector<const Type*> NumberedTypes;
Devang Patel39e64d42009-07-01 19:21:12 +00001269
1270 // Each MDNode is assigned unique MetadataIDNo.
1271 std::map<const MDNode *, unsigned> MDNodes;
1272 unsigned MetadataIDNo;
Devang Pateldec23fd2009-09-16 20:21:17 +00001273
Chris Lattner2f7c9632001-06-06 20:29:01 +00001274public:
Dan Gohmane2745262009-08-12 17:23:50 +00001275 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1276 const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001277 AssemblyAnnotationWriter *AAW)
Devang Patel39e64d42009-07-01 19:21:12 +00001278 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW), MetadataIDNo(0) {
Chris Lattner3243ea12009-03-01 00:03:38 +00001279 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001280 }
1281
Chris Lattner3243ea12009-03-01 00:03:38 +00001282 void write(const Module *M) { printModule(M); }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001283
Chris Lattner0c19df42008-08-23 22:23:09 +00001284 void write(const GlobalValue *G) {
1285 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
1286 printGlobal(GV);
1287 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
1288 printAlias(GA);
1289 else if (const Function *F = dyn_cast<Function>(G))
1290 printFunction(F);
1291 else
Torok Edwinfbcc6632009-07-14 16:55:14 +00001292 llvm_unreachable("Unknown global");
Chris Lattner0c19df42008-08-23 22:23:09 +00001293 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001294
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001295 void write(const BasicBlock *BB) { printBasicBlock(BB); }
1296 void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001297
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001298 void writeOperand(const Value *Op, bool PrintType);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001299 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner1e194682002-04-18 18:53:13 +00001300
Misha Brukmand92f54a2004-11-15 19:30:05 +00001301private:
Chris Lattner7bfee412001-10-29 16:05:51 +00001302 void printModule(const Module *M);
Reid Spencer32af9e82007-01-06 07:24:44 +00001303 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattner7bfee412001-10-29 16:05:51 +00001304 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001305 void printAlias(const GlobalAlias *GV);
Chris Lattner57698e22002-03-26 18:01:55 +00001306 void printFunction(const Function *F);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001307 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattner7bfee412001-10-29 16:05:51 +00001308 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +00001309 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001310
Chris Lattner862e3382001-10-13 06:42:36 +00001311 // printInfoComment - Print a little comment after the instruction indicating
1312 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00001313 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001314};
Chris Lattner3243ea12009-03-01 00:03:38 +00001315} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +00001316
Chris Lattnerd816b532002-04-13 20:53:41 +00001317
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001318void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1319 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001320 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001321 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001322 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001323 TypePrinter.print(Operand->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001324 Out << ' ';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001325 }
Dan Gohman8061d9e2009-08-13 15:27:57 +00001326 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001327 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001328}
1329
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001330void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001331 Attributes Attrs) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001332 if (Operand == 0) {
1333 Out << "<null operand!>";
1334 } else {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001335 // Print the type
Chris Lattnere101c442009-02-28 21:26:53 +00001336 TypePrinter.print(Operand->getType(), Out);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001337 // Print parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001338 if (Attrs != Attribute::None)
1339 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman81313fd2008-09-14 17:21:12 +00001340 Out << ' ';
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001341 // Print the operand
Dan Gohman8061d9e2009-08-13 15:27:57 +00001342 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001343 }
1344}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001345
Chris Lattner7bfee412001-10-29 16:05:51 +00001346void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +00001347 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001348 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001349 // require a comment char before it).
1350 M->getModuleIdentifier().find('\n') == std::string::npos)
1351 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1352
Owen Andersone2237542006-10-18 02:21:12 +00001353 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001354 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001355 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001356 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001357
Chris Lattnereef2fe72006-01-24 04:13:11 +00001358 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001359 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001360 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001361 size_t CurPos = 0;
1362 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman69273e62009-08-12 23:54:22 +00001363 Out << '\n';
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001364 while (NewLine != std::string::npos) {
1365 // We found a newline, print the portion of the asm string from the
1366 // last newline up to this newline.
1367 Out << "module asm \"";
1368 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1369 Out);
1370 Out << "\"\n";
1371 CurPos = NewLine+1;
1372 NewLine = Asm.find_first_of('\n', CurPos);
1373 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +00001374 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001375 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001376 Out << "\"\n";
1377 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001378
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001379 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +00001380 Module::lib_iterator LI = M->lib_begin();
1381 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +00001382 if (LI != LE) {
Dan Gohman69273e62009-08-12 23:54:22 +00001383 Out << '\n';
Chris Lattner730cfe42004-09-14 04:51:44 +00001384 Out << "deplibs = [ ";
1385 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001386 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +00001387 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +00001388 if (LI != LE)
1389 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +00001390 }
Dan Gohman69273e62009-08-12 23:54:22 +00001391 Out << " ]";
Reid Spencercc5ff642004-07-25 18:08:18 +00001392 }
Reid Spencerb9e08772004-09-13 23:44:23 +00001393
Chris Lattner3243ea12009-03-01 00:03:38 +00001394 // Loop over the symbol table, emitting all id'd types.
Dan Gohman69273e62009-08-12 23:54:22 +00001395 if (!M->getTypeSymbolTable().empty() || !NumberedTypes.empty()) Out << '\n';
Reid Spencer32af9e82007-01-06 07:24:44 +00001396 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001397
Dan Gohman69273e62009-08-12 23:54:22 +00001398 // Output all globals.
1399 if (!M->global_empty()) Out << '\n';
Chris Lattner54932b02006-12-06 04:41:52 +00001400 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1401 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +00001402 printGlobal(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001403
Chris Lattnerd2747052007-04-26 02:24:10 +00001404 // Output all aliases.
1405 if (!M->alias_empty()) Out << "\n";
1406 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1407 I != E; ++I)
1408 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001409
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001410 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001411 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1412 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001413
Devang Patel23e68302009-07-29 22:04:47 +00001414 // Output named metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001415 if (!M->named_metadata_empty()) Out << '\n';
Devang Patel23e68302009-07-29 22:04:47 +00001416 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
1417 E = M->named_metadata_end(); I != E; ++I) {
1418 const NamedMDNode *NMD = I;
1419 Out << "!" << NMD->getName() << " = !{";
1420 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1421 if (i) Out << ", ";
Devang Patel847fcac2009-07-30 01:02:04 +00001422 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
Devang Patel23e68302009-07-29 22:04:47 +00001423 Out << '!' << Machine.getMetadataSlot(MD);
1424 }
1425 Out << "}\n";
1426 }
1427
1428 // Output metadata.
Dan Gohman69273e62009-08-12 23:54:22 +00001429 if (!Machine.mdnEmpty()) Out << '\n';
Devang Patel983c6b12009-07-08 21:44:25 +00001430 WriteMDNodes(Out, TypePrinter, Machine);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001431}
1432
Dan Gohmane2745262009-08-12 17:23:50 +00001433static void PrintLinkage(GlobalValue::LinkageTypes LT,
1434 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001435 switch (LT) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001436 case GlobalValue::ExternalLinkage: break;
1437 case GlobalValue::PrivateLinkage: Out << "private "; break;
1438 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
1439 case GlobalValue::InternalLinkage: Out << "internal "; break;
1440 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1441 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1442 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1443 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1444 case GlobalValue::CommonLinkage: Out << "common "; break;
1445 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1446 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1447 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1448 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001449 case GlobalValue::AvailableExternallyLinkage:
1450 Out << "available_externally ";
1451 break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001452 case GlobalValue::GhostLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +00001453 llvm_unreachable("GhostLinkage not allowed in AsmWriter!");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001454 }
1455}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001456
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001457
1458static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohmane2745262009-08-12 17:23:50 +00001459 formatted_raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001460 switch (Vis) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001461 default: llvm_unreachable("Invalid visibility style!");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001462 case GlobalValue::DefaultVisibility: break;
1463 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1464 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1465 }
1466}
1467
Chris Lattner7bfee412001-10-29 16:05:51 +00001468void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohman8061d9e2009-08-13 15:27:57 +00001469 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine);
Dan Gohman466876b2009-08-12 23:32:33 +00001470 Out << " = ";
Chris Lattner37798642001-09-18 04:01:05 +00001471
Chris Lattner1508d3f2008-08-19 05:16:28 +00001472 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1473 Out << "external ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001474
Chris Lattner1508d3f2008-08-19 05:16:28 +00001475 PrintLinkage(GV->getLinkage(), Out);
1476 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001477
1478 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerac161bf2009-01-02 07:01:27 +00001479 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1480 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001481 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001482 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001483
Dan Gohman81313fd2008-09-14 17:21:12 +00001484 if (GV->hasInitializer()) {
1485 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001486 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001487 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001488
Chris Lattner4b96c542005-11-12 00:10:19 +00001489 if (GV->hasSection())
1490 Out << ", section \"" << GV->getSection() << '"';
1491 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001492 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001493
Chris Lattner113f4f42002-06-25 16:13:24 +00001494 printInfoComment(*GV);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001495 Out << '\n';
Chris Lattnerda975502001-09-10 07:58:01 +00001496}
1497
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001498void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen83e468a2008-06-03 18:14:29 +00001499 // Don't crash when dumping partially built GA
1500 if (!GA->hasName())
1501 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001502 else {
1503 PrintLLVMName(Out, GA);
1504 Out << " = ";
1505 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001506 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001507
1508 Out << "alias ";
1509
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001510 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001511
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001512 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001513
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001514 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001515 TypePrinter.print(GV->getType(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00001516 Out << ' ';
1517 PrintLLVMName(Out, GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001518 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001519 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001520 Out << "* ";
1521
Dan Gohman8061d9e2009-08-13 15:27:57 +00001522 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Anton Korobeynikov72d5d422008-03-22 08:17:17 +00001523 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001524 TypePrinter.print(GA->getType(), Out);
1525 Out << ' ';
Chris Lattner033935d2008-08-17 04:40:13 +00001526 PrintLLVMName(Out, GA);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001527 } else {
Chris Lattnercde89e42009-04-25 21:23:19 +00001528 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1529 // The only valid GEP is an all zero GEP.
1530 assert((CE->getOpcode() == Instruction::BitCast ||
1531 CE->getOpcode() == Instruction::GetElementPtr) &&
1532 "Unsupported aliasee");
1533 writeOperand(CE, false);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001534 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001535
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001536 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001537 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001538}
1539
Reid Spencer32af9e82007-01-06 07:24:44 +00001540void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner3243ea12009-03-01 00:03:38 +00001541 // Emit all numbered types.
1542 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman466876b2009-08-12 23:32:33 +00001543 Out << '%' << i << " = type ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001544
Chris Lattner3243ea12009-03-01 00:03:38 +00001545 // Make sure we print out at least one level of the type structure, so
1546 // that we do not get %2 = type %2
1547 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
Dan Gohman69273e62009-08-12 23:54:22 +00001548 Out << '\n';
Chris Lattner3243ea12009-03-01 00:03:38 +00001549 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001550
Chris Lattner3243ea12009-03-01 00:03:38 +00001551 // Print the named types.
Reid Spencer32af9e82007-01-06 07:24:44 +00001552 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1553 TI != TE; ++TI) {
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001554 PrintLLVMName(Out, TI->first, LocalPrefix);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001555 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001556
1557 // Make sure we print out at least one level of the type structure, so
1558 // that we do not get %FILE = type %FILE
Chris Lattnere101c442009-02-28 21:26:53 +00001559 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001560 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001561 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001562}
1563
Misha Brukmanc566ca362004-03-02 00:22:19 +00001564/// printFunction - Print all aspects of a function.
1565///
Chris Lattner113f4f42002-06-25 16:13:24 +00001566void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001567 // Print out the return type and name.
1568 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001569
Misha Brukmana6619a92004-06-21 21:53:56 +00001570 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001571
Reid Spencer5301e7c2007-01-30 20:08:39 +00001572 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001573 Out << "declare ";
1574 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001575 Out << "define ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001576
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001577 PrintLinkage(F->getLinkage(), Out);
1578 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001579
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001580 // Print the calling convention.
1581 switch (F->getCallingConv()) {
1582 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001583 case CallingConv::Fast: Out << "fastcc "; break;
1584 case CallingConv::Cold: Out << "coldcc "; break;
1585 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001586 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1587 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1588 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1589 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001590 default: Out << "cc" << F->getCallingConv() << " "; break;
1591 }
1592
Reid Spencer8c4914c2006-12-31 05:24:50 +00001593 const FunctionType *FT = F->getFunctionType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001594 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel221fe422008-09-29 20:49:50 +00001595 Attributes RetAttrs = Attrs.getRetAttributes();
1596 if (RetAttrs != Attribute::None)
1597 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001598 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001599 Out << ' ';
Dan Gohman8061d9e2009-08-13 15:27:57 +00001600 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Misha Brukmana6619a92004-06-21 21:53:56 +00001601 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001602 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001603
Chris Lattner7bfee412001-10-29 16:05:51 +00001604 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001605
Reid Spencer8c4914c2006-12-31 05:24:50 +00001606 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001607 if (!F->isDeclaration()) {
1608 // If this isn't a declaration, print the argument names as well.
1609 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1610 I != E; ++I) {
1611 // Insert commas as we go... the first arg doesn't get a comma
1612 if (I != F->arg_begin()) Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001613 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner82738fe2007-04-18 00:57:22 +00001614 Idx++;
1615 }
1616 } else {
1617 // Otherwise, print the types from the function type.
1618 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1619 // Insert commas as we go... the first arg doesn't get a comma
1620 if (i) Out << ", ";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001621
Chris Lattner82738fe2007-04-18 00:57:22 +00001622 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001623 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001624
Devang Patela05633e2008-09-26 22:53:05 +00001625 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel4c758ea2008-09-25 21:00:45 +00001626 if (ArgAttrs != Attribute::None)
1627 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner82738fe2007-04-18 00:57:22 +00001628 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001629 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001630
1631 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001632 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001633 if (FT->getNumParams()) Out << ", ";
1634 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001635 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001636 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001637 Attributes FnAttrs = Attrs.getFnAttributes();
1638 if (FnAttrs != Attribute::None)
1639 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner4b96c542005-11-12 00:10:19 +00001640 if (F->hasSection())
1641 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001642 if (F->getAlignment())
1643 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001644 if (F->hasGC())
1645 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001646 if (F->isDeclaration()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001647 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001648 } else {
Chris Lattnerd5fbc8f2008-04-21 06:12:55 +00001649 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001650
Chris Lattner6915f8f2002-04-07 22:49:37 +00001651 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001652 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1653 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001654
Misha Brukmana6619a92004-06-21 21:53:56 +00001655 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001656 }
1657
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001658 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001659}
1660
Misha Brukmanc566ca362004-03-02 00:22:19 +00001661/// printArgument - This member is called for every argument that is passed into
1662/// the function. Simply print it out
1663///
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001664void AssemblyWriter::printArgument(const Argument *Arg,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001665 Attributes Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001666 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001667 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001668
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001669 // Output parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001670 if (Attrs != Attribute::None)
1671 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001672
Chris Lattner2f7c9632001-06-06 20:29:01 +00001673 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001674 if (Arg->hasName()) {
1675 Out << ' ';
1676 PrintLLVMName(Out, Arg);
1677 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001678}
1679
Misha Brukmanc566ca362004-03-02 00:22:19 +00001680/// printBasicBlock - This member is called for each basic block in a method.
1681///
Chris Lattner7bfee412001-10-29 16:05:51 +00001682void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001683 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001684 Out << "\n";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00001685 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001686 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001687 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner67bec132008-04-21 04:20:33 +00001688 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001689 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001690 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001691 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001692 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001693 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001694 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001695
Dan Gohmane2745262009-08-12 17:23:50 +00001696 if (BB->getParent() == 0) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001697 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001698 Out << "; Error: Block without parent!";
1699 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnerff834c02008-04-22 02:45:44 +00001700 // Output predecessors for the block...
Chris Lattneree97b8b2009-08-17 15:48:08 +00001701 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001702 Out << ";";
Chris Lattnerff834c02008-04-22 02:45:44 +00001703 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001704
Chris Lattnerff834c02008-04-22 02:45:44 +00001705 if (PI == PE) {
1706 Out << " No predecessors!";
1707 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001708 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001709 writeOperand(*PI, false);
1710 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001711 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001712 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001713 }
Chris Lattner58185f22002-10-02 19:38:55 +00001714 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001715 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001716
Chris Lattnerff834c02008-04-22 02:45:44 +00001717 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001718
Misha Brukmana6619a92004-06-21 21:53:56 +00001719 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001720
Chris Lattnerfee714f2001-09-07 16:36:04 +00001721 // Output all of the instructions in the basic block...
Dan Gohmana4f709e2009-07-13 18:27:59 +00001722 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner113f4f42002-06-25 16:13:24 +00001723 printInstruction(*I);
Dan Gohmana4f709e2009-07-13 18:27:59 +00001724 Out << '\n';
1725 }
Chris Lattner96cdd272004-03-08 18:51:45 +00001726
Misha Brukmana6619a92004-06-21 21:53:56 +00001727 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001728}
1729
Chris Lattner862e3382001-10-13 06:42:36 +00001730
Misha Brukmanc566ca362004-03-02 00:22:19 +00001731/// printInfoComment - Print a little comment after the instruction indicating
1732/// which slot it occupies.
1733///
Chris Lattner113f4f42002-06-25 16:13:24 +00001734void AssemblyWriter::printInfoComment(const Value &V) {
Owen Anderson55f1c092009-08-13 21:58:54 +00001735 if (V.getType() != Type::getVoidTy(V.getContext())) {
Chris Lattneree97b8b2009-08-17 15:48:08 +00001736 Out.PadToColumn(50);
Dan Gohmane2745262009-08-12 17:23:50 +00001737 Out << "; <";
Chris Lattnere101c442009-02-28 21:26:53 +00001738 TypePrinter.print(V.getType(), Out);
Dan Gohman69273e62009-08-12 23:54:22 +00001739 Out << "> [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001740 }
1741}
1742
Reid Spencere7141c82006-08-28 01:02:49 +00001743// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001744void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001745 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001746
Dan Gohman466876b2009-08-12 23:32:33 +00001747 // Print out indentation for an instruction.
Dan Gohmanb4583b12009-08-13 01:41:52 +00001748 Out << " ";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001749
1750 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001751 if (I.hasName()) {
1752 PrintLLVMName(Out, &I);
1753 Out << " = ";
Owen Anderson55f1c092009-08-13 21:58:54 +00001754 } else if (I.getType() != Type::getVoidTy(I.getContext())) {
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001755 // Print out the def slot taken.
1756 int SlotNum = Machine.getLocalSlot(&I);
1757 if (SlotNum == -1)
1758 Out << "<badref> = ";
1759 else
1760 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001761 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001762
Chris Lattner06038452005-05-06 05:51:46 +00001763 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001764 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001765 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001766 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001767 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1768 // If this is a call, check if it's a tail call.
1769 Out << "tail ";
1770 }
Chris Lattner504f9242003-09-08 17:45:59 +00001771
Chris Lattner2f7c9632001-06-06 20:29:01 +00001772 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001773 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001774
Dan Gohman9c7f8082009-07-27 16:11:46 +00001775 // Print out optimization information.
1776 WriteOptimizationInfo(Out, &I);
1777
Reid Spencer45e52392006-12-03 06:27:29 +00001778 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001779 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001780 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001781
Chris Lattner2f7c9632001-06-06 20:29:01 +00001782 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001783 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001784
1785 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001786 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1787 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001788 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001789 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001790 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001791 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001792 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001793 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001794
Chris Lattner8d48df22002-04-13 18:34:38 +00001795 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001796 // Special case switch statement to get formatting nice and correct...
Dan Gohman81313fd2008-09-14 17:21:12 +00001797 Out << ' ';
Chris Lattner82ff9232008-08-23 22:52:27 +00001798 writeOperand(Operand , true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001799 Out << ", ";
Chris Lattner82ff9232008-08-23 22:52:27 +00001800 writeOperand(I.getOperand(1), true);
1801 Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001802
Chris Lattner113f4f42002-06-25 16:13:24 +00001803 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Dan Gohmanb4583b12009-08-13 01:41:52 +00001804 Out << "\n ";
Chris Lattner82ff9232008-08-23 22:52:27 +00001805 writeOperand(I.getOperand(op ), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001806 Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001807 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001808 }
Dan Gohmanb4583b12009-08-13 01:41:52 +00001809 Out << "\n ]";
Chris Lattnerda558102001-10-02 03:41:24 +00001810 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001811 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001812 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001813 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001814
Chris Lattner113f4f42002-06-25 16:13:24 +00001815 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001816 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001817 Out << "[ ";
1818 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001819 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001820 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001821 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001822 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001823 writeOperand(I.getOperand(0), true);
1824 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1825 Out << ", " << *i;
1826 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001827 Out << ' ';
1828 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001829 writeOperand(I.getOperand(1), true);
1830 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1831 Out << ", " << *i;
Devang Patel59643e52008-02-23 00:35:18 +00001832 } else if (isa<ReturnInst>(I) && !Operand) {
1833 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001834 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1835 // Print the calling convention being used.
1836 switch (CI->getCallingConv()) {
1837 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001838 case CallingConv::Fast: Out << " fastcc"; break;
1839 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerf5270372007-11-18 18:32:16 +00001840 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001841 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
1842 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1843 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1844 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001845 default: Out << " cc" << CI->getCallingConv(); break;
1846 }
1847
Reid Spencer1517de32007-04-09 06:10:42 +00001848 const PointerType *PTy = cast<PointerType>(Operand->getType());
1849 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1850 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001851 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001852
Devang Patel221fe422008-09-29 20:49:50 +00001853 if (PAL.getRetAttributes() != Attribute::None)
1854 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1855
Chris Lattner463d6a52003-08-05 15:34:45 +00001856 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001857 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001858 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001859 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001860 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001861 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001862 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001863 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattnere101c442009-02-28 21:26:53 +00001864 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001865 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001866 writeOperand(Operand, false);
1867 } else {
1868 writeOperand(Operand, true);
1869 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001870 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001871 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1872 if (op > 1)
Dan Gohman81313fd2008-09-14 17:21:12 +00001873 Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001874 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001875 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001876 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001877 if (PAL.getFnAttributes() != Attribute::None)
1878 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001879 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencer1517de32007-04-09 06:10:42 +00001880 const PointerType *PTy = cast<PointerType>(Operand->getType());
1881 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1882 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001883 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001884
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001885 // Print the calling convention being used.
1886 switch (II->getCallingConv()) {
1887 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001888 case CallingConv::Fast: Out << " fastcc"; break;
1889 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman81313fd2008-09-14 17:21:12 +00001890 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1891 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001892 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1893 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1894 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001895 default: Out << " cc" << II->getCallingConv(); break;
1896 }
1897
Devang Patel221fe422008-09-29 20:49:50 +00001898 if (PAL.getRetAttributes() != Attribute::None)
1899 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1900
Chris Lattner463d6a52003-08-05 15:34:45 +00001901 // If possible, print out the short form of the invoke instruction. We can
1902 // only do this if the first argument is a pointer to a nonvararg function,
1903 // and if the return type is not a pointer to a function.
1904 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001905 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001906 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001907 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001908 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattnere101c442009-02-28 21:26:53 +00001909 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001910 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001911 writeOperand(Operand, false);
1912 } else {
1913 writeOperand(Operand, true);
1914 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001915 Out << '(';
Gabor Greif2d60e1e2009-09-03 02:02:59 +00001916 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1917 if (op > 3)
Dan Gohman81313fd2008-09-14 17:21:12 +00001918 Out << ", ";
Gabor Greif2d60e1e2009-09-03 02:02:59 +00001919 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattner862e3382001-10-13 06:42:36 +00001920 }
1921
Dan Gohman81313fd2008-09-14 17:21:12 +00001922 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001923 if (PAL.getFnAttributes() != Attribute::None)
1924 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1925
Dan Gohmanb4583b12009-08-13 01:41:52 +00001926 Out << "\n to ";
Chris Lattner862e3382001-10-13 06:42:36 +00001927 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001928 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001929 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001930
Chris Lattner113f4f42002-06-25 16:13:24 +00001931 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001932 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001933 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohmanb53e26c2009-07-31 18:23:24 +00001934 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001935 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001936 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001937 }
Nate Begeman848622f2005-11-05 09:21:28 +00001938 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001939 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001940 }
Chris Lattner862e3382001-10-13 06:42:36 +00001941 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001942 if (Operand) {
1943 Out << ' ';
1944 writeOperand(Operand, true); // Work with broken code
1945 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001946 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001947 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001948 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001949 if (Operand) {
1950 Out << ' ';
1951 writeOperand(Operand, true); // Work with broken code
1952 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001953 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001954 TypePrinter.print(I.getType(), Out);
1955 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001956
Misha Brukmanb1c93172005-04-21 23:48:37 +00001957 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001958 // omit the type from all but the first operand. If the instruction has
1959 // different type operands (for example br), then they are all printed.
1960 bool PrintAllTypes = false;
1961 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001962
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001963 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001964 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1965 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001966 PrintAllTypes = true;
1967 } else {
1968 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1969 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00001970 // note that Operand shouldn't be null, but the test helps make dump()
1971 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00001972 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001973 PrintAllTypes = true; // We have differing types! Print them all!
1974 break;
1975 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001976 }
1977 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001978
Chris Lattner7bfee412001-10-29 16:05:51 +00001979 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001980 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001981 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00001982 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001983
Dan Gohman81313fd2008-09-14 17:21:12 +00001984 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001985 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001986 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001987 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001988 }
1989 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001990
Christopher Lamb84485702007-04-22 19:24:39 +00001991 // Print post operand alignment for load/store
1992 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1993 Out << ", align " << cast<LoadInst>(I).getAlignment();
1994 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1995 Out << ", align " << cast<StoreInst>(I).getAlignment();
1996 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001997
Devang Pateldec23fd2009-09-16 20:21:17 +00001998 // Print DebugInfo
1999 Metadata &TheMetadata = I.getContext().getMetadata();
2000 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
2001 if (const MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &I))
2002 Out << ", dbg !" << Machine.getMetadataSlot(Dbg);
Chris Lattner862e3382001-10-13 06:42:36 +00002003 printInfoComment(I);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002004}
2005
2006
2007//===----------------------------------------------------------------------===//
2008// External Interface declarations
2009//===----------------------------------------------------------------------===//
2010
Dan Gohmane2745262009-08-12 17:23:50 +00002011void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00002012 SlotTracker SlotTable(this);
Dan Gohmane2745262009-08-12 17:23:50 +00002013 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002014 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002015 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00002016}
2017
Chris Lattner2ee62d22009-02-28 21:11:05 +00002018void Type::print(raw_ostream &OS) const {
2019 if (this == 0) {
2020 OS << "<null Type>";
2021 return;
2022 }
Chris Lattner92c5c122009-02-28 23:20:19 +00002023 TypePrinting().print(this, OS);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00002024}
2025
Dan Gohmane2745262009-08-12 17:23:50 +00002026void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00002027 if (this == 0) {
Dan Gohman12dad632009-08-12 20:56:03 +00002028 ROS << "printing a <null> value\n";
Chris Lattner0c19df42008-08-23 22:23:09 +00002029 return;
2030 }
Dan Gohman12dad632009-08-12 20:56:03 +00002031 formatted_raw_ostream OS(ROS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002032 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2033 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2034 SlotTracker SlotTable(F);
2035 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
2036 W.write(I);
2037 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2038 SlotTracker SlotTable(BB->getParent());
2039 AssemblyWriter W(OS, SlotTable,
2040 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
2041 W.write(BB);
2042 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2043 SlotTracker SlotTable(GV->getParent());
Dan Gohman6c7a4852009-04-20 16:10:33 +00002044 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattner0c19df42008-08-23 22:23:09 +00002045 W.write(GV);
Devang Patel7428d8a2009-07-22 17:43:22 +00002046 } else if (const MDString *MDS = dyn_cast<MDString>(this)) {
2047 TypePrinting TypePrinter;
2048 TypePrinter.print(MDS->getType(), OS);
2049 OS << ' ';
2050 OS << "!\"";
Daniel Dunbare03eecb2009-07-25 23:55:21 +00002051 PrintEscapedString(MDS->getString(), OS);
Devang Patel7428d8a2009-07-22 17:43:22 +00002052 OS << '"';
Devang Patel5546b982009-07-01 20:59:15 +00002053 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Devang Patel983c6b12009-07-08 21:44:25 +00002054 SlotTracker SlotTable(N);
Devang Patel5546b982009-07-01 20:59:15 +00002055 TypePrinting TypePrinter;
Devang Patel983c6b12009-07-08 21:44:25 +00002056 SlotTable.initialize();
2057 WriteMDNodes(OS, TypePrinter, SlotTable);
Devang Patelb4a4e772009-07-30 00:02:57 +00002058 } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) {
2059 SlotTracker SlotTable(N);
2060 TypePrinting TypePrinter;
2061 SlotTable.initialize();
2062 OS << "!" << N->getName() << " = !{";
2063 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
2064 if (i) OS << ", ";
Devang Patel847fcac2009-07-30 01:02:04 +00002065 MDNode *MD = dyn_cast_or_null<MDNode>(N->getElement(i));
2066 if (MD)
2067 OS << '!' << SlotTable.getMetadataSlot(MD);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002068 else
Devang Patel847fcac2009-07-30 01:02:04 +00002069 OS << "null";
Devang Patelb4a4e772009-07-30 00:02:57 +00002070 }
2071 OS << "}\n";
2072 WriteMDNodes(OS, TypePrinter, SlotTable);
Chris Lattner0c19df42008-08-23 22:23:09 +00002073 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00002074 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00002075 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00002076 OS << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00002077 WriteConstantInt(OS, C, TypePrinter, 0);
Chris Lattner0c19df42008-08-23 22:23:09 +00002078 } else if (const Argument *A = dyn_cast<Argument>(this)) {
2079 WriteAsOperand(OS, this, true,
2080 A->getParent() ? A->getParent()->getParent() : 0);
2081 } else if (isa<InlineAsm>(this)) {
2082 WriteAsOperand(OS, this, true, 0);
2083 } else {
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002084 // Otherwise we don't know what it is. Call the virtual function to
2085 // allow a subclass to print itself.
2086 printCustom(OS);
Chris Lattner0c19df42008-08-23 22:23:09 +00002087 }
2088}
2089
Dan Gohmanc0353bf2009-09-23 01:33:16 +00002090// Value::printCustom - subclasses should override this to implement printing.
2091void Value::printCustom(raw_ostream &OS) const {
2092 llvm_unreachable("Unknown value to print out!");
2093}
2094
Chris Lattnerdab942552008-08-25 17:03:15 +00002095// Value::dump - allow easy printing of Values from the debugger.
Dan Gohman4f2fea12009-03-23 15:57:19 +00002096void Value::dump() const { print(errs()); errs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00002097
Chris Lattnerdab942552008-08-25 17:03:15 +00002098// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner41a83d92008-10-01 20:16:19 +00002099// This one uses type names from the given context module
2100void Type::dump(const Module *Context) const {
2101 WriteTypeSymbolic(errs(), this, Context);
2102 errs() << '\n';
Chris Lattner41a83d92008-10-01 20:16:19 +00002103}
2104
Chris Lattner24025262009-02-28 21:05:51 +00002105// Type::dump - allow easy printing of Types from the debugger.
2106void Type::dump() const { dump(0); }
2107
Chris Lattnerdab942552008-08-25 17:03:15 +00002108// Module::dump() - Allow printing of Modules from the debugger.
Dan Gohman4f2fea12009-03-23 15:57:19 +00002109void Module::dump() const { print(errs(), 0); }