2009-06-04 19:53:02 -04:00
|
|
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
2008-09-10 23:11:03 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-06-17 16:27:20 -04:00
|
|
|
USING: accessors assocs heaps kernel namespaces sequences fry math
|
2009-06-19 19:28:30 -04:00
|
|
|
combinators arrays sorting compiler.utilities
|
2009-06-23 22:32:51 -04:00
|
|
|
compiler.cfg.linear-scan.live-intervals
|
2009-06-11 18:55:14 -04:00
|
|
|
compiler.cfg.linear-scan.allocation.coalescing
|
|
|
|
compiler.cfg.linear-scan.allocation.spilling
|
|
|
|
compiler.cfg.linear-scan.allocation.splitting
|
|
|
|
compiler.cfg.linear-scan.allocation.state ;
|
2008-09-11 03:05:22 -04:00
|
|
|
IN: compiler.cfg.linear-scan.allocation
|
2008-09-10 23:11:03 -04:00
|
|
|
|
2009-06-23 22:32:51 -04:00
|
|
|
: free-positions ( new -- assoc )
|
|
|
|
vreg>> reg-class>> registers get at [ 1/0. ] H{ } map>assoc ;
|
|
|
|
|
|
|
|
: active-positions ( new -- assoc )
|
|
|
|
vreg>> active-intervals-for [ reg>> 0 ] H{ } map>assoc ;
|
|
|
|
|
|
|
|
: inactive-positions ( new -- assoc )
|
|
|
|
dup vreg>> inactive-intervals-for
|
|
|
|
[ [ reg>> swap ] keep relevant-ranges intersect-live-ranges ]
|
|
|
|
with H{ } map>assoc ;
|
2009-06-17 16:27:20 -04:00
|
|
|
|
|
|
|
: compute-free-pos ( new -- free-pos )
|
2009-06-23 22:32:51 -04:00
|
|
|
[ free-positions ] [ inactive-positions ] [ active-positions ] tri
|
|
|
|
3array assoc-combine >alist alist-max ;
|
2009-06-17 16:27:20 -04:00
|
|
|
|
2009-06-19 04:42:42 -04:00
|
|
|
: no-free-registers? ( result -- ? )
|
2009-06-17 16:27:20 -04:00
|
|
|
second 0 = ; inline
|
|
|
|
|
|
|
|
: register-available? ( new result -- ? )
|
|
|
|
[ end>> ] [ second ] bi* < ; inline
|
|
|
|
|
|
|
|
: register-available ( new result -- )
|
|
|
|
first >>reg add-active ;
|
|
|
|
|
|
|
|
: register-partially-available ( new result -- )
|
|
|
|
[ second split-before-use ] keep
|
|
|
|
'[ _ register-available ] [ add-unhandled ] bi* ;
|
|
|
|
|
2009-06-04 19:53:02 -04:00
|
|
|
: assign-register ( new -- )
|
|
|
|
dup coalesce? [ coalesce ] [
|
2009-06-19 19:28:30 -04:00
|
|
|
dup compute-free-pos {
|
2009-06-17 16:27:20 -04:00
|
|
|
{ [ dup no-free-registers? ] [ drop assign-blocked-register ] }
|
|
|
|
{ [ 2dup register-available? ] [ register-available ] }
|
|
|
|
[ register-partially-available ]
|
|
|
|
} cond
|
2008-11-02 02:49:57 -05:00
|
|
|
] if ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
2008-09-15 02:54:48 -04:00
|
|
|
: handle-interval ( live-interval -- )
|
2009-06-04 19:53:02 -04:00
|
|
|
[
|
|
|
|
start>>
|
|
|
|
[ progress set ]
|
|
|
|
[ deactivate-intervals ]
|
|
|
|
[ activate-intervals ] tri
|
|
|
|
] [ assign-register ] bi ;
|
2008-09-15 02:54:48 -04:00
|
|
|
|
|
|
|
: (allocate-registers) ( -- )
|
|
|
|
unhandled-intervals get [ handle-interval ] slurp-heap ;
|
|
|
|
|
2009-06-04 19:53:02 -04:00
|
|
|
: finish-allocation ( -- )
|
|
|
|
active-intervals inactive-intervals
|
|
|
|
[ get values [ handled-intervals get push-all ] each ] bi@ ;
|
|
|
|
|
2008-09-15 05:22:12 -04:00
|
|
|
: allocate-registers ( live-intervals machine-registers -- live-intervals )
|
2008-09-17 20:31:35 -04:00
|
|
|
init-allocator
|
2009-06-04 19:53:02 -04:00
|
|
|
init-unhandled
|
|
|
|
(allocate-registers)
|
|
|
|
finish-allocation
|
|
|
|
handled-intervals get ;
|