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