/
mam.cpp
4031 lines (3657 loc) · 164 KB
/
mam.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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) 2006 Microsoft Corporation
Module Name:
mam.cpp
Abstract:
Matching Abstract Machine
Author:
Leonardo de Moura (leonardo) 2007-02-13.
Revision History:
--*/
#include <algorithm>
#include "util/pool.h"
#include "util/trail.h"
#include "util/stopwatch.h"
#include "ast/ast_pp.h"
#include "ast/ast_ll_pp.h"
#include "ast/ast_smt2_pp.h"
#include "smt/mam.h"
#include "smt/smt_context.h"
using namespace smt;
// #define _PROFILE_MAM
// -----------------------------------------
// Flags for _PROFILE_MAM
//
// send profiling information to stdout
#define _PROFILE_MAM_TO_STDOUT
// threshold in secs for being considered expensive
#define _PROFILE_MAM_THRESHOLD 30.0
// dump expensive (> _PROFILE_MAM_THRESHOLD) code trees whenever execute_core is executed.
#define _PROFILE_MAM_EXPENSIVE
//
#define _PROFILE_MAM_EXPENSIVE_FREQ 100000
//
// -----------------------------------------
// #define _PROFILE_PATH_TREE
// -----------------------------------------
// Flags for _PROFILE_PATH_TREE
//
#define _PROFILE_PATH_TREE_THRESHOLD 20000
//
// -----------------------------------------
#define IS_CGR_SUPPORT true
namespace {
class mam_impl;
// ------------------------------------
//
// Auxiliary
//
// ------------------------------------
class label_hasher {
svector<signed char> m_lbl2hash; // cache: lbl_id -> hash
void mk_lbl_hash(unsigned lbl_id) {
unsigned a = 17;
unsigned b = 3;
unsigned c = lbl_id;
mix(a, b, c);
m_lbl2hash[lbl_id] = c & (APPROX_SET_CAPACITY - 1);
}
public:
unsigned char operator()(func_decl * lbl) {
unsigned lbl_id = lbl->get_small_id();
if (lbl_id >= m_lbl2hash.size())
m_lbl2hash.resize(lbl_id + 1, -1);
if (m_lbl2hash[lbl_id] == -1) {
mk_lbl_hash(lbl_id);
}
SASSERT(m_lbl2hash[lbl_id] >= 0);
return m_lbl2hash[lbl_id];
}
void display(std::ostream & out) const {
out << "lbl-hasher:\n";
bool first = true;
for (unsigned i = 0; i < m_lbl2hash.size(); i++) {
if (m_lbl2hash[i] != -1) {
if (first)
first = false;
else
out << ", ";
out << i << " -> " << static_cast<int>(m_lbl2hash[i]);
}
}
out << "\n";
}
};
// ------------------------------------
//
// Instructions
//
// ------------------------------------
typedef enum {
INIT1=0, INIT2, INIT3, INIT4, INIT5, INIT6, INITN,
BIND1, BIND2, BIND3, BIND4, BIND5, BIND6, BINDN,
YIELD1, YIELD2, YIELD3, YIELD4, YIELD5, YIELD6, YIELDN,
COMPARE, CHECK, FILTER, CFILTER, PFILTER, CHOOSE, NOOP, CONTINUE,
GET_ENODE,
GET_CGR1, GET_CGR2, GET_CGR3, GET_CGR4, GET_CGR5, GET_CGR6, GET_CGRN,
IS_CGR
} opcode;
struct instruction {
opcode m_opcode;
instruction* m_next = nullptr;
#ifdef _PROFILE_MAM
unsigned m_counter; // how often it was executed
#endif
bool is_init() const {
return m_opcode >= INIT1 && m_opcode <= INITN;
}
};
struct initn : public instruction {
// We need that because starting at Z3 3.0, some associative
// operators (e.g., + and *) are represented using n-ary
// applications.
// We do not need the extra field for INIT1, ..., INIT6.
unsigned m_num_args;
};
struct compare : public instruction {
unsigned m_reg1;
unsigned m_reg2;
};
struct check : public instruction {
unsigned m_reg;
enode * m_enode;
};
struct filter : public instruction {
unsigned m_reg;
approx_set m_lbl_set;
};
struct pcheck : public instruction {
enode * m_enode;
approx_set m_lbl_set;
};
/**
\brief Copy m_enode to register m_oreg
*/
struct get_enode_instr : public instruction {
unsigned m_oreg;
enode * m_enode;
};
struct choose: public instruction {
choose * m_alt;
};
/**
\brief A depth-2 joint. It is used in CONTINUE instruction.
There are 3 forms of joints
1) Variables: (f ... X ...)
2) Ground terms: (f ... t ...)
3) depth 2 joint: (f ... (g ... X ...) ...)
Joint2 stores the declaration g, and the position of variable X, and its idx.
\remark Z3 has no support for depth 3 joints (f ... (g ... (h ... X ...) ...) ....)
*/
struct joint2 {
func_decl * m_decl;
unsigned m_arg_pos;
unsigned m_reg; // register that contains the variable
joint2(func_decl * f, unsigned pos, unsigned r):m_decl(f), m_arg_pos(pos), m_reg(r) {}
};
#define NULL_TAG 0
#define GROUND_TERM_TAG 1
#define VAR_TAG 2
#define NESTED_VAR_TAG 3
struct cont: public instruction {
func_decl * m_label;
unsigned short m_num_args;
unsigned m_oreg;
approx_set m_lbl_set; // singleton set containing m_label
/*
The following field is an array of tagged pointers.
Each position contains:
1- null (no joint), NULL_TAG
2- a boxed integer (i.e., register that contains the variable bind) VAR_TAG
3- an enode pointer (ground term) GROUND_TERM_TAG
4- or, a joint2 pointer. NESTED_VAR_TAG
The size of the array is m_num_args.
*/
enode * m_joints[0];
};
struct bind : public instruction {
func_decl * m_label;
unsigned short m_num_args;
unsigned m_ireg;
unsigned m_oreg;
};
struct get_cgr : public instruction {
func_decl * m_label;
approx_set m_lbl_set;
unsigned short m_num_args;
unsigned m_oreg;
unsigned m_iregs[0];
};
struct yield : public instruction {
quantifier * m_qa;
app * m_pat;
unsigned short m_num_bindings;
unsigned m_bindings[0];
};
struct is_cgr : public instruction {
unsigned m_ireg;
func_decl * m_label;
unsigned short m_num_args;
unsigned m_iregs[0];
};
void display_num_args(std::ostream & out, unsigned num_args) {
if (num_args <= 6) {
out << num_args;
}
else {
out << "N";
}
}
void display_bind(std::ostream & out, const bind & b) {
out << "(BIND";
display_num_args(out, b.m_num_args);
out << " " << b.m_label->get_name() << " " << b.m_ireg << " " << b.m_oreg << ")";
}
void display_get_cgr(std::ostream & out, const get_cgr & c) {
out << "(GET_CGR";
display_num_args(out, c.m_num_args);
out << " " << c.m_label->get_name() << " " << c.m_oreg;
for (unsigned i = 0; i < c.m_num_args; i++)
out << " " << c.m_iregs[i];
out << ")";
}
void display_is_cgr(std::ostream & out, const is_cgr & c) {
out << "(IS_CGR " << c.m_label->get_name() << " " << c.m_ireg;
for (unsigned i = 0; i < c.m_num_args; i++)
out << " " << c.m_iregs[i];
out << ")";
}
void display_yield(std::ostream & out, const yield & y) {
out << "(YIELD";
display_num_args(out, y.m_num_bindings);
out << " #" << y.m_qa->get_id();
for (unsigned i = 0; i < y.m_num_bindings; i++) {
out << " " << y.m_bindings[i];
}
out << ")";
}
void display_joints(std::ostream & out, unsigned num_joints, enode * const * joints) {
for (unsigned i = 0; i < num_joints; i++) {
if (i > 0)
out << " ";
enode * bare = joints[i];
switch (GET_TAG(bare)) {
case NULL_TAG: out << "nil"; break;
case GROUND_TERM_TAG: out << "#" << UNTAG(enode*, bare)->get_owner_id(); break;
case VAR_TAG: out << UNBOXINT(bare); break;
case NESTED_VAR_TAG: out << "(" << UNTAG(joint2*, bare)->m_decl->get_name() << " " << UNTAG(joint2*, bare)->m_arg_pos << " " << UNTAG(joint2*, bare)->m_reg << ")"; break;
}
}
}
void display_continue(std::ostream & out, const cont & c) {
out << "(CONTINUE " << c.m_label->get_name() << " " << c.m_num_args << " " << c.m_oreg << " "
<< c.m_lbl_set << " (";
display_joints(out, c.m_num_args, c.m_joints);
out << "))";
}
void display_filter(std::ostream & out, char const * op, filter const & instr) {
out << "(" << op << " " << instr.m_reg
<< " " << instr.m_lbl_set << ")";
}
std::ostream & operator<<(std::ostream & out, const instruction & instr) {
switch (instr.m_opcode) {
case INIT1: case INIT2: case INIT3: case INIT4: case INIT5: case INIT6: case INITN:
out << "(INIT";
if (instr.m_opcode <= INIT6)
out << (instr.m_opcode - INIT1 + 1);
else
out << "N";
out << ")";
break;
case BIND1: case BIND2: case BIND3: case BIND4: case BIND5: case BIND6: case BINDN:
display_bind(out, static_cast<const bind &>(instr));
break;
case GET_CGR1: case GET_CGR2: case GET_CGR3: case GET_CGR4: case GET_CGR5: case GET_CGR6: case GET_CGRN:
display_get_cgr(out, static_cast<const get_cgr &>(instr));
break;
case IS_CGR:
display_is_cgr(out, static_cast<const is_cgr &>(instr));
break;
case YIELD1: case YIELD2: case YIELD3: case YIELD4: case YIELD5: case YIELD6: case YIELDN:
display_yield(out, static_cast<const yield &>(instr));
break;
case CONTINUE:
display_continue(out, static_cast<const cont &>(instr));
break;
case COMPARE:
out << "(COMPARE " << static_cast<const compare &>(instr).m_reg1 << " "
<< static_cast<const compare &>(instr).m_reg2 << ")";
break;
case CHECK:
out << "(CHECK " << static_cast<const check &>(instr).m_reg
<< " #" << static_cast<const check &>(instr).m_enode->get_owner_id() << ")";
break;
case FILTER:
display_filter(out, "FILTER", static_cast<const filter &>(instr));
break;
case CFILTER:
display_filter(out, "CFILTER", static_cast<const filter &>(instr));
break;
case PFILTER:
display_filter(out, "PFILTER", static_cast<const filter &>(instr));
break;
case GET_ENODE:
out << "(GET_ENODE " << static_cast<const get_enode_instr &>(instr).m_oreg << " #" <<
static_cast<const get_enode_instr &>(instr).m_enode->get_owner_id() << ")";
break;
case CHOOSE:
out << "(CHOOSE)";
break;
case NOOP:
out << "(NOOP)";
break;
}
#ifdef _PROFILE_MAM
out << "[" << instr.m_counter << "]";
#endif
return out;
}
// ------------------------------------
//
// Code Tree
//
// ------------------------------------
inline enode * mk_enode(context & ctx, quantifier * qa, app * n) {
ctx.internalize(n, false, ctx.get_generation(qa));
enode * e = ctx.get_enode(n);
SASSERT(e);
return e;
}
class code_tree {
label_hasher & m_lbl_hasher;
func_decl * m_root_lbl;
unsigned m_num_args; //!< we need this information to avoid the nary *,+ crash bug
bool m_filter_candidates;
unsigned m_num_regs;
unsigned m_num_choices;
instruction * m_root;
enode_vector m_candidates;
#ifdef Z3DEBUG
context * m_context;
ptr_vector<app> m_patterns;
#endif
#ifdef _PROFILE_MAM
stopwatch m_watch;
unsigned m_counter;
#endif
friend class compiler;
friend class code_tree_manager;
void display_seq(std::ostream & out, instruction * head, unsigned indent) const {
for (unsigned i = 0; i < indent; i++) {
out << " ";
}
instruction * curr = head;
out << *curr;
curr = curr->m_next;
while (curr != nullptr && curr->m_opcode != CHOOSE && curr->m_opcode != NOOP) {
out << "\n";
out << *curr;
curr = curr->m_next;
}
out << "\n";
if (curr != nullptr) {
display_children(out, static_cast<choose*>(curr), indent + 1);
}
}
void display_children(std::ostream & out, choose * first_child, unsigned indent) const {
choose * curr = first_child;
while (curr != nullptr) {
display_seq(out, curr, indent);
curr = curr->m_alt;
}
}
#ifdef Z3DEBUG
inline enode * get_enode(context & ctx, app * n) const {
SASSERT(ctx.e_internalized(n));
enode * e = ctx.get_enode(n);
SASSERT(e);
return e;
}
void display_label_hashes_core(std::ostream & out, app * p) const {
if (p->is_ground()) {
enode * e = get_enode(*m_context, p);
SASSERT(e->has_lbl_hash());
out << "#" << e->get_expr_id() << ":" << e->get_lbl_hash() << " ";
}
else {
out << p->get_decl()->get_name() << ":" << m_lbl_hasher(p->get_decl()) << " ";
for (unsigned i = 0; i < p->get_num_args(); i++) {
expr * arg = p->get_arg(i);
if (is_app(arg))
display_label_hashes(out, to_app(arg));
}
}
}
void display_label_hashes(std::ostream & out, app * p) const {
ast_manager & m = m_context->get_manager();
if (m.is_pattern(p)) {
for (unsigned i = 0; i < p->get_num_args(); i++) {
expr * arg = p->get_arg(i);
if (is_app(arg)) {
display_label_hashes_core(out, to_app(arg));
out << "\n";
}
}
}
else {
display_label_hashes_core(out, p);
out << "\n";
}
}
#endif
public:
code_tree(label_hasher & h, func_decl * lbl, unsigned short num_args, bool filter_candidates):
m_lbl_hasher(h),
m_root_lbl(lbl),
m_num_args(num_args),
m_filter_candidates(filter_candidates),
m_num_regs(num_args + 1),
m_num_choices(0),
m_root(nullptr) {
DEBUG_CODE(m_context = 0;);
#ifdef _PROFILE_MAM
m_counter = 0;
#endif
(void)m_lbl_hasher;
}
#ifdef _PROFILE_MAM
~code_tree() {
#ifdef _PROFILE_MAM_TO_STDOUT
std::cout << "killing code tree for: " << m_root_lbl->get_name() << " " << static_cast<unsigned long long>(m_watch.get_seconds() * 1000) << "\n"; display(std::cout);
#endif
}
stopwatch & get_watch() {
return m_watch;
}
void inc_counter() {
m_counter++;
}
unsigned get_counter() const {
return m_counter;
}
#endif
unsigned expected_num_args() const {
return m_num_args;
}
unsigned get_num_regs() const {
return m_num_regs;
}
unsigned get_num_choices() const {
return m_num_choices;
}
func_decl * get_root_lbl() const {
return m_root_lbl;
}
bool filter_candidates() const {
return m_filter_candidates;
}
const instruction * get_root() const {
return m_root;
}
void add_candidate(enode * n) {
m_candidates.push_back(n);
}
bool has_candidates() const {
return !m_candidates.empty();
}
void reset_candidates() {
m_candidates.reset();
}
enode_vector const & get_candidates() const {
return m_candidates;
}
#ifdef Z3DEBUG
void set_context(context * ctx) {
SASSERT(m_context == 0);
m_context = ctx;
}
ptr_vector<app> & get_patterns() {
return m_patterns;
}
#endif
void display(std::ostream & out) const {
#ifdef Z3DEBUG
if (m_context) {
ast_manager & m = m_context->get_manager();
out << "patterns:\n";
for (app* a : m_patterns)
out << mk_pp(a, m) << "\n";
}
#endif
out << "function: " << m_root_lbl->get_name();
#ifdef _PROFILE_MAM
out << " " << m_watch.get_seconds() << " secs, [" << m_counter << "]";
#endif
out << "\n";
out << "num. regs: " << m_num_regs << "\n"
<< "num. choices: " << m_num_choices << "\n";
display_seq(out, m_root, 0);
}
};
#ifdef _TRACE
std::ostream & operator<<(std::ostream & out, code_tree const & tree) {
tree.display(out);
return out;
}
#endif
// ------------------------------------
//
// Code Tree Manager
//
// ------------------------------------
class code_tree_manager {
label_hasher & m_lbl_hasher;
trail_stack & m_trail_stack;
region & m_region;
template<typename OP>
OP * mk_instr(opcode op, unsigned size) {
void * mem = m_region.allocate(size);
OP * r = new (mem) OP;
r->m_opcode = op;
r->m_next = nullptr;
#ifdef _PROFILE_MAM
r->m_counter = 0;
#endif
return r;
}
instruction * mk_init(unsigned n) {
SASSERT(n >= 1);
opcode op = n <= 6 ? static_cast<opcode>(INIT1 + n - 1) : INITN;
if (op == INITN) {
// We store the actual number of arguments for INITN.
// Starting at Z3 3.0, some associative operators
// (e.g., + and *) are represented using n-ary
// applications.
initn * r = mk_instr<initn>(op, sizeof(initn));
r->m_num_args = n;
return r;
}
else {
return mk_instr<instruction>(op, sizeof(instruction));
}
}
public:
code_tree_manager(label_hasher & h, trail_stack & s):
m_lbl_hasher(h),
m_trail_stack(s),
m_region(s.get_region()) {
}
code_tree * mk_code_tree(func_decl * lbl, unsigned short num_args, bool filter_candidates) {
code_tree * r = alloc(code_tree,m_lbl_hasher, lbl, num_args, filter_candidates);
r->m_root = mk_init(num_args);
return r;
}
joint2 * mk_joint2(func_decl * f, unsigned pos, unsigned reg) {
return new (m_region) joint2(f, pos, reg);
}
compare * mk_compare(unsigned reg1, unsigned reg2) {
compare * r = mk_instr<compare>(COMPARE, sizeof(compare));
r->m_reg1 = reg1;
r->m_reg2 = reg2;
return r;
}
check * mk_check(unsigned reg, enode * n) {
check * r = mk_instr<check>(CHECK, sizeof(check));
r->m_reg = reg;
r->m_enode = n;
return r;
}
filter * mk_filter_core(opcode op, unsigned reg, approx_set s) {
filter * r = mk_instr<filter>(op, sizeof(filter));
r->m_reg = reg;
r->m_lbl_set = s;
return r;
}
filter * mk_filter(unsigned reg, approx_set s) {
return mk_filter_core(FILTER, reg, s);
}
filter * mk_pfilter(unsigned reg, approx_set s) {
return mk_filter_core(PFILTER, reg, s);
}
filter * mk_cfilter(unsigned reg, approx_set s) {
return mk_filter_core(CFILTER, reg, s);
}
get_enode_instr * mk_get_enode(unsigned reg, enode * n) {
get_enode_instr * s = mk_instr<get_enode_instr>(GET_ENODE, sizeof(get_enode_instr));
s->m_oreg = reg;
s->m_enode = n;
return s;
}
choose * mk_choose(choose * alt) {
choose * r = mk_instr<choose>(CHOOSE, sizeof(choose));
r->m_alt = alt;
return r;
}
choose * mk_noop() {
choose * r = mk_instr<choose>(NOOP, sizeof(choose));
r->m_alt = nullptr;
return r;
}
bind * mk_bind(func_decl * lbl, unsigned short num_args, unsigned ireg, unsigned oreg) {
SASSERT(num_args >= 1);
opcode op = num_args <= 6 ? static_cast<opcode>(BIND1 + num_args - 1) : BINDN;
bind * r = mk_instr<bind>(op, sizeof(bind));
r->m_label = lbl;
r->m_num_args = num_args;
r->m_ireg = ireg;
r->m_oreg = oreg;
return r;
}
get_cgr * mk_get_cgr(func_decl * lbl, unsigned oreg, unsigned num_args, unsigned const * iregs) {
SASSERT(num_args >= 1);
opcode op = num_args <= 6 ? static_cast<opcode>(GET_CGR1 + num_args - 1) : GET_CGRN;
get_cgr * r = mk_instr<get_cgr>(op, sizeof(get_cgr) + num_args * sizeof(unsigned));
r->m_label = lbl;
r->m_lbl_set.insert(m_lbl_hasher(lbl));
r->m_oreg = oreg;
r->m_num_args = num_args;
memcpy(r->m_iregs, iregs, sizeof(unsigned) * num_args);
return r;
}
is_cgr * mk_is_cgr(func_decl * lbl, unsigned ireg, unsigned num_args, unsigned const * iregs) {
SASSERT(num_args >= 1);
is_cgr * r = mk_instr<is_cgr>(IS_CGR, sizeof(is_cgr) + num_args * sizeof(unsigned));
r->m_label = lbl;
r->m_ireg = ireg;
r->m_num_args = num_args;
memcpy(r->m_iregs, iregs, sizeof(unsigned) * num_args);
return r;
}
yield * mk_yield(quantifier * qa, app * pat, unsigned num_bindings, unsigned * bindings) {
SASSERT(num_bindings >= 1);
opcode op = num_bindings <= 6 ? static_cast<opcode>(YIELD1 + num_bindings - 1) : YIELDN;
yield * y = mk_instr<yield>(op, sizeof(yield) + num_bindings * sizeof(unsigned));
y->m_qa = qa;
y->m_pat = pat;
y->m_num_bindings = num_bindings;
memcpy(y->m_bindings, bindings, sizeof(unsigned) * num_bindings);
return y;
}
cont * mk_cont(func_decl * lbl, unsigned short num_args, unsigned oreg,
approx_set const & s, enode * const * joints) {
SASSERT(num_args >= 1);
cont * r = mk_instr<cont>(CONTINUE, sizeof(cont) + num_args * sizeof(enode*));
r->m_label = lbl;
r->m_num_args = num_args;
r->m_oreg = oreg;
r->m_lbl_set = s;
memcpy(r->m_joints, joints, num_args * sizeof(enode *));
return r;
}
void set_next(instruction * instr, instruction * new_next) {
m_trail_stack.push(value_trail<instruction*>(instr->m_next));
instr->m_next = new_next;
}
void save_num_regs(code_tree * tree) {
m_trail_stack.push(value_trail<unsigned>(tree->m_num_regs));
}
void save_num_choices(code_tree * tree) {
m_trail_stack.push(value_trail<unsigned>(tree->m_num_choices));
}
void insert_new_lbl_hash(filter * instr, unsigned h) {
m_trail_stack.push(value_trail<approx_set>(instr->m_lbl_set));
instr->m_lbl_set.insert(h);
}
};
// ------------------------------------
//
// Compiler: Pattern ---> Code Tree
//
// ------------------------------------
class compiler {
context & m_context;
ast_manager & m;
code_tree_manager & m_ct_manager;
label_hasher & m_lbl_hasher;
bool m_use_filters;
ptr_vector<expr> m_registers;
unsigned_vector m_todo; // list of registers that have patterns to be processed.
unsigned_vector m_aux;
int_vector m_vars; // -1 the variable is unbound, >= 0 is the register that contains the variable
quantifier * m_qa;
app * m_mp;
code_tree * m_tree;
unsigned m_num_choices;
bool m_is_tmp_tree;
bool_vector m_mp_already_processed;
obj_map<expr, unsigned> m_matched_exprs;
struct pcheck_checked {
func_decl * m_label;
enode * m_enode;
};
typedef enum { NOT_CHECKED,
CHECK_SET,
CHECK_SINGLETON } check_mark;
svector<check_mark> m_mark;
unsigned_vector m_to_reset;
ptr_vector<instruction> m_compatible;
ptr_vector<instruction> m_incompatible;
ptr_vector<instruction> m_seq;
void set_register(unsigned reg, expr * p) {
m_registers.setx(reg, p, nullptr);
}
check_mark get_check_mark(unsigned reg) const {
return m_mark.get(reg, NOT_CHECKED);
}
void set_check_mark(unsigned reg, check_mark cm) {
m_mark.setx(reg, cm, NOT_CHECKED);
}
void init(code_tree * t, quantifier * qa, app * mp, unsigned first_idx) {
SASSERT(m.is_pattern(mp));
#ifdef Z3DEBUG
for (auto cm : m_mark) {
SASSERT(cm == NOT_CHECKED);
}
#endif
m_tree = t;
m_qa = qa;
m_mp = mp;
m_num_choices = 0;
m_todo.reset();
m_registers.fill(0);
app * p = to_app(mp->get_arg(first_idx));
SASSERT(t->get_root_lbl() == p->get_decl());
unsigned num_args = p->get_num_args();
for (unsigned i = 0; i < num_args; i++) {
set_register(i+1, p->get_arg(i));
m_todo.push_back(i+1);
}
unsigned num_decls = m_qa->get_num_decls();
if (num_decls > m_vars.size()) {
m_vars.resize(num_decls, -1);
}
for (unsigned j = 0; j < num_decls; j++) {
m_vars[j] = -1;
}
}
/**
\brief Return true if all arguments of n are bound variables.
That is, during execution time, the variables will be already bound
*/
bool all_args_are_bound_vars(app * n) {
for (expr* arg : *n) {
if (!is_var(arg))
return false;
if (m_vars[to_var(arg)->get_idx()] == -1)
return false;
}
return true;
}
/**
\see get_stats
*/
void get_stats_core(app * n, unsigned & sz, unsigned & num_unbound_vars) {
sz++;
if (n->is_ground()) {
return;
}
for (expr* arg : *n) {
if (is_var(arg)) {
sz++;
unsigned var_id = to_var(arg)->get_idx();
if (m_vars[var_id] == -1)
num_unbound_vars++;
}
else if (is_app(arg)) {
get_stats_core(to_app(arg), sz, num_unbound_vars);
}
}
}
/**
\brief Return statistics for the given pattern
\remark Patterns are small. So, it doesn't hurt to use a recursive function.
*/
void get_stats(app * n, unsigned & sz, unsigned & num_unbound_vars) {
sz = 0;
num_unbound_vars = 0;
get_stats_core(n, sz, num_unbound_vars);
}
/**
\brief Process registers in m_todo. The registers in m_todo
that produce non-BIND operations are processed first. Then,
a single BIND operation b is produced.
After executing this method m_todo will contain the
registers in m_todo that produce BIND operations and were
not processed, and the registers generated when the
operation b was produced.
\remark The new operations are appended to m_seq.
*/
void linearise_core() {
m_aux.reset();
app * first_app = nullptr;
unsigned first_app_reg;
unsigned first_app_sz;
unsigned first_app_num_unbound_vars;
// generate first the non-BIND operations
for (unsigned reg : m_todo) {
expr * p = m_registers[reg];
SASSERT(!is_quantifier(p));
TRACE("mam", tout << "lin: " << reg << " " << get_check_mark(reg) << " " << is_var(p) << "\n";);
if (is_var(p)) {
unsigned var_id = to_var(p)->get_idx();
if (m_vars[var_id] != -1)
m_seq.push_back(m_ct_manager.mk_compare(m_vars[var_id], reg));
else
m_vars[var_id] = reg;
continue;
}
SASSERT(is_app(p));
if (to_app(p)->is_ground()) {
// ground applications are viewed as constants, and eagerly
// converted into enodes.
enode * e = mk_enode(m_context, m_qa, to_app(p));
m_seq.push_back(m_ct_manager.mk_check(reg, e));
set_check_mark(reg, NOT_CHECKED); // reset mark, register was fully processed.
continue;
}
unsigned matched_reg;
if (m_matched_exprs.find(p, matched_reg) && reg != matched_reg) {
m_seq.push_back(m_ct_manager.mk_compare(matched_reg, reg));
set_check_mark(reg, NOT_CHECKED); // reset mark, register was fully processed.
continue;
}
m_matched_exprs.insert(p, reg);
if (m_use_filters && get_check_mark(reg) != CHECK_SINGLETON) {
func_decl * lbl = to_app(p)->get_decl();
approx_set s(m_lbl_hasher(lbl));
m_seq.push_back(m_ct_manager.mk_filter(reg, s));
set_check_mark(reg, CHECK_SINGLETON);
}
if (first_app) {
#if 0
m_aux.push_back(reg);
#else
// Try to select the best first_app
if (first_app_num_unbound_vars == 0) {
// first_app doesn't have free vars... so it is the best choice...
m_aux.push_back(reg);
}
else {
unsigned sz;
unsigned num_unbound_vars;
get_stats(to_app(p), sz, num_unbound_vars);
if (num_unbound_vars == 0 ||
sz > first_app_sz ||
(sz == first_app_sz && num_unbound_vars < first_app_num_unbound_vars)) {
// change the first_app
m_aux.push_back(first_app_reg);
first_app = to_app(p);
first_app_reg = reg;
first_app_sz = sz;
first_app_num_unbound_vars = num_unbound_vars;
}
else {
m_aux.push_back(reg);
}
}
#endif
}
else {
first_app = to_app(p);
first_app_reg = reg;
get_stats(first_app, first_app_sz, first_app_num_unbound_vars);
}
}
if (first_app) {
// m_todo contains at least one (non-ground) application.
func_decl * lbl = first_app->get_decl();
unsigned short num_args = first_app->get_num_args();
if (IS_CGR_SUPPORT && all_args_are_bound_vars(first_app)) {
// use IS_CGR instead of BIND
sbuffer<unsigned> iregs;
for (unsigned i = 0; i < num_args; i++) {
expr * arg = to_app(first_app)->get_arg(i);
SASSERT(is_var(arg));
SASSERT(m_vars[to_var(arg)->get_idx()] != -1);
iregs.push_back(m_vars[to_var(arg)->get_idx()]);