1 ! Copyright (C) 2006, 2008 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
18 compiler.tree.checker ;
20 IN: compiler.tree.debugger
22 ! A simple tool for turning tree IR into quotations and
23 ! printing reports, for debugging purposes.
25 GENERIC: node>quot ( node -- )
27 MACRO: match-choose ( alist -- )
28 [ '[ _ ] ] assoc-map '[ _ match-cond ] ;
30 MATCH-VARS: ?a ?b ?c ;
32 : pretty-shuffle ( effect -- word/f )
33 [ in>> ] [ out>> ] bi 2array {
35 { { { ?a } { ?a } } [ ] }
36 { { { ?a ?b } { ?a ?b } } [ ] }
37 { { { ?a ?b ?c } { ?a ?b ?c } } [ ] }
38 { { { ?a } { } } [ drop ] }
39 { { { ?a ?b } { } } [ 2drop ] }
40 { { { ?a ?b ?c } { } } [ 3drop ] }
41 { { { ?a } { ?a ?a } } [ dup ] }
42 { { { ?a ?b } { ?a ?b ?a ?b } } [ 2dup ] }
43 { { { ?a ?b ?c } { ?a ?b ?c ?a ?b ?c } } [ 3dup ] }
44 { { { ?a ?b } { ?a ?b ?a } } [ over ] }
45 { { { ?b ?a } { ?a ?b } } [ swap ] }
46 { { { ?b ?a ?c } { ?a ?b ?c } } [ swapd ] }
47 { { { ?a ?b } { ?a ?a ?b } } [ dupd ] }
48 { { { ?a ?b } { ?b ?a ?b } } [ tuck ] }
49 { { { ?a ?b ?c } { ?a ?b ?c ?a } } [ pick ] }
50 { { { ?a ?b ?c } { ?c ?a ?b } } [ -rot ] }
51 { { { ?a ?b ?c } { ?b ?c ?a } } [ rot ] }
52 { { { ?a ?b } { ?b } } [ nip ] }
53 { { { ?a ?b ?c } { ?c } } [ 2nip ] }
57 TUPLE: shuffle-node { effect effect } ;
59 M: shuffle-node pprint* effect>> effect>string text ;
61 : (shuffle-effect) ( in out #shuffle -- effect )
62 mapping>> '[ _ at ] map <effect> ;
64 : shuffle-effect ( #shuffle -- effect )
65 [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
67 : #>r? ( #shuffle -- ? )
70 [ out-r>> length 1 = ]
75 : #r>? ( #shuffle -- ? )
80 [ out-d>> length 1 = ]
87 { [ dup #>r? ] [ drop \ >R , ] }
88 { [ dup #r>? ] [ drop \ R> , ] }
90 [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ]
92 shuffle-effect dup pretty-shuffle
93 [ % ] [ shuffle-node boa , ] ?if
96 [ drop "COMPLEX SHUFFLE" , ]
99 M: #push node>quot literal>> literalize , ;
101 M: #call node>quot word>> , ;
103 M: #call-recursive node>quot label>> id>> , ;
109 M: #recursive node>quot
110 [ label>> id>> literalize , ]
111 [ child>> nodes>quot , \ label , ]
115 children>> [ nodes>quot ] map % \ if , ;
117 M: #dispatch node>quot
118 children>> [ nodes>quot ] map , \ dispatch , ;
120 M: #alien-invoke node>quot params>> , \ #alien-invoke , ;
122 M: #alien-indirect node>quot params>> , \ #alien-indirect , ;
124 M: #alien-callback node>quot params>> , \ #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: method-spec optimized. first2 method optimized. ;
135 M: word optimized. specialized-def optimized. ;
137 M: callable optimized. build-tree optimize-tree nodes>quot . ;
140 SYMBOL: generics-called
141 SYMBOL: methods-called
142 SYMBOL: intrinsics-called
145 : make-report ( word/quot -- assoc )
147 dup word? [ build-tree-from-word nip ] [ build-tree ] if
150 H{ } clone words-called set
151 H{ } clone generics-called set
152 H{ } clone methods-called set
153 H{ } clone intrinsics-called set
159 { [ dup "intrinsics" word-prop over "if-intrinsics" word-prop or ] [ intrinsics-called ] }
160 { [ dup generic? ] [ generics-called ] }
161 { [ dup method-body? ] [ methods-called ] }
169 : report. ( report -- )
171 "==== Total number of IR nodes:" print
175 { generics-called "==== Generic word calls:" }
176 { words-called "==== Ordinary word calls:" }
177 { methods-called "==== Non-inlined method calls:" }
178 { intrinsics-called "==== Open-coded intrinsic calls:" }
180 nl print get keys natural-sort stack.
184 : optimizer-report. ( word -- )
185 make-report report. ;
189 : final-info ( quot -- seq )
196 peek node-input-infos ;
198 : final-classes ( quot -- seq )
199 final-info [ class>> ] map ;
201 : final-literals ( quot -- seq )
202 final-info [ literal>> ] map ;
204 : cleaned-up-tree ( quot -- nodes )
207 build-tree optimize-tree
210 : inlined? ( quot seq/word -- ? )
211 [ cleaned-up-tree ] dip
212 dup word? [ 1array ] when
213 '[ dup #call? [ word>> _ member? ] [ drop f ] if ]