1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs kernel math math.order namespaces sets make
4 sequences combinators fry
7 compiler.cfg.instructions
9 compiler.cfg.stacks.height
10 compiler.cfg.parallel-copy ;
11 FROM: namespaces => set ;
12 IN: compiler.cfg.stacks.local
14 ! Local stack analysis. We build three sets for every basic block
16 ! - peek-set: all stack locations that the block reads before writing
17 ! - replace-set: all stack locations that the block writes
18 ! - kill-set: all stack locations which become unavailable after the
19 ! block ends because of the stack height being decremented
20 ! This is done while constructing the CFG.
22 SYMBOLS: peek-sets replace-sets kill-sets ;
26 : loc>vreg ( loc -- vreg ) locs>vregs get [ drop next-vreg ] cache ;
27 : vreg>loc ( vreg -- loc/f ) locs>vregs get value-at ;
33 { emit-r initial: 0 } ;
35 SYMBOLS: local-peek-set local-replace-set replace-mapping ;
37 GENERIC: translate-local-loc ( loc -- loc' )
38 M: ds-loc translate-local-loc n>> current-height get d>> - <ds-loc> ;
39 M: rs-loc translate-local-loc n>> current-height get r>> - <rs-loc> ;
41 : emit-stack-changes ( -- )
42 replace-mapping get dup assoc-empty? [ drop ] [
43 [ [ loc>vreg ] dip ] assoc-map parallel-copy
46 : emit-height-changes ( -- )
48 [ emit-d>> dup 0 = [ drop ] [ ##inc-d ] if ]
49 [ emit-r>> dup 0 = [ drop ] [ ##inc-r ] if ] bi ;
52 ! Insert height and stack changes prior to the last instruction
58 ! inc-d/inc-r: these emit ##inc-d/##inc-r to change the stack height later
61 [ [ + ] change-emit-d drop ]
62 [ [ + ] change-d drop ]
67 [ [ + ] change-emit-r drop ]
68 [ [ + ] change-r drop ]
71 : peek-loc ( loc -- vreg )
73 dup replace-mapping get at
74 [ ] [ dup local-peek-set get conjoin loc>vreg ] ?if ;
76 : replace-loc ( vreg loc -- )
77 translate-local-loc replace-mapping get set-at ;
79 : compute-local-kill-set ( -- assoc )
80 basic-block get current-height get
81 [ [ ds-heights get at dup ] [ d>> ] bi* [-] iota [ swap - <ds-loc> ] with map ]
82 [ [ rs-heights get at dup ] [ r>> ] bi* [-] iota [ swap - <rs-loc> ] with map ] 2bi
85 : begin-local-analysis ( -- )
86 H{ } clone local-peek-set set
87 H{ } clone replace-mapping set
89 [ 0 >>emit-d 0 >>emit-r drop ]
90 [ [ d>> ] [ r>> ] bi basic-block get record-stack-heights ] bi ;
92 : remove-redundant-replaces ( -- )
93 replace-mapping get [ [ loc>vreg ] dip = not ] assoc-filter
94 [ replace-mapping set ] [ keys unique local-replace-set set ] bi ;
96 : end-local-analysis ( -- )
97 remove-redundant-replaces
100 [ [ local-peek-set get ] dip peek-sets get set-at ]
101 [ [ local-replace-set get ] dip replace-sets get set-at ]
102 [ [ compute-local-kill-set ] dip kill-sets get set-at ]
105 : clone-current-height ( -- )
106 current-height [ clone ] change ;
108 : peek-set ( bb -- assoc ) peek-sets get at ;
109 : replace-set ( bb -- assoc ) replace-sets get at ;
110 : kill-set ( bb -- assoc ) kill-sets get at ;