1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.libraries alien.strings arrays
4 assocs classes.struct combinators compiler.cfg compiler.cfg.builder
5 compiler.cfg.builder.alien.boxing compiler.cfg.builder.alien.params
6 compiler.cfg.hats compiler.cfg.instructions compiler.cfg.registers
7 compiler.cfg.stacks compiler.cfg.stacks.local compiler.errors
8 compiler.tree cpu.architecture fry kernel layouts make math namespaces
9 sequences sequences.generalizations words ;
10 IN: compiler.cfg.builder.alien
12 : with-param-regs ( abi quot -- reg-values stack-values )
16 V{ } clone reg-values set
17 V{ } clone stack-values set
24 struct-return-area get
26 struct-return-area set
27 stack-params set ; inline
29 : unbox-parameters ( parameters -- vregs reps )
31 [ length <iota> <reversed> ] keep
32 [ [ <ds-loc> peek-loc ] [ base-type ] bi* unbox-parameter ]
33 2 2 mnmap [ concat ] bi@
35 [ length neg <ds-loc> inc-stack ] bi ;
37 : prepare-struct-caller ( vregs reps return -- vregs' reps' return-vreg/f )
39 heap-size cell f ^^local-allot [
41 [ int-rep struct-return-on-stack? f 3array prefix ] bi*
45 : (caller-parameters) ( vregs reps -- )
46 [ first3 next-parameter ] 2each ;
48 : caller-parameters ( params -- reg-inputs stack-inputs )
49 [ abi>> ] [ parameters>> ] [ return>> ] tri
52 _ prepare-struct-caller struct-return-area set
56 : prepare-caller-return ( params -- reg-outputs )
57 return>> [ { } ] [ base-type load-return ] if-void ;
59 : caller-stack-cleanup ( params stack-size -- cleanup )
60 swap [ return>> ] [ abi>> ] bi stack-cleanup ;
62 : check-dlsym ( symbol library/f -- )
64 { [ dup library-dll dll-valid? not ] [
65 [ library-dll dll-path ] [ dlerror>> ] bi
66 cfg get word>> no-such-library-error drop
68 { [ 2dup library-dll dlsym not ] [
69 drop dlerror cfg get word>> no-such-symbol-error
74 : caller-linkage ( params -- symbol dll/f )
75 [ function>> ] [ library>> lookup-library ] bi
76 2dup check-dlsym library-dll ;
78 : caller-return ( params -- )
81 building get last reg-outputs>>
82 flip [ { } { } ] [ first2 ] if-empty
84 base-type box-return ds-push
87 : params>alien-insn-params ( params --
88 varargs? reg-inputs stack-inputs
89 reg-outputs dead-outputs
94 [ prepare-caller-return { } ]
95 [ stack-params get [ caller-stack-cleanup ] keep ]
98 M: #alien-invoke emit-node
101 [ params>alien-insn-params ]
102 [ caller-linkage ] bi
103 <gc-map> ##alien-invoke,
105 [ caller-return ] bi ;
107 M: #alien-indirect emit-node
110 [ ds-pop ^^unbox-any-c-ptr ] dip
111 params>alien-insn-params
112 <gc-map> ##alien-indirect,
114 [ caller-return ] bi ;
116 M: #alien-assembly emit-node
119 [ params>alien-insn-params ]
123 [ caller-return ] bi ;
125 : callee-parameter ( rep on-stack? odd-register? -- dst )
126 [ next-vreg dup ] 3dip next-parameter ;
128 : prepare-struct-callee ( c-type -- vreg )
130 [ int-rep struct-return-on-stack? f callee-parameter ] [ f ] if ;
132 : (callee-parameters) ( params -- vregs reps )
133 [ flatten-parameter-type ] map
134 [ [ [ first3 callee-parameter ] map ] map ]
135 [ [ keys ] map ] bi ;
137 : box-parameters ( vregs reps params -- )
138 parameters>> [ base-type box-parameter ds-push ] 3each ;
140 : callee-parameters ( params -- vregs reps reg-outputs stack-outputs )
141 [ abi>> ] [ return>> ] [ parameters>> ] tri
143 _ prepare-struct-callee struct-return-area set
144 _ [ base-type ] map (callee-parameters)
147 : callee-return ( params -- reg-inputs )
150 base-type unbox-return store-return
153 : emit-callback-body ( block nodes -- block' )
154 dup last #return? t assert= but-last emit-nodes ;
156 : emit-callback-inputs ( params -- )
157 [ callee-parameters ##callback-inputs, ] keep box-parameters ;
159 : callback-stack-cleanup ( params -- )
161 [ [ stack-params get ] dip [ return>> ] [ abi>> ] bi stack-cleanup ] bi
162 "stack-cleanup" set-word-prop ;
164 : emit-callback-return ( block params -- )
165 swap [ callee-return ##callback-outputs, ] [ drop ] if ;
167 : emit-callback-outputs ( block params -- )
168 [ emit-callback-return ] keep callback-stack-cleanup ;
170 M: #alien-callback emit-node
171 dup params>> xt>> dup
173 t cfg get frame-pointer?<<
175 over params>> emit-callback-inputs
176 over child>> emit-callback-body
177 [ swap params>> emit-callback-outputs ] keep
178 [ end-word drop ] when*