FFI work
parent
14f1f0aaae
commit
9ea2332a2b
|
@ -309,6 +309,7 @@ vectors words ;
|
|||
"/library/compiler/amd64/assembler.factor"
|
||||
"/library/compiler/amd64/architecture.factor"
|
||||
"/library/compiler/x86/generator.factor"
|
||||
"/library/compiler/amd64/generator.factor"
|
||||
"/library/compiler/x86/slots.factor"
|
||||
"/library/compiler/x86/stack.factor"
|
||||
"/library/compiler/x86/fixnum.factor"
|
||||
|
|
|
@ -1,31 +1,36 @@
|
|||
! Copyright (C) 2005 Slava Pestov.
|
||||
! See http://factor.sf.net/license.txt for BSD license.
|
||||
! Copyright (C) 2005, 2006 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
IN: compiler-backend
|
||||
USING: alien assembler kernel math sequences ;
|
||||
USING: alien arrays assembler kernel kernel-internals math
|
||||
sequences ;
|
||||
|
||||
! GENERIC: store-insn ( offset reg-class -- )
|
||||
!
|
||||
! GENERIC: load-insn ( elt parameter reg-class -- )
|
||||
!
|
||||
! M: int-regs store-insn drop >r 3 1 r> stack@ STW ;
|
||||
!
|
||||
! M: int-regs load-insn drop 3 + 1 rot stack@ LWZ ;
|
||||
!
|
||||
! M: %unbox generate-node ( vop -- )
|
||||
! drop
|
||||
! ! Call the unboxer
|
||||
! 1 input f compile-c-call
|
||||
! ! Store the return value on the C stack
|
||||
! 0 input 2 input store-insn ;
|
||||
!
|
||||
! M: %parameter generate-node ( vop -- )
|
||||
! ! Move a value from the C stack into the fastcall register
|
||||
! drop 0 input 1 input 2 input load-insn ;
|
||||
!
|
||||
! M: %box generate-node ( vop -- )
|
||||
! drop
|
||||
! ! Move return value of C function into input register
|
||||
! param-regs first RAX MOV
|
||||
! 0 input f compile-c-call ;
|
||||
!
|
||||
! M: %cleanup generate-node ( vop -- ) drop ;
|
||||
GENERIC: store-insn ( offset reg-class -- )
|
||||
|
||||
GENERIC: load-insn ( elt parameter reg-class -- )
|
||||
|
||||
: stack@ RCX RSP MOV RCX swap 2array ;
|
||||
|
||||
M: int-regs store-insn
|
||||
drop stack@ RAX MOV ;
|
||||
|
||||
M: int-regs load-insn
|
||||
drop param-regs nth swap stack@ MOV ;
|
||||
|
||||
M: %unbox generate-node ( vop -- )
|
||||
drop
|
||||
! Call the unboxer
|
||||
1 input f compile-c-call
|
||||
! Store the return value on the C stack
|
||||
0 input 2 input store-insn ;
|
||||
|
||||
M: %parameter generate-node ( vop -- )
|
||||
! Move a value from the C stack into the fastcall register
|
||||
drop 0 input 1 input 2 input load-insn ;
|
||||
|
||||
M: %box generate-node ( vop -- )
|
||||
drop
|
||||
! Move return value of C function into input register
|
||||
param-regs first RAX MOV
|
||||
0 input f compile-c-call ;
|
||||
|
||||
M: %cleanup generate-node ( vop -- ) drop ;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
! Copyright (C) 2005, 2006 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
IN: compiler-backend
|
||||
USING: alien arrays assembler compiler compiler-backend kernel
|
||||
kernel-internals math sequences ;
|
||||
|
@ -44,10 +46,6 @@ M: float-regs fastcall-regs drop 0 ;
|
|||
|
||||
: prepare-division CQO ; inline
|
||||
|
||||
: compile-prologue RSP 8 SUB ; inline
|
||||
|
||||
: compile-epilogue RSP 8 ADD ; inline
|
||||
|
||||
: load-indirect ( dest literal -- )
|
||||
#! We use RIP-relative addressing. The '3' is a hardcoded
|
||||
#! instruction length.
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
! Copyright (C) 2006 Slava Pestov.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
IN: compiler-backend
|
||||
USING: assembler kernel math namespaces ;
|
||||
|
||||
: stack-increment \ stack-reserve get 16 align 8 + ;
|
||||
|
||||
M: %prologue generate-node ( vop -- )
|
||||
drop RSP stack-increment SUB ;
|
||||
|
||||
: compile-epilogue ( -- )
|
||||
RSP stack-increment ADD ; inline
|
|
@ -5,8 +5,7 @@ USING: alien arrays assembler compiler inference kernel
|
|||
kernel-internals lists math memory namespaces sequences words ;
|
||||
|
||||
! Not used on x86
|
||||
M: %prologue generate-node ( vop -- )
|
||||
drop compile-prologue ;
|
||||
M: %prologue generate-node ( vop -- ) drop ;
|
||||
|
||||
: (%call)
|
||||
label dup postpone-word
|
||||
|
|
|
@ -6,3 +6,9 @@ FUNCTION: void ffi_test_0 ; compiled
|
|||
FUNCTION: int ffi_test_1 ; compiled
|
||||
[ 3 ] [ ffi_test_1 ] unit-test
|
||||
|
||||
FUNCTION: int ffi_test_2 int x int y ; compiled
|
||||
[ 5 ] [ 2 3 ffi_test_2 ] unit-test
|
||||
|
||||
FUNCTION: int ffi_test_3 int x int y int z int t ; compiled
|
||||
[ 25 ] [ 2 3 4 5 ffi_test_3 ] unit-test
|
||||
|
||||
|
|
|
@ -17,3 +17,9 @@ int ffi_test_2(int x, int y)
|
|||
printf("ffi_test_2(%d,%d)\n",x,y);
|
||||
return x + y;
|
||||
}
|
||||
|
||||
int ffi_test_3(int x, int y, int z, int t)
|
||||
{
|
||||
printf("ffi_test_3(%d,%d,%d,%d)\n",x,y,z,t);
|
||||
return x + y + z * t;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue