LLVM  7.0.0svn
MCExpr.cpp
Go to the documentation of this file.
1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
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 
10 #include "llvm/MC/MCExpr.h"
11 #include "llvm/ADT/Statistic.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
25 #include <cassert>
26 #include <cstdint>
27 
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "mcexpr"
31 
32 namespace {
33 namespace stats {
34 
35 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
36 
37 } // end namespace stats
38 } // end anonymous namespace
39 
40 void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens) const {
41  switch (getKind()) {
42  case MCExpr::Target:
43  return cast<MCTargetExpr>(this)->printImpl(OS, MAI);
44  case MCExpr::Constant:
45  OS << cast<MCConstantExpr>(*this).getValue();
46  return;
47 
48  case MCExpr::SymbolRef: {
49  const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
50  const MCSymbol &Sym = SRE.getSymbol();
51  // Parenthesize names that start with $ so that they don't look like
52  // absolute names.
53  bool UseParens =
54  !InParens && !Sym.getName().empty() && Sym.getName()[0] == '$';
55  if (UseParens) {
56  OS << '(';
57  Sym.print(OS, MAI);
58  OS << ')';
59  } else
60  Sym.print(OS, MAI);
61 
62  if (SRE.getKind() != MCSymbolRefExpr::VK_None)
63  SRE.printVariantKind(OS);
64 
65  return;
66  }
67 
68  case MCExpr::Unary: {
69  const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
70  switch (UE.getOpcode()) {
71  case MCUnaryExpr::LNot: OS << '!'; break;
72  case MCUnaryExpr::Minus: OS << '-'; break;
73  case MCUnaryExpr::Not: OS << '~'; break;
74  case MCUnaryExpr::Plus: OS << '+'; break;
75  }
76  UE.getSubExpr()->print(OS, MAI);
77  return;
78  }
79 
80  case MCExpr::Binary: {
81  const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
82 
83  // Only print parens around the LHS if it is non-trivial.
84  if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
85  BE.getLHS()->print(OS, MAI);
86  } else {
87  OS << '(';
88  BE.getLHS()->print(OS, MAI);
89  OS << ')';
90  }
91 
92  switch (BE.getOpcode()) {
93  case MCBinaryExpr::Add:
94  // Print "X-42" instead of "X+-42".
95  if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
96  if (RHSC->getValue() < 0) {
97  OS << RHSC->getValue();
98  return;
99  }
100  }
101 
102  OS << '+';
103  break;
104  case MCBinaryExpr::AShr: OS << ">>"; break;
105  case MCBinaryExpr::And: OS << '&'; break;
106  case MCBinaryExpr::Div: OS << '/'; break;
107  case MCBinaryExpr::EQ: OS << "=="; break;
108  case MCBinaryExpr::GT: OS << '>'; break;
109  case MCBinaryExpr::GTE: OS << ">="; break;
110  case MCBinaryExpr::LAnd: OS << "&&"; break;
111  case MCBinaryExpr::LOr: OS << "||"; break;
112  case MCBinaryExpr::LShr: OS << ">>"; break;
113  case MCBinaryExpr::LT: OS << '<'; break;
114  case MCBinaryExpr::LTE: OS << "<="; break;
115  case MCBinaryExpr::Mod: OS << '%'; break;
116  case MCBinaryExpr::Mul: OS << '*'; break;
117  case MCBinaryExpr::NE: OS << "!="; break;
118  case MCBinaryExpr::Or: OS << '|'; break;
119  case MCBinaryExpr::Shl: OS << "<<"; break;
120  case MCBinaryExpr::Sub: OS << '-'; break;
121  case MCBinaryExpr::Xor: OS << '^'; break;
122  }
123 
124  // Only print parens around the LHS if it is non-trivial.
125  if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
126  BE.getRHS()->print(OS, MAI);
127  } else {
128  OS << '(';
129  BE.getRHS()->print(OS, MAI);
130  OS << ')';
131  }
132  return;
133  }
134  }
135 
136  llvm_unreachable("Invalid expression kind!");
137 }
138 
139 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
140 LLVM_DUMP_METHOD void MCExpr::dump() const {
141  dbgs() << *this;
142  dbgs() << '\n';
143 }
144 #endif
145 
146 /* *** */
147 
148 const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
149  const MCExpr *RHS, MCContext &Ctx,
150  SMLoc Loc) {
151  return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc);
152 }
153 
154 const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
155  MCContext &Ctx, SMLoc Loc) {
156  return new (Ctx) MCUnaryExpr(Opc, Expr, Loc);
157 }
158 
159 const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx) {
160  return new (Ctx) MCConstantExpr(Value);
161 }
162 
163 /* *** */
164 
165 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
166  const MCAsmInfo *MAI, SMLoc Loc)
167  : MCExpr(MCExpr::SymbolRef, Loc), Kind(Kind),
168  UseParensForSymbolVariant(MAI->useParensForSymbolVariant()),
169  HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()),
170  Symbol(Symbol) {
171  assert(Symbol);
172 }
173 
174 const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
175  VariantKind Kind,
176  MCContext &Ctx, SMLoc Loc) {
177  return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc);
178 }
179 
180 const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
181  MCContext &Ctx) {
182  return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
183 }
184 
185 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
186  switch (Kind) {
187  case VK_Invalid: return "<<invalid>>";
188  case VK_None: return "<<none>>";
189 
190  case VK_DTPOFF: return "DTPOFF";
191  case VK_DTPREL: return "DTPREL";
192  case VK_GOT: return "GOT";
193  case VK_GOTOFF: return "GOTOFF";
194  case VK_GOTREL: return "GOTREL";
195  case VK_GOTPCREL: return "GOTPCREL";
196  case VK_GOTTPOFF: return "GOTTPOFF";
197  case VK_INDNTPOFF: return "INDNTPOFF";
198  case VK_NTPOFF: return "NTPOFF";
199  case VK_GOTNTPOFF: return "GOTNTPOFF";
200  case VK_PLT: return "PLT";
201  case VK_TLSGD: return "TLSGD";
202  case VK_TLSLD: return "TLSLD";
203  case VK_TLSLDM: return "TLSLDM";
204  case VK_TPOFF: return "TPOFF";
205  case VK_TPREL: return "TPREL";
206  case VK_TLSCALL: return "tlscall";
207  case VK_TLSDESC: return "tlsdesc";
208  case VK_TLVP: return "TLVP";
209  case VK_TLVPPAGE: return "TLVPPAGE";
210  case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
211  case VK_PAGE: return "PAGE";
212  case VK_PAGEOFF: return "PAGEOFF";
213  case VK_GOTPAGE: return "GOTPAGE";
214  case VK_GOTPAGEOFF: return "GOTPAGEOFF";
215  case VK_SECREL: return "SECREL32";
216  case VK_SIZE: return "SIZE";
217  case VK_WEAKREF: return "WEAKREF";
218  case VK_X86_ABS8: return "ABS8";
219  case VK_ARM_NONE: return "none";
220  case VK_ARM_GOT_PREL: return "GOT_PREL";
221  case VK_ARM_TARGET1: return "target1";
222  case VK_ARM_TARGET2: return "target2";
223  case VK_ARM_PREL31: return "prel31";
224  case VK_ARM_SBREL: return "sbrel";
225  case VK_ARM_TLSLDO: return "tlsldo";
226  case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
227  case VK_AVR_NONE: return "none";
228  case VK_AVR_LO8: return "lo8";
229  case VK_AVR_HI8: return "hi8";
230  case VK_AVR_HLO8: return "hlo8";
231  case VK_AVR_DIFF8: return "diff8";
232  case VK_AVR_DIFF16: return "diff16";
233  case VK_AVR_DIFF32: return "diff32";
234  case VK_PPC_LO: return "l";
235  case VK_PPC_HI: return "h";
236  case VK_PPC_HA: return "ha";
237  case VK_PPC_HIGHER: return "higher";
238  case VK_PPC_HIGHERA: return "highera";
239  case VK_PPC_HIGHEST: return "highest";
240  case VK_PPC_HIGHESTA: return "highesta";
241  case VK_PPC_GOT_LO: return "got@l";
242  case VK_PPC_GOT_HI: return "got@h";
243  case VK_PPC_GOT_HA: return "got@ha";
244  case VK_PPC_TOCBASE: return "tocbase";
245  case VK_PPC_TOC: return "toc";
246  case VK_PPC_TOC_LO: return "toc@l";
247  case VK_PPC_TOC_HI: return "toc@h";
248  case VK_PPC_TOC_HA: return "toc@ha";
249  case VK_PPC_DTPMOD: return "dtpmod";
250  case VK_PPC_TPREL_LO: return "tprel@l";
251  case VK_PPC_TPREL_HI: return "tprel@h";
252  case VK_PPC_TPREL_HA: return "tprel@ha";
253  case VK_PPC_TPREL_HIGHER: return "tprel@higher";
254  case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
255  case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
256  case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
257  case VK_PPC_DTPREL_LO: return "dtprel@l";
258  case VK_PPC_DTPREL_HI: return "dtprel@h";
259  case VK_PPC_DTPREL_HA: return "dtprel@ha";
260  case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
261  case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
262  case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
263  case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
264  case VK_PPC_GOT_TPREL: return "got@tprel";
265  case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
266  case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
267  case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
268  case VK_PPC_GOT_DTPREL: return "got@dtprel";
269  case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
270  case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
271  case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
272  case VK_PPC_TLS: return "tls";
273  case VK_PPC_GOT_TLSGD: return "got@tlsgd";
274  case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
275  case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
276  case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
277  case VK_PPC_TLSGD: return "tlsgd";
278  case VK_PPC_GOT_TLSLD: return "got@tlsld";
279  case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
280  case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
281  case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
282  case VK_PPC_TLSLD: return "tlsld";
283  case VK_PPC_LOCAL: return "local";
284  case VK_COFF_IMGREL32: return "IMGREL";
285  case VK_Hexagon_PCREL: return "PCREL";
286  case VK_Hexagon_LO16: return "LO16";
287  case VK_Hexagon_HI16: return "HI16";
288  case VK_Hexagon_GPREL: return "GPREL";
289  case VK_Hexagon_GD_GOT: return "GDGOT";
290  case VK_Hexagon_LD_GOT: return "LDGOT";
291  case VK_Hexagon_GD_PLT: return "GDPLT";
292  case VK_Hexagon_LD_PLT: return "LDPLT";
293  case VK_Hexagon_IE: return "IE";
294  case VK_Hexagon_IE_GOT: return "IEGOT";
295  case VK_WebAssembly_FUNCTION: return "FUNCTION";
296  case VK_WebAssembly_TYPEINDEX: return "TYPEINDEX";
297  case VK_AMDGPU_GOTPCREL32_LO: return "gotpcrel32@lo";
298  case VK_AMDGPU_GOTPCREL32_HI: return "gotpcrel32@hi";
299  case VK_AMDGPU_REL32_LO: return "rel32@lo";
300  case VK_AMDGPU_REL32_HI: return "rel32@hi";
301  }
302  llvm_unreachable("Invalid variant kind");
303 }
304 
305 MCSymbolRefExpr::VariantKind
306 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
307  return StringSwitch<VariantKind>(Name.lower())
308  .Case("dtprel", VK_DTPREL)
309  .Case("dtpoff", VK_DTPOFF)
310  .Case("got", VK_GOT)
311  .Case("gotoff", VK_GOTOFF)
312  .Case("gotrel", VK_GOTREL)
313  .Case("gotpcrel", VK_GOTPCREL)
314  .Case("gottpoff", VK_GOTTPOFF)
315  .Case("indntpoff", VK_INDNTPOFF)
316  .Case("ntpoff", VK_NTPOFF)
317  .Case("gotntpoff", VK_GOTNTPOFF)
318  .Case("plt", VK_PLT)
319  .Case("tlscall", VK_TLSCALL)
320  .Case("tlsdesc", VK_TLSDESC)
321  .Case("tlsgd", VK_TLSGD)
322  .Case("tlsld", VK_TLSLD)
323  .Case("tlsldm", VK_TLSLDM)
324  .Case("tpoff", VK_TPOFF)
325  .Case("tprel", VK_TPREL)
326  .Case("tlvp", VK_TLVP)
327  .Case("tlvppage", VK_TLVPPAGE)
328  .Case("tlvppageoff", VK_TLVPPAGEOFF)
329  .Case("page", VK_PAGE)
330  .Case("pageoff", VK_PAGEOFF)
331  .Case("gotpage", VK_GOTPAGE)
332  .Case("gotpageoff", VK_GOTPAGEOFF)
333  .Case("imgrel", VK_COFF_IMGREL32)
334  .Case("secrel32", VK_SECREL)
335  .Case("size", VK_SIZE)
336  .Case("abs8", VK_X86_ABS8)
337  .Case("l", VK_PPC_LO)
338  .Case("h", VK_PPC_HI)
339  .Case("ha", VK_PPC_HA)
340  .Case("higher", VK_PPC_HIGHER)
341  .Case("highera", VK_PPC_HIGHERA)
342  .Case("highest", VK_PPC_HIGHEST)
343  .Case("highesta", VK_PPC_HIGHESTA)
344  .Case("got@l", VK_PPC_GOT_LO)
345  .Case("got@h", VK_PPC_GOT_HI)
346  .Case("got@ha", VK_PPC_GOT_HA)
347  .Case("local", VK_PPC_LOCAL)
348  .Case("tocbase", VK_PPC_TOCBASE)
349  .Case("toc", VK_PPC_TOC)
350  .Case("toc@l", VK_PPC_TOC_LO)
351  .Case("toc@h", VK_PPC_TOC_HI)
352  .Case("toc@ha", VK_PPC_TOC_HA)
353  .Case("tls", VK_PPC_TLS)
354  .Case("dtpmod", VK_PPC_DTPMOD)
355  .Case("tprel@l", VK_PPC_TPREL_LO)
356  .Case("tprel@h", VK_PPC_TPREL_HI)
357  .Case("tprel@ha", VK_PPC_TPREL_HA)
358  .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
359  .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
360  .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
361  .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
362  .Case("dtprel@l", VK_PPC_DTPREL_LO)
363  .Case("dtprel@h", VK_PPC_DTPREL_HI)
364  .Case("dtprel@ha", VK_PPC_DTPREL_HA)
365  .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
366  .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
367  .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
368  .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
369  .Case("got@tprel", VK_PPC_GOT_TPREL)
370  .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
371  .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
372  .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
373  .Case("got@dtprel", VK_PPC_GOT_DTPREL)
374  .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
375  .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
376  .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
377  .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
378  .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
379  .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
380  .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
381  .Case("got@tlsld", VK_PPC_GOT_TLSLD)
382  .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
383  .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
384  .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
385  .Case("gdgot", VK_Hexagon_GD_GOT)
386  .Case("gdplt", VK_Hexagon_GD_PLT)
387  .Case("iegot", VK_Hexagon_IE_GOT)
388  .Case("ie", VK_Hexagon_IE)
389  .Case("ldgot", VK_Hexagon_LD_GOT)
390  .Case("ldplt", VK_Hexagon_LD_PLT)
391  .Case("pcrel", VK_Hexagon_PCREL)
392  .Case("none", VK_ARM_NONE)
393  .Case("got_prel", VK_ARM_GOT_PREL)
394  .Case("target1", VK_ARM_TARGET1)
395  .Case("target2", VK_ARM_TARGET2)
396  .Case("prel31", VK_ARM_PREL31)
397  .Case("sbrel", VK_ARM_SBREL)
398  .Case("tlsldo", VK_ARM_TLSLDO)
399  .Case("lo8", VK_AVR_LO8)
400  .Case("hi8", VK_AVR_HI8)
401  .Case("hlo8", VK_AVR_HLO8)
402  .Case("gotpcrel32@lo", VK_AMDGPU_GOTPCREL32_LO)
403  .Case("gotpcrel32@hi", VK_AMDGPU_GOTPCREL32_HI)
404  .Case("rel32@lo", VK_AMDGPU_REL32_LO)
405  .Case("rel32@hi", VK_AMDGPU_REL32_HI)
406  .Default(VK_Invalid);
407 }
408 
409 void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
410  if (UseParensForSymbolVariant)
411  OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
412  else
413  OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
414 }
415 
416 /* *** */
417 
418 void MCTargetExpr::anchor() {}
419 
420 /* *** */
421 
422 bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
423  return evaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
424 }
425 
426 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
427  const MCAsmLayout &Layout) const {
428  return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
429 }
430 
431 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
432  const MCAsmLayout &Layout,
433  const SectionAddrMap &Addrs) const {
434  return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
435 }
436 
437 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
438  return evaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
439 }
440 
441 bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
442  const MCAsmLayout &Layout) const {
443  return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
444  true);
445 }
446 
447 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
448  const MCAsmLayout *Layout,
449  const SectionAddrMap *Addrs) const {
450  // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
451  // absolutize differences across sections and that is what the MachO writer
452  // uses Addrs for.
453  return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
454 }
455 
456 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
457  const MCAsmLayout *Layout,
458  const SectionAddrMap *Addrs, bool InSet) const {
459  MCValue Value;
460 
461  // Fast path constants.
462  if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
463  Res = CE->getValue();
464  return true;
465  }
466 
467  bool IsRelocatable =
468  evaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
469 
470  // Record the current value.
471  Res = Value.getConstant();
472 
473  return IsRelocatable && Value.isAbsolute();
474 }
475 
476 /// \brief Helper method for \see EvaluateSymbolAdd().
477 static void AttemptToFoldSymbolOffsetDifference(
478  const MCAssembler *Asm, const MCAsmLayout *Layout,
479  const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A,
480  const MCSymbolRefExpr *&B, int64_t &Addend) {
481  if (!A || !B)
482  return;
483 
484  const MCSymbol &SA = A->getSymbol();
485  const MCSymbol &SB = B->getSymbol();
486 
487  if (SA.isUndefined() || SB.isUndefined())
488  return;
489 
490  if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
491  return;
492 
493  if (SA.getFragment() == SB.getFragment() && !SA.isVariable() &&
494  !SB.isVariable()) {
495  Addend += (SA.getOffset() - SB.getOffset());
496 
497  // Pointers to Thumb symbols need to have their low-bit set to allow
498  // for interworking.
499  if (Asm->isThumbFunc(&SA))
500  Addend |= 1;
501 
502  // Clear the symbol expr pointers to indicate we have folded these
503  // operands.
504  A = B = nullptr;
505  return;
506  }
507 
508  if (!Layout)
509  return;
510 
511  const MCSection &SecA = *SA.getFragment()->getParent();
512  const MCSection &SecB = *SB.getFragment()->getParent();
513 
514  if ((&SecA != &SecB) && !Addrs)
515  return;
516 
517  // Eagerly evaluate.
518  Addend += Layout->getSymbolOffset(A->getSymbol()) -
519  Layout->getSymbolOffset(B->getSymbol());
520  if (Addrs && (&SecA != &SecB))
521  Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
522 
523  // Pointers to Thumb symbols need to have their low-bit set to allow
524  // for interworking.
525  if (Asm->isThumbFunc(&SA))
526  Addend |= 1;
527 
528  // Clear the symbol expr pointers to indicate we have folded these
529  // operands.
530  A = B = nullptr;
531 }
532 
533 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
534 ///
535 /// This routine conceptually attempts to construct an MCValue:
536 /// Result = (Result_A - Result_B + Result_Cst)
537 /// from two MCValue's LHS and RHS where
538 /// Result = LHS + RHS
539 /// and
540 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
541 ///
542 /// This routine attempts to aggresively fold the operands such that the result
543 /// is representable in an MCValue, but may not always succeed.
544 ///
545 /// \returns True on success, false if the result is not representable in an
546 /// MCValue.
547 
548 /// NOTE: It is really important to have both the Asm and Layout arguments.
549 /// They might look redundant, but this function can be used before layout
550 /// is done (see the object streamer for example) and having the Asm argument
551 /// lets us avoid relaxations early.
552 static bool
553 EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
554  const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
555  const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
556  int64_t RHS_Cst, MCValue &Res) {
557  // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
558  // about dealing with modifiers. This will ultimately bite us, one day.
559  const MCSymbolRefExpr *LHS_A = LHS.getSymA();
560  const MCSymbolRefExpr *LHS_B = LHS.getSymB();
561  int64_t LHS_Cst = LHS.getConstant();
562 
563  // Fold the result constant immediately.
564  int64_t Result_Cst = LHS_Cst + RHS_Cst;
565 
566  assert((!Layout || Asm) &&
567  "Must have an assembler object if layout is given!");
568 
569  // If we have a layout, we can fold resolved differences.
570  if (Asm) {
571  // First, fold out any differences which are fully resolved. By
572  // reassociating terms in
573  // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
574  // we have the four possible differences:
575  // (LHS_A - LHS_B),
576  // (LHS_A - RHS_B),
577  // (RHS_A - LHS_B),
578  // (RHS_A - RHS_B).
579  // Since we are attempting to be as aggressive as possible about folding, we
580  // attempt to evaluate each possible alternative.
581  AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
582  Result_Cst);
583  AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
584  Result_Cst);
585  AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
586  Result_Cst);
587  AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
588  Result_Cst);
589  }
590 
591  // We can't represent the addition or subtraction of two symbols.
592  if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
593  return false;
594 
595  // At this point, we have at most one additive symbol and one subtractive
596  // symbol -- find them.
597  const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
598  const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
599 
600  Res = MCValue::get(A, B, Result_Cst);
601  return true;
602 }
603 
605  const MCAsmLayout *Layout,
606  const MCFixup *Fixup) const {
607  MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
608  return evaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
609  false);
610 }
611 
612 bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
613  MCAssembler *Assembler = &Layout.getAssembler();
614  return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
615  true);
616 }
617 
618 static bool canExpand(const MCSymbol &Sym, bool InSet) {
619  const MCExpr *Expr = Sym.getVariableValue();
620  const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
621  if (Inner) {
622  if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
623  return false;
624  }
625 
626  if (InSet)
627  return true;
628  return !Sym.isInSection();
629 }
630 
632  const MCAsmLayout *Layout,
633  const MCFixup *Fixup,
634  const SectionAddrMap *Addrs,
635  bool InSet) const {
636  ++stats::MCExprEvaluate;
637 
638  switch (getKind()) {
639  case Target:
640  return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
641  Fixup);
642 
643  case Constant:
644  Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
645  return true;
646 
647  case SymbolRef: {
648  const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
649  const MCSymbol &Sym = SRE->getSymbol();
650 
651  // Evaluate recursively if this is a variable.
652  if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None &&
653  canExpand(Sym, InSet)) {
654  bool IsMachO = SRE->hasSubsectionsViaSymbols();
655  if (Sym.getVariableValue()->evaluateAsRelocatableImpl(
656  Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) {
657  if (!IsMachO)
658  return true;
659 
660  const MCSymbolRefExpr *A = Res.getSymA();
661  const MCSymbolRefExpr *B = Res.getSymB();
662  // FIXME: This is small hack. Given
663  // a = b + 4
664  // .long a
665  // the OS X assembler will completely drop the 4. We should probably
666  // include it in the relocation or produce an error if that is not
667  // possible.
668  // Allow constant expressions.
669  if (!A && !B)
670  return true;
671  // Allows aliases with zero offset.
672  if (Res.getConstant() == 0 && (!A || !B))
673  return true;
674  }
675  }
676 
677  Res = MCValue::get(SRE, nullptr, 0);
678  return true;
679  }
680 
681  case Unary: {
682  const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
683  MCValue Value;
684 
685  if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
686  Addrs, InSet))
687  return false;
688 
689  switch (AUE->getOpcode()) {
690  case MCUnaryExpr::LNot:
691  if (!Value.isAbsolute())
692  return false;
693  Res = MCValue::get(!Value.getConstant());
694  break;
695  case MCUnaryExpr::Minus:
696  /// -(a - b + const) ==> (b - a - const)
697  if (Value.getSymA() && !Value.getSymB())
698  return false;
699 
700  // The cast avoids undefined behavior if the constant is INT64_MIN.
701  Res = MCValue::get(Value.getSymB(), Value.getSymA(),
702  -(uint64_t)Value.getConstant());
703  break;
704  case MCUnaryExpr::Not:
705  if (!Value.isAbsolute())
706  return false;
707  Res = MCValue::get(~Value.getConstant());
708  break;
709  case MCUnaryExpr::Plus:
710  Res = Value;
711  break;
712  }
713 
714  return true;
715  }
716 
717  case Binary: {
718  const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
719  MCValue LHSValue, RHSValue;
720 
721  if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
722  Addrs, InSet) ||
723  !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
724  Addrs, InSet))
725  return false;
726 
727  // We only support a few operations on non-constant expressions, handle
728  // those first.
729  if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
730  switch (ABE->getOpcode()) {
731  default:
732  return false;
733  case MCBinaryExpr::Sub:
734  // Negate RHS and add.
735  // The cast avoids undefined behavior if the constant is INT64_MIN.
736  return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
737  RHSValue.getSymB(), RHSValue.getSymA(),
738  -(uint64_t)RHSValue.getConstant(), Res);
739 
740  case MCBinaryExpr::Add:
741  return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
742  RHSValue.getSymA(), RHSValue.getSymB(),
743  RHSValue.getConstant(), Res);
744  }
745  }
746 
747  // FIXME: We need target hooks for the evaluation. It may be limited in
748  // width, and gas defines the result of comparisons differently from
749  // Apple as.
750  int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
751  int64_t Result = 0;
752  switch (ABE->getOpcode()) {
753  case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
754  case MCBinaryExpr::Add: Result = LHS + RHS; break;
755  case MCBinaryExpr::And: Result = LHS & RHS; break;
756  case MCBinaryExpr::Div:
757  // Handle division by zero. gas just emits a warning and keeps going,
758  // we try to be stricter.
759  // FIXME: Currently the caller of this function has no way to understand
760  // we're bailing out because of 'division by zero'. Therefore, it will
761  // emit a 'expected relocatable expression' error. It would be nice to
762  // change this code to emit a better diagnostic.
763  if (RHS == 0)
764  return false;
765  Result = LHS / RHS;
766  break;
767  case MCBinaryExpr::EQ: Result = LHS == RHS; break;
768  case MCBinaryExpr::GT: Result = LHS > RHS; break;
769  case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
770  case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
771  case MCBinaryExpr::LOr: Result = LHS || RHS; break;
772  case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
773  case MCBinaryExpr::LT: Result = LHS < RHS; break;
774  case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
775  case MCBinaryExpr::Mod: Result = LHS % RHS; break;
776  case MCBinaryExpr::Mul: Result = LHS * RHS; break;
777  case MCBinaryExpr::NE: Result = LHS != RHS; break;
778  case MCBinaryExpr::Or: Result = LHS | RHS; break;
779  case MCBinaryExpr::Shl: Result = uint64_t(LHS) << uint64_t(RHS); break;
780  case MCBinaryExpr::Sub: Result = LHS - RHS; break;
781  case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
782  }
783 
784  Res = MCValue::get(Result);
785  return true;
786  }
787  }
788 
789  llvm_unreachable("Invalid assembly expression kind!");
790 }
791 
793  switch (getKind()) {
794  case Target:
795  // We never look through target specific expressions.
796  return cast<MCTargetExpr>(this)->findAssociatedFragment();
797 
798  case Constant:
800 
801  case SymbolRef: {
802  const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
803  const MCSymbol &Sym = SRE->getSymbol();
804  return Sym.getFragment();
805  }
806 
807  case Unary:
808  return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
809 
810  case Binary: {
811  const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
812  MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment();
813  MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment();
814 
815  // If either is absolute, return the other.
817  return RHS_F;
819  return LHS_F;
820 
821  // Not always correct, but probably the best we can do without more context.
822  if (BE->getOpcode() == MCBinaryExpr::Sub)
824 
825  // Otherwise, return the first non-null fragment.
826  return LHS_F ? LHS_F : RHS_F;
827  }
828  }
829 
830  llvm_unreachable("Invalid assembly expression kind!");
831 }
Signed less than comparison (result is either 0 or some target-specific non-zero value).
Definition: MCExpr.h:420
Bitwise negation.
Definition: MCExpr.h:355
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Signed less than or equal comparison (result is either 0 or some target-specific non-zero value)...
Definition: MCExpr.h:422
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
VariantKind getKind() const
Definition: MCExpr.h:328
Bitwise and.
Definition: MCExpr.h:411
Multiplication.
Definition: MCExpr.h:425
Opcode getOpcode() const
Get the kind of this unary expression.
Definition: MCExpr.h:394
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:554
Unary plus.
Definition: MCExpr.h:356
STATISTIC(NumFunctions, "Total number of functions")
Equality comparison.
Definition: MCExpr.h:413
Bitwise or.
Definition: MCExpr.h:427
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
int64_t getConstant() const
Definition: MCValue.h:47
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:49
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:252
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
Arithmetic shift right.
Definition: MCExpr.h:429
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:604
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:557
Logical or.
Definition: MCExpr.h:419
Signed remainder.
Definition: MCExpr.h:424
Unary assembler expressions.
Definition: MCExpr.h:350
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
static bool canExpand(const MCSymbol &Sym, bool InSet)
Definition: MCExpr.cpp:618
Signed division.
Definition: MCExpr.h:412
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:51
Unary expressions.
Definition: MCExpr.h:42
Shift left.
Definition: MCExpr.h:428
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:40
This is an important base class in LLVM.
Definition: Constant.h:42
static bool EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout, const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS, const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst, MCValue &Res)
Evaluate the result of an add between (conceptually) two MCValues.
Definition: MCExpr.cpp:553
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
Logical negation.
Definition: MCExpr.h:353
Logical and.
Definition: MCExpr.h:418
Binary assembler expressions.
Definition: MCExpr.h:407
bool hasSubsectionsViaSymbols() const
Definition: MCExpr.h:332
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static MCFragment * AbsolutePseudoFragment
Definition: MCSymbol.h:64
void printVariantKind(raw_ostream &OS) const
Definition: MCExpr.cpp:409
const MCSymbol & getSymbol() const
Definition: MCExpr.h:326
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:382
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, const MCAsmLayout *Layout, const MCFixup *Fixup, const SectionAddrMap *Addrs, bool InSet) const
Definition: MCExpr.cpp:631
Inequality comparison.
Definition: MCExpr.h:426
Signed greater than comparison (result is either 0 or some target-specific non-zero value) ...
Definition: MCExpr.h:414
MCFragment * findAssociatedFragment() const
Find the "associated section" for this expression, which is currently defined as the absolute section...
Definition: MCExpr.cpp:792
Signed greater than or equal comparison (result is either 0 or some target-specific non-zero value)...
Definition: MCExpr.h:416
Target - Wrapper for Target specific information.
Bitwise exclusive or.
Definition: MCExpr.h:432
Logical shift right.
Definition: MCExpr.h:430
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:63
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
References to labels and assigned expressions.
Definition: MCExpr.h:41
Unary minus.
Definition: MCExpr.h:354
bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const
Try to evaluate the expression to the form (a - b + constant) where neither a nor b are variables...
Definition: MCExpr.cpp:612
Opcode getOpcode() const
Get the kind of this binary expression.
Definition: MCExpr.h:551
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
Definition: MCSymbol.h:299
const MCExpr * getSubExpr() const
Get the child of this unary expression.
Definition: MCExpr.h:397
LLVM Value Representation.
Definition: Value.h:73
Constant expressions.
Definition: MCExpr.h:40
Binary expressions.
Definition: MCExpr.h:39
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
Subtraction.
Definition: MCExpr.h:431
Target specific expression.
Definition: MCExpr.h:43
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59