2009-07-23 21:54:38 -04:00
|
|
|
! Copyright (C) 2009 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2016-03-16 10:21:42 -04:00
|
|
|
USING: accessors assocs compiler.cfg.instructions
|
|
|
|
compiler.cfg.parallel-copy compiler.cfg.registers
|
|
|
|
compiler.cfg.stacks.height hash-sets kernel make math math.order
|
|
|
|
namespaces sequences sets ;
|
2009-07-23 21:54:38 -04:00
|
|
|
IN: compiler.cfg.stacks.local
|
|
|
|
|
2015-04-07 09:05:34 -04:00
|
|
|
: current-height ( state -- ds rs )
|
|
|
|
first2 [ first ] bi@ ;
|
|
|
|
|
2015-03-15 19:14:41 -04:00
|
|
|
: >loc< ( loc -- n ds? )
|
|
|
|
[ n>> ] [ ds-loc? ] bi ;
|
|
|
|
|
|
|
|
: modify-height ( state loc -- )
|
|
|
|
>loc< 0 1 ? rot nth [ + ] with map! drop ;
|
|
|
|
|
|
|
|
: adjust ( state loc -- )
|
|
|
|
>loc< 0 1 ? rot nth dup first swapd + 0 rot set-nth ;
|
|
|
|
|
|
|
|
: reset-emits ( state -- )
|
|
|
|
[ 0 1 rot set-nth ] each ;
|
|
|
|
|
|
|
|
: height-state>insns ( state -- insns )
|
|
|
|
[ second ] map { ds-loc rs-loc } [ new swap >>n ] 2map
|
2015-05-12 21:50:34 -04:00
|
|
|
[ n>> 0 = ] reject [ ##inc new swap >>loc ] map ;
|
2015-03-15 19:14:41 -04:00
|
|
|
|
2015-04-07 09:05:34 -04:00
|
|
|
: translate-local-loc ( loc state -- loc' )
|
|
|
|
[ clone ] dip over >loc< 0 1 ? rot nth first - >>n ;
|
2015-03-15 19:14:41 -04:00
|
|
|
|
|
|
|
: clone-height-state ( state -- state' )
|
|
|
|
[ clone ] map ;
|
|
|
|
|
|
|
|
: initial-height-state ( -- state )
|
|
|
|
{ { 0 0 } { 0 0 } } clone-height-state ;
|
|
|
|
|
|
|
|
: kill-locations ( saved-height height -- seq )
|
|
|
|
dupd [-] iota [ swap - ] with map ;
|
|
|
|
|
2015-04-01 22:56:40 -04:00
|
|
|
: local-kill-set ( ds-height rs-height state -- set )
|
2015-04-07 09:05:34 -04:00
|
|
|
current-height swapd [ kill-locations ] 2bi@
|
2015-03-15 19:14:41 -04:00
|
|
|
[ [ <ds-loc> ] map ] [ [ <rs-loc> ] map ] bi*
|
2015-04-01 22:56:40 -04:00
|
|
|
append >hash-set ;
|
2015-03-15 19:14:41 -04:00
|
|
|
|
2015-03-19 13:03:49 -04:00
|
|
|
SYMBOLS: height-state peek-sets replace-sets kill-sets locs>vregs ;
|
2009-07-23 21:54:38 -04:00
|
|
|
|
2015-03-19 13:03:49 -04:00
|
|
|
: inc-stack ( loc -- )
|
|
|
|
height-state get swap modify-height ;
|
2009-07-23 21:54:38 -04:00
|
|
|
|
2009-08-08 05:02:18 -04:00
|
|
|
: loc>vreg ( loc -- vreg ) locs>vregs get [ drop next-vreg ] cache ;
|
2009-07-24 04:37:18 -04:00
|
|
|
: vreg>loc ( vreg -- loc/f ) locs>vregs get value-at ;
|
2009-07-23 21:54:38 -04:00
|
|
|
|
2015-04-07 09:05:34 -04:00
|
|
|
SYMBOLS: local-peek-set replaces ;
|
2009-07-23 21:54:38 -04:00
|
|
|
|
2015-04-07 09:05:34 -04:00
|
|
|
: replaces>copy-insns ( replaces -- insns )
|
2014-12-21 23:57:53 -05:00
|
|
|
[ [ loc>vreg ] dip ] assoc-map parallel-copy ;
|
2009-07-24 04:37:18 -04:00
|
|
|
|
2015-04-07 09:05:34 -04:00
|
|
|
: changes>insns ( replaces height-state -- insns )
|
2015-04-22 11:56:14 -04:00
|
|
|
[ replaces>copy-insns ] [ height-state>insns ] bi* append ;
|
2015-04-07 09:05:34 -04:00
|
|
|
|
2015-04-22 11:56:14 -04:00
|
|
|
: emit-changes ( replaces state -- )
|
2015-04-07 09:05:34 -04:00
|
|
|
building get pop -rot changes>insns % , ;
|
2009-07-23 21:54:38 -04:00
|
|
|
|
|
|
|
: peek-loc ( loc -- vreg )
|
2015-04-07 09:05:34 -04:00
|
|
|
height-state get translate-local-loc dup replaces get at
|
2015-04-01 22:56:40 -04:00
|
|
|
[ ] [ dup local-peek-set get adjoin loc>vreg ] ?if ;
|
2009-07-23 21:54:38 -04:00
|
|
|
|
|
|
|
: replace-loc ( vreg loc -- )
|
2015-04-07 09:05:34 -04:00
|
|
|
height-state get translate-local-loc replaces get set-at ;
|
2009-07-23 21:54:38 -04:00
|
|
|
|
2015-04-07 09:05:34 -04:00
|
|
|
: compute-local-kill-set ( basic-block -- set )
|
2015-04-29 11:03:50 -04:00
|
|
|
[ ds-heights get at ] [ rs-heights get at ] bi
|
2015-03-15 19:14:41 -04:00
|
|
|
height-state get local-kill-set ;
|
2009-08-01 07:12:43 -04:00
|
|
|
|
2015-04-07 09:05:34 -04:00
|
|
|
: begin-local-analysis ( basic-block -- )
|
|
|
|
height-state get dup reset-emits
|
|
|
|
current-height rot record-stack-heights
|
2016-03-29 20:14:42 -04:00
|
|
|
HS{ } clone local-peek-set namespaces:set
|
|
|
|
H{ } clone replaces namespaces:set ;
|
2009-07-23 21:54:38 -04:00
|
|
|
|
2015-04-07 09:05:34 -04:00
|
|
|
: remove-redundant-replaces ( replaces -- replaces' )
|
2015-05-12 22:08:42 -04:00
|
|
|
[ [ loc>vreg ] dip = ] assoc-reject ;
|
2009-08-19 23:00:21 -04:00
|
|
|
|
2015-03-31 19:34:56 -04:00
|
|
|
: end-local-analysis ( basic-block -- )
|
2015-04-01 22:56:40 -04:00
|
|
|
[
|
2015-04-07 09:05:34 -04:00
|
|
|
replaces get remove-redundant-replaces
|
2016-03-16 10:21:42 -04:00
|
|
|
[ height-state get emit-changes ]
|
2016-03-16 11:29:49 -04:00
|
|
|
[ keys >hash-set swap replace-sets get set-at ] bi
|
2015-04-01 22:56:40 -04:00
|
|
|
]
|
2015-03-31 19:34:56 -04:00
|
|
|
[ [ local-peek-set get ] dip peek-sets get set-at ]
|
2015-04-07 09:05:34 -04:00
|
|
|
[ [ compute-local-kill-set ] keep kill-sets get set-at ] tri ;
|