2009-05-27 19:58:01 -04:00
|
|
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
2008-07-20 05:24:37 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-06-30 23:43:02 -04:00
|
|
|
USING: kernel arrays vectors accessors assocs sets
|
2009-06-30 23:11:15 -04:00
|
|
|
namespaces math make fry sequences
|
|
|
|
combinators.short-circuit
|
|
|
|
compiler.cfg.instructions ;
|
2008-07-20 05:24:37 -04:00
|
|
|
IN: compiler.cfg
|
|
|
|
|
|
|
|
TUPLE: basic-block < identity-tuple
|
2009-06-09 04:36:16 -04:00
|
|
|
{ id integer }
|
2008-07-20 05:24:37 -04:00
|
|
|
number
|
2008-11-03 00:09:31 -05:00
|
|
|
{ instructions vector }
|
|
|
|
{ successors vector }
|
|
|
|
{ predecessors vector } ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2009-05-19 18:28:13 -04:00
|
|
|
M: basic-block hashcode* nip id>> ;
|
|
|
|
|
2009-05-27 19:58:01 -04:00
|
|
|
: <basic-block> ( -- bb )
|
2008-07-20 05:24:37 -04:00
|
|
|
basic-block new
|
|
|
|
V{ } clone >>instructions
|
2008-10-22 19:38:30 -04:00
|
|
|
V{ } clone >>successors
|
2008-11-03 00:09:31 -05:00
|
|
|
V{ } clone >>predecessors
|
2008-10-22 19:38:30 -04:00
|
|
|
\ basic-block counter >>id ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2009-06-30 23:11:15 -04:00
|
|
|
: empty-block? ( bb -- ? )
|
|
|
|
instructions>> {
|
|
|
|
[ length 1 = ]
|
|
|
|
[ first ##branch? ]
|
|
|
|
} 1&& ;
|
|
|
|
|
2009-06-30 23:43:02 -04:00
|
|
|
SYMBOL: visited
|
|
|
|
|
|
|
|
: (skip-empty-blocks) ( bb -- bb' )
|
|
|
|
dup visited get key? [
|
|
|
|
dup empty-block? [
|
|
|
|
dup visited get conjoin
|
|
|
|
successors>> first (skip-empty-blocks)
|
|
|
|
] when
|
|
|
|
] unless ;
|
|
|
|
|
2009-06-30 23:11:15 -04:00
|
|
|
: skip-empty-blocks ( bb -- bb' )
|
2009-06-30 23:43:02 -04:00
|
|
|
H{ } clone visited [ (skip-empty-blocks) ] with-variable ;
|
2009-06-30 23:11:15 -04:00
|
|
|
|
2009-05-27 19:58:01 -04:00
|
|
|
: add-instructions ( bb quot -- )
|
|
|
|
[ instructions>> building ] dip '[
|
|
|
|
building get pop
|
|
|
|
_ dip
|
|
|
|
building get push
|
|
|
|
] with-variable ; inline
|
|
|
|
|
2009-07-10 00:13:30 -04:00
|
|
|
: back-edge? ( from to -- ? )
|
|
|
|
[ number>> ] bi@ > ;
|
|
|
|
|
2009-05-29 14:11:34 -04:00
|
|
|
TUPLE: cfg { entry basic-block } word label spill-counts post-order ;
|
2008-11-03 00:09:31 -05:00
|
|
|
|
2009-05-29 14:11:34 -04:00
|
|
|
: <cfg> ( entry word label -- cfg ) f f cfg boa ;
|
2008-11-03 00:09:31 -05:00
|
|
|
|
2009-05-29 14:11:34 -04:00
|
|
|
TUPLE: mr { instructions array } word label ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-17 20:31:35 -04:00
|
|
|
: <mr> ( instructions word label -- mr )
|
|
|
|
mr new
|
|
|
|
swap >>label
|
|
|
|
swap >>word
|
|
|
|
swap >>instructions ;
|