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"
31 typedef DenseMap<const MCSection *, uint64_t> SectionAddrMap;
33 /// \brief Base class for the full range of assembler expressions which are
34 /// needed for parsing.
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.
49 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
50 const MCAsmLayout *Layout,
51 const SectionAddrMap *Addrs) const;
53 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
54 const MCAsmLayout *Layout,
55 const SectionAddrMap *Addrs, bool InSet) const;
58 explicit MCExpr(ExprKind Kind, SMLoc Loc) : Kind(Kind), Loc(Loc) {}
60 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
61 const MCAsmLayout *Layout,
63 const SectionAddrMap *Addrs, bool InSet) const;
66 MCExpr(const MCExpr &) = delete;
67 MCExpr &operator=(const MCExpr &) = delete;
72 ExprKind getKind() const { return Kind; }
73 SMLoc getLoc() const { return Loc; }
76 /// \name Utility Methods
79 void print(raw_ostream &OS, const MCAsmInfo *MAI,
80 bool InParens = false) const;
84 /// \name Expression Evaluation
87 /// \brief Try to evaluate the expression to an absolute value.
89 /// \param Res - The absolute value, if evaluation succeeds.
90 /// \param Layout - The assembler layout object to use for evaluating symbol
91 /// values. If not given, then only non-symbolic expressions will be
93 /// \return - True on success.
94 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
95 const SectionAddrMap &Addrs) const;
96 bool evaluateAsAbsolute(int64_t &Res) const;
97 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
98 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
100 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
102 /// \brief Try to evaluate the expression to a relocatable value, i.e. an
103 /// expression of the fixed form (a - b + constant).
105 /// \param Res - The relocatable value, if evaluation succeeds.
106 /// \param Layout - The assembler layout object to use for evaluating values.
107 /// \param Fixup - The Fixup object if available.
108 /// \return - True on success.
109 bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
110 const MCFixup *Fixup) const;
112 /// \brief Try to evaluate the expression to the form (a - b + constant) where
113 /// neither a nor b are variables.
115 /// This is a more aggressive variant of evaluateAsRelocatable. The intended
116 /// use is for when relocations are not available, like the .size directive.
117 bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
119 /// \brief Find the "associated section" for this expression, which is
120 /// currently defined as the absolute section for constants, or
121 /// otherwise the section associated with the first defined symbol in the
123 MCFragment *findAssociatedFragment() const;
128 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
129 E.print(OS, nullptr);
133 //// \brief Represent a constant integer expression.
134 class MCConstantExpr : public MCExpr {
137 explicit MCConstantExpr(int64_t Value)
138 : MCExpr(MCExpr::Constant, SMLoc()), Value(Value) {}
141 /// \name Construction
144 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx);
150 int64_t getValue() const { return Value; }
154 static bool classof(const MCExpr *E) {
155 return E->getKind() == MCExpr::Constant;
159 /// \brief Represent a reference to a symbol from inside an expression.
161 /// A symbol reference in an expression may be a use of a label, a use of an
162 /// assembler variable (defined constant), or constitute an implicit definition
163 /// of the symbol as external.
164 class MCSymbolRefExpr : public MCExpr {
166 enum VariantKind : uint16_t {
184 VK_TLSCALL, // symbol(tlscall)
185 VK_TLSDESC, // symbol(tlsdesc)
186 VK_TLVP, // Mach-O thread local variable relocations
194 VK_SIZE, // symbol@SIZE
195 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
204 VK_ARM_SBREL, // symbol(sbrel)
205 VK_ARM_TLSLDO, // symbol(tlsldo)
208 VK_PPC_LO, // symbol@l
209 VK_PPC_HI, // symbol@h
210 VK_PPC_HA, // symbol@ha
211 VK_PPC_HIGHER, // symbol@higher
212 VK_PPC_HIGHERA, // symbol@highera
213 VK_PPC_HIGHEST, // symbol@highest
214 VK_PPC_HIGHESTA, // symbol@highesta
215 VK_PPC_GOT_LO, // symbol@got@l
216 VK_PPC_GOT_HI, // symbol@got@h
217 VK_PPC_GOT_HA, // symbol@got@ha
218 VK_PPC_TOCBASE, // symbol@tocbase
219 VK_PPC_TOC, // symbol@toc
220 VK_PPC_TOC_LO, // symbol@toc@l
221 VK_PPC_TOC_HI, // symbol@toc@h
222 VK_PPC_TOC_HA, // symbol@toc@ha
223 VK_PPC_DTPMOD, // symbol@dtpmod
224 VK_PPC_TPREL_LO, // symbol@tprel@l
225 VK_PPC_TPREL_HI, // symbol@tprel@h
226 VK_PPC_TPREL_HA, // symbol@tprel@ha
227 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
228 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
229 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
230 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
231 VK_PPC_DTPREL_LO, // symbol@dtprel@l
232 VK_PPC_DTPREL_HI, // symbol@dtprel@h
233 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
234 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
235 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
236 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
237 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
238 VK_PPC_GOT_TPREL, // symbol@got@tprel
239 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
240 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
241 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
242 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
243 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
244 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
245 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
246 VK_PPC_TLS, // symbol@tls
247 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
248 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
249 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
250 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
251 VK_PPC_TLSGD, // symbol@tlsgd
252 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
253 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
254 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
255 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
256 VK_PPC_TLSLD, // symbol@tlsld
257 VK_PPC_LOCAL, // symbol@local
259 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
272 VK_WebAssembly_FUNCTION, // Function table index, rather than virtual addr
274 VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
275 VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
276 VK_AMDGPU_REL32_LO, // symbol@rel32@lo
277 VK_AMDGPU_REL32_HI, // symbol@rel32@hi
284 /// The symbol reference modifier.
285 const VariantKind Kind;
287 /// Specifies how the variant kind should be printed.
288 const unsigned UseParensForSymbolVariant : 1;
290 // FIXME: Remove this bit.
291 const unsigned HasSubsectionsViaSymbols : 1;
293 /// The symbol being referenced.
294 const MCSymbol *Symbol;
296 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
297 const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
300 /// \name Construction
303 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
304 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
307 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
308 MCContext &Ctx, SMLoc Loc = SMLoc());
309 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
316 const MCSymbol &getSymbol() const { return *Symbol; }
318 VariantKind getKind() const { return Kind; }
320 void printVariantKind(raw_ostream &OS) const;
322 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
325 /// \name Static Utility Functions
328 static StringRef getVariantKindName(VariantKind Kind);
330 static VariantKind getVariantKindForName(StringRef Name);
334 static bool classof(const MCExpr *E) {
335 return E->getKind() == MCExpr::SymbolRef;
339 /// \brief Unary assembler expressions.
340 class MCUnaryExpr : public MCExpr {
343 LNot, ///< Logical negation.
344 Minus, ///< Unary minus.
345 Not, ///< Bitwise negation.
346 Plus ///< Unary plus.
353 MCUnaryExpr(Opcode Op, const MCExpr *Expr)
354 : MCExpr(MCExpr::Unary, SMLoc()), Op(Op), Expr(Expr) {}
357 /// \name Construction
360 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
363 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx) {
364 return create(LNot, Expr, Ctx);
367 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx) {
368 return create(Minus, Expr, Ctx);
371 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx) {
372 return create(Not, Expr, Ctx);
375 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx) {
376 return create(Plus, Expr, Ctx);
383 /// \brief Get the kind of this unary expression.
384 Opcode getOpcode() const { return Op; }
386 /// \brief Get the child of this unary expression.
387 const MCExpr *getSubExpr() const { return Expr; }
391 static bool classof(const MCExpr *E) {
392 return E->getKind() == MCExpr::Unary;
396 /// \brief Binary assembler expressions.
397 class MCBinaryExpr : public MCExpr {
401 And, ///< Bitwise and.
402 Div, ///< Signed division.
403 EQ, ///< Equality comparison.
404 GT, ///< Signed greater than comparison (result is either 0 or some
405 ///< target-specific non-zero value)
406 GTE, ///< Signed greater than or equal comparison (result is either 0 or
407 ///< some target-specific non-zero value).
408 LAnd, ///< Logical and.
409 LOr, ///< Logical or.
410 LT, ///< Signed less than comparison (result is either 0 or
411 ///< some target-specific non-zero value).
412 LTE, ///< Signed less than or equal comparison (result is either 0 or
413 ///< some target-specific non-zero value).
414 Mod, ///< Signed remainder.
415 Mul, ///< Multiplication.
416 NE, ///< Inequality comparison.
418 Shl, ///< Shift left.
419 AShr, ///< Arithmetic shift right.
420 LShr, ///< Logical shift right.
421 Sub, ///< Subtraction.
422 Xor ///< Bitwise exclusive or.
427 const MCExpr *LHS, *RHS;
429 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
431 : MCExpr(MCExpr::Binary, Loc), Op(Op), LHS(LHS), RHS(RHS) {}
434 /// \name Construction
437 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
438 const MCExpr *RHS, MCContext &Ctx,
439 SMLoc Loc = SMLoc());
441 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
443 return create(Add, LHS, RHS, Ctx);
446 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
448 return create(And, LHS, RHS, Ctx);
451 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
453 return create(Div, LHS, RHS, Ctx);
456 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
458 return create(EQ, LHS, RHS, Ctx);
461 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
463 return create(GT, LHS, RHS, Ctx);
466 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
468 return create(GTE, LHS, RHS, Ctx);
471 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
473 return create(LAnd, LHS, RHS, Ctx);
476 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
478 return create(LOr, LHS, RHS, Ctx);
481 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
483 return create(LT, LHS, RHS, Ctx);
486 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
488 return create(LTE, LHS, RHS, Ctx);
491 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
493 return create(Mod, LHS, RHS, Ctx);
496 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
498 return create(Mul, LHS, RHS, Ctx);
501 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
503 return create(NE, LHS, RHS, Ctx);
506 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
508 return create(Or, LHS, RHS, Ctx);
511 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
513 return create(Shl, LHS, RHS, Ctx);
516 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
518 return create(AShr, LHS, RHS, Ctx);
521 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
523 return create(LShr, LHS, RHS, Ctx);
526 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
528 return create(Sub, LHS, RHS, Ctx);
531 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
533 return create(Xor, LHS, RHS, Ctx);
540 /// \brief Get the kind of this binary expression.
541 Opcode getOpcode() const { return Op; }
543 /// \brief Get the left-hand side expression of the binary operator.
544 const MCExpr *getLHS() const { return LHS; }
546 /// \brief Get the right-hand side expression of the binary operator.
547 const MCExpr *getRHS() const { return RHS; }
551 static bool classof(const MCExpr *E) {
552 return E->getKind() == MCExpr::Binary;
556 /// \brief This is an extension point for target-specific MCExpr subclasses to
559 /// NOTE: All subclasses are required to have trivial destructors because
560 /// MCExprs are bump pointer allocated and not destructed.
561 class MCTargetExpr : public MCExpr {
562 virtual void anchor();
565 MCTargetExpr() : MCExpr(Target, SMLoc()) {}
566 virtual ~MCTargetExpr() = default;
569 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
570 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
571 const MCAsmLayout *Layout,
572 const MCFixup *Fixup) const = 0;
573 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
574 virtual MCFragment *findAssociatedFragment() const = 0;
576 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
578 static bool classof(const MCExpr *E) {
579 return E->getKind() == MCExpr::Target;
583 } // end namespace llvm
585 #endif // LLVM_MC_MCEXPR_H