MCExpr.h revision 8cb9a3b13f3226b7e741768b69d26ecd6b5231f1
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;
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
79inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
80  E.print(OS);
81  return OS;
82}
83
84//// MCConstantExpr - Represent a constant integer expression.
85class MCConstantExpr : public MCExpr {
86  int64_t Value;
87
88  MCConstantExpr(int64_t _Value)
89    : MCExpr(MCExpr::Constant), Value(_Value) {}
90
91public:
92  /// @name Construction
93  /// @{
94
95  static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
96
97  /// @}
98  /// @name Accessors
99  /// @{
100
101  int64_t getValue() const { return Value; }
102
103  /// @}
104
105  static bool classof(const MCExpr *E) {
106    return E->getKind() == MCExpr::Constant;
107  }
108  static bool classof(const MCConstantExpr *) { return true; }
109};
110
111/// MCSymbolRefExpr - Represent a reference to a symbol from inside an
112/// expression.
113///
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.
117class MCSymbolRefExpr : public MCExpr {
118  const MCSymbol *Symbol;
119
120  MCSymbolRefExpr(const MCSymbol *_Symbol)
121    : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol) {}
122
123public:
124  /// @name Construction
125  /// @{
126
127  static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx);
128  static const MCSymbolRefExpr *Create(StringRef Name, MCContext &Ctx);
129
130  /// @}
131  /// @name Accessors
132  /// @{
133
134  const MCSymbol &getSymbol() const { return *Symbol; }
135
136  /// @}
137
138  static bool classof(const MCExpr *E) {
139    return E->getKind() == MCExpr::SymbolRef;
140  }
141  static bool classof(const MCSymbolRefExpr *) { return true; }
142};
143
144/// MCUnaryExpr - Unary assembler expressions.
145class MCUnaryExpr : public MCExpr {
146public:
147  enum Opcode {
148    LNot,  ///< Logical negation.
149    Minus, ///< Unary minus.
150    Not,   ///< Bitwise negation.
151    Plus   ///< Unary plus.
152  };
153
154private:
155  Opcode Op;
156  const MCExpr *Expr;
157
158  MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
159    : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
160
161public:
162  /// @name Construction
163  /// @{
164
165  static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
166                                   MCContext &Ctx);
167  static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
168    return Create(LNot, Expr, Ctx);
169  }
170  static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
171    return Create(Minus, Expr, Ctx);
172  }
173  static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
174    return Create(Not, Expr, Ctx);
175  }
176  static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
177    return Create(Plus, Expr, Ctx);
178  }
179
180  /// @}
181  /// @name Accessors
182  /// @{
183
184  /// getOpcode - Get the kind of this unary expression.
185  Opcode getOpcode() const { return Op; }
186
187  /// getSubExpr - Get the child of this unary expression.
188  const MCExpr *getSubExpr() const { return Expr; }
189
190  /// @}
191
192  static bool classof(const MCExpr *E) {
193    return E->getKind() == MCExpr::Unary;
194  }
195  static bool classof(const MCUnaryExpr *) { return true; }
196};
197
198/// MCBinaryExpr - Binary assembler expressions.
199class MCBinaryExpr : public MCExpr {
200public:
201  enum Opcode {
202    Add,  ///< Addition.
203    And,  ///< Bitwise and.
204    Div,  ///< Division.
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.
212    Mod,  ///< Modulus.
213    Mul,  ///< Multiplication.
214    NE,   ///< Inequality comparison.
215    Or,   ///< Bitwise or.
216    Shl,  ///< Bitwise shift left.
217    Shr,  ///< Bitwise shift right.
218    Sub,  ///< Subtraction.
219    Xor   ///< Bitwise exclusive or.
220  };
221
222private:
223  Opcode Op;
224  const MCExpr *LHS, *RHS;
225
226  MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
227    : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
228
229public:
230  /// @name Construction
231  /// @{
232
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,
236                                       MCContext &Ctx) {
237    return Create(Add, LHS, RHS, Ctx);
238  }
239  static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
240                                       MCContext &Ctx) {
241    return Create(And, LHS, RHS, Ctx);
242  }
243  static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
244                                       MCContext &Ctx) {
245    return Create(Div, LHS, RHS, Ctx);
246  }
247  static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
248                                      MCContext &Ctx) {
249    return Create(EQ, LHS, RHS, Ctx);
250  }
251  static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
252                                      MCContext &Ctx) {
253    return Create(GT, LHS, RHS, Ctx);
254  }
255  static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
256                                       MCContext &Ctx) {
257    return Create(GTE, LHS, RHS, Ctx);
258  }
259  static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
260                                        MCContext &Ctx) {
261    return Create(LAnd, LHS, RHS, Ctx);
262  }
263  static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
264                                       MCContext &Ctx) {
265    return Create(LOr, LHS, RHS, Ctx);
266  }
267  static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
268                                      MCContext &Ctx) {
269    return Create(LT, LHS, RHS, Ctx);
270  }
271  static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
272                                       MCContext &Ctx) {
273    return Create(LTE, LHS, RHS, Ctx);
274  }
275  static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
276                                       MCContext &Ctx) {
277    return Create(Mod, LHS, RHS, Ctx);
278  }
279  static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
280                                       MCContext &Ctx) {
281    return Create(Mul, LHS, RHS, Ctx);
282  }
283  static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
284                                      MCContext &Ctx) {
285    return Create(NE, LHS, RHS, Ctx);
286  }
287  static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
288                                      MCContext &Ctx) {
289    return Create(Or, LHS, RHS, Ctx);
290  }
291  static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
292                                       MCContext &Ctx) {
293    return Create(Shl, LHS, RHS, Ctx);
294  }
295  static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
296                                       MCContext &Ctx) {
297    return Create(Shr, LHS, RHS, Ctx);
298  }
299  static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
300                                       MCContext &Ctx) {
301    return Create(Sub, LHS, RHS, Ctx);
302  }
303  static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
304                                       MCContext &Ctx) {
305    return Create(Xor, LHS, RHS, Ctx);
306  }
307
308  /// @}
309  /// @name Accessors
310  /// @{
311
312  /// getOpcode - Get the kind of this binary expression.
313  Opcode getOpcode() const { return Op; }
314
315  /// getLHS - Get the left-hand side expression of the binary operator.
316  const MCExpr *getLHS() const { return LHS; }
317
318  /// getRHS - Get the right-hand side expression of the binary operator.
319  const MCExpr *getRHS() const { return RHS; }
320
321  /// @}
322
323  static bool classof(const MCExpr *E) {
324    return E->getKind() == MCExpr::Binary;
325  }
326  static bool classof(const MCBinaryExpr *) { return true; }
327};
328
329} // end namespace llvm
330
331#endif
332