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