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