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
9 compiler.tree.recursive
10 compiler.tree.normalization
12 compiler.tree.propagation
13 compiler.tree.propagation.info
16 compiler.tree.optimizer
17 compiler.tree.combinators
18 compiler.tree.checker ;
21 IN: compiler.tree.debugger
23 ! A simple tool for turning tree IR into quotations and
24 ! printing reports, for debugging purposes.
26 GENERIC: node>quot ( node -- )
28 MACRO: match-choose ( alist -- )
29 [ '[ _ ] ] assoc-map '[ _ match-cond ] ;
31 MATCH-VARS: ?a ?b ?c ;
33 : pretty-shuffle ( effect -- word/f )
34 [ in>> ] [ out>> ] bi 2array {
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 ] }
58 TUPLE: shuffle-node { effect effect } ;
60 M: shuffle-node pprint* effect>> effect>string text ;
62 : (shuffle-effect) ( in out #shuffle -- effect )
63 mapping>> '[ _ at ] map <effect> ;
65 : shuffle-effect ( #shuffle -- effect )
66 [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
68 : #>r? ( #shuffle -- ? )
71 [ out-r>> length 1 = ]
76 : #r>? ( #shuffle -- ? )
81 [ out-d>> length 1 = ]
88 { [ dup #>r? ] [ drop \ >R , ] }
89 { [ dup #r>? ] [ drop \ R> , ] }
91 [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
93 shuffle-effect dup pretty-shuffle
94 [ % ] [ shuffle-node boa , ] ?if
97 [ drop "COMPLEX SHUFFLE" , ]
100 M: #push node>quot literal>> literalize , ;
102 M: #call node>quot word>> , ;
104 M: #call-recursive node>quot label>> id>> , ;
110 M: #recursive node>quot
111 [ label>> id>> literalize , ]
112 [ child>> nodes>quot , \ label , ]
116 children>> [ nodes>quot ] map % \ if , ;
118 M: #dispatch node>quot
119 children>> [ nodes>quot ] map , \ dispatch , ;
121 M: #alien-invoke node>quot params>> , \ #alien-invoke , ;
123 M: #alien-indirect node>quot params>> , \ #alien-indirect , ;
125 M: #alien-callback node>quot params>> , \ #alien-callback , ;
127 M: node node>quot drop ;
129 : nodes>quot ( node -- quot )
130 [ [ node>quot ] each ] [ ] make ;
132 GENERIC: optimized. ( quot/word -- )
134 M: word optimized. specialized-def optimized. ;
136 M: callable optimized. build-tree optimize-tree nodes>quot . ;
139 SYMBOL: generics-called
140 SYMBOL: methods-called
141 SYMBOL: intrinsics-called
144 : make-report ( word/quot -- assoc )
146 build-tree optimize-tree
148 H{ } clone words-called set
149 H{ } clone generics-called set
150 H{ } clone methods-called set
151 H{ } clone intrinsics-called set
157 { [ dup "intrinsic" word-prop ] [ intrinsics-called ] }
158 { [ dup generic? ] [ generics-called ] }
159 { [ dup method-body? ] [ methods-called ] }
167 : report. ( report -- )
169 "==== Total number of IR nodes:" print
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:" }
178 nl print get keys natural-sort stack.
182 : optimizer-report. ( word -- )
183 make-report report. ;
187 : final-info ( quot -- seq )
194 peek node-input-infos ;
196 : final-classes ( quot -- seq )
197 final-info [ class>> ] map ;
199 : final-literals ( quot -- seq )
200 final-info [ literal>> ] map ;
202 : cleaned-up-tree ( quot -- nodes )
205 build-tree optimize-tree
208 : inlined? ( quot seq/word -- ? )
209 [ cleaned-up-tree ] dip
210 dup word? [ 1array ] when
211 '[ dup #call? [ word>> _ member? ] [ drop f ] if ]