1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-26 15:16:51 +00:00

Only enable FFI on x86-64, non-windows OSes.

This commit is contained in:
Calvin Rose 2022-06-08 09:50:31 -05:00
parent 3f27d78ab5
commit f92aac14aa
3 changed files with 27 additions and 24 deletions

View File

@ -19,17 +19,17 @@ double double_many(double x, double y, double z, double w, double a, double b) {
} }
double double_lots( double double_lots(
double a, double a,
double b, double b,
double c, double c,
double d, double d,
double e, double e,
double f, double f,
double g, double g,
double h, double h,
double i, double i,
double j) { double j) {
return i + j; return i + j;
} }
double float_fn(float x, float y, float z) { double float_fn(float x, float y, float z) {

View File

@ -162,9 +162,9 @@ static int is_fp_type(JanetFFIPrimType prim) {
} }
JANET_CORE_FN(cfun_ffi_signature, JANET_CORE_FN(cfun_ffi_signature,
"(native-signature calling-convention ret-type & arg-types)", "(native-signature calling-convention ret-type & arg-types)",
"Create a function signature object that can be used to make calls " "Create a function signature object that can be used to make calls "
"with raw function pointers.") { "with raw function pointers.") {
janet_arity(argc, 2, -1); janet_arity(argc, 2, -1);
uint32_t frame_size = 0; uint32_t frame_size = 0;
uint32_t reg_count = 0; uint32_t reg_count = 0;
@ -214,7 +214,7 @@ JANET_CORE_FN(cfun_ffi_signature,
frame_size += janet_ffi_type_info[ptype].size; frame_size += janet_ffi_type_info[ptype].size;
} }
} }
/* Create signature abstract value */ /* Create signature abstract value */
JanetFFISignature *abst = janet_abstract(&janet_signature_type, sizeof(JanetFFISignature)); JanetFFISignature *abst = janet_abstract(&janet_signature_type, sizeof(JanetFFISignature));
abst->frame_size = frame_size; abst->frame_size = frame_size;
@ -232,7 +232,7 @@ JANET_CORE_FN(cfun_ffi_signature,
} }
static void *janet_ffi_getpointer(const Janet *argv, int32_t n) { static void *janet_ffi_getpointer(const Janet *argv, int32_t n) {
switch(janet_type(argv[n])) { switch (janet_type(argv[n])) {
default: default:
janet_panicf("bad slot #%d, expected pointer convertable type, got %v", argv[n]); janet_panicf("bad slot #%d, expected pointer convertable type, got %v", argv[n]);
case JANET_POINTER: case JANET_POINTER:
@ -343,7 +343,7 @@ static Janet janet_ffi_sysv64(JanetFFISignature *signature, void *function_point
} }
uint64_t *stack = alloca(sizeof(uint64_t) * signature->stack_count); uint64_t *stack = alloca(sizeof(uint64_t) * signature->stack_count);
for (uint32_t i = 0; i < signature->stack_count; i++) { for (uint32_t i = 0; i < signature->stack_count; i++) {
stack[signature->stack_count - 1 - i] = janet_ffi_reg64(argv, signature->stack[i]); stack[signature->stack_count - 1 - i] = janet_ffi_reg64(argv, signature->stack[i]);
} }
/* !!ACHTUNG!! */ /* !!ACHTUNG!! */
@ -383,9 +383,9 @@ static Janet janet_ffi_sysv64(JanetFFISignature *signature, void *function_point
switch (signature->variant) { switch (signature->variant) {
default: default:
/* fallthrough */ /* fallthrough */
case 0: case 0:
__asm__( FFI_ASM_PRELUDE __asm__(FFI_ASM_PRELUDE
"call *%2\n\t" "call *%2\n\t"
"mov %%rax, %0\n\t" "mov %%rax, %0\n\t"
"mov %%rdx, %1" "mov %%rdx, %1"
@ -394,7 +394,7 @@ static Janet janet_ffi_sysv64(JanetFFISignature *signature, void *function_point
: "rax", "rdi", "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11"); : "rax", "rdi", "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11");
return janet_ffi_from64(ret, signature->ret_type); return janet_ffi_from64(ret, signature->ret_type);
case 1: case 1:
__asm__( FFI_ASM_PRELUDE __asm__(FFI_ASM_PRELUDE
"call *%2\n\t" "call *%2\n\t"
"movq %%xmm0, %0\n\t" "movq %%xmm0, %0\n\t"
"movq %%xmm1, %1" "movq %%xmm1, %1"
@ -412,9 +412,9 @@ static Janet janet_ffi_sysv64(JanetFFISignature *signature, void *function_point
} }
JANET_CORE_FN(cfun_ffi_call, JANET_CORE_FN(cfun_ffi_call,
"(native-call pointer signature & args)", "(native-call pointer signature & args)",
"Call a raw pointer as a function pointer. The function signature specifies " "Call a raw pointer as a function pointer. The function signature specifies "
"how Janet values in `args` are converted to native machine types.") { "how Janet values in `args` are converted to native machine types.") {
janet_arity(argc, 2, -1); janet_arity(argc, 2, -1);
void *function_pointer = janet_getpointer(argv, 0); void *function_pointer = janet_getpointer(argv, 0);
JanetFFISignature *signature = janet_getabstract(argv, 1, &janet_signature_type); JanetFFISignature *signature = janet_getabstract(argv, 1, &janet_signature_type);

View File

@ -163,10 +163,13 @@ extern "C" {
#define JANET_DYNAMIC_MODULES #define JANET_DYNAMIC_MODULES
#endif #endif
/* Enable or disable the FFI library. */ /* Enable or disable the FFI library. Currently, FFI only enabled on
* x86-64, non-windows operating systems. */
#ifndef JANET_NO_FFI #ifndef JANET_NO_FFI
#if !defined(JANET_WINDOWS) && (defined(__x86_64__) || defined(_M_X64))
#define JANET_FFI #define JANET_FFI
#endif #endif
#endif
/* Enable or disable the assembler. Enabled by default. */ /* Enable or disable the assembler. Enabled by default. */
#ifndef JANET_NO_ASSEMBLER #ifndef JANET_NO_ASSEMBLER