1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCExpr.h"
10 #include "llvm/ADT/Statistic.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCAsmLayout.h"
17 #include "llvm/MC/MCAssembler.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCValue.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
32 #define DEBUG_TYPE "mcexpr"
37 STATISTIC(MCExprEvaluate
, "Number of MCExpr evaluations");
39 } // end namespace stats
40 } // end anonymous namespace
42 void MCExpr::print(raw_ostream
&OS
, const MCAsmInfo
*MAI
, bool InParens
) const {
45 return cast
<MCTargetExpr
>(this)->printImpl(OS
, MAI
);
46 case MCExpr::Constant
: {
47 auto Value
= cast
<MCConstantExpr
>(*this).getValue();
48 auto PrintInHex
= cast
<MCConstantExpr
>(*this).useHexFormat();
49 auto SizeInBytes
= cast
<MCConstantExpr
>(*this).getSizeInBytes();
50 if (Value
< 0 && MAI
&& !MAI
->supportsSignedData())
53 switch (SizeInBytes
) {
55 OS
<< "0x" << Twine::utohexstr(Value
);
58 OS
<< format("0x%02" PRIx64
, Value
);
61 OS
<< format("0x%04" PRIx64
, Value
);
64 OS
<< format("0x%08" PRIx64
, Value
);
67 OS
<< format("0x%016" PRIx64
, Value
);
74 case MCExpr::SymbolRef
: {
75 const MCSymbolRefExpr
&SRE
= cast
<MCSymbolRefExpr
>(*this);
76 const MCSymbol
&Sym
= SRE
.getSymbol();
77 // Parenthesize names that start with $ so that they don't look like
80 !InParens
&& !Sym
.getName().empty() && Sym
.getName()[0] == '$';
88 const MCSymbolRefExpr::VariantKind Kind
= SRE
.getKind();
89 if (Kind
!= MCSymbolRefExpr::VK_None
) {
90 if (MAI
&& MAI
->useParensForSymbolVariant()) // ARM
91 OS
<< '(' << MCSymbolRefExpr::getVariantKindName(Kind
) << ')';
93 OS
<< '@' << MCSymbolRefExpr::getVariantKindName(Kind
);
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;
107 bool Binary
= UE
.getSubExpr()->getKind() == MCExpr::Binary
;
108 if (Binary
) OS
<< "(";
109 UE
.getSubExpr()->print(OS
, MAI
);
110 if (Binary
) OS
<< ")";
114 case MCExpr::Binary
: {
115 const MCBinaryExpr
&BE
= cast
<MCBinaryExpr
>(*this);
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
);
122 BE
.getLHS()->print(OS
, MAI
);
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();
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;
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
);
164 BE
.getRHS()->print(OS
, MAI
);
171 llvm_unreachable("Invalid expression kind!");
174 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
175 LLVM_DUMP_METHOD
void MCExpr::dump() const {
183 const MCBinaryExpr
*MCBinaryExpr::create(Opcode Opc
, const MCExpr
*LHS
,
184 const MCExpr
*RHS
, MCContext
&Ctx
,
186 return new (Ctx
) MCBinaryExpr(Opc
, LHS
, RHS
, Loc
);
189 const MCUnaryExpr
*MCUnaryExpr::create(Opcode Opc
, const MCExpr
*Expr
,
190 MCContext
&Ctx
, SMLoc Loc
) {
191 return new (Ctx
) MCUnaryExpr(Opc
, Expr
, Loc
);
194 const MCConstantExpr
*MCConstantExpr::create(int64_t Value
, MCContext
&Ctx
,
196 unsigned SizeInBytes
) {
197 return new (Ctx
) MCConstantExpr(Value
, PrintInHex
, SizeInBytes
);
202 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol
*Symbol
, VariantKind Kind
,
203 const MCAsmInfo
*MAI
, SMLoc Loc
)
204 : MCExpr(MCExpr::SymbolRef
, Loc
,
205 encodeSubclassData(Kind
, MAI
->hasSubsectionsViaSymbols())),
210 const MCSymbolRefExpr
*MCSymbolRefExpr::create(const MCSymbol
*Sym
,
212 MCContext
&Ctx
, SMLoc Loc
) {
213 return new (Ctx
) MCSymbolRefExpr(Sym
, Kind
, Ctx
.getAsmInfo(), Loc
);
216 const MCSymbolRefExpr
*MCSymbolRefExpr::create(StringRef Name
, VariantKind Kind
,
218 return create(Ctx
.getOrCreateSymbol(Name
), Kind
, Ctx
);
221 StringRef
MCSymbolRefExpr::getVariantKindName(VariantKind Kind
) {
223 case VK_Invalid
: return "<<invalid>>";
224 case VK_None
: return "<<none>>";
226 case VK_DTPOFF
: return "DTPOFF";
227 case VK_DTPREL
: return "DTPREL";
228 case VK_GOT
: return "GOT";
229 case VK_GOTOFF
: return "GOTOFF";
230 case VK_GOTREL
: return "GOTREL";
231 case VK_PCREL
: return "PCREL";
232 case VK_GOTPCREL
: return "GOTPCREL";
233 case VK_GOTTPOFF
: return "GOTTPOFF";
234 case VK_INDNTPOFF
: return "INDNTPOFF";
235 case VK_NTPOFF
: return "NTPOFF";
236 case VK_GOTNTPOFF
: return "GOTNTPOFF";
237 case VK_PLT
: return "PLT";
238 case VK_TLSGD
: return "TLSGD";
239 case VK_TLSLD
: return "TLSLD";
240 case VK_TLSLDM
: return "TLSLDM";
241 case VK_TPOFF
: return "TPOFF";
242 case VK_TPREL
: return "TPREL";
243 case VK_TLSCALL
: return "tlscall";
244 case VK_TLSDESC
: return "tlsdesc";
245 case VK_TLVP
: return "TLVP";
246 case VK_TLVPPAGE
: return "TLVPPAGE";
247 case VK_TLVPPAGEOFF
: return "TLVPPAGEOFF";
248 case VK_PAGE
: return "PAGE";
249 case VK_PAGEOFF
: return "PAGEOFF";
250 case VK_GOTPAGE
: return "GOTPAGE";
251 case VK_GOTPAGEOFF
: return "GOTPAGEOFF";
252 case VK_SECREL
: return "SECREL32";
253 case VK_SIZE
: return "SIZE";
254 case VK_WEAKREF
: return "WEAKREF";
255 case VK_X86_ABS8
: return "ABS8";
256 case VK_X86_PLTOFF
: return "PLTOFF";
257 case VK_ARM_NONE
: return "none";
258 case VK_ARM_GOT_PREL
: return "GOT_PREL";
259 case VK_ARM_TARGET1
: return "target1";
260 case VK_ARM_TARGET2
: return "target2";
261 case VK_ARM_PREL31
: return "prel31";
262 case VK_ARM_SBREL
: return "sbrel";
263 case VK_ARM_TLSLDO
: return "tlsldo";
264 case VK_ARM_TLSDESCSEQ
: return "tlsdescseq";
265 case VK_AVR_NONE
: return "none";
266 case VK_AVR_LO8
: return "lo8";
267 case VK_AVR_HI8
: return "hi8";
268 case VK_AVR_HLO8
: return "hlo8";
269 case VK_AVR_DIFF8
: return "diff8";
270 case VK_AVR_DIFF16
: return "diff16";
271 case VK_AVR_DIFF32
: return "diff32";
272 case VK_AVR_PM
: return "pm";
273 case VK_PPC_LO
: return "l";
274 case VK_PPC_HI
: return "h";
275 case VK_PPC_HA
: return "ha";
276 case VK_PPC_HIGH
: return "high";
277 case VK_PPC_HIGHA
: return "higha";
278 case VK_PPC_HIGHER
: return "higher";
279 case VK_PPC_HIGHERA
: return "highera";
280 case VK_PPC_HIGHEST
: return "highest";
281 case VK_PPC_HIGHESTA
: return "highesta";
282 case VK_PPC_GOT_LO
: return "got@l";
283 case VK_PPC_GOT_HI
: return "got@h";
284 case VK_PPC_GOT_HA
: return "got@ha";
285 case VK_PPC_TOCBASE
: return "tocbase";
286 case VK_PPC_TOC
: return "toc";
287 case VK_PPC_TOC_LO
: return "toc@l";
288 case VK_PPC_TOC_HI
: return "toc@h";
289 case VK_PPC_TOC_HA
: return "toc@ha";
290 case VK_PPC_U
: return "u";
291 case VK_PPC_L
: return "l";
292 case VK_PPC_DTPMOD
: return "dtpmod";
293 case VK_PPC_TPREL_LO
: return "tprel@l";
294 case VK_PPC_TPREL_HI
: return "tprel@h";
295 case VK_PPC_TPREL_HA
: return "tprel@ha";
296 case VK_PPC_TPREL_HIGH
: return "tprel@high";
297 case VK_PPC_TPREL_HIGHA
: return "tprel@higha";
298 case VK_PPC_TPREL_HIGHER
: return "tprel@higher";
299 case VK_PPC_TPREL_HIGHERA
: return "tprel@highera";
300 case VK_PPC_TPREL_HIGHEST
: return "tprel@highest";
301 case VK_PPC_TPREL_HIGHESTA
: return "tprel@highesta";
302 case VK_PPC_DTPREL_LO
: return "dtprel@l";
303 case VK_PPC_DTPREL_HI
: return "dtprel@h";
304 case VK_PPC_DTPREL_HA
: return "dtprel@ha";
305 case VK_PPC_DTPREL_HIGH
: return "dtprel@high";
306 case VK_PPC_DTPREL_HIGHA
: return "dtprel@higha";
307 case VK_PPC_DTPREL_HIGHER
: return "dtprel@higher";
308 case VK_PPC_DTPREL_HIGHERA
: return "dtprel@highera";
309 case VK_PPC_DTPREL_HIGHEST
: return "dtprel@highest";
310 case VK_PPC_DTPREL_HIGHESTA
: return "dtprel@highesta";
311 case VK_PPC_GOT_TPREL
: return "got@tprel";
312 case VK_PPC_GOT_TPREL_LO
: return "got@tprel@l";
313 case VK_PPC_GOT_TPREL_HI
: return "got@tprel@h";
314 case VK_PPC_GOT_TPREL_HA
: return "got@tprel@ha";
315 case VK_PPC_GOT_DTPREL
: return "got@dtprel";
316 case VK_PPC_GOT_DTPREL_LO
: return "got@dtprel@l";
317 case VK_PPC_GOT_DTPREL_HI
: return "got@dtprel@h";
318 case VK_PPC_GOT_DTPREL_HA
: return "got@dtprel@ha";
319 case VK_PPC_TLS
: return "tls";
320 case VK_PPC_GOT_TLSGD
: return "got@tlsgd";
321 case VK_PPC_GOT_TLSGD_LO
: return "got@tlsgd@l";
322 case VK_PPC_GOT_TLSGD_HI
: return "got@tlsgd@h";
323 case VK_PPC_GOT_TLSGD_HA
: return "got@tlsgd@ha";
324 case VK_PPC_TLSGD
: return "tlsgd";
325 case VK_PPC_AIX_TLSGD
:
327 case VK_PPC_AIX_TLSGDM
:
329 case VK_PPC_GOT_TLSLD
: return "got@tlsld";
330 case VK_PPC_GOT_TLSLD_LO
: return "got@tlsld@l";
331 case VK_PPC_GOT_TLSLD_HI
: return "got@tlsld@h";
332 case VK_PPC_GOT_TLSLD_HA
: return "got@tlsld@ha";
333 case VK_PPC_GOT_PCREL
:
335 case VK_PPC_GOT_TLSGD_PCREL
:
336 return "got@tlsgd@pcrel";
337 case VK_PPC_GOT_TLSLD_PCREL
:
338 return "got@tlsld@pcrel";
339 case VK_PPC_GOT_TPREL_PCREL
:
340 return "got@tprel@pcrel";
341 case VK_PPC_TLS_PCREL
:
343 case VK_PPC_TLSLD
: return "tlsld";
344 case VK_PPC_LOCAL
: return "local";
345 case VK_PPC_NOTOC
: return "notoc";
346 case VK_PPC_PCREL_OPT
: return "<<invalid>>";
347 case VK_COFF_IMGREL32
: return "IMGREL";
348 case VK_Hexagon_LO16
: return "LO16";
349 case VK_Hexagon_HI16
: return "HI16";
350 case VK_Hexagon_GPREL
: return "GPREL";
351 case VK_Hexagon_GD_GOT
: return "GDGOT";
352 case VK_Hexagon_LD_GOT
: return "LDGOT";
353 case VK_Hexagon_GD_PLT
: return "GDPLT";
354 case VK_Hexagon_LD_PLT
: return "LDPLT";
355 case VK_Hexagon_IE
: return "IE";
356 case VK_Hexagon_IE_GOT
: return "IEGOT";
357 case VK_WASM_TYPEINDEX
: return "TYPEINDEX";
358 case VK_WASM_MBREL
: return "MBREL";
359 case VK_WASM_TLSREL
: return "TLSREL";
360 case VK_WASM_TBREL
: return "TBREL";
361 case VK_AMDGPU_GOTPCREL32_LO
: return "gotpcrel32@lo";
362 case VK_AMDGPU_GOTPCREL32_HI
: return "gotpcrel32@hi";
363 case VK_AMDGPU_REL32_LO
: return "rel32@lo";
364 case VK_AMDGPU_REL32_HI
: return "rel32@hi";
365 case VK_AMDGPU_REL64
: return "rel64";
366 case VK_AMDGPU_ABS32_LO
: return "abs32@lo";
367 case VK_AMDGPU_ABS32_HI
: return "abs32@hi";
368 case VK_VE_HI32
: return "hi";
369 case VK_VE_LO32
: return "lo";
370 case VK_VE_PC_HI32
: return "pc_hi";
371 case VK_VE_PC_LO32
: return "pc_lo";
372 case VK_VE_GOT_HI32
: return "got_hi";
373 case VK_VE_GOT_LO32
: return "got_lo";
374 case VK_VE_GOTOFF_HI32
: return "gotoff_hi";
375 case VK_VE_GOTOFF_LO32
: return "gotoff_lo";
376 case VK_VE_PLT_HI32
: return "plt_hi";
377 case VK_VE_PLT_LO32
: return "plt_lo";
378 case VK_VE_TLS_GD_HI32
: return "tls_gd_hi";
379 case VK_VE_TLS_GD_LO32
: return "tls_gd_lo";
380 case VK_VE_TPOFF_HI32
: return "tpoff_hi";
381 case VK_VE_TPOFF_LO32
: return "tpoff_lo";
383 llvm_unreachable("Invalid variant kind");
386 MCSymbolRefExpr::VariantKind
387 MCSymbolRefExpr::getVariantKindForName(StringRef Name
) {
388 return StringSwitch
<VariantKind
>(Name
.lower())
389 .Case("dtprel", VK_DTPREL
)
390 .Case("dtpoff", VK_DTPOFF
)
392 .Case("gotoff", VK_GOTOFF
)
393 .Case("gotrel", VK_GOTREL
)
394 .Case("pcrel", VK_PCREL
)
395 .Case("gotpcrel", VK_GOTPCREL
)
396 .Case("gottpoff", VK_GOTTPOFF
)
397 .Case("indntpoff", VK_INDNTPOFF
)
398 .Case("ntpoff", VK_NTPOFF
)
399 .Case("gotntpoff", VK_GOTNTPOFF
)
401 .Case("tlscall", VK_TLSCALL
)
402 .Case("tlsdesc", VK_TLSDESC
)
403 .Case("tlsgd", VK_TLSGD
)
404 .Case("tlsld", VK_TLSLD
)
405 .Case("tlsldm", VK_TLSLDM
)
406 .Case("tpoff", VK_TPOFF
)
407 .Case("tprel", VK_TPREL
)
408 .Case("tlvp", VK_TLVP
)
409 .Case("tlvppage", VK_TLVPPAGE
)
410 .Case("tlvppageoff", VK_TLVPPAGEOFF
)
411 .Case("page", VK_PAGE
)
412 .Case("pageoff", VK_PAGEOFF
)
413 .Case("gotpage", VK_GOTPAGE
)
414 .Case("gotpageoff", VK_GOTPAGEOFF
)
415 .Case("imgrel", VK_COFF_IMGREL32
)
416 .Case("secrel32", VK_SECREL
)
417 .Case("size", VK_SIZE
)
418 .Case("abs8", VK_X86_ABS8
)
419 .Case("pltoff", VK_X86_PLTOFF
)
420 .Case("l", VK_PPC_LO
)
421 .Case("h", VK_PPC_HI
)
422 .Case("ha", VK_PPC_HA
)
423 .Case("high", VK_PPC_HIGH
)
424 .Case("higha", VK_PPC_HIGHA
)
425 .Case("higher", VK_PPC_HIGHER
)
426 .Case("highera", VK_PPC_HIGHERA
)
427 .Case("highest", VK_PPC_HIGHEST
)
428 .Case("highesta", VK_PPC_HIGHESTA
)
429 .Case("got@l", VK_PPC_GOT_LO
)
430 .Case("got@h", VK_PPC_GOT_HI
)
431 .Case("got@ha", VK_PPC_GOT_HA
)
432 .Case("local", VK_PPC_LOCAL
)
433 .Case("tocbase", VK_PPC_TOCBASE
)
434 .Case("toc", VK_PPC_TOC
)
435 .Case("toc@l", VK_PPC_TOC_LO
)
436 .Case("toc@h", VK_PPC_TOC_HI
)
437 .Case("toc@ha", VK_PPC_TOC_HA
)
440 .Case("tls", VK_PPC_TLS
)
441 .Case("dtpmod", VK_PPC_DTPMOD
)
442 .Case("tprel@l", VK_PPC_TPREL_LO
)
443 .Case("tprel@h", VK_PPC_TPREL_HI
)
444 .Case("tprel@ha", VK_PPC_TPREL_HA
)
445 .Case("tprel@high", VK_PPC_TPREL_HIGH
)
446 .Case("tprel@higha", VK_PPC_TPREL_HIGHA
)
447 .Case("tprel@higher", VK_PPC_TPREL_HIGHER
)
448 .Case("tprel@highera", VK_PPC_TPREL_HIGHERA
)
449 .Case("tprel@highest", VK_PPC_TPREL_HIGHEST
)
450 .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA
)
451 .Case("dtprel@l", VK_PPC_DTPREL_LO
)
452 .Case("dtprel@h", VK_PPC_DTPREL_HI
)
453 .Case("dtprel@ha", VK_PPC_DTPREL_HA
)
454 .Case("dtprel@high", VK_PPC_DTPREL_HIGH
)
455 .Case("dtprel@higha", VK_PPC_DTPREL_HIGHA
)
456 .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER
)
457 .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA
)
458 .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST
)
459 .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA
)
460 .Case("got@tprel", VK_PPC_GOT_TPREL
)
461 .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO
)
462 .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI
)
463 .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA
)
464 .Case("got@dtprel", VK_PPC_GOT_DTPREL
)
465 .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO
)
466 .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI
)
467 .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA
)
468 .Case("got@tlsgd", VK_PPC_GOT_TLSGD
)
469 .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO
)
470 .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI
)
471 .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA
)
472 .Case("got@tlsld", VK_PPC_GOT_TLSLD
)
473 .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO
)
474 .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI
)
475 .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA
)
476 .Case("got@pcrel", VK_PPC_GOT_PCREL
)
477 .Case("got@tlsgd@pcrel", VK_PPC_GOT_TLSGD_PCREL
)
478 .Case("got@tlsld@pcrel", VK_PPC_GOT_TLSLD_PCREL
)
479 .Case("got@tprel@pcrel", VK_PPC_GOT_TPREL_PCREL
)
480 .Case("tls@pcrel", VK_PPC_TLS_PCREL
)
481 .Case("notoc", VK_PPC_NOTOC
)
482 .Case("gdgot", VK_Hexagon_GD_GOT
)
483 .Case("gdplt", VK_Hexagon_GD_PLT
)
484 .Case("iegot", VK_Hexagon_IE_GOT
)
485 .Case("ie", VK_Hexagon_IE
)
486 .Case("ldgot", VK_Hexagon_LD_GOT
)
487 .Case("ldplt", VK_Hexagon_LD_PLT
)
488 .Case("none", VK_ARM_NONE
)
489 .Case("got_prel", VK_ARM_GOT_PREL
)
490 .Case("target1", VK_ARM_TARGET1
)
491 .Case("target2", VK_ARM_TARGET2
)
492 .Case("prel31", VK_ARM_PREL31
)
493 .Case("sbrel", VK_ARM_SBREL
)
494 .Case("tlsldo", VK_ARM_TLSLDO
)
495 .Case("lo8", VK_AVR_LO8
)
496 .Case("hi8", VK_AVR_HI8
)
497 .Case("hlo8", VK_AVR_HLO8
)
498 .Case("typeindex", VK_WASM_TYPEINDEX
)
499 .Case("tbrel", VK_WASM_TBREL
)
500 .Case("mbrel", VK_WASM_MBREL
)
501 .Case("tlsrel", VK_WASM_TLSREL
)
502 .Case("gotpcrel32@lo", VK_AMDGPU_GOTPCREL32_LO
)
503 .Case("gotpcrel32@hi", VK_AMDGPU_GOTPCREL32_HI
)
504 .Case("rel32@lo", VK_AMDGPU_REL32_LO
)
505 .Case("rel32@hi", VK_AMDGPU_REL32_HI
)
506 .Case("rel64", VK_AMDGPU_REL64
)
507 .Case("abs32@lo", VK_AMDGPU_ABS32_LO
)
508 .Case("abs32@hi", VK_AMDGPU_ABS32_HI
)
509 .Case("hi", VK_VE_HI32
)
510 .Case("lo", VK_VE_LO32
)
511 .Case("pc_hi", VK_VE_PC_HI32
)
512 .Case("pc_lo", VK_VE_PC_LO32
)
513 .Case("got_hi", VK_VE_GOT_HI32
)
514 .Case("got_lo", VK_VE_GOT_LO32
)
515 .Case("gotoff_hi", VK_VE_GOTOFF_HI32
)
516 .Case("gotoff_lo", VK_VE_GOTOFF_LO32
)
517 .Case("plt_hi", VK_VE_PLT_HI32
)
518 .Case("plt_lo", VK_VE_PLT_LO32
)
519 .Case("tls_gd_hi", VK_VE_TLS_GD_HI32
)
520 .Case("tls_gd_lo", VK_VE_TLS_GD_LO32
)
521 .Case("tpoff_hi", VK_VE_TPOFF_HI32
)
522 .Case("tpoff_lo", VK_VE_TPOFF_LO32
)
523 .Default(VK_Invalid
);
528 void MCTargetExpr::anchor() {}
532 bool MCExpr::evaluateAsAbsolute(int64_t &Res
) const {
533 return evaluateAsAbsolute(Res
, nullptr, nullptr, nullptr, false);
536 bool MCExpr::evaluateAsAbsolute(int64_t &Res
,
537 const MCAsmLayout
&Layout
) const {
538 return evaluateAsAbsolute(Res
, &Layout
.getAssembler(), &Layout
, nullptr, false);
541 bool MCExpr::evaluateAsAbsolute(int64_t &Res
,
542 const MCAsmLayout
&Layout
,
543 const SectionAddrMap
&Addrs
) const {
544 // Setting InSet causes us to absolutize differences across sections and that
545 // is what the MachO writer uses Addrs for.
546 return evaluateAsAbsolute(Res
, &Layout
.getAssembler(), &Layout
, &Addrs
, true);
549 bool MCExpr::evaluateAsAbsolute(int64_t &Res
, const MCAssembler
&Asm
) const {
550 return evaluateAsAbsolute(Res
, &Asm
, nullptr, nullptr, false);
553 bool MCExpr::evaluateAsAbsolute(int64_t &Res
, const MCAssembler
*Asm
) const {
554 return evaluateAsAbsolute(Res
, Asm
, nullptr, nullptr, false);
557 bool MCExpr::evaluateKnownAbsolute(int64_t &Res
,
558 const MCAsmLayout
&Layout
) const {
559 return evaluateAsAbsolute(Res
, &Layout
.getAssembler(), &Layout
, nullptr,
563 bool MCExpr::evaluateAsAbsolute(int64_t &Res
, const MCAssembler
*Asm
,
564 const MCAsmLayout
*Layout
,
565 const SectionAddrMap
*Addrs
, bool InSet
) const {
568 // Fast path constants.
569 if (const MCConstantExpr
*CE
= dyn_cast
<MCConstantExpr
>(this)) {
570 Res
= CE
->getValue();
575 evaluateAsRelocatableImpl(Value
, Asm
, Layout
, nullptr, Addrs
, InSet
);
577 // Record the current value.
578 Res
= Value
.getConstant();
580 return IsRelocatable
&& Value
.isAbsolute();
583 /// Helper method for \see EvaluateSymbolAdd().
584 static void AttemptToFoldSymbolOffsetDifference(
585 const MCAssembler
*Asm
, const MCAsmLayout
*Layout
,
586 const SectionAddrMap
*Addrs
, bool InSet
, const MCSymbolRefExpr
*&A
,
587 const MCSymbolRefExpr
*&B
, int64_t &Addend
) {
591 const MCSymbol
&SA
= A
->getSymbol();
592 const MCSymbol
&SB
= B
->getSymbol();
594 if (SA
.isUndefined() || SB
.isUndefined())
597 if (!Asm
->getWriter().isSymbolRefDifferenceFullyResolved(*Asm
, A
, B
, InSet
))
600 auto FinalizeFolding
= [&]() {
601 // Pointers to Thumb symbols need to have their low-bit set to allow
603 if (Asm
->isThumbFunc(&SA
))
606 // If symbol is labeled as micromips, we set low-bit to ensure
607 // correct offset in .gcc_except_table
608 if (Asm
->getBackend().isMicroMips(&SA
))
611 // Clear the symbol expr pointers to indicate we have folded these
616 const MCFragment
*FA
= SA
.getFragment();
617 const MCFragment
*FB
= SB
.getFragment();
618 // If both symbols are in the same fragment, return the difference of their
620 if (FA
== FB
&& !SA
.isVariable() && !SA
.isUnset() && !SB
.isVariable() &&
622 Addend
+= SA
.getOffset() - SB
.getOffset();
623 return FinalizeFolding();
626 const MCSection
&SecA
= *FA
->getParent();
627 const MCSection
&SecB
= *FB
->getParent();
629 if ((&SecA
!= &SecB
) && !Addrs
)
633 // One of the symbol involved is part of a fragment being laid out. Quit now
634 // to avoid a self loop.
635 if (!Layout
->canGetFragmentOffset(FA
) || !Layout
->canGetFragmentOffset(FB
))
638 // Eagerly evaluate when layout is finalized.
639 Addend
+= Layout
->getSymbolOffset(A
->getSymbol()) -
640 Layout
->getSymbolOffset(B
->getSymbol());
641 if (Addrs
&& (&SecA
!= &SecB
))
642 Addend
+= (Addrs
->lookup(&SecA
) - Addrs
->lookup(&SecB
));
646 // When layout is not finalized, our ability to resolve differences between
647 // symbols is limited to specific cases where the fragments between two
648 // symbols (including the fragments the symbols are defined in) are
649 // fixed-size fragments so the difference can be calculated. For example,
650 // this is important when the Subtarget is changed and a new MCDataFragment
651 // is created in the case of foo: instr; .arch_extension ext; instr .if . -
653 if (SA
.isVariable() || SA
.isUnset() || SB
.isVariable() || SB
.isUnset() ||
654 FA
->getKind() != MCFragment::FT_Data
||
655 FB
->getKind() != MCFragment::FT_Data
||
656 FA
->getSubsectionNumber() != FB
->getSubsectionNumber())
658 // Try to find a constant displacement from FA to FB, add the displacement
659 // between the offset in FA of SA and the offset in FB of SB.
660 int64_t Displacement
= SA
.getOffset() - SB
.getOffset();
661 for (auto FI
= FB
->getIterator(), FE
= SecA
.end(); FI
!= FE
; ++FI
) {
663 Addend
+= Displacement
;
664 return FinalizeFolding();
667 if (FI
->getKind() != MCFragment::FT_Data
)
669 Displacement
+= cast
<MCDataFragment
>(FI
)->getContents().size();
674 /// Evaluate the result of an add between (conceptually) two MCValues.
676 /// This routine conceptually attempts to construct an MCValue:
677 /// Result = (Result_A - Result_B + Result_Cst)
678 /// from two MCValue's LHS and RHS where
679 /// Result = LHS + RHS
681 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
683 /// This routine attempts to aggressively fold the operands such that the result
684 /// is representable in an MCValue, but may not always succeed.
686 /// \returns True on success, false if the result is not representable in an
689 /// NOTE: It is really important to have both the Asm and Layout arguments.
690 /// They might look redundant, but this function can be used before layout
691 /// is done (see the object streamer for example) and having the Asm argument
692 /// lets us avoid relaxations early.
694 EvaluateSymbolicAdd(const MCAssembler
*Asm
, const MCAsmLayout
*Layout
,
695 const SectionAddrMap
*Addrs
, bool InSet
, const MCValue
&LHS
,
696 const MCSymbolRefExpr
*RHS_A
, const MCSymbolRefExpr
*RHS_B
,
697 int64_t RHS_Cst
, MCValue
&Res
) {
698 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
699 // about dealing with modifiers. This will ultimately bite us, one day.
700 const MCSymbolRefExpr
*LHS_A
= LHS
.getSymA();
701 const MCSymbolRefExpr
*LHS_B
= LHS
.getSymB();
702 int64_t LHS_Cst
= LHS
.getConstant();
704 // Fold the result constant immediately.
705 int64_t Result_Cst
= LHS_Cst
+ RHS_Cst
;
707 assert((!Layout
|| Asm
) &&
708 "Must have an assembler object if layout is given!");
710 // If we have a layout, we can fold resolved differences.
712 // First, fold out any differences which are fully resolved. By
713 // reassociating terms in
714 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
715 // we have the four possible differences:
720 // Since we are attempting to be as aggressive as possible about folding, we
721 // attempt to evaluate each possible alternative.
722 AttemptToFoldSymbolOffsetDifference(Asm
, Layout
, Addrs
, InSet
, LHS_A
, LHS_B
,
724 AttemptToFoldSymbolOffsetDifference(Asm
, Layout
, Addrs
, InSet
, LHS_A
, RHS_B
,
726 AttemptToFoldSymbolOffsetDifference(Asm
, Layout
, Addrs
, InSet
, RHS_A
, LHS_B
,
728 AttemptToFoldSymbolOffsetDifference(Asm
, Layout
, Addrs
, InSet
, RHS_A
, RHS_B
,
732 // We can't represent the addition or subtraction of two symbols.
733 if ((LHS_A
&& RHS_A
) || (LHS_B
&& RHS_B
))
736 // At this point, we have at most one additive symbol and one subtractive
737 // symbol -- find them.
738 const MCSymbolRefExpr
*A
= LHS_A
? LHS_A
: RHS_A
;
739 const MCSymbolRefExpr
*B
= LHS_B
? LHS_B
: RHS_B
;
741 Res
= MCValue::get(A
, B
, Result_Cst
);
745 bool MCExpr::evaluateAsRelocatable(MCValue
&Res
,
746 const MCAsmLayout
*Layout
,
747 const MCFixup
*Fixup
) const {
748 MCAssembler
*Assembler
= Layout
? &Layout
->getAssembler() : nullptr;
749 return evaluateAsRelocatableImpl(Res
, Assembler
, Layout
, Fixup
, nullptr,
753 bool MCExpr::evaluateAsValue(MCValue
&Res
, const MCAsmLayout
&Layout
) const {
754 MCAssembler
*Assembler
= &Layout
.getAssembler();
755 return evaluateAsRelocatableImpl(Res
, Assembler
, &Layout
, nullptr, nullptr,
759 static bool canExpand(const MCSymbol
&Sym
, bool InSet
) {
760 const MCExpr
*Expr
= Sym
.getVariableValue();
761 const auto *Inner
= dyn_cast
<MCSymbolRefExpr
>(Expr
);
763 if (Inner
->getKind() == MCSymbolRefExpr::VK_WEAKREF
)
769 return !Sym
.isInSection();
772 bool MCExpr::evaluateAsRelocatableImpl(MCValue
&Res
, const MCAssembler
*Asm
,
773 const MCAsmLayout
*Layout
,
774 const MCFixup
*Fixup
,
775 const SectionAddrMap
*Addrs
,
777 ++stats::MCExprEvaluate
;
781 return cast
<MCTargetExpr
>(this)->evaluateAsRelocatableImpl(Res
, Layout
,
785 Res
= MCValue::get(cast
<MCConstantExpr
>(this)->getValue());
789 const MCSymbolRefExpr
*SRE
= cast
<MCSymbolRefExpr
>(this);
790 const MCSymbol
&Sym
= SRE
->getSymbol();
791 const auto Kind
= SRE
->getKind();
793 // Evaluate recursively if this is a variable.
794 if (Sym
.isVariable() && (Kind
== MCSymbolRefExpr::VK_None
|| Layout
) &&
795 canExpand(Sym
, InSet
)) {
796 bool IsMachO
= SRE
->hasSubsectionsViaSymbols();
797 if (Sym
.getVariableValue()->evaluateAsRelocatableImpl(
798 Res
, Asm
, Layout
, Fixup
, Addrs
, InSet
|| IsMachO
)) {
799 if (Kind
!= MCSymbolRefExpr::VK_None
) {
800 if (Res
.isAbsolute()) {
801 Res
= MCValue::get(SRE
, nullptr, 0);
804 // If the reference has a variant kind, we can only handle expressions
805 // which evaluate exactly to a single unadorned symbol. Attach the
806 // original VariantKind to SymA of the result.
807 if (Res
.getRefKind() != MCSymbolRefExpr::VK_None
|| !Res
.getSymA() ||
808 Res
.getSymB() || Res
.getConstant())
811 MCValue::get(MCSymbolRefExpr::create(&Res
.getSymA()->getSymbol(),
812 Kind
, Asm
->getContext()),
813 Res
.getSymB(), Res
.getConstant(), Res
.getRefKind());
818 const MCSymbolRefExpr
*A
= Res
.getSymA();
819 const MCSymbolRefExpr
*B
= Res
.getSymB();
820 // FIXME: This is small hack. Given
823 // the OS X assembler will completely drop the 4. We should probably
824 // include it in the relocation or produce an error if that is not
826 // Allow constant expressions.
829 // Allows aliases with zero offset.
830 if (Res
.getConstant() == 0 && (!A
|| !B
))
835 Res
= MCValue::get(SRE
, nullptr, 0);
840 const MCUnaryExpr
*AUE
= cast
<MCUnaryExpr
>(this);
843 if (!AUE
->getSubExpr()->evaluateAsRelocatableImpl(Value
, Asm
, Layout
, Fixup
,
847 switch (AUE
->getOpcode()) {
848 case MCUnaryExpr::LNot
:
849 if (!Value
.isAbsolute())
851 Res
= MCValue::get(!Value
.getConstant());
853 case MCUnaryExpr::Minus
:
854 /// -(a - b + const) ==> (b - a - const)
855 if (Value
.getSymA() && !Value
.getSymB())
858 // The cast avoids undefined behavior if the constant is INT64_MIN.
859 Res
= MCValue::get(Value
.getSymB(), Value
.getSymA(),
860 -(uint64_t)Value
.getConstant());
862 case MCUnaryExpr::Not
:
863 if (!Value
.isAbsolute())
865 Res
= MCValue::get(~Value
.getConstant());
867 case MCUnaryExpr::Plus
:
876 const MCBinaryExpr
*ABE
= cast
<MCBinaryExpr
>(this);
877 MCValue LHSValue
, RHSValue
;
879 if (!ABE
->getLHS()->evaluateAsRelocatableImpl(LHSValue
, Asm
, Layout
, Fixup
,
881 !ABE
->getRHS()->evaluateAsRelocatableImpl(RHSValue
, Asm
, Layout
, Fixup
,
883 // Check if both are Target Expressions, see if we can compare them.
884 if (const MCTargetExpr
*L
= dyn_cast
<MCTargetExpr
>(ABE
->getLHS()))
885 if (const MCTargetExpr
*R
= cast
<MCTargetExpr
>(ABE
->getRHS())) {
886 switch (ABE
->getOpcode()) {
887 case MCBinaryExpr::EQ
:
888 Res
= MCValue::get((L
->isEqualTo(R
)) ? -1 : 0);
890 case MCBinaryExpr::NE
:
891 Res
= MCValue::get((R
->isEqualTo(R
)) ? 0 : -1);
899 // We only support a few operations on non-constant expressions, handle
901 if (!LHSValue
.isAbsolute() || !RHSValue
.isAbsolute()) {
902 switch (ABE
->getOpcode()) {
905 case MCBinaryExpr::Sub
:
906 // Negate RHS and add.
907 // The cast avoids undefined behavior if the constant is INT64_MIN.
908 return EvaluateSymbolicAdd(Asm
, Layout
, Addrs
, InSet
, LHSValue
,
909 RHSValue
.getSymB(), RHSValue
.getSymA(),
910 -(uint64_t)RHSValue
.getConstant(), Res
);
912 case MCBinaryExpr::Add
:
913 return EvaluateSymbolicAdd(Asm
, Layout
, Addrs
, InSet
, LHSValue
,
914 RHSValue
.getSymA(), RHSValue
.getSymB(),
915 RHSValue
.getConstant(), Res
);
919 // FIXME: We need target hooks for the evaluation. It may be limited in
920 // width, and gas defines the result of comparisons differently from
922 int64_t LHS
= LHSValue
.getConstant(), RHS
= RHSValue
.getConstant();
924 auto Op
= ABE
->getOpcode();
926 case MCBinaryExpr::AShr
: Result
= LHS
>> RHS
; break;
927 case MCBinaryExpr::Add
: Result
= LHS
+ RHS
; break;
928 case MCBinaryExpr::And
: Result
= LHS
& RHS
; break;
929 case MCBinaryExpr::Div
:
930 case MCBinaryExpr::Mod
:
931 // Handle division by zero. gas just emits a warning and keeps going,
932 // we try to be stricter.
933 // FIXME: Currently the caller of this function has no way to understand
934 // we're bailing out because of 'division by zero'. Therefore, it will
935 // emit a 'expected relocatable expression' error. It would be nice to
936 // change this code to emit a better diagnostic.
939 if (ABE
->getOpcode() == MCBinaryExpr::Div
)
944 case MCBinaryExpr::EQ
: Result
= LHS
== RHS
; break;
945 case MCBinaryExpr::GT
: Result
= LHS
> RHS
; break;
946 case MCBinaryExpr::GTE
: Result
= LHS
>= RHS
; break;
947 case MCBinaryExpr::LAnd
: Result
= LHS
&& RHS
; break;
948 case MCBinaryExpr::LOr
: Result
= LHS
|| RHS
; break;
949 case MCBinaryExpr::LShr
: Result
= uint64_t(LHS
) >> uint64_t(RHS
); break;
950 case MCBinaryExpr::LT
: Result
= LHS
< RHS
; break;
951 case MCBinaryExpr::LTE
: Result
= LHS
<= RHS
; break;
952 case MCBinaryExpr::Mul
: Result
= LHS
* RHS
; break;
953 case MCBinaryExpr::NE
: Result
= LHS
!= RHS
; break;
954 case MCBinaryExpr::Or
: Result
= LHS
| RHS
; break;
955 case MCBinaryExpr::OrNot
: Result
= LHS
| ~RHS
; break;
956 case MCBinaryExpr::Shl
: Result
= uint64_t(LHS
) << uint64_t(RHS
); break;
957 case MCBinaryExpr::Sub
: Result
= LHS
- RHS
; break;
958 case MCBinaryExpr::Xor
: Result
= LHS
^ RHS
; break;
963 Res
= MCValue::get(Result
);
965 case MCBinaryExpr::EQ
:
966 case MCBinaryExpr::GT
:
967 case MCBinaryExpr::GTE
:
968 case MCBinaryExpr::LT
:
969 case MCBinaryExpr::LTE
:
970 case MCBinaryExpr::NE
:
971 // A comparison operator returns a -1 if true and 0 if false.
972 Res
= MCValue::get(Result
? -1 : 0);
980 llvm_unreachable("Invalid assembly expression kind!");
983 MCFragment
*MCExpr::findAssociatedFragment() const {
986 // We never look through target specific expressions.
987 return cast
<MCTargetExpr
>(this)->findAssociatedFragment();
990 return MCSymbol::AbsolutePseudoFragment
;
993 const MCSymbolRefExpr
*SRE
= cast
<MCSymbolRefExpr
>(this);
994 const MCSymbol
&Sym
= SRE
->getSymbol();
995 return Sym
.getFragment();
999 return cast
<MCUnaryExpr
>(this)->getSubExpr()->findAssociatedFragment();
1002 const MCBinaryExpr
*BE
= cast
<MCBinaryExpr
>(this);
1003 MCFragment
*LHS_F
= BE
->getLHS()->findAssociatedFragment();
1004 MCFragment
*RHS_F
= BE
->getRHS()->findAssociatedFragment();
1006 // If either is absolute, return the other.
1007 if (LHS_F
== MCSymbol::AbsolutePseudoFragment
)
1009 if (RHS_F
== MCSymbol::AbsolutePseudoFragment
)
1012 // Not always correct, but probably the best we can do without more context.
1013 if (BE
->getOpcode() == MCBinaryExpr::Sub
)
1014 return MCSymbol::AbsolutePseudoFragment
;
1016 // Otherwise, return the first non-null fragment.
1017 return LHS_F
? LHS_F
: RHS_F
;
1021 llvm_unreachable("Invalid assembly expression kind!");