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/Support/Casting.h"
14 #include "llvm/Support/DataTypes.h"
21 /// MCExpr - Base class for the full range of assembler expressions which are
22 /// needed for parsing.
26 Binary, ///< Binary expressions.
27 Constant, ///< Constant expressions.
28 SymbolRef, ///< References to labels and assigned expressions.
29 Unary ///< Unary expressions.
36 MCExpr(ExprKind _Kind) : Kind(_Kind) {}
41 ExprKind getKind() const { return Kind; }
43 /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
45 /// @param Res - The absolute value, if evaluation succeeds.
46 /// @result - True on success.
47 bool EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const;
49 /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
50 /// value, i.e. an expression of the fixed form (a - b + constant).
52 /// @param Res - The relocatable value, if evaluation succeeds.
53 /// @result - True on success.
54 bool EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const;
56 static bool classof(const MCExpr *) { return true; }
59 //// MCConstantExpr - Represent a constant integer expression.
60 class MCConstantExpr : public MCExpr {
64 MCConstantExpr(int64_t _Value)
65 : MCExpr(MCExpr::Constant), Value(_Value) {}
67 int64_t getValue() const { return Value; }
69 static bool classof(const MCExpr *E) {
70 return E->getKind() == MCExpr::Constant;
72 static bool classof(const MCConstantExpr *) { return true; }
75 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
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.
81 class MCSymbolRefExpr : public MCExpr {
85 MCSymbolRefExpr(MCSymbol *_Symbol)
86 : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol) {}
88 MCSymbol *getSymbol() const { return Symbol; }
90 static bool classof(const MCExpr *E) {
91 return E->getKind() == MCExpr::SymbolRef;
93 static bool classof(const MCSymbolRefExpr *) { return true; }
96 /// MCUnaryExpr - Unary assembler expressions.
97 class MCUnaryExpr : public MCExpr {
100 LNot, ///< Logical negation.
101 Minus, ///< Unary minus.
102 Not, ///< Bitwise negation.
103 Plus ///< Unary plus.
111 MCUnaryExpr(Opcode _Op, MCExpr *_Expr)
112 : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
117 Opcode getOpcode() const { return Op; }
119 MCExpr *getSubExpr() const { return Expr; }
121 static bool classof(const MCExpr *E) {
122 return E->getKind() == MCExpr::Unary;
124 static bool classof(const MCUnaryExpr *) { return true; }
127 /// MCBinaryExpr - Binary assembler expressions.
128 class MCBinaryExpr : public MCExpr {
132 And, ///< Bitwise and.
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.
142 Mul, ///< Multiplication.
143 NE, ///< Inequality comparison.
145 Shl, ///< Bitwise shift left.
146 Shr, ///< Bitwise shift right.
147 Sub, ///< Subtraction.
148 Xor ///< Bitwise exclusive or.
156 MCBinaryExpr(Opcode _Op, MCExpr *_LHS, MCExpr *_RHS)
157 : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
163 Opcode getOpcode() const { return Op; }
165 /// getLHS - Get the left-hand side expression of the binary operator.
166 MCExpr *getLHS() const { return LHS; }
168 /// getRHS - Get the right-hand side expression of the binary operator.
169 MCExpr *getRHS() const { return RHS; }
171 static bool classof(const MCExpr *E) {
172 return E->getKind() == MCExpr::Binary;
174 static bool classof(const MCBinaryExpr *) { return true; }
177 } // end namespace llvm