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