1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: locals alien.c-types alien.syntax arrays kernel
4 math namespaces sequences system layouts io vocabs.loader
5 accessors init combinators command-line cpu.x86.assembler
6 cpu.x86 cpu.architecture compiler compiler.units
7 compiler.constants compiler.alien compiler.codegen
8 compiler.codegen.fixup compiler.cfg.instructions
9 compiler.cfg.builder compiler.cfg.intrinsics make ;
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, and we do
14 ! this on all platforms, sacrificing some stack space for
17 M: x86.32 machine-registers
19 { int-regs { EAX ECX EDX EBP EBX } }
20 { double-float-regs { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } }
23 M: x86.32 ds-reg ESI ;
24 M: x86.32 rs-reg EDI ;
25 M: x86.32 stack-reg ESP ;
26 M: x86.32 temp-reg-1 ECX ;
27 M: x86.32 temp-reg-2 EDX ;
29 M:: x86.32 %dispatch ( src temp -- )
30 ! Load jump table base.
32 0 rc-absolute-cell rel-here
35 ! Fix up the displacement above
37 [ 7 + building get dup pop* push ]
41 ! Registers for fastcall
42 M: x86.32 param-reg-1 EAX ;
43 M: x86.32 param-reg-2 EDX ;
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 ;
51 M: x86.32 %alien-invoke-tail 0 JMP rc-relative rel-dlsym ;
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: int-regs push-return-reg return-reg PUSH ;
68 M: int-regs load-return-reg
69 return-reg swap next-stack@ MOV ;
71 M: int-regs store-return-reg
72 [ stack@ ] [ return-reg ] bi* MOV ;
74 M: float-regs param-regs drop { } ;
76 : FSTP ( operand size -- ) 4 = [ FSTPS ] [ FSTPL ] if ;
78 M: float-regs push-return-reg
79 stack-reg swap reg-size
80 [ SUB ] [ [ [] ] dip FSTP ] 2bi ;
82 : FLD ( operand size -- ) 4 = [ FLDS ] [ FLDL ] if ;
84 M: float-regs load-return-reg
85 [ next-stack@ ] [ reg-size ] bi* FLD ;
87 M: float-regs store-return-reg
88 [ stack@ ] [ reg-size ] bi* FSTP ;
91 [ align-stack ] keep - decr-stack-reg ;
94 align-stack incr-stack-reg ;
96 : with-aligned-stack ( n quot -- )
97 [ [ align-sub ] [ call ] bi* ]
98 [ [ align-add ] [ drop ] bi* ] 2bi ; inline
100 M: x86.32 %prologue ( n -- )
102 0 PUSH rc-absolute-cell rel-this
103 stack-reg swap 3 cells - SUB ;
105 M: object %load-param-reg 3drop ;
107 M: object %save-param-reg 3drop ;
109 : (%box) ( n reg-class -- )
110 #! If n is f, push the return register onto the stack; we
111 #! are boxing a return value of a C function. If n is an
112 #! integer, push [ESP+n] on the stack; we are boxing a
113 #! parameter being passed to a callback from C.
114 over [ load-return-reg ] [ 2drop ] if ;
116 M:: x86.32 %box ( n reg-class func -- )
119 reg-class push-return-reg
121 ] with-aligned-stack ;
123 : (%box-long-long) ( n -- )
125 EDX over next-stack@ MOV
126 EAX swap cell - next-stack@ MOV
129 M: x86.32 %box-long-long ( n func -- )
130 [ (%box-long-long) ] dip
135 ] with-aligned-stack ;
137 M:: x86.32 %box-large-struct ( n c-type -- )
138 ! Compute destination address
139 ECX n struct-return@ LEA
142 c-type heap-size PUSH
143 ! Push destination address
145 ! Copy the struct from the C stack
146 "box_value_struct" f %alien-invoke
147 ] with-aligned-stack ;
149 M: x86.32 %prepare-box-struct ( -- )
150 ! Compute target address for value struct return
151 EAX f struct-return@ LEA
152 ! Store it as the first parameter
155 M: x86.32 %box-small-struct ( c-type -- )
156 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
161 "box_small_struct" f %alien-invoke
162 ] with-aligned-stack ;
164 M: x86.32 %prepare-unbox ( -- )
165 #! Move top of data stack to EAX.
169 : (%unbox) ( func -- )
175 ] with-aligned-stack ;
177 M: x86.32 %unbox ( n reg-class func -- )
178 #! The value being unboxed must already be in EAX.
179 #! If n is f, we're unboxing a return value about to be
180 #! returned by the callback. Otherwise, we're unboxing
181 #! a parameter to a C function about to be called.
183 ! Store the return value on the C stack
184 over [ store-return-reg ] [ 2drop ] if ;
186 M: x86.32 %unbox-long-long ( n func -- )
188 ! Store the return value on the C stack
191 cell + stack@ EDX MOV
194 : %unbox-struct-1 ( -- )
195 #! Alien must be in EAX.
198 "alien_offset" f %alien-invoke
201 ] with-aligned-stack ;
203 : %unbox-struct-2 ( -- )
204 #! Alien must be in EAX.
207 "alien_offset" f %alien-invoke
212 ] with-aligned-stack ;
214 M: x86 %unbox-small-struct ( size -- )
215 #! Alien must be in EAX.
216 heap-size cell align cell /i {
217 { 1 [ %unbox-struct-1 ] }
218 { 2 [ %unbox-struct-2 ] }
221 M: x86.32 %unbox-large-struct ( n c-type -- )
222 ! Alien must be in EAX.
223 ! Compute destination address
228 ! Push destination address
230 ! Push source address
232 ! Copy the struct to the stack
233 "to_value_struct" f %alien-invoke
234 ] with-aligned-stack ;
236 M: x86.32 %prepare-alien-indirect ( -- )
237 "unbox_alien" f %alien-invoke
240 M: x86.32 %alien-indirect ( -- )
243 M: x86.32 %alien-callback ( quot -- )
245 EAX swap %load-reference
247 "c_to_factor" f %alien-invoke
248 ] with-aligned-stack ;
250 M: x86.32 %callback-value ( ctype -- )
253 ! Save top of data stack in non-volatile register
256 ! Restore data/call/retain stacks
257 "unnest_stacks" f %alien-invoke
258 ! Place top of data stack in EAX
265 M: x86.32 %cleanup ( params -- )
266 #! a) If we just called an stdcall function in Windows, it
267 #! cleaned up the stack frame for us. But we don't want that
268 #! so we 'undo' the cleanup since we do that in %epilogue.
269 #! b) If we just called a function returning a struct, we
273 [ dup abi>> "stdcall" = ]
274 [ drop ESP stack-frame get params>> SUB ]
276 [ dup return>> large-struct? ]
282 M: x86.32 %callback-return ( n -- )
283 #! a) If the callback is stdcall, we have to clean up the
284 #! caller's stack frame.
285 #! b) If the callback is returning a large struct, we have
288 { [ dup abi>> "stdcall" = ] [
290 [ params>> ] [ return>> ] bi +
292 { [ dup return>> large-struct? ] [ drop 4 ] }
296 M: x86.32 dummy-stack-params? f ;
298 M: x86.32 dummy-int-params? f ;
300 M: x86.32 dummy-fp-params? f ;
303 cell "longlong" c-type (>>align)
304 cell "ulonglong" c-type (>>align)
305 4 "double" c-type (>>align)
308 USING: cpu.x86.features cpu.x86.features.private ;
310 "-no-sse2" (command-line) member? [
311 [ { check_sse2 } compile ] with-optimizer
313 "Checking if your CPU supports SSE2..." print flush
316 enable-float-intrinsics
319 "This image was built to use SSE2, which your CPU does not support." print
320 "You will need to bootstrap Factor again." print
324 ] "cpu.x86" add-init-hook
325 ] [ " - no" print ] if