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.
33
Target ///< Target specific expression.
39
MCExpr(const MCExpr&); // DO NOT IMPLEMENT
40
void operator=(const MCExpr&); // DO NOT IMPLEMENT
43
explicit MCExpr(ExprKind _Kind) : Kind(_Kind) {}
49
ExprKind getKind() const { return Kind; }
52
/// @name Utility Methods
55
void print(raw_ostream &OS) const;
59
/// @name Expression Evaluation
62
/// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
64
/// @param Res - The absolute value, if evaluation succeeds.
65
/// @result - True on success.
66
bool EvaluateAsAbsolute(int64_t &Res) const;
68
/// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
69
/// value, i.e. an expression of the fixed form (a - b + constant).
71
/// @param Res - The relocatable value, if evaluation succeeds.
72
/// @result - True on success.
73
bool EvaluateAsRelocatable(MCValue &Res) const;
77
static bool classof(const MCExpr *) { return true; }
80
inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
85
//// MCConstantExpr - Represent a constant integer expression.
86
class MCConstantExpr : public MCExpr {
89
explicit MCConstantExpr(int64_t _Value)
90
: MCExpr(MCExpr::Constant), Value(_Value) {}
93
/// @name Construction
96
static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
102
int64_t getValue() const { return Value; }
106
static bool classof(const MCExpr *E) {
107
return E->getKind() == MCExpr::Constant;
109
static bool classof(const MCConstantExpr *) { return true; }
112
/// MCSymbolRefExpr - Represent a reference to a symbol from inside an
115
/// A symbol reference in an expression may be a use of a label, a use of an
116
/// assembler variable (defined constant), or constitute an implicit definition
117
/// of the symbol as external.
118
class MCSymbolRefExpr : public MCExpr {
119
const MCSymbol *Symbol;
121
explicit MCSymbolRefExpr(const MCSymbol *_Symbol)
122
: MCExpr(MCExpr::SymbolRef), Symbol(_Symbol) {}
125
/// @name Construction
128
static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx);
129
static const MCSymbolRefExpr *Create(StringRef Name, MCContext &Ctx);
135
const MCSymbol &getSymbol() const { return *Symbol; }
139
static bool classof(const MCExpr *E) {
140
return E->getKind() == MCExpr::SymbolRef;
142
static bool classof(const MCSymbolRefExpr *) { return true; }
145
/// MCUnaryExpr - Unary assembler expressions.
146
class MCUnaryExpr : public MCExpr {
149
LNot, ///< Logical negation.
150
Minus, ///< Unary minus.
151
Not, ///< Bitwise negation.
152
Plus ///< Unary plus.
159
MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
160
: MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
163
/// @name Construction
166
static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
168
static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
169
return Create(LNot, Expr, Ctx);
171
static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
172
return Create(Minus, Expr, Ctx);
174
static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
175
return Create(Not, Expr, Ctx);
177
static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
178
return Create(Plus, Expr, Ctx);
185
/// getOpcode - Get the kind of this unary expression.
186
Opcode getOpcode() const { return Op; }
188
/// getSubExpr - Get the child of this unary expression.
189
const MCExpr *getSubExpr() const { return Expr; }
193
static bool classof(const MCExpr *E) {
194
return E->getKind() == MCExpr::Unary;
196
static bool classof(const MCUnaryExpr *) { return true; }
199
/// MCBinaryExpr - Binary assembler expressions.
200
class MCBinaryExpr : public MCExpr {
204
And, ///< Bitwise and.
205
Div, ///< Signed division.
206
EQ, ///< Equality comparison.
207
GT, ///< Signed greater than comparison (result is either 0 or some
208
///< target-specific non-zero value)
209
GTE, ///< Signed greater than or equal comparison (result is either 0 or
210
///< some target-specific non-zero value).
211
LAnd, ///< Logical and.
212
LOr, ///< Logical or.
213
LT, ///< Signed less than comparison (result is either 0 or
214
///< some target-specific non-zero value).
215
LTE, ///< Signed less than or equal comparison (result is either 0 or
216
///< some target-specific non-zero value).
217
Mod, ///< Signed remainder.
218
Mul, ///< Multiplication.
219
NE, ///< Inequality comparison.
221
Shl, ///< Shift left.
222
Shr, ///< Shift right (arithmetic or logical, depending on target)
223
Sub, ///< Subtraction.
224
Xor ///< Bitwise exclusive or.
229
const MCExpr *LHS, *RHS;
231
MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
232
: MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
235
/// @name Construction
238
static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
239
const MCExpr *RHS, MCContext &Ctx);
240
static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
242
return Create(Add, LHS, RHS, Ctx);
244
static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
246
return Create(And, LHS, RHS, Ctx);
248
static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
250
return Create(Div, LHS, RHS, Ctx);
252
static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
254
return Create(EQ, LHS, RHS, Ctx);
256
static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
258
return Create(GT, LHS, RHS, Ctx);
260
static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
262
return Create(GTE, LHS, RHS, Ctx);
264
static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
266
return Create(LAnd, LHS, RHS, Ctx);
268
static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
270
return Create(LOr, LHS, RHS, Ctx);
272
static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
274
return Create(LT, LHS, RHS, Ctx);
276
static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
278
return Create(LTE, LHS, RHS, Ctx);
280
static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
282
return Create(Mod, LHS, RHS, Ctx);
284
static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
286
return Create(Mul, LHS, RHS, Ctx);
288
static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
290
return Create(NE, LHS, RHS, Ctx);
292
static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
294
return Create(Or, LHS, RHS, Ctx);
296
static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
298
return Create(Shl, LHS, RHS, Ctx);
300
static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
302
return Create(Shr, LHS, RHS, Ctx);
304
static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
306
return Create(Sub, LHS, RHS, Ctx);
308
static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
310
return Create(Xor, LHS, RHS, Ctx);
317
/// getOpcode - Get the kind of this binary expression.
318
Opcode getOpcode() const { return Op; }
320
/// getLHS - Get the left-hand side expression of the binary operator.
321
const MCExpr *getLHS() const { return LHS; }
323
/// getRHS - Get the right-hand side expression of the binary operator.
324
const MCExpr *getRHS() const { return RHS; }
328
static bool classof(const MCExpr *E) {
329
return E->getKind() == MCExpr::Binary;
331
static bool classof(const MCBinaryExpr *) { return true; }
334
/// MCTargetExpr - This is an extension point for target-specific MCExpr
335
/// subclasses to implement.
337
/// NOTE: All subclasses are required to have trivial destructors because
338
/// MCExprs are bump pointer allocated and not destructed.
339
class MCTargetExpr : public MCExpr {
340
virtual void Anchor();
342
MCTargetExpr() : MCExpr(Target) {}
343
virtual ~MCTargetExpr() {}
346
virtual void PrintImpl(raw_ostream &OS) const = 0;
347
virtual bool EvaluateAsRelocatableImpl(MCValue &Res) const = 0;
350
static bool classof(const MCExpr *E) {
351
return E->getKind() == MCExpr::Target;
353
static bool classof(const MCTargetExpr *) { return true; }
356
} // end namespace llvm