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