]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/compiler/cfg/stacks/local/local.factor
use reject instead of [ ... not ] filter.
[factor.git] / basis / compiler / cfg / stacks / local / local.factor
index c6558b1fb8d0d0b521200bdc605f9a9a39a4c22d..f26bf76654b998527f368bdc1854c87502b41224 100644 (file)
 ! Copyright (C) 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors assocs kernel math math.order namespaces sets make
-sequences combinators fry
-compiler.cfg
-compiler.cfg.hats
-compiler.cfg.instructions
-compiler.cfg.registers
-compiler.cfg.stacks.height
-compiler.cfg.parallel-copy ;
+USING: accessors arrays assocs combinators compiler.cfg
+compiler.cfg.instructions compiler.cfg.parallel-copy
+compiler.cfg.registers compiler.cfg.stacks.height
+fry hash-sets kernel make math math.order namespaces sequences sets ;
+FROM: namespaces => set ;
 IN: compiler.cfg.stacks.local
 
-! Local stack analysis. We build three sets for every basic block
-! in the CFG:
-! - peek-set: all stack locations that the block reads before writing
-! - replace-set: all stack locations that the block writes
-! - kill-set: all stack locations which become unavailable after the
-!   block ends because of the stack height being decremented
-! This is done while constructing the CFG.
+: current-height ( state -- ds rs )
+    first2 [ first ] bi@ ;
 
-SYMBOLS: peek-sets replace-sets kill-sets ;
+: >loc< ( loc -- n ds? )
+    [ n>> ] [ ds-loc? ] bi ;
 
-SYMBOL: locs>vregs
+: modify-height ( state loc -- )
+    >loc< 0 1 ? rot nth [ + ] with map! drop ;
 
-: loc>vreg ( loc -- vreg ) locs>vregs get [ drop i ] cache ;
+: 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
+    [ n>> 0 = ] reject [ ##inc new swap >>loc ] map ;
+
+: translate-local-loc ( loc state -- loc' )
+    [ clone ] dip over >loc< 0 1 ? rot nth first - >>n ;
+
+: 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 ;
+
+: local-kill-set ( ds-height rs-height state -- set )
+    current-height swapd [ kill-locations ] 2bi@
+    [ [ <ds-loc> ] map ] [ [ <rs-loc> ] map ] bi*
+    append >hash-set ;
+
+SYMBOLS: height-state peek-sets replace-sets kill-sets locs>vregs ;
+
+: inc-stack ( loc -- )
+    height-state get swap modify-height ;
+
+: loc>vreg ( loc -- vreg ) locs>vregs get [ drop next-vreg ] cache ;
 : vreg>loc ( vreg -- loc/f ) locs>vregs get value-at ;
 
-TUPLE: current-height
-{ d initial: 0 }
-{ r initial: 0 }
-{ emit-d initial: 0 }
-{ emit-r initial: 0 } ;
-
-SYMBOLS: local-peek-set local-replace-set replace-mapping ;
-
-GENERIC: translate-local-loc ( loc -- loc' )
-M: ds-loc translate-local-loc n>> current-height get d>> - <ds-loc> ;
-M: rs-loc translate-local-loc n>> current-height get r>> - <rs-loc> ;
-
-: emit-stack-changes ( -- )
-    replace-mapping get dup assoc-empty? [ drop ] [
-        [ [ loc>vreg ] dip ] assoc-map parallel-copy
-    ] if ;
-
-: emit-height-changes ( -- )
-    current-height get
-    [ emit-d>> dup 0 = [ drop ] [ ##inc-d ] if ]
-    [ emit-r>> dup 0 = [ drop ] [ ##inc-r ] if ] bi ;
-
-: emit-changes ( -- )
-    ! Insert height and stack changes prior to the last instruction
-    building get pop
-    emit-stack-changes
-    emit-height-changes
-    , ;
-
-! inc-d/inc-r: these emit ##inc-d/##inc-r to change the stack height later
-: inc-d ( n -- )
-    current-height get
-    [ [ + ] change-emit-d drop ]
-    [ [ + ] change-d drop ]
-    2bi ;
-
-: inc-r ( n -- )
-    current-height get
-    [ [ + ] change-emit-r drop ]
-    [ [ + ] change-r drop ]
-    2bi ;
+SYMBOLS: local-peek-set replaces ;
+
+: replaces>copy-insns ( replaces -- insns )
+    [ [ loc>vreg ] dip ] assoc-map parallel-copy ;
+
+: changes>insns ( replaces height-state -- insns )
+    [ replaces>copy-insns ] [ height-state>insns ] bi* append ;
+
+: emit-changes ( replaces state -- )
+    building get pop -rot changes>insns % , ;
 
 : peek-loc ( loc -- vreg )
-    translate-local-loc
-    dup local-replace-set get key? [ dup local-peek-set get conjoin ] unless
-    dup replace-mapping get at [ ] [ loc>vreg ] ?if ;
+    height-state get translate-local-loc dup replaces get at
+    [ ] [ dup local-peek-set get adjoin loc>vreg ] ?if ;
 
 : replace-loc ( vreg loc -- )
-    translate-local-loc
-    2dup loc>vreg =
-    [ nip replace-mapping get delete-at ]
+    height-state get translate-local-loc replaces get set-at ;
+
+: compute-local-kill-set ( basic-block -- set )
+    [ ds-heights get at ] [ rs-heights get at ] bi
+    height-state get local-kill-set ;
+
+: begin-local-analysis ( basic-block -- )
+    height-state get dup reset-emits
+    current-height rot record-stack-heights
+    HS{ } clone local-peek-set set
+    H{ } clone replaces set ;
+
+: remove-redundant-replaces ( replaces -- replaces' )
+    [ [ loc>vreg ] dip = not ] assoc-filter ;
+
+: end-local-analysis ( basic-block -- )
     [
-        [ local-replace-set get conjoin ]
-        [ replace-mapping get set-at ]
-        bi
-    ] if ;
-
-: compute-local-kill-set ( -- assoc )
-    basic-block get current-height get
-    [ [ ds-heights get at dup ] [ d>> ] bi* [-] iota [ swap - <ds-loc> ] with map ]
-    [ [ rs-heights get at dup ] [ r>> ] bi* [-] iota [ swap - <rs-loc> ] with map ] 2bi
-    append unique ;
-
-: begin-local-analysis ( -- )
-    H{ } clone local-peek-set set
-    H{ } clone local-replace-set set
-    H{ } clone replace-mapping set
-    current-height get
-    [ 0 >>emit-d 0 >>emit-r drop ]
-    [ [ d>> ] [ r>> ] bi basic-block get record-stack-heights ] bi ;
-
-: end-local-analysis ( -- )
-    emit-changes
-    basic-block get {
-        [ [ local-peek-set get ] dip peek-sets get set-at ]
-        [ [ local-replace-set get ] dip replace-sets get set-at ]
-        [ [ compute-local-kill-set ] dip kill-sets get set-at ]
-    } cleave ;
-
-: clone-current-height ( -- )
-    current-height [ clone ] change ;
-
-: peek-set ( bb -- assoc ) peek-sets get at ;
-: replace-set ( bb -- assoc ) replace-sets get at ;
-: kill-set ( bb -- assoc ) kill-sets get at ;
\ No newline at end of file
+        replaces get remove-redundant-replaces
+        dup height-state get emit-changes keys
+        swap replace-sets get set-at
+    ]
+    [ [ local-peek-set get ] dip peek-sets get set-at ]
+    [ [ compute-local-kill-set ] keep kill-sets get set-at ] tri ;