[MIPS GlobalISel] Select MSA vector generic and builtin add
[llvm-complete.git] / lib / MC / MCExpr.cpp
blob813c00f6f3bb35ecf815257799d56f4cff214ef9
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 if (PrintInHex)
50 OS << "0x" << Twine::utohexstr(Value);
51 else
52 OS << Value;
53 return;
55 case MCExpr::SymbolRef: {
56 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
57 const MCSymbol &Sym = SRE.getSymbol();
58 // Parenthesize names that start with $ so that they don't look like
59 // absolute names.
60 bool UseParens =
61 !InParens && !Sym.getName().empty() && Sym.getName()[0] == '$';
62 if (UseParens) {
63 OS << '(';
64 Sym.print(OS, MAI);
65 OS << ')';
66 } else
67 Sym.print(OS, MAI);
69 if (SRE.getKind() != MCSymbolRefExpr::VK_None)
70 SRE.printVariantKind(OS);
72 return;
75 case MCExpr::Unary: {
76 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
77 switch (UE.getOpcode()) {
78 case MCUnaryExpr::LNot: OS << '!'; break;
79 case MCUnaryExpr::Minus: OS << '-'; break;
80 case MCUnaryExpr::Not: OS << '~'; break;
81 case MCUnaryExpr::Plus: OS << '+'; break;
83 bool Binary = UE.getSubExpr()->getKind() == MCExpr::Binary;
84 if (Binary) OS << "(";
85 UE.getSubExpr()->print(OS, MAI);
86 if (Binary) OS << ")";
87 return;
90 case MCExpr::Binary: {
91 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
93 // Only print parens around the LHS if it is non-trivial.
94 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
95 BE.getLHS()->print(OS, MAI);
96 } else {
97 OS << '(';
98 BE.getLHS()->print(OS, MAI);
99 OS << ')';
102 switch (BE.getOpcode()) {
103 case MCBinaryExpr::Add:
104 // Print "X-42" instead of "X+-42".
105 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
106 if (RHSC->getValue() < 0) {
107 OS << RHSC->getValue();
108 return;
112 OS << '+';
113 break;
114 case MCBinaryExpr::AShr: OS << ">>"; break;
115 case MCBinaryExpr::And: OS << '&'; break;
116 case MCBinaryExpr::Div: OS << '/'; break;
117 case MCBinaryExpr::EQ: OS << "=="; break;
118 case MCBinaryExpr::GT: OS << '>'; break;
119 case MCBinaryExpr::GTE: OS << ">="; break;
120 case MCBinaryExpr::LAnd: OS << "&&"; break;
121 case MCBinaryExpr::LOr: OS << "||"; break;
122 case MCBinaryExpr::LShr: OS << ">>"; break;
123 case MCBinaryExpr::LT: OS << '<'; break;
124 case MCBinaryExpr::LTE: OS << "<="; break;
125 case MCBinaryExpr::Mod: OS << '%'; break;
126 case MCBinaryExpr::Mul: OS << '*'; break;
127 case MCBinaryExpr::NE: OS << "!="; break;
128 case MCBinaryExpr::Or: OS << '|'; break;
129 case MCBinaryExpr::Shl: OS << "<<"; break;
130 case MCBinaryExpr::Sub: OS << '-'; break;
131 case MCBinaryExpr::Xor: OS << '^'; break;
134 // Only print parens around the LHS if it is non-trivial.
135 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
136 BE.getRHS()->print(OS, MAI);
137 } else {
138 OS << '(';
139 BE.getRHS()->print(OS, MAI);
140 OS << ')';
142 return;
146 llvm_unreachable("Invalid expression kind!");
149 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
150 LLVM_DUMP_METHOD void MCExpr::dump() const {
151 dbgs() << *this;
152 dbgs() << '\n';
154 #endif
156 /* *** */
158 const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
159 const MCExpr *RHS, MCContext &Ctx,
160 SMLoc Loc) {
161 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc);
164 const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
165 MCContext &Ctx, SMLoc Loc) {
166 return new (Ctx) MCUnaryExpr(Opc, Expr, Loc);
169 const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx,
170 bool PrintInHex) {
171 return new (Ctx) MCConstantExpr(Value, PrintInHex);
174 /* *** */
176 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
177 const MCAsmInfo *MAI, SMLoc Loc)
178 : MCExpr(MCExpr::SymbolRef, Loc), Kind(Kind),
179 UseParensForSymbolVariant(MAI->useParensForSymbolVariant()),
180 HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()),
181 Symbol(Symbol) {
182 assert(Symbol);
185 const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
186 VariantKind Kind,
187 MCContext &Ctx, SMLoc Loc) {
188 return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc);
191 const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
192 MCContext &Ctx) {
193 return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
196 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
197 switch (Kind) {
198 case VK_Invalid: return "<<invalid>>";
199 case VK_None: return "<<none>>";
201 case VK_DTPOFF: return "DTPOFF";
202 case VK_DTPREL: return "DTPREL";
203 case VK_GOT: return "GOT";
204 case VK_GOTOFF: return "GOTOFF";
205 case VK_GOTREL: return "GOTREL";
206 case VK_GOTPCREL: return "GOTPCREL";
207 case VK_GOTTPOFF: return "GOTTPOFF";
208 case VK_INDNTPOFF: return "INDNTPOFF";
209 case VK_NTPOFF: return "NTPOFF";
210 case VK_GOTNTPOFF: return "GOTNTPOFF";
211 case VK_PLT: return "PLT";
212 case VK_TLSGD: return "TLSGD";
213 case VK_TLSLD: return "TLSLD";
214 case VK_TLSLDM: return "TLSLDM";
215 case VK_TPOFF: return "TPOFF";
216 case VK_TPREL: return "TPREL";
217 case VK_TLSCALL: return "tlscall";
218 case VK_TLSDESC: return "tlsdesc";
219 case VK_TLVP: return "TLVP";
220 case VK_TLVPPAGE: return "TLVPPAGE";
221 case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
222 case VK_PAGE: return "PAGE";
223 case VK_PAGEOFF: return "PAGEOFF";
224 case VK_GOTPAGE: return "GOTPAGE";
225 case VK_GOTPAGEOFF: return "GOTPAGEOFF";
226 case VK_SECREL: return "SECREL32";
227 case VK_SIZE: return "SIZE";
228 case VK_WEAKREF: return "WEAKREF";
229 case VK_X86_ABS8: return "ABS8";
230 case VK_ARM_NONE: return "none";
231 case VK_ARM_GOT_PREL: return "GOT_PREL";
232 case VK_ARM_TARGET1: return "target1";
233 case VK_ARM_TARGET2: return "target2";
234 case VK_ARM_PREL31: return "prel31";
235 case VK_ARM_SBREL: return "sbrel";
236 case VK_ARM_TLSLDO: return "tlsldo";
237 case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
238 case VK_AVR_NONE: return "none";
239 case VK_AVR_LO8: return "lo8";
240 case VK_AVR_HI8: return "hi8";
241 case VK_AVR_HLO8: return "hlo8";
242 case VK_AVR_DIFF8: return "diff8";
243 case VK_AVR_DIFF16: return "diff16";
244 case VK_AVR_DIFF32: return "diff32";
245 case VK_PPC_LO: return "l";
246 case VK_PPC_HI: return "h";
247 case VK_PPC_HA: return "ha";
248 case VK_PPC_HIGH: return "high";
249 case VK_PPC_HIGHA: return "higha";
250 case VK_PPC_HIGHER: return "higher";
251 case VK_PPC_HIGHERA: return "highera";
252 case VK_PPC_HIGHEST: return "highest";
253 case VK_PPC_HIGHESTA: return "highesta";
254 case VK_PPC_GOT_LO: return "got@l";
255 case VK_PPC_GOT_HI: return "got@h";
256 case VK_PPC_GOT_HA: return "got@ha";
257 case VK_PPC_TOCBASE: return "tocbase";
258 case VK_PPC_TOC: return "toc";
259 case VK_PPC_TOC_LO: return "toc@l";
260 case VK_PPC_TOC_HI: return "toc@h";
261 case VK_PPC_TOC_HA: return "toc@ha";
262 case VK_PPC_U: return "u";
263 case VK_PPC_L: return "l";
264 case VK_PPC_DTPMOD: return "dtpmod";
265 case VK_PPC_TPREL_LO: return "tprel@l";
266 case VK_PPC_TPREL_HI: return "tprel@h";
267 case VK_PPC_TPREL_HA: return "tprel@ha";
268 case VK_PPC_TPREL_HIGH: return "tprel@high";
269 case VK_PPC_TPREL_HIGHA: return "tprel@higha";
270 case VK_PPC_TPREL_HIGHER: return "tprel@higher";
271 case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
272 case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
273 case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
274 case VK_PPC_DTPREL_LO: return "dtprel@l";
275 case VK_PPC_DTPREL_HI: return "dtprel@h";
276 case VK_PPC_DTPREL_HA: return "dtprel@ha";
277 case VK_PPC_DTPREL_HIGH: return "dtprel@high";
278 case VK_PPC_DTPREL_HIGHA: return "dtprel@higha";
279 case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
280 case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
281 case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
282 case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
283 case VK_PPC_GOT_TPREL: return "got@tprel";
284 case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
285 case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
286 case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
287 case VK_PPC_GOT_DTPREL: return "got@dtprel";
288 case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
289 case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
290 case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
291 case VK_PPC_TLS: return "tls";
292 case VK_PPC_GOT_TLSGD: return "got@tlsgd";
293 case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
294 case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
295 case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
296 case VK_PPC_TLSGD: return "tlsgd";
297 case VK_PPC_GOT_TLSLD: return "got@tlsld";
298 case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
299 case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
300 case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
301 case VK_PPC_TLSLD: return "tlsld";
302 case VK_PPC_LOCAL: return "local";
303 case VK_COFF_IMGREL32: return "IMGREL";
304 case VK_Hexagon_PCREL: return "PCREL";
305 case VK_Hexagon_LO16: return "LO16";
306 case VK_Hexagon_HI16: return "HI16";
307 case VK_Hexagon_GPREL: return "GPREL";
308 case VK_Hexagon_GD_GOT: return "GDGOT";
309 case VK_Hexagon_LD_GOT: return "LDGOT";
310 case VK_Hexagon_GD_PLT: return "GDPLT";
311 case VK_Hexagon_LD_PLT: return "LDPLT";
312 case VK_Hexagon_IE: return "IE";
313 case VK_Hexagon_IE_GOT: return "IEGOT";
314 case VK_WASM_TYPEINDEX: return "TYPEINDEX";
315 case VK_WASM_MBREL: return "MBREL";
316 case VK_WASM_TBREL: return "TBREL";
317 case VK_AMDGPU_GOTPCREL32_LO: return "gotpcrel32@lo";
318 case VK_AMDGPU_GOTPCREL32_HI: return "gotpcrel32@hi";
319 case VK_AMDGPU_REL32_LO: return "rel32@lo";
320 case VK_AMDGPU_REL32_HI: return "rel32@hi";
321 case VK_AMDGPU_REL64: return "rel64";
322 case VK_AMDGPU_ABS32_LO: return "abs32@lo";
323 case VK_AMDGPU_ABS32_HI: return "abs32@hi";
325 llvm_unreachable("Invalid variant kind");
328 MCSymbolRefExpr::VariantKind
329 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
330 return StringSwitch<VariantKind>(Name.lower())
331 .Case("dtprel", VK_DTPREL)
332 .Case("dtpoff", VK_DTPOFF)
333 .Case("got", VK_GOT)
334 .Case("gotoff", VK_GOTOFF)
335 .Case("gotrel", VK_GOTREL)
336 .Case("gotpcrel", VK_GOTPCREL)
337 .Case("gottpoff", VK_GOTTPOFF)
338 .Case("indntpoff", VK_INDNTPOFF)
339 .Case("ntpoff", VK_NTPOFF)
340 .Case("gotntpoff", VK_GOTNTPOFF)
341 .Case("plt", VK_PLT)
342 .Case("tlscall", VK_TLSCALL)
343 .Case("tlsdesc", VK_TLSDESC)
344 .Case("tlsgd", VK_TLSGD)
345 .Case("tlsld", VK_TLSLD)
346 .Case("tlsldm", VK_TLSLDM)
347 .Case("tpoff", VK_TPOFF)
348 .Case("tprel", VK_TPREL)
349 .Case("tlvp", VK_TLVP)
350 .Case("tlvppage", VK_TLVPPAGE)
351 .Case("tlvppageoff", VK_TLVPPAGEOFF)
352 .Case("page", VK_PAGE)
353 .Case("pageoff", VK_PAGEOFF)
354 .Case("gotpage", VK_GOTPAGE)
355 .Case("gotpageoff", VK_GOTPAGEOFF)
356 .Case("imgrel", VK_COFF_IMGREL32)
357 .Case("secrel32", VK_SECREL)
358 .Case("size", VK_SIZE)
359 .Case("abs8", VK_X86_ABS8)
360 .Case("l", VK_PPC_LO)
361 .Case("h", VK_PPC_HI)
362 .Case("ha", VK_PPC_HA)
363 .Case("high", VK_PPC_HIGH)
364 .Case("higha", VK_PPC_HIGHA)
365 .Case("higher", VK_PPC_HIGHER)
366 .Case("highera", VK_PPC_HIGHERA)
367 .Case("highest", VK_PPC_HIGHEST)
368 .Case("highesta", VK_PPC_HIGHESTA)
369 .Case("got@l", VK_PPC_GOT_LO)
370 .Case("got@h", VK_PPC_GOT_HI)
371 .Case("got@ha", VK_PPC_GOT_HA)
372 .Case("local", VK_PPC_LOCAL)
373 .Case("tocbase", VK_PPC_TOCBASE)
374 .Case("toc", VK_PPC_TOC)
375 .Case("toc@l", VK_PPC_TOC_LO)
376 .Case("toc@h", VK_PPC_TOC_HI)
377 .Case("toc@ha", VK_PPC_TOC_HA)
378 .Case("u", VK_PPC_U)
379 .Case("l", VK_PPC_L)
380 .Case("tls", VK_PPC_TLS)
381 .Case("dtpmod", VK_PPC_DTPMOD)
382 .Case("tprel@l", VK_PPC_TPREL_LO)
383 .Case("tprel@h", VK_PPC_TPREL_HI)
384 .Case("tprel@ha", VK_PPC_TPREL_HA)
385 .Case("tprel@high", VK_PPC_TPREL_HIGH)
386 .Case("tprel@higha", VK_PPC_TPREL_HIGHA)
387 .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
388 .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
389 .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
390 .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
391 .Case("dtprel@l", VK_PPC_DTPREL_LO)
392 .Case("dtprel@h", VK_PPC_DTPREL_HI)
393 .Case("dtprel@ha", VK_PPC_DTPREL_HA)
394 .Case("dtprel@high", VK_PPC_DTPREL_HIGH)
395 .Case("dtprel@higha", VK_PPC_DTPREL_HIGHA)
396 .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
397 .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
398 .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
399 .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
400 .Case("got@tprel", VK_PPC_GOT_TPREL)
401 .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
402 .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
403 .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
404 .Case("got@dtprel", VK_PPC_GOT_DTPREL)
405 .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
406 .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
407 .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
408 .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
409 .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
410 .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
411 .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
412 .Case("got@tlsld", VK_PPC_GOT_TLSLD)
413 .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
414 .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
415 .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
416 .Case("gdgot", VK_Hexagon_GD_GOT)
417 .Case("gdplt", VK_Hexagon_GD_PLT)
418 .Case("iegot", VK_Hexagon_IE_GOT)
419 .Case("ie", VK_Hexagon_IE)
420 .Case("ldgot", VK_Hexagon_LD_GOT)
421 .Case("ldplt", VK_Hexagon_LD_PLT)
422 .Case("pcrel", VK_Hexagon_PCREL)
423 .Case("none", VK_ARM_NONE)
424 .Case("got_prel", VK_ARM_GOT_PREL)
425 .Case("target1", VK_ARM_TARGET1)
426 .Case("target2", VK_ARM_TARGET2)
427 .Case("prel31", VK_ARM_PREL31)
428 .Case("sbrel", VK_ARM_SBREL)
429 .Case("tlsldo", VK_ARM_TLSLDO)
430 .Case("lo8", VK_AVR_LO8)
431 .Case("hi8", VK_AVR_HI8)
432 .Case("hlo8", VK_AVR_HLO8)
433 .Case("typeindex", VK_WASM_TYPEINDEX)
434 .Case("tbrel", VK_WASM_TBREL)
435 .Case("mbrel", VK_WASM_MBREL)
436 .Case("gotpcrel32@lo", VK_AMDGPU_GOTPCREL32_LO)
437 .Case("gotpcrel32@hi", VK_AMDGPU_GOTPCREL32_HI)
438 .Case("rel32@lo", VK_AMDGPU_REL32_LO)
439 .Case("rel32@hi", VK_AMDGPU_REL32_HI)
440 .Case("rel64", VK_AMDGPU_REL64)
441 .Case("abs32@lo", VK_AMDGPU_ABS32_LO)
442 .Case("abs32@hi", VK_AMDGPU_ABS32_HI)
443 .Default(VK_Invalid);
446 void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
447 if (UseParensForSymbolVariant)
448 OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
449 else
450 OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
453 /* *** */
455 void MCTargetExpr::anchor() {}
457 /* *** */
459 bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
460 return evaluateAsAbsolute(Res, nullptr, nullptr, nullptr, false);
463 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
464 const MCAsmLayout &Layout) const {
465 return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, false);
468 bool MCExpr::evaluateAsAbsolute(int64_t &Res,
469 const MCAsmLayout &Layout,
470 const SectionAddrMap &Addrs) const {
471 // Setting InSet causes us to absolutize differences across sections and that
472 // is what the MachO writer uses Addrs for.
473 return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs, true);
476 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
477 return evaluateAsAbsolute(Res, &Asm, nullptr, nullptr, false);
480 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const {
481 return evaluateAsAbsolute(Res, Asm, nullptr, nullptr, false);
484 bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
485 const MCAsmLayout &Layout) const {
486 return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
487 true);
490 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
491 const MCAsmLayout *Layout,
492 const SectionAddrMap *Addrs, bool InSet) const {
493 MCValue Value;
495 // Fast path constants.
496 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
497 Res = CE->getValue();
498 return true;
501 bool IsRelocatable =
502 evaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
504 // Record the current value.
505 Res = Value.getConstant();
507 return IsRelocatable && Value.isAbsolute();
510 /// Helper method for \see EvaluateSymbolAdd().
511 static void AttemptToFoldSymbolOffsetDifference(
512 const MCAssembler *Asm, const MCAsmLayout *Layout,
513 const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A,
514 const MCSymbolRefExpr *&B, int64_t &Addend) {
515 if (!A || !B)
516 return;
518 const MCSymbol &SA = A->getSymbol();
519 const MCSymbol &SB = B->getSymbol();
521 if (SA.isUndefined() || SB.isUndefined())
522 return;
524 if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
525 return;
527 if (SA.getFragment() == SB.getFragment() && !SA.isVariable() &&
528 !SA.isUnset() && !SB.isVariable() && !SB.isUnset()) {
529 Addend += (SA.getOffset() - SB.getOffset());
531 // Pointers to Thumb symbols need to have their low-bit set to allow
532 // for interworking.
533 if (Asm->isThumbFunc(&SA))
534 Addend |= 1;
536 // If symbol is labeled as micromips, we set low-bit to ensure
537 // correct offset in .gcc_except_table
538 if (Asm->getBackend().isMicroMips(&SA))
539 Addend |= 1;
541 // Clear the symbol expr pointers to indicate we have folded these
542 // operands.
543 A = B = nullptr;
544 return;
547 if (!Layout)
548 return;
550 const MCSection &SecA = *SA.getFragment()->getParent();
551 const MCSection &SecB = *SB.getFragment()->getParent();
553 if ((&SecA != &SecB) && !Addrs)
554 return;
556 // Eagerly evaluate.
557 Addend += Layout->getSymbolOffset(A->getSymbol()) -
558 Layout->getSymbolOffset(B->getSymbol());
559 if (Addrs && (&SecA != &SecB))
560 Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
562 // Pointers to Thumb symbols need to have their low-bit set to allow
563 // for interworking.
564 if (Asm->isThumbFunc(&SA))
565 Addend |= 1;
567 // If symbol is labeled as micromips, we set low-bit to ensure
568 // correct offset in .gcc_except_table
569 if (Asm->getBackend().isMicroMips(&SA))
570 Addend |= 1;
572 // Clear the symbol expr pointers to indicate we have folded these
573 // operands.
574 A = B = nullptr;
577 static bool canFold(const MCAssembler *Asm, const MCSymbolRefExpr *A,
578 const MCSymbolRefExpr *B, bool InSet) {
579 if (InSet)
580 return true;
582 if (!Asm->getBackend().requiresDiffExpressionRelocations())
583 return true;
585 const MCSymbol &CheckSym = A ? A->getSymbol() : B->getSymbol();
586 if (!CheckSym.isInSection())
587 return true;
589 if (!CheckSym.getSection().hasInstructions())
590 return true;
592 return false;
595 /// Evaluate the result of an add between (conceptually) two MCValues.
597 /// This routine conceptually attempts to construct an MCValue:
598 /// Result = (Result_A - Result_B + Result_Cst)
599 /// from two MCValue's LHS and RHS where
600 /// Result = LHS + RHS
601 /// and
602 /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
604 /// This routine attempts to aggresively fold the operands such that the result
605 /// is representable in an MCValue, but may not always succeed.
607 /// \returns True on success, false if the result is not representable in an
608 /// MCValue.
610 /// NOTE: It is really important to have both the Asm and Layout arguments.
611 /// They might look redundant, but this function can be used before layout
612 /// is done (see the object streamer for example) and having the Asm argument
613 /// lets us avoid relaxations early.
614 static bool
615 EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
616 const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
617 const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
618 int64_t RHS_Cst, MCValue &Res) {
619 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
620 // about dealing with modifiers. This will ultimately bite us, one day.
621 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
622 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
623 int64_t LHS_Cst = LHS.getConstant();
625 // Fold the result constant immediately.
626 int64_t Result_Cst = LHS_Cst + RHS_Cst;
628 assert((!Layout || Asm) &&
629 "Must have an assembler object if layout is given!");
631 // If we have a layout, we can fold resolved differences. Do not do this if
632 // the backend requires this to be emitted as individual relocations, unless
633 // the InSet flag is set to get the current difference anyway (used for
634 // example to calculate symbol sizes).
635 if (Asm && canFold(Asm, LHS_A, LHS_B, InSet)) {
636 // First, fold out any differences which are fully resolved. By
637 // reassociating terms in
638 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
639 // we have the four possible differences:
640 // (LHS_A - LHS_B),
641 // (LHS_A - RHS_B),
642 // (RHS_A - LHS_B),
643 // (RHS_A - RHS_B).
644 // Since we are attempting to be as aggressive as possible about folding, we
645 // attempt to evaluate each possible alternative.
646 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
647 Result_Cst);
648 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
649 Result_Cst);
650 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
651 Result_Cst);
652 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
653 Result_Cst);
656 // We can't represent the addition or subtraction of two symbols.
657 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
658 return false;
660 // At this point, we have at most one additive symbol and one subtractive
661 // symbol -- find them.
662 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
663 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
665 Res = MCValue::get(A, B, Result_Cst);
666 return true;
669 bool MCExpr::evaluateAsRelocatable(MCValue &Res,
670 const MCAsmLayout *Layout,
671 const MCFixup *Fixup) const {
672 MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
673 return evaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
674 false);
677 bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
678 MCAssembler *Assembler = &Layout.getAssembler();
679 return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
680 true);
683 static bool canExpand(const MCSymbol &Sym, bool InSet) {
684 const MCExpr *Expr = Sym.getVariableValue();
685 const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
686 if (Inner) {
687 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
688 return false;
691 if (InSet)
692 return true;
693 return !Sym.isInSection();
696 bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
697 const MCAsmLayout *Layout,
698 const MCFixup *Fixup,
699 const SectionAddrMap *Addrs,
700 bool InSet) const {
701 ++stats::MCExprEvaluate;
703 switch (getKind()) {
704 case Target:
705 return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
706 Fixup);
708 case Constant:
709 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
710 return true;
712 case SymbolRef: {
713 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
714 const MCSymbol &Sym = SRE->getSymbol();
716 // Evaluate recursively if this is a variable.
717 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None &&
718 canExpand(Sym, InSet)) {
719 bool IsMachO = SRE->hasSubsectionsViaSymbols();
720 if (Sym.getVariableValue()->evaluateAsRelocatableImpl(
721 Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) {
722 if (!IsMachO)
723 return true;
725 const MCSymbolRefExpr *A = Res.getSymA();
726 const MCSymbolRefExpr *B = Res.getSymB();
727 // FIXME: This is small hack. Given
728 // a = b + 4
729 // .long a
730 // the OS X assembler will completely drop the 4. We should probably
731 // include it in the relocation or produce an error if that is not
732 // possible.
733 // Allow constant expressions.
734 if (!A && !B)
735 return true;
736 // Allows aliases with zero offset.
737 if (Res.getConstant() == 0 && (!A || !B))
738 return true;
742 Res = MCValue::get(SRE, nullptr, 0);
743 return true;
746 case Unary: {
747 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
748 MCValue Value;
750 if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
751 Addrs, InSet))
752 return false;
754 switch (AUE->getOpcode()) {
755 case MCUnaryExpr::LNot:
756 if (!Value.isAbsolute())
757 return false;
758 Res = MCValue::get(!Value.getConstant());
759 break;
760 case MCUnaryExpr::Minus:
761 /// -(a - b + const) ==> (b - a - const)
762 if (Value.getSymA() && !Value.getSymB())
763 return false;
765 // The cast avoids undefined behavior if the constant is INT64_MIN.
766 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
767 -(uint64_t)Value.getConstant());
768 break;
769 case MCUnaryExpr::Not:
770 if (!Value.isAbsolute())
771 return false;
772 Res = MCValue::get(~Value.getConstant());
773 break;
774 case MCUnaryExpr::Plus:
775 Res = Value;
776 break;
779 return true;
782 case Binary: {
783 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
784 MCValue LHSValue, RHSValue;
786 if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
787 Addrs, InSet) ||
788 !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
789 Addrs, InSet)) {
790 // Check if both are Target Expressions, see if we can compare them.
791 if (const MCTargetExpr *L = dyn_cast<MCTargetExpr>(ABE->getLHS()))
792 if (const MCTargetExpr *R = cast<MCTargetExpr>(ABE->getRHS())) {
793 switch (ABE->getOpcode()) {
794 case MCBinaryExpr::EQ:
795 Res = MCValue::get((L->isEqualTo(R)) ? -1 : 0);
796 return true;
797 case MCBinaryExpr::NE:
798 Res = MCValue::get((R->isEqualTo(R)) ? 0 : -1);
799 return true;
800 default: break;
803 return false;
806 // We only support a few operations on non-constant expressions, handle
807 // those first.
808 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
809 switch (ABE->getOpcode()) {
810 default:
811 return false;
812 case MCBinaryExpr::Sub:
813 // Negate RHS and add.
814 // The cast avoids undefined behavior if the constant is INT64_MIN.
815 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
816 RHSValue.getSymB(), RHSValue.getSymA(),
817 -(uint64_t)RHSValue.getConstant(), Res);
819 case MCBinaryExpr::Add:
820 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
821 RHSValue.getSymA(), RHSValue.getSymB(),
822 RHSValue.getConstant(), Res);
826 // FIXME: We need target hooks for the evaluation. It may be limited in
827 // width, and gas defines the result of comparisons differently from
828 // Apple as.
829 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
830 int64_t Result = 0;
831 auto Op = ABE->getOpcode();
832 switch (Op) {
833 case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
834 case MCBinaryExpr::Add: Result = LHS + RHS; break;
835 case MCBinaryExpr::And: Result = LHS & RHS; break;
836 case MCBinaryExpr::Div:
837 case MCBinaryExpr::Mod:
838 // Handle division by zero. gas just emits a warning and keeps going,
839 // we try to be stricter.
840 // FIXME: Currently the caller of this function has no way to understand
841 // we're bailing out because of 'division by zero'. Therefore, it will
842 // emit a 'expected relocatable expression' error. It would be nice to
843 // change this code to emit a better diagnostic.
844 if (RHS == 0)
845 return false;
846 if (ABE->getOpcode() == MCBinaryExpr::Div)
847 Result = LHS / RHS;
848 else
849 Result = LHS % RHS;
850 break;
851 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
852 case MCBinaryExpr::GT: Result = LHS > RHS; break;
853 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
854 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
855 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
856 case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
857 case MCBinaryExpr::LT: Result = LHS < RHS; break;
858 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
859 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
860 case MCBinaryExpr::NE: Result = LHS != RHS; break;
861 case MCBinaryExpr::Or: Result = LHS | RHS; break;
862 case MCBinaryExpr::Shl: Result = uint64_t(LHS) << uint64_t(RHS); break;
863 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
864 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
867 switch (Op) {
868 default:
869 Res = MCValue::get(Result);
870 break;
871 case MCBinaryExpr::EQ:
872 case MCBinaryExpr::GT:
873 case MCBinaryExpr::GTE:
874 case MCBinaryExpr::LT:
875 case MCBinaryExpr::LTE:
876 case MCBinaryExpr::NE:
877 // A comparison operator returns a -1 if true and 0 if false.
878 Res = MCValue::get(Result ? -1 : 0);
879 break;
882 return true;
886 llvm_unreachable("Invalid assembly expression kind!");
889 MCFragment *MCExpr::findAssociatedFragment() const {
890 switch (getKind()) {
891 case Target:
892 // We never look through target specific expressions.
893 return cast<MCTargetExpr>(this)->findAssociatedFragment();
895 case Constant:
896 return MCSymbol::AbsolutePseudoFragment;
898 case SymbolRef: {
899 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
900 const MCSymbol &Sym = SRE->getSymbol();
901 return Sym.getFragment();
904 case Unary:
905 return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
907 case Binary: {
908 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
909 MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment();
910 MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment();
912 // If either is absolute, return the other.
913 if (LHS_F == MCSymbol::AbsolutePseudoFragment)
914 return RHS_F;
915 if (RHS_F == MCSymbol::AbsolutePseudoFragment)
916 return LHS_F;
918 // Not always correct, but probably the best we can do without more context.
919 if (BE->getOpcode() == MCBinaryExpr::Sub)
920 return MCSymbol::AbsolutePseudoFragment;
922 // Otherwise, return the first non-null fragment.
923 return LHS_F ? LHS_F : RHS_F;
927 llvm_unreachable("Invalid assembly expression kind!");