factor/library/compiler/x86/alien-macosx.factor

112 lines
3.1 KiB
Factor
Raw Normal View History

2006-07-03 03:27:30 -04:00
! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: compiler
USING: assembler errors kernel kernel-internals math namespaces ;
2006-07-03 03:27:30 -04:00
! OS X uses a different ABI. The stack must be 16-byte aligned.
2006-07-04 02:04:33 -04:00
: stack-increment \ stack-reserve get 16 align 16 + cell - ;
: %prologue ( n -- )
\ stack-reserve set stack-reg stack-increment SUB ;
: %epilogue ( -- )
stack-reg stack-increment ADD ;
2006-07-03 03:27:30 -04:00
: align-sub ( n -- )
2006-07-04 02:04:33 -04:00
dup 16 align swap - ESP swap SUB ;
2006-07-03 03:27:30 -04:00
: align-add ( n -- )
2006-07-04 02:04:33 -04:00
16 align ESP swap ADD ;
2006-07-03 03:27:30 -04:00
: with-aligned-stack ( n quot -- )
swap dup align-sub slip align-add ; inline
: struct-ptr/size ( n size func -- )
2006-07-04 02:04:33 -04:00
EAX ESP MOV ! Save stack pointer
>r >r EAX swap ADD r> r> ! Add n
8 [
2006-07-03 03:27:30 -04:00
! Push struct size
>r PUSH r>
2006-07-04 02:04:33 -04:00
! Push destination address
EAX PUSH
2006-07-03 03:27:30 -04:00
! Copy the struct to the stack
2006-07-04 02:04:33 -04:00
f %alien-invoke
2006-07-03 03:27:30 -04:00
] with-aligned-stack ;
: %unbox-struct ( n size -- )
"unbox_value_struct" struct-ptr/size ;
: %unbox ( n reg-class func -- )
! Call the unboxer
2006-07-04 02:04:33 -04:00
f %alien-invoke
2006-07-03 03:27:30 -04:00
! Store the return value on the C stack
2006-07-04 02:04:33 -04:00
store-return-reg ;
2006-07-03 03:27:30 -04:00
: %box-pair ( -- )
#! Box an 8-byte struct returned in EAX:EDX.
#! Why did Apple have to make things so complex?
#! Just use objc_msgSend_stret for all structs... jesus.
8 [
EDX PUSH
EAX PUSH
"box_value_pair" f %alien-invoke
] with-aligned-stack ;
2006-07-03 03:27:30 -04:00
: %box-struct ( n size -- )
over [
>r stack-increment + cell + r>
"box_value_struct" struct-ptr/size
] [
nip 8 = [
"Cannot %box-struct which is not 8 bytes." throw
] unless
%box-pair
] if ;
2006-07-03 03:27:30 -04:00
2006-07-04 02:04:33 -04:00
: box@ ( n reg-class -- stack@ )
#! Used for callbacks; we want to box the values given to
#! us by the C function caller. Computes stack location of
#! nth parameter; note that we must go back one more stack
#! frame, since %box sets one up to call the one-arg boxer
#! function. The size of this stack frame so far depends on
#! the reg-class of the boxer's arg.
16 swap reg-size - + stack-increment + cell + ;
: (%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.
over [ [ box@ ] keep [ load-return-reg ] keep ] [ nip ] if
push-return-reg ;
2006-07-03 03:27:30 -04:00
: %box ( n reg-class func -- )
2006-07-04 02:04:33 -04:00
over reg-size [
>r (%box) r> f %alien-invoke
2006-07-03 03:27:30 -04:00
] with-aligned-stack ;
: %alien-callback ( quot -- )
2006-07-04 02:04:33 -04:00
4 [
2006-07-03 03:27:30 -04:00
EAX load-indirect
EAX PUSH
2006-07-04 02:04:33 -04:00
"run_callback" f %alien-invoke
2006-07-03 03:27:30 -04:00
] with-aligned-stack ;
2006-07-06 01:01:05 -04:00
: align-callback-value ( reg-class -- reg n )
ESP 16 rot reg-size - ;
2006-07-03 03:27:30 -04:00
: %callback-value ( reg-class func -- )
2006-07-06 01:01:05 -04:00
! Call the unboxer
f %alien-invoke
dup align-callback-value SUB
2006-07-03 03:27:30 -04:00
! Save return register
dup push-return-reg
! Restore data/call/retain stacks
2006-07-04 02:04:33 -04:00
"unnest_stacks" f %alien-invoke
2006-07-03 03:27:30 -04:00
! Restore return register
2006-07-06 01:01:05 -04:00
dup pop-return-reg
align-callback-value ADD ;
2006-07-03 03:27:30 -04:00
: %cleanup ( n -- ) drop ;