1 //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_MC_MCEXPR_H
10 #define LLVM_MC_MCEXPR_H
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/Support/SMLoc.h"
31 using SectionAddrMap = DenseMap<const MCSection *, uint64_t>;
33 /// Base class for the full range of assembler expressions which are
34 /// needed for parsing.
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.
46 static const unsigned NumSubclassDataBits = 24;
48 NumSubclassDataBits == CHAR_BIT * (sizeof(unsigned) - sizeof(ExprKind)),
49 "ExprKind and SubclassData together should take up one word");
52 /// Field reserved for use by MCExpr subclasses.
53 unsigned SubclassData : NumSubclassDataBits;
56 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
57 const MCAsmLayout *Layout,
58 const SectionAddrMap *Addrs, bool InSet) const;
61 explicit MCExpr(ExprKind Kind, SMLoc Loc, unsigned SubclassData = 0)
62 : Kind(Kind), SubclassData(SubclassData), Loc(Loc) {
63 assert(SubclassData < (1 << NumSubclassDataBits) &&
64 "Subclass data too large");
67 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
68 const MCAsmLayout *Layout,
70 const SectionAddrMap *Addrs, bool InSet) const;
72 unsigned getSubclassData() const { return SubclassData; }
75 MCExpr(const MCExpr &) = delete;
76 MCExpr &operator=(const MCExpr &) = delete;
81 ExprKind getKind() const { return Kind; }
82 SMLoc getLoc() const { return Loc; }
85 /// \name Utility Methods
88 void print(raw_ostream &OS, const MCAsmInfo *MAI,
89 bool InParens = false) const;
93 /// \name Expression Evaluation
96 /// Try to evaluate the expression to an absolute value.
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
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;
110 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
112 /// Try to evaluate the expression to a relocatable value, i.e. an
113 /// expression of the fixed form (a - b + constant).
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;
122 /// Try to evaluate the expression to the form (a - b + constant) where
123 /// neither a nor b are variables.
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;
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
133 MCFragment *findAssociatedFragment() const;
138 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
139 E.print(OS, nullptr);
143 //// Represent a constant integer expression.
144 class MCConstantExpr : public MCExpr {
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;
152 static unsigned encodeSubclassData(bool PrintInHex, unsigned SizeInBytes) {
153 assert(SizeInBytes <= sizeof(int64_t) && "Excessive size");
154 return SizeInBytes | (PrintInHex ? PrintInHexBit : 0);
157 MCConstantExpr(int64_t Value, bool PrintInHex, unsigned SizeInBytes)
158 : MCExpr(MCExpr::Constant, SMLoc(),
159 encodeSubclassData(PrintInHex, SizeInBytes)), Value(Value) {}
162 /// \name Construction
165 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx,
166 bool PrintInHex = false,
167 unsigned SizeInBytes = 0);
173 int64_t getValue() const { return Value; }
174 unsigned getSizeInBytes() const {
175 return getSubclassData() & SizeInBytesMask;
178 bool useHexFormat() const { return (getSubclassData() & PrintInHexBit) != 0; }
182 static bool classof(const MCExpr *E) {
183 return E->getKind() == MCExpr::Constant;
187 /// Represent a reference to a symbol from inside an expression.
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.
192 class MCSymbolRefExpr : public MCExpr {
194 enum VariantKind : uint16_t {
213 VK_TLSCALL, // symbol(tlscall)
214 VK_TLSDESC, // symbol(tlsdesc)
215 VK_TLVP, // Mach-O thread local variable relocations
223 VK_SIZE, // symbol@SIZE
224 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
233 VK_ARM_SBREL, // symbol(sbrel)
234 VK_ARM_TLSLDO, // symbol(tlsldo)
245 VK_PPC_LO, // symbol@l
246 VK_PPC_HI, // symbol@h
247 VK_PPC_HA, // symbol@ha
248 VK_PPC_HIGH, // symbol@high
249 VK_PPC_HIGHA, // symbol@higha
250 VK_PPC_HIGHER, // symbol@higher
251 VK_PPC_HIGHERA, // symbol@highera
252 VK_PPC_HIGHEST, // symbol@highest
253 VK_PPC_HIGHESTA, // symbol@highesta
254 VK_PPC_GOT_LO, // symbol@got@l
255 VK_PPC_GOT_HI, // symbol@got@h
256 VK_PPC_GOT_HA, // symbol@got@ha
257 VK_PPC_TOCBASE, // symbol@tocbase
258 VK_PPC_TOC, // symbol@toc
259 VK_PPC_TOC_LO, // symbol@toc@l
260 VK_PPC_TOC_HI, // symbol@toc@h
261 VK_PPC_TOC_HA, // symbol@toc@ha
262 VK_PPC_U, // symbol@u
263 VK_PPC_L, // symbol@l
264 VK_PPC_DTPMOD, // symbol@dtpmod
265 VK_PPC_TPREL_LO, // symbol@tprel@l
266 VK_PPC_TPREL_HI, // symbol@tprel@h
267 VK_PPC_TPREL_HA, // symbol@tprel@ha
268 VK_PPC_TPREL_HIGH, // symbol@tprel@high
269 VK_PPC_TPREL_HIGHA, // symbol@tprel@higha
270 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
271 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
272 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
273 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
274 VK_PPC_DTPREL_LO, // symbol@dtprel@l
275 VK_PPC_DTPREL_HI, // symbol@dtprel@h
276 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
277 VK_PPC_DTPREL_HIGH, // symbol@dtprel@high
278 VK_PPC_DTPREL_HIGHA, // symbol@dtprel@higha
279 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
280 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
281 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
282 VK_PPC_DTPREL_HIGHESTA, // symbol@dtprel@highesta
283 VK_PPC_GOT_TPREL, // symbol@got@tprel
284 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
285 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
286 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
287 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
288 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
289 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
290 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
291 VK_PPC_TLS, // symbol@tls
292 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
293 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
294 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
295 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
296 VK_PPC_TLSGD, // symbol@tlsgd
297 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
298 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
299 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
300 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
301 VK_PPC_GOT_PCREL, // symbol@got@pcrel
302 VK_PPC_TLSLD, // symbol@tlsld
303 VK_PPC_LOCAL, // symbol@local
304 VK_PPC_NOTOC, // symbol@notoc
306 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
318 VK_WASM_TYPEINDEX, // Reference to a symbol's type (signature)
319 VK_WASM_MBREL, // Memory address relative to memory base
320 VK_WASM_TBREL, // Table index relative to table bare
322 VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
323 VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
324 VK_AMDGPU_REL32_LO, // symbol@rel32@lo
325 VK_AMDGPU_REL32_HI, // symbol@rel32@hi
326 VK_AMDGPU_REL64, // symbol@rel64
327 VK_AMDGPU_ABS32_LO, // symbol@abs32@lo
328 VK_AMDGPU_ABS32_HI, // symbol@abs32@hi
330 VK_VE_HI32, // symbol@hi
331 VK_VE_LO32, // symbol@lo
332 VK_VE_PC_HI32, // symbol@pc_hi
333 VK_VE_PC_LO32, // symbol@pc_lo
334 VK_VE_GOT_HI32, // symbol@got_hi
335 VK_VE_GOT_LO32, // symbol@got_lo
336 VK_VE_GOTOFF_HI32, // symbol@gotoff_hi
337 VK_VE_GOTOFF_LO32, // symbol@gotoff_lo
338 VK_VE_PLT_HI32, // symbol@plt_hi
339 VK_VE_PLT_LO32, // symbol@plt_lo
340 VK_VE_TLS_GD_HI32, // symbol@tls_gd_hi
341 VK_VE_TLS_GD_LO32, // symbol@tls_gd_lo
342 VK_VE_TPOFF_HI32, // symbol@tpoff_hi
343 VK_VE_TPOFF_LO32, // symbol@tpoff_lo
350 /// The symbol being referenced.
351 const MCSymbol *Symbol;
353 // Subclass data stores VariantKind in bits 0..15, UseParensForSymbolVariant
354 // in bit 16 and HasSubsectionsViaSymbols in bit 17.
355 static const unsigned VariantKindBits = 16;
356 static const unsigned VariantKindMask = (1 << VariantKindBits) - 1;
358 /// Specifies how the variant kind should be printed.
359 static const unsigned UseParensForSymbolVariantBit = 1 << VariantKindBits;
361 // FIXME: Remove this bit.
362 static const unsigned HasSubsectionsViaSymbolsBit =
363 1 << (VariantKindBits + 1);
365 static unsigned encodeSubclassData(VariantKind Kind,
366 bool UseParensForSymbolVariant,
367 bool HasSubsectionsViaSymbols) {
368 return (unsigned)Kind |
369 (UseParensForSymbolVariant ? UseParensForSymbolVariantBit : 0) |
370 (HasSubsectionsViaSymbols ? HasSubsectionsViaSymbolsBit : 0);
373 bool useParensForSymbolVariant() const {
374 return (getSubclassData() & UseParensForSymbolVariantBit) != 0;
377 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
378 const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
381 /// \name Construction
384 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
385 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
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,
397 const MCSymbol &getSymbol() const { return *Symbol; }
399 VariantKind getKind() const {
400 return (VariantKind)(getSubclassData() & VariantKindMask);
403 void printVariantKind(raw_ostream &OS) const;
405 bool hasSubsectionsViaSymbols() const {
406 return (getSubclassData() & HasSubsectionsViaSymbolsBit) != 0;
410 /// \name Static Utility Functions
413 static StringRef getVariantKindName(VariantKind Kind);
415 static VariantKind getVariantKindForName(StringRef Name);
419 static bool classof(const MCExpr *E) {
420 return E->getKind() == MCExpr::SymbolRef;
424 /// Unary assembler expressions.
425 class MCUnaryExpr : public MCExpr {
428 LNot, ///< Logical negation.
429 Minus, ///< Unary minus.
430 Not, ///< Bitwise negation.
431 Plus ///< Unary plus.
437 MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
438 : MCExpr(MCExpr::Unary, Loc, Op), Expr(Expr) {}
441 /// \name Construction
444 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
445 MCContext &Ctx, SMLoc Loc = SMLoc());
447 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
448 return create(LNot, Expr, Ctx, Loc);
451 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
452 return create(Minus, Expr, Ctx, Loc);
455 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
456 return create(Not, Expr, Ctx, Loc);
459 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
460 return create(Plus, Expr, Ctx, Loc);
467 /// Get the kind of this unary expression.
468 Opcode getOpcode() const { return (Opcode)getSubclassData(); }
470 /// Get the child of this unary expression.
471 const MCExpr *getSubExpr() const { return Expr; }
475 static bool classof(const MCExpr *E) {
476 return E->getKind() == MCExpr::Unary;
480 /// Binary assembler expressions.
481 class MCBinaryExpr : public MCExpr {
485 And, ///< Bitwise and.
486 Div, ///< Signed division.
487 EQ, ///< Equality comparison.
488 GT, ///< Signed greater than comparison (result is either 0 or some
489 ///< target-specific non-zero value)
490 GTE, ///< Signed greater than or equal comparison (result is either 0 or
491 ///< some target-specific non-zero value).
492 LAnd, ///< Logical and.
493 LOr, ///< Logical or.
494 LT, ///< Signed less than comparison (result is either 0 or
495 ///< some target-specific non-zero value).
496 LTE, ///< Signed less than or equal comparison (result is either 0 or
497 ///< some target-specific non-zero value).
498 Mod, ///< Signed remainder.
499 Mul, ///< Multiplication.
500 NE, ///< Inequality comparison.
502 Shl, ///< Shift left.
503 AShr, ///< Arithmetic shift right.
504 LShr, ///< Logical shift right.
505 Sub, ///< Subtraction.
506 Xor ///< Bitwise exclusive or.
510 const MCExpr *LHS, *RHS;
512 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
514 : MCExpr(MCExpr::Binary, Loc, Op), LHS(LHS), RHS(RHS) {}
517 /// \name Construction
520 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
521 const MCExpr *RHS, MCContext &Ctx,
522 SMLoc Loc = SMLoc());
524 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
526 return create(Add, LHS, RHS, Ctx);
529 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
531 return create(And, LHS, RHS, Ctx);
534 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
536 return create(Div, LHS, RHS, Ctx);
539 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
541 return create(EQ, LHS, RHS, Ctx);
544 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
546 return create(GT, LHS, RHS, Ctx);
549 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
551 return create(GTE, LHS, RHS, Ctx);
554 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
556 return create(LAnd, LHS, RHS, Ctx);
559 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
561 return create(LOr, LHS, RHS, Ctx);
564 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
566 return create(LT, LHS, RHS, Ctx);
569 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
571 return create(LTE, LHS, RHS, Ctx);
574 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
576 return create(Mod, LHS, RHS, Ctx);
579 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
581 return create(Mul, LHS, RHS, Ctx);
584 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
586 return create(NE, LHS, RHS, Ctx);
589 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
591 return create(Or, LHS, RHS, Ctx);
594 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
596 return create(Shl, LHS, RHS, Ctx);
599 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
601 return create(AShr, LHS, RHS, Ctx);
604 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
606 return create(LShr, LHS, RHS, Ctx);
609 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
611 return create(Sub, LHS, RHS, Ctx);
614 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
616 return create(Xor, LHS, RHS, Ctx);
623 /// Get the kind of this binary expression.
624 Opcode getOpcode() const { return (Opcode)getSubclassData(); }
626 /// Get the left-hand side expression of the binary operator.
627 const MCExpr *getLHS() const { return LHS; }
629 /// Get the right-hand side expression of the binary operator.
630 const MCExpr *getRHS() const { return RHS; }
634 static bool classof(const MCExpr *E) {
635 return E->getKind() == MCExpr::Binary;
639 /// This is an extension point for target-specific MCExpr subclasses to
642 /// NOTE: All subclasses are required to have trivial destructors because
643 /// MCExprs are bump pointer allocated and not destructed.
644 class MCTargetExpr : public MCExpr {
645 virtual void anchor();
648 MCTargetExpr() : MCExpr(Target, SMLoc()) {}
649 virtual ~MCTargetExpr() = default;
652 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
653 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
654 const MCAsmLayout *Layout,
655 const MCFixup *Fixup) const = 0;
656 // allow Target Expressions to be checked for equality
657 virtual bool isEqualTo(const MCExpr *x) const { return false; }
658 // This should be set when assigned expressions are not valid ".set"
659 // expressions, e.g. registers, and must be inlined.
660 virtual bool inlineAssignedExpr() const { return false; }
661 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
662 virtual MCFragment *findAssociatedFragment() const = 0;
664 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
666 static bool classof(const MCExpr *E) {
667 return E->getKind() == MCExpr::Target;
671 } // end namespace llvm
673 #endif // LLVM_MC_MCEXPR_H