From 9ea2332a2b1fe09a008cd2a12c841f1ae193dfca Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 25 Jan 2006 00:56:08 +0000 Subject: [PATCH] FFI work --- library/bootstrap/boot-stage1.factor | 1 + library/compiler/amd64/alien.factor | 63 ++++++++++++---------- library/compiler/amd64/architecture.factor | 6 +-- library/compiler/amd64/generator.factor | 12 +++++ library/compiler/x86/generator.factor | 3 +- library/test/compiler/alien.factor | 6 +++ native/ffi_test.c | 6 +++ 7 files changed, 62 insertions(+), 35 deletions(-) create mode 100644 library/compiler/amd64/generator.factor diff --git a/library/bootstrap/boot-stage1.factor b/library/bootstrap/boot-stage1.factor index cb4331a400..1737ba017c 100644 --- a/library/bootstrap/boot-stage1.factor +++ b/library/bootstrap/boot-stage1.factor @@ -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" diff --git a/library/compiler/amd64/alien.factor b/library/compiler/amd64/alien.factor index e89c6f318a..88b73fb931 100644 --- a/library/compiler/amd64/alien.factor +++ b/library/compiler/amd64/alien.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 ; diff --git a/library/compiler/amd64/architecture.factor b/library/compiler/amd64/architecture.factor index 080ff57f25..8aff995125 100644 --- a/library/compiler/amd64/architecture.factor +++ b/library/compiler/amd64/architecture.factor @@ -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. diff --git a/library/compiler/amd64/generator.factor b/library/compiler/amd64/generator.factor new file mode 100644 index 0000000000..2a7972799f --- /dev/null +++ b/library/compiler/amd64/generator.factor @@ -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 diff --git a/library/compiler/x86/generator.factor b/library/compiler/x86/generator.factor index 12bf29f9ef..6e2e93735a 100644 --- a/library/compiler/x86/generator.factor +++ b/library/compiler/x86/generator.factor @@ -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 diff --git a/library/test/compiler/alien.factor b/library/test/compiler/alien.factor index c25bb06012..cb4396a7a6 100644 --- a/library/test/compiler/alien.factor +++ b/library/test/compiler/alien.factor @@ -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 + diff --git a/native/ffi_test.c b/native/ffi_test.c index d24625d2ef..12e56a2a92 100644 --- a/native/ffi_test.c +++ b/native/ffi_test.c @@ -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; +}