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 macros arrays
5 prettyprint prettyprint.backend prettyprint.config
6 prettyprint.custom prettyprint.sections math words combinators
7 combinators.short-circuit io sorting hints sets
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 ;
25 FROM: namespaces => set ;
27 IN: compiler.tree.debugger
29 ! A simple tool for turning tree IR into quotations and
30 ! printing reports, for debugging purposes.
32 GENERIC: node>quot ( node -- )
34 MACRO: match-choose ( alist -- )
35 [ '[ _ ] ] assoc-map '[ _ match-cond ] ;
37 MATCH-VARS: ?a ?b ?c ;
39 : pretty-shuffle ( effect -- word/f )
40 [ in>> ] [ out>> ] bi 2array {
42 { { { ?a } { ?a } } [ ] }
43 { { { ?a ?b } { ?a ?b } } [ ] }
44 { { { ?a ?b ?c } { ?a ?b ?c } } [ ] }
45 { { { ?a } { } } [ drop ] }
46 { { { ?a ?b } { } } [ 2drop ] }
47 { { { ?a ?b ?c } { } } [ 3drop ] }
48 { { { ?a } { ?a ?a } } [ dup ] }
49 { { { ?a ?b } { ?a ?b ?a ?b } } [ 2dup ] }
50 { { { ?a ?b ?c } { ?a ?b ?c ?a ?b ?c } } [ 3dup ] }
51 { { { ?a ?b } { ?a ?b ?a } } [ over ] }
52 { { { ?b ?a } { ?a ?b } } [ swap ] }
53 { { { ?b ?a ?c } { ?a ?b ?c } } [ swapd ] }
54 { { { ?a ?b } { ?a ?a ?b } } [ dupd ] }
55 { { { ?a ?b ?c } { ?a ?b ?c ?a } } [ pick ] }
56 { { { ?a ?b ?c } { ?c ?a ?b } } [ -rot ] }
57 { { { ?a ?b ?c } { ?b ?c ?a } } [ rot ] }
58 { { { ?a ?b } { ?b } } [ nip ] }
59 { { { ?a ?b ?c } { ?c } } [ 2nip ] }
63 TUPLE: shuffle-node { effect effect } ;
65 M: shuffle-node pprint* effect>> effect>string text ;
67 : (shuffle-effect) ( in out #shuffle -- effect )
68 mapping>> '[ _ at ] map [ >array ] bi@ <effect> ;
70 : shuffle-effect ( #shuffle -- effect )
71 [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
73 : #>r? ( #shuffle -- ? )
76 [ out-r>> length 1 = ]
81 : #r>? ( #shuffle -- ? )
86 [ out-d>> length 1 = ]
93 { [ dup #>r? ] [ drop \ >R , ] }
94 { [ dup #r>? ] [ drop \ R> , ] }
96 [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
98 shuffle-effect dup pretty-shuffle
99 [ % ] [ shuffle-node boa , ] ?if
102 [ drop "COMPLEX SHUFFLE" , ]
105 M: #push node>quot literal>> literalize , ;
107 M: #call node>quot word>> , ;
109 M: #call-recursive node>quot label>> id>> , ;
115 M: #recursive node>quot
116 [ label>> id>> literalize , ]
117 [ child>> nodes>quot , \ label , ]
121 children>> [ nodes>quot ] map % \ if , ;
123 M: #dispatch node>quot
124 children>> [ nodes>quot ] map , \ dispatch , ;
126 M: #alien-invoke node>quot params>> , #alien-invoke , ;
128 M: #alien-indirect node>quot params>> , #alien-indirect , ;
130 M: #alien-assembly node>quot params>> , #alien-assembly , ;
132 M: #alien-callback node>quot
133 [ params>> , ] [ child>> nodes>quot , ] bi #alien-callback , ;
135 M: node node>quot drop ;
137 : nodes>quot ( node -- quot )
138 [ [ node>quot ] each ] [ ] make ;
140 GENERIC: optimized. ( quot/word -- )
142 M: word optimized. specialized-def optimized. ;
144 M: callable optimized.
145 build-tree optimize-tree nodes>quot
146 [ length-limit off . ] with-scope ;
149 SYMBOL: generics-called
150 SYMBOL: methods-called
151 SYMBOL: intrinsics-called
154 : make-report ( word/quot -- assoc )
156 build-tree optimize-tree
158 H{ } clone words-called set
159 H{ } clone generics-called set
160 H{ } clone methods-called set
161 H{ } clone intrinsics-called set
167 { [ dup "intrinsic" word-prop ] [ intrinsics-called ] }
168 { [ dup generic? ] [ generics-called ] }
169 { [ dup method? ] [ methods-called ] }
177 : report. ( report -- )
179 "==== Total number of IR nodes:" print
183 { generics-called "==== Generic word calls:" }
184 { words-called "==== Ordinary word calls:" }
185 { methods-called "==== Non-inlined method calls:" }
186 { intrinsics-called "==== Open-coded intrinsic calls:" }
188 nl print get keys natural-sort stack.
192 : optimizer-report. ( word -- )
193 make-report report. ;
197 : final-info ( quot -- seq )
204 last node-input-infos ;
206 : final-classes ( quot -- seq )
207 final-info [ class>> ] map ;
209 : final-literals ( quot -- seq )
210 final-info [ literal>> ] map ;
212 : cleaned-up-tree ( quot -- nodes )
225 optimize-modular-arithmetic
228 : inlined? ( quot seq/word -- ? )
229 dup word? [ 1array ] when swap
230 [ cleaned-up-tree [ dup #call? [ word>> , ] [ drop ] if ] each-node ] V{ } make