set the temporary bit on MCSymbols correctly.
[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/System/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     Target     ///< Target specific expression.
34   };
35
36 private:
37   ExprKind Kind;
38
39   MCExpr(const MCExpr&); // DO NOT IMPLEMENT
40   void operator=(const MCExpr&); // DO NOT IMPLEMENT
41
42 protected:
43   explicit MCExpr(ExprKind _Kind) : Kind(_Kind) {}
44
45 public:
46   /// @name Accessors
47   /// @{
48
49   ExprKind getKind() const { return Kind; }
50
51   /// @}
52   /// @name Utility Methods
53   /// @{
54
55   void print(raw_ostream &OS) const;
56   void dump() const;
57
58   /// @}
59   /// @name Expression Evaluation
60   /// @{
61
62   /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
63   ///
64   /// @param Res - The absolute value, if evaluation succeeds.
65   /// @result - True on success.
66   bool EvaluateAsAbsolute(int64_t &Res) const;
67
68   /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
69   /// value, i.e. an expression of the fixed form (a - b + constant).
70   ///
71   /// @param Res - The relocatable value, if evaluation succeeds.
72   /// @result - True on success.
73   bool EvaluateAsRelocatable(MCValue &Res) const;
74
75   /// @}
76
77   static bool classof(const MCExpr *) { return true; }
78 };
79   
80 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
81   E.print(OS);
82   return OS;
83 }
84
85 //// MCConstantExpr - Represent a constant integer expression.
86 class MCConstantExpr : public MCExpr {
87   int64_t Value;
88
89   explicit MCConstantExpr(int64_t _Value)
90     : MCExpr(MCExpr::Constant), Value(_Value) {}
91
92 public:
93   /// @name Construction
94   /// @{
95
96   static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
97
98   /// @}
99   /// @name Accessors
100   /// @{
101
102   int64_t getValue() const { return Value; }
103
104   /// @}
105
106   static bool classof(const MCExpr *E) {
107     return E->getKind() == MCExpr::Constant;
108   }
109   static bool classof(const MCConstantExpr *) { return true; }
110 };
111
112 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
113 /// expression.
114 ///
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;
120
121   explicit MCSymbolRefExpr(const MCSymbol *_Symbol)
122     : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol) {}
123
124 public:
125   /// @name Construction
126   /// @{
127
128   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx);
129   static const MCSymbolRefExpr *Create(StringRef Name, MCContext &Ctx);
130   
131   /// CreateTemp - Create a reference to an assembler temporary label with the
132   /// specified name.
133   static const MCSymbolRefExpr *CreateTemp(StringRef Name, MCContext &Ctx);
134
135   /// @}
136   /// @name Accessors
137   /// @{
138
139   const MCSymbol &getSymbol() const { return *Symbol; }
140
141   /// @}
142
143   static bool classof(const MCExpr *E) {
144     return E->getKind() == MCExpr::SymbolRef;
145   }
146   static bool classof(const MCSymbolRefExpr *) { return true; }
147 };
148
149 /// MCUnaryExpr - Unary assembler expressions.
150 class MCUnaryExpr : public MCExpr {
151 public:
152   enum Opcode {
153     LNot,  ///< Logical negation.
154     Minus, ///< Unary minus.
155     Not,   ///< Bitwise negation.
156     Plus   ///< Unary plus.
157   };
158
159 private:
160   Opcode Op;
161   const MCExpr *Expr;
162
163   MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
164     : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
165
166 public:
167   /// @name Construction
168   /// @{
169
170   static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
171                                    MCContext &Ctx);
172   static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
173     return Create(LNot, Expr, Ctx);
174   }
175   static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
176     return Create(Minus, Expr, Ctx);
177   }
178   static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
179     return Create(Not, Expr, Ctx);
180   }
181   static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
182     return Create(Plus, Expr, Ctx);
183   }
184
185   /// @}
186   /// @name Accessors
187   /// @{
188
189   /// getOpcode - Get the kind of this unary expression.
190   Opcode getOpcode() const { return Op; }
191
192   /// getSubExpr - Get the child of this unary expression.
193   const MCExpr *getSubExpr() const { return Expr; }
194
195   /// @}
196
197   static bool classof(const MCExpr *E) {
198     return E->getKind() == MCExpr::Unary;
199   }
200   static bool classof(const MCUnaryExpr *) { return true; }
201 };
202
203 /// MCBinaryExpr - Binary assembler expressions.
204 class MCBinaryExpr : public MCExpr {
205 public:
206   enum Opcode {
207     Add,  ///< Addition.
208     And,  ///< Bitwise and.
209     Div,  ///< Signed division.
210     EQ,   ///< Equality comparison.
211     GT,   ///< Signed greater than comparison (result is either 0 or some
212           ///< target-specific non-zero value)
213     GTE,  ///< Signed greater than or equal comparison (result is either 0 or
214           ///< some target-specific non-zero value).
215     LAnd, ///< Logical and.
216     LOr,  ///< Logical or.
217     LT,   ///< Signed less than comparison (result is either 0 or
218           ///< some target-specific non-zero value).
219     LTE,  ///< Signed less than or equal comparison (result is either 0 or
220           ///< some target-specific non-zero value).
221     Mod,  ///< Signed remainder.
222     Mul,  ///< Multiplication.
223     NE,   ///< Inequality comparison.
224     Or,   ///< Bitwise or.
225     Shl,  ///< Shift left.
226     Shr,  ///< Shift right (arithmetic or logical, depending on target)
227     Sub,  ///< Subtraction.
228     Xor   ///< Bitwise exclusive or.
229   };
230
231 private:
232   Opcode Op;
233   const MCExpr *LHS, *RHS;
234
235   MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
236     : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
237
238 public:
239   /// @name Construction
240   /// @{
241
242   static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
243                                     const MCExpr *RHS, MCContext &Ctx);
244   static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
245                                        MCContext &Ctx) {
246     return Create(Add, LHS, RHS, Ctx);
247   }
248   static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
249                                        MCContext &Ctx) {
250     return Create(And, LHS, RHS, Ctx);
251   }
252   static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
253                                        MCContext &Ctx) {
254     return Create(Div, LHS, RHS, Ctx);
255   }
256   static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
257                                       MCContext &Ctx) {
258     return Create(EQ, LHS, RHS, Ctx);
259   }
260   static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
261                                       MCContext &Ctx) {
262     return Create(GT, LHS, RHS, Ctx);
263   }
264   static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
265                                        MCContext &Ctx) {
266     return Create(GTE, LHS, RHS, Ctx);
267   }
268   static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
269                                         MCContext &Ctx) {
270     return Create(LAnd, LHS, RHS, Ctx);
271   }
272   static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
273                                        MCContext &Ctx) {
274     return Create(LOr, LHS, RHS, Ctx);
275   }
276   static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
277                                       MCContext &Ctx) {
278     return Create(LT, LHS, RHS, Ctx);
279   }
280   static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
281                                        MCContext &Ctx) {
282     return Create(LTE, LHS, RHS, Ctx);
283   }
284   static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
285                                        MCContext &Ctx) {
286     return Create(Mod, LHS, RHS, Ctx);
287   }
288   static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
289                                        MCContext &Ctx) {
290     return Create(Mul, LHS, RHS, Ctx);
291   }
292   static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
293                                       MCContext &Ctx) {
294     return Create(NE, LHS, RHS, Ctx);
295   }
296   static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
297                                       MCContext &Ctx) {
298     return Create(Or, LHS, RHS, Ctx);
299   }
300   static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
301                                        MCContext &Ctx) {
302     return Create(Shl, LHS, RHS, Ctx);
303   }
304   static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
305                                        MCContext &Ctx) {
306     return Create(Shr, LHS, RHS, Ctx);
307   }
308   static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
309                                        MCContext &Ctx) {
310     return Create(Sub, LHS, RHS, Ctx);
311   }
312   static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
313                                        MCContext &Ctx) {
314     return Create(Xor, LHS, RHS, Ctx);
315   }
316
317   /// @}
318   /// @name Accessors
319   /// @{
320
321   /// getOpcode - Get the kind of this binary expression.
322   Opcode getOpcode() const { return Op; }
323
324   /// getLHS - Get the left-hand side expression of the binary operator.
325   const MCExpr *getLHS() const { return LHS; }
326
327   /// getRHS - Get the right-hand side expression of the binary operator.
328   const MCExpr *getRHS() const { return RHS; }
329
330   /// @}
331
332   static bool classof(const MCExpr *E) {
333     return E->getKind() == MCExpr::Binary;
334   }
335   static bool classof(const MCBinaryExpr *) { return true; }
336 };
337
338 /// MCTargetExpr - This is an extension point for target-specific MCExpr
339 /// subclasses to implement.
340 ///
341 /// NOTE: All subclasses are required to have trivial destructors because
342 /// MCExprs are bump pointer allocated and not destructed.
343 class MCTargetExpr : public MCExpr {
344   virtual void Anchor();
345 protected:
346   MCTargetExpr() : MCExpr(Target) {}
347   virtual ~MCTargetExpr() {}
348 public:
349   
350   virtual void PrintImpl(raw_ostream &OS) const = 0;
351   virtual bool EvaluateAsRelocatableImpl(MCValue &Res) const = 0;
352
353   
354   static bool classof(const MCExpr *E) {
355     return E->getKind() == MCExpr::Target;
356   }
357   static bool classof(const MCTargetExpr *) { return true; }
358 };
359
360 } // end namespace llvm
361
362 #endif