1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: locals alien.c-types alien.syntax arrays kernel fry math
4 namespaces sequences system layouts io vocabs.loader accessors init
5 combinators command-line make compiler compiler.units
6 compiler.constants compiler.alien compiler.codegen
7 compiler.codegen.fixup compiler.cfg.instructions compiler.cfg.builder
8 compiler.cfg.intrinsics compiler.cfg.stack-frame cpu.x86.assembler
9 cpu.x86.assembler.operands cpu.x86 cpu.architecture ;
12 ! We implement the FFI for Linux, OS X and Windows all at once.
13 ! OS X requires that the stack be 16-byte aligned.
15 M: x86.32 machine-registers
17 { int-regs { EAX ECX EDX EBP EBX } }
18 { float-regs { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } }
21 M: x86.32 ds-reg ESI ;
22 M: x86.32 rs-reg EDI ;
23 M: x86.32 stack-reg ESP ;
24 M: x86.32 temp-reg ECX ;
26 M:: x86.32 %dispatch ( src temp -- )
27 ! Load jump table base.
28 temp src HEX: ffffffff [+] LEA
29 building get length cell - :> start
30 0 rc-absolute-cell rel-here
33 building get length :> end
34 ! Fix up the displacement above
36 [ end start - + building get dup pop* push ]
40 ! Registers for fastcall
41 M: x86.32 param-reg-1 EAX ;
42 M: x86.32 param-reg-2 EDX ;
43 M: x86.32 param-reg-3 ECX ;
45 M: x86.32 pic-tail-reg EBX ;
47 M: x86.32 reserved-area-size 0 ;
49 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
52 temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument
55 M: x86.32 %vm-invoke-1st-arg ( function -- )
60 M: x86.32 %vm-invoke-3rd-arg ( function -- )
61 %vm-invoke-1st-arg ; ! first 2 args are regs, 3rd is stack so vm-invoke-1st-arg works here
63 M: x86.32 return-struct-in-registers? ( c-type -- ? )
65 [ return-in-registers?>> ]
66 [ heap-size { 1 2 4 8 } member? ] bi
67 os { linux netbsd solaris } member? not
70 : struct-return@ ( n -- operand )
71 [ next-stack@ ] [ stack-frame get params>> stack@ ] if* ;
73 ! On x86, parameters are never passed in registers.
74 M: int-regs return-reg drop EAX ;
75 M: int-regs param-regs drop { } ;
76 M: float-regs param-regs drop { } ;
78 GENERIC: push-return-reg ( rep -- )
79 GENERIC: load-return-reg ( n rep -- )
80 GENERIC: store-return-reg ( n rep -- )
82 M: int-rep push-return-reg drop EAX PUSH ;
83 M: int-rep load-return-reg drop EAX swap next-stack@ MOV ;
84 M: int-rep store-return-reg drop stack@ EAX MOV ;
86 M: float-rep push-return-reg drop ESP 4 SUB ESP [] FSTPS ;
87 M: float-rep load-return-reg drop next-stack@ FLDS ;
88 M: float-rep store-return-reg drop stack@ FSTPS ;
90 M: double-rep push-return-reg drop ESP 8 SUB ESP [] FSTPL ;
91 M: double-rep load-return-reg drop next-stack@ FLDL ;
92 M: double-rep store-return-reg drop stack@ FSTPL ;
95 [ align-stack ] keep - decr-stack-reg ;
98 align-stack incr-stack-reg ;
100 : with-aligned-stack ( n quot -- )
101 '[ align-sub @ ] [ align-add ] bi ; inline
103 M: x86.32 %prologue ( n -- )
105 0 PUSH rc-absolute-cell rel-this
106 3 cells - decr-stack-reg ;
108 M: x86.32 %load-param-reg 3drop ;
110 M: x86.32 %save-param-reg 3drop ;
112 : (%box) ( n rep -- )
113 #! If n is f, push the return register onto the stack; we
114 #! are boxing a return value of a C function. If n is an
115 #! integer, push [ESP+n] on the stack; we are boxing a
116 #! parameter being passed to a callback from C.
117 over [ load-return-reg ] [ 2drop ] if ;
119 CONSTANT: vm-ptr-size 4
121 M:: x86.32 %box ( n rep func -- )
123 rep rep-size vm-ptr-size + [
127 ] with-aligned-stack ;
129 : (%box-long-long) ( n -- )
131 EDX over next-stack@ MOV
132 EAX swap cell - next-stack@ MOV
135 M: x86.32 %box-long-long ( n func -- )
136 [ (%box-long-long) ] dip
142 ] with-aligned-stack ;
144 M:: x86.32 %box-large-struct ( n c-type -- )
145 ! Compute destination address
146 EDX n struct-return@ LEA
150 c-type heap-size PUSH
151 ! Push destination address
153 ! Copy the struct from the C stack
154 "box_value_struct" f %alien-invoke
155 ] with-aligned-stack ;
157 M: x86.32 %prepare-box-struct ( -- )
158 ! Compute target address for value struct return
159 EAX f struct-return@ LEA
160 ! Store it as the first parameter
163 M: x86.32 %box-small-struct ( c-type -- )
164 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
170 "box_small_struct" f %alien-invoke
171 ] with-aligned-stack ;
173 M: x86.32 %prepare-unbox ( -- )
174 #! Move top of data stack to EAX.
178 : call-unbox-func ( func -- )
180 ! push the vm ptr as an argument
186 ] with-aligned-stack ;
188 M: x86.32 %unbox ( n rep func -- )
189 #! The value being unboxed must already be in EAX.
190 #! If n is f, we're unboxing a return value about to be
191 #! returned by the callback. Otherwise, we're unboxing
192 #! a parameter to a C function about to be called.
194 ! Store the return value on the C stack
195 over [ store-return-reg ] [ 2drop ] if ;
197 M: x86.32 %unbox-long-long ( n func -- )
199 ! Store the return value on the C stack
202 cell + stack@ EDX MOV
205 : %unbox-struct-1 ( -- )
206 #! Alien must be in EAX.
210 "alien_offset" f %alien-invoke
213 ] with-aligned-stack ;
215 : %unbox-struct-2 ( -- )
216 #! Alien must be in EAX.
220 "alien_offset" f %alien-invoke
225 ] with-aligned-stack ;
227 M: x86 %unbox-small-struct ( size -- )
228 #! Alien must be in EAX.
229 heap-size cell align cell /i {
230 { 1 [ %unbox-struct-1 ] }
231 { 2 [ %unbox-struct-2 ] }
234 M:: x86.32 %unbox-large-struct ( n c-type -- )
235 ! Alien must be in EAX.
236 ! Compute destination address
241 c-type heap-size PUSH
242 ! Push destination address
244 ! Push source address
246 ! Copy the struct to the stack
247 "to_value_struct" f %alien-invoke
248 ] with-aligned-stack ;
250 M: x86.32 %prepare-alien-indirect ( -- )
251 push-vm-ptr "unbox_alien" f %alien-invoke
255 M: x86.32 %alien-indirect ( -- )
258 M: x86.32 %alien-callback ( quot -- )
260 EAX swap %load-reference
262 param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup
263 "c_to_factor" f %alien-invoke
264 ] with-aligned-stack ;
266 M: x86.32 %callback-value ( ctype -- )
269 ! Save top of data stack in non-volatile register
273 ! Restore data/call/retain stacks
274 "unnest_stacks" f %alien-invoke
275 ! Place top of data stack in EAX
283 M: x86.32 %cleanup ( params -- )
284 #! a) If we just called an stdcall function in Windows, it
285 #! cleaned up the stack frame for us. But we don't want that
286 #! so we 'undo' the cleanup since we do that in %epilogue.
287 #! b) If we just called a function returning a struct, we
291 [ dup abi>> "stdcall" = ]
292 [ drop ESP stack-frame get params>> SUB ]
294 [ dup return>> large-struct? ]
300 M: x86.32 %callback-return ( n -- )
301 #! a) If the callback is stdcall, we have to clean up the
302 #! caller's stack frame.
303 #! b) If the callback is returning a large struct, we have
306 { [ dup abi>> "stdcall" = ] [
308 [ params>> ] [ return>> ] bi +
310 { [ dup return>> large-struct? ] [ drop 4 ] }
314 M: x86.32 dummy-stack-params? f ;
316 M: x86.32 dummy-int-params? f ;
318 M: x86.32 dummy-fp-params? f ;
321 cell "longlong" c-type (>>align)
322 cell "ulonglong" c-type (>>align)
323 4 "double" c-type (>>align)