]> gitweb.factorcode.org Git - factor.git/blob - unfinished/compiler/cfg/cfg.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / unfinished / compiler / cfg / cfg.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel accessors namespaces assocs sequences sets fry ;
4 IN: compiler.cfg
5
6 TUPLE: procedure entry word label ;
7
8 C: <procedure> procedure
9
10 ! - "id" is a globally unique id used for hashcode*.
11 ! - "number" is assigned by linearization.
12 TUPLE: basic-block < identity-tuple
13 id
14 number
15 label
16 instructions
17 successors
18 predecessors ;
19
20 SYMBOL: next-block-id
21
22 : <basic-block> ( -- basic-block )
23     basic-block new
24         next-block-id counter >>id
25         V{ } clone >>instructions
26         V{ } clone >>successors
27         V{ } clone >>predecessors ;
28
29 M: basic-block hashcode* id>> nip ;
30
31 ! Utilities
32 SYMBOL: visited-blocks
33
34 : visit-block ( basic-block quot -- )
35     over visited-blocks get 2dup key?
36     [ 2drop 2drop ] [ conjoin call ] if ; inline
37
38 : (each-block) ( basic-block quot -- )
39     '[
40         _
41         [ call ]
42         [ [ successors>> ] dip '[ _ (each-block) ] each ]
43         2bi
44     ] visit-block ; inline
45
46 : each-block ( basic-block quot -- )
47     H{ } clone visited-blocks [ (each-block) ] with-variable ; inline