factor/basis/cpu/x86/32/32.factor

298 lines
8.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-10-20 06:55:57 -04:00
USING: locals alien.c-types alien.syntax arrays kernel
math namespaces sequences system layouts io vocabs.loader
accessors init combinators command-line cpu.x86.assembler
cpu.x86.architecture cpu.architecture compiler compiler.units
2008-10-07 21:00:38 -04:00
compiler.constants compiler.alien compiler.codegen
2008-10-20 06:55:57 -04:00
compiler.codegen.fixup compiler.cfg.instructions
compiler.cfg.builder compiler.cfg.intrinsics ;
2007-09-20 18:09:08 -04:00
IN: cpu.x86.32
! We implement the FFI for Linux, OS X and Windows all at once.
! OS X requires that the stack be 16-byte aligned, and we do
! this on all platforms, sacrificing some stack space for
! code simplicity.
2008-10-07 21:00:38 -04:00
M: x86.32 machine-registers
{
{ int-regs { EAX ECX EDX EBP EBX } }
{ double-float-regs { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } }
} ;
M: x86.32 ds-reg ESI ;
M: x86.32 rs-reg EDI ;
M: x86.32 stack-reg ESP ;
2008-04-19 05:52:34 -04:00
M: x86.32 temp-reg-1 EAX ;
M: x86.32 temp-reg-2 ECX ;
2007-09-20 18:09:08 -04:00
2008-05-09 00:21:46 -04:00
M: x86.32 %alien-global 0 [] MOV rc-absolute-cell rel-dlsym ;
M: x86.32 %alien-invoke (CALL) rel-dlsym ;
2007-09-20 18:09:08 -04:00
M: x86.32 struct-small-enough? ( size -- ? )
heap-size { 1 2 4 8 } member?
os { linux netbsd solaris } member? not and ;
2008-10-06 01:20:00 -04:00
: struct-return@ ( n -- operand )
[ next-stack@ ] [ stack-frame get params>> stack@ ] if* ;
2008-10-05 22:30:29 -04:00
2007-09-20 18:09:08 -04:00
! On x86, parameters are never passed in registers.
M: int-regs return-reg drop EAX ;
M: int-regs param-regs drop { } ;
M: int-regs push-return-reg return-reg PUSH ;
2008-10-05 22:30:29 -04:00
M: int-regs load-return-reg
return-reg swap next-stack@ MOV ;
M: int-regs store-return-reg
[ stack@ ] [ return-reg ] bi* MOV ;
2007-09-20 18:09:08 -04:00
M: float-regs param-regs drop { } ;
2008-06-08 16:32:55 -04:00
: FSTP ( operand size -- ) 4 = [ FSTPS ] [ FSTPL ] if ;
2007-09-20 18:09:08 -04:00
M: float-regs push-return-reg
2008-10-05 22:30:29 -04:00
stack-reg swap reg-size
[ SUB ] [ [ [] ] dip FSTP ] 2bi ;
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: FLD ( operand size -- ) 4 = [ FLDS ] [ FLDL ] if ;
2007-09-20 18:09:08 -04:00
2008-10-05 22:30:29 -04:00
M: float-regs load-return-reg
[ next-stack@ ] [ reg-size ] bi* FLD ;
M: float-regs store-return-reg
[ stack@ ] [ reg-size ] bi* FSTP ;
2007-09-20 18:09:08 -04:00
: align-sub ( n -- )
2008-10-06 01:20:00 -04:00
[ align-stack ] keep - decr-stack-reg ;
2007-09-20 18:09:08 -04:00
: align-add ( n -- )
2008-10-06 01:20:00 -04:00
align-stack incr-stack-reg ;
2007-09-20 18:09:08 -04:00
: with-aligned-stack ( n quot -- )
2008-10-05 22:30:29 -04:00
[ [ align-sub ] [ call ] bi* ]
[ [ align-add ] [ drop ] bi* ] 2bi ; inline
2007-09-20 18:09:08 -04:00
2008-10-20 02:56:28 -04:00
M: x86.32 %load-indirect
2008-10-20 06:55:57 -04:00
0 [] MOV rc-absolute-cell rel-literal ;
2007-09-20 18:09:08 -04:00
M: object %load-param-reg 3drop ;
M: object %save-param-reg 3drop ;
: (%box) ( n reg-class -- )
#! If n is f, push the return register onto the stack; we
#! are boxing a return value of a C function. If n is an
#! integer, push [ESP+n] on the stack; we are boxing a
#! parameter being passed to a callback from C.
2008-10-05 22:30:29 -04:00
over [ load-return-reg ] [ 2drop ] if ;
2007-09-20 18:09:08 -04:00
2008-10-05 22:30:29 -04:00
M:: x86.32 %box ( n reg-class func -- )
n reg-class (%box)
reg-class reg-size [
reg-class push-return-reg
func f %alien-invoke
2007-09-20 18:09:08 -04:00
] with-aligned-stack ;
2008-06-08 16:32:55 -04:00
: (%box-long-long) ( n -- )
2007-09-20 18:09:08 -04:00
[
2008-10-05 22:30:29 -04:00
EDX over next-stack@ MOV
EAX swap cell - next-stack@ MOV
] when* ;
2007-09-20 18:09:08 -04:00
M: x86.32 %box-long-long ( n func -- )
2008-10-05 22:30:29 -04:00
[ (%box-long-long) ] dip
2007-09-20 18:09:08 -04:00
8 [
2008-10-05 22:30:29 -04:00
EDX PUSH
EAX PUSH
f %alien-invoke
2007-09-20 18:09:08 -04:00
] with-aligned-stack ;
2008-10-05 22:30:29 -04:00
M:: x86.32 %box-large-struct ( n c-type -- )
2007-09-20 18:09:08 -04:00
! Compute destination address
2008-10-06 01:20:00 -04:00
ECX n struct-return@ LEA
2007-09-20 18:09:08 -04:00
8 [
! Push struct size
2008-10-05 22:30:29 -04:00
c-type heap-size PUSH
2007-09-20 18:09:08 -04:00
! Push destination address
ECX PUSH
! Copy the struct from the C stack
"box_value_struct" f %alien-invoke
] with-aligned-stack ;
2008-10-06 01:20:00 -04:00
M: x86.32 %prepare-box-struct ( -- )
2007-09-20 18:09:08 -04:00
! Compute target address for value struct return
2008-10-06 01:20:00 -04:00
EAX f struct-return@ LEA
2007-09-20 18:09:08 -04:00
! Store it as the first parameter
2008-10-05 22:30:29 -04:00
0 stack@ EAX MOV ;
2007-09-20 18:09:08 -04:00
M: x86.32 %box-small-struct ( c-type -- )
#! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
12 [
heap-size PUSH
EDX PUSH
EAX PUSH
"box_small_struct" f %alien-invoke
] with-aligned-stack ;
M: x86.32 %prepare-unbox ( -- )
#! Move top of data stack to EAX.
EAX ESI [] MOV
ESI 4 SUB ;
: (%unbox) ( func -- )
4 [
! Push parameter
EAX PUSH
! Call the unboxer
f %alien-invoke
] with-aligned-stack ;
M: x86.32 %unbox ( n reg-class func -- )
#! The value being unboxed must already be in EAX.
#! If n is f, we're unboxing a return value about to be
#! returned by the callback. Otherwise, we're unboxing
#! a parameter to a C function about to be called.
(%unbox)
! Store the return value on the C stack
over [ store-return-reg ] [ 2drop ] if ;
M: x86.32 %unbox-long-long ( n func -- )
(%unbox)
! Store the return value on the C stack
[
dup stack@ EAX MOV
cell + stack@ EDX MOV
] when* ;
: %unbox-struct-1 ( -- )
#! Alien must be in EAX.
4 [
EAX PUSH
"alien_offset" f %alien-invoke
! Load first cell
EAX EAX [] MOV
] with-aligned-stack ;
: %unbox-struct-2 ( -- )
2007-09-20 18:09:08 -04:00
#! Alien must be in EAX.
4 [
EAX PUSH
"alien_offset" f %alien-invoke
! Load second cell
EDX EAX 4 [+] MOV
2007-09-20 18:09:08 -04:00
! Load first cell
EAX EAX [] MOV
] with-aligned-stack ;
M: x86 %unbox-small-struct ( size -- )
#! Alien must be in EAX.
heap-size cell align cell /i {
{ 1 [ %unbox-struct-1 ] }
{ 2 [ %unbox-struct-2 ] }
} case ;
M: x86.32 %unbox-large-struct ( n c-type -- )
2008-10-05 22:30:29 -04:00
! Alien must be in EAX.
! Compute destination address
2008-10-05 22:30:29 -04:00
ECX rot stack@ LEA
2007-09-20 18:09:08 -04:00
12 [
! Push struct size
2008-10-05 22:30:29 -04:00
heap-size PUSH
! Push destination address
ECX PUSH
! Push source address
2007-09-20 18:09:08 -04:00
EAX PUSH
! Copy the struct to the stack
"to_value_struct" f %alien-invoke
2007-09-20 18:09:08 -04:00
] with-aligned-stack ;
M: x86.32 %prepare-alien-indirect ( -- )
2007-09-20 18:09:08 -04:00
"unbox_alien" f %alien-invoke
2008-10-05 22:30:29 -04:00
EBP EAX MOV ;
2007-09-20 18:09:08 -04:00
M: x86.32 %alien-indirect ( -- )
2008-10-05 22:30:29 -04:00
EBP CALL ;
2007-09-20 18:09:08 -04:00
M: x86.32 %alien-callback ( quot -- )
2007-09-20 18:09:08 -04:00
4 [
2008-10-20 06:55:57 -04:00
EAX %load-indirect
2007-09-20 18:09:08 -04:00
EAX PUSH
"c_to_factor" f %alien-invoke
] with-aligned-stack ;
M: x86.32 %callback-value ( ctype -- )
2007-09-20 18:09:08 -04:00
! Align C stack
ESP 12 SUB
2008-10-05 22:30:29 -04:00
! Save top of data stack in non-volatile register
2007-09-20 18:09:08 -04:00
%prepare-unbox
EAX PUSH
! Restore data/call/retain stacks
"unnest_stacks" f %alien-invoke
! Place top of data stack in EAX
EAX POP
! Restore C stack
ESP 12 ADD
! Unbox EAX
unbox-return ;
2008-10-13 00:32:14 -04:00
M: x86.32 %cleanup ( params -- )
2007-09-20 18:09:08 -04:00
#! a) If we just called an stdcall function in Windows, it
#! cleaned up the stack frame for us. But we don't want that
#! so we 'undo' the cleanup since we do that in %epilogue.
#! b) If we just called a function returning a struct, we
#! have to fix ESP.
{
{
2008-03-20 21:13:13 -04:00
[ dup abi>> "stdcall" = ]
2008-10-06 01:20:00 -04:00
[ drop ESP stack-frame get params>> SUB ]
2007-09-20 18:09:08 -04:00
} {
2008-03-20 21:13:13 -04:00
[ dup return>> large-struct? ]
2007-09-20 18:09:08 -04:00
[ drop EAX PUSH ]
}
2008-04-11 13:53:22 -04:00
[ drop ]
2007-09-20 18:09:08 -04:00
} cond ;
2008-10-13 00:32:14 -04:00
M: x86.32 %callback-return ( n -- )
#! a) If the callback is stdcall, we have to clean up the
#! caller's stack frame.
#! b) If the callback is returning a large struct, we have
#! to fix ESP.
{
2008-10-19 02:10:45 -04:00
{ [ dup abi>> "stdcall" = ] [
<alien-stack-frame>
[ params>> ] [ return>> ] bi +
] }
2008-10-13 00:32:14 -04:00
{ [ dup return>> large-struct? ] [ drop 4 ] }
[ drop 0 ]
} cond RET ;
2007-09-20 18:09:08 -04:00
2008-04-02 19:25:33 -04:00
os windows? [
cell "longlong" c-type (>>align)
cell "ulonglong" c-type (>>align)
4 "double" c-type (>>align)
2008-02-11 17:56:48 -05:00
] unless
2008-02-04 18:33:59 -05:00
2008-10-20 06:55:57 -04:00
FUNCTION: bool check_sse2 ( ) ;
2007-09-20 18:09:08 -04:00
2008-10-20 06:55:57 -04:00
: sse2? ( -- ? )
[ optimized-recompile-hook ] recompile-hook
[ [ check_sse2 ] compile-call ] with-variable ;
2008-06-27 21:04:03 -04:00
2007-09-20 18:09:08 -04:00
"-no-sse2" cli-args member? [
"Checking if your CPU supports SSE2..." print flush
2008-10-20 06:55:57 -04:00
sse2? [
2007-09-20 18:09:08 -04:00
" - yes" print
2008-10-20 06:55:57 -04:00
enable-float-intrinsics
2008-06-27 21:04:03 -04:00
[
sse2? [
"This image was built to use SSE2, which your CPU does not support." print
"You will need to bootstrap Factor again." print
flush
1 exit
] unless
] "cpu.x86" add-init-hook
2008-10-20 06:55:57 -04:00
] [ " - no" print ] if
2007-09-20 18:09:08 -04:00
] unless