factor/basis/compiler/cfg/linear-scan/allocation/allocation.factor

74 lines
2.4 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2010 Slava Pestov.
2008-09-10 23:11:03 -04:00
! See http://factorcode.org/license.txt for BSD license.
2014-12-13 19:10:21 -05:00
USING: accessors assocs combinators combinators.short-circuit
compiler.cfg.linear-scan.allocation.spilling
2014-12-13 19:10:21 -05:00
compiler.cfg.linear-scan.allocation.state
compiler.cfg.linear-scan.live-intervals compiler.utilities fry
heaps kernel locals math namespaces sequences ;
2008-09-11 03:05:22 -04:00
IN: compiler.cfg.linear-scan.allocation
2008-09-10 23:11:03 -04:00
: active-positions ( new assoc -- )
2010-04-28 03:35:46 -04:00
[ active-intervals-for ] dip
'[ [ 0 ] dip reg>> _ add-use-position ] each ;
: inactive-positions ( new assoc -- )
2010-04-28 03:35:46 -04:00
[ [ inactive-intervals-for ] keep ] dip
'[
[ _ relevant-ranges intersect-live-ranges 1/0. or ] [ reg>> ] bi
_ add-use-position
] each ;
: register-status ( new -- free-pos )
dup free-positions
[ inactive-positions ] [ active-positions ] [ nip ] 2tri
>alist alist-max ;
: no-free-registers? ( result -- ? )
second 0 = ; inline
: assign-register ( new -- )
dup register-status {
{ [ dup no-free-registers? ] [ drop assign-blocked-register ] }
{ [ 2dup register-available? ] [ register-available ] }
[ drop assign-blocked-register ]
} cond ;
2008-09-10 23:11:03 -04:00
: spill-at-sync-point? ( sync-point live-interval -- ? )
! If the live interval has a definition at a keep-dst?
! sync-point, don't spill.
{
[ drop keep-dst?>> not ]
[ [ n>> ] dip find-use dup [ def-rep>> ] when not ]
} 2|| ;
: spill-at-sync-point ( sync-point live-interval -- ? )
2dup spill-at-sync-point?
[ swap n>> spill f ] [ 2drop t ] if ;
GENERIC: handle ( obj -- )
M: live-interval-state handle
[ start>> [ deactivate-intervals ] [ activate-intervals ] bi ]
[ assign-register ] bi ;
: handle-sync-point ( sync-point -- )
active-intervals get values
[ [ spill-at-sync-point ] with filter! drop ] with each ;
M: sync-point handle ( sync-point -- )
[ n>> [ deactivate-intervals ] [ activate-intervals ] bi ]
[ handle-sync-point ] bi ;
: (allocate-registers) ( unhandled-min-heap -- )
[ drop handle ] slurp-heap ;
2008-09-15 02:54:48 -04:00
: finish-allocation ( -- )
active-intervals inactive-intervals
[ get values [ handled-intervals get push-all ] each ] bi@ ;
: allocate-registers ( live-intervals sync-point machine-registers -- live-intervals )
init-allocator
unhandled-min-heap get (allocate-registers)
finish-allocation
handled-intervals get ;