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