-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen2.h
224 lines (178 loc) · 4.41 KB
/
gen2.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
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sstream>
#include <string>
#include <list>
#include <utility>
#define ASSERT assert
using std::string;
typedef uint64_t Val;
enum {
NO_TOP_SHL1 = 0x01,
NO_TOP_SHR1 = 0x02,
NO_TOP_SHR4 = 0x04,
NO_TOP_SHR16 = 0x08
};
class Context
{
public:
Context(): count(0) {}
void push(Val val) { values[count++] = val; }
void pop() { ASSERT(count); --count; }
Val get(int id) { return values[id]; }
int count;
Val values[1000];
};
enum Op {
DUMMY_OP, // 0
FIRST_OP,
IF0 = FIRST_OP, // 1
FOLD, // 2
NOT,
SHL1, // 4
SHR1,
SHR4, // 6
SHR16,
AND, // 8
OR,
XOR, // 10
PLUS,
C0, // 12
C1, // 13
VAR, // 14
TFOLD,
MAX_OP
};
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
class OpSet
{
public:
OpSet() : set_(0) {}
void add(Op op) { set_ |= 1 << op; }
bool has(Op op) { return set_ & (1 << op); }
void del(Op op) { set_ &= ~(1 << op); }
int set_;
};
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
class Expr
{
public:
enum Flags {
F_CONST = 0x1,
F_IN_FOLD = 0x2
};
int arity();
static int arity(Op op);
string code();
string program();
bool is_var(int id) { return op == VAR && var == id; }
bool is_const() { return flags & F_CONST; }
bool is_const(Val x) { return is_const() && val == x; }
Val run(Val input);
Val eval(Context* ctx);
Val do_fold(Context* ctx);
Op op;
Expr* parent;
Expr* opnd[3];
int flags;
union {
Val val; // for const
int var; // if op is VAR
};
};
class Callback
{
public:
virtual ~Callback() {}
virtual bool action(Expr* e, int size) {};
};
class Arena : public Callback
{
public:
Arena();
void set_callback(Callback* c) { callback_ = c; }
void set_properties(int p) { properties_ = p; }
void generate(int size, int valence = 1, int args = 1);
void gen(int left_ops, int valence);
void try_emit(Op op, int left_ops, int valence);
void emit(Op op, int var = -1);
void emit_fold();
bool action(Expr* e, int size);
int push_op(Op op, int var = -1);
void pop_op();
Expr* peep_arg(int arg);
void allow_all();
void add_allowed_op(Op op);
virtual bool complete(Expr* e, int size);
Callback* callback_;
Expr* fold_lambda_;
int size_;
int num_vars_;
int count_;
int args_;
int bound_args_;
bool optimize_;
bool no_more_fold_;
int valence_;
bool done_;
int properties_;
OpSet allowed_ops_;
int valents[30];
int valents_ptr;
Expr arena[1000];
int arena_ptr;
};
class ArenaBonus : public Arena
{
public:
void generate(int size, int args = 1);
virtual bool complete(Expr* e, int size);
};
class ArenaTfold : public Arena
{
public:
void generate(int size, int args = 1);
virtual bool complete(Expr* e, int size);
};
class Printer : public Callback
{
public:
Printer() : count_(0) {}
bool action(Expr* e, int size);
int count_;
};
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
class Verifier: public Callback
{
public:
Verifier() : count(0) {}
void add(Val input, Val output);
virtual bool action(Expr* e, int size);
protected:
typedef std::list< std::pair<Val, Val> > Pairs;
Pairs pairs;
int count;
};
class Generator
{
public:
Generator() : callback_(NULL), mode_bonus_(false), mode_tfold_(false), properties_(0) {}
void set_callback(Callback* c) { callback_ = c; }
void generate(int size);
void set_properties(int p) { properties_ = p; }
void add_allowed_op(Op op) { allowed_ops_.add(op); }
bool mode_bonus_;
bool mode_tfold_;
OpSet allowed_ops_;
int properties_;
Callback* callback_;
};