blob: e8650e468f2426eff116aeacb596d4cdd7a38852 [file] [log] [blame]
Daniel Dunbara1f04642009-06-29 20:40:36 +00001//===- 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
16namespace llvm {
17class MCContext;
18class MCSymbol;
Daniel Dunbarc6c054e2009-06-30 01:49:52 +000019class MCValue;
Daniel Dunbara1f04642009-06-29 20:40:36 +000020
21class AsmExpr {
22public:
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
30private:
31 AsmExprKind Kind;
32
33protected:
34 AsmExpr(AsmExprKind _Kind) : Kind(_Kind) {}
35
36public:
37 virtual ~AsmExpr();
38
39 AsmExprKind getKind() const { return Kind; }
40
41 /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
42 ///
Daniel Dunbarc6c054e2009-06-30 01:49:52 +000043 /// @param Res - The absolute value, if evaluation succeeds.
Daniel Dunbara1f04642009-06-29 20:40:36 +000044 /// @result - True on success.
45 bool EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const;
46
Daniel Dunbarc6c054e2009-06-30 01:49:52 +000047 /// 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
Daniel Dunbara1f04642009-06-29 20:40:36 +000054 static bool classof(const AsmExpr *) { return true; }
55};
56
57class AsmConstantExpr : public AsmExpr {
58 int64_t Value;
59
60public:
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
72class AsmSymbolRefExpr : public AsmExpr {
73 MCSymbol *Symbol;
74
75public:
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
87class AsmUnaryExpr : public AsmExpr {
88public:
89 enum Opcode {
90 LNot, /// Logical negation.
91 Minus, /// Unary minus.
92 Not, /// Bit-wise negation.
93 Plus /// Unary plus.
94 };
95
96private:
97 Opcode Op;
98 AsmExpr *Expr;
99
100public:
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
117class AsmBinaryExpr : public AsmExpr {
118public:
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
140private:
141 Opcode Op;
142 AsmExpr *LHS, *RHS;
143
144public:
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