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)
217 VK_PPC_LO, // symbol@l
218 VK_PPC_HI, // symbol@h
219 VK_PPC_HA, // symbol@ha
220 VK_PPC_HIGHER, // symbol@higher
221 VK_PPC_HIGHERA, // symbol@highera
222 VK_PPC_HIGHEST, // symbol@highest
223 VK_PPC_HIGHESTA, // symbol@highesta
224 VK_PPC_GOT_LO, // symbol@got@l
225 VK_PPC_GOT_HI, // symbol@got@h
226 VK_PPC_GOT_HA, // symbol@got@ha
227 VK_PPC_TOCBASE, // symbol@tocbase
228 VK_PPC_TOC, // symbol@toc
229 VK_PPC_TOC_LO, // symbol@toc@l
230 VK_PPC_TOC_HI, // symbol@toc@h
231 VK_PPC_TOC_HA, // symbol@toc@ha
232 VK_PPC_DTPMOD, // symbol@dtpmod
233 VK_PPC_TPREL_LO, // symbol@tprel@l
234 VK_PPC_TPREL_HI, // symbol@tprel@h
235 VK_PPC_TPREL_HA, // symbol@tprel@ha
236 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
237 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
238 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
239 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
240 VK_PPC_DTPREL_LO, // symbol@dtprel@l
241 VK_PPC_DTPREL_HI, // symbol@dtprel@h
242 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
243 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
244 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
245 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
246 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
247 VK_PPC_GOT_TPREL, // symbol@got@tprel
248 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
249 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
250 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
251 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
252 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
253 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
254 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
255 VK_PPC_TLS, // symbol@tls
256 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
257 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
258 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
259 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
260 VK_PPC_TLSGD, // symbol@tlsgd
261 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
262 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
263 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
264 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
265 VK_PPC_TLSLD, // symbol@tlsld
266 VK_PPC_LOCAL, // symbol@local
268 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
281 VK_WebAssembly_FUNCTION, // Function table index, rather than virtual addr
282 VK_WebAssembly_TYPEINDEX,// Type table index
284 VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
285 VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
286 VK_AMDGPU_REL32_LO, // symbol@rel32@lo
287 VK_AMDGPU_REL32_HI, // symbol@rel32@hi
294 /// The symbol reference modifier.
295 const VariantKind Kind;
297 /// Specifies how the variant kind should be printed.
298 const unsigned UseParensForSymbolVariant : 1;
300 // FIXME: Remove this bit.
301 const unsigned HasSubsectionsViaSymbols : 1;
303 /// The symbol being referenced.
304 const MCSymbol *Symbol;
306 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
307 const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
310 /// \name Construction
313 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
314 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
317 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
318 MCContext &Ctx, SMLoc Loc = SMLoc());
319 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
326 const MCSymbol &getSymbol() const { return *Symbol; }
328 VariantKind getKind() const { return Kind; }
330 void printVariantKind(raw_ostream &OS) const;
332 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
335 /// \name Static Utility Functions
338 static StringRef getVariantKindName(VariantKind Kind);
340 static VariantKind getVariantKindForName(StringRef Name);
344 static bool classof(const MCExpr *E) {
345 return E->getKind() == MCExpr::SymbolRef;
349 /// \brief Unary assembler expressions.
350 class MCUnaryExpr : public MCExpr {
353 LNot, ///< Logical negation.
354 Minus, ///< Unary minus.
355 Not, ///< Bitwise negation.
356 Plus ///< Unary plus.
363 MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
364 : MCExpr(MCExpr::Unary, Loc), Op(Op), Expr(Expr) {}
367 /// \name Construction
370 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
371 MCContext &Ctx, SMLoc Loc = SMLoc());
373 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
374 return create(LNot, Expr, Ctx, Loc);
377 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
378 return create(Minus, Expr, Ctx, Loc);
381 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
382 return create(Not, Expr, Ctx, Loc);
385 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
386 return create(Plus, Expr, Ctx, Loc);
393 /// \brief Get the kind of this unary expression.
394 Opcode getOpcode() const { return Op; }
396 /// \brief Get the child of this unary expression.
397 const MCExpr *getSubExpr() const { return Expr; }
401 static bool classof(const MCExpr *E) {
402 return E->getKind() == MCExpr::Unary;
406 /// \brief Binary assembler expressions.
407 class MCBinaryExpr : public MCExpr {
411 And, ///< Bitwise and.
412 Div, ///< Signed division.
413 EQ, ///< Equality comparison.
414 GT, ///< Signed greater than comparison (result is either 0 or some
415 ///< target-specific non-zero value)
416 GTE, ///< Signed greater than or equal comparison (result is either 0 or
417 ///< some target-specific non-zero value).
418 LAnd, ///< Logical and.
419 LOr, ///< Logical or.
420 LT, ///< Signed less than comparison (result is either 0 or
421 ///< some target-specific non-zero value).
422 LTE, ///< Signed less than or equal comparison (result is either 0 or
423 ///< some target-specific non-zero value).
424 Mod, ///< Signed remainder.
425 Mul, ///< Multiplication.
426 NE, ///< Inequality comparison.
428 Shl, ///< Shift left.
429 AShr, ///< Arithmetic shift right.
430 LShr, ///< Logical shift right.
431 Sub, ///< Subtraction.
432 Xor ///< Bitwise exclusive or.
437 const MCExpr *LHS, *RHS;
439 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
441 : MCExpr(MCExpr::Binary, Loc), Op(Op), LHS(LHS), RHS(RHS) {}
444 /// \name Construction
447 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
448 const MCExpr *RHS, MCContext &Ctx,
449 SMLoc Loc = SMLoc());
451 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
453 return create(Add, LHS, RHS, Ctx);
456 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
458 return create(And, LHS, RHS, Ctx);
461 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
463 return create(Div, LHS, RHS, Ctx);
466 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
468 return create(EQ, LHS, RHS, Ctx);
471 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
473 return create(GT, LHS, RHS, Ctx);
476 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
478 return create(GTE, LHS, RHS, Ctx);
481 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
483 return create(LAnd, LHS, RHS, Ctx);
486 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
488 return create(LOr, LHS, RHS, Ctx);
491 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
493 return create(LT, LHS, RHS, Ctx);
496 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
498 return create(LTE, LHS, RHS, Ctx);
501 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
503 return create(Mod, LHS, RHS, Ctx);
506 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
508 return create(Mul, LHS, RHS, Ctx);
511 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
513 return create(NE, LHS, RHS, Ctx);
516 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
518 return create(Or, LHS, RHS, Ctx);
521 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
523 return create(Shl, LHS, RHS, Ctx);
526 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
528 return create(AShr, LHS, RHS, Ctx);
531 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
533 return create(LShr, LHS, RHS, Ctx);
536 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
538 return create(Sub, LHS, RHS, Ctx);
541 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
543 return create(Xor, LHS, RHS, Ctx);
550 /// \brief Get the kind of this binary expression.
551 Opcode getOpcode() const { return Op; }
553 /// \brief Get the left-hand side expression of the binary operator.
554 const MCExpr *getLHS() const { return LHS; }
556 /// \brief Get the right-hand side expression of the binary operator.
557 const MCExpr *getRHS() const { return RHS; }
561 static bool classof(const MCExpr *E) {
562 return E->getKind() == MCExpr::Binary;
566 /// \brief This is an extension point for target-specific MCExpr subclasses to
569 /// NOTE: All subclasses are required to have trivial destructors because
570 /// MCExprs are bump pointer allocated and not destructed.
571 class MCTargetExpr : public MCExpr {
572 virtual void anchor();
575 MCTargetExpr() : MCExpr(Target, SMLoc()) {}
576 virtual ~MCTargetExpr() = default;
579 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
580 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
581 const MCAsmLayout *Layout,
582 const MCFixup *Fixup) const = 0;
583 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
584 virtual MCFragment *findAssociatedFragment() const = 0;
586 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
588 static bool classof(const MCExpr *E) {
589 return E->getKind() == MCExpr::Target;
593 } // end namespace llvm
595 #endif // LLVM_MC_MCEXPR_H