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