1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors compiler.tree compiler.tree.def-use kernel
4 namespaces sequences sets stack-checker.branches ;
5 IN: compiler.tree.def-use.simplified
7 ! Simplified def-use follows chains of copies.
9 ! A 'real' usage is a usage of a value that is not a #renaming.
10 TUPLE: real-usage value node ;
14 SYMBOLS: visited accum ;
16 : if-not-visited ( value quot -- )
17 over visited get ?adjoin [ call ] [ 2drop ] if ; inline
19 : with-simplified-def-use ( quot -- real-usages )
21 HS{ } clone visited namespaces:set
22 HS{ } clone accum namespaces:set
30 GENERIC: actually-defined-by* ( value node -- )
32 : (actually-defined-by) ( value -- )
33 [ dup defined-by actually-defined-by* ] if-not-visited ;
35 M: #renaming actually-defined-by*
36 inputs/outputs swap [ index ] dip nth (actually-defined-by) ;
38 M: #call-recursive actually-defined-by*
39 [ out-d>> index ] [ label>> return>> in-d>> nth ] bi
40 (actually-defined-by) ;
42 M: #enter-recursive actually-defined-by*
43 [ out-d>> index ] keep
44 [ in-d>> nth (actually-defined-by) ]
45 [ label>> calls>> [ node>> in-d>> nth (actually-defined-by) ] with each ] 2bi ;
47 M: #phi actually-defined-by*
48 [ out-d>> index ] [ phi-in-d>> ] bi
51 [ drop ] [ (actually-defined-by) ] if
54 M: node actually-defined-by*
55 real-usage boa accum get adjoin ;
57 : actually-defined-by ( value -- real-usages )
58 [ (actually-defined-by) ] with-simplified-def-use ;
61 GENERIC: actually-used-by* ( value node -- )
63 : (actually-used-by) ( value -- )
64 [ dup used-by [ actually-used-by* ] with each ] if-not-visited ;
66 M: #renaming actually-used-by*
67 inputs/outputs [ indices ] dip nths
68 [ (actually-used-by) ] each ;
70 M: #return-recursive actually-used-by*
72 [ out-d>> nth (actually-used-by) ]
73 [ label>> calls>> [ node>> out-d>> nth (actually-used-by) ] with each ] 2bi ;
75 M: #call-recursive actually-used-by*
76 [ in-d>> index ] [ label>> enter-out>> nth ] bi
79 M: #enter-recursive actually-used-by*
80 [ in-d>> index ] [ out-d>> nth ] bi (actually-used-by) ;
82 M: #phi actually-used-by*
83 [ phi-in-d>> [ index ] with map-find drop ] [ out-d>> nth ] bi
86 M: #recursive actually-used-by* 2drop ;
88 M: node actually-used-by*
89 real-usage boa accum get adjoin ;
91 : actually-used-by ( value -- real-usages )
92 [ (actually-used-by) ] with-simplified-def-use ;