-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
user_func.c
1350 lines (1216 loc) · 37.3 KB
/
user_func.c
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 2012-2020 Dustin DeWeese
This file is part of PoprC.
PoprC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PoprC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PoprC. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rt_types.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "startle/error.h"
#include "startle/test.h"
#include "startle/support.h"
#include "startle/log.h"
#include "cells.h"
#include "rt.h"
#include "special.h"
#include "ir/compile.h"
#include "ir/trace.h"
#include "list.h"
#include "user_func-local.h"
#include "debug/print.h"
#include "parse/parse.h" // for string_printf
#include "debug/tags.h"
#include "var.h"
#include "ir/analysis.h"
#include "primitive/core.h"
#include "parameters.h"
// hash_entry() of `[id] map`
#define ID_MAP_HASH 0xee5107e7
#if INTERFACE
#define is_user_func(c) _is_user_func(GET_CELL(c))
#endif
bool _is_user_func(const cell_t *c) {
return c->op == OP_exec;
}
tcell_t *closure_entry(cell_t *c) {
return is_user_func(c) ?
(tcell_t *)c->expr.arg[closure_in(c)] :
NULL;
}
const tcell_t *closure_entry_const(const cell_t *c) {
return is_user_func(c) ?
(tcell_t *)c->expr.arg[closure_in(c)] :
NULL;
}
// alts in trace are used to temporarily point to cell instances
// they are copied to during expansion.
// this gets that pointer
static
cell_t *map_cell(tcell_t *entry, intptr_t x) {
return x <= 0 ? NULL : entry[x].alt;
}
// get instance for a return
static
cell_t *get_return_arg(tcell_t *entry, tcell_t *returns, intptr_t x) {
assert_error(x < entry->entry.out);
trace_index_t i = tr_index(returns->value.ptr[x]);
assert_error(i > 0);
return map_cell(entry, i);
}
// given a list l with arity in -> out, produce an application of the list
// [X -> Y] => [X -> Y] apXY
static
cell_t *apply_list(cell_t *l, csize_t in, csize_t out) {
LOG("apply list %C %d %d", l, in, out);
cell_t *c = func(OP_ap, in + 1, out + 1);
if(in) {
c->expr.arg[0] = (cell_t *)(intptr_t)(in - 1);
} else {
FLAG_CLEAR(*c, expr, NEEDS_ARG);
}
c->expr.arg[in] = l;
RANGEUP(i, in+1, in+1+out) {
c->expr.arg[i] = dep(c);
}
refn(c, out);
return c;
}
static
bool match_op(const cell_t *a, const cell_t *b) {
if(is_value(a) || is_value(b)) return false;
if(a->op != b->op) return false;
if(a->op == OP_exec &&
closure_entry_const(a) !=
closure_entry_const(b)) return false;
if(closure_in(a) != closure_in(b)) return false;
return true;
}
// build a binding list by applying the pattern to c
// don't produce bindings if tail is NULL, used to simplify before USE_TMP()
// TODO add reduction back in
static
cell_t **bind_pattern(tcell_t *entry, cell_t **cp, cell_t *pattern, cell_t **tail) {
cell_t *c;
start:
c = *cp;
assert_error(c);
CONTEXT("bind_pattern %s %C %C @barrier", strfield(entry, word_name), c, pattern);
if(!pattern) return tail;
if(c->tmp || (tail && c == *tail)) return tail;
relates_to(c, pattern);
if(c == pattern) { // c == pattern, so add all variables
if(tail) {
cell_t **t = tail;
tail = trace_var_list(c, tail);
FOLLOW(p, *t, tmp) {
ref(p);
}
}
if(entry && is_var(pattern)) switch_entry(entry, pattern);
} else if(is_var(pattern)) { // bind the pattern variable to c
assert_error(!pattern->alt);
assert_error(!is_persistent(c));
if(entry) {
switch_entry(entry, pattern);
} else {
entry = var_entry(pattern->value.var);
}
LOG("bound %s[%d] = %C", entry->word_name, pattern->value.var-entry, c);
if(tail) LIST_ADD(tmp, tail, ref(c));
} else if(is_id_list(c)) { // walk through id lists
cp = &c->value.ptr[0];
if(!tail) simplify(cp);
goto start;
} else if(is_id_list(pattern)) {
pattern = pattern->value.ptr[0];
goto start;
} else if(is_list(pattern) && !is_empty_list(pattern)) { // match inside list pattern
// push entry in
if(pattern->pos) entry = pos_entry(pattern->pos);
if(is_list(c)) { // match the list
list_iterator_t ci = list_begin(c), pi = list_begin(pattern);
cell_t **cp, **pp;
while(cp = list_next(&ci, true),
pp = list_next(&pi, true),
cp && pp) {
if(!tail &&
closure_is_ready(*cp) && !is_row_arg(&ci)) {
simplify(cp); // ***
LOG_WHEN((*cp)->alt, MARK("WARN") " bind drops alt %C -> %C", *cp, (*cp)->alt);
}
tail = bind_pattern(entry, cp, *pp, tail);
}
assert_error(!cp && !pp, "mismatched lists %C %C", c, pattern);
} else { // c isn't expanded yet, so add code to extract match
csize_t
in = function_in(pattern),
out = function_out(pattern, false);
if(out) {
// this will rip the list apart (later)
cell_t *l = apply_list(ref(c), in, out);
drop(l); // don't care about the result
list_iterator_t it = list_begin(pattern);
COUNTDOWN(i, out) {
cell_t **p = list_next(&it, false);
assert_error(p);
cell_t **dp = &l->expr.arg[in+1+i], *d = *dp;
tail = bind_pattern(entry, dp, *p, tail);
drop(d);
}
}
}
} else if(pattern->op == OP_seq ||
(pattern->op == OP_id && // walk through id
pattern->expr.alt_set == 0)) {
pattern = pattern->expr.arg[0];
goto start;
} else if(is_row_list(c)) {
cell_t **r = left_elem(c);
LOG("match through row list %C -> %C", c, *r);
*cp = *r;
*r = NULL;
drop(c);
goto start;
} else if(ONEOF(c->op, OP_compose, OP_seq)) {
cell_t **r = &c->expr.arg[0];
LOG("match through %O %C -> %C", c->op, c, *r);
*cp = *r;
*r = NULL;
drop(c);
goto start;
} else if(match_op(c, pattern)) { // fusion
COUNTUP(i, closure_in(c)) {
tail = bind_pattern(entry, &c->expr.arg[i], pattern->expr.arg[i], tail);
}
} else if(is_var(c)) { // if c is a var, there's no need to unify it
LOG("bind var %C %C", c, pattern);
if(tail) LIST_ADD(tmp, tail, ref(c));
} else {
// This can be caused by an operation before an infinite loop
// This will prevent reducing the operation, and therefore fail to unify
assert_error(false, "binding error: %C %C", c, pattern);
// TODO promote to var instead of failing
return NULL;
}
return tail;
}
// re-pack results of a recursive function back into a list
// see: func_exec_specialize
static
response exec_list(cell_t **cp, context_t *ctx) {
PRE_NO_CONTEXT(exec_list);
if(ctx->t != T_LIST || closure_out(c) != 0) {
return SUCCESS;
}
CONTEXT_LOG("exec_list %C", c);
cell_t *res;
csize_t in = closure_in(c);
tcell_t *entry = closure_entry(c);
csize_t out = entry->entry.out - 1;
bool row = FLAG(entry->entry, specialize, ROW);
csize_t ln = max(entry->entry.out, ctx->s.out + row);
assert_error(entry->entry.specialize);
const uintptr_t dep_mask = entry->entry.specialize->dep_mask;
if(NOT_FLAG(entry->entry, specialize, UNWRAPPED)) {
return SUCCESS;
}
// the list may be larger than the outputs due to dropping,
// so pad with fail_cell when corresponding bit isn't set in dep_mask
cell_t *nc = expand(c, out);
nc->expr.out = out;
res = make_list(ln);
cell_t **out_arg = &nc->expr.arg[in + 1];
uintptr_t remaining = dep_mask;
RANGEUP(i, 1, ln) {
remaining &= ~(1 << i);
cell_t *d = &fail_cell;
if(dep_mask & (1 << i)) {
d = remaining ? dep(ref(nc)) : nc;
*out_arg++ = d;
}
res->value.ptr[REVI(i) - 1] = d;
}
if(!dep_mask || // can't drop all outputs
dep_mask & 1) {
res->value.ptr[ln-1] = nc;
} else {
res->value.ptr[ln-1] = &fail_cell;
}
if(FLAG(entry->entry, specialize, ROW)) {
FLAG_SET(*res, value, ROW);
}
LOG(TODO " fix condition %C @condition", res);
*cp = res;
return RETRY;
}
// unify c with pattern pat if possible, returning the unified result
// The purpose is to fuse a recursive function to itself e.g.:
// for F = g . f . g', where g . g' = id,
// F^n = (g . f . g')^n = g . f^n . g'
// eliminating the intermediate cancelling g/g' pairs.
static
response unify_exec(cell_t **cp, context_t *ctx) {
PRE(unify_exec, "#specialize");
csize_t
in = closure_in(c),
out = closure_out(c);
tcell_t *entry = closure_entry(c);
cell_t *initial = entry->entry.specialize->initial;
if(!initial) return FAIL;
assert_error(NOT_FLAG(*c, expr, NO_UNIFY));
assert_eq(in, closure_in(initial)); // TODO skip over non-changing args
LOG_WHEN(out != 0, TODO " unify_convert %d: out(%d) != 0 @unify-multiout", c-cells, out);
// simplify
COUNTUP(i, in) {
simplify(&c->expr.arg[i]);
bind_pattern(NULL, &c->expr.arg[i], initial->expr.arg[i], NULL);
}
// match c against initial and extract variable bindings
USE_TMP();
cell_t
*vl = 0,
**tail = &vl;
COUNTUP(i, in) {
tail = bind_pattern(NULL, &c->expr.arg[i], initial->expr.arg[i], tail);
if(!tail) {
LOG("bind_pattern failed %C %C", c->expr.arg[i], initial->expr.arg[i]);
break;
}
}
if(!tail) { // match failed
FOLLOW(p, vl, tmp) {
drop(p);
}
clean_tmp(vl);
return FAIL;
}
csize_t n_in = tmp_list_length(vl);
assert_error(n_in, "no inputs %C", c);
cell_t *n = ALLOC(n_in + out + 1,
.expr.out = out,
.op = OP_exec
);
n->expr.arg[n_in] = (cell_t *)entry;
FLAG_SET(*n, expr, RECURSIVE);
FLAG_SET(*n, expr, NO_UNIFY);
// TODO this list should be sorted first by the parent variable pos's
int pos = 0;
FOLLOW(p, vl, tmp) { // get arguments from the binding list
n->expr.arg[pos++] = p;
}
clean_tmp(vl);
LOG("unified %s %C with initial %C",
entry->word_name, c, initial);
LOG_WHEN(closure_out(c), TODO " handle deps in c = %C, n = %C", c, n);
TRAVERSE(c, in) {
drop(*p);
}
reassign_deps(c, n);
store_lazy(cp, n, 0);
return exec_list(cp, ctx);
}
// reassign c's deps to r
static
void reassign_deps(cell_t *c, cell_t *r) {
refcount_t n = 0;
csize_t out_n = closure_out(c);
assert_error(closure_out(r) == out_n);
if(out_n) {
cell_t
**c_out = &c->expr.arg[closure_args(c) - out_n],
**r_out = &r->expr.arg[closure_args(r) - out_n];
COUNTUP(i, out_n) {
cell_t *d = c_out[i];
if(d) {
d->expr.arg[0] = r;
r_out[i] = d;
n++;
}
}
// update ref counts
refn(r, n);
assert_error(c->n >= n);
c->n -= n;
}
}
// expand the user function into an instance
static
cell_t *exec_expand(cell_t *c, tcell_t *new_entry) {
size_t
in = closure_in(c),
out = closure_out(c),
n = closure_args(c);
tcell_t *entry = closure_entry(c);
cell_t *res;
tcell_t *returns = NULL;
int pos = c->pos;
WATCH(c, "exec_expand", "%E", entry);
// pointers to the outputs
cell_t **results[out + 1];
results[out] = &res;
COUNTUP(i, out) {
cell_t **dp = &c->expr.arg[n - 1 - i]; // ***
assert_error(!*dp || (*dp)->expr.arg[0] == c,
"%C refers to %C instead of %C",
*dp, (*dp)->expr.arg[0], c);
results[i] = dp;
}
CONTEXT("exec_expand %s: %C 0x%x", entry->word_name, c, c->expr.flags);
assert_error(entry->entry.len && FLAG(*entry, entry, COMPLETE));
trace_clear_alt(entry); // *** probably shouldn't need this
// assign function inputs to entry inputs
COUNTUP(i, in) {
tcell_t *p = &entry[i + 1];
cell_t *a = c->expr.arg[REVI(i)];
assert_error(is_var(p), "%d", i);
if(p->n + 1 == 0) {
// unused inputs
p->alt = NULL;
drop(a);
} else {
p->alt = refn(a, p->n);
}
}
// allocate, copy, and index
FOR_TRACE(p, entry, in + 1) {
int i = p - entry;
if(!p->op) {
p->alt = 0;
continue; // skip empty cells TODO remove these
}
if(trace_type(p) == T_RETURN) {
// store a pointer to the first return
if(!returns) returns = p;
continue;
}
// allocate a cell and copy instruction
tcell_t *e = is_user_func(p) ? get_entry(p) : NULL;
cell_t *nc;
csize_t p_in;
if(e && e->entry.out == 1 &&
(p_in = closure_in(p)) < e->entry.in) {
// wrap incomplete function in a quote
LOG("quote wrap %d", (int)i);
nc = closure_alloc(e->entry.in + 1);
memcpy(nc, &p->c, offsetof(cell_t, expr.arg));
nc->size = e->entry.in + 1;
csize_t remaining = e->entry.in - p_in;
memcpy(&nc->expr.arg[remaining], p->expr.arg, p_in * sizeof(cell_t *));
nc->expr.idx[0] = remaining - 1;
nc->expr.arg[e->entry.in] = p->expr.arg[p_in];
closure_set_ready(nc, false);
nc = row_quote(nc);
} else {
nc = copy(&p->c);
nc->expr.flags &= EXPR_TRACE; // only keep TRACE
nc->n = p->n;
}
nc->tmp = 0;
nc->src = c->src;
p->alt = nc;
}
// check that a return was found
assert_error(returns);
// rewrite args/ptrs to pointers stored in alts
FOR_TRACE(p, entry, in + 1) {
int i = p - entry;
cell_t *t = map_cell(entry, i);
if((is_value(p) && p->value.type == T_RETURN) || !t) continue;
unwrap_id_lists(&t); // for row quotes created above
TRAVERSE(t, args, ptrs) {
if(*p) {
trace_index_t x = tr_index(*p);
*p = map_cell(entry, x);
}
}
// rewrite entries
if(is_user_func(t)) {
cell_t **t_entry = &t->expr.arg[closure_in(t)];
tcell_t *e = tr_entry(*t_entry);
*t_entry = (cell_t *)e;
if(e == entry) {
// recursive call
FLAG_SET(*t, expr, RECURSIVE);
t->pos = pos; // *** move tail calls out
if(new_entry) {
// replace if specialized
*t_entry = (cell_t *)new_entry;
LOG("replaced self call %C %E -> %E", c, entry, new_entry);
}
}
}
}
// handle returns
// HACK: only allocate alt ids when compiling
bool tracing = trace_current_entry() != NULL || NOT_FLAG(*entry, entry, RECURSIVE);
uint8_t alt_n = tracing ? int_log2(entry->entry.alts) : 0;
uint8_t alt_id = new_alt_id(alt_n);
unsigned int branch = 0;
// first one
alt_set_t alt_set = tracing ? as_multi(alt_id, alt_n, branch++) : 0;
*results[out] = id(get_return_arg(entry, returns, out), alt_set);
COUNTUP(i, out) {
store_lazy_dep(*results[i], get_return_arg(entry, returns, i), alt_set);
}
// rest
trace_index_t next = tr_index(returns->alt);
while(next > 0) {
alt_set_t as = tracing ? as_multi(alt_id, alt_n, branch++) : 0;
returns = &entry[next];
FOREACH(i, results) {
cell_t *a = get_return_arg(entry, returns, i);
if(*results[i]) {
results[i] = &(*results[i])->alt;
*results[i] = a ? id(a, as) : NULL;
} else {
drop(a);
}
}
next = tr_index(returns->alt);
}
mark_pos(res, pos);
return res;
}
// builds a temporary list of referenced variables
static
cell_t **input_var_list(cell_t *c, cell_t **tail) {
if(c && !c->tmp_val && tail != &c->tmp) {
if(is_var(c) && !is_list(c)) {
LIST_ADD(tmp, tail, c);
} else {
c->tmp_val = true; // prevent loops
if(is_list(c)) {
COUNTDOWN(i, list_size(c)) {
tail = trace_var_list(c->value.ptr[i], tail);
}
} else {
TRAVERSE(c, in) {
tail = trace_var_list(*p, tail);
}
}
c->tmp_val = false;
}
}
return tail;
}
// remove vars from p that are not in entry
static
void vars_in_entry(cell_t **p, tcell_t *entry) {
while(*p) {
cell_t *v = *p;
cell_t **next = &v->tmp;
if(!var_for_entry(entry, v->value.var)) {
// remove from list
LOG("remove var %C", v);
*p = *next;
v->tmp = 0;
} else {
p = next;
}
}
}
// the argument order needs to be consistent on recursive calls
// re-number inputs to match the order they are reached from specialize->expand
static
void reassign_input_order(tcell_t *entry) {
if(!entry->entry.specialize) return;
cell_t *c = entry->entry.specialize->expand;
if(!c) return;
tcell_t *parent_entry = entry->entry.parent;
CONTEXT("reassign input order %C (%s -> %s)", c,
parent_entry->word_name, entry->word_name);
USE_TMP(); // fix
cell_t *vl = 0;
input_var_list(c, &vl);
vars_in_entry(&vl, entry); // ***
on_assert_error(tmp_list_length(vl) == entry->entry.in,
"%d != %d, %s %C @specialize",
tmp_list_length(vl), entry->entry.in,
entry->word_name, c) {
FOLLOW(p, vl, tmp) {
LOG("input var %C", p);
}
}
int i = entry->entry.in;
FOLLOW(p, vl, tmp) {
tcell_t *tn = var_for_entry(entry, p->value.var);
assert_error(tn);
assert_error(is_var(tn));
assert_error(tn->var_index, "%s[%d] (%C)",
entry->word_name, tn-entry, p);
tn->var_index = i--;
}
clean_tmp(vl);
}
// build a call into entry from its parent
// this doesn't work for quotes
// all c's input args must be params
static
cell_t *flat_call(cell_t *c, tcell_t *entry) {
tcell_t *parent_entry = entry->entry.parent;
CONTEXT("flat call %C (%s -> %s)", c,
parent_entry->word_name, entry->word_name);
USE_TMP(); // fix
cell_t *vl = 0;
input_var_list(c, &vl); // expand if needed
FOLLOW(p, vl, tmp) {
switch_entry(entry, p);
}
csize_t
in = entry->entry.in,
out = entry->entry.out;
cell_t *nc = ALLOC(in + out,
.expr.out = out - 1,
.op = OP_exec
);
assert_error(tmp_list_length(vl) == in,
"%d != %d, %s @specialize",
tmp_list_length(vl), in, entry->word_name);
int i = 0;
FOLLOW(p, vl, tmp) {
tcell_t *tn = get_var(entry, p);
assert_error(tn, "get_var(%E, %C)", entry, p);
// Is it okay to update this from reassign_input_order?
tn->var_index = in - i;
// assert_error(tn->var_index == i, "%T (%C)", p->value.var, p);
switch_entry(parent_entry, p);
cell_t *v = var_create_nonlist(T_ANY, p->value.var);
nc->expr.arg[i] = v;
LOG("arg[%d] -> %T", i, p->value.var);
i++;
}
clean_tmp(vl);
nc->expr.arg[in] = (cell_t *)entry;
return nc;
}
// TODO generalize these
// pull out the contents of a list using ap
// i.e. remove a level of nesting: [[...] ...] -> [... ...]
// TODO handle row
static
cell_t *unwrap(cell_t *c, uintptr_t dep_mask, int out, int dropped, bool row) {
int offset = list_size(c) - 1;
// head (offset == 0)
assert_error(is_list(c) && list_size(c) == 1, TODO " size = %d, %C", list_size(c), c);
// N = out, ap0N swapN drop
cell_t *l = make_list(out + offset - dropped + row);
if(row) FLAG_SET(*l, value, ROW);
cell_t *ap = ready_func(OP_ap, 1, out + 1);
LOG("unwrap %C %d %C [%C]", c, out, l, ap);
COUNTUP(i, offset) {
l->value.ptr[i] = ref(c->value.ptr[i]);
}
cell_t **p = &l->value.ptr[offset];
COUNTDOWN(i, out) {
cell_t **a = &ap->expr.arg[i + 1];
if(dep_mask & (1 << i)) {
*p++ = *a = dep(ref(ap));
} else {
*a = NULL;
}
}
ap->expr.arg[0] = ref(c->value.ptr[offset]);
if(row) {
*p++ = ap;
} else {
drop(ap);
}
drop(c);
return l;
}
// [res dep_0 ... dep_out-1]
cell_t *wrap_vars(cell_t **res, cell_t *p, uintptr_t dep_mask, csize_t out) {
assert_error(dep_mask);
assert_error(out);
cell_t *l = make_list(out);
int offset = closure_args(p) - closure_out(p);
cell_t **out_arg = &p->expr.arg[offset];
cell_t *out_arg0 = NULL;
LOG("wrap_vars %d %C", out, l);
int dpos = offset;
COUNTUP(i, out) {
cell_t *d;
if(dep_mask & (1 << i)) {
if(out_arg0) {
d = closure_alloc(1);
store_dep(d, (*res)->value.var, dpos++, T_ANY, default_bound, 0);
*out_arg++ = d;
} else {
d = out_arg0 = *res;
}
} else {
d = &fail_cell;
}
l->value.ptr[REVI(i)] = d;
}
assert_error(out_arg0);
*res = l;
return out_arg0;
}
// expand a user function into a list of outputs
static
cell_t *expand_list(cell_t *c, tcell_t *new_entry) {
size_t out = closure_out(c), n = out;
cell_t *l = make_list(out + 1);
TRAVERSE(c, out) {
l->value.ptr[--n] = *p ? (*p = dep(c)) : &fail_cell;
}
refn(c, out);
l->value.ptr[out] = exec_expand(c, new_entry); // deps will be in c ***
return l;
}
// collect references to up to n deps from the context
// i.e. where the top n list items will go
static
context_t *collect_ap_deps(context_t *ctx, cell_t **deps, int n) {
while(n > 0) {
const cell_t *c = *ctx->src;
if(c->op != OP_ap ||
closure_in(c) != 1 ||
c->n > count_out_used(c)) return NULL;
int out = closure_out(c);
int out_n = min(out, n);
n -= out_n;
memcpy(&deps[n],
&c->expr.arg[closure_args(c) - out],
out_n * sizeof(cell_t *));
if(n > 0) ctx = ctx->up;
}
return ctx;
}
// generate a specialized sub-trace
static
response func_exec_specialize(cell_t **cp, context_t *ctx) {
csize_t
in = closure_in(*cp),
out = closure_out(*cp);
tcell_t *entry = closure_entry(*cp);
specialize_data specialize;
PRE(exec_specialize, "%s 0x%x #specialize", entry->word_name, (*cp)->expr.flags);
LOG_UNLESS(entry->entry.out == 1, "out = %d #unify-multiout", entry->entry.out);
specialize.entry = entry;
specialize.flags = 0;
// calculate dep_mask, which indicates which list items will be used,
// as well as collecting references to where they will be stored.
assert_error(ctx->s.out < sizeof_bits(specialize.dep_mask));
specialize.dep_mask = (1 << ctx->s.out) - 1;
int dropped = 0;
context_t *top = NULL;
if(ctx->t == T_LIST && ctx->s.out) {
cell_t *deps[ctx->s.out];
top = collect_ap_deps(ctx->up, deps, ctx->s.out);
if(top) {
COUNTUP(i, ctx->s.out) {
if(!deps[i]) {
specialize.dep_mask &= ~(1 << i);
dropped++;
LOG("dropped ap %C, out = %d", c, i);
}
}
LOG("dep_mask %x", specialize.dep_mask);
}
}
// start a new entry
tcell_t *parent_entry = trace_current_entry();
tcell_t *new_entry = trace_start_entry(parent_entry, entry->entry.out);
new_entry->entry.specialize = &specialize;
new_entry->module_name = parent_entry->module_name;
int sub_id = parent_entry->entry.sub_id++;
const char *sub_name = entry->entry.parent && FLAG(*entry->entry.parent, entry, RECURSIVE) ?
suffix(entry->word_name, ':') :
entry->word_name;
new_entry->word_name = sub_id ?
string_printf("%s:%s_%d", parent_entry->word_name, sub_name, sub_id) :
string_printf("%s:%s", parent_entry->word_name, sub_name);
LOG("created entry for %s(%d)", new_entry->word_name, TRACE_INDEX(new_entry));
specialize.initial = TAG_PTR(ref(c), "specialize.initial");
insert_root(&c);
// make a list with expanded outputs of c
cell_t *nc = COPY_REF(c, in);
specialize.expand = TAG_PTR(nc, "specialize.expand");
insert_root(&nc);
// constrain movement of inputs
mark_barriers(new_entry, nc);
COUNTUP(i, in) {
if(c->expr.arg[i]->op != OP_ap ||
TWEAK(true, "to disable ap simplify %C", c->expr.arg[i]))
simplify(&c->expr.arg[i]);
}
move_changing_values(new_entry, c);
cell_t *l = expand_list(nc, new_entry);
// eliminate intermediate list using unwrap
// this will be undone by exec_list
bool row = false;
if(ctx->t == T_LIST) {
row = FLAG(*entry, entry, ROW);
l = unwrap(l, specialize.dep_mask, ctx->s.out, dropped, row);
new_entry->entry.out += ctx->s.out - 1 - dropped + row;
FLAG_SET(new_entry->entry, specialize, UNWRAPPED);
if(row) {
FLAG_SET(new_entry->entry, specialize, ROW);
specialize.dep_mask |= 1 << ctx->s.out;
}
}
// perform reduction
TRAVERSE_REF(nc, in);
new_entry->entry.alts = trace_reduce(new_entry, &l);
drop(l);
remove_root(&nc);
// eliminate [id] map
uintptr_t hash = hash_entry(new_entry);
LOG("new_entry hash = 0x%x", hash);
if(hash == ID_MAP_HASH) {
LOG("[id] map %C %C", c, nc);
cell_t *p = c->expr.arg[0];
tcell_t *tn = get_var(parent_entry, p);
cell_t *v = var_create_nonlist(tn->trace.type, tn);
*cp = v;
drop(nc);
dropn(c, 2);
remove_root(&c);
trace_reset(new_entry);
return SUCCESS;
}
// build self call
TRAVERSE(nc, in) simplify(p);
cell_t *p = flat_call(nc, new_entry);
drop(nc);
if(top) {
trace_drop_return(new_entry, ctx->s.out, specialize.dep_mask);
}
// must drop everything so that trace_drop is called
// before the trace is compacted and rearranged
drop(specialize.initial);
TRAVERSE(c, in) drop(*p);
trace_end_entry(new_entry);
trace_clear_alt(parent_entry);
trace_t tr;
get_trace_info_for_output(&tr, new_entry, 0);
cell_t *res = var_create_with_entry(tr.type, parent_entry, p->size);
res->value.range = tr.range;
tcell_t *tc = res->value.var;
// build list expected by caller
if(FLAG_(specialize.flags, SPECIALIZE_UNWRAPPED)) {
cell_t *l = wrap_vars(&res, p, specialize.dep_mask, max(new_entry->entry.out, ctx->s.out + row));
if(FLAG_(specialize.flags, SPECIALIZE_ROW)) FLAG_SET(*res, value, ROW);
trace_reduction(p, l);
}
// handle deps
csize_t p_in = closure_in(p);
if(out) {
// replace outputs with variables
int offset = new_entry->entry.out - 1 - out;
cell_t **c_out = &c->expr.arg[in + 1];
RANGEUP(i, offset, new_entry->entry.out) {
cell_t *d = c_out[i - offset];
if(d && is_dep(d)) { // NOTE should null deps be removed?
assert_error(d->expr.arg[0] == c);
drop(c);
get_trace_info_for_output(&tr, new_entry, i + 1);
store_dep(d, tc, i + p_in + 1, tr.type, tr.range, 0);
d->value.range = tr.range;
p->expr.arg[p_in + 1 + i] = d;
}
}
}
trace_reduction(p, res);
replace_cell(cp, ctx, res);
add_conditions_from_array(res, p->expr.arg, in);
remove_root(&c);
drop(p);
return SUCCESS;
}
// call trace_update on all reachable vars
static
void trace_update_all(cell_t *c) {
TRAVERSE(c, in, ptrs) {
if(*p) {
if(is_var(*p)) {
trace_update(*p);
} else {
trace_update_all(*p);
}
}
}
}
// trace this call instead of expanding it
static
response func_exec_trace(cell_t **cp, context_t *ctx) {
size_t in = closure_in(*cp);
tcell_t *entry = closure_entry(*cp);
PRE(exec_trace, "%s 0x%x", entry->word_name, (*cp)->expr.flags);
cell_t *res;
const size_t out = closure_out(c) + 1;
trace_t tr;
assert_ge(out, entry->entry.out);
if(in < entry->entry.in) { // move this into specialization of v?
assert_error(in && c->expr.arg[0]);
int offset = entry->entry.in - in;
cell_t *v = c->expr.arg[0]; // ***
while(is_id_list(v)) v = v->value.ptr[0];
assert_error(is_var(v));
tcell_t *v_entry = get_entry(v->value.var);
assert_error(v_entry);
assert_eq(closure_args(v->value.var), v_entry->entry.in + v_entry->entry.out);
assert_error(offset < v_entry->entry.out);
c = expand(c, offset); // *** could break deps
assert_eq(closure_args(c), entry->entry.in + entry->entry.out);
memmove(&c->expr.arg[entry->entry.in], &c->expr.arg[in], sizeof(cell_t *) * out);
int d0 = closure_args(v->value.var) - closure_out(v->value.var);
COUNTUP(i, offset) {
c->expr.arg[i + in] = dep_var(v, d0 + i, T_ANY);
}
in = entry->entry.in;
}
// reduce all inputs
if(in) {
csize_t n = 0;
uint8_t in_types[in];
val_t in_opaque[in];
memset(in_types, 0, in * sizeof(in_types[0]));
reassign_input_order(entry);
// find types of inputs
FOR_TRACE(p, entry) {
if(is_var(p) && p->var_index) {
assert_le(p->var_index, entry->entry.in);
int i = entry->entry.in - p->var_index;
type_t t = p->value.type;
in_types[i] = t;
if(t == T_OPAQUE) in_opaque[i] = p->trace.range.min;
if(++n >= in) break;
}
}
// first reduce up to assertions
COUNTUP(i, in) {
type_t t = in_types[i];
context_t arg_ctx = t == T_OPAQUE ?
CTX(opaque, in_opaque[i]) :
CTX(t, t);
CHECK_IF(WITH(x, &arg_ctx,
x->flags &= ~CONTEXT_REDUCE_LISTS,
x->priority = PRIORITY_ASSERT - 1,