Bug Summary

File:llvm/lib/MC/MCExpr.cpp
Warning:line 626, column 28
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name MCExpr.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/build-llvm/lib/MC -resource-dir /usr/lib/llvm-13/lib/clang/13.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/build-llvm/lib/MC -I /build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC -I /build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/build-llvm/include -I /build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-13/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/build-llvm/lib/MC -fdebug-prefix-map=/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-06-13-111025-38230-1 -x c++ /build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC/MCExpr.cpp

/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC/MCExpr.cpp

1//===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/MC/MCExpr.h"
10#include "llvm/ADT/Statistic.h"
11#include "llvm/ADT/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>
29
30using namespace llvm;
31
32#define DEBUG_TYPE"mcexpr" "mcexpr"
33
34namespace {
35namespace stats {
36
37STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations")static llvm::Statistic MCExprEvaluate = {"mcexpr", "MCExprEvaluate"
, "Number of MCExpr evaluations"}
;
38
39} // end namespace stats
40} // end anonymous namespace
41
42void 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"l" "x", Value);
59 break;
60 case 2:
61 OS << format("0x%04" PRIx64"l" "x", Value);
62 break;
63 case 4:
64 OS << format("0x%08" PRIx64"l" "x", Value);
65 break;
66 case 8:
67 OS << format("0x%016" PRIx64"l" "x", Value);
68 break;
69 }
70 else
71 OS << Value;
72 return;
73 }
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);
87
88 const MCSymbolRefExpr::VariantKind Kind = SRE.getKind();
89 if (Kind != MCSymbolRefExpr::VK_None) {
90 if (MAI && MAI->useParensForSymbolVariant()) // ARM
91 OS << '(' << MCSymbolRefExpr::getVariantKindName(Kind) << ')';
92 else
93 OS << '@' << MCSymbolRefExpr::getVariantKindName(Kind);
94 }
95
96 return;
97 }
98
99 case MCExpr::Unary: {
100 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
101 switch (UE.getOpcode()) {
102 case MCUnaryExpr::LNot: OS << '!'; break;
103 case MCUnaryExpr::Minus: OS << '-'; break;
104 case MCUnaryExpr::Not: OS << '~'; break;
105 case MCUnaryExpr::Plus: OS << '+'; break;
106 }
107 bool Binary = UE.getSubExpr()->getKind() == MCExpr::Binary;
108 if (Binary) OS << "(";
109 UE.getSubExpr()->print(OS, MAI);
110 if (Binary) OS << ")";
111 return;
112 }
113
114 case MCExpr::Binary: {
115 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
116
117 // Only print parens around the LHS if it is non-trivial.
118 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
119 BE.getLHS()->print(OS, MAI);
120 } else {
121 OS << '(';
122 BE.getLHS()->print(OS, MAI);
123 OS << ')';
124 }
125
126 switch (BE.getOpcode()) {
127 case MCBinaryExpr::Add:
128 // Print "X-42" instead of "X+-42".
129 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
130 if (RHSC->getValue() < 0) {
131 OS << RHSC->getValue();
132 return;
133 }
134 }
135
136 OS << '+';
137 break;
138 case MCBinaryExpr::AShr: OS << ">>"; break;
139 case MCBinaryExpr::And: OS << '&'; break;
140 case MCBinaryExpr::Div: OS << '/'; break;
141 case MCBinaryExpr::EQ: OS << "=="; break;
142 case MCBinaryExpr::GT: OS << '>'; break;
143 case MCBinaryExpr::GTE: OS << ">="; break;
144 case MCBinaryExpr::LAnd: OS << "&&"; break;
145 case MCBinaryExpr::LOr: OS << "||"; break;
146 case MCBinaryExpr::LShr: OS << ">>"; break;
147 case MCBinaryExpr::LT: OS << '<'; break;
148 case MCBinaryExpr::LTE: OS << "<="; break;
149 case MCBinaryExpr::Mod: OS << '%'; break;
150 case MCBinaryExpr::Mul: OS << '*'; break;
151 case MCBinaryExpr::NE: OS << "!="; break;
152 case MCBinaryExpr::Or: OS << '|'; break;
153 case MCBinaryExpr::OrNot: OS << '!'; break;
154 case MCBinaryExpr::Shl: OS << "<<"; break;
155 case MCBinaryExpr::Sub: OS << '-'; break;
156 case MCBinaryExpr::Xor: OS << '^'; break;
157 }
158
159 // Only print parens around the LHS if it is non-trivial.
160 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
161 BE.getRHS()->print(OS, MAI);
162 } else {
163 OS << '(';
164 BE.getRHS()->print(OS, MAI);
165 OS << ')';
166 }
167 return;
168 }
169 }
170
171 llvm_unreachable("Invalid expression kind!")::llvm::llvm_unreachable_internal("Invalid expression kind!",
"/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC/MCExpr.cpp"
, 171)
;
172}
173
174#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
175LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MCExpr::dump() const {
176 dbgs() << *this;
177 dbgs() << '\n';
178}
179#endif
180
181/* *** */
182
183const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
184 const MCExpr *RHS, MCContext &Ctx,
185 SMLoc Loc) {
186 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc);
187}
188
189const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
190 MCContext &Ctx, SMLoc Loc) {
191 return new (Ctx) MCUnaryExpr(Opc, Expr, Loc);
192}
193
194const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx,
195 bool PrintInHex,
196 unsigned SizeInBytes) {
197 return new (Ctx) MCConstantExpr(Value, PrintInHex, SizeInBytes);
198}
199
200/* *** */
201
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)(static_cast <bool> (Symbol) ? void (0) : __assert_fail
("Symbol", "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC/MCExpr.cpp"
, 207, __extension__ __PRETTY_FUNCTION__))
;
208}
209
210const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
211 VariantKind Kind,
212 MCContext &Ctx, SMLoc Loc) {
213 return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc);
214}
215
216const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
217 MCContext &Ctx) {
218 return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
219}
220
221StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
222 switch (Kind) {
223 case VK_Invalid: return "<<invalid>>";
224 case VK_None: return "<<none>>";
225
226 case VK_DTPOFF: return "DTPOFF";
227 case VK_DTPREL: return "DTPREL";
228 case VK_GOT: return "GOT";
229 case VK_GOTOFF: return "GOTOFF";
230 case VK_GOTREL: return "GOTREL";
231 case VK_PCREL: return "PCREL";
232 case VK_GOTPCREL: return "GOTPCREL";
233 case VK_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";
382 }
383 llvm_unreachable("Invalid variant kind")::llvm::llvm_unreachable_internal("Invalid variant kind", "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC/MCExpr.cpp"
, 383)
;
384}
385
386MCSymbolRefExpr::VariantKind
387MCSymbolRefExpr::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);
524}
525
526/* *** */
527
528void MCTargetExpr::anchor() {}
529
530/* *** */
531
532bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
533 return evaluateAsAbsolute(Res, nullptr, nullptr, nullptr, false);
534}
535
536bool MCExpr::evaluateAsAbsolute(int64_t &Res,
537 const MCAsmLayout &Layout) const {
538 return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, false);
539}
540
541bool 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);
547}
548
549bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
550 return evaluateAsAbsolute(Res, &Asm, nullptr, nullptr, false);
551}
552
553bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const {
554 return evaluateAsAbsolute(Res, Asm, nullptr, nullptr, false);
555}
556
557bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
558 const MCAsmLayout &Layout) const {
559 return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
560 true);
561}
562
563bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
564 const MCAsmLayout *Layout,
565 const SectionAddrMap *Addrs, bool InSet) const {
566 MCValue Value;
567
568 // Fast path constants.
569 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
570 Res = CE->getValue();
571 return true;
572 }
573
574 bool IsRelocatable =
575 evaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
576
577 // Record the current value.
578 Res = Value.getConstant();
579
580 return IsRelocatable && Value.isAbsolute();
581}
582
583/// Helper method for \see EvaluateSymbolAdd().
584static 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
23.1
'A' is non-null
23.1
'A' is non-null
23.1
'A' is non-null
23.1
'A' is non-null
|| !B)
24
Assuming 'B' is non-null
25
Taking false branch
589 return;
590
591 const MCSymbol &SA = A->getSymbol();
592 const MCSymbol &SB = B->getSymbol();
593
594 if (SA.isUndefined() || SB.isUndefined())
26
Calling 'MCSymbol::isUndefined'
29
Returning from 'MCSymbol::isUndefined'
30
Calling 'MCSymbol::isUndefined'
32
Returning from 'MCSymbol::isUndefined'
33
Taking false branch
595 return;
596
597 if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
34
Assuming the condition is false
35
Taking false branch
598 return;
599
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;
605
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;
610
611 // Clear the symbol expr pointers to indicate we have folded these
612 // operands.
613 A = B = nullptr;
614 };
615
616 const MCFragment *FA = SA.getFragment();
36
Calling 'MCSymbol::getFragment'
60
Returning from 'MCSymbol::getFragment'
61
'FA' initialized here
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() &&
62
Assuming 'FA' is equal to 'FB'
63
Calling 'MCSymbol::isVariable'
65
Returning from 'MCSymbol::isVariable'
621 !SB.isUnset()) {
622 Addend += SA.getOffset() - SB.getOffset();
623 return FinalizeFolding();
624 }
625
626 const MCSection &SecA = *FA->getParent();
66
Called C++ object pointer is null
627 const MCSection &SecB = *FB->getParent();
628
629 if ((&SecA != &SecB) && !Addrs)
630 return;
631
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;
637
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));
643
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();
665 }
666
667 if (FI->getKind() != MCFragment::FT_Data)
668 return;
669 Displacement += cast<MCDataFragment>(FI)->getContents().size();
670 }
671 }
672}
673
674static bool canFold(const MCAssembler *Asm, const MCSymbolRefExpr *A,
675 const MCSymbolRefExpr *B, bool InSet) {
676 if (InSet)
16
Assuming 'InSet' is false
17
Taking false branch
677 return true;
678
679 if (!Asm->getBackend().requiresDiffExpressionRelocations())
18
Assuming the condition is true
19
Taking true branch
680 return true;
20
Returning the value 1, which participates in a condition later
681
682 const MCSymbol &CheckSym = A ? A->getSymbol() : B->getSymbol();
683 if (!CheckSym.isInSection())
684 return true;
685
686 if (!CheckSym.getSection().hasInstructions())
687 return true;
688
689 return false;
690}
691
692/// Evaluate the result of an add between (conceptually) two MCValues.
693///
694/// This routine conceptually attempts to construct an MCValue:
695/// Result = (Result_A - Result_B + Result_Cst)
696/// from two MCValue's LHS and RHS where
697/// Result = LHS + RHS
698/// and
699/// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
700///
701/// This routine attempts to aggressively fold the operands such that the result
702/// is representable in an MCValue, but may not always succeed.
703///
704/// \returns True on success, false if the result is not representable in an
705/// MCValue.
706
707/// NOTE: It is really important to have both the Asm and Layout arguments.
708/// They might look redundant, but this function can be used before layout
709/// is done (see the object streamer for example) and having the Asm argument
710/// lets us avoid relaxations early.
711static bool
712EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
713 const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
714 const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
715 int64_t RHS_Cst, MCValue &Res) {
716 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
717 // about dealing with modifiers. This will ultimately bite us, one day.
718 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
719 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
720 int64_t LHS_Cst = LHS.getConstant();
721
722 // Fold the result constant immediately.
723 int64_t Result_Cst = LHS_Cst + RHS_Cst;
724
725 assert((!Layout || Asm) &&(static_cast <bool> ((!Layout || Asm) && "Must have an assembler object if layout is given!"
) ? void (0) : __assert_fail ("(!Layout || Asm) && \"Must have an assembler object if layout is given!\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC/MCExpr.cpp"
, 726, __extension__ __PRETTY_FUNCTION__))
12
Assuming 'Layout' is non-null
13
Assuming 'Asm' is non-null
14
'?' condition is true
726 "Must have an assembler object if layout is given!")(static_cast <bool> ((!Layout || Asm) && "Must have an assembler object if layout is given!"
) ? void (0) : __assert_fail ("(!Layout || Asm) && \"Must have an assembler object if layout is given!\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC/MCExpr.cpp"
, 726, __extension__ __PRETTY_FUNCTION__))
;
727
728 // If we have a layout, we can fold resolved differences. Do not do this if
729 // the backend requires this to be emitted as individual relocations, unless
730 // the InSet flag is set to get the current difference anyway (used for
731 // example to calculate symbol sizes).
732 if (Asm
14.1
'Asm' is non-null
14.1
'Asm' is non-null
14.1
'Asm' is non-null
14.1
'Asm' is non-null
&& canFold(Asm, LHS_A, LHS_B, InSet)) {
15
Calling 'canFold'
21
Returning from 'canFold'
22
Taking true branch
733 // First, fold out any differences which are fully resolved. By
734 // reassociating terms in
735 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
736 // we have the four possible differences:
737 // (LHS_A - LHS_B),
738 // (LHS_A - RHS_B),
739 // (RHS_A - LHS_B),
740 // (RHS_A - RHS_B).
741 // Since we are attempting to be as aggressive as possible about folding, we
742 // attempt to evaluate each possible alternative.
743 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
23
Calling 'AttemptToFoldSymbolOffsetDifference'
744 Result_Cst);
745 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
746 Result_Cst);
747 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
748 Result_Cst);
749 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
750 Result_Cst);
751 }
752
753 // We can't represent the addition or subtraction of two symbols.
754 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
755 return false;
756
757 // At this point, we have at most one additive symbol and one subtractive
758 // symbol -- find them.
759 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
760 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
761
762 Res = MCValue::get(A, B, Result_Cst);
763 return true;
764}
765
766bool MCExpr::evaluateAsRelocatable(MCValue &Res,
767 const MCAsmLayout *Layout,
768 const MCFixup *Fixup) const {
769 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
770 return evaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
771 false);
772}
773
774bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
775 MCAssembler *Assembler = &Layout.getAssembler();
776 return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
777 true);
778}
779
780static bool canExpand(const MCSymbol &Sym, bool InSet) {
781 const MCExpr *Expr = Sym.getVariableValue();
782 const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
783 if (Inner) {
784 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
785 return false;
786 }
787
788 if (InSet)
789 return true;
790 return !Sym.isInSection();
791}
792
793bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
794 const MCAsmLayout *Layout,
795 const MCFixup *Fixup,
796 const SectionAddrMap *Addrs,
797 bool InSet) const {
798 ++stats::MCExprEvaluate;
799
800 switch (getKind()) {
1
Control jumps to 'case Binary:' at line 896
801 case Target:
802 return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
803 Fixup);
804
805 case Constant:
806 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
807 return true;
808
809 case SymbolRef: {
810 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
811 const MCSymbol &Sym = SRE->getSymbol();
812 const auto Kind = SRE->getKind();
813
814 // Evaluate recursively if this is a variable.
815 if (Sym.isVariable() && (Kind == MCSymbolRefExpr::VK_None || Layout) &&
816 canExpand(Sym, InSet)) {
817 bool IsMachO = SRE->hasSubsectionsViaSymbols();
818 if (Sym.getVariableValue()->evaluateAsRelocatableImpl(
819 Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) {
820 if (Kind != MCSymbolRefExpr::VK_None) {
821 if (Res.isAbsolute()) {
822 Res = MCValue::get(SRE, nullptr, 0);
823 return true;
824 }
825 // If the reference has a variant kind, we can only handle expressions
826 // which evaluate exactly to a single unadorned symbol. Attach the
827 // original VariantKind to SymA of the result.
828 if (Res.getRefKind() != MCSymbolRefExpr::VK_None || !Res.getSymA() ||
829 Res.getSymB() || Res.getConstant())
830 return false;
831 Res =
832 MCValue::get(MCSymbolRefExpr::create(&Res.getSymA()->getSymbol(),
833 Kind, Asm->getContext()),
834 Res.getSymB(), Res.getConstant(), Res.getRefKind());
835 }
836 if (!IsMachO)
837 return true;
838
839 const MCSymbolRefExpr *A = Res.getSymA();
840 const MCSymbolRefExpr *B = Res.getSymB();
841 // FIXME: This is small hack. Given
842 // a = b + 4
843 // .long a
844 // the OS X assembler will completely drop the 4. We should probably
845 // include it in the relocation or produce an error if that is not
846 // possible.
847 // Allow constant expressions.
848 if (!A && !B)
849 return true;
850 // Allows aliases with zero offset.
851 if (Res.getConstant() == 0 && (!A || !B))
852 return true;
853 }
854 }
855
856 Res = MCValue::get(SRE, nullptr, 0);
857 return true;
858 }
859
860 case Unary: {
861 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
862 MCValue Value;
863
864 if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
865 Addrs, InSet))
866 return false;
867
868 switch (AUE->getOpcode()) {
869 case MCUnaryExpr::LNot:
870 if (!Value.isAbsolute())
871 return false;
872 Res = MCValue::get(!Value.getConstant());
873 break;
874 case MCUnaryExpr::Minus:
875 /// -(a - b + const) ==> (b - a - const)
876 if (Value.getSymA() && !Value.getSymB())
877 return false;
878
879 // The cast avoids undefined behavior if the constant is INT64_MIN.
880 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
881 -(uint64_t)Value.getConstant());
882 break;
883 case MCUnaryExpr::Not:
884 if (!Value.isAbsolute())
885 return false;
886 Res = MCValue::get(~Value.getConstant());
887 break;
888 case MCUnaryExpr::Plus:
889 Res = Value;
890 break;
891 }
892
893 return true;
894 }
895
896 case Binary: {
897 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
2
The object is a 'MCBinaryExpr'
898 MCValue LHSValue, RHSValue;
899
900 if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
3
Assuming the condition is false
5
Taking false branch
901 Addrs, InSet) ||
902 !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
4
Assuming the condition is false
903 Addrs, InSet)) {
904 // Check if both are Target Expressions, see if we can compare them.
905 if (const MCTargetExpr *L = dyn_cast<MCTargetExpr>(ABE->getLHS()))
906 if (const MCTargetExpr *R = cast<MCTargetExpr>(ABE->getRHS())) {
907 switch (ABE->getOpcode()) {
908 case MCBinaryExpr::EQ:
909 Res = MCValue::get((L->isEqualTo(R)) ? -1 : 0);
910 return true;
911 case MCBinaryExpr::NE:
912 Res = MCValue::get((R->isEqualTo(R)) ? 0 : -1);
913 return true;
914 default: break;
915 }
916 }
917 return false;
918 }
919
920 // We only support a few operations on non-constant expressions, handle
921 // those first.
922 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
6
Calling 'MCValue::isAbsolute'
9
Returning from 'MCValue::isAbsolute'
923 switch (ABE->getOpcode()) {
10
Control jumps to 'case Sub:' at line 926
924 default:
925 return false;
926 case MCBinaryExpr::Sub:
927 // Negate RHS and add.
928 // The cast avoids undefined behavior if the constant is INT64_MIN.
929 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
11
Calling 'EvaluateSymbolicAdd'
930 RHSValue.getSymB(), RHSValue.getSymA(),
931 -(uint64_t)RHSValue.getConstant(), Res);
932
933 case MCBinaryExpr::Add:
934 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
935 RHSValue.getSymA(), RHSValue.getSymB(),
936 RHSValue.getConstant(), Res);
937 }
938 }
939
940 // FIXME: We need target hooks for the evaluation. It may be limited in
941 // width, and gas defines the result of comparisons differently from
942 // Apple as.
943 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
944 int64_t Result = 0;
945 auto Op = ABE->getOpcode();
946 switch (Op) {
947 case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
948 case MCBinaryExpr::Add: Result = LHS + RHS; break;
949 case MCBinaryExpr::And: Result = LHS & RHS; break;
950 case MCBinaryExpr::Div:
951 case MCBinaryExpr::Mod:
952 // Handle division by zero. gas just emits a warning and keeps going,
953 // we try to be stricter.
954 // FIXME: Currently the caller of this function has no way to understand
955 // we're bailing out because of 'division by zero'. Therefore, it will
956 // emit a 'expected relocatable expression' error. It would be nice to
957 // change this code to emit a better diagnostic.
958 if (RHS == 0)
959 return false;
960 if (ABE->getOpcode() == MCBinaryExpr::Div)
961 Result = LHS / RHS;
962 else
963 Result = LHS % RHS;
964 break;
965 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
966 case MCBinaryExpr::GT: Result = LHS > RHS; break;
967 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
968 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
969 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
970 case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
971 case MCBinaryExpr::LT: Result = LHS < RHS; break;
972 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
973 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
974 case MCBinaryExpr::NE: Result = LHS != RHS; break;
975 case MCBinaryExpr::Or: Result = LHS | RHS; break;
976 case MCBinaryExpr::OrNot: Result = LHS | ~RHS; break;
977 case MCBinaryExpr::Shl: Result = uint64_t(LHS) << uint64_t(RHS); break;
978 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
979 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
980 }
981
982 switch (Op) {
983 default:
984 Res = MCValue::get(Result);
985 break;
986 case MCBinaryExpr::EQ:
987 case MCBinaryExpr::GT:
988 case MCBinaryExpr::GTE:
989 case MCBinaryExpr::LT:
990 case MCBinaryExpr::LTE:
991 case MCBinaryExpr::NE:
992 // A comparison operator returns a -1 if true and 0 if false.
993 Res = MCValue::get(Result ? -1 : 0);
994 break;
995 }
996
997 return true;
998 }
999 }
1000
1001 llvm_unreachable("Invalid assembly expression kind!")::llvm::llvm_unreachable_internal("Invalid assembly expression kind!"
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC/MCExpr.cpp"
, 1001)
;
1002}
1003
1004MCFragment *MCExpr::findAssociatedFragment() const {
1005 switch (getKind()) {
43
Calling 'MCExpr::getKind'
45
Returning from 'MCExpr::getKind'
46
Control jumps to 'case Binary:' at line 1022
1006 case Target:
1007 // We never look through target specific expressions.
1008 return cast<MCTargetExpr>(this)->findAssociatedFragment();
1009
1010 case Constant:
1011 return MCSymbol::AbsolutePseudoFragment;
1012
1013 case SymbolRef: {
1014 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
1015 const MCSymbol &Sym = SRE->getSymbol();
1016 return Sym.getFragment();
1017 }
1018
1019 case Unary:
1020 return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
1021
1022 case Binary: {
1023 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
47
The object is a 'MCBinaryExpr'
1024 MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment();
1025 MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment();
48
'RHS_F' initialized here
1026
1027 // If either is absolute, return the other.
1028 if (LHS_F == MCSymbol::AbsolutePseudoFragment)
49
Assuming 'LHS_F' is not equal to 'AbsolutePseudoFragment'
50
Taking false branch
1029 return RHS_F;
1030 if (RHS_F == MCSymbol::AbsolutePseudoFragment)
51
Assuming 'RHS_F' is not equal to 'AbsolutePseudoFragment'
52
Taking false branch
1031 return LHS_F;
1032
1033 // Not always correct, but probably the best we can do without more context.
1034 if (BE->getOpcode() == MCBinaryExpr::Sub)
53
Taking false branch
1035 return MCSymbol::AbsolutePseudoFragment;
1036
1037 // Otherwise, return the first non-null fragment.
1038 return LHS_F ? LHS_F : RHS_F;
54
Assuming 'LHS_F' is null
55
'?' condition is false
56
Returning pointer
1039 }
1040 }
1041
1042 llvm_unreachable("Invalid assembly expression kind!")::llvm::llvm_unreachable_internal("Invalid assembly expression kind!"
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/lib/MC/MCExpr.cpp"
, 1042)
;
1043}

