]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/linearization/linearization.factor
Merge branch 'master' of git://factorcode.org/git/factor
[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.comparisons
7 compiler.cfg.stack-frame
8 compiler.cfg.instructions
9 compiler.cfg.utilities
10 compiler.cfg.linearization.order ;
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     [ block-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 linearization
25     ! order then we don't need to branch.
26     [ block-number ] bi@ 1 - = ; inline
27
28 : emit-branch ( bb successor -- )
29     2dup useless-branch? [ 2drop ] [ nip block-number _branch ] if ;
30
31 M: ##branch linearize-insn
32     drop dup successors>> first emit-branch ;
33
34 : successors ( bb -- first second ) successors>> first2 ; inline
35
36 : (binary-conditional) ( bb insn -- bb successor1 successor2 src1 src2 cc )
37     [ dup successors ]
38     [ [ src1>> ] [ src2>> ] [ cc>> ] tri ] bi* ; inline
39
40 : binary-conditional ( bb insn -- bb successor label2 src1 src2 cc )
41     [ (binary-conditional) ]
42     [ drop dup successors>> second useless-branch? ] 2bi
43     [ [ swap block-number ] 3dip ] [ [ block-number ] 3dip negate-cc ] if ;
44
45 : with-regs ( insn quot -- )
46     over regs>> [ call ] dip building get last (>>regs) ; inline
47
48 M: ##compare-branch linearize-insn
49     [ binary-conditional _compare-branch ] with-regs emit-branch ;
50
51 M: ##compare-imm-branch linearize-insn
52     [ binary-conditional _compare-imm-branch ] with-regs emit-branch ;
53
54 M: ##compare-float-branch linearize-insn
55     [ binary-conditional _compare-float-branch ] with-regs emit-branch ;
56
57 : overflow-conditional ( bb insn -- bb successor label2 dst src1 src2 )
58     [ dup successors block-number ]
59     [ [ dst>> ] [ src1>> ] [ src2>> ] tri ] bi* ; inline
60
61 M: ##fixnum-add linearize-insn
62     [ overflow-conditional _fixnum-add ] with-regs emit-branch ;
63
64 M: ##fixnum-sub linearize-insn
65     [ overflow-conditional _fixnum-sub ] with-regs emit-branch ;
66
67 M: ##fixnum-mul linearize-insn
68     [ overflow-conditional _fixnum-mul ] with-regs emit-branch ;
69
70 M: ##dispatch linearize-insn
71     swap
72     [ [ [ src>> ] [ temp>> ] bi _dispatch ] with-regs ]
73     [ successors>> [ block-number _dispatch-label ] each ]
74     bi* ;
75
76 : (compute-gc-roots) ( n live-values -- n )
77     [
78         [ nip 2array , ]
79         [ drop reg-class>> reg-size + ]
80         3bi
81     ] assoc-each ;
82
83 : oop-values ( regs -- regs' )
84     [ drop reg-class>> int-regs eq? ] assoc-filter ;
85
86 : data-values ( regs -- regs' )
87     [ drop reg-class>> double-float-regs eq? ] assoc-filter ;
88
89 : compute-gc-roots ( live-values -- alist )
90     [
91         [ 0 ] dip
92         ! we put float registers last; the GC doesn't actually scan them
93         [ oop-values (compute-gc-roots) ]
94         [ data-values (compute-gc-roots) ] bi
95         drop
96     ] { } make ;
97
98 : count-gc-roots ( live-values -- n )
99     ! Size of GC root area, minus the float registers
100     oop-values assoc-size ;
101
102 M: ##gc linearize-insn
103     nip
104     [
105         [ temp1>> ]
106         [ temp2>> ]
107         [
108             live-values>>
109             [ compute-gc-roots ]
110             [ count-gc-roots ]
111             [ gc-roots-size ]
112             tri
113         ] tri
114         _gc
115     ] with-regs ;
116
117 : linearize-basic-blocks ( cfg -- insns )
118     [
119         [ linearization-order [ linearize-basic-block ] each ]
120         [ spill-counts>> _spill-counts ]
121         bi
122     ] { } make ;
123
124 : flatten-cfg ( cfg -- mr )
125     [ linearize-basic-blocks ] [ word>> ] [ label>> ] tri
126     <mr> ;