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/Casting.h"
15 #include "llvm/Support/DataTypes.h"
30 typedef DenseMap<const MCSection *, uint64_t> SectionAddrMap;
32 /// \brief Base class for the full range of assembler expressions which are
33 /// needed for parsing.
37 Binary, ///< Binary expressions.
38 Constant, ///< Constant expressions.
39 SymbolRef, ///< References to labels and assigned expressions.
40 Unary, ///< Unary expressions.
41 Target ///< Target specific expression.
47 MCExpr(const MCExpr&) = delete;
48 void operator=(const MCExpr&) = delete;
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) : Kind(Kind) {}
61 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
62 const MCAsmLayout *Layout,
64 const SectionAddrMap *Addrs, bool InSet) const;
70 ExprKind getKind() const { return Kind; }
73 /// \name Utility Methods
76 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
80 /// \name Expression Evaluation
83 /// \brief Try to evaluate the expression to an absolute value.
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
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 MCAsmLayout &Layout) const;
96 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
98 /// \brief Try to evaluate the expression to a relocatable value, i.e. an
99 /// expression of the fixed form (a - b + constant).
101 /// \param Res - The relocatable value, if evaluation succeeds.
102 /// \param Layout - The assembler layout object to use for evaluating values.
103 /// \param Fixup - The Fixup object if available.
104 /// \return - True on success.
105 bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
106 const MCFixup *Fixup) const;
108 /// \brief Try to evaluate the expression to the form (a - b + constant) where
109 /// neither a nor b are variables.
111 /// This is a more aggressive variant of evaluateAsRelocatable. The intended
112 /// use is for when relocations are not available, like the .size directive.
113 bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
115 /// \brief Find the "associated section" for this expression, which is
116 /// currently defined as the absolute section for constants, or
117 /// otherwise the section associated with the first defined symbol in the
119 MCFragment *findAssociatedFragment() const;
124 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
125 E.print(OS, nullptr);
129 //// \brief Represent a constant integer expression.
130 class MCConstantExpr : public MCExpr {
133 explicit MCConstantExpr(int64_t Value)
134 : MCExpr(MCExpr::Constant), Value(Value) {}
137 /// \name Construction
140 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx);
146 int64_t getValue() const { return Value; }
150 static bool classof(const MCExpr *E) {
151 return E->getKind() == MCExpr::Constant;
155 /// \brief Represent a reference to a symbol from inside an expression.
157 /// A symbol reference in an expression may be a use of a label, a use of an
158 /// assembler variable (defined constant), or constitute an implicit definition
159 /// of the symbol as external.
160 class MCSymbolRefExpr : public MCExpr {
162 enum VariantKind : uint16_t {
179 VK_TLVP, // Mach-O thread local variable relocations
187 VK_SIZE, // symbol@SIZE
188 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
195 VK_ARM_SBREL, // symbol(sbrel)
196 VK_ARM_TLSLDO, // symbol(tlsldo)
197 VK_ARM_TLSCALL, // symbol(tlscall)
198 VK_ARM_TLSDESC, // symbol(tlsdesc)
201 VK_PPC_LO, // symbol@l
202 VK_PPC_HI, // symbol@h
203 VK_PPC_HA, // symbol@ha
204 VK_PPC_HIGHER, // symbol@higher
205 VK_PPC_HIGHERA, // symbol@highera
206 VK_PPC_HIGHEST, // symbol@highest
207 VK_PPC_HIGHESTA, // symbol@highesta
208 VK_PPC_GOT_LO, // symbol@got@l
209 VK_PPC_GOT_HI, // symbol@got@h
210 VK_PPC_GOT_HA, // symbol@got@ha
211 VK_PPC_TOCBASE, // symbol@tocbase
212 VK_PPC_TOC, // symbol@toc
213 VK_PPC_TOC_LO, // symbol@toc@l
214 VK_PPC_TOC_HI, // symbol@toc@h
215 VK_PPC_TOC_HA, // symbol@toc@ha
216 VK_PPC_DTPMOD, // symbol@dtpmod
217 VK_PPC_TPREL, // symbol@tprel
218 VK_PPC_TPREL_LO, // symbol@tprel@l
219 VK_PPC_TPREL_HI, // symbol@tprel@h
220 VK_PPC_TPREL_HA, // symbol@tprel@ha
221 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
222 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
223 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
224 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
225 VK_PPC_DTPREL, // symbol@dtprel
226 VK_PPC_DTPREL_LO, // symbol@dtprel@l
227 VK_PPC_DTPREL_HI, // symbol@dtprel@h
228 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
229 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
230 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
231 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
232 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
233 VK_PPC_GOT_TPREL, // symbol@got@tprel
234 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
235 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
236 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
237 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
238 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
239 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
240 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
241 VK_PPC_TLS, // symbol@tls
242 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
243 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
244 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
245 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
246 VK_PPC_TLSGD, // symbol@tlsgd
247 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
248 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
249 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
250 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
251 VK_PPC_TLSLD, // symbol@tlsld
252 VK_PPC_LOCAL, // symbol@local
281 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
294 VK_WebAssembly_FUNCTION, // Function table index, rather than virtual addr
301 /// The symbol reference modifier.
302 const VariantKind Kind;
304 /// Specifies how the variant kind should be printed.
305 const unsigned UseParensForSymbolVariant : 1;
307 // FIXME: Remove this bit.
308 const unsigned HasSubsectionsViaSymbols : 1;
310 /// The symbol being referenced.
311 const MCSymbol *Symbol;
313 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
314 const MCAsmInfo *MAI);
317 /// \name Construction
320 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
321 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
324 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
326 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
333 const MCSymbol &getSymbol() const { return *Symbol; }
335 VariantKind getKind() const { return Kind; }
337 void printVariantKind(raw_ostream &OS) const;
339 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
342 /// \name Static Utility Functions
345 static StringRef getVariantKindName(VariantKind Kind);
347 static VariantKind getVariantKindForName(StringRef Name);
351 static bool classof(const MCExpr *E) {
352 return E->getKind() == MCExpr::SymbolRef;
356 /// \brief Unary assembler expressions.
357 class MCUnaryExpr : public MCExpr {
360 LNot, ///< Logical negation.
361 Minus, ///< Unary minus.
362 Not, ///< Bitwise negation.
363 Plus ///< Unary plus.
370 MCUnaryExpr(Opcode Op, const MCExpr *Expr)
371 : MCExpr(MCExpr::Unary), Op(Op), Expr(Expr) {}
374 /// \name Construction
377 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
379 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx) {
380 return create(LNot, Expr, Ctx);
382 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx) {
383 return create(Minus, Expr, Ctx);
385 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx) {
386 return create(Not, Expr, Ctx);
388 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx) {
389 return create(Plus, Expr, Ctx);
396 /// \brief Get the kind of this unary expression.
397 Opcode getOpcode() const { return Op; }
399 /// \brief Get the child of this unary expression.
400 const MCExpr *getSubExpr() const { return Expr; }
404 static bool classof(const MCExpr *E) {
405 return E->getKind() == MCExpr::Unary;
409 /// \brief Binary assembler expressions.
410 class MCBinaryExpr : public MCExpr {
414 And, ///< Bitwise and.
415 Div, ///< Signed division.
416 EQ, ///< Equality comparison.
417 GT, ///< Signed greater than comparison (result is either 0 or some
418 ///< target-specific non-zero value)
419 GTE, ///< Signed greater than or equal comparison (result is either 0 or
420 ///< some target-specific non-zero value).
421 LAnd, ///< Logical and.
422 LOr, ///< Logical or.
423 LT, ///< Signed less than comparison (result is either 0 or
424 ///< some target-specific non-zero value).
425 LTE, ///< Signed less than or equal comparison (result is either 0 or
426 ///< some target-specific non-zero value).
427 Mod, ///< Signed remainder.
428 Mul, ///< Multiplication.
429 NE, ///< Inequality comparison.
431 Shl, ///< Shift left.
432 AShr, ///< Arithmetic shift right.
433 LShr, ///< Logical shift right.
434 Sub, ///< Subtraction.
435 Xor ///< Bitwise exclusive or.
440 const MCExpr *LHS, *RHS;
442 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS)
443 : MCExpr(MCExpr::Binary), Op(Op), LHS(LHS), RHS(RHS) {}
446 /// \name Construction
449 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
450 const MCExpr *RHS, MCContext &Ctx);
451 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
453 return create(Add, LHS, RHS, Ctx);
455 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
457 return create(And, LHS, RHS, Ctx);
459 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
461 return create(Div, LHS, RHS, Ctx);
463 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
465 return create(EQ, LHS, RHS, Ctx);
467 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
469 return create(GT, LHS, RHS, Ctx);
471 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
473 return create(GTE, LHS, RHS, Ctx);
475 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
477 return create(LAnd, LHS, RHS, Ctx);
479 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
481 return create(LOr, LHS, RHS, Ctx);
483 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
485 return create(LT, LHS, RHS, Ctx);
487 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
489 return create(LTE, LHS, RHS, Ctx);
491 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
493 return create(Mod, LHS, RHS, Ctx);
495 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
497 return create(Mul, LHS, RHS, Ctx);
499 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
501 return create(NE, LHS, RHS, Ctx);
503 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
505 return create(Or, LHS, RHS, Ctx);
507 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
509 return create(Shl, LHS, RHS, Ctx);
511 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
513 return create(AShr, LHS, RHS, Ctx);
515 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
517 return create(LShr, LHS, RHS, Ctx);
519 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
521 return create(Sub, LHS, RHS, Ctx);
523 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
525 return create(Xor, LHS, RHS, Ctx);
532 /// \brief Get the kind of this binary expression.
533 Opcode getOpcode() const { return Op; }
535 /// \brief Get the left-hand side expression of the binary operator.
536 const MCExpr *getLHS() const { return LHS; }
538 /// \brief Get the right-hand side expression of the binary operator.
539 const MCExpr *getRHS() const { return RHS; }
543 static bool classof(const MCExpr *E) {
544 return E->getKind() == MCExpr::Binary;
548 /// \brief This is an extension point for target-specific MCExpr subclasses to
551 /// NOTE: All subclasses are required to have trivial destructors because
552 /// MCExprs are bump pointer allocated and not destructed.
553 class MCTargetExpr : public MCExpr {
554 virtual void anchor();
556 MCTargetExpr() : MCExpr(Target) {}
557 virtual ~MCTargetExpr() {}
559 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
560 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
561 const MCAsmLayout *Layout,
562 const MCFixup *Fixup) const = 0;
563 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
564 virtual MCFragment *findAssociatedFragment() const = 0;
566 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
568 static bool classof(const MCExpr *E) {
569 return E->getKind() == MCExpr::Target;
573 } // end namespace llvm