1 ! Copyright (C) 2006, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators
4 combinators.short-circuit compiler.tree compiler.tree.builder
5 compiler.tree.checker compiler.tree.cleanup
6 compiler.tree.combinators compiler.tree.dead-code
7 compiler.tree.def-use compiler.tree.escape-analysis
8 compiler.tree.identities compiler.tree.modular-arithmetic
9 compiler.tree.normalization compiler.tree.optimizer
10 compiler.tree.propagation compiler.tree.propagation.info
11 compiler.tree.recursive compiler.tree.tuple-unboxing effects fry
12 generic hints io kernel macros make match math namespaces
13 prettyprint prettyprint.config prettyprint.custom
14 prettyprint.sections quotations sequences sequences.private sets
18 IN: compiler.tree.debugger
20 ! A simple tool for turning tree IR into quotations and
21 ! printing reports, for debugging purposes.
23 GENERIC: node>quot ( node -- )
25 MACRO: match-choose ( alist -- quot )
26 [ '[ _ ] ] assoc-map '[ _ match-cond ] ;
28 MATCH-VARS: ?a ?b ?c ;
30 : pretty-shuffle ( effect -- word/f )
31 [ in>> ] [ out>> ] bi 2array {
33 { { { ?a } { ?a } } [ ] }
34 { { { ?a ?b } { ?a ?b } } [ ] }
35 { { { ?a ?b ?c } { ?a ?b ?c } } [ ] }
36 { { { ?a } { } } [ drop ] }
37 { { { ?a ?b } { } } [ 2drop ] }
38 { { { ?a ?b ?c } { } } [ 3drop ] }
39 { { { ?a } { ?a ?a } } [ dup ] }
40 { { { ?a ?b } { ?a ?b ?a ?b } } [ 2dup ] }
41 { { { ?a ?b ?c } { ?a ?b ?c ?a ?b ?c } } [ 3dup ] }
42 { { { ?a ?b } { ?a ?b ?a } } [ over ] }
43 { { { ?b ?a } { ?a ?b } } [ swap ] }
44 { { { ?b ?a ?c } { ?a ?b ?c } } [ swapd ] }
45 { { { ?a ?b } { ?a ?a ?b } } [ dupd ] }
46 { { { ?a ?b ?c } { ?a ?b ?c ?a } } [ pick ] }
47 { { { ?a ?b ?c } { ?c ?a ?b } } [ -rot ] }
48 { { { ?a ?b ?c } { ?b ?c ?a } } [ rot ] }
49 { { { ?a ?b } { ?b } } [ nip ] }
50 { { { ?a ?b ?c } { ?c } } [ 2nip ] }
54 TUPLE: shuffle-node { effect effect } ;
56 M: shuffle-node pprint* effect>> effect>string text ;
58 : (shuffle-effect) ( in out #shuffle -- effect )
59 mapping>> '[ _ at ] map [ >array ] bi@ <effect> ;
61 : shuffle-effect ( #shuffle -- effect )
62 [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
64 : #>r? ( #shuffle -- ? )
67 [ out-r>> length 1 = ]
72 : #r>? ( #shuffle -- ? )
77 [ out-d>> length 1 = ]
84 { [ dup #>r? ] [ drop \ >R , ] }
85 { [ dup #r>? ] [ drop \ R> , ] }
87 [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
89 shuffle-effect dup pretty-shuffle
90 [ % ] [ shuffle-node boa , ] ?if
93 [ drop "COMPLEX SHUFFLE" , ]
96 M: #push node>quot literal>> literalize , ;
98 M: #call node>quot word>> , ;
100 M: #call-recursive node>quot label>> id>> , ;
106 M: #recursive node>quot
107 [ label>> id>> literalize , ]
108 [ child>> nodes>quot , \ label , ]
112 children>> [ nodes>quot ] map % \ if , ;
114 M: #dispatch node>quot
115 children>> [ nodes>quot ] map , \ dispatch , ;
117 M: #alien-invoke node>quot params>> , #alien-invoke , ;
119 M: #alien-indirect node>quot params>> , #alien-indirect , ;
121 M: #alien-assembly node>quot params>> , #alien-assembly , ;
123 M: #alien-callback node>quot
124 [ params>> , ] [ child>> nodes>quot , ] bi #alien-callback , ;
126 M: node node>quot drop ;
128 : nodes>quot ( node -- quot )
129 [ [ node>quot ] each ] [ ] make ;
131 GENERIC: optimized. ( quot/word -- )
133 M: word optimized. specialized-def optimized. ;
135 M: callable optimized.
136 build-tree optimize-tree nodes>quot
137 f length-limit [ . ] with-variable ;
140 SYMBOL: generics-called
141 SYMBOL: methods-called
142 SYMBOL: intrinsics-called
145 : make-report ( word/quot -- assoc )
147 build-tree optimize-tree
149 H{ } clone words-called ,,
150 H{ } clone generics-called ,,
151 H{ } clone methods-called ,,
152 H{ } clone intrinsics-called ,,
158 { [ dup "intrinsic" word-prop ] [ intrinsics-called ] }
159 { [ dup generic? ] [ generics-called ] }
160 { [ dup method? ] [ methods-called ] }
162 } cond building get at inc-at
168 : report. ( report -- )
170 "==== Total number of IR nodes:" print
174 { generics-called "==== Generic word calls:" }
175 { words-called "==== Ordinary word calls:" }
176 { methods-called "==== Non-inlined method calls:" }
177 { intrinsics-called "==== Open-coded intrinsic calls:" }
179 nl print get keys natural-sort stack.
183 : optimizer-report. ( word -- )
184 make-report report. ;
187 : cleaned-up-tree ( quot -- nodes )
200 optimize-modular-arithmetic
203 : inlined? ( quot seq/word -- ? )
204 dup word? [ 1array ] when swap
205 [ cleaned-up-tree [ dup #call? [ word>> , ] [ drop ] if ] each-node ] V{ } make