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;
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 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
84 //// MCConstantExpr - Represent a constant integer expression.
85 class MCConstantExpr : public MCExpr {
88 MCConstantExpr(int64_t _Value)
89 : MCExpr(MCExpr::Constant), Value(_Value) {}
92 /// @name Construction
95 static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
101 int64_t getValue() const { return Value; }
105 static bool classof(const MCExpr *E) {
106 return E->getKind() == MCExpr::Constant;
108 static bool classof(const MCConstantExpr *) { return true; }
111 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
114 /// A symbol reference in an expression may be a use of a label, a use of an
115 /// assembler variable (defined constant), or constitute an implicit definition
116 /// of the symbol as external.
117 class MCSymbolRefExpr : public MCExpr {
118 const MCSymbol *Symbol;
120 MCSymbolRefExpr(const MCSymbol *_Symbol)
121 : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol) {}
124 /// @name Construction
127 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx);
128 static const MCSymbolRefExpr *Create(StringRef Name, MCContext &Ctx);
134 const MCSymbol &getSymbol() const { return *Symbol; }
138 static bool classof(const MCExpr *E) {
139 return E->getKind() == MCExpr::SymbolRef;
141 static bool classof(const MCSymbolRefExpr *) { return true; }
144 /// MCUnaryExpr - Unary assembler expressions.
145 class MCUnaryExpr : public MCExpr {
148 LNot, ///< Logical negation.
149 Minus, ///< Unary minus.
150 Not, ///< Bitwise negation.
151 Plus ///< Unary plus.
158 MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
159 : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
162 /// @name Construction
165 static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
167 static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
168 return Create(LNot, Expr, Ctx);
170 static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
171 return Create(Minus, Expr, Ctx);
173 static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
174 return Create(Not, Expr, Ctx);
176 static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
177 return Create(Plus, Expr, Ctx);
184 /// getOpcode - Get the kind of this unary expression.
185 Opcode getOpcode() const { return Op; }
187 /// getSubExpr - Get the child of this unary expression.
188 const MCExpr *getSubExpr() const { return Expr; }
192 static bool classof(const MCExpr *E) {
193 return E->getKind() == MCExpr::Unary;
195 static bool classof(const MCUnaryExpr *) { return true; }
198 /// MCBinaryExpr - Binary assembler expressions.
199 class MCBinaryExpr : public MCExpr {
203 And, ///< Bitwise and.
205 EQ, ///< Equality comparison.
206 GT, ///< Greater than comparison.
207 GTE, ///< Greater than or equal comparison.
208 LAnd, ///< Logical and.
209 LOr, ///< Logical or.
210 LT, ///< Less than comparison.
211 LTE, ///< Less than or equal comparison.
213 Mul, ///< Multiplication.
214 NE, ///< Inequality comparison.
216 Shl, ///< Bitwise shift left.
217 Shr, ///< Bitwise shift right.
218 Sub, ///< Subtraction.
219 Xor ///< Bitwise exclusive or.
224 const MCExpr *LHS, *RHS;
226 MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
227 : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
230 /// @name Construction
233 static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
234 const MCExpr *RHS, MCContext &Ctx);
235 static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
237 return Create(Add, LHS, RHS, Ctx);
239 static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
241 return Create(And, LHS, RHS, Ctx);
243 static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
245 return Create(Div, LHS, RHS, Ctx);
247 static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
249 return Create(EQ, LHS, RHS, Ctx);
251 static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
253 return Create(GT, LHS, RHS, Ctx);
255 static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
257 return Create(GTE, LHS, RHS, Ctx);
259 static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
261 return Create(LAnd, LHS, RHS, Ctx);
263 static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
265 return Create(LOr, LHS, RHS, Ctx);
267 static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
269 return Create(LT, LHS, RHS, Ctx);
271 static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
273 return Create(LTE, LHS, RHS, Ctx);
275 static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
277 return Create(Mod, LHS, RHS, Ctx);
279 static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
281 return Create(Mul, LHS, RHS, Ctx);
283 static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
285 return Create(NE, LHS, RHS, Ctx);
287 static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
289 return Create(Or, LHS, RHS, Ctx);
291 static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
293 return Create(Shl, LHS, RHS, Ctx);
295 static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
297 return Create(Shr, LHS, RHS, Ctx);
299 static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
301 return Create(Sub, LHS, RHS, Ctx);
303 static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
305 return Create(Xor, LHS, RHS, Ctx);
312 /// getOpcode - Get the kind of this binary expression.
313 Opcode getOpcode() const { return Op; }
315 /// getLHS - Get the left-hand side expression of the binary operator.
316 const MCExpr *getLHS() const { return LHS; }
318 /// getRHS - Get the right-hand side expression of the binary operator.
319 const MCExpr *getRHS() const { return RHS; }
323 static bool classof(const MCExpr *E) {
324 return E->getKind() == MCExpr::Binary;
326 static bool classof(const MCBinaryExpr *) { return true; }
329 } // end namespace llvm