Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 1 | //===- AsmExpr.h - Assembly file 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 ASMEXPR_H |
| 11 | #define ASMEXPR_H |
| 12 | |
| 13 | #include "llvm/Support/Casting.h" |
| 14 | #include "llvm/Support/DataTypes.h" |
| 15 | |
| 16 | namespace llvm { |
| 17 | class MCContext; |
| 18 | class MCSymbol; |
Daniel Dunbar | c6c054e | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 19 | class MCValue; |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 20 | |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 21 | /// AsmExpr - Base class for the full range of assembler expressions which are |
| 22 | /// needed for parsing. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 23 | class AsmExpr { |
| 24 | public: |
| 25 | enum AsmExprKind { |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 26 | Binary, ///< Binary expressions. |
| 27 | Constant, ///< Constant expressions. |
| 28 | SymbolRef, ///< References to labels and assigned expressions. |
| 29 | Unary ///< Unary expressions. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | private: |
| 33 | AsmExprKind Kind; |
| 34 | |
| 35 | protected: |
| 36 | AsmExpr(AsmExprKind _Kind) : Kind(_Kind) {} |
| 37 | |
| 38 | public: |
| 39 | virtual ~AsmExpr(); |
| 40 | |
| 41 | AsmExprKind getKind() const { return Kind; } |
| 42 | |
| 43 | /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value. |
| 44 | /// |
Daniel Dunbar | c6c054e | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 45 | /// @param Res - The absolute value, if evaluation succeeds. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 46 | /// @result - True on success. |
| 47 | bool EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const; |
| 48 | |
Daniel Dunbar | c6c054e | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 49 | /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 50 | /// value, i.e. an expression of the fixed form (a - b + constant). |
Daniel Dunbar | c6c054e | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 51 | /// |
| 52 | /// @param Res - The relocatable value, if evaluation succeeds. |
| 53 | /// @result - True on success. |
| 54 | bool EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const; |
| 55 | |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 56 | static bool classof(const AsmExpr *) { return true; } |
| 57 | }; |
| 58 | |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 59 | //// AsmConstantExpr - Represent a constant integer expression. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 60 | class AsmConstantExpr : public AsmExpr { |
| 61 | int64_t Value; |
| 62 | |
| 63 | public: |
| 64 | AsmConstantExpr(int64_t _Value) |
| 65 | : AsmExpr(AsmExpr::Constant), Value(_Value) {} |
| 66 | |
| 67 | int64_t getValue() const { return Value; } |
| 68 | |
| 69 | static bool classof(const AsmExpr *E) { |
| 70 | return E->getKind() == AsmExpr::Constant; |
| 71 | } |
| 72 | static bool classof(const AsmConstantExpr *) { return true; } |
| 73 | }; |
| 74 | |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 75 | /// AsmSymbolRefExpr - Represent a reference to a symbol from inside an |
| 76 | /// expression. |
| 77 | /// |
| 78 | /// A symbol reference in an expression may be a use of a label, a use of an |
| 79 | /// assembler variable (defined constant), or constitute an implicit definition |
| 80 | /// of the symbol as external. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 81 | class AsmSymbolRefExpr : public AsmExpr { |
| 82 | MCSymbol *Symbol; |
| 83 | |
| 84 | public: |
| 85 | AsmSymbolRefExpr(MCSymbol *_Symbol) |
| 86 | : AsmExpr(AsmExpr::SymbolRef), Symbol(_Symbol) {} |
| 87 | |
| 88 | MCSymbol *getSymbol() const { return Symbol; } |
| 89 | |
| 90 | static bool classof(const AsmExpr *E) { |
| 91 | return E->getKind() == AsmExpr::SymbolRef; |
| 92 | } |
| 93 | static bool classof(const AsmSymbolRefExpr *) { return true; } |
| 94 | }; |
| 95 | |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 96 | /// AsmUnaryExpr - Unary assembler expressions. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 97 | class AsmUnaryExpr : public AsmExpr { |
| 98 | public: |
| 99 | enum Opcode { |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 100 | LNot, ///< Logical negation. |
| 101 | Minus, ///< Unary minus. |
| 102 | Not, ///< Bitwise negation. |
| 103 | Plus ///< Unary plus. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | private: |
| 107 | Opcode Op; |
| 108 | AsmExpr *Expr; |
| 109 | |
| 110 | public: |
| 111 | AsmUnaryExpr(Opcode _Op, AsmExpr *_Expr) |
| 112 | : AsmExpr(AsmExpr::Unary), Op(_Op), Expr(_Expr) {} |
| 113 | ~AsmUnaryExpr() { |
| 114 | delete Expr; |
| 115 | } |
| 116 | |
| 117 | Opcode getOpcode() const { return Op; } |
| 118 | |
| 119 | AsmExpr *getSubExpr() const { return Expr; } |
| 120 | |
| 121 | static bool classof(const AsmExpr *E) { |
| 122 | return E->getKind() == AsmExpr::Unary; |
| 123 | } |
| 124 | static bool classof(const AsmUnaryExpr *) { return true; } |
| 125 | }; |
| 126 | |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 127 | /// AsmBinaryExpr - Binary assembler expressions. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 128 | class AsmBinaryExpr : public AsmExpr { |
| 129 | public: |
| 130 | enum Opcode { |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 131 | Add, ///< Addition. |
| 132 | And, ///< Bitwise and. |
| 133 | Div, ///< Division. |
| 134 | EQ, ///< Equality comparison. |
| 135 | GT, ///< Greater than comparison. |
| 136 | GTE, ///< Greater than or equal comparison. |
| 137 | LAnd, ///< Logical and. |
| 138 | LOr, ///< Logical or. |
| 139 | LT, ///< Less than comparison. |
| 140 | LTE, ///< Less than or equal comparison. |
| 141 | Mod, ///< Modulus. |
| 142 | Mul, ///< Multiplication. |
| 143 | NE, ///< Inequality comparison. |
| 144 | Or, ///< Bitwise or. |
| 145 | Shl, ///< Bitwise shift left. |
| 146 | Shr, ///< Bitwise shift right. |
| 147 | Sub, ///< Subtraction. |
| 148 | Xor ///< Bitwise exclusive or. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | private: |
| 152 | Opcode Op; |
| 153 | AsmExpr *LHS, *RHS; |
| 154 | |
| 155 | public: |
| 156 | AsmBinaryExpr(Opcode _Op, AsmExpr *_LHS, AsmExpr *_RHS) |
| 157 | : AsmExpr(AsmExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {} |
| 158 | ~AsmBinaryExpr() { |
| 159 | delete LHS; |
| 160 | delete RHS; |
| 161 | } |
| 162 | |
| 163 | Opcode getOpcode() const { return Op; } |
| 164 | |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 165 | /// getLHS - Get the left-hand side expression of the binary operator. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 166 | AsmExpr *getLHS() const { return LHS; } |
Daniel Dunbar | d06b310 | 2009-07-01 15:14:50 +0000 | [diff] [blame] | 167 | |
| 168 | /// getRHS - Get the right-hand side expression of the binary operator. |
Daniel Dunbar | a1f0464 | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 169 | AsmExpr *getRHS() const { return RHS; } |
| 170 | |
| 171 | static bool classof(const AsmExpr *E) { |
| 172 | return E->getKind() == AsmExpr::Binary; |
| 173 | } |
| 174 | static bool classof(const AsmBinaryExpr *) { return true; } |
| 175 | }; |
| 176 | |
| 177 | } // end namespace llvm |
| 178 | |
| 179 | #endif |