1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs kernel math namespaces sets make sequences
6 compiler.cfg.instructions
8 compiler.cfg.stacks.height
9 compiler.cfg.parallel-copy ;
10 IN: compiler.cfg.stacks.local
12 ! Local stack analysis. We build local peek and replace sets for every basic
13 ! block while constructing the CFG.
15 SYMBOLS: peek-sets replace-sets ;
19 : loc>vreg ( loc -- vreg ) locs>vregs get [ drop i ] cache ;
20 : vreg>loc ( vreg -- loc/f ) locs>vregs get value-at ;
22 TUPLE: current-height { d initial: 0 } { r initial: 0 } { emit-d initial: 0 } { emit-r initial: 0 } ;
24 SYMBOLS: local-peek-set local-replace-set replace-mapping ;
26 GENERIC: translate-local-loc ( loc -- loc' )
27 M: ds-loc translate-local-loc n>> current-height get d>> - <ds-loc> ;
28 M: rs-loc translate-local-loc n>> current-height get r>> - <rs-loc> ;
30 : emit-stack-changes ( -- )
31 replace-mapping get dup assoc-empty? [ drop ] [
32 [ [ loc>vreg ] dip ] assoc-map parallel-copy
35 : emit-height-changes ( -- )
37 [ emit-d>> dup 0 = [ drop ] [ ##inc-d ] if ]
38 [ emit-r>> dup 0 = [ drop ] [ ##inc-r ] if ] bi ;
41 ! Insert height and stack changes prior to the last instruction
47 ! inc-d/inc-r: these emit ##inc-d/##inc-r to change the stack height later
50 [ [ + ] change-emit-d drop ]
51 [ [ + ] change-d drop ]
56 [ [ + ] change-emit-r drop ]
57 [ [ + ] change-r drop ]
60 : peek-loc ( loc -- vreg )
62 dup local-replace-set get key? [ dup local-peek-set get conjoin ] unless
63 dup replace-mapping get at [ ] [ loc>vreg ] ?if ;
65 : replace-loc ( vreg loc -- )
68 [ nip replace-mapping get delete-at ]
70 [ local-replace-set get conjoin ]
71 [ replace-mapping get set-at ]
75 : begin-local-analysis ( -- )
76 H{ } clone local-peek-set set
77 H{ } clone local-replace-set set
78 H{ } clone replace-mapping set
79 current-height get 0 >>emit-d 0 >>emit-r drop
80 current-height get [ d>> ] [ r>> ] bi basic-block get record-stack-heights ;
82 : end-local-analysis ( -- )
84 local-peek-set get basic-block get peek-sets get set-at
85 local-replace-set get basic-block get replace-sets get set-at ;
87 : clone-current-height ( -- )
88 current-height [ clone ] change ;
90 : peek-set ( bb -- assoc ) peek-sets get at ;
91 : replace-set ( bb -- assoc ) replace-sets get at ;