]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/debugger/debugger.factor
4bf4cf88f02bb4efb92c0cd341d9977c12dff984
[factor.git] / basis / compiler / tree / debugger / debugger.factor
1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel assocs match fry accessors namespaces make effects
4 sequences sequences.private quotations generic macros arrays
5 prettyprint prettyprint.backend prettyprint.custom
6 prettyprint.sections math words combinators
7 combinators.short-circuit io sorting hints
8 compiler.tree
9 compiler.tree.recursive
10 compiler.tree.normalization
11 compiler.tree.cleanup
12 compiler.tree.propagation
13 compiler.tree.propagation.info
14 compiler.tree.escape-analysis
15 compiler.tree.tuple-unboxing
16 compiler.tree.def-use
17 compiler.tree.builder
18 compiler.tree.optimizer
19 compiler.tree.combinators
20 compiler.tree.checker
21 compiler.tree.identities
22 compiler.tree.dead-code
23 compiler.tree.modular-arithmetic ;
24 FROM: fry => _ ;
25 RENAME: _ match => __
26 IN: compiler.tree.debugger
27
28 ! A simple tool for turning tree IR into quotations and
29 ! printing reports, for debugging purposes.
30
31 GENERIC: node>quot ( node -- )
32
33 MACRO: match-choose ( alist -- )
34     [ '[ _ ] ] assoc-map '[ _ match-cond ] ;
35
36 MATCH-VARS: ?a ?b ?c ;
37
38 : pretty-shuffle ( effect -- word/f )
39     [ in>> ] [ out>> ] bi 2array {
40         { { { } { } } [ ] }
41         { { { ?a } { ?a } } [ ] }
42         { { { ?a ?b } { ?a ?b } } [ ] }
43         { { { ?a ?b ?c } { ?a ?b ?c } } [ ] }
44         { { { ?a } { } } [ drop ] }
45         { { { ?a ?b } { } } [ 2drop ] }
46         { { { ?a ?b ?c } { } } [ 3drop ] }
47         { { { ?a } { ?a ?a } } [ dup ] }
48         { { { ?a ?b } { ?a ?b ?a ?b } } [ 2dup ] }
49         { { { ?a ?b ?c } { ?a ?b ?c ?a ?b ?c } } [ 3dup ] }
50         { { { ?a ?b } { ?a ?b ?a } } [ over ] }
51         { { { ?b ?a } { ?a ?b } } [ swap ] }
52         { { { ?b ?a ?c } { ?a ?b ?c } } [ swapd ] }
53         { { { ?a ?b } { ?a ?a ?b } } [ dupd ] }
54         { { { ?a ?b } { ?b ?a ?b } } [ tuck ] }
55         { { { ?a ?b ?c } { ?a ?b ?c ?a } } [ pick ] }
56         { { { ?a ?b ?c } { ?c ?a ?b } } [ -rot ] }
57         { { { ?a ?b ?c } { ?b ?c ?a } } [ rot ] }
58         { { { ?a ?b } { ?b } } [ nip ] }
59         { { { ?a ?b ?c } { ?c } } [ 2nip ] }
60         { __ f }
61     } match-choose ;
62
63 TUPLE: shuffle-node { effect effect } ;
64
65 M: shuffle-node pprint* effect>> effect>string text ;
66  
67 : (shuffle-effect) ( in out #shuffle -- effect )
68     mapping>> '[ _ at ] map <effect> ;
69
70 : shuffle-effect ( #shuffle -- effect )
71     [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
72
73 : #>r? ( #shuffle -- ? )
74     {
75         [ in-d>> length 1 = ]
76         [ out-r>> length 1 = ]
77         [ in-r>> empty? ]
78         [ out-d>> empty? ]
79     } 1&& ;
80
81 : #r>? ( #shuffle -- ? )
82     {
83         [ in-d>> empty? ]
84         [ out-r>> empty? ]
85         [ in-r>> length 1 = ]
86         [ out-d>> length 1 = ]
87     } 1&& ;
88
89 SYMBOLS: >R R> ;
90
91 M: #shuffle node>quot
92     {
93         { [ dup #>r? ] [ drop \ >R , ] }
94         { [ dup #r>? ] [ drop \ R> , ] }
95         {
96             [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
97             [
98                 shuffle-effect dup pretty-shuffle
99                 [ % ] [ shuffle-node boa , ] ?if
100             ]
101         }
102         [ drop "COMPLEX SHUFFLE" , ]
103     } cond ;
104
105 M: #push node>quot literal>> literalize , ;
106
107 M: #call node>quot word>> , ;
108
109 M: #call-recursive node>quot label>> id>> , ;
110
111 DEFER: nodes>quot
112
113 DEFER: label
114
115 M: #recursive node>quot
116     [ label>> id>> literalize , ]
117     [ child>> nodes>quot , \ label , ]
118     bi ;
119
120 M: #if node>quot
121     children>> [ nodes>quot ] map % \ if , ;
122
123 M: #dispatch node>quot
124     children>> [ nodes>quot ] map , \ dispatch , ;
125
126 M: #alien-invoke node>quot params>> , \ #alien-invoke , ;
127
128 M: #alien-indirect node>quot params>> , \ #alien-indirect , ;
129
130 M: #alien-callback node>quot params>> , \ #alien-callback , ;
131
132 M: node node>quot drop ;
133
134 : nodes>quot ( node -- quot )
135     [ [ node>quot ] each ] [ ] make ;
136
137 GENERIC: optimized. ( quot/word -- )
138
139 M: word optimized. specialized-def optimized. ;
140
141 M: callable optimized. build-tree optimize-tree nodes>quot . ;
142
143 SYMBOL: words-called
144 SYMBOL: generics-called
145 SYMBOL: methods-called
146 SYMBOL: intrinsics-called
147 SYMBOL: node-count
148
149 : make-report ( word/quot -- assoc )
150     [
151         build-tree optimize-tree
152
153         H{ } clone words-called set
154         H{ } clone generics-called set
155         H{ } clone methods-called set
156         H{ } clone intrinsics-called set
157
158         0 swap [
159             [ 1 + ] dip
160             dup #call? [
161                 word>> {
162                     { [ dup "intrinsic" word-prop ] [ intrinsics-called ] }
163                     { [ dup generic? ] [ generics-called ] }
164                     { [ dup method-body? ] [ methods-called ] }
165                     [ words-called ]
166                 } cond get inc-at
167             ] [ drop ] if
168         ] each-node
169         node-count set
170     ] H{ } make-assoc ;
171
172 : report. ( report -- )
173     [
174         "==== Total number of IR nodes:" print
175         node-count get .
176
177         {
178             { generics-called "==== Generic word calls:" }
179             { words-called "==== Ordinary word calls:" }
180             { methods-called "==== Non-inlined method calls:" }
181             { intrinsics-called "==== Open-coded intrinsic calls:" }
182         } [
183             nl print get keys natural-sort stack.
184         ] assoc-each
185     ] bind ;
186
187 : optimizer-report. ( word -- )
188     make-report report. ;
189
190 ! More utilities
191
192 : final-info ( quot -- seq )
193     build-tree
194     analyze-recursive
195     normalize
196     propagate
197     compute-def-use
198     dup check-nodes
199     last node-input-infos ;
200
201 : final-classes ( quot -- seq )
202     final-info [ class>> ] map ;
203
204 : final-literals ( quot -- seq )
205     final-info [ literal>> ] map ;
206
207 : cleaned-up-tree ( quot -- nodes )
208     [
209         build-tree
210         analyze-recursive
211         normalize
212         propagate
213         cleanup
214         escape-analysis
215         unbox-tuples
216         apply-identities
217         compute-def-use
218         remove-dead-code
219         compute-def-use
220         optimize-modular-arithmetic 
221     ] with-scope ;
222
223 : inlined? ( quot seq/word -- ? )
224     [ cleaned-up-tree ] dip
225     dup word? [ 1array ] when
226     '[ dup #call? [ word>> _ member? ] [ drop f ] if ]
227     contains-node? not ;