blob: 9a803a16628094a2482ad0cba72162836a75efd1 [file] [log] [blame]
Chris Lattner8da78af2002-04-07 22:31:46 +00001//===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Assembly/Writer.h
11//
Chris Lattner02b93992002-04-12 18:21:53 +000012// Note that these routines must be extremely tolerant of various errors in the
Chris Lattner8f77dae2003-05-08 02:44:12 +000013// LLVM code, because it can be used for debugging transformations.
Chris Lattner02b93992002-04-12 18:21:53 +000014//
Chris Lattner00950542001-06-06 20:29:01 +000015//===----------------------------------------------------------------------===//
16
Chris Lattner75cf7cf2002-04-08 22:03:40 +000017#include "llvm/Assembly/Writer.h"
Chris Lattnerf082b802002-07-23 18:07:49 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner95e5a2c2003-10-30 23:41:03 +000019#include "llvm/Assembly/AsmAnnotationWriter.h"
Chris Lattnerd5118982005-05-06 20:26:43 +000020#include "llvm/CallingConv.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000021#include "llvm/Constants.h"
Chris Lattner3eb59c02002-04-29 18:46:50 +000022#include "llvm/DerivedTypes.h"
Chris Lattner863517a2006-01-25 18:57:27 +000023#include "llvm/InlineAsm.h"
Vikram S. Adveb4dbb442002-07-14 23:14:45 +000024#include "llvm/Instruction.h"
Misha Brukman44336292004-07-29 16:53:53 +000025#include "llvm/Instructions.h"
Chris Lattnerf3523592009-10-27 17:08:31 +000026#include "llvm/LLVMContext.h"
Dan Gohman1224c382009-07-20 21:19:07 +000027#include "llvm/Operator.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000028#include "llvm/Metadata.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000029#include "llvm/Module.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000030#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000031#include "llvm/TypeSymbolTable.h"
Chris Lattner413fd232009-03-01 00:03:38 +000032#include "llvm/ADT/DenseSet.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/ADT/StringExtras.h"
34#include "llvm/ADT/STLExtras.h"
Bill Wendling8f487662006-11-28 02:09:03 +000035#include "llvm/Support/CFG.h"
Devang Patel2d5988d2009-09-30 20:16:54 +000036#include "llvm/Support/Dwarf.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000037#include "llvm/Support/ErrorHandling.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000038#include "llvm/Support/MathExtras.h"
Dan Gohman683e9222009-08-12 17:23:50 +000039#include "llvm/Support/FormattedStream.h"
Chris Lattner007377f2001-09-07 16:36:04 +000040#include <algorithm>
Reid Spencer4ad513c2007-05-22 19:27:35 +000041#include <cctype>
Devang Patel923078c2009-07-01 19:21:12 +000042#include <map>
Chris Lattner31f84992003-11-21 20:23:48 +000043using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000044
Reid Spenceredd5d9e2005-05-15 16:13:11 +000045// Make virtual table appear in this compilation unit.
46AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {}
47
Chris Lattner6ab910b2008-08-19 04:36:02 +000048//===----------------------------------------------------------------------===//
49// Helper Functions
50//===----------------------------------------------------------------------===//
51
52static const Module *getModuleFromVal(const Value *V) {
53 if (const Argument *MA = dyn_cast<Argument>(V))
54 return MA->getParent() ? MA->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000055
Chris Lattner6ab910b2008-08-19 04:36:02 +000056 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
57 return BB->getParent() ? BB->getParent()->getParent() : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +000058
Chris Lattner6ab910b2008-08-19 04:36:02 +000059 if (const Instruction *I = dyn_cast<Instruction>(V)) {
60 const Function *M = I->getParent() ? I->getParent()->getParent() : 0;
61 return M ? M->getParent() : 0;
62 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000063
Chris Lattner6ab910b2008-08-19 04:36:02 +000064 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
65 return GV->getParent();
66 return 0;
67}
68
Daniel Dunbare9da1332008-10-28 19:33:02 +000069// PrintEscapedString - Print each character of the specified string, escaping
70// it if it is not printable or if it is an escape char.
Dan Gohman683e9222009-08-12 17:23:50 +000071static void PrintEscapedString(const StringRef &Name,
Dan Gohman1220e102009-08-12 20:56:03 +000072 raw_ostream &Out) {
Daniel Dunbar03d76512009-07-25 23:55:21 +000073 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
74 unsigned char C = Name[i];
Nick Lewycky34a40862009-03-15 06:39:52 +000075 if (isprint(C) && C != '\\' && C != '"')
Daniel Dunbare9da1332008-10-28 19:33:02 +000076 Out << C;
77 else
78 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
79 }
80}
81
Chris Lattner6ab910b2008-08-19 04:36:02 +000082enum PrefixType {
83 GlobalPrefix,
84 LabelPrefix,
Daniel Dunbarcad35802008-10-14 23:28:09 +000085 LocalPrefix,
86 NoPrefix
Chris Lattner6ab910b2008-08-19 04:36:02 +000087};
88
89/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
90/// prefixed with % (if the string only contains simple characters) or is
91/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +000092static void PrintLLVMName(raw_ostream &OS, const StringRef &Name,
Daniel Dunbar03d76512009-07-25 23:55:21 +000093 PrefixType Prefix) {
94 assert(Name.data() && "Cannot get empty name!");
Chris Lattner6ab910b2008-08-19 04:36:02 +000095 switch (Prefix) {
Torok Edwinc23197a2009-07-14 16:55:14 +000096 default: llvm_unreachable("Bad prefix!");
Daniel Dunbarcad35802008-10-14 23:28:09 +000097 case NoPrefix: break;
Chris Lattner52b26de2008-08-19 05:16:28 +000098 case GlobalPrefix: OS << '@'; break;
99 case LabelPrefix: break;
100 case LocalPrefix: OS << '%'; break;
Nick Lewycky04234832009-03-19 06:31:22 +0000101 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000102
Chris Lattner6ab910b2008-08-19 04:36:02 +0000103 // Scan the name to see if it needs quotes first.
Daniel Dunbar03d76512009-07-25 23:55:21 +0000104 bool NeedsQuotes = isdigit(Name[0]);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000105 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000106 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
107 char C = Name[i];
Chris Lattner6ab910b2008-08-19 04:36:02 +0000108 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
109 NeedsQuotes = true;
110 break;
111 }
112 }
113 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000114
Chris Lattner6ab910b2008-08-19 04:36:02 +0000115 // If we didn't need any quotes, just write out the name in one blast.
116 if (!NeedsQuotes) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000117 OS << Name;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000118 return;
119 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000120
Chris Lattner6ab910b2008-08-19 04:36:02 +0000121 // Okay, we need quotes. Output the quotes and escape any scary characters as
122 // needed.
123 OS << '"';
Daniel Dunbar03d76512009-07-25 23:55:21 +0000124 PrintEscapedString(Name, OS);
Chris Lattner6ab910b2008-08-19 04:36:02 +0000125 OS << '"';
126}
127
128/// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either
129/// prefixed with % (if the string only contains simple characters) or is
130/// surrounded with ""'s (if it has special chars in it). Print it out.
Dan Gohman1220e102009-08-12 20:56:03 +0000131static void PrintLLVMName(raw_ostream &OS, const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000132 PrintLLVMName(OS, V->getName(),
Chris Lattner6ab910b2008-08-19 04:36:02 +0000133 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
134}
135
Chris Lattner9cc34462009-02-28 20:25:14 +0000136//===----------------------------------------------------------------------===//
137// TypePrinting Class: Type printing machinery
138//===----------------------------------------------------------------------===//
139
Chris Lattner87185e82009-02-28 23:03:55 +0000140static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) {
141 return *static_cast<DenseMap<const Type *, std::string>*>(M);
Chris Lattnerd8030a72009-02-28 22:34:45 +0000142}
143
144void TypePrinting::clear() {
145 getTypeNamesMap(TypeNames).clear();
146}
Chris Lattner9cc34462009-02-28 20:25:14 +0000147
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000148bool TypePrinting::hasTypeName(const Type *Ty) const {
149 return getTypeNamesMap(TypeNames).count(Ty);
150}
151
152void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
153 getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
154}
155
156
157TypePrinting::TypePrinting() {
Chris Lattner87185e82009-02-28 23:03:55 +0000158 TypeNames = new DenseMap<const Type *, std::string>();
Chris Lattner9cc34462009-02-28 20:25:14 +0000159}
160
Chris Lattnerd8030a72009-02-28 22:34:45 +0000161TypePrinting::~TypePrinting() {
162 delete &getTypeNamesMap(TypeNames);
163}
164
Chris Lattner534361e2009-02-28 20:49:40 +0000165/// CalcTypeName - Write the specified type to the specified raw_ostream, making
166/// use of type names or up references to shorten the type name where possible.
167void TypePrinting::CalcTypeName(const Type *Ty,
Chris Lattnerb840cac2009-02-28 20:34:19 +0000168 SmallVectorImpl<const Type *> &TypeStack,
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000169 raw_ostream &OS, bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000170 // Check to see if the type is named.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000171 if (!IgnoreTopLevelName) {
Nick Lewycky04234832009-03-19 06:31:22 +0000172 DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000173 DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
174 if (I != TM.end()) {
175 OS << I->second;
176 return;
177 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000178 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000179
Chris Lattner9cc34462009-02-28 20:25:14 +0000180 // Check to see if the Type is already on the stack...
181 unsigned Slot = 0, CurSize = TypeStack.size();
182 while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
Daniel Dunbara279bc32009-09-20 02:20:51 +0000183
Chris Lattner9cc34462009-02-28 20:25:14 +0000184 // This is another base case for the recursion. In this case, we know
185 // that we have looped back to a type that we have previously visited.
186 // Generate the appropriate upreference to handle this.
187 if (Slot < CurSize) {
Chris Lattner30794262009-02-28 21:27:31 +0000188 OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference
Chris Lattner9cc34462009-02-28 20:25:14 +0000189 return;
190 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000191
Chris Lattner9cc34462009-02-28 20:25:14 +0000192 TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
Daniel Dunbara279bc32009-09-20 02:20:51 +0000193
Chris Lattner9cc34462009-02-28 20:25:14 +0000194 switch (Ty->getTypeID()) {
Chris Lattner30794262009-02-28 21:27:31 +0000195 case Type::VoidTyID: OS << "void"; break;
196 case Type::FloatTyID: OS << "float"; break;
197 case Type::DoubleTyID: OS << "double"; break;
198 case Type::X86_FP80TyID: OS << "x86_fp80"; break;
199 case Type::FP128TyID: OS << "fp128"; break;
200 case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
201 case Type::LabelTyID: OS << "label"; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000202 case Type::MetadataTyID: OS << "metadata"; break;
Chris Lattner583ffd82009-02-28 21:18:43 +0000203 case Type::IntegerTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000204 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
Chris Lattner583ffd82009-02-28 21:18:43 +0000205 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000206
Chris Lattner36942d72009-02-28 20:35:42 +0000207 case Type::FunctionTyID: {
208 const FunctionType *FTy = cast<FunctionType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000209 CalcTypeName(FTy->getReturnType(), TypeStack, OS);
210 OS << " (";
Chris Lattner36942d72009-02-28 20:35:42 +0000211 for (FunctionType::param_iterator I = FTy->param_begin(),
212 E = FTy->param_end(); I != E; ++I) {
213 if (I != FTy->param_begin())
Chris Lattner30794262009-02-28 21:27:31 +0000214 OS << ", ";
215 CalcTypeName(*I, TypeStack, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000216 }
Chris Lattner36942d72009-02-28 20:35:42 +0000217 if (FTy->isVarArg()) {
Chris Lattner30794262009-02-28 21:27:31 +0000218 if (FTy->getNumParams()) OS << ", ";
219 OS << "...";
Chris Lattner9cc34462009-02-28 20:25:14 +0000220 }
Chris Lattner30794262009-02-28 21:27:31 +0000221 OS << ')';
Chris Lattner36942d72009-02-28 20:35:42 +0000222 break;
223 }
224 case Type::StructTyID: {
225 const StructType *STy = cast<StructType>(Ty);
226 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000227 OS << '<';
228 OS << "{ ";
Chris Lattner36942d72009-02-28 20:35:42 +0000229 for (StructType::element_iterator I = STy->element_begin(),
230 E = STy->element_end(); I != E; ++I) {
Chris Lattner30794262009-02-28 21:27:31 +0000231 CalcTypeName(*I, TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000232 if (next(I) != STy->element_end())
Chris Lattner30794262009-02-28 21:27:31 +0000233 OS << ',';
234 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +0000235 }
Chris Lattner30794262009-02-28 21:27:31 +0000236 OS << '}';
Chris Lattner36942d72009-02-28 20:35:42 +0000237 if (STy->isPacked())
Chris Lattner30794262009-02-28 21:27:31 +0000238 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000239 break;
240 }
241 case Type::PointerTyID: {
242 const PointerType *PTy = cast<PointerType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000243 CalcTypeName(PTy->getElementType(), TypeStack, OS);
Chris Lattner36942d72009-02-28 20:35:42 +0000244 if (unsigned AddressSpace = PTy->getAddressSpace())
Chris Lattner30794262009-02-28 21:27:31 +0000245 OS << " addrspace(" << AddressSpace << ')';
246 OS << '*';
Chris Lattner36942d72009-02-28 20:35:42 +0000247 break;
248 }
249 case Type::ArrayTyID: {
250 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000251 OS << '[' << ATy->getNumElements() << " x ";
252 CalcTypeName(ATy->getElementType(), TypeStack, OS);
253 OS << ']';
Chris Lattner36942d72009-02-28 20:35:42 +0000254 break;
255 }
256 case Type::VectorTyID: {
257 const VectorType *PTy = cast<VectorType>(Ty);
Chris Lattner30794262009-02-28 21:27:31 +0000258 OS << "<" << PTy->getNumElements() << " x ";
259 CalcTypeName(PTy->getElementType(), TypeStack, OS);
260 OS << '>';
Chris Lattner36942d72009-02-28 20:35:42 +0000261 break;
262 }
263 case Type::OpaqueTyID:
Chris Lattner30794262009-02-28 21:27:31 +0000264 OS << "opaque";
Chris Lattner36942d72009-02-28 20:35:42 +0000265 break;
266 default:
Chris Lattner30794262009-02-28 21:27:31 +0000267 OS << "<unrecognized-type>";
Chris Lattner36942d72009-02-28 20:35:42 +0000268 break;
Chris Lattner9cc34462009-02-28 20:25:14 +0000269 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000270
Chris Lattner534361e2009-02-28 20:49:40 +0000271 TypeStack.pop_back(); // Remove self from stack.
Chris Lattner9cc34462009-02-28 20:25:14 +0000272}
273
274/// printTypeInt - The internal guts of printing out a type that has a
275/// potentially named portion.
276///
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000277void TypePrinting::print(const Type *Ty, raw_ostream &OS,
278 bool IgnoreTopLevelName) {
Chris Lattner9cc34462009-02-28 20:25:14 +0000279 // Check to see if the type is named.
Chris Lattner87185e82009-02-28 23:03:55 +0000280 DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000281 if (!IgnoreTopLevelName) {
282 DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
283 if (I != TM.end()) {
284 OS << I->second;
285 return;
286 }
Chris Lattner9cc34462009-02-28 20:25:14 +0000287 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000288
Chris Lattner9cc34462009-02-28 20:25:14 +0000289 // Otherwise we have a type that has not been named but is a derived type.
290 // Carefully recurse the type hierarchy to print out any contained symbolic
291 // names.
Chris Lattnerb840cac2009-02-28 20:34:19 +0000292 SmallVector<const Type *, 16> TypeStack;
Chris Lattner9cc34462009-02-28 20:25:14 +0000293 std::string TypeName;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000294
Chris Lattner534361e2009-02-28 20:49:40 +0000295 raw_string_ostream TypeOS(TypeName);
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000296 CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
Chris Lattner534361e2009-02-28 20:49:40 +0000297 OS << TypeOS.str();
298
299 // Cache type name for later use.
Chris Lattnerd2b6cb02009-03-01 01:16:21 +0000300 if (!IgnoreTopLevelName)
301 TM.insert(std::make_pair(Ty, TypeOS.str()));
Chris Lattner9cc34462009-02-28 20:25:14 +0000302}
303
Chris Lattner413fd232009-03-01 00:03:38 +0000304namespace {
305 class TypeFinder {
306 // To avoid walking constant expressions multiple times and other IR
307 // objects, we keep several helper maps.
308 DenseSet<const Value*> VisitedConstants;
309 DenseSet<const Type*> VisitedTypes;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000310
Chris Lattner413fd232009-03-01 00:03:38 +0000311 TypePrinting &TP;
312 std::vector<const Type*> &NumberedTypes;
313 public:
314 TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
315 : TP(tp), NumberedTypes(numberedTypes) {}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000316
Chris Lattner413fd232009-03-01 00:03:38 +0000317 void Run(const Module &M) {
Chris Lattner88485862009-03-01 00:32:33 +0000318 // Get types from the type symbol table. This gets opaque types referened
319 // only through derived named types.
320 const TypeSymbolTable &ST = M.getTypeSymbolTable();
321 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
322 TI != E; ++TI)
323 IncorporateType(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000324
Chris Lattner413fd232009-03-01 00:03:38 +0000325 // Get types from global variables.
326 for (Module::const_global_iterator I = M.global_begin(),
327 E = M.global_end(); I != E; ++I) {
328 IncorporateType(I->getType());
329 if (I->hasInitializer())
330 IncorporateValue(I->getInitializer());
331 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000332
Chris Lattner413fd232009-03-01 00:03:38 +0000333 // Get types from aliases.
334 for (Module::const_alias_iterator I = M.alias_begin(),
335 E = M.alias_end(); I != E; ++I) {
336 IncorporateType(I->getType());
337 IncorporateValue(I->getAliasee());
338 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000339
Chris Lattner413fd232009-03-01 00:03:38 +0000340 // Get types from functions.
341 for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
342 IncorporateType(FI->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000343
Chris Lattner413fd232009-03-01 00:03:38 +0000344 for (Function::const_iterator BB = FI->begin(), E = FI->end();
345 BB != E;++BB)
346 for (BasicBlock::const_iterator II = BB->begin(),
347 E = BB->end(); II != E; ++II) {
348 const Instruction &I = *II;
349 // Incorporate the type of the instruction and all its operands.
350 IncorporateType(I.getType());
351 for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
352 OI != OE; ++OI)
353 IncorporateValue(*OI);
354 }
355 }
356 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000357
Chris Lattner413fd232009-03-01 00:03:38 +0000358 private:
359 void IncorporateType(const Type *Ty) {
360 // Check to see if we're already visited this type.
Chris Lattner88485862009-03-01 00:32:33 +0000361 if (!VisitedTypes.insert(Ty).second)
Chris Lattner413fd232009-03-01 00:03:38 +0000362 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000363
Chris Lattner413fd232009-03-01 00:03:38 +0000364 // If this is a structure or opaque type, add a name for the type.
Nick Lewycky21cc4462009-04-04 07:22:01 +0000365 if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
366 || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
Chris Lattner413fd232009-03-01 00:03:38 +0000367 TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
368 NumberedTypes.push_back(Ty);
369 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000370
Chris Lattner413fd232009-03-01 00:03:38 +0000371 // Recursively walk all contained types.
372 for (Type::subtype_iterator I = Ty->subtype_begin(),
373 E = Ty->subtype_end(); I != E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000374 IncorporateType(*I);
Chris Lattner413fd232009-03-01 00:03:38 +0000375 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000376
Chris Lattner413fd232009-03-01 00:03:38 +0000377 /// IncorporateValue - This method is used to walk operand lists finding
378 /// types hiding in constant expressions and other operands that won't be
379 /// walked in other ways. GlobalValues, basic blocks, instructions, and
380 /// inst operands are all explicitly enumerated.
381 void IncorporateValue(const Value *V) {
382 if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000383
Chris Lattner413fd232009-03-01 00:03:38 +0000384 // Already visited?
385 if (!VisitedConstants.insert(V).second)
386 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000387
Chris Lattner413fd232009-03-01 00:03:38 +0000388 // Check this type.
389 IncorporateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000390
Chris Lattner413fd232009-03-01 00:03:38 +0000391 // Look in operands for types.
392 const Constant *C = cast<Constant>(V);
393 for (Constant::const_op_iterator I = C->op_begin(),
394 E = C->op_end(); I != E;++I)
395 IncorporateValue(*I);
396 }
397 };
398} // end anonymous namespace
399
400
401/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
402/// the specified module to the TypePrinter and all numbered types to it and the
403/// NumberedTypes table.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000404static void AddModuleTypesToPrinter(TypePrinting &TP,
Chris Lattner413fd232009-03-01 00:03:38 +0000405 std::vector<const Type*> &NumberedTypes,
406 const Module *M) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000407 if (M == 0) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000408
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000409 // If the module has a symbol table, take all global types and stuff their
410 // names into the TypeNames map.
411 const TypeSymbolTable &ST = M->getTypeSymbolTable();
412 for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
413 TI != E; ++TI) {
414 const Type *Ty = cast<Type>(TI->second);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000415
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000416 // As a heuristic, don't insert pointer to primitive types, because
417 // they are used too often to have a single useful name.
418 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
419 const Type *PETy = PTy->getElementType();
420 if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
421 !isa<OpaqueType>(PETy))
422 continue;
423 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000424
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000425 // Likewise don't insert primitives either.
426 if (Ty->isInteger() || Ty->isPrimitiveType())
427 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000428
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000429 // Get the name as a string and insert it into TypeNames.
430 std::string NameStr;
Dan Gohman683e9222009-08-12 17:23:50 +0000431 raw_string_ostream NameROS(NameStr);
432 formatted_raw_ostream NameOS(NameROS);
Daniel Dunbar03d76512009-07-25 23:55:21 +0000433 PrintLLVMName(NameOS, TI->first, LocalPrefix);
Dan Gohman683e9222009-08-12 17:23:50 +0000434 NameOS.flush();
435 TP.addTypeName(Ty, NameStr);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000436 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000437
Chris Lattner413fd232009-03-01 00:03:38 +0000438 // Walk the entire module to find references to unnamed structure and opaque
439 // types. This is required for correctness by opaque types (because multiple
440 // uses of an unnamed opaque type needs to be referred to by the same ID) and
441 // it shrinks complex recursive structure types substantially in some cases.
442 TypeFinder(TP, NumberedTypes).Run(*M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000443}
444
Chris Lattner9cc34462009-02-28 20:25:14 +0000445
Chris Lattner9cc34462009-02-28 20:25:14 +0000446/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
447/// type, iff there is an entry in the modules symbol table for the specified
Chris Lattnerc2871372009-02-28 21:05:51 +0000448/// type or one of it's component types.
Chris Lattner9cc34462009-02-28 20:25:14 +0000449///
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000450void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
451 TypePrinting Printer;
Chris Lattner413fd232009-03-01 00:03:38 +0000452 std::vector<const Type*> NumberedTypes;
453 AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Chris Lattnere9fa33e2009-02-28 23:20:19 +0000454 Printer.print(Ty, OS);
Chris Lattner9cc34462009-02-28 20:25:14 +0000455}
456
Chris Lattner6ab910b2008-08-19 04:36:02 +0000457//===----------------------------------------------------------------------===//
458// SlotTracker Class: Enumerate slot numbers for unnamed values
459//===----------------------------------------------------------------------===//
460
Chris Lattnerb64871a2008-08-19 04:28:07 +0000461namespace {
462
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000463/// This class provides computation of slot numbers for LLVM Assembly writing.
Chris Lattner45d4c732008-08-17 04:17:45 +0000464///
Chris Lattner0d9574a2008-08-19 04:26:57 +0000465class SlotTracker {
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000466public:
Devang Patel320671d2009-07-08 21:44:25 +0000467 /// ValueMap - A mapping of Values to slot numbers.
Chris Lattner92255072008-08-17 17:25:25 +0000468 typedef DenseMap<const Value*, unsigned> ValueMap;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000469
470private:
Devang Patel320671d2009-07-08 21:44:25 +0000471 /// TheModule - The module for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000472 const Module* TheModule;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000473
Devang Patel320671d2009-07-08 21:44:25 +0000474 /// TheFunction - The function for which we are holding slot numbers.
Chris Lattner45d4c732008-08-17 04:17:45 +0000475 const Function* TheFunction;
476 bool FunctionProcessed;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000477
Devang Patel320671d2009-07-08 21:44:25 +0000478 /// TheMDNode - The MDNode for which we are holding slot numbers.
479 const MDNode *TheMDNode;
480
Devang Patelc29d5b32009-07-30 00:02:57 +0000481 /// TheNamedMDNode - The MDNode for which we are holding slot numbers.
482 const NamedMDNode *TheNamedMDNode;
483
Devang Patel320671d2009-07-08 21:44:25 +0000484 /// mMap - The TypePlanes map for the module level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000485 ValueMap mMap;
486 unsigned mNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000487
Devang Patel320671d2009-07-08 21:44:25 +0000488 /// fMap - The TypePlanes map for the function level data.
Chris Lattner45d4c732008-08-17 04:17:45 +0000489 ValueMap fMap;
490 unsigned fNext;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000491
Devang Patel320671d2009-07-08 21:44:25 +0000492 /// mdnMap - Map for MDNodes.
493 ValueMap mdnMap;
494 unsigned mdnNext;
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000495public:
Chris Lattner45d4c732008-08-17 04:17:45 +0000496 /// Construct from a module
Chris Lattner0d9574a2008-08-19 04:26:57 +0000497 explicit SlotTracker(const Module *M);
Chris Lattner45d4c732008-08-17 04:17:45 +0000498 /// Construct from a function, starting out in incorp state.
Chris Lattner0d9574a2008-08-19 04:26:57 +0000499 explicit SlotTracker(const Function *F);
Devang Patel320671d2009-07-08 21:44:25 +0000500 /// Construct from a mdnode.
501 explicit SlotTracker(const MDNode *N);
Devang Patelc29d5b32009-07-30 00:02:57 +0000502 /// Construct from a named mdnode.
503 explicit SlotTracker(const NamedMDNode *N);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000504
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000505 /// Return the slot number of the specified value in it's type
Chris Lattner0d9574a2008-08-19 04:26:57 +0000506 /// plane. If something is not in the SlotTracker, return -1.
Chris Lattner22379bc2007-01-11 03:54:27 +0000507 int getLocalSlot(const Value *V);
508 int getGlobalSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000509 int getMetadataSlot(const MDNode *N);
Reid Spencerfc621e22004-06-09 15:26:53 +0000510
Misha Brukmanfd939082005-04-21 23:48:37 +0000511 /// If you'd like to deal with a function instead of just a module, use
Chris Lattner0d9574a2008-08-19 04:26:57 +0000512 /// this method to get its data into the SlotTracker.
Misha Brukmanfd939082005-04-21 23:48:37 +0000513 void incorporateFunction(const Function *F) {
514 TheFunction = F;
Reid Spencer28531c72004-08-16 07:46:33 +0000515 FunctionProcessed = false;
516 }
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000517
Misha Brukmanfd939082005-04-21 23:48:37 +0000518 /// After calling incorporateFunction, use this method to remove the
Chris Lattner0d9574a2008-08-19 04:26:57 +0000519 /// most recently incorporated function from the SlotTracker. This
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000520 /// will reset the state of the machine back to just the module contents.
521 void purgeFunction();
522
Devang Patel320671d2009-07-08 21:44:25 +0000523 /// MDNode map iterators.
524 ValueMap::iterator mdnBegin() { return mdnMap.begin(); }
525 ValueMap::iterator mdnEnd() { return mdnMap.end(); }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +0000526 unsigned mdnSize() const { return mdnMap.size(); }
527 bool mdnEmpty() const { return mdnMap.empty(); }
Devang Patel320671d2009-07-08 21:44:25 +0000528
Reid Spencerb03de0c2004-05-26 21:56:09 +0000529 /// This function does the actual initialization.
530 inline void initialize();
531
Devang Patel320671d2009-07-08 21:44:25 +0000532 // Implementation Details
533private:
Chris Lattner9446bbe2007-01-09 07:55:49 +0000534 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
535 void CreateModuleSlot(const GlobalValue *V);
Devang Patel320671d2009-07-08 21:44:25 +0000536
537 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
538 void CreateMetadataSlot(const MDNode *N);
539
Chris Lattner9446bbe2007-01-09 07:55:49 +0000540 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
541 void CreateFunctionSlot(const Value *V);
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000542
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000543 /// Add all of the module level global variables (and their initializers)
544 /// and function declarations, but not the contents of those functions.
545 void processModule();
546
Devang Patel320671d2009-07-08 21:44:25 +0000547 /// Add all of the functions arguments, basic blocks, and instructions.
Reid Spencerb03de0c2004-05-26 21:56:09 +0000548 void processFunction();
549
Devang Patel320671d2009-07-08 21:44:25 +0000550 /// Add all MDNode operands.
551 void processMDNode();
552
Devang Patelc29d5b32009-07-30 00:02:57 +0000553 /// Add all MDNode operands.
554 void processNamedMDNode();
555
Chris Lattner0d9574a2008-08-19 04:26:57 +0000556 SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT
557 void operator=(const SlotTracker &); // DO NOT IMPLEMENT
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000558};
559
Chris Lattnerb64871a2008-08-19 04:28:07 +0000560} // end anonymous namespace
Reid Spencer0d1b77e2004-05-26 07:18:52 +0000561
Chris Lattner6ab910b2008-08-19 04:36:02 +0000562
563static SlotTracker *createSlotTracker(const Value *V) {
564 if (const Argument *FA = dyn_cast<Argument>(V))
565 return new SlotTracker(FA->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000566
Chris Lattner6ab910b2008-08-19 04:36:02 +0000567 if (const Instruction *I = dyn_cast<Instruction>(V))
568 return new SlotTracker(I->getParent()->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000569
Chris Lattner6ab910b2008-08-19 04:36:02 +0000570 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
571 return new SlotTracker(BB->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000572
Chris Lattner6ab910b2008-08-19 04:36:02 +0000573 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
574 return new SlotTracker(GV->getParent());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000575
Chris Lattner6ab910b2008-08-19 04:36:02 +0000576 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Daniel Dunbara279bc32009-09-20 02:20:51 +0000577 return new SlotTracker(GA->getParent());
578
Chris Lattner6ab910b2008-08-19 04:36:02 +0000579 if (const Function *Func = dyn_cast<Function>(V))
580 return new SlotTracker(Func);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000581
Chris Lattner6ab910b2008-08-19 04:36:02 +0000582 return 0;
583}
584
585#if 0
Dan Gohmanfd87a542009-07-25 01:43:01 +0000586#define ST_DEBUG(X) errs() << X
Chris Lattner6ab910b2008-08-19 04:36:02 +0000587#else
Chris Lattner24233032008-08-19 04:47:09 +0000588#define ST_DEBUG(X)
Chris Lattner6ab910b2008-08-19 04:36:02 +0000589#endif
590
591// Module level constructor. Causes the contents of the Module (sans functions)
592// to be added to the slot table.
593SlotTracker::SlotTracker(const Module *M)
Devang Patel320671d2009-07-08 21:44:25 +0000594 : TheModule(M), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
Devang Patelc29d5b32009-07-30 00:02:57 +0000595 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000596}
597
598// Function level constructor. Causes the contents of the Module and the one
599// function provided to be added to the slot table.
600SlotTracker::SlotTracker(const Function *F)
Chris Lattnercfb5a202008-08-19 05:06:27 +0000601 : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false),
Devang Patelc29d5b32009-07-30 00:02:57 +0000602 TheMDNode(0), TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
Devang Patel320671d2009-07-08 21:44:25 +0000603}
604
605// Constructor to handle single MDNode.
606SlotTracker::SlotTracker(const MDNode *C)
607 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(C),
Devang Patelc29d5b32009-07-30 00:02:57 +0000608 TheNamedMDNode(0), mNext(0), fNext(0), mdnNext(0) {
609}
610
611// Constructor to handle single NamedMDNode.
612SlotTracker::SlotTracker(const NamedMDNode *N)
613 : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(0),
614 TheNamedMDNode(N), mNext(0), fNext(0), mdnNext(0) {
Chris Lattner6ab910b2008-08-19 04:36:02 +0000615}
616
617inline void SlotTracker::initialize() {
618 if (TheModule) {
619 processModule();
620 TheModule = 0; ///< Prevent re-processing next time we're called.
621 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000622
Chris Lattner6ab910b2008-08-19 04:36:02 +0000623 if (TheFunction && !FunctionProcessed)
624 processFunction();
Devang Patel320671d2009-07-08 21:44:25 +0000625
626 if (TheMDNode)
627 processMDNode();
Devang Patelc29d5b32009-07-30 00:02:57 +0000628
629 if (TheNamedMDNode)
630 processNamedMDNode();
Chris Lattner6ab910b2008-08-19 04:36:02 +0000631}
632
633// Iterate through all the global variables, functions, and global
634// variable initializers and create slots for them.
635void SlotTracker::processModule() {
Chris Lattner24233032008-08-19 04:47:09 +0000636 ST_DEBUG("begin processModule!\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000637
Chris Lattner6ab910b2008-08-19 04:36:02 +0000638 // Add all of the unnamed global variables to the value table.
639 for (Module::const_global_iterator I = TheModule->global_begin(),
Devang Patel320671d2009-07-08 21:44:25 +0000640 E = TheModule->global_end(); I != E; ++I) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000641 if (!I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000642 CreateModuleSlot(I);
Devang Patel320671d2009-07-08 21:44:25 +0000643 if (I->hasInitializer()) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000644 if (MDNode *N = dyn_cast<MDNode>(I->getInitializer()))
Devang Patel320671d2009-07-08 21:44:25 +0000645 CreateMetadataSlot(N);
646 }
647 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000648
Devang Patel37c4a2d2009-07-29 22:04:47 +0000649 // Add metadata used by named metadata.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000650 for (Module::const_named_metadata_iterator
Devang Patel37c4a2d2009-07-29 22:04:47 +0000651 I = TheModule->named_metadata_begin(),
652 E = TheModule->named_metadata_end(); I != E; ++I) {
653 const NamedMDNode *NMD = I;
654 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
655 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
Devang Patele8861b82009-07-30 01:02:04 +0000656 if (MD)
657 CreateMetadataSlot(MD);
Devang Patel37c4a2d2009-07-29 22:04:47 +0000658 }
659 }
660
Chris Lattner6ab910b2008-08-19 04:36:02 +0000661 // Add all the unnamed functions to the table.
662 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
663 I != E; ++I)
664 if (!I->hasName())
665 CreateModuleSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000666
Chris Lattner24233032008-08-19 04:47:09 +0000667 ST_DEBUG("end processModule!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000668}
669
Chris Lattner6ab910b2008-08-19 04:36:02 +0000670// Process the arguments, basic blocks, and instructions of a function.
671void SlotTracker::processFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000672 ST_DEBUG("begin processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000673 fNext = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000674
Chris Lattner6ab910b2008-08-19 04:36:02 +0000675 // Add all the function arguments with no names.
676 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
677 AE = TheFunction->arg_end(); AI != AE; ++AI)
678 if (!AI->hasName())
679 CreateFunctionSlot(AI);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000680
Chris Lattner24233032008-08-19 04:47:09 +0000681 ST_DEBUG("Inserting Instructions:\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000682
Devang Patele30e6782009-09-28 21:41:20 +0000683 MetadataContext &TheMetadata = TheFunction->getContext().getMetadata();
Devang Patelf61b2372009-10-22 18:55:16 +0000684 typedef SmallVector<std::pair<unsigned, TrackingVH<MDNode> >, 2> MDMapTy;
685 MDMapTy MDs;
Devang Patel43215782009-09-16 20:21:17 +0000686
Chris Lattner6ab910b2008-08-19 04:36:02 +0000687 // Add all of the basic blocks and instructions with no names.
688 for (Function::const_iterator BB = TheFunction->begin(),
689 E = TheFunction->end(); BB != E; ++BB) {
690 if (!BB->hasName())
691 CreateFunctionSlot(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000692 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E;
Devang Patel320671d2009-07-08 21:44:25 +0000693 ++I) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000694 if (I->getType() != Type::getVoidTy(TheFunction->getContext()) &&
695 !I->hasName())
Chris Lattner6ab910b2008-08-19 04:36:02 +0000696 CreateFunctionSlot(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000697 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dan Gohman48cc73d2009-08-17 15:28:08 +0000698 if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i)))
Devang Patel320671d2009-07-08 21:44:25 +0000699 CreateMetadataSlot(N);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000700
Devang Patel43215782009-09-16 20:21:17 +0000701 // Process metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +0000702 MDs.clear();
703 TheMetadata.getMDs(I, MDs);
704 for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME;
705 ++MI)
706 CreateMetadataSlot(MI->second);
Devang Patel320671d2009-07-08 21:44:25 +0000707 }
Chris Lattner6ab910b2008-08-19 04:36:02 +0000708 }
Devang Patel43215782009-09-16 20:21:17 +0000709
Chris Lattner6ab910b2008-08-19 04:36:02 +0000710 FunctionProcessed = true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000711
Chris Lattner24233032008-08-19 04:47:09 +0000712 ST_DEBUG("end processFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000713}
714
Devang Patel320671d2009-07-08 21:44:25 +0000715/// processMDNode - Process TheMDNode.
716void SlotTracker::processMDNode() {
717 ST_DEBUG("begin processMDNode!\n");
718 mdnNext = 0;
719 CreateMetadataSlot(TheMDNode);
720 TheMDNode = 0;
721 ST_DEBUG("end processMDNode!\n");
722}
723
Devang Patelc29d5b32009-07-30 00:02:57 +0000724/// processNamedMDNode - Process TheNamedMDNode.
725void SlotTracker::processNamedMDNode() {
726 ST_DEBUG("begin processNamedMDNode!\n");
727 mdnNext = 0;
728 for (unsigned i = 0, e = TheNamedMDNode->getNumElements(); i != e; ++i) {
729 MDNode *MD = dyn_cast_or_null<MDNode>(TheNamedMDNode->getElement(i));
730 if (MD)
731 CreateMetadataSlot(MD);
732 }
733 TheNamedMDNode = 0;
734 ST_DEBUG("end processNamedMDNode!\n");
735}
736
Chris Lattner6ab910b2008-08-19 04:36:02 +0000737/// Clean up after incorporating a function. This is the only way to get out of
738/// the function incorporation state that affects get*Slot/Create*Slot. Function
739/// incorporation state is indicated by TheFunction != 0.
740void SlotTracker::purgeFunction() {
Chris Lattner24233032008-08-19 04:47:09 +0000741 ST_DEBUG("begin purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000742 fMap.clear(); // Simply discard the function level map
743 TheFunction = 0;
744 FunctionProcessed = false;
Chris Lattner24233032008-08-19 04:47:09 +0000745 ST_DEBUG("end purgeFunction!\n");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000746}
747
748/// getGlobalSlot - Get the slot number of a global value.
749int SlotTracker::getGlobalSlot(const GlobalValue *V) {
750 // Check for uninitialized state and do lazy initialization.
751 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000752
Chris Lattner6ab910b2008-08-19 04:36:02 +0000753 // Find the type plane in the module map
754 ValueMap::iterator MI = mMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000755 return MI == mMap.end() ? -1 : (int)MI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000756}
757
Devang Patel320671d2009-07-08 21:44:25 +0000758/// getGlobalSlot - Get the slot number of a MDNode.
759int SlotTracker::getMetadataSlot(const MDNode *N) {
760 // Check for uninitialized state and do lazy initialization.
761 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000762
Devang Patel320671d2009-07-08 21:44:25 +0000763 // Find the type plane in the module map
764 ValueMap::iterator MI = mdnMap.find(N);
765 return MI == mdnMap.end() ? -1 : (int)MI->second;
766}
767
Chris Lattner6ab910b2008-08-19 04:36:02 +0000768
769/// getLocalSlot - Get the slot number for a value that is local to a function.
770int SlotTracker::getLocalSlot(const Value *V) {
771 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000772
Chris Lattner6ab910b2008-08-19 04:36:02 +0000773 // Check for uninitialized state and do lazy initialization.
774 initialize();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000775
Chris Lattner6ab910b2008-08-19 04:36:02 +0000776 ValueMap::iterator FI = fMap.find(V);
Dan Gohmanaeaf2452008-10-01 19:58:59 +0000777 return FI == fMap.end() ? -1 : (int)FI->second;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000778}
779
780
781/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
782void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
783 assert(V && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000784 assert(V->getType() != Type::getVoidTy(V->getContext()) &&
Owen Anderson1d0be152009-08-13 21:58:54 +0000785 "Doesn't need a slot!");
Chris Lattner6ab910b2008-08-19 04:36:02 +0000786 assert(!V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000787
Chris Lattner6ab910b2008-08-19 04:36:02 +0000788 unsigned DestSlot = mNext++;
789 mMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000790
Chris Lattner24233032008-08-19 04:47:09 +0000791 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000792 DestSlot << " [");
793 // G = Global, F = Function, A = Alias, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000794 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
Chris Lattner6ab910b2008-08-19 04:36:02 +0000795 (isa<Function>(V) ? 'F' :
796 (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n");
797}
798
Chris Lattner6ab910b2008-08-19 04:36:02 +0000799/// CreateSlot - Create a new slot for the specified value if it has no name.
800void SlotTracker::CreateFunctionSlot(const Value *V) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000801 assert(V->getType() != Type::getVoidTy(TheFunction->getContext()) &&
Owen Anderson1d0be152009-08-13 21:58:54 +0000802 !V->hasName() && "Doesn't need a slot!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000803
Chris Lattner6ab910b2008-08-19 04:36:02 +0000804 unsigned DestSlot = fNext++;
805 fMap[V] = DestSlot;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000806
Chris Lattner6ab910b2008-08-19 04:36:02 +0000807 // G = Global, F = Function, o = other
Chris Lattner24233032008-08-19 04:47:09 +0000808 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
Chris Lattner6ab910b2008-08-19 04:36:02 +0000809 DestSlot << " [o]\n");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000810}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000811
Devang Patel320671d2009-07-08 21:44:25 +0000812/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
813void SlotTracker::CreateMetadataSlot(const MDNode *N) {
814 assert(N && "Can't insert a null Value into SlotTracker!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000815
Devang Patel320671d2009-07-08 21:44:25 +0000816 ValueMap::iterator I = mdnMap.find(N);
817 if (I != mdnMap.end())
818 return;
Chris Lattner6ab910b2008-08-19 04:36:02 +0000819
Devang Patel320671d2009-07-08 21:44:25 +0000820 unsigned DestSlot = mdnNext++;
821 mdnMap[N] = DestSlot;
822
Devang Patel028fa772009-10-21 21:25:09 +0000823 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
824 const Value *TV = N->getElement(i);
Devang Patel320671d2009-07-08 21:44:25 +0000825 if (TV)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000826 if (const MDNode *N2 = dyn_cast<MDNode>(TV))
Devang Patel320671d2009-07-08 21:44:25 +0000827 CreateMetadataSlot(N2);
828 }
829}
Chris Lattner6ab910b2008-08-19 04:36:02 +0000830
831//===----------------------------------------------------------------------===//
832// AsmWriter Implementation
833//===----------------------------------------------------------------------===//
Chris Lattnerf082b802002-07-23 18:07:49 +0000834
Dan Gohman1220e102009-08-12 20:56:03 +0000835static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +0000836 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +0000837 SlotTracker *Machine);
Reid Spencer0e25e1c2004-07-04 11:50:43 +0000838
Chris Lattnerc97536e2008-08-17 04:40:13 +0000839
Chris Lattner207b5bc2001-10-29 16:37:48 +0000840
Chris Lattner82c4bc72006-12-06 06:40:49 +0000841static const char *getPredicateText(unsigned predicate) {
Reid Spencer81dfeb32006-12-04 05:19:18 +0000842 const char * pred = "unknown";
843 switch (predicate) {
844 case FCmpInst::FCMP_FALSE: pred = "false"; break;
845 case FCmpInst::FCMP_OEQ: pred = "oeq"; break;
846 case FCmpInst::FCMP_OGT: pred = "ogt"; break;
847 case FCmpInst::FCMP_OGE: pred = "oge"; break;
848 case FCmpInst::FCMP_OLT: pred = "olt"; break;
849 case FCmpInst::FCMP_OLE: pred = "ole"; break;
850 case FCmpInst::FCMP_ONE: pred = "one"; break;
851 case FCmpInst::FCMP_ORD: pred = "ord"; break;
852 case FCmpInst::FCMP_UNO: pred = "uno"; break;
853 case FCmpInst::FCMP_UEQ: pred = "ueq"; break;
854 case FCmpInst::FCMP_UGT: pred = "ugt"; break;
855 case FCmpInst::FCMP_UGE: pred = "uge"; break;
856 case FCmpInst::FCMP_ULT: pred = "ult"; break;
857 case FCmpInst::FCMP_ULE: pred = "ule"; break;
858 case FCmpInst::FCMP_UNE: pred = "une"; break;
859 case FCmpInst::FCMP_TRUE: pred = "true"; break;
860 case ICmpInst::ICMP_EQ: pred = "eq"; break;
861 case ICmpInst::ICMP_NE: pred = "ne"; break;
862 case ICmpInst::ICMP_SGT: pred = "sgt"; break;
863 case ICmpInst::ICMP_SGE: pred = "sge"; break;
864 case ICmpInst::ICMP_SLT: pred = "slt"; break;
865 case ICmpInst::ICMP_SLE: pred = "sle"; break;
866 case ICmpInst::ICMP_UGT: pred = "ugt"; break;
867 case ICmpInst::ICMP_UGE: pred = "uge"; break;
868 case ICmpInst::ICMP_ULT: pred = "ult"; break;
869 case ICmpInst::ICMP_ULE: pred = "ule"; break;
870 }
871 return pred;
872}
873
Devang Patel2d5988d2009-09-30 20:16:54 +0000874static void WriteMDNodeComment(const MDNode *Node,
875 formatted_raw_ostream &Out) {
876 if (Node->getNumElements() < 1)
877 return;
Devang Patel4d7a2062009-09-30 21:26:51 +0000878 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Node->getElement(0));
Devang Patel2d5988d2009-09-30 20:16:54 +0000879 if (!CI) return;
880 unsigned Val = CI->getZExtValue();
881 unsigned Tag = Val & ~LLVMDebugVersionMask;
882 if (Val >= LLVMDebugVersion) {
883 if (Tag == dwarf::DW_TAG_auto_variable)
884 Out << "; [ DW_TAG_auto_variable ]";
885 else if (Tag == dwarf::DW_TAG_arg_variable)
886 Out << "; [ DW_TAG_arg_variable ]";
887 else if (Tag == dwarf::DW_TAG_return_variable)
888 Out << "; [ DW_TAG_return_variable ]";
889 else if (Tag == dwarf::DW_TAG_vector_type)
890 Out << "; [ DW_TAG_vector_type ]";
891 else if (Tag == dwarf::DW_TAG_user_base)
892 Out << "; [ DW_TAG_user_base ]";
893 else
894 Out << "; [" << dwarf::TagString(Tag) << " ]";
895 }
896}
897
Dan Gohman683e9222009-08-12 17:23:50 +0000898static void WriteMDNodes(formatted_raw_ostream &Out, TypePrinting &TypePrinter,
Devang Patel320671d2009-07-08 21:44:25 +0000899 SlotTracker &Machine) {
900 SmallVector<const MDNode *, 16> Nodes;
901 Nodes.resize(Machine.mdnSize());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000902 for (SlotTracker::ValueMap::iterator I =
903 Machine.mdnBegin(), E = Machine.mdnEnd(); I != E; ++I)
Devang Patel320671d2009-07-08 21:44:25 +0000904 Nodes[I->second] = cast<MDNode>(I->first);
905
906 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Devang Patel2214c942009-07-08 21:57:07 +0000907 Out << '!' << i << " = metadata ";
Devang Patel320671d2009-07-08 21:44:25 +0000908 const MDNode *Node = Nodes[i];
909 Out << "!{";
Devang Patel028fa772009-10-21 21:25:09 +0000910 for (unsigned mi = 0, me = Node->getNumElements(); mi != me; ++mi) {
911 const Value *V = Node->getElement(mi);
Devang Patel320671d2009-07-08 21:44:25 +0000912 if (!V)
913 Out << "null";
914 else if (const MDNode *N = dyn_cast<MDNode>(V)) {
915 Out << "metadata ";
916 Out << '!' << Machine.getMetadataSlot(N);
917 }
918 else {
Devang Patel028fa772009-10-21 21:25:09 +0000919 TypePrinter.print(V->getType(), Out);
Devang Patel320671d2009-07-08 21:44:25 +0000920 Out << ' ';
Devang Patel028fa772009-10-21 21:25:09 +0000921 WriteAsOperandInternal(Out, Node->getElement(mi),
922 &TypePrinter, &Machine);
Devang Patel320671d2009-07-08 21:44:25 +0000923 }
Devang Patel028fa772009-10-21 21:25:09 +0000924 if (mi + 1 != me)
Devang Patel320671d2009-07-08 21:44:25 +0000925 Out << ", ";
926 }
Devang Patel2d5988d2009-09-30 20:16:54 +0000927
928 Out << "}";
929 WriteMDNodeComment(Node, Out);
930 Out << "\n";
Devang Patel320671d2009-07-08 21:44:25 +0000931 }
932}
933
Dan Gohman1220e102009-08-12 20:56:03 +0000934static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
Dan Gohman1224c382009-07-20 21:19:07 +0000935 if (const OverflowingBinaryOperator *OBO =
936 dyn_cast<OverflowingBinaryOperator>(U)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000937 if (OBO->hasNoUnsignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000938 Out << " nuw";
Dan Gohman5078f842009-08-20 17:11:38 +0000939 if (OBO->hasNoSignedWrap())
Dan Gohman59858cf2009-07-27 16:11:46 +0000940 Out << " nsw";
Dan Gohman1224c382009-07-20 21:19:07 +0000941 } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) {
942 if (Div->isExact())
Dan Gohman59858cf2009-07-27 16:11:46 +0000943 Out << " exact";
Dan Gohmandd8004d2009-07-27 21:53:46 +0000944 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
945 if (GEP->isInBounds())
946 Out << " inbounds";
Dan Gohman1224c382009-07-20 21:19:07 +0000947 }
948}
949
Dan Gohman1220e102009-08-12 20:56:03 +0000950static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Chris Lattner9cc34462009-02-28 20:25:14 +0000951 TypePrinting &TypePrinter, SlotTracker *Machine) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000952 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000953 if (CI->getType() == Type::getInt1Ty(CV->getContext())) {
Reid Spencer579dca12007-01-12 04:24:46 +0000954 Out << (CI->getZExtValue() ? "true" : "false");
Chris Lattnerfad86b02008-08-17 07:19:36 +0000955 return;
956 }
957 Out << CI->getValue();
958 return;
959 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000960
Chris Lattnerfad86b02008-08-17 07:19:36 +0000961 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000962 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
963 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
964 // We would like to output the FP constant value in exponential notation,
965 // but we cannot do this if doing so will lose precision. Check here to
966 // make sure that we only output it in exponential format if we can parse
967 // the value back and get the same value.
968 //
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000969 bool ignored;
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000970 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
Chris Lattnerfad86b02008-08-17 07:19:36 +0000971 double Val = isDouble ? CFP->getValueAPF().convertToDouble() :
972 CFP->getValueAPF().convertToFloat();
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000973 std::string StrVal = ftostr(CFP->getValueAPF());
Chris Lattner66e810b2002-04-18 18:53:13 +0000974
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000975 // Check to make sure that the stringized number is not some string like
976 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
977 // that the string matches the "[-+]?[0-9]" regex.
978 //
979 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
980 ((StrVal[0] == '-' || StrVal[0] == '+') &&
981 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
982 // Reparse stringized version!
983 if (atof(StrVal.c_str()) == Val) {
984 Out << StrVal;
985 return;
986 }
Chris Lattner66e810b2002-04-18 18:53:13 +0000987 }
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000988 // Otherwise we could not reparse it to exactly the same value, so we must
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000989 // output the string in hexadecimal format! Note that loading and storing
990 // floating point types changes the bits of NaNs on some hosts, notably
991 // x86, so we must not use these types.
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000992 assert(sizeof(double) == sizeof(uint64_t) &&
993 "assuming that double is 64 bits!");
Chris Lattnerc6a13462008-11-10 04:30:26 +0000994 char Buffer[40];
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000995 APFloat apf = CFP->getValueAPF();
996 // Floats are represented in ASCII IR as double, convert.
997 if (!isDouble)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000998 apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
Dale Johannesen541ed9f2009-01-21 20:32:55 +0000999 &ignored);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001000 Out << "0x" <<
1001 utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()),
Dale Johannesen541ed9f2009-01-21 20:32:55 +00001002 Buffer+40);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001003 return;
1004 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001005
Chris Lattnercfb5a202008-08-19 05:06:27 +00001006 // Some form of long double. These appear as a magic letter identifying
1007 // the type, then a fixed number of hex digits.
1008 Out << "0x";
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001009 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001010 Out << 'K';
Dale Johannesen1b25cb22009-03-23 21:16:53 +00001011 // api needed to prevent premature destruction
1012 APInt api = CFP->getValueAPF().bitcastToAPInt();
1013 const uint64_t* p = api.getRawData();
1014 uint64_t word = p[1];
1015 int shiftcount=12;
1016 int width = api.getBitWidth();
1017 for (int j=0; j<width; j+=4, shiftcount-=4) {
1018 unsigned int nibble = (word>>shiftcount) & 15;
1019 if (nibble < 10)
1020 Out << (unsigned char)(nibble + '0');
1021 else
1022 Out << (unsigned char)(nibble - 10 + 'A');
1023 if (shiftcount == 0 && j+4 < width) {
1024 word = *p;
1025 shiftcount = 64;
1026 if (width-j-4 < 64)
1027 shiftcount = width-j-4;
1028 }
1029 }
1030 return;
1031 } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Chris Lattnercfb5a202008-08-19 05:06:27 +00001032 Out << 'L';
1033 else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
1034 Out << 'M';
1035 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001036 llvm_unreachable("Unsupported floating point type");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001037 // api needed to prevent premature destruction
Dale Johannesen7111b022008-10-09 18:53:47 +00001038 APInt api = CFP->getValueAPF().bitcastToAPInt();
Chris Lattnercfb5a202008-08-19 05:06:27 +00001039 const uint64_t* p = api.getRawData();
1040 uint64_t word = *p;
1041 int shiftcount=60;
1042 int width = api.getBitWidth();
1043 for (int j=0; j<width; j+=4, shiftcount-=4) {
1044 unsigned int nibble = (word>>shiftcount) & 15;
1045 if (nibble < 10)
1046 Out << (unsigned char)(nibble + '0');
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001047 else
Chris Lattnercfb5a202008-08-19 05:06:27 +00001048 Out << (unsigned char)(nibble - 10 + 'A');
1049 if (shiftcount == 0 && j+4 < width) {
1050 word = *(++p);
1051 shiftcount = 64;
1052 if (width-j-4 < 64)
1053 shiftcount = width-j-4;
Dale Johannesen9d5f4562007-09-12 03:30:33 +00001054 }
1055 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001056 return;
1057 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001058
Chris Lattnercfb5a202008-08-19 05:06:27 +00001059 if (isa<ConstantAggregateZero>(CV)) {
Chris Lattnerde512b52004-02-15 05:55:15 +00001060 Out << "zeroinitializer";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001061 return;
1062 }
Chris Lattner73050e12009-10-28 03:38:12 +00001063
1064 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1065 Out << "blockaddress(";
1066 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine);
1067 Out << ", ";
1068 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine);
1069 Out << ")";
1070 return;
1071 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001072
Chris Lattnercfb5a202008-08-19 05:06:27 +00001073 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
Chris Lattner66e810b2002-04-18 18:53:13 +00001074 // As a special case, print the array as a string if it is an array of
Dan Gohman9b38d7d2008-05-12 16:34:30 +00001075 // i8 with ConstantInt values.
Misha Brukmanfd939082005-04-21 23:48:37 +00001076 //
Chris Lattner66e810b2002-04-18 18:53:13 +00001077 const Type *ETy = CA->getType()->getElementType();
Chris Lattner18365502006-01-23 23:03:36 +00001078 if (CA->isString()) {
Chris Lattner66e810b2002-04-18 18:53:13 +00001079 Out << "c\"";
Chris Lattner18365502006-01-23 23:03:36 +00001080 PrintEscapedString(CA->getAsString(), Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001081 Out << '"';
Chris Lattner66e810b2002-04-18 18:53:13 +00001082 } else { // Cannot output in string format...
Misha Brukman40c732c2004-06-04 21:11:51 +00001083 Out << '[';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001084 if (CA->getNumOperands()) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001085 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001086 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001087 WriteAsOperandInternal(Out, CA->getOperand(0),
Dan Gohmand6c0f652009-08-13 15:27:57 +00001088 &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001089 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1090 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001091 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001092 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001093 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001094 }
1095 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001096 Out << ']';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001097 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001098 return;
1099 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001100
Chris Lattnercfb5a202008-08-19 05:06:27 +00001101 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001102 if (CS->getType()->isPacked())
1103 Out << '<';
Misha Brukman40c732c2004-06-04 21:11:51 +00001104 Out << '{';
Jim Laskeya3f332b2006-02-25 12:27:03 +00001105 unsigned N = CS->getNumOperands();
1106 if (N) {
Chris Lattner24233032008-08-19 04:47:09 +00001107 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001108 TypePrinter.print(CS->getOperand(0)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001109 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001110
Dan Gohmand6c0f652009-08-13 15:27:57 +00001111 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001112
Jim Laskeya3f332b2006-02-25 12:27:03 +00001113 for (unsigned i = 1; i < N; i++) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001114 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001115 TypePrinter.print(CS->getOperand(i)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001116 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001117
Dan Gohmand6c0f652009-08-13 15:27:57 +00001118 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine);
Chris Lattner7a716ad2002-04-16 21:36:08 +00001119 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001120 Out << ' ';
Chris Lattner7a716ad2002-04-16 21:36:08 +00001121 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001122
Dan Gohman8dae1382008-09-14 17:21:12 +00001123 Out << '}';
Andrew Lenharth43f344a2007-01-08 18:21:30 +00001124 if (CS->getType()->isPacked())
1125 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001126 return;
1127 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001128
Chris Lattnercfb5a202008-08-19 05:06:27 +00001129 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
1130 const Type *ETy = CP->getType()->getElementType();
1131 assert(CP->getNumOperands() > 0 &&
1132 "Number of operands for a PackedConst must be > 0");
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001133 Out << '<';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001134 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001135 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001136 WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001137 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Chris Lattner4667b712008-08-19 05:26:17 +00001138 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001139 TypePrinter.print(ETy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001140 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001141 WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001142 }
Dan Gohman7dfa07f2009-02-11 00:25:25 +00001143 Out << '>';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001144 return;
1145 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001146
Chris Lattnercfb5a202008-08-19 05:06:27 +00001147 if (isa<ConstantPointerNull>(CV)) {
Chris Lattner7a716ad2002-04-16 21:36:08 +00001148 Out << "null";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001149 return;
1150 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001151
Chris Lattnercfb5a202008-08-19 05:06:27 +00001152 if (isa<UndefValue>(CV)) {
Chris Lattnerb976e662004-10-16 18:08:06 +00001153 Out << "undef";
Chris Lattnercfb5a202008-08-19 05:06:27 +00001154 return;
1155 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001156
Devang Patel320671d2009-07-08 21:44:25 +00001157 if (const MDNode *Node = dyn_cast<MDNode>(CV)) {
1158 Out << "!" << Machine->getMetadataSlot(Node);
1159 return;
1160 }
1161
Chris Lattnercfb5a202008-08-19 05:06:27 +00001162 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Reid Spencer81dfeb32006-12-04 05:19:18 +00001163 Out << CE->getOpcodeName();
Dan Gohman59858cf2009-07-27 16:11:46 +00001164 WriteOptimizationInfo(Out, CE);
Reid Spencer81dfeb32006-12-04 05:19:18 +00001165 if (CE->isCompare())
Chris Lattnercfb5a202008-08-19 05:06:27 +00001166 Out << ' ' << getPredicateText(CE->getPredicate());
Reid Spencer81dfeb32006-12-04 05:19:18 +00001167 Out << " (";
Misha Brukmanfd939082005-04-21 23:48:37 +00001168
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001169 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001170 TypePrinter.print((*OI)->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001171 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001172 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine);
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001173 if (OI+1 != CE->op_end())
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001174 Out << ", ";
Vikram S. Adveb4dbb442002-07-14 23:14:45 +00001175 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001176
Dan Gohman995be7d2008-05-31 19:12:39 +00001177 if (CE->hasIndices()) {
1178 const SmallVector<unsigned, 4> &Indices = CE->getIndices();
1179 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1180 Out << ", " << Indices[i];
1181 }
1182
Reid Spencer3da59db2006-11-27 01:05:10 +00001183 if (CE->isCast()) {
Chris Lattner95586b82002-08-15 19:37:43 +00001184 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001185 TypePrinter.print(CE->getType(), Out);
Chris Lattner95586b82002-08-15 19:37:43 +00001186 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001187
Misha Brukman40c732c2004-06-04 21:11:51 +00001188 Out << ')';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001189 return;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001190 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001191
Chris Lattnercfb5a202008-08-19 05:06:27 +00001192 Out << "<placeholder or erroneous Constant>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001193}
1194
1195
Misha Brukmanab5c6002004-03-02 00:22:19 +00001196/// WriteAsOperand - Write the name of the specified value out to the specified
1197/// ostream. This can be useful when you just want to print int %reg126, not
1198/// the whole instruction that generated it.
1199///
Dan Gohman1220e102009-08-12 20:56:03 +00001200static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
Dan Gohmand6c0f652009-08-13 15:27:57 +00001201 TypePrinting *TypePrinter,
Chris Lattner0d9574a2008-08-19 04:26:57 +00001202 SlotTracker *Machine) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001203 if (V->hasName()) {
1204 PrintLLVMName(Out, V);
1205 return;
1206 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001207
Chris Lattnerc97536e2008-08-17 04:40:13 +00001208 const Constant *CV = dyn_cast<Constant>(V);
1209 if (CV && !isa<GlobalValue>(CV)) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001210 assert(TypePrinter && "Constants require TypePrinting!");
1211 WriteConstantInt(Out, CV, *TypePrinter, Machine);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001212 return;
1213 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001214
Chris Lattnercfb5a202008-08-19 05:06:27 +00001215 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Chris Lattnerc97536e2008-08-17 04:40:13 +00001216 Out << "asm ";
1217 if (IA->hasSideEffects())
1218 Out << "sideeffect ";
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00001219 if (IA->isAlignStack())
1220 Out << "alignstack ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001221 Out << '"';
1222 PrintEscapedString(IA->getAsmString(), Out);
1223 Out << "\", \"";
1224 PrintEscapedString(IA->getConstraintString(), Out);
1225 Out << '"';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001226 return;
1227 }
Devang Patele54abc92009-07-22 17:43:22 +00001228
Devang Patel104cf9e2009-07-23 01:07:34 +00001229 if (const MDNode *N = dyn_cast<MDNode>(V)) {
1230 Out << '!' << Machine->getMetadataSlot(N);
1231 return;
1232 }
1233
Devang Patele54abc92009-07-22 17:43:22 +00001234 if (const MDString *MDS = dyn_cast<MDString>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +00001235 Out << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001236 PrintEscapedString(MDS->getString(), Out);
Devang Patele54abc92009-07-22 17:43:22 +00001237 Out << '"';
1238 return;
1239 }
1240
Dan Gohmancd26ec52009-09-23 01:33:16 +00001241 if (V->getValueID() == Value::PseudoSourceValueVal) {
1242 V->print(Out);
1243 return;
1244 }
1245
Chris Lattnercfb5a202008-08-19 05:06:27 +00001246 char Prefix = '%';
1247 int Slot;
1248 if (Machine) {
1249 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1250 Slot = Machine->getGlobalSlot(GV);
1251 Prefix = '@';
1252 } else {
1253 Slot = Machine->getLocalSlot(V);
1254 }
Chris Lattnerc97536e2008-08-17 04:40:13 +00001255 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001256 Machine = createSlotTracker(V);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001257 if (Machine) {
1258 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1259 Slot = Machine->getGlobalSlot(GV);
1260 Prefix = '@';
1261 } else {
1262 Slot = Machine->getLocalSlot(V);
1263 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001264 delete Machine;
Chris Lattner80cd1152006-01-25 22:26:05 +00001265 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001266 Slot = -1;
Chris Lattner7a716ad2002-04-16 21:36:08 +00001267 }
1268 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001269
Chris Lattnercfb5a202008-08-19 05:06:27 +00001270 if (Slot != -1)
1271 Out << Prefix << Slot;
1272 else
1273 Out << "<badref>";
Chris Lattner7a716ad2002-04-16 21:36:08 +00001274}
1275
Dan Gohman1220e102009-08-12 20:56:03 +00001276void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
1277 bool PrintType, const Module *Context) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001278
1279 // Fast path: Don't construct and populate a TypePrinting object if we
1280 // won't be needing any types printed.
Dan Gohman009fc9e2009-08-13 23:07:11 +00001281 if (!PrintType &&
1282 (!isa<Constant>(V) || V->hasName() || isa<GlobalValue>(V))) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001283 WriteAsOperandInternal(Out, V, 0, 0);
1284 return;
1285 }
1286
Chris Lattner607dc682002-07-10 16:48:17 +00001287 if (Context == 0) Context = getModuleFromVal(V);
Chris Lattner207b5bc2001-10-29 16:37:48 +00001288
Chris Lattnere9fa33e2009-02-28 23:20:19 +00001289 TypePrinting TypePrinter;
Chris Lattner413fd232009-03-01 00:03:38 +00001290 std::vector<const Type*> NumberedTypes;
1291 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
Dan Gohman8dae1382008-09-14 17:21:12 +00001292 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001293 TypePrinter.print(V->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001294 Out << ' ';
1295 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001296
Dan Gohmand6c0f652009-08-13 15:27:57 +00001297 WriteAsOperandInternal(Out, V, &TypePrinter, 0);
Chris Lattner622f7402001-07-20 19:15:21 +00001298}
1299
Chris Lattnercfb5a202008-08-19 05:06:27 +00001300namespace {
Chris Lattnerd8c2e422001-07-12 23:35:26 +00001301
Chris Lattner007377f2001-09-07 16:36:04 +00001302class AssemblyWriter {
Dan Gohman683e9222009-08-12 17:23:50 +00001303 formatted_raw_ostream &Out;
Chris Lattner0d9574a2008-08-19 04:26:57 +00001304 SlotTracker &Machine;
Chris Lattnerc1824992001-10-29 16:05:51 +00001305 const Module *TheModule;
Chris Lattner9cc34462009-02-28 20:25:14 +00001306 TypePrinting TypePrinter;
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001307 AssemblyAnnotationWriter *AnnotationWriter;
Chris Lattner413fd232009-03-01 00:03:38 +00001308 std::vector<const Type*> NumberedTypes;
Devang Patelce6a1c92009-10-22 01:01:24 +00001309 DenseMap<unsigned, StringRef> MDNames;
Devang Patel923078c2009-07-01 19:21:12 +00001310
Chris Lattner00950542001-06-06 20:29:01 +00001311public:
Dan Gohman683e9222009-08-12 17:23:50 +00001312 inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
1313 const Module *M,
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001314 AssemblyAnnotationWriter *AAW)
Devang Patel3168b792009-09-28 18:31:56 +00001315 : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
Chris Lattner413fd232009-03-01 00:03:38 +00001316 AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
Devang Patel18f0c262009-09-28 20:56:00 +00001317 // FIXME: Provide MDPrinter
Devang Patel7f93f4d2009-10-07 16:37:55 +00001318 if (M) {
1319 MetadataContext &TheMetadata = M->getContext().getMetadata();
Devang Patelce6a1c92009-10-22 01:01:24 +00001320 SmallVector<std::pair<unsigned, StringRef>, 4> Names;
1321 TheMetadata.getHandlerNames(Names);
1322 for (SmallVector<std::pair<unsigned, StringRef>, 4>::iterator
1323 I = Names.begin(),
1324 E = Names.end(); I != E; ++I) {
1325 MDNames[I->first] = I->second;
Devang Patel7f93f4d2009-10-07 16:37:55 +00001326 }
Devang Patel18f0c262009-09-28 20:56:00 +00001327 }
Chris Lattner00950542001-06-06 20:29:01 +00001328 }
1329
Chris Lattner413fd232009-03-01 00:03:38 +00001330 void write(const Module *M) { printModule(M); }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001331
Chris Lattner944fac72008-08-23 22:23:09 +00001332 void write(const GlobalValue *G) {
1333 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
1334 printGlobal(GV);
1335 else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G))
1336 printAlias(GA);
1337 else if (const Function *F = dyn_cast<Function>(G))
1338 printFunction(F);
1339 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001340 llvm_unreachable("Unknown global");
Chris Lattner944fac72008-08-23 22:23:09 +00001341 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001342
Chris Lattnercfb5a202008-08-19 05:06:27 +00001343 void write(const BasicBlock *BB) { printBasicBlock(BB); }
1344 void write(const Instruction *I) { printInstruction(*I); }
Chris Lattner00950542001-06-06 20:29:01 +00001345
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001346 void writeOperand(const Value *Op, bool PrintType);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001347 void writeParamOperand(const Value *Operand, Attributes Attrs);
Chris Lattner66e810b2002-04-18 18:53:13 +00001348
Misha Brukmanf771bea2004-11-15 19:30:05 +00001349private:
Chris Lattnerc1824992001-10-29 16:05:51 +00001350 void printModule(const Module *M);
Reid Spencer78d033e2007-01-06 07:24:44 +00001351 void printTypeSymbolTable(const TypeSymbolTable &ST);
Chris Lattnerc1824992001-10-29 16:05:51 +00001352 void printGlobal(const GlobalVariable *GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001353 void printAlias(const GlobalAlias *GV);
Chris Lattner79df7c02002-03-26 18:01:55 +00001354 void printFunction(const Function *F);
Devang Pateleaf42ab2008-09-23 23:03:40 +00001355 void printArgument(const Argument *FA, Attributes Attrs);
Chris Lattnerc1824992001-10-29 16:05:51 +00001356 void printBasicBlock(const BasicBlock *BB);
Chris Lattner7e708292002-06-25 16:13:24 +00001357 void printInstruction(const Instruction &I);
Chris Lattner2761e9f2002-04-13 20:53:41 +00001358
Chris Lattnere02fa852001-10-13 06:42:36 +00001359 // printInfoComment - Print a little comment after the instruction indicating
1360 // which slot it occupies.
Chris Lattner7e708292002-06-25 16:13:24 +00001361 void printInfoComment(const Value &V);
Chris Lattner00950542001-06-06 20:29:01 +00001362};
Chris Lattner413fd232009-03-01 00:03:38 +00001363} // end of anonymous namespace
Chris Lattner00950542001-06-06 20:29:01 +00001364
Chris Lattner2761e9f2002-04-13 20:53:41 +00001365
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001366void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
1367 if (Operand == 0) {
Chris Lattneraab18202005-02-24 16:58:29 +00001368 Out << "<null operand!>";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001369 } else {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001370 if (PrintType) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001371 TypePrinter.print(Operand->getType(), Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001372 Out << ' ';
Chris Lattnercfb5a202008-08-19 05:06:27 +00001373 }
Dan Gohmand6c0f652009-08-13 15:27:57 +00001374 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Chris Lattneraab18202005-02-24 16:58:29 +00001375 }
Chris Lattner00950542001-06-06 20:29:01 +00001376}
1377
Daniel Dunbara279bc32009-09-20 02:20:51 +00001378void AssemblyWriter::writeParamOperand(const Value *Operand,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001379 Attributes Attrs) {
Duncan Sandsdc024672007-11-27 13:23:08 +00001380 if (Operand == 0) {
1381 Out << "<null operand!>";
1382 } else {
Duncan Sandsdc024672007-11-27 13:23:08 +00001383 // Print the type
Chris Lattner0f7364b2009-02-28 21:26:53 +00001384 TypePrinter.print(Operand->getType(), Out);
Duncan Sandsdc024672007-11-27 13:23:08 +00001385 // Print parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001386 if (Attrs != Attribute::None)
1387 Out << ' ' << Attribute::getAsString(Attrs);
Dan Gohman8dae1382008-09-14 17:21:12 +00001388 Out << ' ';
Duncan Sandsdc024672007-11-27 13:23:08 +00001389 // Print the operand
Dan Gohmand6c0f652009-08-13 15:27:57 +00001390 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine);
Duncan Sandsdc024672007-11-27 13:23:08 +00001391 }
1392}
Chris Lattner00950542001-06-06 20:29:01 +00001393
Chris Lattnerc1824992001-10-29 16:05:51 +00001394void AssemblyWriter::printModule(const Module *M) {
Chris Lattner31ab1b32005-03-02 23:12:40 +00001395 if (!M->getModuleIdentifier().empty() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001396 // Don't print the ID if it will start a new line (which would
Chris Lattner31ab1b32005-03-02 23:12:40 +00001397 // require a comment char before it).
1398 M->getModuleIdentifier().find('\n') == std::string::npos)
1399 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
1400
Owen Andersoncf7ff2b2006-10-18 02:21:12 +00001401 if (!M->getDataLayout().empty())
Chris Lattnerd2f9e602006-10-22 06:06:56 +00001402 Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
Reid Spencercddc86f2004-07-25 21:44:54 +00001403 if (!M->getTargetTriple().empty())
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001404 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
Misha Brukmanfd939082005-04-21 23:48:37 +00001405
Chris Lattnercc041ba2006-01-24 04:13:11 +00001406 if (!M->getModuleInlineAsm().empty()) {
Chris Lattner42a162e2006-01-24 00:45:30 +00001407 // Split the string into lines, to make it easier to read the .ll file.
Chris Lattnercc041ba2006-01-24 04:13:11 +00001408 std::string Asm = M->getModuleInlineAsm();
Chris Lattner42a162e2006-01-24 00:45:30 +00001409 size_t CurPos = 0;
1410 size_t NewLine = Asm.find_first_of('\n', CurPos);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001411 Out << '\n';
Chris Lattner42a162e2006-01-24 00:45:30 +00001412 while (NewLine != std::string::npos) {
1413 // We found a newline, print the portion of the asm string from the
1414 // last newline up to this newline.
1415 Out << "module asm \"";
1416 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine),
1417 Out);
1418 Out << "\"\n";
1419 CurPos = NewLine+1;
1420 NewLine = Asm.find_first_of('\n', CurPos);
1421 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001422 Out << "module asm \"";
Chris Lattner42a162e2006-01-24 00:45:30 +00001423 PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out);
Chris Lattner18365502006-01-23 23:03:36 +00001424 Out << "\"\n";
1425 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001426
Chris Lattner44da7d72004-09-14 05:06:58 +00001427 // Loop over the dependent libraries and emit them.
Chris Lattnercfe97b72004-09-14 04:51:44 +00001428 Module::lib_iterator LI = M->lib_begin();
1429 Module::lib_iterator LE = M->lib_end();
Reid Spencercddc86f2004-07-25 21:44:54 +00001430 if (LI != LE) {
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001431 Out << '\n';
Chris Lattnercfe97b72004-09-14 04:51:44 +00001432 Out << "deplibs = [ ";
1433 while (LI != LE) {
Chris Lattner44da7d72004-09-14 05:06:58 +00001434 Out << '"' << *LI << '"';
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001435 ++LI;
Chris Lattnercfe97b72004-09-14 04:51:44 +00001436 if (LI != LE)
1437 Out << ", ";
Reid Spencerc9a1f0d2004-07-25 21:29:43 +00001438 }
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001439 Out << " ]";
Reid Spencer83f6a772004-07-25 18:08:18 +00001440 }
Reid Spencere59eaf42004-09-13 23:44:23 +00001441
Chris Lattner413fd232009-03-01 00:03:38 +00001442 // Loop over the symbol table, emitting all id'd types.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001443 if (!M->getTypeSymbolTable().empty() || !NumberedTypes.empty()) Out << '\n';
Reid Spencer78d033e2007-01-06 07:24:44 +00001444 printTypeSymbolTable(M->getTypeSymbolTable());
Misha Brukmanfd939082005-04-21 23:48:37 +00001445
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001446 // Output all globals.
1447 if (!M->global_empty()) Out << '\n';
Chris Lattnerd6d826c2006-12-06 04:41:52 +00001448 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1449 I != E; ++I)
Chris Lattner7e708292002-06-25 16:13:24 +00001450 printGlobal(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001451
Chris Lattner69dacfc2007-04-26 02:24:10 +00001452 // Output all aliases.
1453 if (!M->alias_empty()) Out << "\n";
1454 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
1455 I != E; ++I)
1456 printAlias(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001457
Chris Lattner44da7d72004-09-14 05:06:58 +00001458 // Output all of the functions.
Chris Lattner7e708292002-06-25 16:13:24 +00001459 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1460 printFunction(I);
Devang Patel320671d2009-07-08 21:44:25 +00001461
Devang Patel37c4a2d2009-07-29 22:04:47 +00001462 // Output named metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001463 if (!M->named_metadata_empty()) Out << '\n';
Devang Patel37c4a2d2009-07-29 22:04:47 +00001464 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
1465 E = M->named_metadata_end(); I != E; ++I) {
1466 const NamedMDNode *NMD = I;
1467 Out << "!" << NMD->getName() << " = !{";
1468 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1469 if (i) Out << ", ";
Devang Patele8861b82009-07-30 01:02:04 +00001470 MDNode *MD = dyn_cast_or_null<MDNode>(NMD->getElement(i));
Devang Patel37c4a2d2009-07-29 22:04:47 +00001471 Out << '!' << Machine.getMetadataSlot(MD);
1472 }
1473 Out << "}\n";
1474 }
1475
1476 // Output metadata.
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001477 if (!Machine.mdnEmpty()) Out << '\n';
Devang Patel320671d2009-07-08 21:44:25 +00001478 WriteMDNodes(Out, TypePrinter, Machine);
Chris Lattner007377f2001-09-07 16:36:04 +00001479}
1480
Dan Gohman683e9222009-08-12 17:23:50 +00001481static void PrintLinkage(GlobalValue::LinkageTypes LT,
1482 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001483 switch (LT) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001484 case GlobalValue::ExternalLinkage: break;
1485 case GlobalValue::PrivateLinkage: Out << "private "; break;
1486 case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break;
1487 case GlobalValue::InternalLinkage: Out << "internal "; break;
1488 case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
1489 case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
1490 case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
1491 case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
1492 case GlobalValue::CommonLinkage: Out << "common "; break;
1493 case GlobalValue::AppendingLinkage: Out << "appending "; break;
1494 case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
1495 case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
1496 case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001497 case GlobalValue::AvailableExternallyLinkage:
1498 Out << "available_externally ";
1499 break;
Chris Lattnercfb5a202008-08-19 05:06:27 +00001500 case GlobalValue::GhostLinkage:
Torok Edwinc23197a2009-07-14 16:55:14 +00001501 llvm_unreachable("GhostLinkage not allowed in AsmWriter!");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001502 }
1503}
Duncan Sands667d4b82009-03-07 15:45:40 +00001504
Chris Lattnercfb5a202008-08-19 05:06:27 +00001505
1506static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
Dan Gohman683e9222009-08-12 17:23:50 +00001507 formatted_raw_ostream &Out) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001508 switch (Vis) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001509 default: llvm_unreachable("Invalid visibility style!");
Chris Lattnercfb5a202008-08-19 05:06:27 +00001510 case GlobalValue::DefaultVisibility: break;
1511 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
1512 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
1513 }
1514}
1515
Chris Lattnerc1824992001-10-29 16:05:51 +00001516void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
Dan Gohmand6c0f652009-08-13 15:27:57 +00001517 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine);
Dan Gohman3845e502009-08-12 23:32:33 +00001518 Out << " = ";
Chris Lattnerd70684f2001-09-18 04:01:05 +00001519
Chris Lattner52b26de2008-08-19 05:16:28 +00001520 if (!GV->hasInitializer() && GV->hasExternalLinkage())
1521 Out << "external ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001522
Chris Lattner52b26de2008-08-19 05:16:28 +00001523 PrintLinkage(GV->getLinkage(), Out);
1524 PrintVisibility(GV->getVisibility(), Out);
Lauro Ramos Venancioc7635522007-04-12 18:32:50 +00001525
1526 if (GV->isThreadLocal()) Out << "thread_local ";
Chris Lattnerdf986172009-01-02 07:01:27 +00001527 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
1528 Out << "addrspace(" << AddressSpace << ") ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001529 Out << (GV->isConstant() ? "constant " : "global ");
Chris Lattner0f7364b2009-02-28 21:26:53 +00001530 TypePrinter.print(GV->getType()->getElementType(), Out);
Chris Lattnerd70684f2001-09-18 04:01:05 +00001531
Dan Gohman8dae1382008-09-14 17:21:12 +00001532 if (GV->hasInitializer()) {
1533 Out << ' ';
Devang Patel320671d2009-07-08 21:44:25 +00001534 writeOperand(GV->getInitializer(), false);
Dan Gohman8dae1382008-09-14 17:21:12 +00001535 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001536
Chris Lattner60962db2005-11-12 00:10:19 +00001537 if (GV->hasSection())
1538 Out << ", section \"" << GV->getSection() << '"';
1539 if (GV->getAlignment())
Chris Lattner30caa282005-11-06 06:48:53 +00001540 Out << ", align " << GV->getAlignment();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001541
Chris Lattner7e708292002-06-25 16:13:24 +00001542 printInfoComment(*GV);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001543 Out << '\n';
Chris Lattner70cc3392001-09-10 07:58:01 +00001544}
1545
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001546void AssemblyWriter::printAlias(const GlobalAlias *GA) {
Dale Johannesen24f07dc2008-06-03 18:14:29 +00001547 // Don't crash when dumping partially built GA
1548 if (!GA->hasName())
1549 Out << "<<nameless>> = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001550 else {
1551 PrintLLVMName(Out, GA);
1552 Out << " = ";
1553 }
Chris Lattnercfb5a202008-08-19 05:06:27 +00001554 PrintVisibility(GA->getVisibility(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001555
1556 Out << "alias ";
1557
Chris Lattnercfb5a202008-08-19 05:06:27 +00001558 PrintLinkage(GA->getLinkage(), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001559
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +00001560 const Constant *Aliasee = GA->getAliasee();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001561
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001562 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001563 TypePrinter.print(GV->getType(), Out);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001564 Out << ' ';
1565 PrintLLVMName(Out, GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001566 } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001567 TypePrinter.print(F->getFunctionType(), Out);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001568 Out << "* ";
1569
Dan Gohmand6c0f652009-08-13 15:27:57 +00001570 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Anton Korobeynikov591858a2008-03-22 08:17:17 +00001571 } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001572 TypePrinter.print(GA->getType(), Out);
1573 Out << ' ';
Chris Lattnerc97536e2008-08-17 04:40:13 +00001574 PrintLLVMName(Out, GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001575 } else {
Chris Lattnera2165ed2009-04-25 21:23:19 +00001576 const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
1577 // The only valid GEP is an all zero GEP.
1578 assert((CE->getOpcode() == Instruction::BitCast ||
1579 CE->getOpcode() == Instruction::GetElementPtr) &&
1580 "Unsupported aliasee");
1581 writeOperand(CE, false);
Anton Korobeynikova80e1182007-04-28 13:45:00 +00001582 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001583
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001584 printInfoComment(*GA);
Chris Lattner52b26de2008-08-19 05:16:28 +00001585 Out << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +00001586}
1587
Reid Spencer78d033e2007-01-06 07:24:44 +00001588void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
Chris Lattner413fd232009-03-01 00:03:38 +00001589 // Emit all numbered types.
1590 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
Dan Gohman3845e502009-08-12 23:32:33 +00001591 Out << '%' << i << " = type ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001592
Chris Lattner413fd232009-03-01 00:03:38 +00001593 // Make sure we print out at least one level of the type structure, so
1594 // that we do not get %2 = type %2
1595 TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001596 Out << '\n';
Chris Lattner413fd232009-03-01 00:03:38 +00001597 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001598
Chris Lattner413fd232009-03-01 00:03:38 +00001599 // Print the named types.
Reid Spencer78d033e2007-01-06 07:24:44 +00001600 for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
1601 TI != TE; ++TI) {
Daniel Dunbar03d76512009-07-25 23:55:21 +00001602 PrintLLVMName(Out, TI->first, LocalPrefix);
Chris Lattner52b26de2008-08-19 05:16:28 +00001603 Out << " = type ";
Reid Spencer9231ac82004-05-25 08:53:40 +00001604
1605 // Make sure we print out at least one level of the type structure, so
1606 // that we do not get %FILE = type %FILE
Chris Lattner0f7364b2009-02-28 21:26:53 +00001607 TypePrinter.printAtLeastOneLevel(TI->second, Out);
Chris Lattnercfb5a202008-08-19 05:06:27 +00001608 Out << '\n';
Reid Spencer9231ac82004-05-25 08:53:40 +00001609 }
Reid Spencer78d033e2007-01-06 07:24:44 +00001610}
1611
Misha Brukmanab5c6002004-03-02 00:22:19 +00001612/// printFunction - Print all aspects of a function.
1613///
Chris Lattner7e708292002-06-25 16:13:24 +00001614void AssemblyWriter::printFunction(const Function *F) {
Chris Lattnercfb5a202008-08-19 05:06:27 +00001615 // Print out the return type and name.
1616 Out << '\n';
Chris Lattner4ad02e72003-04-16 20:28:45 +00001617
Misha Brukman0313e0b2004-06-21 21:53:56 +00001618 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001619
Reid Spencer5cbf9852007-01-30 20:08:39 +00001620 if (F->isDeclaration())
Chris Lattner3aa60662007-08-19 22:15:26 +00001621 Out << "declare ";
1622 else
Reid Spencerb951bc02006-12-29 20:29:48 +00001623 Out << "define ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001624
Chris Lattnercfb5a202008-08-19 05:06:27 +00001625 PrintLinkage(F->getLinkage(), Out);
1626 PrintVisibility(F->getVisibility(), Out);
Chris Lattner4ad02e72003-04-16 20:28:45 +00001627
Chris Lattnerd5118982005-05-06 20:26:43 +00001628 // Print the calling convention.
1629 switch (F->getCallingConv()) {
1630 case CallingConv::C: break; // default
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001631 case CallingConv::Fast: Out << "fastcc "; break;
1632 case CallingConv::Cold: Out << "coldcc "; break;
1633 case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001634 case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
1635 case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
1636 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
1637 case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001638 default: Out << "cc" << F->getCallingConv() << " "; break;
1639 }
1640
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001641 const FunctionType *FT = F->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00001642 const AttrListPtr &Attrs = F->getAttributes();
Devang Patel652203f2008-09-29 20:49:50 +00001643 Attributes RetAttrs = Attrs.getRetAttributes();
1644 if (RetAttrs != Attribute::None)
1645 Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001646 TypePrinter.print(F->getReturnType(), Out);
Chris Lattner4667b712008-08-19 05:26:17 +00001647 Out << ' ';
Dan Gohmand6c0f652009-08-13 15:27:57 +00001648 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001649 Out << '(';
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001650 Machine.incorporateFunction(F);
Chris Lattner007377f2001-09-07 16:36:04 +00001651
Chris Lattnerc1824992001-10-29 16:05:51 +00001652 // Loop over the arguments, printing them...
Chris Lattner007377f2001-09-07 16:36:04 +00001653
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001654 unsigned Idx = 1;
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001655 if (!F->isDeclaration()) {
1656 // If this isn't a declaration, print the argument names as well.
1657 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1658 I != E; ++I) {
1659 // Insert commas as we go... the first arg doesn't get a comma
1660 if (I != F->arg_begin()) Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001661 printArgument(I, Attrs.getParamAttributes(Idx));
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001662 Idx++;
1663 }
1664 } else {
1665 // Otherwise, print the types from the function type.
1666 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1667 // Insert commas as we go... the first arg doesn't get a comma
1668 if (i) Out << ", ";
Daniel Dunbara279bc32009-09-20 02:20:51 +00001669
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001670 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001671 TypePrinter.print(FT->getParamType(i), Out);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001672
Devang Patel19c87462008-09-26 22:53:05 +00001673 Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
Devang Patel05988662008-09-25 21:00:45 +00001674 if (ArgAttrs != Attribute::None)
1675 Out << ' ' << Attribute::getAsString(ArgAttrs);
Chris Lattner8dcd2f12007-04-18 00:57:22 +00001676 }
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001677 }
Chris Lattner007377f2001-09-07 16:36:04 +00001678
1679 // Finish printing arguments...
Chris Lattner7e708292002-06-25 16:13:24 +00001680 if (FT->isVarArg()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001681 if (FT->getNumParams()) Out << ", ";
1682 Out << "..."; // Output varargs portion of signature!
Chris Lattner007377f2001-09-07 16:36:04 +00001683 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001684 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001685 Attributes FnAttrs = Attrs.getFnAttributes();
1686 if (FnAttrs != Attribute::None)
1687 Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes());
Chris Lattner60962db2005-11-12 00:10:19 +00001688 if (F->hasSection())
1689 Out << " section \"" << F->getSection() << '"';
Chris Lattner30caa282005-11-06 06:48:53 +00001690 if (F->getAlignment())
1691 Out << " align " << F->getAlignment();
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001692 if (F->hasGC())
1693 Out << " gc \"" << F->getGC() << '"';
Reid Spencer5cbf9852007-01-30 20:08:39 +00001694 if (F->isDeclaration()) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001695 Out << "\n";
Chris Lattner03e2acb2002-05-06 03:00:40 +00001696 } else {
Chris Lattnera1b58582008-04-21 06:12:55 +00001697 Out << " {";
Misha Brukmanfd939082005-04-21 23:48:37 +00001698
Chris Lattnerb5794002002-04-07 22:49:37 +00001699 // Output all of its basic blocks... for the function
Chris Lattner7e708292002-06-25 16:13:24 +00001700 for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
1701 printBasicBlock(I);
Chris Lattner007377f2001-09-07 16:36:04 +00001702
Misha Brukman0313e0b2004-06-21 21:53:56 +00001703 Out << "}\n";
Chris Lattner007377f2001-09-07 16:36:04 +00001704 }
1705
Reid Spencer0d1b77e2004-05-26 07:18:52 +00001706 Machine.purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +00001707}
1708
Misha Brukmanab5c6002004-03-02 00:22:19 +00001709/// printArgument - This member is called for every argument that is passed into
1710/// the function. Simply print it out
1711///
Daniel Dunbara279bc32009-09-20 02:20:51 +00001712void AssemblyWriter::printArgument(const Argument *Arg,
Devang Pateleaf42ab2008-09-23 23:03:40 +00001713 Attributes Attrs) {
Chris Lattner00950542001-06-06 20:29:01 +00001714 // Output type...
Chris Lattner0f7364b2009-02-28 21:26:53 +00001715 TypePrinter.print(Arg->getType(), Out);
Misha Brukmanfd939082005-04-21 23:48:37 +00001716
Duncan Sandsdc024672007-11-27 13:23:08 +00001717 // Output parameter attributes list
Devang Patel05988662008-09-25 21:00:45 +00001718 if (Attrs != Attribute::None)
1719 Out << ' ' << Attribute::getAsString(Attrs);
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001720
Chris Lattner00950542001-06-06 20:29:01 +00001721 // Output name, if available...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001722 if (Arg->hasName()) {
1723 Out << ' ';
1724 PrintLLVMName(Out, Arg);
1725 }
Chris Lattner00950542001-06-06 20:29:01 +00001726}
1727
Misha Brukmanab5c6002004-03-02 00:22:19 +00001728/// printBasicBlock - This member is called for each basic block in a method.
1729///
Chris Lattnerc1824992001-10-29 16:05:51 +00001730void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
Nick Lewycky280a6e62008-04-25 16:53:59 +00001731 if (BB->hasName()) { // Print out the label if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001732 Out << "\n";
Daniel Dunbar03d76512009-07-25 23:55:21 +00001733 PrintLLVMName(Out, BB->getName(), LabelPrefix);
Chris Lattnerc97536e2008-08-17 04:40:13 +00001734 Out << ':';
Nick Lewycky280a6e62008-04-25 16:53:59 +00001735 } else if (!BB->use_empty()) { // Don't print block # of no uses...
Chris Lattner96c5b2f2008-04-21 04:20:33 +00001736 Out << "\n; <label>:";
Chris Lattner22379bc2007-01-11 03:54:27 +00001737 int Slot = Machine.getLocalSlot(BB);
Chris Lattner69566452004-06-09 19:41:19 +00001738 if (Slot != -1)
Misha Brukman0313e0b2004-06-21 21:53:56 +00001739 Out << Slot;
Chris Lattner69566452004-06-09 19:41:19 +00001740 else
Misha Brukman0313e0b2004-06-21 21:53:56 +00001741 Out << "<badref>";
Chris Lattner061269b2002-10-02 19:38:55 +00001742 }
Chris Lattner4e4d8622003-11-20 00:09:43 +00001743
Dan Gohman683e9222009-08-12 17:23:50 +00001744 if (BB->getParent() == 0) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001745 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001746 Out << "; Error: Block without parent!";
1747 } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
Chris Lattnereb411292008-04-22 02:45:44 +00001748 // Output predecessors for the block...
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001749 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001750 Out << ";";
Chris Lattnereb411292008-04-22 02:45:44 +00001751 pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001752
Chris Lattnereb411292008-04-22 02:45:44 +00001753 if (PI == PE) {
1754 Out << " No predecessors!";
1755 } else {
Dan Gohman8dae1382008-09-14 17:21:12 +00001756 Out << " preds = ";
Chris Lattnereb411292008-04-22 02:45:44 +00001757 writeOperand(*PI, false);
1758 for (++PI; PI != PE; ++PI) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001759 Out << ", ";
Chris Lattner2fcfdb72006-12-06 06:24:27 +00001760 writeOperand(*PI, false);
Chris Lattner40efcec2003-11-16 22:59:57 +00001761 }
Chris Lattner061269b2002-10-02 19:38:55 +00001762 }
Chris Lattner00950542001-06-06 20:29:01 +00001763 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001764
Chris Lattnereb411292008-04-22 02:45:44 +00001765 Out << "\n";
Chris Lattner00950542001-06-06 20:29:01 +00001766
Misha Brukman0313e0b2004-06-21 21:53:56 +00001767 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001768
Chris Lattner007377f2001-09-07 16:36:04 +00001769 // Output all of the instructions in the basic block...
Dan Gohmanbeca6892009-07-13 18:27:59 +00001770 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
Chris Lattner7e708292002-06-25 16:13:24 +00001771 printInstruction(*I);
Dan Gohmanbeca6892009-07-13 18:27:59 +00001772 Out << '\n';
1773 }
Chris Lattner9f717ef2004-03-08 18:51:45 +00001774
Misha Brukman0313e0b2004-06-21 21:53:56 +00001775 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
Chris Lattner00950542001-06-06 20:29:01 +00001776}
1777
Chris Lattnere02fa852001-10-13 06:42:36 +00001778
Misha Brukmanab5c6002004-03-02 00:22:19 +00001779/// printInfoComment - Print a little comment after the instruction indicating
1780/// which slot it occupies.
1781///
Chris Lattner7e708292002-06-25 16:13:24 +00001782void AssemblyWriter::printInfoComment(const Value &V) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001783 if (V.getType() != Type::getVoidTy(V.getContext())) {
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001784 Out.PadToColumn(50);
Dan Gohman683e9222009-08-12 17:23:50 +00001785 Out << "; <";
Chris Lattner0f7364b2009-02-28 21:26:53 +00001786 TypePrinter.print(V.getType(), Out);
Dan Gohman9bf0b9b2009-08-12 23:54:22 +00001787 Out << "> [#uses=" << V.getNumUses() << ']'; // Output # uses
Chris Lattnere02fa852001-10-13 06:42:36 +00001788 }
1789}
1790
Reid Spencer3a9ec242006-08-28 01:02:49 +00001791// This member is called for each Instruction in a function..
Chris Lattner7e708292002-06-25 16:13:24 +00001792void AssemblyWriter::printInstruction(const Instruction &I) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001793 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
Chris Lattner95e5a2c2003-10-30 23:41:03 +00001794
Dan Gohman3845e502009-08-12 23:32:33 +00001795 // Print out indentation for an instruction.
Dan Gohman01889ca2009-08-13 01:41:52 +00001796 Out << " ";
Chris Lattner00950542001-06-06 20:29:01 +00001797
1798 // Print out name if it exists...
Chris Lattnerc97536e2008-08-17 04:40:13 +00001799 if (I.hasName()) {
1800 PrintLLVMName(Out, &I);
1801 Out << " = ";
Owen Anderson1d0be152009-08-13 21:58:54 +00001802 } else if (I.getType() != Type::getVoidTy(I.getContext())) {
Chris Lattner828db8a2008-08-29 17:19:30 +00001803 // Print out the def slot taken.
1804 int SlotNum = Machine.getLocalSlot(&I);
1805 if (SlotNum == -1)
1806 Out << "<badref> = ";
1807 else
1808 Out << '%' << SlotNum << " = ";
Chris Lattnerc97536e2008-08-17 04:40:13 +00001809 }
Chris Lattner00950542001-06-06 20:29:01 +00001810
Chris Lattnerddb6db42005-05-06 05:51:46 +00001811 // If this is a volatile load or store, print out the volatile marker.
Chris Lattnere5e475e2003-09-08 17:45:59 +00001812 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
Chris Lattnerddb6db42005-05-06 05:51:46 +00001813 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001814 Out << "volatile ";
Chris Lattnerddb6db42005-05-06 05:51:46 +00001815 } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) {
1816 // If this is a call, check if it's a tail call.
1817 Out << "tail ";
1818 }
Chris Lattnere5e475e2003-09-08 17:45:59 +00001819
Chris Lattner00950542001-06-06 20:29:01 +00001820 // Print out the opcode...
Misha Brukman0313e0b2004-06-21 21:53:56 +00001821 Out << I.getOpcodeName();
Chris Lattner00950542001-06-06 20:29:01 +00001822
Dan Gohman59858cf2009-07-27 16:11:46 +00001823 // Print out optimization information.
1824 WriteOptimizationInfo(Out, &I);
1825
Reid Spencer74f16422006-12-03 06:27:29 +00001826 // Print out the compare instruction predicates
Nate Begemanac80ade2008-05-12 19:01:56 +00001827 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
Chris Lattnerab49ee72008-08-23 22:52:27 +00001828 Out << ' ' << getPredicateText(CI->getPredicate());
Reid Spencer74f16422006-12-03 06:27:29 +00001829
Chris Lattner00950542001-06-06 20:29:01 +00001830 // Print out the type of the operands...
Chris Lattner7e708292002-06-25 16:13:24 +00001831 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0;
Chris Lattner00950542001-06-06 20:29:01 +00001832
1833 // Special case conditional branches to swizzle the condition out to the front
Gabor Greifccd27fb2009-02-09 15:45:06 +00001834 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
1835 BranchInst &BI(cast<BranchInst>(I));
Dan Gohman8dae1382008-09-14 17:21:12 +00001836 Out << ' ';
Gabor Greifccd27fb2009-02-09 15:45:06 +00001837 writeOperand(BI.getCondition(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001838 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001839 writeOperand(BI.getSuccessor(0), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001840 Out << ", ";
Gabor Greifccd27fb2009-02-09 15:45:06 +00001841 writeOperand(BI.getSuccessor(1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001842
Chris Lattner94dc1f22002-04-13 18:34:38 +00001843 } else if (isa<SwitchInst>(I)) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001844 // Special case switch instruction to get formatting nice and correct.
Dan Gohman8dae1382008-09-14 17:21:12 +00001845 Out << ' ';
Chris Lattnerab49ee72008-08-23 22:52:27 +00001846 writeOperand(Operand , true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001847 Out << ", ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001848 writeOperand(I.getOperand(1), true);
1849 Out << " [";
Chris Lattner00950542001-06-06 20:29:01 +00001850
Chris Lattner7e708292002-06-25 16:13:24 +00001851 for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) {
Dan Gohman01889ca2009-08-13 01:41:52 +00001852 Out << "\n ";
Chris Lattnerab49ee72008-08-23 22:52:27 +00001853 writeOperand(I.getOperand(op ), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001854 Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00001855 writeOperand(I.getOperand(op+1), true);
Chris Lattner00950542001-06-06 20:29:01 +00001856 }
Dan Gohman01889ca2009-08-13 01:41:52 +00001857 Out << "\n ]";
Chris Lattnerab21db72009-10-28 00:19:10 +00001858 } else if (isa<IndirectBrInst>(I)) {
1859 // Special case indirectbr instruction to get formatting nice and correct.
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001860 Out << ' ';
1861 writeOperand(Operand, true);
Dan Gohman0ed1f422009-10-30 02:01:10 +00001862 Out << ", [";
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001863
1864 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1865 if (i != 1)
1866 Out << ", ";
1867 writeOperand(I.getOperand(i), true);
1868 }
1869 Out << ']';
Chris Lattnerb00c5822001-10-02 03:41:24 +00001870 } else if (isa<PHINode>(I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001871 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001872 TypePrinter.print(I.getType(), Out);
Misha Brukman0313e0b2004-06-21 21:53:56 +00001873 Out << ' ';
Chris Lattner00950542001-06-06 20:29:01 +00001874
Chris Lattner7e708292002-06-25 16:13:24 +00001875 for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001876 if (op) Out << ", ";
Dan Gohman8dae1382008-09-14 17:21:12 +00001877 Out << "[ ";
1878 writeOperand(I.getOperand(op ), false); Out << ", ";
Misha Brukman0313e0b2004-06-21 21:53:56 +00001879 writeOperand(I.getOperand(op+1), false); Out << " ]";
Chris Lattnerc24d2082001-06-11 15:04:20 +00001880 }
Dan Gohman995be7d2008-05-31 19:12:39 +00001881 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001882 Out << ' ';
Dan Gohman995be7d2008-05-31 19:12:39 +00001883 writeOperand(I.getOperand(0), true);
1884 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1885 Out << ", " << *i;
1886 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001887 Out << ' ';
1888 writeOperand(I.getOperand(0), true); Out << ", ";
Dan Gohman995be7d2008-05-31 19:12:39 +00001889 writeOperand(I.getOperand(1), true);
1890 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1891 Out << ", " << *i;
Devang Patel57ef4f42008-02-23 00:35:18 +00001892 } else if (isa<ReturnInst>(I) && !Operand) {
1893 Out << " void";
Chris Lattnerd5118982005-05-06 20:26:43 +00001894 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
1895 // Print the calling convention being used.
1896 switch (CI->getCallingConv()) {
1897 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001898 case CallingConv::Fast: Out << " fastcc"; break;
1899 case CallingConv::Cold: Out << " coldcc"; break;
Chris Lattnerb28a6dc2007-11-18 18:32:16 +00001900 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001901 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
1902 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1903 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1904 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001905 default: Out << " cc" << CI->getCallingConv(); break;
1906 }
1907
Reid Spencerb138a062007-04-09 06:10:42 +00001908 const PointerType *PTy = cast<PointerType>(Operand->getType());
1909 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1910 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001911 const AttrListPtr &PAL = CI->getAttributes();
Chris Lattner268de042001-11-06 21:28:12 +00001912
Devang Patel652203f2008-09-29 20:49:50 +00001913 if (PAL.getRetAttributes() != Attribute::None)
1914 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1915
Chris Lattner7a012292003-08-05 15:34:45 +00001916 // If possible, print out the short form of the call instruction. We can
Chris Lattnerb5794002002-04-07 22:49:37 +00001917 // only do this if the first argument is a pointer to a nonvararg function,
Chris Lattner7a012292003-08-05 15:34:45 +00001918 // and if the return type is not a pointer to a function.
Chris Lattner268de042001-11-06 21:28:12 +00001919 //
Dan Gohman8dae1382008-09-14 17:21:12 +00001920 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001921 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001922 (!isa<PointerType>(RetTy) ||
Chris Lattnerc1b27182002-07-25 20:58:51 +00001923 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001924 TypePrinter.print(RetTy, Out);
Dan Gohman8dae1382008-09-14 17:21:12 +00001925 Out << ' ';
Chris Lattner268de042001-11-06 21:28:12 +00001926 writeOperand(Operand, false);
1927 } else {
1928 writeOperand(Operand, true);
1929 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001930 Out << '(';
Reid Spencerbd5db8e2006-12-31 05:24:50 +00001931 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1932 if (op > 1)
Dan Gohman8dae1382008-09-14 17:21:12 +00001933 Out << ", ";
Devang Patel19c87462008-09-26 22:53:05 +00001934 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
Chris Lattner00950542001-06-06 20:29:01 +00001935 }
Dan Gohman8dae1382008-09-14 17:21:12 +00001936 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001937 if (PAL.getFnAttributes() != Attribute::None)
1938 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
Chris Lattner7e708292002-06-25 16:13:24 +00001939 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Reid Spencerb138a062007-04-09 06:10:42 +00001940 const PointerType *PTy = cast<PointerType>(Operand->getType());
1941 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1942 const Type *RetTy = FTy->getReturnType();
Devang Patel05988662008-09-25 21:00:45 +00001943 const AttrListPtr &PAL = II->getAttributes();
Chris Lattner7a012292003-08-05 15:34:45 +00001944
Chris Lattnerd5118982005-05-06 20:26:43 +00001945 // Print the calling convention being used.
1946 switch (II->getCallingConv()) {
1947 case CallingConv::C: break; // default
Chris Lattner0deaab82006-05-19 21:58:52 +00001948 case CallingConv::Fast: Out << " fastcc"; break;
1949 case CallingConv::Cold: Out << " coldcc"; break;
Dan Gohman8dae1382008-09-14 17:21:12 +00001950 case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
1951 case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001952 case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
1953 case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
1954 case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
Chris Lattnerd5118982005-05-06 20:26:43 +00001955 default: Out << " cc" << II->getCallingConv(); break;
1956 }
1957
Devang Patel652203f2008-09-29 20:49:50 +00001958 if (PAL.getRetAttributes() != Attribute::None)
1959 Out << ' ' << Attribute::getAsString(PAL.getRetAttributes());
1960
Chris Lattner7a012292003-08-05 15:34:45 +00001961 // If possible, print out the short form of the invoke instruction. We can
1962 // only do this if the first argument is a pointer to a nonvararg function,
1963 // and if the return type is not a pointer to a function.
1964 //
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001965 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001966 if (!FTy->isVarArg() &&
Misha Brukmanfd939082005-04-21 23:48:37 +00001967 (!isa<PointerType>(RetTy) ||
Chris Lattner7a012292003-08-05 15:34:45 +00001968 !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
Chris Lattner0f7364b2009-02-28 21:26:53 +00001969 TypePrinter.print(RetTy, Out);
Dan Gohman2b6c3d92008-10-15 18:02:08 +00001970 Out << ' ';
Chris Lattner7a012292003-08-05 15:34:45 +00001971 writeOperand(Operand, false);
1972 } else {
1973 writeOperand(Operand, true);
1974 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00001975 Out << '(';
Gabor Greif03a5f132009-09-03 02:02:59 +00001976 for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) {
1977 if (op > 3)
Dan Gohman8dae1382008-09-14 17:21:12 +00001978 Out << ", ";
Gabor Greif03a5f132009-09-03 02:02:59 +00001979 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2));
Chris Lattnere02fa852001-10-13 06:42:36 +00001980 }
1981
Dan Gohman8dae1382008-09-14 17:21:12 +00001982 Out << ')';
Devang Patel19c87462008-09-26 22:53:05 +00001983 if (PAL.getFnAttributes() != Attribute::None)
1984 Out << ' ' << Attribute::getAsString(PAL.getFnAttributes());
1985
Dan Gohman01889ca2009-08-13 01:41:52 +00001986 Out << "\n to ";
Chris Lattnere02fa852001-10-13 06:42:36 +00001987 writeOperand(II->getNormalDest(), true);
Dan Gohman8dae1382008-09-14 17:21:12 +00001988 Out << " unwind ";
Chris Lattneraeb2a1d2004-02-08 21:44:31 +00001989 writeOperand(II->getUnwindDest(), true);
Chris Lattnere02fa852001-10-13 06:42:36 +00001990
Victor Hernandez7b929da2009-10-23 21:09:37 +00001991 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00001992 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00001993 TypePrinter.print(AI->getType()->getElementType(), Out);
Dan Gohman69bff072009-07-31 18:23:24 +00001994 if (!AI->getArraySize() || AI->isArrayAllocation()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001995 Out << ", ";
Chris Lattner94dc1f22002-04-13 18:34:38 +00001996 writeOperand(AI->getArraySize(), true);
Chris Lattner00950542001-06-06 20:29:01 +00001997 }
Nate Begeman14b05292005-11-05 09:21:28 +00001998 if (AI->getAlignment()) {
Chris Lattner9fad0b92005-11-05 21:20:34 +00001999 Out << ", align " << AI->getAlignment();
Nate Begeman14b05292005-11-05 09:21:28 +00002000 }
Chris Lattnere02fa852001-10-13 06:42:36 +00002001 } else if (isa<CastInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002002 if (Operand) {
2003 Out << ' ';
2004 writeOperand(Operand, true); // Work with broken code
2005 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00002006 Out << " to ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00002007 TypePrinter.print(I.getType(), Out);
Chris Lattner4d45bd02003-10-18 05:57:43 +00002008 } else if (isa<VAArgInst>(I)) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002009 if (Operand) {
2010 Out << ' ';
2011 writeOperand(Operand, true); // Work with broken code
2012 }
Misha Brukman0313e0b2004-06-21 21:53:56 +00002013 Out << ", ";
Chris Lattner0f7364b2009-02-28 21:26:53 +00002014 TypePrinter.print(I.getType(), Out);
2015 } else if (Operand) { // Print the normal way.
Chris Lattner00950542001-06-06 20:29:01 +00002016
Misha Brukmanfd939082005-04-21 23:48:37 +00002017 // PrintAllTypes - Instructions who have operands of all the same type
Chris Lattner00950542001-06-06 20:29:01 +00002018 // omit the type from all but the first operand. If the instruction has
2019 // different type operands (for example br), then they are all printed.
2020 bool PrintAllTypes = false;
2021 const Type *TheType = Operand->getType();
Chris Lattner00950542001-06-06 20:29:01 +00002022
Reid Spencerebe57e32007-02-02 13:54:55 +00002023 // Select, Store and ShuffleVector always print all types.
Devang Patel64947682008-03-04 22:05:14 +00002024 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
2025 || isa<ReturnInst>(I)) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00002026 PrintAllTypes = true;
2027 } else {
2028 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
2029 Operand = I.getOperand(i);
Nuno Lopes6ad2b2a2009-01-15 18:40:57 +00002030 // note that Operand shouldn't be null, but the test helps make dump()
2031 // more tolerant of malformed IR
Nuno Lopesa8c78a92009-01-14 17:51:41 +00002032 if (Operand && Operand->getType() != TheType) {
Chris Lattnerffd9bf42003-04-16 20:20:02 +00002033 PrintAllTypes = true; // We have differing types! Print them all!
2034 break;
2035 }
Chris Lattner00950542001-06-06 20:29:01 +00002036 }
2037 }
Misha Brukmanfd939082005-04-21 23:48:37 +00002038
Chris Lattnerc1824992001-10-29 16:05:51 +00002039 if (!PrintAllTypes) {
Misha Brukman0313e0b2004-06-21 21:53:56 +00002040 Out << ' ';
Chris Lattner0f7364b2009-02-28 21:26:53 +00002041 TypePrinter.print(TheType, Out);
Chris Lattnerc1824992001-10-29 16:05:51 +00002042 }
Chris Lattner00950542001-06-06 20:29:01 +00002043
Dan Gohman8dae1382008-09-14 17:21:12 +00002044 Out << ' ';
Chris Lattner7e708292002-06-25 16:13:24 +00002045 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
Dan Gohman8dae1382008-09-14 17:21:12 +00002046 if (i) Out << ", ";
Chris Lattner7e708292002-06-25 16:13:24 +00002047 writeOperand(I.getOperand(i), PrintAllTypes);
Chris Lattner00950542001-06-06 20:29:01 +00002048 }
2049 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002050
Christopher Lamb43c7f372007-04-22 19:24:39 +00002051 // Print post operand alignment for load/store
2052 if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) {
2053 Out << ", align " << cast<LoadInst>(I).getAlignment();
2054 } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) {
2055 Out << ", align " << cast<StoreInst>(I).getAlignment();
2056 }
Chris Lattner00950542001-06-06 20:29:01 +00002057
Devang Patel18f0c262009-09-28 20:56:00 +00002058 // Print Metadata info
Devang Patel7f93f4d2009-10-07 16:37:55 +00002059 if (!MDNames.empty()) {
2060 MetadataContext &TheMetadata = I.getContext().getMetadata();
Devang Patelf61b2372009-10-22 18:55:16 +00002061 typedef SmallVector<std::pair<unsigned, TrackingVH<MDNode> >, 2> MDMapTy;
2062 MDMapTy MDs;
2063 TheMetadata.getMDs(&I, MDs);
2064 for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME;
2065 ++MI)
2066 Out << ", !" << MDNames[MI->first]
2067 << " !" << Machine.getMetadataSlot(MI->second);
Devang Patel7f93f4d2009-10-07 16:37:55 +00002068 }
Chris Lattnere02fa852001-10-13 06:42:36 +00002069 printInfoComment(I);
Chris Lattner00950542001-06-06 20:29:01 +00002070}
2071
2072
2073//===----------------------------------------------------------------------===//
2074// External Interface declarations
2075//===----------------------------------------------------------------------===//
2076
Dan Gohman683e9222009-08-12 17:23:50 +00002077void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner0d9574a2008-08-19 04:26:57 +00002078 SlotTracker SlotTable(this);
Dan Gohman683e9222009-08-12 17:23:50 +00002079 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002080 AssemblyWriter W(OS, SlotTable, this, AAW);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002081 W.write(this);
Chris Lattner00950542001-06-06 20:29:01 +00002082}
2083
Chris Lattner6d4306e2009-02-28 21:11:05 +00002084void Type::print(raw_ostream &OS) const {
2085 if (this == 0) {
2086 OS << "<null Type>";
2087 return;
2088 }
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002089 TypePrinting().print(this, OS);
Chris Lattner75cf7cf2002-04-08 22:03:40 +00002090}
2091
Dan Gohman683e9222009-08-12 17:23:50 +00002092void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
Chris Lattner944fac72008-08-23 22:23:09 +00002093 if (this == 0) {
Dan Gohman1220e102009-08-12 20:56:03 +00002094 ROS << "printing a <null> value\n";
Chris Lattner944fac72008-08-23 22:23:09 +00002095 return;
2096 }
Dan Gohman1220e102009-08-12 20:56:03 +00002097 formatted_raw_ostream OS(ROS);
Chris Lattner944fac72008-08-23 22:23:09 +00002098 if (const Instruction *I = dyn_cast<Instruction>(this)) {
2099 const Function *F = I->getParent() ? I->getParent()->getParent() : 0;
2100 SlotTracker SlotTable(F);
2101 AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW);
2102 W.write(I);
2103 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
2104 SlotTracker SlotTable(BB->getParent());
2105 AssemblyWriter W(OS, SlotTable,
2106 BB->getParent() ? BB->getParent()->getParent() : 0, AAW);
2107 W.write(BB);
2108 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
2109 SlotTracker SlotTable(GV->getParent());
Dan Gohmanba0941f2009-04-20 16:10:33 +00002110 AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
Chris Lattner944fac72008-08-23 22:23:09 +00002111 W.write(GV);
Devang Patele54abc92009-07-22 17:43:22 +00002112 } else if (const MDString *MDS = dyn_cast<MDString>(this)) {
2113 TypePrinting TypePrinter;
2114 TypePrinter.print(MDS->getType(), OS);
2115 OS << ' ';
2116 OS << "!\"";
Daniel Dunbar03d76512009-07-25 23:55:21 +00002117 PrintEscapedString(MDS->getString(), OS);
Devang Patele54abc92009-07-22 17:43:22 +00002118 OS << '"';
Devang Patelfcd65ae2009-07-01 20:59:15 +00002119 } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
Devang Patel320671d2009-07-08 21:44:25 +00002120 SlotTracker SlotTable(N);
Devang Patelfcd65ae2009-07-01 20:59:15 +00002121 TypePrinting TypePrinter;
Devang Patel320671d2009-07-08 21:44:25 +00002122 SlotTable.initialize();
2123 WriteMDNodes(OS, TypePrinter, SlotTable);
Devang Patelc29d5b32009-07-30 00:02:57 +00002124 } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) {
2125 SlotTracker SlotTable(N);
2126 TypePrinting TypePrinter;
2127 SlotTable.initialize();
2128 OS << "!" << N->getName() << " = !{";
2129 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
2130 if (i) OS << ", ";
Devang Patele8861b82009-07-30 01:02:04 +00002131 MDNode *MD = dyn_cast_or_null<MDNode>(N->getElement(i));
2132 if (MD)
2133 OS << '!' << SlotTable.getMetadataSlot(MD);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002134 else
Devang Patele8861b82009-07-30 01:02:04 +00002135 OS << "null";
Devang Patelc29d5b32009-07-30 00:02:57 +00002136 }
2137 OS << "}\n";
2138 WriteMDNodes(OS, TypePrinter, SlotTable);
Chris Lattner944fac72008-08-23 22:23:09 +00002139 } else if (const Constant *C = dyn_cast<Constant>(this)) {
Chris Lattnere9fa33e2009-02-28 23:20:19 +00002140 TypePrinting TypePrinter;
Chris Lattner0f7364b2009-02-28 21:26:53 +00002141 TypePrinter.print(C->getType(), OS);
Chris Lattner6d4306e2009-02-28 21:11:05 +00002142 OS << ' ';
Chris Lattner9cc34462009-02-28 20:25:14 +00002143 WriteConstantInt(OS, C, TypePrinter, 0);
Chris Lattner944fac72008-08-23 22:23:09 +00002144 } else if (const Argument *A = dyn_cast<Argument>(this)) {
2145 WriteAsOperand(OS, this, true,
2146 A->getParent() ? A->getParent()->getParent() : 0);
2147 } else if (isa<InlineAsm>(this)) {
2148 WriteAsOperand(OS, this, true, 0);
2149 } else {
Dan Gohmancd26ec52009-09-23 01:33:16 +00002150 // Otherwise we don't know what it is. Call the virtual function to
2151 // allow a subclass to print itself.
2152 printCustom(OS);
Chris Lattner944fac72008-08-23 22:23:09 +00002153 }
2154}
2155
Dan Gohmancd26ec52009-09-23 01:33:16 +00002156// Value::printCustom - subclasses should override this to implement printing.
2157void Value::printCustom(raw_ostream &OS) const {
2158 llvm_unreachable("Unknown value to print out!");
2159}
2160
Chris Lattner7059e532008-08-25 17:03:15 +00002161// Value::dump - allow easy printing of Values from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00002162void Value::dump() const { print(errs()); errs() << '\n'; }
Reid Spencerfa452c02004-05-25 18:14:38 +00002163
Chris Lattner7059e532008-08-25 17:03:15 +00002164// Type::dump - allow easy printing of Types from the debugger.
Chris Lattner795daec2008-10-01 20:16:19 +00002165// This one uses type names from the given context module
2166void Type::dump(const Module *Context) const {
2167 WriteTypeSymbolic(errs(), this, Context);
2168 errs() << '\n';
Chris Lattner795daec2008-10-01 20:16:19 +00002169}
2170
Chris Lattnerc2871372009-02-28 21:05:51 +00002171// Type::dump - allow easy printing of Types from the debugger.
2172void Type::dump() const { dump(0); }
2173
Chris Lattner7059e532008-08-25 17:03:15 +00002174// Module::dump() - Allow printing of Modules from the debugger.
Dan Gohmanf871ccb2009-03-23 15:57:19 +00002175void Module::dump() const { print(errs(), 0); }