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
16 compiler.tree.optimizer
17 compiler.tree.combinators
19 compiler.tree.identities
20 compiler.tree.dead-code
21 compiler.tree.modular-arithmetic ;
24 IN: compiler.tree.debugger
26 ! A simple tool for turning tree IR into quotations and
27 ! printing reports, for debugging purposes.
29 GENERIC: node>quot ( node -- )
31 MACRO: match-choose ( alist -- )
32 [ '[ _ ] ] assoc-map '[ _ match-cond ] ;
34 MATCH-VARS: ?a ?b ?c ;
36 : pretty-shuffle ( effect -- word/f )
37 [ in>> ] [ out>> ] bi 2array {
39 { { { ?a } { ?a } } [ ] }
40 { { { ?a ?b } { ?a ?b } } [ ] }
41 { { { ?a ?b ?c } { ?a ?b ?c } } [ ] }
42 { { { ?a } { } } [ drop ] }
43 { { { ?a ?b } { } } [ 2drop ] }
44 { { { ?a ?b ?c } { } } [ 3drop ] }
45 { { { ?a } { ?a ?a } } [ dup ] }
46 { { { ?a ?b } { ?a ?b ?a ?b } } [ 2dup ] }
47 { { { ?a ?b ?c } { ?a ?b ?c ?a ?b ?c } } [ 3dup ] }
48 { { { ?a ?b } { ?a ?b ?a } } [ over ] }
49 { { { ?b ?a } { ?a ?b } } [ swap ] }
50 { { { ?b ?a ?c } { ?a ?b ?c } } [ swapd ] }
51 { { { ?a ?b } { ?a ?a ?b } } [ dupd ] }
52 { { { ?a ?b } { ?b ?a ?b } } [ tuck ] }
53 { { { ?a ?b ?c } { ?a ?b ?c ?a } } [ pick ] }
54 { { { ?a ?b ?c } { ?c ?a ?b } } [ -rot ] }
55 { { { ?a ?b ?c } { ?b ?c ?a } } [ rot ] }
56 { { { ?a ?b } { ?b } } [ nip ] }
57 { { { ?a ?b ?c } { ?c } } [ 2nip ] }
61 TUPLE: shuffle-node { effect effect } ;
63 M: shuffle-node pprint* effect>> effect>string text ;
65 : (shuffle-effect) ( in out #shuffle -- effect )
66 mapping>> '[ _ at ] map <effect> ;
68 : shuffle-effect ( #shuffle -- effect )
69 [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
71 : #>r? ( #shuffle -- ? )
74 [ out-r>> length 1 = ]
79 : #r>? ( #shuffle -- ? )
84 [ out-d>> length 1 = ]
91 { [ dup #>r? ] [ drop \ >R , ] }
92 { [ dup #r>? ] [ drop \ R> , ] }
94 [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
96 shuffle-effect dup pretty-shuffle
97 [ % ] [ shuffle-node boa , ] ?if
100 [ drop "COMPLEX SHUFFLE" , ]
103 M: #push node>quot literal>> literalize , ;
105 M: #call node>quot word>> , ;
107 M: #call-recursive node>quot label>> id>> , ;
113 M: #recursive node>quot
114 [ label>> id>> literalize , ]
115 [ child>> nodes>quot , \ label , ]
119 children>> [ nodes>quot ] map % \ if , ;
121 M: #dispatch node>quot
122 children>> [ nodes>quot ] map , \ dispatch , ;
124 M: #alien-invoke node>quot params>> , \ #alien-invoke , ;
126 M: #alien-indirect node>quot params>> , \ #alien-indirect , ;
128 M: #alien-callback node>quot params>> , \ #alien-callback , ;
130 M: node node>quot drop ;
132 : nodes>quot ( node -- quot )
133 [ [ node>quot ] each ] [ ] make ;
135 GENERIC: optimized. ( quot/word -- )
137 M: word optimized. specialized-def optimized. ;
139 M: callable optimized. build-tree optimize-tree nodes>quot . ;
142 SYMBOL: generics-called
143 SYMBOL: methods-called
144 SYMBOL: intrinsics-called
147 : make-report ( word/quot -- assoc )
149 build-tree optimize-tree
151 H{ } clone words-called set
152 H{ } clone generics-called set
153 H{ } clone methods-called set
154 H{ } clone intrinsics-called set
160 { [ dup "intrinsic" word-prop ] [ intrinsics-called ] }
161 { [ dup generic? ] [ generics-called ] }
162 { [ dup method-body? ] [ methods-called ] }
170 : report. ( report -- )
172 "==== Total number of IR nodes:" print
176 { generics-called "==== Generic word calls:" }
177 { words-called "==== Ordinary word calls:" }
178 { methods-called "==== Non-inlined method calls:" }
179 { intrinsics-called "==== Open-coded intrinsic calls:" }
181 nl print get keys natural-sort stack.
185 : optimizer-report. ( word -- )
186 make-report report. ;
190 : final-info ( quot -- seq )
197 last node-input-infos ;
199 : final-classes ( quot -- seq )
200 final-info [ class>> ] map ;
202 : final-literals ( quot -- seq )
203 final-info [ literal>> ] map ;
205 : cleaned-up-tree ( quot -- nodes )
216 optimize-modular-arithmetic
219 : inlined? ( quot seq/word -- ? )
220 [ cleaned-up-tree ] dip
221 dup word? [ 1array ] when
222 '[ dup #call? [ word>> _ member? ] [ drop f ] if ]