1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences accessors combinators math namespaces
4 init sets words assocs alien.libraries alien alien.c-types
5 cpu.architecture fry stack-checker.backend stack-checker.errors
6 stack-checker.visitor stack-checker.dependencies ;
7 IN: stack-checker.alien
9 TUPLE: alien-node-params return parameters abi in-d out-d ;
11 TUPLE: alien-invoke-params < alien-node-params library function ;
13 TUPLE: alien-indirect-params < alien-node-params ;
15 TUPLE: alien-assembly-params < alien-node-params quot ;
17 TUPLE: alien-callback-params < alien-node-params quot xt ;
19 : param-prep-quot ( params -- quot )
20 parameters>> [ c-type c-type-unboxer-quot ] map spread>quot ;
22 : infer-params ( params -- )
23 param-prep-quot infer-quot-here ;
25 : alien-stack ( params extra -- )
26 over parameters>> length + consume-d >>in-d
27 dup return>> void? 0 1 ? produce-d >>out-d
30 : return-prep-quot ( params -- quot )
31 return>> [ [ ] ] [ c-type c-type-boxer-quot ] if-void ;
33 : infer-return ( params -- )
34 return-prep-quot infer-quot-here ;
36 : pop-return ( params -- params )
37 pop-literal [ depends-on-c-type ] [ nip >>return ] bi ;
39 : pop-library ( params -- params )
40 pop-literal nip >>library ;
42 : pop-function ( params -- params )
43 pop-literal nip >>function ;
45 : pop-params ( params -- params )
46 pop-literal [ [ depends-on-c-type ] each ] [ nip >>parameters ] bi ;
48 : pop-abi ( params -- params )
49 pop-literal nip >>abi ;
51 : pop-quot ( params -- params )
52 pop-literal nip >>quot ;
54 : infer-alien-invoke ( -- )
55 alien-invoke-params new
56 ! Compile-time parameters
62 dup library>> library [ abi>> ] [ "cdecl" ] if* >>abi
63 ! Quotation which coerces parameters to required types
65 ! Magic #: consume exactly the number of inputs
69 ! Quotation which coerces return value to required type
72 : infer-alien-indirect ( -- )
73 alien-indirect-params new
74 ! Compile-time parameters
78 ! Quotation which coerces parameters to required types
82 ! Magic #: consume the function pointer, too
86 ! Quotation which coerces return value to required type
89 : infer-alien-assembly ( -- )
90 alien-assembly-params new
91 ! Compile-time parameters
96 ! Quotation which coerces parameters to required types
98 ! Magic #: consume exactly the number of inputs
102 ! Quotation which coerces return value to required type
105 : callback-xt ( word return-rewind -- alien )
106 [ callbacks get ] dip '[ _ <callback> ] cache ;
108 : callback-bottom ( params -- )
109 [ xt>> ] [ callback-return-rewind ] bi
110 '[ _ _ callback-xt ] infer-quot-here ;
112 : infer-alien-callback ( -- )
113 alien-callback-params new
118 "( callback )" <uninterned-word> >>xt