-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatterns.h
More file actions
295 lines (214 loc) · 7.73 KB
/
patterns.h
File metadata and controls
295 lines (214 loc) · 7.73 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#pragma once
#include <ciso646>
#include <vector>
#include "ir.h"
class Matcher;
class Pattern {
public:
virtual ~Pattern() {}
virtual void registerWith(Matcher * matcher) = 0;
/** Pattern replacement.
*/
void replaceWith(Pattern * with) {
}
protected:
/** Each pattern has the location to which it matches. This is always an instruction.
For instruction patterns, this is the location of the instruction matched to the pattern.
For value patterns, this is the instruction that produced the value, or in more general terms, a handle that we can use to query other analyses to give us additional details.
TODO this is a big vague still as it would work lovely for SSA but have problems elsewhere...
*/
Instruction * location_;
Code * code_;
};
namespace analysis {
class Analysis;
}
class Visitlet {
public:
virtual Pattern * pattern() = 0;
virtual void match() = 0;
/** Destructor gets rid of the pattern the visitlet matches accordingly. */
virtual ~Visitlet() {
delete pattern();
}
protected:
// TODO This should be in the matcher actually and the analysis should only retrieve it from it
template<typename ANALYSIS>
ANALYSIS & analysis() {
static ANALYSIS a;
return a;
}
};
/** Matcher is responsible for creating the matching automaton to search for the visitlet patterns.
I am not dealing with the actual creation, but it should not be too hard - in fact guys at CTU are doing research into tree and graph searching algorithms so they can point me to highly optimized variants, I would hope.
All I need is list of visitlets, and then for each instruction, knowing which visitlets will match it, and for each analysis, which visitlets will match on its abstract value (this is to allow prioritizing).
*/
class Matcher {
public:
template<typename ANALYSIS>
void attachToAnalysis(Pattern * p, typename ANALYSIS::Value const & value) {
std::cout << "Attaching pattern " << p << " to analysis " << analysisIndex<ANALYSIS>() << " and value " << value << std::endl;
}
void attachToInstruction(Pattern * p, Instruction::Opcode opcode) {
std::cout << "Attaching pattern " << p << " to instruction " << Instruction::opcodeToStr(opcode) << std::endl;
}
void addVisitlet(Visitlet * visitlet) {
visitlets_.push_back(visitlet);
visitlet->pattern()->registerWith(this);
}
private:
size_t reserveAnalysis() {
size_t result = analyses_.size();
analyses_.push_back(nullptr);
return result;
}
template<typename ANALYSIS>
size_t analysisIndex() {
static size_t index = reserveAnalysis();
return index;
}
std::vector<Visitlet *> visitlets_;
std::vector<analysis::Analysis *> analyses_;
};
/** Do not care pattern. */
class Any : public Pattern {};
/** Template for simple creation of abstract value patterns.
*/
template<typename ANALYSIS>
class TypePattern : public Pattern {
public:
TypePattern(typename ANALYSIS::Value const & value) :
value_(value) {}
typename ANALYSIS::Value const & value() const {
return value_;
}
void registerWith(Matcher * matcher) {
matcher->attachToAnalysis<ANALYSIS>(this, value_);
}
private:
typename ANALYSIS::Value value_;
};
/** Base class for instruction patterns.
*/
class InstructionPattern : public Pattern {
public:
/** Returns list of opcodes the pattern matches. */
virtual std::vector<Instruction::Opcode> const & matches() const = 0;
/** Returns the number of operands of *all* instructions the pattern matches.
*/
virtual size_t operands() const = 0;
/** Returns the pattern to be matched for index-th operand of the instruction.
*/
virtual Pattern * operand(size_t index) const = 0;
// --- proxy for the slim instruction ---
/** Returns the PC of the matched instruction. */
size_t pc() const {
return reinterpret_cast<char *>(location_) - code_->at<char>(0);
}
/** Returns the opcode of the matched instruction. */
Instruction::Opcode opcode() const {
return location_->opcode;
}
/** Returs the size of the matched instruction. */
size_t size() const {
return location_->size();
}
void registerWith(Matcher * matcher) override {
for (Instruction::Opcode opcode : matches())
matcher->attachToInstruction(this, opcode);
for (size_t i = 0, e = operands(); i, e; ++i)
operand(i)->registerWith(matcher);
}
};
class Push : public InstructionPattern {
public:
std::vector<Instruction::Opcode> const & matches() const override {
static std::vector<Instruction::Opcode> m({ Instruction::Opcode::Push });
return m;
}
size_t operands() const override {
return 0;
}
Pattern * operand(size_t index) const override {
assert(false and "instruction has no operands");
return nullptr;
}
/** Using the knowledge of the matched location and code, returns not the index to the constant pool, but the actual value it pushes on stack. */
int immediate() const {
return code_->pool[reinterpret_cast<Instruction::Push*>(location_)->index];
}
};
/** This is a non-templated version of the Add instruction.
If we made it templated, then its lhs and rhs would point to proper types they have based on the pattern. Having Add, or anything with static number of pops templated will be really easy.
*/
class Add : public InstructionPattern {
public:
Add(Pattern * lhs, Pattern * rhs) :
lhs_(lhs),
rhs_(rhs) {}
~Add() {
delete lhs_;
delete rhs_;
}
std::vector<Instruction::Opcode> const & matches() const override {
static std::vector<Instruction::Opcode> m({ Instruction::Opcode::Add });
return m;
}
size_t operands() const override {
return 2;
}
Pattern * operand(size_t index) const override {
if (index == 0)
return lhs_;
else if (index == 1)
return rhs_;
else
assert(false and "instruction has only two operands");
return nullptr;
}
Pattern * lhs() const { // everything is pointers for simplicity and similarity with C where references are, sadly, not
return lhs_;
}
Pattern * rhs() const {
return rhs_;
}
private:
Pattern * lhs_;
Pattern * rhs_;
};
/** Pattern for matching call instruction.
Call instruction has f, the function it calls to, and a variable number of operands. This is the non-templated version. The templated version can either use std::tuple, with not really nice syntax, or with some more thinking we can create our better syntax - but the syntax of variable sized instruction patterns would never be as straightforward as say for Add.
*/
class Call : public InstructionPattern {
public:
Call(Pattern * f, std::initializer_list<Pattern*> arguments) {
operands_.push_back(f);
for (Pattern * p : arguments)
operands_.push_back(p);
}
~Call() {
for (Pattern * p : operands_)
delete p;
}
std::vector<Instruction::Opcode> const & matches() const override {
static std::vector<Instruction::Opcode> m({ Instruction::Opcode::Call });
return m;
}
size_t operands() const override {
return operands_.size();
}
Pattern * operand(size_t index) const override {
return operands_[index];
}
Pattern * f() const {
return operands_[0];
}
size_t arguments() const {
return operands_.size() - 1;
}
Pattern * argument(size_t index) const {
return operands_[index + 1];
}
private:
std::vector<Pattern *> operands_;
};