-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen2.cc
531 lines (452 loc) · 12.4 KB
/
gen2.cc
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "gen2.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sstream>
#include <string>
#include <list>
#include <utility>
int Expr::arity(Op op)
{
switch (op)
{
case C0:
case C1:
case VAR:
return 0;
case NOT:
case SHL1:
case SHR1:
case SHR4:
case SHR16:
return 1;
case PLUS:
case XOR:
case OR:
case AND:
return 2;
case IF0:
case FOLD:
return 3;
default:
fprintf(stderr, "bad op at arity\n");
exit(1);
}
}
int Expr::arity()
{
return arity(op);
}
Val Expr::eval(Context* ctx)
{
if (flags & F_CONST)
return val;
Val res = 0;
switch (op) {
case C0: res = 0; break;
case C1: res = 1; break;
case VAR: res = ctx->get(var); break;
case NOT: res = ~opnd[0]->eval(ctx); break;
case SHL1: res = opnd[0]->eval(ctx) << 1; break;
case SHR1: res = opnd[0]->eval(ctx) >> 1; break;
case SHR4: res = opnd[0]->eval(ctx) >> 4; break;
case SHR16: res = opnd[0]->eval(ctx) >> 16; break;
case PLUS: res = opnd[0]->eval(ctx) + opnd[1]->eval(ctx); break;
case AND: res = opnd[0]->eval(ctx) & opnd[1]->eval(ctx); break;
case OR: res = opnd[0]->eval(ctx) | opnd[1]->eval(ctx); break;
case XOR: res = opnd[0]->eval(ctx) ^ opnd[1]->eval(ctx); break;
case IF0: res = opnd[0]->eval(ctx) == 0 ? opnd[1]->eval(ctx) : opnd[2]->eval(ctx); break;
case FOLD: res = do_fold(ctx); break;
default:
fprintf(stderr, "Error: Unknown op %d\n", op);
ASSERT(0);
}
// printf("eval: %d = 0x%016lx\n", op, res);
return res;
}
Val Expr::do_fold(Context* ctx)
{
Val data = opnd[0]->eval(ctx);
Val acc = opnd[1]->eval(ctx);
for (int i = 0; i < 8; i++) {
Val byte = data & 0xff;
ctx->push(byte);
ctx->push(acc);
acc = opnd[2]->eval(ctx);
ctx->pop();
ctx->pop();
data >>= 8;
}
return acc;
}
static string itos(int i) // convert int to string
{
std::stringstream s;
s << i;
return s.str();
}
string Expr::code()
{
switch (op) {
case C0: return "0";
case C1: return "1";
case VAR: return "x" + itos(var);
case NOT: return "(not " + opnd[0]->code() + ")";
case SHL1: return "(shl1 " + opnd[0]->code() + ")";
case SHR1: return "(shr1 " + opnd[0]->code() + ")";
case SHR4: return "(shr4 " + opnd[0]->code() + ")";
case SHR16: return "(shr16 " + opnd[0]->code() + ")";
case PLUS: return "(plus " + opnd[0]->code() + " " + opnd[1]->code() + ")";
case AND: return "(and " + opnd[0]->code() + " " + opnd[1]->code() + ")";
case OR: return "(or " + opnd[0]->code() + " " + opnd[1]->code() + ")";
case XOR: return "(xor " + opnd[0]->code() + " " + opnd[1]->code() + ")";
case IF0: return "(if0 " + opnd[0]->code() + " " + opnd[1]->code() + " " + opnd[2]->code() + ")";
// The code below assumes there's the only fold operation.
case FOLD: return "(fold " + opnd[0]->code() + " " + opnd[1]->code() +
" (lambda (x1 x2) " + opnd[2]->code() + "))";
default:
fprintf(stderr, "Error: Unknown op %d\n", op);
ASSERT(0);
}
}
string Expr::program()
{
return "(lambda (x0) " + code() + ")";
}
Val Expr::run(Val input)
{
Context ctx;
ctx.push(input);
return eval(&ctx);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
Arena::Arena()
{
// memset(arena, 0, sizeof(arena));
optimize_ = true;
callback_ = NULL;
no_more_fold_ = false;
}
void Arena::generate(int size, int valence, int args)
{
// printf("generate %d %d %d\n", size, valence, args);
count_ = 0;
optimize_ = true;
int min_size = valence + 1;
allowed_ops_.add(C0);
allowed_ops_.add(C1);
allowed_ops_.add(VAR);
done_ = false;
for (int sz = min_size; sz <= size; sz++) {
size_ = sz - 1;
// printf("current size %d\n", size_);
arena_ptr = 0;
valents_ptr = 0;
valence_ = valence;
num_vars_ = args;
gen(size_, 0);
if (done_)
break;
}
// printf("generated: %d\n", count_);
}
void Arena::gen(int left_ops, int valence)
{
// printf("gen %d %d\n", left_ops, valence);
int max_valence = valence_ + (left_ops - 1) * 2;
int min_valence = valence_ - (left_ops - 1);
if (allowed_ops_.has(IF0) && valence >= 3) {
Expr* cond_opnd = peep_arg(0);
if (!optimize_ || !(cond_opnd->flags & Expr::F_CONST))
try_emit(IF0, left_ops, valence);
}
// fold consumes at least 3 ops: fold, lambda, and its expr.
int fold_max_valence = valence_ + (left_ops - 3) * 2;
int fold_min_valence = valence_ - (left_ops - 3);
if (fold_min_valence <= valence - 1 && valence - 1 <= fold_max_valence && valence >= 2) {
emit_fold();
}
try_emit(C0, left_ops, valence);
try_emit(C1, left_ops, valence);
try_emit(VAR, left_ops, valence);
if (valence >= 1) {
Expr* opnd = peep_arg(0);
if (!optimize_ || opnd->op != NOT) {
try_emit(NOT, left_ops, valence);
}
// Do not shift 0
if (!optimize_ || !opnd->is_const(0)) {
try_emit(SHL1, left_ops, valence);
}
if (!optimize_ || !opnd->is_const(0) && !opnd->is_const(1)) {
try_emit(SHR1, left_ops, valence);
try_emit(SHR4, left_ops, valence);
try_emit(SHR16, left_ops, valence);
}
}
if (valence >= 2) {
Expr* opnd1 = peep_arg(0);
Expr* opnd2 = peep_arg(1);
if (!optimize_ || !opnd1->is_const(0) && !opnd2->is_const(0)) {
try_emit(PLUS, left_ops, valence);
try_emit(OR, left_ops, valence);
try_emit(XOR, left_ops, valence);
try_emit(AND, left_ops, valence);
}
}
}
Expr* Arena::peep_arg(int arg)
{
return &arena[valents[valents_ptr - arg - 1]];
}
void Arena::try_emit(Op op, int left_ops, int valence)
{
int max_valence = valence_ + (left_ops - 1) * 2;
int min_valence = valence_ - (left_ops - 1);
if (!allowed_ops_.has(op))
return;
if (left_ops == 1) {
if (op == SHL1 && (properties_ & NO_TOP_SHL1)) return;
if (op == SHR1 && (properties_ & NO_TOP_SHR1)) return;
if (op == SHR4 && (properties_ & NO_TOP_SHR4)) return;
if (op == SHR16 && (properties_ & NO_TOP_SHR16)) return;
}
int arity = Expr::arity(op);
// ensure we don't miss a fold if it's required
if (!no_more_fold_ && allowed_ops_.has(FOLD)) {
int new_valence = valence - arity + 1;
int new_left_ops = left_ops - 1;
// folds valency of 2
if (new_valence > 2)
new_left_ops -= allowed_ops_.has(IF0) ? (new_valence - 2) / 2 : new_valence - 2; // need to consume extra valence
else
new_left_ops += 2 - new_valence; // need to generate extra valence
if (new_left_ops < 3)
return; // no chance fold will fit after this op
}
if (min_valence <= valence - arity + 1 && valence - arity + 1 <= max_valence && valence >= arity) {
if (op == VAR) {
for (int i = 0; i < num_vars_; i++)
emit(VAR, i);
} else {
emit(op);
}
}
}
void Arena::emit(Op op, int var)
{
if (done_)
return;
int my_ptr = push_op(op, var);
Expr& e = arena[my_ptr];
// printf("arena: %d size:%d\n", arena_ptr, size_);
if (size_ == arena_ptr) {
done_ = complete(&e, size_ + 1);
} else {
gen(size_ - arena_ptr, valents_ptr);
}
pop_op();
}
bool Arena::complete(Expr* e, int size)
{
count_++;
return callback_ ? !callback_->action(e, size) : false;
}
int Arena::push_op(Op op, int var)
{
int my_ptr = arena_ptr++;
// printf("push_op %d -> [%d]: ", op, my_ptr);
Expr& e = arena[my_ptr];
memset(&e, 0, sizeof(Expr));
e.op = op;
e.flags = 0;
e.val = (unsigned long)-1;
if (op == VAR)
e.var = var;
bool const_expr = op != VAR && op != FOLD;
int arity = e.arity();
if (op == FOLD)
arity = 2; // special processing for the fold's lambda
for (int i = 0; i < arity; i++) {
ASSERT(valents_ptr > 0);
int opnd_index = valents[--valents_ptr];
Expr& opnd = arena[opnd_index];
e.opnd[i] = &opnd;
opnd.parent = &e;
const_expr = const_expr && (opnd.flags & Expr::F_CONST);
}
if (op == FOLD) {
e.opnd[2] = fold_lambda_;
fold_lambda_->parent = &e;
}
if (const_expr) {
if (op == FOLD) {
Context ctx;
e.val = e.eval(&ctx);
} else {
// no context as it shouldn't reference any var
// things like (plus x 0) should no appear in the code as well.
e.val = e.eval(NULL);
}
// eval before setting the flag, otherwise it won't really do eval.
e.flags |= Expr::F_CONST;
}
valents[valents_ptr++] = my_ptr;
// printf("curried into: %s\n", e.code().c_str());
// printf("\tvalents after push_op %d\n", valents_ptr);
return my_ptr;
}
void Arena::pop_op()
{
arena_ptr--;
// printf("pop_op [%d]\n", arena_ptr);
int my_ptr = arena_ptr;
Expr& e = arena[my_ptr];
valents_ptr--;
int arity = e.arity();
if (e.op == FOLD)
arity = 2; // special processing for the fold's lambda
for (int i = arity - 1; i >= 0; i--) {
valents[valents_ptr++] = e.opnd[i] - arena;
}
}
void Arena::emit_fold()
{
if (no_more_fold_)
return;
if (!allowed_ops_.has(FOLD))
return;
no_more_fold_ = true;
int max_size = size_ - arena_ptr - 1; // 1 takes FOLD
Arena fold_lambda;
fold_lambda.set_callback(this);
fold_lambda.no_more_fold_ = true; // disable inner folds
fold_lambda.allowed_ops_ = allowed_ops_;
fold_lambda.generate(max_size, 1, 3);
no_more_fold_ = false;
}
bool Arena::action(Expr* expr, int size)
{
if (optimize_ && (expr->is_const() || expr->is_var(0)))
return true;
arena_ptr += size;
fold_lambda_ = expr;
emit(FOLD);
arena_ptr -= size;
return true;
}
void Arena::add_allowed_op(Op op)
{
allowed_ops_.add(op);
}
/////////////////////////////////////////////////
void ArenaBonus::generate(int size, int args)
{
no_more_fold_ = true;
Arena::generate(size - 3, 3, 1);
}
bool ArenaBonus::complete(Expr* e, int size)
{
push_op(C1);
push_op(AND);
int op_ptr = push_op(IF0);
bool res = Arena::complete(&arena[op_ptr], size + 3);
pop_op();
pop_op();
pop_op();
return res;
}
///////////////////////
void ArenaTfold::generate(int size, int args)
{
no_more_fold_ = true;
Arena::generate(size - 4, 1, 3);
}
bool ArenaTfold::complete(Expr* e, int size)
{
fold_lambda_ = e;
push_op(C0);
push_op(VAR, 0);
int op_ptr = push_op(FOLD);
bool res = Arena::complete(&arena[op_ptr], size + 4);
pop_op();
pop_op();
pop_op();
return res;
}
bool Printer::action(Expr* e, int size)
{
count_++;
static int cnt = 0;
cnt++;
#ifdef GEN2
printf("%9d: [%2d] %s\n", cnt, size, e->program().c_str());
#else
if ((cnt & 0x3fffff) == 0) printf("%9d: [%2d] %s\n", cnt, size, e->program().c_str());
#endif
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
void Verifier::add(Val input, Val output)
{
pairs.push_back(std::pair<Val,Val>(input, output));
}
bool Verifier::action(Expr* program, int size)
{
for (Pairs::iterator it = pairs.begin(); it != pairs.end(); ++it) {
Val actual = program->run((*it).first);
if (actual != (*it).second) {
// printf("%s failed 0x%lx -> 0x%lx\n", program->program().c_str(), (*it).first, actual);
return true;
}
}
printf("--- %6d: %s\n", ++count, program->program().c_str());
#if 0
for (Pairs::iterator it = pairs.begin(); it != pairs.end(); ++it) {
printf(" 0x%016lx -> 0x%016lx\n", (*it).first, (*it).second);
}
#endif
return false;
}
void Generator::generate(int size)
{
if (mode_tfold_) {
ArenaTfold a;
a.set_callback(callback_);
a.allowed_ops_ = allowed_ops_;
a.generate(size);
printf("count=%d\n", a.count_);
} else if (mode_bonus_) {
ArenaBonus a;
a.set_callback(callback_);
a.allowed_ops_ = allowed_ops_;
a.generate(size);
printf("count=%d\n", a.count_);
} else {
Arena a;
a.set_callback(callback_);
a.set_properties(properties_);
a.allowed_ops_ = allowed_ops_;
a.generate(size);
printf("count=%d\n", a.count_);
}
}
#ifdef GEN2
int main()
{
Printer p;
Arena a;
a.set_callback(&p);
a.add_allowed_op(IF0);
a.add_allowed_op(FOLD);
a.generate(GEN2);
printf("Total: %d\n", p.count_);
return 0;
}
#endif