LLVM API Documentation

MCExpr.h
Go to the documentation of this file.
00001 //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_MC_MCEXPR_H
00011 #define LLVM_MC_MCEXPR_H
00012 
00013 #include "llvm/ADT/DenseMap.h"
00014 #include "llvm/Support/Casting.h"
00015 #include "llvm/Support/DataTypes.h"
00016 
00017 namespace llvm {
00018 class MCAsmLayout;
00019 class MCAssembler;
00020 class MCContext;
00021 class MCSection;
00022 class MCSectionData;
00023 class MCSymbol;
00024 class MCValue;
00025 class raw_ostream;
00026 class StringRef;
00027 typedef DenseMap<const MCSectionData*, uint64_t> SectionAddrMap;
00028 
00029 /// MCExpr - Base class for the full range of assembler expressions which are
00030 /// needed for parsing.
00031 class MCExpr {
00032 public:
00033   enum ExprKind {
00034     Binary,    ///< Binary expressions.
00035     Constant,  ///< Constant expressions.
00036     SymbolRef, ///< References to labels and assigned expressions.
00037     Unary,     ///< Unary expressions.
00038     Target     ///< Target specific expression.
00039   };
00040 
00041 private:
00042   ExprKind Kind;
00043 
00044   MCExpr(const MCExpr&) LLVM_DELETED_FUNCTION;
00045   void operator=(const MCExpr&) LLVM_DELETED_FUNCTION;
00046 
00047   bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
00048                           const MCAsmLayout *Layout,
00049                           const SectionAddrMap *Addrs) const;
00050 protected:
00051   explicit MCExpr(ExprKind _Kind) : Kind(_Kind) {}
00052 
00053   bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
00054                                  const MCAsmLayout *Layout,
00055                                  const SectionAddrMap *Addrs,
00056                                  bool InSet) const;
00057 public:
00058   /// @name Accessors
00059   /// @{
00060 
00061   ExprKind getKind() const { return Kind; }
00062 
00063   /// @}
00064   /// @name Utility Methods
00065   /// @{
00066 
00067   void print(raw_ostream &OS) const;
00068   void dump() const;
00069 
00070   /// @}
00071   /// @name Expression Evaluation
00072   /// @{
00073 
00074   /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
00075   ///
00076   /// @param Res - The absolute value, if evaluation succeeds.
00077   /// @param Layout - The assembler layout object to use for evaluating symbol
00078   /// values. If not given, then only non-symbolic expressions will be
00079   /// evaluated.
00080   /// @result - True on success.
00081   bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
00082                           const SectionAddrMap &Addrs) const;
00083   bool EvaluateAsAbsolute(int64_t &Res) const;
00084   bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
00085   bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
00086 
00087   /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
00088   /// value, i.e. an expression of the fixed form (a - b + constant).
00089   ///
00090   /// @param Res - The relocatable value, if evaluation succeeds.
00091   /// @param Layout - The assembler layout object to use for evaluating values.
00092   /// @result - True on success.
00093   bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout &Layout) const;
00094 
00095   /// FindAssociatedSection - Find the "associated section" for this expression,
00096   /// which is currently defined as the absolute section for constants, or
00097   /// otherwise the section associated with the first defined symbol in the
00098   /// expression.
00099   const MCSection *FindAssociatedSection() const;
00100 
00101   /// @}
00102 };
00103 
00104 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
00105   E.print(OS);
00106   return OS;
00107 }
00108 
00109 //// MCConstantExpr - Represent a constant integer expression.
00110 class MCConstantExpr : public MCExpr {
00111   int64_t Value;
00112 
00113   explicit MCConstantExpr(int64_t _Value)
00114     : MCExpr(MCExpr::Constant), Value(_Value) {}
00115 
00116 public:
00117   /// @name Construction
00118   /// @{
00119 
00120   static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
00121 
00122   /// @}
00123   /// @name Accessors
00124   /// @{
00125 
00126   int64_t getValue() const { return Value; }
00127 
00128   /// @}
00129 
00130   static bool classof(const MCExpr *E) {
00131     return E->getKind() == MCExpr::Constant;
00132   }
00133 };
00134 
00135 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
00136 /// expression.
00137 ///
00138 /// A symbol reference in an expression may be a use of a label, a use of an
00139 /// assembler variable (defined constant), or constitute an implicit definition
00140 /// of the symbol as external.
00141 class MCSymbolRefExpr : public MCExpr {
00142 public:
00143   enum VariantKind {
00144     VK_None,
00145     VK_Invalid,
00146 
00147     VK_GOT,
00148     VK_GOTOFF,
00149     VK_GOTPCREL,
00150     VK_GOTTPOFF,
00151     VK_INDNTPOFF,
00152     VK_NTPOFF,
00153     VK_GOTNTPOFF,
00154     VK_PLT,
00155     VK_TLSGD,
00156     VK_TLSLD,
00157     VK_TLSLDM,
00158     VK_TPOFF,
00159     VK_DTPOFF,
00160     VK_TLVP,      // Mach-O thread local variable relocation
00161     VK_SECREL,
00162     // FIXME: We'd really like to use the generic Kinds listed above for these.
00163     VK_ARM_NONE,
00164     VK_ARM_PLT,   // ARM-style PLT references. i.e., (PLT) instead of @PLT
00165     VK_ARM_TLSGD, //   ditto for TLSGD, GOT, GOTOFF, TPOFF and GOTTPOFF
00166     VK_ARM_GOT,
00167     VK_ARM_GOTOFF,
00168     VK_ARM_TPOFF,
00169     VK_ARM_GOTTPOFF,
00170     VK_ARM_TARGET1,
00171     VK_ARM_TARGET2,
00172     VK_ARM_PREL31,
00173 
00174     VK_PPC_TOC,          // TOC base
00175     VK_PPC_TOC_ENTRY,    // TOC entry
00176     VK_PPC_DARWIN_HA16,  // ha16(symbol)
00177     VK_PPC_DARWIN_LO16,  // lo16(symbol)
00178     VK_PPC_GAS_HA16,     // symbol@ha
00179     VK_PPC_GAS_LO16,     // symbol@l
00180     VK_PPC_TPREL16_HA,   // symbol@tprel@ha
00181     VK_PPC_TPREL16_LO,   // symbol@tprel@l
00182     VK_PPC_DTPREL16_HA,  // symbol@dtprel@ha
00183     VK_PPC_DTPREL16_LO,  // symbol@dtprel@l
00184     VK_PPC_TOC16_HA,     // symbol@toc@ha
00185     VK_PPC_TOC16_LO,     // symbol@toc@l
00186     VK_PPC_GOT_TPREL16_HA, // symbol@got@tprel@ha
00187     VK_PPC_GOT_TPREL16_LO, // symbol@got@tprel@l
00188     VK_PPC_TLS,            // symbol@tls
00189     VK_PPC_GOT_TLSGD16_HA, // symbol@got@tlsgd@ha
00190     VK_PPC_GOT_TLSGD16_LO, // symbol@got@tlsgd@l
00191     VK_PPC_TLSGD,          // symbol@tlsgd
00192     VK_PPC_GOT_TLSLD16_HA, // symbol@got@tlsld@ha
00193     VK_PPC_GOT_TLSLD16_LO, // symbol@got@tlsld@l
00194     VK_PPC_TLSLD,          // symbol@tlsld
00195 
00196     VK_Mips_GPREL,
00197     VK_Mips_GOT_CALL,
00198     VK_Mips_GOT16,
00199     VK_Mips_GOT,
00200     VK_Mips_ABS_HI,
00201     VK_Mips_ABS_LO,
00202     VK_Mips_TLSGD,
00203     VK_Mips_TLSLDM,
00204     VK_Mips_DTPREL_HI,
00205     VK_Mips_DTPREL_LO,
00206     VK_Mips_GOTTPREL,
00207     VK_Mips_TPREL_HI,
00208     VK_Mips_TPREL_LO,
00209     VK_Mips_GPOFF_HI,
00210     VK_Mips_GPOFF_LO,
00211     VK_Mips_GOT_DISP,
00212     VK_Mips_GOT_PAGE,
00213     VK_Mips_GOT_OFST,
00214     VK_Mips_HIGHER,
00215     VK_Mips_HIGHEST,
00216     VK_Mips_GOT_HI16,
00217     VK_Mips_GOT_LO16,
00218     VK_Mips_CALL_HI16,
00219     VK_Mips_CALL_LO16,
00220 
00221     VK_COFF_IMGREL32 // symbol@imgrel (image-relative)
00222   };
00223 
00224 private:
00225   /// The symbol being referenced.
00226   const MCSymbol *Symbol;
00227 
00228   /// The symbol reference modifier.
00229   const VariantKind Kind;
00230 
00231   explicit MCSymbolRefExpr(const MCSymbol *_Symbol, VariantKind _Kind)
00232     : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol), Kind(_Kind) {
00233     assert(Symbol);
00234   }
00235 
00236 public:
00237   /// @name Construction
00238   /// @{
00239 
00240   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) {
00241     return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx);
00242   }
00243 
00244   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind,
00245                                        MCContext &Ctx);
00246   static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind,
00247                                        MCContext &Ctx);
00248 
00249   /// @}
00250   /// @name Accessors
00251   /// @{
00252 
00253   const MCSymbol &getSymbol() const { return *Symbol; }
00254 
00255   VariantKind getKind() const { return Kind; }
00256 
00257   /// @}
00258   /// @name Static Utility Functions
00259   /// @{
00260 
00261   static StringRef getVariantKindName(VariantKind Kind);
00262 
00263   static VariantKind getVariantKindForName(StringRef Name);
00264 
00265   /// @}
00266 
00267   static bool classof(const MCExpr *E) {
00268     return E->getKind() == MCExpr::SymbolRef;
00269   }
00270 };
00271 
00272 /// MCUnaryExpr - Unary assembler expressions.
00273 class MCUnaryExpr : public MCExpr {
00274 public:
00275   enum Opcode {
00276     LNot,  ///< Logical negation.
00277     Minus, ///< Unary minus.
00278     Not,   ///< Bitwise negation.
00279     Plus   ///< Unary plus.
00280   };
00281 
00282 private:
00283   Opcode Op;
00284   const MCExpr *Expr;
00285 
00286   MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
00287     : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
00288 
00289 public:
00290   /// @name Construction
00291   /// @{
00292 
00293   static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
00294                                    MCContext &Ctx);
00295   static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
00296     return Create(LNot, Expr, Ctx);
00297   }
00298   static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
00299     return Create(Minus, Expr, Ctx);
00300   }
00301   static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
00302     return Create(Not, Expr, Ctx);
00303   }
00304   static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
00305     return Create(Plus, Expr, Ctx);
00306   }
00307 
00308   /// @}
00309   /// @name Accessors
00310   /// @{
00311 
00312   /// getOpcode - Get the kind of this unary expression.
00313   Opcode getOpcode() const { return Op; }
00314 
00315   /// getSubExpr - Get the child of this unary expression.
00316   const MCExpr *getSubExpr() const { return Expr; }
00317 
00318   /// @}
00319 
00320   static bool classof(const MCExpr *E) {
00321     return E->getKind() == MCExpr::Unary;
00322   }
00323 };
00324 
00325 /// MCBinaryExpr - Binary assembler expressions.
00326 class MCBinaryExpr : public MCExpr {
00327 public:
00328   enum Opcode {
00329     Add,  ///< Addition.
00330     And,  ///< Bitwise and.
00331     Div,  ///< Signed division.
00332     EQ,   ///< Equality comparison.
00333     GT,   ///< Signed greater than comparison (result is either 0 or some
00334           ///< target-specific non-zero value)
00335     GTE,  ///< Signed greater than or equal comparison (result is either 0 or
00336           ///< some target-specific non-zero value).
00337     LAnd, ///< Logical and.
00338     LOr,  ///< Logical or.
00339     LT,   ///< Signed less than comparison (result is either 0 or
00340           ///< some target-specific non-zero value).
00341     LTE,  ///< Signed less than or equal comparison (result is either 0 or
00342           ///< some target-specific non-zero value).
00343     Mod,  ///< Signed remainder.
00344     Mul,  ///< Multiplication.
00345     NE,   ///< Inequality comparison.
00346     Or,   ///< Bitwise or.
00347     Shl,  ///< Shift left.
00348     Shr,  ///< Shift right (arithmetic or logical, depending on target)
00349     Sub,  ///< Subtraction.
00350     Xor   ///< Bitwise exclusive or.
00351   };
00352 
00353 private:
00354   Opcode Op;
00355   const MCExpr *LHS, *RHS;
00356 
00357   MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
00358     : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
00359 
00360 public:
00361   /// @name Construction
00362   /// @{
00363 
00364   static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
00365                                     const MCExpr *RHS, MCContext &Ctx);
00366   static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
00367                                        MCContext &Ctx) {
00368     return Create(Add, LHS, RHS, Ctx);
00369   }
00370   static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
00371                                        MCContext &Ctx) {
00372     return Create(And, LHS, RHS, Ctx);
00373   }
00374   static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
00375                                        MCContext &Ctx) {
00376     return Create(Div, LHS, RHS, Ctx);
00377   }
00378   static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
00379                                       MCContext &Ctx) {
00380     return Create(EQ, LHS, RHS, Ctx);
00381   }
00382   static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
00383                                       MCContext &Ctx) {
00384     return Create(GT, LHS, RHS, Ctx);
00385   }
00386   static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
00387                                        MCContext &Ctx) {
00388     return Create(GTE, LHS, RHS, Ctx);
00389   }
00390   static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
00391                                         MCContext &Ctx) {
00392     return Create(LAnd, LHS, RHS, Ctx);
00393   }
00394   static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
00395                                        MCContext &Ctx) {
00396     return Create(LOr, LHS, RHS, Ctx);
00397   }
00398   static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
00399                                       MCContext &Ctx) {
00400     return Create(LT, LHS, RHS, Ctx);
00401   }
00402   static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
00403                                        MCContext &Ctx) {
00404     return Create(LTE, LHS, RHS, Ctx);
00405   }
00406   static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
00407                                        MCContext &Ctx) {
00408     return Create(Mod, LHS, RHS, Ctx);
00409   }
00410   static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
00411                                        MCContext &Ctx) {
00412     return Create(Mul, LHS, RHS, Ctx);
00413   }
00414   static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
00415                                       MCContext &Ctx) {
00416     return Create(NE, LHS, RHS, Ctx);
00417   }
00418   static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
00419                                       MCContext &Ctx) {
00420     return Create(Or, LHS, RHS, Ctx);
00421   }
00422   static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
00423                                        MCContext &Ctx) {
00424     return Create(Shl, LHS, RHS, Ctx);
00425   }
00426   static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
00427                                        MCContext &Ctx) {
00428     return Create(Shr, LHS, RHS, Ctx);
00429   }
00430   static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
00431                                        MCContext &Ctx) {
00432     return Create(Sub, LHS, RHS, Ctx);
00433   }
00434   static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
00435                                        MCContext &Ctx) {
00436     return Create(Xor, LHS, RHS, Ctx);
00437   }
00438 
00439   /// @}
00440   /// @name Accessors
00441   /// @{
00442 
00443   /// getOpcode - Get the kind of this binary expression.
00444   Opcode getOpcode() const { return Op; }
00445 
00446   /// getLHS - Get the left-hand side expression of the binary operator.
00447   const MCExpr *getLHS() const { return LHS; }
00448 
00449   /// getRHS - Get the right-hand side expression of the binary operator.
00450   const MCExpr *getRHS() const { return RHS; }
00451 
00452   /// @}
00453 
00454   static bool classof(const MCExpr *E) {
00455     return E->getKind() == MCExpr::Binary;
00456   }
00457 };
00458 
00459 /// MCTargetExpr - This is an extension point for target-specific MCExpr
00460 /// subclasses to implement.
00461 ///
00462 /// NOTE: All subclasses are required to have trivial destructors because
00463 /// MCExprs are bump pointer allocated and not destructed.
00464 class MCTargetExpr : public MCExpr {
00465   virtual void anchor();
00466 protected:
00467   MCTargetExpr() : MCExpr(Target) {}
00468   virtual ~MCTargetExpr() {}
00469 public:
00470 
00471   virtual void PrintImpl(raw_ostream &OS) const = 0;
00472   virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
00473                                          const MCAsmLayout *Layout) const = 0;
00474   virtual void AddValueSymbols(MCAssembler *) const = 0;
00475   virtual const MCSection *FindAssociatedSection() const = 0;
00476 
00477   virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
00478 
00479   static bool classof(const MCExpr *E) {
00480     return E->getKind() == MCExpr::Target;
00481   }
00482 };
00483 
00484 } // end namespace llvm
00485 
00486 #endif