From f9db19f91705ff786a6285d7706b873d29b7c9cf Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 21 Jan 2006 02:37:38 +0000 Subject: [PATCH] fixes --- Makefile | 3 ++- library/compiler/amd64/alien.factor | 24 ++++++++++++++++++---- library/compiler/amd64/architecture.factor | 4 +--- library/compiler/ppc/alien.factor | 2 +- library/test/compiler/alien.factor | 8 ++++++++ native/ffi_test.c | 19 +++++++++++++++++ 6 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 library/test/compiler/alien.factor create mode 100644 native/ffi_test.c diff --git a/Makefile b/Makefile index 0144cdbb52..9f6356b856 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,8 @@ OBJS = $(PLAF_OBJS) native/array.o native/bignum.o \ native/hashtable.o \ native/icache.o \ native/io.o \ - native/wrapper.o + native/wrapper.o \ + native/ffi_test.o default: @echo "Run 'make' with one of the following parameters:" diff --git a/library/compiler/amd64/alien.factor b/library/compiler/amd64/alien.factor index d6b496e285..bd0df2d9bf 100644 --- a/library/compiler/amd64/alien.factor +++ b/library/compiler/amd64/alien.factor @@ -1,15 +1,31 @@ ! Copyright (C) 2005 Slava Pestov. ! See http://factor.sf.net/license.txt for BSD license. IN: compiler-backend -USING: alien assembler kernel math ; +USING: alien assembler kernel 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 ; + 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 ; + drop 0 input 1 input 2 input load-insn ; -M: %box generate-node ( vop -- ) drop ; +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 ; diff --git a/library/compiler/amd64/architecture.factor b/library/compiler/amd64/architecture.factor index 325d2394e2..080ff57f25 100644 --- a/library/compiler/amd64/architecture.factor +++ b/library/compiler/amd64/architecture.factor @@ -17,8 +17,6 @@ kernel-internals math sequences ; : vregs { RAX RCX RDX RSI RDI R8 R9 R10 R11 } ; inline -: alien-regs { RDI RSI RDX RCX R8 R9 } ; inline - : param-regs { RDI RSI RDX RCX R8 R9 } ; inline : compile-c-call ( symbol dll -- ) @@ -29,7 +27,7 @@ kernel-internals math sequences ; param-regs swap [ MOV ] 2each compile-c-call ; M: int-regs return-reg drop RAX ; -M: int-regs fastcall-regs drop alien-regs length ; +M: int-regs fastcall-regs drop param-regs length ; M: float-regs fastcall-regs drop 0 ; diff --git a/library/compiler/ppc/alien.factor b/library/compiler/ppc/alien.factor index b5d48ce92a..35ff721e68 100644 --- a/library/compiler/ppc/alien.factor +++ b/library/compiler/ppc/alien.factor @@ -3,7 +3,7 @@ IN: compiler-backend USING: alien assembler kernel math ; -GENERIC: store-insn ( to offset reg-class -- ) +GENERIC: store-insn ( offset reg-class -- ) GENERIC: load-insn ( elt parameter reg-class -- ) diff --git a/library/test/compiler/alien.factor b/library/test/compiler/alien.factor new file mode 100644 index 0000000000..c25bb06012 --- /dev/null +++ b/library/test/compiler/alien.factor @@ -0,0 +1,8 @@ +USING: compiler test ; + +FUNCTION: void ffi_test_0 ; compiled +[ ] [ ffi_test_0 ] unit-test + +FUNCTION: int ffi_test_1 ; compiled +[ 3 ] [ ffi_test_1 ] unit-test + diff --git a/native/ffi_test.c b/native/ffi_test.c new file mode 100644 index 0000000000..d24625d2ef --- /dev/null +++ b/native/ffi_test.c @@ -0,0 +1,19 @@ +/* This file is linked into the runtime for the sole purpose + * of testing FFI code. */ + +void ffi_test_0(void) +{ + printf("ffi_test_0()\n"); +} + +int ffi_test_1(void) +{ + printf("ffi_test_1()\n"); + return 3; +} + +int ffi_test_2(int x, int y) +{ + printf("ffi_test_2(%d,%d)\n",x,y); + return x + y; +}