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 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 MATCH-VARS: ?a ?b ?c ;
35 : pretty-shuffle ( effect -- word/f )
36 [ in>> ] [ out>> ] bi 2array {
37 { { { } { } } [ [ ] ] }
38 { { { ?a } { ?a } } [ [ ] ] }
39 { { { ?a ?b } { ?a ?b } } [ [ ] ] }
40 { { { ?a ?b ?c } { ?a ?b ?c } } [ [ ] ] }
41 { { { ?a } { } } [ [ drop ] ] }
42 { { { ?a ?b } { } } [ [ 2drop ] ] }
43 { { { ?a ?b ?c } { } } [ [ 3drop ] ] }
44 { { { ?a } { ?a ?a } } [ [ dup ] ] }
45 { { { ?a ?b } { ?a ?b ?a ?b } } [ [ 2dup ] ] }
46 { { { ?a ?b ?c } { ?a ?b ?c ?a ?b ?c } } [ [ 3dup ] ] }
47 { { { ?a ?b } { ?a ?b ?a } } [ [ over ] ] }
48 { { { ?b ?a } { ?a ?b } } [ [ swap ] ] }
49 { { { ?b ?a ?c } { ?a ?b ?c } } [ [ swapd ] ] }
50 { { { ?a ?b } { ?a ?a ?b } } [ [ dupd ] ] }
51 { { { ?a ?b ?c } { ?a ?b ?c ?a } } [ [ pick ] ] }
52 { { { ?a ?b ?c } { ?c ?a ?b } } [ [ -rot ] ] }
53 { { { ?a ?b ?c } { ?b ?c ?a } } [ [ rot ] ] }
54 { { { ?a ?b } { ?b } } [ [ nip ] ] }
55 { { { ?a ?b ?c } { ?c } } [ [ 2nip ] ] }
59 TUPLE: shuffle-node { effect effect } ;
61 M: shuffle-node pprint* effect>> effect>string text ;
63 : (shuffle-effect) ( in out #shuffle -- effect )
64 mapping>> '[ _ at ] map [ >array ] bi@ <effect> ;
66 : shuffle-effect ( #shuffle -- effect )
67 [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
69 : #>r? ( #shuffle -- ? )
72 [ out-r>> length 1 = ]
77 : #r>? ( #shuffle -- ? )
82 [ out-d>> length 1 = ]
89 { [ dup #>r? ] [ drop \ >R , ] }
90 { [ dup #r>? ] [ drop \ R> , ] }
92 [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
94 shuffle-effect dup pretty-shuffle
95 [ % ] [ shuffle-node boa , ] ?if
98 [ drop "COMPLEX SHUFFLE" , ]
101 M: #push node>quot literal>> literalize , ;
103 M: #call node>quot word>> , ;
105 M: #call-recursive node>quot label>> id>> , ;
111 M: #recursive node>quot
112 [ label>> id>> literalize , ]
113 [ child>> nodes>quot , \ label , ]
117 children>> [ nodes>quot ] map % \ if , ;
119 M: #dispatch node>quot
120 children>> [ nodes>quot ] map , \ dispatch , ;
122 M: #alien-invoke node>quot params>> , \ #alien-invoke , ;
124 M: #alien-indirect node>quot params>> , \ #alien-indirect , ;
126 M: #alien-assembly node>quot params>> , \ #alien-assembly , ;
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? ] [ 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 )
218 optimize-modular-arithmetic
221 : inlined? ( quot seq/word -- ? )
222 [ cleaned-up-tree ] dip
223 dup word? [ 1array ] when
224 '[ dup #call? [ word>> _ member? ] [ drop f ] if ]