2010-01-02 07:03:30 -05:00
|
|
|
! Copyright (C) 2008, 2010 Slava Pestov.
|
2008-08-12 03:41:18 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: kernel sequences accessors combinators math namespaces
|
2010-03-18 05:06:00 -04:00
|
|
|
init sets words assocs alien.libraries alien alien.private
|
|
|
|
alien.c-types cpu.architecture fry stack-checker.backend
|
|
|
|
stack-checker.errors stack-checker.visitor
|
|
|
|
stack-checker.dependencies ;
|
2008-08-12 03:41:18 -04:00
|
|
|
IN: stack-checker.alien
|
|
|
|
|
|
|
|
TUPLE: alien-node-params return parameters abi in-d out-d ;
|
|
|
|
|
|
|
|
TUPLE: alien-invoke-params < alien-node-params library function ;
|
|
|
|
|
|
|
|
TUPLE: alien-indirect-params < alien-node-params ;
|
|
|
|
|
2010-01-06 23:39:22 -05:00
|
|
|
TUPLE: alien-assembly-params < alien-node-params quot ;
|
|
|
|
|
2008-08-12 03:41:18 -04:00
|
|
|
TUPLE: alien-callback-params < alien-node-params quot xt ;
|
|
|
|
|
2010-02-22 03:32:41 -05:00
|
|
|
: param-prep-quot ( params -- quot )
|
2008-08-12 03:41:18 -04:00
|
|
|
parameters>> [ c-type c-type-unboxer-quot ] map spread>quot ;
|
|
|
|
|
2010-02-22 03:32:41 -05:00
|
|
|
: infer-params ( params -- )
|
|
|
|
param-prep-quot infer-quot-here ;
|
|
|
|
|
2008-08-12 03:41:18 -04:00
|
|
|
: alien-stack ( params extra -- )
|
|
|
|
over parameters>> length + consume-d >>in-d
|
2009-09-15 22:43:18 -04:00
|
|
|
dup return>> void? 0 1 ? produce-d >>out-d
|
2008-08-12 03:41:18 -04:00
|
|
|
drop ;
|
|
|
|
|
2010-02-22 03:32:41 -05:00
|
|
|
: return-prep-quot ( params -- quot )
|
2008-08-12 03:41:18 -04:00
|
|
|
return>> [ [ ] ] [ c-type c-type-boxer-quot ] if-void ;
|
|
|
|
|
2010-02-22 03:32:41 -05:00
|
|
|
: infer-return ( params -- )
|
|
|
|
return-prep-quot infer-quot-here ;
|
|
|
|
|
|
|
|
: pop-return ( params -- params )
|
|
|
|
pop-literal [ depends-on-c-type ] [ nip >>return ] bi ;
|
|
|
|
|
|
|
|
: pop-library ( params -- params )
|
|
|
|
pop-literal nip >>library ;
|
|
|
|
|
|
|
|
: pop-function ( params -- params )
|
|
|
|
pop-literal nip >>function ;
|
|
|
|
|
|
|
|
: pop-params ( params -- params )
|
|
|
|
pop-literal [ [ depends-on-c-type ] each ] [ nip >>parameters ] bi ;
|
|
|
|
|
|
|
|
: pop-abi ( params -- params )
|
|
|
|
pop-literal nip >>abi ;
|
|
|
|
|
|
|
|
: pop-quot ( params -- params )
|
|
|
|
pop-literal nip >>quot ;
|
|
|
|
|
2008-08-12 03:41:18 -04:00
|
|
|
: infer-alien-invoke ( -- )
|
|
|
|
alien-invoke-params new
|
|
|
|
! Compile-time parameters
|
2010-02-22 03:32:41 -05:00
|
|
|
pop-params
|
|
|
|
pop-function
|
|
|
|
pop-library
|
|
|
|
pop-return
|
2008-08-12 03:41:18 -04:00
|
|
|
! Set ABI
|
2010-02-22 07:28:56 -05:00
|
|
|
dup library>> library-abi >>abi
|
2010-02-22 03:32:41 -05:00
|
|
|
! Quotation which coerces parameters to required types
|
|
|
|
dup infer-params
|
2008-08-12 03:41:18 -04:00
|
|
|
! Magic #: consume exactly the number of inputs
|
|
|
|
dup 0 alien-stack
|
|
|
|
! Add node to IR
|
|
|
|
dup #alien-invoke,
|
|
|
|
! Quotation which coerces return value to required type
|
2010-02-22 03:32:41 -05:00
|
|
|
infer-return ;
|
2008-08-12 03:41:18 -04:00
|
|
|
|
|
|
|
: infer-alien-indirect ( -- )
|
|
|
|
alien-indirect-params new
|
|
|
|
! Compile-time parameters
|
2010-02-22 03:32:41 -05:00
|
|
|
pop-abi
|
|
|
|
pop-params
|
|
|
|
pop-return
|
2008-08-12 03:41:18 -04:00
|
|
|
! Quotation which coerces parameters to required types
|
2010-02-22 03:32:41 -05:00
|
|
|
1 infer->r
|
|
|
|
dup infer-params
|
|
|
|
1 infer-r>
|
2008-08-12 03:41:18 -04:00
|
|
|
! Magic #: consume the function pointer, too
|
|
|
|
dup 1 alien-stack
|
|
|
|
! Add node to IR
|
|
|
|
dup #alien-indirect,
|
|
|
|
! Quotation which coerces return value to required type
|
2010-02-22 03:32:41 -05:00
|
|
|
infer-return ;
|
2008-08-12 03:41:18 -04:00
|
|
|
|
2010-01-06 23:39:22 -05:00
|
|
|
: infer-alien-assembly ( -- )
|
|
|
|
alien-assembly-params new
|
|
|
|
! Compile-time parameters
|
2010-02-22 03:32:41 -05:00
|
|
|
pop-quot
|
|
|
|
pop-abi
|
|
|
|
pop-params
|
|
|
|
pop-return
|
2010-01-06 23:39:22 -05:00
|
|
|
! Quotation which coerces parameters to required types
|
2010-02-22 03:32:41 -05:00
|
|
|
dup infer-params
|
2010-01-06 23:39:22 -05:00
|
|
|
! Magic #: consume exactly the number of inputs
|
|
|
|
dup 0 alien-stack
|
|
|
|
! Add node to IR
|
|
|
|
dup #alien-assembly,
|
|
|
|
! Quotation which coerces return value to required type
|
2010-02-22 03:32:41 -05:00
|
|
|
infer-return ;
|
2010-01-06 23:39:22 -05:00
|
|
|
|
2010-01-02 07:03:30 -05:00
|
|
|
: callback-xt ( word return-rewind -- alien )
|
|
|
|
[ callbacks get ] dip '[ _ <callback> ] cache ;
|
2008-08-12 03:41:18 -04:00
|
|
|
|
|
|
|
: callback-bottom ( params -- )
|
2010-04-10 19:54:17 -04:00
|
|
|
[ xt>> ] [ stack-cleanup ] bi '[ _ _ callback-xt ] infer-quot-here ;
|
2008-08-12 03:41:18 -04:00
|
|
|
|
|
|
|
: infer-alien-callback ( -- )
|
|
|
|
alien-callback-params new
|
2010-02-22 03:32:41 -05:00
|
|
|
pop-quot
|
|
|
|
pop-abi
|
|
|
|
pop-params
|
|
|
|
pop-return
|
2009-11-13 08:55:47 -05:00
|
|
|
"( callback )" <uninterned-word> >>xt
|
2008-08-12 03:41:18 -04:00
|
|
|
dup callback-bottom
|
|
|
|
#alien-callback, ;
|