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/System/DataTypes.h"
24 /// MCExpr - Base class for the full range of assembler expressions which are
25 /// needed for parsing.
29 Binary, ///< Binary expressions.
30 Constant, ///< Constant expressions.
31 SymbolRef, ///< References to labels and assigned expressions.
32 Unary ///< Unary expressions.
38 MCExpr(const MCExpr&); // DO NOT IMPLEMENT
39 void operator=(const MCExpr&); // DO NOT IMPLEMENT
42 MCExpr(ExprKind _Kind) : Kind(_Kind) {}
48 ExprKind getKind() const { return Kind; }
51 /// @name Utility Methods
54 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
58 /// @name Expression Evaluation
61 /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
63 /// @param Res - The absolute value, if evaluation succeeds.
64 /// @result - True on success.
65 bool EvaluateAsAbsolute(int64_t &Res) const;
67 /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
68 /// value, i.e. an expression of the fixed form (a - b + constant).
70 /// @param Res - The relocatable value, if evaluation succeeds.
71 /// @result - True on success.
72 bool EvaluateAsRelocatable(MCValue &Res) const;
76 static bool classof(const MCExpr *) { return true; }
79 //// MCConstantExpr - Represent a constant integer expression.
80 class MCConstantExpr : public MCExpr {
83 MCConstantExpr(int64_t _Value)
84 : MCExpr(MCExpr::Constant), Value(_Value) {}
87 /// @name Construction
90 static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
96 int64_t getValue() const { return Value; }
100 static bool classof(const MCExpr *E) {
101 return E->getKind() == MCExpr::Constant;
103 static bool classof(const MCConstantExpr *) { return true; }
106 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
109 /// A symbol reference in an expression may be a use of a label, a use of an
110 /// assembler variable (defined constant), or constitute an implicit definition
111 /// of the symbol as external.
112 class MCSymbolRefExpr : public MCExpr {
113 const MCSymbol *Symbol;
115 MCSymbolRefExpr(const MCSymbol *_Symbol)
116 : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol) {}
119 /// @name Construction
122 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx);
123 static const MCSymbolRefExpr *Create(StringRef Name, MCContext &Ctx);
129 const MCSymbol &getSymbol() const { return *Symbol; }
133 static bool classof(const MCExpr *E) {
134 return E->getKind() == MCExpr::SymbolRef;
136 static bool classof(const MCSymbolRefExpr *) { return true; }
139 /// MCUnaryExpr - Unary assembler expressions.
140 class MCUnaryExpr : public MCExpr {
143 LNot, ///< Logical negation.
144 Minus, ///< Unary minus.
145 Not, ///< Bitwise negation.
146 Plus ///< Unary plus.
153 MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
154 : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
157 /// @name Construction
160 static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
162 static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
163 return Create(LNot, Expr, Ctx);
165 static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
166 return Create(Minus, Expr, Ctx);
168 static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
169 return Create(Not, Expr, Ctx);
171 static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
172 return Create(Plus, Expr, Ctx);
179 /// getOpcode - Get the kind of this unary expression.
180 Opcode getOpcode() const { return Op; }
182 /// getSubExpr - Get the child of this unary expression.
183 const MCExpr *getSubExpr() const { return Expr; }
187 static bool classof(const MCExpr *E) {
188 return E->getKind() == MCExpr::Unary;
190 static bool classof(const MCUnaryExpr *) { return true; }
193 /// MCBinaryExpr - Binary assembler expressions.
194 class MCBinaryExpr : public MCExpr {
198 And, ///< Bitwise and.
200 EQ, ///< Equality comparison.
201 GT, ///< Greater than comparison.
202 GTE, ///< Greater than or equal comparison.
203 LAnd, ///< Logical and.
204 LOr, ///< Logical or.
205 LT, ///< Less than comparison.
206 LTE, ///< Less than or equal comparison.
208 Mul, ///< Multiplication.
209 NE, ///< Inequality comparison.
211 Shl, ///< Bitwise shift left.
212 Shr, ///< Bitwise shift right.
213 Sub, ///< Subtraction.
214 Xor ///< Bitwise exclusive or.
219 const MCExpr *LHS, *RHS;
221 MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
222 : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
225 /// @name Construction
228 static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
229 const MCExpr *RHS, MCContext &Ctx);
230 static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
232 return Create(Add, LHS, RHS, Ctx);
234 static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
236 return Create(And, LHS, RHS, Ctx);
238 static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
240 return Create(Div, LHS, RHS, Ctx);
242 static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
244 return Create(EQ, LHS, RHS, Ctx);
246 static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
248 return Create(GT, LHS, RHS, Ctx);
250 static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
252 return Create(GTE, LHS, RHS, Ctx);
254 static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
256 return Create(LAnd, LHS, RHS, Ctx);
258 static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
260 return Create(LOr, LHS, RHS, Ctx);
262 static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
264 return Create(LT, LHS, RHS, Ctx);
266 static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
268 return Create(LTE, LHS, RHS, Ctx);
270 static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
272 return Create(Mod, LHS, RHS, Ctx);
274 static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
276 return Create(Mul, LHS, RHS, Ctx);
278 static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
280 return Create(NE, LHS, RHS, Ctx);
282 static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
284 return Create(Or, LHS, RHS, Ctx);
286 static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
288 return Create(Shl, LHS, RHS, Ctx);
290 static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
292 return Create(Shr, LHS, RHS, Ctx);
294 static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
296 return Create(Sub, LHS, RHS, Ctx);
298 static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
300 return Create(Xor, LHS, RHS, Ctx);
307 /// getOpcode - Get the kind of this binary expression.
308 Opcode getOpcode() const { return Op; }
310 /// getLHS - Get the left-hand side expression of the binary operator.
311 const MCExpr *getLHS() const { return LHS; }
313 /// getRHS - Get the right-hand side expression of the binary operator.
314 const MCExpr *getRHS() const { return RHS; }
318 static bool classof(const MCExpr *E) {
319 return E->getKind() == MCExpr::Binary;
321 static bool classof(const MCBinaryExpr *) { return true; }
324 } // end namespace llvm