2009-06-30 21:13:35 -04:00
|
|
|
! Copyright (C) 2004, 2009 Slava Pestov.
|
2008-07-20 05:24:37 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-09-10 23:11:03 -04:00
|
|
|
USING: accessors arrays assocs combinators hashtables kernel
|
2008-09-17 19:52:11 -04:00
|
|
|
math fry namespaces make sequences words byte-arrays
|
2008-10-20 02:56:28 -04:00
|
|
|
layouts alien.c-types alien.structs
|
|
|
|
stack-checker.inlining cpu.architecture
|
2008-08-11 03:49:37 -04:00
|
|
|
compiler.tree
|
2008-09-10 23:11:03 -04:00
|
|
|
compiler.tree.builder
|
2008-08-11 03:49:37 -04:00
|
|
|
compiler.tree.combinators
|
|
|
|
compiler.tree.propagation.info
|
|
|
|
compiler.cfg
|
2008-10-20 21:40:15 -04:00
|
|
|
compiler.cfg.hats
|
|
|
|
compiler.cfg.stacks
|
2008-10-22 19:38:30 -04:00
|
|
|
compiler.cfg.utilities
|
2008-09-15 02:54:48 -04:00
|
|
|
compiler.cfg.registers
|
2008-10-20 21:40:15 -04:00
|
|
|
compiler.cfg.intrinsics
|
2009-06-02 19:23:47 -04:00
|
|
|
compiler.cfg.stack-frame
|
2008-10-20 02:56:28 -04:00
|
|
|
compiler.cfg.instructions
|
2008-09-15 02:54:48 -04:00
|
|
|
compiler.alien ;
|
2008-07-20 05:24:37 -04:00
|
|
|
IN: compiler.cfg.builder
|
|
|
|
|
2008-10-20 02:56:28 -04:00
|
|
|
! Convert tree SSA IR to CFG SSA IR.
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
SYMBOL: procedures
|
|
|
|
SYMBOL: current-word
|
|
|
|
SYMBOL: current-label
|
|
|
|
SYMBOL: loops
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
: add-procedure ( -- )
|
|
|
|
basic-block get current-word get current-label get
|
2008-09-11 03:05:22 -04:00
|
|
|
<cfg> procedures get push ;
|
2008-08-11 03:49:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
: begin-procedure ( word label -- )
|
|
|
|
end-basic-block
|
|
|
|
begin-basic-block
|
|
|
|
H{ } clone loops set
|
|
|
|
current-label set
|
|
|
|
current-word set
|
|
|
|
add-procedure ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
: with-cfg-builder ( nodes word label quot -- )
|
|
|
|
'[ begin-procedure @ ] with-scope ; inline
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2009-06-30 21:13:35 -04:00
|
|
|
GENERIC: emit-node ( node -- )
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
: check-basic-block ( node -- node' )
|
|
|
|
basic-block get [ drop f ] unless ; inline
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
: emit-nodes ( nodes -- )
|
2009-06-30 21:13:35 -04:00
|
|
|
[ basic-block get [ emit-node ] [ drop ] if ] each ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
: begin-word ( -- )
|
2008-09-17 01:46:38 -04:00
|
|
|
##prologue
|
|
|
|
##branch
|
2009-06-30 21:13:35 -04:00
|
|
|
begin-basic-block ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
: (build-cfg) ( nodes word label -- )
|
2008-07-20 05:24:37 -04:00
|
|
|
[
|
2008-09-10 23:11:03 -04:00
|
|
|
begin-word
|
2008-10-12 17:46:59 -04:00
|
|
|
emit-nodes
|
2008-09-10 23:11:03 -04:00
|
|
|
] with-cfg-builder ;
|
|
|
|
|
2008-09-17 01:46:38 -04:00
|
|
|
: build-cfg ( nodes word -- procedures )
|
2008-09-10 23:11:03 -04:00
|
|
|
V{ } clone [
|
|
|
|
procedures [
|
2008-09-17 01:46:38 -04:00
|
|
|
dup (build-cfg)
|
2008-09-10 23:11:03 -04:00
|
|
|
] with-variable
|
|
|
|
] keep ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2009-06-30 21:13:35 -04:00
|
|
|
: local-recursive-call ( basic-block -- )
|
2008-09-17 01:46:38 -04:00
|
|
|
##branch
|
2008-09-10 23:11:03 -04:00
|
|
|
basic-block get successors>> push
|
2009-06-30 21:13:35 -04:00
|
|
|
basic-block off ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
2009-06-30 21:13:35 -04:00
|
|
|
: emit-call ( word height -- )
|
|
|
|
over loops get key?
|
|
|
|
[ drop loops get at local-recursive-call ]
|
|
|
|
[ ##call ##branch begin-basic-block ]
|
|
|
|
if ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
! #recursive
|
2009-05-19 18:28:13 -04:00
|
|
|
: recursive-height ( #recursive -- n )
|
|
|
|
[ label>> return>> in-d>> length ] [ in-d>> length ] bi - ;
|
|
|
|
|
2009-06-30 22:07:55 -04:00
|
|
|
: emit-recursive ( #recursive -- )
|
2009-05-19 18:28:13 -04:00
|
|
|
[ [ label>> id>> ] [ recursive-height ] bi emit-call ]
|
2008-09-10 23:11:03 -04:00
|
|
|
[ [ child>> ] [ label>> word>> ] [ label>> id>> ] tri (build-cfg) ] bi ;
|
|
|
|
|
2008-10-10 03:33:32 -04:00
|
|
|
: remember-loop ( label -- )
|
|
|
|
basic-block get swap loops get set-at ;
|
|
|
|
|
2009-06-30 21:13:35 -04:00
|
|
|
: emit-loop ( node -- )
|
2008-11-03 07:20:51 -05:00
|
|
|
##loop-entry
|
2009-05-25 20:16:36 -04:00
|
|
|
##branch
|
2008-09-10 23:11:03 -04:00
|
|
|
begin-basic-block
|
2009-06-30 21:13:35 -04:00
|
|
|
[ label>> id>> remember-loop ] [ child>> emit-nodes ] bi ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
M: #recursive emit-node
|
2009-05-19 18:28:13 -04:00
|
|
|
dup label>> loop?>> [ emit-loop ] [ emit-recursive ] if ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
! #if
|
2008-10-20 02:56:28 -04:00
|
|
|
: emit-branch ( obj -- final-bb )
|
|
|
|
[
|
2008-10-21 04:20:48 -04:00
|
|
|
begin-basic-block
|
2008-10-20 02:56:28 -04:00
|
|
|
emit-nodes
|
2008-09-17 01:46:38 -04:00
|
|
|
basic-block get dup [ ##branch ] when
|
2008-09-10 23:11:03 -04:00
|
|
|
] with-scope ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-10-20 02:56:28 -04:00
|
|
|
: emit-if ( node -- )
|
2009-06-30 21:13:35 -04:00
|
|
|
children>> [ emit-branch ] map
|
2008-08-11 03:49:37 -04:00
|
|
|
end-basic-block
|
2008-09-10 23:11:03 -04:00
|
|
|
begin-basic-block
|
2008-10-21 04:20:48 -04:00
|
|
|
basic-block get '[ [ _ swap successors>> push ] when* ] each ;
|
2008-09-17 01:46:38 -04:00
|
|
|
|
2008-10-20 02:56:28 -04:00
|
|
|
: ##branch-t ( vreg -- )
|
2008-10-20 06:55:20 -04:00
|
|
|
\ f tag-number cc/= ##compare-imm-branch ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
2008-11-06 10:09:21 -05:00
|
|
|
: trivial-branch? ( nodes -- value ? )
|
|
|
|
dup length 1 = [
|
|
|
|
first dup #push? [ literal>> t ] [ drop f f ] if
|
|
|
|
] [ drop f f ] if ;
|
|
|
|
|
|
|
|
: trivial-if? ( #if -- ? )
|
|
|
|
children>> first2
|
|
|
|
[ trivial-branch? [ t eq? ] when ]
|
|
|
|
[ trivial-branch? [ f eq? ] when ] bi*
|
|
|
|
and ;
|
|
|
|
|
|
|
|
: emit-trivial-if ( -- )
|
|
|
|
ds-pop \ f tag-number cc/= ^^compare-imm ds-push ;
|
|
|
|
|
|
|
|
: trivial-not-if? ( #if -- ? )
|
|
|
|
children>> first2
|
|
|
|
[ trivial-branch? [ f eq? ] when ]
|
|
|
|
[ trivial-branch? [ t eq? ] when ] bi*
|
|
|
|
and ;
|
|
|
|
|
|
|
|
: emit-trivial-not-if ( -- )
|
|
|
|
ds-pop \ f tag-number cc= ^^compare-imm ds-push ;
|
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
M: #if emit-node
|
2008-11-06 10:09:21 -05:00
|
|
|
{
|
|
|
|
{ [ dup trivial-if? ] [ drop emit-trivial-if ] }
|
|
|
|
{ [ dup trivial-not-if? ] [ drop emit-trivial-not-if ] }
|
|
|
|
[ ds-pop ##branch-t emit-if ]
|
2009-06-30 21:13:35 -04:00
|
|
|
} cond ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
! #dispatch
|
|
|
|
M: #dispatch emit-node
|
2009-06-30 21:13:35 -04:00
|
|
|
ds-pop ^^offset>slot i ##dispatch emit-if ;
|
2008-08-11 03:49:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
! #call
|
|
|
|
M: #call emit-node
|
2008-10-20 02:56:28 -04:00
|
|
|
dup word>> dup "intrinsic" word-prop
|
2009-05-19 18:28:13 -04:00
|
|
|
[ emit-intrinsic ] [ swap call-height emit-call ] if ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
! #call-recursive
|
2009-05-19 18:28:13 -04:00
|
|
|
M: #call-recursive emit-node [ label>> id>> ] [ call-height ] bi emit-call ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
! #push
|
|
|
|
M: #push emit-node
|
2009-06-30 21:13:35 -04:00
|
|
|
literal>> ^^load-literal ds-push ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
! #shuffle
|
|
|
|
M: #shuffle emit-node
|
2008-11-11 19:46:31 -05:00
|
|
|
dup
|
|
|
|
H{ } clone
|
|
|
|
[ [ in-d>> [ length ds-load ] keep ] dip '[ _ set-at ] 2each ]
|
|
|
|
[ [ in-r>> [ length rs-load ] keep ] dip '[ _ set-at ] 2each ]
|
|
|
|
[ nip ] 2tri
|
|
|
|
[ [ [ out-d>> ] [ mapping>> ] bi ] dip '[ _ at _ at ] map ds-store ]
|
2009-06-30 21:13:35 -04:00
|
|
|
[ [ [ out-r>> ] [ mapping>> ] bi ] dip '[ _ at _ at ] map rs-store ] 2bi ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
! #return
|
|
|
|
M: #return emit-node
|
2009-06-30 21:13:35 -04:00
|
|
|
drop ##epilogue ##return ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
M: #return-recursive emit-node
|
|
|
|
label>> id>> loops get key?
|
2009-06-30 21:13:35 -04:00
|
|
|
[ ##epilogue ##return ] unless ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
! #terminate
|
2009-06-30 21:13:35 -04:00
|
|
|
M: #terminate emit-node drop ;
|
2008-09-10 23:11:03 -04:00
|
|
|
|
|
|
|
! FFI
|
2008-10-07 17:13:29 -04:00
|
|
|
: return-size ( ctype -- n )
|
|
|
|
#! Amount of space we reserve for a return value.
|
|
|
|
{
|
|
|
|
{ [ dup c-struct? not ] [ drop 0 ] }
|
|
|
|
{ [ dup large-struct? not ] [ drop 2 cells ] }
|
|
|
|
[ heap-size ]
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: <alien-stack-frame> ( params -- stack-frame )
|
|
|
|
stack-frame new
|
|
|
|
swap
|
|
|
|
[ return>> return-size >>return ]
|
2008-10-19 02:10:21 -04:00
|
|
|
[ alien-parameters parameter-sizes drop >>params ] bi ;
|
2008-10-07 17:13:29 -04:00
|
|
|
|
2008-10-13 00:32:14 -04:00
|
|
|
: alien-stack-frame ( params -- )
|
|
|
|
<alien-stack-frame> ##stack-frame ;
|
|
|
|
|
2009-06-30 21:13:35 -04:00
|
|
|
: emit-alien-node ( node quot -- )
|
2008-10-13 00:32:14 -04:00
|
|
|
[ params>> ] dip [ drop alien-stack-frame ] [ call ] 2bi
|
2009-06-30 21:13:35 -04:00
|
|
|
##branch begin-basic-block ; inline
|
2008-10-07 17:13:29 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
M: #alien-invoke emit-node
|
2008-10-07 17:13:29 -04:00
|
|
|
[ ##alien-invoke ] emit-alien-node ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
M: #alien-indirect emit-node
|
2008-10-07 17:13:29 -04:00
|
|
|
[ ##alien-indirect ] emit-alien-node ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
M: #alien-callback emit-node
|
2008-10-12 18:37:26 -04:00
|
|
|
dup params>> xt>> dup
|
2008-10-07 17:13:29 -04:00
|
|
|
[
|
2008-10-13 00:32:14 -04:00
|
|
|
##prologue
|
2009-06-30 21:13:35 -04:00
|
|
|
dup [ ##alien-callback ] emit-alien-node
|
2008-10-13 00:32:14 -04:00
|
|
|
##epilogue
|
|
|
|
params>> ##callback-return
|
2009-06-30 21:13:35 -04:00
|
|
|
] with-cfg-builder ;
|
2008-08-11 03:49:37 -04:00
|
|
|
|
2008-09-10 23:11:03 -04:00
|
|
|
! No-op nodes
|
2009-06-30 21:13:35 -04:00
|
|
|
M: #introduce emit-node drop ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2009-06-30 21:13:35 -04:00
|
|
|
M: #copy emit-node drop ;
|
2008-08-11 03:49:37 -04:00
|
|
|
|
2009-06-30 21:13:35 -04:00
|
|
|
M: #enter-recursive emit-node drop ;
|
2008-08-11 03:49:37 -04:00
|
|
|
|
2009-06-30 21:13:35 -04:00
|
|
|
M: #phi emit-node drop ;
|