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