Home | History | Annotate | Download | only in MC
      1 //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef LLVM_MC_MCEXPR_H
     11 #define LLVM_MC_MCEXPR_H
     12 
     13 #include "llvm/ADT/DenseMap.h"
     14 #include "llvm/Support/Casting.h"
     15 #include "llvm/Support/DataTypes.h"
     16 
     17 namespace llvm {
     18 class MCAsmInfo;
     19 class MCAsmLayout;
     20 class MCAssembler;
     21 class MCContext;
     22 class MCSection;
     23 class MCSectionData;
     24 class MCStreamer;
     25 class MCSymbol;
     26 class MCValue;
     27 class raw_ostream;
     28 class StringRef;
     29 typedef DenseMap<const MCSectionData*, uint64_t> SectionAddrMap;
     30 
     31 /// MCExpr - Base class for the full range of assembler expressions which are
     32 /// needed for parsing.
     33 class MCExpr {
     34 public:
     35   enum ExprKind {
     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.
     41   };
     42 
     43 private:
     44   ExprKind Kind;
     45 
     46   MCExpr(const MCExpr&) LLVM_DELETED_FUNCTION;
     47   void operator=(const MCExpr&) LLVM_DELETED_FUNCTION;
     48 
     49   bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
     50                           const MCAsmLayout *Layout,
     51                           const SectionAddrMap *Addrs) const;
     52 protected:
     53   explicit MCExpr(ExprKind _Kind) : Kind(_Kind) {}
     54 
     55   bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
     56                                  const MCAsmLayout *Layout,
     57                                  const SectionAddrMap *Addrs, bool InSet,
     58                                  bool ForceVarExpansion) const;
     59 
     60 public:
     61   /// @name Accessors
     62   /// @{
     63 
     64   ExprKind getKind() const { return Kind; }
     65 
     66   /// @}
     67   /// @name Utility Methods
     68   /// @{
     69 
     70   void print(raw_ostream &OS) const;
     71   void dump() const;
     72 
     73   /// @}
     74   /// @name Expression Evaluation
     75   /// @{
     76 
     77   /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
     78   ///
     79   /// @param Res - The absolute value, if evaluation succeeds.
     80   /// @param Layout - The assembler layout object to use for evaluating symbol
     81   /// values. If not given, then only non-symbolic expressions will be
     82   /// evaluated.
     83   /// @result - True on success.
     84   bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
     85                           const SectionAddrMap &Addrs) const;
     86   bool EvaluateAsAbsolute(int64_t &Res) const;
     87   bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
     88   bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
     89 
     90   /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
     91   /// value, i.e. an expression of the fixed form (a - b + constant).
     92   ///
     93   /// @param Res - The relocatable value, if evaluation succeeds.
     94   /// @param Layout - The assembler layout object to use for evaluating values.
     95   /// @result - True on success.
     96   bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout) const;
     97 
     98   /// \brief Try to evaluate the expression to the form (a - b + constant) where
     99   /// neither a nor b are variables.
    100   ///
    101   /// This is a more aggressive variant of EvaluateAsRelocatable. The intended
    102   /// use is for when relocations are not available, like the symbol value in
    103   /// the symbol table.
    104   bool EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout) const;
    105 
    106   /// FindAssociatedSection - Find the "associated section" for this expression,
    107   /// which is currently defined as the absolute section for constants, or
    108   /// otherwise the section associated with the first defined symbol in the
    109   /// expression.
    110   const MCSection *FindAssociatedSection() const;
    111 
    112   /// @}
    113 };
    114 
    115 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
    116   E.print(OS);
    117   return OS;
    118 }
    119 
    120 //// MCConstantExpr - Represent a constant integer expression.
    121 class MCConstantExpr : public MCExpr {
    122   int64_t Value;
    123 
    124   explicit MCConstantExpr(int64_t _Value)
    125     : MCExpr(MCExpr::Constant), Value(_Value) {}
    126 
    127 public:
    128   /// @name Construction
    129   /// @{
    130 
    131   static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
    132 
    133   /// @}
    134   /// @name Accessors
    135   /// @{
    136 
    137   int64_t getValue() const { return Value; }
    138 
    139   /// @}
    140 
    141   static bool classof(const MCExpr *E) {
    142     return E->getKind() == MCExpr::Constant;
    143   }
    144 };
    145 
    146 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
    147 /// expression.
    148 ///
    149 /// A symbol reference in an expression may be a use of a label, a use of an
    150 /// assembler variable (defined constant), or constitute an implicit definition
    151 /// of the symbol as external.
    152 class MCSymbolRefExpr : public MCExpr {
    153 public:
    154   enum VariantKind {
    155     VK_None,
    156     VK_Invalid,
    157 
    158     VK_GOT,
    159     VK_GOTOFF,
    160     VK_GOTPCREL,
    161     VK_GOTTPOFF,
    162     VK_INDNTPOFF,
    163     VK_NTPOFF,
    164     VK_GOTNTPOFF,
    165     VK_PLT,
    166     VK_TLSGD,
    167     VK_TLSLD,
    168     VK_TLSLDM,
    169     VK_TPOFF,
    170     VK_DTPOFF,
    171     VK_TLVP,      // Mach-O thread local variable relocations
    172     VK_TLVPPAGE,
    173     VK_TLVPPAGEOFF,
    174     VK_PAGE,
    175     VK_PAGEOFF,
    176     VK_GOTPAGE,
    177     VK_GOTPAGEOFF,
    178     VK_SECREL,
    179     VK_WEAKREF,   // The link between the symbols in .weakref foo, bar
    180 
    181     VK_ARM_NONE,
    182     VK_ARM_TARGET1,
    183     VK_ARM_TARGET2,
    184     VK_ARM_PREL31,
    185     VK_ARM_TLSLDO,         // symbol(tlsldo)
    186     VK_ARM_TLSCALL,        // symbol(tlscall)
    187     VK_ARM_TLSDESC,        // symbol(tlsdesc)
    188     VK_ARM_TLSDESCSEQ,
    189 
    190     VK_PPC_LO,             // symbol@l
    191     VK_PPC_HI,             // symbol@h
    192     VK_PPC_HA,             // symbol@ha
    193     VK_PPC_HIGHER,         // symbol@higher
    194     VK_PPC_HIGHERA,        // symbol@highera
    195     VK_PPC_HIGHEST,        // symbol@highest
    196     VK_PPC_HIGHESTA,       // symbol@highesta
    197     VK_PPC_GOT_LO,         // symbol@got@l
    198     VK_PPC_GOT_HI,         // symbol@got@h
    199     VK_PPC_GOT_HA,         // symbol@got@ha
    200     VK_PPC_TOCBASE,        // symbol@tocbase
    201     VK_PPC_TOC,            // symbol@toc
    202     VK_PPC_TOC_LO,         // symbol@toc@l
    203     VK_PPC_TOC_HI,         // symbol@toc@h
    204     VK_PPC_TOC_HA,         // symbol@toc@ha
    205     VK_PPC_DTPMOD,         // symbol@dtpmod
    206     VK_PPC_TPREL,          // symbol@tprel
    207     VK_PPC_TPREL_LO,       // symbol@tprel@l
    208     VK_PPC_TPREL_HI,       // symbol@tprel@h
    209     VK_PPC_TPREL_HA,       // symbol@tprel@ha
    210     VK_PPC_TPREL_HIGHER,   // symbol@tprel@higher
    211     VK_PPC_TPREL_HIGHERA,  // symbol@tprel@highera
    212     VK_PPC_TPREL_HIGHEST,  // symbol@tprel@highest
    213     VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
    214     VK_PPC_DTPREL,         // symbol@dtprel
    215     VK_PPC_DTPREL_LO,      // symbol@dtprel@l
    216     VK_PPC_DTPREL_HI,      // symbol@dtprel@h
    217     VK_PPC_DTPREL_HA,      // symbol@dtprel@ha
    218     VK_PPC_DTPREL_HIGHER,  // symbol@dtprel@higher
    219     VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
    220     VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
    221     VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta
    222     VK_PPC_GOT_TPREL,      // symbol@got@tprel
    223     VK_PPC_GOT_TPREL_LO,   // symbol@got@tprel@l
    224     VK_PPC_GOT_TPREL_HI,   // symbol@got@tprel@h
    225     VK_PPC_GOT_TPREL_HA,   // symbol@got@tprel@ha
    226     VK_PPC_GOT_DTPREL,     // symbol@got@dtprel
    227     VK_PPC_GOT_DTPREL_LO,  // symbol@got@dtprel@l
    228     VK_PPC_GOT_DTPREL_HI,  // symbol@got@dtprel@h
    229     VK_PPC_GOT_DTPREL_HA,  // symbol@got@dtprel@ha
    230     VK_PPC_TLS,            // symbol@tls
    231     VK_PPC_GOT_TLSGD,      // symbol@got@tlsgd
    232     VK_PPC_GOT_TLSGD_LO,   // symbol@got@tlsgd@l
    233     VK_PPC_GOT_TLSGD_HI,   // symbol@got@tlsgd@h
    234     VK_PPC_GOT_TLSGD_HA,   // symbol@got@tlsgd@ha
    235     VK_PPC_TLSGD,          // symbol@tlsgd
    236     VK_PPC_GOT_TLSLD,      // symbol@got@tlsld
    237     VK_PPC_GOT_TLSLD_LO,   // symbol@got@tlsld@l
    238     VK_PPC_GOT_TLSLD_HI,   // symbol@got@tlsld@h
    239     VK_PPC_GOT_TLSLD_HA,   // symbol@got@tlsld@ha
    240     VK_PPC_TLSLD,          // symbol@tlsld
    241 
    242     VK_Mips_GPREL,
    243     VK_Mips_GOT_CALL,
    244     VK_Mips_GOT16,
    245     VK_Mips_GOT,
    246     VK_Mips_ABS_HI,
    247     VK_Mips_ABS_LO,
    248     VK_Mips_TLSGD,
    249     VK_Mips_TLSLDM,
    250     VK_Mips_DTPREL_HI,
    251     VK_Mips_DTPREL_LO,
    252     VK_Mips_GOTTPREL,
    253     VK_Mips_TPREL_HI,
    254     VK_Mips_TPREL_LO,
    255     VK_Mips_GPOFF_HI,
    256     VK_Mips_GPOFF_LO,
    257     VK_Mips_GOT_DISP,
    258     VK_Mips_GOT_PAGE,
    259     VK_Mips_GOT_OFST,
    260     VK_Mips_HIGHER,
    261     VK_Mips_HIGHEST,
    262     VK_Mips_GOT_HI16,
    263     VK_Mips_GOT_LO16,
    264     VK_Mips_CALL_HI16,
    265     VK_Mips_CALL_LO16,
    266     VK_Mips_PCREL_HI16,
    267     VK_Mips_PCREL_LO16,
    268 
    269     VK_COFF_IMGREL32 // symbol@imgrel (image-relative)
    270   };
    271 
    272 private:
    273   /// The symbol being referenced.
    274   const MCSymbol *Symbol;
    275 
    276   /// The symbol reference modifier.
    277   const VariantKind Kind;
    278 
    279   /// MCAsmInfo that is used to print symbol variants correctly.
    280   const MCAsmInfo *MAI;
    281 
    282   explicit MCSymbolRefExpr(const MCSymbol *_Symbol, VariantKind _Kind,
    283                            const MCAsmInfo *_MAI)
    284     : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol), Kind(_Kind), MAI(_MAI) {
    285     assert(Symbol);
    286     assert(MAI);
    287   }
    288 
    289 public:
    290   /// @name Construction
    291   /// @{
    292 
    293   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) {
    294     return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx);
    295   }
    296 
    297   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind,
    298                                        MCContext &Ctx);
    299   static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind,
    300                                        MCContext &Ctx);
    301 
    302   /// @}
    303   /// @name Accessors
    304   /// @{
    305 
    306   const MCSymbol &getSymbol() const { return *Symbol; }
    307   const MCAsmInfo &getMCAsmInfo() const { return *MAI; }
    308 
    309   VariantKind getKind() const { return Kind; }
    310 
    311   /// @}
    312   /// @name Static Utility Functions
    313   /// @{
    314 
    315   static StringRef getVariantKindName(VariantKind Kind);
    316 
    317   static VariantKind getVariantKindForName(StringRef Name);
    318 
    319   /// @}
    320 
    321   static bool classof(const MCExpr *E) {
    322     return E->getKind() == MCExpr::SymbolRef;
    323   }
    324 };
    325 
    326 /// MCUnaryExpr - Unary assembler expressions.
    327 class MCUnaryExpr : public MCExpr {
    328 public:
    329   enum Opcode {
    330     LNot,  ///< Logical negation.
    331     Minus, ///< Unary minus.
    332     Not,   ///< Bitwise negation.
    333     Plus   ///< Unary plus.
    334   };
    335 
    336 private:
    337   Opcode Op;
    338   const MCExpr *Expr;
    339 
    340   MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
    341     : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
    342 
    343 public:
    344   /// @name Construction
    345   /// @{
    346 
    347   static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
    348                                    MCContext &Ctx);
    349   static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
    350     return Create(LNot, Expr, Ctx);
    351   }
    352   static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
    353     return Create(Minus, Expr, Ctx);
    354   }
    355   static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
    356     return Create(Not, Expr, Ctx);
    357   }
    358   static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
    359     return Create(Plus, Expr, Ctx);
    360   }
    361 
    362   /// @}
    363   /// @name Accessors
    364   /// @{
    365 
    366   /// getOpcode - Get the kind of this unary expression.
    367   Opcode getOpcode() const { return Op; }
    368 
    369   /// getSubExpr - Get the child of this unary expression.
    370   const MCExpr *getSubExpr() const { return Expr; }
    371 
    372   /// @}
    373 
    374   static bool classof(const MCExpr *E) {
    375     return E->getKind() == MCExpr::Unary;
    376   }
    377 };
    378 
    379 /// MCBinaryExpr - Binary assembler expressions.
    380 class MCBinaryExpr : public MCExpr {
    381 public:
    382   enum Opcode {
    383     Add,  ///< Addition.
    384     And,  ///< Bitwise and.
    385     Div,  ///< Signed division.
    386     EQ,   ///< Equality comparison.
    387     GT,   ///< Signed greater than comparison (result is either 0 or some
    388           ///< target-specific non-zero value)
    389     GTE,  ///< Signed greater than or equal comparison (result is either 0 or
    390           ///< some target-specific non-zero value).
    391     LAnd, ///< Logical and.
    392     LOr,  ///< Logical or.
    393     LT,   ///< Signed less than comparison (result is either 0 or
    394           ///< some target-specific non-zero value).
    395     LTE,  ///< Signed less than or equal comparison (result is either 0 or
    396           ///< some target-specific non-zero value).
    397     Mod,  ///< Signed remainder.
    398     Mul,  ///< Multiplication.
    399     NE,   ///< Inequality comparison.
    400     Or,   ///< Bitwise or.
    401     Shl,  ///< Shift left.
    402     Shr,  ///< Shift right (arithmetic or logical, depending on target)
    403     Sub,  ///< Subtraction.
    404     Xor   ///< Bitwise exclusive or.
    405   };
    406 
    407 private:
    408   Opcode Op;
    409   const MCExpr *LHS, *RHS;
    410 
    411   MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
    412     : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
    413 
    414 public:
    415   /// @name Construction
    416   /// @{
    417 
    418   static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
    419                                     const MCExpr *RHS, MCContext &Ctx);
    420   static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
    421                                        MCContext &Ctx) {
    422     return Create(Add, LHS, RHS, Ctx);
    423   }
    424   static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
    425                                        MCContext &Ctx) {
    426     return Create(And, LHS, RHS, Ctx);
    427   }
    428   static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
    429                                        MCContext &Ctx) {
    430     return Create(Div, LHS, RHS, Ctx);
    431   }
    432   static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
    433                                       MCContext &Ctx) {
    434     return Create(EQ, LHS, RHS, Ctx);
    435   }
    436   static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
    437                                       MCContext &Ctx) {
    438     return Create(GT, LHS, RHS, Ctx);
    439   }
    440   static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
    441                                        MCContext &Ctx) {
    442     return Create(GTE, LHS, RHS, Ctx);
    443   }
    444   static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
    445                                         MCContext &Ctx) {
    446     return Create(LAnd, LHS, RHS, Ctx);
    447   }
    448   static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
    449                                        MCContext &Ctx) {
    450     return Create(LOr, LHS, RHS, Ctx);
    451   }
    452   static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
    453                                       MCContext &Ctx) {
    454     return Create(LT, LHS, RHS, Ctx);
    455   }
    456   static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
    457                                        MCContext &Ctx) {
    458     return Create(LTE, LHS, RHS, Ctx);
    459   }
    460   static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
    461                                        MCContext &Ctx) {
    462     return Create(Mod, LHS, RHS, Ctx);
    463   }
    464   static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
    465                                        MCContext &Ctx) {
    466     return Create(Mul, LHS, RHS, Ctx);
    467   }
    468   static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
    469                                       MCContext &Ctx) {
    470     return Create(NE, LHS, RHS, Ctx);
    471   }
    472   static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
    473                                       MCContext &Ctx) {
    474     return Create(Or, LHS, RHS, Ctx);
    475   }
    476   static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
    477                                        MCContext &Ctx) {
    478     return Create(Shl, LHS, RHS, Ctx);
    479   }
    480   static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
    481                                        MCContext &Ctx) {
    482     return Create(Shr, LHS, RHS, Ctx);
    483   }
    484   static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
    485                                        MCContext &Ctx) {
    486     return Create(Sub, LHS, RHS, Ctx);
    487   }
    488   static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
    489                                        MCContext &Ctx) {
    490     return Create(Xor, LHS, RHS, Ctx);
    491   }
    492 
    493   /// @}
    494   /// @name Accessors
    495   /// @{
    496 
    497   /// getOpcode - Get the kind of this binary expression.
    498   Opcode getOpcode() const { return Op; }
    499 
    500   /// getLHS - Get the left-hand side expression of the binary operator.
    501   const MCExpr *getLHS() const { return LHS; }
    502 
    503   /// getRHS - Get the right-hand side expression of the binary operator.
    504   const MCExpr *getRHS() const { return RHS; }
    505 
    506   /// @}
    507 
    508   static bool classof(const MCExpr *E) {
    509     return E->getKind() == MCExpr::Binary;
    510   }
    511 };
    512 
    513 /// MCTargetExpr - This is an extension point for target-specific MCExpr
    514 /// subclasses to implement.
    515 ///
    516 /// NOTE: All subclasses are required to have trivial destructors because
    517 /// MCExprs are bump pointer allocated and not destructed.
    518 class MCTargetExpr : public MCExpr {
    519   virtual void anchor();
    520 protected:
    521   MCTargetExpr() : MCExpr(Target) {}
    522   virtual ~MCTargetExpr() {}
    523 public:
    524 
    525   virtual void PrintImpl(raw_ostream &OS) const = 0;
    526   virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
    527                                          const MCAsmLayout *Layout) const = 0;
    528   virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
    529   virtual const MCSection *FindAssociatedSection() const = 0;
    530 
    531   virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
    532 
    533   static bool classof(const MCExpr *E) {
    534     return E->getKind() == MCExpr::Target;
    535   }
    536 };
    537 
    538 } // end namespace llvm
    539 
    540 #endif
    541