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"
29 typedef DenseMap<const MCSection *, uint64_t> SectionAddrMap;
31 /// \brief Base class for the full range of assembler expressions which are
32 /// needed for parsing.
36 Binary, ///< Binary expressions.
37 Constant, ///< Constant expressions.
38 SymbolRef, ///< References to labels and assigned expressions.
39 Unary, ///< Unary expressions.
40 Target ///< Target specific expression.
46 MCExpr(const MCExpr&) = delete;
47 void operator=(const MCExpr&) = delete;
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) : Kind(Kind) {}
60 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
61 const MCAsmLayout *Layout,
63 const SectionAddrMap *Addrs, bool InSet) const;
69 ExprKind getKind() const { return Kind; }
72 /// \name Utility Methods
75 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
79 /// \name Expression Evaluation
82 /// \brief Try to evaluate the expression to an absolute value.
84 /// \param Res - The absolute value, if evaluation succeeds.
85 /// \param Layout - The assembler layout object to use for evaluating symbol
86 /// values. If not given, then only non-symbolic expressions will be
88 /// \return - True on success.
89 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
90 const SectionAddrMap &Addrs) const;
91 bool evaluateAsAbsolute(int64_t &Res) const;
92 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
93 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
95 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
97 /// \brief Try to evaluate the expression to a relocatable value, i.e. an
98 /// expression of the fixed form (a - b + constant).
100 /// \param Res - The relocatable value, if evaluation succeeds.
101 /// \param Layout - The assembler layout object to use for evaluating values.
102 /// \param Fixup - The Fixup object if available.
103 /// \return - True on success.
104 bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
105 const MCFixup *Fixup) const;
107 /// \brief Try to evaluate the expression to the form (a - b + constant) where
108 /// neither a nor b are variables.
110 /// This is a more aggressive variant of evaluateAsRelocatable. The intended
111 /// use is for when relocations are not available, like the .size directive.
112 bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
114 /// \brief Find the "associated section" for this expression, which is
115 /// currently defined as the absolute section for constants, or
116 /// otherwise the section associated with the first defined symbol in the
118 MCSection *findAssociatedSection() const;
123 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
124 E.print(OS, nullptr);
128 //// \brief Represent a constant integer expression.
129 class MCConstantExpr : public MCExpr {
132 explicit MCConstantExpr(int64_t Value)
133 : MCExpr(MCExpr::Constant), Value(Value) {}
136 /// \name Construction
139 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx);
145 int64_t getValue() const { return Value; }
149 static bool classof(const MCExpr *E) {
150 return E->getKind() == MCExpr::Constant;
154 /// \brief Represent a reference to a symbol from inside an expression.
156 /// A symbol reference in an expression may be a use of a label, a use of an
157 /// assembler variable (defined constant), or constitute an implicit definition
158 /// of the symbol as external.
159 class MCSymbolRefExpr : public MCExpr {
161 enum VariantKind : uint16_t {
178 VK_TLVP, // Mach-O thread local variable relocations
186 VK_SIZE, // symbol@SIZE
187 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
193 VK_ARM_SBREL, // symbol(sbrel)
194 VK_ARM_TLSLDO, // symbol(tlsldo)
195 VK_ARM_TLSCALL, // symbol(tlscall)
196 VK_ARM_TLSDESC, // symbol(tlsdesc)
199 VK_PPC_LO, // symbol@l
200 VK_PPC_HI, // symbol@h
201 VK_PPC_HA, // symbol@ha
202 VK_PPC_HIGHER, // symbol@higher
203 VK_PPC_HIGHERA, // symbol@highera
204 VK_PPC_HIGHEST, // symbol@highest
205 VK_PPC_HIGHESTA, // symbol@highesta
206 VK_PPC_GOT_LO, // symbol@got@l
207 VK_PPC_GOT_HI, // symbol@got@h
208 VK_PPC_GOT_HA, // symbol@got@ha
209 VK_PPC_TOCBASE, // symbol@tocbase
210 VK_PPC_TOC, // symbol@toc
211 VK_PPC_TOC_LO, // symbol@toc@l
212 VK_PPC_TOC_HI, // symbol@toc@h
213 VK_PPC_TOC_HA, // symbol@toc@ha
214 VK_PPC_DTPMOD, // symbol@dtpmod
215 VK_PPC_TPREL, // symbol@tprel
216 VK_PPC_TPREL_LO, // symbol@tprel@l
217 VK_PPC_TPREL_HI, // symbol@tprel@h
218 VK_PPC_TPREL_HA, // symbol@tprel@ha
219 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
220 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
221 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
222 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
223 VK_PPC_DTPREL, // symbol@dtprel
224 VK_PPC_DTPREL_LO, // symbol@dtprel@l
225 VK_PPC_DTPREL_HI, // symbol@dtprel@h
226 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
227 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
228 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
229 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
230 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
231 VK_PPC_GOT_TPREL, // symbol@got@tprel
232 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
233 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
234 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
235 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
236 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
237 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
238 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
239 VK_PPC_TLS, // symbol@tls
240 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
241 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
242 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
243 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
244 VK_PPC_TLSGD, // symbol@tlsgd
245 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
246 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
247 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
248 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
249 VK_PPC_TLSLD, // symbol@tlsld
250 VK_PPC_LOCAL, // symbol@local
279 VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
296 /// The symbol reference modifier.
297 const VariantKind Kind;
299 /// Specifies how the variant kind should be printed.
300 const unsigned UseParensForSymbolVariant : 1;
302 // FIXME: Remove this bit.
303 const unsigned HasSubsectionsViaSymbols : 1;
305 /// The symbol being referenced.
306 const MCSymbol *Symbol;
308 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
309 const MCAsmInfo *MAI);
312 /// \name Construction
315 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
316 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
319 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
321 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
328 const MCSymbol &getSymbol() const { return *Symbol; }
330 VariantKind getKind() const { return Kind; }
332 void printVariantKind(raw_ostream &OS) const;
334 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
337 /// \name Static Utility Functions
340 static StringRef getVariantKindName(VariantKind Kind);
342 static VariantKind getVariantKindForName(StringRef Name);
346 static bool classof(const MCExpr *E) {
347 return E->getKind() == MCExpr::SymbolRef;
351 /// \brief Unary assembler expressions.
352 class MCUnaryExpr : public MCExpr {
355 LNot, ///< Logical negation.
356 Minus, ///< Unary minus.
357 Not, ///< Bitwise negation.
358 Plus ///< Unary plus.
365 MCUnaryExpr(Opcode Op, const MCExpr *Expr)
366 : MCExpr(MCExpr::Unary), Op(Op), Expr(Expr) {}
369 /// \name Construction
372 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
374 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx) {
375 return create(LNot, Expr, Ctx);
377 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx) {
378 return create(Minus, Expr, Ctx);
380 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx) {
381 return create(Not, Expr, Ctx);
383 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx) {
384 return create(Plus, Expr, Ctx);
391 /// \brief Get the kind of this unary expression.
392 Opcode getOpcode() const { return Op; }
394 /// \brief Get the child of this unary expression.
395 const MCExpr *getSubExpr() const { return Expr; }
399 static bool classof(const MCExpr *E) {
400 return E->getKind() == MCExpr::Unary;
404 /// \brief Binary assembler expressions.
405 class MCBinaryExpr : public MCExpr {
409 And, ///< Bitwise and.
410 Div, ///< Signed division.
411 EQ, ///< Equality comparison.
412 GT, ///< Signed greater than comparison (result is either 0 or some
413 ///< target-specific non-zero value)
414 GTE, ///< Signed greater than or equal comparison (result is either 0 or
415 ///< some target-specific non-zero value).
416 LAnd, ///< Logical and.
417 LOr, ///< Logical or.
418 LT, ///< Signed less than comparison (result is either 0 or
419 ///< some target-specific non-zero value).
420 LTE, ///< Signed less than or equal comparison (result is either 0 or
421 ///< some target-specific non-zero value).
422 Mod, ///< Signed remainder.
423 Mul, ///< Multiplication.
424 NE, ///< Inequality comparison.
426 Shl, ///< Shift left.
427 AShr, ///< Arithmetic shift right.
428 LShr, ///< Logical shift right.
429 Sub, ///< Subtraction.
430 Xor ///< Bitwise exclusive or.
435 const MCExpr *LHS, *RHS;
437 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS)
438 : MCExpr(MCExpr::Binary), Op(Op), LHS(LHS), RHS(RHS) {}
441 /// \name Construction
444 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
445 const MCExpr *RHS, MCContext &Ctx);
446 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
448 return create(Add, LHS, RHS, Ctx);
450 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
452 return create(And, LHS, RHS, Ctx);
454 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
456 return create(Div, LHS, RHS, Ctx);
458 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
460 return create(EQ, LHS, RHS, Ctx);
462 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
464 return create(GT, LHS, RHS, Ctx);
466 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
468 return create(GTE, LHS, RHS, Ctx);
470 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
472 return create(LAnd, LHS, RHS, Ctx);
474 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
476 return create(LOr, LHS, RHS, Ctx);
478 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
480 return create(LT, LHS, RHS, Ctx);
482 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
484 return create(LTE, LHS, RHS, Ctx);
486 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
488 return create(Mod, LHS, RHS, Ctx);
490 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
492 return create(Mul, LHS, RHS, Ctx);
494 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
496 return create(NE, LHS, RHS, Ctx);
498 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
500 return create(Or, LHS, RHS, Ctx);
502 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
504 return create(Shl, LHS, RHS, Ctx);
506 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
508 return create(AShr, LHS, RHS, Ctx);
510 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
512 return create(LShr, LHS, RHS, Ctx);
514 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
516 return create(Sub, LHS, RHS, Ctx);
518 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
520 return create(Xor, LHS, RHS, Ctx);
527 /// \brief Get the kind of this binary expression.
528 Opcode getOpcode() const { return Op; }
530 /// \brief Get the left-hand side expression of the binary operator.
531 const MCExpr *getLHS() const { return LHS; }
533 /// \brief Get the right-hand side expression of the binary operator.
534 const MCExpr *getRHS() const { return RHS; }
538 static bool classof(const MCExpr *E) {
539 return E->getKind() == MCExpr::Binary;
543 /// \brief This is an extension point for target-specific MCExpr subclasses to
546 /// NOTE: All subclasses are required to have trivial destructors because
547 /// MCExprs are bump pointer allocated and not destructed.
548 class MCTargetExpr : public MCExpr {
549 virtual void anchor();
551 MCTargetExpr() : MCExpr(Target) {}
552 virtual ~MCTargetExpr() {}
554 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
555 virtual bool evaluateAsRelocatableImpl(MCValue &Res,
556 const MCAsmLayout *Layout,
557 const MCFixup *Fixup) const = 0;
558 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
559 virtual MCSection *findAssociatedSection() const = 0;
561 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
563 static bool classof(const MCExpr *E) {
564 return E->getKind() == MCExpr::Target;
568 } // end namespace llvm