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 MCSectionData*, uint64_t> SectionAddrMap;
32 /// MCExpr - 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,
65 bool ForceVarExpansion) const;
71 ExprKind getKind() const { return Kind; }
74 /// @name Utility Methods
77 void print(raw_ostream &OS) const;
81 /// @name Expression Evaluation
84 /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
86 /// @param Res - The absolute value, if evaluation succeeds.
87 /// @param Layout - The assembler layout object to use for evaluating symbol
88 /// values. If not given, then only non-symbolic expressions will be
90 /// @result - True on success.
91 bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
92 const SectionAddrMap &Addrs) const;
93 bool EvaluateAsAbsolute(int64_t &Res) const;
94 bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
95 bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
97 int64_t evaluateKnownAbsolute(const MCAsmLayout &Layout) const;
99 /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
100 /// value, i.e. an expression of the fixed form (a - b + constant).
102 /// @param Res - The relocatable value, if evaluation succeeds.
103 /// @param Layout - The assembler layout object to use for evaluating values.
104 /// @param Fixup - The Fixup object if available.
105 /// @result - True on success.
106 bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
107 const MCFixup *Fixup) const;
109 /// \brief Try to evaluate the expression to the form (a - b + constant) where
110 /// neither a nor b are variables.
112 /// This is a more aggressive variant of EvaluateAsRelocatable. The intended
113 /// use is for when relocations are not available, like the symbol value in
114 /// the symbol table.
115 bool EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout,
116 const MCFixup *Fixup) const;
118 /// FindAssociatedSection - Find the "associated section" for this expression,
119 /// which is currently defined as the absolute section for constants, or
120 /// otherwise the section associated with the first defined symbol in the
122 const MCSection *FindAssociatedSection() const;
127 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
132 //// MCConstantExpr - Represent a constant integer expression.
133 class MCConstantExpr : public MCExpr {
136 explicit MCConstantExpr(int64_t _Value)
137 : MCExpr(MCExpr::Constant), Value(_Value) {}
140 /// @name Construction
143 static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
149 int64_t getValue() const { return Value; }
153 static bool classof(const MCExpr *E) {
154 return E->getKind() == MCExpr::Constant;
158 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
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 {
183 VK_TLVP, // Mach-O thread local variable relocations
191 VK_SIZE, // symbol@SIZE
192 VK_WEAKREF, // The link between the symbols in .weakref foo, bar
198 VK_ARM_SBREL, // symbol(sbrel)
199 VK_ARM_TLSLDO, // symbol(tlsldo)
200 VK_ARM_TLSCALL, // symbol(tlscall)
201 VK_ARM_TLSDESC, // symbol(tlsdesc)
204 VK_PPC_LO, // symbol@l
205 VK_PPC_HI, // symbol@h
206 VK_PPC_HA, // symbol@ha
207 VK_PPC_HIGHER, // symbol@higher
208 VK_PPC_HIGHERA, // symbol@highera
209 VK_PPC_HIGHEST, // symbol@highest
210 VK_PPC_HIGHESTA, // symbol@highesta
211 VK_PPC_GOT_LO, // symbol@got@l
212 VK_PPC_GOT_HI, // symbol@got@h
213 VK_PPC_GOT_HA, // symbol@got@ha
214 VK_PPC_TOCBASE, // symbol@tocbase
215 VK_PPC_TOC, // symbol@toc
216 VK_PPC_TOC_LO, // symbol@toc@l
217 VK_PPC_TOC_HI, // symbol@toc@h
218 VK_PPC_TOC_HA, // symbol@toc@ha
219 VK_PPC_DTPMOD, // symbol@dtpmod
220 VK_PPC_TPREL, // symbol@tprel
221 VK_PPC_TPREL_LO, // symbol@tprel@l
222 VK_PPC_TPREL_HI, // symbol@tprel@h
223 VK_PPC_TPREL_HA, // symbol@tprel@ha
224 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
225 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
226 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
227 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
228 VK_PPC_DTPREL, // symbol@dtprel
229 VK_PPC_DTPREL_LO, // symbol@dtprel@l
230 VK_PPC_DTPREL_HI, // symbol@dtprel@h
231 VK_PPC_DTPREL_HA, // symbol@dtprel@ha
232 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
233 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
234 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
235 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
236 VK_PPC_GOT_TPREL, // symbol@got@tprel
237 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
238 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
239 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
240 VK_PPC_GOT_DTPREL, // symbol@got@dtprel
241 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
242 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
243 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
244 VK_PPC_TLS, // symbol@tls
245 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
246 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
247 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
248 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
249 VK_PPC_TLSGD, // symbol@tlsgd
250 VK_PPC_GOT_TLSLD, // symbol@got@tlsld
251 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
252 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
253 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
254 VK_PPC_TLSLD, // symbol@tlsld
255 VK_PPC_LOCAL, // symbol@local
284 VK_COFF_IMGREL32 // symbol@imgrel (image-relative)
288 /// The symbol reference modifier.
289 const unsigned Kind : 16;
291 /// Specifies how the variant kind should be printed.
292 const unsigned UseParensForSymbolVariant : 1;
294 // FIXME: Remove this bit.
295 const unsigned HasSubsectionsViaSymbols : 1;
297 /// The symbol being referenced.
298 const MCSymbol *Symbol;
300 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
301 const MCAsmInfo *MAI);
304 /// @name Construction
307 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) {
308 return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx);
311 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind,
313 static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind,
320 const MCSymbol &getSymbol() const { return *Symbol; }
322 VariantKind getKind() const { return static_cast<VariantKind>(Kind); }
324 void printVariantKind(raw_ostream &OS) const;
326 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
329 /// @name Static Utility Functions
332 static StringRef getVariantKindName(VariantKind Kind);
334 static VariantKind getVariantKindForName(StringRef Name);
338 static bool classof(const MCExpr *E) {
339 return E->getKind() == MCExpr::SymbolRef;
343 /// MCUnaryExpr - Unary assembler expressions.
344 class MCUnaryExpr : public MCExpr {
347 LNot, ///< Logical negation.
348 Minus, ///< Unary minus.
349 Not, ///< Bitwise negation.
350 Plus ///< Unary plus.
357 MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
358 : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
361 /// @name Construction
364 static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
366 static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
367 return Create(LNot, Expr, Ctx);
369 static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
370 return Create(Minus, Expr, Ctx);
372 static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
373 return Create(Not, Expr, Ctx);
375 static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
376 return Create(Plus, Expr, Ctx);
383 /// getOpcode - Get the kind of this unary expression.
384 Opcode getOpcode() const { return Op; }
386 /// getSubExpr - 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 /// MCBinaryExpr - 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 Shr, ///< Shift right (arithmetic or logical, depending on target)
420 Sub, ///< Subtraction.
421 Xor ///< Bitwise exclusive or.
426 const MCExpr *LHS, *RHS;
428 MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
429 : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
432 /// @name Construction
435 static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
436 const MCExpr *RHS, MCContext &Ctx);
437 static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
439 return Create(Add, LHS, RHS, Ctx);
441 static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
443 return Create(And, LHS, RHS, Ctx);
445 static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
447 return Create(Div, LHS, RHS, Ctx);
449 static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
451 return Create(EQ, LHS, RHS, Ctx);
453 static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
455 return Create(GT, LHS, RHS, Ctx);
457 static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
459 return Create(GTE, LHS, RHS, Ctx);
461 static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
463 return Create(LAnd, LHS, RHS, Ctx);
465 static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
467 return Create(LOr, LHS, RHS, Ctx);
469 static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
471 return Create(LT, LHS, RHS, Ctx);
473 static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
475 return Create(LTE, LHS, RHS, Ctx);
477 static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
479 return Create(Mod, LHS, RHS, Ctx);
481 static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
483 return Create(Mul, LHS, RHS, Ctx);
485 static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
487 return Create(NE, LHS, RHS, Ctx);
489 static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
491 return Create(Or, LHS, RHS, Ctx);
493 static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
495 return Create(Shl, LHS, RHS, Ctx);
497 static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
499 return Create(Shr, LHS, RHS, Ctx);
501 static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
503 return Create(Sub, LHS, RHS, Ctx);
505 static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
507 return Create(Xor, LHS, RHS, Ctx);
514 /// getOpcode - Get the kind of this binary expression.
515 Opcode getOpcode() const { return Op; }
517 /// getLHS - Get the left-hand side expression of the binary operator.
518 const MCExpr *getLHS() const { return LHS; }
520 /// getRHS - Get the right-hand side expression of the binary operator.
521 const MCExpr *getRHS() const { return RHS; }
525 static bool classof(const MCExpr *E) {
526 return E->getKind() == MCExpr::Binary;
530 /// MCTargetExpr - This is an extension point for target-specific MCExpr
531 /// subclasses to implement.
533 /// NOTE: All subclasses are required to have trivial destructors because
534 /// MCExprs are bump pointer allocated and not destructed.
535 class MCTargetExpr : public MCExpr {
536 virtual void anchor();
538 MCTargetExpr() : MCExpr(Target) {}
539 virtual ~MCTargetExpr() {}
542 virtual void PrintImpl(raw_ostream &OS) const = 0;
543 virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
544 const MCAsmLayout *Layout,
545 const MCFixup *Fixup) const = 0;
546 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
547 virtual const MCSection *FindAssociatedSection() const = 0;
549 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
551 static bool classof(const MCExpr *E) {
552 return E->getKind() == MCExpr::Target;
556 } // end namespace llvm