2008-09-11 03:05:22 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: kernel math accessors sequences namespaces make
|
2008-09-15 02:54:48 -04:00
|
|
|
combinators
|
|
|
|
compiler.cfg
|
|
|
|
compiler.cfg.rpo
|
2008-10-24 10:17:06 -04:00
|
|
|
compiler.cfg.instructions ;
|
2008-09-11 03:05:22 -04:00
|
|
|
IN: compiler.cfg.linearization
|
|
|
|
|
|
|
|
! Convert CFG IR to machine IR.
|
|
|
|
GENERIC: linearize-insn ( basic-block insn -- )
|
|
|
|
|
|
|
|
: linearize-insns ( basic-block -- )
|
|
|
|
dup instructions>> [ linearize-insn ] with each ; inline
|
|
|
|
|
|
|
|
M: insn linearize-insn , drop ;
|
|
|
|
|
|
|
|
: useless-branch? ( basic-block successor -- ? )
|
|
|
|
#! If our successor immediately follows us in RPO, then we
|
|
|
|
#! don't need to branch.
|
|
|
|
[ number>> 1+ ] [ number>> ] bi* = ; inline
|
|
|
|
|
|
|
|
: branch-to-return? ( successor -- ? )
|
|
|
|
#! A branch to a block containing just a return is cloned.
|
|
|
|
instructions>> dup length 2 = [
|
2008-09-17 01:46:38 -04:00
|
|
|
[ first ##epilogue? ] [ second ##return? ] bi and
|
2008-09-11 03:05:22 -04:00
|
|
|
] [ drop f ] if ;
|
|
|
|
|
|
|
|
: emit-branch ( basic-block successor -- )
|
|
|
|
{
|
|
|
|
{ [ 2dup useless-branch? ] [ 2drop ] }
|
|
|
|
{ [ dup branch-to-return? ] [ nip linearize-insns ] }
|
2008-09-17 01:46:38 -04:00
|
|
|
[ nip number>> _branch ]
|
2008-09-11 03:05:22 -04:00
|
|
|
} cond ;
|
|
|
|
|
2008-09-17 01:46:38 -04:00
|
|
|
M: ##branch linearize-insn
|
2008-09-11 03:05:22 -04:00
|
|
|
drop dup successors>> first emit-branch ;
|
|
|
|
|
|
|
|
: conditional ( basic-block -- basic-block successor1 label2 )
|
2008-09-17 01:46:38 -04:00
|
|
|
dup successors>> first2 swap number>> ; inline
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2008-10-20 02:56:28 -04:00
|
|
|
: binary-conditional ( basic-block insn -- basic-block successor label2 src1 src2 cc )
|
|
|
|
[ conditional ] [ [ src1>> ] [ src2>> ] [ cc>> ] tri ] bi* ; inline
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2008-10-20 06:55:20 -04:00
|
|
|
M: ##compare-branch linearize-insn
|
|
|
|
binary-conditional _compare-branch emit-branch ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2008-10-20 06:55:20 -04:00
|
|
|
M: ##compare-imm-branch linearize-insn
|
|
|
|
binary-conditional _compare-imm-branch emit-branch ;
|
|
|
|
|
|
|
|
M: ##compare-float-branch linearize-insn
|
|
|
|
binary-conditional _compare-float-branch emit-branch ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
|
|
|
: linearize-basic-block ( bb -- )
|
2008-10-22 19:39:41 -04:00
|
|
|
[ number>> _label ]
|
|
|
|
[ gc>> [ _gc ] when ]
|
|
|
|
[ linearize-insns ]
|
|
|
|
tri ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
|
|
|
: linearize-basic-blocks ( rpo -- insns )
|
|
|
|
[ [ linearize-basic-block ] each ] { } make ;
|
|
|
|
|
|
|
|
: build-mr ( cfg -- mr )
|
2008-09-17 20:31:35 -04:00
|
|
|
[ entry>> reverse-post-order linearize-basic-blocks ]
|
|
|
|
[ word>> ] [ label>> ]
|
|
|
|
tri <mr> ;
|