]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/linearization/linearization.factor
_gc instruction doesn't need slot to hold GC root area size, since that's just tagged...
[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 layouts hashtables
5 cpu.architecture
6 compiler.cfg
7 compiler.cfg.comparisons
8 compiler.cfg.stack-frame
9 compiler.cfg.instructions
10 compiler.cfg.utilities
11 compiler.cfg.linearization.order ;
12 IN: compiler.cfg.linearization
13
14 <PRIVATE
15
16 SYMBOL: numbers
17
18 : block-number ( bb -- n ) numbers get at ;
19
20 : number-blocks ( bbs -- ) [ 2array ] map-index >hashtable numbers set ;
21
22 ! Convert CFG IR to machine IR.
23 GENERIC: linearize-insn ( basic-block insn -- )
24
25 : linearize-basic-block ( bb -- )
26     [ block-number _label ]
27     [ dup instructions>> [ linearize-insn ] with each ]
28     bi ;
29
30 M: insn linearize-insn , drop ;
31
32 : useless-branch? ( basic-block successor -- ? )
33     ! If our successor immediately follows us in linearization
34     ! order then we don't need to branch.
35     [ block-number ] bi@ 1 - = ; inline
36
37 : emit-branch ( bb successor -- )
38     2dup useless-branch? [ 2drop ] [ nip block-number _branch ] if ;
39
40 M: ##branch linearize-insn
41     drop dup successors>> first emit-branch ;
42
43 : successors ( bb -- first second ) successors>> first2 ; inline
44
45 : (binary-conditional) ( bb insn -- bb successor1 successor2 src1 src2 cc )
46     [ dup successors ]
47     [ [ src1>> ] [ src2>> ] [ cc>> ] tri ] bi* ; inline
48
49 : binary-conditional ( bb insn -- bb successor label2 src1 src2 cc )
50     [ (binary-conditional) ]
51     [ drop dup successors>> second useless-branch? ] 2bi
52     [ [ swap block-number ] 3dip ] [ [ block-number ] 3dip negate-cc ] if ;
53
54 M: ##compare-branch linearize-insn
55     binary-conditional _compare-branch emit-branch ;
56
57 M: ##compare-imm-branch linearize-insn
58     binary-conditional _compare-imm-branch emit-branch ;
59
60 M: ##compare-float-branch linearize-insn
61     binary-conditional _compare-float-branch emit-branch ;
62
63 : overflow-conditional ( bb insn -- bb successor label2 dst src1 src2 )
64     [ dup successors block-number ]
65     [ [ dst>> ] [ src1>> ] [ src2>> ] tri ] bi* ; inline
66
67 M: ##fixnum-add linearize-insn
68     overflow-conditional _fixnum-add emit-branch ;
69
70 M: ##fixnum-sub linearize-insn
71     overflow-conditional _fixnum-sub emit-branch ;
72
73 M: ##fixnum-mul linearize-insn
74     overflow-conditional _fixnum-mul emit-branch ;
75
76 M: ##dispatch linearize-insn
77     swap
78     [ [ src>> ] [ temp>> ] bi _dispatch ]
79     [ successors>> [ block-number _dispatch-label ] each ]
80     bi* ;
81
82 : gc-root-offsets ( registers -- alist )
83     ! Outputs a sequence of { offset register/spill-slot } pairs
84     [ length iota [ cell * ] map ] keep zip ;
85
86 M: ##gc linearize-insn
87     nip
88     {
89         [ temp1>> ]
90         [ temp2>> ]
91         [ data-values>> ]
92         [ tagged-values>> gc-root-offsets ]
93         [ uninitialized-locs>> ]
94     } cleave
95     _gc ;
96
97 : linearize-basic-blocks ( cfg -- insns )
98     [
99         [
100             linearization-order
101             [ number-blocks ]
102             [ [ linearize-basic-block ] each ] bi
103         ] [ spill-area-size>> _spill-area-size ] bi
104     ] { } make ;
105
106 PRIVATE>
107         
108 : flatten-cfg ( cfg -- mr )
109     [ linearize-basic-blocks ] [ word>> ] [ label>> ] tri
110     <mr> ;