xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCExpr.cpp (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCExpr.h"
10 #include "llvm/ADT/Statistic.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCAsmLayout.h"
17 #include "llvm/MC/MCAssembler.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCValue.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <cassert>
28 #include <cstdint>
29 
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "mcexpr"
33 
34 namespace {
35 namespace stats {
36 
37 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
38 
39 } // end namespace stats
40 } // end anonymous namespace
41 
42 void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens) const {
43   switch (getKind()) {
44   case MCExpr::Target:
45     return cast<MCTargetExpr>(this)->printImpl(OS, MAI);
46   case MCExpr::Constant: {
47     auto Value = cast<MCConstantExpr>(*this).getValue();
48     auto PrintInHex = cast<MCConstantExpr>(*this).useHexFormat();
49     auto SizeInBytes = cast<MCConstantExpr>(*this).getSizeInBytes();
50     if (Value < 0 && MAI && !MAI->supportsSignedData())
51       PrintInHex = true;
52     if (PrintInHex)
53       switch (SizeInBytes) {
54       default:
55         OS << "0x" << Twine::utohexstr(Value);
56         break;
57       case 1:
58         OS << format("0x%02" PRIx64, Value);
59         break;
60       case 2:
61         OS << format("0x%04" PRIx64, Value);
62         break;
63       case 4:
64         OS << format("0x%08" PRIx64, Value);
65         break;
66       case 8:
67         OS << format("0x%016" PRIx64, Value);
68         break;
69       }
70     else
71       OS << Value;
72     return;
73   }
74   case MCExpr::SymbolRef: {
75     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
76     const MCSymbol &Sym = SRE.getSymbol();
77     // Parenthesize names that start with $ so that they don't look like
78     // absolute names.
79     bool UseParens =
80         !InParens && !Sym.getName().empty() && Sym.getName()[0] == '$';
81     if (UseParens) {
82       OS << '(';
83       Sym.print(OS, MAI);
84       OS << ')';
85     } else
86       Sym.print(OS, MAI);
87 
88     const MCSymbolRefExpr::VariantKind Kind = SRE.getKind();
89     if (Kind != MCSymbolRefExpr::VK_None) {
90       if (MAI && MAI->useParensForSymbolVariant()) // ARM
91         OS << '(' << MCSymbolRefExpr::getVariantKindName(Kind) << ')';
92       else
93         OS << '@' << MCSymbolRefExpr::getVariantKindName(Kind);
94     }
95 
96     return;
97   }
98 
99   case MCExpr::Unary: {
100     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
101     switch (UE.getOpcode()) {
102     case MCUnaryExpr::LNot:  OS << '!'; break;
103     case MCUnaryExpr::Minus: OS << '-'; break;
104     case MCUnaryExpr::Not:   OS << '~'; break;
105     case MCUnaryExpr::Plus:  OS << '+'; break;
106     }
107     bool Binary = UE.getSubExpr()->getKind() == MCExpr::Binary;
108     if (Binary) OS << "(";
109     UE.getSubExpr()->print(OS, MAI);
110     if (Binary) OS << ")";
111     return;
112   }
113 
114   case MCExpr::Binary: {
115     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
116 
117     // Only print parens around the LHS if it is non-trivial.
118     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
119       BE.getLHS()->print(OS, MAI);
120     } else {
121       OS << '(';
122       BE.getLHS()->print(OS, MAI);
123       OS << ')';
124     }
125 
126     switch (BE.getOpcode()) {
127     case MCBinaryExpr::Add:
128       // Print "X-42" instead of "X+-42".
129       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
130         if (RHSC->getValue() < 0) {
131           OS << RHSC->getValue();
132           return;
133         }
134       }
135 
136       OS <<  '+';
137       break;
138     case MCBinaryExpr::AShr: OS << ">>"; break;
139     case MCBinaryExpr::And:  OS <<  '&'; break;
140     case MCBinaryExpr::Div:  OS <<  '/'; break;
141     case MCBinaryExpr::EQ:   OS << "=="; break;
142     case MCBinaryExpr::GT:   OS <<  '>'; break;
143     case MCBinaryExpr::GTE:  OS << ">="; break;
144     case MCBinaryExpr::LAnd: OS << "&&"; break;
145     case MCBinaryExpr::LOr:  OS << "||"; break;
146     case MCBinaryExpr::LShr: OS << ">>"; break;
147     case MCBinaryExpr::LT:   OS <<  '<'; break;
148     case MCBinaryExpr::LTE:  OS << "<="; break;
149     case MCBinaryExpr::Mod:  OS <<  '%'; break;
150     case MCBinaryExpr::Mul:  OS <<  '*'; break;
151     case MCBinaryExpr::NE:   OS << "!="; break;
152     case MCBinaryExpr::Or:   OS <<  '|'; break;
153     case MCBinaryExpr::OrNot: OS << '!'; break;
154     case MCBinaryExpr::Shl:  OS << "<<"; break;
155     case MCBinaryExpr::Sub:  OS <<  '-'; break;
156     case MCBinaryExpr::Xor:  OS <<  '^'; break;
157     }
158 
159     // Only print parens around the LHS if it is non-trivial.
160     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
161       BE.getRHS()->print(OS, MAI);
162     } else {
163       OS << '(';
164       BE.getRHS()->print(OS, MAI);
165       OS << ')';
166     }
167     return;
168   }
169   }
170 
171   llvm_unreachable("Invalid expression kind!");
172 }
173 
174 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
175 LLVM_DUMP_METHOD void MCExpr::dump() const {
176   dbgs() << *this;
177   dbgs() << '\n';
178 }
179 #endif
180 
181 /* *** */
182 
183 const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
184                                          const MCExpr *RHS, MCContext &Ctx,
185                                          SMLoc Loc) {
186   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc);
187 }
188 
189 const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
190                                        MCContext &Ctx, SMLoc Loc) {
191   return new (Ctx) MCUnaryExpr(Opc, Expr, Loc);
192 }
193 
194 const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx,
195                                              bool PrintInHex,
196                                              unsigned SizeInBytes) {
197   return new (Ctx) MCConstantExpr(Value, PrintInHex, SizeInBytes);
198 }
199 
200 /* *** */
201 
202 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
203                                  const MCAsmInfo *MAI, SMLoc Loc)
204     : MCExpr(MCExpr::SymbolRef, Loc,
205              encodeSubclassData(Kind, MAI->hasSubsectionsViaSymbols())),
206       Symbol(Symbol) {
207   assert(Symbol);
208 }
209 
210 const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
211                                                VariantKind Kind,
212                                                MCContext &Ctx, SMLoc Loc) {
213   return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc);
214 }
215 
216 const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
217                                                MCContext &Ctx) {
218   return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
219 }
220 
221 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
222   switch (Kind) {
223   case VK_Invalid: return "<<invalid>>";
224   case VK_None: return "<<none>>";
225 
226   case VK_DTPOFF: return "DTPOFF";
227   case VK_DTPREL: return "DTPREL";
228   case VK_GOT: return "GOT";
229   case VK_GOTOFF: return "GOTOFF";
230   case VK_GOTREL: return "GOTREL";
231   case VK_PCREL: return "PCREL";
232   case VK_GOTPCREL: return "GOTPCREL";
233   case VK_GOTTPOFF: return "GOTTPOFF";
234   case VK_INDNTPOFF: return "INDNTPOFF";
235   case VK_NTPOFF: return "NTPOFF";
236   case VK_GOTNTPOFF: return "GOTNTPOFF";
237   case VK_PLT: return "PLT";
238   case VK_TLSGD: return "TLSGD";
239   case VK_TLSLD: return "TLSLD";
240   case VK_TLSLDM: return "TLSLDM";
241   case VK_TPOFF: return "TPOFF";
242   case VK_TPREL: return "TPREL";
243   case VK_TLSCALL: return "tlscall";
244   case VK_TLSDESC: return "tlsdesc";
245   case VK_TLVP: return "TLVP";
246   case VK_TLVPPAGE: return "TLVPPAGE";
247   case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
248   case VK_PAGE: return "PAGE";
249   case VK_PAGEOFF: return "PAGEOFF";
250   case VK_GOTPAGE: return "GOTPAGE";
251   case VK_GOTPAGEOFF: return "GOTPAGEOFF";
252   case VK_SECREL: return "SECREL32";
253   case VK_SIZE: return "SIZE";
254   case VK_WEAKREF: return "WEAKREF";
255   case VK_X86_ABS8: return "ABS8";
256   case VK_X86_PLTOFF: return "PLTOFF";
257   case VK_ARM_NONE: return "none";
258   case VK_ARM_GOT_PREL: return "GOT_PREL";
259   case VK_ARM_TARGET1: return "target1";
260   case VK_ARM_TARGET2: return "target2";
261   case VK_ARM_PREL31: return "prel31";
262   case VK_ARM_SBREL: return "sbrel";
263   case VK_ARM_TLSLDO: return "tlsldo";
264   case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
265   case VK_AVR_NONE: return "none";
266   case VK_AVR_LO8: return "lo8";
267   case VK_AVR_HI8: return "hi8";
268   case VK_AVR_HLO8: return "hlo8";
269   case VK_AVR_DIFF8: return "diff8";
270   case VK_AVR_DIFF16: return "diff16";
271   case VK_AVR_DIFF32: return "diff32";
272   case VK_PPC_LO: return "l";
273   case VK_PPC_HI: return "h";
274   case VK_PPC_HA: return "ha";
275   case VK_PPC_HIGH: return "high";
276   case VK_PPC_HIGHA: return "higha";
277   case VK_PPC_HIGHER: return "higher";
278   case VK_PPC_HIGHERA: return "highera";
279   case VK_PPC_HIGHEST: return "highest";
280   case VK_PPC_HIGHESTA: return "highesta";
281   case VK_PPC_GOT_LO: return "got@l";
282   case VK_PPC_GOT_HI: return "got@h";
283   case VK_PPC_GOT_HA: return "got@ha";
284   case VK_PPC_TOCBASE: return "tocbase";
285   case VK_PPC_TOC: return "toc";
286   case VK_PPC_TOC_LO: return "toc@l";
287   case VK_PPC_TOC_HI: return "toc@h";
288   case VK_PPC_TOC_HA: return "toc@ha";
289   case VK_PPC_U: return "u";
290   case VK_PPC_L: return "l";
291   case VK_PPC_DTPMOD: return "dtpmod";
292   case VK_PPC_TPREL_LO: return "tprel@l";
293   case VK_PPC_TPREL_HI: return "tprel@h";
294   case VK_PPC_TPREL_HA: return "tprel@ha";
295   case VK_PPC_TPREL_HIGH: return "tprel@high";
296   case VK_PPC_TPREL_HIGHA: return "tprel@higha";
297   case VK_PPC_TPREL_HIGHER: return "tprel@higher";
298   case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
299   case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
300   case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
301   case VK_PPC_DTPREL_LO: return "dtprel@l";
302   case VK_PPC_DTPREL_HI: return "dtprel@h";
303   case VK_PPC_DTPREL_HA: return "dtprel@ha";
304   case VK_PPC_DTPREL_HIGH: return "dtprel@high";
305   case VK_PPC_DTPREL_HIGHA: return "dtprel@higha";
306   case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
307   case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
308   case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
309   case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
310   case VK_PPC_GOT_TPREL: return "got@tprel";
311   case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
312   case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
313   case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
314   case VK_PPC_GOT_DTPREL: return "got@dtprel";
315   case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
316   case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
317   case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
318   case VK_PPC_TLS: return "tls";
319   case VK_PPC_GOT_TLSGD: return "got@tlsgd";
320   case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
321   case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
322   case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
323   case VK_PPC_TLSGD: return "tlsgd";
324   case VK_PPC_GOT_TLSLD: return "got@tlsld";
325   case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
326   case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
327   case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
328   case VK_PPC_GOT_PCREL:
329     return "got@pcrel";
330   case VK_PPC_GOT_TLSGD_PCREL:
331     return "got@tlsgd@pcrel";
332   case VK_PPC_GOT_TLSLD_PCREL:
333     return "got@tlsld@pcrel";
334   case VK_PPC_GOT_TPREL_PCREL:
335     return "got@tprel@pcrel";
336   case VK_PPC_TLS_PCREL:
337     return "tls@pcrel";
338   case VK_PPC_TLSLD: return "tlsld";
339   case VK_PPC_LOCAL: return "local";
340   case VK_PPC_NOTOC: return "notoc";
341   case VK_PPC_PCREL_OPT: return "<<invalid>>";
342   case VK_COFF_IMGREL32: return "IMGREL";
343   case VK_Hexagon_LO16: return "LO16";
344   case VK_Hexagon_HI16: return "HI16";
345   case VK_Hexagon_GPREL: return "GPREL";
346   case VK_Hexagon_GD_GOT: return "GDGOT";
347   case VK_Hexagon_LD_GOT: return "LDGOT";
348   case VK_Hexagon_GD_PLT: return "GDPLT";
349   case VK_Hexagon_LD_PLT: return "LDPLT";
350   case VK_Hexagon_IE: return "IE";
351   case VK_Hexagon_IE_GOT: return "IEGOT";
352   case VK_WASM_TYPEINDEX: return "TYPEINDEX";
353   case VK_WASM_MBREL: return "MBREL";
354   case VK_WASM_TLSREL: return "TLSREL";
355   case VK_WASM_TBREL: return "TBREL";
356   case VK_AMDGPU_GOTPCREL32_LO: return "gotpcrel32@lo";
357   case VK_AMDGPU_GOTPCREL32_HI: return "gotpcrel32@hi";
358   case VK_AMDGPU_REL32_LO: return "rel32@lo";
359   case VK_AMDGPU_REL32_HI: return "rel32@hi";
360   case VK_AMDGPU_REL64: return "rel64";
361   case VK_AMDGPU_ABS32_LO: return "abs32@lo";
362   case VK_AMDGPU_ABS32_HI: return "abs32@hi";
363   case VK_VE_HI32: return "hi";
364   case VK_VE_LO32: return "lo";
365   case VK_VE_PC_HI32: return "pc_hi";
366   case VK_VE_PC_LO32: return "pc_lo";
367   case VK_VE_GOT_HI32: return "got_hi";
368   case VK_VE_GOT_LO32: return "got_lo";
369   case VK_VE_GOTOFF_HI32: return "gotoff_hi";
370   case VK_VE_GOTOFF_LO32: return "gotoff_lo";
371   case VK_VE_PLT_HI32: return "plt_hi";
372   case VK_VE_PLT_LO32: return "plt_lo";
373   case VK_VE_TLS_GD_HI32: return "tls_gd_hi";
374   case VK_VE_TLS_GD_LO32: return "tls_gd_lo";
375   case VK_VE_TPOFF_HI32: return "tpoff_hi";
376   case VK_VE_TPOFF_LO32: return "tpoff_lo";
377   }
378   llvm_unreachable("Invalid variant kind");
379 }
380 
381 MCSymbolRefExpr::VariantKind
382 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
383   return StringSwitch<VariantKind>(Name.lower())
384     .Case("dtprel", VK_DTPREL)
385     .Case("dtpoff", VK_DTPOFF)
386     .Case("got", VK_GOT)
387     .Case("gotoff", VK_GOTOFF)
388     .Case("gotrel", VK_GOTREL)
389     .Case("pcrel", VK_PCREL)
390     .Case("gotpcrel", VK_GOTPCREL)
391     .Case("gottpoff", VK_GOTTPOFF)
392     .Case("indntpoff", VK_INDNTPOFF)
393     .Case("ntpoff", VK_NTPOFF)
394     .Case("gotntpoff", VK_GOTNTPOFF)
395     .Case("plt", VK_PLT)
396     .Case("tlscall", VK_TLSCALL)
397     .Case("tlsdesc", VK_TLSDESC)
398     .Case("tlsgd", VK_TLSGD)
399     .Case("tlsld", VK_TLSLD)
400     .Case("tlsldm", VK_TLSLDM)
401     .Case("tpoff", VK_TPOFF)
402     .Case("tprel", VK_TPREL)
403     .Case("tlvp", VK_TLVP)
404     .Case("tlvppage", VK_TLVPPAGE)
405     .Case("tlvppageoff", VK_TLVPPAGEOFF)
406     .Case("page", VK_PAGE)
407     .Case("pageoff", VK_PAGEOFF)
408     .Case("gotpage", VK_GOTPAGE)
409     .Case("gotpageoff", VK_GOTPAGEOFF)
410     .Case("imgrel", VK_COFF_IMGREL32)
411     .Case("secrel32", VK_SECREL)
412     .Case("size", VK_SIZE)
413     .Case("abs8", VK_X86_ABS8)
414     .Case("pltoff", VK_X86_PLTOFF)
415     .Case("l", VK_PPC_LO)
416     .Case("h", VK_PPC_HI)
417     .Case("ha", VK_PPC_HA)
418     .Case("high", VK_PPC_HIGH)
419     .Case("higha", VK_PPC_HIGHA)
420     .Case("higher", VK_PPC_HIGHER)
421     .Case("highera", VK_PPC_HIGHERA)
422     .Case("highest", VK_PPC_HIGHEST)
423     .Case("highesta", VK_PPC_HIGHESTA)
424     .Case("got@l", VK_PPC_GOT_LO)
425     .Case("got@h", VK_PPC_GOT_HI)
426     .Case("got@ha", VK_PPC_GOT_HA)
427     .Case("local", VK_PPC_LOCAL)
428     .Case("tocbase", VK_PPC_TOCBASE)
429     .Case("toc", VK_PPC_TOC)
430     .Case("toc@l", VK_PPC_TOC_LO)
431     .Case("toc@h", VK_PPC_TOC_HI)
432     .Case("toc@ha", VK_PPC_TOC_HA)
433     .Case("u", VK_PPC_U)
434     .Case("l", VK_PPC_L)
435     .Case("tls", VK_PPC_TLS)
436     .Case("dtpmod", VK_PPC_DTPMOD)
437     .Case("tprel@l", VK_PPC_TPREL_LO)
438     .Case("tprel@h", VK_PPC_TPREL_HI)
439     .Case("tprel@ha", VK_PPC_TPREL_HA)
440     .Case("tprel@high", VK_PPC_TPREL_HIGH)
441     .Case("tprel@higha", VK_PPC_TPREL_HIGHA)
442     .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
443     .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
444     .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
445     .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
446     .Case("dtprel@l", VK_PPC_DTPREL_LO)
447     .Case("dtprel@h", VK_PPC_DTPREL_HI)
448     .Case("dtprel@ha", VK_PPC_DTPREL_HA)
449     .Case("dtprel@high", VK_PPC_DTPREL_HIGH)
450     .Case("dtprel@higha", VK_PPC_DTPREL_HIGHA)
451     .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
452     .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
453     .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
454     .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
455     .Case("got@tprel", VK_PPC_GOT_TPREL)
456     .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
457     .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
458     .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
459     .Case("got@dtprel", VK_PPC_GOT_DTPREL)
460     .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
461     .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
462     .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
463     .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
464     .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
465     .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
466     .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
467     .Case("got@tlsld", VK_PPC_GOT_TLSLD)
468     .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
469     .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
470     .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
471     .Case("got@pcrel", VK_PPC_GOT_PCREL)
472     .Case("got@tlsgd@pcrel", VK_PPC_GOT_TLSGD_PCREL)
473     .Case("got@tlsld@pcrel", VK_PPC_GOT_TLSLD_PCREL)
474     .Case("got@tprel@pcrel", VK_PPC_GOT_TPREL_PCREL)
475     .Case("tls@pcrel", VK_PPC_TLS_PCREL)
476     .Case("notoc", VK_PPC_NOTOC)
477     .Case("gdgot", VK_Hexagon_GD_GOT)
478     .Case("gdplt", VK_Hexagon_GD_PLT)
479     .Case("iegot", VK_Hexagon_IE_GOT)
480     .Case("ie", VK_Hexagon_IE)
481     .Case("ldgot", VK_Hexagon_LD_GOT)
482     .Case("ldplt", VK_Hexagon_LD_PLT)
483     .Case("none", VK_ARM_NONE)
484     .Case("got_prel", VK_ARM_GOT_PREL)
485     .Case("target1", VK_ARM_TARGET1)
486     .Case("target2", VK_ARM_TARGET2)
487     .Case("prel31", VK_ARM_PREL31)
488     .Case("sbrel", VK_ARM_SBREL)
489     .Case("tlsldo", VK_ARM_TLSLDO)
490     .Case("lo8", VK_AVR_LO8)
491     .Case("hi8", VK_AVR_HI8)
492     .Case("hlo8", VK_AVR_HLO8)
493     .Case("typeindex", VK_WASM_TYPEINDEX)
494     .Case("tbrel", VK_WASM_TBREL)
495     .Case("mbrel", VK_WASM_MBREL)
496     .Case("tlsrel", VK_WASM_TLSREL)
497     .Case("gotpcrel32@lo", VK_AMDGPU_GOTPCREL32_LO)
498     .Case("gotpcrel32@hi", VK_AMDGPU_GOTPCREL32_HI)
499     .Case("rel32@lo", VK_AMDGPU_REL32_LO)
500     .Case("rel32@hi", VK_AMDGPU_REL32_HI)
501     .Case("rel64", VK_AMDGPU_REL64)
502     .Case("abs32@lo", VK_AMDGPU_ABS32_LO)
503     .Case("abs32@hi", VK_AMDGPU_ABS32_HI)
504     .Case("hi", VK_VE_HI32)
505     .Case("lo", VK_VE_LO32)
506     .Case("pc_hi", VK_VE_PC_HI32)
507     .Case("pc_lo", VK_VE_PC_LO32)
508     .Case("got_hi", VK_VE_GOT_HI32)
509     .Case("got_lo", VK_VE_GOT_LO32)
510     .Case("gotoff_hi", VK_VE_GOTOFF_HI32)
511     .Case("gotoff_lo", VK_VE_GOTOFF_LO32)
512     .Case("plt_hi", VK_VE_PLT_HI32)
513     .Case("plt_lo", VK_VE_PLT_LO32)
514     .Case("tls_gd_hi", VK_VE_TLS_GD_HI32)
515     .Case("tls_gd_lo", VK_VE_TLS_GD_LO32)
516     .Case("tpoff_hi", VK_VE_TPOFF_HI32)
517     .Case("tpoff_lo", VK_VE_TPOFF_LO32)
518     .Default(VK_Invalid);
519 }
520 
521 /* *** */
522 
523 void MCTargetExpr::anchor() {}
524 
525 /* *** */
526 
527 bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
528   return evaluateAsAbsolute(Res, nullptr, nullptr, nullptr, false);
529 }
530 
531 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
532                                 const MCAsmLayout &Layout) const {
533   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, false);
534 }
535 
536 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
537                                 const MCAsmLayout &Layout,
538                                 const SectionAddrMap &Addrs) const {
539   // Setting InSet causes us to absolutize differences across sections and that
540   // is what the MachO writer uses Addrs for.
541   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs, true);
542 }
543 
544 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
545   return evaluateAsAbsolute(Res, &Asm, nullptr, nullptr, false);
546 }
547 
548 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const {
549   return evaluateAsAbsolute(Res, Asm, nullptr, nullptr, false);
550 }
551 
552 bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
553                                    const MCAsmLayout &Layout) const {
554   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
555                             true);
556 }
557 
558 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
559                                 const MCAsmLayout *Layout,
560                                 const SectionAddrMap *Addrs, bool InSet) const {
561   MCValue Value;
562 
563   // Fast path constants.
564   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
565     Res = CE->getValue();
566     return true;
567   }
568 
569   bool IsRelocatable =
570       evaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
571 
572   // Record the current value.
573   Res = Value.getConstant();
574 
575   return IsRelocatable && Value.isAbsolute();
576 }
577 
578 /// Helper method for \see EvaluateSymbolAdd().
579 static void AttemptToFoldSymbolOffsetDifference(
580     const MCAssembler *Asm, const MCAsmLayout *Layout,
581     const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A,
582     const MCSymbolRefExpr *&B, int64_t &Addend) {
583   if (!A || !B)
584     return;
585 
586   const MCSymbol &SA = A->getSymbol();
587   const MCSymbol &SB = B->getSymbol();
588 
589   if (SA.isUndefined() || SB.isUndefined())
590     return;
591 
592   if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
593     return;
594 
595   auto FinalizeFolding = [&]() {
596     // Pointers to Thumb symbols need to have their low-bit set to allow
597     // for interworking.
598     if (Asm->isThumbFunc(&SA))
599       Addend |= 1;
600 
601     // If symbol is labeled as micromips, we set low-bit to ensure
602     // correct offset in .gcc_except_table
603     if (Asm->getBackend().isMicroMips(&SA))
604       Addend |= 1;
605 
606     // Clear the symbol expr pointers to indicate we have folded these
607     // operands.
608     A = B = nullptr;
609   };
610 
611   const MCFragment *FA = SA.getFragment();
612   const MCFragment *FB = SB.getFragment();
613   // If both symbols are in the same fragment, return the difference of their
614   // offsets
615   if (FA == FB && !SA.isVariable() && !SA.isUnset() && !SB.isVariable() &&
616       !SB.isUnset()) {
617     Addend += SA.getOffset() - SB.getOffset();
618     return FinalizeFolding();
619   }
620 
621   const MCSection &SecA = *FA->getParent();
622   const MCSection &SecB = *FB->getParent();
623 
624   if ((&SecA != &SecB) && !Addrs)
625     return;
626 
627   if (Layout) {
628     // One of the symbol involved is part of a fragment being laid out. Quit now
629     // to avoid a self loop.
630     if (!Layout->canGetFragmentOffset(FA) || !Layout->canGetFragmentOffset(FB))
631       return;
632 
633     // Eagerly evaluate when layout is finalized.
634     Addend += Layout->getSymbolOffset(A->getSymbol()) -
635               Layout->getSymbolOffset(B->getSymbol());
636     if (Addrs && (&SecA != &SecB))
637       Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
638 
639     FinalizeFolding();
640   } else {
641     // When layout is not finalized, our ability to resolve differences between
642     // symbols is limited to specific cases where the fragments between two
643     // symbols (including the fragments the symbols are defined in) are
644     // fixed-size fragments so the difference can be calculated. For example,
645     // this is important when the Subtarget is changed and a new MCDataFragment
646     // is created in the case of foo: instr; .arch_extension ext; instr .if . -
647     // foo.
648     if (SA.isVariable() || SA.isUnset() || SB.isVariable() || SB.isUnset() ||
649         FA->getKind() != MCFragment::FT_Data ||
650         FB->getKind() != MCFragment::FT_Data ||
651         FA->getSubsectionNumber() != FB->getSubsectionNumber())
652       return;
653     // Try to find a constant displacement from FA to FB, add the displacement
654     // between the offset in FA of SA and the offset in FB of SB.
655     int64_t Displacement = SA.getOffset() - SB.getOffset();
656     for (auto FI = FB->getIterator(), FE = SecA.end(); FI != FE; ++FI) {
657       if (&*FI == FA) {
658         Addend += Displacement;
659         return FinalizeFolding();
660       }
661 
662       if (FI->getKind() != MCFragment::FT_Data)
663         return;
664       Displacement += cast<MCDataFragment>(FI)->getContents().size();
665     }
666   }
667 }
668 
669 static bool canFold(const MCAssembler *Asm, const MCSymbolRefExpr *A,
670                     const MCSymbolRefExpr *B, bool InSet) {
671   if (InSet)
672     return true;
673 
674   if (!Asm->getBackend().requiresDiffExpressionRelocations())
675     return true;
676 
677   const MCSymbol &CheckSym = A ? A->getSymbol() : B->getSymbol();
678   if (!CheckSym.isInSection())
679     return true;
680 
681   if (!CheckSym.getSection().hasInstructions())
682     return true;
683 
684   return false;
685 }
686 
687 /// Evaluate the result of an add between (conceptually) two MCValues.
688 ///
689 /// This routine conceptually attempts to construct an MCValue:
690 ///   Result = (Result_A - Result_B + Result_Cst)
691 /// from two MCValue's LHS and RHS where
692 ///   Result = LHS + RHS
693 /// and
694 ///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
695 ///
696 /// This routine attempts to aggressively fold the operands such that the result
697 /// is representable in an MCValue, but may not always succeed.
698 ///
699 /// \returns True on success, false if the result is not representable in an
700 /// MCValue.
701 
702 /// NOTE: It is really important to have both the Asm and Layout arguments.
703 /// They might look redundant, but this function can be used before layout
704 /// is done (see the object streamer for example) and having the Asm argument
705 /// lets us avoid relaxations early.
706 static bool
707 EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
708                     const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
709                     const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
710                     int64_t RHS_Cst, MCValue &Res) {
711   // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
712   // about dealing with modifiers. This will ultimately bite us, one day.
713   const MCSymbolRefExpr *LHS_A = LHS.getSymA();
714   const MCSymbolRefExpr *LHS_B = LHS.getSymB();
715   int64_t LHS_Cst = LHS.getConstant();
716 
717   // Fold the result constant immediately.
718   int64_t Result_Cst = LHS_Cst + RHS_Cst;
719 
720   assert((!Layout || Asm) &&
721          "Must have an assembler object if layout is given!");
722 
723   // If we have a layout, we can fold resolved differences. Do not do this if
724   // the backend requires this to be emitted as individual relocations, unless
725   // the InSet flag is set to get the current difference anyway (used for
726   // example to calculate symbol sizes).
727   if (Asm && canFold(Asm, LHS_A, LHS_B, InSet)) {
728     // First, fold out any differences which are fully resolved. By
729     // reassociating terms in
730     //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
731     // we have the four possible differences:
732     //   (LHS_A - LHS_B),
733     //   (LHS_A - RHS_B),
734     //   (RHS_A - LHS_B),
735     //   (RHS_A - RHS_B).
736     // Since we are attempting to be as aggressive as possible about folding, we
737     // attempt to evaluate each possible alternative.
738     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
739                                         Result_Cst);
740     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
741                                         Result_Cst);
742     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
743                                         Result_Cst);
744     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
745                                         Result_Cst);
746   }
747 
748   // We can't represent the addition or subtraction of two symbols.
749   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
750     return false;
751 
752   // At this point, we have at most one additive symbol and one subtractive
753   // symbol -- find them.
754   const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
755   const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
756 
757   Res = MCValue::get(A, B, Result_Cst);
758   return true;
759 }
760 
761 bool MCExpr::evaluateAsRelocatable(MCValue &Res,
762                                    const MCAsmLayout *Layout,
763                                    const MCFixup *Fixup) const {
764   MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
765   return evaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
766                                    false);
767 }
768 
769 bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
770   MCAssembler *Assembler = &Layout.getAssembler();
771   return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
772                                    true);
773 }
774 
775 static bool canExpand(const MCSymbol &Sym, bool InSet) {
776   const MCExpr *Expr = Sym.getVariableValue();
777   const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
778   if (Inner) {
779     if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
780       return false;
781   }
782 
783   if (InSet)
784     return true;
785   return !Sym.isInSection();
786 }
787 
788 bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
789                                        const MCAsmLayout *Layout,
790                                        const MCFixup *Fixup,
791                                        const SectionAddrMap *Addrs,
792                                        bool InSet) const {
793   ++stats::MCExprEvaluate;
794 
795   switch (getKind()) {
796   case Target:
797     return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
798                                                                Fixup);
799 
800   case Constant:
801     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
802     return true;
803 
804   case SymbolRef: {
805     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
806     const MCSymbol &Sym = SRE->getSymbol();
807     const auto Kind = SRE->getKind();
808 
809     // Evaluate recursively if this is a variable.
810     if (Sym.isVariable() && (Kind == MCSymbolRefExpr::VK_None || Layout) &&
811         canExpand(Sym, InSet)) {
812       bool IsMachO = SRE->hasSubsectionsViaSymbols();
813       if (Sym.getVariableValue()->evaluateAsRelocatableImpl(
814               Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) {
815         if (Kind != MCSymbolRefExpr::VK_None) {
816           if (Res.isAbsolute()) {
817             Res = MCValue::get(SRE, nullptr, 0);
818             return true;
819           }
820           // If the reference has a variant kind, we can only handle expressions
821           // which evaluate exactly to a single unadorned symbol. Attach the
822           // original VariantKind to SymA of the result.
823           if (Res.getRefKind() != MCSymbolRefExpr::VK_None || !Res.getSymA() ||
824               Res.getSymB() || Res.getConstant())
825             return false;
826           Res =
827               MCValue::get(MCSymbolRefExpr::create(&Res.getSymA()->getSymbol(),
828                                                    Kind, Asm->getContext()),
829                            Res.getSymB(), Res.getConstant(), Res.getRefKind());
830         }
831         if (!IsMachO)
832           return true;
833 
834         const MCSymbolRefExpr *A = Res.getSymA();
835         const MCSymbolRefExpr *B = Res.getSymB();
836         // FIXME: This is small hack. Given
837         // a = b + 4
838         // .long a
839         // the OS X assembler will completely drop the 4. We should probably
840         // include it in the relocation or produce an error if that is not
841         // possible.
842         // Allow constant expressions.
843         if (!A && !B)
844           return true;
845         // Allows aliases with zero offset.
846         if (Res.getConstant() == 0 && (!A || !B))
847           return true;
848       }
849     }
850 
851     Res = MCValue::get(SRE, nullptr, 0);
852     return true;
853   }
854 
855   case Unary: {
856     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
857     MCValue Value;
858 
859     if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
860                                                       Addrs, InSet))
861       return false;
862 
863     switch (AUE->getOpcode()) {
864     case MCUnaryExpr::LNot:
865       if (!Value.isAbsolute())
866         return false;
867       Res = MCValue::get(!Value.getConstant());
868       break;
869     case MCUnaryExpr::Minus:
870       /// -(a - b + const) ==> (b - a - const)
871       if (Value.getSymA() && !Value.getSymB())
872         return false;
873 
874       // The cast avoids undefined behavior if the constant is INT64_MIN.
875       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
876                          -(uint64_t)Value.getConstant());
877       break;
878     case MCUnaryExpr::Not:
879       if (!Value.isAbsolute())
880         return false;
881       Res = MCValue::get(~Value.getConstant());
882       break;
883     case MCUnaryExpr::Plus:
884       Res = Value;
885       break;
886     }
887 
888     return true;
889   }
890 
891   case Binary: {
892     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
893     MCValue LHSValue, RHSValue;
894 
895     if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
896                                                   Addrs, InSet) ||
897         !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
898                                                   Addrs, InSet)) {
899       // Check if both are Target Expressions, see if we can compare them.
900       if (const MCTargetExpr *L = dyn_cast<MCTargetExpr>(ABE->getLHS()))
901         if (const MCTargetExpr *R = cast<MCTargetExpr>(ABE->getRHS())) {
902           switch (ABE->getOpcode()) {
903           case MCBinaryExpr::EQ:
904             Res = MCValue::get((L->isEqualTo(R)) ? -1 : 0);
905             return true;
906           case MCBinaryExpr::NE:
907             Res = MCValue::get((R->isEqualTo(R)) ? 0 : -1);
908             return true;
909           default: break;
910           }
911         }
912       return false;
913     }
914 
915     // We only support a few operations on non-constant expressions, handle
916     // those first.
917     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
918       switch (ABE->getOpcode()) {
919       default:
920         return false;
921       case MCBinaryExpr::Sub:
922         // Negate RHS and add.
923         // The cast avoids undefined behavior if the constant is INT64_MIN.
924         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
925                                    RHSValue.getSymB(), RHSValue.getSymA(),
926                                    -(uint64_t)RHSValue.getConstant(), Res);
927 
928       case MCBinaryExpr::Add:
929         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
930                                    RHSValue.getSymA(), RHSValue.getSymB(),
931                                    RHSValue.getConstant(), Res);
932       }
933     }
934 
935     // FIXME: We need target hooks for the evaluation. It may be limited in
936     // width, and gas defines the result of comparisons differently from
937     // Apple as.
938     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
939     int64_t Result = 0;
940     auto Op = ABE->getOpcode();
941     switch (Op) {
942     case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
943     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
944     case MCBinaryExpr::And:  Result = LHS & RHS; break;
945     case MCBinaryExpr::Div:
946     case MCBinaryExpr::Mod:
947       // Handle division by zero. gas just emits a warning and keeps going,
948       // we try to be stricter.
949       // FIXME: Currently the caller of this function has no way to understand
950       // we're bailing out because of 'division by zero'. Therefore, it will
951       // emit a 'expected relocatable expression' error. It would be nice to
952       // change this code to emit a better diagnostic.
953       if (RHS == 0)
954         return false;
955       if (ABE->getOpcode() == MCBinaryExpr::Div)
956         Result = LHS / RHS;
957       else
958         Result = LHS % RHS;
959       break;
960     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
961     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
962     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
963     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
964     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
965     case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
966     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
967     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
968     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
969     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
970     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
971     case MCBinaryExpr::OrNot: Result = LHS | ~RHS; break;
972     case MCBinaryExpr::Shl:  Result = uint64_t(LHS) << uint64_t(RHS); break;
973     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
974     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
975     }
976 
977     switch (Op) {
978     default:
979       Res = MCValue::get(Result);
980       break;
981     case MCBinaryExpr::EQ:
982     case MCBinaryExpr::GT:
983     case MCBinaryExpr::GTE:
984     case MCBinaryExpr::LT:
985     case MCBinaryExpr::LTE:
986     case MCBinaryExpr::NE:
987       // A comparison operator returns a -1 if true and 0 if false.
988       Res = MCValue::get(Result ? -1 : 0);
989       break;
990     }
991 
992     return true;
993   }
994   }
995 
996   llvm_unreachable("Invalid assembly expression kind!");
997 }
998 
999 MCFragment *MCExpr::findAssociatedFragment() const {
1000   switch (getKind()) {
1001   case Target:
1002     // We never look through target specific expressions.
1003     return cast<MCTargetExpr>(this)->findAssociatedFragment();
1004 
1005   case Constant:
1006     return MCSymbol::AbsolutePseudoFragment;
1007 
1008   case SymbolRef: {
1009     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
1010     const MCSymbol &Sym = SRE->getSymbol();
1011     return Sym.getFragment();
1012   }
1013 
1014   case Unary:
1015     return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
1016 
1017   case Binary: {
1018     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
1019     MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment();
1020     MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment();
1021 
1022     // If either is absolute, return the other.
1023     if (LHS_F == MCSymbol::AbsolutePseudoFragment)
1024       return RHS_F;
1025     if (RHS_F == MCSymbol::AbsolutePseudoFragment)
1026       return LHS_F;
1027 
1028     // Not always correct, but probably the best we can do without more context.
1029     if (BE->getOpcode() == MCBinaryExpr::Sub)
1030       return MCSymbol::AbsolutePseudoFragment;
1031 
1032     // Otherwise, return the first non-null fragment.
1033     return LHS_F ? LHS_F : RHS_F;
1034   }
1035   }
1036 
1037   llvm_unreachable("Invalid assembly expression kind!");
1038 }
1039