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 /// 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 /// 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 MCAssembler *Asm) const;
100 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
102 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
104 /// Try to evaluate the expression to a relocatable value, i.e. an
105 /// expression of the fixed form (a - b + constant).
107 /// \param Res - The relocatable value, if evaluation succeeds.
108 /// \param Layout - The assembler layout object to use for evaluating values.
109 /// \param Fixup - The Fixup object if available.
110 /// \return - True on success.
111 bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
112 const MCFixup *Fixup) const;
114 /// Try to evaluate the expression to the form (a - b + constant) where
115 /// neither a nor b are variables.
117 /// This is a more aggressive variant of evaluateAsRelocatable. The intended
118 /// use is for when relocations are not available, like the .size directive.
119 bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
121 /// Find the "associated section" for this expression, which is
122 /// currently defined as the absolute section for constants, or
123 /// otherwise the section associated with the first defined symbol in the
125 MCFragment *findAssociatedFragment() const;
130 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
131 E.print(OS, nullptr);
135 //// Represent a constant integer expression.
136 class MCConstantExpr : public MCExpr {
139 explicit MCConstantExpr(int64_t Value)
140 : MCExpr(MCExpr::Constant, SMLoc()), Value(Value) {}
143 /// \name Construction
146 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx);
152 int64_t getValue() const { return Value; }
156 static bool classof(const MCExpr *E) {
157 return E->getKind() == MCExpr::Constant;
161 /// Represent a reference to a symbol from inside an expression.
163 /// A symbol reference in an expression may be a use of a label, a use of an
164 /// assembler variable (defined constant), or constitute an implicit definition
165 /// of the symbol as external.
166 class MCSymbolRefExpr : public MCExpr {
168 enum VariantKind : uint16_t {
186 VK_TLSCALL, // symbol(tlscall)
187 VK_TLSDESC, // symbol(tlsdesc)
188 VK_TLVP, // Mach-O thread local variable relocations
196 VK_SIZE, // symbol@SIZE
197 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
206 VK_ARM_SBREL, // symbol(sbrel)
207 VK_ARM_TLSLDO, // symbol(tlsldo)
218 VK_PPC_LO, // symbol@l
219 VK_PPC_HI, // symbol@h
220 VK_PPC_HA, // symbol@ha
221 VK_PPC_HIGH, // symbol@high
222 VK_PPC_HIGHA, // symbol@higha
223 VK_PPC_HIGHER, // symbol@higher
224 VK_PPC_HIGHERA, // symbol@highera
225 VK_PPC_HIGHEST, // symbol@highest
226 VK_PPC_HIGHESTA, // symbol@highesta
227 VK_PPC_GOT_LO, // symbol@got@l
228 VK_PPC_GOT_HI, // symbol@got@h
229 VK_PPC_GOT_HA, // symbol@got@ha
230 VK_PPC_TOCBASE, // symbol@tocbase
231 VK_PPC_TOC, // symbol@toc
232 VK_PPC_TOC_LO, // symbol@toc@l
233 VK_PPC_TOC_HI, // symbol@toc@h
234 VK_PPC_TOC_HA, // symbol@toc@ha
235 VK_PPC_DTPMOD, // symbol@dtpmod
236 VK_PPC_TPREL_LO, // symbol@tprel@l
237 VK_PPC_TPREL_HI, // symbol@tprel@h
238 VK_PPC_TPREL_HA, // symbol@tprel@ha
239 VK_PPC_TPREL_HIGH, // symbol@tprel@high
240 VK_PPC_TPREL_HIGHA, // symbol@tprel@higha
241 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
242 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
243 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
244 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
245 VK_PPC_DTPREL_LO, // symbol@dtprel@l
246 VK_PPC_DTPREL_HI, // symbol@dtprel@h
247 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
248 VK_PPC_DTPREL_HIGH, // symbol@dtprel@high
249 VK_PPC_DTPREL_HIGHA, // symbol@dtprel@higha
250 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
251 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
252 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
253 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
254 VK_PPC_GOT_TPREL, // symbol@got@tprel
255 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
256 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
257 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
258 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
259 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
260 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
261 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
262 VK_PPC_TLS, // symbol@tls
263 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
264 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
265 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
266 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
267 VK_PPC_TLSGD, // symbol@tlsgd
268 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
269 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
270 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
271 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
272 VK_PPC_TLSLD, // symbol@tlsld
273 VK_PPC_LOCAL, // symbol@local
275 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
288 VK_WebAssembly_FUNCTION, // Function table index, rather than virtual addr
289 VK_WebAssembly_TYPEINDEX,// Type table index
291 VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
292 VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
293 VK_AMDGPU_REL32_LO, // symbol@rel32@lo
294 VK_AMDGPU_REL32_HI, // symbol@rel32@hi
295 VK_AMDGPU_REL64, // symbol@rel64
302 /// The symbol reference modifier.
303 const VariantKind Kind;
305 /// Specifies how the variant kind should be printed.
306 const unsigned UseParensForSymbolVariant : 1;
308 // FIXME: Remove this bit.
309 const unsigned HasSubsectionsViaSymbols : 1;
311 /// The symbol being referenced.
312 const MCSymbol *Symbol;
314 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
315 const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
318 /// \name Construction
321 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
322 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
325 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
326 MCContext &Ctx, SMLoc Loc = SMLoc());
327 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
334 const MCSymbol &getSymbol() const { return *Symbol; }
336 VariantKind getKind() const { return Kind; }
338 void printVariantKind(raw_ostream &OS) const;
340 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
343 /// \name Static Utility Functions
346 static StringRef getVariantKindName(VariantKind Kind);
348 static VariantKind getVariantKindForName(StringRef Name);
352 static bool classof(const MCExpr *E) {
353 return E->getKind() == MCExpr::SymbolRef;
357 /// Unary assembler expressions.
358 class MCUnaryExpr : public MCExpr {
361 LNot, ///< Logical negation.
362 Minus, ///< Unary minus.
363 Not, ///< Bitwise negation.
364 Plus ///< Unary plus.
371 MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
372 : MCExpr(MCExpr::Unary, Loc), Op(Op), Expr(Expr) {}
375 /// \name Construction
378 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
379 MCContext &Ctx, SMLoc Loc = SMLoc());
381 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
382 return create(LNot, Expr, Ctx, Loc);
385 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
386 return create(Minus, Expr, Ctx, Loc);
389 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
390 return create(Not, Expr, Ctx, Loc);
393 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
394 return create(Plus, Expr, Ctx, Loc);
401 /// Get the kind of this unary expression.
402 Opcode getOpcode() const { return Op; }
404 /// Get the child of this unary expression.
405 const MCExpr *getSubExpr() const { return Expr; }
409 static bool classof(const MCExpr *E) {
410 return E->getKind() == MCExpr::Unary;
414 /// Binary assembler expressions.
415 class MCBinaryExpr : public MCExpr {
419 And, ///< Bitwise and.
420 Div, ///< Signed division.
421 EQ, ///< Equality comparison.
422 GT, ///< Signed greater than comparison (result is either 0 or some
423 ///< target-specific non-zero value)
424 GTE, ///< Signed greater than or equal comparison (result is either 0 or
425 ///< some target-specific non-zero value).
426 LAnd, ///< Logical and.
427 LOr, ///< Logical or.
428 LT, ///< Signed less than comparison (result is either 0 or
429 ///< some target-specific non-zero value).
430 LTE, ///< Signed less than or equal comparison (result is either 0 or
431 ///< some target-specific non-zero value).
432 Mod, ///< Signed remainder.
433 Mul, ///< Multiplication.
434 NE, ///< Inequality comparison.
436 Shl, ///< Shift left.
437 AShr, ///< Arithmetic shift right.
438 LShr, ///< Logical shift right.
439 Sub, ///< Subtraction.
440 Xor ///< Bitwise exclusive or.
445 const MCExpr *LHS, *RHS;
447 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
449 : MCExpr(MCExpr::Binary, Loc), Op(Op), LHS(LHS), RHS(RHS) {}
452 /// \name Construction
455 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
456 const MCExpr *RHS, MCContext &Ctx,
457 SMLoc Loc = SMLoc());
459 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
461 return create(Add, LHS, RHS, Ctx);
464 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
466 return create(And, LHS, RHS, Ctx);
469 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
471 return create(Div, LHS, RHS, Ctx);
474 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
476 return create(EQ, LHS, RHS, Ctx);
479 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
481 return create(GT, LHS, RHS, Ctx);
484 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
486 return create(GTE, LHS, RHS, Ctx);
489 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
491 return create(LAnd, LHS, RHS, Ctx);
494 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
496 return create(LOr, LHS, RHS, Ctx);
499 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
501 return create(LT, LHS, RHS, Ctx);
504 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
506 return create(LTE, LHS, RHS, Ctx);
509 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
511 return create(Mod, LHS, RHS, Ctx);
514 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
516 return create(Mul, LHS, RHS, Ctx);
519 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
521 return create(NE, LHS, RHS, Ctx);
524 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
526 return create(Or, LHS, RHS, Ctx);
529 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
531 return create(Shl, LHS, RHS, Ctx);
534 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
536 return create(AShr, LHS, RHS, Ctx);
539 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
541 return create(LShr, LHS, RHS, Ctx);
544 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
546 return create(Sub, LHS, RHS, Ctx);
549 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
551 return create(Xor, LHS, RHS, Ctx);
558 /// Get the kind of this binary expression.
559 Opcode getOpcode() const { return Op; }
561 /// Get the left-hand side expression of the binary operator.
562 const MCExpr *getLHS() const { return LHS; }
564 /// Get the right-hand side expression of the binary operator.
565 const MCExpr *getRHS() const { return RHS; }
569 static bool classof(const MCExpr *E) {
570 return E->getKind() == MCExpr::Binary;
574 /// This is an extension point for target-specific MCExpr subclasses to
577 /// NOTE: All subclasses are required to have trivial destructors because
578 /// MCExprs are bump pointer allocated and not destructed.
579 class MCTargetExpr : public MCExpr {
580 virtual void anchor();
583 MCTargetExpr() : MCExpr(Target, SMLoc()) {}
584 virtual ~MCTargetExpr() = default;
587 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
588 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
589 const MCAsmLayout *Layout,
590 const MCFixup *Fixup) const = 0;
591 // allow Target Expressions to be checked for equality
592 virtual bool isEqualTo(const MCExpr *x) const { return false; }
593 // This should be set when assigned expressions are not valid ".set"
594 // expressions, e.g. registers, and must be inlined.
595 virtual bool inlineAssignedExpr() const { return false; }
596 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
597 virtual MCFragment *findAssociatedFragment() const = 0;
599 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
601 static bool classof(const MCExpr *E) {
602 return E->getKind() == MCExpr::Target;
606 } // end namespace llvm
608 #endif // LLVM_MC_MCEXPR_H