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