]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/propagation/simple/simple.factor
Updating code for make and fry changes
[factor.git] / basis / compiler / tree / propagation / simple / simple.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: fry accessors kernel sequences sequences.private assocs words
4 namespaces classes.algebra combinators classes classes.tuple
5 classes.tuple.private continuations arrays
6 math math.private slots generic definitions
7 stack-checker.state
8 compiler.tree
9 compiler.tree.propagation.info
10 compiler.tree.propagation.nodes
11 compiler.tree.propagation.slots
12 compiler.tree.propagation.inlining
13 compiler.tree.propagation.constraints ;
14 IN: compiler.tree.propagation.simple
15
16 ! Propagation for straight-line code.
17
18 M: #introduce propagate-before
19     out-d>> [ object-info swap set-value-info ] each ;
20
21 M: #push propagate-before
22     [ literal>> <literal-info> ] [ out-d>> first ] bi
23     set-value-info ;
24
25 : refine-value-infos ( classes values -- )
26     [ refine-value-info ] 2each ;
27
28 : class-infos ( classes -- infos )
29     [ <class-info> ] map ;
30
31 : set-value-infos ( infos values -- )
32     [ set-value-info ] 2each ;
33
34 M: #declare propagate-before
35     #! We need to force the caller word to recompile when the
36     #! classes mentioned in the declaration are redefined, since
37     #! now we're making assumptions but their definitions.
38     declaration>> [
39         [ inlined-dependency depends-on ]
40         [ <class-info> swap refine-value-info ]
41         bi
42     ] assoc-each ;
43
44 : predicate-constraints ( value class boolean-value -- constraint )
45     [ [ is-instance-of ] dip t--> ]
46     [ [ class-not is-instance-of ] dip f--> ]
47     3bi /\ ;
48
49 : custom-constraints ( #call quot -- )
50     [ [ in-d>> ] [ out-d>> ] bi append ] dip
51     with-datastack first assume ;
52
53 : compute-constraints ( #call word -- )
54     dup "constraints" word-prop [ nip custom-constraints ] [
55         dup predicate? [
56             [ [ in-d>> first ] [ out-d>> first ] bi ]
57             [ "predicating" word-prop ] bi*
58             swap predicate-constraints assume
59         ] [ 2drop ] if
60     ] if* ;
61
62 : call-outputs-quot ( #call word -- infos )
63     [ in-d>> [ value-info ] map ] [ "outputs" word-prop ] bi*
64     with-datastack ;
65
66 : foldable-call? ( #call word -- ? )
67     "foldable" word-prop
68     [ in-d>> [ value-info literal?>> ] all? ] [ drop f ] if ;
69
70 : (fold-call) ( #call word -- info )
71     [ [ out-d>> ] [ in-d>> [ value-info literal>> ] map ] bi ] [ '[ _ execute ] ] bi*
72     '[ _ _ with-datastack [ <literal-info> ] map nip ]
73     [ drop [ object-info ] replicate ]
74     recover ;
75
76 : fold-call ( #call word -- )
77     [ (fold-call) ] [ drop out-d>> ] 2bi set-value-infos ;
78
79 : predicate-output-infos ( info class -- info )
80     [ class>> ] dip {
81         { [ 2dup class<= ] [ t <literal-info> ] }
82         { [ 2dup classes-intersect? not ] [ f <literal-info> ] }
83         [ object-info ]
84     } cond 2nip ;
85
86 : propagate-predicate ( #call word -- infos )
87     #! We need to force the caller word to recompile when the class
88     #! is redefined, since now we're making assumptions but the
89     #! class definition itself.
90     [ in-d>> first value-info ]
91     [ "predicating" word-prop dup inlined-dependency depends-on ] bi*
92     predicate-output-infos 1array ;
93
94 : default-output-value-infos ( #call word -- infos )
95     "default-output-classes" word-prop
96     [ class-infos ] [ out-d>> length object-info <repetition> ] ?if ;
97
98 : output-value-infos ( #call word -- infos )
99     {
100         { [ dup tuple-constructor? ] [ propagate-tuple-constructor ] }
101         { [ dup sequence-constructor? ] [ propagate-sequence-constructor ] }
102         { [ dup predicate? ] [ propagate-predicate ] }
103         { [ dup "outputs" word-prop ] [ call-outputs-quot ] }
104         [ default-output-value-infos ]
105     } cond ;
106
107 M: #call propagate-before
108     dup word>> {
109         { [ 2dup foldable-call? ] [ fold-call ] }
110         { [ 2dup do-inlining ] [ 2drop ] }
111         [
112             [ [ output-value-infos ] [ drop out-d>> ] 2bi set-value-infos ]
113             [ compute-constraints ]
114             2bi
115         ]
116     } cond ;
117
118 M: #call annotate-node
119     dup [ in-d>> ] [ out-d>> ] bi append (annotate-node) ;
120
121 : propagate-input-classes ( node input-classes -- )
122     class-infos swap in-d>> refine-value-infos ;
123
124 M: #call propagate-after
125     dup word>> "input-classes" word-prop dup
126     [ propagate-input-classes ] [ 2drop ] if ;
127
128 M: #alien-invoke propagate-before
129     out-d>> [ object-info swap set-value-info ] each ;
130
131 M: #alien-indirect propagate-before
132     out-d>> [ object-info swap set-value-info ] each ;
133
134 M: #return annotate-node
135     dup in-d>> (annotate-node) ;