1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.libraries
4 alien.private arrays assocs combinators effects fry kernel math
5 namespaces quotations sequences stack-checker.backend
6 stack-checker.dependencies stack-checker.state
7 stack-checker.visitor strings words ;
8 FROM: kernel.private => declare ;
9 IN: stack-checker.alien
11 TUPLE: alien-node-params
13 { abi abi initial: cdecl } varargs? ;
15 TUPLE: alien-invoke-params < alien-node-params
19 TUPLE: alien-indirect-params < alien-node-params ;
21 TUPLE: alien-assembly-params < alien-node-params
24 TUPLE: alien-callback-params < alien-node-params
27 : param-prep-quot ( params -- quot )
28 parameters>> [ lookup-c-type c-type-unboxer-quot ] map deep-spread>quot ;
30 : stack-shape ( params -- in out )
32 [ parameters>> length ] [ alien-indirect-params? 1 0 ? ] bi +
33 ] [ return>> void? 0 1 ? ] bi ;
35 : inputs/outputs ( params -- in-d out-d )
36 stack-shape [ consume-d ] [ produce-d ] bi* ;
38 : return-prep-quot ( params -- quot )
39 return>> [ [ ] ] [ lookup-c-type c-type-boxer-quot ] if-void ;
41 : infer-return ( params -- )
42 return-prep-quot infer-quot-here ;
44 : pop-abi ( params -- params )
47 : pop-function ( params -- params )
48 pop-literal >>function ;
50 : pop-library ( params -- params )
51 pop-literal >>library ;
53 : pop-params ( params -- params )
54 pop-literal [ [ add-depends-on-c-type ] each ] [ >>parameters ] bi ;
56 : pop-quot ( params -- params )
59 : pop-return ( params -- params )
60 pop-literal [ add-depends-on-c-type ] [ >>return ] bi ;
62 : pop-varargs? ( params -- params )
63 pop-literal >>varargs? ;
65 : infer-alien-invoke ( -- )
66 alien-invoke-params new
67 ! Compile-time parameters
74 dup library>> library-abi >>abi
75 ! Quotation which coerces parameters to required types
76 dup param-prep-quot infer-quot-here
77 ! Consume inputs and outputs and add node to IR
78 dup dup inputs/outputs #alien-invoke,
79 ! Quotation which coerces return value to required type
82 : infer-alien-indirect ( -- )
83 alien-indirect-params new
84 ! Compile-time parameters
88 ! Coerce parameters to required types
89 dup param-prep-quot '[ _ [ >c-ptr ] bi* ] infer-quot-here
90 ! Consume inputs and outputs and add node to IR
91 dup dup inputs/outputs #alien-indirect,
92 ! Quotation which coerces return value to required type
95 : infer-alien-assembly ( -- )
96 alien-assembly-params new
97 ! Compile-time parameters
102 ! Quotation which coerces parameters to required types
103 dup param-prep-quot infer-quot-here
104 ! Consume inputs and outputs and add node to IR
105 dup dup inputs/outputs #alien-assembly,
106 ! Quotation which coerces return value to required type
109 : callback-xt ( word -- alien )
110 callbacks get [ dup "stack-cleanup" word-prop <callback> ] cache ;
112 : callback-bottom ( params -- )
113 "( callback )" <uninterned-word> >>xt
114 xt>> '[ _ callback-xt { alien } declare ] infer-quot-here ;
116 : callback-return-quot ( ctype -- quot )
117 return>> [ [ ] ] [ lookup-c-type c-type-unboxer-quot ] if-void ;
119 : callback-parameter-quot ( params -- quot )
120 parameters>> [ lookup-c-type ] map
121 [ [ c-type-class ] map '[ _ declare ] ]
122 [ [ c-type-boxer-quot ] map deep-spread>quot ]
125 GENERIC: wrap-callback-quot ( params quot -- quot' )
127 SYMBOL: wait-for-callback-hook
129 wait-for-callback-hook [ [ drop ] ] initialize
131 M: callable wrap-callback-quot
132 swap [ callback-parameter-quot ] [ callback-return-quot ] bi surround
133 wait-for-callback-hook get
134 '[ _ _ do-callback ] >quotation ;
136 : callback-effect ( params -- effect )
137 stack-shape [ "x" <array> ] bi@ <effect> ;
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