]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/linearization/linearization.factor
Merge branch 'master' into dcn
[factor.git] / basis / compiler / cfg / linearization / linearization.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math accessors sequences namespaces make
4 combinators assocs arrays locals cpu.architecture
5 compiler.cfg
6 compiler.cfg.rpo
7 compiler.cfg.comparisons
8 compiler.cfg.stack-frame
9 compiler.cfg.instructions
10 compiler.cfg.utilities ;
11 IN: compiler.cfg.linearization
12
13 ! Convert CFG IR to machine IR.
14 GENERIC: linearize-insn ( basic-block insn -- )
15
16 : linearize-basic-block ( bb -- )
17     [ number>> _label ]
18     [ dup instructions>> [ linearize-insn ] with each ]
19     bi ;
20
21 M: insn linearize-insn , drop ;
22
23 : useless-branch? ( basic-block successor -- ? )
24     #! If our successor immediately follows us in RPO, then we
25     #! don't need to branch.
26     [ number>> ] bi@ 1 - = ; inline
27
28 : emit-loop-entry? ( bb successor -- ? )
29     [ back-edge? not ]
30     [ nip dup predecessors>> [ swap back-edge? ] with any? ] 2bi and ;
31
32 : emit-branch ( bb successor -- )
33     2dup emit-loop-entry? [ _loop-entry ] when
34     2dup useless-branch? [ 2drop ] [ nip number>> _branch ] if ;
35
36 M: ##branch linearize-insn
37     drop dup successors>> first emit-branch ;
38
39 : successors ( bb -- first second ) successors>> first2 ; inline
40
41 : (binary-conditional) ( bb insn -- bb successor1 successor2 src1 src2 cc )
42     [ dup successors ]
43     [ [ src1>> ] [ src2>> ] [ cc>> ] tri ] bi* ; inline
44
45 : binary-conditional ( bb insn -- bb successor label2 src1 src2 cc )
46     [ (binary-conditional) ]
47     [ drop dup successors>> second useless-branch? ] 2bi
48     [ [ swap number>> ] 3dip ] [ [ number>> ] 3dip negate-cc ] if ;
49
50 : with-regs ( insn quot -- )
51     over regs>> [ call ] dip building get last (>>regs) ; inline
52
53 M: ##compare-branch linearize-insn
54     [ binary-conditional _compare-branch ] with-regs emit-branch ;
55
56 M: ##compare-imm-branch linearize-insn
57     [ binary-conditional _compare-imm-branch ] with-regs emit-branch ;
58
59 M: ##compare-float-branch linearize-insn
60     [ binary-conditional _compare-float-branch ] with-regs emit-branch ;
61
62 : overflow-conditional ( bb insn -- bb successor label2 dst src1 src2 )
63     [ dup successors number>> ]
64     [ [ dst>> ] [ src1>> ] [ src2>> ] tri ] bi* ; inline
65
66 M: ##fixnum-add linearize-insn
67     [ overflow-conditional _fixnum-add ] with-regs emit-branch ;
68
69 M: ##fixnum-sub linearize-insn
70     [ overflow-conditional _fixnum-sub ] with-regs emit-branch ;
71
72 M: ##fixnum-mul linearize-insn
73     [ overflow-conditional _fixnum-mul ] with-regs emit-branch ;
74
75 M: ##dispatch linearize-insn
76     swap
77     [ [ [ src>> ] [ temp>> ] bi _dispatch ] with-regs ]
78     [ successors>> [ number>> _dispatch-label ] each ]
79     bi* ;
80
81 : (compute-gc-roots) ( n live-values -- n )
82     [
83         [ nip 2array , ]
84         [ drop reg-class>> reg-size + ]
85         3bi
86     ] assoc-each ;
87
88 : oop-values ( regs -- regs' )
89     [ drop reg-class>> int-regs eq? ] assoc-filter ;
90
91 : data-values ( regs -- regs' )
92     [ drop reg-class>> double-float-regs eq? ] assoc-filter ;
93
94 : compute-gc-roots ( live-values -- alist )
95     [
96         [ 0 ] dip
97         ! we put float registers last; the GC doesn't actually scan them
98         [ oop-values (compute-gc-roots) ]
99         [ data-values (compute-gc-roots) ] bi
100         drop
101     ] { } make ;
102
103 : count-gc-roots ( live-values -- n )
104     ! Size of GC root area, minus the float registers
105     oop-values assoc-size ;
106
107 M: ##gc linearize-insn
108     nip
109     [
110         [ temp1>> ]
111         [ temp2>> ]
112         [
113             live-values>>
114             [ compute-gc-roots ]
115             [ count-gc-roots ]
116             [ gc-roots-size ]
117             tri
118         ] tri
119         _gc
120     ] with-regs ;
121
122 : linearize-basic-blocks ( cfg -- insns )
123     [
124         [ [ linearize-basic-block ] each-basic-block ]
125         [ spill-counts>> _spill-counts ]
126         bi
127     ] { } make ;
128
129 : flatten-cfg ( cfg -- mr )
130     [ linearize-basic-blocks ] [ word>> ] [ label>> ] tri
131     <mr> ;