-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expression.h
245 lines (168 loc) · 6.22 KB
/
Expression.h
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef __expression_h__
#define __expression_h__
//#include "common.h"
#include <string>
#include <vector>
#include <unordered_map>
#include <utility>
#include <memory>
#include <functional>
#include "dp_register.h"
#include "intern.h"
//enum AddressMode;
class Expression;
class VectorExpression;
typedef std::shared_ptr<Expression> ExpressionPtr;
typedef std::shared_ptr<VectorExpression> VectorExpressionPtr;
typedef const std::string *identifier;
typedef std::unordered_map<identifier, uint32_t> identifier_map;
class Expression : public std::enable_shared_from_this<Expression> {
public:
static ExpressionPtr PC();
static ExpressionPtr Integer(uint32_t value);
static ExpressionPtr Register(dp_register value);
static ExpressionPtr Rel(uint32_t offset);
static ExpressionPtr Identifier(identifier name);
static ExpressionPtr Identifier(std::string &&s) { return Identifier(intern(std::move(s))); }
static ExpressionPtr Identifier(const std::string &s) { return Identifier(intern(s)); }
static ExpressionPtr WeakIdentifier(identifier name);
static ExpressionPtr WeakIdentifier(std::string &&s) { return WeakIdentifier(intern(std::move(s))); }
static ExpressionPtr WeakIdentifier(const std::string &s) { return WeakIdentifier(intern(s)); }
static ExpressionPtr Unary(unsigned op, ExpressionPtr a);
static ExpressionPtr Binary(unsigned op, ExpressionPtr a, ExpressionPtr b);
static VectorExpressionPtr Vector();
static VectorExpressionPtr Vector(const std::vector<ExpressionPtr> &);
static VectorExpressionPtr Vector(std::vector<ExpressionPtr> &&);
static ExpressionPtr String(const std::string *name);
static void ErasePool();
enum expression_type {
type_unknown = 0,
type_integer,
type_register,
type_pc,
type_identifier,
type_string,
type_rel,
type_unary,
type_binary,
type_vector,
};
expression_type type() const {
return _type;
}
bool is_binary() const { return _type == type_binary; }
bool is_identifier() const { return _type == type_identifier; }
bool is_integer() const { return _type == type_integer; }
bool is_pc() const { return _type == type_pc; }
bool is_register() const { return _type == type_register; }
bool is_rel() const {return _type == type_rel; }
bool is_string() const {return _type == type_string; }
bool is_unary() const { return _type == type_unary; }
bool is_vector() const {return _type == type_vector; }
bool is_terminal() const { return _type < type_unary; }
bool is_temporary(dp_register &rv) const {
if (is_register(rv)) {
return rv.is_temporary();
}
return false;
}
std::string to_string() const {
std::string rv;
to_string(rv);
return rv;
}
std::vector<identifier> identifiers() const;
std::vector<uint8_t> to_omf(unsigned type, unsigned size) {
std::vector<uint8_t> rv;
rv.push_back(type);
rv.push_back(size);
to_omf(rv);
rv.push_back(0x00);
return rv;
}
std::vector<uint8_t> to_omf(unsigned type, unsigned size, unsigned displacement) {
std::vector<uint8_t> rv;
rv.push_back(type);
rv.push_back(size);
rv.push_back(displacement >> 0);
rv.push_back(displacement >> 8);
rv.push_back(displacement >> 16);
rv.push_back(displacement >> 24);
to_omf(rv);
rv.push_back(0x00);
return rv;
}
virtual bool is_identifier(identifier &) const;
virtual bool is_integer(uint32_t &) const;
virtual bool is_register(dp_register &) const;
virtual bool is_rel(uint32_t &) const;
virtual bool is_string(const std::string *&) const;
virtual bool is_vector(std::vector<ExpressionPtr> &) const;
ExpressionPtr rename(identifier oldname, identifier newname);
ExpressionPtr rename(dp_register oldname, dp_register newname);
ExpressionPtr replace_identifier(std::function<ExpressionPtr(identifier)> &fx);
ExpressionPtr make_relative(uint32_t pc) /* const */;
ExpressionPtr make_relative(uint32_t pc, const identifier_map &env) /* const */;
ExpressionPtr simplify();
struct register_info {
std::unordered_map<dp_register, uint32_t> trmap;
uint32_t vbase = 0;
uint32_t pbase = 0;
};
ExpressionPtr assign_registers(const register_info &ri);
bool evaluate(uint32_t pc, const identifier_map &env, uint32_t &result) const;
bool evaluate_relative(uint32_t pc, int32_t &result) const;
virtual uint32_t evaluate(uint32_t pc, const identifier_map &env) const /* throw(std::runtime_exception) */;
virtual int32_t evaluate_relative(uint32_t pc) const /* throw(std::runtime_exception) */;
virtual bool operator==(const Expression &) const;
bool operator!=(const Expression &rhs) const {
return !(*this == rhs);
}
class Visitor;
class ConstVisitor;
class MapVisitor;
// semi-private constructors.
Expression(expression_type type) : _type(type)
{}
Expression(const Expression &) = delete;
Expression &operator=(const Expression &) = delete;
virtual ~Expression();
protected:
friend class UnaryExpression;
friend class BinaryExpression;
friend class VectorExpression;
virtual void accept(Visitor &) = 0;
virtual void accept(ConstVisitor &) const= 0;
virtual ExpressionPtr accept(MapVisitor &) const = 0;
// children override
virtual void to_string(std::string &) const = 0;
virtual void to_omf(std::vector<uint8_t> &) const;
private:
expression_type _type = type_unknown;
};
class VectorExpression : public Expression {
public:
typedef std::vector<ExpressionPtr> children_type;
size_t size() const { return _children.size(); }
void push_back(ExpressionPtr e) { _children.push_back(e); }
virtual bool is_vector(std::vector<ExpressionPtr> &) const override final;
virtual bool operator==(const Expression &) const override final;
bool operator==(const VectorExpression &) const;
// semi-private constructors.
VectorExpression() : Expression(type_vector)
{}
VectorExpression(std::vector<ExpressionPtr> &&);
VectorExpression(const std::vector<ExpressionPtr> &);
VectorExpression(const VectorExpression &) = delete;
VectorExpression &operator=(const VectorExpression &) = delete;
virtual ~VectorExpression() final;
protected:
virtual void accept(Visitor &) override final;
virtual void accept(ConstVisitor &) const override final;
virtual ExpressionPtr accept(MapVisitor &) const override final;
virtual void to_string(std::string &) const final;
private:
friend class Expression;
children_type _children;
};
#endif