MC: Remove dead MCAssembler argument -- Rafael, can you check the FIXME I added
[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/ADT/DenseMap.h"
14 #include "llvm/Support/Casting.h"
15 #include "llvm/Support/DataTypes.h"
16
17 namespace llvm {
18 class MCAsmInfo;
19 class MCAsmLayout;
20 class MCAssembler;
21 class MCContext;
22 class MCSectionData;
23 class MCSymbol;
24 class MCValue;
25 class raw_ostream;
26 class StringRef;
27 typedef DenseMap<const MCSectionData*, uint64_t> SectionAddrMap;
28
29 /// MCExpr - Base class for the full range of assembler expressions which are
30 /// needed for parsing.
31 class MCExpr {
32 public:
33   enum ExprKind {
34     Binary,    ///< Binary expressions.
35     Constant,  ///< Constant expressions.
36     SymbolRef, ///< References to labels and assigned expressions.
37     Unary,     ///< Unary expressions.
38     Target     ///< Target specific expression.
39   };
40
41 private:
42   ExprKind Kind;
43
44   MCExpr(const MCExpr&); // DO NOT IMPLEMENT
45   void operator=(const MCExpr&); // DO NOT IMPLEMENT
46
47   bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
48                           const MCAsmLayout *Layout,
49                           const SectionAddrMap *Addrs) const;
50 protected:
51   explicit MCExpr(ExprKind _Kind) : Kind(_Kind) {}
52
53   bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout,
54                                  const SectionAddrMap *Addrs,
55                                  bool InSet) const;
56 public:
57   /// @name Accessors
58   /// @{
59
60   ExprKind getKind() const { return Kind; }
61
62   /// @}
63   /// @name Utility Methods
64   /// @{
65
66   void print(raw_ostream &OS) const;
67   void dump() const;
68
69   /// @}
70   /// @name Expression Evaluation
71   /// @{
72
73   /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
74   ///
75   /// @param Res - The absolute value, if evaluation succeeds.
76   /// @param Layout - The assembler layout object to use for evaluating symbol
77   /// values. If not given, then only non-symbolic expressions will be
78   /// evaluated.
79   /// @result - True on success.
80   bool EvaluateAsAbsolute(int64_t &Res) const;
81   bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
82   bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
83   bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
84                           const SectionAddrMap &Addrs) const;
85
86   /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
87   /// value, i.e. an expression of the fixed form (a - b + constant).
88   ///
89   /// @param Res - The relocatable value, if evaluation succeeds.
90   /// @param Layout - The assembler layout object to use for evaluating values.
91   /// @result - True on success.
92   bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout = 0) const;
93
94   /// @}
95
96   static bool classof(const MCExpr *) { return true; }
97 };
98
99 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
100   E.print(OS);
101   return OS;
102 }
103
104 //// MCConstantExpr - Represent a constant integer expression.
105 class MCConstantExpr : public MCExpr {
106   int64_t Value;
107
108   explicit MCConstantExpr(int64_t _Value)
109     : MCExpr(MCExpr::Constant), Value(_Value) {}
110
111 public:
112   /// @name Construction
113   /// @{
114
115   static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
116
117   /// @}
118   /// @name Accessors
119   /// @{
120
121   int64_t getValue() const { return Value; }
122
123   /// @}
124
125   static bool classof(const MCExpr *E) {
126     return E->getKind() == MCExpr::Constant;
127   }
128   static bool classof(const MCConstantExpr *) { return true; }
129 };
130
131 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
132 /// expression.
133 ///
134 /// A symbol reference in an expression may be a use of a label, a use of an
135 /// assembler variable (defined constant), or constitute an implicit definition
136 /// of the symbol as external.
137 class MCSymbolRefExpr : public MCExpr {
138 public:
139   enum VariantKind {
140     VK_None,
141     VK_Invalid,
142
143     VK_GOT,
144     VK_GOTOFF,
145     VK_GOTPCREL,
146     VK_GOTTPOFF,
147     VK_INDNTPOFF,
148     VK_NTPOFF,
149     VK_GOTNTPOFF,
150     VK_PLT,
151     VK_TLSGD,
152     VK_TLSLD,
153     VK_TLSLDM,
154     VK_TPOFF,
155     VK_DTPOFF,
156     VK_TLVP,      // Mach-O thread local variable relocation
157     VK_ARM_HI16,  // The R_ARM_MOVT_ABS relocation (:upper16: in the .s file)
158     VK_ARM_LO16,  // The R_ARM_MOVW_ABS_NC relocation (:lower16: in the .w file)
159     // FIXME: We'd really like to use the generic Kinds listed above for these.
160     VK_ARM_PLT,   // ARM-style PLT references. i.e., (PLT) instead of @PLT
161     VK_ARM_TLSGD, //   ditto for TLSGD, GOT, GOTOFF, TPOFF and GOTTPOFF
162     VK_ARM_GOT,
163     VK_ARM_GOTOFF,
164     VK_ARM_TPOFF,
165     VK_ARM_GOTTPOFF,
166     
167     VK_PPC_TOC,
168     VK_PPC_HA16,  // ha16(symbol)
169     VK_PPC_LO16   // lo16(symbol)
170   };
171
172 private:
173   /// The symbol being referenced.
174   const MCSymbol *Symbol;
175
176   /// The symbol reference modifier.
177   const VariantKind Kind;
178
179   explicit MCSymbolRefExpr(const MCSymbol *_Symbol, VariantKind _Kind)
180     : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol), Kind(_Kind) {}
181
182 public:
183   /// @name Construction
184   /// @{
185
186   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) {
187     return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx);
188   }
189
190   static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind,
191                                        MCContext &Ctx);
192   static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind,
193                                        MCContext &Ctx);
194   
195   /// @}
196   /// @name Accessors
197   /// @{
198
199   const MCSymbol &getSymbol() const { return *Symbol; }
200
201   VariantKind getKind() const { return Kind; }
202
203   /// @}
204   /// @name Static Utility Functions
205   /// @{
206
207   static StringRef getVariantKindName(VariantKind Kind);
208
209   static VariantKind getVariantKindForName(StringRef Name);
210
211   /// @}
212
213   static bool classof(const MCExpr *E) {
214     return E->getKind() == MCExpr::SymbolRef;
215   }
216   static bool classof(const MCSymbolRefExpr *) { return true; }
217 };
218
219 /// MCUnaryExpr - Unary assembler expressions.
220 class MCUnaryExpr : public MCExpr {
221 public:
222   enum Opcode {
223     LNot,  ///< Logical negation.
224     Minus, ///< Unary minus.
225     Not,   ///< Bitwise negation.
226     Plus   ///< Unary plus.
227   };
228
229 private:
230   Opcode Op;
231   const MCExpr *Expr;
232
233   MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
234     : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
235
236 public:
237   /// @name Construction
238   /// @{
239
240   static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
241                                    MCContext &Ctx);
242   static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
243     return Create(LNot, Expr, Ctx);
244   }
245   static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
246     return Create(Minus, Expr, Ctx);
247   }
248   static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
249     return Create(Not, Expr, Ctx);
250   }
251   static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
252     return Create(Plus, Expr, Ctx);
253   }
254
255   /// @}
256   /// @name Accessors
257   /// @{
258
259   /// getOpcode - Get the kind of this unary expression.
260   Opcode getOpcode() const { return Op; }
261
262   /// getSubExpr - Get the child of this unary expression.
263   const MCExpr *getSubExpr() const { return Expr; }
264
265   /// @}
266
267   static bool classof(const MCExpr *E) {
268     return E->getKind() == MCExpr::Unary;
269   }
270   static bool classof(const MCUnaryExpr *) { return true; }
271 };
272
273 /// MCBinaryExpr - Binary assembler expressions.
274 class MCBinaryExpr : public MCExpr {
275 public:
276   enum Opcode {
277     Add,  ///< Addition.
278     And,  ///< Bitwise and.
279     Div,  ///< Signed division.
280     EQ,   ///< Equality comparison.
281     GT,   ///< Signed greater than comparison (result is either 0 or some
282           ///< target-specific non-zero value)
283     GTE,  ///< Signed greater than or equal comparison (result is either 0 or
284           ///< some target-specific non-zero value).
285     LAnd, ///< Logical and.
286     LOr,  ///< Logical or.
287     LT,   ///< Signed less than comparison (result is either 0 or
288           ///< some target-specific non-zero value).
289     LTE,  ///< Signed less than or equal comparison (result is either 0 or
290           ///< some target-specific non-zero value).
291     Mod,  ///< Signed remainder.
292     Mul,  ///< Multiplication.
293     NE,   ///< Inequality comparison.
294     Or,   ///< Bitwise or.
295     Shl,  ///< Shift left.
296     Shr,  ///< Shift right (arithmetic or logical, depending on target)
297     Sub,  ///< Subtraction.
298     Xor   ///< Bitwise exclusive or.
299   };
300
301 private:
302   Opcode Op;
303   const MCExpr *LHS, *RHS;
304
305   MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
306     : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
307
308 public:
309   /// @name Construction
310   /// @{
311
312   static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
313                                     const MCExpr *RHS, MCContext &Ctx);
314   static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
315                                        MCContext &Ctx) {
316     return Create(Add, LHS, RHS, Ctx);
317   }
318   static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
319                                        MCContext &Ctx) {
320     return Create(And, LHS, RHS, Ctx);
321   }
322   static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
323                                        MCContext &Ctx) {
324     return Create(Div, LHS, RHS, Ctx);
325   }
326   static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
327                                       MCContext &Ctx) {
328     return Create(EQ, LHS, RHS, Ctx);
329   }
330   static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
331                                       MCContext &Ctx) {
332     return Create(GT, LHS, RHS, Ctx);
333   }
334   static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
335                                        MCContext &Ctx) {
336     return Create(GTE, LHS, RHS, Ctx);
337   }
338   static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
339                                         MCContext &Ctx) {
340     return Create(LAnd, LHS, RHS, Ctx);
341   }
342   static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
343                                        MCContext &Ctx) {
344     return Create(LOr, LHS, RHS, Ctx);
345   }
346   static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
347                                       MCContext &Ctx) {
348     return Create(LT, LHS, RHS, Ctx);
349   }
350   static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
351                                        MCContext &Ctx) {
352     return Create(LTE, LHS, RHS, Ctx);
353   }
354   static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
355                                        MCContext &Ctx) {
356     return Create(Mod, LHS, RHS, Ctx);
357   }
358   static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
359                                        MCContext &Ctx) {
360     return Create(Mul, LHS, RHS, Ctx);
361   }
362   static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
363                                       MCContext &Ctx) {
364     return Create(NE, LHS, RHS, Ctx);
365   }
366   static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
367                                       MCContext &Ctx) {
368     return Create(Or, LHS, RHS, Ctx);
369   }
370   static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
371                                        MCContext &Ctx) {
372     return Create(Shl, LHS, RHS, Ctx);
373   }
374   static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
375                                        MCContext &Ctx) {
376     return Create(Shr, LHS, RHS, Ctx);
377   }
378   static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
379                                        MCContext &Ctx) {
380     return Create(Sub, LHS, RHS, Ctx);
381   }
382   static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
383                                        MCContext &Ctx) {
384     return Create(Xor, LHS, RHS, Ctx);
385   }
386
387   /// @}
388   /// @name Accessors
389   /// @{
390
391   /// getOpcode - Get the kind of this binary expression.
392   Opcode getOpcode() const { return Op; }
393
394   /// getLHS - Get the left-hand side expression of the binary operator.
395   const MCExpr *getLHS() const { return LHS; }
396
397   /// getRHS - Get the right-hand side expression of the binary operator.
398   const MCExpr *getRHS() const { return RHS; }
399
400   /// @}
401
402   static bool classof(const MCExpr *E) {
403     return E->getKind() == MCExpr::Binary;
404   }
405   static bool classof(const MCBinaryExpr *) { return true; }
406 };
407
408 /// MCTargetExpr - This is an extension point for target-specific MCExpr
409 /// subclasses to implement.
410 ///
411 /// NOTE: All subclasses are required to have trivial destructors because
412 /// MCExprs are bump pointer allocated and not destructed.
413 class MCTargetExpr : public MCExpr {
414   virtual void Anchor();
415 protected:
416   MCTargetExpr() : MCExpr(Target) {}
417   virtual ~MCTargetExpr() {}
418 public:
419
420   virtual void PrintImpl(raw_ostream &OS) const = 0;
421   virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
422                                          const MCAsmLayout *Layout) const = 0;
423
424
425   static bool classof(const MCExpr *E) {
426     return E->getKind() == MCExpr::Target;
427   }
428   static bool classof(const MCTargetExpr *) { return true; }
429 };
430
431 } // end namespace llvm
432
433 #endif