! Copyright (C) 2008, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: accessors assocs heaps kernel namespaces sequences fry math math.order combinators arrays sorting compiler.utilities compiler.cfg.linear-scan.live-intervals compiler.cfg.linear-scan.allocation.coalescing compiler.cfg.linear-scan.allocation.spilling compiler.cfg.linear-scan.allocation.splitting compiler.cfg.linear-scan.allocation.state ; IN: compiler.cfg.linear-scan.allocation : active-positions ( new assoc -- ) [ vreg>> active-intervals-for ] dip '[ [ 0 ] dip reg>> _ add-use-position ] each ; : inactive-positions ( new assoc -- ) [ [ vreg>> 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 : split-to-fit ( new n -- before after ) split-interval [ [ compute-start/end ] bi@ ] [ >>split-next drop ] [ ] 2tri ; : register-partially-available ( new result -- ) { { [ 2dup second 1 - spill-live-out? ] [ drop spill-live-out ] } { [ 2dup second 1 - spill-live-in? ] [ drop spill-live-in ] } [ [ second 1 - split-to-fit ] keep '[ _ register-available ] [ add-unhandled ] bi* ] } cond ; : assign-register ( new -- ) dup coalesce? [ coalesce ] [ dup register-status { { [ dup no-free-registers? ] [ drop assign-blocked-register ] } { [ 2dup register-available? ] [ register-available ] } ! [ register-partially-available ] [ drop assign-blocked-register ] } cond ] if ; : handle-interval ( live-interval -- ) [ start>> [ progress set ] [ deactivate-intervals ] [ activate-intervals ] tri ] [ assign-register ] bi ; : (allocate-registers) ( -- ) unhandled-intervals get [ handle-interval ] slurp-heap ; : finish-allocation ( -- ) active-intervals inactive-intervals [ get values [ handled-intervals get push-all ] each ] bi@ ; : allocate-registers ( live-intervals machine-registers -- live-intervals ) init-allocator init-unhandled (allocate-registers) finish-allocation handled-intervals get ;