1 //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCEXPR_H
11 #define LLVM_MC_MCEXPR_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/Support/SMLoc.h"
32 using SectionAddrMap = DenseMap<const MCSection *, uint64_t>;
34 /// \brief Base class for the full range of assembler expressions which are
35 /// needed for parsing.
39 Binary, ///< Binary expressions.
40 Constant, ///< Constant expressions.
41 SymbolRef, ///< References to labels and assigned expressions.
42 Unary, ///< Unary expressions.
43 Target ///< Target specific expression.
50 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
51 const MCAsmLayout *Layout,
52 const SectionAddrMap *Addrs) const;
54 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
55 const MCAsmLayout *Layout,
56 const SectionAddrMap *Addrs, bool InSet) const;
59 explicit MCExpr(ExprKind Kind, SMLoc Loc) : Kind(Kind), Loc(Loc) {}
61 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
62 const MCAsmLayout *Layout,
64 const SectionAddrMap *Addrs, bool InSet) const;
67 MCExpr(const MCExpr &) = delete;
68 MCExpr &operator=(const MCExpr &) = delete;
73 ExprKind getKind() const { return Kind; }
74 SMLoc getLoc() const { return Loc; }
77 /// \name Utility Methods
80 void print(raw_ostream &OS, const MCAsmInfo *MAI,
81 bool InParens = false) const;
85 /// \name Expression Evaluation
88 /// \brief Try to evaluate the expression to an absolute value.
90 /// \param Res - The absolute value, if evaluation succeeds.
91 /// \param Layout - The assembler layout object to use for evaluating symbol
92 /// values. If not given, then only non-symbolic expressions will be
94 /// \return - True on success.
95 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
96 const SectionAddrMap &Addrs) const;
97 bool evaluateAsAbsolute(int64_t &Res) const;
98 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
99 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
101 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
103 /// \brief Try to evaluate the expression to a relocatable value, i.e. an
104 /// expression of the fixed form (a - b + constant).
106 /// \param Res - The relocatable value, if evaluation succeeds.
107 /// \param Layout - The assembler layout object to use for evaluating values.
108 /// \param Fixup - The Fixup object if available.
109 /// \return - True on success.
110 bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
111 const MCFixup *Fixup) const;
113 /// \brief Try to evaluate the expression to the form (a - b + constant) where
114 /// neither a nor b are variables.
116 /// This is a more aggressive variant of evaluateAsRelocatable. The intended
117 /// use is for when relocations are not available, like the .size directive.
118 bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
120 /// \brief Find the "associated section" for this expression, which is
121 /// currently defined as the absolute section for constants, or
122 /// otherwise the section associated with the first defined symbol in the
124 MCFragment *findAssociatedFragment() const;
129 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
130 E.print(OS, nullptr);
134 //// \brief Represent a constant integer expression.
135 class MCConstantExpr : public MCExpr {
138 explicit MCConstantExpr(int64_t Value)
139 : MCExpr(MCExpr::Constant, SMLoc()), Value(Value) {}
142 /// \name Construction
145 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx);
151 int64_t getValue() const { return Value; }
155 static bool classof(const MCExpr *E) {
156 return E->getKind() == MCExpr::Constant;
160 /// \brief Represent a reference to a symbol from inside an expression.
162 /// A symbol reference in an expression may be a use of a label, a use of an
163 /// assembler variable (defined constant), or constitute an implicit definition
164 /// of the symbol as external.
165 class MCSymbolRefExpr : public MCExpr {
167 enum VariantKind : uint16_t {
185 VK_TLSCALL, // symbol(tlscall)
186 VK_TLSDESC, // symbol(tlsdesc)
187 VK_TLVP, // Mach-O thread local variable relocations
195 VK_SIZE, // symbol@SIZE
196 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
205 VK_ARM_SBREL, // symbol(sbrel)
206 VK_ARM_TLSLDO, // symbol(tlsldo)
209 VK_PPC_LO, // symbol@l
210 VK_PPC_HI, // symbol@h
211 VK_PPC_HA, // symbol@ha
212 VK_PPC_HIGHER, // symbol@higher
213 VK_PPC_HIGHERA, // symbol@highera
214 VK_PPC_HIGHEST, // symbol@highest
215 VK_PPC_HIGHESTA, // symbol@highesta
216 VK_PPC_GOT_LO, // symbol@got@l
217 VK_PPC_GOT_HI, // symbol@got@h
218 VK_PPC_GOT_HA, // symbol@got@ha
219 VK_PPC_TOCBASE, // symbol@tocbase
220 VK_PPC_TOC, // symbol@toc
221 VK_PPC_TOC_LO, // symbol@toc@l
222 VK_PPC_TOC_HI, // symbol@toc@h
223 VK_PPC_TOC_HA, // symbol@toc@ha
224 VK_PPC_DTPMOD, // symbol@dtpmod
225 VK_PPC_TPREL_LO, // symbol@tprel@l
226 VK_PPC_TPREL_HI, // symbol@tprel@h
227 VK_PPC_TPREL_HA, // symbol@tprel@ha
228 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
229 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
230 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
231 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
232 VK_PPC_DTPREL_LO, // symbol@dtprel@l
233 VK_PPC_DTPREL_HI, // symbol@dtprel@h
234 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
235 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
236 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
237 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
238 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
239 VK_PPC_GOT_TPREL, // symbol@got@tprel
240 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
241 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
242 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
243 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
244 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
245 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
246 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
247 VK_PPC_TLS, // symbol@tls
248 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
249 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
250 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
251 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
252 VK_PPC_TLSGD, // symbol@tlsgd
253 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
254 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
255 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
256 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
257 VK_PPC_TLSLD, // symbol@tlsld
258 VK_PPC_LOCAL, // symbol@local
260 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
273 VK_WebAssembly_FUNCTION, // Function table index, rather than virtual addr
274 VK_WebAssembly_TYPEINDEX,// Type table index
276 VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
277 VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
278 VK_AMDGPU_REL32_LO, // symbol@rel32@lo
279 VK_AMDGPU_REL32_HI, // symbol@rel32@hi
286 /// The symbol reference modifier.
287 const VariantKind Kind;
289 /// Specifies how the variant kind should be printed.
290 const unsigned UseParensForSymbolVariant : 1;
292 // FIXME: Remove this bit.
293 const unsigned HasSubsectionsViaSymbols : 1;
295 /// The symbol being referenced.
296 const MCSymbol *Symbol;
298 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
299 const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
302 /// \name Construction
305 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
306 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
309 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
310 MCContext &Ctx, SMLoc Loc = SMLoc());
311 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
318 const MCSymbol &getSymbol() const { return *Symbol; }
320 VariantKind getKind() const { return Kind; }
322 void printVariantKind(raw_ostream &OS) const;
324 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
327 /// \name Static Utility Functions
330 static StringRef getVariantKindName(VariantKind Kind);
332 static VariantKind getVariantKindForName(StringRef Name);
336 static bool classof(const MCExpr *E) {
337 return E->getKind() == MCExpr::SymbolRef;
341 /// \brief Unary assembler expressions.
342 class MCUnaryExpr : public MCExpr {
345 LNot, ///< Logical negation.
346 Minus, ///< Unary minus.
347 Not, ///< Bitwise negation.
348 Plus ///< Unary plus.
355 MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
356 : MCExpr(MCExpr::Unary, Loc), Op(Op), Expr(Expr) {}
359 /// \name Construction
362 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
363 MCContext &Ctx, SMLoc Loc = SMLoc());
365 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
366 return create(LNot, Expr, Ctx, Loc);
369 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
370 return create(Minus, Expr, Ctx, Loc);
373 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
374 return create(Not, Expr, Ctx, Loc);
377 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
378 return create(Plus, Expr, Ctx, Loc);
385 /// \brief Get the kind of this unary expression.
386 Opcode getOpcode() const { return Op; }
388 /// \brief Get the child of this unary expression.
389 const MCExpr *getSubExpr() const { return Expr; }
393 static bool classof(const MCExpr *E) {
394 return E->getKind() == MCExpr::Unary;
398 /// \brief Binary assembler expressions.
399 class MCBinaryExpr : public MCExpr {
403 And, ///< Bitwise and.
404 Div, ///< Signed division.
405 EQ, ///< Equality comparison.
406 GT, ///< Signed greater than comparison (result is either 0 or some
407 ///< target-specific non-zero value)
408 GTE, ///< Signed greater than or equal comparison (result is either 0 or
409 ///< some target-specific non-zero value).
410 LAnd, ///< Logical and.
411 LOr, ///< Logical or.
412 LT, ///< Signed less than comparison (result is either 0 or
413 ///< some target-specific non-zero value).
414 LTE, ///< Signed less than or equal comparison (result is either 0 or
415 ///< some target-specific non-zero value).
416 Mod, ///< Signed remainder.
417 Mul, ///< Multiplication.
418 NE, ///< Inequality comparison.
420 Shl, ///< Shift left.
421 AShr, ///< Arithmetic shift right.
422 LShr, ///< Logical shift right.
423 Sub, ///< Subtraction.
424 Xor ///< Bitwise exclusive or.
429 const MCExpr *LHS, *RHS;
431 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
433 : MCExpr(MCExpr::Binary, Loc), Op(Op), LHS(LHS), RHS(RHS) {}
436 /// \name Construction
439 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
440 const MCExpr *RHS, MCContext &Ctx,
441 SMLoc Loc = SMLoc());
443 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
445 return create(Add, LHS, RHS, Ctx);
448 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
450 return create(And, LHS, RHS, Ctx);
453 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
455 return create(Div, LHS, RHS, Ctx);
458 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
460 return create(EQ, LHS, RHS, Ctx);
463 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
465 return create(GT, LHS, RHS, Ctx);
468 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
470 return create(GTE, LHS, RHS, Ctx);
473 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
475 return create(LAnd, LHS, RHS, Ctx);
478 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
480 return create(LOr, LHS, RHS, Ctx);
483 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
485 return create(LT, LHS, RHS, Ctx);
488 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
490 return create(LTE, LHS, RHS, Ctx);
493 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
495 return create(Mod, LHS, RHS, Ctx);
498 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
500 return create(Mul, LHS, RHS, Ctx);
503 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
505 return create(NE, LHS, RHS, Ctx);
508 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
510 return create(Or, LHS, RHS, Ctx);
513 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
515 return create(Shl, LHS, RHS, Ctx);
518 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
520 return create(AShr, LHS, RHS, Ctx);
523 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
525 return create(LShr, LHS, RHS, Ctx);
528 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
530 return create(Sub, LHS, RHS, Ctx);
533 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
535 return create(Xor, LHS, RHS, Ctx);
542 /// \brief Get the kind of this binary expression.
543 Opcode getOpcode() const { return Op; }
545 /// \brief Get the left-hand side expression of the binary operator.
546 const MCExpr *getLHS() const { return LHS; }
548 /// \brief Get the right-hand side expression of the binary operator.
549 const MCExpr *getRHS() const { return RHS; }
553 static bool classof(const MCExpr *E) {
554 return E->getKind() == MCExpr::Binary;
558 /// \brief This is an extension point for target-specific MCExpr subclasses to
561 /// NOTE: All subclasses are required to have trivial destructors because
562 /// MCExprs are bump pointer allocated and not destructed.
563 class MCTargetExpr : public MCExpr {
564 virtual void anchor();
567 MCTargetExpr() : MCExpr(Target, SMLoc()) {}
568 virtual ~MCTargetExpr() = default;
571 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
572 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
573 const MCAsmLayout *Layout,
574 const MCFixup *Fixup) const = 0;
575 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
576 virtual MCFragment *findAssociatedFragment() const = 0;
578 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
580 static bool classof(const MCExpr *E) {
581 return E->getKind() == MCExpr::Target;
585 } // end namespace llvm
587 #endif // LLVM_MC_MCEXPR_H