1 IN: compiler.tree.escape-analysis.tests
2 USING: compiler.tree.escape-analysis
3 compiler.tree.escape-analysis.allocations compiler.tree.builder
4 compiler.tree.recursive compiler.tree.normalization
5 math.functions compiler.tree.propagation compiler.tree.cleanup
6 compiler.tree.combinators compiler.tree sequences math
7 math.private kernel tools.test accessors slots.private
8 quotations.private prettyprint classes.tuple.private classes
9 classes.tuple namespaces
10 compiler.tree.propagation.info stack-checker.errors
14 \ escape-analysis must-infer
16 GENERIC: count-unboxed-allocations* ( m node -- n )
18 : (count-unboxed-allocations) ( m node -- n )
19 out-d>> first escaping-allocation? [ 1+ ] unless ;
21 M: #call count-unboxed-allocations*
22 dup [ immutable-tuple-boa? ] [ word>> \ <complex> eq? ] bi or
23 [ (count-unboxed-allocations) ] [ drop ] if ;
25 M: #push count-unboxed-allocations*
26 dup literal>> class immutable-tuple-class?
27 [ (count-unboxed-allocations) ] [ drop ] if ;
29 M: node count-unboxed-allocations* drop ;
31 : count-unboxed-allocations ( quot -- sizes )
39 0 swap [ count-unboxed-allocations* ] each-node ;
41 [ 0 ] [ [ [ + ] curry ] count-unboxed-allocations ] unit-test
43 [ 1 ] [ [ [ + ] curry drop ] count-unboxed-allocations ] unit-test
45 [ 1 ] [ [ [ + ] curry 3 slot ] count-unboxed-allocations ] unit-test
47 [ 1 ] [ [ [ + ] curry 3 slot drop ] count-unboxed-allocations ] unit-test
49 [ 1 ] [ [ [ + ] curry uncurry ] count-unboxed-allocations ] unit-test
51 [ 1 ] [ [ [ + ] curry call ] count-unboxed-allocations ] unit-test
53 [ 1 ] [ [ [ + ] curry call ] count-unboxed-allocations ] unit-test
55 [ 0 ] [ [ [ [ + ] curry ] [ drop [ ] ] if ] count-unboxed-allocations ] unit-test
58 [ [ [ + ] curry ] [ [ * ] curry ] if uncurry ] count-unboxed-allocations
62 [ [ [ + ] curry ] [ [ * ] curry ] if ] count-unboxed-allocations
66 [ [ [ + ] curry ] [ dup [ [ * ] curry ] [ [ / ] curry ] if ] if uncurry ] count-unboxed-allocations
70 [ [ [ + ] curry 4 ] [ dup [ [ * ] curry ] [ [ / ] curry ] if uncurry ] if ] count-unboxed-allocations
74 [ [ [ + ] curry ] [ dup [ [ * ] curry ] [ [ / ] curry ] if ] if ] count-unboxed-allocations
77 TUPLE: cons { car read-only } { cdr read-only } ;
88 ] count-unboxed-allocations
102 ] count-unboxed-allocations
114 ] count-unboxed-allocations
128 ] count-unboxed-allocations
142 ] count-unboxed-allocations
147 [ dup cons boa ] [ drop 1 2 cons boa ] if car>>
148 ] count-unboxed-allocations
154 [ cons boa ] [ cons boa 3 cons boa ] if
155 [ car>> ] [ cdr>> ] bi
156 ] count-unboxed-allocations
161 3dup [ cons boa ] [ cons boa . 1 2 cons boa ] if
162 [ car>> ] [ cdr>> ] bi
163 ] count-unboxed-allocations
167 [ [ 3 cons boa ] [ "A" throw ] if car>> ]
168 count-unboxed-allocations
172 [ 10 [ drop ] each-integer ] count-unboxed-allocations
177 1 2 cons boa 10 [ 2drop 1 2 cons boa ] each-integer car>>
178 ] count-unboxed-allocations
183 1 2 cons boa 10 [ drop 2 cons boa ] each-integer car>>
184 ] count-unboxed-allocations
187 : infinite-cons-loop ( a -- b ) 2 cons boa infinite-cons-loop ; inline recursive
191 1 2 cons boa infinite-cons-loop
192 ] count-unboxed-allocations
199 [ 0 ] [ [ <rw-box> i>> ] count-unboxed-allocations ] unit-test
201 : fake-fib ( m -- n )
202 dup i>> 1 <= [ drop 1 <rw-box> ] when ; inline recursive
204 [ 0 ] [ [ <rw-box> fake-fib i>> ] count-unboxed-allocations ] unit-test
206 TUPLE: ro-box { i read-only } ;
210 : tuple-fib ( m -- n )
219 swap i>> swap i>> + <ro-box>
220 ] if ; inline recursive
222 [ 5 ] [ [ <ro-box> tuple-fib i>> ] count-unboxed-allocations ] unit-test
224 [ 3 ] [ [ <ro-box> tuple-fib ] count-unboxed-allocations ] unit-test
226 : tuple-fib' ( m -- n )
227 dup 1 <= [ 1- tuple-fib' i>> ] when <ro-box> ; inline recursive
229 [ 0 ] [ [ tuple-fib' ] count-unboxed-allocations ] unit-test
231 : bad-tuple-fib-1 ( m -- n )
239 bad-tuple-fib-1 dup .
240 swap i>> swap i>> + <ro-box>
241 ] if ; inline recursive
243 [ 3 ] [ [ <ro-box> bad-tuple-fib-1 i>> ] count-unboxed-allocations ] unit-test
245 : bad-tuple-fib-2 ( m -- n )
255 swap i>> swap i>> + <ro-box>
256 ] if ; inline recursive
258 [ 2 ] [ [ <ro-box> bad-tuple-fib-2 i>> ] count-unboxed-allocations ] unit-test
260 : tuple-fib-2 ( m -- n )
267 swap i>> swap i>> + <ro-box>
268 ] if ; inline recursive
270 [ 2 ] [ [ tuple-fib-2 i>> ] count-unboxed-allocations ] unit-test
272 : tuple-fib-3 ( m -- n )
279 swap i>> swap i>> + <ro-box>
280 ] if ; inline recursive
282 [ 0 ] [ [ tuple-fib-3 i>> ] count-unboxed-allocations ] unit-test
284 : bad-tuple-fib-3 ( m -- n )
288 1- dup bad-tuple-fib-3
292 ] if ; inline recursive
294 [ 0 ] [ [ bad-tuple-fib-3 i>> ] count-unboxed-allocations ] unit-test
296 [ 1 ] [ [ <complex> >rect ] count-unboxed-allocations ] unit-test
298 [ 0 ] [ [ 1 cons boa 2 cons boa ] count-unboxed-allocations ] unit-test
300 [ 1 ] [ [ 1 cons boa 2 cons boa car>> ] count-unboxed-allocations ] unit-test
302 [ 0 ] [ [ 1 cons boa 2 cons boa dup . car>> ] count-unboxed-allocations ] unit-test
304 [ 0 ] [ [ 1 cons boa "x" get slot ] count-unboxed-allocations ] unit-test
306 : impeach-node ( quot: ( node -- ) -- )
307 dup slip impeach-node ; inline recursive
309 : bleach-node ( quot: ( node -- ) -- )
310 [ bleach-node ] curry [ ] compose impeach-node ; inline recursive
312 [ 3 ] [ [ [ ] bleach-node ] count-unboxed-allocations ] unit-test
315 [ dup -1 over >= [ 0 >= [ "A" throw ] unless ] [ drop ] if ]
316 count-unboxed-allocations
320 [ \ too-many->r boa f f \ inference-error boa ]
321 count-unboxed-allocations
325 [ { null } declare [ 1 ] [ 2 ] if ] count-unboxed-allocations