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 : param-reg-1 ( -- reg ) EAX ;
42 : param-reg-2 ( -- reg ) EDX ;
44 M: x86.32 pic-tail-reg EBX ;
46 M: x86.32 reserved-area-size 0 ;
48 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
51 0 PUSH rc-absolute-cell rt-vm rel-fixup ; ! push the vm ptr as an argument
53 M: x86.32 return-struct-in-registers? ( c-type -- ? )
55 [ return-in-registers?>> ]
56 [ heap-size { 1 2 4 8 } member? ] bi
57 os { linux netbsd solaris } member? not
60 : struct-return@ ( n -- operand )
61 [ next-stack@ ] [ stack-frame get params>> stack@ ] if* ;
63 ! On x86, parameters are never passed in registers.
64 M: int-regs return-reg drop EAX ;
65 M: int-regs param-regs drop { } ;
66 M: float-regs param-regs drop { } ;
68 GENERIC: push-return-reg ( rep -- )
69 GENERIC: load-return-reg ( n rep -- )
70 GENERIC: store-return-reg ( n rep -- )
72 M: int-rep push-return-reg drop EAX PUSH ;
73 M: int-rep load-return-reg drop EAX swap next-stack@ MOV ;
74 M: int-rep store-return-reg drop stack@ EAX MOV ;
76 M: float-rep push-return-reg drop ESP 4 SUB ESP [] FSTPS ;
77 M: float-rep load-return-reg drop next-stack@ FLDS ;
78 M: float-rep store-return-reg drop stack@ FSTPS ;
80 M: double-rep push-return-reg drop ESP 8 SUB ESP [] FSTPL ;
81 M: double-rep load-return-reg drop next-stack@ FLDL ;
82 M: double-rep store-return-reg drop stack@ FSTPL ;
85 [ align-stack ] keep - decr-stack-reg ;
88 align-stack incr-stack-reg ;
90 : with-aligned-stack ( n quot -- )
91 '[ align-sub @ ] [ align-add ] bi ; inline
93 M: x86.32 %prologue ( n -- )
95 0 PUSH rc-absolute-cell rel-this
96 3 cells - decr-stack-reg ;
98 M: x86.32 %load-param-reg 3drop ;
100 M: x86.32 %save-param-reg 3drop ;
102 : (%box) ( n rep -- )
103 #! If n is f, push the return register onto the stack; we
104 #! are boxing a return value of a C function. If n is an
105 #! integer, push [ESP+n] on the stack; we are boxing a
106 #! parameter being passed to a callback from C.
107 over [ load-return-reg ] [ 2drop ] if ;
109 CONSTANT: vm-ptr-size 4
111 M:: x86.32 %box ( n rep func -- )
113 rep rep-size vm-ptr-size + [
117 ] with-aligned-stack ;
119 : (%box-long-long) ( n -- )
121 EDX over next-stack@ MOV
122 EAX swap cell - next-stack@ MOV
125 M: x86.32 %box-long-long ( n func -- )
126 [ (%box-long-long) ] dip
132 ] with-aligned-stack ;
134 M:: x86.32 %box-large-struct ( n c-type -- )
135 ! Compute destination address
136 EDX n struct-return@ LEA
140 c-type heap-size PUSH
141 ! Push destination address
143 ! Copy the struct from the C stack
144 "box_value_struct" f %alien-invoke
145 ] with-aligned-stack ;
147 M: x86.32 %prepare-box-struct ( -- )
148 ! Compute target address for value struct return
149 EAX f struct-return@ LEA
150 ! Store it as the first parameter
153 M: x86.32 %box-small-struct ( c-type -- )
154 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
160 "box_small_struct" f %alien-invoke
161 ] with-aligned-stack ;
163 M: x86.32 %prepare-unbox ( -- )
164 #! Move top of data stack to EAX.
168 : call-unbox-func ( func -- )
170 ! push the vm ptr as an argument
176 ] with-aligned-stack ;
178 M: x86.32 %unbox ( n rep func -- )
179 #! The value being unboxed must already be in EAX.
180 #! If n is f, we're unboxing a return value about to be
181 #! returned by the callback. Otherwise, we're unboxing
182 #! a parameter to a C function about to be called.
184 ! Store the return value on the C stack
185 over [ store-return-reg ] [ 2drop ] if ;
187 M: x86.32 %unbox-long-long ( n func -- )
189 ! Store the return value on the C stack
192 cell + stack@ EDX MOV
195 : %unbox-struct-1 ( -- )
196 #! Alien must be in EAX.
200 "alien_offset" f %alien-invoke
203 ] with-aligned-stack ;
205 : %unbox-struct-2 ( -- )
206 #! Alien must be in EAX.
210 "alien_offset" f %alien-invoke
215 ] with-aligned-stack ;
217 M: x86 %unbox-small-struct ( size -- )
218 #! Alien must be in EAX.
219 heap-size cell align cell /i {
220 { 1 [ %unbox-struct-1 ] }
221 { 2 [ %unbox-struct-2 ] }
224 M:: x86.32 %unbox-large-struct ( n c-type -- )
225 ! Alien must be in EAX.
226 ! Compute destination address
231 c-type heap-size PUSH
232 ! Push destination address
234 ! Push source address
236 ! Copy the struct to the stack
237 "to_value_struct" f %alien-invoke
238 ] with-aligned-stack ;
240 M: x86.32 %nest-stacks ( -- )
243 "nest_stacks" f %alien-invoke
244 ] with-aligned-stack ;
246 M: x86.32 %unnest-stacks ( -- )
249 "unnest_stacks" f %alien-invoke
250 ] with-aligned-stack ;
252 M: x86.32 %prepare-alien-indirect ( -- )
253 push-vm-ptr "unbox_alien" f %alien-invoke
257 M: x86.32 %alien-indirect ( -- )
260 M: x86.32 %alien-callback ( quot -- )
262 EAX swap %load-reference
264 param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup
265 "c_to_factor" f %alien-invoke
266 ] with-aligned-stack ;
268 M: x86.32 %callback-value ( ctype -- )
271 ! Save top of data stack in non-volatile register
275 ! Restore data/call/retain stacks
276 "unnest_stacks" f %alien-invoke
277 ! Place top of data stack in EAX
286 M: x86.32 %cleanup ( params -- )
287 #! a) If we just called an stdcall function in Windows, it
288 #! cleaned up the stack frame for us. But we don't want that
289 #! so we 'undo' the cleanup since we do that in %epilogue.
290 #! b) If we just called a function returning a struct, we
294 [ dup abi>> "stdcall" = ]
295 [ drop ESP stack-frame get params>> SUB ]
297 [ dup return>> large-struct? ]
303 M: x86.32 %callback-return ( n -- )
304 #! a) If the callback is stdcall, we have to clean up the
305 #! caller's stack frame.
306 #! b) If the callback is returning a large struct, we have
309 { [ dup abi>> "stdcall" = ] [
311 [ params>> ] [ return>> ] bi +
313 { [ dup return>> large-struct? ] [ drop 4 ] }
317 M:: x86.32 %call-gc ( gc-root-count temp -- )
318 temp gc-root-base param@ LEA
320 ! Pass the VM ptr as the third parameter
321 0 PUSH rc-absolute-cell rt-vm rel-fixup
322 ! Pass number of roots as second parameter
324 ! Pass pointer to start of GC roots as first parameter
327 "inline_gc" f %alien-invoke
328 ] with-aligned-stack ;
330 M: x86.32 dummy-stack-params? f ;
332 M: x86.32 dummy-int-params? f ;
334 M: x86.32 dummy-fp-params? f ;
337 cell "longlong" c-type (>>align)
338 cell "ulonglong" c-type (>>align)
339 4 "double" c-type (>>align)