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 IN: compiler.cfg.stacks.local
13 ! Local stack analysis. We build local peek and replace sets for every basic
14 ! block while constructing the CFG.
16 SYMBOLS: peek-sets replace-sets kill-sets ;
20 : loc>vreg ( loc -- vreg ) locs>vregs get [ drop i ] cache ;
21 : vreg>loc ( vreg -- loc/f ) locs>vregs get value-at ;
27 { emit-r initial: 0 } ;
29 SYMBOLS: local-peek-set local-replace-set replace-mapping ;
31 GENERIC: translate-local-loc ( loc -- loc' )
32 M: ds-loc translate-local-loc n>> current-height get d>> - <ds-loc> ;
33 M: rs-loc translate-local-loc n>> current-height get r>> - <rs-loc> ;
35 : emit-stack-changes ( -- )
36 replace-mapping get dup assoc-empty? [ drop ] [
37 [ [ loc>vreg ] dip ] assoc-map parallel-copy
40 : emit-height-changes ( -- )
42 [ emit-d>> dup 0 = [ drop ] [ ##inc-d ] if ]
43 [ emit-r>> dup 0 = [ drop ] [ ##inc-r ] if ] bi ;
46 ! Insert height and stack changes prior to the last instruction
52 ! inc-d/inc-r: these emit ##inc-d/##inc-r to change the stack height later
55 [ [ + ] change-emit-d drop ]
56 [ [ + ] change-d drop ]
61 [ [ + ] change-emit-r drop ]
62 [ [ + ] change-r drop ]
65 : peek-loc ( loc -- vreg )
67 dup local-replace-set get key? [ dup local-peek-set get conjoin ] unless
68 dup replace-mapping get at [ ] [ loc>vreg ] ?if ;
70 : replace-loc ( vreg loc -- )
73 [ nip replace-mapping get delete-at ]
75 [ local-replace-set get conjoin ]
76 [ replace-mapping get set-at ]
80 : compute-local-kill-set ( -- assoc )
81 basic-block get current-height get
82 [ [ ds-heights get at dup ] [ d>> ] bi* [-] iota [ swap - <ds-loc> ] with map ]
83 [ [ rs-heights get at dup ] [ r>> ] bi* [-] iota [ swap - <rs-loc> ] with map ]
84 [ drop local-replace-set get at ] 2tri
85 [ append unique dup ] dip update ;
87 : begin-local-analysis ( -- )
88 H{ } clone local-peek-set set
89 H{ } clone local-replace-set set
90 H{ } clone replace-mapping set
92 [ 0 >>emit-d 0 >>emit-r drop ]
93 [ [ d>> ] [ r>> ] bi basic-block get record-stack-heights ] bi ;
95 : end-local-analysis ( -- )
98 [ [ local-peek-set get ] dip peek-sets get set-at ]
99 [ [ local-replace-set get ] dip replace-sets get set-at ]
100 [ [ compute-local-kill-set ] dip kill-sets get set-at ]
103 : clone-current-height ( -- )
104 current-height [ clone ] change ;
106 : peek-set ( bb -- assoc ) peek-sets get at ;
107 : replace-set ( bb -- assoc ) replace-sets get at ;
108 : kill-set ( bb -- assoc ) kill-sets get at ;