factor/basis/compiler/cfg/linearization/linearization.factor

91 lines
2.7 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2009 Slava Pestov.
2008-09-11 03:05:22 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math accessors sequences namespaces make
combinators assocs
cpu.architecture
2008-09-15 02:54:48 -04:00
compiler.cfg
compiler.cfg.rpo
compiler.cfg.liveness
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 ( bb insns -- )
dup instructions>> [ linearize-insn ] with each ;
: gc? ( bb -- ? )
instructions>> [ ##allocation? ] any? ;
: object-pointer-regs ( basic-block -- vregs )
live-in keys [ reg-class>> int-regs eq? ] filter ;
: linearize-basic-block ( bb -- )
[ number>> _label ]
[ dup gc? [ object-pointer-regs _gc ] [ drop ] if ]
[ linearize-insns ]
tri ;
2008-09-11 03:05:22 -04:00
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>> ] bi@ 1 - = ; inline
2008-09-11 03:05:22 -04:00
: branch-to-branch? ( successor -- ? )
#! A branch to a block containing just a jump return is cloned.
2008-09-11 03:05:22 -04:00
instructions>> dup length 2 = [
[ first ##epilogue? ]
[ second [ ##return? ] [ ##jump? ] bi or ] 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-branch? ] [ nip linearize-basic-block ] }
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 ;
2008-12-15 23:21:56 -05:00
: (binary-conditional) ( basic-block insn -- basic-block successor1 successor2 src1 src2 cc )
2008-11-03 00:09:31 -05:00
[ dup successors>> first2 ]
[ [ src1>> ] [ src2>> ] [ cc>> ] tri ] bi* ; 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 )
2008-11-03 00:09:31 -05:00
[ (binary-conditional) ]
[ drop dup successors>> second useless-branch? ] 2bi
[ [ swap number>> ] 3dip ] [ [ number>> ] 3dip negate-cc ] if ;
2008-09-11 03:05:22 -04:00
: with-regs ( insn quot -- )
over regs>> [ call ] dip building get peek (>>regs) ; inline
M: ##compare-branch linearize-insn
[ binary-conditional _compare-branch ] with-regs emit-branch ;
2008-09-11 03:05:22 -04:00
M: ##compare-imm-branch linearize-insn
[ binary-conditional _compare-imm-branch ] with-regs emit-branch ;
M: ##compare-float-branch linearize-insn
[ binary-conditional _compare-float-branch ] with-regs emit-branch ;
2008-09-11 03:05:22 -04:00
M: ##dispatch linearize-insn
swap
[ [ [ src>> ] [ temp>> ] bi _dispatch ] with-regs ]
[ successors>> [ number>> _dispatch-label ] each ]
bi* ;
2008-09-11 03:05:22 -04:00
: linearize-basic-blocks ( cfg -- insns )
[
[ [ linearize-basic-block ] each-basic-block ]
[ spill-counts>> _spill-counts ]
bi
] { } make ;
2008-09-11 03:05:22 -04:00
: build-mr ( cfg -- mr )
[ linearize-basic-blocks ] [ word>> ] [ label>> ] tri
<mr> ;