1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators
4 combinators.short-circuit compiler.cfg.linear-scan.allocation.spilling
5 compiler.cfg.linear-scan.allocation.state
6 compiler.cfg.linear-scan.live-intervals compiler.utilities heaps
7 kernel namespaces sequences ;
8 IN: compiler.cfg.linear-scan.allocation
10 : active-positions ( new assoc -- )
11 swap active-intervals-for [ reg>> 0 2array ] map assoc-union! drop ;
13 : inactive-positions ( new assoc -- )
14 [ [ inactive-intervals-for ] keep ] dip
16 [ _ intersect-intervals 1/0. or ] [ reg>> ] bi
20 : free-positions ( registers reg-class -- avail-registers )
21 of [ 1/0. 2array ] map ;
23 : register-status ( new registers -- free-pos )
24 over interval-reg-class free-positions [
25 [ inactive-positions ] [ active-positions ] 2bi
28 : assign-register ( new registers -- )
29 dupd register-status {
30 { [ dup second 0 = ] [ drop assign-blocked-register ] }
31 { [ 2dup register-available? ] [ register-available ] }
32 [ drop assign-blocked-register ]
35 : spill-at-sync-point? ( sync-point live-interval -- ? )
37 [ drop keep-dst?>> not ]
38 [ [ n>> ] dip find-use [ def-rep>> ] ?call not ]
41 : spill-at-sync-point ( sync-point live-interval -- ? )
42 2dup spill-at-sync-point?
43 [ swap n>> spill f ] [ 2drop t ] if ;
45 GENERIC: handle ( obj -- )
47 M: live-interval-state handle
50 [ deactivate-intervals ] [ activate-intervals ] bi
52 [ registers get assign-register ] bi ;
54 : handle-sync-point ( sync-point active-intervals -- )
55 values [ [ spill-at-sync-point ] with filter! drop ] with each ;
57 M: sync-point handle ( sync-point -- )
58 [ n>> [ deactivate-intervals ] [ activate-intervals ] bi ]
59 [ active-intervals get handle-sync-point ] bi ;
61 : (allocate-registers) ( unhandled-min-heap -- )
62 [ drop handle ] slurp-heap ;
64 : gather-intervals ( -- live-intervals )
66 active-intervals inactive-intervals [ get values concat ] bi@ 3append ;
68 : allocate-registers ( intervals/sync-points registers -- live-intervals' )
69 init-allocator unhandled-min-heap get (allocate-registers)