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 ;
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 temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument
54 M: x86.32 %vm-invoke-1st-arg ( function -- )
59 M: x86.32 %vm-invoke-3rd-arg ( function -- )
60 %vm-invoke-1st-arg ; ! first 2 args are regs, 3rd is stack so vm-invoke-1st-arg works here
62 M: x86.32 return-struct-in-registers? ( c-type -- ? )
64 [ return-in-registers?>> ]
65 [ heap-size { 1 2 4 8 } member? ] bi
66 os { linux netbsd solaris } member? not
69 : struct-return@ ( n -- operand )
70 [ next-stack@ ] [ stack-frame get params>> stack@ ] if* ;
72 ! On x86, parameters are never passed in registers.
73 M: int-regs return-reg drop EAX ;
74 M: int-regs param-regs drop { } ;
75 M: float-regs param-regs drop { } ;
77 GENERIC: push-return-reg ( rep -- )
78 GENERIC: load-return-reg ( n rep -- )
79 GENERIC: store-return-reg ( n rep -- )
81 M: int-rep push-return-reg drop EAX PUSH ;
82 M: int-rep load-return-reg drop EAX swap next-stack@ MOV ;
83 M: int-rep store-return-reg drop stack@ EAX MOV ;
85 M: float-rep push-return-reg drop ESP 4 SUB ESP [] FSTPS ;
86 M: float-rep load-return-reg drop next-stack@ FLDS ;
87 M: float-rep store-return-reg drop stack@ FSTPS ;
89 M: double-rep push-return-reg drop ESP 8 SUB ESP [] FSTPL ;
90 M: double-rep load-return-reg drop next-stack@ FLDL ;
91 M: double-rep store-return-reg drop stack@ FSTPL ;
94 [ align-stack ] keep - decr-stack-reg ;
97 align-stack incr-stack-reg ;
99 : with-aligned-stack ( n quot -- )
100 '[ align-sub @ ] [ align-add ] bi ; inline
102 M: x86.32 %prologue ( n -- )
104 0 PUSH rc-absolute-cell rel-this
105 3 cells - decr-stack-reg ;
107 M: x86.32 %load-param-reg 3drop ;
109 M: x86.32 %save-param-reg 3drop ;
111 : (%box) ( n rep -- )
112 #! If n is f, push the return register onto the stack; we
113 #! are boxing a return value of a C function. If n is an
114 #! integer, push [ESP+n] on the stack; we are boxing a
115 #! parameter being passed to a callback from C.
116 over [ load-return-reg ] [ 2drop ] if ;
118 CONSTANT: vm-ptr-size 4
120 M:: x86.32 %box ( n rep func -- )
122 rep rep-size vm-ptr-size + [
126 ] with-aligned-stack ;
128 : (%box-long-long) ( n -- )
130 EDX over next-stack@ MOV
131 EAX swap cell - next-stack@ MOV
134 M: x86.32 %box-long-long ( n func -- )
135 [ (%box-long-long) ] dip
141 ] with-aligned-stack ;
143 M:: x86.32 %box-large-struct ( n c-type -- )
144 ! Compute destination address
145 EDX n struct-return@ LEA
149 c-type heap-size PUSH
150 ! Push destination address
152 ! Copy the struct from the C stack
153 "box_value_struct" f %alien-invoke
154 ] with-aligned-stack ;
156 M: x86.32 %prepare-box-struct ( -- )
157 ! Compute target address for value struct return
158 EAX f struct-return@ LEA
159 ! Store it as the first parameter
162 M: x86.32 %box-small-struct ( c-type -- )
163 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
169 "box_small_struct" f %alien-invoke
170 ] with-aligned-stack ;
172 M: x86.32 %prepare-unbox ( -- )
173 #! Move top of data stack to EAX.
177 : call-unbox-func ( func -- )
179 ! push the vm ptr as an argument
185 ] with-aligned-stack ;
187 M: x86.32 %unbox ( n rep func -- )
188 #! The value being unboxed must already be in EAX.
189 #! If n is f, we're unboxing a return value about to be
190 #! returned by the callback. Otherwise, we're unboxing
191 #! a parameter to a C function about to be called.
193 ! Store the return value on the C stack
194 over [ store-return-reg ] [ 2drop ] if ;
196 M: x86.32 %unbox-long-long ( n func -- )
198 ! Store the return value on the C stack
201 cell + stack@ EDX MOV
204 : %unbox-struct-1 ( -- )
205 #! Alien must be in EAX.
209 "alien_offset" f %alien-invoke
212 ] with-aligned-stack ;
214 : %unbox-struct-2 ( -- )
215 #! Alien must be in EAX.
219 "alien_offset" f %alien-invoke
224 ] with-aligned-stack ;
226 M: x86 %unbox-small-struct ( size -- )
227 #! Alien must be in EAX.
228 heap-size cell align cell /i {
229 { 1 [ %unbox-struct-1 ] }
230 { 2 [ %unbox-struct-2 ] }
233 M:: x86.32 %unbox-large-struct ( n c-type -- )
234 ! Alien must be in EAX.
235 ! Compute destination address
240 c-type heap-size PUSH
241 ! Push destination address
243 ! Push source address
245 ! Copy the struct to the stack
246 "to_value_struct" f %alien-invoke
247 ] with-aligned-stack ;
249 M: x86.32 %prepare-alien-indirect ( -- )
250 push-vm-ptr "unbox_alien" f %alien-invoke
254 M: x86.32 %alien-indirect ( -- )
257 M: x86.32 %alien-callback ( quot -- )
259 EAX swap %load-reference
261 param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup
262 "c_to_factor" f %alien-invoke
263 ] with-aligned-stack ;
265 M: x86.32 %callback-value ( ctype -- )
268 ! Save top of data stack in non-volatile register
272 ! Restore data/call/retain stacks
273 "unnest_stacks" f %alien-invoke
274 ! Place top of data stack in EAX
282 M: x86.32 %cleanup ( params -- )
283 #! a) If we just called an stdcall function in Windows, it
284 #! cleaned up the stack frame for us. But we don't want that
285 #! so we 'undo' the cleanup since we do that in %epilogue.
286 #! b) If we just called a function returning a struct, we
290 [ dup abi>> "stdcall" = ]
291 [ drop ESP stack-frame get params>> SUB ]
293 [ dup return>> large-struct? ]
299 M: x86.32 %callback-return ( n -- )
300 #! a) If the callback is stdcall, we have to clean up the
301 #! caller's stack frame.
302 #! b) If the callback is returning a large struct, we have
305 { [ dup abi>> "stdcall" = ] [
307 [ params>> ] [ return>> ] bi +
309 { [ dup return>> large-struct? ] [ drop 4 ] }
313 M: x86.32 dummy-stack-params? f ;
315 M: x86.32 dummy-int-params? f ;
317 M: x86.32 dummy-fp-params? f ;
320 cell "longlong" c-type (>>align)
321 cell "ulonglong" c-type (>>align)
322 4 "double" c-type (>>align)
325 "cpu.x86.features" require