MC: Remove unneeded context argument to MCExpr::Evaluate*.
[oota-llvm.git] / include / llvm / MC / MCExpr.h
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/Support/DataTypes.h"
15
16 namespace llvm {
17 class MCAsmInfo;
18 class MCContext;
19 class MCSymbol;
20 class MCValue;
21 class raw_ostream;
22 class StringRef;
23
24 /// MCExpr - Base class for the full range of assembler expressions which are
25 /// needed for parsing.
26 class MCExpr {
27 public:
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
35 private:
36   ExprKind Kind;
37
38   MCExpr(const MCExpr&); // DO NOT IMPLEMENT
39   void operator=(const MCExpr&); // DO NOT IMPLEMENT
40
41 protected:
42   MCExpr(ExprKind _Kind) : Kind(_Kind) {}
43
44 public:
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.
80 class MCConstantExpr : public MCExpr {
81   int64_t Value;
82
83   MCConstantExpr(int64_t _Value)
84     : MCExpr(MCExpr::Constant), Value(_Value) {}
85
86 public:
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.
112 class MCSymbolRefExpr : public MCExpr {
113   const MCSymbol *Symbol;
114
115   MCSymbolRefExpr(const MCSymbol *_Symbol)
116     : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol) {}
117
118 public:
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   
127   /// @}
128   /// @name Accessors
129   /// @{
130
131   const MCSymbol &getSymbol() const { return *Symbol; }
132
133   /// @}
134
135   static bool classof(const MCExpr *E) {
136     return E->getKind() == MCExpr::SymbolRef;
137   }
138   static bool classof(const MCSymbolRefExpr *) { return true; }
139 };
140
141 /// MCUnaryExpr - Unary assembler expressions.
142 class MCUnaryExpr : public MCExpr {
143 public:
144   enum Opcode {
145     LNot,  ///< Logical negation.
146     Minus, ///< Unary minus.
147     Not,   ///< Bitwise negation.
148     Plus   ///< Unary plus.
149   };
150
151 private:
152   Opcode Op;
153   const MCExpr *Expr;
154
155   MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
156     : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
157
158 public:
159   /// @name Construction
160   /// @{
161
162   static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
163                                    MCContext &Ctx);
164   static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
165     return Create(LNot, Expr, Ctx);
166   }
167   static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
168     return Create(Minus, Expr, Ctx);
169   }
170   static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
171     return Create(Not, Expr, Ctx);
172   }
173   static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
174     return Create(Plus, Expr, Ctx);
175   }
176
177   /// @}
178   /// @name Accessors
179   /// @{
180
181   /// getOpcode - Get the kind of this unary expression.
182   Opcode getOpcode() const { return Op; }
183
184   /// getSubExpr - Get the child of this unary expression.
185   const MCExpr *getSubExpr() const { return Expr; }
186
187   /// @}
188
189   static bool classof(const MCExpr *E) {
190     return E->getKind() == MCExpr::Unary;
191   }
192   static bool classof(const MCUnaryExpr *) { return true; }
193 };
194
195 /// MCBinaryExpr - Binary assembler expressions.
196 class MCBinaryExpr : public MCExpr {
197 public:
198   enum Opcode {
199     Add,  ///< Addition.
200     And,  ///< Bitwise and.
201     Div,  ///< Division.
202     EQ,   ///< Equality comparison.
203     GT,   ///< Greater than comparison.
204     GTE,  ///< Greater than or equal comparison.
205     LAnd, ///< Logical and.
206     LOr,  ///< Logical or.
207     LT,   ///< Less than comparison.
208     LTE,  ///< Less than or equal comparison.
209     Mod,  ///< Modulus.
210     Mul,  ///< Multiplication.
211     NE,   ///< Inequality comparison.
212     Or,   ///< Bitwise or.
213     Shl,  ///< Bitwise shift left.
214     Shr,  ///< Bitwise shift right.
215     Sub,  ///< Subtraction.
216     Xor   ///< Bitwise exclusive or.
217   };
218
219 private:
220   Opcode Op;
221   const MCExpr *LHS, *RHS;
222
223   MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
224     : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
225
226 public:
227   /// @name Construction
228   /// @{
229
230   static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
231                                     const MCExpr *RHS, MCContext &Ctx);
232   static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
233                                        MCContext &Ctx) {
234     return Create(Add, LHS, RHS, Ctx);
235   }
236   static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
237                                        MCContext &Ctx) {
238     return Create(And, LHS, RHS, Ctx);
239   }
240   static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
241                                        MCContext &Ctx) {
242     return Create(Div, LHS, RHS, Ctx);
243   }
244   static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
245                                       MCContext &Ctx) {
246     return Create(EQ, LHS, RHS, Ctx);
247   }
248   static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
249                                       MCContext &Ctx) {
250     return Create(GT, LHS, RHS, Ctx);
251   }
252   static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
253                                        MCContext &Ctx) {
254     return Create(GTE, LHS, RHS, Ctx);
255   }
256   static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
257                                         MCContext &Ctx) {
258     return Create(LAnd, LHS, RHS, Ctx);
259   }
260   static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
261                                        MCContext &Ctx) {
262     return Create(LOr, LHS, RHS, Ctx);
263   }
264   static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
265                                       MCContext &Ctx) {
266     return Create(LT, LHS, RHS, Ctx);
267   }
268   static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
269                                        MCContext &Ctx) {
270     return Create(LTE, LHS, RHS, Ctx);
271   }
272   static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
273                                        MCContext &Ctx) {
274     return Create(Mod, LHS, RHS, Ctx);
275   }
276   static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
277                                        MCContext &Ctx) {
278     return Create(Mul, LHS, RHS, Ctx);
279   }
280   static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
281                                       MCContext &Ctx) {
282     return Create(NE, LHS, RHS, Ctx);
283   }
284   static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
285                                       MCContext &Ctx) {
286     return Create(Or, LHS, RHS, Ctx);
287   }
288   static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
289                                        MCContext &Ctx) {
290     return Create(Shl, LHS, RHS, Ctx);
291   }
292   static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
293                                        MCContext &Ctx) {
294     return Create(Shr, LHS, RHS, Ctx);
295   }
296   static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
297                                        MCContext &Ctx) {
298     return Create(Sub, LHS, RHS, Ctx);
299   }
300   static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
301                                        MCContext &Ctx) {
302     return Create(Xor, LHS, RHS, Ctx);
303   }
304
305   /// @}
306   /// @name Accessors
307   /// @{
308
309   /// getOpcode - Get the kind of this binary expression.
310   Opcode getOpcode() const { return Op; }
311
312   /// getLHS - Get the left-hand side expression of the binary operator.
313   const MCExpr *getLHS() const { return LHS; }
314
315   /// getRHS - Get the right-hand side expression of the binary operator.
316   const MCExpr *getRHS() const { return RHS; }
317
318   /// @}
319
320   static bool classof(const MCExpr *E) {
321     return E->getKind() == MCExpr::Binary;
322   }
323   static bool classof(const MCBinaryExpr *) { return true; }
324 };
325
326 } // end namespace llvm
327
328 #endif