/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCValue.h

1//===-- llvm/MC/MCValue.h - MCValue class -----------------------*- C++ -*-===//
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// This file contains the declaration of the MCValue class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_MC_MCVALUE_H
14#define LLVM_MC_MCVALUE_H
15
16#include "llvm/MC/MCExpr.h"
17#include "llvm/Support/DataTypes.h"
18#include <cassert>
19
20namespace llvm {
21class raw_ostream;
22
23/// This represents an "assembler immediate".
24///
25/// In its most general form, this can hold ":Kind:(SymbolA - SymbolB +
26/// imm64)". Not all targets supports relocations of this general form, but we
27/// need to represent this anyway.
28///
29/// In general both SymbolA and SymbolB will also have a modifier
30/// analogous to the top-level Kind. Current targets are not expected
31/// to make use of both though. The choice comes down to whether
32/// relocation modifiers apply to the closest symbol or the whole
33/// expression.
34///
35/// Note that this class must remain a simple POD value class, because we need
36/// it to live in unions etc.
37class MCValue {
38 const MCSymbolRefExpr *SymA = nullptr, *SymB = nullptr;
39 int64_t Cst = 0;
40 uint32_t RefKind = 0;
41
42public:
43 MCValue() = default;
44 int64_t getConstant() const { return Cst; }
45 const MCSymbolRefExpr *getSymA() const { return SymA; }
46 const MCSymbolRefExpr *getSymB() const { return SymB; }
47 uint32_t getRefKind() const { return RefKind; }
48
49 /// Is this an absolute (as opposed to relocatable) value.
50 bool isAbsolute() const { return !SymA && !SymB; }
7
Assuming field 'SymA' is non-null
8
Returning zero, which participates in a condition later
51
52 /// Print the value to the stream \p OS.
53 void print(raw_ostream &OS) const;
54
55 /// Print the value to stderr.
56 void dump() const;
57
58 MCSymbolRefExpr::VariantKind getAccessVariant() const;
59
60 static MCValue get(const MCSymbolRefExpr *SymA,
61 const MCSymbolRefExpr *SymB = nullptr,
62 int64_t Val = 0, uint32_t RefKind = 0) {
63 MCValue R;
64 R.Cst = Val;
65 R.SymA = SymA;
66 R.SymB = SymB;
67 R.RefKind = RefKind;
68 return R;
69 }
70
71 static MCValue get(int64_t Val) {
72 MCValue R;
73 R.Cst = Val;
74 R.SymA = nullptr;
75 R.SymB = nullptr;
76 R.RefKind = 0;
77 return R;
78 }
79
80};
81
82} // end namespace llvm
83
84#endif

