]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/debugger/debugger.factor
Removing integers-as-sequences
[factor.git] / basis / compiler / tree / debugger / debugger.factor
1 ! Copyright (C) 2006, 2010 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 ?c } { ?a ?b ?c ?a } } [ pick ] }
55         { { { ?a ?b ?c } { ?c ?a ?b } } [ -rot ] }
56         { { { ?a ?b ?c } { ?b ?c ?a } } [ rot ] }
57         { { { ?a ?b } { ?b } } [ nip ] }
58         { { { ?a ?b ?c } { ?c } } [ 2nip ] }
59         { __ f }
60     } match-choose ;
61
62 TUPLE: shuffle-node { effect effect } ;
63
64 M: shuffle-node pprint* effect>> effect>string text ;
65  
66 : (shuffle-effect) ( in out #shuffle -- effect )
67     mapping>> '[ _ at ] map [ >array ] bi@ <effect> ;
68
69 : shuffle-effect ( #shuffle -- effect )
70     [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
71
72 : #>r? ( #shuffle -- ? )
73     {
74         [ in-d>> length 1 = ]
75         [ out-r>> length 1 = ]
76         [ in-r>> empty? ]
77         [ out-d>> empty? ]
78     } 1&& ;
79
80 : #r>? ( #shuffle -- ? )
81     {
82         [ in-d>> empty? ]
83         [ out-r>> empty? ]
84         [ in-r>> length 1 = ]
85         [ out-d>> length 1 = ]
86     } 1&& ;
87
88 SYMBOLS: >R R> ;
89
90 M: #shuffle node>quot
91     {
92         { [ dup #>r? ] [ drop \ >R , ] }
93         { [ dup #r>? ] [ drop \ R> , ] }
94         {
95             [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
96             [
97                 shuffle-effect dup pretty-shuffle
98                 [ % ] [ shuffle-node boa , ] ?if
99             ]
100         }
101         [ drop "COMPLEX SHUFFLE" , ]
102     } cond ;
103
104 M: #push node>quot literal>> literalize , ;
105
106 M: #call node>quot word>> , ;
107
108 M: #call-recursive node>quot label>> id>> , ;
109
110 DEFER: nodes>quot
111
112 DEFER: label
113
114 M: #recursive node>quot
115     [ label>> id>> literalize , ]
116     [ child>> nodes>quot , \ label , ]
117     bi ;
118
119 M: #if node>quot
120     children>> [ nodes>quot ] map % \ if , ;
121
122 M: #dispatch node>quot
123     children>> [ nodes>quot ] map , \ dispatch , ;
124
125 M: #alien-invoke node>quot params>> , \ #alien-invoke , ;
126
127 M: #alien-indirect node>quot params>> , \ #alien-indirect , ;
128
129 M: #alien-assembly node>quot params>> , \ #alien-assembly , ;
130
131 M: #alien-callback node>quot params>> , \ #alien-callback , ;
132
133 M: node node>quot drop ;
134
135 : nodes>quot ( node -- quot )
136     [ [ node>quot ] each ] [ ] make ;
137
138 GENERIC: optimized. ( quot/word -- )
139
140 M: word optimized. specialized-def optimized. ;
141
142 M: callable optimized. build-tree optimize-tree nodes>quot . ;
143
144 SYMBOL: words-called
145 SYMBOL: generics-called
146 SYMBOL: methods-called
147 SYMBOL: intrinsics-called
148 SYMBOL: node-count
149
150 : make-report ( word/quot -- assoc )
151     [
152         build-tree optimize-tree
153
154         H{ } clone words-called set
155         H{ } clone generics-called set
156         H{ } clone methods-called set
157         H{ } clone intrinsics-called set
158
159         0 swap [
160             [ 1 + ] dip
161             dup #call? [
162                 word>> {
163                     { [ dup "intrinsic" word-prop ] [ intrinsics-called ] }
164                     { [ dup generic? ] [ generics-called ] }
165                     { [ dup method-body? ] [ methods-called ] }
166                     [ words-called ]
167                 } cond get inc-at
168             ] [ drop ] if
169         ] each-node
170         node-count set
171     ] H{ } make-assoc ;
172
173 : report. ( report -- )
174     [
175         "==== Total number of IR nodes:" print
176         node-count get .
177
178         {
179             { generics-called "==== Generic word calls:" }
180             { words-called "==== Ordinary word calls:" }
181             { methods-called "==== Non-inlined method calls:" }
182             { intrinsics-called "==== Open-coded intrinsic calls:" }
183         } [
184             nl print get keys natural-sort stack.
185         ] assoc-each
186     ] bind ;
187
188 : optimizer-report. ( word -- )
189     make-report report. ;
190
191 ! More utilities
192
193 : final-info ( quot -- seq )
194     build-tree
195     analyze-recursive
196     normalize
197     propagate
198     compute-def-use
199     dup check-nodes
200     last node-input-infos ;
201
202 : final-classes ( quot -- seq )
203     final-info [ class>> ] map ;
204
205 : final-literals ( quot -- seq )
206     final-info [ literal>> ] map ;
207
208 : cleaned-up-tree ( quot -- nodes )
209     [
210         build-tree
211         analyze-recursive
212         normalize
213         propagate
214         cleanup
215         escape-analysis
216         unbox-tuples
217         apply-identities
218         compute-def-use
219         remove-dead-code
220         compute-def-use
221         optimize-modular-arithmetic 
222     ] with-scope ;
223
224 : inlined? ( quot seq/word -- ? )
225     [ cleaned-up-tree ] dip
226     dup word? [ 1array ] when
227     '[ dup #call? [ word>> _ member? ] [ drop f ] if ]
228     contains-node? not ;