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