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