2009-05-27 19:55:49 -04:00
|
|
|
! 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
|
2009-05-27 19:55:49 -04:00
|
|
|
combinators assocs
|
|
|
|
cpu.architecture
|
2008-09-15 02:54:48 -04:00
|
|
|
compiler.cfg
|
|
|
|
compiler.cfg.rpo
|
2009-05-27 19:55:49 -04:00
|
|
|
compiler.cfg.liveness
|
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 -- )
|
|
|
|
|
2009-05-29 06:36:04 -04:00
|
|
|
: linearize-insns ( bb insns -- )
|
2009-05-29 06:45:40 -04:00
|
|
|
dup instructions>> [ linearize-insn ] with each ;
|
2009-05-29 06:36:04 -04:00
|
|
|
|
|
|
|
: gc? ( bb -- ? )
|
|
|
|
instructions>> [ ##allocation? ] any? ;
|
|
|
|
|
|
|
|
: object-pointer-regs ( basic-block -- vregs )
|
2009-05-29 06:45:40 -04:00
|
|
|
live-in keys [ reg-class>> int-regs eq? ] filter ;
|
2009-05-29 06:36:04 -04:00
|
|
|
|
|
|
|
: linearize-basic-block ( bb -- )
|
|
|
|
[ number>> _label ]
|
2009-05-29 06:45:40 -04:00
|
|
|
[ 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.
|
2009-05-27 19:55:49 -04:00
|
|
|
[ number>> ] bi@ 1 - = ; inline
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2008-11-03 22:02:34 -05: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 = [
|
2008-11-03 22:02:34 -05:00
|
|
|
[ 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 ] }
|
2009-05-29 06:36:04 -04:00
|
|
|
{ [ 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) ]
|
2008-11-13 04:52:01 -05:00
|
|
|
[ drop dup successors>> second useless-branch? ] 2bi
|
|
|
|
[ [ swap number>> ] 3dip ] [ [ number>> ] 3dip negate-cc ] if ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2009-05-29 14:11:34 -04:00
|
|
|
: with-regs ( insn quot -- )
|
|
|
|
over regs>> [ call ] dip building get peek (>>regs) ; inline
|
|
|
|
|
2008-10-20 06:55:20 -04:00
|
|
|
M: ##compare-branch linearize-insn
|
2009-05-29 14:11:34 -04:00
|
|
|
[ binary-conditional _compare-branch ] with-regs emit-branch ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2008-10-20 06:55:20 -04:00
|
|
|
M: ##compare-imm-branch linearize-insn
|
2009-05-29 14:11:34 -04:00
|
|
|
[ binary-conditional _compare-imm-branch ] with-regs emit-branch ;
|
2008-10-20 06:55:20 -04:00
|
|
|
|
|
|
|
M: ##compare-float-branch linearize-insn
|
2009-05-29 14:11:34 -04:00
|
|
|
[ binary-conditional _compare-float-branch ] with-regs emit-branch ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2009-05-29 06:36:04 -04:00
|
|
|
M: ##dispatch linearize-insn
|
|
|
|
swap
|
2009-05-29 14:11:34 -04:00
|
|
|
[ [ [ src>> ] [ temp>> ] bi _dispatch ] with-regs ]
|
2009-05-29 06:36:04 -04:00
|
|
|
[ successors>> [ number>> _dispatch-label ] each ]
|
|
|
|
bi* ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2009-05-29 14:11:34 -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 )
|
2009-05-29 14:11:34 -04:00
|
|
|
[ linearize-basic-blocks ] [ word>> ] [ label>> ] tri
|
|
|
|
<mr> ;
|