blob: f13d39d12e0f03332d5d8a76193437ae0848bf07 [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"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000026#include "llvm/MDNode.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000027#include "llvm/Module.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000028#include "llvm/ValueSymbolTable.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000029#include "llvm/TypeSymbolTable.h"
Chris Lattner3243ea12009-03-01 00:03:38 +000030#include "llvm/ADT/DenseSet.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/STLExtras.h"
Bill Wendlingdfc91892006-11-28 02:09:03 +000033#include "llvm/Support/CFG.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000034#include "llvm/Support/ErrorHandling.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000035#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000036#include "llvm/Support/Streams.h"
Chris Lattner393b7cd2008-08-17 04:17:45 +000037#include "llvm/Support/raw_ostream.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;
53
54 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
55 return BB->getParent() ? BB->getParent()->getParent() : 0;
56
57 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 }
61
62 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.
69static void PrintEscapedString(const char *Str, unsigned Length,
70 raw_ostream &Out) {
71 for (unsigned i = 0; i != Length; ++i) {
72 unsigned char C = Str[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
80// PrintEscapedString - Print each character of the specified string, escaping
81// it if it is not printable or if it is an escape char.
82static void PrintEscapedString(const std::string &Str, raw_ostream &Out) {
83 PrintEscapedString(Str.c_str(), Str.size(), Out);
84}
85
Chris Lattner3eee99c2008-08-19 04:36:02 +000086enum PrefixType {
87 GlobalPrefix,
88 LabelPrefix,
Daniel Dunbar389529a2008-10-14 23:28:09 +000089 LocalPrefix,
90 NoPrefix
Chris Lattner3eee99c2008-08-19 04:36:02 +000091};
92
93/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
94/// prefixed with % (if the string only contains simple characters) or is
95/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner0c19df42008-08-23 22:23:09 +000096static void PrintLLVMName(raw_ostream &OS, const char *NameStr,
Chris Lattner1508d3f2008-08-19 05:16:28 +000097 unsigned NameLen, PrefixType Prefix) {
98 assert(NameStr && "Cannot get empty name!");
Chris Lattner3eee99c2008-08-19 04:36:02 +000099 switch (Prefix) {
Chris Lattner1508d3f2008-08-19 05:16:28 +0000100 default: assert(0 && "Bad prefix!");
Daniel Dunbar389529a2008-10-14 23:28:09 +0000101 case NoPrefix: break;
Chris Lattner1508d3f2008-08-19 05:16:28 +0000102 case GlobalPrefix: OS << '@'; break;
103 case LabelPrefix: break;
104 case LocalPrefix: OS << '%'; break;
Nick Lewycky063699a2009-03-19 06:31:22 +0000105 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000106
107 // Scan the name to see if it needs quotes first.
Daniel Dunbardb0b70a2008-10-28 19:33:02 +0000108 bool NeedsQuotes = isdigit(NameStr[0]);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000109 if (!NeedsQuotes) {
110 for (unsigned i = 0; i != NameLen; ++i) {
111 char C = NameStr[i];
112 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
113 NeedsQuotes = true;
114 break;
115 }
116 }
117 }
118
119 // If we didn't need any quotes, just write out the name in one blast.
120 if (!NeedsQuotes) {
121 OS.write(NameStr, NameLen);
122 return;
123 }
124
125 // Okay, we need quotes. Output the quotes and escape any scary characters as
126 // needed.
127 OS << '"';
Daniel Dunbardb0b70a2008-10-28 19:33:02 +0000128 PrintEscapedString(NameStr, NameLen, OS);
Chris Lattner3eee99c2008-08-19 04:36:02 +0000129 OS << '"';
130}
131
132/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
133/// prefixed with % (if the string only contains simple characters) or is
134/// surrounded with ""'s (if it has special chars in it). Print it out.
Chris Lattner0c19df42008-08-23 22:23:09 +0000135static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Chris Lattner1508d3f2008-08-19 05:16:28 +0000136 PrintLLVMName(OS, V->getNameStart(), V->getNameLen(),
Chris Lattner3eee99c2008-08-19 04:36:02 +0000137 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
138}
139
Chris Lattner0f578952009-02-28 20:25:14 +0000140//===----------------------------------------------------------------------===//
141// TypePrinting Class: Type printing machinery
142//===----------------------------------------------------------------------===//
143
Chris Lattner3339d7f2009-02-28 23:03:55 +0000144static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) {
145 return *static_cast<DenseMap<const Type *, std::string>*>(M);
Chris Lattner3ad46822009-02-28 22:34:45 +0000146}
147
148void TypePrinting::clear() {
149 getTypeNamesMap(TypeNames).clear();
150}
Chris Lattner0f578952009-02-28 20:25:14 +0000151
Chris Lattner92c5c122009-02-28 23:20:19 +0000152bool TypePrinting::hasTypeName(const Type *Ty) const {
153 return getTypeNamesMap(TypeNames).count(Ty);
154}
155
156void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
157 getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
158}
159
160
161TypePrinting::TypePrinting() {
Chris Lattner3339d7f2009-02-28 23:03:55 +0000162 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner0f578952009-02-28 20:25:14 +0000163}
164
Chris Lattner3ad46822009-02-28 22:34:45 +0000165TypePrinting::~TypePrinting() {
166 delete &getTypeNamesMap(TypeNames);
167}
168
Chris Lattner40959d02009-02-28 20:49:40 +0000169/// CalcTypeName - Write the specified type to the specified raw_ostream, making
170/// use of type names or up references to shorten the type name where possible.
171void TypePrinting::CalcTypeName(const Type *Ty,
Chris Lattner765338d2009-02-28 20:34:19 +0000172 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattner242d91a2009-03-01 01:16:21 +0000173 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner0f578952009-02-28 20:25:14 +0000174 // Check to see if the type is named.
Chris Lattner242d91a2009-03-01 01:16:21 +0000175 if (!IgnoreTopLevelName) {
Nick Lewycky063699a2009-03-19 06:31:22 +0000176 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattner242d91a2009-03-01 01:16:21 +0000177 DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
178 if (I != TM.end()) {
179 OS << I->second;
180 return;
181 }
Chris Lattner0f578952009-02-28 20:25:14 +0000182 }
183
184 // Check to see if the Type is already on the stack...
185 unsigned Slot = 0, CurSize = TypeStack.size();
186 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
187
188 // This is another base case for the recursion. In this case, we know
189 // that we have looped back to a type that we have previously visited.
190 // Generate the appropriate upreference to handle this.
191 if (Slot < CurSize) {
Chris Lattner06a23212009-02-28 21:27:31 +0000192 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner0f578952009-02-28 20:25:14 +0000193 return;
194 }
195
196 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
197
198 switch (Ty->getTypeID()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000199 case Type::VoidTyID: OS << "void"; break;
200 case Type::FloatTyID: OS << "float"; break;
201 case Type::DoubleTyID: OS << "double"; break;
202 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
203 case Type::FP128TyID: OS << "fp128"; break;
204 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
205 case Type::LabelTyID: OS << "label"; break;
Nick Lewyckyadbc2842009-05-30 05:06:04 +0000206 case Type::MetadataTyID: OS << "metadata"; break;
Chris Lattner68318b12009-02-28 21:18:43 +0000207 case Type::IntegerTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000208 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner68318b12009-02-28 21:18:43 +0000209 break;
210
Chris Lattner07d88292009-02-28 20:35:42 +0000211 case Type::FunctionTyID: {
212 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000213 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
214 OS << " (";
Chris Lattner07d88292009-02-28 20:35:42 +0000215 for (FunctionType::param_iterator I = FTy->param_begin(),
216 E = FTy->param_end(); I != E; ++I) {
217 if (I != FTy->param_begin())
Chris Lattner06a23212009-02-28 21:27:31 +0000218 OS << ", ";
219 CalcTypeName(*I, TypeStack, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000220 }
Chris Lattner07d88292009-02-28 20:35:42 +0000221 if (FTy->isVarArg()) {
Chris Lattner06a23212009-02-28 21:27:31 +0000222 if (FTy->getNumParams()) OS << ", ";
223 OS << "...";
Chris Lattner0f578952009-02-28 20:25:14 +0000224 }
Chris Lattner06a23212009-02-28 21:27:31 +0000225 OS << ')';
Chris Lattner07d88292009-02-28 20:35:42 +0000226 break;
227 }
228 case Type::StructTyID: {
229 const StructType *STy = cast<StructType>(Ty);
230 if (STy->isPacked())
Chris Lattner06a23212009-02-28 21:27:31 +0000231 OS << '<';
232 OS << "{ ";
Chris Lattner07d88292009-02-28 20:35:42 +0000233 for (StructType::element_iterator I = STy->element_begin(),
234 E = STy->element_end(); I != E; ++I) {
Chris Lattner06a23212009-02-28 21:27:31 +0000235 CalcTypeName(*I, TypeStack, OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000236 if (next(I) != STy->element_end())
Chris Lattner06a23212009-02-28 21:27:31 +0000237 OS << ',';
238 OS << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +0000239 }
Chris Lattner06a23212009-02-28 21:27:31 +0000240 OS << '}';
Chris Lattner07d88292009-02-28 20:35:42 +0000241 if (STy->isPacked())
Chris Lattner06a23212009-02-28 21:27:31 +0000242 OS << '>';
Chris Lattner07d88292009-02-28 20:35:42 +0000243 break;
244 }
245 case Type::PointerTyID: {
246 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000247 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner07d88292009-02-28 20:35:42 +0000248 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner06a23212009-02-28 21:27:31 +0000249 OS << " addrspace(" << AddressSpace << ')';
250 OS << '*';
Chris Lattner07d88292009-02-28 20:35:42 +0000251 break;
252 }
253 case Type::ArrayTyID: {
254 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000255 OS << '[' << ATy->getNumElements() << " x ";
256 CalcTypeName(ATy->getElementType(), TypeStack, OS);
257 OS << ']';
Chris Lattner07d88292009-02-28 20:35:42 +0000258 break;
259 }
260 case Type::VectorTyID: {
261 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner06a23212009-02-28 21:27:31 +0000262 OS << "<" << PTy->getNumElements() << " x ";
263 CalcTypeName(PTy->getElementType(), TypeStack, OS);
264 OS << '>';
Chris Lattner07d88292009-02-28 20:35:42 +0000265 break;
266 }
267 case Type::OpaqueTyID:
Chris Lattner06a23212009-02-28 21:27:31 +0000268 OS << "opaque";
Chris Lattner07d88292009-02-28 20:35:42 +0000269 break;
270 default:
Chris Lattner06a23212009-02-28 21:27:31 +0000271 OS << "<unrecognized-type>";
Chris Lattner07d88292009-02-28 20:35:42 +0000272 break;
Chris Lattner0f578952009-02-28 20:25:14 +0000273 }
274
Chris Lattner40959d02009-02-28 20:49:40 +0000275 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner0f578952009-02-28 20:25:14 +0000276}
277
278/// printTypeInt - The internal guts of printing out a type that has a
279/// potentially named portion.
280///
Chris Lattner242d91a2009-03-01 01:16:21 +0000281void TypePrinting::print(const Type *Ty, raw_ostream &OS,
282 bool IgnoreTopLevelName) {
Chris Lattner0f578952009-02-28 20:25:14 +0000283 // Check to see if the type is named.
Chris Lattner3339d7f2009-02-28 23:03:55 +0000284 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattner242d91a2009-03-01 01:16:21 +0000285 if (!IgnoreTopLevelName) {
286 DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
287 if (I != TM.end()) {
288 OS << I->second;
289 return;
290 }
Chris Lattner0f578952009-02-28 20:25:14 +0000291 }
292
293 // Otherwise we have a type that has not been named but is a derived type.
294 // Carefully recurse the type hierarchy to print out any contained symbolic
295 // names.
Chris Lattner765338d2009-02-28 20:34:19 +0000296 SmallVector<const Type *, 16> TypeStack;
Chris Lattner0f578952009-02-28 20:25:14 +0000297 std::string TypeName;
Chris Lattner40959d02009-02-28 20:49:40 +0000298
299 raw_string_ostream TypeOS(TypeName);
Chris Lattner242d91a2009-03-01 01:16:21 +0000300 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner40959d02009-02-28 20:49:40 +0000301 OS << TypeOS.str();
302
303 // Cache type name for later use.
Chris Lattner242d91a2009-03-01 01:16:21 +0000304 if (!IgnoreTopLevelName)
305 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner0f578952009-02-28 20:25:14 +0000306}
307
Chris Lattner3243ea12009-03-01 00:03:38 +0000308namespace {
309 class TypeFinder {
310 // To avoid walking constant expressions multiple times and other IR
311 // objects, we keep several helper maps.
312 DenseSet<const Value*> VisitedConstants;
313 DenseSet<const Type*> VisitedTypes;
314
315 TypePrinting &TP;
316 std::vector<const Type*> &NumberedTypes;
317 public:
318 TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
319 : TP(tp), NumberedTypes(numberedTypes) {}
320
321 void Run(const Module &M) {
Chris Lattner84516482009-03-01 00:32:33 +0000322 // Get types from the type symbol table. This gets opaque types referened
323 // only through derived named types.
324 const TypeSymbolTable &ST = M.getTypeSymbolTable();
325 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
326 TI != E; ++TI)
327 IncorporateType(TI->second);
328
Chris Lattner3243ea12009-03-01 00:03:38 +0000329 // Get types from global variables.
330 for (Module::const_global_iterator I = M.global_begin(),
331 E = M.global_end(); I != E; ++I) {
332 IncorporateType(I->getType());
333 if (I->hasInitializer())
334 IncorporateValue(I->getInitializer());
335 }
336
337 // Get types from aliases.
338 for (Module::const_alias_iterator I = M.alias_begin(),
339 E = M.alias_end(); I != E; ++I) {
340 IncorporateType(I->getType());
341 IncorporateValue(I->getAliasee());
342 }
343
344 // Get types from functions.
345 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
346 IncorporateType(FI->getType());
347
348 for (Function::const_iterator BB = FI->begin(), E = FI->end();
349 BB != E;++BB)
350 for (BasicBlock::const_iterator II = BB->begin(),
351 E = BB->end(); II != E; ++II) {
352 const Instruction &I = *II;
353 // Incorporate the type of the instruction and all its operands.
354 IncorporateType(I.getType());
355 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
356 OI != OE; ++OI)
357 IncorporateValue(*OI);
358 }
359 }
360 }
361
362 private:
363 void IncorporateType(const Type *Ty) {
364 // Check to see if we're already visited this type.
Chris Lattner84516482009-03-01 00:32:33 +0000365 if (!VisitedTypes.insert(Ty).second)
Chris Lattner3243ea12009-03-01 00:03:38 +0000366 return;
367
368 // If this is a structure or opaque type, add a name for the type.
Nick Lewycky49f89192009-04-04 07:22:01 +0000369 if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
370 || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
Chris Lattner3243ea12009-03-01 00:03:38 +0000371 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
372 NumberedTypes.push_back(Ty);
373 }
374
375 // Recursively walk all contained types.
376 for (Type::subtype_iterator I = Ty->subtype_begin(),
377 E = Ty->subtype_end(); I != E; ++I)
378 IncorporateType(*I);
379 }
380
381 /// IncorporateValue - This method is used to walk operand lists finding
382 /// types hiding in constant expressions and other operands that won't be
383 /// walked in other ways. GlobalValues, basic blocks, instructions, and
384 /// inst operands are all explicitly enumerated.
385 void IncorporateValue(const Value *V) {
386 if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
387
388 // Already visited?
389 if (!VisitedConstants.insert(V).second)
390 return;
391
392 // Check this type.
393 IncorporateType(V->getType());
394
395 // Look in operands for types.
396 const Constant *C = cast<Constant>(V);
397 for (Constant::const_op_iterator I = C->op_begin(),
398 E = C->op_end(); I != E;++I)
399 IncorporateValue(*I);
400 }
401 };
402} // end anonymous namespace
403
404
405/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
406/// the specified module to the TypePrinter and all numbered types to it and the
407/// NumberedTypes table.
408static void AddModuleTypesToPrinter(TypePrinting &TP,
409 std::vector<const Type*> &NumberedTypes,
410 const Module *M) {
Chris Lattner92c5c122009-02-28 23:20:19 +0000411 if (M == 0) return;
412
413 // If the module has a symbol table, take all global types and stuff their
414 // names into the TypeNames map.
415 const TypeSymbolTable &ST = M->getTypeSymbolTable();
416 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
417 TI != E; ++TI) {
418 const Type *Ty = cast<Type>(TI->second);
419
420 // As a heuristic, don't insert pointer to primitive types, because
421 // they are used too often to have a single useful name.
422 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
423 const Type *PETy = PTy->getElementType();
424 if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
425 !isa<OpaqueType>(PETy))
426 continue;
427 }
428
429 // Likewise don't insert primitives either.
430 if (Ty->isInteger() || Ty->isPrimitiveType())
431 continue;
432
433 // Get the name as a string and insert it into TypeNames.
434 std::string NameStr;
435 raw_string_ostream NameOS(NameStr);
436 PrintLLVMName(NameOS, TI->first.c_str(), TI->first.length(), LocalPrefix);
437 TP.addTypeName(Ty, NameOS.str());
438 }
Chris Lattner3243ea12009-03-01 00:03:38 +0000439
440 // Walk the entire module to find references to unnamed structure and opaque
441 // types. This is required for correctness by opaque types (because multiple
442 // uses of an unnamed opaque type needs to be referred to by the same ID) and
443 // it shrinks complex recursive structure types substantially in some cases.
444 TypeFinder(TP, NumberedTypes).Run(*M);
Chris Lattner92c5c122009-02-28 23:20:19 +0000445}
446
Chris Lattner0f578952009-02-28 20:25:14 +0000447
Chris Lattner0f578952009-02-28 20:25:14 +0000448/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
449/// type, iff there is an entry in the modules symbol table for the specified
Chris Lattner24025262009-02-28 21:05:51 +0000450/// type or one of it's component types.
Chris Lattner0f578952009-02-28 20:25:14 +0000451///
Chris Lattner92c5c122009-02-28 23:20:19 +0000452void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
453 TypePrinting Printer;
Chris Lattner3243ea12009-03-01 00:03:38 +0000454 std::vector<const Type*> NumberedTypes;
455 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattner92c5c122009-02-28 23:20:19 +0000456 Printer.print(Ty, OS);
Chris Lattner0f578952009-02-28 20:25:14 +0000457}
458
Chris Lattner3eee99c2008-08-19 04:36:02 +0000459//===----------------------------------------------------------------------===//
460// SlotTracker Class: Enumerate slot numbers for unnamed values
461//===----------------------------------------------------------------------===//
462
Chris Lattner3ee58762008-08-19 04:28:07 +0000463namespace {
464
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000465/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000466///
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000467class SlotTracker {
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000468public:
Devang Patel983c6b12009-07-08 21:44:25 +0000469 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattnera204d412008-08-17 17:25:25 +0000470 typedef DenseMap<const Value*, unsigned> ValueMap;
Chris Lattner393b7cd2008-08-17 04:17:45 +0000471
472private:
Devang Patel983c6b12009-07-08 21:44:25 +0000473 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000474 const Module* TheModule;
475
Devang Patel983c6b12009-07-08 21:44:25 +0000476 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000477 const Function* TheFunction;
478 bool FunctionProcessed;
479
Devang Patel983c6b12009-07-08 21:44:25 +0000480 /// TheMDNode - The MDNode for which we are holding slot numbers.
481 const MDNode *TheMDNode;
482
483 /// mMap - The TypePlanes map for the module level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000484 ValueMap mMap;
485 unsigned mNext;
486
Devang Patel983c6b12009-07-08 21:44:25 +0000487 /// fMap - The TypePlanes map for the function level data.
Chris Lattner393b7cd2008-08-17 04:17:45 +0000488 ValueMap fMap;
489 unsigned fNext;
490
Devang Patel983c6b12009-07-08 21:44:25 +0000491 /// mdnMap - Map for MDNodes.
492 ValueMap mdnMap;
493 unsigned mdnNext;
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000494public:
Chris Lattner393b7cd2008-08-17 04:17:45 +0000495 /// Construct from a module
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000496 explicit SlotTracker(const Module *M);
Chris Lattner393b7cd2008-08-17 04:17:45 +0000497 /// Construct from a function, starting out in incorp state.
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000498 explicit SlotTracker(const Function *F);
Devang Patel983c6b12009-07-08 21:44:25 +0000499 /// Construct from a mdnode.
500 explicit SlotTracker(const MDNode *N);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000501
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000502 /// Return the slot number of the specified value in it's type
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000503 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner5e043322007-01-11 03:54:27 +0000504 int getLocalSlot(const Value *V);
505 int getGlobalSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000506 int getMetadataSlot(const MDNode *N);
Reid Spencer8beac692004-06-09 15:26:53 +0000507
Misha Brukmanb1c93172005-04-21 23:48:37 +0000508 /// If you'd like to deal with a function instead of just a module, use
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000509 /// this method to get its data into the SlotTracker.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000510 void incorporateFunction(const Function *F) {
511 TheFunction = F;
Reid Spencerb0ac8c42004-08-16 07:46:33 +0000512 FunctionProcessed = false;
513 }
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000514
Misha Brukmanb1c93172005-04-21 23:48:37 +0000515 /// After calling incorporateFunction, use this method to remove the
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000516 /// most recently incorporated function from the SlotTracker. This
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000517 /// will reset the state of the machine back to just the module contents.
518 void purgeFunction();
519
Devang Patel983c6b12009-07-08 21:44:25 +0000520 /// MDNode map iterators.
521 ValueMap::iterator mdnBegin() { return mdnMap.begin(); }
522 ValueMap::iterator mdnEnd() { return mdnMap.end(); }
523 unsigned mdnSize() { return mdnMap.size(); }
524
Reid Spencer56010e42004-05-26 21:56:09 +0000525 /// This function does the actual initialization.
526 inline void initialize();
527
Devang Patel983c6b12009-07-08 21:44:25 +0000528 // Implementation Details
529private:
Chris Lattnerea862a32007-01-09 07:55:49 +0000530 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
531 void CreateModuleSlot(const GlobalValue *V);
Devang Patel983c6b12009-07-08 21:44:25 +0000532
533 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
534 void CreateMetadataSlot(const MDNode *N);
535
Chris Lattnerea862a32007-01-09 07:55:49 +0000536 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
537 void CreateFunctionSlot(const Value *V);
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000538
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000539 /// Add all of the module level global variables (and their initializers)
540 /// and function declarations, but not the contents of those functions.
541 void processModule();
542
Devang Patel983c6b12009-07-08 21:44:25 +0000543 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencer56010e42004-05-26 21:56:09 +0000544 void processFunction();
545
Devang Patel983c6b12009-07-08 21:44:25 +0000546 /// Add all MDNode operands.
547 void processMDNode();
548
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000549 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
550 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000551};
552
Chris Lattner3ee58762008-08-19 04:28:07 +0000553} // end anonymous namespace
Reid Spencer16f2f7f2004-05-26 07:18:52 +0000554
Chris Lattner3eee99c2008-08-19 04:36:02 +0000555
556static SlotTracker *createSlotTracker(const Value *V) {
557 if (const Argument *FA = dyn_cast<Argument>(V))
558 return new SlotTracker(FA->getParent());
559
560 if (const Instruction *I = dyn_cast<Instruction>(V))
561 return new SlotTracker(I->getParent()->getParent());
562
563 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
564 return new SlotTracker(BB->getParent());
565
566 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
567 return new SlotTracker(GV->getParent());
568
569 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
570 return new SlotTracker(GA->getParent());
571
572 if (const Function *Func = dyn_cast<Function>(V))
573 return new SlotTracker(Func);
574
575 return 0;
576}
577
578#if 0
Chris Lattner604e3512008-08-19 04:47:09 +0000579#define ST_DEBUG(X) cerr << X
Chris Lattner3eee99c2008-08-19 04:36:02 +0000580#else
Chris Lattner604e3512008-08-19 04:47:09 +0000581#define ST_DEBUG(X)
Chris Lattner3eee99c2008-08-19 04:36:02 +0000582#endif
583
584// Module level constructor. Causes the contents of the Module (sans functions)
585// to be added to the slot table.
586SlotTracker::SlotTracker(const Module *M)
Devang Patel983c6b12009-07-08 21:44:25 +0000587 : TheModule(M), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
588 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000589}
590
591// Function level constructor. Causes the contents of the Module and the one
592// function provided to be added to the slot table.
593SlotTracker::SlotTracker(const Function *F)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000594 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Devang Patel983c6b12009-07-08 21:44:25 +0000595 TheMDNode(0), mNext(0), fNext(0) {
596}
597
598// Constructor to handle single MDNode.
599SlotTracker::SlotTracker(const MDNode *C)
600 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(C),
601 mNext(0), fNext(0), mdnNext(0) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000602}
603
604inline void SlotTracker::initialize() {
605 if (TheModule) {
606 processModule();
607 TheModule = 0; ///< Prevent re-processing next time we're called.
608 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000609
Chris Lattner3eee99c2008-08-19 04:36:02 +0000610 if (TheFunction && !FunctionProcessed)
611 processFunction();
Devang Patel983c6b12009-07-08 21:44:25 +0000612
613 if (TheMDNode)
614 processMDNode();
Chris Lattner3eee99c2008-08-19 04:36:02 +0000615}
616
617// Iterate through all the global variables, functions, and global
618// variable initializers and create slots for them.
619void SlotTracker::processModule() {
Chris Lattner604e3512008-08-19 04:47:09 +0000620 ST_DEBUG("begin processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000621
622 // Add all of the unnamed global variables to the value table.
623 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel983c6b12009-07-08 21:44:25 +0000624 E = TheModule->global_end(); I != E; ++I) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000625 if (!I->hasName())
626 CreateModuleSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000627 if (I->hasInitializer()) {
628 if (MDNode *N = dyn_cast<MDNode>(I->getInitializer()))
629 CreateMetadataSlot(N);
630 }
631 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000632
633 // Add all the unnamed functions to the table.
634 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
635 I != E; ++I)
636 if (!I->hasName())
637 CreateModuleSlot(I);
638
Chris Lattner604e3512008-08-19 04:47:09 +0000639 ST_DEBUG("end processModule!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000640}
641
Chris Lattner3eee99c2008-08-19 04:36:02 +0000642// Process the arguments, basic blocks, and instructions of a function.
643void SlotTracker::processFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000644 ST_DEBUG("begin processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000645 fNext = 0;
646
647 // Add all the function arguments with no names.
648 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
649 AE = TheFunction->arg_end(); AI != AE; ++AI)
650 if (!AI->hasName())
651 CreateFunctionSlot(AI);
652
Chris Lattner604e3512008-08-19 04:47:09 +0000653 ST_DEBUG("Inserting Instructions:\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000654
655 // Add all of the basic blocks and instructions with no names.
656 for (Function::const_iterator BB = TheFunction->begin(),
657 E = TheFunction->end(); BB != E; ++BB) {
658 if (!BB->hasName())
659 CreateFunctionSlot(BB);
Devang Patel983c6b12009-07-08 21:44:25 +0000660 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
661 ++I) {
Chris Lattner3eee99c2008-08-19 04:36:02 +0000662 if (I->getType() != Type::VoidTy && !I->hasName())
663 CreateFunctionSlot(I);
Devang Patel983c6b12009-07-08 21:44:25 +0000664 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
665 if (MDNode *N = dyn_cast<MDNode>(I->getOperand(i)))
666 CreateMetadataSlot(N);
667 }
Chris Lattner3eee99c2008-08-19 04:36:02 +0000668 }
669
670 FunctionProcessed = true;
671
Chris Lattner604e3512008-08-19 04:47:09 +0000672 ST_DEBUG("end processFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000673}
674
Devang Patel983c6b12009-07-08 21:44:25 +0000675/// processMDNode - Process TheMDNode.
676void SlotTracker::processMDNode() {
677 ST_DEBUG("begin processMDNode!\n");
678 mdnNext = 0;
679 CreateMetadataSlot(TheMDNode);
680 TheMDNode = 0;
681 ST_DEBUG("end processMDNode!\n");
682}
683
Chris Lattner3eee99c2008-08-19 04:36:02 +0000684/// Clean up after incorporating a function. This is the only way to get out of
685/// the function incorporation state that affects get*Slot/Create*Slot. Function
686/// incorporation state is indicated by TheFunction != 0.
687void SlotTracker::purgeFunction() {
Chris Lattner604e3512008-08-19 04:47:09 +0000688 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000689 fMap.clear(); // Simply discard the function level map
690 TheFunction = 0;
691 FunctionProcessed = false;
Chris Lattner604e3512008-08-19 04:47:09 +0000692 ST_DEBUG("end purgeFunction!\n");
Chris Lattner3eee99c2008-08-19 04:36:02 +0000693}
694
695/// getGlobalSlot - Get the slot number of a global value.
696int SlotTracker::getGlobalSlot(const GlobalValue *V) {
697 // Check for uninitialized state and do lazy initialization.
698 initialize();
699
700 // Find the type plane in the module map
701 ValueMap::iterator MI = mMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000702 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000703}
704
Devang Patel983c6b12009-07-08 21:44:25 +0000705/// getGlobalSlot - Get the slot number of a MDNode.
706int SlotTracker::getMetadataSlot(const MDNode *N) {
707 // Check for uninitialized state and do lazy initialization.
708 initialize();
709
710 // Find the type plane in the module map
711 ValueMap::iterator MI = mdnMap.find(N);
712 return MI == mdnMap.end() ? -1 : (int)MI->second;
713}
714
Chris Lattner3eee99c2008-08-19 04:36:02 +0000715
716/// getLocalSlot - Get the slot number for a value that is local to a function.
717int SlotTracker::getLocalSlot(const Value *V) {
718 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
719
720 // Check for uninitialized state and do lazy initialization.
721 initialize();
722
723 ValueMap::iterator FI = fMap.find(V);
Dan Gohman1dd27572008-10-01 19:58:59 +0000724 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000725}
726
727
728/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
729void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
730 assert(V && "Can't insert a null Value into SlotTracker!");
731 assert(V->getType() != Type::VoidTy && "Doesn't need a slot!");
732 assert(!V->hasName() && "Doesn't need a slot!");
733
734 unsigned DestSlot = mNext++;
735 mMap[V] = DestSlot;
736
Chris Lattner604e3512008-08-19 04:47:09 +0000737 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000738 DestSlot << " [");
739 // G = Global, F = Function, A = Alias, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000740 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner3eee99c2008-08-19 04:36:02 +0000741 (isa<Function>(V) ? 'F' :
742 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
743}
744
Chris Lattner3eee99c2008-08-19 04:36:02 +0000745/// CreateSlot - Create a new slot for the specified value if it has no name.
746void SlotTracker::CreateFunctionSlot(const Value *V) {
747 assert(V->getType() != Type::VoidTy && !V->hasName() &&
748 "Doesn't need a slot!");
749
750 unsigned DestSlot = fNext++;
751 fMap[V] = DestSlot;
752
753 // G = Global, F = Function, o = other
Chris Lattner604e3512008-08-19 04:47:09 +0000754 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner3eee99c2008-08-19 04:36:02 +0000755 DestSlot << " [o]\n");
756}
757
Devang Patel983c6b12009-07-08 21:44:25 +0000758/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
759void SlotTracker::CreateMetadataSlot(const MDNode *N) {
760 assert(N && "Can't insert a null Value into SlotTracker!");
761
762 ValueMap::iterator I = mdnMap.find(N);
763 if (I != mdnMap.end())
764 return;
Chris Lattner3eee99c2008-08-19 04:36:02 +0000765
Devang Patel983c6b12009-07-08 21:44:25 +0000766 unsigned DestSlot = mdnNext++;
767 mdnMap[N] = DestSlot;
768
769 for (MDNode::const_elem_iterator MDI = N->elem_begin(),
770 MDE = N->elem_end(); MDI != MDE; ++MDI) {
771 const Value *TV = *MDI;
772 if (TV)
773 if (const MDNode *N2 = dyn_cast<MDNode>(TV))
774 CreateMetadataSlot(N2);
775 }
776}
Chris Lattner3eee99c2008-08-19 04:36:02 +0000777
778//===----------------------------------------------------------------------===//
779// AsmWriter Implementation
780//===----------------------------------------------------------------------===//
Chris Lattner7f8845a2002-07-23 18:07:49 +0000781
Chris Lattner0c19df42008-08-23 22:23:09 +0000782static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattner0f578952009-02-28 20:25:14 +0000783 TypePrinting &TypePrinter,
Chris Lattnere36fd8a2008-08-19 04:26:57 +0000784 SlotTracker *Machine);
Reid Spencer58d30f22004-07-04 11:50:43 +0000785
Chris Lattner033935d2008-08-17 04:40:13 +0000786
Chris Lattnerb86620e2001-10-29 16:37:48 +0000787
Chris Lattnerfc9f1c92006-12-06 06:40:49 +0000788static const char *getPredicateText(unsigned predicate) {
Reid Spencer812a1be2006-12-04 05:19:18 +0000789 const char * pred = "unknown";
790 switch (predicate) {
791 case FCmpInst::FCMP_FALSE: pred = "false"; break;
792 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
793 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
794 case FCmpInst::FCMP_OGE: pred = "oge"; break;
795 case FCmpInst::FCMP_OLT: pred = "olt"; break;
796 case FCmpInst::FCMP_OLE: pred = "ole"; break;
797 case FCmpInst::FCMP_ONE: pred = "one"; break;
798 case FCmpInst::FCMP_ORD: pred = "ord"; break;
799 case FCmpInst::FCMP_UNO: pred = "uno"; break;
800 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
801 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
802 case FCmpInst::FCMP_UGE: pred = "uge"; break;
803 case FCmpInst::FCMP_ULT: pred = "ult"; break;
804 case FCmpInst::FCMP_ULE: pred = "ule"; break;
805 case FCmpInst::FCMP_UNE: pred = "une"; break;
806 case FCmpInst::FCMP_TRUE: pred = "true"; break;
807 case ICmpInst::ICMP_EQ: pred = "eq"; break;
808 case ICmpInst::ICMP_NE: pred = "ne"; break;
809 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
810 case ICmpInst::ICMP_SGE: pred = "sge"; break;
811 case ICmpInst::ICMP_SLT: pred = "slt"; break;
812 case ICmpInst::ICMP_SLE: pred = "sle"; break;
813 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
814 case ICmpInst::ICMP_UGE: pred = "uge"; break;
815 case ICmpInst::ICMP_ULT: pred = "ult"; break;
816 case ICmpInst::ICMP_ULE: pred = "ule"; break;
817 }
818 return pred;
819}
820
Devang Patel983c6b12009-07-08 21:44:25 +0000821static void WriteMDNodes(raw_ostream &Out, TypePrinting &TypePrinter,
822 SlotTracker &Machine) {
823 SmallVector<const MDNode *, 16> Nodes;
824 Nodes.resize(Machine.mdnSize());
825 for (SlotTracker::ValueMap::iterator I =
826 Machine.mdnBegin(), E = Machine.mdnEnd(); I != E; ++I)
827 Nodes[I->second] = cast<MDNode>(I->first);
828
829 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
830 Out << '!' << i << " = constant metadata ";
831 const MDNode *Node = Nodes[i];
832 Out << "!{";
833 for (MDNode::const_elem_iterator NI = Node->elem_begin(),
834 NE = Node->elem_end(); NI != NE;) {
835 const Value *V = *NI;
836 if (!V)
837 Out << "null";
838 else if (const MDNode *N = dyn_cast<MDNode>(V)) {
839 Out << "metadata ";
840 Out << '!' << Machine.getMetadataSlot(N);
841 }
842 else {
843 TypePrinter.print((*NI)->getType(), Out);
844 Out << ' ';
845 WriteAsOperandInternal(Out, *NI, TypePrinter, &Machine);
846 }
847 if (++NI != NE)
848 Out << ", ";
849 }
850 Out << "}\n";
851 }
852}
853
Chris Lattner0c19df42008-08-23 22:23:09 +0000854static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattner0f578952009-02-28 20:25:14 +0000855 TypePrinting &TypePrinter, SlotTracker *Machine) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000856 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Chris Lattner17f71652008-08-17 07:19:36 +0000857 if (CI->getType() == Type::Int1Ty) {
Reid Spencercddc9df2007-01-12 04:24:46 +0000858 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattner17f71652008-08-17 07:19:36 +0000859 return;
860 }
861 Out << CI->getValue();
862 return;
863 }
864
865 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen028084e2007-09-12 03:30:33 +0000866 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
867 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
868 // We would like to output the FP constant value in exponential notation,
869 // but we cannot do this if doing so will lose precision. Check here to
870 // make sure that we only output it in exponential format if we can parse
871 // the value back and get the same value.
872 //
Dale Johannesen1f864982009-01-21 20:32:55 +0000873 bool ignored;
Dale Johannesen028084e2007-09-12 03:30:33 +0000874 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattner17f71652008-08-17 07:19:36 +0000875 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
876 CFP->getValueAPF().convertToFloat();
Dale Johannesen028084e2007-09-12 03:30:33 +0000877 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner1e194682002-04-18 18:53:13 +0000878
Dale Johannesen028084e2007-09-12 03:30:33 +0000879 // Check to make sure that the stringized number is not some string like
880 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
881 // that the string matches the "[-+]?[0-9]" regex.
882 //
883 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
884 ((StrVal[0] == '-' || StrVal[0] == '+') &&
885 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
886 // Reparse stringized version!
887 if (atof(StrVal.c_str()) == Val) {
888 Out << StrVal;
889 return;
890 }
Chris Lattner1e194682002-04-18 18:53:13 +0000891 }
Dale Johannesen028084e2007-09-12 03:30:33 +0000892 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen1f864982009-01-21 20:32:55 +0000893 // output the string in hexadecimal format! Note that loading and storing
894 // floating point types changes the bits of NaNs on some hosts, notably
895 // x86, so we must not use these types.
Dale Johannesen028084e2007-09-12 03:30:33 +0000896 assert(sizeof(double) == sizeof(uint64_t) &&
897 "assuming that double is 64 bits!");
Chris Lattner5505eed2008-11-10 04:30:26 +0000898 char Buffer[40];
Dale Johannesen1f864982009-01-21 20:32:55 +0000899 APFloat apf = CFP->getValueAPF();
900 // Floats are represented in ASCII IR as double, convert.
901 if (!isDouble)
902 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
903 &ignored);
904 Out << "0x" <<
905 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
906 Buffer+40);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000907 return;
908 }
909
910 // Some form of long double. These appear as a magic letter identifying
911 // the type, then a fixed number of hex digits.
912 Out << "0x";
Dale Johannesen93eefa02009-03-23 21:16:53 +0000913 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000914 Out << 'K';
Dale Johannesen93eefa02009-03-23 21:16:53 +0000915 // api needed to prevent premature destruction
916 APInt api = CFP->getValueAPF().bitcastToAPInt();
917 const uint64_t* p = api.getRawData();
918 uint64_t word = p[1];
919 int shiftcount=12;
920 int width = api.getBitWidth();
921 for (int j=0; j<width; j+=4, shiftcount-=4) {
922 unsigned int nibble = (word>>shiftcount) & 15;
923 if (nibble < 10)
924 Out << (unsigned char)(nibble + '0');
925 else
926 Out << (unsigned char)(nibble - 10 + 'A');
927 if (shiftcount == 0 && j+4 < width) {
928 word = *p;
929 shiftcount = 64;
930 if (width-j-4 < 64)
931 shiftcount = width-j-4;
932 }
933 }
934 return;
935 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000936 Out << 'L';
937 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
938 Out << 'M';
939 else
940 assert(0 && "Unsupported floating point type");
941 // api needed to prevent premature destruction
Dale Johannesen54306fe2008-10-09 18:53:47 +0000942 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000943 const uint64_t* p = api.getRawData();
944 uint64_t word = *p;
945 int shiftcount=60;
946 int width = api.getBitWidth();
947 for (int j=0; j<width; j+=4, shiftcount-=4) {
948 unsigned int nibble = (word>>shiftcount) & 15;
949 if (nibble < 10)
950 Out << (unsigned char)(nibble + '0');
Dale Johannesen028084e2007-09-12 03:30:33 +0000951 else
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000952 Out << (unsigned char)(nibble - 10 + 'A');
953 if (shiftcount == 0 && j+4 < width) {
954 word = *(++p);
955 shiftcount = 64;
956 if (width-j-4 < 64)
957 shiftcount = width-j-4;
Dale Johannesen028084e2007-09-12 03:30:33 +0000958 }
959 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000960 return;
961 }
962
963 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattner76b2ff42004-02-15 05:55:15 +0000964 Out << "zeroinitializer";
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000965 return;
966 }
967
968 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner1e194682002-04-18 18:53:13 +0000969 // As a special case, print the array as a string if it is an array of
Dan Gohmane9bc2ba2008-05-12 16:34:30 +0000970 // i8 with ConstantInt values.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000971 //
Chris Lattner1e194682002-04-18 18:53:13 +0000972 const Type *ETy = CA->getType()->getElementType();
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000973 if (CA->isString()) {
Chris Lattner1e194682002-04-18 18:53:13 +0000974 Out << "c\"";
Chris Lattner6ed87bd2006-01-23 23:03:36 +0000975 PrintEscapedString(CA->getAsString(), Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000976 Out << '"';
Chris Lattner1e194682002-04-18 18:53:13 +0000977 } else { // Cannot output in string format...
Misha Brukman21bbdb92004-06-04 21:11:51 +0000978 Out << '[';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000979 if (CA->getNumOperands()) {
Chris Lattnere101c442009-02-28 21:26:53 +0000980 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000981 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000982 WriteAsOperandInternal(Out, CA->getOperand(0),
Chris Lattner0f578952009-02-28 20:25:14 +0000983 TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000984 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
985 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +0000986 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +0000987 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +0000988 WriteAsOperandInternal(Out, CA->getOperand(i), TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +0000989 }
990 }
Dan Gohman81313fd2008-09-14 17:21:12 +0000991 Out << ']';
Chris Lattnerd84bb632002-04-16 21:36:08 +0000992 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +0000993 return;
994 }
995
996 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth0d124b82007-01-08 18:21:30 +0000997 if (CS->getType()->isPacked())
998 Out << '<';
Misha Brukman21bbdb92004-06-04 21:11:51 +0000999 Out << '{';
Jim Laskey3bb78742006-02-25 12:27:03 +00001000 unsigned N = CS->getNumOperands();
1001 if (N) {
Chris Lattner604e3512008-08-19 04:47:09 +00001002 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001003 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001004 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001005
Chris Lattner0f578952009-02-28 20:25:14 +00001006 WriteAsOperandInternal(Out, CS->getOperand(0), TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001007
Jim Laskey3bb78742006-02-25 12:27:03 +00001008 for (unsigned i = 1; i < N; i++) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001009 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001010 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001011 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001012
Chris Lattner0f578952009-02-28 20:25:14 +00001013 WriteAsOperandInternal(Out, CS->getOperand(i), TypePrinter, Machine);
Chris Lattnerd84bb632002-04-16 21:36:08 +00001014 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001015 Out << ' ';
Chris Lattnerd84bb632002-04-16 21:36:08 +00001016 }
Jim Laskey3bb78742006-02-25 12:27:03 +00001017
Dan Gohman81313fd2008-09-14 17:21:12 +00001018 Out << '}';
Andrew Lenharth0d124b82007-01-08 18:21:30 +00001019 if (CS->getType()->isPacked())
1020 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001021 return;
1022 }
1023
1024 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1025 const Type *ETy = CP->getType()->getElementType();
1026 assert(CP->getNumOperands() > 0 &&
1027 "Number of operands for a PackedConst must be > 0");
Dan Gohman1262a252009-02-11 00:25:25 +00001028 Out << '<';
Chris Lattnere101c442009-02-28 21:26:53 +00001029 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001030 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001031 WriteAsOperandInternal(Out, CP->getOperand(0), TypePrinter, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001032 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner585297e82008-08-19 05:26:17 +00001033 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001034 TypePrinter.print(ETy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001035 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001036 WriteAsOperandInternal(Out, CP->getOperand(i), TypePrinter, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001037 }
Dan Gohman1262a252009-02-11 00:25:25 +00001038 Out << '>';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001039 return;
1040 }
1041
1042 if (isa<ConstantPointerNull>(CV)) {
Chris Lattnerd84bb632002-04-16 21:36:08 +00001043 Out << "null";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001044 return;
1045 }
1046
1047 if (isa<UndefValue>(CV)) {
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001048 Out << "undef";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001049 return;
1050 }
Nick Lewycky49f89192009-04-04 07:22:01 +00001051
1052 if (const MDString *S = dyn_cast<MDString>(CV)) {
1053 Out << "!\"";
1054 PrintEscapedString(S->begin(), S->size(), Out);
1055 Out << '"';
1056 return;
1057 }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001058
Devang Patel983c6b12009-07-08 21:44:25 +00001059 if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
1060 Out << "!" << Machine->getMetadataSlot(Node);
1061 return;
1062 }
1063
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001064 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer812a1be2006-12-04 05:19:18 +00001065 Out << CE->getOpcodeName();
1066 if (CE->isCompare())
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001067 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer812a1be2006-12-04 05:19:18 +00001068 Out << " (";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001069
Vikram S. Adveb952b542002-07-14 23:14:45 +00001070 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattnere101c442009-02-28 21:26:53 +00001071 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001072 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001073 WriteAsOperandInternal(Out, *OI, TypePrinter, Machine);
Vikram S. Adveb952b542002-07-14 23:14:45 +00001074 if (OI+1 != CE->op_end())
Chris Lattner3cd8c562002-07-30 18:54:25 +00001075 Out << ", ";
Vikram S. Adveb952b542002-07-14 23:14:45 +00001076 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001077
Dan Gohmana76f0f72008-05-31 19:12:39 +00001078 if (CE->hasIndices()) {
1079 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
1080 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1081 Out << ", " << Indices[i];
1082 }
1083
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001084 if (CE->isCast()) {
Chris Lattner83b396b2002-08-15 19:37:43 +00001085 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001086 TypePrinter.print(CE->getType(), Out);
Chris Lattner83b396b2002-08-15 19:37:43 +00001087 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001088
Misha Brukman21bbdb92004-06-04 21:11:51 +00001089 Out << ')';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001090 return;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001091 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001092
1093 Out << "<placeholder or erroneous Constant>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001094}
1095
1096
Misha Brukmanc566ca362004-03-02 00:22:19 +00001097/// WriteAsOperand - Write the name of the specified value out to the specified
1098/// ostream. This can be useful when you just want to print int %reg126, not
1099/// the whole instruction that generated it.
1100///
Chris Lattner0c19df42008-08-23 22:23:09 +00001101static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Chris Lattner0f578952009-02-28 20:25:14 +00001102 TypePrinting &TypePrinter,
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001103 SlotTracker *Machine) {
Chris Lattner033935d2008-08-17 04:40:13 +00001104 if (V->hasName()) {
1105 PrintLLVMName(Out, V);
1106 return;
1107 }
1108
1109 const Constant *CV = dyn_cast<Constant>(V);
1110 if (CV && !isa<GlobalValue>(CV)) {
Chris Lattner0f578952009-02-28 20:25:14 +00001111 WriteConstantInt(Out, CV, TypePrinter, Machine);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001112 return;
1113 }
1114
1115 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattner033935d2008-08-17 04:40:13 +00001116 Out << "asm ";
1117 if (IA->hasSideEffects())
1118 Out << "sideeffect ";
1119 Out << '"';
1120 PrintEscapedString(IA->getAsmString(), Out);
1121 Out << "\", \"";
1122 PrintEscapedString(IA->getConstraintString(), Out);
1123 Out << '"';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001124 return;
1125 }
1126
1127 char Prefix = '%';
1128 int Slot;
1129 if (Machine) {
1130 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1131 Slot = Machine->getGlobalSlot(GV);
1132 Prefix = '@';
1133 } else {
1134 Slot = Machine->getLocalSlot(V);
1135 }
Chris Lattner033935d2008-08-17 04:40:13 +00001136 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001137 Machine = createSlotTracker(V);
Chris Lattner033935d2008-08-17 04:40:13 +00001138 if (Machine) {
1139 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1140 Slot = Machine->getGlobalSlot(GV);
1141 Prefix = '@';
1142 } else {
1143 Slot = Machine->getLocalSlot(V);
1144 }
Chris Lattnera2d810d2006-01-25 22:26:05 +00001145 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001146 Slot = -1;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001147 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001148 delete Machine;
Chris Lattnerd84bb632002-04-16 21:36:08 +00001149 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001150
1151 if (Slot != -1)
1152 Out << Prefix << Slot;
1153 else
1154 Out << "<badref>";
Chris Lattnerd84bb632002-04-16 21:36:08 +00001155}
1156
Misha Brukmanb22d09c2004-03-01 19:48:13 +00001157/// WriteAsOperand - Write the name of the specified value out to the specified
1158/// ostream. This can be useful when you just want to print int %reg126, not
1159/// the whole instruction that generated it.
1160///
Chris Lattner604e3512008-08-19 04:47:09 +00001161void llvm::WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType,
1162 const Module *Context) {
Chris Lattner0c19df42008-08-23 22:23:09 +00001163 raw_os_ostream OS(Out);
1164 WriteAsOperand(OS, V, PrintType, Context);
1165}
1166
1167void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType,
1168 const Module *Context) {
Chris Lattner5a9f63e2002-07-10 16:48:17 +00001169 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattnerb86620e2001-10-29 16:37:48 +00001170
Chris Lattner92c5c122009-02-28 23:20:19 +00001171 TypePrinting TypePrinter;
Chris Lattner3243ea12009-03-01 00:03:38 +00001172 std::vector<const Type*> NumberedTypes;
1173 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman81313fd2008-09-14 17:21:12 +00001174 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001175 TypePrinter.print(V->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001176 Out << ' ';
1177 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001178
Chris Lattner0f578952009-02-28 20:25:14 +00001179 WriteAsOperandInternal(Out, V, TypePrinter, 0);
Chris Lattner5e5abe32001-07-20 19:15:21 +00001180}
1181
Reid Spencer58d30f22004-07-04 11:50:43 +00001182
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001183namespace {
Chris Lattner2e9fee42001-07-12 23:35:26 +00001184
Chris Lattnerfee714f2001-09-07 16:36:04 +00001185class AssemblyWriter {
Chris Lattner0c19df42008-08-23 22:23:09 +00001186 raw_ostream &Out;
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001187 SlotTracker &Machine;
Chris Lattner7bfee412001-10-29 16:05:51 +00001188 const Module *TheModule;
Chris Lattner0f578952009-02-28 20:25:14 +00001189 TypePrinting TypePrinter;
Chris Lattner8339f7d2003-10-30 23:41:03 +00001190 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner3243ea12009-03-01 00:03:38 +00001191 std::vector<const Type*> NumberedTypes;
Devang Patel39e64d42009-07-01 19:21:12 +00001192
1193 // Each MDNode is assigned unique MetadataIDNo.
1194 std::map<const MDNode *, unsigned> MDNodes;
1195 unsigned MetadataIDNo;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001196public:
Chris Lattner0c19df42008-08-23 22:23:09 +00001197 inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M,
Chris Lattner8339f7d2003-10-30 23:41:03 +00001198 AssemblyAnnotationWriter *AAW)
Devang Patel39e64d42009-07-01 19:21:12 +00001199 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW), MetadataIDNo(0) {
Chris Lattner3243ea12009-03-01 00:03:38 +00001200 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001201 }
1202
Chris Lattner3243ea12009-03-01 00:03:38 +00001203 void write(const Module *M) { printModule(M); }
Chris Lattner0c19df42008-08-23 22:23:09 +00001204
1205 void write(const GlobalValue *G) {
1206 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
1207 printGlobal(GV);
1208 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
1209 printAlias(GA);
1210 else if (const Function *F = dyn_cast<Function>(G))
1211 printFunction(F);
1212 else
1213 assert(0 && "Unknown global");
1214 }
1215
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001216 void write(const BasicBlock *BB) { printBasicBlock(BB); }
1217 void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001218
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001219 void writeOperand(const Value *Op, bool PrintType);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001220 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner1e194682002-04-18 18:53:13 +00001221
Misha Brukman4685e262004-04-28 15:31:21 +00001222 const Module* getModule() { return TheModule; }
1223
Misha Brukmand92f54a2004-11-15 19:30:05 +00001224private:
Chris Lattner7bfee412001-10-29 16:05:51 +00001225 void printModule(const Module *M);
Reid Spencer32af9e82007-01-06 07:24:44 +00001226 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattner7bfee412001-10-29 16:05:51 +00001227 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001228 void printAlias(const GlobalAlias *GV);
Chris Lattner57698e22002-03-26 18:01:55 +00001229 void printFunction(const Function *F);
Devang Patelba3fa6c2008-09-23 23:03:40 +00001230 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattner7bfee412001-10-29 16:05:51 +00001231 void printBasicBlock(const BasicBlock *BB);
Chris Lattner113f4f42002-06-25 16:13:24 +00001232 void printInstruction(const Instruction &I);
Chris Lattnerd816b532002-04-13 20:53:41 +00001233
Chris Lattner862e3382001-10-13 06:42:36 +00001234 // printInfoComment - Print a little comment after the instruction indicating
1235 // which slot it occupies.
Chris Lattner113f4f42002-06-25 16:13:24 +00001236 void printInfoComment(const Value &V);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001237};
Chris Lattner3243ea12009-03-01 00:03:38 +00001238} // end of anonymous namespace
Chris Lattner2f7c9632001-06-06 20:29:01 +00001239
Chris Lattnerd816b532002-04-13 20:53:41 +00001240
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001241void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1242 if (Operand == 0) {
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001243 Out << "<null operand!>";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001244 } else {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001245 if (PrintType) {
Chris Lattnere101c442009-02-28 21:26:53 +00001246 TypePrinter.print(Operand->getType(), Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001247 Out << ' ';
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001248 }
Chris Lattner0f578952009-02-28 20:25:14 +00001249 WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine);
Chris Lattner08f7d0c2005-02-24 16:58:29 +00001250 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001251}
1252
Dale Johannesen89268bc2008-02-19 21:38:47 +00001253void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001254 Attributes Attrs) {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001255 if (Operand == 0) {
1256 Out << "<null operand!>";
1257 } else {
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001258 // Print the type
Chris Lattnere101c442009-02-28 21:26:53 +00001259 TypePrinter.print(Operand->getType(), Out);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001260 // Print parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001261 if (Attrs != Attribute::None)
1262 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman81313fd2008-09-14 17:21:12 +00001263 Out << ' ';
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001264 // Print the operand
Chris Lattner0f578952009-02-28 20:25:14 +00001265 WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine);
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001266 }
1267}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001268
Chris Lattner7bfee412001-10-29 16:05:51 +00001269void AssemblyWriter::printModule(const Module *M) {
Chris Lattner4d8689e2005-03-02 23:12:40 +00001270 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001271 // Don't print the ID if it will start a new line (which would
Chris Lattner4d8689e2005-03-02 23:12:40 +00001272 // require a comment char before it).
1273 M->getModuleIdentifier().find('\n') == std::string::npos)
1274 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1275
Owen Andersone2237542006-10-18 02:21:12 +00001276 if (!M->getDataLayout().empty())
Chris Lattner04897162006-10-22 06:06:56 +00001277 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencer48f98c82004-07-25 21:44:54 +00001278 if (!M->getTargetTriple().empty())
Reid Spencerffec7df2004-07-25 21:29:43 +00001279 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001280
Chris Lattnereef2fe72006-01-24 04:13:11 +00001281 if (!M->getModuleInlineAsm().empty()) {
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001282 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnereef2fe72006-01-24 04:13:11 +00001283 std::string Asm = M->getModuleInlineAsm();
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001284 size_t CurPos = 0;
1285 size_t NewLine = Asm.find_first_of('\n', CurPos);
1286 while (NewLine != std::string::npos) {
1287 // We found a newline, print the portion of the asm string from the
1288 // last newline up to this newline.
1289 Out << "module asm \"";
1290 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1291 Out);
1292 Out << "\"\n";
1293 CurPos = NewLine+1;
1294 NewLine = Asm.find_first_of('\n', CurPos);
1295 }
Chris Lattner3acaf5c2006-01-24 00:40:17 +00001296 Out << "module asm \"";
Chris Lattnerefaf35d2006-01-24 00:45:30 +00001297 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner6ed87bd2006-01-23 23:03:36 +00001298 Out << "\"\n";
1299 }
1300
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001301 // Loop over the dependent libraries and emit them.
Chris Lattner730cfe42004-09-14 04:51:44 +00001302 Module::lib_iterator LI = M->lib_begin();
1303 Module::lib_iterator LE = M->lib_end();
Reid Spencer48f98c82004-07-25 21:44:54 +00001304 if (LI != LE) {
Chris Lattner730cfe42004-09-14 04:51:44 +00001305 Out << "deplibs = [ ";
1306 while (LI != LE) {
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001307 Out << '"' << *LI << '"';
Reid Spencerffec7df2004-07-25 21:29:43 +00001308 ++LI;
Chris Lattner730cfe42004-09-14 04:51:44 +00001309 if (LI != LE)
1310 Out << ", ";
Reid Spencerffec7df2004-07-25 21:29:43 +00001311 }
1312 Out << " ]\n";
Reid Spencercc5ff642004-07-25 18:08:18 +00001313 }
Reid Spencerb9e08772004-09-13 23:44:23 +00001314
Chris Lattner3243ea12009-03-01 00:03:38 +00001315 // Loop over the symbol table, emitting all id'd types.
Reid Spencer32af9e82007-01-06 07:24:44 +00001316 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanb1c93172005-04-21 23:48:37 +00001317
Chris Lattner54932b02006-12-06 04:41:52 +00001318 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1319 I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +00001320 printGlobal(I);
Chris Lattnerd2747052007-04-26 02:24:10 +00001321
1322 // Output all aliases.
1323 if (!M->alias_empty()) Out << "\n";
1324 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1325 I != E; ++I)
1326 printAlias(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001327
Chris Lattner2cdd49d2004-09-14 05:06:58 +00001328 // Output all of the functions.
Chris Lattner113f4f42002-06-25 16:13:24 +00001329 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1330 printFunction(I);
Devang Patel983c6b12009-07-08 21:44:25 +00001331
1332 WriteMDNodes(Out, TypePrinter, Machine);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001333}
1334
Chris Lattner0c19df42008-08-23 22:23:09 +00001335static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001336 switch (LT) {
Duncan Sands12da8ce2009-03-07 15:45:40 +00001337 case GlobalValue::PrivateLinkage: Out << "private "; break;
1338 case GlobalValue::InternalLinkage: Out << "internal "; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001339 case GlobalValue::AvailableExternallyLinkage:
1340 Out << "available_externally ";
1341 break;
Duncan Sands12da8ce2009-03-07 15:45:40 +00001342 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1343 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1344 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1345 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
Duncan Sands4581beb2009-03-11 20:14:15 +00001346 case GlobalValue::CommonLinkage: Out << "common "; break;
Duncan Sands12da8ce2009-03-07 15:45:40 +00001347 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1348 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1349 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
Duncan Sandse2881052009-03-11 08:08:06 +00001350 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001351 case GlobalValue::ExternalLinkage: break;
1352 case GlobalValue::GhostLinkage:
Torok Edwinfb8d6d52009-07-08 20:53:28 +00001353 LLVM_UNREACHABLE("GhostLinkage not allowed in AsmWriter!");
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001354 }
1355}
Duncan Sands12da8ce2009-03-07 15:45:40 +00001356
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001357
1358static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Chris Lattner0c19df42008-08-23 22:23:09 +00001359 raw_ostream &Out) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001360 switch (Vis) {
1361 default: assert(0 && "Invalid visibility style!");
1362 case GlobalValue::DefaultVisibility: break;
1363 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1364 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1365 }
1366}
1367
Chris Lattner7bfee412001-10-29 16:05:51 +00001368void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Chris Lattner033935d2008-08-17 04:40:13 +00001369 if (GV->hasName()) {
1370 PrintLLVMName(Out, GV);
1371 Out << " = ";
1372 }
Chris Lattner37798642001-09-18 04:01:05 +00001373
Chris Lattner1508d3f2008-08-19 05:16:28 +00001374 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1375 Out << "external ";
1376
1377 PrintLinkage(GV->getLinkage(), Out);
1378 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancio749e4662007-04-12 18:32:50 +00001379
1380 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerac161bf2009-01-02 07:01:27 +00001381 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1382 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001383 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattnere101c442009-02-28 21:26:53 +00001384 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattner37798642001-09-18 04:01:05 +00001385
Dan Gohman81313fd2008-09-14 17:21:12 +00001386 if (GV->hasInitializer()) {
1387 Out << ' ';
Devang Patel983c6b12009-07-08 21:44:25 +00001388 writeOperand(GV->getInitializer(), false);
Dan Gohman81313fd2008-09-14 17:21:12 +00001389 }
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001390
Chris Lattner4b96c542005-11-12 00:10:19 +00001391 if (GV->hasSection())
1392 Out << ", section \"" << GV->getSection() << '"';
1393 if (GV->getAlignment())
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001394 Out << ", align " << GV->getAlignment();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001395
Chris Lattner113f4f42002-06-25 16:13:24 +00001396 printInfoComment(*GV);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001397 Out << '\n';
Chris Lattnerda975502001-09-10 07:58:01 +00001398}
1399
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001400void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen83e468a2008-06-03 18:14:29 +00001401 // Don't crash when dumping partially built GA
1402 if (!GA->hasName())
1403 Out << "<<nameless>> = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001404 else {
1405 PrintLLVMName(Out, GA);
1406 Out << " = ";
1407 }
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001408 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001409
1410 Out << "alias ";
1411
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001412 PrintLinkage(GA->getLinkage(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001413
Anton Korobeynikov546ea7e2007-04-29 18:02:48 +00001414 const Constant *Aliasee = GA->getAliasee();
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001415
1416 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001417 TypePrinter.print(GV->getType(), Out);
Chris Lattner033935d2008-08-17 04:40:13 +00001418 Out << ' ';
1419 PrintLLVMName(Out, GV);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001420 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001421 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001422 Out << "* ";
1423
Chris Lattner0f578952009-02-28 20:25:14 +00001424 WriteAsOperandInternal(Out, F, TypePrinter, &Machine);
Anton Korobeynikov72d5d422008-03-22 08:17:17 +00001425 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattnere101c442009-02-28 21:26:53 +00001426 TypePrinter.print(GA->getType(), Out);
1427 Out << ' ';
Chris Lattner033935d2008-08-17 04:40:13 +00001428 PrintLLVMName(Out, GA);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001429 } else {
Chris Lattnercde89e42009-04-25 21:23:19 +00001430 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1431 // The only valid GEP is an all zero GEP.
1432 assert((CE->getOpcode() == Instruction::BitCast ||
1433 CE->getOpcode() == Instruction::GetElementPtr) &&
1434 "Unsupported aliasee");
1435 writeOperand(CE, false);
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +00001436 }
1437
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001438 printInfoComment(*GA);
Chris Lattner1508d3f2008-08-19 05:16:28 +00001439 Out << '\n';
Anton Korobeynikova97b6942007-04-25 14:27:10 +00001440}
1441
Reid Spencer32af9e82007-01-06 07:24:44 +00001442void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner3243ea12009-03-01 00:03:38 +00001443 // Emit all numbered types.
1444 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
1445 Out << "\ttype ";
1446
1447 // Make sure we print out at least one level of the type structure, so
1448 // that we do not get %2 = type %2
1449 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
1450 Out << "\t\t; type %" << i << '\n';
1451 }
1452
1453 // Print the named types.
Reid Spencer32af9e82007-01-06 07:24:44 +00001454 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1455 TI != TE; ++TI) {
Chris Lattner1508d3f2008-08-19 05:16:28 +00001456 Out << '\t';
1457 PrintLLVMName(Out, &TI->first[0], TI->first.size(), LocalPrefix);
1458 Out << " = type ";
Reid Spencere7e96712004-05-25 08:53:40 +00001459
1460 // Make sure we print out at least one level of the type structure, so
1461 // that we do not get %FILE = type %FILE
Chris Lattnere101c442009-02-28 21:26:53 +00001462 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001463 Out << '\n';
Reid Spencere7e96712004-05-25 08:53:40 +00001464 }
Reid Spencer32af9e82007-01-06 07:24:44 +00001465}
1466
Misha Brukmanc566ca362004-03-02 00:22:19 +00001467/// printFunction - Print all aspects of a function.
1468///
Chris Lattner113f4f42002-06-25 16:13:24 +00001469void AssemblyWriter::printFunction(const Function *F) {
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001470 // Print out the return type and name.
1471 Out << '\n';
Chris Lattner379a8d22003-04-16 20:28:45 +00001472
Misha Brukmana6619a92004-06-21 21:53:56 +00001473 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001474
Reid Spencer5301e7c2007-01-30 20:08:39 +00001475 if (F->isDeclaration())
Chris Lattner10f03a62007-08-19 22:15:26 +00001476 Out << "declare ";
1477 else
Reid Spencer7ce2d2a2006-12-29 20:29:48 +00001478 Out << "define ";
Chris Lattner3a36d2c2008-08-19 05:06:27 +00001479
1480 PrintLinkage(F->getLinkage(), Out);
1481 PrintVisibility(F->getVisibility(), Out);
Chris Lattner379a8d22003-04-16 20:28:45 +00001482
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001483 // Print the calling convention.
1484 switch (F->getCallingConv()) {
1485 case CallingConv::C: break; // default
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +00001486 case CallingConv::Fast: Out << "fastcc "; break;
1487 case CallingConv::Cold: Out << "coldcc "; break;
1488 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001489 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1490 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1491 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1492 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001493 default: Out << "cc" << F->getCallingConv() << " "; break;
1494 }
1495
Reid Spencer8c4914c2006-12-31 05:24:50 +00001496 const FunctionType *FT = F->getFunctionType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001497 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel221fe422008-09-29 20:49:50 +00001498 Attributes RetAttrs = Attrs.getRetAttributes();
1499 if (RetAttrs != Attribute::None)
1500 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001501 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001502 Out << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001503 WriteAsOperandInternal(Out, F, TypePrinter, &Machine);
Misha Brukmana6619a92004-06-21 21:53:56 +00001504 Out << '(';
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001505 Machine.incorporateFunction(F);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001506
Chris Lattner7bfee412001-10-29 16:05:51 +00001507 // Loop over the arguments, printing them...
Chris Lattnerfee714f2001-09-07 16:36:04 +00001508
Reid Spencer8c4914c2006-12-31 05:24:50 +00001509 unsigned Idx = 1;
Chris Lattner82738fe2007-04-18 00:57:22 +00001510 if (!F->isDeclaration()) {
1511 // If this isn't a declaration, print the argument names as well.
1512 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1513 I != E; ++I) {
1514 // Insert commas as we go... the first arg doesn't get a comma
1515 if (I != F->arg_begin()) Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001516 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner82738fe2007-04-18 00:57:22 +00001517 Idx++;
1518 }
1519 } else {
1520 // Otherwise, print the types from the function type.
1521 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1522 // Insert commas as we go... the first arg doesn't get a comma
1523 if (i) Out << ", ";
1524
1525 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001526 TypePrinter.print(FT->getParamType(i), Out);
Chris Lattner82738fe2007-04-18 00:57:22 +00001527
Devang Patela05633e2008-09-26 22:53:05 +00001528 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel4c758ea2008-09-25 21:00:45 +00001529 if (ArgAttrs != Attribute::None)
1530 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner82738fe2007-04-18 00:57:22 +00001531 }
Reid Spencer8c4914c2006-12-31 05:24:50 +00001532 }
Chris Lattnerfee714f2001-09-07 16:36:04 +00001533
1534 // Finish printing arguments...
Chris Lattner113f4f42002-06-25 16:13:24 +00001535 if (FT->isVarArg()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001536 if (FT->getNumParams()) Out << ", ";
1537 Out << "..."; // Output varargs portion of signature!
Chris Lattnerfee714f2001-09-07 16:36:04 +00001538 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001539 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001540 Attributes FnAttrs = Attrs.getFnAttributes();
1541 if (FnAttrs != Attribute::None)
1542 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner4b96c542005-11-12 00:10:19 +00001543 if (F->hasSection())
1544 Out << " section \"" << F->getSection() << '"';
Chris Lattnerf8a974d2005-11-06 06:48:53 +00001545 if (F->getAlignment())
1546 Out << " align " << F->getAlignment();
Gordon Henriksend930f912008-08-17 18:44:35 +00001547 if (F->hasGC())
1548 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5301e7c2007-01-30 20:08:39 +00001549 if (F->isDeclaration()) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001550 Out << "\n";
Chris Lattnerb2f02e52002-05-06 03:00:40 +00001551 } else {
Chris Lattnerd5fbc8f2008-04-21 06:12:55 +00001552 Out << " {";
Misha Brukmanb1c93172005-04-21 23:48:37 +00001553
Chris Lattner6915f8f2002-04-07 22:49:37 +00001554 // Output all of its basic blocks... for the function
Chris Lattner113f4f42002-06-25 16:13:24 +00001555 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1556 printBasicBlock(I);
Chris Lattnerfee714f2001-09-07 16:36:04 +00001557
Misha Brukmana6619a92004-06-21 21:53:56 +00001558 Out << "}\n";
Chris Lattnerfee714f2001-09-07 16:36:04 +00001559 }
1560
Reid Spencer16f2f7f2004-05-26 07:18:52 +00001561 Machine.purgeFunction();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001562}
1563
Misha Brukmanc566ca362004-03-02 00:22:19 +00001564/// printArgument - This member is called for every argument that is passed into
1565/// the function. Simply print it out
1566///
Dale Johannesen89268bc2008-02-19 21:38:47 +00001567void AssemblyWriter::printArgument(const Argument *Arg,
Devang Patelba3fa6c2008-09-23 23:03:40 +00001568 Attributes Attrs) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001569 // Output type...
Chris Lattnere101c442009-02-28 21:26:53 +00001570 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanb1c93172005-04-21 23:48:37 +00001571
Duncan Sandsad0ea2d2007-11-27 13:23:08 +00001572 // Output parameter attributes list
Devang Patel4c758ea2008-09-25 21:00:45 +00001573 if (Attrs != Attribute::None)
1574 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencer8c4914c2006-12-31 05:24:50 +00001575
Chris Lattner2f7c9632001-06-06 20:29:01 +00001576 // Output name, if available...
Chris Lattner033935d2008-08-17 04:40:13 +00001577 if (Arg->hasName()) {
1578 Out << ' ';
1579 PrintLLVMName(Out, Arg);
1580 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001581}
1582
Misha Brukmanc566ca362004-03-02 00:22:19 +00001583/// printBasicBlock - This member is called for each basic block in a method.
1584///
Chris Lattner7bfee412001-10-29 16:05:51 +00001585void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001586 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001587 Out << "\n";
Chris Lattner1508d3f2008-08-19 05:16:28 +00001588 PrintLLVMName(Out, BB->getNameStart(), BB->getNameLen(), LabelPrefix);
Chris Lattner033935d2008-08-17 04:40:13 +00001589 Out << ':';
Nick Lewycky4d43d3c2008-04-25 16:53:59 +00001590 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner67bec132008-04-21 04:20:33 +00001591 Out << "\n; <label>:";
Chris Lattner5e043322007-01-11 03:54:27 +00001592 int Slot = Machine.getLocalSlot(BB);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001593 if (Slot != -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001594 Out << Slot;
Chris Lattner757ee0b2004-06-09 19:41:19 +00001595 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001596 Out << "<badref>";
Chris Lattner58185f22002-10-02 19:38:55 +00001597 }
Chris Lattner2447ef52003-11-20 00:09:43 +00001598
1599 if (BB->getParent() == 0)
Misha Brukmana6619a92004-06-21 21:53:56 +00001600 Out << "\t\t; Error: Block without parent!";
Chris Lattnerff834c02008-04-22 02:45:44 +00001601 else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
1602 // Output predecessors for the block...
1603 Out << "\t\t;";
1604 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
1605
1606 if (PI == PE) {
1607 Out << " No predecessors!";
1608 } else {
Dan Gohman81313fd2008-09-14 17:21:12 +00001609 Out << " preds = ";
Chris Lattnerff834c02008-04-22 02:45:44 +00001610 writeOperand(*PI, false);
1611 for (++PI; PI != PE; ++PI) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001612 Out << ", ";
Chris Lattner78e2e8b2006-12-06 06:24:27 +00001613 writeOperand(*PI, false);
Chris Lattner00211f12003-11-16 22:59:57 +00001614 }
Chris Lattner58185f22002-10-02 19:38:55 +00001615 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001616 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001617
Chris Lattnerff834c02008-04-22 02:45:44 +00001618 Out << "\n";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001619
Misha Brukmana6619a92004-06-21 21:53:56 +00001620 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001621
Chris Lattnerfee714f2001-09-07 16:36:04 +00001622 // Output all of the instructions in the basic block...
Chris Lattner113f4f42002-06-25 16:13:24 +00001623 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1624 printInstruction(*I);
Chris Lattner96cdd272004-03-08 18:51:45 +00001625
Misha Brukmana6619a92004-06-21 21:53:56 +00001626 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001627}
1628
Chris Lattner862e3382001-10-13 06:42:36 +00001629
Misha Brukmanc566ca362004-03-02 00:22:19 +00001630/// printInfoComment - Print a little comment after the instruction indicating
1631/// which slot it occupies.
1632///
Chris Lattner113f4f42002-06-25 16:13:24 +00001633void AssemblyWriter::printInfoComment(const Value &V) {
1634 if (V.getType() != Type::VoidTy) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001635 Out << "\t\t; <";
Chris Lattnere101c442009-02-28 21:26:53 +00001636 TypePrinter.print(V.getType(), Out);
Chris Lattner585297e82008-08-19 05:26:17 +00001637 Out << '>';
Chris Lattner862e3382001-10-13 06:42:36 +00001638
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001639 if (!V.hasName() && !isa<Instruction>(V)) {
Chris Lattner5e043322007-01-11 03:54:27 +00001640 int SlotNum;
1641 if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V))
1642 SlotNum = Machine.getGlobalSlot(GV);
1643 else
1644 SlotNum = Machine.getLocalSlot(&V);
Chris Lattner757ee0b2004-06-09 19:41:19 +00001645 if (SlotNum == -1)
Misha Brukmana6619a92004-06-21 21:53:56 +00001646 Out << ":<badref>";
Reid Spencer8beac692004-06-09 15:26:53 +00001647 else
Misha Brukmana6619a92004-06-21 21:53:56 +00001648 Out << ':' << SlotNum; // Print out the def slot taken.
Chris Lattner862e3382001-10-13 06:42:36 +00001649 }
Chris Lattnerb6c21db2005-02-01 01:24:01 +00001650 Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattner862e3382001-10-13 06:42:36 +00001651 }
1652}
1653
Reid Spencere7141c82006-08-28 01:02:49 +00001654// This member is called for each Instruction in a function..
Chris Lattner113f4f42002-06-25 16:13:24 +00001655void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001656 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner8339f7d2003-10-30 23:41:03 +00001657
Chris Lattner82ff9232008-08-23 22:52:27 +00001658 Out << '\t';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001659
1660 // Print out name if it exists...
Chris Lattner033935d2008-08-17 04:40:13 +00001661 if (I.hasName()) {
1662 PrintLLVMName(Out, &I);
1663 Out << " = ";
Chris Lattnerb25e5ea2008-08-29 17:19:30 +00001664 } else if (I.getType() != Type::VoidTy) {
1665 // Print out the def slot taken.
1666 int SlotNum = Machine.getLocalSlot(&I);
1667 if (SlotNum == -1)
1668 Out << "<badref> = ";
1669 else
1670 Out << '%' << SlotNum << " = ";
Chris Lattner033935d2008-08-17 04:40:13 +00001671 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001672
Chris Lattner06038452005-05-06 05:51:46 +00001673 // If this is a volatile load or store, print out the volatile marker.
Chris Lattner504f9242003-09-08 17:45:59 +00001674 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattner06038452005-05-06 05:51:46 +00001675 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001676 Out << "volatile ";
Chris Lattner06038452005-05-06 05:51:46 +00001677 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1678 // If this is a call, check if it's a tail call.
1679 Out << "tail ";
1680 }
Chris Lattner504f9242003-09-08 17:45:59 +00001681
Chris Lattner2f7c9632001-06-06 20:29:01 +00001682 // Print out the opcode...
Misha Brukmana6619a92004-06-21 21:53:56 +00001683 Out << I.getOpcodeName();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001684
Reid Spencer45e52392006-12-03 06:27:29 +00001685 // Print out the compare instruction predicates
Nate Begemand2195702008-05-12 19:01:56 +00001686 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattner82ff9232008-08-23 22:52:27 +00001687 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer45e52392006-12-03 06:27:29 +00001688
Chris Lattner2f7c9632001-06-06 20:29:01 +00001689 // Print out the type of the operands...
Chris Lattner113f4f42002-06-25 16:13:24 +00001690 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001691
1692 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifcab008f2009-02-09 15:45:06 +00001693 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1694 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman81313fd2008-09-14 17:21:12 +00001695 Out << ' ';
Gabor Greifcab008f2009-02-09 15:45:06 +00001696 writeOperand(BI.getCondition(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001697 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001698 writeOperand(BI.getSuccessor(0), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001699 Out << ", ";
Gabor Greifcab008f2009-02-09 15:45:06 +00001700 writeOperand(BI.getSuccessor(1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001701
Chris Lattner8d48df22002-04-13 18:34:38 +00001702 } else if (isa<SwitchInst>(I)) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001703 // Special case switch statement to get formatting nice and correct...
Dan Gohman81313fd2008-09-14 17:21:12 +00001704 Out << ' ';
Chris Lattner82ff9232008-08-23 22:52:27 +00001705 writeOperand(Operand , true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001706 Out << ", ";
Chris Lattner82ff9232008-08-23 22:52:27 +00001707 writeOperand(I.getOperand(1), true);
1708 Out << " [";
Chris Lattner2f7c9632001-06-06 20:29:01 +00001709
Chris Lattner113f4f42002-06-25 16:13:24 +00001710 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001711 Out << "\n\t\t";
Chris Lattner82ff9232008-08-23 22:52:27 +00001712 writeOperand(I.getOperand(op ), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001713 Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001714 writeOperand(I.getOperand(op+1), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001715 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001716 Out << "\n\t]";
Chris Lattnerda558102001-10-02 03:41:24 +00001717 } else if (isa<PHINode>(I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001718 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001719 TypePrinter.print(I.getType(), Out);
Misha Brukmana6619a92004-06-21 21:53:56 +00001720 Out << ' ';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001721
Chris Lattner113f4f42002-06-25 16:13:24 +00001722 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001723 if (op) Out << ", ";
Dan Gohman81313fd2008-09-14 17:21:12 +00001724 Out << "[ ";
1725 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukmana6619a92004-06-21 21:53:56 +00001726 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattner931ef3b2001-06-11 15:04:20 +00001727 }
Dan Gohmana76f0f72008-05-31 19:12:39 +00001728 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001729 Out << ' ';
Dan Gohmana76f0f72008-05-31 19:12:39 +00001730 writeOperand(I.getOperand(0), true);
1731 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1732 Out << ", " << *i;
1733 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001734 Out << ' ';
1735 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohmana76f0f72008-05-31 19:12:39 +00001736 writeOperand(I.getOperand(1), true);
1737 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1738 Out << ", " << *i;
Devang Patel59643e52008-02-23 00:35:18 +00001739 } else if (isa<ReturnInst>(I) && !Operand) {
1740 Out << " void";
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001741 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1742 // Print the calling convention being used.
1743 switch (CI->getCallingConv()) {
1744 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001745 case CallingConv::Fast: Out << " fastcc"; break;
1746 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerf5270372007-11-18 18:32:16 +00001747 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001748 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
1749 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1750 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1751 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001752 default: Out << " cc" << CI->getCallingConv(); break;
1753 }
1754
Reid Spencer1517de32007-04-09 06:10:42 +00001755 const PointerType *PTy = cast<PointerType>(Operand->getType());
1756 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1757 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001758 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner2f2d9472001-11-06 21:28:12 +00001759
Devang Patel221fe422008-09-29 20:49:50 +00001760 if (PAL.getRetAttributes() != Attribute::None)
1761 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1762
Chris Lattner463d6a52003-08-05 15:34:45 +00001763 // If possible, print out the short form of the call instruction. We can
Chris Lattner6915f8f2002-04-07 22:49:37 +00001764 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner463d6a52003-08-05 15:34:45 +00001765 // and if the return type is not a pointer to a function.
Chris Lattner2f2d9472001-11-06 21:28:12 +00001766 //
Dan Gohman81313fd2008-09-14 17:21:12 +00001767 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001768 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001769 (!isa<PointerType>(RetTy) ||
Chris Lattnerd9a36a62002-07-25 20:58:51 +00001770 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattnere101c442009-02-28 21:26:53 +00001771 TypePrinter.print(RetTy, Out);
Dan Gohman81313fd2008-09-14 17:21:12 +00001772 Out << ' ';
Chris Lattner2f2d9472001-11-06 21:28:12 +00001773 writeOperand(Operand, false);
1774 } else {
1775 writeOperand(Operand, true);
1776 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001777 Out << '(';
Reid Spencer8c4914c2006-12-31 05:24:50 +00001778 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1779 if (op > 1)
Dan Gohman81313fd2008-09-14 17:21:12 +00001780 Out << ", ";
Devang Patela05633e2008-09-26 22:53:05 +00001781 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001782 }
Dan Gohman81313fd2008-09-14 17:21:12 +00001783 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001784 if (PAL.getFnAttributes() != Attribute::None)
1785 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner113f4f42002-06-25 16:13:24 +00001786 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencer1517de32007-04-09 06:10:42 +00001787 const PointerType *PTy = cast<PointerType>(Operand->getType());
1788 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1789 const Type *RetTy = FTy->getReturnType();
Devang Patel4c758ea2008-09-25 21:00:45 +00001790 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner463d6a52003-08-05 15:34:45 +00001791
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001792 // Print the calling convention being used.
1793 switch (II->getCallingConv()) {
1794 case CallingConv::C: break; // default
Chris Lattner29d20852006-05-19 21:58:52 +00001795 case CallingConv::Fast: Out << " fastcc"; break;
1796 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman81313fd2008-09-14 17:21:12 +00001797 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1798 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001799 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1800 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1801 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerf7b6d312005-05-06 20:26:43 +00001802 default: Out << " cc" << II->getCallingConv(); break;
1803 }
1804
Devang Patel221fe422008-09-29 20:49:50 +00001805 if (PAL.getRetAttributes() != Attribute::None)
1806 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1807
Chris Lattner463d6a52003-08-05 15:34:45 +00001808 // If possible, print out the short form of the invoke instruction. We can
1809 // only do this if the first argument is a pointer to a nonvararg function,
1810 // and if the return type is not a pointer to a function.
1811 //
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001812 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001813 if (!FTy->isVarArg() &&
Misha Brukmanb1c93172005-04-21 23:48:37 +00001814 (!isa<PointerType>(RetTy) ||
Chris Lattner463d6a52003-08-05 15:34:45 +00001815 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattnere101c442009-02-28 21:26:53 +00001816 TypePrinter.print(RetTy, Out);
Dan Gohmanc7e00ba2008-10-15 18:02:08 +00001817 Out << ' ';
Chris Lattner463d6a52003-08-05 15:34:45 +00001818 writeOperand(Operand, false);
1819 } else {
1820 writeOperand(Operand, true);
1821 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001822 Out << '(';
Bill Wendling4bb96e92009-03-13 21:15:59 +00001823 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1824 if (op > 3)
Dan Gohman81313fd2008-09-14 17:21:12 +00001825 Out << ", ";
Bill Wendling4bb96e92009-03-13 21:15:59 +00001826 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattner862e3382001-10-13 06:42:36 +00001827 }
1828
Dan Gohman81313fd2008-09-14 17:21:12 +00001829 Out << ')';
Devang Patela05633e2008-09-26 22:53:05 +00001830 if (PAL.getFnAttributes() != Attribute::None)
1831 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1832
Dan Gohman81313fd2008-09-14 17:21:12 +00001833 Out << "\n\t\t\tto ";
Chris Lattner862e3382001-10-13 06:42:36 +00001834 writeOperand(II->getNormalDest(), true);
Dan Gohman81313fd2008-09-14 17:21:12 +00001835 Out << " unwind ";
Chris Lattnerfae8ab32004-02-08 21:44:31 +00001836 writeOperand(II->getUnwindDest(), true);
Chris Lattner862e3382001-10-13 06:42:36 +00001837
Chris Lattner113f4f42002-06-25 16:13:24 +00001838 } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001839 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001840 TypePrinter.print(AI->getType()->getElementType(), Out);
Chris Lattner8d48df22002-04-13 18:34:38 +00001841 if (AI->isArrayAllocation()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001842 Out << ", ";
Chris Lattner8d48df22002-04-13 18:34:38 +00001843 writeOperand(AI->getArraySize(), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001844 }
Nate Begeman848622f2005-11-05 09:21:28 +00001845 if (AI->getAlignment()) {
Chris Lattner7aeee3a2005-11-05 21:20:34 +00001846 Out << ", align " << AI->getAlignment();
Nate Begeman848622f2005-11-05 09:21:28 +00001847 }
Chris Lattner862e3382001-10-13 06:42:36 +00001848 } else if (isa<CastInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001849 if (Operand) {
1850 Out << ' ';
1851 writeOperand(Operand, true); // Work with broken code
1852 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001853 Out << " to ";
Chris Lattnere101c442009-02-28 21:26:53 +00001854 TypePrinter.print(I.getType(), Out);
Chris Lattner5b337482003-10-18 05:57:43 +00001855 } else if (isa<VAArgInst>(I)) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001856 if (Operand) {
1857 Out << ' ';
1858 writeOperand(Operand, true); // Work with broken code
1859 }
Misha Brukmana6619a92004-06-21 21:53:56 +00001860 Out << ", ";
Chris Lattnere101c442009-02-28 21:26:53 +00001861 TypePrinter.print(I.getType(), Out);
1862 } else if (Operand) { // Print the normal way.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001863
Misha Brukmanb1c93172005-04-21 23:48:37 +00001864 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner2f7c9632001-06-06 20:29:01 +00001865 // omit the type from all but the first operand. If the instruction has
1866 // different type operands (for example br), then they are all printed.
1867 bool PrintAllTypes = false;
1868 const Type *TheType = Operand->getType();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001869
Reid Spencer0cdd04f2007-02-02 13:54:55 +00001870 // Select, Store and ShuffleVector always print all types.
Devang Patelce556d92008-03-04 22:05:14 +00001871 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
1872 || isa<ReturnInst>(I)) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001873 PrintAllTypes = true;
1874 } else {
1875 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
1876 Operand = I.getOperand(i);
Nuno Lopes04fe2f02009-01-15 18:40:57 +00001877 // note that Operand shouldn't be null, but the test helps make dump()
1878 // more tolerant of malformed IR
Nuno Lopes0971e772009-01-14 17:51:41 +00001879 if (Operand && Operand->getType() != TheType) {
Chris Lattnerdeccfaf2003-04-16 20:20:02 +00001880 PrintAllTypes = true; // We have differing types! Print them all!
1881 break;
1882 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001883 }
1884 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001885
Chris Lattner7bfee412001-10-29 16:05:51 +00001886 if (!PrintAllTypes) {
Misha Brukmana6619a92004-06-21 21:53:56 +00001887 Out << ' ';
Chris Lattnere101c442009-02-28 21:26:53 +00001888 TypePrinter.print(TheType, Out);
Chris Lattner7bfee412001-10-29 16:05:51 +00001889 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001890
Dan Gohman81313fd2008-09-14 17:21:12 +00001891 Out << ' ';
Chris Lattner113f4f42002-06-25 16:13:24 +00001892 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman81313fd2008-09-14 17:21:12 +00001893 if (i) Out << ", ";
Chris Lattner113f4f42002-06-25 16:13:24 +00001894 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001895 }
1896 }
Christopher Lamb84485702007-04-22 19:24:39 +00001897
1898 // Print post operand alignment for load/store
1899 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
1900 Out << ", align " << cast<LoadInst>(I).getAlignment();
1901 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
1902 Out << ", align " << cast<StoreInst>(I).getAlignment();
1903 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001904
Chris Lattner862e3382001-10-13 06:42:36 +00001905 printInfoComment(I);
Chris Lattner82ff9232008-08-23 22:52:27 +00001906 Out << '\n';
Chris Lattner2f7c9632001-06-06 20:29:01 +00001907}
1908
1909
1910//===----------------------------------------------------------------------===//
1911// External Interface declarations
1912//===----------------------------------------------------------------------===//
1913
Chris Lattner8339f7d2003-10-30 23:41:03 +00001914void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00001915 raw_os_ostream OS(o);
1916 print(OS, AAW);
1917}
1918void Module::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
Chris Lattnere36fd8a2008-08-19 04:26:57 +00001919 SlotTracker SlotTable(this);
Chris Lattner0c19df42008-08-23 22:23:09 +00001920 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001921 W.write(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001922}
1923
Misha Brukmanb1c93172005-04-21 23:48:37 +00001924void Type::print(std::ostream &o) const {
Chris Lattner0c19df42008-08-23 22:23:09 +00001925 raw_os_ostream OS(o);
1926 print(OS);
1927}
1928
Chris Lattner2ee62d22009-02-28 21:11:05 +00001929void Type::print(raw_ostream &OS) const {
1930 if (this == 0) {
1931 OS << "<null Type>";
1932 return;
1933 }
Chris Lattner92c5c122009-02-28 23:20:19 +00001934 TypePrinting().print(this, OS);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001935}
1936
Chris Lattner0c19df42008-08-23 22:23:09 +00001937void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
1938 if (this == 0) {
1939 OS << "printing a <null> value\n";
1940 return;
1941 }
Chris Lattner0c19df42008-08-23 22:23:09 +00001942 if (const Instruction *I = dyn_cast<Instruction>(this)) {
1943 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
1944 SlotTracker SlotTable(F);
1945 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
1946 W.write(I);
1947 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
1948 SlotTracker SlotTable(BB->getParent());
1949 AssemblyWriter W(OS, SlotTable,
1950 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
1951 W.write(BB);
1952 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
1953 SlotTracker SlotTable(GV->getParent());
Dan Gohman6c7a4852009-04-20 16:10:33 +00001954 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattner0c19df42008-08-23 22:23:09 +00001955 W.write(GV);
Devang Patel5546b982009-07-01 20:59:15 +00001956 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Devang Patel983c6b12009-07-08 21:44:25 +00001957 SlotTracker SlotTable(N);
Devang Patel5546b982009-07-01 20:59:15 +00001958 TypePrinting TypePrinter;
Devang Patel983c6b12009-07-08 21:44:25 +00001959 SlotTable.initialize();
1960 WriteMDNodes(OS, TypePrinter, SlotTable);
Chris Lattner0c19df42008-08-23 22:23:09 +00001961 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattner92c5c122009-02-28 23:20:19 +00001962 TypePrinting TypePrinter;
Chris Lattnere101c442009-02-28 21:26:53 +00001963 TypePrinter.print(C->getType(), OS);
Chris Lattner2ee62d22009-02-28 21:11:05 +00001964 OS << ' ';
Chris Lattner0f578952009-02-28 20:25:14 +00001965 WriteConstantInt(OS, C, TypePrinter, 0);
Chris Lattner0c19df42008-08-23 22:23:09 +00001966 } else if (const Argument *A = dyn_cast<Argument>(this)) {
1967 WriteAsOperand(OS, this, true,
1968 A->getParent() ? A->getParent()->getParent() : 0);
1969 } else if (isa<InlineAsm>(this)) {
1970 WriteAsOperand(OS, this, true, 0);
1971 } else {
Dan Gohmanee1cd172008-12-03 21:37:21 +00001972 assert(0 && "Unknown value to print out!");
Chris Lattner0c19df42008-08-23 22:23:09 +00001973 }
1974}
1975
1976void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const {
1977 raw_os_ostream OS(O);
1978 print(OS, AAW);
Chris Lattnerc0b4c7b2002-04-08 22:03:40 +00001979}
1980
Chris Lattnerdab942552008-08-25 17:03:15 +00001981// Value::dump - allow easy printing of Values from the debugger.
Dan Gohman4f2fea12009-03-23 15:57:19 +00001982void Value::dump() const { print(errs()); errs() << '\n'; }
Reid Spencer52641832004-05-25 18:14:38 +00001983
Chris Lattnerdab942552008-08-25 17:03:15 +00001984// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner41a83d92008-10-01 20:16:19 +00001985// This one uses type names from the given context module
1986void Type::dump(const Module *Context) const {
1987 WriteTypeSymbolic(errs(), this, Context);
1988 errs() << '\n';
Chris Lattner41a83d92008-10-01 20:16:19 +00001989}
1990
Chris Lattner24025262009-02-28 21:05:51 +00001991// Type::dump - allow easy printing of Types from the debugger.
1992void Type::dump() const { dump(0); }
1993
Chris Lattnerdab942552008-08-25 17:03:15 +00001994// Module::dump() - Allow printing of Modules from the debugger.
Dan Gohman4f2fea12009-03-23 15:57:19 +00001995void Module::dump() const { print(errs(), 0); }