1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators hashtables kernel
4 math fry namespaces make sequences words byte-arrays
6 stack-checker.inlining cpu.architecture
9 compiler.tree.combinators
10 compiler.tree.propagation.info
13 compiler.cfg.utilities
14 compiler.cfg.registers
15 compiler.cfg.intrinsics
16 compiler.cfg.comparisons
17 compiler.cfg.stack-frame
18 compiler.cfg.instructions
19 compiler.cfg.predecessors
20 compiler.cfg.builder.blocks
22 compiler.cfg.stacks.local ;
23 IN: compiler.cfg.builder
25 ! Convert tree SSA IR to CFG IR. The result is not in SSA form; this is
26 ! constructed later by calling compiler.cfg.ssa.construction:construct-ssa.
31 : begin-cfg ( word label -- cfg )
34 [ basic-block get ] 2dip <cfg> dup cfg set ;
36 : begin-procedure ( word label -- )
37 begin-cfg procedures get push ;
39 : with-cfg-builder ( nodes word label quot -- )
47 : with-dummy-cfg-builder ( node quot -- )
49 [ V{ } clone procedures ] 2dip
50 '[ _ t t [ _ call( node -- ) ] with-cfg-builder ] with-variable
53 GENERIC: emit-node ( node -- )
55 : emit-nodes ( nodes -- )
56 [ basic-block get [ emit-node ] [ drop ] if ] each ;
65 : (build-cfg) ( nodes word label -- )
71 : build-cfg ( nodes word -- procedures )
78 : emit-loop-call ( basic-block -- )
81 basic-block get successors>> push
84 : emit-call ( word height -- )
86 [ drop loops get at emit-loop-call ]
89 [ ##call, ] [ adjust-d ] bi*
95 : recursive-height ( #recursive -- n )
96 [ label>> return>> in-d>> length ] [ in-d>> length ] bi - ;
98 : emit-recursive ( #recursive -- )
99 [ [ label>> id>> ] [ recursive-height ] bi emit-call ]
100 [ [ child>> ] [ label>> word>> ] [ label>> id>> ] tri (build-cfg) ] bi ;
102 : remember-loop ( label -- )
103 basic-block get swap loops get set-at ;
105 : emit-loop ( node -- )
108 [ label>> id>> remember-loop ] [ child>> emit-nodes ] bi ;
110 M: #recursive emit-node
111 dup label>> loop?>> [ emit-loop ] [ emit-recursive ] if ;
114 : emit-branch ( obj -- final-bb )
115 [ emit-nodes ] with-branch ;
117 : emit-if ( node -- )
118 children>> [ emit-branch ] map emit-conditional ;
120 : trivial-branch? ( nodes -- value ? )
122 first dup #push? [ literal>> t ] [ drop f f ] if
125 : trivial-if? ( #if -- ? )
127 [ trivial-branch? [ t eq? ] when ]
128 [ trivial-branch? [ f eq? ] when ] bi*
131 : emit-trivial-if ( -- )
132 [ f cc/= ^^compare-imm ] unary-op ;
134 : trivial-not-if? ( #if -- ? )
136 [ trivial-branch? [ f eq? ] when ]
137 [ trivial-branch? [ t eq? ] when ] bi*
140 : emit-trivial-not-if ( -- )
141 [ f cc= ^^compare-imm ] unary-op ;
143 : emit-actual-if ( #if -- )
144 ! Inputs to the final instruction need to be copied because of
146 ds-pop any-rep ^^copy f cc/= ##compare-imm-branch, emit-if ;
150 { [ dup trivial-if? ] [ drop emit-trivial-if ] }
151 { [ dup trivial-not-if? ] [ drop emit-trivial-not-if ] }
156 M: #dispatch emit-node
157 ! Inputs to the final instruction need to be copied because of
158 ! loc>vreg sync. ^^offset>slot always returns a fresh vreg,
160 ds-pop ^^offset>slot next-vreg ##dispatch, emit-if ;
164 dup word>> dup "intrinsic" word-prop
165 [ emit-intrinsic ] [ swap call-height emit-call ] if ;
168 M: #call-recursive emit-node [ label>> id>> ] [ call-height ] bi emit-call ;
172 literal>> ^^load-literal ds-push ;
176 ! Even though low level IR has its own dead code elimination pass,
177 ! we try not to introduce useless ##peeks here, since this reduces
178 ! the accuracy of global stack analysis.
180 : make-input-map ( #shuffle -- assoc )
181 ! Assoc maps high-level IR values to stack locations.
183 [ in-d>> <reversed> [ <ds-loc> swap set ] each-index ]
184 [ in-r>> <reversed> [ <rs-loc> swap set ] each-index ] bi
187 : make-output-seq ( values mapping input-map -- vregs )
188 '[ _ at _ at peek-loc ] map ;
190 : load-shuffle ( #shuffle mapping input-map -- ds-vregs rs-vregs )
191 [ [ out-d>> ] 2dip make-output-seq ]
192 [ [ out-r>> ] 2dip make-output-seq ] 3bi ;
194 : store-shuffle ( #shuffle ds-vregs rs-vregs -- )
195 [ [ in-d>> length neg inc-d ] dip ds-store ]
196 [ [ in-r>> length neg inc-r ] dip rs-store ]
199 M: #shuffle emit-node
200 dup dup [ mapping>> ] [ make-input-map ] bi load-shuffle store-shuffle ;
211 M: #return emit-node drop end-word ;
213 M: #return-recursive emit-node
214 label>> id>> loops get key? [ end-word ] unless ;
217 M: #terminate emit-node drop ##no-tco, end-basic-block ;
220 M: #introduce emit-node drop ;
222 M: #copy emit-node drop ;
224 M: #enter-recursive emit-node drop ;
226 M: #phi emit-node drop ;
228 M: #declare emit-node drop ;