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 ;
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 ( node -- quot )
20 parameters>> [ c-type c-type-unboxer-quot ] map spread>quot ;
22 : alien-stack ( params extra -- )
23 over parameters>> length + consume-d >>in-d
24 dup return>> void? 0 1 ? produce-d >>out-d
27 : return-prep-quot ( node -- quot )
28 return>> [ [ ] ] [ c-type c-type-boxer-quot ] if-void ;
30 : infer-alien-invoke ( -- )
31 alien-invoke-params new
32 ! Compile-time parameters
33 pop-literal nip >>parameters
34 pop-literal nip >>function
35 pop-literal nip >>library
36 pop-literal nip >>return
37 ! Quotation which coerces parameters to required types
38 dup param-prep-quot infer-quot-here
40 dup library>> library [ abi>> ] [ "cdecl" ] if* >>abi
41 ! Magic #: consume exactly the number of inputs
45 ! Quotation which coerces return value to required type
46 return-prep-quot infer-quot-here ;
48 : infer-alien-indirect ( -- )
49 alien-indirect-params new
50 ! Compile-time parameters
52 pop-literal nip >>parameters
53 pop-literal nip >>return
54 ! Quotation which coerces parameters to required types
55 dup param-prep-quot '[ _ dip ] infer-quot-here
56 ! Magic #: consume the function pointer, too
60 ! Quotation which coerces return value to required type
61 return-prep-quot infer-quot-here ;
63 : infer-alien-assembly ( -- )
64 alien-assembly-params new
65 ! Compile-time parameters
66 pop-literal nip >>quot
68 pop-literal nip >>parameters
69 pop-literal nip >>return
70 ! Quotation which coerces parameters to required types
71 dup param-prep-quot infer-quot-here
72 ! Magic #: consume exactly the number of inputs
76 ! Quotation which coerces return value to required type
77 return-prep-quot infer-quot-here ;
79 : callback-xt ( word return-rewind -- alien )
80 [ callbacks get ] dip '[ _ <callback> ] cache ;
82 : callback-bottom ( params -- )
83 [ xt>> ] [ callback-return-rewind ] bi
84 '[ _ _ callback-xt ] infer-quot-here ;
86 : infer-alien-callback ( -- )
87 alien-callback-params new
88 pop-literal nip >>quot
90 pop-literal nip >>parameters
91 pop-literal nip >>return
92 "( callback )" <uninterned-word> >>xt