-
Notifications
You must be signed in to change notification settings - Fork 1
/
bf_optimizations.cpp
198 lines (164 loc) · 4.7 KB
/
bf_optimizations.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
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
#include "bf.hpp"
namespace bf {
using Instructions = std::vector<Instruction>;
using Actions = std::initializer_list<Action>;
static bool matches(const Instructions& instrs, int offset, Actions actions)
{
const int sz = instrs.size();
for(auto a : actions){
if(offset >= sz || a != instrs.at(offset++).action){
return false;
}
}
return true;
}
// Two passes allows for simple forward looking pattern matches
Instructions optimizations(const Instructions& instrs, bool take_second_pass)
{
Instructions result;
const int sz = instrs.size();
for(int i=0; i<sz; i++){
#define AT(x) instrs.at(i+x)
#define VAL(x) instrs.at(i+x).val
auto pattern = [&](Actions actions){ return matches(instrs, i, actions); };
//////////////////////////////////////////////
// The following block contains optimizations
// for non-terminals, available in pass two.
//////////////////////////////////////////////
{
if(pattern({ZERO, MOVE}) && VAL(1) == 1){
result.push_back({ ZEROM1 });
i++;
continue;
}
if(pattern({MOVE, ZERO}) && VAL(0) == 1){
result.push_back({ M1ZERO });
i++;
continue;
}
if(pattern({FALSEJUMP, MOVE, ZERO, MXDECR1, TRUEJUMP }) &&
VAL(1) == 1 && VAL(3) == -1){
result.push_back({ZERO2IF});
i+=4;
continue;
}
if(pattern({ MOVE, GIVE, MOVE}) &&
VAL(2) == VAL(1) &&
VAL(0) == -VAL(2)){
result.push_back({ TAKE, VAL(0) });
i+=2;
continue;
}
if(pattern({FALSEJUMP, MOVE, GIVE, MOVE, TRUEJUMP})){
result.push_back({VALUE, VAL(1) });
result.push_back({VALUE, VAL(2) });
result.push_back({WHILEMGM, VAL(3) });
i += 4;
continue;
}
if(pattern({DECR1MX, GIVE, MOVE}) &&
VAL(0) == -VAL(1) && VAL(0) == -VAL(2)){
result.push_back({INCR, -1});
result.push_back({TAKE, VAL(0)});
i += 2;
continue;
}
if(pattern({ADVANCE, VALUE, VALUE, VALUE, VALUE, VALUE, VALUE, WHILEIM3}) &&
VAL(1) == -1 && VAL(3) ==1 && VAL(5) == 1 && (VAL(2) + VAL(4) + VAL(6) == 0)){
result.push_back({VALUE, VAL(2)});
result.push_back({VALUE, (short) (VAL(4)+VAL(2))});
result.push_back({WHILEDECROI2});
i += 7;
continue;
}
}
///////////////////////////////////////////////////
// All that follow are primitives for first pass.
//////////////////////////////////////////////////
{
// subtract value from current cell and add it to another cell
if(pattern({ FALSEJUMP, INCR, MOVE, INCR, MOVE, TRUEJUMP }) &&
VAL(1) == -1 && VAL(3) == 1 && VAL(2) == -VAL(4)){
result.push_back( { GIVE, VAL(2) } );
i += 5;
continue;
}
// alternate version decrements last
// subtract value from current cell and add it to another cell
if(pattern({ FALSEJUMP, MOVE, INCR, MOVE, INCR, TRUEJUMP }) &&
VAL(2) == 1 && VAL(4) == -1 && VAL(1) == -VAL(3)){
result.push_back( { GIVE, VAL(1) });
i += 5;
continue;
}
// [+>-<<<---->>>>>]
if(pattern({ FALSEJUMP, INCR, MOVE, INCR, MOVE, INCR, MOVE, TRUEJUMP})){
result.push_back({ ADVANCE, 6 });
for(int j=0; j<6; j++){
result.push_back({ VALUE, VAL(j+1) });
}
result.push_back({ WHILEIM3 });
i += 7;
continue;
}
// [+>-<]
if(pattern({ FALSEJUMP, INCR, MOVE, INCR, MOVE, TRUEJUMP})){
result.push_back({ ADVANCE, 4 });
for(int j=0; j<4; j++){
result.push_back({ VALUE, VAL(j+1) });
}
result.push_back({ WHILEIM2 });
i += 5;
continue;
}
// >>+++<<
if(pattern({ MOVE, INCR, MOVE}) && AT(0).val == -AT(2).val){
result.push_back({VALUE, VAL(0)});
result.push_back({OFFINCR, VAL(1)});
i+=2;
continue;
}
if(pattern({ INCR, MOVE}) && VAL(0) == -1){
result.push_back({ DECR1MX, VAL(1) });
i++;
continue;
}
if(pattern({ INCR, MOVE}) && VAL(0) == 1){
result.push_back({ INCR1MX, VAL(1) });
i++;
continue;
}
if(pattern({ MOVE, INCR }) && VAL(1) == 1){
result.push_back({ MXINCR1, VAL(0) });
i++;
continue;
}
if(pattern({ MOVE, INCR }) && VAL(1) == -1){
result.push_back({ MXDECR1, VAL(0) });
i++;
continue;
}
// [-]
if(pattern({ FALSEJUMP, INCR, TRUEJUMP })){
while(result.back().action == ZERO || result.back().action == INCR){
result.pop_back();
}
result.push_back({ ZERO });
i+=2;
continue;
}
// move while, e.g. [<<<]
if(pattern({ FALSEJUMP, MOVE, TRUEJUMP })){
result.push_back({ MOVEWHILE, VAL(1) });
i += 2;
continue;
}
}
result.push_back(instrs.at(i));
}
if(take_second_pass){
result = optimizations(result,false);
}
return result;
}
}