1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel arrays sequences accessors combinators math
4 namespaces init sets words assocs alien.libraries alien
5 alien.private alien.c-types fry quotations strings
6 stack-checker.backend stack-checker.errors stack-checker.visitor
7 stack-checker.dependencies stack-checker.state
8 compiler.utilities effects ;
9 FROM: kernel.private => declare ;
10 IN: stack-checker.alien
12 TUPLE: alien-node-params
14 { abi abi initial: cdecl }
18 TUPLE: alien-invoke-params < alien-node-params library { function string } ;
20 TUPLE: alien-indirect-params < alien-node-params ;
22 TUPLE: alien-assembly-params < alien-node-params { quot callable } ;
24 TUPLE: alien-callback-params < alien-node-params xt ;
26 : param-prep-quot ( params -- quot )
27 parameters>> [ c-type c-type-unboxer-quot ] map deep-spread>quot ;
29 : alien-stack ( params extra -- )
30 over parameters>> length + consume-d >>in-d
31 dup return>> void? 0 1 ? produce-d >>out-d
34 : return-prep-quot ( params -- quot )
35 return>> [ [ ] ] [ c-type c-type-boxer-quot ] if-void ;
37 : infer-return ( params -- )
38 return-prep-quot infer-quot-here ;
40 : pop-return ( params -- params )
41 pop-literal [ depends-on-c-type ] [ nip >>return ] bi ;
43 : pop-library ( params -- params )
44 pop-literal nip >>library ;
46 : pop-function ( params -- params )
47 pop-literal nip >>function ;
49 : pop-params ( params -- params )
50 pop-literal [ [ depends-on-c-type ] each ] [ nip >>parameters ] bi ;
52 : pop-abi ( params -- params )
53 pop-literal nip >>abi ;
55 : pop-quot ( params -- params )
56 pop-literal nip >>quot ;
58 : infer-alien-invoke ( -- )
59 alien-invoke-params new
60 ! Compile-time parameters
66 dup library>> library-abi >>abi
67 ! Quotation which coerces parameters to required types
68 dup param-prep-quot infer-quot-here
69 ! Magic #: consume exactly the number of inputs
73 ! Quotation which coerces return value to required type
76 : infer-alien-indirect ( -- )
77 alien-indirect-params new
78 ! Compile-time parameters
82 ! Coerce parameters to required types
83 dup param-prep-quot '[ _ [ >c-ptr ] bi* ] infer-quot-here
84 ! Magic #: consume the function pointer, too
88 ! Quotation which coerces return value to required type
91 : infer-alien-assembly ( -- )
92 alien-assembly-params new
93 ! Compile-time parameters
98 ! Quotation which coerces parameters to required types
99 dup param-prep-quot infer-quot-here
100 ! Magic #: consume exactly the number of inputs
104 ! Quotation which coerces return value to required type
107 : callback-xt ( word -- alien )
108 callbacks get [ dup "stack-cleanup" word-prop <callback> ] cache ;
110 : callback-bottom ( params -- )
111 "( callback )" <uninterned-word> >>xt
112 xt>> '[ _ callback-xt { alien } declare ] infer-quot-here ;
114 : callback-return-quot ( ctype -- quot )
115 return>> [ [ ] ] [ c-type c-type-unboxer-quot ] if-void ;
117 : callback-parameter-quot ( params -- quot )
118 parameters>> [ c-type ] map
119 [ [ c-type-class ] map '[ _ declare ] ]
120 [ [ c-type-boxer-quot ] map deep-spread>quot ]
123 GENERIC: wrap-callback-quot ( params quot -- quot' )
125 SYMBOL: wait-for-callback-hook
127 wait-for-callback-hook [ [ drop ] ] initialize
129 M: callable wrap-callback-quot
130 swap [ callback-parameter-quot ] [ callback-return-quot ] bi surround
131 wait-for-callback-hook get
135 : callback-effect ( params -- effect )
136 [ parameters>> length "x" <array> ] [ return>> void? { } { "x" } ? ] bi
139 : infer-callback-quot ( params quot -- child )
145 callback-effect check-effect
149 : infer-alien-callback ( -- )
151 alien-callback-params new
158 ] dip wrap-callback-quot infer-callback-quot