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-06-02 19:23:47 -04:00
|
|
|
combinators assocs arrays locals cpu.architecture
|
2008-09-15 02:54:48 -04:00
|
|
|
compiler.cfg
|
2009-07-13 15:42:52 -04:00
|
|
|
compiler.cfg.comparisons
|
2009-06-02 19:23:47 -04:00
|
|
|
compiler.cfg.stack-frame
|
2009-07-22 07:05:17 -04:00
|
|
|
compiler.cfg.instructions
|
2009-07-28 22:31:08 -04:00
|
|
|
compiler.cfg.utilities
|
|
|
|
compiler.cfg.linearization.order ;
|
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-basic-block ( bb -- )
|
2009-07-28 22:31:08 -04:00
|
|
|
[ block-number _label ]
|
2009-05-31 13:20:46 -04:00
|
|
|
[ dup instructions>> [ linearize-insn ] with each ]
|
|
|
|
bi ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
|
|
|
M: insn linearize-insn , drop ;
|
|
|
|
|
|
|
|
: useless-branch? ( basic-block successor -- ? )
|
2009-07-28 22:31:08 -04:00
|
|
|
! If our successor immediately follows us in linearization
|
|
|
|
! order then we don't need to branch.
|
|
|
|
[ block-number ] bi@ 1 - = ; inline
|
2009-07-22 07:05:17 -04:00
|
|
|
|
|
|
|
: emit-branch ( bb successor -- )
|
2009-07-28 22:31:08 -04:00
|
|
|
2dup useless-branch? [ 2drop ] [ nip block-number _branch ] if ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
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 ;
|
|
|
|
|
2009-07-16 19:29:40 -04:00
|
|
|
: successors ( bb -- first second ) successors>> first2 ; inline
|
|
|
|
|
2009-07-22 07:05:17 -04:00
|
|
|
: (binary-conditional) ( bb insn -- bb successor1 successor2 src1 src2 cc )
|
2009-07-16 19:29:40 -04:00
|
|
|
[ dup successors ]
|
2008-11-03 00:09:31 -05:00
|
|
|
[ [ src1>> ] [ src2>> ] [ cc>> ] tri ] bi* ; inline
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2009-07-22 07:05:17 -04:00
|
|
|
: binary-conditional ( bb insn -- bb 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
|
2009-07-28 22:31:08 -04:00
|
|
|
[ [ swap block-number ] 3dip ] [ [ block-number ] 3dip negate-cc ] if ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2008-10-20 06:55:20 -04:00
|
|
|
M: ##compare-branch linearize-insn
|
2009-07-29 07:50:46 -04:00
|
|
|
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
|
2009-07-29 07:50:46 -04:00
|
|
|
binary-conditional _compare-imm-branch emit-branch ;
|
2008-10-20 06:55:20 -04:00
|
|
|
|
|
|
|
M: ##compare-float-branch linearize-insn
|
2009-07-29 07:50:46 -04:00
|
|
|
binary-conditional _compare-float-branch emit-branch ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2009-07-22 07:05:17 -04:00
|
|
|
: overflow-conditional ( bb insn -- bb successor label2 dst src1 src2 )
|
2009-07-28 22:31:08 -04:00
|
|
|
[ dup successors block-number ]
|
2009-07-16 19:29:40 -04:00
|
|
|
[ [ dst>> ] [ src1>> ] [ src2>> ] tri ] bi* ; inline
|
|
|
|
|
|
|
|
M: ##fixnum-add linearize-insn
|
2009-07-29 07:50:46 -04:00
|
|
|
overflow-conditional _fixnum-add emit-branch ;
|
2009-07-16 19:29:40 -04:00
|
|
|
|
|
|
|
M: ##fixnum-sub linearize-insn
|
2009-07-29 07:50:46 -04:00
|
|
|
overflow-conditional _fixnum-sub emit-branch ;
|
2009-07-16 19:29:40 -04:00
|
|
|
|
|
|
|
M: ##fixnum-mul linearize-insn
|
2009-07-29 07:50:46 -04:00
|
|
|
overflow-conditional _fixnum-mul emit-branch ;
|
2009-07-16 19:29:40 -04:00
|
|
|
|
2009-05-29 06:36:04 -04:00
|
|
|
M: ##dispatch linearize-insn
|
|
|
|
swap
|
2009-07-29 07:50:46 -04:00
|
|
|
[ [ src>> ] [ temp>> ] bi _dispatch ]
|
2009-07-28 22:31:08 -04:00
|
|
|
[ successors>> [ block-number _dispatch-label ] each ]
|
2009-05-29 06:36:04 -04:00
|
|
|
bi* ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2009-07-04 00:11:23 -04:00
|
|
|
: (compute-gc-roots) ( n live-values -- n )
|
2009-06-02 19:23:47 -04:00
|
|
|
[
|
2009-07-04 00:11:23 -04:00
|
|
|
[ nip 2array , ]
|
|
|
|
[ drop reg-class>> reg-size + ]
|
|
|
|
3bi
|
|
|
|
] assoc-each ;
|
2009-06-02 19:23:47 -04:00
|
|
|
|
2009-07-04 00:11:23 -04:00
|
|
|
: oop-values ( regs -- regs' )
|
|
|
|
[ drop reg-class>> int-regs eq? ] assoc-filter ;
|
2009-06-02 19:23:47 -04:00
|
|
|
|
2009-07-04 00:11:23 -04:00
|
|
|
: data-values ( regs -- regs' )
|
|
|
|
[ drop reg-class>> double-float-regs eq? ] assoc-filter ;
|
2009-06-02 19:23:47 -04:00
|
|
|
|
2009-07-04 00:11:23 -04:00
|
|
|
: compute-gc-roots ( live-values -- alist )
|
2009-06-02 19:23:47 -04:00
|
|
|
[
|
2009-07-04 00:11:23 -04:00
|
|
|
[ 0 ] dip
|
2009-06-02 19:23:47 -04:00
|
|
|
! we put float registers last; the GC doesn't actually scan them
|
2009-07-04 00:11:23 -04:00
|
|
|
[ oop-values (compute-gc-roots) ]
|
|
|
|
[ data-values (compute-gc-roots) ] bi
|
2009-06-02 19:23:47 -04:00
|
|
|
drop
|
|
|
|
] { } make ;
|
|
|
|
|
2009-07-04 00:11:23 -04:00
|
|
|
: count-gc-roots ( live-values -- n )
|
2009-06-02 19:23:47 -04:00
|
|
|
! Size of GC root area, minus the float registers
|
2009-07-04 00:11:23 -04:00
|
|
|
oop-values assoc-size ;
|
2009-06-02 19:23:47 -04:00
|
|
|
|
|
|
|
M: ##gc linearize-insn
|
|
|
|
nip
|
2009-07-30 10:19:44 -04:00
|
|
|
{
|
|
|
|
[ temp1>> ]
|
|
|
|
[ temp2>> ]
|
|
|
|
[
|
|
|
|
live-values>>
|
|
|
|
[ compute-gc-roots ]
|
|
|
|
[ count-gc-roots ]
|
|
|
|
[ gc-roots-size ]
|
|
|
|
tri
|
|
|
|
]
|
|
|
|
[ uninitialized-locs>> ]
|
|
|
|
} cleave
|
2009-07-29 07:50:46 -04:00
|
|
|
_gc ;
|
2009-06-02 19:23:47 -04:00
|
|
|
|
2009-05-29 14:11:34 -04:00
|
|
|
: linearize-basic-blocks ( cfg -- insns )
|
|
|
|
[
|
2009-07-28 22:31:08 -04:00
|
|
|
[ linearization-order [ linearize-basic-block ] each ]
|
2009-05-29 14:11:34 -04:00
|
|
|
[ spill-counts>> _spill-counts ]
|
|
|
|
bi
|
|
|
|
] { } make ;
|
2008-09-11 03:05:22 -04:00
|
|
|
|
2009-05-31 13:20:46 -04:00
|
|
|
: flatten-cfg ( cfg -- mr )
|
2009-05-29 14:11:34 -04:00
|
|
|
[ linearize-basic-blocks ] [ word>> ] [ label>> ] tri
|
|
|
|
<mr> ;
|