]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/stacks/local/local.factor
factor: don't need FROM: namespaces => set or namespaces:set anymore
[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 arrays assocs combinators compiler.cfg
4 compiler.cfg.instructions compiler.cfg.parallel-copy
5 compiler.cfg.registers compiler.cfg.stacks.height
6 fry hash-sets kernel make math math.order namespaces sequences sets ;
7 IN: compiler.cfg.stacks.local
8
9 : current-height ( state -- ds rs )
10     first2 [ first ] bi@ ;
11
12 : >loc< ( loc -- n ds? )
13     [ n>> ] [ ds-loc? ] bi ;
14
15 : modify-height ( state loc -- )
16     >loc< 0 1 ? rot nth [ + ] with map! drop ;
17
18 : adjust ( state loc -- )
19     >loc< 0 1 ? rot nth dup first swapd + 0 rot set-nth ;
20
21 : reset-emits ( state -- )
22     [ 0 1 rot set-nth ] each ;
23
24 : height-state>insns ( state -- insns )
25     [ second ] map { ds-loc rs-loc } [ new swap >>n ] 2map
26     [ n>> 0 = ] reject [ ##inc new swap >>loc ] map ;
27
28 : translate-local-loc ( loc state -- loc' )
29     [ clone ] dip over >loc< 0 1 ? rot nth first - >>n ;
30
31 : clone-height-state ( state -- state' )
32     [ clone ] map ;
33
34 : initial-height-state ( -- state )
35     { { 0 0 } { 0 0 } } clone-height-state ;
36
37 : kill-locations ( saved-height height -- seq )
38     dupd [-] iota [ swap - ] with map ;
39
40 : local-kill-set ( ds-height rs-height state -- set )
41     current-height swapd [ kill-locations ] 2bi@
42     [ [ <ds-loc> ] map ] [ [ <rs-loc> ] map ] bi*
43     append >hash-set ;
44
45 SYMBOLS: height-state peek-sets replace-sets kill-sets locs>vregs ;
46
47 : inc-stack ( loc -- )
48     height-state get swap modify-height ;
49
50 : loc>vreg ( loc -- vreg ) locs>vregs get [ drop next-vreg ] cache ;
51 : vreg>loc ( vreg -- loc/f ) locs>vregs get value-at ;
52
53 SYMBOLS: local-peek-set replaces ;
54
55 : replaces>copy-insns ( replaces -- insns )
56     [ [ loc>vreg ] dip ] assoc-map parallel-copy ;
57
58 : changes>insns ( replaces height-state -- insns )
59     [ replaces>copy-insns ] [ height-state>insns ] bi* append ;
60
61 : emit-changes ( replaces state -- )
62     building get pop -rot changes>insns % , ;
63
64 : peek-loc ( loc -- vreg )
65     height-state get translate-local-loc dup replaces get at
66     [ ] [ dup local-peek-set get adjoin loc>vreg ] ?if ;
67
68 : replace-loc ( vreg loc -- )
69     height-state get translate-local-loc replaces get set-at ;
70
71 : compute-local-kill-set ( basic-block -- set )
72     [ ds-heights get at ] [ rs-heights get at ] bi
73     height-state get local-kill-set ;
74
75 : begin-local-analysis ( basic-block -- )
76     height-state get dup reset-emits
77     current-height rot record-stack-heights
78     HS{ } clone local-peek-set set
79     H{ } clone replaces set ;
80
81 : remove-redundant-replaces ( replaces -- replaces' )
82     [ [ loc>vreg ] dip = ] assoc-reject ;
83
84 : end-local-analysis ( basic-block -- )
85     [
86         replaces get remove-redundant-replaces
87         dup height-state get emit-changes keys
88         swap replace-sets get set-at
89     ]
90     [ [ local-peek-set get ] dip peek-sets get set-at ]
91     [ [ compute-local-kill-set ] keep kill-sets get set-at ] tri ;