-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression_evaluate.cpp
168 lines (125 loc) · 3.2 KB
/
Expression_evaluate.cpp
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
#include "Expression_Internal.h"
#include <stdexcept>
using namespace Expression_Internal;
#pragma mark - evaluate
#if 0
bool Expression::evaluate(uint32_t pc,
const identifier_map &env,
uint32_t &result) const {
return false;
}
bool IntegerExpression::evaluate(uint32_t pc,
const identifier_map &env,
uint32_t &result) const {
result = _value;
return true;
}
bool PCExpression::evaluate(uint32_t pc,
const identifier_map &env,
uint32_t &result) const {
result = pc;
return true;
}
bool IdentifierExpression::evaluate(uint32_t pc,
const identifier_map &env,
uint32_t &result) const {
auto iter = env.find(_value);
if (iter != env.end()) {
result = iter->second;
return true;
}
return false;
}
bool UnaryExpression::evaluate(uint32_t pc,
const identifier_map &env,
uint32_t &result) const {
uint32_t value;
if (!_children[0]->evaluate(pc, env, value)) return false;
result = unary_op(_op, value);
return true;
}
bool BinaryExpression::evaluate(uint32_t pc,
const identifier_map &env,
uint32_t &result) const {
uint32_t a, b;
if (!_children[0]->evaluate(pc, env, a)) return false;
if (!_children[1]->evaluate(pc, env, b)) return false;
result = binary_op(_op, a, b);
return true;
}
#endif
#pragma mark - evaluate
bool Expression::evaluate(uint32_t pc,
const identifier_map &env,
uint32_t &result) const {
try {
result = evaluate(pc, env);
return true;
} catch (...) {
return false;
}
}
bool Expression::evaluate_relative(uint32_t pc, int32_t &result) const {
try {
result = evaluate_relative(pc);
return true;
} catch (...) {
return false;
}
}
/*
* these throw a runtime error if unable to calculate an answer.
*
*/
uint32_t Expression::evaluate(uint32_t pc, const identifier_map &env) const
{
throw std::runtime_error("unable to evaluate expression");
}
uint32_t PCExpression::evaluate(uint32_t pc, const identifier_map &env) const
{
return pc;
}
uint32_t RelExpression::evaluate(uint32_t pc, const identifier_map &env) const
{
return _offset;
}
uint32_t IntegerExpression::evaluate(uint32_t pc, const identifier_map &env) const
{
return _value;
}
uint32_t IdentifierExpression::evaluate(uint32_t pc, const identifier_map &env) const
{
auto iter = env.find(_value);
if (iter == env.end()) {
std::string msg = "unable to evaluate expression: " + *_value + " not defined";
throw std::runtime_error(msg);
}
return iter->second;
}
uint32_t UnaryExpression::evaluate(uint32_t pc, const identifier_map &env) const
{
uint32_t a = _children[0]->evaluate(pc, env);
return unary_op(_op, a);
}
uint32_t BinaryExpression::evaluate(uint32_t pc, const identifier_map &env) const
{
uint32_t a = _children[0]->evaluate(pc, env);
uint32_t b = _children[1]->evaluate(pc, env);
return binary_op(_op, a, b);
}
// bra label
// make_relative converts to -> bra [__start + pc]
// bra label +- offset will be optimized to a relative.
// all others, leave for the linker to handle.
int32_t Expression::evaluate_relative(uint32_t pc) const
{
throw std::runtime_error("unable to evaluate expression");
}
int32_t RelExpression::evaluate_relative(uint32_t pc) const {
return _offset - pc;
}
/*
uint32_t IntegerExpression::evaluate_relative(uint32_t pc) {
// bra $1234 ?
}
*/