[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / MC / MCExpr.cpp
blob84ec0f6bb57ba5861f84d0ca560443fa1c7e5f91
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/StringExtras.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCAsmLayout.h"
17 #include "llvm/MC/MCAssembler.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCValue.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <cassert>
28 #include <cstdint>
30 using namespace llvm;
32 #define DEBUG_TYPE "mcexpr"
34 namespace {
35 namespace stats {
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 {
43 switch (getKind()) {
44 case MCExpr::Target:
45 return cast<MCTargetExpr>(this)->printImpl(OS, MAI);
46 case MCExpr::Constant: {
47 auto Value = cast<MCConstantExpr>(*this).getValue();
48 auto PrintInHex = cast<MCConstantExpr>(*this).useHexFormat();
49 auto SizeInBytes = cast<MCConstantExpr>(*this).getSizeInBytes();
50 if (Value < 0 && MAI && !MAI->supportsSignedData())
51 PrintInHex = true;
52 if (PrintInHex)
53 switch (SizeInBytes) {
54 default:
55 OS << "0x" << Twine::utohexstr(Value);
56 break;
57 case 1:
58 OS << format("0x%02" PRIx64, Value);
59 break;
60 case 2:
61 OS << format("0x%04" PRIx64, Value);
62 break;
63 case 4:
64 OS << format("0x%08" PRIx64, Value);
65 break;
66 case 8:
67 OS << format("0x%016" PRIx64, Value);
68 break;
70 else
71 OS << Value;
72 return;
74 case MCExpr::SymbolRef: {
75 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
76 const MCSymbol &Sym = SRE.getSymbol();
77 // Parenthesize names that start with $ so that they don't look like
78 // absolute names.
79 bool UseParens =
80 !InParens && !Sym.getName().empty() && Sym.getName()[0] == '$';
81 if (UseParens) {
82 OS << '(';
83 Sym.print(OS, MAI);
84 OS << ')';
85 } else
86 Sym.print(OS, MAI);
88 const MCSymbolRefExpr::VariantKind Kind = SRE.getKind();
89 if (Kind != MCSymbolRefExpr::VK_None) {
90 if (MAI && MAI->useParensForSymbolVariant()) // ARM
91 OS << '(' << MCSymbolRefExpr::getVariantKindName(Kind) << ')';
92 else
93 OS << '@' << MCSymbolRefExpr::getVariantKindName(Kind);
96 return;
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;
107 bool Binary = UE.getSubExpr()->getKind() == MCExpr::Binary;
108 if (Binary) OS << "(";
109 UE.getSubExpr()->print(OS, MAI);
110 if (Binary) OS << ")";
111 return;
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);
120 } else {
121 OS << '(';
122 BE.getLHS()->print(OS, MAI);
123 OS << ')';
126 switch (BE.getOpcode()) {
127 case MCBinaryExpr::Add:
128 // Print "X-42" instead of "X+-42".
129 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
130 if (RHSC->getValue() < 0) {
131 OS << RHSC->getValue();
132 return;
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;
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 << ')';
167 return;
171 llvm_unreachable("Invalid expression kind!");
174 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
175 LLVM_DUMP_METHOD void MCExpr::dump() const {
176 dbgs() << *this;
177 dbgs() << '\n';
179 #endif
181 /* *** */
183 const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
184 const MCExpr *RHS, MCContext &Ctx,
185 SMLoc Loc) {
186 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc);
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,
195 bool PrintInHex,
196 unsigned SizeInBytes) {
197 return new (Ctx) MCConstantExpr(Value, PrintInHex, SizeInBytes);
200 /* *** */
202 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
203 const MCAsmInfo *MAI, SMLoc Loc)
204 : MCExpr(MCExpr::SymbolRef, Loc,
205 encodeSubclassData(Kind, MAI->hasSubsectionsViaSymbols())),
206 Symbol(Symbol) {
207 assert(Symbol);
210 const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
211 VariantKind Kind,
212 MCContext &Ctx, SMLoc Loc) {
213 return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc);
216 const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
217 MCContext &Ctx) {
218 return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
221 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
222 switch (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:
326 return "gd";
327 case VK_PPC_AIX_TLSGDM:
328 return "m";
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:
334 return "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:
342 return "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)
391 .Case("got", VK_GOT)
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)
400 .Case("plt", VK_PLT)
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)
438 .Case("u", VK_PPC_U)
439 .Case("l", VK_PPC_L)
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);
526 /* *** */
528 void MCTargetExpr::anchor() {}
530 /* *** */
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,
560 true);
563 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
564 const MCAsmLayout *Layout,
565 const SectionAddrMap *Addrs, bool InSet) const {
566 MCValue Value;
568 // Fast path constants.
569 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
570 Res = CE->getValue();
571 return true;
574 bool IsRelocatable =
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) {
588 if (!A || !B)
589 return;
591 const MCSymbol &SA = A->getSymbol();
592 const MCSymbol &SB = B->getSymbol();
594 if (SA.isUndefined() || SB.isUndefined())
595 return;
597 if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
598 return;
600 auto FinalizeFolding = [&]() {
601 // Pointers to Thumb symbols need to have their low-bit set to allow
602 // for interworking.
603 if (Asm->isThumbFunc(&SA))
604 Addend |= 1;
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))
609 Addend |= 1;
611 // Clear the symbol expr pointers to indicate we have folded these
612 // operands.
613 A = B = nullptr;
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
619 // offsets
620 if (FA == FB && !SA.isVariable() && !SA.isUnset() && !SB.isVariable() &&
621 !SB.isUnset()) {
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)
630 return;
632 if (Layout) {
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))
636 return;
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));
644 FinalizeFolding();
645 } else {
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 . -
652 // foo.
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())
657 return;
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) {
662 if (&*FI == FA) {
663 Addend += Displacement;
664 return FinalizeFolding();
667 if (FI->getKind() != MCFragment::FT_Data)
668 return;
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
680 /// and
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
687 /// MCValue.
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.
693 static bool
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.
711 if (Asm) {
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:
716 // (LHS_A - LHS_B),
717 // (LHS_A - RHS_B),
718 // (RHS_A - LHS_B),
719 // (RHS_A - RHS_B).
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,
723 Result_Cst);
724 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
725 Result_Cst);
726 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
727 Result_Cst);
728 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
729 Result_Cst);
732 // We can't represent the addition or subtraction of two symbols.
733 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
734 return false;
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);
742 return true;
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,
750 false);
753 bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
754 MCAssembler *Assembler = &Layout.getAssembler();
755 return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
756 true);
759 static bool canExpand(const MCSymbol &Sym, bool InSet) {
760 const MCExpr *Expr = Sym.getVariableValue();
761 const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
762 if (Inner) {
763 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
764 return false;
767 if (InSet)
768 return true;
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,
776 bool InSet) const {
777 ++stats::MCExprEvaluate;
779 switch (getKind()) {
780 case Target:
781 return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
782 Fixup);
784 case Constant:
785 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
786 return true;
788 case SymbolRef: {
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);
802 return true;
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())
809 return false;
810 Res =
811 MCValue::get(MCSymbolRefExpr::create(&Res.getSymA()->getSymbol(),
812 Kind, Asm->getContext()),
813 Res.getSymB(), Res.getConstant(), Res.getRefKind());
815 if (!IsMachO)
816 return true;
818 const MCSymbolRefExpr *A = Res.getSymA();
819 const MCSymbolRefExpr *B = Res.getSymB();
820 // FIXME: This is small hack. Given
821 // a = b + 4
822 // .long a
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
825 // possible.
826 // Allow constant expressions.
827 if (!A && !B)
828 return true;
829 // Allows aliases with zero offset.
830 if (Res.getConstant() == 0 && (!A || !B))
831 return true;
835 Res = MCValue::get(SRE, nullptr, 0);
836 return true;
839 case Unary: {
840 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
841 MCValue Value;
843 if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
844 Addrs, InSet))
845 return false;
847 switch (AUE->getOpcode()) {
848 case MCUnaryExpr::LNot:
849 if (!Value.isAbsolute())
850 return false;
851 Res = MCValue::get(!Value.getConstant());
852 break;
853 case MCUnaryExpr::Minus:
854 /// -(a - b + const) ==> (b - a - const)
855 if (Value.getSymA() && !Value.getSymB())
856 return false;
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());
861 break;
862 case MCUnaryExpr::Not:
863 if (!Value.isAbsolute())
864 return false;
865 Res = MCValue::get(~Value.getConstant());
866 break;
867 case MCUnaryExpr::Plus:
868 Res = Value;
869 break;
872 return true;
875 case Binary: {
876 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
877 MCValue LHSValue, RHSValue;
879 if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
880 Addrs, InSet) ||
881 !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
882 Addrs, InSet)) {
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);
889 return true;
890 case MCBinaryExpr::NE:
891 Res = MCValue::get((R->isEqualTo(R)) ? 0 : -1);
892 return true;
893 default: break;
896 return false;
899 // We only support a few operations on non-constant expressions, handle
900 // those first.
901 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
902 switch (ABE->getOpcode()) {
903 default:
904 return false;
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
921 // Apple as.
922 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
923 int64_t Result = 0;
924 auto Op = ABE->getOpcode();
925 switch (Op) {
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.
937 if (RHS == 0)
938 return false;
939 if (ABE->getOpcode() == MCBinaryExpr::Div)
940 Result = LHS / RHS;
941 else
942 Result = LHS % RHS;
943 break;
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;
961 switch (Op) {
962 default:
963 Res = MCValue::get(Result);
964 break;
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);
973 break;
976 return true;
980 llvm_unreachable("Invalid assembly expression kind!");
983 MCFragment *MCExpr::findAssociatedFragment() const {
984 switch (getKind()) {
985 case Target:
986 // We never look through target specific expressions.
987 return cast<MCTargetExpr>(this)->findAssociatedFragment();
989 case Constant:
990 return MCSymbol::AbsolutePseudoFragment;
992 case SymbolRef: {
993 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
994 const MCSymbol &Sym = SRE->getSymbol();
995 return Sym.getFragment();
998 case Unary:
999 return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
1001 case Binary: {
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)
1008 return RHS_F;
1009 if (RHS_F == MCSymbol::AbsolutePseudoFragment)
1010 return LHS_F;
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!");