/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h

1//===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===//
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// This file contains the declaration of the MCSymbol class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_MC_MCSYMBOL_H
14#define LLVM_MC_MCSYMBOL_H
15
16#include "llvm/ADT/PointerIntPair.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCFragment.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/MathExtras.h"
23#include <cassert>
24#include <cstddef>
25#include <cstdint>
26
27namespace llvm {
28
29class MCAsmInfo;
30class MCContext;
31class MCSection;
32class raw_ostream;
33
34/// MCSymbol - Instances of this class represent a symbol name in the MC file,
35/// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
36/// should only be constructed with valid names for the object file.
37///
38/// If the symbol is defined/emitted into the current translation unit, the
39/// Section member is set to indicate what section it lives in. Otherwise, if
40/// it is a reference to an external entity, it has a null section.
41class MCSymbol {
42protected:
43 /// The kind of the symbol. If it is any value other than unset then this
44 /// class is actually one of the appropriate subclasses of MCSymbol.
45 enum SymbolKind {
46 SymbolKindUnset,
47 SymbolKindCOFF,
48 SymbolKindELF,
49 SymbolKindMachO,
50 SymbolKindWasm,
51 SymbolKindXCOFF,
52 };
53
54 /// A symbol can contain an Offset, or Value, or be Common, but never more
55 /// than one of these.
56 enum Contents : uint8_t {
57 SymContentsUnset,
58 SymContentsOffset,
59 SymContentsVariable,
60 SymContentsCommon,
61 SymContentsTargetCommon, // Index stores the section index
62 };
63
64 // Special sentinal value for the absolute pseudo fragment.
65 static MCFragment *AbsolutePseudoFragment;
66
67 /// If a symbol has a Fragment, the section is implied, so we only need
68 /// one pointer.
69 /// The special AbsolutePseudoFragment value is for absolute symbols.
70 /// If this is a variable symbol, this caches the variable value's fragment.
71 /// FIXME: We might be able to simplify this by having the asm streamer create
72 /// dummy fragments.
73 /// If this is a section, then it gives the symbol is defined in. This is null
74 /// for undefined symbols.
75 ///
76 /// If this is a fragment, then it gives the fragment this symbol's value is
77 /// relative to, if any.
78 ///
79 /// For the 'HasName' integer, this is true if this symbol is named.
80 /// A named symbol will have a pointer to the name allocated in the bytes
81 /// immediately prior to the MCSymbol.
82 mutable PointerIntPair<MCFragment *, 1> FragmentAndHasName;
83
84 /// IsTemporary - True if this is an assembler temporary label, which
85 /// typically does not survive in the .o file's symbol table. Usually
86 /// "Lfoo" or ".foo".
87 unsigned IsTemporary : 1;
88
89 /// True if this symbol can be redefined.
90 unsigned IsRedefinable : 1;
91
92 /// IsUsed - True if this symbol has been used.
93 mutable unsigned IsUsed : 1;
94
95 mutable unsigned IsRegistered : 1;
96
97 /// True if this symbol is visible outside this translation unit. Note: ELF
98 /// uses binding instead of this bit.
99 mutable unsigned IsExternal : 1;
100
101 /// This symbol is private extern.
102 mutable unsigned IsPrivateExtern : 1;
103
104 /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
105 /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
106 unsigned Kind : 3;
107
108 /// True if we have created a relocation that uses this symbol.
109 mutable unsigned IsUsedInReloc : 1;
110
111 /// This is actually a Contents enumerator, but is unsigned to avoid sign
112 /// extension and achieve better bitpacking with MSVC.
113 unsigned SymbolContents : 3;
114
115 /// The alignment of the symbol, if it is 'common', or -1.
116 ///
117 /// The alignment is stored as log2(align) + 1. This allows all values from
118 /// 0 to 2^31 to be stored which is every power of 2 representable by an
119 /// unsigned.
120 enum : unsigned { NumCommonAlignmentBits = 5 };
121 unsigned CommonAlignLog2 : NumCommonAlignmentBits;
122
123 /// The Flags field is used by object file implementations to store
124 /// additional per symbol information which is not easily classified.
125 enum : unsigned { NumFlagsBits = 16 };
126 mutable uint32_t Flags : NumFlagsBits;
127
128 /// Index field, for use by the object file implementation.
129 mutable uint32_t Index = 0;
130
131 union {
132 /// The offset to apply to the fragment address to form this symbol's value.
133 uint64_t Offset;
134
135 /// The size of the symbol, if it is 'common'.
136 uint64_t CommonSize;
137
138 /// If non-null, the value for a variable symbol.
139 const MCExpr *Value;
140 };
141
142 // MCContext creates and uniques these.
143 friend class MCExpr;
144 friend class MCContext;
145
146 /// The name for a symbol.
147 /// MCSymbol contains a uint64_t so is probably aligned to 8. On a 32-bit
148 /// system, the name is a pointer so isn't going to satisfy the 8 byte
149 /// alignment of uint64_t. Account for that here.
150 using NameEntryStorageTy = union {
151 const StringMapEntry<bool> *NameEntry;
152 uint64_t AlignmentPadding;
153 };
154
155 MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
156 : IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
157 IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
158 Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset),
159 CommonAlignLog2(0), Flags(0) {
160 Offset = 0;
161 FragmentAndHasName.setInt(!!Name);
162 if (Name)
163 getNameEntryPtr() = Name;
164 }
165
166 // Provide custom new/delete as we will only allocate space for a name
167 // if we need one.
168 void *operator new(size_t s, const StringMapEntry<bool> *Name,
169 MCContext &Ctx);
170
171private:
172 void operator delete(void *);
173 /// Placement delete - required by std, but never called.
174 void operator delete(void*, unsigned) {
175 llvm_unreachable("Constructor throws?")::llvm::llvm_unreachable_internal("Constructor throws?", "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 175)
;
176 }
177 /// Placement delete - required by std, but never called.
178 void operator delete(void*, unsigned, bool) {
179 llvm_unreachable("Constructor throws?")::llvm::llvm_unreachable_internal("Constructor throws?", "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 179)
;
180 }
181
182 /// Get a reference to the name field. Requires that we have a name
183 const StringMapEntry<bool> *&getNameEntryPtr() {
184 assert(FragmentAndHasName.getInt() && "Name is required")(static_cast <bool> (FragmentAndHasName.getInt() &&
"Name is required") ? void (0) : __assert_fail ("FragmentAndHasName.getInt() && \"Name is required\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 184, __extension__ __PRETTY_FUNCTION__))
;
185 NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
186 return (*(Name - 1)).NameEntry;
187 }
188 const StringMapEntry<bool> *&getNameEntryPtr() const {
189 return const_cast<MCSymbol*>(this)->getNameEntryPtr();
190 }
191
192public:
193 MCSymbol(const MCSymbol &) = delete;
194 MCSymbol &operator=(const MCSymbol &) = delete;
195
196 /// getName - Get the symbol name.
197 StringRef getName() const {
198 if (!FragmentAndHasName.getInt())
199 return StringRef();
200
201 return getNameEntryPtr()->first();
202 }
203
204 bool isRegistered() const { return IsRegistered; }
205 void setIsRegistered(bool Value) const { IsRegistered = Value; }
206
207 void setUsedInReloc() const { IsUsedInReloc = true; }
208 bool isUsedInReloc() const { return IsUsedInReloc; }
209
210 /// \name Accessors
211 /// @{
212
213 /// isTemporary - Check if this is an assembler temporary symbol.
214 bool isTemporary() const { return IsTemporary; }
215
216 /// isUsed - Check if this is used.
217 bool isUsed() const { return IsUsed; }
218
219 /// Check if this symbol is redefinable.
220 bool isRedefinable() const { return IsRedefinable; }
221 /// Mark this symbol as redefinable.
222 void setRedefinable(bool Value) { IsRedefinable = Value; }
223 /// Prepare this symbol to be redefined.
224 void redefineIfPossible() {
225 if (IsRedefinable) {
226 if (SymbolContents == SymContentsVariable) {
227 Value = nullptr;
228 SymbolContents = SymContentsUnset;
229 }
230 setUndefined();
231 IsRedefinable = false;
232 }
233 }
234
235 /// @}
236 /// \name Associated Sections
237 /// @{
238
239 /// isDefined - Check if this symbol is defined (i.e., it has an address).
240 ///
241 /// Defined symbols are either absolute or in some section.
242 bool isDefined() const { return !isUndefined(); }
243
244 /// isInSection - Check if this symbol is defined in some section (i.e., it
245 /// is defined but not absolute).
246 bool isInSection() const {
247 return isDefined() && !isAbsolute();
248 }
249
250 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
251 bool isUndefined(bool SetUsed = true) const {
252 return getFragment(SetUsed) == nullptr;
27
Assuming the condition is false
28
Returning zero, which participates in a condition later
31
Returning zero, which participates in a condition later
253 }
254
255 /// isAbsolute - Check if this is an absolute symbol.
256 bool isAbsolute() const {
257 return getFragment() == AbsolutePseudoFragment;
258 }
259
260 /// Get the section associated with a defined, non-absolute symbol.
261 MCSection &getSection() const {
262 assert(isInSection() && "Invalid accessor!")(static_cast <bool> (isInSection() && "Invalid accessor!"
) ? void (0) : __assert_fail ("isInSection() && \"Invalid accessor!\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 262, __extension__ __PRETTY_FUNCTION__))
;
263 return *getFragment()->getParent();
264 }
265
266 /// Mark the symbol as defined in the fragment \p F.
267 void setFragment(MCFragment *F) const {
268 assert(!isVariable() && "Cannot set fragment of variable")(static_cast <bool> (!isVariable() && "Cannot set fragment of variable"
) ? void (0) : __assert_fail ("!isVariable() && \"Cannot set fragment of variable\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 268, __extension__ __PRETTY_FUNCTION__))
;
269 FragmentAndHasName.setPointer(F);
270 }
271
272 /// Mark the symbol as undefined.
273 void setUndefined() { FragmentAndHasName.setPointer(nullptr); }
274
275 bool isELF() const { return Kind == SymbolKindELF; }
276
277 bool isCOFF() const { return Kind == SymbolKindCOFF; }
278
279 bool isMachO() const { return Kind == SymbolKindMachO; }
280
281 bool isWasm() const { return Kind == SymbolKindWasm; }
282
283 bool isXCOFF() const { return Kind == SymbolKindXCOFF; }
284
285 /// @}
286 /// \name Variable Symbols
287 /// @{
288
289 /// isVariable - Check if this is a variable symbol.
290 bool isVariable() const {
291 return SymbolContents == SymContentsVariable;
39
Returning the value 1, which participates in a condition later
64
Returning the value 1, which participates in a condition later
292 }
293
294 /// getVariableValue - Get the value for variable symbols.
295 const MCExpr *getVariableValue(bool SetUsed = true) const {
296 assert(isVariable() && "Invalid accessor!")(static_cast <bool> (isVariable() && "Invalid accessor!"
) ? void (0) : __assert_fail ("isVariable() && \"Invalid accessor!\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 296, __extension__ __PRETTY_FUNCTION__))
;
297 IsUsed |= SetUsed;
298 return Value;
299 }
300
301 void setVariableValue(const MCExpr *Value);
302
303 /// @}
304
305 /// Get the (implementation defined) index.
306 uint32_t getIndex() const {
307 return Index;
308 }
309
310 /// Set the (implementation defined) index.
311 void setIndex(uint32_t Value) const {
312 Index = Value;
313 }
314
315 bool isUnset() const { return SymbolContents == SymContentsUnset; }
316
317 uint64_t getOffset() const {
318 assert((SymbolContents == SymContentsUnset ||(static_cast <bool> ((SymbolContents == SymContentsUnset
|| SymbolContents == SymContentsOffset) && "Cannot get offset for a common/variable symbol"
) ? void (0) : __assert_fail ("(SymbolContents == SymContentsUnset || SymbolContents == SymContentsOffset) && \"Cannot get offset for a common/variable symbol\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 320, __extension__ __PRETTY_FUNCTION__))
319 SymbolContents == SymContentsOffset) &&(static_cast <bool> ((SymbolContents == SymContentsUnset
|| SymbolContents == SymContentsOffset) && "Cannot get offset for a common/variable symbol"
) ? void (0) : __assert_fail ("(SymbolContents == SymContentsUnset || SymbolContents == SymContentsOffset) && \"Cannot get offset for a common/variable symbol\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 320, __extension__ __PRETTY_FUNCTION__))
320 "Cannot get offset for a common/variable symbol")(static_cast <bool> ((SymbolContents == SymContentsUnset
|| SymbolContents == SymContentsOffset) && "Cannot get offset for a common/variable symbol"
) ? void (0) : __assert_fail ("(SymbolContents == SymContentsUnset || SymbolContents == SymContentsOffset) && \"Cannot get offset for a common/variable symbol\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 320, __extension__ __PRETTY_FUNCTION__))
;
321 return Offset;
322 }
323 void setOffset(uint64_t Value) {
324 assert((SymbolContents == SymContentsUnset ||(static_cast <bool> ((SymbolContents == SymContentsUnset
|| SymbolContents == SymContentsOffset) && "Cannot set offset for a common/variable symbol"
) ? void (0) : __assert_fail ("(SymbolContents == SymContentsUnset || SymbolContents == SymContentsOffset) && \"Cannot set offset for a common/variable symbol\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 326, __extension__ __PRETTY_FUNCTION__))
325 SymbolContents == SymContentsOffset) &&(static_cast <bool> ((SymbolContents == SymContentsUnset
|| SymbolContents == SymContentsOffset) && "Cannot set offset for a common/variable symbol"
) ? void (0) : __assert_fail ("(SymbolContents == SymContentsUnset || SymbolContents == SymContentsOffset) && \"Cannot set offset for a common/variable symbol\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 326, __extension__ __PRETTY_FUNCTION__))
326 "Cannot set offset for a common/variable symbol")(static_cast <bool> ((SymbolContents == SymContentsUnset
|| SymbolContents == SymContentsOffset) && "Cannot set offset for a common/variable symbol"
) ? void (0) : __assert_fail ("(SymbolContents == SymContentsUnset || SymbolContents == SymContentsOffset) && \"Cannot set offset for a common/variable symbol\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 326, __extension__ __PRETTY_FUNCTION__))
;
327 Offset = Value;
328 SymbolContents = SymContentsOffset;
329 }
330
331 /// Return the size of a 'common' symbol.
332 uint64_t getCommonSize() const {
333 assert(isCommon() && "Not a 'common' symbol!")(static_cast <bool> (isCommon() && "Not a 'common' symbol!"
) ? void (0) : __assert_fail ("isCommon() && \"Not a 'common' symbol!\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 333, __extension__ __PRETTY_FUNCTION__))
;
334 return CommonSize;
335 }
336
337 /// Mark this symbol as being 'common'.
338 ///
339 /// \param Size - The size of the symbol.
340 /// \param Align - The alignment of the symbol.
341 /// \param Target - Is the symbol a target-specific common-like symbol.
342 void setCommon(uint64_t Size, unsigned Align, bool Target = false) {
343 assert(getOffset() == 0)(static_cast <bool> (getOffset() == 0) ? void (0) : __assert_fail
("getOffset() == 0", "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 343, __extension__ __PRETTY_FUNCTION__))
;
344 CommonSize = Size;
345 SymbolContents = Target ? SymContentsTargetCommon : SymContentsCommon;
346
347 assert((!Align || isPowerOf2_32(Align)) &&(static_cast <bool> ((!Align || isPowerOf2_32(Align)) &&
"Alignment must be a power of 2") ? void (0) : __assert_fail
("(!Align || isPowerOf2_32(Align)) && \"Alignment must be a power of 2\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 348, __extension__ __PRETTY_FUNCTION__))
348 "Alignment must be a power of 2")(static_cast <bool> ((!Align || isPowerOf2_32(Align)) &&
"Alignment must be a power of 2") ? void (0) : __assert_fail
("(!Align || isPowerOf2_32(Align)) && \"Alignment must be a power of 2\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 348, __extension__ __PRETTY_FUNCTION__))
;
349 unsigned Log2Align = Log2_32(Align) + 1;
350 assert(Log2Align < (1U << NumCommonAlignmentBits) &&(static_cast <bool> (Log2Align < (1U << NumCommonAlignmentBits
) && "Out of range alignment") ? void (0) : __assert_fail
("Log2Align < (1U << NumCommonAlignmentBits) && \"Out of range alignment\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 351, __extension__ __PRETTY_FUNCTION__))
351 "Out of range alignment")(static_cast <bool> (Log2Align < (1U << NumCommonAlignmentBits
) && "Out of range alignment") ? void (0) : __assert_fail
("Log2Align < (1U << NumCommonAlignmentBits) && \"Out of range alignment\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 351, __extension__ __PRETTY_FUNCTION__))
;
352 CommonAlignLog2 = Log2Align;
353 }
354
355 /// Return the alignment of a 'common' symbol.
356 unsigned getCommonAlignment() const {
357 assert(isCommon() && "Not a 'common' symbol!")(static_cast <bool> (isCommon() && "Not a 'common' symbol!"
) ? void (0) : __assert_fail ("isCommon() && \"Not a 'common' symbol!\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 357, __extension__ __PRETTY_FUNCTION__))
;
358 return CommonAlignLog2 ? (1U << (CommonAlignLog2 - 1)) : 0;
359 }
360
361 /// Declare this symbol as being 'common'.
362 ///
363 /// \param Size - The size of the symbol.
364 /// \param Align - The alignment of the symbol.
365 /// \param Target - Is the symbol a target-specific common-like symbol.
366 /// \return True if symbol was already declared as a different type
367 bool declareCommon(uint64_t Size, unsigned Align, bool Target = false) {
368 assert(isCommon() || getOffset() == 0)(static_cast <bool> (isCommon() || getOffset() == 0) ? void
(0) : __assert_fail ("isCommon() || getOffset() == 0", "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 368, __extension__ __PRETTY_FUNCTION__))
;
369 if(isCommon()) {
370 if (CommonSize != Size || getCommonAlignment() != Align ||
371 isTargetCommon() != Target)
372 return true;
373 } else
374 setCommon(Size, Align, Target);
375 return false;
376 }
377
378 /// Is this a 'common' symbol.
379 bool isCommon() const {
380 return SymbolContents == SymContentsCommon ||
381 SymbolContents == SymContentsTargetCommon;
382 }
383
384 /// Is this a target-specific common-like symbol.
385 bool isTargetCommon() const {
386 return SymbolContents == SymContentsTargetCommon;
387 }
388
389 MCFragment *getFragment(bool SetUsed = true) const {
390 MCFragment *Fragment = FragmentAndHasName.getPointer();
391 if (Fragment || !isVariable())
37
Assuming 'Fragment' is null
38
Calling 'MCSymbol::isVariable'
40
Returning from 'MCSymbol::isVariable'
41
Taking false branch
392 return Fragment;
393 Fragment = getVariableValue(SetUsed)->findAssociatedFragment();
42
Calling 'MCExpr::findAssociatedFragment'
57
Returning from 'MCExpr::findAssociatedFragment'
58
Value assigned to 'Fragment'
394 FragmentAndHasName.setPointer(Fragment);
395 return Fragment;
59
Returning pointer (loaded from 'Fragment')
396 }
397
398 bool isExternal() const { return IsExternal; }
399 void setExternal(bool Value) const { IsExternal = Value; }
400
401 bool isPrivateExtern() const { return IsPrivateExtern; }
402 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
403
404 /// print - Print the value to the stream \p OS.
405 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
406
407 /// dump - Print the value to stderr.
408 void dump() const;
409
410protected:
411 /// Get the (implementation defined) symbol flags.
412 uint32_t getFlags() const { return Flags; }
413
414 /// Set the (implementation defined) symbol flags.
415 void setFlags(uint32_t Value) const {
416 assert(Value < (1U << NumFlagsBits) && "Out of range flags")(static_cast <bool> (Value < (1U << NumFlagsBits
) && "Out of range flags") ? void (0) : __assert_fail
("Value < (1U << NumFlagsBits) && \"Out of range flags\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 416, __extension__ __PRETTY_FUNCTION__))
;
417 Flags = Value;
418 }
419
420 /// Modify the flags via a mask
421 void modifyFlags(uint32_t Value, uint32_t Mask) const {
422 assert(Value < (1U << NumFlagsBits) && "Out of range flags")(static_cast <bool> (Value < (1U << NumFlagsBits
) && "Out of range flags") ? void (0) : __assert_fail
("Value < (1U << NumFlagsBits) && \"Out of range flags\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCSymbol.h"
, 422, __extension__ __PRETTY_FUNCTION__))
;
423 Flags = (Flags & ~Mask) | Value;
424 }
425};
426
427inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
428 Sym.print(OS, nullptr);
429 return OS;
430}
431
432} // end namespace llvm
433
434#endif // LLVM_MC_MCSYMBOL_H

/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCExpr.h

1//===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===//
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#ifndef LLVM_MC_MCEXPR_H
10#define LLVM_MC_MCEXPR_H
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/Support/SMLoc.h"
14#include <cstdint>
15
16namespace llvm {
17
18class MCAsmInfo;
19class MCAsmLayout;
20class MCAssembler;
21class MCContext;
22class MCFixup;
23class MCFragment;
24class MCSection;
25class MCStreamer;
26class MCSymbol;
27class MCValue;
28class raw_ostream;
29class StringRef;
30
31using SectionAddrMap = DenseMap<const MCSection *, uint64_t>;
32
33/// Base class for the full range of assembler expressions which are
34/// needed for parsing.
35class MCExpr {
36public:
37 enum ExprKind : uint8_t {
38 Binary, ///< Binary expressions.
39 Constant, ///< Constant expressions.
40 SymbolRef, ///< References to labels and assigned expressions.
41 Unary, ///< Unary expressions.
42 Target ///< Target specific expression.
43 };
44
45private:
46 static const unsigned NumSubclassDataBits = 24;
47 static_assert(
48 NumSubclassDataBits == CHAR_BIT8 * (sizeof(unsigned) - sizeof(ExprKind)),
49 "ExprKind and SubclassData together should take up one word");
50
51 ExprKind Kind;
52 /// Field reserved for use by MCExpr subclasses.
53 unsigned SubclassData : NumSubclassDataBits;
54 SMLoc Loc;
55
56 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
57 const MCAsmLayout *Layout,
58 const SectionAddrMap *Addrs, bool InSet) const;
59
60protected:
61 explicit MCExpr(ExprKind Kind, SMLoc Loc, unsigned SubclassData = 0)
62 : Kind(Kind), SubclassData(SubclassData), Loc(Loc) {
63 assert(SubclassData < (1 << NumSubclassDataBits) &&(static_cast <bool> (SubclassData < (1 << NumSubclassDataBits
) && "Subclass data too large") ? void (0) : __assert_fail
("SubclassData < (1 << NumSubclassDataBits) && \"Subclass data too large\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCExpr.h"
, 64, __extension__ __PRETTY_FUNCTION__))
64 "Subclass data too large")(static_cast <bool> (SubclassData < (1 << NumSubclassDataBits
) && "Subclass data too large") ? void (0) : __assert_fail
("SubclassData < (1 << NumSubclassDataBits) && \"Subclass data too large\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCExpr.h"
, 64, __extension__ __PRETTY_FUNCTION__))
;
65 }
66
67 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
68 const MCAsmLayout *Layout,
69 const MCFixup *Fixup,
70 const SectionAddrMap *Addrs, bool InSet) const;
71
72 unsigned getSubclassData() const { return SubclassData; }
73
74public:
75 MCExpr(const MCExpr &) = delete;
76 MCExpr &operator=(const MCExpr &) = delete;
77
78 /// \name Accessors
79 /// @{
80
81 ExprKind getKind() const { return Kind; }
44
Returning zero, which participates in a condition later
82 SMLoc getLoc() const { return Loc; }
83
84 /// @}
85 /// \name Utility Methods
86 /// @{
87
88 void print(raw_ostream &OS, const MCAsmInfo *MAI,
89 bool InParens = false) const;
90 void dump() const;
91
92 /// @}
93 /// \name Expression Evaluation
94 /// @{
95
96 /// Try to evaluate the expression to an absolute value.
97 ///
98 /// \param Res - The absolute value, if evaluation succeeds.
99 /// \param Layout - The assembler layout object to use for evaluating symbol
100 /// values. If not given, then only non-symbolic expressions will be
101 /// evaluated.
102 /// \return - True on success.
103 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
104 const SectionAddrMap &Addrs) const;
105 bool evaluateAsAbsolute(int64_t &Res) const;
106 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
107 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const;
108 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
109
110 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
111
112 /// Try to evaluate the expression to a relocatable value, i.e. an
113 /// expression of the fixed form (a - b + constant).
114 ///
115 /// \param Res - The relocatable value, if evaluation succeeds.
116 /// \param Layout - The assembler layout object to use for evaluating values.
117 /// \param Fixup - The Fixup object if available.
118 /// \return - True on success.
119 bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
120 const MCFixup *Fixup) const;
121
122 /// Try to evaluate the expression to the form (a - b + constant) where
123 /// neither a nor b are variables.
124 ///
125 /// This is a more aggressive variant of evaluateAsRelocatable. The intended
126 /// use is for when relocations are not available, like the .size directive.
127 bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
128
129 /// Find the "associated section" for this expression, which is
130 /// currently defined as the absolute section for constants, or
131 /// otherwise the section associated with the first defined symbol in the
132 /// expression.
133 MCFragment *findAssociatedFragment() const;
134
135 /// @}
136};
137
138inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
139 E.print(OS, nullptr);
140 return OS;
141}
142
143//// Represent a constant integer expression.
144class MCConstantExpr : public MCExpr {
145 int64_t Value;
146
147 // Subclass data stores SizeInBytes in bits 0..7 and PrintInHex in bit 8.
148 static const unsigned SizeInBytesBits = 8;
149 static const unsigned SizeInBytesMask = (1 << SizeInBytesBits) - 1;
150 static const unsigned PrintInHexBit = 1 << SizeInBytesBits;
151
152 static unsigned encodeSubclassData(bool PrintInHex, unsigned SizeInBytes) {
153 assert(SizeInBytes <= sizeof(int64_t) && "Excessive size")(static_cast <bool> (SizeInBytes <= sizeof(int64_t) &&
"Excessive size") ? void (0) : __assert_fail ("SizeInBytes <= sizeof(int64_t) && \"Excessive size\""
, "/build/llvm-toolchain-snapshot-13~++20210613111130+5be314f79ba7/llvm/include/llvm/MC/MCExpr.h"
, 153, __extension__ __PRETTY_FUNCTION__))
;
154 return SizeInBytes | (PrintInHex ? PrintInHexBit : 0);
155 }
156
157 MCConstantExpr(int64_t Value, bool PrintInHex, unsigned SizeInBytes)
158 : MCExpr(MCExpr::Constant, SMLoc(),
159 encodeSubclassData(PrintInHex, SizeInBytes)), Value(Value) {}
160
161public:
162 /// \name Construction
163 /// @{
164
165 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx,
166 bool PrintInHex = false,
167 unsigned SizeInBytes = 0);
168
169 /// @}
170 /// \name Accessors
171 /// @{
172
173 int64_t getValue() const { return Value; }
174 unsigned getSizeInBytes() const {
175 return getSubclassData() & SizeInBytesMask;
176 }
177
178 bool useHexFormat() const { return (getSubclassData() & PrintInHexBit) != 0; }
179
180 /// @}
181
182 static bool classof(const MCExpr *E) {
183 return E->getKind() == MCExpr::Constant;
184 }
185};
186
187/// Represent a reference to a symbol from inside an expression.
188///
189/// A symbol reference in an expression may be a use of a label, a use of an
190/// assembler variable (defined constant), or constitute an implicit definition
191/// of the symbol as external.
192class MCSymbolRefExpr : public MCExpr {
193public:
194 enum VariantKind : uint16_t {
195 VK_None,
196 VK_Invalid,
197
198 VK_GOT,
199 VK_GOTOFF,
200 VK_GOTREL,
201 VK_PCREL,
202 VK_GOTPCREL,
203 VK_GOTTPOFF,
204 VK_INDNTPOFF,
205 VK_NTPOFF,
206 VK_GOTNTPOFF,
207 VK_PLT,
208 VK_TLSGD,
209 VK_TLSLD,
210 VK_TLSLDM,
211 VK_TPOFF,
212 VK_DTPOFF,
213 VK_TLSCALL, // symbol(tlscall)
214 VK_TLSDESC, // symbol(tlsdesc)
215 VK_TLVP, // Mach-O thread local variable relocations
216 VK_TLVPPAGE,
217 VK_TLVPPAGEOFF,
218 VK_PAGE,
219 VK_PAGEOFF,
220 VK_GOTPAGE,
221 VK_GOTPAGEOFF,
222 VK_SECREL,
223 VK_SIZE, // symbol@SIZE
224 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
225
226 VK_X86_ABS8,
227 VK_X86_PLTOFF,
228
229 VK_ARM_NONE,
230 VK_ARM_GOT_PREL,
231 VK_ARM_TARGET1,
232 VK_ARM_TARGET2,
233 VK_ARM_PREL31,
234 VK_ARM_SBREL, // symbol(sbrel)
235 VK_ARM_TLSLDO, // symbol(tlsldo)
236 VK_ARM_TLSDESCSEQ,
237
238 VK_AVR_NONE,
239 VK_AVR_LO8,
240 VK_AVR_HI8,
241 VK_AVR_HLO8,
242 VK_AVR_DIFF8,
243 VK_AVR_DIFF16,
244 VK_AVR_DIFF32,
245 VK_AVR_PM,
246
247 VK_PPC_LO, // symbol@l
248 VK_PPC_HI, // symbol@h
249 VK_PPC_HA, // symbol@ha
250 VK_PPC_HIGH, // symbol@high
251 VK_PPC_HIGHA, // symbol@higha
252 VK_PPC_HIGHER, // symbol@higher
253 VK_PPC_HIGHERA, // symbol@highera
254 VK_PPC_HIGHEST, // symbol@highest
255 VK_PPC_HIGHESTA, // symbol@highesta
256 VK_PPC_GOT_LO, // symbol@got@l
257 VK_PPC_GOT_HI, // symbol@got@h
258 VK_PPC_GOT_HA, // symbol@got@ha
259 VK_PPC_TOCBASE, // symbol@tocbase
260 VK_PPC_TOC, // symbol@toc
261 VK_PPC_TOC_LO, // symbol@toc@l
262 VK_PPC_TOC_HI, // symbol@toc@h
263 VK_PPC_TOC_HA, // symbol@toc@ha
264 VK_PPC_U, // symbol@u
265 VK_PPC_L, // symbol@l
266 VK_PPC_DTPMOD, // symbol@dtpmod
267 VK_PPC_TPREL_LO, // symbol@tprel@l
268 VK_PPC_TPREL_HI, // symbol@tprel@h
269 VK_PPC_TPREL_HA, // symbol@tprel@ha
270 VK_PPC_TPREL_HIGH, // symbol@tprel@high
271 VK_PPC_TPREL_HIGHA, // symbol@tprel@higha
272 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
273 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
274 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
275 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
276 VK_PPC_DTPREL_LO, // symbol@dtprel@l
277 VK_PPC_DTPREL_HI, // symbol@dtprel@h
278 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
279 VK_PPC_DTPREL_HIGH, // symbol@dtprel@high
280 VK_PPC_DTPREL_HIGHA, // symbol@dtprel@higha
281 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
282 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
283 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
284 VK_PPC_DTPREL_HIGHESTA, // symbol@dtprel@highesta
285 VK_PPC_GOT_TPREL, // symbol@got@tprel
286 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
287 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
288 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
289 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
290 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
291 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
292 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
293 VK_PPC_TLS, // symbol@tls
294 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
295 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
296 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
297 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
298 VK_PPC_TLSGD, // symbol@tlsgd
299 VK_PPC_AIX_TLSGD, // symbol@gd
300 VK_PPC_AIX_TLSGDM, // symbol@m
301 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
302 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
303 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
304 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
305 VK_PPC_GOT_PCREL, // symbol@got@pcrel
306 VK_PPC_GOT_TLSGD_PCREL, // symbol@got@tlsgd@pcrel
307 VK_PPC_GOT_TLSLD_PCREL, // symbol@got@tlsld@pcrel
308 VK_PPC_GOT_TPREL_PCREL, // symbol@got@tprel@pcrel
309 VK_PPC_TLS_PCREL, // symbol@tls@pcrel
310 VK_PPC_TLSLD, // symbol@tlsld
311 VK_PPC_LOCAL, // symbol@local
312 VK_PPC_NOTOC, // symbol@notoc
313 VK_PPC_PCREL_OPT, // .reloc expr, R_PPC64_PCREL_OPT, expr
314
315 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
316
317 VK_Hexagon_LO16,
318 VK_Hexagon_HI16,
319 VK_Hexagon_GPREL,
320 VK_Hexagon_GD_GOT,
321 VK_Hexagon_LD_GOT,
322 VK_Hexagon_GD_PLT,
323 VK_Hexagon_LD_PLT,
324 VK_Hexagon_IE,
325 VK_Hexagon_IE_GOT,
326
327 VK_WASM_TYPEINDEX, // Reference to a symbol's type (signature)
328 VK_WASM_TLSREL, // Memory address relative to __tls_base
329 VK_WASM_MBREL, // Memory address relative to __memory_base
330 VK_WASM_TBREL, // Table index relative to __table_base
331
332 VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
333 VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
334 VK_AMDGPU_REL32_LO, // symbol@rel32@lo
335 VK_AMDGPU_REL32_HI, // symbol@rel32@hi
336 VK_AMDGPU_REL64, // symbol@rel64
337 VK_AMDGPU_ABS32_LO, // symbol@abs32@lo
338 VK_AMDGPU_ABS32_HI, // symbol@abs32@hi
339
340 VK_VE_HI32, // symbol@hi
341 VK_VE_LO32, // symbol@lo
342 VK_VE_PC_HI32, // symbol@pc_hi
343 VK_VE_PC_LO32, // symbol@pc_lo
344 VK_VE_GOT_HI32, // symbol@got_hi
345 VK_VE_GOT_LO32, // symbol@got_lo
346 VK_VE_GOTOFF_HI32, // symbol@gotoff_hi
347 VK_VE_GOTOFF_LO32, // symbol@gotoff_lo
348 VK_VE_PLT_HI32, // symbol@plt_hi
349 VK_VE_PLT_LO32, // symbol@plt_lo
350 VK_VE_TLS_GD_HI32, // symbol@tls_gd_hi
351 VK_VE_TLS_GD_LO32, // symbol@tls_gd_lo
352 VK_VE_TPOFF_HI32, // symbol@tpoff_hi
353 VK_VE_TPOFF_LO32, // symbol@tpoff_lo
354
355 VK_TPREL,
356 VK_DTPREL
357 };
358
359private:
360 /// The symbol being referenced.
361 const MCSymbol *Symbol;
362
363 // Subclass data stores VariantKind in bits 0..15 and HasSubsectionsViaSymbols
364 // in bit 16.
365 static const unsigned VariantKindBits = 16;
366 static const unsigned VariantKindMask = (1 << VariantKindBits) - 1;
367
368 // FIXME: Remove this bit.
369 static const unsigned HasSubsectionsViaSymbolsBit = 1 << VariantKindBits;
370
371 static unsigned encodeSubclassData(VariantKind Kind,
372 bool HasSubsectionsViaSymbols) {
373 return (unsigned)Kind |
374 (HasSubsectionsViaSymbols ? HasSubsectionsViaSymbolsBit : 0);
375 }
376
377 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
378 const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
379
380public:
381 /// \name Construction
382 /// @{
383
384 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
385 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
386 }
387
388 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
389 MCContext &Ctx, SMLoc Loc = SMLoc());
390 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
391 MCContext &Ctx);
392
393 /// @}
394 /// \name Accessors
395 /// @{
396
397 const MCSymbol &getSymbol() const { return *Symbol; }
398
399 VariantKind getKind() const {
400 return (VariantKind)(getSubclassData() & VariantKindMask);
401 }
402
403 bool hasSubsectionsViaSymbols() const {
404 return (getSubclassData() & HasSubsectionsViaSymbolsBit) != 0;
405 }
406
407 /// @}
408 /// \name Static Utility Functions
409 /// @{
410
411 static StringRef getVariantKindName(VariantKind Kind);
412
413 static VariantKind getVariantKindForName(StringRef Name);
414
415 /// @}
416
417 static bool classof(const MCExpr *E) {
418 return E->getKind() == MCExpr::SymbolRef;
419 }
420};
421
422/// Unary assembler expressions.
423class MCUnaryExpr : public MCExpr {
424public:
425 enum Opcode {
426 LNot, ///< Logical negation.
427 Minus, ///< Unary minus.
428 Not, ///< Bitwise negation.
429 Plus ///< Unary plus.
430 };
431
432private:
433 const MCExpr *Expr;
434
435 MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
436 : MCExpr(MCExpr::Unary, Loc, Op), Expr(Expr) {}
437
438public:
439 /// \name Construction
440 /// @{
441
442 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
443 MCContext &Ctx, SMLoc Loc = SMLoc());
444
445 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
446 return create(LNot, Expr, Ctx, Loc);
447 }
448
449 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
450 return create(Minus, Expr, Ctx, Loc);
451 }
452
453 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
454 return create(Not, Expr, Ctx, Loc);
455 }
456
457 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
458 return create(Plus, Expr, Ctx, Loc);
459 }
460
461 /// @}
462 /// \name Accessors
463 /// @{
464
465 /// Get the kind of this unary expression.
466 Opcode getOpcode() const { return (Opcode)getSubclassData(); }
467
468 /// Get the child of this unary expression.
469 const MCExpr *getSubExpr() const { return Expr; }
470
471 /// @}
472
473 static bool classof(const MCExpr *E) {
474 return E->getKind() == MCExpr::Unary;
475 }
476};
477
478/// Binary assembler expressions.
479class MCBinaryExpr : public MCExpr {
480public:
481 enum Opcode {
482 Add, ///< Addition.
483 And, ///< Bitwise and.
484 Div, ///< Signed division.
485 EQ, ///< Equality comparison.
486 GT, ///< Signed greater than comparison (result is either 0 or some
487 ///< target-specific non-zero value)
488 GTE, ///< Signed greater than or equal comparison (result is either 0 or
489 ///< some target-specific non-zero value).
490 LAnd, ///< Logical and.
491 LOr, ///< Logical or.
492 LT, ///< Signed less than comparison (result is either 0 or
493 ///< some target-specific non-zero value).
494 LTE, ///< Signed less than or equal comparison (result is either 0 or
495 ///< some target-specific non-zero value).
496 Mod, ///< Signed remainder.
497 Mul, ///< Multiplication.
498 NE, ///< Inequality comparison.
499 Or, ///< Bitwise or.
500 OrNot, ///< Bitwise or not.
501 Shl, ///< Shift left.
502 AShr, ///< Arithmetic shift right.
503 LShr, ///< Logical shift right.
504 Sub, ///< Subtraction.
505 Xor ///< Bitwise exclusive or.
506 };
507
508private:
509 const MCExpr *LHS, *RHS;
510
511 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
512 SMLoc Loc = SMLoc())
513 : MCExpr(MCExpr::Binary, Loc, Op), LHS(LHS), RHS(RHS) {}
514
515public:
516 /// \name Construction
517 /// @{
518
519 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
520 const MCExpr *RHS, MCContext &Ctx,
521 SMLoc Loc = SMLoc());
522
523 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
524 MCContext &Ctx) {
525 return create(Add, LHS, RHS, Ctx);
526 }
527
528 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
529 MCContext &Ctx) {
530 return create(And, LHS, RHS, Ctx);
531 }
532
533 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
534 MCContext &Ctx) {
535 return create(Div, LHS, RHS, Ctx);
536 }
537
538 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
539 MCContext &Ctx) {
540 return create(EQ, LHS, RHS, Ctx);
541 }
542
543 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
544 MCContext &Ctx) {
545 return create(GT, LHS, RHS, Ctx);
546 }
547
548 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
549 MCContext &Ctx) {
550 return create(GTE, LHS, RHS, Ctx);
551 }
552
553 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
554 MCContext &Ctx) {
555 return create(LAnd, LHS, RHS, Ctx);
556 }
557
558 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
559 MCContext &Ctx) {
560 return create(LOr, LHS, RHS, Ctx);
561 }
562
563 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
564 MCContext &Ctx) {
565 return create(LT, LHS, RHS, Ctx);
566 }
567
568 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
569 MCContext &Ctx) {
570 return create(LTE, LHS, RHS, Ctx);
571 }
572
573 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
574 MCContext &Ctx) {
575 return create(Mod, LHS, RHS, Ctx);
576 }
577
578 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
579 MCContext &Ctx) {
580 return create(Mul, LHS, RHS, Ctx);
581 }
582
583 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
584 MCContext &Ctx) {
585 return create(NE, LHS, RHS, Ctx);
586 }
587
588 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
589 MCContext &Ctx) {
590 return create(Or, LHS, RHS, Ctx);
591 }
592
593 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
594 MCContext &Ctx) {
595 return create(Shl, LHS, RHS, Ctx);
596 }
597
598 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
599 MCContext &Ctx) {
600 return create(AShr, LHS, RHS, Ctx);
601 }
602
603 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
604 MCContext &Ctx) {
605 return create(LShr, LHS, RHS, Ctx);
606 }
607
608 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
609 MCContext &Ctx) {
610 return create(Sub, LHS, RHS, Ctx);
611 }
612
613 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
614 MCContext &Ctx) {
615 return create(Xor, LHS, RHS, Ctx);
616 }
617
618 /// @}
619 /// \name Accessors
620 /// @{
621
622 /// Get the kind of this binary expression.
623 Opcode getOpcode() const { return (Opcode)getSubclassData(); }
624
625 /// Get the left-hand side expression of the binary operator.
626 const MCExpr *getLHS() const { return LHS; }
627
628 /// Get the right-hand side expression of the binary operator.
629 const MCExpr *getRHS() const { return RHS; }
630
631 /// @}
632
633 static bool classof(const MCExpr *E) {
634 return E->getKind() == MCExpr::Binary;
635 }
636};
637
638/// This is an extension point for target-specific MCExpr subclasses to
639/// implement.
640///
641/// NOTE: All subclasses are required to have trivial destructors because
642/// MCExprs are bump pointer allocated and not destructed.
643class MCTargetExpr : public MCExpr {
644 virtual void anchor();
645
646protected:
647 MCTargetExpr() : MCExpr(Target, SMLoc()) {}
648 virtual ~MCTargetExpr() = default;
649
650public:
651 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
652 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
653 const MCAsmLayout *Layout,
654 const MCFixup *Fixup) const = 0;
655 // allow Target Expressions to be checked for equality
656 virtual bool isEqualTo(const MCExpr *x) const { return false; }
657 // This should be set when assigned expressions are not valid ".set"
658 // expressions, e.g. registers, and must be inlined.
659 virtual bool inlineAssignedExpr() const { return false; }
660 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
661 virtual MCFragment *findAssociatedFragment() const = 0;
662
663 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
664
665 static bool classof(const MCExpr *E) {
666 return E->getKind() == MCExpr::Target;
667 }
668};
669
670} // end namespace llvm
671
672#endif // LLVM_MC_MCEXPR_H