[ValueTracking] Remove unused matchSelectPattern optional argument. NFCI.
[llvm-core.git] / include / llvm / MC / MCExpr.h
blob74c3392df755fe7011f5944f1730dcad660b889f
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_MC_MCEXPR_H
10 #define LLVM_MC_MCEXPR_H
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/Support/SMLoc.h"
14 #include <cstdint>
16 namespace llvm {
18 class MCAsmInfo;
19 class MCAsmLayout;
20 class MCAssembler;
21 class MCContext;
22 class MCFixup;
23 class MCFragment;
24 class MCSection;
25 class MCStreamer;
26 class MCSymbol;
27 class MCValue;
28 class raw_ostream;
29 class StringRef;
31 using SectionAddrMap = DenseMap<const MCSection *, uint64_t>;
33 /// Base class for the full range of assembler expressions which are
34 /// needed for parsing.
35 class MCExpr {
36 public:
37 enum ExprKind {
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.
45 private:
46 ExprKind Kind;
47 SMLoc Loc;
49 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
50 const MCAsmLayout *Layout,
51 const SectionAddrMap *Addrs, bool InSet) const;
53 protected:
54 explicit MCExpr(ExprKind Kind, SMLoc Loc) : Kind(Kind), Loc(Loc) {}
56 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
57 const MCAsmLayout *Layout,
58 const MCFixup *Fixup,
59 const SectionAddrMap *Addrs, bool InSet) const;
61 public:
62 MCExpr(const MCExpr &) = delete;
63 MCExpr &operator=(const MCExpr &) = delete;
65 /// \name Accessors
66 /// @{
68 ExprKind getKind() const { return Kind; }
69 SMLoc getLoc() const { return Loc; }
71 /// @}
72 /// \name Utility Methods
73 /// @{
75 void print(raw_ostream &OS, const MCAsmInfo *MAI,
76 bool InParens = false) const;
77 void dump() const;
79 /// @}
80 /// \name Expression Evaluation
81 /// @{
83 /// Try to evaluate the expression to an absolute value.
84 ///
85 /// \param Res - The absolute value, if evaluation succeeds.
86 /// \param Layout - The assembler layout object to use for evaluating symbol
87 /// values. If not given, then only non-symbolic expressions will be
88 /// evaluated.
89 /// \return - True on success.
90 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
91 const SectionAddrMap &Addrs) const;
92 bool evaluateAsAbsolute(int64_t &Res) const;
93 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
94 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const;
95 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
97 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
99 /// Try to evaluate the expression to a relocatable value, i.e. an
100 /// expression of the fixed form (a - b + constant).
102 /// \param Res - The relocatable value, if evaluation succeeds.
103 /// \param Layout - The assembler layout object to use for evaluating values.
104 /// \param Fixup - The Fixup object if available.
105 /// \return - True on success.
106 bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
107 const MCFixup *Fixup) const;
109 /// Try to evaluate the expression to the form (a - b + constant) where
110 /// neither a nor b are variables.
112 /// This is a more aggressive variant of evaluateAsRelocatable. The intended
113 /// use is for when relocations are not available, like the .size directive.
114 bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
116 /// Find the "associated section" for this expression, which is
117 /// currently defined as the absolute section for constants, or
118 /// otherwise the section associated with the first defined symbol in the
119 /// expression.
120 MCFragment *findAssociatedFragment() const;
122 /// @}
125 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
126 E.print(OS, nullptr);
127 return OS;
130 //// Represent a constant integer expression.
131 class MCConstantExpr : public MCExpr {
132 int64_t Value;
133 bool PrintInHex = false;
135 explicit MCConstantExpr(int64_t Value)
136 : MCExpr(MCExpr::Constant, SMLoc()), Value(Value) {}
138 MCConstantExpr(int64_t Value, bool PrintInHex)
139 : MCExpr(MCExpr::Constant, SMLoc()), Value(Value),
140 PrintInHex(PrintInHex) {}
142 public:
143 /// \name Construction
144 /// @{
146 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx,
147 bool PrintInHex = false);
149 /// @}
150 /// \name Accessors
151 /// @{
153 int64_t getValue() const { return Value; }
155 bool useHexFormat() const { return PrintInHex; }
157 /// @}
159 static bool classof(const MCExpr *E) {
160 return E->getKind() == MCExpr::Constant;
164 /// Represent a reference to a symbol from inside an expression.
166 /// A symbol reference in an expression may be a use of a label, a use of an
167 /// assembler variable (defined constant), or constitute an implicit definition
168 /// of the symbol as external.
169 class MCSymbolRefExpr : public MCExpr {
170 public:
171 enum VariantKind : uint16_t {
172 VK_None,
173 VK_Invalid,
175 VK_GOT,
176 VK_GOTOFF,
177 VK_GOTREL,
178 VK_GOTPCREL,
179 VK_GOTTPOFF,
180 VK_INDNTPOFF,
181 VK_NTPOFF,
182 VK_GOTNTPOFF,
183 VK_PLT,
184 VK_TLSGD,
185 VK_TLSLD,
186 VK_TLSLDM,
187 VK_TPOFF,
188 VK_DTPOFF,
189 VK_TLSCALL, // symbol(tlscall)
190 VK_TLSDESC, // symbol(tlsdesc)
191 VK_TLVP, // Mach-O thread local variable relocations
192 VK_TLVPPAGE,
193 VK_TLVPPAGEOFF,
194 VK_PAGE,
195 VK_PAGEOFF,
196 VK_GOTPAGE,
197 VK_GOTPAGEOFF,
198 VK_SECREL,
199 VK_SIZE, // symbol@SIZE
200 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
202 VK_X86_ABS8,
204 VK_ARM_NONE,
205 VK_ARM_GOT_PREL,
206 VK_ARM_TARGET1,
207 VK_ARM_TARGET2,
208 VK_ARM_PREL31,
209 VK_ARM_SBREL, // symbol(sbrel)
210 VK_ARM_TLSLDO, // symbol(tlsldo)
211 VK_ARM_TLSDESCSEQ,
213 VK_AVR_NONE,
214 VK_AVR_LO8,
215 VK_AVR_HI8,
216 VK_AVR_HLO8,
217 VK_AVR_DIFF8,
218 VK_AVR_DIFF16,
219 VK_AVR_DIFF32,
221 VK_PPC_LO, // symbol@l
222 VK_PPC_HI, // symbol@h
223 VK_PPC_HA, // symbol@ha
224 VK_PPC_HIGH, // symbol@high
225 VK_PPC_HIGHA, // symbol@higha
226 VK_PPC_HIGHER, // symbol@higher
227 VK_PPC_HIGHERA, // symbol@highera
228 VK_PPC_HIGHEST, // symbol@highest
229 VK_PPC_HIGHESTA, // symbol@highesta
230 VK_PPC_GOT_LO, // symbol@got@l
231 VK_PPC_GOT_HI, // symbol@got@h
232 VK_PPC_GOT_HA, // symbol@got@ha
233 VK_PPC_TOCBASE, // symbol@tocbase
234 VK_PPC_TOC, // symbol@toc
235 VK_PPC_TOC_LO, // symbol@toc@l
236 VK_PPC_TOC_HI, // symbol@toc@h
237 VK_PPC_TOC_HA, // symbol@toc@ha
238 VK_PPC_DTPMOD, // symbol@dtpmod
239 VK_PPC_TPREL_LO, // symbol@tprel@l
240 VK_PPC_TPREL_HI, // symbol@tprel@h
241 VK_PPC_TPREL_HA, // symbol@tprel@ha
242 VK_PPC_TPREL_HIGH, // symbol@tprel@high
243 VK_PPC_TPREL_HIGHA, // symbol@tprel@higha
244 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
245 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
246 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
247 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
248 VK_PPC_DTPREL_LO, // symbol@dtprel@l
249 VK_PPC_DTPREL_HI, // symbol@dtprel@h
250 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
251 VK_PPC_DTPREL_HIGH, // symbol@dtprel@high
252 VK_PPC_DTPREL_HIGHA, // symbol@dtprel@higha
253 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
254 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
255 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
256 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
257 VK_PPC_GOT_TPREL, // symbol@got@tprel
258 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
259 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
260 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
261 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
262 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
263 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
264 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
265 VK_PPC_TLS, // symbol@tls
266 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
267 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
268 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
269 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
270 VK_PPC_TLSGD, // symbol@tlsgd
271 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
272 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
273 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
274 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
275 VK_PPC_TLSLD, // symbol@tlsld
276 VK_PPC_LOCAL, // symbol@local
278 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
280 VK_Hexagon_PCREL,
281 VK_Hexagon_LO16,
282 VK_Hexagon_HI16,
283 VK_Hexagon_GPREL,
284 VK_Hexagon_GD_GOT,
285 VK_Hexagon_LD_GOT,
286 VK_Hexagon_GD_PLT,
287 VK_Hexagon_LD_PLT,
288 VK_Hexagon_IE,
289 VK_Hexagon_IE_GOT,
291 VK_WASM_TYPEINDEX, // Reference to a symbol's type (signature)
292 VK_WASM_MBREL, // Memory address relative to memory base
293 VK_WASM_TBREL, // Table index relative to table bare
295 VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
296 VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
297 VK_AMDGPU_REL32_LO, // symbol@rel32@lo
298 VK_AMDGPU_REL32_HI, // symbol@rel32@hi
299 VK_AMDGPU_REL64, // symbol@rel64
300 VK_AMDGPU_ABS32_LO, // symbol@abs32@lo
301 VK_AMDGPU_ABS32_HI, // symbol@abs32@hi
303 VK_TPREL,
304 VK_DTPREL
307 private:
308 /// The symbol reference modifier.
309 const VariantKind Kind;
311 /// Specifies how the variant kind should be printed.
312 const unsigned UseParensForSymbolVariant : 1;
314 // FIXME: Remove this bit.
315 const unsigned HasSubsectionsViaSymbols : 1;
317 /// The symbol being referenced.
318 const MCSymbol *Symbol;
320 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
321 const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
323 public:
324 /// \name Construction
325 /// @{
327 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
328 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
331 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
332 MCContext &Ctx, SMLoc Loc = SMLoc());
333 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
334 MCContext &Ctx);
336 /// @}
337 /// \name Accessors
338 /// @{
340 const MCSymbol &getSymbol() const { return *Symbol; }
342 VariantKind getKind() const { return Kind; }
344 void printVariantKind(raw_ostream &OS) const;
346 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
348 /// @}
349 /// \name Static Utility Functions
350 /// @{
352 static StringRef getVariantKindName(VariantKind Kind);
354 static VariantKind getVariantKindForName(StringRef Name);
356 /// @}
358 static bool classof(const MCExpr *E) {
359 return E->getKind() == MCExpr::SymbolRef;
363 /// Unary assembler expressions.
364 class MCUnaryExpr : public MCExpr {
365 public:
366 enum Opcode {
367 LNot, ///< Logical negation.
368 Minus, ///< Unary minus.
369 Not, ///< Bitwise negation.
370 Plus ///< Unary plus.
373 private:
374 Opcode Op;
375 const MCExpr *Expr;
377 MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
378 : MCExpr(MCExpr::Unary, Loc), Op(Op), Expr(Expr) {}
380 public:
381 /// \name Construction
382 /// @{
384 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
385 MCContext &Ctx, SMLoc Loc = SMLoc());
387 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
388 return create(LNot, Expr, Ctx, Loc);
391 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
392 return create(Minus, Expr, Ctx, Loc);
395 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
396 return create(Not, Expr, Ctx, Loc);
399 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
400 return create(Plus, Expr, Ctx, Loc);
403 /// @}
404 /// \name Accessors
405 /// @{
407 /// Get the kind of this unary expression.
408 Opcode getOpcode() const { return Op; }
410 /// Get the child of this unary expression.
411 const MCExpr *getSubExpr() const { return Expr; }
413 /// @}
415 static bool classof(const MCExpr *E) {
416 return E->getKind() == MCExpr::Unary;
420 /// Binary assembler expressions.
421 class MCBinaryExpr : public MCExpr {
422 public:
423 enum Opcode {
424 Add, ///< Addition.
425 And, ///< Bitwise and.
426 Div, ///< Signed division.
427 EQ, ///< Equality comparison.
428 GT, ///< Signed greater than comparison (result is either 0 or some
429 ///< target-specific non-zero value)
430 GTE, ///< Signed greater than or equal comparison (result is either 0 or
431 ///< some target-specific non-zero value).
432 LAnd, ///< Logical and.
433 LOr, ///< Logical or.
434 LT, ///< Signed less than comparison (result is either 0 or
435 ///< some target-specific non-zero value).
436 LTE, ///< Signed less than or equal comparison (result is either 0 or
437 ///< some target-specific non-zero value).
438 Mod, ///< Signed remainder.
439 Mul, ///< Multiplication.
440 NE, ///< Inequality comparison.
441 Or, ///< Bitwise or.
442 Shl, ///< Shift left.
443 AShr, ///< Arithmetic shift right.
444 LShr, ///< Logical shift right.
445 Sub, ///< Subtraction.
446 Xor ///< Bitwise exclusive or.
449 private:
450 Opcode Op;
451 const MCExpr *LHS, *RHS;
453 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
454 SMLoc Loc = SMLoc())
455 : MCExpr(MCExpr::Binary, Loc), Op(Op), LHS(LHS), RHS(RHS) {}
457 public:
458 /// \name Construction
459 /// @{
461 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
462 const MCExpr *RHS, MCContext &Ctx,
463 SMLoc Loc = SMLoc());
465 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
466 MCContext &Ctx) {
467 return create(Add, LHS, RHS, Ctx);
470 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
471 MCContext &Ctx) {
472 return create(And, LHS, RHS, Ctx);
475 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
476 MCContext &Ctx) {
477 return create(Div, LHS, RHS, Ctx);
480 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
481 MCContext &Ctx) {
482 return create(EQ, LHS, RHS, Ctx);
485 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
486 MCContext &Ctx) {
487 return create(GT, LHS, RHS, Ctx);
490 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
491 MCContext &Ctx) {
492 return create(GTE, LHS, RHS, Ctx);
495 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
496 MCContext &Ctx) {
497 return create(LAnd, LHS, RHS, Ctx);
500 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
501 MCContext &Ctx) {
502 return create(LOr, LHS, RHS, Ctx);
505 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
506 MCContext &Ctx) {
507 return create(LT, LHS, RHS, Ctx);
510 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
511 MCContext &Ctx) {
512 return create(LTE, LHS, RHS, Ctx);
515 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
516 MCContext &Ctx) {
517 return create(Mod, LHS, RHS, Ctx);
520 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
521 MCContext &Ctx) {
522 return create(Mul, LHS, RHS, Ctx);
525 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
526 MCContext &Ctx) {
527 return create(NE, LHS, RHS, Ctx);
530 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
531 MCContext &Ctx) {
532 return create(Or, LHS, RHS, Ctx);
535 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
536 MCContext &Ctx) {
537 return create(Shl, LHS, RHS, Ctx);
540 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
541 MCContext &Ctx) {
542 return create(AShr, LHS, RHS, Ctx);
545 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
546 MCContext &Ctx) {
547 return create(LShr, LHS, RHS, Ctx);
550 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
551 MCContext &Ctx) {
552 return create(Sub, LHS, RHS, Ctx);
555 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
556 MCContext &Ctx) {
557 return create(Xor, LHS, RHS, Ctx);
560 /// @}
561 /// \name Accessors
562 /// @{
564 /// Get the kind of this binary expression.
565 Opcode getOpcode() const { return Op; }
567 /// Get the left-hand side expression of the binary operator.
568 const MCExpr *getLHS() const { return LHS; }
570 /// Get the right-hand side expression of the binary operator.
571 const MCExpr *getRHS() const { return RHS; }
573 /// @}
575 static bool classof(const MCExpr *E) {
576 return E->getKind() == MCExpr::Binary;
580 /// This is an extension point for target-specific MCExpr subclasses to
581 /// implement.
583 /// NOTE: All subclasses are required to have trivial destructors because
584 /// MCExprs are bump pointer allocated and not destructed.
585 class MCTargetExpr : public MCExpr {
586 virtual void anchor();
588 protected:
589 MCTargetExpr() : MCExpr(Target, SMLoc()) {}
590 virtual ~MCTargetExpr() = default;
592 public:
593 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
594 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
595 const MCAsmLayout *Layout,
596 const MCFixup *Fixup) const = 0;
597 // allow Target Expressions to be checked for equality
598 virtual bool isEqualTo(const MCExpr *x) const { return false; }
599 // This should be set when assigned expressions are not valid ".set"
600 // expressions, e.g. registers, and must be inlined.
601 virtual bool inlineAssignedExpr() const { return false; }
602 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
603 virtual MCFragment *findAssociatedFragment() const = 0;
605 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
607 static bool classof(const MCExpr *E) {
608 return E->getKind() == MCExpr::Target;
612 } // end namespace llvm
614 #endif // LLVM_MC_MCEXPR_H