llvm-mc: Emit parsed instructions to the MCStreamer.
[oota-llvm.git] / tools / llvm-mc / AsmExpr.h
1 //===- AsmExpr.h - Assembly file 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 ASMEXPR_H
11 #define ASMEXPR_H
12
13 #include "llvm/Support/Casting.h"
14 #include "llvm/Support/DataTypes.h"
15
16 namespace llvm {
17 class MCContext;
18 class MCSymbol;
19 class MCValue;
20
21 class AsmExpr {
22 public:
23   enum AsmExprKind {
24     Binary,    /// Binary expressions.
25     Constant,  /// Constant expressions.
26     SymbolRef, /// References to labels and assigned expressions.
27     Unary      /// Unary expressions.
28   };
29   
30 private:
31   AsmExprKind Kind;
32   
33 protected:
34   AsmExpr(AsmExprKind _Kind) : Kind(_Kind) {}
35   
36 public:
37   virtual ~AsmExpr();
38
39   AsmExprKind getKind() const { return Kind; }
40
41   /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
42   ///
43   /// @param Res - The absolute value, if evaluation succeeds.
44   /// @result - True on success.
45   bool EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const;
46
47   /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
48   /// value.
49   ///
50   /// @param Res - The relocatable value, if evaluation succeeds.
51   /// @result - True on success.
52   bool EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const;
53
54   static bool classof(const AsmExpr *) { return true; }
55 };
56
57 class AsmConstantExpr : public AsmExpr {
58   int64_t Value;
59
60 public:
61   AsmConstantExpr(int64_t _Value) 
62     : AsmExpr(AsmExpr::Constant), Value(_Value) {}
63   
64   int64_t getValue() const { return Value; }
65
66   static bool classof(const AsmExpr *E) { 
67     return E->getKind() == AsmExpr::Constant; 
68   }
69   static bool classof(const AsmConstantExpr *) { return true; }
70 };
71
72 class AsmSymbolRefExpr : public AsmExpr {
73   MCSymbol *Symbol;
74
75 public:
76   AsmSymbolRefExpr(MCSymbol *_Symbol) 
77     : AsmExpr(AsmExpr::SymbolRef), Symbol(_Symbol) {}
78   
79   MCSymbol *getSymbol() const { return Symbol; }
80
81   static bool classof(const AsmExpr *E) { 
82     return E->getKind() == AsmExpr::SymbolRef; 
83   }
84   static bool classof(const AsmSymbolRefExpr *) { return true; }
85 };
86
87 class AsmUnaryExpr : public AsmExpr {
88 public:
89   enum Opcode {
90     LNot,  /// Logical negation.
91     Minus, /// Unary minus.
92     Not,   /// Bit-wise negation.
93     Plus   /// Unary plus.
94   };
95
96 private:
97   Opcode Op;
98   AsmExpr *Expr;
99
100 public:
101   AsmUnaryExpr(Opcode _Op, AsmExpr *_Expr)
102     : AsmExpr(AsmExpr::Unary), Op(_Op), Expr(_Expr) {}
103   ~AsmUnaryExpr() {
104     delete Expr;
105   }
106
107   Opcode getOpcode() const { return Op; }
108
109   AsmExpr *getSubExpr() const { return Expr; }
110
111   static bool classof(const AsmExpr *E) { 
112     return E->getKind() == AsmExpr::Unary; 
113   }
114   static bool classof(const AsmUnaryExpr *) { return true; }
115 };
116
117 class AsmBinaryExpr : public AsmExpr {
118 public:
119   enum Opcode {
120     Add,  /// Addition.
121     And,  /// Bitwise and.
122     Div,  /// Division.
123     EQ,   /// Equality comparison.
124     GT,   /// Greater than comparison.
125     GTE,  /// Greater than or equal comparison.
126     LAnd, /// Logical and.
127     LOr,  /// Logical or.
128     LT,   /// Less than comparison.
129     LTE,  /// Less than or equal comparison.
130     Mod,  /// Modulus.
131     Mul,  /// Multiplication.
132     NE,   /// Inequality comparison.
133     Or,   /// Bitwise or.
134     Shl,  /// Bitwise shift left.
135     Shr,  /// Bitwise shift right.
136     Sub,  /// Subtraction.
137     Xor   /// Bitwise exclusive or.
138   };
139
140 private:
141   Opcode Op;
142   AsmExpr *LHS, *RHS;
143
144 public:
145   AsmBinaryExpr(Opcode _Op, AsmExpr *_LHS, AsmExpr *_RHS)
146     : AsmExpr(AsmExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
147   ~AsmBinaryExpr() {
148     delete LHS;
149     delete RHS;
150   }
151
152   Opcode getOpcode() const { return Op; }
153
154   AsmExpr *getLHS() const { return LHS; }
155   AsmExpr *getRHS() const { return RHS; }
156
157   static bool classof(const AsmExpr *E) { 
158     return E->getKind() == AsmExpr::Binary; 
159   }
160   static bool classof(const AsmBinaryExpr *) { return true; }
161 };
162
163 } // end namespace llvm
164
165 #endif