]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/escape-analysis/recursive/recursive.factor
3f8746dd4119e827c1d87d5e81de4453ea01f836
[factor.git] / basis / compiler / tree / escape-analysis / recursive / recursive.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators compiler.tree
4 compiler.tree.combinators
5 compiler.tree.escape-analysis.allocations
6 compiler.tree.escape-analysis.branches
7 compiler.tree.escape-analysis.nodes compiler.tree.recursive
8 disjoint-sets fry kernel namespaces sequences ;
9 IN: compiler.tree.escape-analysis.recursive
10
11 : congruent? ( alloc1 alloc2 -- ? )
12     {
13         { [ 2dup [ boolean? ] either? ] [ eq? ] }
14         { [ 2dup 2length @ = not ] [ 2drop f ] }
15         [ [ [ allocation ] bi@ congruent? ] 2all? ]
16     } cond ;
17
18 : check-fixed-point ( node alloc1 alloc2 -- )
19     [ congruent? ] 2all? [ drop ] [ label>> f >>fixed-point drop ] if ;
20
21 : node-input-allocations ( node -- allocations )
22     in-d>> [ allocation ] map ;
23
24 : node-output-allocations ( node -- allocations )
25     out-d>> [ allocation ] map ;
26
27 : recursive-stacks ( #enter-recursive -- stacks )
28     recursive-phi-in
29     escaping-values get '[ [ _ disjoint-set-member? ] all? ] filter
30     flip ;
31
32 : analyze-recursive-phi ( #enter-recursive -- )
33     [ ] [ recursive-stacks ] [ out-d>> ] tri
34     [ [ merge-values ] 2each ]
35     [
36         [ (merge-allocations) ] dip
37         [ [ allocation ] map check-fixed-point ]
38         [ record-allocations ]
39         2bi
40     ] 2bi ;
41
42 M: #recursive escape-analysis* ( #recursive -- )
43     [ label>> return>> in-d>> introduce-values ]
44     [
45         [
46             child>>
47             [ first out-d>> introduce-values ]
48             [ first analyze-recursive-phi ]
49             [ (escape-analysis) ]
50             tri
51         ] until-fixed-point
52     ] bi ;
53
54 M: #enter-recursive escape-analysis* ( #enter-recursive -- )
55     ! Handled by #recursive
56     drop ;
57
58 M: #call-recursive escape-analysis* ( #call-label -- )
59     [ ] [ label>> return>> ] [ node-output-allocations ] tri
60     [ [ node-input-allocations ] dip check-fixed-point ]
61     [ drop swap [ in-d>> ] [ out-d>> ] bi* copy-values ]
62     3bi ;
63
64 M: #return-recursive escape-analysis* ( #return-recursive -- )
65     [ call-next-method ]
66     [
67         [ in-d>> ] [ label>> calls>> ] bi
68         [ node>> out-d>> escaping-values get '[ _ equate ] 2each ] with each
69     ] bi ;