]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/recursive/recursive.factor
Merge branch 'bags' of git://github.com/littledan/Factor
[factor.git] / basis / compiler / tree / recursive / recursive.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel assocs arrays namespaces accessors sequences deques fry
4 search-deques dlists combinators.short-circuit make sets compiler.tree ;
5 FROM: namespaces => set ;
6 IN: compiler.tree.recursive
7
8 TUPLE: call-site tail? node label ;
9
10 : recursive-phi-in ( #enter-recursive -- seq )
11     [ label>> calls>> [ node>> in-d>> ] map ] [ in-d>> ] bi suffix ;
12
13 <PRIVATE
14
15 TUPLE: call-graph-node tail? label children calls ;
16
17 : (tail-calls) ( tail? seq -- seq' )
18     reverse [ swap [ and ] keep ] map nip reverse ;
19
20 : tail-calls ( tail? node -- seq )
21     [
22         {
23             [ #phi? ]
24             [ #return? ]
25             [ #return-recursive? ]
26         } 1||
27     ] map (tail-calls) ;
28
29 SYMBOLS: children calls ;
30
31 GENERIC: node-call-graph ( tail? node -- )
32
33 : (build-call-graph) ( tail? nodes -- )
34     [ tail-calls ] keep
35     [ node-call-graph ] 2each ;
36
37 : build-call-graph ( nodes -- labels calls )
38     [
39         V{ } clone children set
40         V{ } clone calls set
41         [ t ] dip (build-call-graph)
42         children get
43         calls get
44     ] with-scope ;
45
46 M: #return-recursive node-call-graph
47     nip dup label>> (>>return) ;
48
49 M: #call-recursive node-call-graph
50     [ dup label>> call-site boa ] keep
51     [ drop calls get push ]
52     [ label>> calls>> push ] 2bi ;
53
54 M: #recursive node-call-graph
55     [ label>> V{ } clone >>calls drop ]
56     [
57         [ label>> ] [ child>> build-call-graph ] bi
58         call-graph-node boa children get push
59     ] bi ;
60
61 M: #branch node-call-graph
62     children>> [ (build-call-graph) ] with each ;
63
64 M: node node-call-graph 2drop ;
65
66 SYMBOLS: not-loops recursive-nesting ;
67
68 : not-a-loop ( label -- ) not-loops get conjoin ;
69
70 : not-a-loop? ( label -- ? ) not-loops get key? ;
71
72 : non-tail-calls ( call-graph-node -- seq )
73     calls>> [ tail?>> not ] filter ;
74
75 : visit-back-edges ( call-graph -- )
76     [
77         [ non-tail-calls [ label>> not-a-loop ] each ]
78         [ children>> visit-back-edges ]
79         bi
80     ] each ;
81
82 SYMBOL: changed?
83
84 : check-cross-frame-call ( call-site -- )
85     label>> dup not-a-loop? [ drop ] [
86         recursive-nesting get <reversed> [
87             2dup label>> eq? [ 2drop f ] [
88                 [ label>> not-a-loop? ] [ tail?>> not ] bi or
89                 [ not-a-loop changed? on ] [ drop ] if t
90             ] if
91         ] with all? drop
92     ] if ;
93
94 : detect-cross-frame-calls ( call-graph -- )
95     ! Suppose we have a nesting of recursives A --> B --> C
96     ! B tail-calls A, and C non-tail-calls B. Then A cannot be
97     ! a loop, it needs its own procedure, since the call from
98     ! C to A crosses a call-frame boundary.
99     [
100         [ recursive-nesting get push ]
101         [ calls>> [ check-cross-frame-call ] each ]
102         [ children>> detect-cross-frame-calls ] tri
103         recursive-nesting get pop*
104     ] each ;
105
106 : while-changing ( ... quot: ( ... -- ... ) -- ... )
107     changed? off
108     [ call ] [ changed? get [ while-changing ] [ drop ] if ] bi ;
109     inline recursive
110
111 : detect-loops ( call-graph -- )
112     H{ } clone not-loops set
113     V{ } clone recursive-nesting set
114     [ visit-back-edges ]
115     [ '[ _ detect-cross-frame-calls ] while-changing ]
116     bi ;
117
118 : mark-loops ( call-graph -- )
119     [
120         [ label>> dup not-a-loop? [ t >>loop? ] unless drop ]
121         [ children>> mark-loops ]
122         bi
123     ] each ;
124
125 PRIVATE>
126
127 SYMBOL: call-graph
128
129 : analyze-recursive ( nodes -- nodes )
130     dup build-call-graph drop
131     [ call-graph set ]
132     [ detect-loops ]
133     [ mark-loops ]
134     tri ;