]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/stacks/local/local.factor
compiler.cfg.*: more compiler docs
[factor.git] / basis / compiler / cfg / stacks / local / local.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators compiler.cfg
4 compiler.cfg.instructions compiler.cfg.parallel-copy
5 compiler.cfg.registers compiler.cfg.stacks.height kernel make
6 math math.order namespaces sequences sets ;
7 FROM: namespaces => set ;
8 IN: compiler.cfg.stacks.local
9
10 SYMBOLS: peek-sets replace-sets kill-sets ;
11
12 SYMBOL: locs>vregs
13
14 : loc>vreg ( loc -- vreg ) locs>vregs get [ drop next-vreg ] cache ;
15 : vreg>loc ( vreg -- loc/f ) locs>vregs get value-at ;
16
17 TUPLE: current-height
18 { d initial: 0 }
19 { r initial: 0 }
20 { emit-d initial: 0 }
21 { emit-r initial: 0 } ;
22
23 SYMBOLS: local-peek-set local-replace-set replace-mapping ;
24
25 GENERIC: translate-local-loc ( loc -- loc' )
26 M: ds-loc translate-local-loc n>> current-height get d>> - <ds-loc> ;
27 M: rs-loc translate-local-loc n>> current-height get r>> - <rs-loc> ;
28
29 : emit-stack-changes ( -- )
30     replace-mapping get dup assoc-empty? [ drop ] [
31         [ [ loc>vreg ] dip ] assoc-map parallel-copy
32     ] if ;
33
34 : emit-height-changes ( -- )
35     current-height get
36     [ emit-d>> dup 0 = [ drop ] [ ##inc-d, ] if ]
37     [ emit-r>> dup 0 = [ drop ] [ ##inc-r, ] if ] bi ;
38
39 : emit-changes ( -- )
40     ! Insert height and stack changes prior to the last instruction
41     building get pop
42     emit-stack-changes
43     emit-height-changes
44     , ;
45
46 ! inc-d/inc-r: these emit ##inc-d/##inc-r to change the stack height later
47 : inc-d ( n -- )
48     current-height get
49     [ [ + ] change-emit-d drop ]
50     [ [ + ] change-d drop ]
51     2bi ;
52
53 : inc-r ( n -- )
54     current-height get
55     [ [ + ] change-emit-r drop ]
56     [ [ + ] change-r drop ]
57     2bi ;
58
59 : peek-loc ( loc -- vreg )
60     translate-local-loc
61     dup replace-mapping get at
62     [ ] [ dup local-peek-set get conjoin loc>vreg ] ?if ;
63
64 : replace-loc ( vreg loc -- )
65     translate-local-loc replace-mapping get set-at ;
66
67 : compute-local-kill-set ( -- assoc )
68     basic-block get current-height get
69     [ [ ds-heights get at dup ] [ d>> ] bi* [-] iota [ swap - <ds-loc> ] with map ]
70     [ [ rs-heights get at dup ] [ r>> ] bi* [-] iota [ swap - <rs-loc> ] with map ] 2bi
71     append unique ;
72
73 : begin-local-analysis ( -- )
74     H{ } clone local-peek-set set
75     H{ } clone replace-mapping set
76     current-height get
77     [ 0 >>emit-d 0 >>emit-r drop ]
78     [ [ d>> ] [ r>> ] bi basic-block get record-stack-heights ] bi ;
79
80 : remove-redundant-replaces ( -- )
81     replace-mapping get [ [ loc>vreg ] dip = not ] assoc-filter
82     [ replace-mapping set ] [ keys unique local-replace-set set ] bi ;
83
84 : end-local-analysis ( -- )
85     remove-redundant-replaces
86     emit-changes
87     basic-block get {
88         [ [ local-peek-set get ] dip peek-sets get set-at ]
89         [ [ local-replace-set get ] dip replace-sets get set-at ]
90         [ [ compute-local-kill-set ] dip kill-sets get set-at ]
91     } cleave ;
92
93 : clone-current-height ( -- )
94     current-height [ clone ] change ;
95
96 : peek-set ( bb -- assoc ) peek-sets get at ;
97 : replace-set ( bb -- assoc ) replace-sets get at ;
98 : kill-set ( bb -- assoc ) kill-sets get at ;