1 ! Copyright (C) 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators combinators.private compiler.tree
4 compiler.tree.propagation.call-effect compiler.units math effects kernel
5 compiler.tree.builder compiler.tree.optimizer compiler.tree.debugger sequences
6 eval fry kernel.private tools.test ;
7 IN: compiler.tree.propagation.call-effect.tests
10 { t } [ [ + ] cached-effect ( a b -- c ) effect= ] unit-test
11 { t } [ 5 [ + ] curry cached-effect ( a -- c ) effect= ] unit-test
12 { t } [ 5 [ ] curry cached-effect ( -- c ) effect= ] unit-test
13 { t } [ [ dup ] [ drop ] compose cached-effect ( a -- b ) effect= ] unit-test
14 { t } [ [ drop ] [ dup ] compose cached-effect ( a b -- c d ) effect= ] unit-test
15 { t } [ [ 2drop ] [ dup ] compose cached-effect ( a b c -- d e ) effect= ] unit-test
16 { t } [ [ 1 2 3 ] [ 2drop ] compose cached-effect ( -- a ) effect= ] unit-test
17 { t } [ [ 1 2 ] [ 3drop ] compose cached-effect ( a -- ) effect= ] unit-test
21 [ drop ( a -- b ) T{ inline-cache } call-effect-ic ]
23 ( a -- b ) call-effect>quot
26 ! call-effect-slow>quot
28 100 [ sq ] ( a -- b ) call-effect-slow>quot call
37 [ [ datastack ] dip dip ] dip dup terminated?>>
39 dup in>> length swap out>> length
53 ( -- a b c ) call-effect-slow>quot
58 [ ] ( m -- ) call-effect-unsafe?
59 [ ] ( x -- x ) call-effect-unsafe?
64 [ drop f T{ inline-cache } call-effect-ic ]
68 { in-d V{ 165186755 165186756 165186754 } }
69 { out-d { 165186757 } }
73 ! execute-effect-unsafe?
74 [ t ] [ \ + ( a b -- c ) execute-effect-unsafe? ] unit-test
75 [ t ] [ \ + ( a b c -- d e ) execute-effect-unsafe? ] unit-test
76 [ f ] [ \ + ( a b c -- d ) execute-effect-unsafe? ] unit-test
77 [ f ] [ \ call ( x -- ) execute-effect-unsafe? ] unit-test
81 [ boa ] inline-cache new [ update-inline-cache ] keep
82 [ boa ] effect-counter inline-cache boa =
86 : optimized-quot ( quot -- quot' )
87 build-tree optimize-tree nodes>quot ;
89 : compiled-call2 ( a quot: ( a -- b ) -- b )
92 : compiled-execute2 ( a b word: ( a b -- c ) -- c )
95 [ [ 3 ] ] [ [ 1 2 \ + execute( a b -- c ) ] optimized-quot ] unit-test
96 [ [ 3 ] ] [ [ 1 2 [ + ] call( a b -- c ) ] optimized-quot ] unit-test
97 [ [ 3 ] ] [ [ 1 2 '[ _ + ] call( a -- b ) ] optimized-quot ] unit-test
98 [ [ 3 ] ] [ [ 1 2 '[ _ ] [ + ] compose call( a -- b ) ] optimized-quot ] unit-test
100 [ 1 2 { [ + ] } first compiled-call2 ] must-fail
101 [ 3 ] [ 1 2 { + } first compiled-execute2 ] unit-test
102 [ 3 ] [ 1 2 '[ _ + ] compiled-call2 ] unit-test
103 [ 3 ] [ 1 2 '[ _ ] [ + ] compose compiled-call2 ] unit-test
104 [ 3 ] [ 1 2 \ + compiled-execute2 ] unit-test
106 [ 3 ] [ 1 2 { [ + ] } first call( a b -- c ) ] unit-test
107 [ 3 ] [ 1 2 { + } first execute( a b -- c ) ] unit-test
108 [ 3 ] [ 1 2 '[ _ + ] call( a -- b ) ] unit-test
109 [ 3 ] [ 1 2 '[ _ ] [ + ] compose call( a -- b ) ] unit-test
111 [ t ] [ [ 2 '[ _ ] [ + ] compose ] final-info first infer-value ( object -- object ) effect= ] unit-test
112 [ t ] [ [ 2 '[ _ ] 1 '[ _ + ] compose ] final-info first infer-value ( -- object ) effect= ] unit-test
113 [ t ] [ [ 2 '[ _ + ] ] final-info first infer-value ( object -- object ) effect= ] unit-test
114 [ f ] [ [ [ [ ] [ 1 ] if ] ] final-info first infer-value ] unit-test
115 [ t ] [ [ [ 1 ] '[ @ ] ] final-info first infer-value ( -- object ) effect= ] unit-test
116 [ f ] [ [ dup drop ] final-info first infer-value ] unit-test
118 ! This should not hang
119 [ ] [ [ [ dup call( quot -- ) ] dup call( quot -- ) ] final-info drop ] unit-test
120 [ ] [ [ [ dup curry call( quot -- ) ] dup curry call( quot -- ) ] final-info drop ] unit-test
122 ! This should get inlined, because the parameter to the curry is literal even though
123 ! [ boa ] by itself doesn't infer
126 [ V{ a-tuple } ] [ [ a-tuple '[ _ boa ] call( x -- tuple ) ] final-classes ] unit-test
128 ! See if redefinitions are handled correctly
129 : call(-redefine-test ( a -- b ) 1 + ;
131 : test-quotatation ( -- quot ) [ call(-redefine-test ] ;
133 [ t ] [ test-quotatation cached-effect ( a -- b ) effect<= ] unit-test
135 [ ] [ "IN: compiler.tree.propagation.call-effect.tests USE: math : call(-redefine-test ( a b -- c ) + ;" eval( -- ) ] unit-test
137 [ t ] [ test-quotatation cached-effect ( a b -- c ) effect<= ] unit-test
139 : inline-cache-invalidation-test ( a b c -- c ) call( a b -- c ) ;
141 [ 4 ] [ 1 3 test-quotatation inline-cache-invalidation-test ] unit-test
143 [ ] [ "IN: compiler.tree.propagation.call-effect.tests USE: math : call(-redefine-test ( a -- c ) 1 + ;" eval( -- ) ] unit-test
145 [ 1 3 test-quotatation inline-cache-invalidation-test ] [ T{ wrong-values f [ call(-redefine-test ] ( a b -- c ) } = ] must-fail-with
147 ! See if redefining a tuple class bumps effect counter
148 TUPLE: my-tuple a b c ;
150 : my-quot ( -- quot ) [ my-tuple boa ] ;
152 : my-word ( a b c q -- result ) call( a b c -- result ) ;
154 [ T{ my-tuple f 1 2 3 } ] [ 1 2 3 my-quot my-word ] unit-test
156 [ ] [ "IN: compiler.tree.propagation.call-effect.tests TUPLE: my-tuple a b ;" eval( -- ) ] unit-test
158 [ 1 2 3 my-quot my-word ] [ wrong-values? ] must-fail-with