1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences math combinators accessors namespaces
6 compiler.tree.combinators
7 compiler.tree.escape-analysis.nodes
8 compiler.tree.escape-analysis.branches
9 compiler.tree.escape-analysis.allocations ;
10 IN: compiler.tree.escape-analysis.recursive
12 : congruent? ( alloc1 alloc2 -- ? )
14 { [ 2dup [ f eq? ] either? ] [ eq? ] }
15 { [ 2dup [ t eq? ] either? ] [ eq? ] }
16 { [ 2dup [ length ] bi@ = not ] [ 2drop f ] }
17 [ [ [ allocation ] bi@ congruent? ] 2all? ]
20 : check-fixed-point ( node alloc1 alloc2 -- )
21 [ congruent? ] 2all? [ drop ] [ label>> f >>fixed-point drop ] if ;
23 : node-input-allocations ( node -- allocations )
24 in-d>> [ allocation ] map ;
26 : node-output-allocations ( node -- allocations )
27 out-d>> [ allocation ] map ;
29 : recursive-stacks ( #enter-recursive -- stacks )
31 escaping-values get '[ [ , disjoint-set-member? ] all? ] filter
34 : analyze-recursive-phi ( #enter-recursive -- )
35 [ ] [ recursive-stacks ] [ out-d>> ] tri
36 [ [ merge-values ] 2each ]
38 [ (merge-allocations) ] dip
39 [ [ allocation ] map check-fixed-point ]
40 [ record-allocations ]
44 M: #recursive escape-analysis* ( #recursive -- )
45 [ label>> return>> in-d>> introduce-values ]
49 [ first out-d>> introduce-values ]
50 [ first analyze-recursive-phi ]
56 M: #enter-recursive escape-analysis* ( #enter-recursive -- )
57 #! Handled by #recursive
60 M: #call-recursive escape-analysis* ( #call-label -- )
61 [ ] [ label>> return>> ] [ node-output-allocations ] tri
62 [ [ node-input-allocations ] dip check-fixed-point ]
63 [ drop swap [ in-d>> ] [ out-d>> ] bi* copy-values ]
66 M: #return-recursive escape-analysis* ( #return-recursive -- )
69 [ in-d>> ] [ label>> calls>> ] bi
70 [ out-d>> escaping-values get '[ , equate ] 2each ] with each