]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/propagation/call-effect/call-effect.factor
sequences: move last2 to the sequences vocab
[factor.git] / basis / compiler / tree / propagation / call-effect / call-effect.factor
1 ! Copyright (C) 2009, 2010 Slava Pestov, Daniel Ehrenberg.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators combinators.private
4 combinators.short-circuit compiler.tree.propagation.info
5 compiler.tree.propagation.inlining compiler.units continuations
6 effects fry kernel kernel.private namespaces quotations
7 sequences stack-checker stack-checker.dependencies
8 stack-checker.transforms words ;
9 IN: compiler.tree.propagation.call-effect
10
11 TUPLE: inline-cache value counter ;
12
13 : inline-cache-hit? ( word/quot ic -- ? )
14     { [ value>> eq? ] [ nip counter>> effect-counter eq? ] } 2&& ; inline
15
16 : update-inline-cache ( word/quot ic -- )
17     swap >>value effect-counter >>counter drop ; inline
18
19 SINGLETON: +unknown+
20
21 GENERIC: cached-effect ( quot -- effect )
22
23 M: object cached-effect drop +unknown+ ;
24
25 GENERIC: curry-effect* ( effect -- effect' )
26
27 M: +unknown+ curry-effect* ;
28
29 M: effect curry-effect* curry-effect ;
30
31 M: curried cached-effect
32     quot>> cached-effect curry-effect* ;
33
34 : compose-effects* ( effect1 effect2 -- effect' )
35     {
36         { [ 2dup [ effect? ] both? ] [ compose-effects ] }
37         { [ 2dup [ +unknown+ eq? ] either? ] [ 2drop +unknown+ ] }
38     } cond ;
39
40 M: composed cached-effect
41     [ first>> ] [ second>> ] bi [ cached-effect ] bi@ compose-effects* ;
42
43 : safe-infer ( quot -- effect )
44     error get-global error-continuation get-global
45     [ [ [ infer ] [ 2drop +unknown+ ] recover ] without-dependencies ] 2dip
46     [ error set-global ] [ error-continuation set-global ] bi* ;
47
48 : cached-effect-valid? ( quot -- ? )
49     cache-counter>> effect-counter eq? ; inline
50
51 : save-effect ( effect quot -- )
52     swap >>cached-effect effect-counter >>cache-counter drop ;
53
54 M: quotation cached-effect
55     dup cached-effect-valid?
56     [ cached-effect>> ] [ [ safe-infer dup ] keep save-effect ] if ;
57
58 : call-effect-slow>quot ( effect -- quot )
59     [ \ call-effect def>> curry ] [ add-effect-input ] bi
60     '[ _ _ call-effect-unsafe ] ;
61
62 : call-effect-slow ( quot effect -- ) drop call ;
63
64 \ call-effect-slow [ call-effect-slow>quot ] 1 define-transform
65
66 \ call-effect-slow t "no-compile" set-word-prop
67
68 : call-effect-unsafe? ( quot effect -- ? )
69     [ cached-effect ] dip
70     over +unknown+ eq?
71     [ 2drop f ] [ [ { effect } declare ] dip effect<= ] if ; inline
72
73 : call-effect-fast ( quot effect inline-cache -- )
74     2over call-effect-unsafe?
75     [ [ nip update-inline-cache ] [ drop call-effect-unsafe ] 3bi ]
76     [ drop call-effect-slow ]
77     if ; inline
78
79 : call-effect-ic ( quot effect inline-cache -- )
80     3dup nip inline-cache-hit?
81     [ drop call-effect-unsafe ]
82     [ call-effect-fast ]
83     if ; inline
84
85 : call-effect>quot ( effect -- quot )
86     inline-cache new '[ drop _ _ call-effect-ic ] ;
87
88 : execute-effect-slow ( word effect -- )
89     [ '[ _ execute ] ] dip call-effect-slow ; inline
90
91 : execute-effect-unsafe? ( word effect -- ? )
92     over word-optimized?
93     [ [ stack-effect { effect } declare ] dip effect<= ]
94     [ 2drop f ]
95     if ; inline
96
97 : execute-effect-fast ( word effect inline-cache -- )
98     2over execute-effect-unsafe?
99     [ [ nip update-inline-cache ] [ drop execute-effect-unsafe ] 3bi ]
100     [ drop execute-effect-slow ]
101     if ; inline
102
103 : execute-effect-ic ( word effect inline-cache -- )
104     3dup nip inline-cache-hit?
105     [ drop execute-effect-unsafe ]
106     [ execute-effect-fast ]
107     if ; inline
108
109 : execute-effect>quot ( effect -- quot )
110     inline-cache new '[ drop _ _ execute-effect-ic ] ;
111
112 GENERIC: already-inlined-quot? ( quot -- ? )
113
114 M: curried already-inlined-quot? quot>> already-inlined-quot? ;
115
116 M: composed already-inlined-quot?
117     {
118         [ first>> already-inlined-quot? ]
119         [ second>> already-inlined-quot? ]
120     } 1|| ;
121
122 M: quotation already-inlined-quot? already-inlined? ;
123
124 GENERIC: add-quot-to-history ( quot -- )
125
126 M: curried add-quot-to-history quot>> add-quot-to-history ;
127
128 M: composed add-quot-to-history
129     [ first>> add-quot-to-history ]
130     [ second>> add-quot-to-history ] bi ;
131
132 M: quotation add-quot-to-history add-to-history ;
133
134 : top-two ( #call -- effect value )
135     in-d>> last2 [ value-info ] bi@
136     literal>> swap ;
137
138 ERROR: uninferable ;
139
140 : remove-effect-input ( effect -- effect' )
141     ( -- object ) swap compose-effects ;
142
143 : (infer-value) ( value-info -- effect )
144     dup literal?>> [
145         literal>>
146         [ callable? [ uninferable ] unless ]
147         [ already-inlined-quot? [ uninferable ] when ]
148         [ safe-infer dup +unknown+ = [ uninferable ] when ] tri
149     ] [
150         dup { [ slots>> empty? not ] [ class>> ] } 1&& {
151             { \ curried [ slots>> third (infer-value) remove-effect-input ] }
152             { \ composed [ slots>> last2 [ (infer-value) ] bi@ compose-effects ] }
153             [ uninferable ]
154         } case
155     ] if ;
156
157 : infer-value ( value-info -- effect/f )
158     '[ _ (infer-value) ] [ uninferable? ] ignore-error/f ;
159
160 : (value>quot) ( value-info -- quot )
161     dup literal?>> [
162         literal>> [ add-quot-to-history ] [ '[ drop @ ] ] bi
163     ] [
164         dup class>> {
165             { \ curried [
166                 slots>> third (value>quot)
167                 '[ [ obj>> ] [ quot>> @ ] bi ]
168             ] }
169             { \ composed [
170                 slots>> last2 [ (value>quot) ] bi@
171                 '[ [ first>> @ ] [ second>> @ ] bi ]
172             ] }
173         } case
174     ] if ;
175
176 : value>quot ( value-info -- quot: ( code effect -- ) )
177     (value>quot) '[ drop @ ] ;
178
179 : call-inlining ( #call -- quot/f )
180     top-two dup infer-value [
181         pick effect<=
182         [ nip value>quot ]
183         [ drop call-effect>quot ] if
184     ] [ drop call-effect>quot ] if* ;
185
186 \ call-effect [ call-inlining ] "custom-inlining" set-word-prop
187
188 : execute-inlining ( #call -- quot/f )
189     top-two >literal< [
190         2dup swap execute-effect-unsafe?
191         [ nip '[ 2drop _ execute ] ]
192         [ drop execute-effect>quot ] if
193     ] [ drop execute-effect>quot ] if ;
194
195 \ execute-effect [ execute-inlining ] "custom-inlining" set-word-prop