Chris Lattner | 8da78af | 2002-04-07 22:31:46 +0000 | [diff] [blame] | 1 | //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This library implements the functionality defined in llvm/Assembly/Writer.h |
| 11 | // |
Chris Lattner | 02b9399 | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 12 | // Note that these routines must be extremely tolerant of various errors in the |
Chris Lattner | 8f77dae | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 13 | // LLVM code, because it can be used for debugging transformations. |
Chris Lattner | 02b9399 | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | f082b80 | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/PrintModulePass.h" |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 19 | #include "llvm/Assembly/AsmAnnotationWriter.h" |
Erick Tryzelaar | f3d5c91 | 2010-03-02 05:32:52 +0000 | [diff] [blame] | 20 | #include "llvm/LLVMContext.h" |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 21 | #include "llvm/CallingConv.h" |
Chris Lattner | f2d577b | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Chris Lattner | 3eb59c0 | 2002-04-29 18:46:50 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 863517a | 2006-01-25 18:57:27 +0000 | [diff] [blame] | 24 | #include "llvm/InlineAsm.h" |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 25 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 26 | #include "llvm/Operator.h" |
Chris Lattner | f2d577b | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 28 | #include "llvm/ValueSymbolTable.h" |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 29 | #include "llvm/TypeSymbolTable.h" |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DenseSet.h" |
Benjamin Kramer | 5908839 | 2010-01-29 14:42:22 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringExtras.h" |
| 33 | #include "llvm/ADT/STLExtras.h" |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CFG.h" |
David Greene | d865e02 | 2010-01-05 01:29:26 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Devang Patel | 2d5988d | 2009-09-30 20:16:54 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Dwarf.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ErrorHandling.h" |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 39 | #include "llvm/Support/FormattedStream.h" |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 40 | #include <algorithm> |
Reid Spencer | 4ad513c | 2007-05-22 19:27:35 +0000 | [diff] [blame] | 41 | #include <cctype> |
Devang Patel | 923078c | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 42 | #include <map> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 43 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 44 | |
Reid Spencer | edd5d9e | 2005-05-15 16:13:11 +0000 | [diff] [blame] | 45 | // Make virtual table appear in this compilation unit. |
| 46 | AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {} |
| 47 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // Helper Functions |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
| 52 | static const Module *getModuleFromVal(const Value *V) { |
| 53 | if (const Argument *MA = dyn_cast<Argument>(V)) |
| 54 | return MA->getParent() ? MA->getParent()->getParent() : 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 56 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 57 | return BB->getParent() ? BB->getParent()->getParent() : 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 59 | 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 | } |
Chris Lattner | 4a3d3a5 | 2009-12-31 01:41:14 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 64 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 65 | return GV->getParent(); |
Chris Lattner | 4a3d3a5 | 2009-12-31 01:41:14 +0000 | [diff] [blame] | 66 | if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V)) |
| 67 | return NMD->getParent(); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
Daniel Dunbar | e9da133 | 2008-10-28 19:33:02 +0000 | [diff] [blame] | 71 | // PrintEscapedString - Print each character of the specified string, escaping |
| 72 | // it if it is not printable or if it is an escape char. |
Chris Lattner | 8fff126 | 2010-07-07 23:16:37 +0000 | [diff] [blame] | 73 | static void PrintEscapedString(StringRef Name, raw_ostream &Out) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 74 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
| 75 | unsigned char C = Name[i]; |
Nick Lewycky | 34a4086 | 2009-03-15 06:39:52 +0000 | [diff] [blame] | 76 | if (isprint(C) && C != '\\' && C != '"') |
Daniel Dunbar | e9da133 | 2008-10-28 19:33:02 +0000 | [diff] [blame] | 77 | Out << C; |
| 78 | else |
| 79 | Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); |
| 80 | } |
| 81 | } |
| 82 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 83 | enum PrefixType { |
| 84 | GlobalPrefix, |
| 85 | LabelPrefix, |
Daniel Dunbar | cad3580 | 2008-10-14 23:28:09 +0000 | [diff] [blame] | 86 | LocalPrefix, |
| 87 | NoPrefix |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either |
| 91 | /// prefixed with % (if the string only contains simple characters) or is |
| 92 | /// surrounded with ""'s (if it has special chars in it). Print it out. |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame^] | 93 | static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 94 | assert(Name.data() && "Cannot get empty name!"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 95 | switch (Prefix) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 96 | default: llvm_unreachable("Bad prefix!"); |
Daniel Dunbar | cad3580 | 2008-10-14 23:28:09 +0000 | [diff] [blame] | 97 | case NoPrefix: break; |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 98 | case GlobalPrefix: OS << '@'; break; |
| 99 | case LabelPrefix: break; |
| 100 | case LocalPrefix: OS << '%'; break; |
Nick Lewycky | 0423483 | 2009-03-19 06:31:22 +0000 | [diff] [blame] | 101 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 103 | // Scan the name to see if it needs quotes first. |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 104 | bool NeedsQuotes = isdigit(Name[0]); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 105 | if (!NeedsQuotes) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 106 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
| 107 | char C = Name[i]; |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 108 | if (!isalnum(C) && C != '-' && C != '.' && C != '_') { |
| 109 | NeedsQuotes = true; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 115 | // If we didn't need any quotes, just write out the name in one blast. |
| 116 | if (!NeedsQuotes) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 117 | OS << Name; |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 121 | // Okay, we need quotes. Output the quotes and escape any scary characters as |
| 122 | // needed. |
| 123 | OS << '"'; |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 124 | PrintEscapedString(Name, OS); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 125 | 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 Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 131 | static void PrintLLVMName(raw_ostream &OS, const Value *V) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 132 | PrintLLVMName(OS, V->getName(), |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 133 | isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix); |
| 134 | } |
| 135 | |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 136 | //===----------------------------------------------------------------------===// |
| 137 | // TypePrinting Class: Type printing machinery |
| 138 | //===----------------------------------------------------------------------===// |
| 139 | |
Chris Lattner | 87185e8 | 2009-02-28 23:03:55 +0000 | [diff] [blame] | 140 | static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) { |
| 141 | return *static_cast<DenseMap<const Type *, std::string>*>(M); |
Chris Lattner | d8030a7 | 2009-02-28 22:34:45 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void TypePrinting::clear() { |
| 145 | getTypeNamesMap(TypeNames).clear(); |
| 146 | } |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 147 | |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 148 | bool TypePrinting::hasTypeName(const Type *Ty) const { |
| 149 | return getTypeNamesMap(TypeNames).count(Ty); |
| 150 | } |
| 151 | |
| 152 | void TypePrinting::addTypeName(const Type *Ty, const std::string &N) { |
| 153 | getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N)); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | TypePrinting::TypePrinting() { |
Chris Lattner | 87185e8 | 2009-02-28 23:03:55 +0000 | [diff] [blame] | 158 | TypeNames = new DenseMap<const Type *, std::string>(); |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Chris Lattner | d8030a7 | 2009-02-28 22:34:45 +0000 | [diff] [blame] | 161 | TypePrinting::~TypePrinting() { |
| 162 | delete &getTypeNamesMap(TypeNames); |
| 163 | } |
| 164 | |
Chris Lattner | 534361e | 2009-02-28 20:49:40 +0000 | [diff] [blame] | 165 | /// 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. |
| 167 | void TypePrinting::CalcTypeName(const Type *Ty, |
Chris Lattner | b840cac | 2009-02-28 20:34:19 +0000 | [diff] [blame] | 168 | SmallVectorImpl<const Type *> &TypeStack, |
Chris Lattner | d2b6cb0 | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 169 | raw_ostream &OS, bool IgnoreTopLevelName) { |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 170 | // Check to see if the type is named. |
Chris Lattner | d2b6cb0 | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 171 | if (!IgnoreTopLevelName) { |
Nick Lewycky | 0423483 | 2009-03-19 06:31:22 +0000 | [diff] [blame] | 172 | DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames); |
Chris Lattner | d2b6cb0 | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 173 | DenseMap<const Type *, std::string>::iterator I = TM.find(Ty); |
| 174 | if (I != TM.end()) { |
| 175 | OS << I->second; |
| 176 | return; |
| 177 | } |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 178 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 180 | // 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 Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 184 | // 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 Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 188 | OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 189 | return; |
| 190 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 192 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 194 | switch (Ty->getTypeID()) { |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 195 | 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 Lewycky | 7a0370f | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 202 | case Type::MetadataTyID: OS << "metadata"; break; |
Chris Lattner | 583ffd8 | 2009-02-28 21:18:43 +0000 | [diff] [blame] | 203 | case Type::IntegerTyID: |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 204 | OS << 'i' << cast<IntegerType>(Ty)->getBitWidth(); |
Chris Lattner | 583ffd8 | 2009-02-28 21:18:43 +0000 | [diff] [blame] | 205 | break; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 207 | case Type::FunctionTyID: { |
| 208 | const FunctionType *FTy = cast<FunctionType>(Ty); |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 209 | CalcTypeName(FTy->getReturnType(), TypeStack, OS); |
| 210 | OS << " ("; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 211 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 212 | E = FTy->param_end(); I != E; ++I) { |
| 213 | if (I != FTy->param_begin()) |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 214 | OS << ", "; |
| 215 | CalcTypeName(*I, TypeStack, OS); |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 217 | if (FTy->isVarArg()) { |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 218 | if (FTy->getNumParams()) OS << ", "; |
| 219 | OS << "..."; |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 221 | OS << ')'; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | case Type::StructTyID: { |
| 225 | const StructType *STy = cast<StructType>(Ty); |
| 226 | if (STy->isPacked()) |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 227 | OS << '<'; |
Dan Gohman | 29e13e0 | 2010-04-08 18:03:05 +0000 | [diff] [blame] | 228 | OS << '{'; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 229 | for (StructType::element_iterator I = STy->element_begin(), |
| 230 | E = STy->element_end(); I != E; ++I) { |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 231 | OS << ' '; |
Dan Gohman | 29e13e0 | 2010-04-08 18:03:05 +0000 | [diff] [blame] | 232 | CalcTypeName(*I, TypeStack, OS); |
| 233 | if (next(I) == STy->element_end()) |
| 234 | OS << ' '; |
| 235 | else |
| 236 | OS << ','; |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 238 | OS << '}'; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 239 | if (STy->isPacked()) |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 240 | OS << '>'; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 241 | break; |
| 242 | } |
Chris Lattner | fdfeb69 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 243 | case Type::UnionTyID: { |
| 244 | const UnionType *UTy = cast<UnionType>(Ty); |
Dan Gohman | 29e13e0 | 2010-04-08 18:03:05 +0000 | [diff] [blame] | 245 | OS << "union {"; |
Chris Lattner | fdfeb69 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 246 | for (StructType::element_iterator I = UTy->element_begin(), |
| 247 | E = UTy->element_end(); I != E; ++I) { |
Chris Lattner | fdfeb69 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 248 | OS << ' '; |
Dan Gohman | 29e13e0 | 2010-04-08 18:03:05 +0000 | [diff] [blame] | 249 | CalcTypeName(*I, TypeStack, OS); |
| 250 | if (next(I) == UTy->element_end()) |
| 251 | OS << ' '; |
| 252 | else |
| 253 | OS << ','; |
Chris Lattner | fdfeb69 | 2010-02-12 20:49:41 +0000 | [diff] [blame] | 254 | } |
| 255 | OS << '}'; |
| 256 | break; |
| 257 | } |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 258 | case Type::PointerTyID: { |
| 259 | const PointerType *PTy = cast<PointerType>(Ty); |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 260 | CalcTypeName(PTy->getElementType(), TypeStack, OS); |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 261 | if (unsigned AddressSpace = PTy->getAddressSpace()) |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 262 | OS << " addrspace(" << AddressSpace << ')'; |
| 263 | OS << '*'; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | case Type::ArrayTyID: { |
| 267 | const ArrayType *ATy = cast<ArrayType>(Ty); |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 268 | OS << '[' << ATy->getNumElements() << " x "; |
| 269 | CalcTypeName(ATy->getElementType(), TypeStack, OS); |
| 270 | OS << ']'; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | case Type::VectorTyID: { |
| 274 | const VectorType *PTy = cast<VectorType>(Ty); |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 275 | OS << "<" << PTy->getNumElements() << " x "; |
| 276 | CalcTypeName(PTy->getElementType(), TypeStack, OS); |
| 277 | OS << '>'; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | case Type::OpaqueTyID: |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 281 | OS << "opaque"; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 282 | break; |
| 283 | default: |
Chris Lattner | 3079426 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 284 | OS << "<unrecognized-type>"; |
Chris Lattner | 36942d7 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 285 | break; |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 286 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 534361e | 2009-02-28 20:49:40 +0000 | [diff] [blame] | 288 | TypeStack.pop_back(); // Remove self from stack. |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /// printTypeInt - The internal guts of printing out a type that has a |
| 292 | /// potentially named portion. |
| 293 | /// |
Chris Lattner | d2b6cb0 | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 294 | void TypePrinting::print(const Type *Ty, raw_ostream &OS, |
| 295 | bool IgnoreTopLevelName) { |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 296 | // Check to see if the type is named. |
Chris Lattner | 87185e8 | 2009-02-28 23:03:55 +0000 | [diff] [blame] | 297 | DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames); |
Chris Lattner | d2b6cb0 | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 298 | if (!IgnoreTopLevelName) { |
| 299 | DenseMap<const Type*, std::string>::iterator I = TM.find(Ty); |
| 300 | if (I != TM.end()) { |
| 301 | OS << I->second; |
| 302 | return; |
| 303 | } |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 304 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 306 | // Otherwise we have a type that has not been named but is a derived type. |
| 307 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 308 | // names. |
Chris Lattner | b840cac | 2009-02-28 20:34:19 +0000 | [diff] [blame] | 309 | SmallVector<const Type *, 16> TypeStack; |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 310 | std::string TypeName; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 534361e | 2009-02-28 20:49:40 +0000 | [diff] [blame] | 312 | raw_string_ostream TypeOS(TypeName); |
Chris Lattner | d2b6cb0 | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 313 | CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName); |
Chris Lattner | 534361e | 2009-02-28 20:49:40 +0000 | [diff] [blame] | 314 | OS << TypeOS.str(); |
| 315 | |
| 316 | // Cache type name for later use. |
Chris Lattner | d2b6cb0 | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 317 | if (!IgnoreTopLevelName) |
| 318 | TM.insert(std::make_pair(Ty, TypeOS.str())); |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 321 | namespace { |
| 322 | class TypeFinder { |
| 323 | // To avoid walking constant expressions multiple times and other IR |
| 324 | // objects, we keep several helper maps. |
| 325 | DenseSet<const Value*> VisitedConstants; |
| 326 | DenseSet<const Type*> VisitedTypes; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 328 | TypePrinting &TP; |
| 329 | std::vector<const Type*> &NumberedTypes; |
| 330 | public: |
| 331 | TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes) |
| 332 | : TP(tp), NumberedTypes(numberedTypes) {} |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 334 | void Run(const Module &M) { |
Chris Lattner | 8848586 | 2009-03-01 00:32:33 +0000 | [diff] [blame] | 335 | // Get types from the type symbol table. This gets opaque types referened |
| 336 | // only through derived named types. |
| 337 | const TypeSymbolTable &ST = M.getTypeSymbolTable(); |
| 338 | for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end(); |
| 339 | TI != E; ++TI) |
| 340 | IncorporateType(TI->second); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 342 | // Get types from global variables. |
| 343 | for (Module::const_global_iterator I = M.global_begin(), |
| 344 | E = M.global_end(); I != E; ++I) { |
| 345 | IncorporateType(I->getType()); |
| 346 | if (I->hasInitializer()) |
| 347 | IncorporateValue(I->getInitializer()); |
| 348 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 350 | // Get types from aliases. |
| 351 | for (Module::const_alias_iterator I = M.alias_begin(), |
| 352 | E = M.alias_end(); I != E; ++I) { |
| 353 | IncorporateType(I->getType()); |
| 354 | IncorporateValue(I->getAliasee()); |
| 355 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 357 | // Get types from functions. |
| 358 | for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) { |
| 359 | IncorporateType(FI->getType()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 361 | for (Function::const_iterator BB = FI->begin(), E = FI->end(); |
| 362 | BB != E;++BB) |
| 363 | for (BasicBlock::const_iterator II = BB->begin(), |
| 364 | E = BB->end(); II != E; ++II) { |
| 365 | const Instruction &I = *II; |
| 366 | // Incorporate the type of the instruction and all its operands. |
| 367 | IncorporateType(I.getType()); |
| 368 | for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end(); |
| 369 | OI != OE; ++OI) |
| 370 | IncorporateValue(*OI); |
| 371 | } |
| 372 | } |
| 373 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 375 | private: |
| 376 | void IncorporateType(const Type *Ty) { |
| 377 | // Check to see if we're already visited this type. |
Chris Lattner | 8848586 | 2009-03-01 00:32:33 +0000 | [diff] [blame] | 378 | if (!VisitedTypes.insert(Ty).second) |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 379 | return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 381 | // If this is a structure or opaque type, add a name for the type. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 382 | if (((Ty->isStructTy() && cast<StructType>(Ty)->getNumElements()) |
Duncan Sands | 47c5188 | 2010-02-16 14:50:09 +0000 | [diff] [blame] | 383 | || Ty->isOpaqueTy()) && !TP.hasTypeName(Ty)) { |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 384 | TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size()))); |
| 385 | NumberedTypes.push_back(Ty); |
| 386 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 388 | // Recursively walk all contained types. |
| 389 | for (Type::subtype_iterator I = Ty->subtype_begin(), |
| 390 | E = Ty->subtype_end(); I != E; ++I) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 391 | IncorporateType(*I); |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 392 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 394 | /// IncorporateValue - This method is used to walk operand lists finding |
| 395 | /// types hiding in constant expressions and other operands that won't be |
| 396 | /// walked in other ways. GlobalValues, basic blocks, instructions, and |
| 397 | /// inst operands are all explicitly enumerated. |
| 398 | void IncorporateValue(const Value *V) { |
| 399 | if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 401 | // Already visited? |
| 402 | if (!VisitedConstants.insert(V).second) |
| 403 | return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 405 | // Check this type. |
| 406 | IncorporateType(V->getType()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 407 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 408 | // Look in operands for types. |
| 409 | const Constant *C = cast<Constant>(V); |
| 410 | for (Constant::const_op_iterator I = C->op_begin(), |
| 411 | E = C->op_end(); I != E;++I) |
| 412 | IncorporateValue(*I); |
| 413 | } |
| 414 | }; |
| 415 | } // end anonymous namespace |
| 416 | |
| 417 | |
| 418 | /// AddModuleTypesToPrinter - Add all of the symbolic type names for types in |
| 419 | /// the specified module to the TypePrinter and all numbered types to it and the |
| 420 | /// NumberedTypes table. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 421 | static void AddModuleTypesToPrinter(TypePrinting &TP, |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 422 | std::vector<const Type*> &NumberedTypes, |
| 423 | const Module *M) { |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 424 | if (M == 0) return; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 425 | |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 426 | // If the module has a symbol table, take all global types and stuff their |
| 427 | // names into the TypeNames map. |
| 428 | const TypeSymbolTable &ST = M->getTypeSymbolTable(); |
| 429 | for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end(); |
| 430 | TI != E; ++TI) { |
| 431 | const Type *Ty = cast<Type>(TI->second); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 432 | |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 433 | // As a heuristic, don't insert pointer to primitive types, because |
| 434 | // they are used too often to have a single useful name. |
| 435 | if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
| 436 | const Type *PETy = PTy->getElementType(); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 437 | if ((PETy->isPrimitiveType() || PETy->isIntegerTy()) && |
Duncan Sands | 47c5188 | 2010-02-16 14:50:09 +0000 | [diff] [blame] | 438 | !PETy->isOpaqueTy()) |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 439 | continue; |
| 440 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 441 | |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 442 | // Likewise don't insert primitives either. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 443 | if (Ty->isIntegerTy() || Ty->isPrimitiveType()) |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 444 | continue; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 445 | |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 446 | // Get the name as a string and insert it into TypeNames. |
| 447 | std::string NameStr; |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 448 | raw_string_ostream NameROS(NameStr); |
| 449 | formatted_raw_ostream NameOS(NameROS); |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 450 | PrintLLVMName(NameOS, TI->first, LocalPrefix); |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 451 | NameOS.flush(); |
| 452 | TP.addTypeName(Ty, NameStr); |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 453 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 454 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 455 | // Walk the entire module to find references to unnamed structure and opaque |
| 456 | // types. This is required for correctness by opaque types (because multiple |
| 457 | // uses of an unnamed opaque type needs to be referred to by the same ID) and |
| 458 | // it shrinks complex recursive structure types substantially in some cases. |
| 459 | TypeFinder(TP, NumberedTypes).Run(*M); |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 463 | /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic |
| 464 | /// type, iff there is an entry in the modules symbol table for the specified |
Chris Lattner | c287137 | 2009-02-28 21:05:51 +0000 | [diff] [blame] | 465 | /// type or one of it's component types. |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 466 | /// |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 467 | void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) { |
| 468 | TypePrinting Printer; |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 469 | std::vector<const Type*> NumberedTypes; |
| 470 | AddModuleTypesToPrinter(Printer, NumberedTypes, M); |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 471 | Printer.print(Ty, OS); |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 474 | //===----------------------------------------------------------------------===// |
| 475 | // SlotTracker Class: Enumerate slot numbers for unnamed values |
| 476 | //===----------------------------------------------------------------------===// |
| 477 | |
Chris Lattner | b64871a | 2008-08-19 04:28:07 +0000 | [diff] [blame] | 478 | namespace { |
| 479 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 480 | /// This class provides computation of slot numbers for LLVM Assembly writing. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 481 | /// |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 482 | class SlotTracker { |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 483 | public: |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 484 | /// ValueMap - A mapping of Values to slot numbers. |
Chris Lattner | 9225507 | 2008-08-17 17:25:25 +0000 | [diff] [blame] | 485 | typedef DenseMap<const Value*, unsigned> ValueMap; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 486 | |
| 487 | private: |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 488 | /// TheModule - The module for which we are holding slot numbers. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 489 | const Module* TheModule; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 490 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 491 | /// TheFunction - The function for which we are holding slot numbers. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 492 | const Function* TheFunction; |
| 493 | bool FunctionProcessed; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 494 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 495 | /// mMap - The TypePlanes map for the module level data. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 496 | ValueMap mMap; |
| 497 | unsigned mNext; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 498 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 499 | /// fMap - The TypePlanes map for the function level data. |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 500 | ValueMap fMap; |
| 501 | unsigned fNext; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 502 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 503 | /// mdnMap - Map for MDNodes. |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 504 | DenseMap<const MDNode*, unsigned> mdnMap; |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 505 | unsigned mdnNext; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 506 | public: |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 507 | /// Construct from a module |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 508 | explicit SlotTracker(const Module *M); |
Chris Lattner | 45d4c73 | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 509 | /// Construct from a function, starting out in incorp state. |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 510 | explicit SlotTracker(const Function *F); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 511 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 512 | /// Return the slot number of the specified value in it's type |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 513 | /// plane. If something is not in the SlotTracker, return -1. |
Chris Lattner | 22379bc | 2007-01-11 03:54:27 +0000 | [diff] [blame] | 514 | int getLocalSlot(const Value *V); |
| 515 | int getGlobalSlot(const GlobalValue *V); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 516 | int getMetadataSlot(const MDNode *N); |
Reid Spencer | fc621e2 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 517 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 518 | /// If you'd like to deal with a function instead of just a module, use |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 519 | /// this method to get its data into the SlotTracker. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 520 | void incorporateFunction(const Function *F) { |
| 521 | TheFunction = F; |
Reid Spencer | 28531c7 | 2004-08-16 07:46:33 +0000 | [diff] [blame] | 522 | FunctionProcessed = false; |
| 523 | } |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 524 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 525 | /// After calling incorporateFunction, use this method to remove the |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 526 | /// most recently incorporated function from the SlotTracker. This |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 527 | /// will reset the state of the machine back to just the module contents. |
| 528 | void purgeFunction(); |
| 529 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 530 | /// MDNode map iterators. |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 531 | typedef DenseMap<const MDNode*, unsigned>::iterator mdn_iterator; |
| 532 | mdn_iterator mdn_begin() { return mdnMap.begin(); } |
| 533 | mdn_iterator mdn_end() { return mdnMap.end(); } |
| 534 | unsigned mdn_size() const { return mdnMap.size(); } |
| 535 | bool mdn_empty() const { return mdnMap.empty(); } |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 536 | |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 537 | /// This function does the actual initialization. |
| 538 | inline void initialize(); |
| 539 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 540 | // Implementation Details |
| 541 | private: |
Chris Lattner | 9446bbe | 2007-01-09 07:55:49 +0000 | [diff] [blame] | 542 | /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. |
| 543 | void CreateModuleSlot(const GlobalValue *V); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 544 | |
| 545 | /// CreateMetadataSlot - Insert the specified MDNode* into the slot table. |
| 546 | void CreateMetadataSlot(const MDNode *N); |
| 547 | |
Chris Lattner | 9446bbe | 2007-01-09 07:55:49 +0000 | [diff] [blame] | 548 | /// CreateFunctionSlot - Insert the specified Value* into the slot table. |
| 549 | void CreateFunctionSlot(const Value *V); |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 550 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 551 | /// Add all of the module level global variables (and their initializers) |
| 552 | /// and function declarations, but not the contents of those functions. |
| 553 | void processModule(); |
| 554 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 555 | /// Add all of the functions arguments, basic blocks, and instructions. |
Reid Spencer | b03de0c | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 556 | void processFunction(); |
| 557 | |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 558 | SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT |
| 559 | void operator=(const SlotTracker &); // DO NOT IMPLEMENT |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 560 | }; |
| 561 | |
Chris Lattner | b64871a | 2008-08-19 04:28:07 +0000 | [diff] [blame] | 562 | } // end anonymous namespace |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 563 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 564 | |
| 565 | static SlotTracker *createSlotTracker(const Value *V) { |
| 566 | if (const Argument *FA = dyn_cast<Argument>(V)) |
| 567 | return new SlotTracker(FA->getParent()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 568 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 569 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 570 | return new SlotTracker(I->getParent()->getParent()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 572 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 573 | return new SlotTracker(BB->getParent()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 575 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 576 | return new SlotTracker(GV->getParent()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 578 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 579 | return new SlotTracker(GA->getParent()); |
| 580 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 581 | if (const Function *Func = dyn_cast<Function>(V)) |
| 582 | return new SlotTracker(Func); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 583 | |
Dale Johannesen | 5f72a5e | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 584 | if (isa<MDNode>(V)) |
| 585 | return new SlotTracker((Function *)0); |
| 586 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | #if 0 |
David Greene | d865e02 | 2010-01-05 01:29:26 +0000 | [diff] [blame] | 591 | #define ST_DEBUG(X) dbgs() << X |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 592 | #else |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 593 | #define ST_DEBUG(X) |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 594 | #endif |
| 595 | |
| 596 | // Module level constructor. Causes the contents of the Module (sans functions) |
| 597 | // to be added to the slot table. |
| 598 | SlotTracker::SlotTracker(const Module *M) |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 599 | : TheModule(M), TheFunction(0), FunctionProcessed(false), |
Chris Lattner | 38cf02e | 2009-12-31 01:36:50 +0000 | [diff] [blame] | 600 | mNext(0), fNext(0), mdnNext(0) { |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | // Function level constructor. Causes the contents of the Module and the one |
| 604 | // function provided to be added to the slot table. |
| 605 | SlotTracker::SlotTracker(const Function *F) |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 606 | : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false), |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 607 | mNext(0), fNext(0), mdnNext(0) { |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | inline void SlotTracker::initialize() { |
| 611 | if (TheModule) { |
| 612 | processModule(); |
| 613 | TheModule = 0; ///< Prevent re-processing next time we're called. |
| 614 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 616 | if (TheFunction && !FunctionProcessed) |
| 617 | processFunction(); |
| 618 | } |
| 619 | |
| 620 | // Iterate through all the global variables, functions, and global |
| 621 | // variable initializers and create slots for them. |
| 622 | void SlotTracker::processModule() { |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 623 | ST_DEBUG("begin processModule!\n"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 625 | // Add all of the unnamed global variables to the value table. |
| 626 | for (Module::const_global_iterator I = TheModule->global_begin(), |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 627 | E = TheModule->global_end(); I != E; ++I) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 628 | if (!I->hasName()) |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 629 | CreateModuleSlot(I); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 630 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 631 | |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 632 | // Add metadata used by named metadata. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 633 | for (Module::const_named_metadata_iterator |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 634 | I = TheModule->named_metadata_begin(), |
| 635 | E = TheModule->named_metadata_end(); I != E; ++I) { |
| 636 | const NamedMDNode *NMD = I; |
Chris Lattner | 5d0cacd | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 637 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
Devang Patel | 69d02e0 | 2010-01-05 21:47:32 +0000 | [diff] [blame] | 638 | if (MDNode *MD = NMD->getOperand(i)) |
Devang Patel | e8861b8 | 2009-07-30 01:02:04 +0000 | [diff] [blame] | 639 | CreateMetadataSlot(MD); |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 643 | // Add all the unnamed functions to the table. |
| 644 | for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); |
| 645 | I != E; ++I) |
| 646 | if (!I->hasName()) |
| 647 | CreateModuleSlot(I); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 649 | ST_DEBUG("end processModule!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 652 | // Process the arguments, basic blocks, and instructions of a function. |
| 653 | void SlotTracker::processFunction() { |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 654 | ST_DEBUG("begin processFunction!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 655 | fNext = 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 657 | // Add all the function arguments with no names. |
| 658 | for(Function::const_arg_iterator AI = TheFunction->arg_begin(), |
| 659 | AE = TheFunction->arg_end(); AI != AE; ++AI) |
| 660 | if (!AI->hasName()) |
| 661 | CreateFunctionSlot(AI); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 663 | ST_DEBUG("Inserting Instructions:\n"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 664 | |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 665 | SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; |
Devang Patel | 4321578 | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 666 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 667 | // Add all of the basic blocks and instructions with no names. |
| 668 | for (Function::const_iterator BB = TheFunction->begin(), |
| 669 | E = TheFunction->end(); BB != E; ++BB) { |
| 670 | if (!BB->hasName()) |
| 671 | CreateFunctionSlot(BB); |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 672 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 673 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 674 | ++I) { |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 675 | if (!I->getType()->isVoidTy() && !I->hasName()) |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 676 | CreateFunctionSlot(I); |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 677 | |
Chris Lattner | fd450c0 | 2010-05-10 20:53:17 +0000 | [diff] [blame] | 678 | // Intrinsics can directly use metadata. We allow direct calls to any |
| 679 | // llvm.foo function here, because the target may not be linked into the |
| 680 | // optimizer. |
| 681 | if (const CallInst *CI = dyn_cast<CallInst>(I)) { |
| 682 | if (Function *F = CI->getCalledFunction()) |
| 683 | if (F->getName().startswith("llvm.")) |
| 684 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 685 | if (MDNode *N = dyn_cast_or_null<MDNode>(I->getOperand(i))) |
| 686 | CreateMetadataSlot(N); |
| 687 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 688 | |
Devang Patel | 4321578 | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 689 | // Process metadata attached with this instruction. |
Chris Lattner | 3990b12 | 2009-12-28 23:41:32 +0000 | [diff] [blame] | 690 | I->getAllMetadata(MDForInst); |
| 691 | for (unsigned i = 0, e = MDForInst.size(); i != e; ++i) |
| 692 | CreateMetadataSlot(MDForInst[i].second); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 693 | MDForInst.clear(); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 694 | } |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 695 | } |
Devang Patel | 4321578 | 2009-09-16 20:21:17 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 697 | FunctionProcessed = true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 698 | |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 699 | ST_DEBUG("end processFunction!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /// Clean up after incorporating a function. This is the only way to get out of |
| 703 | /// the function incorporation state that affects get*Slot/Create*Slot. Function |
| 704 | /// incorporation state is indicated by TheFunction != 0. |
| 705 | void SlotTracker::purgeFunction() { |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 706 | ST_DEBUG("begin purgeFunction!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 707 | fMap.clear(); // Simply discard the function level map |
| 708 | TheFunction = 0; |
| 709 | FunctionProcessed = false; |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 710 | ST_DEBUG("end purgeFunction!\n"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /// getGlobalSlot - Get the slot number of a global value. |
| 714 | int SlotTracker::getGlobalSlot(const GlobalValue *V) { |
| 715 | // Check for uninitialized state and do lazy initialization. |
| 716 | initialize(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 718 | // Find the type plane in the module map |
| 719 | ValueMap::iterator MI = mMap.find(V); |
Dan Gohman | aeaf245 | 2008-10-01 19:58:59 +0000 | [diff] [blame] | 720 | return MI == mMap.end() ? -1 : (int)MI->second; |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 723 | /// getMetadataSlot - Get the slot number of a MDNode. |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 724 | int SlotTracker::getMetadataSlot(const MDNode *N) { |
| 725 | // Check for uninitialized state and do lazy initialization. |
| 726 | initialize(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 727 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 728 | // Find the type plane in the module map |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 729 | mdn_iterator MI = mdnMap.find(N); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 730 | return MI == mdnMap.end() ? -1 : (int)MI->second; |
| 731 | } |
| 732 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 733 | |
| 734 | /// getLocalSlot - Get the slot number for a value that is local to a function. |
| 735 | int SlotTracker::getLocalSlot(const Value *V) { |
| 736 | assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 738 | // Check for uninitialized state and do lazy initialization. |
| 739 | initialize(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 740 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 741 | ValueMap::iterator FI = fMap.find(V); |
Dan Gohman | aeaf245 | 2008-10-01 19:58:59 +0000 | [diff] [blame] | 742 | return FI == fMap.end() ? -1 : (int)FI->second; |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | |
| 746 | /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. |
| 747 | void SlotTracker::CreateModuleSlot(const GlobalValue *V) { |
| 748 | assert(V && "Can't insert a null Value into SlotTracker!"); |
Chris Lattner | 4ee93c4 | 2009-12-29 07:25:48 +0000 | [diff] [blame] | 749 | assert(!V->getType()->isVoidTy() && "Doesn't need a slot!"); |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 750 | assert(!V->hasName() && "Doesn't need a slot!"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 751 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 752 | unsigned DestSlot = mNext++; |
| 753 | mMap[V] = DestSlot; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 754 | |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 755 | ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 756 | DestSlot << " ["); |
| 757 | // G = Global, F = Function, A = Alias, o = other |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 758 | ST_DEBUG((isa<GlobalVariable>(V) ? 'G' : |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 759 | (isa<Function>(V) ? 'F' : |
| 760 | (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n"); |
| 761 | } |
| 762 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 763 | /// CreateSlot - Create a new slot for the specified value if it has no name. |
| 764 | void SlotTracker::CreateFunctionSlot(const Value *V) { |
Chris Lattner | 4ee93c4 | 2009-12-29 07:25:48 +0000 | [diff] [blame] | 765 | assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 766 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 767 | unsigned DestSlot = fNext++; |
| 768 | fMap[V] = DestSlot; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 769 | |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 770 | // G = Global, F = Function, o = other |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 771 | ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 772 | DestSlot << " [o]\n"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 773 | } |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 774 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 775 | /// CreateModuleSlot - Insert the specified MDNode* into the slot table. |
| 776 | void SlotTracker::CreateMetadataSlot(const MDNode *N) { |
| 777 | assert(N && "Can't insert a null Value into SlotTracker!"); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 2b4b1e2 | 2009-12-31 02:27:30 +0000 | [diff] [blame] | 779 | // Don't insert if N is a function-local metadata, these are always printed |
| 780 | // inline. |
Victor Hernandez | 5d30162 | 2009-12-18 20:09:14 +0000 | [diff] [blame] | 781 | if (N->isFunctionLocal()) |
| 782 | return; |
Victor Hernandez | ff7707e | 2009-12-04 20:07:10 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 784 | mdn_iterator I = mdnMap.find(N); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 785 | if (I != mdnMap.end()) |
| 786 | return; |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 787 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 788 | unsigned DestSlot = mdnNext++; |
| 789 | mdnMap[N] = DestSlot; |
| 790 | |
Chris Lattner | 2b4b1e2 | 2009-12-31 02:27:30 +0000 | [diff] [blame] | 791 | // Recursively add any MDNodes referenced by operands. |
| 792 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 793 | if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i))) |
| 794 | CreateMetadataSlot(Op); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 795 | } |
Chris Lattner | 6ab910b | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 796 | |
| 797 | //===----------------------------------------------------------------------===// |
| 798 | // AsmWriter Implementation |
| 799 | //===----------------------------------------------------------------------===// |
Chris Lattner | f082b80 | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 800 | |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 801 | static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 802 | TypePrinting *TypePrinter, |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 803 | SlotTracker *Machine); |
Reid Spencer | 0e25e1c | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 804 | |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 805 | |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 806 | |
Chris Lattner | 82c4bc7 | 2006-12-06 06:40:49 +0000 | [diff] [blame] | 807 | static const char *getPredicateText(unsigned predicate) { |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 808 | const char * pred = "unknown"; |
| 809 | switch (predicate) { |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 810 | case FCmpInst::FCMP_FALSE: pred = "false"; break; |
| 811 | case FCmpInst::FCMP_OEQ: pred = "oeq"; break; |
| 812 | case FCmpInst::FCMP_OGT: pred = "ogt"; break; |
| 813 | case FCmpInst::FCMP_OGE: pred = "oge"; break; |
| 814 | case FCmpInst::FCMP_OLT: pred = "olt"; break; |
| 815 | case FCmpInst::FCMP_OLE: pred = "ole"; break; |
| 816 | case FCmpInst::FCMP_ONE: pred = "one"; break; |
| 817 | case FCmpInst::FCMP_ORD: pred = "ord"; break; |
| 818 | case FCmpInst::FCMP_UNO: pred = "uno"; break; |
| 819 | case FCmpInst::FCMP_UEQ: pred = "ueq"; break; |
| 820 | case FCmpInst::FCMP_UGT: pred = "ugt"; break; |
| 821 | case FCmpInst::FCMP_UGE: pred = "uge"; break; |
| 822 | case FCmpInst::FCMP_ULT: pred = "ult"; break; |
| 823 | case FCmpInst::FCMP_ULE: pred = "ule"; break; |
| 824 | case FCmpInst::FCMP_UNE: pred = "une"; break; |
| 825 | case FCmpInst::FCMP_TRUE: pred = "true"; break; |
| 826 | case ICmpInst::ICMP_EQ: pred = "eq"; break; |
| 827 | case ICmpInst::ICMP_NE: pred = "ne"; break; |
| 828 | case ICmpInst::ICMP_SGT: pred = "sgt"; break; |
| 829 | case ICmpInst::ICMP_SGE: pred = "sge"; break; |
| 830 | case ICmpInst::ICMP_SLT: pred = "slt"; break; |
| 831 | case ICmpInst::ICMP_SLE: pred = "sle"; break; |
| 832 | case ICmpInst::ICMP_UGT: pred = "ugt"; break; |
| 833 | case ICmpInst::ICMP_UGE: pred = "uge"; break; |
| 834 | case ICmpInst::ICMP_ULT: pred = "ult"; break; |
| 835 | case ICmpInst::ICMP_ULE: pred = "ule"; break; |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 836 | } |
| 837 | return pred; |
| 838 | } |
| 839 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 840 | |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 841 | static void WriteOptimizationInfo(raw_ostream &Out, const User *U) { |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 842 | if (const OverflowingBinaryOperator *OBO = |
| 843 | dyn_cast<OverflowingBinaryOperator>(U)) { |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 844 | if (OBO->hasNoUnsignedWrap()) |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 845 | Out << " nuw"; |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 846 | if (OBO->hasNoSignedWrap()) |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 847 | Out << " nsw"; |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 848 | } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) { |
| 849 | if (Div->isExact()) |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 850 | Out << " exact"; |
Dan Gohman | dd8004d | 2009-07-27 21:53:46 +0000 | [diff] [blame] | 851 | } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) { |
| 852 | if (GEP->isInBounds()) |
| 853 | Out << " inbounds"; |
Dan Gohman | 1224c38 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
Dan Gohman | 40cf12f | 2010-07-14 20:57:55 +0000 | [diff] [blame] | 857 | static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, |
| 858 | TypePrinting &TypePrinter, |
| 859 | SlotTracker *Machine) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 860 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 861 | if (CI->getType()->isIntegerTy(1)) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 862 | Out << (CI->getZExtValue() ? "true" : "false"); |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 863 | return; |
| 864 | } |
| 865 | Out << CI->getValue(); |
| 866 | return; |
| 867 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 868 | |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 869 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 870 | if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble || |
| 871 | &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) { |
| 872 | // We would like to output the FP constant value in exponential notation, |
| 873 | // but we cannot do this if doing so will lose precision. Check here to |
| 874 | // make sure that we only output it in exponential format if we can parse |
| 875 | // the value back and get the same value. |
| 876 | // |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 877 | bool ignored; |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 878 | bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble; |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 879 | double Val = isDouble ? CFP->getValueAPF().convertToDouble() : |
| 880 | CFP->getValueAPF().convertToFloat(); |
Benjamin Kramer | 5908839 | 2010-01-29 14:42:22 +0000 | [diff] [blame] | 881 | SmallString<128> StrVal; |
| 882 | raw_svector_ostream(StrVal) << Val; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 883 | |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 884 | // Check to make sure that the stringized number is not some string like |
| 885 | // "Inf" or NaN, that atof will accept, but the lexer will not. Check |
| 886 | // that the string matches the "[-+]?[0-9]" regex. |
| 887 | // |
| 888 | if ((StrVal[0] >= '0' && StrVal[0] <= '9') || |
| 889 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
| 890 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) { |
| 891 | // Reparse stringized version! |
| 892 | if (atof(StrVal.c_str()) == Val) { |
Benjamin Kramer | 5908839 | 2010-01-29 14:42:22 +0000 | [diff] [blame] | 893 | Out << StrVal.str(); |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 894 | return; |
| 895 | } |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 896 | } |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 897 | // Otherwise we could not reparse it to exactly the same value, so we must |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 898 | // output the string in hexadecimal format! Note that loading and storing |
| 899 | // floating point types changes the bits of NaNs on some hosts, notably |
| 900 | // x86, so we must not use these types. |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 901 | assert(sizeof(double) == sizeof(uint64_t) && |
| 902 | "assuming that double is 64 bits!"); |
Chris Lattner | c6a1346 | 2008-11-10 04:30:26 +0000 | [diff] [blame] | 903 | char Buffer[40]; |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 904 | APFloat apf = CFP->getValueAPF(); |
| 905 | // Floats are represented in ASCII IR as double, convert. |
| 906 | if (!isDouble) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 907 | apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 908 | &ignored); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 909 | Out << "0x" << |
| 910 | utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()), |
Dale Johannesen | 541ed9f | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 911 | Buffer+40); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 912 | return; |
| 913 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 914 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 915 | // Some form of long double. These appear as a magic letter identifying |
| 916 | // the type, then a fixed number of hex digits. |
| 917 | Out << "0x"; |
Dale Johannesen | 1b25cb2 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 918 | if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 919 | Out << 'K'; |
Dale Johannesen | 1b25cb2 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 920 | // api needed to prevent premature destruction |
| 921 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
| 922 | const uint64_t* p = api.getRawData(); |
| 923 | uint64_t word = p[1]; |
| 924 | int shiftcount=12; |
| 925 | int width = api.getBitWidth(); |
| 926 | for (int j=0; j<width; j+=4, shiftcount-=4) { |
| 927 | unsigned int nibble = (word>>shiftcount) & 15; |
| 928 | if (nibble < 10) |
| 929 | Out << (unsigned char)(nibble + '0'); |
| 930 | else |
| 931 | Out << (unsigned char)(nibble - 10 + 'A'); |
| 932 | if (shiftcount == 0 && j+4 < width) { |
| 933 | word = *p; |
| 934 | shiftcount = 64; |
| 935 | if (width-j-4 < 64) |
| 936 | shiftcount = width-j-4; |
| 937 | } |
| 938 | } |
| 939 | return; |
| 940 | } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 941 | Out << 'L'; |
| 942 | else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) |
| 943 | Out << 'M'; |
| 944 | else |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 945 | llvm_unreachable("Unsupported floating point type"); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 946 | // api needed to prevent premature destruction |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 947 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 948 | const uint64_t* p = api.getRawData(); |
| 949 | uint64_t word = *p; |
| 950 | int shiftcount=60; |
| 951 | int width = api.getBitWidth(); |
| 952 | for (int j=0; j<width; j+=4, shiftcount-=4) { |
| 953 | unsigned int nibble = (word>>shiftcount) & 15; |
| 954 | if (nibble < 10) |
| 955 | Out << (unsigned char)(nibble + '0'); |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 956 | else |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 957 | Out << (unsigned char)(nibble - 10 + 'A'); |
| 958 | if (shiftcount == 0 && j+4 < width) { |
| 959 | word = *(++p); |
| 960 | shiftcount = 64; |
| 961 | if (width-j-4 < 64) |
| 962 | shiftcount = width-j-4; |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 963 | } |
| 964 | } |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 965 | return; |
| 966 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 967 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 968 | if (isa<ConstantAggregateZero>(CV)) { |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 969 | Out << "zeroinitializer"; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 970 | return; |
| 971 | } |
Chris Lattner | 73050e1 | 2009-10-28 03:38:12 +0000 | [diff] [blame] | 972 | |
| 973 | if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) { |
| 974 | Out << "blockaddress("; |
| 975 | WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine); |
| 976 | Out << ", "; |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 977 | WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine); |
Chris Lattner | 73050e1 | 2009-10-28 03:38:12 +0000 | [diff] [blame] | 978 | Out << ")"; |
| 979 | return; |
| 980 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 981 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 982 | if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 983 | // As a special case, print the array as a string if it is an array of |
Dan Gohman | 9b38d7d | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 984 | // i8 with ConstantInt values. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 985 | // |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 986 | const Type *ETy = CA->getType()->getElementType(); |
Chris Lattner | 1836550 | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 987 | if (CA->isString()) { |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 988 | Out << "c\""; |
Chris Lattner | 1836550 | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 989 | PrintEscapedString(CA->getAsString(), Out); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 990 | Out << '"'; |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 991 | } else { // Cannot output in string format... |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 992 | Out << '['; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 993 | if (CA->getNumOperands()) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 994 | TypePrinter.print(ETy, Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 995 | Out << ' '; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 996 | WriteAsOperandInternal(Out, CA->getOperand(0), |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 997 | &TypePrinter, Machine); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 998 | for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { |
| 999 | Out << ", "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1000 | TypePrinter.print(ETy, Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1001 | Out << ' '; |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1002 | WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1005 | Out << ']'; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1006 | } |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1007 | return; |
| 1008 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1010 | if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { |
Andrew Lenharth | 43f344a | 2007-01-08 18:21:30 +0000 | [diff] [blame] | 1011 | if (CS->getType()->isPacked()) |
| 1012 | Out << '<'; |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1013 | Out << '{'; |
Jim Laskey | a3f332b | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 1014 | unsigned N = CS->getNumOperands(); |
| 1015 | if (N) { |
Chris Lattner | 2423303 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 1016 | Out << ' '; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1017 | TypePrinter.print(CS->getOperand(0)->getType(), Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1018 | Out << ' '; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1019 | |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1020 | WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1021 | |
Jim Laskey | a3f332b | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 1022 | for (unsigned i = 1; i < N; i++) { |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1023 | Out << ", "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1024 | TypePrinter.print(CS->getOperand(i)->getType(), Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1025 | Out << ' '; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1026 | |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1027 | WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine); |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1028 | } |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1029 | Out << ' '; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1030 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1031 | |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1032 | Out << '}'; |
Andrew Lenharth | 43f344a | 2007-01-08 18:21:30 +0000 | [diff] [blame] | 1033 | if (CS->getType()->isPacked()) |
| 1034 | Out << '>'; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1035 | return; |
| 1036 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1037 | |
Chris Lattner | 94c484d | 2010-03-16 21:21:35 +0000 | [diff] [blame] | 1038 | if (const ConstantUnion *CU = dyn_cast<ConstantUnion>(CV)) { |
| 1039 | Out << "{ "; |
| 1040 | TypePrinter.print(CU->getOperand(0)->getType(), Out); |
| 1041 | Out << ' '; |
| 1042 | WriteAsOperandInternal(Out, CU->getOperand(0), &TypePrinter, Machine); |
| 1043 | Out << " }"; |
| 1044 | return; |
| 1045 | } |
| 1046 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1047 | if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { |
| 1048 | const Type *ETy = CP->getType()->getElementType(); |
| 1049 | assert(CP->getNumOperands() > 0 && |
| 1050 | "Number of operands for a PackedConst must be > 0"); |
Dan Gohman | 7dfa07f | 2009-02-11 00:25:25 +0000 | [diff] [blame] | 1051 | Out << '<'; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1052 | TypePrinter.print(ETy, Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1053 | Out << ' '; |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1054 | WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1055 | for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) { |
Chris Lattner | 4667b71 | 2008-08-19 05:26:17 +0000 | [diff] [blame] | 1056 | Out << ", "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1057 | TypePrinter.print(ETy, Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1058 | Out << ' '; |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1059 | WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1060 | } |
Dan Gohman | 7dfa07f | 2009-02-11 00:25:25 +0000 | [diff] [blame] | 1061 | Out << '>'; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1062 | return; |
| 1063 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1064 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1065 | if (isa<ConstantPointerNull>(CV)) { |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1066 | Out << "null"; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1067 | return; |
| 1068 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1069 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1070 | if (isa<UndefValue>(CV)) { |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1071 | Out << "undef"; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1072 | return; |
| 1073 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1074 | |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1075 | if (const MDNode *Node = dyn_cast<MDNode>(CV)) { |
| 1076 | Out << "!" << Machine->getMetadataSlot(Node); |
| 1077 | return; |
| 1078 | } |
| 1079 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1080 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1081 | Out << CE->getOpcodeName(); |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 1082 | WriteOptimizationInfo(Out, CE); |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1083 | if (CE->isCompare()) |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1084 | Out << ' ' << getPredicateText(CE->getPredicate()); |
Reid Spencer | 81dfeb3 | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1085 | Out << " ("; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1086 | |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 1087 | for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1088 | TypePrinter.print((*OI)->getType(), Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1089 | Out << ' '; |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1090 | WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine); |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 1091 | if (OI+1 != CE->op_end()) |
Chris Lattner | c188eeb | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 1092 | Out << ", "; |
Vikram S. Adve | b4dbb44 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 1093 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1094 | |
Dan Gohman | 995be7d | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1095 | if (CE->hasIndices()) { |
| 1096 | const SmallVector<unsigned, 4> &Indices = CE->getIndices(); |
| 1097 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) |
| 1098 | Out << ", " << Indices[i]; |
| 1099 | } |
| 1100 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1101 | if (CE->isCast()) { |
Chris Lattner | 95586b8 | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 1102 | Out << " to "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1103 | TypePrinter.print(CE->getType(), Out); |
Chris Lattner | 95586b8 | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 1104 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1105 | |
Misha Brukman | 40c732c | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1106 | Out << ')'; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1107 | return; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1108 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1109 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1110 | Out << "<placeholder or erroneous Constant>"; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Chris Lattner | 85b1912 | 2009-12-31 02:31:59 +0000 | [diff] [blame] | 1113 | static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, |
| 1114 | TypePrinting *TypePrinter, |
| 1115 | SlotTracker *Machine) { |
| 1116 | Out << "!{"; |
| 1117 | for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) { |
| 1118 | const Value *V = Node->getOperand(mi); |
| 1119 | if (V == 0) |
| 1120 | Out << "null"; |
| 1121 | else { |
| 1122 | TypePrinter->print(V->getType(), Out); |
| 1123 | Out << ' '; |
| 1124 | WriteAsOperandInternal(Out, Node->getOperand(mi), |
| 1125 | TypePrinter, Machine); |
| 1126 | } |
| 1127 | if (mi + 1 != me) |
| 1128 | Out << ", "; |
| 1129 | } |
| 1130 | |
| 1131 | Out << "}"; |
| 1132 | } |
| 1133 | |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1134 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1135 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 1136 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 1137 | /// the whole instruction that generated it. |
| 1138 | /// |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 1139 | static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1140 | TypePrinting *TypePrinter, |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 1141 | SlotTracker *Machine) { |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1142 | if (V->hasName()) { |
| 1143 | PrintLLVMName(Out, V); |
| 1144 | return; |
| 1145 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1146 | |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1147 | const Constant *CV = dyn_cast<Constant>(V); |
| 1148 | if (CV && !isa<GlobalValue>(CV)) { |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1149 | assert(TypePrinter && "Constants require TypePrinting!"); |
Dan Gohman | 40cf12f | 2010-07-14 20:57:55 +0000 | [diff] [blame] | 1150 | WriteConstantInternal(Out, CV, *TypePrinter, Machine); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1151 | return; |
| 1152 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1153 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1154 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1155 | Out << "asm "; |
| 1156 | if (IA->hasSideEffects()) |
| 1157 | Out << "sideeffect "; |
Dale Johannesen | 8ba2d5b | 2009-10-21 23:28:00 +0000 | [diff] [blame] | 1158 | if (IA->isAlignStack()) |
| 1159 | Out << "alignstack "; |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1160 | Out << '"'; |
| 1161 | PrintEscapedString(IA->getAsmString(), Out); |
| 1162 | Out << "\", \""; |
| 1163 | PrintEscapedString(IA->getConstraintString(), Out); |
| 1164 | Out << '"'; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1165 | return; |
| 1166 | } |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1167 | |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1168 | if (const MDNode *N = dyn_cast<MDNode>(V)) { |
Victor Hernandez | 5d30162 | 2009-12-18 20:09:14 +0000 | [diff] [blame] | 1169 | if (N->isFunctionLocal()) { |
Victor Hernandez | 97e2450 | 2009-12-04 01:35:02 +0000 | [diff] [blame] | 1170 | // Print metadata inline, not via slot reference number. |
Chris Lattner | 85b1912 | 2009-12-31 02:31:59 +0000 | [diff] [blame] | 1171 | WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine); |
Victor Hernandez | 97e2450 | 2009-12-04 01:35:02 +0000 | [diff] [blame] | 1172 | return; |
| 1173 | } |
| 1174 | |
Dale Johannesen | 5f72a5e | 2010-01-13 00:00:24 +0000 | [diff] [blame] | 1175 | if (!Machine) |
| 1176 | Machine = createSlotTracker(V); |
Devang Patel | 104cf9e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1177 | Out << '!' << Machine->getMetadataSlot(N); |
| 1178 | return; |
| 1179 | } |
| 1180 | |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1181 | if (const MDString *MDS = dyn_cast<MDString>(V)) { |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1182 | Out << "!\""; |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 1183 | PrintEscapedString(MDS->getString(), Out); |
Devang Patel | e54abc9 | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1184 | Out << '"'; |
| 1185 | return; |
| 1186 | } |
| 1187 | |
Evan Cheng | 746d546 | 2009-11-16 07:10:36 +0000 | [diff] [blame] | 1188 | if (V->getValueID() == Value::PseudoSourceValueVal || |
| 1189 | V->getValueID() == Value::FixedStackPseudoSourceValueVal) { |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 1190 | V->print(Out); |
| 1191 | return; |
| 1192 | } |
| 1193 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1194 | char Prefix = '%'; |
| 1195 | int Slot; |
| 1196 | if (Machine) { |
| 1197 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 1198 | Slot = Machine->getGlobalSlot(GV); |
| 1199 | Prefix = '@'; |
| 1200 | } else { |
| 1201 | Slot = Machine->getLocalSlot(V); |
| 1202 | } |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1203 | } else { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1204 | Machine = createSlotTracker(V); |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1205 | if (Machine) { |
| 1206 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 1207 | Slot = Machine->getGlobalSlot(GV); |
| 1208 | Prefix = '@'; |
| 1209 | } else { |
| 1210 | Slot = Machine->getLocalSlot(V); |
| 1211 | } |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1212 | delete Machine; |
Chris Lattner | 80cd115 | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 1213 | } else { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1214 | Slot = -1; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1215 | } |
| 1216 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1218 | if (Slot != -1) |
| 1219 | Out << Prefix << Slot; |
| 1220 | else |
| 1221 | Out << "<badref>"; |
Chris Lattner | 7a716ad | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 1224 | void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, |
| 1225 | bool PrintType, const Module *Context) { |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1226 | |
| 1227 | // Fast path: Don't construct and populate a TypePrinting object if we |
| 1228 | // won't be needing any types printed. |
Dan Gohman | 009fc9e | 2009-08-13 23:07:11 +0000 | [diff] [blame] | 1229 | if (!PrintType && |
| 1230 | (!isa<Constant>(V) || V->hasName() || isa<GlobalValue>(V))) { |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1231 | WriteAsOperandInternal(Out, V, 0, 0); |
| 1232 | return; |
| 1233 | } |
| 1234 | |
Chris Lattner | 607dc68 | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 1235 | if (Context == 0) Context = getModuleFromVal(V); |
Chris Lattner | 207b5bc | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 1236 | |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 1237 | TypePrinting TypePrinter; |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1238 | std::vector<const Type*> NumberedTypes; |
| 1239 | AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1240 | if (PrintType) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1241 | TypePrinter.print(V->getType(), Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1242 | Out << ' '; |
| 1243 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1244 | |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1245 | WriteAsOperandInternal(Out, V, &TypePrinter, 0); |
Chris Lattner | 622f740 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1248 | namespace { |
Chris Lattner | d8c2e42 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 1249 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1250 | class AssemblyWriter { |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1251 | formatted_raw_ostream &Out; |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 1252 | SlotTracker &Machine; |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1253 | const Module *TheModule; |
Chris Lattner | 9cc3446 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1254 | TypePrinting TypePrinter; |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1255 | AssemblyAnnotationWriter *AnnotationWriter; |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1256 | std::vector<const Type*> NumberedTypes; |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 1257 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1258 | public: |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1259 | inline AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, |
| 1260 | const Module *M, |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1261 | AssemblyAnnotationWriter *AAW) |
Devang Patel | 3168b79 | 2009-09-28 18:31:56 +0000 | [diff] [blame] | 1262 | : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) { |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1263 | AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 1266 | void printMDNodeBody(const MDNode *MD); |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 1267 | void printNamedMDNode(const NamedMDNode *NMD); |
| 1268 | |
Chris Lattner | bd72b32 | 2009-12-31 02:23:35 +0000 | [diff] [blame] | 1269 | void printModule(const Module *M); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | 2fcfdb7 | 2006-12-06 06:24:27 +0000 | [diff] [blame] | 1271 | void writeOperand(const Value *Op, bool PrintType); |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 1272 | void writeParamOperand(const Value *Operand, Attributes Attrs); |
Chris Lattner | 66e810b | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 1273 | |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 1274 | void writeAllMDNodes(); |
| 1275 | |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1276 | void printTypeSymbolTable(const TypeSymbolTable &ST); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1277 | void printGlobal(const GlobalVariable *GV); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1278 | void printAlias(const GlobalAlias *GV); |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 1279 | void printFunction(const Function *F); |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 1280 | void printArgument(const Argument *FA, Attributes Attrs); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1281 | void printBasicBlock(const BasicBlock *BB); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1282 | void printInstruction(const Instruction &I); |
Chris Lattner | 2761e9f | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 1283 | |
Dan Gohman | 659d1e8 | 2010-02-10 20:42:57 +0000 | [diff] [blame] | 1284 | private: |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1285 | // printInfoComment - Print a little comment after the instruction indicating |
| 1286 | // which slot it occupies. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1287 | void printInfoComment(const Value &V); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1288 | }; |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1289 | } // end of anonymous namespace |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | 2fcfdb7 | 2006-12-06 06:24:27 +0000 | [diff] [blame] | 1291 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) { |
| 1292 | if (Operand == 0) { |
Chris Lattner | aab1820 | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 1293 | Out << "<null operand!>"; |
Chris Lattner | c65b72c | 2009-12-31 02:33:14 +0000 | [diff] [blame] | 1294 | return; |
Chris Lattner | aab1820 | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 1295 | } |
Chris Lattner | c65b72c | 2009-12-31 02:33:14 +0000 | [diff] [blame] | 1296 | if (PrintType) { |
| 1297 | TypePrinter.print(Operand->getType(), Out); |
| 1298 | Out << ' '; |
| 1299 | } |
| 1300 | WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1303 | void AssemblyWriter::writeParamOperand(const Value *Operand, |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 1304 | Attributes Attrs) { |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1305 | if (Operand == 0) { |
| 1306 | Out << "<null operand!>"; |
Chris Lattner | c65b72c | 2009-12-31 02:33:14 +0000 | [diff] [blame] | 1307 | return; |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1308 | } |
Chris Lattner | c65b72c | 2009-12-31 02:33:14 +0000 | [diff] [blame] | 1309 | |
| 1310 | // Print the type |
| 1311 | TypePrinter.print(Operand->getType(), Out); |
| 1312 | // Print parameter attributes list |
| 1313 | if (Attrs != Attribute::None) |
| 1314 | Out << ' ' << Attribute::getAsString(Attrs); |
| 1315 | Out << ' '; |
| 1316 | // Print the operand |
| 1317 | WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine); |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1318 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1319 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1320 | void AssemblyWriter::printModule(const Module *M) { |
Chris Lattner | 31ab1b3 | 2005-03-02 23:12:40 +0000 | [diff] [blame] | 1321 | if (!M->getModuleIdentifier().empty() && |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1322 | // Don't print the ID if it will start a new line (which would |
Chris Lattner | 31ab1b3 | 2005-03-02 23:12:40 +0000 | [diff] [blame] | 1323 | // require a comment char before it). |
| 1324 | M->getModuleIdentifier().find('\n') == std::string::npos) |
| 1325 | Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n"; |
| 1326 | |
Owen Anderson | cf7ff2b | 2006-10-18 02:21:12 +0000 | [diff] [blame] | 1327 | if (!M->getDataLayout().empty()) |
Chris Lattner | d2f9e60 | 2006-10-22 06:06:56 +0000 | [diff] [blame] | 1328 | Out << "target datalayout = \"" << M->getDataLayout() << "\"\n"; |
Reid Spencer | cddc86f | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 1329 | if (!M->getTargetTriple().empty()) |
Reid Spencer | c9a1f0d | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 1330 | Out << "target triple = \"" << M->getTargetTriple() << "\"\n"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1331 | |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1332 | if (!M->getModuleInlineAsm().empty()) { |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1333 | // Split the string into lines, to make it easier to read the .ll file. |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1334 | std::string Asm = M->getModuleInlineAsm(); |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1335 | size_t CurPos = 0; |
| 1336 | size_t NewLine = Asm.find_first_of('\n', CurPos); |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1337 | Out << '\n'; |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1338 | while (NewLine != std::string::npos) { |
| 1339 | // We found a newline, print the portion of the asm string from the |
| 1340 | // last newline up to this newline. |
| 1341 | Out << "module asm \""; |
| 1342 | PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine), |
| 1343 | Out); |
| 1344 | Out << "\"\n"; |
| 1345 | CurPos = NewLine+1; |
| 1346 | NewLine = Asm.find_first_of('\n', CurPos); |
| 1347 | } |
Chris Lattner | 71cdba3 | 2006-01-24 00:40:17 +0000 | [diff] [blame] | 1348 | Out << "module asm \""; |
Chris Lattner | 42a162e | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1349 | PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out); |
Chris Lattner | 1836550 | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 1350 | Out << "\"\n"; |
| 1351 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1352 | |
Chris Lattner | 44da7d7 | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 1353 | // Loop over the dependent libraries and emit them. |
Chris Lattner | cfe97b7 | 2004-09-14 04:51:44 +0000 | [diff] [blame] | 1354 | Module::lib_iterator LI = M->lib_begin(); |
| 1355 | Module::lib_iterator LE = M->lib_end(); |
Reid Spencer | cddc86f | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 1356 | if (LI != LE) { |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1357 | Out << '\n'; |
Chris Lattner | cfe97b7 | 2004-09-14 04:51:44 +0000 | [diff] [blame] | 1358 | Out << "deplibs = [ "; |
| 1359 | while (LI != LE) { |
Chris Lattner | 44da7d7 | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 1360 | Out << '"' << *LI << '"'; |
Reid Spencer | c9a1f0d | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 1361 | ++LI; |
Chris Lattner | cfe97b7 | 2004-09-14 04:51:44 +0000 | [diff] [blame] | 1362 | if (LI != LE) |
| 1363 | Out << ", "; |
Reid Spencer | c9a1f0d | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 1364 | } |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1365 | Out << " ]"; |
Reid Spencer | 83f6a77 | 2004-07-25 18:08:18 +0000 | [diff] [blame] | 1366 | } |
Reid Spencer | e59eaf4 | 2004-09-13 23:44:23 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1368 | // Loop over the symbol table, emitting all id'd types. |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1369 | if (!M->getTypeSymbolTable().empty() || !NumberedTypes.empty()) Out << '\n'; |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1370 | printTypeSymbolTable(M->getTypeSymbolTable()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1371 | |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1372 | // Output all globals. |
| 1373 | if (!M->global_empty()) Out << '\n'; |
Chris Lattner | d6d826c | 2006-12-06 04:41:52 +0000 | [diff] [blame] | 1374 | for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
| 1375 | I != E; ++I) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1376 | printGlobal(I); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 69dacfc | 2007-04-26 02:24:10 +0000 | [diff] [blame] | 1378 | // Output all aliases. |
| 1379 | if (!M->alias_empty()) Out << "\n"; |
| 1380 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 1381 | I != E; ++I) |
| 1382 | printAlias(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1383 | |
Chris Lattner | 44da7d7 | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 1384 | // Output all of the functions. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1385 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 1386 | printFunction(I); |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1387 | |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 1388 | // Output named metadata. |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1389 | if (!M->named_metadata_empty()) Out << '\n'; |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 1390 | |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 1391 | for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 1392 | E = M->named_metadata_end(); I != E; ++I) |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 1393 | printNamedMDNode(I); |
Devang Patel | 37c4a2d | 2009-07-29 22:04:47 +0000 | [diff] [blame] | 1394 | |
| 1395 | // Output metadata. |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 1396 | if (!Machine.mdn_empty()) { |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 1397 | Out << '\n'; |
| 1398 | writeAllMDNodes(); |
| 1399 | } |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 1402 | void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) { |
| 1403 | Out << "!" << NMD->getName() << " = !{"; |
| 1404 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { |
| 1405 | if (i) Out << ", "; |
Devang Patel | 69d02e0 | 2010-01-05 21:47:32 +0000 | [diff] [blame] | 1406 | if (MDNode *MD = NMD->getOperand(i)) |
| 1407 | Out << '!' << Machine.getMetadataSlot(MD); |
| 1408 | else |
| 1409 | Out << "null"; |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 1410 | } |
| 1411 | Out << "}\n"; |
| 1412 | } |
| 1413 | |
| 1414 | |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1415 | static void PrintLinkage(GlobalValue::LinkageTypes LT, |
| 1416 | formatted_raw_ostream &Out) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1417 | switch (LT) { |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1418 | case GlobalValue::ExternalLinkage: break; |
| 1419 | case GlobalValue::PrivateLinkage: Out << "private "; break; |
| 1420 | case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break; |
Bill Wendling | 5e721d7 | 2010-07-01 21:55:59 +0000 | [diff] [blame] | 1421 | case GlobalValue::LinkerPrivateWeakLinkage: |
| 1422 | Out << "linker_private_weak "; |
| 1423 | break; |
Bill Wendling | 3d10a5a | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1424 | case GlobalValue::InternalLinkage: Out << "internal "; break; |
| 1425 | case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break; |
| 1426 | case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break; |
| 1427 | case GlobalValue::WeakAnyLinkage: Out << "weak "; break; |
| 1428 | case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break; |
| 1429 | case GlobalValue::CommonLinkage: Out << "common "; break; |
| 1430 | case GlobalValue::AppendingLinkage: Out << "appending "; break; |
| 1431 | case GlobalValue::DLLImportLinkage: Out << "dllimport "; break; |
| 1432 | case GlobalValue::DLLExportLinkage: Out << "dllexport "; break; |
| 1433 | case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break; |
Chris Lattner | 266c7bb | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 1434 | case GlobalValue::AvailableExternallyLinkage: |
| 1435 | Out << "available_externally "; |
| 1436 | break; |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 1439 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1440 | |
| 1441 | static void PrintVisibility(GlobalValue::VisibilityTypes Vis, |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1442 | formatted_raw_ostream &Out) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1443 | switch (Vis) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1444 | case GlobalValue::DefaultVisibility: break; |
| 1445 | case GlobalValue::HiddenVisibility: Out << "hidden "; break; |
| 1446 | case GlobalValue::ProtectedVisibility: Out << "protected "; break; |
| 1447 | } |
| 1448 | } |
| 1449 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1450 | void AssemblyWriter::printGlobal(const GlobalVariable *GV) { |
Dan Gohman | 4483c7b | 2010-01-29 23:12:36 +0000 | [diff] [blame] | 1451 | if (GV->isMaterializable()) |
| 1452 | Out << "; Materializable\n"; |
| 1453 | |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1454 | WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine); |
Dan Gohman | 3845e50 | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 1455 | Out << " = "; |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1457 | if (!GV->hasInitializer() && GV->hasExternalLinkage()) |
| 1458 | Out << "external "; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1459 | |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1460 | PrintLinkage(GV->getLinkage(), Out); |
| 1461 | PrintVisibility(GV->getVisibility(), Out); |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 1462 | |
| 1463 | if (GV->isThreadLocal()) Out << "thread_local "; |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1464 | if (unsigned AddressSpace = GV->getType()->getAddressSpace()) |
| 1465 | Out << "addrspace(" << AddressSpace << ") "; |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1466 | Out << (GV->isConstant() ? "constant " : "global "); |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1467 | TypePrinter.print(GV->getType()->getElementType(), Out); |
Chris Lattner | d70684f | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1468 | |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1469 | if (GV->hasInitializer()) { |
| 1470 | Out << ' '; |
Devang Patel | 320671d | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1471 | writeOperand(GV->getInitializer(), false); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1472 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | 8fff126 | 2010-07-07 23:16:37 +0000 | [diff] [blame] | 1474 | if (GV->hasSection()) { |
| 1475 | Out << ", section \""; |
| 1476 | PrintEscapedString(GV->getSection(), Out); |
| 1477 | Out << '"'; |
| 1478 | } |
Chris Lattner | 60962db | 2005-11-12 00:10:19 +0000 | [diff] [blame] | 1479 | if (GV->getAlignment()) |
Chris Lattner | 30caa28 | 2005-11-06 06:48:53 +0000 | [diff] [blame] | 1480 | Out << ", align " << GV->getAlignment(); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1481 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1482 | printInfoComment(*GV); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1483 | Out << '\n'; |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1486 | void AssemblyWriter::printAlias(const GlobalAlias *GA) { |
Dan Gohman | 4483c7b | 2010-01-29 23:12:36 +0000 | [diff] [blame] | 1487 | if (GA->isMaterializable()) |
| 1488 | Out << "; Materializable\n"; |
| 1489 | |
Dale Johannesen | 24f07dc | 2008-06-03 18:14:29 +0000 | [diff] [blame] | 1490 | // Don't crash when dumping partially built GA |
| 1491 | if (!GA->hasName()) |
| 1492 | Out << "<<nameless>> = "; |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1493 | else { |
| 1494 | PrintLLVMName(Out, GA); |
| 1495 | Out << " = "; |
| 1496 | } |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1497 | PrintVisibility(GA->getVisibility(), Out); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1498 | |
| 1499 | Out << "alias "; |
| 1500 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1501 | PrintLinkage(GA->getLinkage(), Out); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1502 | |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 1503 | const Constant *Aliasee = GA->getAliasee(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1504 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1505 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1506 | TypePrinter.print(GV->getType(), Out); |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1507 | Out << ' '; |
| 1508 | PrintLLVMName(Out, GV); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1509 | } else if (const Function *F = dyn_cast<Function>(Aliasee)) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1510 | TypePrinter.print(F->getFunctionType(), Out); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1511 | Out << "* "; |
| 1512 | |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1513 | WriteAsOperandInternal(Out, F, &TypePrinter, &Machine); |
Anton Korobeynikov | 591858a | 2008-03-22 08:17:17 +0000 | [diff] [blame] | 1514 | } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1515 | TypePrinter.print(GA->getType(), Out); |
| 1516 | Out << ' '; |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1517 | PrintLLVMName(Out, GA); |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 1518 | } else { |
Chris Lattner | a2165ed | 2009-04-25 21:23:19 +0000 | [diff] [blame] | 1519 | const ConstantExpr *CE = cast<ConstantExpr>(Aliasee); |
| 1520 | // The only valid GEP is an all zero GEP. |
| 1521 | assert((CE->getOpcode() == Instruction::BitCast || |
| 1522 | CE->getOpcode() == Instruction::GetElementPtr) && |
| 1523 | "Unsupported aliasee"); |
| 1524 | writeOperand(CE, false); |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 1525 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1526 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1527 | printInfoComment(*GA); |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1528 | Out << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1531 | void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) { |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1532 | // Emit all numbered types. |
| 1533 | for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) { |
Dan Gohman | 3845e50 | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 1534 | Out << '%' << i << " = type "; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1536 | // Make sure we print out at least one level of the type structure, so |
| 1537 | // that we do not get %2 = type %2 |
| 1538 | TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out); |
Dan Gohman | 9bf0b9b | 2009-08-12 23:54:22 +0000 | [diff] [blame] | 1539 | Out << '\n'; |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1540 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1541 | |
Chris Lattner | 413fd23 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1542 | // Print the named types. |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1543 | for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end(); |
| 1544 | TI != TE; ++TI) { |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 1545 | PrintLLVMName(Out, TI->first, LocalPrefix); |
Chris Lattner | 52b26de | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1546 | Out << " = type "; |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Make sure we print out at least one level of the type structure, so |
| 1549 | // that we do not get %FILE = type %FILE |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1550 | TypePrinter.printAtLeastOneLevel(TI->second, Out); |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1551 | Out << '\n'; |
Reid Spencer | 9231ac8 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1552 | } |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1555 | /// printFunction - Print all aspects of a function. |
| 1556 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1557 | void AssemblyWriter::printFunction(const Function *F) { |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1558 | // Print out the return type and name. |
| 1559 | Out << '\n'; |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 1560 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1561 | if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1562 | |
Dan Gohman | 4483c7b | 2010-01-29 23:12:36 +0000 | [diff] [blame] | 1563 | if (F->isMaterializable()) |
| 1564 | Out << "; Materializable\n"; |
| 1565 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1566 | if (F->isDeclaration()) |
Chris Lattner | 3aa6066 | 2007-08-19 22:15:26 +0000 | [diff] [blame] | 1567 | Out << "declare "; |
| 1568 | else |
Reid Spencer | b951bc0 | 2006-12-29 20:29:48 +0000 | [diff] [blame] | 1569 | Out << "define "; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1570 | |
Chris Lattner | cfb5a20 | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1571 | PrintLinkage(F->getLinkage(), Out); |
| 1572 | PrintVisibility(F->getVisibility(), Out); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1574 | // Print the calling convention. |
| 1575 | switch (F->getCallingConv()) { |
| 1576 | case CallingConv::C: break; // default |
Anton Korobeynikov | f824868 | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 1577 | case CallingConv::Fast: Out << "fastcc "; break; |
| 1578 | case CallingConv::Cold: Out << "coldcc "; break; |
| 1579 | case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break; |
Anton Korobeynikov | 385f5a9 | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1580 | case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break; |
Anton Korobeynikov | ded05e3 | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 1581 | case CallingConv::X86_ThisCall: Out << "x86_thiscallcc "; break; |
Anton Korobeynikov | 385f5a9 | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1582 | case CallingConv::ARM_APCS: Out << "arm_apcscc "; break; |
| 1583 | case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break; |
| 1584 | case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break; |
Anton Korobeynikov | 211a14e | 2009-12-07 02:27:35 +0000 | [diff] [blame] | 1585 | case CallingConv::MSP430_INTR: Out << "msp430_intrcc "; break; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1586 | default: Out << "cc" << F->getCallingConv() << " "; break; |
| 1587 | } |
| 1588 | |
Reid Spencer | bd5db8e | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1589 | const FunctionType *FT = F->getFunctionType(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1590 | const AttrListPtr &Attrs = F->getAttributes(); |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1591 | Attributes RetAttrs = Attrs.getRetAttributes(); |
| 1592 | if (RetAttrs != Attribute::None) |
| 1593 | Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' '; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1594 | TypePrinter.print(F->getReturnType(), Out); |
Chris Lattner | 4667b71 | 2008-08-19 05:26:17 +0000 | [diff] [blame] | 1595 | Out << ' '; |
Dan Gohman | d6c0f65 | 2009-08-13 15:27:57 +0000 | [diff] [blame] | 1596 | WriteAsOperandInternal(Out, F, &TypePrinter, &Machine); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1597 | Out << '('; |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1598 | Machine.incorporateFunction(F); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1599 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1600 | // Loop over the arguments, printing them... |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1601 | |
Reid Spencer | bd5db8e | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1602 | unsigned Idx = 1; |
Chris Lattner | 8dcd2f1 | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1603 | if (!F->isDeclaration()) { |
| 1604 | // If this isn't a declaration, print the argument names as well. |
| 1605 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 1606 | I != E; ++I) { |
| 1607 | // Insert commas as we go... the first arg doesn't get a comma |
| 1608 | if (I != F->arg_begin()) Out << ", "; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1609 | printArgument(I, Attrs.getParamAttributes(Idx)); |
Chris Lattner | 8dcd2f1 | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1610 | Idx++; |
| 1611 | } |
| 1612 | } else { |
| 1613 | // Otherwise, print the types from the function type. |
| 1614 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
| 1615 | // Insert commas as we go... the first arg doesn't get a comma |
| 1616 | if (i) Out << ", "; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1617 | |
Chris Lattner | 8dcd2f1 | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1618 | // Output type... |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1619 | TypePrinter.print(FT->getParamType(i), Out); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1620 | |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1621 | Attributes ArgAttrs = Attrs.getParamAttributes(i+1); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1622 | if (ArgAttrs != Attribute::None) |
| 1623 | Out << ' ' << Attribute::getAsString(ArgAttrs); |
Chris Lattner | 8dcd2f1 | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1624 | } |
Reid Spencer | bd5db8e | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1625 | } |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Finish printing arguments... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1628 | if (FT->isVarArg()) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1629 | if (FT->getNumParams()) Out << ", "; |
| 1630 | Out << "..."; // Output varargs portion of signature! |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1631 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1632 | Out << ')'; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1633 | Attributes FnAttrs = Attrs.getFnAttributes(); |
| 1634 | if (FnAttrs != Attribute::None) |
| 1635 | Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes()); |
Chris Lattner | 8fff126 | 2010-07-07 23:16:37 +0000 | [diff] [blame] | 1636 | if (F->hasSection()) { |
| 1637 | Out << " section \""; |
| 1638 | PrintEscapedString(F->getSection(), Out); |
| 1639 | Out << '"'; |
| 1640 | } |
Chris Lattner | 30caa28 | 2005-11-06 06:48:53 +0000 | [diff] [blame] | 1641 | if (F->getAlignment()) |
| 1642 | Out << " align " << F->getAlignment(); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1643 | if (F->hasGC()) |
| 1644 | Out << " gc \"" << F->getGC() << '"'; |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1645 | if (F->isDeclaration()) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1646 | Out << "\n"; |
Chris Lattner | 03e2acb | 2002-05-06 03:00:40 +0000 | [diff] [blame] | 1647 | } else { |
Chris Lattner | a1b5858 | 2008-04-21 06:12:55 +0000 | [diff] [blame] | 1648 | Out << " {"; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1649 | |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 1650 | // Output all of its basic blocks... for the function |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1651 | for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 1652 | printBasicBlock(I); |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1653 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1654 | Out << "}\n"; |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Reid Spencer | 0d1b77e | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1657 | Machine.purgeFunction(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1660 | /// printArgument - This member is called for every argument that is passed into |
| 1661 | /// the function. Simply print it out |
| 1662 | /// |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1663 | void AssemblyWriter::printArgument(const Argument *Arg, |
Devang Patel | eaf42ab | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 1664 | Attributes Attrs) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1665 | // Output type... |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1666 | TypePrinter.print(Arg->getType(), Out); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1667 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1668 | // Output parameter attributes list |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1669 | if (Attrs != Attribute::None) |
| 1670 | Out << ' ' << Attribute::getAsString(Attrs); |
Reid Spencer | bd5db8e | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1671 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1672 | // Output name, if available... |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1673 | if (Arg->hasName()) { |
| 1674 | Out << ' '; |
| 1675 | PrintLLVMName(Out, Arg); |
| 1676 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1679 | /// printBasicBlock - This member is called for each basic block in a method. |
| 1680 | /// |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1681 | void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1682 | if (BB->hasName()) { // Print out the label if it exists... |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1683 | Out << "\n"; |
Daniel Dunbar | 03d7651 | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 1684 | PrintLLVMName(Out, BB->getName(), LabelPrefix); |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1685 | Out << ':'; |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1686 | } else if (!BB->use_empty()) { // Don't print block # of no uses... |
Chris Lattner | 96c5b2f | 2008-04-21 04:20:33 +0000 | [diff] [blame] | 1687 | Out << "\n; <label>:"; |
Chris Lattner | 22379bc | 2007-01-11 03:54:27 +0000 | [diff] [blame] | 1688 | int Slot = Machine.getLocalSlot(BB); |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1689 | if (Slot != -1) |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1690 | Out << Slot; |
Chris Lattner | 6956645 | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1691 | else |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1692 | Out << "<badref>"; |
Chris Lattner | 061269b | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 1693 | } |
Chris Lattner | 4e4d862 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1694 | |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1695 | if (BB->getParent() == 0) { |
Chris Lattner | 8f4b1ec | 2009-08-17 15:48:08 +0000 | [diff] [blame] | 1696 | Out.PadToColumn(50); |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1697 | Out << "; Error: Block without parent!"; |
| 1698 | } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block? |
Chris Lattner | eb41129 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1699 | // Output predecessors for the block... |
Chris Lattner | 8f4b1ec | 2009-08-17 15:48:08 +0000 | [diff] [blame] | 1700 | Out.PadToColumn(50); |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 1701 | Out << ";"; |
Gabor Greif | 4442464 | 2010-03-25 23:25:28 +0000 | [diff] [blame] | 1702 | const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1703 | |
Chris Lattner | eb41129 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1704 | if (PI == PE) { |
| 1705 | Out << " No predecessors!"; |
| 1706 | } else { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1707 | Out << " preds = "; |
Chris Lattner | eb41129 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1708 | writeOperand(*PI, false); |
| 1709 | for (++PI; PI != PE; ++PI) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1710 | Out << ", "; |
Chris Lattner | 2fcfdb7 | 2006-12-06 06:24:27 +0000 | [diff] [blame] | 1711 | writeOperand(*PI, false); |
Chris Lattner | 40efcec | 2003-11-16 22:59:57 +0000 | [diff] [blame] | 1712 | } |
Chris Lattner | 061269b | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 1713 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1714 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | eb41129 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1716 | Out << "\n"; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1717 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1718 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1719 | |
Chris Lattner | 007377f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1720 | // Output all of the instructions in the basic block... |
Dan Gohman | beca689 | 2009-07-13 18:27:59 +0000 | [diff] [blame] | 1721 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1722 | printInstruction(*I); |
Dan Gohman | beca689 | 2009-07-13 18:27:59 +0000 | [diff] [blame] | 1723 | Out << '\n'; |
| 1724 | } |
Chris Lattner | 9f717ef | 2004-03-08 18:51:45 +0000 | [diff] [blame] | 1725 | |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1726 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1729 | /// printInfoComment - Print a little comment after the instruction indicating |
| 1730 | /// which slot it occupies. |
| 1731 | /// |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1732 | void AssemblyWriter::printInfoComment(const Value &V) { |
Dan Gohman | 7a5666e | 2010-02-10 20:41:46 +0000 | [diff] [blame] | 1733 | if (AnnotationWriter) { |
| 1734 | AnnotationWriter->printInfoComment(V, Out); |
| 1735 | return; |
| 1736 | } |
| 1737 | |
Chris Lattner | 4ee93c4 | 2009-12-29 07:25:48 +0000 | [diff] [blame] | 1738 | if (V.getType()->isVoidTy()) return; |
| 1739 | |
| 1740 | Out.PadToColumn(50); |
| 1741 | Out << "; <"; |
| 1742 | TypePrinter.print(V.getType(), Out); |
| 1743 | Out << "> [#uses=" << V.getNumUses() << ']'; // Output # uses |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Reid Spencer | 3a9ec24 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 1746 | // This member is called for each Instruction in a function.. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1747 | void AssemblyWriter::printInstruction(const Instruction &I) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1748 | if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out); |
Chris Lattner | 95e5a2c | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1749 | |
Dan Gohman | 3845e50 | 2009-08-12 23:32:33 +0000 | [diff] [blame] | 1750 | // Print out indentation for an instruction. |
Dan Gohman | 01889ca | 2009-08-13 01:41:52 +0000 | [diff] [blame] | 1751 | Out << " "; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1752 | |
| 1753 | // Print out name if it exists... |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1754 | if (I.hasName()) { |
| 1755 | PrintLLVMName(Out, &I); |
| 1756 | Out << " = "; |
Chris Lattner | 4ee93c4 | 2009-12-29 07:25:48 +0000 | [diff] [blame] | 1757 | } else if (!I.getType()->isVoidTy()) { |
Chris Lattner | 828db8a | 2008-08-29 17:19:30 +0000 | [diff] [blame] | 1758 | // Print out the def slot taken. |
| 1759 | int SlotNum = Machine.getLocalSlot(&I); |
| 1760 | if (SlotNum == -1) |
| 1761 | Out << "<badref> = "; |
| 1762 | else |
| 1763 | Out << '%' << SlotNum << " = "; |
Chris Lattner | c97536e | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1764 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1765 | |
Chris Lattner | ddb6db4 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1766 | // If this is a volatile load or store, print out the volatile marker. |
Chris Lattner | e5e475e | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 1767 | if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) || |
Chris Lattner | ddb6db4 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1768 | (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1769 | Out << "volatile "; |
Chris Lattner | ddb6db4 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1770 | } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) { |
| 1771 | // If this is a call, check if it's a tail call. |
| 1772 | Out << "tail "; |
| 1773 | } |
Chris Lattner | e5e475e | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 1774 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1775 | // Print out the opcode... |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1776 | Out << I.getOpcodeName(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1777 | |
Dan Gohman | 59858cf | 2009-07-27 16:11:46 +0000 | [diff] [blame] | 1778 | // Print out optimization information. |
| 1779 | WriteOptimizationInfo(Out, &I); |
| 1780 | |
Reid Spencer | 74f1642 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 1781 | // Print out the compare instruction predicates |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1782 | if (const CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Chris Lattner | ab49ee7 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1783 | Out << ' ' << getPredicateText(CI->getPredicate()); |
Reid Spencer | 74f1642 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1785 | // Print out the type of the operands... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1786 | const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1787 | |
| 1788 | // Special case conditional branches to swizzle the condition out to the front |
Gabor Greif | ccd27fb | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1789 | if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) { |
| 1790 | BranchInst &BI(cast<BranchInst>(I)); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1791 | Out << ' '; |
Gabor Greif | ccd27fb | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1792 | writeOperand(BI.getCondition(), true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1793 | Out << ", "; |
Gabor Greif | ccd27fb | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1794 | writeOperand(BI.getSuccessor(0), true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1795 | Out << ", "; |
Gabor Greif | ccd27fb | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1796 | writeOperand(BI.getSuccessor(1), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1798 | } else if (isa<SwitchInst>(I)) { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 1799 | // Special case switch instruction to get formatting nice and correct. |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1800 | Out << ' '; |
Chris Lattner | ab49ee7 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1801 | writeOperand(Operand , true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1802 | Out << ", "; |
Chris Lattner | ab49ee7 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1803 | writeOperand(I.getOperand(1), true); |
| 1804 | Out << " ["; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1805 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1806 | for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Dan Gohman | 01889ca | 2009-08-13 01:41:52 +0000 | [diff] [blame] | 1807 | Out << "\n "; |
Chris Lattner | ab49ee7 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1808 | writeOperand(I.getOperand(op ), true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1809 | Out << ", "; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1810 | writeOperand(I.getOperand(op+1), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1811 | } |
Dan Gohman | 01889ca | 2009-08-13 01:41:52 +0000 | [diff] [blame] | 1812 | Out << "\n ]"; |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 1813 | } else if (isa<IndirectBrInst>(I)) { |
| 1814 | // Special case indirectbr instruction to get formatting nice and correct. |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 1815 | Out << ' '; |
| 1816 | writeOperand(Operand, true); |
Dan Gohman | 0ed1f42 | 2009-10-30 02:01:10 +0000 | [diff] [blame] | 1817 | Out << ", ["; |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 1818 | |
| 1819 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 1820 | if (i != 1) |
| 1821 | Out << ", "; |
| 1822 | writeOperand(I.getOperand(i), true); |
| 1823 | } |
| 1824 | Out << ']'; |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 1825 | } else if (isa<PHINode>(I)) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1826 | Out << ' '; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1827 | TypePrinter.print(I.getType(), Out); |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1828 | Out << ' '; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1829 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1830 | for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1831 | if (op) Out << ", "; |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1832 | Out << "[ "; |
| 1833 | writeOperand(I.getOperand(op ), false); Out << ", "; |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1834 | writeOperand(I.getOperand(op+1), false); Out << " ]"; |
Chris Lattner | c24d208 | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 1835 | } |
Dan Gohman | 995be7d | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1836 | } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1837 | Out << ' '; |
Dan Gohman | 995be7d | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1838 | writeOperand(I.getOperand(0), true); |
| 1839 | for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) |
| 1840 | Out << ", " << *i; |
| 1841 | } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1842 | Out << ' '; |
| 1843 | writeOperand(I.getOperand(0), true); Out << ", "; |
Dan Gohman | 995be7d | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1844 | writeOperand(I.getOperand(1), true); |
| 1845 | for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) |
| 1846 | Out << ", " << *i; |
Devang Patel | 57ef4f4 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 1847 | } else if (isa<ReturnInst>(I) && !Operand) { |
| 1848 | Out << " void"; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1849 | } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 1850 | // Print the calling convention being used. |
| 1851 | switch (CI->getCallingConv()) { |
| 1852 | case CallingConv::C: break; // default |
Chris Lattner | 0deaab8 | 2006-05-19 21:58:52 +0000 | [diff] [blame] | 1853 | case CallingConv::Fast: Out << " fastcc"; break; |
| 1854 | case CallingConv::Cold: Out << " coldcc"; break; |
Chris Lattner | b28a6dc | 2007-11-18 18:32:16 +0000 | [diff] [blame] | 1855 | case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break; |
Anton Korobeynikov | 385f5a9 | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1856 | case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break; |
Anton Korobeynikov | ded05e3 | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 1857 | case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break; |
Anton Korobeynikov | 385f5a9 | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1858 | case CallingConv::ARM_APCS: Out << " arm_apcscc "; break; |
| 1859 | case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break; |
| 1860 | case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break; |
Anton Korobeynikov | 211a14e | 2009-12-07 02:27:35 +0000 | [diff] [blame] | 1861 | case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1862 | default: Out << " cc" << CI->getCallingConv(); break; |
| 1863 | } |
| 1864 | |
Gabor Greif | 7bbdf0c | 2010-06-23 13:09:06 +0000 | [diff] [blame] | 1865 | Operand = CI->getCalledValue(); |
Reid Spencer | b138a06 | 2007-04-09 06:10:42 +0000 | [diff] [blame] | 1866 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1867 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1868 | const Type *RetTy = FTy->getReturnType(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1869 | const AttrListPtr &PAL = CI->getAttributes(); |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1870 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1871 | if (PAL.getRetAttributes() != Attribute::None) |
| 1872 | Out << ' ' << Attribute::getAsString(PAL.getRetAttributes()); |
| 1873 | |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1874 | // If possible, print out the short form of the call instruction. We can |
Chris Lattner | b579400 | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 1875 | // only do this if the first argument is a pointer to a nonvararg function, |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1876 | // and if the return type is not a pointer to a function. |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1877 | // |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1878 | Out << ' '; |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1879 | if (!FTy->isVarArg() && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1880 | (!RetTy->isPointerTy() || |
| 1881 | !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1882 | TypePrinter.print(RetTy, Out); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1883 | Out << ' '; |
Chris Lattner | 268de04 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1884 | writeOperand(Operand, false); |
| 1885 | } else { |
| 1886 | writeOperand(Operand, true); |
| 1887 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1888 | Out << '('; |
Gabor Greif | 7bbdf0c | 2010-06-23 13:09:06 +0000 | [diff] [blame] | 1889 | for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) { |
| 1890 | if (op > 0) |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1891 | Out << ", "; |
Gabor Greif | 7bbdf0c | 2010-06-23 13:09:06 +0000 | [diff] [blame] | 1892 | writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op + 1)); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1893 | } |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1894 | Out << ')'; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1895 | if (PAL.getFnAttributes() != Attribute::None) |
| 1896 | Out << ' ' << Attribute::getAsString(PAL.getFnAttributes()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1897 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 1898 | Operand = II->getCalledValue(); |
Reid Spencer | b138a06 | 2007-04-09 06:10:42 +0000 | [diff] [blame] | 1899 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1900 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1901 | const Type *RetTy = FTy->getReturnType(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1902 | const AttrListPtr &PAL = II->getAttributes(); |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1904 | // Print the calling convention being used. |
| 1905 | switch (II->getCallingConv()) { |
| 1906 | case CallingConv::C: break; // default |
Chris Lattner | 0deaab8 | 2006-05-19 21:58:52 +0000 | [diff] [blame] | 1907 | case CallingConv::Fast: Out << " fastcc"; break; |
| 1908 | case CallingConv::Cold: Out << " coldcc"; break; |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1909 | case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break; |
| 1910 | case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break; |
Anton Korobeynikov | ded05e3 | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 1911 | case CallingConv::X86_ThisCall: Out << " x86_thiscallcc"; break; |
Anton Korobeynikov | 385f5a9 | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1912 | case CallingConv::ARM_APCS: Out << " arm_apcscc "; break; |
| 1913 | case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break; |
| 1914 | case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break; |
Anton Korobeynikov | 211a14e | 2009-12-07 02:27:35 +0000 | [diff] [blame] | 1915 | case CallingConv::MSP430_INTR: Out << " msp430_intrcc "; break; |
Chris Lattner | d511898 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1916 | default: Out << " cc" << II->getCallingConv(); break; |
| 1917 | } |
| 1918 | |
Devang Patel | 652203f | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1919 | if (PAL.getRetAttributes() != Attribute::None) |
| 1920 | Out << ' ' << Attribute::getAsString(PAL.getRetAttributes()); |
| 1921 | |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1922 | // If possible, print out the short form of the invoke instruction. We can |
| 1923 | // only do this if the first argument is a pointer to a nonvararg function, |
| 1924 | // and if the return type is not a pointer to a function. |
| 1925 | // |
Dan Gohman | 2b6c3d9 | 2008-10-15 18:02:08 +0000 | [diff] [blame] | 1926 | Out << ' '; |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1927 | if (!FTy->isVarArg() && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1928 | (!RetTy->isPointerTy() || |
| 1929 | !cast<PointerType>(RetTy)->getElementType()->isFunctionTy())) { |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1930 | TypePrinter.print(RetTy, Out); |
Dan Gohman | 2b6c3d9 | 2008-10-15 18:02:08 +0000 | [diff] [blame] | 1931 | Out << ' '; |
Chris Lattner | 7a01229 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1932 | writeOperand(Operand, false); |
| 1933 | } else { |
| 1934 | writeOperand(Operand, true); |
| 1935 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1936 | Out << '('; |
Gabor Greif | 7bbdf0c | 2010-06-23 13:09:06 +0000 | [diff] [blame] | 1937 | for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) { |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 1938 | if (op) |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1939 | Out << ", "; |
Gabor Greif | 7bbdf0c | 2010-06-23 13:09:06 +0000 | [diff] [blame] | 1940 | writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op + 1)); |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1943 | Out << ')'; |
Devang Patel | 19c8746 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1944 | if (PAL.getFnAttributes() != Attribute::None) |
| 1945 | Out << ' ' << Attribute::getAsString(PAL.getFnAttributes()); |
| 1946 | |
Dan Gohman | 01889ca | 2009-08-13 01:41:52 +0000 | [diff] [blame] | 1947 | Out << "\n to "; |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1948 | writeOperand(II->getNormalDest(), true); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1949 | Out << " unwind "; |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 1950 | writeOperand(II->getUnwindDest(), true); |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1951 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1952 | } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1953 | Out << ' '; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1954 | TypePrinter.print(AI->getType()->getElementType(), Out); |
Dan Gohman | 69bff07 | 2009-07-31 18:23:24 +0000 | [diff] [blame] | 1955 | if (!AI->getArraySize() || AI->isArrayAllocation()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1956 | Out << ", "; |
Chris Lattner | 94dc1f2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1957 | writeOperand(AI->getArraySize(), true); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1958 | } |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 1959 | if (AI->getAlignment()) { |
Chris Lattner | 9fad0b9 | 2005-11-05 21:20:34 +0000 | [diff] [blame] | 1960 | Out << ", align " << AI->getAlignment(); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 1961 | } |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1962 | } else if (isa<CastInst>(I)) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1963 | if (Operand) { |
| 1964 | Out << ' '; |
| 1965 | writeOperand(Operand, true); // Work with broken code |
| 1966 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1967 | Out << " to "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1968 | TypePrinter.print(I.getType(), Out); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1969 | } else if (isa<VAArgInst>(I)) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1970 | if (Operand) { |
| 1971 | Out << ' '; |
| 1972 | writeOperand(Operand, true); // Work with broken code |
| 1973 | } |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1974 | Out << ", "; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1975 | TypePrinter.print(I.getType(), Out); |
| 1976 | } else if (Operand) { // Print the normal way. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1977 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1978 | // PrintAllTypes - Instructions who have operands of all the same type |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1979 | // omit the type from all but the first operand. If the instruction has |
| 1980 | // different type operands (for example br), then they are all printed. |
| 1981 | bool PrintAllTypes = false; |
| 1982 | const Type *TheType = Operand->getType(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1983 | |
Reid Spencer | ebe57e3 | 2007-02-02 13:54:55 +0000 | [diff] [blame] | 1984 | // Select, Store and ShuffleVector always print all types. |
Devang Patel | 6494768 | 2008-03-04 22:05:14 +0000 | [diff] [blame] | 1985 | if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I) |
| 1986 | || isa<ReturnInst>(I)) { |
Chris Lattner | ffd9bf4 | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1987 | PrintAllTypes = true; |
| 1988 | } else { |
| 1989 | for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) { |
| 1990 | Operand = I.getOperand(i); |
Nuno Lopes | 6ad2b2a | 2009-01-15 18:40:57 +0000 | [diff] [blame] | 1991 | // note that Operand shouldn't be null, but the test helps make dump() |
| 1992 | // more tolerant of malformed IR |
Nuno Lopes | a8c78a9 | 2009-01-14 17:51:41 +0000 | [diff] [blame] | 1993 | if (Operand && Operand->getType() != TheType) { |
Chris Lattner | ffd9bf4 | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1994 | PrintAllTypes = true; // We have differing types! Print them all! |
| 1995 | break; |
| 1996 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1997 | } |
| 1998 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1999 | |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 2000 | if (!PrintAllTypes) { |
Misha Brukman | 0313e0b | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 2001 | Out << ' '; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 2002 | TypePrinter.print(TheType, Out); |
Chris Lattner | c182499 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 2003 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2004 | |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 2005 | Out << ' '; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2006 | for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 2007 | if (i) Out << ", "; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 2008 | writeOperand(I.getOperand(i), PrintAllTypes); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2009 | } |
| 2010 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 2011 | |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 2012 | // Print post operand alignment for load/store. |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 2013 | if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) { |
| 2014 | Out << ", align " << cast<LoadInst>(I).getAlignment(); |
| 2015 | } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) { |
| 2016 | Out << ", align " << cast<StoreInst>(I).getAlignment(); |
| 2017 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2018 | |
Chris Lattner | 7d05c46 | 2009-12-28 20:10:43 +0000 | [diff] [blame] | 2019 | // Print Metadata info. |
Nick Lewycky | fa0c54e | 2010-02-25 06:53:04 +0000 | [diff] [blame] | 2020 | SmallVector<std::pair<unsigned, MDNode*>, 4> InstMD; |
| 2021 | I.getAllMetadata(InstMD); |
Erick Tryzelaar | f3d5c91 | 2010-03-02 05:32:52 +0000 | [diff] [blame] | 2022 | if (!InstMD.empty()) { |
| 2023 | SmallVector<StringRef, 8> MDNames; |
| 2024 | I.getType()->getContext().getMDKindNames(MDNames); |
| 2025 | for (unsigned i = 0, e = InstMD.size(); i != e; ++i) { |
| 2026 | unsigned Kind = InstMD[i].first; |
| 2027 | if (Kind < MDNames.size()) { |
| 2028 | Out << ", !" << MDNames[Kind]; |
| 2029 | } else { |
| 2030 | Out << ", !<unknown kind #" << Kind << ">"; |
| 2031 | } |
| 2032 | Out << " !" << Machine.getMetadataSlot(InstMD[i].second); |
Nick Lewycky | fa0c54e | 2010-02-25 06:53:04 +0000 | [diff] [blame] | 2033 | } |
Devang Patel | 7f93f4d | 2009-10-07 16:37:55 +0000 | [diff] [blame] | 2034 | } |
Chris Lattner | e02fa85 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 2035 | printInfoComment(I); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2038 | static void WriteMDNodeComment(const MDNode *Node, |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 2039 | formatted_raw_ostream &Out) { |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2040 | if (Node->getNumOperands() < 1) |
| 2041 | return; |
| 2042 | ConstantInt *CI = dyn_cast_or_null<ConstantInt>(Node->getOperand(0)); |
| 2043 | if (!CI) return; |
Dan Gohman | 66797ff | 2010-05-07 22:15:24 +0000 | [diff] [blame] | 2044 | APInt Val = CI->getValue(); |
| 2045 | APInt Tag = Val & ~APInt(Val.getBitWidth(), LLVMDebugVersionMask); |
| 2046 | if (Val.ult(LLVMDebugVersion)) |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2047 | return; |
| 2048 | |
| 2049 | Out.PadToColumn(50); |
| 2050 | if (Tag == dwarf::DW_TAG_auto_variable) |
| 2051 | Out << "; [ DW_TAG_auto_variable ]"; |
| 2052 | else if (Tag == dwarf::DW_TAG_arg_variable) |
| 2053 | Out << "; [ DW_TAG_arg_variable ]"; |
| 2054 | else if (Tag == dwarf::DW_TAG_return_variable) |
| 2055 | Out << "; [ DW_TAG_return_variable ]"; |
| 2056 | else if (Tag == dwarf::DW_TAG_vector_type) |
| 2057 | Out << "; [ DW_TAG_vector_type ]"; |
| 2058 | else if (Tag == dwarf::DW_TAG_user_base) |
| 2059 | Out << "; [ DW_TAG_user_base ]"; |
Dan Gohman | 66797ff | 2010-05-07 22:15:24 +0000 | [diff] [blame] | 2060 | else if (Tag.isIntN(32)) { |
| 2061 | if (const char *TagName = dwarf::TagString(Tag.getZExtValue())) |
| 2062 | Out << "; [ " << TagName << " ]"; |
| 2063 | } |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | void AssemblyWriter::writeAllMDNodes() { |
| 2067 | SmallVector<const MDNode *, 16> Nodes; |
Chris Lattner | 307c989 | 2009-12-31 02:20:11 +0000 | [diff] [blame] | 2068 | Nodes.resize(Machine.mdn_size()); |
| 2069 | for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end(); |
| 2070 | I != E; ++I) |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2071 | Nodes[I->second] = cast<MDNode>(I->first); |
| 2072 | |
| 2073 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 2074 | Out << '!' << i << " = metadata "; |
Chris Lattner | 2b4b1e2 | 2009-12-31 02:27:30 +0000 | [diff] [blame] | 2075 | printMDNodeBody(Nodes[i]); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | void AssemblyWriter::printMDNodeBody(const MDNode *Node) { |
Chris Lattner | 85b1912 | 2009-12-31 02:31:59 +0000 | [diff] [blame] | 2080 | WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2081 | WriteMDNodeComment(Node, Out); |
| 2082 | Out << "\n"; |
| 2083 | } |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2084 | |
| 2085 | //===----------------------------------------------------------------------===// |
| 2086 | // External Interface declarations |
| 2087 | //===----------------------------------------------------------------------===// |
| 2088 | |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 2089 | void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 0d9574a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 2090 | SlotTracker SlotTable(this); |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 2091 | formatted_raw_ostream OS(ROS); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2092 | AssemblyWriter W(OS, SlotTable, this, AAW); |
Chris Lattner | bd72b32 | 2009-12-31 02:23:35 +0000 | [diff] [blame] | 2093 | W.printModule(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Chris Lattner | 6d4306e | 2009-02-28 21:11:05 +0000 | [diff] [blame] | 2096 | void Type::print(raw_ostream &OS) const { |
| 2097 | if (this == 0) { |
| 2098 | OS << "<null Type>"; |
| 2099 | return; |
| 2100 | } |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 2101 | TypePrinting().print(this, OS); |
Chris Lattner | 75cf7cf | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Dan Gohman | 683e922 | 2009-08-12 17:23:50 +0000 | [diff] [blame] | 2104 | void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2105 | if (this == 0) { |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 2106 | ROS << "printing a <null> value\n"; |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2107 | return; |
| 2108 | } |
Dan Gohman | 1220e10 | 2009-08-12 20:56:03 +0000 | [diff] [blame] | 2109 | formatted_raw_ostream OS(ROS); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2110 | if (const Instruction *I = dyn_cast<Instruction>(this)) { |
| 2111 | const Function *F = I->getParent() ? I->getParent()->getParent() : 0; |
| 2112 | SlotTracker SlotTable(F); |
Chris Lattner | dbe85bf | 2009-12-31 08:23:09 +0000 | [diff] [blame] | 2113 | AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), AAW); |
Chris Lattner | bd72b32 | 2009-12-31 02:23:35 +0000 | [diff] [blame] | 2114 | W.printInstruction(*I); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2115 | } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) { |
| 2116 | SlotTracker SlotTable(BB->getParent()); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2117 | AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), AAW); |
Chris Lattner | bd72b32 | 2009-12-31 02:23:35 +0000 | [diff] [blame] | 2118 | W.printBasicBlock(BB); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2119 | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) { |
| 2120 | SlotTracker SlotTable(GV->getParent()); |
Dan Gohman | ba0941f | 2009-04-20 16:10:33 +0000 | [diff] [blame] | 2121 | AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW); |
Chris Lattner | bd72b32 | 2009-12-31 02:23:35 +0000 | [diff] [blame] | 2122 | if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV)) |
| 2123 | W.printGlobal(V); |
| 2124 | else if (const Function *F = dyn_cast<Function>(GV)) |
| 2125 | W.printFunction(F); |
| 2126 | else |
| 2127 | W.printAlias(cast<GlobalAlias>(GV)); |
Devang Patel | fcd65ae | 2009-07-01 20:59:15 +0000 | [diff] [blame] | 2128 | } else if (const MDNode *N = dyn_cast<MDNode>(this)) { |
Victor Hernandez | 8fffff5 | 2010-01-20 04:45:57 +0000 | [diff] [blame] | 2129 | const Function *F = N->getFunction(); |
Victor Hernandez | 559588b | 2010-01-14 01:47:37 +0000 | [diff] [blame] | 2130 | SlotTracker SlotTable(F); |
Dan Gohman | 79b78a4 | 2010-07-14 21:12:44 +0000 | [diff] [blame] | 2131 | AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW); |
Chris Lattner | 6e6b180 | 2009-12-31 02:13:35 +0000 | [diff] [blame] | 2132 | W.printMDNodeBody(N); |
Devang Patel | c29d5b3 | 2009-07-30 00:02:57 +0000 | [diff] [blame] | 2133 | } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(this)) { |
Chris Lattner | 38cf02e | 2009-12-31 01:36:50 +0000 | [diff] [blame] | 2134 | SlotTracker SlotTable(N->getParent()); |
Chris Lattner | fdb3356 | 2009-12-31 01:54:05 +0000 | [diff] [blame] | 2135 | AssemblyWriter W(OS, SlotTable, N->getParent(), AAW); |
| 2136 | W.printNamedMDNode(N); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2137 | } else if (const Constant *C = dyn_cast<Constant>(this)) { |
Chris Lattner | e9fa33e | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 2138 | TypePrinting TypePrinter; |
Chris Lattner | 0f7364b | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 2139 | TypePrinter.print(C->getType(), OS); |
Chris Lattner | 6d4306e | 2009-02-28 21:11:05 +0000 | [diff] [blame] | 2140 | OS << ' '; |
Dan Gohman | 40cf12f | 2010-07-14 20:57:55 +0000 | [diff] [blame] | 2141 | WriteConstantInternal(OS, C, TypePrinter, 0); |
Chris Lattner | 4a3d3a5 | 2009-12-31 01:41:14 +0000 | [diff] [blame] | 2142 | } else if (isa<InlineAsm>(this) || isa<MDString>(this) || |
| 2143 | isa<Argument>(this)) { |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2144 | WriteAsOperand(OS, this, true, 0); |
| 2145 | } else { |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 2146 | // Otherwise we don't know what it is. Call the virtual function to |
| 2147 | // allow a subclass to print itself. |
| 2148 | printCustom(OS); |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 2152 | // Value::printCustom - subclasses should override this to implement printing. |
| 2153 | void Value::printCustom(raw_ostream &OS) const { |
| 2154 | llvm_unreachable("Unknown value to print out!"); |
| 2155 | } |
| 2156 | |
Chris Lattner | 7059e53 | 2008-08-25 17:03:15 +0000 | [diff] [blame] | 2157 | // Value::dump - allow easy printing of Values from the debugger. |
David Greene | d865e02 | 2010-01-05 01:29:26 +0000 | [diff] [blame] | 2158 | void Value::dump() const { print(dbgs()); dbgs() << '\n'; } |
Reid Spencer | fa452c0 | 2004-05-25 18:14:38 +0000 | [diff] [blame] | 2159 | |
Chris Lattner | 7059e53 | 2008-08-25 17:03:15 +0000 | [diff] [blame] | 2160 | // Type::dump - allow easy printing of Types from the debugger. |
Chris Lattner | 795daec | 2008-10-01 20:16:19 +0000 | [diff] [blame] | 2161 | // This one uses type names from the given context module |
| 2162 | void Type::dump(const Module *Context) const { |
David Greene | d865e02 | 2010-01-05 01:29:26 +0000 | [diff] [blame] | 2163 | WriteTypeSymbolic(dbgs(), this, Context); |
| 2164 | dbgs() << '\n'; |
Chris Lattner | 795daec | 2008-10-01 20:16:19 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Chris Lattner | c287137 | 2009-02-28 21:05:51 +0000 | [diff] [blame] | 2167 | // Type::dump - allow easy printing of Types from the debugger. |
| 2168 | void Type::dump() const { dump(0); } |
| 2169 | |
Chris Lattner | 7059e53 | 2008-08-25 17:03:15 +0000 | [diff] [blame] | 2170 | // Module::dump() - Allow printing of Modules from the debugger. |
David Greene | d865e02 | 2010-01-05 01:29:26 +0000 | [diff] [blame] | 2171 | void Module::dump() const { print(dbgs(), 0); } |