1 ! Copyright (C) 2009, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors binary-search combinators
4 compiler.cfg.linear-scan.allocation.state
5 compiler.cfg.linear-scan.live-intervals fry hints kernel locals
6 math math.order namespaces sequences ;
7 IN: compiler.cfg.linear-scan.allocation.splitting
9 : split-range ( live-range n -- before after )
10 [ [ from>> ] dip <live-range> ]
11 [ 1 + swap to>> <live-range> ]
14 : split-last-range? ( last n -- ? )
17 : split-last-range ( before after last n -- before' after' )
18 split-range [ [ but-last ] dip suffix ] [ prefix ] bi-curry* bi* ;
20 : split-ranges ( live-ranges n -- before after )
21 [ '[ from>> _ <= ] partition ]
23 [ over last ] dip 2dup split-last-range?
24 [ split-last-range ] [ 2drop ] if
27 :: split-uses ( uses n -- before after )
28 uses n uses [ n>> <=> ] with search
30 { +eq+ [ [ head-slice ] [ 1 + tail-slice ] 2bi ] }
31 { +lt+ [ 1 + cut-slice ] }
32 { +gt+ [ cut-slice ] }
35 ERROR: splitting-too-early ;
37 ERROR: splitting-too-late ;
39 ERROR: splitting-atomic-interval ;
41 : check-split ( live-interval n -- )
42 check-allocation? get [
43 [ [ start>> ] dip > [ throw-splitting-too-early ] when ]
44 [ [ end>> ] dip < [ throw-splitting-too-late ] when ]
46 drop [ end>> ] [ start>> ] bi =
47 [ throw-splitting-atomic-interval ] when
49 ] [ 2drop ] if ; inline
51 : split-before ( before -- before' )
54 : split-after ( after -- after' )
55 f >>reg f >>reload-from ; inline
57 :: split-interval ( live-interval n -- before after )
58 live-interval n check-split
59 live-interval clone :> before
60 live-interval clone :> after
61 live-interval uses>> n split-uses before after [ uses<< ] bi-curry@ bi*
62 live-interval ranges>> n split-ranges before after [ ranges<< ] bi-curry@ bi*
66 HINTS: split-interval live-interval object ;