1 ! Copyright (C) 2004, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: arrays generic hashtables inference kernel math
5 namespaces sequences words ;
7 : node-union ( node quot -- hash )
9 swap [ swap call [ dup set ] each ] each-node-with
12 GENERIC: literals* ( node -- seq )
14 : literals ( node -- hash )
15 [ literals* ] node-union ;
17 GENERIC: live-values* ( node -- seq )
19 : live-values ( node -- hash )
20 #! All values that are returned or passed to calls.
21 [ live-values* ] node-union ;
23 : kill-node* ( values node -- )
24 2dup [ node-in-d remove-all ] keep set-node-in-d
25 2dup [ node-out-d remove-all ] keep set-node-out-d
26 2dup [ node-in-r remove-all ] keep set-node-in-r
27 [ node-out-r remove-all ] keep set-node-out-r ;
29 : kill-node ( values node -- )
31 [ 2drop ] [ [ kill-node* ] each-node-with ] if ;
33 : kill-values ( node -- )
34 dup live-values over literals hash-diff swap kill-node ;
37 M: node literals* drop { } ;
40 node-in-d [ value? ] subset ;
43 M: #push literals* node-out-d ;
46 M: #return live-values*
47 #! Values returned by local labels can be killed.
48 dup node-param [ drop { } ] [ delegate live-values* ] if ;
50 ! nodes that don't use their values directly
52 #push #shuffle #>r #r> #call-label #merge #values #entry ;
54 M: #killable live-values* drop { } ;
56 : purge-invariants ( stacks -- seq )
57 #! Output a sequence of values which are not present in the
58 #! same position in each sequence of the stacks sequence.
59 unify-lengths flip [ all-eq? not ] subset concat ;
62 M: #label live-values*
63 dup node-child node-in-d over node-in-d 2array
64 swap collect-recursion append purge-invariants ;
67 UNION: #branch #if #dispatch ;
69 M: #branch live-values*
70 #! This assumes that the last element of each branch is a
72 dup delegate live-values* >r
73 node-children [ last-node node-in-d ] map purge-invariants