1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators combinators.short-circuit
4 compiler.cfg compiler.cfg.instructions cpu.architecture kernel
5 layouts locals make math namespaces sequences sets vectors fry ;
6 IN: compiler.cfg.utilities
8 : value-info-small-fixnum? ( value-info -- ? )
10 { [ dup fixnum? ] [ tag-fixnum small-enough? ] }
14 : value-info-small-tagged? ( value-info -- ? )
17 { [ dup fixnum? ] [ tag-fixnum small-enough? ] }
18 { [ dup not ] [ drop t ] }
23 : set-basic-block ( basic-block -- )
24 [ basic-block set ] [ instructions>> building set ] bi ;
26 : begin-basic-block ( -- )
27 <basic-block> basic-block get [
28 dupd successors>> push
32 : end-basic-block ( -- )
36 : emit-primitive ( node -- )
37 word>> ##call ##branch begin-basic-block ;
39 : with-branch ( quot -- final-bb )
43 basic-block get dup [ ##branch ] when
46 : emit-conditional ( branches -- )
49 basic-block get '[ [ _ swap successors>> push ] when* ] each ;
51 : back-edge? ( from to -- ? )
54 : empty-block? ( bb -- ? )
62 : (skip-empty-blocks) ( bb -- bb' )
63 dup visited get key? [
65 dup visited get conjoin
66 successors>> first (skip-empty-blocks)
70 : skip-empty-blocks ( bb -- bb' )
71 H{ } clone visited [ (skip-empty-blocks) ] with-variable ;
73 ! assoc mapping predecessors to sequences
74 SYMBOL: added-instructions
76 : add-instructions ( predecessor quot -- )
78 added-instructions get
79 [ drop V{ } clone ] cache
81 ] dip with-variable ; inline
83 :: insert-basic-block ( from to bb -- )
84 bb from 1vector >>predecessors drop
85 bb to 1vector >>successors drop
86 to predecessors>> [ dup from eq? [ drop bb ] when ] change-each
87 from successors>> [ dup to eq? [ drop bb ] when ] change-each ;
89 : <simple-block> ( insns -- bb )
92 \ ##branch new-insn over push
95 : insert-basic-blocks ( bb -- )
96 [ added-instructions get ] dip
97 '[ [ _ ] dip <simple-block> insert-basic-block ] assoc-each ;