2005-08-04 17:39:39 -04:00
|
|
|
! Copyright (C) 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-09-09 00:17:19 -04:00
|
|
|
IN: optimizer
|
|
|
|
USING: inference kernel namespaces prettyprint sequences vectors ;
|
2005-08-04 17:39:39 -04:00
|
|
|
|
|
|
|
GENERIC: collect-recursion* ( label node -- )
|
|
|
|
|
|
|
|
M: node collect-recursion* ( label node -- ) 2drop ;
|
|
|
|
|
|
|
|
M: #call-label collect-recursion* ( label node -- )
|
|
|
|
tuck node-param = [ node-in-d , ] [ drop ] ifte ;
|
|
|
|
|
2005-09-17 04:15:05 -04:00
|
|
|
: collect-recursion ( #label -- seq )
|
2005-08-04 17:39:39 -04:00
|
|
|
#! Collect the input stacks of all #call-label nodes that
|
|
|
|
#! call given label.
|
2005-09-17 04:15:05 -04:00
|
|
|
dup node-param swap
|
|
|
|
[ [ collect-recursion* ] each-node-with ] @{ }@ make ;
|
2005-08-04 17:39:39 -04:00
|
|
|
|
2005-08-04 23:59:45 -04:00
|
|
|
GENERIC: solve-recursion*
|
|
|
|
|
|
|
|
M: node solve-recursion* ( node -- ) drop ;
|
2005-08-04 17:39:39 -04:00
|
|
|
|
2005-09-17 04:15:05 -04:00
|
|
|
: purge-invariants ( stacks -- seq )
|
|
|
|
#! Output a sequence of values which are not present in the
|
|
|
|
#! same position in each sequence of the stacks sequence.
|
|
|
|
flip [ [ eq? ] monotonic? not ] subset concat ;
|
|
|
|
|
|
|
|
: join-values ( calls entry -- new old live )
|
|
|
|
add unify-lengths
|
|
|
|
[ flip [ unify-values ] map ] keep
|
|
|
|
[ peek ] keep
|
|
|
|
purge-invariants ;
|
|
|
|
|
|
|
|
: entry-values ( node -- new old live )
|
|
|
|
dup collect-recursion swap node-child node-in-d join-values ;
|
2005-08-07 18:11:20 -04:00
|
|
|
|
2005-08-04 23:59:45 -04:00
|
|
|
M: #label solve-recursion* ( node -- )
|
2005-09-17 04:15:05 -04:00
|
|
|
#! #entry node-out-d is abused; its not a stack slice, but
|
|
|
|
#! a set of values.
|
|
|
|
[ entry-values ] keep node-child
|
|
|
|
[ set-node-out-d ] keep
|
|
|
|
node-successor subst-values ;
|
2005-08-04 17:39:39 -04:00
|
|
|
|
2005-08-04 23:59:45 -04:00
|
|
|
: solve-recursion ( node -- )
|
|
|
|
#! Figure out which values survive inner recursions in
|
|
|
|
#! #labels, and those that don't should be fudged.
|
2005-08-06 01:59:49 -04:00
|
|
|
[ solve-recursion* ] each-node ;
|