1 ! Copyright (C) 2004, 2009 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
5 layouts alien.c-types alien.structs
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
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 GENERIC: emit-node ( node -- )
49 : emit-nodes ( nodes -- )
50 [ basic-block get [ emit-node ] [ drop ] if ] each ;
57 : (build-cfg) ( nodes word label -- )
63 : build-cfg ( nodes word -- procedures )
70 : emit-loop-call ( basic-block -- )
72 basic-block get successors>> push
75 : emit-call ( word height -- )
77 [ drop loops get at emit-loop-call ]
78 [ [ [ ##call ] [ adjust-d ] bi* ] emit-trivial-block ]
82 : recursive-height ( #recursive -- n )
83 [ label>> return>> in-d>> length ] [ in-d>> length ] bi - ;
85 : emit-recursive ( #recursive -- )
86 [ [ label>> id>> ] [ recursive-height ] bi emit-call ]
87 [ [ child>> ] [ label>> word>> ] [ label>> id>> ] tri (build-cfg) ] bi ;
89 : remember-loop ( label -- )
90 basic-block get swap loops get set-at ;
92 : emit-loop ( node -- )
95 [ label>> id>> remember-loop ] [ child>> emit-nodes ] bi ;
97 M: #recursive emit-node
98 dup label>> loop?>> [ emit-loop ] [ emit-recursive ] if ;
101 : emit-branch ( obj -- final-bb )
102 [ emit-nodes ] with-branch ;
104 : emit-if ( node -- )
105 children>> [ emit-branch ] map emit-conditional ;
107 : trivial-branch? ( nodes -- value ? )
109 first dup #push? [ literal>> t ] [ drop f f ] if
112 : trivial-if? ( #if -- ? )
114 [ trivial-branch? [ t eq? ] when ]
115 [ trivial-branch? [ f eq? ] when ] bi*
118 : emit-trivial-if ( -- )
119 ds-pop \ f tag-number cc/= ^^compare-imm ds-push ;
121 : trivial-not-if? ( #if -- ? )
123 [ trivial-branch? [ f eq? ] when ]
124 [ trivial-branch? [ t eq? ] when ] bi*
127 : emit-trivial-not-if ( -- )
128 ds-pop \ f tag-number cc= ^^compare-imm ds-push ;
130 : emit-actual-if ( #if -- )
131 ! Inputs to the final instruction need to be copied because of
133 ds-pop ^^copy \ f tag-number cc/= ##compare-imm-branch emit-if ;
137 { [ dup trivial-if? ] [ drop emit-trivial-if ] }
138 { [ dup trivial-not-if? ] [ drop emit-trivial-not-if ] }
143 M: #dispatch emit-node
144 ! Inputs to the final instruction need to be copied because of
145 ! loc>vreg sync. ^^offset>slot always returns a fresh vreg,
147 ds-pop ^^offset>slot i ##dispatch emit-if ;
151 dup word>> dup "intrinsic" word-prop
152 [ emit-intrinsic ] [ swap call-height emit-call ] if ;
155 M: #call-recursive emit-node [ label>> id>> ] [ call-height ] bi emit-call ;
159 literal>> ^^load-literal ds-push ;
162 M: #shuffle emit-node
165 [ [ in-d>> [ length ds-load ] keep ] dip '[ _ set-at ] 2each ]
166 [ [ in-r>> [ length rs-load ] keep ] dip '[ _ set-at ] 2each ]
168 [ [ [ out-d>> ] [ mapping>> ] bi ] dip '[ _ at _ at ] map ds-store ]
169 [ [ [ out-r>> ] [ mapping>> ] bi ] dip '[ _ at _ at ] map rs-store ] 2bi ;
173 ##branch begin-basic-block ##epilogue ##return ;
175 M: #return emit-node drop emit-return ;
177 M: #return-recursive emit-node
178 label>> id>> loops get key? [ emit-return ] unless ;
181 M: #terminate emit-node drop ##no-tco end-basic-block ;
184 : return-size ( ctype -- n )
185 #! Amount of space we reserve for a return value.
187 { [ dup c-struct? not ] [ drop 0 ] }
188 { [ dup large-struct? not ] [ drop 2 cells ] }
192 : <alien-stack-frame> ( params -- stack-frame )
195 [ return>> return-size >>return ]
196 [ alien-parameters parameter-sizes drop >>params ] bi ;
198 : alien-node-height ( params -- )
199 [ out-d>> length ] [ in-d>> length ] bi - adjust-d ;
201 : emit-alien-node ( node quot -- )
203 [ params>> dup dup <alien-stack-frame> ] dip call
205 ] emit-trivial-block ; inline
207 M: #alien-invoke emit-node
208 [ ##alien-invoke ] emit-alien-node ;
210 M: #alien-indirect emit-node
211 [ ##alien-indirect ] emit-alien-node ;
213 M: #alien-callback emit-node
214 dup params>> xt>> dup
217 dup [ ##alien-callback ] emit-alien-node
219 params>> ##callback-return
223 M: #introduce emit-node drop ;
225 M: #copy emit-node drop ;
227 M: #enter-recursive emit-node drop ;
229 M: #phi emit-node drop ;