Repository navigation
Expand file tree
/
Copy pathevaluator.cc
More file actions
186 lines (162 loc) · 4.91 KB
/
Copy pathevaluator.cc
File metadata and controls
186 lines (162 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "evaluator.h"
#include "ast.h"
#include "token.h"
template<typename T>
void Evaluator<T>::VisitBinaryOp(BinaryOp* binary)
{
#define L Evaluator<T>().Eval(binary->_lhs)
#define R Evaluator<T>().Eval(binary->_rhs)
#define LL Evaluator<long>().Eval(binary->_lhs)
#define LR Evaluator<long>().Eval(binary->_rhs)
if (binary->Type()->ToPointerType()) {
auto val = Evaluator<Addr>().Eval(binary);
if (val._label.size()) {
Error(binary, "expect constant integer expression");
}
_val = static_cast<T>(val._offset);
return;
}
switch (binary->_op) {
case '+': _val = L + R; break;
case '-': _val = L - R; break;
case '*': _val = L * R; break;
case '/': {
auto l = L, r = R;
if (r == 0)
Error(binary, "division by zero");
_val = l / r;
} break;
case '%': {
auto l = LL, r = LR;
if (r == 0)
Error(binary, "division by zero");
_val = l % r;
} break;
// Bitwise operators that do not accept float
case '|': _val = LL | LR; break;
case '&': _val = LL & LR; break;
case '^': _val = LL ^ LR; break;
case Token::LEFT_OP: _val = LL << LR; break;
case Token::RIGHT_OP: _val = LL >> LR; break;
case '<': _val = L < R; break;
case '>': _val = L > R; break;
case Token::AND_OP: _val = L && R; break;
case Token::OR_OP: _val = L || R; break;
case Token::EQ_OP: _val = L == R; break;
case Token::NE_OP: _val = L != R; break;
case Token::LE_OP: _val = L <= R; break;
case Token::GE_OP: _val = L >= R; break;
case '=': case ',': _val = R; break;
case ']': case '.':
Error(binary, "expect constant expression");
default: assert(false);
}
#undef L
#undef R
#undef LL
#undef LR
}
template<typename T>
void Evaluator<T>::VisitUnaryOp(UnaryOp* unary)
{
#define VAL Evaluator<T>().Eval(unary->_operand)
#define LVAL Evaluator<long>().Eval(unary->_operand)
switch (unary->_op) {
case Token::PLUS: _val = VAL; break;
case Token::MINUS: _val = -VAL; break;
case '~': _val = ~LVAL; break;
case '!': _val = !VAL; break;
case Token::CAST:
if (unary->Type()->IsInteger())
_val = static_cast<long>(VAL);
else
_val = VAL;
break;
default: Error(unary, "expect constant expression");
}
#undef VAL
}
template<typename T>
void Evaluator<T>::VisitConditionalOp(ConditionalOp* condOp)
{
bool cond;
auto condType = condOp->_cond->Type();
if (condType->IsInteger()) {
auto val = Evaluator<long>().Eval(condOp->_cond);
cond = val != 0;
} else if (condType->IsFloat()) {
auto val = Evaluator<double>().Eval(condOp->_cond);
cond = val != 0.0;
} else if (condType->ToPointerType()) {
auto val = Evaluator<Addr>().Eval(condOp->_cond);
cond = val._label.size() || val._offset;
}
if (cond) {
_val = Evaluator<T>().Eval(condOp->_exprTrue);
} else {
_val = Evaluator<T>().Eval(condOp->_exprFalse);
}
}
void Evaluator<Addr>::VisitBinaryOp(BinaryOp* binary)
{
auto l = Evaluator<Addr>().Eval(binary->_lhs);
auto r = Evaluator<Addr>().Eval(binary->_rhs);
int width;
auto pointerType = binary->_lhs->Type()->ToPointerType();
if (pointerType) {
width = pointerType->Derived()->Width();
}
switch (binary->_op) {
case '+':
_addr._label = l._label;
_addr._offset = l._offset + r._offset * width;
break;
case '-':
_addr._label = l._label;
_addr._offset = l._offset + r._offset * width;
break;
case ']':
_addr._label = l._label;
_addr._offset = l._offset + r._offset * width;
break;
case '.': {
_addr._label = l._label;
auto type = binary->_lhs->Type()->ToStructUnionType();
auto offset = type->GetMember(r._label)->Offset();
_addr._offset = l._offset + offset;
break;
}
default: assert(false);
}
}
void Evaluator<Addr>::VisitUnaryOp(UnaryOp* unary)
{
auto addr = Evaluator<Addr>().Eval(unary->_operand);
switch (unary->_op) {
case Token::CAST:
case Token::ADDR:
case Token::DEREF:
_addr = addr; break;
default: assert(false);
}
}
void Evaluator<Addr>::VisitConditionalOp(ConditionalOp* condOp)
{
bool cond;
auto condType = condOp->_cond->Type();
if (condType->IsInteger()) {
auto val = Evaluator<long>().Eval(condOp->_cond);
cond = val != 0;
} else if (condType->IsFloat()) {
auto val = Evaluator<double>().Eval(condOp->_cond);
cond = val != 0.0;
} else if (condType->ToPointerType()) {
auto val = Evaluator<Addr>().Eval(condOp->_cond);
cond = val._label.size() || val._offset;
}
if (cond) {
_addr = Evaluator<Addr>().Eval(condOp->_exprTrue);
} else {
_addr = Evaluator<Addr>().Eval(condOp->_exprFalse);
}
}