factor/basis/compiler/cfg/builder/alien/alien.factor

203 lines
5.7 KiB
Factor
Raw Normal View History

2010-05-16 04:09:47 -04:00
! Copyright (C) 2008, 2010 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2014-12-13 19:10:21 -05:00
USING: accessors alien alien.c-types alien.libraries
alien.strings arrays assocs classes.struct combinators
compiler.cfg compiler.cfg.builder
compiler.cfg.builder.alien.boxing
compiler.cfg.builder.alien.params compiler.cfg.hats
compiler.cfg.instructions compiler.cfg.registers
compiler.cfg.stacks compiler.cfg.stacks.local compiler.errors
compiler.tree cpu.architecture fry kernel layouts make math
math.parser namespaces sequences sequences.generalizations
strings words ;
2010-05-16 04:09:47 -04:00
IN: compiler.cfg.builder.alien
: with-param-regs* ( quot -- reg-values stack-values )
'[
V{ } clone reg-values set
V{ } clone stack-values set
@
reg-values get
stack-values get
stack-params get
struct-return-area get
] with-param-regs
struct-return-area set
stack-params set ; inline
2010-05-16 04:09:47 -04:00
: unbox-parameters ( parameters -- vregs reps )
[
[ length iota <reversed> ] keep
2010-07-02 15:44:12 -04:00
[ [ <ds-loc> peek-loc ] [ base-type ] bi* unbox-parameter ]
2010-05-16 04:09:47 -04:00
2 2 mnmap [ concat ] bi@
]
[ length neg <ds-loc> inc-stack ] bi ;
2010-05-16 04:09:47 -04:00
: prepare-struct-caller ( vregs reps return -- vregs' reps' return-vreg/f )
dup large-struct? [
heap-size cell f ^^local-allot [
'[ _ prefix ]
2011-05-20 18:11:50 -04:00
[ int-rep struct-return-on-stack? f 3array prefix ] bi*
] keep
] [ drop f ] if ;
2010-05-16 04:09:47 -04:00
: (caller-parameters) ( vregs reps -- )
2011-05-20 18:11:50 -04:00
[ first3 next-parameter ] 2each ;
2010-05-16 04:09:47 -04:00
: caller-parameters ( params -- reg-inputs stack-inputs )
2010-05-16 04:09:47 -04:00
[ abi>> ] [ parameters>> ] [ return>> ] tri
'[
2010-05-16 04:09:47 -04:00
_ unbox-parameters
_ prepare-struct-caller struct-return-area set
2010-05-16 04:09:47 -04:00
(caller-parameters)
] with-param-regs* ;
: prepare-caller-return ( params -- reg-outputs dead-outputs )
return>> [ { } ] [ base-type load-return ] if-void { } ;
2010-05-16 04:09:47 -04:00
: caller-stack-frame ( params -- cleanup stack-size )
[ stack-params get ] dip [ return>> ] [ abi>> ] bi stack-cleanup
stack-params get ;
2010-05-16 04:09:47 -04:00
GENERIC# dlsym-valid? 1 ( symbols dll -- ? )
M: string dlsym-valid? dlsym ;
M: array dlsym-valid? '[ _ dlsym ] any? ;
: check-dlsym ( symbols library -- )
{
{ [ dup library-dll dll-valid? not ] [
[ library-dll dll-path ] [ dlerror>> ] bi
cfg get word>> no-such-library-error drop
] }
{ [ 2dup library-dll dlsym-valid? not ] [
drop dlerror cfg get word>> no-such-symbol-error
] }
[ 2drop ]
} cond ;
2010-05-16 04:09:47 -04:00
: decorated-symbol ( params -- symbols )
[ function>> ] [ parameters>> [ stack-size ] map-sum number>string ] bi
{
[ drop ]
[ "@" glue ]
[ "@" glue "_" prepend ]
[ "@" glue "@" prepend ]
} 2cleave
4array ;
: caller-linkage ( params -- symbols dll )
2010-05-16 04:09:47 -04:00
[ dup abi>> callee-cleanup? [ decorated-symbol ] [ function>> ] if ]
[ library>> lookup-library ]
bi 2dup check-dlsym library-dll ;
2010-05-16 04:09:47 -04:00
: caller-return ( params -- )
return>> [ ] [
[
building get last reg-outputs>>
flip [ { } { } ] [ first2 ] if-empty
] dip
base-type box-return ds-push
] if-void ;
2010-05-16 04:09:47 -04:00
M: #alien-invoke emit-node
2010-07-02 15:44:12 -04:00
params>>
[
{
[ caller-parameters ]
[ prepare-caller-return ]
[ caller-stack-frame ]
[ caller-linkage ]
} cleave
<gc-map> ##alien-invoke,
]
[ caller-return ]
bi ;
2010-05-16 04:09:47 -04:00
2010-07-02 15:44:12 -04:00
M: #alien-indirect emit-node ( node -- )
params>>
2010-05-16 04:09:47 -04:00
[
[ ds-pop ^^unbox-any-c-ptr ] dip
[ caller-parameters ]
[ prepare-caller-return ]
[ caller-stack-frame ] tri
<gc-map> ##alien-indirect,
2010-07-02 15:44:12 -04:00
]
[ caller-return ]
bi ;
2010-07-02 15:44:12 -04:00
M: #alien-assembly emit-node ( node -- )
params>>
[
{
[ caller-parameters ]
[ prepare-caller-return ]
[ caller-stack-frame ]
[ quot>> ]
} cleave ##alien-assembly,
]
[ caller-return ]
bi ;
2011-05-20 18:11:50 -04:00
: callee-parameter ( rep on-stack? odd-register? -- dst )
[ next-vreg dup ] 3dip next-parameter ;
2010-05-16 04:09:47 -04:00
: prepare-struct-callee ( c-type -- vreg )
large-struct?
2011-05-20 18:11:50 -04:00
[ int-rep struct-return-on-stack? f callee-parameter ] [ f ] if ;
2010-05-16 04:09:47 -04:00
: (callee-parameters) ( params -- vregs reps )
[ flatten-parameter-type ] map
2011-05-20 18:11:50 -04:00
[ [ [ first3 callee-parameter ] map ] map ]
2010-05-16 04:09:47 -04:00
[ [ keys ] map ]
bi ;
: box-parameters ( vregs reps params -- )
parameters>> [ base-type box-parameter ds-push ] 3each ;
2010-05-16 04:09:47 -04:00
: callee-parameters ( params -- vregs reps reg-outputs stack-outputs )
2010-05-16 04:09:47 -04:00
[ abi>> ] [ return>> ] [ parameters>> ] tri
'[
2010-05-16 04:09:47 -04:00
_ prepare-struct-callee struct-return-area set
_ [ base-type ] map (callee-parameters)
] with-param-regs* ;
: callee-return ( params -- reg-inputs )
return>> [ { } ] [
[ ds-pop ] dip
base-type unbox-return store-return
] if-void ;
: callback-stack-cleanup ( params -- )
[ xt>> ]
[ [ stack-params get ] dip [ return>> ] [ abi>> ] bi stack-cleanup ] bi
2010-05-16 04:09:47 -04:00
"stack-cleanup" set-word-prop ;
: needs-frame-pointer ( -- )
cfg get t >>frame-pointer? drop ;
: emit-callback-body ( nodes -- )
[ last #return? t assert= ] [ but-last emit-nodes ] bi ;
: emit-callback-return ( params -- )
basic-block get [ callee-return ##callback-outputs, ] [ drop ] if ;
2010-05-16 04:09:47 -04:00
M: #alien-callback emit-node
dup params>> xt>> dup
2010-05-16 04:09:47 -04:00
[
needs-frame-pointer
2010-07-02 15:44:12 -04:00
begin-word
{
[ params>> callee-parameters ##callback-inputs, ]
[ params>> box-parameters ]
[ child>> emit-callback-body ]
[ params>> emit-callback-return ]
[ params>> callback-stack-cleanup ]
2010-07-02 15:44:12 -04:00
} cleave
basic-block get [ end-word ] when
2010-05-16 04:09:47 -04:00
] with-cfg-builder ;