Chris Lattner | f7e7948 | 2002-04-07 22:31:46 +0000 | [diff] [blame] | 1 | //===-- AsmWriter.cpp - Printing LLVM as an assembly file -----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 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 | 189088e | 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 | f70da10 | 2003-05-08 02:44:12 +0000 | [diff] [blame] | 13 | // LLVM code, because it can be used for debugging transformations. |
Chris Lattner | 189088e | 2002-04-12 18:21:53 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 7f8845a | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/PrintModulePass.h" |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 19 | #include "llvm/Assembly/AsmAnnotationWriter.h" |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 20 | #include "llvm/CallingConv.h" |
Chris Lattner | c70b3f6 | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Chris Lattner | 913d18f | 2002-04-29 18:46:50 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 8bbcda2 | 2006-01-25 18:57:27 +0000 | [diff] [blame] | 23 | #include "llvm/InlineAsm.h" |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 24 | #include "llvm/Instruction.h" |
Misha Brukman | 2d3fa9e | 2004-07-29 16:53:53 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 26 | #include "llvm/Operator.h" |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 27 | #include "llvm/MDNode.h" |
Chris Lattner | c70b3f6 | 2004-01-20 19:50:34 +0000 | [diff] [blame] | 28 | #include "llvm/Module.h" |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 29 | #include "llvm/ValueSymbolTable.h" |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 30 | #include "llvm/TypeSymbolTable.h" |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseSet.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringExtras.h" |
| 33 | #include "llvm/ADT/STLExtras.h" |
Bill Wendling | dfc9189 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CFG.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Jim Laskey | b74c666 | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 393b7cd | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Reid Spencer | bdf03b4 | 2007-05-22 19:27:35 +0000 | [diff] [blame] | 39 | #include <cctype> |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 40 | #include <map> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 42 | |
Reid Spencer | 294715b | 2005-05-15 16:13:11 +0000 | [diff] [blame] | 43 | // Make virtual table appear in this compilation unit. |
| 44 | AssemblyAnnotationWriter::~AssemblyAnnotationWriter() {} |
| 45 | |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | // Helper Functions |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
| 50 | static const Module *getModuleFromVal(const Value *V) { |
| 51 | if (const Argument *MA = dyn_cast<Argument>(V)) |
| 52 | return MA->getParent() ? MA->getParent()->getParent() : 0; |
| 53 | |
| 54 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 55 | return BB->getParent() ? BB->getParent()->getParent() : 0; |
| 56 | |
| 57 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 58 | const Function *M = I->getParent() ? I->getParent()->getParent() : 0; |
| 59 | return M ? M->getParent() : 0; |
| 60 | } |
| 61 | |
| 62 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 63 | return GV->getParent(); |
| 64 | return 0; |
| 65 | } |
| 66 | |
Daniel Dunbar | db0b70a | 2008-10-28 19:33:02 +0000 | [diff] [blame] | 67 | // PrintEscapedString - Print each character of the specified string, escaping |
| 68 | // it if it is not printable or if it is an escape char. |
| 69 | static void PrintEscapedString(const char *Str, unsigned Length, |
| 70 | raw_ostream &Out) { |
| 71 | for (unsigned i = 0; i != Length; ++i) { |
| 72 | unsigned char C = Str[i]; |
Nick Lewycky | 5aa592a | 2009-03-15 06:39:52 +0000 | [diff] [blame] | 73 | if (isprint(C) && C != '\\' && C != '"') |
Daniel Dunbar | db0b70a | 2008-10-28 19:33:02 +0000 | [diff] [blame] | 74 | Out << C; |
| 75 | else |
| 76 | Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // PrintEscapedString - Print each character of the specified string, escaping |
| 81 | // it if it is not printable or if it is an escape char. |
| 82 | static void PrintEscapedString(const std::string &Str, raw_ostream &Out) { |
| 83 | PrintEscapedString(Str.c_str(), Str.size(), Out); |
| 84 | } |
| 85 | |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 86 | enum PrefixType { |
| 87 | GlobalPrefix, |
| 88 | LabelPrefix, |
Daniel Dunbar | 389529a | 2008-10-14 23:28:09 +0000 | [diff] [blame] | 89 | LocalPrefix, |
| 90 | NoPrefix |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either |
| 94 | /// prefixed with % (if the string only contains simple characters) or is |
| 95 | /// surrounded with ""'s (if it has special chars in it). Print it out. |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 96 | static void PrintLLVMName(raw_ostream &OS, const char *NameStr, |
Chris Lattner | 1508d3f | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 97 | unsigned NameLen, PrefixType Prefix) { |
| 98 | assert(NameStr && "Cannot get empty name!"); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 99 | switch (Prefix) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 100 | default: llvm_unreachable("Bad prefix!"); |
Daniel Dunbar | 389529a | 2008-10-14 23:28:09 +0000 | [diff] [blame] | 101 | case NoPrefix: break; |
Chris Lattner | 1508d3f | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 102 | case GlobalPrefix: OS << '@'; break; |
| 103 | case LabelPrefix: break; |
| 104 | case LocalPrefix: OS << '%'; break; |
Nick Lewycky | 063699a | 2009-03-19 06:31:22 +0000 | [diff] [blame] | 105 | } |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 106 | |
| 107 | // Scan the name to see if it needs quotes first. |
Daniel Dunbar | db0b70a | 2008-10-28 19:33:02 +0000 | [diff] [blame] | 108 | bool NeedsQuotes = isdigit(NameStr[0]); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 109 | if (!NeedsQuotes) { |
| 110 | for (unsigned i = 0; i != NameLen; ++i) { |
| 111 | char C = NameStr[i]; |
| 112 | if (!isalnum(C) && C != '-' && C != '.' && C != '_') { |
| 113 | NeedsQuotes = true; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // If we didn't need any quotes, just write out the name in one blast. |
| 120 | if (!NeedsQuotes) { |
| 121 | OS.write(NameStr, NameLen); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // Okay, we need quotes. Output the quotes and escape any scary characters as |
| 126 | // needed. |
| 127 | OS << '"'; |
Daniel Dunbar | db0b70a | 2008-10-28 19:33:02 +0000 | [diff] [blame] | 128 | PrintEscapedString(NameStr, NameLen, OS); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 129 | OS << '"'; |
| 130 | } |
| 131 | |
| 132 | /// PrintLLVMName - Turn the specified name into an 'LLVM name', which is either |
| 133 | /// prefixed with % (if the string only contains simple characters) or is |
| 134 | /// surrounded with ""'s (if it has special chars in it). Print it out. |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 135 | static void PrintLLVMName(raw_ostream &OS, const Value *V) { |
Chris Lattner | 1508d3f | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 136 | PrintLLVMName(OS, V->getNameStart(), V->getNameLen(), |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 137 | isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix); |
| 138 | } |
| 139 | |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 140 | //===----------------------------------------------------------------------===// |
| 141 | // TypePrinting Class: Type printing machinery |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | |
Chris Lattner | 3339d7f | 2009-02-28 23:03:55 +0000 | [diff] [blame] | 144 | static DenseMap<const Type *, std::string> &getTypeNamesMap(void *M) { |
| 145 | return *static_cast<DenseMap<const Type *, std::string>*>(M); |
Chris Lattner | 3ad4682 | 2009-02-28 22:34:45 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void TypePrinting::clear() { |
| 149 | getTypeNamesMap(TypeNames).clear(); |
| 150 | } |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 92c5c12 | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 152 | bool TypePrinting::hasTypeName(const Type *Ty) const { |
| 153 | return getTypeNamesMap(TypeNames).count(Ty); |
| 154 | } |
| 155 | |
| 156 | void TypePrinting::addTypeName(const Type *Ty, const std::string &N) { |
| 157 | getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N)); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | TypePrinting::TypePrinting() { |
Chris Lattner | 3339d7f | 2009-02-28 23:03:55 +0000 | [diff] [blame] | 162 | TypeNames = new DenseMap<const Type *, std::string>(); |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Chris Lattner | 3ad4682 | 2009-02-28 22:34:45 +0000 | [diff] [blame] | 165 | TypePrinting::~TypePrinting() { |
| 166 | delete &getTypeNamesMap(TypeNames); |
| 167 | } |
| 168 | |
Chris Lattner | 40959d0 | 2009-02-28 20:49:40 +0000 | [diff] [blame] | 169 | /// CalcTypeName - Write the specified type to the specified raw_ostream, making |
| 170 | /// use of type names or up references to shorten the type name where possible. |
| 171 | void TypePrinting::CalcTypeName(const Type *Ty, |
Chris Lattner | 765338d | 2009-02-28 20:34:19 +0000 | [diff] [blame] | 172 | SmallVectorImpl<const Type *> &TypeStack, |
Chris Lattner | 242d91a | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 173 | raw_ostream &OS, bool IgnoreTopLevelName) { |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 174 | // Check to see if the type is named. |
Chris Lattner | 242d91a | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 175 | if (!IgnoreTopLevelName) { |
Nick Lewycky | 063699a | 2009-03-19 06:31:22 +0000 | [diff] [blame] | 176 | DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames); |
Chris Lattner | 242d91a | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 177 | DenseMap<const Type *, std::string>::iterator I = TM.find(Ty); |
| 178 | if (I != TM.end()) { |
| 179 | OS << I->second; |
| 180 | return; |
| 181 | } |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Check to see if the Type is already on the stack... |
| 185 | unsigned Slot = 0, CurSize = TypeStack.size(); |
| 186 | while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type |
| 187 | |
| 188 | // This is another base case for the recursion. In this case, we know |
| 189 | // that we have looped back to a type that we have previously visited. |
| 190 | // Generate the appropriate upreference to handle this. |
| 191 | if (Slot < CurSize) { |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 192 | OS << '\\' << unsigned(CurSize-Slot); // Here's the upreference |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
| 196 | TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. |
| 197 | |
| 198 | switch (Ty->getTypeID()) { |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 199 | case Type::VoidTyID: OS << "void"; break; |
| 200 | case Type::FloatTyID: OS << "float"; break; |
| 201 | case Type::DoubleTyID: OS << "double"; break; |
| 202 | case Type::X86_FP80TyID: OS << "x86_fp80"; break; |
| 203 | case Type::FP128TyID: OS << "fp128"; break; |
| 204 | case Type::PPC_FP128TyID: OS << "ppc_fp128"; break; |
| 205 | case Type::LabelTyID: OS << "label"; break; |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 206 | case Type::MetadataTyID: OS << "metadata"; break; |
Chris Lattner | 68318b1 | 2009-02-28 21:18:43 +0000 | [diff] [blame] | 207 | case Type::IntegerTyID: |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 208 | OS << 'i' << cast<IntegerType>(Ty)->getBitWidth(); |
Chris Lattner | 68318b1 | 2009-02-28 21:18:43 +0000 | [diff] [blame] | 209 | break; |
| 210 | |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 211 | case Type::FunctionTyID: { |
| 212 | const FunctionType *FTy = cast<FunctionType>(Ty); |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 213 | CalcTypeName(FTy->getReturnType(), TypeStack, OS); |
| 214 | OS << " ("; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 215 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 216 | E = FTy->param_end(); I != E; ++I) { |
| 217 | if (I != FTy->param_begin()) |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 218 | OS << ", "; |
| 219 | CalcTypeName(*I, TypeStack, OS); |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 221 | if (FTy->isVarArg()) { |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 222 | if (FTy->getNumParams()) OS << ", "; |
| 223 | OS << "..."; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 224 | } |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 225 | OS << ')'; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | case Type::StructTyID: { |
| 229 | const StructType *STy = cast<StructType>(Ty); |
| 230 | if (STy->isPacked()) |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 231 | OS << '<'; |
| 232 | OS << "{ "; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 233 | for (StructType::element_iterator I = STy->element_begin(), |
| 234 | E = STy->element_end(); I != E; ++I) { |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 235 | CalcTypeName(*I, TypeStack, OS); |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 236 | if (next(I) != STy->element_end()) |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 237 | OS << ','; |
| 238 | OS << ' '; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 240 | OS << '}'; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 241 | if (STy->isPacked()) |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 242 | OS << '>'; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | case Type::PointerTyID: { |
| 246 | const PointerType *PTy = cast<PointerType>(Ty); |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 247 | CalcTypeName(PTy->getElementType(), TypeStack, OS); |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 248 | if (unsigned AddressSpace = PTy->getAddressSpace()) |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 249 | OS << " addrspace(" << AddressSpace << ')'; |
| 250 | OS << '*'; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 251 | break; |
| 252 | } |
| 253 | case Type::ArrayTyID: { |
| 254 | const ArrayType *ATy = cast<ArrayType>(Ty); |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 255 | OS << '[' << ATy->getNumElements() << " x "; |
| 256 | CalcTypeName(ATy->getElementType(), TypeStack, OS); |
| 257 | OS << ']'; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | case Type::VectorTyID: { |
| 261 | const VectorType *PTy = cast<VectorType>(Ty); |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 262 | OS << "<" << PTy->getNumElements() << " x "; |
| 263 | CalcTypeName(PTy->getElementType(), TypeStack, OS); |
| 264 | OS << '>'; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | case Type::OpaqueTyID: |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 268 | OS << "opaque"; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 269 | break; |
| 270 | default: |
Chris Lattner | 06a2321 | 2009-02-28 21:27:31 +0000 | [diff] [blame] | 271 | OS << "<unrecognized-type>"; |
Chris Lattner | 07d8829 | 2009-02-28 20:35:42 +0000 | [diff] [blame] | 272 | break; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Lattner | 40959d0 | 2009-02-28 20:49:40 +0000 | [diff] [blame] | 275 | TypeStack.pop_back(); // Remove self from stack. |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /// printTypeInt - The internal guts of printing out a type that has a |
| 279 | /// potentially named portion. |
| 280 | /// |
Chris Lattner | 242d91a | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 281 | void TypePrinting::print(const Type *Ty, raw_ostream &OS, |
| 282 | bool IgnoreTopLevelName) { |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 283 | // Check to see if the type is named. |
Chris Lattner | 3339d7f | 2009-02-28 23:03:55 +0000 | [diff] [blame] | 284 | DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames); |
Chris Lattner | 242d91a | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 285 | if (!IgnoreTopLevelName) { |
| 286 | DenseMap<const Type*, std::string>::iterator I = TM.find(Ty); |
| 287 | if (I != TM.end()) { |
| 288 | OS << I->second; |
| 289 | return; |
| 290 | } |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // Otherwise we have a type that has not been named but is a derived type. |
| 294 | // Carefully recurse the type hierarchy to print out any contained symbolic |
| 295 | // names. |
Chris Lattner | 765338d | 2009-02-28 20:34:19 +0000 | [diff] [blame] | 296 | SmallVector<const Type *, 16> TypeStack; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 297 | std::string TypeName; |
Chris Lattner | 40959d0 | 2009-02-28 20:49:40 +0000 | [diff] [blame] | 298 | |
| 299 | raw_string_ostream TypeOS(TypeName); |
Chris Lattner | 242d91a | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 300 | CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName); |
Chris Lattner | 40959d0 | 2009-02-28 20:49:40 +0000 | [diff] [blame] | 301 | OS << TypeOS.str(); |
| 302 | |
| 303 | // Cache type name for later use. |
Chris Lattner | 242d91a | 2009-03-01 01:16:21 +0000 | [diff] [blame] | 304 | if (!IgnoreTopLevelName) |
| 305 | TM.insert(std::make_pair(Ty, TypeOS.str())); |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 308 | namespace { |
| 309 | class TypeFinder { |
| 310 | // To avoid walking constant expressions multiple times and other IR |
| 311 | // objects, we keep several helper maps. |
| 312 | DenseSet<const Value*> VisitedConstants; |
| 313 | DenseSet<const Type*> VisitedTypes; |
| 314 | |
| 315 | TypePrinting &TP; |
| 316 | std::vector<const Type*> &NumberedTypes; |
| 317 | public: |
| 318 | TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes) |
| 319 | : TP(tp), NumberedTypes(numberedTypes) {} |
| 320 | |
| 321 | void Run(const Module &M) { |
Chris Lattner | 8451648 | 2009-03-01 00:32:33 +0000 | [diff] [blame] | 322 | // Get types from the type symbol table. This gets opaque types referened |
| 323 | // only through derived named types. |
| 324 | const TypeSymbolTable &ST = M.getTypeSymbolTable(); |
| 325 | for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end(); |
| 326 | TI != E; ++TI) |
| 327 | IncorporateType(TI->second); |
| 328 | |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 329 | // Get types from global variables. |
| 330 | for (Module::const_global_iterator I = M.global_begin(), |
| 331 | E = M.global_end(); I != E; ++I) { |
| 332 | IncorporateType(I->getType()); |
| 333 | if (I->hasInitializer()) |
| 334 | IncorporateValue(I->getInitializer()); |
| 335 | } |
| 336 | |
| 337 | // Get types from aliases. |
| 338 | for (Module::const_alias_iterator I = M.alias_begin(), |
| 339 | E = M.alias_end(); I != E; ++I) { |
| 340 | IncorporateType(I->getType()); |
| 341 | IncorporateValue(I->getAliasee()); |
| 342 | } |
| 343 | |
| 344 | // Get types from functions. |
| 345 | for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) { |
| 346 | IncorporateType(FI->getType()); |
| 347 | |
| 348 | for (Function::const_iterator BB = FI->begin(), E = FI->end(); |
| 349 | BB != E;++BB) |
| 350 | for (BasicBlock::const_iterator II = BB->begin(), |
| 351 | E = BB->end(); II != E; ++II) { |
| 352 | const Instruction &I = *II; |
| 353 | // Incorporate the type of the instruction and all its operands. |
| 354 | IncorporateType(I.getType()); |
| 355 | for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end(); |
| 356 | OI != OE; ++OI) |
| 357 | IncorporateValue(*OI); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | private: |
| 363 | void IncorporateType(const Type *Ty) { |
| 364 | // Check to see if we're already visited this type. |
Chris Lattner | 8451648 | 2009-03-01 00:32:33 +0000 | [diff] [blame] | 365 | if (!VisitedTypes.insert(Ty).second) |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 366 | return; |
| 367 | |
| 368 | // If this is a structure or opaque type, add a name for the type. |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 369 | if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements()) |
| 370 | || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) { |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 371 | TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size()))); |
| 372 | NumberedTypes.push_back(Ty); |
| 373 | } |
| 374 | |
| 375 | // Recursively walk all contained types. |
| 376 | for (Type::subtype_iterator I = Ty->subtype_begin(), |
| 377 | E = Ty->subtype_end(); I != E; ++I) |
| 378 | IncorporateType(*I); |
| 379 | } |
| 380 | |
| 381 | /// IncorporateValue - This method is used to walk operand lists finding |
| 382 | /// types hiding in constant expressions and other operands that won't be |
| 383 | /// walked in other ways. GlobalValues, basic blocks, instructions, and |
| 384 | /// inst operands are all explicitly enumerated. |
| 385 | void IncorporateValue(const Value *V) { |
| 386 | if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return; |
| 387 | |
| 388 | // Already visited? |
| 389 | if (!VisitedConstants.insert(V).second) |
| 390 | return; |
| 391 | |
| 392 | // Check this type. |
| 393 | IncorporateType(V->getType()); |
| 394 | |
| 395 | // Look in operands for types. |
| 396 | const Constant *C = cast<Constant>(V); |
| 397 | for (Constant::const_op_iterator I = C->op_begin(), |
| 398 | E = C->op_end(); I != E;++I) |
| 399 | IncorporateValue(*I); |
| 400 | } |
| 401 | }; |
| 402 | } // end anonymous namespace |
| 403 | |
| 404 | |
| 405 | /// AddModuleTypesToPrinter - Add all of the symbolic type names for types in |
| 406 | /// the specified module to the TypePrinter and all numbered types to it and the |
| 407 | /// NumberedTypes table. |
| 408 | static void AddModuleTypesToPrinter(TypePrinting &TP, |
| 409 | std::vector<const Type*> &NumberedTypes, |
| 410 | const Module *M) { |
Chris Lattner | 92c5c12 | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 411 | if (M == 0) return; |
| 412 | |
| 413 | // If the module has a symbol table, take all global types and stuff their |
| 414 | // names into the TypeNames map. |
| 415 | const TypeSymbolTable &ST = M->getTypeSymbolTable(); |
| 416 | for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end(); |
| 417 | TI != E; ++TI) { |
| 418 | const Type *Ty = cast<Type>(TI->second); |
| 419 | |
| 420 | // As a heuristic, don't insert pointer to primitive types, because |
| 421 | // they are used too often to have a single useful name. |
| 422 | if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
| 423 | const Type *PETy = PTy->getElementType(); |
| 424 | if ((PETy->isPrimitiveType() || PETy->isInteger()) && |
| 425 | !isa<OpaqueType>(PETy)) |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | // Likewise don't insert primitives either. |
| 430 | if (Ty->isInteger() || Ty->isPrimitiveType()) |
| 431 | continue; |
| 432 | |
| 433 | // Get the name as a string and insert it into TypeNames. |
| 434 | std::string NameStr; |
| 435 | raw_string_ostream NameOS(NameStr); |
| 436 | PrintLLVMName(NameOS, TI->first.c_str(), TI->first.length(), LocalPrefix); |
| 437 | TP.addTypeName(Ty, NameOS.str()); |
| 438 | } |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 439 | |
| 440 | // Walk the entire module to find references to unnamed structure and opaque |
| 441 | // types. This is required for correctness by opaque types (because multiple |
| 442 | // uses of an unnamed opaque type needs to be referred to by the same ID) and |
| 443 | // it shrinks complex recursive structure types substantially in some cases. |
| 444 | TypeFinder(TP, NumberedTypes).Run(*M); |
Chris Lattner | 92c5c12 | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 448 | /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic |
| 449 | /// type, iff there is an entry in the modules symbol table for the specified |
Chris Lattner | 2402526 | 2009-02-28 21:05:51 +0000 | [diff] [blame] | 450 | /// type or one of it's component types. |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 451 | /// |
Chris Lattner | 92c5c12 | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 452 | void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) { |
| 453 | TypePrinting Printer; |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 454 | std::vector<const Type*> NumberedTypes; |
| 455 | AddModuleTypesToPrinter(Printer, NumberedTypes, M); |
Chris Lattner | 92c5c12 | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 456 | Printer.print(Ty, OS); |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 459 | //===----------------------------------------------------------------------===// |
| 460 | // SlotTracker Class: Enumerate slot numbers for unnamed values |
| 461 | //===----------------------------------------------------------------------===// |
| 462 | |
Chris Lattner | 3ee5876 | 2008-08-19 04:28:07 +0000 | [diff] [blame] | 463 | namespace { |
| 464 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 465 | /// This class provides computation of slot numbers for LLVM Assembly writing. |
Chris Lattner | 393b7cd | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 466 | /// |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 467 | class SlotTracker { |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 468 | public: |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 469 | /// ValueMap - A mapping of Values to slot numbers. |
Chris Lattner | a204d41 | 2008-08-17 17:25:25 +0000 | [diff] [blame] | 470 | typedef DenseMap<const Value*, unsigned> ValueMap; |
Chris Lattner | 393b7cd | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 471 | |
| 472 | private: |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 473 | /// TheModule - The module for which we are holding slot numbers. |
Chris Lattner | 393b7cd | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 474 | const Module* TheModule; |
| 475 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 476 | /// TheFunction - The function for which we are holding slot numbers. |
Chris Lattner | 393b7cd | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 477 | const Function* TheFunction; |
| 478 | bool FunctionProcessed; |
| 479 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 480 | /// TheMDNode - The MDNode for which we are holding slot numbers. |
| 481 | const MDNode *TheMDNode; |
| 482 | |
| 483 | /// mMap - The TypePlanes map for the module level data. |
Chris Lattner | 393b7cd | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 484 | ValueMap mMap; |
| 485 | unsigned mNext; |
| 486 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 487 | /// fMap - The TypePlanes map for the function level data. |
Chris Lattner | 393b7cd | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 488 | ValueMap fMap; |
| 489 | unsigned fNext; |
| 490 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 491 | /// mdnMap - Map for MDNodes. |
| 492 | ValueMap mdnMap; |
| 493 | unsigned mdnNext; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 494 | public: |
Chris Lattner | 393b7cd | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 495 | /// Construct from a module |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 496 | explicit SlotTracker(const Module *M); |
Chris Lattner | 393b7cd | 2008-08-17 04:17:45 +0000 | [diff] [blame] | 497 | /// Construct from a function, starting out in incorp state. |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 498 | explicit SlotTracker(const Function *F); |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 499 | /// Construct from a mdnode. |
| 500 | explicit SlotTracker(const MDNode *N); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 501 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 502 | /// Return the slot number of the specified value in it's type |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 503 | /// plane. If something is not in the SlotTracker, return -1. |
Chris Lattner | 5e04332 | 2007-01-11 03:54:27 +0000 | [diff] [blame] | 504 | int getLocalSlot(const Value *V); |
| 505 | int getGlobalSlot(const GlobalValue *V); |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 506 | int getMetadataSlot(const MDNode *N); |
Reid Spencer | 8beac69 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 507 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 508 | /// If you'd like to deal with a function instead of just a module, use |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 509 | /// this method to get its data into the SlotTracker. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 510 | void incorporateFunction(const Function *F) { |
| 511 | TheFunction = F; |
Reid Spencer | b0ac8c4 | 2004-08-16 07:46:33 +0000 | [diff] [blame] | 512 | FunctionProcessed = false; |
| 513 | } |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 514 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 515 | /// After calling incorporateFunction, use this method to remove the |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 516 | /// most recently incorporated function from the SlotTracker. This |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 517 | /// will reset the state of the machine back to just the module contents. |
| 518 | void purgeFunction(); |
| 519 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 520 | /// MDNode map iterators. |
| 521 | ValueMap::iterator mdnBegin() { return mdnMap.begin(); } |
| 522 | ValueMap::iterator mdnEnd() { return mdnMap.end(); } |
| 523 | unsigned mdnSize() { return mdnMap.size(); } |
| 524 | |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 525 | /// This function does the actual initialization. |
| 526 | inline void initialize(); |
| 527 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 528 | // Implementation Details |
| 529 | private: |
Chris Lattner | ea862a3 | 2007-01-09 07:55:49 +0000 | [diff] [blame] | 530 | /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. |
| 531 | void CreateModuleSlot(const GlobalValue *V); |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 532 | |
| 533 | /// CreateMetadataSlot - Insert the specified MDNode* into the slot table. |
| 534 | void CreateMetadataSlot(const MDNode *N); |
| 535 | |
Chris Lattner | ea862a3 | 2007-01-09 07:55:49 +0000 | [diff] [blame] | 536 | /// CreateFunctionSlot - Insert the specified Value* into the slot table. |
| 537 | void CreateFunctionSlot(const Value *V); |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 538 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 539 | /// Add all of the module level global variables (and their initializers) |
| 540 | /// and function declarations, but not the contents of those functions. |
| 541 | void processModule(); |
| 542 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 543 | /// Add all of the functions arguments, basic blocks, and instructions. |
Reid Spencer | 56010e4 | 2004-05-26 21:56:09 +0000 | [diff] [blame] | 544 | void processFunction(); |
| 545 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 546 | /// Add all MDNode operands. |
| 547 | void processMDNode(); |
| 548 | |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 549 | SlotTracker(const SlotTracker &); // DO NOT IMPLEMENT |
| 550 | void operator=(const SlotTracker &); // DO NOT IMPLEMENT |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 551 | }; |
| 552 | |
Chris Lattner | 3ee5876 | 2008-08-19 04:28:07 +0000 | [diff] [blame] | 553 | } // end anonymous namespace |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 555 | |
| 556 | static SlotTracker *createSlotTracker(const Value *V) { |
| 557 | if (const Argument *FA = dyn_cast<Argument>(V)) |
| 558 | return new SlotTracker(FA->getParent()); |
| 559 | |
| 560 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 561 | return new SlotTracker(I->getParent()->getParent()); |
| 562 | |
| 563 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 564 | return new SlotTracker(BB->getParent()); |
| 565 | |
| 566 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 567 | return new SlotTracker(GV->getParent()); |
| 568 | |
| 569 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
| 570 | return new SlotTracker(GA->getParent()); |
| 571 | |
| 572 | if (const Function *Func = dyn_cast<Function>(V)) |
| 573 | return new SlotTracker(Func); |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | #if 0 |
Dan Gohman | 1ddf98a | 2009-07-25 01:43:01 +0000 | [diff] [blame^] | 579 | #define ST_DEBUG(X) errs() << X |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 580 | #else |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 581 | #define ST_DEBUG(X) |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 582 | #endif |
| 583 | |
| 584 | // Module level constructor. Causes the contents of the Module (sans functions) |
| 585 | // to be added to the slot table. |
| 586 | SlotTracker::SlotTracker(const Module *M) |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 587 | : TheModule(M), TheFunction(0), FunctionProcessed(false), TheMDNode(0), |
| 588 | mNext(0), fNext(0), mdnNext(0) { |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // Function level constructor. Causes the contents of the Module and the one |
| 592 | // function provided to be added to the slot table. |
| 593 | SlotTracker::SlotTracker(const Function *F) |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 594 | : TheModule(F ? F->getParent() : 0), TheFunction(F), FunctionProcessed(false), |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 595 | TheMDNode(0), mNext(0), fNext(0) { |
| 596 | } |
| 597 | |
| 598 | // Constructor to handle single MDNode. |
| 599 | SlotTracker::SlotTracker(const MDNode *C) |
| 600 | : TheModule(0), TheFunction(0), FunctionProcessed(false), TheMDNode(C), |
| 601 | mNext(0), fNext(0), mdnNext(0) { |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | inline void SlotTracker::initialize() { |
| 605 | if (TheModule) { |
| 606 | processModule(); |
| 607 | TheModule = 0; ///< Prevent re-processing next time we're called. |
| 608 | } |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 610 | if (TheFunction && !FunctionProcessed) |
| 611 | processFunction(); |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 612 | |
| 613 | if (TheMDNode) |
| 614 | processMDNode(); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // Iterate through all the global variables, functions, and global |
| 618 | // variable initializers and create slots for them. |
| 619 | void SlotTracker::processModule() { |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 620 | ST_DEBUG("begin processModule!\n"); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 621 | |
| 622 | // Add all of the unnamed global variables to the value table. |
| 623 | for (Module::const_global_iterator I = TheModule->global_begin(), |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 624 | E = TheModule->global_end(); I != E; ++I) { |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 625 | if (!I->hasName()) |
| 626 | CreateModuleSlot(I); |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 627 | if (I->hasInitializer()) { |
| 628 | if (MDNode *N = dyn_cast<MDNode>(I->getInitializer())) |
| 629 | CreateMetadataSlot(N); |
| 630 | } |
| 631 | } |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 632 | |
| 633 | // Add all the unnamed functions to the table. |
| 634 | for (Module::const_iterator I = TheModule->begin(), E = TheModule->end(); |
| 635 | I != E; ++I) |
| 636 | if (!I->hasName()) |
| 637 | CreateModuleSlot(I); |
| 638 | |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 639 | ST_DEBUG("end processModule!\n"); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 642 | // Process the arguments, basic blocks, and instructions of a function. |
| 643 | void SlotTracker::processFunction() { |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 644 | ST_DEBUG("begin processFunction!\n"); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 645 | fNext = 0; |
| 646 | |
| 647 | // Add all the function arguments with no names. |
| 648 | for(Function::const_arg_iterator AI = TheFunction->arg_begin(), |
| 649 | AE = TheFunction->arg_end(); AI != AE; ++AI) |
| 650 | if (!AI->hasName()) |
| 651 | CreateFunctionSlot(AI); |
| 652 | |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 653 | ST_DEBUG("Inserting Instructions:\n"); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 654 | |
| 655 | // Add all of the basic blocks and instructions with no names. |
| 656 | for (Function::const_iterator BB = TheFunction->begin(), |
| 657 | E = TheFunction->end(); BB != E; ++BB) { |
| 658 | if (!BB->hasName()) |
| 659 | CreateFunctionSlot(BB); |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 660 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; |
| 661 | ++I) { |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 662 | if (I->getType() != Type::VoidTy && !I->hasName()) |
| 663 | CreateFunctionSlot(I); |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 664 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 665 | if (MDNode *N = dyn_cast<MDNode>(I->getOperand(i))) |
| 666 | CreateMetadataSlot(N); |
| 667 | } |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | FunctionProcessed = true; |
| 671 | |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 672 | ST_DEBUG("end processFunction!\n"); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 675 | /// processMDNode - Process TheMDNode. |
| 676 | void SlotTracker::processMDNode() { |
| 677 | ST_DEBUG("begin processMDNode!\n"); |
| 678 | mdnNext = 0; |
| 679 | CreateMetadataSlot(TheMDNode); |
| 680 | TheMDNode = 0; |
| 681 | ST_DEBUG("end processMDNode!\n"); |
| 682 | } |
| 683 | |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 684 | /// Clean up after incorporating a function. This is the only way to get out of |
| 685 | /// the function incorporation state that affects get*Slot/Create*Slot. Function |
| 686 | /// incorporation state is indicated by TheFunction != 0. |
| 687 | void SlotTracker::purgeFunction() { |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 688 | ST_DEBUG("begin purgeFunction!\n"); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 689 | fMap.clear(); // Simply discard the function level map |
| 690 | TheFunction = 0; |
| 691 | FunctionProcessed = false; |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 692 | ST_DEBUG("end purgeFunction!\n"); |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | /// getGlobalSlot - Get the slot number of a global value. |
| 696 | int SlotTracker::getGlobalSlot(const GlobalValue *V) { |
| 697 | // Check for uninitialized state and do lazy initialization. |
| 698 | initialize(); |
| 699 | |
| 700 | // Find the type plane in the module map |
| 701 | ValueMap::iterator MI = mMap.find(V); |
Dan Gohman | 1dd2757 | 2008-10-01 19:58:59 +0000 | [diff] [blame] | 702 | return MI == mMap.end() ? -1 : (int)MI->second; |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 705 | /// getGlobalSlot - Get the slot number of a MDNode. |
| 706 | int SlotTracker::getMetadataSlot(const MDNode *N) { |
| 707 | // Check for uninitialized state and do lazy initialization. |
| 708 | initialize(); |
| 709 | |
| 710 | // Find the type plane in the module map |
| 711 | ValueMap::iterator MI = mdnMap.find(N); |
| 712 | return MI == mdnMap.end() ? -1 : (int)MI->second; |
| 713 | } |
| 714 | |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 715 | |
| 716 | /// getLocalSlot - Get the slot number for a value that is local to a function. |
| 717 | int SlotTracker::getLocalSlot(const Value *V) { |
| 718 | assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!"); |
| 719 | |
| 720 | // Check for uninitialized state and do lazy initialization. |
| 721 | initialize(); |
| 722 | |
| 723 | ValueMap::iterator FI = fMap.find(V); |
Dan Gohman | 1dd2757 | 2008-10-01 19:58:59 +0000 | [diff] [blame] | 724 | return FI == fMap.end() ? -1 : (int)FI->second; |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
| 728 | /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. |
| 729 | void SlotTracker::CreateModuleSlot(const GlobalValue *V) { |
| 730 | assert(V && "Can't insert a null Value into SlotTracker!"); |
| 731 | assert(V->getType() != Type::VoidTy && "Doesn't need a slot!"); |
| 732 | assert(!V->hasName() && "Doesn't need a slot!"); |
| 733 | |
| 734 | unsigned DestSlot = mNext++; |
| 735 | mMap[V] = DestSlot; |
| 736 | |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 737 | ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 738 | DestSlot << " ["); |
| 739 | // G = Global, F = Function, A = Alias, o = other |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 740 | ST_DEBUG((isa<GlobalVariable>(V) ? 'G' : |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 741 | (isa<Function>(V) ? 'F' : |
| 742 | (isa<GlobalAlias>(V) ? 'A' : 'o'))) << "]\n"); |
| 743 | } |
| 744 | |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 745 | /// CreateSlot - Create a new slot for the specified value if it has no name. |
| 746 | void SlotTracker::CreateFunctionSlot(const Value *V) { |
| 747 | assert(V->getType() != Type::VoidTy && !V->hasName() && |
| 748 | "Doesn't need a slot!"); |
| 749 | |
| 750 | unsigned DestSlot = fNext++; |
| 751 | fMap[V] = DestSlot; |
| 752 | |
| 753 | // G = Global, F = Function, o = other |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 754 | ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 755 | DestSlot << " [o]\n"); |
| 756 | } |
| 757 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 758 | /// CreateModuleSlot - Insert the specified MDNode* into the slot table. |
| 759 | void SlotTracker::CreateMetadataSlot(const MDNode *N) { |
| 760 | assert(N && "Can't insert a null Value into SlotTracker!"); |
| 761 | |
| 762 | ValueMap::iterator I = mdnMap.find(N); |
| 763 | if (I != mdnMap.end()) |
| 764 | return; |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 765 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 766 | unsigned DestSlot = mdnNext++; |
| 767 | mdnMap[N] = DestSlot; |
| 768 | |
| 769 | for (MDNode::const_elem_iterator MDI = N->elem_begin(), |
| 770 | MDE = N->elem_end(); MDI != MDE; ++MDI) { |
| 771 | const Value *TV = *MDI; |
| 772 | if (TV) |
| 773 | if (const MDNode *N2 = dyn_cast<MDNode>(TV)) |
| 774 | CreateMetadataSlot(N2); |
| 775 | } |
| 776 | } |
Chris Lattner | 3eee99c | 2008-08-19 04:36:02 +0000 | [diff] [blame] | 777 | |
| 778 | //===----------------------------------------------------------------------===// |
| 779 | // AsmWriter Implementation |
| 780 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7f8845a | 2002-07-23 18:07:49 +0000 | [diff] [blame] | 781 | |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 782 | static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 783 | TypePrinting &TypePrinter, |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 784 | SlotTracker *Machine); |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 785 | |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 786 | |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 787 | |
Chris Lattner | fc9f1c9 | 2006-12-06 06:40:49 +0000 | [diff] [blame] | 788 | static const char *getPredicateText(unsigned predicate) { |
Reid Spencer | 812a1be | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 789 | const char * pred = "unknown"; |
| 790 | switch (predicate) { |
| 791 | case FCmpInst::FCMP_FALSE: pred = "false"; break; |
| 792 | case FCmpInst::FCMP_OEQ: pred = "oeq"; break; |
| 793 | case FCmpInst::FCMP_OGT: pred = "ogt"; break; |
| 794 | case FCmpInst::FCMP_OGE: pred = "oge"; break; |
| 795 | case FCmpInst::FCMP_OLT: pred = "olt"; break; |
| 796 | case FCmpInst::FCMP_OLE: pred = "ole"; break; |
| 797 | case FCmpInst::FCMP_ONE: pred = "one"; break; |
| 798 | case FCmpInst::FCMP_ORD: pred = "ord"; break; |
| 799 | case FCmpInst::FCMP_UNO: pred = "uno"; break; |
| 800 | case FCmpInst::FCMP_UEQ: pred = "ueq"; break; |
| 801 | case FCmpInst::FCMP_UGT: pred = "ugt"; break; |
| 802 | case FCmpInst::FCMP_UGE: pred = "uge"; break; |
| 803 | case FCmpInst::FCMP_ULT: pred = "ult"; break; |
| 804 | case FCmpInst::FCMP_ULE: pred = "ule"; break; |
| 805 | case FCmpInst::FCMP_UNE: pred = "une"; break; |
| 806 | case FCmpInst::FCMP_TRUE: pred = "true"; break; |
| 807 | case ICmpInst::ICMP_EQ: pred = "eq"; break; |
| 808 | case ICmpInst::ICMP_NE: pred = "ne"; break; |
| 809 | case ICmpInst::ICMP_SGT: pred = "sgt"; break; |
| 810 | case ICmpInst::ICMP_SGE: pred = "sge"; break; |
| 811 | case ICmpInst::ICMP_SLT: pred = "slt"; break; |
| 812 | case ICmpInst::ICMP_SLE: pred = "sle"; break; |
| 813 | case ICmpInst::ICMP_UGT: pred = "ugt"; break; |
| 814 | case ICmpInst::ICMP_UGE: pred = "uge"; break; |
| 815 | case ICmpInst::ICMP_ULT: pred = "ult"; break; |
| 816 | case ICmpInst::ICMP_ULE: pred = "ule"; break; |
| 817 | } |
| 818 | return pred; |
| 819 | } |
| 820 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 821 | static void WriteMDNodes(raw_ostream &Out, TypePrinting &TypePrinter, |
| 822 | SlotTracker &Machine) { |
| 823 | SmallVector<const MDNode *, 16> Nodes; |
| 824 | Nodes.resize(Machine.mdnSize()); |
| 825 | for (SlotTracker::ValueMap::iterator I = |
| 826 | Machine.mdnBegin(), E = Machine.mdnEnd(); I != E; ++I) |
| 827 | Nodes[I->second] = cast<MDNode>(I->first); |
| 828 | |
| 829 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
Devang Patel | f04d63a | 2009-07-08 21:57:07 +0000 | [diff] [blame] | 830 | Out << '!' << i << " = metadata "; |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 831 | const MDNode *Node = Nodes[i]; |
| 832 | Out << "!{"; |
| 833 | for (MDNode::const_elem_iterator NI = Node->elem_begin(), |
| 834 | NE = Node->elem_end(); NI != NE;) { |
| 835 | const Value *V = *NI; |
| 836 | if (!V) |
| 837 | Out << "null"; |
| 838 | else if (const MDNode *N = dyn_cast<MDNode>(V)) { |
| 839 | Out << "metadata "; |
| 840 | Out << '!' << Machine.getMetadataSlot(N); |
| 841 | } |
| 842 | else { |
| 843 | TypePrinter.print((*NI)->getType(), Out); |
| 844 | Out << ' '; |
| 845 | WriteAsOperandInternal(Out, *NI, TypePrinter, &Machine); |
| 846 | } |
| 847 | if (++NI != NE) |
| 848 | Out << ", "; |
| 849 | } |
| 850 | Out << "}\n"; |
| 851 | } |
| 852 | } |
| 853 | |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 854 | static void WriteOptimizationInfo(raw_ostream &Out, const User *U) { |
| 855 | if (const OverflowingBinaryOperator *OBO = |
| 856 | dyn_cast<OverflowingBinaryOperator>(U)) { |
| 857 | if (OBO->hasNoUnsignedOverflow()) |
Dan Gohman | 902dfff | 2009-07-22 22:44:56 +0000 | [diff] [blame] | 858 | Out << "nuw "; |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 859 | if (OBO->hasNoSignedOverflow()) |
Dan Gohman | 902dfff | 2009-07-22 22:44:56 +0000 | [diff] [blame] | 860 | Out << "nsw "; |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 861 | } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(U)) { |
| 862 | if (Div->isExact()) |
| 863 | Out << "exact "; |
| 864 | } |
| 865 | } |
| 866 | |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 867 | static void WriteConstantInt(raw_ostream &Out, const Constant *CV, |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 868 | TypePrinting &TypePrinter, SlotTracker *Machine) { |
Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 869 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 870 | if (CI->getType() == Type::Int1Ty) { |
Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 871 | Out << (CI->getZExtValue() ? "true" : "false"); |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 872 | return; |
| 873 | } |
| 874 | Out << CI->getValue(); |
| 875 | return; |
| 876 | } |
| 877 | |
| 878 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 879 | if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble || |
| 880 | &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) { |
| 881 | // We would like to output the FP constant value in exponential notation, |
| 882 | // but we cannot do this if doing so will lose precision. Check here to |
| 883 | // make sure that we only output it in exponential format if we can parse |
| 884 | // the value back and get the same value. |
| 885 | // |
Dale Johannesen | 1f86498 | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 886 | bool ignored; |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 887 | bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble; |
Chris Lattner | 17f7165 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 888 | double Val = isDouble ? CFP->getValueAPF().convertToDouble() : |
| 889 | CFP->getValueAPF().convertToFloat(); |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 890 | std::string StrVal = ftostr(CFP->getValueAPF()); |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 891 | |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 892 | // Check to make sure that the stringized number is not some string like |
| 893 | // "Inf" or NaN, that atof will accept, but the lexer will not. Check |
| 894 | // that the string matches the "[-+]?[0-9]" regex. |
| 895 | // |
| 896 | if ((StrVal[0] >= '0' && StrVal[0] <= '9') || |
| 897 | ((StrVal[0] == '-' || StrVal[0] == '+') && |
| 898 | (StrVal[1] >= '0' && StrVal[1] <= '9'))) { |
| 899 | // Reparse stringized version! |
| 900 | if (atof(StrVal.c_str()) == Val) { |
| 901 | Out << StrVal; |
| 902 | return; |
| 903 | } |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 904 | } |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 905 | // Otherwise we could not reparse it to exactly the same value, so we must |
Dale Johannesen | 1f86498 | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 906 | // output the string in hexadecimal format! Note that loading and storing |
| 907 | // floating point types changes the bits of NaNs on some hosts, notably |
| 908 | // x86, so we must not use these types. |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 909 | assert(sizeof(double) == sizeof(uint64_t) && |
| 910 | "assuming that double is 64 bits!"); |
Chris Lattner | 5505eed | 2008-11-10 04:30:26 +0000 | [diff] [blame] | 911 | char Buffer[40]; |
Dale Johannesen | 1f86498 | 2009-01-21 20:32:55 +0000 | [diff] [blame] | 912 | APFloat apf = CFP->getValueAPF(); |
| 913 | // Floats are represented in ASCII IR as double, convert. |
| 914 | if (!isDouble) |
| 915 | apf.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, |
| 916 | &ignored); |
| 917 | Out << "0x" << |
| 918 | utohex_buffer(uint64_t(apf.bitcastToAPInt().getZExtValue()), |
| 919 | Buffer+40); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 920 | return; |
| 921 | } |
| 922 | |
| 923 | // Some form of long double. These appear as a magic letter identifying |
| 924 | // the type, then a fixed number of hex digits. |
| 925 | Out << "0x"; |
Dale Johannesen | 93eefa0 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 926 | if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) { |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 927 | Out << 'K'; |
Dale Johannesen | 93eefa0 | 2009-03-23 21:16:53 +0000 | [diff] [blame] | 928 | // api needed to prevent premature destruction |
| 929 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
| 930 | const uint64_t* p = api.getRawData(); |
| 931 | uint64_t word = p[1]; |
| 932 | int shiftcount=12; |
| 933 | int width = api.getBitWidth(); |
| 934 | for (int j=0; j<width; j+=4, shiftcount-=4) { |
| 935 | unsigned int nibble = (word>>shiftcount) & 15; |
| 936 | if (nibble < 10) |
| 937 | Out << (unsigned char)(nibble + '0'); |
| 938 | else |
| 939 | Out << (unsigned char)(nibble - 10 + 'A'); |
| 940 | if (shiftcount == 0 && j+4 < width) { |
| 941 | word = *p; |
| 942 | shiftcount = 64; |
| 943 | if (width-j-4 < 64) |
| 944 | shiftcount = width-j-4; |
| 945 | } |
| 946 | } |
| 947 | return; |
| 948 | } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 949 | Out << 'L'; |
| 950 | else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble) |
| 951 | Out << 'M'; |
| 952 | else |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 953 | llvm_unreachable("Unsupported floating point type"); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 954 | // api needed to prevent premature destruction |
Dale Johannesen | 54306fe | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 955 | APInt api = CFP->getValueAPF().bitcastToAPInt(); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 956 | const uint64_t* p = api.getRawData(); |
| 957 | uint64_t word = *p; |
| 958 | int shiftcount=60; |
| 959 | int width = api.getBitWidth(); |
| 960 | for (int j=0; j<width; j+=4, shiftcount-=4) { |
| 961 | unsigned int nibble = (word>>shiftcount) & 15; |
| 962 | if (nibble < 10) |
| 963 | Out << (unsigned char)(nibble + '0'); |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 964 | else |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 965 | Out << (unsigned char)(nibble - 10 + 'A'); |
| 966 | if (shiftcount == 0 && j+4 < width) { |
| 967 | word = *(++p); |
| 968 | shiftcount = 64; |
| 969 | if (width-j-4 < 64) |
| 970 | shiftcount = width-j-4; |
Dale Johannesen | 028084e | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 971 | } |
| 972 | } |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 973 | return; |
| 974 | } |
| 975 | |
| 976 | if (isa<ConstantAggregateZero>(CV)) { |
Chris Lattner | 76b2ff4 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 977 | Out << "zeroinitializer"; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 978 | return; |
| 979 | } |
| 980 | |
| 981 | if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 982 | // As a special case, print the array as a string if it is an array of |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 983 | // i8 with ConstantInt values. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 984 | // |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 985 | const Type *ETy = CA->getType()->getElementType(); |
Chris Lattner | 6ed87bd | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 986 | if (CA->isString()) { |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 987 | Out << "c\""; |
Chris Lattner | 6ed87bd | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 988 | PrintEscapedString(CA->getAsString(), Out); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 989 | Out << '"'; |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 990 | } else { // Cannot output in string format... |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 991 | Out << '['; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 992 | if (CA->getNumOperands()) { |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 993 | TypePrinter.print(ETy, Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 994 | Out << ' '; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 995 | WriteAsOperandInternal(Out, CA->getOperand(0), |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 996 | TypePrinter, Machine); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 997 | for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { |
| 998 | Out << ", "; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 999 | TypePrinter.print(ETy, Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1000 | Out << ' '; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1001 | WriteAsOperandInternal(Out, CA->getOperand(i), TypePrinter, Machine); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1004 | Out << ']'; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1005 | } |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { |
Andrew Lenharth | 0d124b8 | 2007-01-08 18:21:30 +0000 | [diff] [blame] | 1010 | if (CS->getType()->isPacked()) |
| 1011 | Out << '<'; |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1012 | Out << '{'; |
Jim Laskey | 3bb7874 | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 1013 | unsigned N = CS->getNumOperands(); |
| 1014 | if (N) { |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 1015 | Out << ' '; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1016 | TypePrinter.print(CS->getOperand(0)->getType(), Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1017 | Out << ' '; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1018 | |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1019 | WriteAsOperandInternal(Out, CS->getOperand(0), TypePrinter, Machine); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1020 | |
Jim Laskey | 3bb7874 | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 1021 | for (unsigned i = 1; i < N; i++) { |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1022 | Out << ", "; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1023 | TypePrinter.print(CS->getOperand(i)->getType(), Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1024 | Out << ' '; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1026 | WriteAsOperandInternal(Out, CS->getOperand(i), TypePrinter, Machine); |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1027 | } |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1028 | Out << ' '; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1029 | } |
Jim Laskey | 3bb7874 | 2006-02-25 12:27:03 +0000 | [diff] [blame] | 1030 | |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1031 | Out << '}'; |
Andrew Lenharth | 0d124b8 | 2007-01-08 18:21:30 +0000 | [diff] [blame] | 1032 | if (CS->getType()->isPacked()) |
| 1033 | Out << '>'; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1034 | return; |
| 1035 | } |
| 1036 | |
| 1037 | if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { |
| 1038 | const Type *ETy = CP->getType()->getElementType(); |
| 1039 | assert(CP->getNumOperands() > 0 && |
| 1040 | "Number of operands for a PackedConst must be > 0"); |
Dan Gohman | 1262a25 | 2009-02-11 00:25:25 +0000 | [diff] [blame] | 1041 | Out << '<'; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1042 | TypePrinter.print(ETy, Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1043 | Out << ' '; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1044 | WriteAsOperandInternal(Out, CP->getOperand(0), TypePrinter, Machine); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1045 | for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) { |
Chris Lattner | 585297e8 | 2008-08-19 05:26:17 +0000 | [diff] [blame] | 1046 | Out << ", "; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1047 | TypePrinter.print(ETy, Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1048 | Out << ' '; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1049 | WriteAsOperandInternal(Out, CP->getOperand(i), TypePrinter, Machine); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1050 | } |
Dan Gohman | 1262a25 | 2009-02-11 00:25:25 +0000 | [diff] [blame] | 1051 | Out << '>'; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | if (isa<ConstantPointerNull>(CV)) { |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1056 | Out << "null"; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1057 | return; |
| 1058 | } |
| 1059 | |
| 1060 | if (isa<UndefValue>(CV)) { |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1061 | Out << "undef"; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1062 | return; |
| 1063 | } |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 1064 | |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1065 | if (const MDNode *Node = dyn_cast<MDNode>(CV)) { |
| 1066 | Out << "!" << Machine->getMetadataSlot(Node); |
| 1067 | return; |
| 1068 | } |
| 1069 | |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1070 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 1071 | WriteOptimizationInfo(Out, CE); |
Reid Spencer | 812a1be | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1072 | Out << CE->getOpcodeName(); |
| 1073 | if (CE->isCompare()) |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1074 | Out << ' ' << getPredicateText(CE->getPredicate()); |
Reid Spencer | 812a1be | 2006-12-04 05:19:18 +0000 | [diff] [blame] | 1075 | Out << " ("; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1076 | |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 1077 | for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1078 | TypePrinter.print((*OI)->getType(), Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1079 | Out << ' '; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1080 | WriteAsOperandInternal(Out, *OI, TypePrinter, Machine); |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 1081 | if (OI+1 != CE->op_end()) |
Chris Lattner | 3cd8c56 | 2002-07-30 18:54:25 +0000 | [diff] [blame] | 1082 | Out << ", "; |
Vikram S. Adve | b952b54 | 2002-07-14 23:14:45 +0000 | [diff] [blame] | 1083 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1084 | |
Dan Gohman | a76f0f7 | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1085 | if (CE->hasIndices()) { |
| 1086 | const SmallVector<unsigned, 4> &Indices = CE->getIndices(); |
| 1087 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) |
| 1088 | Out << ", " << Indices[i]; |
| 1089 | } |
| 1090 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1091 | if (CE->isCast()) { |
Chris Lattner | 83b396b | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 1092 | Out << " to "; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1093 | TypePrinter.print(CE->getType(), Out); |
Chris Lattner | 83b396b | 2002-08-15 19:37:43 +0000 | [diff] [blame] | 1094 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1095 | |
Misha Brukman | 21bbdb9 | 2004-06-04 21:11:51 +0000 | [diff] [blame] | 1096 | Out << ')'; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1097 | return; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1098 | } |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1099 | |
| 1100 | Out << "<placeholder or erroneous Constant>"; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1104 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 1105 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 1106 | /// the whole instruction that generated it. |
| 1107 | /// |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1108 | static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1109 | TypePrinting &TypePrinter, |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 1110 | SlotTracker *Machine) { |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1111 | if (V->hasName()) { |
| 1112 | PrintLLVMName(Out, V); |
| 1113 | return; |
| 1114 | } |
| 1115 | |
| 1116 | const Constant *CV = dyn_cast<Constant>(V); |
| 1117 | if (CV && !isa<GlobalValue>(CV)) { |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1118 | WriteConstantInt(Out, CV, TypePrinter, Machine); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1119 | return; |
| 1120 | } |
| 1121 | |
| 1122 | if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1123 | Out << "asm "; |
| 1124 | if (IA->hasSideEffects()) |
| 1125 | Out << "sideeffect "; |
| 1126 | Out << '"'; |
| 1127 | PrintEscapedString(IA->getAsmString(), Out); |
| 1128 | Out << "\", \""; |
| 1129 | PrintEscapedString(IA->getConstraintString(), Out); |
| 1130 | Out << '"'; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1131 | return; |
| 1132 | } |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1133 | |
Devang Patel | e059ba6e | 2009-07-23 01:07:34 +0000 | [diff] [blame] | 1134 | if (const MDNode *N = dyn_cast<MDNode>(V)) { |
| 1135 | Out << '!' << Machine->getMetadataSlot(N); |
| 1136 | return; |
| 1137 | } |
| 1138 | |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1139 | if (const MDString *MDS = dyn_cast<MDString>(V)) { |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1140 | Out << "!\""; |
Devang Patel | 6292003 | 2009-07-23 02:00:51 +0000 | [diff] [blame] | 1141 | PrintEscapedString(MDS->begin(), MDS->length(), Out); |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1142 | Out << '"'; |
| 1143 | return; |
| 1144 | } |
| 1145 | |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1146 | char Prefix = '%'; |
| 1147 | int Slot; |
| 1148 | if (Machine) { |
| 1149 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 1150 | Slot = Machine->getGlobalSlot(GV); |
| 1151 | Prefix = '@'; |
| 1152 | } else { |
| 1153 | Slot = Machine->getLocalSlot(V); |
| 1154 | } |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1155 | } else { |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1156 | Machine = createSlotTracker(V); |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1157 | if (Machine) { |
| 1158 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 1159 | Slot = Machine->getGlobalSlot(GV); |
| 1160 | Prefix = '@'; |
| 1161 | } else { |
| 1162 | Slot = Machine->getLocalSlot(V); |
| 1163 | } |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 1164 | } else { |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1165 | Slot = -1; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1166 | } |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1167 | delete Machine; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1168 | } |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1169 | |
| 1170 | if (Slot != -1) |
| 1171 | Out << Prefix << Slot; |
| 1172 | else |
| 1173 | Out << "<badref>"; |
Chris Lattner | d84bb63 | 2002-04-16 21:36:08 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Misha Brukman | b22d09c | 2004-03-01 19:48:13 +0000 | [diff] [blame] | 1176 | /// WriteAsOperand - Write the name of the specified value out to the specified |
| 1177 | /// ostream. This can be useful when you just want to print int %reg126, not |
| 1178 | /// the whole instruction that generated it. |
| 1179 | /// |
Chris Lattner | 604e351 | 2008-08-19 04:47:09 +0000 | [diff] [blame] | 1180 | void llvm::WriteAsOperand(std::ostream &Out, const Value *V, bool PrintType, |
| 1181 | const Module *Context) { |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1182 | raw_os_ostream OS(Out); |
| 1183 | WriteAsOperand(OS, V, PrintType, Context); |
| 1184 | } |
| 1185 | |
| 1186 | void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType, |
| 1187 | const Module *Context) { |
Chris Lattner | 5a9f63e | 2002-07-10 16:48:17 +0000 | [diff] [blame] | 1188 | if (Context == 0) Context = getModuleFromVal(V); |
Chris Lattner | b86620e | 2001-10-29 16:37:48 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | 92c5c12 | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 1190 | TypePrinting TypePrinter; |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1191 | std::vector<const Type*> NumberedTypes; |
| 1192 | AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1193 | if (PrintType) { |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1194 | TypePrinter.print(V->getType(), Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1195 | Out << ' '; |
| 1196 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1198 | WriteAsOperandInternal(Out, V, TypePrinter, 0); |
Chris Lattner | 5e5abe3 | 2001-07-20 19:15:21 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Reid Spencer | 58d30f2 | 2004-07-04 11:50:43 +0000 | [diff] [blame] | 1201 | |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1202 | namespace { |
Chris Lattner | 2e9fee4 | 2001-07-12 23:35:26 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1204 | class AssemblyWriter { |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1205 | raw_ostream &Out; |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 1206 | SlotTracker &Machine; |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1207 | const Module *TheModule; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1208 | TypePrinting TypePrinter; |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1209 | AssemblyAnnotationWriter *AnnotationWriter; |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1210 | std::vector<const Type*> NumberedTypes; |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 1211 | |
| 1212 | // Each MDNode is assigned unique MetadataIDNo. |
| 1213 | std::map<const MDNode *, unsigned> MDNodes; |
| 1214 | unsigned MetadataIDNo; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1215 | public: |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1216 | inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M, |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1217 | AssemblyAnnotationWriter *AAW) |
Devang Patel | 39e64d4 | 2009-07-01 19:21:12 +0000 | [diff] [blame] | 1218 | : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW), MetadataIDNo(0) { |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1219 | AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1222 | void write(const Module *M) { printModule(M); } |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1223 | |
| 1224 | void write(const GlobalValue *G) { |
| 1225 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) |
| 1226 | printGlobal(GV); |
| 1227 | else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) |
| 1228 | printAlias(GA); |
| 1229 | else if (const Function *F = dyn_cast<Function>(G)) |
| 1230 | printFunction(F); |
| 1231 | else |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1232 | llvm_unreachable("Unknown global"); |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1235 | void write(const BasicBlock *BB) { printBasicBlock(BB); } |
| 1236 | void write(const Instruction *I) { printInstruction(*I); } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | 78e2e8b | 2006-12-06 06:24:27 +0000 | [diff] [blame] | 1238 | void writeOperand(const Value *Op, bool PrintType); |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 1239 | void writeParamOperand(const Value *Operand, Attributes Attrs); |
Chris Lattner | 1e19468 | 2002-04-18 18:53:13 +0000 | [diff] [blame] | 1240 | |
Misha Brukman | 4685e26 | 2004-04-28 15:31:21 +0000 | [diff] [blame] | 1241 | const Module* getModule() { return TheModule; } |
| 1242 | |
Misha Brukman | d92f54a | 2004-11-15 19:30:05 +0000 | [diff] [blame] | 1243 | private: |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1244 | void printModule(const Module *M); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1245 | void printTypeSymbolTable(const TypeSymbolTable &ST); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1246 | void printGlobal(const GlobalVariable *GV); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1247 | void printAlias(const GlobalAlias *GV); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 1248 | void printFunction(const Function *F); |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 1249 | void printArgument(const Argument *FA, Attributes Attrs); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1250 | void printBasicBlock(const BasicBlock *BB); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1251 | void printInstruction(const Instruction &I); |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1253 | // printInfoComment - Print a little comment after the instruction indicating |
| 1254 | // which slot it occupies. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1255 | void printInfoComment(const Value &V); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1256 | }; |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1257 | } // end of anonymous namespace |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1258 | |
Chris Lattner | d816b53 | 2002-04-13 20:53:41 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 78e2e8b | 2006-12-06 06:24:27 +0000 | [diff] [blame] | 1260 | void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) { |
| 1261 | if (Operand == 0) { |
Chris Lattner | 08f7d0c | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 1262 | Out << "<null operand!>"; |
Chris Lattner | 78e2e8b | 2006-12-06 06:24:27 +0000 | [diff] [blame] | 1263 | } else { |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1264 | if (PrintType) { |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1265 | TypePrinter.print(Operand->getType(), Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1266 | Out << ' '; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1267 | } |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1268 | WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine); |
Chris Lattner | 08f7d0c | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 1269 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 1272 | void AssemblyWriter::writeParamOperand(const Value *Operand, |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 1273 | Attributes Attrs) { |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1274 | if (Operand == 0) { |
| 1275 | Out << "<null operand!>"; |
| 1276 | } else { |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1277 | // Print the type |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1278 | TypePrinter.print(Operand->getType(), Out); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1279 | // Print parameter attributes list |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1280 | if (Attrs != Attribute::None) |
| 1281 | Out << ' ' << Attribute::getAsString(Attrs); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1282 | Out << ' '; |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1283 | // Print the operand |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1284 | WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine); |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1285 | } |
| 1286 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1287 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1288 | void AssemblyWriter::printModule(const Module *M) { |
Chris Lattner | 4d8689e | 2005-03-02 23:12:40 +0000 | [diff] [blame] | 1289 | if (!M->getModuleIdentifier().empty() && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1290 | // Don't print the ID if it will start a new line (which would |
Chris Lattner | 4d8689e | 2005-03-02 23:12:40 +0000 | [diff] [blame] | 1291 | // require a comment char before it). |
| 1292 | M->getModuleIdentifier().find('\n') == std::string::npos) |
| 1293 | Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n"; |
| 1294 | |
Owen Anderson | e223754 | 2006-10-18 02:21:12 +0000 | [diff] [blame] | 1295 | if (!M->getDataLayout().empty()) |
Chris Lattner | 0489716 | 2006-10-22 06:06:56 +0000 | [diff] [blame] | 1296 | Out << "target datalayout = \"" << M->getDataLayout() << "\"\n"; |
Reid Spencer | 48f98c8 | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 1297 | if (!M->getTargetTriple().empty()) |
Reid Spencer | ffec7df | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 1298 | Out << "target triple = \"" << M->getTargetTriple() << "\"\n"; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1299 | |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1300 | if (!M->getModuleInlineAsm().empty()) { |
Chris Lattner | efaf35d | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1301 | // Split the string into lines, to make it easier to read the .ll file. |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1302 | std::string Asm = M->getModuleInlineAsm(); |
Chris Lattner | efaf35d | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1303 | size_t CurPos = 0; |
| 1304 | size_t NewLine = Asm.find_first_of('\n', CurPos); |
| 1305 | while (NewLine != std::string::npos) { |
| 1306 | // We found a newline, print the portion of the asm string from the |
| 1307 | // last newline up to this newline. |
| 1308 | Out << "module asm \""; |
| 1309 | PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.begin()+NewLine), |
| 1310 | Out); |
| 1311 | Out << "\"\n"; |
| 1312 | CurPos = NewLine+1; |
| 1313 | NewLine = Asm.find_first_of('\n', CurPos); |
| 1314 | } |
Chris Lattner | 3acaf5c | 2006-01-24 00:40:17 +0000 | [diff] [blame] | 1315 | Out << "module asm \""; |
Chris Lattner | efaf35d | 2006-01-24 00:45:30 +0000 | [diff] [blame] | 1316 | PrintEscapedString(std::string(Asm.begin()+CurPos, Asm.end()), Out); |
Chris Lattner | 6ed87bd | 2006-01-23 23:03:36 +0000 | [diff] [blame] | 1317 | Out << "\"\n"; |
| 1318 | } |
| 1319 | |
Chris Lattner | 2cdd49d | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 1320 | // Loop over the dependent libraries and emit them. |
Chris Lattner | 730cfe4 | 2004-09-14 04:51:44 +0000 | [diff] [blame] | 1321 | Module::lib_iterator LI = M->lib_begin(); |
| 1322 | Module::lib_iterator LE = M->lib_end(); |
Reid Spencer | 48f98c8 | 2004-07-25 21:44:54 +0000 | [diff] [blame] | 1323 | if (LI != LE) { |
Chris Lattner | 730cfe4 | 2004-09-14 04:51:44 +0000 | [diff] [blame] | 1324 | Out << "deplibs = [ "; |
| 1325 | while (LI != LE) { |
Chris Lattner | 2cdd49d | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 1326 | Out << '"' << *LI << '"'; |
Reid Spencer | ffec7df | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 1327 | ++LI; |
Chris Lattner | 730cfe4 | 2004-09-14 04:51:44 +0000 | [diff] [blame] | 1328 | if (LI != LE) |
| 1329 | Out << ", "; |
Reid Spencer | ffec7df | 2004-07-25 21:29:43 +0000 | [diff] [blame] | 1330 | } |
| 1331 | Out << " ]\n"; |
Reid Spencer | cc5ff64 | 2004-07-25 18:08:18 +0000 | [diff] [blame] | 1332 | } |
Reid Spencer | b9e0877 | 2004-09-13 23:44:23 +0000 | [diff] [blame] | 1333 | |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1334 | // Loop over the symbol table, emitting all id'd types. |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1335 | printTypeSymbolTable(M->getTypeSymbolTable()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | 54932b0 | 2006-12-06 04:41:52 +0000 | [diff] [blame] | 1337 | for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); |
| 1338 | I != E; ++I) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1339 | printGlobal(I); |
Chris Lattner | d274705 | 2007-04-26 02:24:10 +0000 | [diff] [blame] | 1340 | |
| 1341 | // Output all aliases. |
| 1342 | if (!M->alias_empty()) Out << "\n"; |
| 1343 | for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 1344 | I != E; ++I) |
| 1345 | printAlias(I); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | 2cdd49d | 2004-09-14 05:06:58 +0000 | [diff] [blame] | 1347 | // Output all of the functions. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1348 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 1349 | printFunction(I); |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1350 | |
| 1351 | WriteMDNodes(Out, TypePrinter, Machine); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1354 | static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) { |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1355 | switch (LT) { |
Bill Wendling | a3c6f6b | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 1356 | case GlobalValue::ExternalLinkage: break; |
| 1357 | case GlobalValue::PrivateLinkage: Out << "private "; break; |
| 1358 | case GlobalValue::LinkerPrivateLinkage: Out << "linker_private "; break; |
| 1359 | case GlobalValue::InternalLinkage: Out << "internal "; break; |
| 1360 | case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break; |
| 1361 | case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break; |
| 1362 | case GlobalValue::WeakAnyLinkage: Out << "weak "; break; |
| 1363 | case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break; |
| 1364 | case GlobalValue::CommonLinkage: Out << "common "; break; |
| 1365 | case GlobalValue::AppendingLinkage: Out << "appending "; break; |
| 1366 | case GlobalValue::DLLImportLinkage: Out << "dllimport "; break; |
| 1367 | case GlobalValue::DLLExportLinkage: Out << "dllexport "; break; |
| 1368 | case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break; |
Chris Lattner | 184f1be | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 1369 | case GlobalValue::AvailableExternallyLinkage: |
| 1370 | Out << "available_externally "; |
| 1371 | break; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1372 | case GlobalValue::GhostLinkage: |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1373 | llvm_unreachable("GhostLinkage not allowed in AsmWriter!"); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
Duncan Sands | 12da8ce | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 1376 | |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1377 | |
| 1378 | static void PrintVisibility(GlobalValue::VisibilityTypes Vis, |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1379 | raw_ostream &Out) { |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1380 | switch (Vis) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1381 | default: llvm_unreachable("Invalid visibility style!"); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1382 | case GlobalValue::DefaultVisibility: break; |
| 1383 | case GlobalValue::HiddenVisibility: Out << "hidden "; break; |
| 1384 | case GlobalValue::ProtectedVisibility: Out << "protected "; break; |
| 1385 | } |
| 1386 | } |
| 1387 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1388 | void AssemblyWriter::printGlobal(const GlobalVariable *GV) { |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1389 | if (GV->hasName()) { |
| 1390 | PrintLLVMName(Out, GV); |
| 1391 | Out << " = "; |
| 1392 | } |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1393 | |
Chris Lattner | 1508d3f | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1394 | if (!GV->hasInitializer() && GV->hasExternalLinkage()) |
| 1395 | Out << "external "; |
| 1396 | |
| 1397 | PrintLinkage(GV->getLinkage(), Out); |
| 1398 | PrintVisibility(GV->getVisibility(), Out); |
Lauro Ramos Venancio | 749e466 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 1399 | |
| 1400 | if (GV->isThreadLocal()) Out << "thread_local "; |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1401 | if (unsigned AddressSpace = GV->getType()->getAddressSpace()) |
| 1402 | Out << "addrspace(" << AddressSpace << ") "; |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1403 | Out << (GV->isConstant() ? "constant " : "global "); |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1404 | TypePrinter.print(GV->getType()->getElementType(), Out); |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 1405 | |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1406 | if (GV->hasInitializer()) { |
| 1407 | Out << ' '; |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1408 | writeOperand(GV->getInitializer(), false); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1409 | } |
Christopher Lamb | 54dd24c | 2007-12-11 08:59:05 +0000 | [diff] [blame] | 1410 | |
Chris Lattner | 4b96c54 | 2005-11-12 00:10:19 +0000 | [diff] [blame] | 1411 | if (GV->hasSection()) |
| 1412 | Out << ", section \"" << GV->getSection() << '"'; |
| 1413 | if (GV->getAlignment()) |
Chris Lattner | f8a974d | 2005-11-06 06:48:53 +0000 | [diff] [blame] | 1414 | Out << ", align " << GV->getAlignment(); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1415 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1416 | printInfoComment(*GV); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1417 | Out << '\n'; |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1420 | void AssemblyWriter::printAlias(const GlobalAlias *GA) { |
Dale Johannesen | 83e468a | 2008-06-03 18:14:29 +0000 | [diff] [blame] | 1421 | // Don't crash when dumping partially built GA |
| 1422 | if (!GA->hasName()) |
| 1423 | Out << "<<nameless>> = "; |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1424 | else { |
| 1425 | PrintLLVMName(Out, GA); |
| 1426 | Out << " = "; |
| 1427 | } |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1428 | PrintVisibility(GA->getVisibility(), Out); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1429 | |
| 1430 | Out << "alias "; |
| 1431 | |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1432 | PrintLinkage(GA->getLinkage(), Out); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1433 | |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 1434 | const Constant *Aliasee = GA->getAliasee(); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1435 | |
| 1436 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) { |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1437 | TypePrinter.print(GV->getType(), Out); |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1438 | Out << ' '; |
| 1439 | PrintLLVMName(Out, GV); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1440 | } else if (const Function *F = dyn_cast<Function>(Aliasee)) { |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1441 | TypePrinter.print(F->getFunctionType(), Out); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1442 | Out << "* "; |
| 1443 | |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1444 | WriteAsOperandInternal(Out, F, TypePrinter, &Machine); |
Anton Korobeynikov | 72d5d42 | 2008-03-22 08:17:17 +0000 | [diff] [blame] | 1445 | } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) { |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1446 | TypePrinter.print(GA->getType(), Out); |
| 1447 | Out << ' '; |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1448 | PrintLLVMName(Out, GA); |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 1449 | } else { |
Chris Lattner | cde89e4 | 2009-04-25 21:23:19 +0000 | [diff] [blame] | 1450 | const ConstantExpr *CE = cast<ConstantExpr>(Aliasee); |
| 1451 | // The only valid GEP is an all zero GEP. |
| 1452 | assert((CE->getOpcode() == Instruction::BitCast || |
| 1453 | CE->getOpcode() == Instruction::GetElementPtr) && |
| 1454 | "Unsupported aliasee"); |
| 1455 | writeOperand(CE, false); |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1458 | printInfoComment(*GA); |
Chris Lattner | 1508d3f | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1459 | Out << '\n'; |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1462 | void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) { |
Chris Lattner | 3243ea1 | 2009-03-01 00:03:38 +0000 | [diff] [blame] | 1463 | // Emit all numbered types. |
| 1464 | for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) { |
| 1465 | Out << "\ttype "; |
| 1466 | |
| 1467 | // Make sure we print out at least one level of the type structure, so |
| 1468 | // that we do not get %2 = type %2 |
| 1469 | TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out); |
| 1470 | Out << "\t\t; type %" << i << '\n'; |
| 1471 | } |
| 1472 | |
| 1473 | // Print the named types. |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1474 | for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end(); |
| 1475 | TI != TE; ++TI) { |
Chris Lattner | 1508d3f | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1476 | Out << '\t'; |
| 1477 | PrintLLVMName(Out, &TI->first[0], TI->first.size(), LocalPrefix); |
| 1478 | Out << " = type "; |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Make sure we print out at least one level of the type structure, so |
| 1481 | // that we do not get %FILE = type %FILE |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1482 | TypePrinter.printAtLeastOneLevel(TI->second, Out); |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1483 | Out << '\n'; |
Reid Spencer | e7e9671 | 2004-05-25 08:53:40 +0000 | [diff] [blame] | 1484 | } |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1487 | /// printFunction - Print all aspects of a function. |
| 1488 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1489 | void AssemblyWriter::printFunction(const Function *F) { |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1490 | // Print out the return type and name. |
| 1491 | Out << '\n'; |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 1492 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1493 | if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1494 | |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1495 | if (F->isDeclaration()) |
Chris Lattner | 10f03a6 | 2007-08-19 22:15:26 +0000 | [diff] [blame] | 1496 | Out << "declare "; |
| 1497 | else |
Reid Spencer | 7ce2d2a | 2006-12-29 20:29:48 +0000 | [diff] [blame] | 1498 | Out << "define "; |
Chris Lattner | 3a36d2c | 2008-08-19 05:06:27 +0000 | [diff] [blame] | 1499 | |
| 1500 | PrintLinkage(F->getLinkage(), Out); |
| 1501 | PrintVisibility(F->getVisibility(), Out); |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 1502 | |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1503 | // Print the calling convention. |
| 1504 | switch (F->getCallingConv()) { |
| 1505 | case CallingConv::C: break; // default |
Anton Korobeynikov | 3c5b3df | 2006-09-20 22:03:51 +0000 | [diff] [blame] | 1506 | case CallingConv::Fast: Out << "fastcc "; break; |
| 1507 | case CallingConv::Cold: Out << "coldcc "; break; |
| 1508 | case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break; |
Anton Korobeynikov | a8fd40b | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1509 | case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break; |
| 1510 | case CallingConv::ARM_APCS: Out << "arm_apcscc "; break; |
| 1511 | case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break; |
| 1512 | case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break; |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1513 | default: Out << "cc" << F->getCallingConv() << " "; break; |
| 1514 | } |
| 1515 | |
Reid Spencer | 8c4914c | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1516 | const FunctionType *FT = F->getFunctionType(); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1517 | const AttrListPtr &Attrs = F->getAttributes(); |
Devang Patel | 221fe42 | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1518 | Attributes RetAttrs = Attrs.getRetAttributes(); |
| 1519 | if (RetAttrs != Attribute::None) |
| 1520 | Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' '; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1521 | TypePrinter.print(F->getReturnType(), Out); |
Chris Lattner | 585297e8 | 2008-08-19 05:26:17 +0000 | [diff] [blame] | 1522 | Out << ' '; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1523 | WriteAsOperandInternal(Out, F, TypePrinter, &Machine); |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1524 | Out << '('; |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1525 | Machine.incorporateFunction(F); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1526 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1527 | // Loop over the arguments, printing them... |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1528 | |
Reid Spencer | 8c4914c | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1529 | unsigned Idx = 1; |
Chris Lattner | 82738fe | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1530 | if (!F->isDeclaration()) { |
| 1531 | // If this isn't a declaration, print the argument names as well. |
| 1532 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| 1533 | I != E; ++I) { |
| 1534 | // Insert commas as we go... the first arg doesn't get a comma |
| 1535 | if (I != F->arg_begin()) Out << ", "; |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1536 | printArgument(I, Attrs.getParamAttributes(Idx)); |
Chris Lattner | 82738fe | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1537 | Idx++; |
| 1538 | } |
| 1539 | } else { |
| 1540 | // Otherwise, print the types from the function type. |
| 1541 | for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { |
| 1542 | // Insert commas as we go... the first arg doesn't get a comma |
| 1543 | if (i) Out << ", "; |
| 1544 | |
| 1545 | // Output type... |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1546 | TypePrinter.print(FT->getParamType(i), Out); |
Chris Lattner | 82738fe | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1547 | |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1548 | Attributes ArgAttrs = Attrs.getParamAttributes(i+1); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1549 | if (ArgAttrs != Attribute::None) |
| 1550 | Out << ' ' << Attribute::getAsString(ArgAttrs); |
Chris Lattner | 82738fe | 2007-04-18 00:57:22 +0000 | [diff] [blame] | 1551 | } |
Reid Spencer | 8c4914c | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1552 | } |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1553 | |
| 1554 | // Finish printing arguments... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1555 | if (FT->isVarArg()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1556 | if (FT->getNumParams()) Out << ", "; |
| 1557 | Out << "..."; // Output varargs portion of signature! |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1558 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1559 | Out << ')'; |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1560 | Attributes FnAttrs = Attrs.getFnAttributes(); |
| 1561 | if (FnAttrs != Attribute::None) |
| 1562 | Out << ' ' << Attribute::getAsString(Attrs.getFnAttributes()); |
Chris Lattner | 4b96c54 | 2005-11-12 00:10:19 +0000 | [diff] [blame] | 1563 | if (F->hasSection()) |
| 1564 | Out << " section \"" << F->getSection() << '"'; |
Chris Lattner | f8a974d | 2005-11-06 06:48:53 +0000 | [diff] [blame] | 1565 | if (F->getAlignment()) |
| 1566 | Out << " align " << F->getAlignment(); |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1567 | if (F->hasGC()) |
| 1568 | Out << " gc \"" << F->getGC() << '"'; |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1569 | if (F->isDeclaration()) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1570 | Out << "\n"; |
Chris Lattner | b2f02e5 | 2002-05-06 03:00:40 +0000 | [diff] [blame] | 1571 | } else { |
Chris Lattner | d5fbc8f | 2008-04-21 06:12:55 +0000 | [diff] [blame] | 1572 | Out << " {"; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 1574 | // Output all of its basic blocks... for the function |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1575 | for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 1576 | printBasicBlock(I); |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1577 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1578 | Out << "}\n"; |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Reid Spencer | 16f2f7f | 2004-05-26 07:18:52 +0000 | [diff] [blame] | 1581 | Machine.purgeFunction(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1584 | /// printArgument - This member is called for every argument that is passed into |
| 1585 | /// the function. Simply print it out |
| 1586 | /// |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 1587 | void AssemblyWriter::printArgument(const Argument *Arg, |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 1588 | Attributes Attrs) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1589 | // Output type... |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1590 | TypePrinter.print(Arg->getType(), Out); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1591 | |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 1592 | // Output parameter attributes list |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1593 | if (Attrs != Attribute::None) |
| 1594 | Out << ' ' << Attribute::getAsString(Attrs); |
Reid Spencer | 8c4914c | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1595 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1596 | // Output name, if available... |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1597 | if (Arg->hasName()) { |
| 1598 | Out << ' '; |
| 1599 | PrintLLVMName(Out, Arg); |
| 1600 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1603 | /// printBasicBlock - This member is called for each basic block in a method. |
| 1604 | /// |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1605 | void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { |
Nick Lewycky | 4d43d3c | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1606 | if (BB->hasName()) { // Print out the label if it exists... |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1607 | Out << "\n"; |
Chris Lattner | 1508d3f | 2008-08-19 05:16:28 +0000 | [diff] [blame] | 1608 | PrintLLVMName(Out, BB->getNameStart(), BB->getNameLen(), LabelPrefix); |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1609 | Out << ':'; |
Nick Lewycky | 4d43d3c | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 1610 | } else if (!BB->use_empty()) { // Don't print block # of no uses... |
Chris Lattner | 67bec13 | 2008-04-21 04:20:33 +0000 | [diff] [blame] | 1611 | Out << "\n; <label>:"; |
Chris Lattner | 5e04332 | 2007-01-11 03:54:27 +0000 | [diff] [blame] | 1612 | int Slot = Machine.getLocalSlot(BB); |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1613 | if (Slot != -1) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1614 | Out << Slot; |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1615 | else |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1616 | Out << "<badref>"; |
Chris Lattner | 58185f2 | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 1617 | } |
Chris Lattner | 2447ef5 | 2003-11-20 00:09:43 +0000 | [diff] [blame] | 1618 | |
| 1619 | if (BB->getParent() == 0) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1620 | Out << "\t\t; Error: Block without parent!"; |
Chris Lattner | ff834c0 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1621 | else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block? |
| 1622 | // Output predecessors for the block... |
| 1623 | Out << "\t\t;"; |
| 1624 | pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 1625 | |
| 1626 | if (PI == PE) { |
| 1627 | Out << " No predecessors!"; |
| 1628 | } else { |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1629 | Out << " preds = "; |
Chris Lattner | ff834c0 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1630 | writeOperand(*PI, false); |
| 1631 | for (++PI; PI != PE; ++PI) { |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1632 | Out << ", "; |
Chris Lattner | 78e2e8b | 2006-12-06 06:24:27 +0000 | [diff] [blame] | 1633 | writeOperand(*PI, false); |
Chris Lattner | 00211f1 | 2003-11-16 22:59:57 +0000 | [diff] [blame] | 1634 | } |
Chris Lattner | 58185f2 | 2002-10-02 19:38:55 +0000 | [diff] [blame] | 1635 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1636 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1637 | |
Chris Lattner | ff834c0 | 2008-04-22 02:45:44 +0000 | [diff] [blame] | 1638 | Out << "\n"; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1639 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1640 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | fee714f | 2001-09-07 16:36:04 +0000 | [diff] [blame] | 1642 | // Output all of the instructions in the basic block... |
Dan Gohman | a4f709e | 2009-07-13 18:27:59 +0000 | [diff] [blame] | 1643 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1644 | printInstruction(*I); |
Dan Gohman | a4f709e | 2009-07-13 18:27:59 +0000 | [diff] [blame] | 1645 | Out << '\n'; |
| 1646 | } |
Chris Lattner | 96cdd27 | 2004-03-08 18:51:45 +0000 | [diff] [blame] | 1647 | |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1648 | if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1651 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1652 | /// printInfoComment - Print a little comment after the instruction indicating |
| 1653 | /// which slot it occupies. |
| 1654 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1655 | void AssemblyWriter::printInfoComment(const Value &V) { |
| 1656 | if (V.getType() != Type::VoidTy) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1657 | Out << "\t\t; <"; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1658 | TypePrinter.print(V.getType(), Out); |
Chris Lattner | 585297e8 | 2008-08-19 05:26:17 +0000 | [diff] [blame] | 1659 | Out << '>'; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1660 | |
Chris Lattner | b25e5ea | 2008-08-29 17:19:30 +0000 | [diff] [blame] | 1661 | if (!V.hasName() && !isa<Instruction>(V)) { |
Chris Lattner | 5e04332 | 2007-01-11 03:54:27 +0000 | [diff] [blame] | 1662 | int SlotNum; |
| 1663 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(&V)) |
| 1664 | SlotNum = Machine.getGlobalSlot(GV); |
| 1665 | else |
| 1666 | SlotNum = Machine.getLocalSlot(&V); |
Chris Lattner | 757ee0b | 2004-06-09 19:41:19 +0000 | [diff] [blame] | 1667 | if (SlotNum == -1) |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1668 | Out << ":<badref>"; |
Reid Spencer | 8beac69 | 2004-06-09 15:26:53 +0000 | [diff] [blame] | 1669 | else |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1670 | Out << ':' << SlotNum; // Print out the def slot taken. |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1671 | } |
Chris Lattner | b6c21db | 2005-02-01 01:24:01 +0000 | [diff] [blame] | 1672 | Out << " [#uses=" << V.getNumUses() << ']'; // Output # uses |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | |
Reid Spencer | e7141c8 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 1676 | // This member is called for each Instruction in a function.. |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1677 | void AssemblyWriter::printInstruction(const Instruction &I) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1678 | if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out); |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1679 | |
Chris Lattner | 82ff923 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1680 | Out << '\t'; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1681 | |
| 1682 | // Print out name if it exists... |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1683 | if (I.hasName()) { |
| 1684 | PrintLLVMName(Out, &I); |
| 1685 | Out << " = "; |
Chris Lattner | b25e5ea | 2008-08-29 17:19:30 +0000 | [diff] [blame] | 1686 | } else if (I.getType() != Type::VoidTy) { |
| 1687 | // Print out the def slot taken. |
| 1688 | int SlotNum = Machine.getLocalSlot(&I); |
| 1689 | if (SlotNum == -1) |
| 1690 | Out << "<badref> = "; |
| 1691 | else |
| 1692 | Out << '%' << SlotNum << " = "; |
Chris Lattner | 033935d | 2008-08-17 04:40:13 +0000 | [diff] [blame] | 1693 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1694 | |
Chris Lattner | 0603845 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1695 | // If this is a volatile load or store, print out the volatile marker. |
Chris Lattner | 504f924 | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 1696 | if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) || |
Chris Lattner | 0603845 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1697 | (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1698 | Out << "volatile "; |
Chris Lattner | 0603845 | 2005-05-06 05:51:46 +0000 | [diff] [blame] | 1699 | } else if (isa<CallInst>(I) && cast<CallInst>(I).isTailCall()) { |
| 1700 | // If this is a call, check if it's a tail call. |
| 1701 | Out << "tail "; |
| 1702 | } |
Chris Lattner | 504f924 | 2003-09-08 17:45:59 +0000 | [diff] [blame] | 1703 | |
Dan Gohman | 0ebd696 | 2009-07-20 21:19:07 +0000 | [diff] [blame] | 1704 | // Print out optimization information. |
| 1705 | WriteOptimizationInfo(Out, &I); |
| 1706 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1707 | // Print out the opcode... |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1708 | Out << I.getOpcodeName(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1709 | |
Reid Spencer | 45e5239 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 1710 | // Print out the compare instruction predicates |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 1711 | if (const CmpInst *CI = dyn_cast<CmpInst>(&I)) |
Chris Lattner | 82ff923 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1712 | Out << ' ' << getPredicateText(CI->getPredicate()); |
Reid Spencer | 45e5239 | 2006-12-03 06:27:29 +0000 | [diff] [blame] | 1713 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1714 | // Print out the type of the operands... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1715 | const Value *Operand = I.getNumOperands() ? I.getOperand(0) : 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1716 | |
| 1717 | // Special case conditional branches to swizzle the condition out to the front |
Gabor Greif | cab008f | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1718 | if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) { |
| 1719 | BranchInst &BI(cast<BranchInst>(I)); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1720 | Out << ' '; |
Gabor Greif | cab008f | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1721 | writeOperand(BI.getCondition(), true); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1722 | Out << ", "; |
Gabor Greif | cab008f | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1723 | writeOperand(BI.getSuccessor(0), true); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1724 | Out << ", "; |
Gabor Greif | cab008f | 2009-02-09 15:45:06 +0000 | [diff] [blame] | 1725 | writeOperand(BI.getSuccessor(1), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1727 | } else if (isa<SwitchInst>(I)) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1728 | // Special case switch statement to get formatting nice and correct... |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1729 | Out << ' '; |
Chris Lattner | 82ff923 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1730 | writeOperand(Operand , true); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1731 | Out << ", "; |
Chris Lattner | 82ff923 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1732 | writeOperand(I.getOperand(1), true); |
| 1733 | Out << " ["; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1734 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1735 | for (unsigned op = 2, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1736 | Out << "\n\t\t"; |
Chris Lattner | 82ff923 | 2008-08-23 22:52:27 +0000 | [diff] [blame] | 1737 | writeOperand(I.getOperand(op ), true); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1738 | Out << ", "; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1739 | writeOperand(I.getOperand(op+1), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1740 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1741 | Out << "\n\t]"; |
Chris Lattner | da55810 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 1742 | } else if (isa<PHINode>(I)) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1743 | Out << ' '; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1744 | TypePrinter.print(I.getType(), Out); |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1745 | Out << ' '; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1746 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1747 | for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1748 | if (op) Out << ", "; |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1749 | Out << "[ "; |
| 1750 | writeOperand(I.getOperand(op ), false); Out << ", "; |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1751 | writeOperand(I.getOperand(op+1), false); Out << " ]"; |
Chris Lattner | 931ef3b | 2001-06-11 15:04:20 +0000 | [diff] [blame] | 1752 | } |
Dan Gohman | a76f0f7 | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1753 | } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) { |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1754 | Out << ' '; |
Dan Gohman | a76f0f7 | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1755 | writeOperand(I.getOperand(0), true); |
| 1756 | for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) |
| 1757 | Out << ", " << *i; |
| 1758 | } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) { |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1759 | Out << ' '; |
| 1760 | writeOperand(I.getOperand(0), true); Out << ", "; |
Dan Gohman | a76f0f7 | 2008-05-31 19:12:39 +0000 | [diff] [blame] | 1761 | writeOperand(I.getOperand(1), true); |
| 1762 | for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) |
| 1763 | Out << ", " << *i; |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 1764 | } else if (isa<ReturnInst>(I) && !Operand) { |
| 1765 | Out << " void"; |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1766 | } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 1767 | // Print the calling convention being used. |
| 1768 | switch (CI->getCallingConv()) { |
| 1769 | case CallingConv::C: break; // default |
Chris Lattner | 29d2085 | 2006-05-19 21:58:52 +0000 | [diff] [blame] | 1770 | case CallingConv::Fast: Out << " fastcc"; break; |
| 1771 | case CallingConv::Cold: Out << " coldcc"; break; |
Chris Lattner | f527037 | 2007-11-18 18:32:16 +0000 | [diff] [blame] | 1772 | case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break; |
Anton Korobeynikov | a8fd40b | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1773 | case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break; |
| 1774 | case CallingConv::ARM_APCS: Out << " arm_apcscc "; break; |
| 1775 | case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break; |
| 1776 | case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break; |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1777 | default: Out << " cc" << CI->getCallingConv(); break; |
| 1778 | } |
| 1779 | |
Reid Spencer | 1517de3 | 2007-04-09 06:10:42 +0000 | [diff] [blame] | 1780 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1781 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1782 | const Type *RetTy = FTy->getReturnType(); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1783 | const AttrListPtr &PAL = CI->getAttributes(); |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1784 | |
Devang Patel | 221fe42 | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1785 | if (PAL.getRetAttributes() != Attribute::None) |
| 1786 | Out << ' ' << Attribute::getAsString(PAL.getRetAttributes()); |
| 1787 | |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1788 | // If possible, print out the short form of the call instruction. We can |
Chris Lattner | 6915f8f | 2002-04-07 22:49:37 +0000 | [diff] [blame] | 1789 | // only do this if the first argument is a pointer to a nonvararg function, |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1790 | // and if the return type is not a pointer to a function. |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1791 | // |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1792 | Out << ' '; |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1793 | if (!FTy->isVarArg() && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1794 | (!isa<PointerType>(RetTy) || |
Chris Lattner | d9a36a6 | 2002-07-25 20:58:51 +0000 | [diff] [blame] | 1795 | !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1796 | TypePrinter.print(RetTy, Out); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1797 | Out << ' '; |
Chris Lattner | 2f2d947 | 2001-11-06 21:28:12 +0000 | [diff] [blame] | 1798 | writeOperand(Operand, false); |
| 1799 | } else { |
| 1800 | writeOperand(Operand, true); |
| 1801 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1802 | Out << '('; |
Reid Spencer | 8c4914c | 2006-12-31 05:24:50 +0000 | [diff] [blame] | 1803 | for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) { |
| 1804 | if (op > 1) |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1805 | Out << ", "; |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1806 | writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1807 | } |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1808 | Out << ')'; |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1809 | if (PAL.getFnAttributes() != Attribute::None) |
| 1810 | Out << ' ' << Attribute::getAsString(PAL.getFnAttributes()); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1811 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
Reid Spencer | 1517de3 | 2007-04-09 06:10:42 +0000 | [diff] [blame] | 1812 | const PointerType *PTy = cast<PointerType>(Operand->getType()); |
| 1813 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 1814 | const Type *RetTy = FTy->getReturnType(); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1815 | const AttrListPtr &PAL = II->getAttributes(); |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1816 | |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1817 | // Print the calling convention being used. |
| 1818 | switch (II->getCallingConv()) { |
| 1819 | case CallingConv::C: break; // default |
Chris Lattner | 29d2085 | 2006-05-19 21:58:52 +0000 | [diff] [blame] | 1820 | case CallingConv::Fast: Out << " fastcc"; break; |
| 1821 | case CallingConv::Cold: Out << " coldcc"; break; |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1822 | case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break; |
| 1823 | case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break; |
Anton Korobeynikov | a8fd40b | 2009-06-16 18:50:49 +0000 | [diff] [blame] | 1824 | case CallingConv::ARM_APCS: Out << " arm_apcscc "; break; |
| 1825 | case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break; |
| 1826 | case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break; |
Chris Lattner | f7b6d31 | 2005-05-06 20:26:43 +0000 | [diff] [blame] | 1827 | default: Out << " cc" << II->getCallingConv(); break; |
| 1828 | } |
| 1829 | |
Devang Patel | 221fe42 | 2008-09-29 20:49:50 +0000 | [diff] [blame] | 1830 | if (PAL.getRetAttributes() != Attribute::None) |
| 1831 | Out << ' ' << Attribute::getAsString(PAL.getRetAttributes()); |
| 1832 | |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1833 | // If possible, print out the short form of the invoke instruction. We can |
| 1834 | // only do this if the first argument is a pointer to a nonvararg function, |
| 1835 | // and if the return type is not a pointer to a function. |
| 1836 | // |
Dan Gohman | c7e00ba | 2008-10-15 18:02:08 +0000 | [diff] [blame] | 1837 | Out << ' '; |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1838 | if (!FTy->isVarArg() && |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1839 | (!isa<PointerType>(RetTy) || |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1840 | !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1841 | TypePrinter.print(RetTy, Out); |
Dan Gohman | c7e00ba | 2008-10-15 18:02:08 +0000 | [diff] [blame] | 1842 | Out << ' '; |
Chris Lattner | 463d6a5 | 2003-08-05 15:34:45 +0000 | [diff] [blame] | 1843 | writeOperand(Operand, false); |
| 1844 | } else { |
| 1845 | writeOperand(Operand, true); |
| 1846 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1847 | Out << '('; |
Bill Wendling | 4bb96e9 | 2009-03-13 21:15:59 +0000 | [diff] [blame] | 1848 | for (unsigned op = 3, Eop = I.getNumOperands(); op < Eop; ++op) { |
| 1849 | if (op > 3) |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1850 | Out << ", "; |
Bill Wendling | 4bb96e9 | 2009-03-13 21:15:59 +0000 | [diff] [blame] | 1851 | writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op-2)); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1854 | Out << ')'; |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1855 | if (PAL.getFnAttributes() != Attribute::None) |
| 1856 | Out << ' ' << Attribute::getAsString(PAL.getFnAttributes()); |
| 1857 | |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1858 | Out << "\n\t\t\tto "; |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1859 | writeOperand(II->getNormalDest(), true); |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1860 | Out << " unwind "; |
Chris Lattner | fae8ab3 | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 1861 | writeOperand(II->getUnwindDest(), true); |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1862 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1863 | } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1864 | Out << ' '; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1865 | TypePrinter.print(AI->getType()->getElementType(), Out); |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1866 | if (AI->isArrayAllocation()) { |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1867 | Out << ", "; |
Chris Lattner | 8d48df2 | 2002-04-13 18:34:38 +0000 | [diff] [blame] | 1868 | writeOperand(AI->getArraySize(), true); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1869 | } |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 1870 | if (AI->getAlignment()) { |
Chris Lattner | 7aeee3a | 2005-11-05 21:20:34 +0000 | [diff] [blame] | 1871 | Out << ", align " << AI->getAlignment(); |
Nate Begeman | 848622f | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 1872 | } |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1873 | } else if (isa<CastInst>(I)) { |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1874 | if (Operand) { |
| 1875 | Out << ' '; |
| 1876 | writeOperand(Operand, true); // Work with broken code |
| 1877 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1878 | Out << " to "; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1879 | TypePrinter.print(I.getType(), Out); |
Chris Lattner | 5b33748 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 1880 | } else if (isa<VAArgInst>(I)) { |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1881 | if (Operand) { |
| 1882 | Out << ' '; |
| 1883 | writeOperand(Operand, true); // Work with broken code |
| 1884 | } |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1885 | Out << ", "; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1886 | TypePrinter.print(I.getType(), Out); |
| 1887 | } else if (Operand) { // Print the normal way. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1888 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1889 | // PrintAllTypes - Instructions who have operands of all the same type |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1890 | // omit the type from all but the first operand. If the instruction has |
| 1891 | // different type operands (for example br), then they are all printed. |
| 1892 | bool PrintAllTypes = false; |
| 1893 | const Type *TheType = Operand->getType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1894 | |
Reid Spencer | 0cdd04f | 2007-02-02 13:54:55 +0000 | [diff] [blame] | 1895 | // Select, Store and ShuffleVector always print all types. |
Devang Patel | ce556d9 | 2008-03-04 22:05:14 +0000 | [diff] [blame] | 1896 | if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I) |
| 1897 | || isa<ReturnInst>(I)) { |
Chris Lattner | deccfaf | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1898 | PrintAllTypes = true; |
| 1899 | } else { |
| 1900 | for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) { |
| 1901 | Operand = I.getOperand(i); |
Nuno Lopes | 04fe2f0 | 2009-01-15 18:40:57 +0000 | [diff] [blame] | 1902 | // note that Operand shouldn't be null, but the test helps make dump() |
| 1903 | // more tolerant of malformed IR |
Nuno Lopes | 0971e77 | 2009-01-14 17:51:41 +0000 | [diff] [blame] | 1904 | if (Operand && Operand->getType() != TheType) { |
Chris Lattner | deccfaf | 2003-04-16 20:20:02 +0000 | [diff] [blame] | 1905 | PrintAllTypes = true; // We have differing types! Print them all! |
| 1906 | break; |
| 1907 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1908 | } |
| 1909 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1910 | |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1911 | if (!PrintAllTypes) { |
Misha Brukman | a6619a9 | 2004-06-21 21:53:56 +0000 | [diff] [blame] | 1912 | Out << ' '; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1913 | TypePrinter.print(TheType, Out); |
Chris Lattner | 7bfee41 | 2001-10-29 16:05:51 +0000 | [diff] [blame] | 1914 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1915 | |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1916 | Out << ' '; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1917 | for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) { |
Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1918 | if (i) Out << ", "; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 1919 | writeOperand(I.getOperand(i), PrintAllTypes); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1920 | } |
| 1921 | } |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1922 | |
| 1923 | // Print post operand alignment for load/store |
| 1924 | if (isa<LoadInst>(I) && cast<LoadInst>(I).getAlignment()) { |
| 1925 | Out << ", align " << cast<LoadInst>(I).getAlignment(); |
| 1926 | } else if (isa<StoreInst>(I) && cast<StoreInst>(I).getAlignment()) { |
| 1927 | Out << ", align " << cast<StoreInst>(I).getAlignment(); |
| 1928 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | 862e338 | 2001-10-13 06:42:36 +0000 | [diff] [blame] | 1930 | printInfoComment(I); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | //===----------------------------------------------------------------------===// |
| 1935 | // External Interface declarations |
| 1936 | //===----------------------------------------------------------------------===// |
| 1937 | |
Chris Lattner | 8339f7d | 2003-10-30 23:41:03 +0000 | [diff] [blame] | 1938 | void Module::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1939 | raw_os_ostream OS(o); |
| 1940 | print(OS, AAW); |
| 1941 | } |
| 1942 | void Module::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const { |
Chris Lattner | e36fd8a | 2008-08-19 04:26:57 +0000 | [diff] [blame] | 1943 | SlotTracker SlotTable(this); |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1944 | AssemblyWriter W(OS, SlotTable, this, AAW); |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1945 | W.write(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1948 | void Type::print(std::ostream &o) const { |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1949 | raw_os_ostream OS(o); |
| 1950 | print(OS); |
| 1951 | } |
| 1952 | |
Chris Lattner | 2ee62d2 | 2009-02-28 21:11:05 +0000 | [diff] [blame] | 1953 | void Type::print(raw_ostream &OS) const { |
| 1954 | if (this == 0) { |
| 1955 | OS << "<null Type>"; |
| 1956 | return; |
| 1957 | } |
Chris Lattner | 92c5c12 | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 1958 | TypePrinting().print(this, OS); |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1961 | void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const { |
| 1962 | if (this == 0) { |
| 1963 | OS << "printing a <null> value\n"; |
| 1964 | return; |
| 1965 | } |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1966 | if (const Instruction *I = dyn_cast<Instruction>(this)) { |
| 1967 | const Function *F = I->getParent() ? I->getParent()->getParent() : 0; |
| 1968 | SlotTracker SlotTable(F); |
| 1969 | AssemblyWriter W(OS, SlotTable, F ? F->getParent() : 0, AAW); |
| 1970 | W.write(I); |
| 1971 | } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) { |
| 1972 | SlotTracker SlotTable(BB->getParent()); |
| 1973 | AssemblyWriter W(OS, SlotTable, |
| 1974 | BB->getParent() ? BB->getParent()->getParent() : 0, AAW); |
| 1975 | W.write(BB); |
| 1976 | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) { |
| 1977 | SlotTracker SlotTable(GV->getParent()); |
Dan Gohman | 6c7a485 | 2009-04-20 16:10:33 +0000 | [diff] [blame] | 1978 | AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW); |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1979 | W.write(GV); |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1980 | } else if (const MDString *MDS = dyn_cast<MDString>(this)) { |
| 1981 | TypePrinting TypePrinter; |
| 1982 | TypePrinter.print(MDS->getType(), OS); |
| 1983 | OS << ' '; |
| 1984 | OS << "!\""; |
Devang Patel | 6292003 | 2009-07-23 02:00:51 +0000 | [diff] [blame] | 1985 | PrintEscapedString(MDS->begin(), MDS->length(), OS); |
Devang Patel | 7428d8a | 2009-07-22 17:43:22 +0000 | [diff] [blame] | 1986 | OS << '"'; |
Devang Patel | 5546b98 | 2009-07-01 20:59:15 +0000 | [diff] [blame] | 1987 | } else if (const MDNode *N = dyn_cast<MDNode>(this)) { |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1988 | SlotTracker SlotTable(N); |
Devang Patel | 5546b98 | 2009-07-01 20:59:15 +0000 | [diff] [blame] | 1989 | TypePrinting TypePrinter; |
Devang Patel | 983c6b1 | 2009-07-08 21:44:25 +0000 | [diff] [blame] | 1990 | SlotTable.initialize(); |
| 1991 | WriteMDNodes(OS, TypePrinter, SlotTable); |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1992 | } else if (const Constant *C = dyn_cast<Constant>(this)) { |
Chris Lattner | 92c5c12 | 2009-02-28 23:20:19 +0000 | [diff] [blame] | 1993 | TypePrinting TypePrinter; |
Chris Lattner | e101c44 | 2009-02-28 21:26:53 +0000 | [diff] [blame] | 1994 | TypePrinter.print(C->getType(), OS); |
Chris Lattner | 2ee62d2 | 2009-02-28 21:11:05 +0000 | [diff] [blame] | 1995 | OS << ' '; |
Chris Lattner | 0f57895 | 2009-02-28 20:25:14 +0000 | [diff] [blame] | 1996 | WriteConstantInt(OS, C, TypePrinter, 0); |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 1997 | } else if (const Argument *A = dyn_cast<Argument>(this)) { |
| 1998 | WriteAsOperand(OS, this, true, |
| 1999 | A->getParent() ? A->getParent()->getParent() : 0); |
| 2000 | } else if (isa<InlineAsm>(this)) { |
| 2001 | WriteAsOperand(OS, this, true, 0); |
| 2002 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2003 | llvm_unreachable("Unknown value to print out!"); |
Chris Lattner | 0c19df4 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | void Value::print(std::ostream &O, AssemblyAnnotationWriter *AAW) const { |
| 2008 | raw_os_ostream OS(O); |
| 2009 | print(OS, AAW); |
Chris Lattner | c0b4c7b | 2002-04-08 22:03:40 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Chris Lattner | dab94255 | 2008-08-25 17:03:15 +0000 | [diff] [blame] | 2012 | // Value::dump - allow easy printing of Values from the debugger. |
Dan Gohman | 4f2fea1 | 2009-03-23 15:57:19 +0000 | [diff] [blame] | 2013 | void Value::dump() const { print(errs()); errs() << '\n'; } |
Reid Spencer | 5264183 | 2004-05-25 18:14:38 +0000 | [diff] [blame] | 2014 | |
Chris Lattner | dab94255 | 2008-08-25 17:03:15 +0000 | [diff] [blame] | 2015 | // Type::dump - allow easy printing of Types from the debugger. |
Chris Lattner | 41a83d9 | 2008-10-01 20:16:19 +0000 | [diff] [blame] | 2016 | // This one uses type names from the given context module |
| 2017 | void Type::dump(const Module *Context) const { |
| 2018 | WriteTypeSymbolic(errs(), this, Context); |
| 2019 | errs() << '\n'; |
Chris Lattner | 41a83d9 | 2008-10-01 20:16:19 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Chris Lattner | 2402526 | 2009-02-28 21:05:51 +0000 | [diff] [blame] | 2022 | // Type::dump - allow easy printing of Types from the debugger. |
| 2023 | void Type::dump() const { dump(0); } |
| 2024 | |
Chris Lattner | dab94255 | 2008-08-25 17:03:15 +0000 | [diff] [blame] | 2025 | // Module::dump() - Allow printing of Modules from the debugger. |
Dan Gohman | 4f2fea1 | 2009-03-23 15:57:19 +0000 | [diff] [blame] | 2026 | void Module::dump() const { print(errs(), 0); } |