MCExpr.h revision 87392fde1f261fea161b48886fafbedddb18dcce
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/Support/Casting.h" 14#include "llvm/Support/DataTypes.h" 15 16namespace llvm { 17class MCContext; 18class MCSymbol; 19class MCValue; 20class raw_ostream; 21 22/// MCExpr - Base class for the full range of assembler expressions which are 23/// needed for parsing. 24class MCExpr { 25public: 26 enum ExprKind { 27 Binary, ///< Binary expressions. 28 Constant, ///< Constant expressions. 29 SymbolRef, ///< References to labels and assigned expressions. 30 Unary ///< Unary expressions. 31 }; 32 33private: 34 ExprKind Kind; 35 36 MCExpr(const MCExpr&); // DO NOT IMPLEMENT 37 void operator=(const MCExpr&); // DO NOT IMPLEMENT 38 39protected: 40 MCExpr(ExprKind _Kind) : Kind(_Kind) {} 41 42public: 43 /// @name Accessors 44 /// @{ 45 46 ExprKind getKind() const { return Kind; } 47 48 /// @} 49 /// @name Utility Methods 50 /// @{ 51 52 void print(raw_ostream &OS) const; 53 void dump() const; 54 55 /// @} 56 /// @name Expression Evaluation 57 /// @{ 58 59 /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value. 60 /// 61 /// @param Res - The absolute value, if evaluation succeeds. 62 /// @result - True on success. 63 bool EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const; 64 65 /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable 66 /// value, i.e. an expression of the fixed form (a - b + constant). 67 /// 68 /// @param Res - The relocatable value, if evaluation succeeds. 69 /// @result - True on success. 70 bool EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const; 71 72 /// @} 73 74 static bool classof(const MCExpr *) { return true; } 75}; 76 77//// MCConstantExpr - Represent a constant integer expression. 78class MCConstantExpr : public MCExpr { 79 int64_t Value; 80 81 MCConstantExpr(int64_t _Value) 82 : MCExpr(MCExpr::Constant), Value(_Value) {} 83 84public: 85 /// @name Construction 86 /// @{ 87 88 static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx); 89 90 /// @} 91 /// @name Accessors 92 /// @{ 93 94 int64_t getValue() const { return Value; } 95 96 /// @} 97 98 static bool classof(const MCExpr *E) { 99 return E->getKind() == MCExpr::Constant; 100 } 101 static bool classof(const MCConstantExpr *) { return true; } 102}; 103 104/// MCSymbolRefExpr - Represent a reference to a symbol from inside an 105/// expression. 106/// 107/// A symbol reference in an expression may be a use of a label, a use of an 108/// assembler variable (defined constant), or constitute an implicit definition 109/// of the symbol as external. 110class MCSymbolRefExpr : public MCExpr { 111 const MCSymbol *Symbol; 112 113 MCSymbolRefExpr(const MCSymbol *_Symbol) 114 : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol) {} 115 116public: 117 /// @name Construction 118 /// @{ 119 120 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx); 121 122 /// @} 123 /// @name Accessors 124 /// @{ 125 126 const MCSymbol &getSymbol() const { return *Symbol; } 127 128 /// @} 129 130 static bool classof(const MCExpr *E) { 131 return E->getKind() == MCExpr::SymbolRef; 132 } 133 static bool classof(const MCSymbolRefExpr *) { return true; } 134}; 135 136/// MCUnaryExpr - Unary assembler expressions. 137class MCUnaryExpr : public MCExpr { 138public: 139 enum Opcode { 140 LNot, ///< Logical negation. 141 Minus, ///< Unary minus. 142 Not, ///< Bitwise negation. 143 Plus ///< Unary plus. 144 }; 145 146private: 147 Opcode Op; 148 const MCExpr *Expr; 149 150 MCUnaryExpr(Opcode _Op, const MCExpr *_Expr) 151 : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {} 152 153public: 154 /// @name Construction 155 /// @{ 156 157 static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr, 158 MCContext &Ctx); 159 static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) { 160 return Create(LNot, Expr, Ctx); 161 } 162 static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) { 163 return Create(Minus, Expr, Ctx); 164 } 165 static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) { 166 return Create(Not, Expr, Ctx); 167 } 168 static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) { 169 return Create(Plus, Expr, Ctx); 170 } 171 172 /// @} 173 /// @name Accessors 174 /// @{ 175 176 /// getOpcode - Get the kind of this unary expression. 177 Opcode getOpcode() const { return Op; } 178 179 /// getSubExpr - Get the child of this unary expression. 180 const MCExpr *getSubExpr() const { return Expr; } 181 182 /// @} 183 184 static bool classof(const MCExpr *E) { 185 return E->getKind() == MCExpr::Unary; 186 } 187 static bool classof(const MCUnaryExpr *) { return true; } 188}; 189 190/// MCBinaryExpr - Binary assembler expressions. 191class MCBinaryExpr : public MCExpr { 192public: 193 enum Opcode { 194 Add, ///< Addition. 195 And, ///< Bitwise and. 196 Div, ///< Division. 197 EQ, ///< Equality comparison. 198 GT, ///< Greater than comparison. 199 GTE, ///< Greater than or equal comparison. 200 LAnd, ///< Logical and. 201 LOr, ///< Logical or. 202 LT, ///< Less than comparison. 203 LTE, ///< Less than or equal comparison. 204 Mod, ///< Modulus. 205 Mul, ///< Multiplication. 206 NE, ///< Inequality comparison. 207 Or, ///< Bitwise or. 208 Shl, ///< Bitwise shift left. 209 Shr, ///< Bitwise shift right. 210 Sub, ///< Subtraction. 211 Xor ///< Bitwise exclusive or. 212 }; 213 214private: 215 Opcode Op; 216 const MCExpr *LHS, *RHS; 217 218 MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS) 219 : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {} 220 221public: 222 /// @name Construction 223 /// @{ 224 225 static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS, 226 const MCExpr *RHS, MCContext &Ctx); 227 static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS, 228 MCContext &Ctx) { 229 return Create(Add, LHS, RHS, Ctx); 230 } 231 static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS, 232 MCContext &Ctx) { 233 return Create(And, LHS, RHS, Ctx); 234 } 235 static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS, 236 MCContext &Ctx) { 237 return Create(Div, LHS, RHS, Ctx); 238 } 239 static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS, 240 MCContext &Ctx) { 241 return Create(EQ, LHS, RHS, Ctx); 242 } 243 static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS, 244 MCContext &Ctx) { 245 return Create(GT, LHS, RHS, Ctx); 246 } 247 static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS, 248 MCContext &Ctx) { 249 return Create(GTE, LHS, RHS, Ctx); 250 } 251 static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS, 252 MCContext &Ctx) { 253 return Create(LAnd, LHS, RHS, Ctx); 254 } 255 static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS, 256 MCContext &Ctx) { 257 return Create(LOr, LHS, RHS, Ctx); 258 } 259 static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS, 260 MCContext &Ctx) { 261 return Create(LT, LHS, RHS, Ctx); 262 } 263 static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS, 264 MCContext &Ctx) { 265 return Create(LTE, LHS, RHS, Ctx); 266 } 267 static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS, 268 MCContext &Ctx) { 269 return Create(Mod, LHS, RHS, Ctx); 270 } 271 static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS, 272 MCContext &Ctx) { 273 return Create(Mul, LHS, RHS, Ctx); 274 } 275 static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS, 276 MCContext &Ctx) { 277 return Create(NE, LHS, RHS, Ctx); 278 } 279 static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS, 280 MCContext &Ctx) { 281 return Create(Or, LHS, RHS, Ctx); 282 } 283 static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS, 284 MCContext &Ctx) { 285 return Create(Shl, LHS, RHS, Ctx); 286 } 287 static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS, 288 MCContext &Ctx) { 289 return Create(Shr, LHS, RHS, Ctx); 290 } 291 static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS, 292 MCContext &Ctx) { 293 return Create(Sub, LHS, RHS, Ctx); 294 } 295 static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS, 296 MCContext &Ctx) { 297 return Create(Xor, LHS, RHS, Ctx); 298 } 299 300 /// @} 301 /// @name Accessors 302 /// @{ 303 304 /// getOpcode - Get the kind of this binary expression. 305 Opcode getOpcode() const { return Op; } 306 307 /// getLHS - Get the left-hand side expression of the binary operator. 308 const MCExpr *getLHS() const { return LHS; } 309 310 /// getRHS - Get the right-hand side expression of the binary operator. 311 const MCExpr *getRHS() const { return RHS; } 312 313 /// @} 314 315 static bool classof(const MCExpr *E) { 316 return E->getKind() == MCExpr::Binary; 317 } 318 static bool classof(const MCBinaryExpr *) { return true; } 319}; 320 321} // end namespace llvm 322 323#endif 324