Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1 | //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===// |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 10 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringSwitch.h" |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 5c5228a | 2010-03-11 05:53:37 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmLayout.h" |
| 15 | #include "llvm/MC/MCAssembler.h" |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCContext.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCObjectWriter.h" |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSymbol.h" |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCValue.h" |
David Greene | a8cbfb8 | 2010-01-05 01:28:07 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Chandler Carruth | f3e8502 | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chandler Carruth | f58e376 | 2014-04-22 03:04:17 +0000 | [diff] [blame] | 25 | #define DEBUG_TYPE "mcexpr" |
| 26 | |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | namespace stats { |
| 29 | STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations"); |
| 30 | } |
| 31 | } |
| 32 | |
Chris Lattner | c8f7717 | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 33 | void MCExpr::print(raw_ostream &OS) const { |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 34 | switch (getKind()) { |
Chris Lattner | 38d022e | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 35 | case MCExpr::Target: |
| 36 | return cast<MCTargetExpr>(this)->PrintImpl(OS); |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 37 | case MCExpr::Constant: |
| 38 | OS << cast<MCConstantExpr>(*this).getValue(); |
| 39 | return; |
| 40 | |
Chris Lattner | 22833a3 | 2009-09-08 23:20:50 +0000 | [diff] [blame] | 41 | case MCExpr::SymbolRef: { |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 42 | const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this); |
| 43 | const MCSymbol &Sym = SRE.getSymbol(); |
Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 44 | // Parenthesize names that start with $ so that they don't look like |
| 45 | // absolute names. |
| 46 | bool UseParens = Sym.getName()[0] == '$'; |
Chris Lattner | edb9d84 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 47 | if (UseParens) |
Chris Lattner | 8b5d55e | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 48 | OS << '(' << Sym << ')'; |
| 49 | else |
| 50 | OS << Sym; |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 51 | |
Benjamin Kramer | dd13643 | 2014-10-11 17:57:27 +0000 | [diff] [blame] | 52 | if (SRE.getKind() != MCSymbolRefExpr::VK_None) |
| 53 | SRE.printVariantKind(OS); |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 54 | |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 55 | return; |
Chris Lattner | 22833a3 | 2009-09-08 23:20:50 +0000 | [diff] [blame] | 56 | } |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 57 | |
| 58 | case MCExpr::Unary: { |
| 59 | const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this); |
| 60 | switch (UE.getOpcode()) { |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 61 | case MCUnaryExpr::LNot: OS << '!'; break; |
| 62 | case MCUnaryExpr::Minus: OS << '-'; break; |
| 63 | case MCUnaryExpr::Not: OS << '~'; break; |
| 64 | case MCUnaryExpr::Plus: OS << '+'; break; |
| 65 | } |
Chris Lattner | c8f7717 | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 66 | OS << *UE.getSubExpr(); |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 67 | return; |
| 68 | } |
| 69 | |
| 70 | case MCExpr::Binary: { |
| 71 | const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this); |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 3cfc551 | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 73 | // Only print parens around the LHS if it is non-trivial. |
| 74 | if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) { |
Chris Lattner | c8f7717 | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 75 | OS << *BE.getLHS(); |
Chris Lattner | 3cfc551 | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 76 | } else { |
Chris Lattner | c8f7717 | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 77 | OS << '(' << *BE.getLHS() << ')'; |
Chris Lattner | 3cfc551 | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 78 | } |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 79 | |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 80 | switch (BE.getOpcode()) { |
Chris Lattner | 7975b8f | 2009-09-08 06:37:35 +0000 | [diff] [blame] | 81 | case MCBinaryExpr::Add: |
| 82 | // Print "X-42" instead of "X+-42". |
| 83 | if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) { |
| 84 | if (RHSC->getValue() < 0) { |
| 85 | OS << RHSC->getValue(); |
| 86 | return; |
| 87 | } |
| 88 | } |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 7975b8f | 2009-09-08 06:37:35 +0000 | [diff] [blame] | 90 | OS << '+'; |
| 91 | break; |
Ahmed Bougacha | 177c148 | 2015-04-28 00:21:32 +0000 | [diff] [blame] | 92 | case MCBinaryExpr::AShr: OS << ">>"; break; |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 93 | case MCBinaryExpr::And: OS << '&'; break; |
| 94 | case MCBinaryExpr::Div: OS << '/'; break; |
| 95 | case MCBinaryExpr::EQ: OS << "=="; break; |
| 96 | case MCBinaryExpr::GT: OS << '>'; break; |
| 97 | case MCBinaryExpr::GTE: OS << ">="; break; |
| 98 | case MCBinaryExpr::LAnd: OS << "&&"; break; |
| 99 | case MCBinaryExpr::LOr: OS << "||"; break; |
Ahmed Bougacha | 177c148 | 2015-04-28 00:21:32 +0000 | [diff] [blame] | 100 | case MCBinaryExpr::LShr: OS << ">>"; break; |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 101 | case MCBinaryExpr::LT: OS << '<'; break; |
| 102 | case MCBinaryExpr::LTE: OS << "<="; break; |
| 103 | case MCBinaryExpr::Mod: OS << '%'; break; |
| 104 | case MCBinaryExpr::Mul: OS << '*'; break; |
| 105 | case MCBinaryExpr::NE: OS << "!="; break; |
| 106 | case MCBinaryExpr::Or: OS << '|'; break; |
| 107 | case MCBinaryExpr::Shl: OS << "<<"; break; |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 108 | case MCBinaryExpr::Sub: OS << '-'; break; |
| 109 | case MCBinaryExpr::Xor: OS << '^'; break; |
| 110 | } |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 3cfc551 | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 112 | // Only print parens around the LHS if it is non-trivial. |
| 113 | if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) { |
Chris Lattner | c8f7717 | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 114 | OS << *BE.getRHS(); |
Chris Lattner | 3cfc551 | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 115 | } else { |
Chris Lattner | c8f7717 | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 116 | OS << '(' << *BE.getRHS() << ')'; |
Chris Lattner | 3cfc551 | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 117 | } |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | } |
| 121 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 122 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Manman Ren | 49d684e | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 125 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 126 | void MCExpr::dump() const { |
Chris Lattner | c8f7717 | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 127 | print(dbgs()); |
David Greene | a8cbfb8 | 2010-01-05 01:28:07 +0000 | [diff] [blame] | 128 | dbgs() << '\n'; |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 129 | } |
Manman Ren | c3366cc | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 130 | #endif |
Daniel Dunbar | b34a2b9 | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 131 | |
| 132 | /* *** */ |
| 133 | |
Chris Lattner | a0020be | 2009-09-08 06:27:48 +0000 | [diff] [blame] | 134 | const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS, |
| 135 | const MCExpr *RHS, MCContext &Ctx) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 136 | return new (Ctx) MCBinaryExpr(Opc, LHS, RHS); |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | a0020be | 2009-09-08 06:27:48 +0000 | [diff] [blame] | 139 | const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr, |
| 140 | MCContext &Ctx) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 141 | return new (Ctx) MCUnaryExpr(Opc, Expr); |
| 142 | } |
| 143 | |
| 144 | const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) { |
| 145 | return new (Ctx) MCConstantExpr(Value); |
| 146 | } |
| 147 | |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 148 | /* *** */ |
| 149 | |
Benjamin Kramer | dd13643 | 2014-10-11 17:57:27 +0000 | [diff] [blame] | 150 | MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind, |
| 151 | const MCAsmInfo *MAI) |
| 152 | : MCExpr(MCExpr::SymbolRef), Kind(Kind), |
| 153 | UseParensForSymbolVariant(MAI->useParensForSymbolVariant()), |
| 154 | HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()), |
| 155 | Symbol(Symbol) { |
| 156 | assert(Symbol); |
| 157 | } |
| 158 | |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 159 | const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym, |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 160 | VariantKind Kind, |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 161 | MCContext &Ctx) { |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 162 | return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo()); |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 165 | const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind, |
| 166 | MCContext &Ctx) { |
| 167 | return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx); |
Chris Lattner | 6a833f6 | 2009-09-16 01:26:31 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 170 | StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { |
| 171 | switch (Kind) { |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 172 | case VK_Invalid: return "<<invalid>>"; |
| 173 | case VK_None: return "<<none>>"; |
| 174 | |
| 175 | case VK_GOT: return "GOT"; |
| 176 | case VK_GOTOFF: return "GOTOFF"; |
| 177 | case VK_GOTPCREL: return "GOTPCREL"; |
| 178 | case VK_GOTTPOFF: return "GOTTPOFF"; |
| 179 | case VK_INDNTPOFF: return "INDNTPOFF"; |
| 180 | case VK_NTPOFF: return "NTPOFF"; |
Rafael Espindola | b3b49bb | 2010-10-28 14:22:44 +0000 | [diff] [blame] | 181 | case VK_GOTNTPOFF: return "GOTNTPOFF"; |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 182 | case VK_PLT: return "PLT"; |
| 183 | case VK_TLSGD: return "TLSGD"; |
Rafael Espindola | 2dbec3f | 2010-10-28 15:02:40 +0000 | [diff] [blame] | 184 | case VK_TLSLD: return "TLSLD"; |
Rafael Espindola | 6f23eb3 | 2010-10-28 14:37:09 +0000 | [diff] [blame] | 185 | case VK_TLSLDM: return "TLSLDM"; |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 186 | case VK_TPOFF: return "TPOFF"; |
Rafael Espindola | e8f08be | 2010-10-28 14:48:59 +0000 | [diff] [blame] | 187 | case VK_DTPOFF: return "DTPOFF"; |
Chris Lattner | 94f0c14 | 2010-11-14 22:22:59 +0000 | [diff] [blame] | 188 | case VK_TLVP: return "TLVP"; |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 189 | case VK_TLVPPAGE: return "TLVPPAGE"; |
| 190 | case VK_TLVPPAGEOFF: return "TLVPPAGEOFF"; |
| 191 | case VK_PAGE: return "PAGE"; |
| 192 | case VK_PAGEOFF: return "PAGEOFF"; |
| 193 | case VK_GOTPAGE: return "GOTPAGE"; |
| 194 | case VK_GOTPAGEOFF: return "GOTPAGEOFF"; |
Benjamin Kramer | 9c9e0a2 | 2013-03-30 16:21:50 +0000 | [diff] [blame] | 195 | case VK_SECREL: return "SECREL32"; |
Davide Italiano | fcae934 | 2015-03-04 06:49:39 +0000 | [diff] [blame] | 196 | case VK_SIZE: return "SIZE"; |
Rafael Espindola | 7fadc0e | 2014-03-20 02:12:01 +0000 | [diff] [blame] | 197 | case VK_WEAKREF: return "WEAKREF"; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 198 | case VK_ARM_NONE: return "none"; |
| 199 | case VK_ARM_TARGET1: return "target1"; |
| 200 | case VK_ARM_TARGET2: return "target2"; |
| 201 | case VK_ARM_PREL31: return "prel31"; |
Saleem Abdulrasool | fe78197 | 2015-01-11 04:39:18 +0000 | [diff] [blame] | 202 | case VK_ARM_SBREL: return "sbrel"; |
Kai Nacke | e51c813 | 2014-01-20 11:00:40 +0000 | [diff] [blame] | 203 | case VK_ARM_TLSLDO: return "tlsldo"; |
Saleem Abdulrasool | 6e00ca8 | 2014-01-30 04:02:31 +0000 | [diff] [blame] | 204 | case VK_ARM_TLSCALL: return "tlscall"; |
Saleem Abdulrasool | a3f12bd | 2014-01-30 04:02:38 +0000 | [diff] [blame] | 205 | case VK_ARM_TLSDESC: return "tlsdesc"; |
Saleem Abdulrasool | 56e06e8 | 2014-01-30 04:02:47 +0000 | [diff] [blame] | 206 | case VK_ARM_TLSDESCSEQ: return "tlsdescseq"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 207 | case VK_PPC_LO: return "l"; |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 208 | case VK_PPC_HI: return "h"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 209 | case VK_PPC_HA: return "ha"; |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 210 | case VK_PPC_HIGHER: return "higher"; |
| 211 | case VK_PPC_HIGHERA: return "highera"; |
| 212 | case VK_PPC_HIGHEST: return "highest"; |
| 213 | case VK_PPC_HIGHESTA: return "highesta"; |
Ulrich Weigand | 93372b4 | 2013-06-25 16:49:50 +0000 | [diff] [blame] | 214 | case VK_PPC_GOT_LO: return "got@l"; |
| 215 | case VK_PPC_GOT_HI: return "got@h"; |
| 216 | case VK_PPC_GOT_HA: return "got@ha"; |
Ulrich Weigand | 68e2e1b | 2013-06-20 22:39:42 +0000 | [diff] [blame] | 217 | case VK_PPC_TOCBASE: return "tocbase"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 218 | case VK_PPC_TOC: return "toc"; |
| 219 | case VK_PPC_TOC_LO: return "toc@l"; |
Ulrich Weigand | 72ddbd6 | 2013-06-21 14:43:10 +0000 | [diff] [blame] | 220 | case VK_PPC_TOC_HI: return "toc@h"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 221 | case VK_PPC_TOC_HA: return "toc@ha"; |
Ulrich Weigand | f11efe7 | 2013-07-01 23:33:29 +0000 | [diff] [blame] | 222 | case VK_PPC_DTPMOD: return "dtpmod"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 223 | case VK_PPC_TPREL: return "tprel"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 224 | case VK_PPC_TPREL_LO: return "tprel@l"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 225 | case VK_PPC_TPREL_HI: return "tprel@h"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 226 | case VK_PPC_TPREL_HA: return "tprel@ha"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 227 | case VK_PPC_TPREL_HIGHER: return "tprel@higher"; |
| 228 | case VK_PPC_TPREL_HIGHERA: return "tprel@highera"; |
| 229 | case VK_PPC_TPREL_HIGHEST: return "tprel@highest"; |
| 230 | case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta"; |
| 231 | case VK_PPC_DTPREL: return "dtprel"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 232 | case VK_PPC_DTPREL_LO: return "dtprel@l"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 233 | case VK_PPC_DTPREL_HI: return "dtprel@h"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 234 | case VK_PPC_DTPREL_HA: return "dtprel@ha"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 235 | case VK_PPC_DTPREL_HIGHER: return "dtprel@higher"; |
| 236 | case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera"; |
| 237 | case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest"; |
| 238 | case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta"; |
| 239 | case VK_PPC_GOT_TPREL: return "got@tprel"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 240 | case VK_PPC_GOT_TPREL_LO: return "got@tprel@l"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 241 | case VK_PPC_GOT_TPREL_HI: return "got@tprel@h"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 242 | case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 243 | case VK_PPC_GOT_DTPREL: return "got@dtprel"; |
| 244 | case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l"; |
| 245 | case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h"; |
| 246 | case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha"; |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 247 | case VK_PPC_TLS: return "tls"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 248 | case VK_PPC_GOT_TLSGD: return "got@tlsgd"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 249 | case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 250 | case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 251 | case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha"; |
Ulrich Weigand | 52cf8e4 | 2013-07-09 16:41:09 +0000 | [diff] [blame] | 252 | case VK_PPC_TLSGD: return "tlsgd"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 253 | case VK_PPC_GOT_TLSLD: return "got@tlsld"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 254 | case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l"; |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 255 | case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h"; |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 256 | case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha"; |
Ulrich Weigand | 52cf8e4 | 2013-07-09 16:41:09 +0000 | [diff] [blame] | 257 | case VK_PPC_TLSLD: return "tlsld"; |
Justin Hibbits | a88b605 | 2014-11-12 15:16:30 +0000 | [diff] [blame] | 258 | case VK_PPC_LOCAL: return "local"; |
Akira Hatanaka | e2eed96 | 2011-12-22 01:05:17 +0000 | [diff] [blame] | 259 | case VK_Mips_GPREL: return "GPREL"; |
| 260 | case VK_Mips_GOT_CALL: return "GOT_CALL"; |
| 261 | case VK_Mips_GOT16: return "GOT16"; |
| 262 | case VK_Mips_GOT: return "GOT"; |
| 263 | case VK_Mips_ABS_HI: return "ABS_HI"; |
| 264 | case VK_Mips_ABS_LO: return "ABS_LO"; |
| 265 | case VK_Mips_TLSGD: return "TLSGD"; |
| 266 | case VK_Mips_TLSLDM: return "TLSLDM"; |
| 267 | case VK_Mips_DTPREL_HI: return "DTPREL_HI"; |
| 268 | case VK_Mips_DTPREL_LO: return "DTPREL_LO"; |
| 269 | case VK_Mips_GOTTPREL: return "GOTTPREL"; |
| 270 | case VK_Mips_TPREL_HI: return "TPREL_HI"; |
| 271 | case VK_Mips_TPREL_LO: return "TPREL_LO"; |
| 272 | case VK_Mips_GPOFF_HI: return "GPOFF_HI"; |
| 273 | case VK_Mips_GPOFF_LO: return "GPOFF_LO"; |
| 274 | case VK_Mips_GOT_DISP: return "GOT_DISP"; |
| 275 | case VK_Mips_GOT_PAGE: return "GOT_PAGE"; |
| 276 | case VK_Mips_GOT_OFST: return "GOT_OFST"; |
Akira Hatanaka | f73e362 | 2012-07-21 02:15:19 +0000 | [diff] [blame] | 277 | case VK_Mips_HIGHER: return "HIGHER"; |
| 278 | case VK_Mips_HIGHEST: return "HIGHEST"; |
Akira Hatanaka | 64b52d8 | 2012-11-21 19:50:22 +0000 | [diff] [blame] | 279 | case VK_Mips_GOT_HI16: return "GOT_HI16"; |
| 280 | case VK_Mips_GOT_LO16: return "GOT_LO16"; |
| 281 | case VK_Mips_CALL_HI16: return "CALL_HI16"; |
| 282 | case VK_Mips_CALL_LO16: return "CALL_LO16"; |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 283 | case VK_Mips_PCREL_HI16: return "PCREL_HI16"; |
| 284 | case VK_Mips_PCREL_LO16: return "PCREL_LO16"; |
Reid Kleckner | 81782f0 | 2014-09-25 02:09:18 +0000 | [diff] [blame] | 285 | case VK_COFF_IMGREL32: return "IMGREL"; |
Colin LeMahieu | b662565 | 2015-05-01 21:14:21 +0000 | [diff] [blame^] | 286 | case VK_Hexagon_PCREL: return "PCREL"; |
| 287 | case VK_Hexagon_LO16: return "LO16"; |
| 288 | case VK_Hexagon_HI16: return "HI16"; |
| 289 | case VK_Hexagon_GPREL: return "GPREL"; |
| 290 | case VK_Hexagon_GD_GOT: return "GDGOT"; |
| 291 | case VK_Hexagon_LD_GOT: return "LDGOT"; |
| 292 | case VK_Hexagon_GD_PLT: return "GDPLT"; |
| 293 | case VK_Hexagon_LD_PLT: return "LDPLT"; |
| 294 | case VK_Hexagon_IE: return "IE"; |
| 295 | case VK_Hexagon_IE_GOT: return "IEGOT"; |
| 296 | case VK_TPREL: return "tprel"; |
| 297 | case VK_DTPREL: return "dtprel"; |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 298 | } |
Chandler Carruth | f3e8502 | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 299 | llvm_unreachable("Invalid variant kind"); |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | MCSymbolRefExpr::VariantKind |
| 303 | MCSymbolRefExpr::getVariantKindForName(StringRef Name) { |
Roman Divacky | a93d002 | 2014-12-18 23:12:34 +0000 | [diff] [blame] | 304 | return StringSwitch<VariantKind>(Name.lower()) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 305 | .Case("got", VK_GOT) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 306 | .Case("gotoff", VK_GOTOFF) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 307 | .Case("gotpcrel", VK_GOTPCREL) |
Joerg Sonnenberger | dd18d5b | 2014-04-29 13:42:02 +0000 | [diff] [blame] | 308 | .Case("got_prel", VK_GOTPCREL) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 309 | .Case("gottpoff", VK_GOTTPOFF) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 310 | .Case("indntpoff", VK_INDNTPOFF) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 311 | .Case("ntpoff", VK_NTPOFF) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 312 | .Case("gotntpoff", VK_GOTNTPOFF) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 313 | .Case("plt", VK_PLT) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 314 | .Case("tlsgd", VK_TLSGD) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 315 | .Case("tlsld", VK_TLSLD) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 316 | .Case("tlsldm", VK_TLSLDM) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 317 | .Case("tpoff", VK_TPOFF) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 318 | .Case("dtpoff", VK_DTPOFF) |
Rafael Espindola | 8bac423 | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 319 | .Case("tlvp", VK_TLVP) |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 320 | .Case("tlvppage", VK_TLVPPAGE) |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 321 | .Case("tlvppageoff", VK_TLVPPAGEOFF) |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 322 | .Case("page", VK_PAGE) |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 323 | .Case("pageoff", VK_PAGEOFF) |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 324 | .Case("gotpage", VK_GOTPAGE) |
Tim Northover | 00ed996 | 2014-03-29 10:18:08 +0000 | [diff] [blame] | 325 | .Case("gotpageoff", VK_GOTPAGEOFF) |
Nico Rieck | 1da4529 | 2013-04-10 23:28:17 +0000 | [diff] [blame] | 326 | .Case("imgrel", VK_COFF_IMGREL32) |
Rafael Espindola | b770f89 | 2013-04-25 19:27:05 +0000 | [diff] [blame] | 327 | .Case("secrel32", VK_SECREL) |
Davide Italiano | fcae934 | 2015-03-04 06:49:39 +0000 | [diff] [blame] | 328 | .Case("size", VK_SIZE) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 329 | .Case("l", VK_PPC_LO) |
Ulrich Weigand | e67c565 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 330 | .Case("h", VK_PPC_HI) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 331 | .Case("ha", VK_PPC_HA) |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 332 | .Case("higher", VK_PPC_HIGHER) |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 333 | .Case("highera", VK_PPC_HIGHERA) |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 334 | .Case("highest", VK_PPC_HIGHEST) |
Ulrich Weigand | e9126f5 | 2013-06-21 14:43:42 +0000 | [diff] [blame] | 335 | .Case("highesta", VK_PPC_HIGHESTA) |
Ulrich Weigand | 93372b4 | 2013-06-25 16:49:50 +0000 | [diff] [blame] | 336 | .Case("got@l", VK_PPC_GOT_LO) |
Ulrich Weigand | 93372b4 | 2013-06-25 16:49:50 +0000 | [diff] [blame] | 337 | .Case("got@h", VK_PPC_GOT_HI) |
Ulrich Weigand | 93372b4 | 2013-06-25 16:49:50 +0000 | [diff] [blame] | 338 | .Case("got@ha", VK_PPC_GOT_HA) |
Justin Hibbits | 0c0d5de | 2014-12-17 06:23:35 +0000 | [diff] [blame] | 339 | .Case("local", VK_PPC_LOCAL) |
Ulrich Weigand | 68e2e1b | 2013-06-20 22:39:42 +0000 | [diff] [blame] | 340 | .Case("tocbase", VK_PPC_TOCBASE) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 341 | .Case("toc", VK_PPC_TOC) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 342 | .Case("toc@l", VK_PPC_TOC_LO) |
Ulrich Weigand | 72ddbd6 | 2013-06-21 14:43:10 +0000 | [diff] [blame] | 343 | .Case("toc@h", VK_PPC_TOC_HI) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 344 | .Case("toc@ha", VK_PPC_TOC_HA) |
Ulrich Weigand | 2c3a219 | 2013-05-03 19:52:35 +0000 | [diff] [blame] | 345 | .Case("tls", VK_PPC_TLS) |
Ulrich Weigand | f11efe7 | 2013-07-01 23:33:29 +0000 | [diff] [blame] | 346 | .Case("dtpmod", VK_PPC_DTPMOD) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 347 | .Case("tprel", VK_PPC_TPREL) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 348 | .Case("tprel@l", VK_PPC_TPREL_LO) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 349 | .Case("tprel@h", VK_PPC_TPREL_HI) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 350 | .Case("tprel@ha", VK_PPC_TPREL_HA) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 351 | .Case("tprel@higher", VK_PPC_TPREL_HIGHER) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 352 | .Case("tprel@highera", VK_PPC_TPREL_HIGHERA) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 353 | .Case("tprel@highest", VK_PPC_TPREL_HIGHEST) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 354 | .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 355 | .Case("dtprel", VK_PPC_DTPREL) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 356 | .Case("dtprel@l", VK_PPC_DTPREL_LO) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 357 | .Case("dtprel@h", VK_PPC_DTPREL_HI) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 358 | .Case("dtprel@ha", VK_PPC_DTPREL_HA) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 359 | .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 360 | .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 361 | .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 362 | .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 363 | .Case("got@tprel", VK_PPC_GOT_TPREL) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 364 | .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 365 | .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 366 | .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 367 | .Case("got@dtprel", VK_PPC_GOT_DTPREL) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 368 | .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 369 | .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 370 | .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 371 | .Case("got@tlsgd", VK_PPC_GOT_TLSGD) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 372 | .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 373 | .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 374 | .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 375 | .Case("got@tlsld", VK_PPC_GOT_TLSLD) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 376 | .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO) |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 377 | .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI) |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 378 | .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA) |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 379 | .Case("none", VK_ARM_NONE) |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 380 | .Case("target1", VK_ARM_TARGET1) |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 381 | .Case("target2", VK_ARM_TARGET2) |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 382 | .Case("prel31", VK_ARM_PREL31) |
Saleem Abdulrasool | fe78197 | 2015-01-11 04:39:18 +0000 | [diff] [blame] | 383 | .Case("sbrel", VK_ARM_SBREL) |
Kai Nacke | e51c813 | 2014-01-20 11:00:40 +0000 | [diff] [blame] | 384 | .Case("tlsldo", VK_ARM_TLSLDO) |
Saleem Abdulrasool | 6e00ca8 | 2014-01-30 04:02:31 +0000 | [diff] [blame] | 385 | .Case("tlscall", VK_ARM_TLSCALL) |
Saleem Abdulrasool | a3f12bd | 2014-01-30 04:02:38 +0000 | [diff] [blame] | 386 | .Case("tlsdesc", VK_ARM_TLSDESC) |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 387 | .Default(VK_Invalid); |
| 388 | } |
| 389 | |
Benjamin Kramer | dd13643 | 2014-10-11 17:57:27 +0000 | [diff] [blame] | 390 | void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const { |
| 391 | if (UseParensForSymbolVariant) |
| 392 | OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')'; |
| 393 | else |
| 394 | OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind()); |
| 395 | } |
| 396 | |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 397 | /* *** */ |
| 398 | |
Craig Topper | 2a6a08b | 2012-09-26 06:36:36 +0000 | [diff] [blame] | 399 | void MCTargetExpr::anchor() {} |
Chris Lattner | 6a833f6 | 2009-09-16 01:26:31 +0000 | [diff] [blame] | 400 | |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 401 | /* *** */ |
| 402 | |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 403 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const { |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 404 | return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr); |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res, |
| 408 | const MCAsmLayout &Layout) const { |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 409 | return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr); |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res, |
| 413 | const MCAsmLayout &Layout, |
| 414 | const SectionAddrMap &Addrs) const { |
| 415 | return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs); |
| 416 | } |
| 417 | |
| 418 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const { |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 419 | return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr); |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Rafael Espindola | dbb4021 | 2015-03-25 00:25:37 +0000 | [diff] [blame] | 422 | bool MCExpr::evaluateKnownAbsolute(int64_t &Res, |
| 423 | const MCAsmLayout &Layout) const { |
| 424 | return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, |
| 425 | true); |
Rafael Espindola | adbe024 | 2014-08-15 14:20:32 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 428 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, |
| 429 | const MCAsmLayout *Layout, |
Rafael Espindola | 93e3cf0 | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 430 | const SectionAddrMap *Addrs) const { |
Rafael Espindola | adbe024 | 2014-08-15 14:20:32 +0000 | [diff] [blame] | 431 | // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us |
| 432 | // absolutize differences across sections and that is what the MachO writer |
| 433 | // uses Addrs for. |
| 434 | return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs); |
| 435 | } |
| 436 | |
| 437 | bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, |
| 438 | const MCAsmLayout *Layout, |
| 439 | const SectionAddrMap *Addrs, bool InSet) const { |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 440 | MCValue Value; |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 441 | |
Daniel Dunbar | 5cfb587 | 2010-03-23 23:47:07 +0000 | [diff] [blame] | 442 | // Fast path constants. |
| 443 | if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) { |
| 444 | Res = CE->getValue(); |
| 445 | return true; |
| 446 | } |
| 447 | |
Rafael Espindola | f275ad8 | 2015-03-25 13:16:53 +0000 | [diff] [blame] | 448 | bool IsRelocatable = |
| 449 | EvaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet); |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 450 | |
Rafael Espindola | 4124ab1 | 2010-12-22 22:16:24 +0000 | [diff] [blame] | 451 | // Record the current value. |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 452 | Res = Value.getConstant(); |
Rafael Espindola | 4124ab1 | 2010-12-22 22:16:24 +0000 | [diff] [blame] | 453 | |
| 454 | return IsRelocatable && Value.isAbsolute(); |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 457 | /// \brief Helper method for \see EvaluateSymbolAdd(). |
Rafael Espindola | 59f90b2 | 2015-03-25 19:24:39 +0000 | [diff] [blame] | 458 | static void AttemptToFoldSymbolOffsetDifference( |
| 459 | const MCAssembler *Asm, const MCAsmLayout *Layout, |
| 460 | const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A, |
| 461 | const MCSymbolRefExpr *&B, int64_t &Addend) { |
Rafael Espindola | ee54636 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 462 | if (!A || !B) |
| 463 | return; |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 464 | |
Rafael Espindola | ee54636 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 465 | const MCSymbol &SA = A->getSymbol(); |
| 466 | const MCSymbol &SB = B->getSymbol(); |
| 467 | |
| 468 | if (SA.isUndefined() || SB.isUndefined()) |
| 469 | return; |
| 470 | |
| 471 | if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet)) |
| 472 | return; |
| 473 | |
David Blaikie | 908f4d4 | 2014-04-24 16:59:40 +0000 | [diff] [blame] | 474 | const MCSymbolData &AD = Asm->getSymbolData(SA); |
| 475 | const MCSymbolData &BD = Asm->getSymbolData(SB); |
Rafael Espindola | ee54636 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 476 | |
| 477 | if (AD.getFragment() == BD.getFragment()) { |
| 478 | Addend += (AD.getOffset() - BD.getOffset()); |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 479 | |
Owen Anderson | 9746286 | 2011-03-21 23:13:43 +0000 | [diff] [blame] | 480 | // Pointers to Thumb symbols need to have their low-bit set to allow |
| 481 | // for interworking. |
| 482 | if (Asm->isThumbFunc(&SA)) |
| 483 | Addend |= 1; |
| 484 | |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 485 | // Clear the symbol expr pointers to indicate we have folded these |
| 486 | // operands. |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 487 | A = B = nullptr; |
Rafael Espindola | ee54636 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 488 | return; |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 489 | } |
Rafael Espindola | ee54636 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 490 | |
| 491 | if (!Layout) |
| 492 | return; |
| 493 | |
| 494 | const MCSectionData &SecA = *AD.getFragment()->getParent(); |
| 495 | const MCSectionData &SecB = *BD.getFragment()->getParent(); |
| 496 | |
| 497 | if ((&SecA != &SecB) && !Addrs) |
| 498 | return; |
| 499 | |
| 500 | // Eagerly evaluate. |
| 501 | Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) - |
| 502 | Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol()))); |
| 503 | if (Addrs && (&SecA != &SecB)) |
| 504 | Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB)); |
| 505 | |
Jim Grosbach | 213039a | 2012-02-24 05:12:18 +0000 | [diff] [blame] | 506 | // Pointers to Thumb symbols need to have their low-bit set to allow |
| 507 | // for interworking. |
| 508 | if (Asm->isThumbFunc(&SA)) |
| 509 | Addend |= 1; |
| 510 | |
Rafael Espindola | ee54636 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 511 | // Clear the symbol expr pointers to indicate we have folded these |
| 512 | // operands. |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 513 | A = B = nullptr; |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Daniel Dunbar | 395a099 | 2010-12-16 18:36:25 +0000 | [diff] [blame] | 516 | /// \brief Evaluate the result of an add between (conceptually) two MCValues. |
| 517 | /// |
| 518 | /// This routine conceptually attempts to construct an MCValue: |
| 519 | /// Result = (Result_A - Result_B + Result_Cst) |
| 520 | /// from two MCValue's LHS and RHS where |
| 521 | /// Result = LHS + RHS |
| 522 | /// and |
| 523 | /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst). |
| 524 | /// |
| 525 | /// This routine attempts to aggresively fold the operands such that the result |
| 526 | /// is representable in an MCValue, but may not always succeed. |
| 527 | /// |
| 528 | /// \returns True on success, false if the result is not representable in an |
| 529 | /// MCValue. |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 530 | |
| 531 | /// NOTE: It is really important to have both the Asm and Layout arguments. |
| 532 | /// They might look redundant, but this function can be used before layout |
| 533 | /// is done (see the object streamer for example) and having the Asm argument |
Rafael Espindola | 1ea7f18 | 2010-12-18 04:01:45 +0000 | [diff] [blame] | 534 | /// lets us avoid relaxations early. |
Rafael Espindola | 59f90b2 | 2015-03-25 19:24:39 +0000 | [diff] [blame] | 535 | static bool |
| 536 | EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout, |
| 537 | const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS, |
| 538 | const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B, |
| 539 | int64_t RHS_Cst, MCValue &Res) { |
Daniel Dunbar | 76793ba | 2010-12-17 01:07:20 +0000 | [diff] [blame] | 540 | // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy |
| 541 | // about dealing with modifiers. This will ultimately bite us, one day. |
| 542 | const MCSymbolRefExpr *LHS_A = LHS.getSymA(); |
| 543 | const MCSymbolRefExpr *LHS_B = LHS.getSymB(); |
| 544 | int64_t LHS_Cst = LHS.getConstant(); |
| 545 | |
| 546 | // Fold the result constant immediately. |
| 547 | int64_t Result_Cst = LHS_Cst + RHS_Cst; |
| 548 | |
Rafael Espindola | ee54636 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 549 | assert((!Layout || Asm) && |
| 550 | "Must have an assembler object if layout is given!"); |
| 551 | |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 552 | // If we have a layout, we can fold resolved differences. |
Rafael Espindola | ee54636 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 553 | if (Asm) { |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 554 | // First, fold out any differences which are fully resolved. By |
| 555 | // reassociating terms in |
| 556 | // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst). |
| 557 | // we have the four possible differences: |
| 558 | // (LHS_A - LHS_B), |
| 559 | // (LHS_A - RHS_B), |
| 560 | // (RHS_A - LHS_B), |
| 561 | // (RHS_A - RHS_B). |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 562 | // Since we are attempting to be as aggressive as possible about folding, we |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 563 | // attempt to evaluate each possible alternative. |
Rafael Espindola | ee54636 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 564 | AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B, |
| 565 | Result_Cst); |
| 566 | AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B, |
| 567 | Result_Cst); |
| 568 | AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B, |
| 569 | Result_Cst); |
| 570 | AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B, |
| 571 | Result_Cst); |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Daniel Dunbar | 76793ba | 2010-12-17 01:07:20 +0000 | [diff] [blame] | 574 | // We can't represent the addition or subtraction of two symbols. |
| 575 | if ((LHS_A && RHS_A) || (LHS_B && RHS_B)) |
Daniel Dunbar | 19f847f | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 576 | return false; |
| 577 | |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 578 | // At this point, we have at most one additive symbol and one subtractive |
| 579 | // symbol -- find them. |
Daniel Dunbar | 76793ba | 2010-12-17 01:07:20 +0000 | [diff] [blame] | 580 | const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A; |
| 581 | const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B; |
Daniel Dunbar | 137d422 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 582 | |
| 583 | // If we have a negated symbol, then we must have also have a non-negated |
| 584 | // symbol in order to encode the expression. |
| 585 | if (B && !A) |
| 586 | return false; |
Rafael Espindola | 4262a22 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 587 | |
Daniel Dunbar | 76793ba | 2010-12-17 01:07:20 +0000 | [diff] [blame] | 588 | Res = MCValue::get(A, B, Result_Cst); |
Daniel Dunbar | 19f847f | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 589 | return true; |
| 590 | } |
| 591 | |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 592 | bool MCExpr::EvaluateAsRelocatable(MCValue &Res, |
Joerg Sonnenberger | 752b91b | 2014-08-10 11:35:12 +0000 | [diff] [blame] | 593 | const MCAsmLayout *Layout, |
Joerg Sonnenberger | b696d46 | 2014-08-10 11:37:07 +0000 | [diff] [blame] | 594 | const MCFixup *Fixup) const { |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 595 | MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr; |
Joerg Sonnenberger | 752b91b | 2014-08-10 11:35:12 +0000 | [diff] [blame] | 596 | return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr, |
Rafael Espindola | f275ad8 | 2015-03-25 13:16:53 +0000 | [diff] [blame] | 597 | false); |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Rafael Espindola | aeed3cb | 2015-03-26 21:11:00 +0000 | [diff] [blame] | 600 | bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const { |
| 601 | MCAssembler *Assembler = &Layout.getAssembler(); |
| 602 | return EvaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr, |
| 603 | true); |
| 604 | } |
| 605 | |
| 606 | static bool canExpand(const MCSymbol &Sym, const MCAssembler *Asm, bool InSet) { |
| 607 | if (InSet) |
| 608 | return true; |
Rafael Espindola | f275ad8 | 2015-03-25 13:16:53 +0000 | [diff] [blame] | 609 | if (!Asm) |
| 610 | return false; |
| 611 | const MCSymbolData &SD = Asm->getSymbolData(Sym); |
| 612 | return !Asm->getWriter().isWeak(SD); |
Rafael Espindola | bc91d7e | 2014-04-28 20:53:11 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, |
Rafael Espindola | 4262a22 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 616 | const MCAsmLayout *Layout, |
Joerg Sonnenberger | 752b91b | 2014-08-10 11:35:12 +0000 | [diff] [blame] | 617 | const MCFixup *Fixup, |
Rafael Espindola | f275ad8 | 2015-03-25 13:16:53 +0000 | [diff] [blame] | 618 | const SectionAddrMap *Addrs, |
| 619 | bool InSet) const { |
Daniel Dunbar | 5376c2a | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 620 | ++stats::MCExprEvaluate; |
| 621 | |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 622 | switch (getKind()) { |
Chris Lattner | 38d022e | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 623 | case Target: |
Joerg Sonnenberger | 752b91b | 2014-08-10 11:35:12 +0000 | [diff] [blame] | 624 | return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout, |
| 625 | Fixup); |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 626 | |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 627 | case Constant: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 628 | Res = MCValue::get(cast<MCConstantExpr>(this)->getValue()); |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 629 | return true; |
| 630 | |
| 631 | case SymbolRef: { |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 632 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this); |
| 633 | const MCSymbol &Sym = SRE->getSymbol(); |
Daniel Dunbar | 17b9027 | 2009-10-16 01:33:57 +0000 | [diff] [blame] | 634 | |
| 635 | // Evaluate recursively if this is a variable. |
Rafael Espindola | f275ad8 | 2015-03-25 13:16:53 +0000 | [diff] [blame] | 636 | if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None && |
Rafael Espindola | aeed3cb | 2015-03-26 21:11:00 +0000 | [diff] [blame] | 637 | canExpand(Sym, Asm, InSet)) { |
| 638 | bool IsMachO = SRE->hasSubsectionsViaSymbols(); |
Rafael Espindola | bc91d7e | 2014-04-28 20:53:11 +0000 | [diff] [blame] | 639 | if (Sym.getVariableValue()->EvaluateAsRelocatableImpl( |
Rafael Espindola | aeed3cb | 2015-03-26 21:11:00 +0000 | [diff] [blame] | 640 | Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) { |
| 641 | if (!IsMachO) |
Rafael Espindola | f275ad8 | 2015-03-25 13:16:53 +0000 | [diff] [blame] | 642 | return true; |
| 643 | |
Rafael Espindola | 7fadc0e | 2014-03-20 02:12:01 +0000 | [diff] [blame] | 644 | const MCSymbolRefExpr *A = Res.getSymA(); |
| 645 | const MCSymbolRefExpr *B = Res.getSymB(); |
Rafael Espindola | f275ad8 | 2015-03-25 13:16:53 +0000 | [diff] [blame] | 646 | // FIXME: This is small hack. Given |
| 647 | // a = b + 4 |
| 648 | // .long a |
| 649 | // the OS X assembler will completely drop the 4. We should probably |
| 650 | // include it in the relocation or produce an error if that is not |
| 651 | // possible. |
| 652 | if (!A && !B) |
| 653 | return true; |
Rafael Espindola | 7fadc0e | 2014-03-20 02:12:01 +0000 | [diff] [blame] | 654 | } |
Rafael Espindola | 8c3039b | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 655 | } |
Daniel Dunbar | 17b9027 | 2009-10-16 01:33:57 +0000 | [diff] [blame] | 656 | |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 657 | Res = MCValue::get(SRE, nullptr, 0); |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 658 | return true; |
| 659 | } |
| 660 | |
| 661 | case Unary: { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 662 | const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this); |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 663 | MCValue Value; |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 664 | |
Rafael Espindola | f275ad8 | 2015-03-25 13:16:53 +0000 | [diff] [blame] | 665 | if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout, Fixup, |
| 666 | Addrs, InSet)) |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 667 | return false; |
| 668 | |
| 669 | switch (AUE->getOpcode()) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 670 | case MCUnaryExpr::LNot: |
Daniel Dunbar | 0d5fc9a | 2009-07-01 06:48:00 +0000 | [diff] [blame] | 671 | if (!Value.isAbsolute()) |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 672 | return false; |
| 673 | Res = MCValue::get(!Value.getConstant()); |
| 674 | break; |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 675 | case MCUnaryExpr::Minus: |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 676 | /// -(a - b + const) ==> (b - a - const) |
Daniel Dunbar | 7f8a9eb | 2009-08-11 17:47:52 +0000 | [diff] [blame] | 677 | if (Value.getSymA() && !Value.getSymB()) |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 678 | return false; |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 679 | Res = MCValue::get(Value.getSymB(), Value.getSymA(), |
| 680 | -Value.getConstant()); |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 681 | break; |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 682 | case MCUnaryExpr::Not: |
Daniel Dunbar | 0d5fc9a | 2009-07-01 06:48:00 +0000 | [diff] [blame] | 683 | if (!Value.isAbsolute()) |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 684 | return false; |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 685 | Res = MCValue::get(~Value.getConstant()); |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 686 | break; |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 687 | case MCUnaryExpr::Plus: |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 688 | Res = Value; |
| 689 | break; |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | return true; |
| 693 | } |
| 694 | |
| 695 | case Binary: { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 696 | const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this); |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 697 | MCValue LHSValue, RHSValue; |
Daniel Dunbar | 9c64ec0 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 698 | |
Rafael Espindola | f275ad8 | 2015-03-25 13:16:53 +0000 | [diff] [blame] | 699 | if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup, |
| 700 | Addrs, InSet) || |
| 701 | !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup, |
| 702 | Addrs, InSet)) |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 703 | return false; |
| 704 | |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 705 | // We only support a few operations on non-constant expressions, handle |
| 706 | // those first. |
Daniel Dunbar | 0d5fc9a | 2009-07-01 06:48:00 +0000 | [diff] [blame] | 707 | if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) { |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 708 | switch (ABE->getOpcode()) { |
| 709 | default: |
| 710 | return false; |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 711 | case MCBinaryExpr::Sub: |
Daniel Dunbar | 19f847f | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 712 | // Negate RHS and add. |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 713 | return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue, |
Daniel Dunbar | 19f847f | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 714 | RHSValue.getSymB(), RHSValue.getSymA(), |
Rafael Espindola | 59f90b2 | 2015-03-25 19:24:39 +0000 | [diff] [blame] | 715 | -RHSValue.getConstant(), Res); |
Daniel Dunbar | 19f847f | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 716 | |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 717 | case MCBinaryExpr::Add: |
Rafael Espindola | 5004f4a | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 718 | return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue, |
Daniel Dunbar | 19f847f | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 719 | RHSValue.getSymA(), RHSValue.getSymB(), |
Rafael Espindola | 59f90b2 | 2015-03-25 19:24:39 +0000 | [diff] [blame] | 720 | RHSValue.getConstant(), Res); |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 724 | // FIXME: We need target hooks for the evaluation. It may be limited in |
Ahmed Bougacha | 177c148 | 2015-04-28 00:21:32 +0000 | [diff] [blame] | 725 | // width, and gas defines the result of comparisons differently from |
| 726 | // Apple as. |
Daniel Dunbar | 3971273 | 2009-06-30 16:02:47 +0000 | [diff] [blame] | 727 | int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant(); |
| 728 | int64_t Result = 0; |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 729 | switch (ABE->getOpcode()) { |
Ahmed Bougacha | 177c148 | 2015-04-28 00:21:32 +0000 | [diff] [blame] | 730 | case MCBinaryExpr::AShr: Result = LHS >> RHS; break; |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 731 | case MCBinaryExpr::Add: Result = LHS + RHS; break; |
| 732 | case MCBinaryExpr::And: Result = LHS & RHS; break; |
| 733 | case MCBinaryExpr::Div: Result = LHS / RHS; break; |
| 734 | case MCBinaryExpr::EQ: Result = LHS == RHS; break; |
| 735 | case MCBinaryExpr::GT: Result = LHS > RHS; break; |
| 736 | case MCBinaryExpr::GTE: Result = LHS >= RHS; break; |
| 737 | case MCBinaryExpr::LAnd: Result = LHS && RHS; break; |
| 738 | case MCBinaryExpr::LOr: Result = LHS || RHS; break; |
Ahmed Bougacha | 177c148 | 2015-04-28 00:21:32 +0000 | [diff] [blame] | 739 | case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break; |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 740 | case MCBinaryExpr::LT: Result = LHS < RHS; break; |
| 741 | case MCBinaryExpr::LTE: Result = LHS <= RHS; break; |
| 742 | case MCBinaryExpr::Mod: Result = LHS % RHS; break; |
| 743 | case MCBinaryExpr::Mul: Result = LHS * RHS; break; |
| 744 | case MCBinaryExpr::NE: Result = LHS != RHS; break; |
| 745 | case MCBinaryExpr::Or: Result = LHS | RHS; break; |
| 746 | case MCBinaryExpr::Shl: Result = LHS << RHS; break; |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 747 | case MCBinaryExpr::Sub: Result = LHS - RHS; break; |
| 748 | case MCBinaryExpr::Xor: Result = LHS ^ RHS; break; |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Daniel Dunbar | bd4bf3d | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 751 | Res = MCValue::get(Result); |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 752 | return true; |
| 753 | } |
| 754 | } |
Daniel Dunbar | 84bfd7b | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 755 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 756 | llvm_unreachable("Invalid assembly expression kind!"); |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 757 | } |
Daniel Dunbar | dc3e4cc | 2011-04-29 18:00:03 +0000 | [diff] [blame] | 758 | |
| 759 | const MCSection *MCExpr::FindAssociatedSection() const { |
| 760 | switch (getKind()) { |
| 761 | case Target: |
| 762 | // We never look through target specific expressions. |
| 763 | return cast<MCTargetExpr>(this)->FindAssociatedSection(); |
| 764 | |
| 765 | case Constant: |
| 766 | return MCSymbol::AbsolutePseudoSection; |
| 767 | |
| 768 | case SymbolRef: { |
| 769 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this); |
| 770 | const MCSymbol &Sym = SRE->getSymbol(); |
| 771 | |
| 772 | if (Sym.isDefined()) |
| 773 | return &Sym.getSection(); |
| 774 | |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 775 | return nullptr; |
Daniel Dunbar | dc3e4cc | 2011-04-29 18:00:03 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | case Unary: |
| 779 | return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection(); |
| 780 | |
| 781 | case Binary: { |
| 782 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(this); |
| 783 | const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection(); |
| 784 | const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection(); |
| 785 | |
| 786 | // If either section is absolute, return the other. |
| 787 | if (LHS_S == MCSymbol::AbsolutePseudoSection) |
| 788 | return RHS_S; |
| 789 | if (RHS_S == MCSymbol::AbsolutePseudoSection) |
| 790 | return LHS_S; |
| 791 | |
Peter Collingbourne | 10d362c | 2015-04-03 01:46:11 +0000 | [diff] [blame] | 792 | // Not always correct, but probably the best we can do without more context. |
| 793 | if (BE->getOpcode() == MCBinaryExpr::Sub) |
| 794 | return MCSymbol::AbsolutePseudoSection; |
| 795 | |
Daniel Dunbar | dc3e4cc | 2011-04-29 18:00:03 +0000 | [diff] [blame] | 796 | // Otherwise, return the first non-null section. |
| 797 | return LHS_S ? LHS_S : RHS_S; |
| 798 | } |
| 799 | } |
| 800 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 801 | llvm_unreachable("Invalid assembly expression kind!"); |
Daniel Dunbar | dc3e4cc | 2011-04-29 18:00:03 +0000 | [diff] [blame] | 802 | } |