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