Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1 | //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===// |
Daniel Dunbar | fc6877a | 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 | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "mcexpr" |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Statistic.h" |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | 979ba5b | 2010-03-11 05:53:37 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAsmLayout.h" |
| 15 | #include "llvm/MC/MCAssembler.h" |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCContext.h" |
Craig Topper | f1d0f77 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCObjectWriter.h" |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSymbol.h" |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCValue.h" |
David Greene | 5d2bcb1 | 2010-01-05 01:28:07 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 732f05c | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | namespace stats { |
| 27 | STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations"); |
| 28 | } |
| 29 | } |
| 30 | |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 31 | void MCExpr::print(raw_ostream &OS) const { |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 32 | switch (getKind()) { |
Chris Lattner | 5d917a8 | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 33 | case MCExpr::Target: |
| 34 | return cast<MCTargetExpr>(this)->PrintImpl(OS); |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 35 | case MCExpr::Constant: |
| 36 | OS << cast<MCConstantExpr>(*this).getValue(); |
| 37 | return; |
| 38 | |
Chris Lattner | d50c2b9 | 2009-09-08 23:20:50 +0000 | [diff] [blame] | 39 | case MCExpr::SymbolRef: { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 40 | const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this); |
| 41 | const MCSymbol &Sym = SRE.getSymbol(); |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 42 | // Parenthesize names that start with $ so that they don't look like |
| 43 | // absolute names. |
| 44 | bool UseParens = Sym.getName()[0] == '$'; |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 45 | if (UseParens) |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 46 | OS << '(' << Sym << ')'; |
| 47 | else |
| 48 | OS << Sym; |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 49 | |
Logan Chien | 37c7461 | 2012-12-12 07:14:46 +0000 | [diff] [blame] | 50 | if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_NONE || |
| 51 | SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT || |
Jim Grosbach | 2c4d512 | 2010-11-10 03:26:07 +0000 | [diff] [blame] | 52 | SRE.getKind() == MCSymbolRefExpr::VK_ARM_TLSGD || |
| 53 | SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOT || |
| 54 | SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTOFF || |
| 55 | SRE.getKind() == MCSymbolRefExpr::VK_ARM_TPOFF || |
James Molloy | 3498257 | 2012-01-26 09:25:43 +0000 | [diff] [blame] | 56 | SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTTPOFF || |
Anton Korobeynikov | 12cfa11 | 2012-11-09 20:20:12 +0000 | [diff] [blame] | 57 | SRE.getKind() == MCSymbolRefExpr::VK_ARM_TARGET1 || |
Logan Chien | 37c7461 | 2012-12-12 07:14:46 +0000 | [diff] [blame] | 58 | SRE.getKind() == MCSymbolRefExpr::VK_ARM_TARGET2 || |
| 59 | SRE.getKind() == MCSymbolRefExpr::VK_ARM_PREL31) |
Jim Grosbach | 637d89f | 2010-09-22 23:27:36 +0000 | [diff] [blame] | 60 | OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind()); |
Ulrich Weigand | edaa58e | 2013-05-23 22:26:41 +0000 | [diff] [blame] | 61 | else if (SRE.getKind() != MCSymbolRefExpr::VK_None) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 62 | OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind()); |
| 63 | |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 64 | return; |
Chris Lattner | d50c2b9 | 2009-09-08 23:20:50 +0000 | [diff] [blame] | 65 | } |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 66 | |
| 67 | case MCExpr::Unary: { |
| 68 | const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this); |
| 69 | switch (UE.getOpcode()) { |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 70 | case MCUnaryExpr::LNot: OS << '!'; break; |
| 71 | case MCUnaryExpr::Minus: OS << '-'; break; |
| 72 | case MCUnaryExpr::Not: OS << '~'; break; |
| 73 | case MCUnaryExpr::Plus: OS << '+'; break; |
| 74 | } |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 75 | OS << *UE.getSubExpr(); |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 76 | return; |
| 77 | } |
| 78 | |
| 79 | case MCExpr::Binary: { |
| 80 | const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this); |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 81 | |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 82 | // Only print parens around the LHS if it is non-trivial. |
| 83 | if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 84 | OS << *BE.getLHS(); |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 85 | } else { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 86 | OS << '(' << *BE.getLHS() << ')'; |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 87 | } |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 88 | |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 89 | switch (BE.getOpcode()) { |
Chris Lattner | d19ceb9 | 2009-09-08 06:37:35 +0000 | [diff] [blame] | 90 | case MCBinaryExpr::Add: |
| 91 | // Print "X-42" instead of "X+-42". |
| 92 | if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) { |
| 93 | if (RHSC->getValue() < 0) { |
| 94 | OS << RHSC->getValue(); |
| 95 | return; |
| 96 | } |
| 97 | } |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 98 | |
Chris Lattner | d19ceb9 | 2009-09-08 06:37:35 +0000 | [diff] [blame] | 99 | OS << '+'; |
| 100 | break; |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 101 | case MCBinaryExpr::And: OS << '&'; break; |
| 102 | case MCBinaryExpr::Div: OS << '/'; break; |
| 103 | case MCBinaryExpr::EQ: OS << "=="; break; |
| 104 | case MCBinaryExpr::GT: OS << '>'; break; |
| 105 | case MCBinaryExpr::GTE: OS << ">="; break; |
| 106 | case MCBinaryExpr::LAnd: OS << "&&"; break; |
| 107 | case MCBinaryExpr::LOr: OS << "||"; break; |
| 108 | case MCBinaryExpr::LT: OS << '<'; break; |
| 109 | case MCBinaryExpr::LTE: OS << "<="; break; |
| 110 | case MCBinaryExpr::Mod: OS << '%'; break; |
| 111 | case MCBinaryExpr::Mul: OS << '*'; break; |
| 112 | case MCBinaryExpr::NE: OS << "!="; break; |
| 113 | case MCBinaryExpr::Or: OS << '|'; break; |
| 114 | case MCBinaryExpr::Shl: OS << "<<"; break; |
| 115 | case MCBinaryExpr::Shr: OS << ">>"; break; |
| 116 | case MCBinaryExpr::Sub: OS << '-'; break; |
| 117 | case MCBinaryExpr::Xor: OS << '^'; break; |
| 118 | } |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 119 | |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 120 | // Only print parens around the LHS if it is non-trivial. |
| 121 | if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 122 | OS << *BE.getRHS(); |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 123 | } else { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 124 | OS << '(' << *BE.getRHS() << ')'; |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 125 | } |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 130 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Manman Ren | 286c4dc | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 133 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 134 | void MCExpr::dump() const { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 135 | print(dbgs()); |
David Greene | 5d2bcb1 | 2010-01-05 01:28:07 +0000 | [diff] [blame] | 136 | dbgs() << '\n'; |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 137 | } |
Manman Ren | cc77eec | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 138 | #endif |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 139 | |
| 140 | /* *** */ |
| 141 | |
Chris Lattner | 8b4ada2 | 2009-09-08 06:27:48 +0000 | [diff] [blame] | 142 | const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS, |
| 143 | const MCExpr *RHS, MCContext &Ctx) { |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 144 | return new (Ctx) MCBinaryExpr(Opc, LHS, RHS); |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | 8b4ada2 | 2009-09-08 06:27:48 +0000 | [diff] [blame] | 147 | const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr, |
| 148 | MCContext &Ctx) { |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 149 | return new (Ctx) MCUnaryExpr(Opc, Expr); |
| 150 | } |
| 151 | |
| 152 | const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) { |
| 153 | return new (Ctx) MCConstantExpr(Value); |
| 154 | } |
| 155 | |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 156 | /* *** */ |
| 157 | |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 158 | const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym, |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 159 | VariantKind Kind, |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 160 | MCContext &Ctx) { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 161 | return new (Ctx) MCSymbolRefExpr(Sym, Kind); |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 164 | const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind, |
| 165 | MCContext &Ctx) { |
| 166 | return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx); |
Chris Lattner | 4f3e7aa | 2009-09-16 01:26:31 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 169 | StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { |
| 170 | switch (Kind) { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 171 | case VK_Invalid: return "<<invalid>>"; |
| 172 | case VK_None: return "<<none>>"; |
| 173 | |
| 174 | case VK_GOT: return "GOT"; |
| 175 | case VK_GOTOFF: return "GOTOFF"; |
| 176 | case VK_GOTPCREL: return "GOTPCREL"; |
| 177 | case VK_GOTTPOFF: return "GOTTPOFF"; |
| 178 | case VK_INDNTPOFF: return "INDNTPOFF"; |
| 179 | case VK_NTPOFF: return "NTPOFF"; |
Rafael Espindola | a0a2f87 | 2010-10-28 14:22:44 +0000 | [diff] [blame] | 180 | case VK_GOTNTPOFF: return "GOTNTPOFF"; |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 181 | case VK_PLT: return "PLT"; |
| 182 | case VK_TLSGD: return "TLSGD"; |
Rafael Espindola | b4d1721 | 2010-10-28 15:02:40 +0000 | [diff] [blame] | 183 | case VK_TLSLD: return "TLSLD"; |
Rafael Espindola | a264f72 | 2010-10-28 14:37:09 +0000 | [diff] [blame] | 184 | case VK_TLSLDM: return "TLSLDM"; |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 185 | case VK_TPOFF: return "TPOFF"; |
Rafael Espindola | 0cf15d6 | 2010-10-28 14:48:59 +0000 | [diff] [blame] | 186 | case VK_DTPOFF: return "DTPOFF"; |
Chris Lattner | 6135a96 | 2010-11-14 22:22:59 +0000 | [diff] [blame] | 187 | case VK_TLVP: return "TLVP"; |
Benjamin Kramer | 42734cf | 2013-03-30 16:21:50 +0000 | [diff] [blame] | 188 | case VK_SECREL: return "SECREL32"; |
Logan Chien | 37c7461 | 2012-12-12 07:14:46 +0000 | [diff] [blame] | 189 | case VK_ARM_NONE: return "(NONE)"; |
Jim Grosbach | 637d89f | 2010-09-22 23:27:36 +0000 | [diff] [blame] | 190 | case VK_ARM_PLT: return "(PLT)"; |
Jim Grosbach | 2c4d512 | 2010-11-10 03:26:07 +0000 | [diff] [blame] | 191 | case VK_ARM_GOT: return "(GOT)"; |
| 192 | case VK_ARM_GOTOFF: return "(GOTOFF)"; |
| 193 | case VK_ARM_TPOFF: return "(tpoff)"; |
| 194 | case VK_ARM_GOTTPOFF: return "(gottpoff)"; |
Jim Grosbach | 4d08863 | 2010-11-17 19:30:11 +0000 | [diff] [blame] | 195 | case VK_ARM_TLSGD: return "(tlsgd)"; |
James Molloy | 3498257 | 2012-01-26 09:25:43 +0000 | [diff] [blame] | 196 | case VK_ARM_TARGET1: return "(target1)"; |
Anton Korobeynikov | 12cfa11 | 2012-11-09 20:20:12 +0000 | [diff] [blame] | 197 | case VK_ARM_TARGET2: return "(target2)"; |
Logan Chien | 37c7461 | 2012-12-12 07:14:46 +0000 | [diff] [blame] | 198 | case VK_ARM_PREL31: return "(prel31)"; |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 199 | case VK_PPC_LO: return "l"; |
Ulrich Weigand | d284957 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 200 | case VK_PPC_HI: return "h"; |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 201 | case VK_PPC_HA: return "ha"; |
Ulrich Weigand | 8465659 | 2013-06-20 22:39:42 +0000 | [diff] [blame] | 202 | case VK_PPC_TOCBASE: return "tocbase"; |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 203 | case VK_PPC_TOC: return "toc"; |
| 204 | case VK_PPC_TOC_LO: return "toc@l"; |
Ulrich Weigand | f8f87dc | 2013-06-21 14:43:10 +0000 | [diff] [blame^] | 205 | case VK_PPC_TOC_HI: return "toc@h"; |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 206 | case VK_PPC_TOC_HA: return "toc@ha"; |
| 207 | case VK_PPC_TPREL_LO: return "tprel@l"; |
| 208 | case VK_PPC_TPREL_HA: return "tprel@ha"; |
| 209 | case VK_PPC_DTPREL_LO: return "dtprel@l"; |
| 210 | case VK_PPC_DTPREL_HA: return "dtprel@ha"; |
| 211 | case VK_PPC_GOT_TPREL_LO: return "got@tprel@l"; |
| 212 | case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha"; |
Bill Schmidt | d7802bf | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 213 | case VK_PPC_TLS: return "tls"; |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 214 | case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l"; |
| 215 | case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha"; |
Bill Schmidt | 57ac1f4 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 216 | case VK_PPC_TLSGD: return "tlsgd"; |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 217 | case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l"; |
| 218 | case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha"; |
Bill Schmidt | 349c278 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 219 | case VK_PPC_TLSLD: return "tlsld"; |
Akira Hatanaka | bc24985 | 2011-12-22 01:05:17 +0000 | [diff] [blame] | 220 | case VK_Mips_GPREL: return "GPREL"; |
| 221 | case VK_Mips_GOT_CALL: return "GOT_CALL"; |
| 222 | case VK_Mips_GOT16: return "GOT16"; |
| 223 | case VK_Mips_GOT: return "GOT"; |
| 224 | case VK_Mips_ABS_HI: return "ABS_HI"; |
| 225 | case VK_Mips_ABS_LO: return "ABS_LO"; |
| 226 | case VK_Mips_TLSGD: return "TLSGD"; |
| 227 | case VK_Mips_TLSLDM: return "TLSLDM"; |
| 228 | case VK_Mips_DTPREL_HI: return "DTPREL_HI"; |
| 229 | case VK_Mips_DTPREL_LO: return "DTPREL_LO"; |
| 230 | case VK_Mips_GOTTPREL: return "GOTTPREL"; |
| 231 | case VK_Mips_TPREL_HI: return "TPREL_HI"; |
| 232 | case VK_Mips_TPREL_LO: return "TPREL_LO"; |
| 233 | case VK_Mips_GPOFF_HI: return "GPOFF_HI"; |
| 234 | case VK_Mips_GPOFF_LO: return "GPOFF_LO"; |
| 235 | case VK_Mips_GOT_DISP: return "GOT_DISP"; |
| 236 | case VK_Mips_GOT_PAGE: return "GOT_PAGE"; |
| 237 | case VK_Mips_GOT_OFST: return "GOT_OFST"; |
Akira Hatanaka | b7dd9fc | 2012-07-21 02:15:19 +0000 | [diff] [blame] | 238 | case VK_Mips_HIGHER: return "HIGHER"; |
| 239 | case VK_Mips_HIGHEST: return "HIGHEST"; |
Akira Hatanaka | e390434 | 2012-11-21 19:50:22 +0000 | [diff] [blame] | 240 | case VK_Mips_GOT_HI16: return "GOT_HI16"; |
| 241 | case VK_Mips_GOT_LO16: return "GOT_LO16"; |
| 242 | case VK_Mips_CALL_HI16: return "CALL_HI16"; |
| 243 | case VK_Mips_CALL_LO16: return "CALL_LO16"; |
Nico Rieck | 18d49ac | 2013-04-10 23:28:17 +0000 | [diff] [blame] | 244 | case VK_COFF_IMGREL32: return "IMGREL32"; |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 245 | } |
Chandler Carruth | 732f05c | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 246 | llvm_unreachable("Invalid variant kind"); |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | MCSymbolRefExpr::VariantKind |
| 250 | MCSymbolRefExpr::getVariantKindForName(StringRef Name) { |
| 251 | return StringSwitch<VariantKind>(Name) |
| 252 | .Case("GOT", VK_GOT) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 253 | .Case("got", VK_GOT) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 254 | .Case("GOTOFF", VK_GOTOFF) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 255 | .Case("gotoff", VK_GOTOFF) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 256 | .Case("GOTPCREL", VK_GOTPCREL) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 257 | .Case("gotpcrel", VK_GOTPCREL) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 258 | .Case("GOTTPOFF", VK_GOTTPOFF) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 259 | .Case("gottpoff", VK_GOTTPOFF) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 260 | .Case("INDNTPOFF", VK_INDNTPOFF) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 261 | .Case("indntpoff", VK_INDNTPOFF) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 262 | .Case("NTPOFF", VK_NTPOFF) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 263 | .Case("ntpoff", VK_NTPOFF) |
Rafael Espindola | a0a2f87 | 2010-10-28 14:22:44 +0000 | [diff] [blame] | 264 | .Case("GOTNTPOFF", VK_GOTNTPOFF) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 265 | .Case("gotntpoff", VK_GOTNTPOFF) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 266 | .Case("PLT", VK_PLT) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 267 | .Case("plt", VK_PLT) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 268 | .Case("TLSGD", VK_TLSGD) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 269 | .Case("tlsgd", VK_TLSGD) |
Rafael Espindola | b4d1721 | 2010-10-28 15:02:40 +0000 | [diff] [blame] | 270 | .Case("TLSLD", VK_TLSLD) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 271 | .Case("tlsld", VK_TLSLD) |
Rafael Espindola | a264f72 | 2010-10-28 14:37:09 +0000 | [diff] [blame] | 272 | .Case("TLSLDM", VK_TLSLDM) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 273 | .Case("tlsldm", VK_TLSLDM) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 274 | .Case("TPOFF", VK_TPOFF) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 275 | .Case("tpoff", VK_TPOFF) |
Rafael Espindola | 0cf15d6 | 2010-10-28 14:48:59 +0000 | [diff] [blame] | 276 | .Case("DTPOFF", VK_DTPOFF) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 277 | .Case("dtpoff", VK_DTPOFF) |
Eric Christopher | 96ac515 | 2010-05-26 00:02:12 +0000 | [diff] [blame] | 278 | .Case("TLVP", VK_TLVP) |
Rafael Espindola | 54104db | 2011-01-23 16:11:25 +0000 | [diff] [blame] | 279 | .Case("tlvp", VK_TLVP) |
Nico Rieck | 18d49ac | 2013-04-10 23:28:17 +0000 | [diff] [blame] | 280 | .Case("IMGREL", VK_COFF_IMGREL32) |
| 281 | .Case("imgrel", VK_COFF_IMGREL32) |
Rafael Espindola | 93d0b06 | 2013-04-25 19:27:05 +0000 | [diff] [blame] | 282 | .Case("SECREL32", VK_SECREL) |
| 283 | .Case("secrel32", VK_SECREL) |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 284 | .Case("L", VK_PPC_LO) |
| 285 | .Case("l", VK_PPC_LO) |
Ulrich Weigand | d284957 | 2013-06-21 14:42:49 +0000 | [diff] [blame] | 286 | .Case("H", VK_PPC_HI) |
| 287 | .Case("h", VK_PPC_HI) |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 288 | .Case("HA", VK_PPC_HA) |
| 289 | .Case("ha", VK_PPC_HA) |
Ulrich Weigand | 8465659 | 2013-06-20 22:39:42 +0000 | [diff] [blame] | 290 | .Case("TOCBASE", VK_PPC_TOCBASE) |
| 291 | .Case("tocbase", VK_PPC_TOCBASE) |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 292 | .Case("TOC", VK_PPC_TOC) |
| 293 | .Case("toc", VK_PPC_TOC) |
| 294 | .Case("TOC@L", VK_PPC_TOC_LO) |
| 295 | .Case("toc@l", VK_PPC_TOC_LO) |
Ulrich Weigand | f8f87dc | 2013-06-21 14:43:10 +0000 | [diff] [blame^] | 296 | .Case("TOC@H", VK_PPC_TOC_HI) |
| 297 | .Case("toc@h", VK_PPC_TOC_HI) |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 298 | .Case("TOC@HA", VK_PPC_TOC_HA) |
| 299 | .Case("toc@ha", VK_PPC_TOC_HA) |
Ulrich Weigand | a7e5e6b | 2013-05-03 19:52:35 +0000 | [diff] [blame] | 300 | .Case("TLS", VK_PPC_TLS) |
| 301 | .Case("tls", VK_PPC_TLS) |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 302 | .Case("TPREL@L", VK_PPC_TPREL_LO) |
| 303 | .Case("tprel@l", VK_PPC_TPREL_LO) |
| 304 | .Case("TPREL@HA", VK_PPC_TPREL_HA) |
| 305 | .Case("tprel@ha", VK_PPC_TPREL_HA) |
| 306 | .Case("DTPREL@L", VK_PPC_DTPREL_LO) |
| 307 | .Case("dtprel@l", VK_PPC_DTPREL_LO) |
| 308 | .Case("DTPREL@HA", VK_PPC_DTPREL_HA) |
| 309 | .Case("dtprel@ha", VK_PPC_DTPREL_HA) |
| 310 | .Case("GOT@TPREL@L", VK_PPC_GOT_TPREL_LO) |
| 311 | .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO) |
| 312 | .Case("GOT@TPREL@HA", VK_PPC_GOT_TPREL_HA) |
| 313 | .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA) |
| 314 | .Case("GOT@TLSGD@L", VK_PPC_GOT_TLSGD_LO) |
| 315 | .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO) |
| 316 | .Case("GOT@TLSGD@HA", VK_PPC_GOT_TLSGD_HA) |
| 317 | .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA) |
| 318 | .Case("GOT@TLSLD@L", VK_PPC_GOT_TLSLD_LO) |
| 319 | .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO) |
| 320 | .Case("GOT@TLSLD@HA", VK_PPC_GOT_TLSLD_HA) |
| 321 | .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA) |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 322 | .Default(VK_Invalid); |
| 323 | } |
| 324 | |
| 325 | /* *** */ |
| 326 | |
Craig Topper | 4bb51cc | 2012-09-26 06:36:36 +0000 | [diff] [blame] | 327 | void MCTargetExpr::anchor() {} |
Chris Lattner | 4f3e7aa | 2009-09-16 01:26:31 +0000 | [diff] [blame] | 328 | |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 329 | /* *** */ |
| 330 | |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 331 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const { |
| 332 | return EvaluateAsAbsolute(Res, 0, 0, 0); |
| 333 | } |
| 334 | |
| 335 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res, |
| 336 | const MCAsmLayout &Layout) const { |
| 337 | return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0); |
| 338 | } |
| 339 | |
| 340 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res, |
| 341 | const MCAsmLayout &Layout, |
| 342 | const SectionAddrMap &Addrs) const { |
| 343 | return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs); |
| 344 | } |
| 345 | |
| 346 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const { |
| 347 | return EvaluateAsAbsolute(Res, &Asm, 0, 0); |
| 348 | } |
| 349 | |
| 350 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, |
| 351 | const MCAsmLayout *Layout, |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 352 | const SectionAddrMap *Addrs) const { |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 353 | MCValue Value; |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 354 | |
Daniel Dunbar | ef6e96f | 2010-03-23 23:47:07 +0000 | [diff] [blame] | 355 | // Fast path constants. |
| 356 | if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) { |
| 357 | Res = CE->getValue(); |
| 358 | return true; |
| 359 | } |
| 360 | |
Rafael Espindola | 3f037ef | 2010-12-18 04:19:20 +0000 | [diff] [blame] | 361 | // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us |
| 362 | // absolutize differences across sections and that is what the MachO writer |
| 363 | // uses Addrs for. |
Rafael Espindola | 10b6d33 | 2010-12-22 22:16:24 +0000 | [diff] [blame] | 364 | bool IsRelocatable = |
| 365 | EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 366 | |
Rafael Espindola | 10b6d33 | 2010-12-22 22:16:24 +0000 | [diff] [blame] | 367 | // Record the current value. |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 368 | Res = Value.getConstant(); |
Rafael Espindola | 10b6d33 | 2010-12-22 22:16:24 +0000 | [diff] [blame] | 369 | |
| 370 | return IsRelocatable && Value.isAbsolute(); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 373 | /// \brief Helper method for \see EvaluateSymbolAdd(). |
Rafael Espindola | c5b0e44 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 374 | static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm, |
| 375 | const MCAsmLayout *Layout, |
| 376 | const SectionAddrMap *Addrs, |
| 377 | bool InSet, |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 378 | const MCSymbolRefExpr *&A, |
| 379 | const MCSymbolRefExpr *&B, |
| 380 | int64_t &Addend) { |
Rafael Espindola | c5b0e44 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 381 | if (!A || !B) |
| 382 | return; |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 383 | |
Rafael Espindola | c5b0e44 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 384 | const MCSymbol &SA = A->getSymbol(); |
| 385 | const MCSymbol &SB = B->getSymbol(); |
| 386 | |
| 387 | if (SA.isUndefined() || SB.isUndefined()) |
| 388 | return; |
| 389 | |
| 390 | if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet)) |
| 391 | return; |
| 392 | |
| 393 | MCSymbolData &AD = Asm->getSymbolData(SA); |
| 394 | MCSymbolData &BD = Asm->getSymbolData(SB); |
| 395 | |
| 396 | if (AD.getFragment() == BD.getFragment()) { |
| 397 | Addend += (AD.getOffset() - BD.getOffset()); |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 398 | |
Owen Anderson | ce3a1ba | 2011-03-21 23:13:43 +0000 | [diff] [blame] | 399 | // Pointers to Thumb symbols need to have their low-bit set to allow |
| 400 | // for interworking. |
| 401 | if (Asm->isThumbFunc(&SA)) |
| 402 | Addend |= 1; |
| 403 | |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 404 | // Clear the symbol expr pointers to indicate we have folded these |
| 405 | // operands. |
| 406 | A = B = 0; |
Rafael Espindola | c5b0e44 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 407 | return; |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 408 | } |
Rafael Espindola | c5b0e44 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 409 | |
| 410 | if (!Layout) |
| 411 | return; |
| 412 | |
| 413 | const MCSectionData &SecA = *AD.getFragment()->getParent(); |
| 414 | const MCSectionData &SecB = *BD.getFragment()->getParent(); |
| 415 | |
| 416 | if ((&SecA != &SecB) && !Addrs) |
| 417 | return; |
| 418 | |
| 419 | // Eagerly evaluate. |
| 420 | Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) - |
| 421 | Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol()))); |
| 422 | if (Addrs && (&SecA != &SecB)) |
| 423 | Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB)); |
| 424 | |
Jim Grosbach | 0db1241 | 2012-02-24 05:12:18 +0000 | [diff] [blame] | 425 | // Pointers to Thumb symbols need to have their low-bit set to allow |
| 426 | // for interworking. |
| 427 | if (Asm->isThumbFunc(&SA)) |
| 428 | Addend |= 1; |
| 429 | |
Rafael Espindola | c5b0e44 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 430 | // Clear the symbol expr pointers to indicate we have folded these |
| 431 | // operands. |
| 432 | A = B = 0; |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Daniel Dunbar | dd18e28 | 2010-12-16 18:36:25 +0000 | [diff] [blame] | 435 | /// \brief Evaluate the result of an add between (conceptually) two MCValues. |
| 436 | /// |
| 437 | /// This routine conceptually attempts to construct an MCValue: |
| 438 | /// Result = (Result_A - Result_B + Result_Cst) |
| 439 | /// from two MCValue's LHS and RHS where |
| 440 | /// Result = LHS + RHS |
| 441 | /// and |
| 442 | /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst). |
| 443 | /// |
| 444 | /// This routine attempts to aggresively fold the operands such that the result |
| 445 | /// is representable in an MCValue, but may not always succeed. |
| 446 | /// |
| 447 | /// \returns True on success, false if the result is not representable in an |
| 448 | /// MCValue. |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 449 | |
| 450 | /// NOTE: It is really important to have both the Asm and Layout arguments. |
| 451 | /// They might look redundant, but this function can be used before layout |
| 452 | /// is done (see the object streamer for example) and having the Asm argument |
Rafael Espindola | f705a7e | 2010-12-18 04:01:45 +0000 | [diff] [blame] | 453 | /// lets us avoid relaxations early. |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 454 | static bool EvaluateSymbolicAdd(const MCAssembler *Asm, |
| 455 | const MCAsmLayout *Layout, |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 456 | const SectionAddrMap *Addrs, |
Rafael Espindola | 32a006e | 2010-12-03 00:55:40 +0000 | [diff] [blame] | 457 | bool InSet, |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 458 | const MCValue &LHS,const MCSymbolRefExpr *RHS_A, |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 459 | const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst, |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 460 | MCValue &Res) { |
Daniel Dunbar | f2ed62d | 2010-12-17 01:07:20 +0000 | [diff] [blame] | 461 | // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy |
| 462 | // about dealing with modifiers. This will ultimately bite us, one day. |
| 463 | const MCSymbolRefExpr *LHS_A = LHS.getSymA(); |
| 464 | const MCSymbolRefExpr *LHS_B = LHS.getSymB(); |
| 465 | int64_t LHS_Cst = LHS.getConstant(); |
| 466 | |
| 467 | // Fold the result constant immediately. |
| 468 | int64_t Result_Cst = LHS_Cst + RHS_Cst; |
| 469 | |
Rafael Espindola | c5b0e44 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 470 | assert((!Layout || Asm) && |
| 471 | "Must have an assembler object if layout is given!"); |
| 472 | |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 473 | // If we have a layout, we can fold resolved differences. |
Rafael Espindola | c5b0e44 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 474 | if (Asm) { |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 475 | // First, fold out any differences which are fully resolved. By |
| 476 | // reassociating terms in |
| 477 | // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst). |
| 478 | // we have the four possible differences: |
| 479 | // (LHS_A - LHS_B), |
| 480 | // (LHS_A - RHS_B), |
| 481 | // (RHS_A - LHS_B), |
| 482 | // (RHS_A - RHS_B). |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 483 | // Since we are attempting to be as aggressive as possible about folding, we |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 484 | // attempt to evaluate each possible alternative. |
Rafael Espindola | c5b0e44 | 2010-12-19 04:18:56 +0000 | [diff] [blame] | 485 | AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B, |
| 486 | Result_Cst); |
| 487 | AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B, |
| 488 | Result_Cst); |
| 489 | AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B, |
| 490 | Result_Cst); |
| 491 | AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B, |
| 492 | Result_Cst); |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Daniel Dunbar | f2ed62d | 2010-12-17 01:07:20 +0000 | [diff] [blame] | 495 | // We can't represent the addition or subtraction of two symbols. |
| 496 | if ((LHS_A && RHS_A) || (LHS_B && RHS_B)) |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 497 | return false; |
| 498 | |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 499 | // At this point, we have at most one additive symbol and one subtractive |
| 500 | // symbol -- find them. |
Daniel Dunbar | f2ed62d | 2010-12-17 01:07:20 +0000 | [diff] [blame] | 501 | const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A; |
| 502 | const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B; |
Daniel Dunbar | 17304b3 | 2010-12-17 05:50:33 +0000 | [diff] [blame] | 503 | |
| 504 | // If we have a negated symbol, then we must have also have a non-negated |
| 505 | // symbol in order to encode the expression. |
| 506 | if (B && !A) |
| 507 | return false; |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 508 | |
Daniel Dunbar | f2ed62d | 2010-12-17 01:07:20 +0000 | [diff] [blame] | 509 | Res = MCValue::get(A, B, Result_Cst); |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 510 | return true; |
| 511 | } |
| 512 | |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 513 | bool MCExpr::EvaluateAsRelocatable(MCValue &Res, |
Rafael Espindola | 33a38a1 | 2010-12-22 16:11:57 +0000 | [diff] [blame] | 514 | const MCAsmLayout &Layout) const { |
| 515 | return EvaluateAsRelocatableImpl(Res, &Layout.getAssembler(), &Layout, |
| 516 | 0, false); |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 519 | bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 520 | const MCAssembler *Asm, |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 521 | const MCAsmLayout *Layout, |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 522 | const SectionAddrMap *Addrs, |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 523 | bool InSet) const { |
Daniel Dunbar | ff54784 | 2010-03-23 23:47:14 +0000 | [diff] [blame] | 524 | ++stats::MCExprEvaluate; |
| 525 | |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 526 | switch (getKind()) { |
Chris Lattner | 5d917a8 | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 527 | case Target: |
Daniel Dunbar | f82f449 | 2010-03-11 02:28:59 +0000 | [diff] [blame] | 528 | return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout); |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 529 | |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 530 | case Constant: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 531 | Res = MCValue::get(cast<MCConstantExpr>(this)->getValue()); |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 532 | return true; |
| 533 | |
| 534 | case SymbolRef: { |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 535 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this); |
| 536 | const MCSymbol &Sym = SRE->getSymbol(); |
Daniel Dunbar | 7c3600d | 2009-10-16 01:33:57 +0000 | [diff] [blame] | 537 | |
| 538 | // Evaluate recursively if this is a variable. |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 539 | if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) { |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 540 | bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm, |
| 541 | Layout, |
| 542 | Addrs, |
| 543 | true); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 544 | // If we failed to simplify this to a constant, let the target |
| 545 | // handle it. |
| 546 | if (Ret && !Res.getSymA() && !Res.getSymB()) |
| 547 | return true; |
| 548 | } |
Daniel Dunbar | 7c3600d | 2009-10-16 01:33:57 +0000 | [diff] [blame] | 549 | |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 550 | Res = MCValue::get(SRE, 0, 0); |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 551 | return true; |
| 552 | } |
| 553 | |
| 554 | case Unary: { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 555 | const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 556 | MCValue Value; |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 557 | |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 558 | if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout, |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 559 | Addrs, InSet)) |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 560 | return false; |
| 561 | |
| 562 | switch (AUE->getOpcode()) { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 563 | case MCUnaryExpr::LNot: |
Daniel Dunbar | 80f62d0 | 2009-07-01 06:48:00 +0000 | [diff] [blame] | 564 | if (!Value.isAbsolute()) |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 565 | return false; |
| 566 | Res = MCValue::get(!Value.getConstant()); |
| 567 | break; |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 568 | case MCUnaryExpr::Minus: |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 569 | /// -(a - b + const) ==> (b - a - const) |
Daniel Dunbar | b27a41b | 2009-08-11 17:47:52 +0000 | [diff] [blame] | 570 | if (Value.getSymA() && !Value.getSymB()) |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 571 | return false; |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 572 | Res = MCValue::get(Value.getSymB(), Value.getSymA(), |
| 573 | -Value.getConstant()); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 574 | break; |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 575 | case MCUnaryExpr::Not: |
Daniel Dunbar | 80f62d0 | 2009-07-01 06:48:00 +0000 | [diff] [blame] | 576 | if (!Value.isAbsolute()) |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 577 | return false; |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 578 | Res = MCValue::get(~Value.getConstant()); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 579 | break; |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 580 | case MCUnaryExpr::Plus: |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 581 | Res = Value; |
| 582 | break; |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | case Binary: { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 589 | const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 590 | MCValue LHSValue, RHSValue; |
Daniel Dunbar | 9a1d200 | 2010-03-18 00:59:10 +0000 | [diff] [blame] | 591 | |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 592 | if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout, |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 593 | Addrs, InSet) || |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 594 | !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout, |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 595 | Addrs, InSet)) |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 596 | return false; |
| 597 | |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 598 | // We only support a few operations on non-constant expressions, handle |
| 599 | // those first. |
Daniel Dunbar | 80f62d0 | 2009-07-01 06:48:00 +0000 | [diff] [blame] | 600 | if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) { |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 601 | switch (ABE->getOpcode()) { |
| 602 | default: |
| 603 | return false; |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 604 | case MCBinaryExpr::Sub: |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 605 | // Negate RHS and add. |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 606 | return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue, |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 607 | RHSValue.getSymB(), RHSValue.getSymA(), |
| 608 | -RHSValue.getConstant(), |
| 609 | Res); |
| 610 | |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 611 | case MCBinaryExpr::Add: |
Rafael Espindola | d076482 | 2010-12-18 03:57:21 +0000 | [diff] [blame] | 612 | return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue, |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 613 | RHSValue.getSymA(), RHSValue.getSymB(), |
| 614 | RHSValue.getConstant(), |
| 615 | Res); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 619 | // FIXME: We need target hooks for the evaluation. It may be limited in |
Dan Gohman | 036c130 | 2010-02-08 23:58:47 +0000 | [diff] [blame] | 620 | // width, and gas defines the result of comparisons and right shifts |
| 621 | // differently from Apple as. |
Daniel Dunbar | b79742c | 2009-06-30 16:02:47 +0000 | [diff] [blame] | 622 | int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant(); |
| 623 | int64_t Result = 0; |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 624 | switch (ABE->getOpcode()) { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 625 | case MCBinaryExpr::Add: Result = LHS + RHS; break; |
| 626 | case MCBinaryExpr::And: Result = LHS & RHS; break; |
| 627 | case MCBinaryExpr::Div: Result = LHS / RHS; break; |
| 628 | case MCBinaryExpr::EQ: Result = LHS == RHS; break; |
| 629 | case MCBinaryExpr::GT: Result = LHS > RHS; break; |
| 630 | case MCBinaryExpr::GTE: Result = LHS >= RHS; break; |
| 631 | case MCBinaryExpr::LAnd: Result = LHS && RHS; break; |
| 632 | case MCBinaryExpr::LOr: Result = LHS || RHS; break; |
| 633 | case MCBinaryExpr::LT: Result = LHS < RHS; break; |
| 634 | case MCBinaryExpr::LTE: Result = LHS <= RHS; break; |
| 635 | case MCBinaryExpr::Mod: Result = LHS % RHS; break; |
| 636 | case MCBinaryExpr::Mul: Result = LHS * RHS; break; |
| 637 | case MCBinaryExpr::NE: Result = LHS != RHS; break; |
| 638 | case MCBinaryExpr::Or: Result = LHS | RHS; break; |
| 639 | case MCBinaryExpr::Shl: Result = LHS << RHS; break; |
| 640 | case MCBinaryExpr::Shr: Result = LHS >> RHS; break; |
| 641 | case MCBinaryExpr::Sub: Result = LHS - RHS; break; |
| 642 | case MCBinaryExpr::Xor: Result = LHS ^ RHS; break; |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 645 | Res = MCValue::get(Result); |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 646 | return true; |
| 647 | } |
| 648 | } |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 649 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 650 | llvm_unreachable("Invalid assembly expression kind!"); |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 651 | } |
Daniel Dunbar | 0eab5c4 | 2011-04-29 18:00:03 +0000 | [diff] [blame] | 652 | |
| 653 | const MCSection *MCExpr::FindAssociatedSection() const { |
| 654 | switch (getKind()) { |
| 655 | case Target: |
| 656 | // We never look through target specific expressions. |
| 657 | return cast<MCTargetExpr>(this)->FindAssociatedSection(); |
| 658 | |
| 659 | case Constant: |
| 660 | return MCSymbol::AbsolutePseudoSection; |
| 661 | |
| 662 | case SymbolRef: { |
| 663 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this); |
| 664 | const MCSymbol &Sym = SRE->getSymbol(); |
| 665 | |
| 666 | if (Sym.isDefined()) |
| 667 | return &Sym.getSection(); |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | case Unary: |
| 673 | return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection(); |
| 674 | |
| 675 | case Binary: { |
| 676 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(this); |
| 677 | const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection(); |
| 678 | const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection(); |
| 679 | |
| 680 | // If either section is absolute, return the other. |
| 681 | if (LHS_S == MCSymbol::AbsolutePseudoSection) |
| 682 | return RHS_S; |
| 683 | if (RHS_S == MCSymbol::AbsolutePseudoSection) |
| 684 | return LHS_S; |
| 685 | |
| 686 | // Otherwise, return the first non-null section. |
| 687 | return LHS_S ? LHS_S : RHS_S; |
| 688 | } |
| 689 | } |
| 690 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 691 | llvm_unreachable("Invalid assembly expression kind!"); |
Daniel Dunbar | 0eab5c4 | 2011-04-29 18:00:03 +0000 | [diff] [blame] | 692 | } |