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
9 compiler.tree.recursive
10 compiler.tree.normalization
12 compiler.tree.propagation
13 compiler.tree.propagation.info
14 compiler.tree.escape-analysis
15 compiler.tree.tuple-unboxing
18 compiler.tree.optimizer
19 compiler.tree.combinators
21 compiler.tree.identities
22 compiler.tree.dead-code
23 compiler.tree.modular-arithmetic ;
26 IN: compiler.tree.debugger
28 ! A simple tool for turning tree IR into quotations and
29 ! printing reports, for debugging purposes.
31 GENERIC: node>quot ( node -- )
33 MACRO: match-choose ( alist -- )
34 [ '[ _ ] ] assoc-map '[ _ match-cond ] ;
36 MATCH-VARS: ?a ?b ?c ;
38 : pretty-shuffle ( effect -- word/f )
39 [ in>> ] [ out>> ] bi 2array {
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 ] }
62 TUPLE: shuffle-node { effect effect } ;
64 M: shuffle-node pprint* effect>> effect>string text ;
66 : (shuffle-effect) ( in out #shuffle -- effect )
67 mapping>> '[ _ at ] map <effect> ;
69 : shuffle-effect ( #shuffle -- effect )
70 [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
72 : #>r? ( #shuffle -- ? )
75 [ out-r>> length 1 = ]
80 : #r>? ( #shuffle -- ? )
85 [ out-d>> length 1 = ]
92 { [ dup #>r? ] [ drop \ >R , ] }
93 { [ dup #r>? ] [ drop \ R> , ] }
95 [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
97 shuffle-effect dup pretty-shuffle
98 [ % ] [ shuffle-node boa , ] ?if
101 [ drop "COMPLEX SHUFFLE" , ]
104 M: #push node>quot literal>> literalize , ;
106 M: #call node>quot word>> , ;
108 M: #call-recursive node>quot label>> id>> , ;
114 M: #recursive node>quot
115 [ label>> id>> literalize , ]
116 [ child>> nodes>quot , \ label , ]
120 children>> [ nodes>quot ] map % \ if , ;
122 M: #dispatch node>quot
123 children>> [ nodes>quot ] map , \ dispatch , ;
125 M: #alien-invoke node>quot params>> , \ #alien-invoke , ;
127 M: #alien-indirect node>quot params>> , \ #alien-indirect , ;
129 M: #alien-callback node>quot params>> , \ #alien-callback , ;
131 M: node node>quot drop ;
133 : nodes>quot ( node -- quot )
134 [ [ node>quot ] each ] [ ] make ;
136 GENERIC: optimized. ( quot/word -- )
138 M: word optimized. specialized-def optimized. ;
140 M: callable optimized. build-tree optimize-tree nodes>quot . ;
143 SYMBOL: generics-called
144 SYMBOL: methods-called
145 SYMBOL: intrinsics-called
148 : make-report ( word/quot -- assoc )
150 build-tree optimize-tree
152 H{ } clone words-called set
153 H{ } clone generics-called set
154 H{ } clone methods-called set
155 H{ } clone intrinsics-called set
161 { [ dup "intrinsic" word-prop ] [ intrinsics-called ] }
162 { [ dup generic? ] [ generics-called ] }
163 { [ dup method-body? ] [ methods-called ] }
171 : report. ( report -- )
173 "==== Total number of IR nodes:" print
177 { generics-called "==== Generic word calls:" }
178 { words-called "==== Ordinary word calls:" }
179 { methods-called "==== Non-inlined method calls:" }
180 { intrinsics-called "==== Open-coded intrinsic calls:" }
182 nl print get keys natural-sort stack.
186 : optimizer-report. ( word -- )
187 make-report report. ;
191 : final-info ( quot -- seq )
198 last node-input-infos ;
200 : final-classes ( quot -- seq )
201 final-info [ class>> ] map ;
203 : final-literals ( quot -- seq )
204 final-info [ literal>> ] map ;
206 : cleaned-up-tree ( quot -- nodes )
219 optimize-modular-arithmetic
222 : inlined? ( quot seq/word -- ? )
223 [ cleaned-up-tree ] dip
224 dup word? [ 1array ] when
225 '[ dup #call? [ word>> _ member? ] [ drop f ] if ]