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