aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2011-01-06 23:30:04 +0100
committerMike Pall <mike>2011-01-06 23:30:04 +0100
commitfb0a18ca0bbc96aac105d903e1db92d31f1d4d3c (patch)
tree019c7f7f80e2b078ce24db5cae8db0b461ad7810 /src
parent97875c4fa98dcdd0447ae0d4b73b591bfcec6f12 (diff)
downloadluajit-fb0a18ca0bbc96aac105d903e1db92d31f1d4d3c.tar.gz
luajit-fb0a18ca0bbc96aac105d903e1db92d31f1d4d3c.tar.bz2
luajit-fb0a18ca0bbc96aac105d903e1db92d31f1d4d3c.zip
FFI: Add support for calling C functions.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile2
-rw-r--r--src/Makefile.dep24
-rw-r--r--src/buildvm.c3
-rw-r--r--src/buildvm_ppc.dasc9
-rw-r--r--src/buildvm_x86.dasc95
-rw-r--r--src/lib_ffi.c8
-rw-r--r--src/lj_ccall.c349
-rw-r--r--src/lj_ccall.h131
-rw-r--r--src/lj_ctype.h15
-rw-r--r--src/lj_errmsg.h4
-rw-r--r--src/ljamalg.c1
11 files changed, 617 insertions, 24 deletions
diff --git a/src/Makefile b/src/Makefile
index ba94f2fa..b925b4e5 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -328,7 +328,7 @@ LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o \
328 lj_opt_dce.o lj_opt_loop.o \ 328 lj_opt_dce.o lj_opt_loop.o \
329 lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \ 329 lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \
330 lj_asm.o lj_trace.o lj_gdbjit.o \ 330 lj_asm.o lj_trace.o lj_gdbjit.o \
331 lj_ctype.o lj_cdata.o lj_cconv.o lj_cparse.o \ 331 lj_ctype.o lj_cdata.o lj_cconv.o lj_ccall.o lj_cparse.o \
332 lj_lib.o lj_alloc.o lib_aux.o \ 332 lj_lib.o lj_alloc.o lib_aux.o \
333 $(LJLIB_O) lib_init.o 333 $(LJLIB_O) lib_init.o
334 334
diff --git a/src/Makefile.dep b/src/Makefile.dep
index f3a2e710..9b790fb8 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -1,6 +1,6 @@
1buildvm.o: buildvm.c buildvm.h lj_def.h lua.h luaconf.h lj_arch.h \ 1buildvm.o: buildvm.c buildvm.h lj_def.h lua.h luaconf.h lj_arch.h \
2 lj_obj.h lj_gc.h lj_bc.h lj_ir.h lj_frame.h lj_dispatch.h lj_jit.h \ 2 lj_obj.h lj_gc.h lj_bc.h lj_ir.h lj_frame.h lj_dispatch.h lj_jit.h \
3 luajit.h \ 3 lj_ccall.h luajit.h \
4 lj_traceerr.h 4 lj_traceerr.h
5buildvm_asm.o: buildvm_asm.c buildvm.h lj_def.h lua.h luaconf.h lj_arch.h \ 5buildvm_asm.o: buildvm_asm.c buildvm.h lj_def.h lua.h luaconf.h lj_arch.h \
6 lj_bc.h 6 lj_bc.h
@@ -22,7 +22,7 @@ lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
22 lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h lj_libdef.h 22 lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h lj_libdef.h
23lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ 23lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
24 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h \ 24 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h \
25 lj_cdata.h lj_cconv.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h 25 lj_cdata.h lj_cconv.h lj_ccall.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h
26lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h 26lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h
27lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ 27lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
28 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ff.h lj_ffdef.h \ 28 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ff.h lj_ffdef.h \
@@ -55,6 +55,9 @@ lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
55 lj_asm.h lj_vm.h lj_target.h lj_target_*.h 55 lj_asm.h lj_vm.h lj_target.h lj_target_*.h
56lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \ 56lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \
57 lj_bcdef.h 57 lj_bcdef.h
58lj_ccall.o: lj_ccall.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
59 lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cconv.h lj_cdata.h \
60 lj_ccall.h
58lj_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 61lj_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
59 lj_err.h lj_errmsg.h lj_tab.h lj_ctype.h lj_gc.h lj_cdata.h lj_cconv.h 62 lj_err.h lj_errmsg.h lj_tab.h lj_ctype.h lj_gc.h lj_cdata.h lj_cconv.h
60lj_cdata.o: lj_cdata.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 63lj_cdata.o: lj_cdata.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
@@ -153,12 +156,13 @@ ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \
153 lj_func.c lj_udata.c lj_meta.c lj_state.c lj_lex.h lj_alloc.h \ 156 lj_func.c lj_udata.c lj_meta.c lj_state.c lj_lex.h lj_alloc.h \
154 lj_dispatch.c lj_ff.h lj_ffdef.h luajit.h lj_vmevent.c lj_vmevent.h \ 157 lj_dispatch.c lj_ff.h lj_ffdef.h luajit.h lj_vmevent.c lj_vmevent.h \
155 lj_api.c lj_parse.h lj_lex.c lualib.h lj_parse.c lj_ctype.c lj_cdata.c \ 158 lj_api.c lj_parse.h lj_lex.c lualib.h lj_parse.c lj_ctype.c lj_cdata.c \
156 lj_cconv.h lj_cconv.c lj_cparse.c lj_cparse.h lj_lib.c lj_lib.h lj_ir.c \ 159 lj_cconv.h lj_cconv.c lj_ccall.c lj_ccall.h lj_cparse.c lj_cparse.h \
157 lj_iropt.h lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c \ 160 lj_lib.c lj_lib.h lj_ir.c lj_iropt.h lj_opt_mem.c lj_opt_fold.c \
158 lj_opt_dce.c lj_opt_loop.c lj_snap.h lj_mcode.c lj_mcode.h lj_snap.c \ 161 lj_folddef.h lj_opt_narrow.c lj_opt_dce.c lj_opt_loop.c lj_snap.h \
159 lj_target.h lj_target_*.h lj_record.c lj_record.h lj_ffrecord.h \ 162 lj_mcode.c lj_mcode.h lj_snap.c lj_target.h lj_target_*.h lj_record.c \
160 lj_crecord.c lj_crecord.h lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h \ 163 lj_record.h lj_ffrecord.h lj_crecord.c lj_crecord.h lj_ffrecord.c \
161 lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c lib_base.c \ 164 lj_recdef.h lj_asm.c lj_asm.h lj_trace.c lj_gdbjit.h lj_gdbjit.c \
162 lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c lib_os.c \ 165 lj_alloc.c lib_aux.c lib_base.c lj_libdef.h lib_math.c lib_string.c \
163 lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_ffi.c lib_init.c 166 lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_bit.c \
167 lib_jit.c lib_ffi.c lib_init.c
164luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h 168luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h
diff --git a/src/buildvm.c b/src/buildvm.c
index 8c127d45..ddf4e0f0 100644
--- a/src/buildvm.c
+++ b/src/buildvm.c
@@ -21,6 +21,9 @@
21#include "lj_ir.h" 21#include "lj_ir.h"
22#include "lj_frame.h" 22#include "lj_frame.h"
23#include "lj_dispatch.h" 23#include "lj_dispatch.h"
24#if LJ_HASFFI
25#include "lj_ccall.h"
26#endif
24#include "luajit.h" 27#include "luajit.h"
25 28
26#if defined(_WIN32) 29#if defined(_WIN32)
diff --git a/src/buildvm_ppc.dasc b/src/buildvm_ppc.dasc
index a81335db..22f6da55 100644
--- a/src/buildvm_ppc.dasc
+++ b/src/buildvm_ppc.dasc
@@ -2074,6 +2074,15 @@ static void build_subroutines(BuildCtx *ctx)
2074 |//----------------------------------------------------------------------- 2074 |//-----------------------------------------------------------------------
2075 | 2075 |
2076 |//----------------------------------------------------------------------- 2076 |//-----------------------------------------------------------------------
2077 |//-- FFI helper functions -----------------------------------------------
2078 |//-----------------------------------------------------------------------
2079 |
2080 |->vm_ffi_call:
2081#if LJ_HASFFI
2082 | NYI
2083#endif
2084 |
2085 |//-----------------------------------------------------------------------
2077} 2086}
2078 2087
2079/* Generate the code for a single instruction. */ 2088/* Generate the code for a single instruction. */
diff --git a/src/buildvm_x86.dasc b/src/buildvm_x86.dasc
index b4f34782..31a09b7e 100644
--- a/src/buildvm_x86.dasc
+++ b/src/buildvm_x86.dasc
@@ -3409,6 +3409,101 @@ static void build_subroutines(BuildCtx *ctx, int cmov, int sse)
3409 |.endif 3409 |.endif
3410 | 3410 |
3411 |//----------------------------------------------------------------------- 3411 |//-----------------------------------------------------------------------
3412 |//-- FFI helper functions -----------------------------------------------
3413 |//-----------------------------------------------------------------------
3414 |
3415 |->vm_ffi_call:
3416#if LJ_HASFFI
3417 |.if X64
3418 | .type CCSTATE, CCallState, rbx
3419 | push rbp; mov rbp, rsp; push rbx; mov CCSTATE, CARG1
3420 |.else
3421 | .type CCSTATE, CCallState, ebx
3422 | push ebp; mov ebp, esp; push ebx; mov CCSTATE, FCARG1
3423 |.endif
3424 |
3425 | // Readjust stack.
3426 |.if X64
3427 | mov eax, CCSTATE->spadj
3428 | sub rsp, rax
3429 |.else
3430 | sub esp, CCSTATE->spadj
3431 |.endif
3432 |
3433 | // Copy stack slots.
3434 | movzx ecx, byte CCSTATE->nsp
3435 | sub ecx, 1
3436 | js >2
3437 |1:
3438 |.if X64
3439 | mov rax, [CCSTATE+rcx*8+offsetof(CCallState, stack)]
3440 | mov [rsp+rcx*8+CCALL_SPS_EXTRA*8], rax
3441 |.else
3442 | mov eax, [CCSTATE+ecx*4+offsetof(CCallState, stack)]
3443 | mov [esp+ecx*4], eax
3444 |.endif
3445 | sub ecx, 1
3446 | jns <1
3447 |2:
3448 |
3449 |.if X64
3450 | movzx eax, byte CCSTATE->nfpr
3451 | mov CARG1, CCSTATE->gpr[0]
3452 | mov CARG2, CCSTATE->gpr[1]
3453 | mov CARG3, CCSTATE->gpr[2]
3454 | mov CARG4, CCSTATE->gpr[3]
3455 |.if not X64WIN
3456 | mov CARG5, CCSTATE->gpr[4]
3457 | mov CARG6, CCSTATE->gpr[5]
3458 |.endif
3459 | test eax, eax; jz >5
3460 | movaps xmm0, CCSTATE->fpr[0]
3461 | movaps xmm1, CCSTATE->fpr[1]
3462 | movaps xmm2, CCSTATE->fpr[2]
3463 | movaps xmm3, CCSTATE->fpr[3]
3464 |.if not X64WIN
3465 | cmp eax, 4; jbe >5
3466 | movaps xmm4, CCSTATE->fpr[4]
3467 | movaps xmm5, CCSTATE->fpr[5]
3468 | movaps xmm6, CCSTATE->fpr[6]
3469 | movaps xmm7, CCSTATE->fpr[7]
3470 |.endif
3471 |5:
3472 |.else
3473 | mov FCARG1, CCSTATE->gpr[0]
3474 | mov FCARG2, CCSTATE->gpr[1]
3475 |.endif
3476 |
3477 | call aword CCSTATE->func
3478 |
3479 |.if X64
3480 | mov CCSTATE->gpr[0], rax
3481 | movaps CCSTATE->fpr[0], xmm0
3482 |.if not X64WIN
3483 | mov CCSTATE->gpr[1], rdx
3484 | movaps CCSTATE->fpr[1], xmm1
3485 |.endif
3486 |.else
3487 | mov CCSTATE->gpr[0], eax
3488 | mov CCSTATE->gpr[1], edx
3489 | cmp byte CCSTATE->resx87, 1
3490 | jb >7
3491 | je >6
3492 | fstp qword CCSTATE->fpr[0].d[0]
3493 | jmp >7
3494 |6:
3495 | fstp dword CCSTATE->fpr[0].f[0]
3496 |7:
3497 |.endif
3498 |
3499 |.if X64
3500 | mov rbx, [rbp-8]; leave; ret
3501 |.else
3502 | mov ebx, [ebp-4]; leave; ret
3503 |.endif
3504#endif
3505 |
3506 |//-----------------------------------------------------------------------
3412} 3507}
3413 3508
3414/* Generate the code for a single instruction. */ 3509/* Generate the code for a single instruction. */
diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index 18a22ce1..c80f3fb6 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -21,6 +21,7 @@
21#include "lj_cparse.h" 21#include "lj_cparse.h"
22#include "lj_cdata.h" 22#include "lj_cdata.h"
23#include "lj_cconv.h" 23#include "lj_cconv.h"
24#include "lj_ccall.h"
24#include "lj_ff.h" 25#include "lj_ff.h"
25#include "lj_lib.h" 26#include "lj_lib.h"
26 27
@@ -148,10 +149,13 @@ static int lj_cf_ffi_new(lua_State *L);
148LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call) 149LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)
149{ 150{
150 GCcdata *cd = ffi_checkcdata(L, 1); 151 GCcdata *cd = ffi_checkcdata(L, 1);
152 int ret;
151 if (cd->typeid == CTID_CTYPEID) 153 if (cd->typeid == CTID_CTYPEID)
152 return lj_cf_ffi_new(L); 154 return lj_cf_ffi_new(L);
153 lj_err_caller(L, LJ_ERR_FFI_NYICALL); 155 if ((ret = lj_ccall_func(L, cd)) < 0)
154 return 0; /* unreachable */ 156 lj_err_callerv(L, LJ_ERR_FFI_BADCALL,
157 strdata(lj_ctype_repr(L, cd->typeid, NULL)));
158 return ret;
155} 159}
156 160
157/* Pointer arithmetic. */ 161/* Pointer arithmetic. */
diff --git a/src/lj_ccall.c b/src/lj_ccall.c
new file mode 100644
index 00000000..5e91d962
--- /dev/null
+++ b/src/lj_ccall.c
@@ -0,0 +1,349 @@
1/*
2** FFI C call handling.
3** Copyright (C) 2005-2010 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#include "lj_obj.h"
7
8#if LJ_HASFFI
9
10#include "lj_err.h"
11#include "lj_str.h"
12#include "lj_ctype.h"
13#include "lj_cconv.h"
14#include "lj_cdata.h"
15#include "lj_ccall.h"
16
17/* Target-specific handling of register arguments. */
18#if LJ_TARGET_X86
19
20#define CCALL_HANDLE_REGARG \
21 if (!isfp) { /* Only non-FP values may be passed in registers. */ \
22 if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \
23 ngpr = maxgpr; /* Prevent reordering. */ \
24 } else if (ngpr + 1 <= maxgpr) { \
25 dp = &cc->gpr[ngpr]; \
26 ngpr += n; \
27 goto done; \
28 } \
29 }
30
31#elif LJ_TARGET_X64 && LJ_ABI_WIN
32
33/* Windows/x64 argument registers are strictly positional (use ngpr). */
34#define CCALL_HANDLE_REGARG \
35 if (isfp) { \
36 if (ngpr < 4) { dp = &cc->fpr[ngpr++]; nfpr = ngpr; goto done; } \
37 } else { \
38 if (ngpr < 4) { dp = &cc->gpr[ngpr++]; goto done; } \
39 }
40
41#elif LJ_TARGET_X64
42
43#define CCALL_HANDLE_REGARG \
44 if (isfp) { /* Try to pass argument in FPRs. */ \
45 if (nfpr + isfp <= CCALL_NARG_FPR) { \
46 dp = &cc->fpr[nfpr]; \
47 nfpr += isfp; \
48 goto done; \
49 } \
50 } else { /* Try to pass argument in GPRs. */ \
51 /* Note that reordering is explicitly allowed in the x64 ABI. */ \
52 if (n <= 2 && ngpr + n <= maxgpr) { \
53 dp = &cc->gpr[ngpr]; \
54 ngpr += n; \
55 goto done; \
56 } \
57 }
58
59#elif LJ_TARGET_PPCSPE
60
61/* PPC/SPE has a softfp ABI. */
62#define CCALL_HANDLE_REGARG \
63 if (n > 1) { /* Doesn't fit in a single GPR? */ \
64 lua_assert(n == 2 || n == 4); /* int64_t, double or complex (float). */ \
65 if (n == 2) \
66 ngpr = (ngpr + 1u) & ~1u; /* Only align 64 bit value to regpair. */ \
67 else if (ngpr + n > maxgpr) \
68 ngpr = maxgpr; /* Prevent reordering. */ \
69 } \
70 if (ngpr + n <= maxgpr) { \
71 dp = &cc->gpr[ngpr]; \
72 ngpr += n; \
73 goto done; \
74 }
75
76#else
77#error "missing definition for handling of register arguments"
78#endif
79
80/* Infer the destination CTypeID for a vararg argument. */
81static CTypeID ccall_ctid_vararg(CTState *cts, cTValue *o)
82{
83 if (tvisnum(o)) {
84 return CTID_DOUBLE;
85 } else if (tviscdata(o)) {
86 CTypeID id = cdataV(o)->typeid;
87 CType *s = ctype_get(cts, id);
88 if (ctype_isrefarray(s->info)) {
89 return lj_ctype_intern(cts,
90 CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(s->info)), CTSIZE_PTR);
91 } else if (ctype_isstruct(s->info) || ctype_isfunc(s->info)) {
92 return lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR);
93 } if (ctype_isfp(s->info) && s->size == sizeof(float)) {
94 return CTID_DOUBLE;
95 } else {
96 return id;
97 }
98 } else if (tvisstr(o)) {
99 return CTID_P_CCHAR;
100 } else if (tvisbool(o)) {
101 return CTID_BOOL;
102 } else {
103 return CTID_P_VOID;
104 }
105}
106
107/* Setup arguments for C call. */
108static void ccall_set_args(lua_State *L, CTState *cts, CType *ct,
109 CCallState *cc)
110{
111 TValue *o, *top = L->top;
112 CTypeID fid;
113 CType *ctr;
114 MSize maxgpr, ngpr = 0, nsp = 0;
115#if CCALL_NARG_FPR
116 MSize nfpr = 0;
117#endif
118
119 /* Clear unused regs to get some determinism in case of misdeclaration. */
120 memset(cc->gpr, 0, sizeof(cc->gpr));
121#if CCALL_NUM_FPR
122 memset(cc->fpr, 0, sizeof(cc->fpr));
123#endif
124
125#if LJ_TARGET_X86
126 /* x86 has several different calling conventions. */
127 cc->resx87 = 0;
128 switch ((ct->info >> CTSHIFT_CCONV) & CTMASK_CCONV) {
129 case CTCC_FASTCALL: maxgpr = 2; break;
130 case CTCC_THISCALL: maxgpr = 1; break;
131 default: maxgpr = 0; break;
132 }
133#else
134 maxgpr = CCALL_NARG_GPR;
135#endif
136
137 /* Perform required setup for some result types. */
138 ctr = ctype_rawchild(cts, ct);
139 if (ctype_isvector(ctr->info)) {
140 if (!(CCALL_VECTOR_REG && (ctr->size == 8 || ctr->size == 16)))
141 goto err_nyi;
142 } else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) {
143 /* Preallocate cdata object and anchor it after arguments. */
144 CTSize sz = ctr->size;
145 GCcdata *cd = lj_cdata_new(cts, ctype_cid(ct->info), sz);
146 setcdataV(L, L->top++, cd);
147 if (ctype_iscomplex(ctr->info)) {
148 cc->retref = (sz == 2*sizeof(float)) ? CCALL_COMPLEXF_RETREF :
149 CCALL_COMPLEX_RETREF;
150 } else {
151#if CCALL_STRUCT_RETREF
152 cc->retref = 1; /* Return all structs by reference. */
153#elif LJ_TARGET_X64
154#if LJ_ABI_WIN
155 /* Return structs of size 1, 2, 4 or 8 in a GPR. */
156 cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8);
157#else
158 if (sz <= 16) goto err_nyi; /* NYI: crazy x64 rules for structs. */
159 cc->retref = 1; /* Return all bigger structs by reference. */
160#endif
161#else
162#error "missing definition for handling of struct return values"
163#endif
164 }
165 /* Pass reference to returned aggregate in first argument. */
166 if (cc->retref) {
167 if (ngpr < maxgpr)
168 cc->gpr[ngpr++] = (GPRArg)cdataptr(cd);
169 else
170 cc->stack[nsp++] = (GPRArg)cdataptr(cd);
171 }
172#if LJ_TARGET_X86
173 } else if (ctype_isfp(ctr->info)) {
174 cc->resx87 = ctr->size == sizeof(float) ? 1 : 2;
175#endif
176 }
177
178 /* Walk through all passed arguments. */
179 for (fid = ct->sib, o = L->base+1; o < top; o++) {
180 CTypeID did;
181 CType *d;
182 CTSize sz;
183 MSize n, isfp = 0, isva = 0;
184 void *dp, *rp = NULL;
185
186 if (fid) { /* Get argument type from field. */
187 CType *ctf = ctype_get(cts, fid);
188 fid = ctf->sib;
189 lua_assert(ctype_isfield(ctf->info));
190 did = ctype_cid(ctf->info);
191 } else {
192 if (!(ct->info & CTF_VARARG))
193 lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too many arguments. */
194 did = ccall_ctid_vararg(cts, o); /* Infer vararg type. */
195 isva = 1;
196 }
197 d = ctype_raw(cts, did);
198 sz = d->size;
199
200 /* Find out how (by value/ref) and where (GPR/FPR) to pass an argument. */
201 if (ctype_isnum(d->info)) {
202 if (sz > 8) goto err_nyi;
203 if ((d->info & CTF_FP)) {
204 isfp = 1;
205 } else if (sz < CTSIZE_PTR) {
206 d = ctype_get(cts, CTID_INT_PSZ);
207 }
208 } else if (ctype_isvector(d->info)) {
209 if (CCALL_VECTOR_REG && (sz == 8 || sz == 16))
210 isfp = 1;
211 else
212 goto err_nyi;
213 } else if (ctype_iscomplex(d->info)) {
214#if CCALL_COMPLEX_ARGREF
215 rp = cdataptr(lj_cdata_new(cts, did, sz));
216 sz = CTSIZE_PTR;
217#else
218 isfp = 2;
219#endif
220 } else if (ctype_isstruct(d->info)) {
221 int sref = CCALL_STRUCT_ARGREF;
222#if LJ_TARGET_X86
223 ngpr = maxgpr; /* Pass all structs by value on the stack. */
224#elif LJ_TARGET_X64
225#if LJ_ABI_WIN
226 /* Pass structs of size 1, 2, 4 or 8 in a GPR by value. */
227 sref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8);
228#else
229 if (sz <= 16) goto err_nyi; /* NYI: crazy x64 rules for structs. */
230 /* Pass all bigger structs by value on the stack. */
231#endif
232#endif
233 if (sref) { /* Pass struct by reference. */
234 rp = cdataptr(lj_cdata_new(cts, did, sz));
235 sz = CTSIZE_PTR; /* Pass all other structs by reference. */
236 }
237 } else {
238 sz = CTSIZE_PTR;
239 }
240 sz = (sz + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);
241 n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */
242
243 CCALL_HANDLE_REGARG /* Handle register arguments. */
244
245 /* Otherwise pass argument on stack. */
246 if (CCALL_ALIGN_STACKARG && !rp && (d->info & CTF_ALIGN) > CTALIGN_PTR) {
247 MSize align = (1u << ctype_align(d->info-CTALIGN_PTR)) -1;
248 nsp = (nsp + align) & ~align; /* Align argument on stack. */
249 }
250 if (nsp + n >= CCALL_MAXSTACK) { /* Too many arguments. */
251 err_nyi:
252 lj_err_caller(L, LJ_ERR_FFI_NYICALL);
253 }
254 dp = &cc->stack[nsp];
255 nsp += n;
256 isva = 0;
257
258 done:
259 if (rp) { /* Pass by reference. */
260 *(void **)dp = rp;
261 dp = rp;
262 }
263 lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, 0);
264#if LJ_TARGET_X64 && LJ_ABI_WIN
265 if (isva) { /* Windows/x64 mirrors varargs in both register sets. */
266 if (nfpr == ngpr)
267 cc->gpr[ngpr-1] = cc->fpr[ngpr-1].l[0];
268 else
269 cc->fpr[ngpr-1].l[0] = cc->gpr[ngpr-1];
270 }
271#endif
272 }
273 if (fid) lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too few arguments. */
274
275#if LJ_TARGET_X64
276 cc->nfpr = nfpr; /* Required for vararg functions. */
277#endif
278 cc->nsp = nsp;
279 cc->spadj = (CCALL_SPS_FREE + CCALL_SPS_EXTRA)*CTSIZE_PTR;
280 if (nsp > CCALL_SPS_FREE)
281 cc->spadj += (((nsp-CCALL_SPS_FREE)*CTSIZE_PTR + 15u) & ~15u);
282}
283
284/* Get results from C call. */
285static int ccall_get_results(lua_State *L, CTState *cts, CType *ct,
286 CCallState *cc)
287{
288 CType *ctr = ctype_rawchild(cts, ct);
289 void *sp = &cc->gpr[0];
290 if (ctype_isvoid(ctr->info))
291 return 0; /* Zero results. */
292 if (ctype_isstruct(ctr->info)) {
293 if (!CCALL_STRUCT_RETREF && !cc->retref) {
294 void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */
295 memcpy(dp, sp, ctr->size); /* Copy struct return value from GPRs. */
296 }
297 return 1; /* Return cdata object which is already on top of stack. */
298 }
299 if (ctype_iscomplex(ctr->info)) {
300#if !CCALL_COMPLEX_RETREF || !CCALL_COMPLEXF_RETREF
301 void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */
302#if CCALL_COMPLEX_RETREF && !CCALL_COMPLEXF_RETREF
303 if (ctr->size == 2*sizeof(float))
304 memcpy(dp, sp, ctr->size); /* Copy complex float from GPRs. */
305#elif CCALL_NUM_FPR
306 /* Copy non-contiguous re/im part from FPRs to cdata object. */
307 if (ctr->size == 2*sizeof(float)) {
308 ((float *)dp)[0] = cc->fpr[0].f[0];
309 ((float *)dp)[1] = cc->fpr[1].f[0];
310 } else {
311 ((double *)dp)[0] = cc->fpr[0].d[0];
312 ((double *)dp)[1] = cc->fpr[1].d[0];
313 }
314#else
315 memcpy(dp, sp, ctr->size); /* Copy complex from GPRs. */
316#endif
317#endif
318 return 1; /* Return cdata object which is already on top of stack. */
319 }
320#if CCALL_NUM_FPR
321 if (ctype_isfp(ctr->info) || ctype_isvector(ctr->info))
322 sp = &cc->fpr[0];
323#endif
324 /* No reference types end up here, so there's no need for the CTypeID. */
325 lua_assert(!(ctype_isrefarray(ctr->info) || ctype_isstruct(ctr->info)));
326 lj_cconv_tv_ct(cts, ctr, 0, L->top-1, (uint8_t *)sp);
327 return 1; /* One result. */
328}
329
330/* Call C function. */
331int lj_ccall_func(lua_State *L, GCcdata *cd)
332{
333 CTState *cts = ctype_cts(L);
334 CType *ct = ctype_raw(cts, cd->typeid);
335 CTSize sz = ct->size;
336 void *p = cdataptr(cd);
337 if (ctype_isptr(ct->info))
338 ct = ctype_rawchild(cts, ct);
339 if (ctype_isfunc(ct->info)) {
340 CCallState cc;
341 cc.func = (void (*)(void))cdata_getptr(p, sz);
342 ccall_set_args(L, cts, ct, &cc);
343 lj_vm_ffi_call(&cc);
344 return ccall_get_results(L, cts, ct, &cc);
345 }
346 return -1; /* Not a function. */
347}
348
349#endif
diff --git a/src/lj_ccall.h b/src/lj_ccall.h
new file mode 100644
index 00000000..a0737d04
--- /dev/null
+++ b/src/lj_ccall.h
@@ -0,0 +1,131 @@
1/*
2** FFI C call handling.
3** Copyright (C) 2005-2010 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#ifndef _LJ_CCALL_H
7#define _LJ_CCALL_H
8
9#include "lj_obj.h"
10
11#if LJ_HASFFI
12
13/* -- C calling conventions ----------------------------------------------- */
14
15#if LJ_TARGET_X86ORX64
16
17#if LJ_TARGET_X86
18#define CCALL_NARG_GPR 2 /* For fastcall arguments. */
19#define CCALL_NARG_FPR 0
20#define CCALL_NRET_GPR 2
21#define CCALL_NRET_FPR 1 /* For FP results on x87 stack. */
22#define CCALL_STRUCT_RETREF 1 /* Return structs by reference. */
23#define CCALL_COMPLEX_RETREF 1 /* Return complex by reference. */
24#define CCALL_COMPLEXF_RETREF 0 /* Return complex float by value. */
25#define CCALL_ALIGN_STACKARG 0 /* Don't align argument on stack. */
26#elif LJ_ABI_WIN
27#define CCALL_NARG_GPR 4
28#define CCALL_NARG_FPR 4
29#define CCALL_NRET_GPR 1
30#define CCALL_NRET_FPR 1
31#define CCALL_SPS_EXTRA 4
32#define CCALL_COMPLEX_ARGREF 1 /* Pass complex by reference. */
33#define CCALL_COMPLEX_RETREF 1 /* Return complex by reference. */
34#define CCALL_COMPLEXF_RETREF 0 /* Return complex float by value. */
35#else
36#define CCALL_NARG_GPR 6
37#define CCALL_NARG_FPR 8
38#define CCALL_NRET_GPR 2
39#define CCALL_NRET_FPR 2
40#define CCALL_VECTOR_REG 1 /* Pass vectors in registers. */
41#endif
42
43#define CCALL_SPS_FREE 1
44
45typedef LJ_ALIGN(16) union FPRArg {
46 double d[2];
47 float f[4];
48 uint8_t b[16];
49 uint16_t s[8];
50 int i[4];
51 int64_t l[2];
52} FPRArg;
53
54typedef intptr_t GPRArg;
55
56#elif LJ_TARGET_PPCSPE
57
58#define CCALL_NARG_GPR 8
59#define CCALL_NARG_FPR 0
60#define CCALL_NRET_GPR 4 /* For softfp complex double. */
61#define CCALL_NRET_FPR 0
62#define CCALL_SPS_FREE 0 /* NYI */
63#define CCALL_STRUCT_ARGREF 1 /* Pass structs by reference. */
64#define CCALL_STRUCT_RETREF 1 /* Return structs by reference. */
65
66typedef intptr_t GPRArg;
67
68#else
69#error "missing calling convention definitions for this architecture"
70#endif
71
72#ifndef CCALL_SPS_EXTRA
73#define CCALL_SPS_EXTRA 0
74#endif
75#ifndef CCALL_STRUCT_ARGREF
76#define CCALL_STRUCT_ARGREF 0
77#endif
78#ifndef CCALL_STRUCT_RETREF
79#define CCALL_STRUCT_RETREF 0
80#endif
81#ifndef CCALL_COMPLEX_ARGREF
82#define CCALL_COMPLEX_ARGREF 0
83#endif
84#ifndef CCALL_COMPLEX_RETREF
85#define CCALL_COMPLEX_RETREF 0
86#endif
87#ifndef CCALL_COMPLEXF_RETREF
88#define CCALL_COMPLEXF_RETREF CCALL_COMPLEX_RETREF
89#endif
90#ifndef CCALL_VECTOR_REG
91#define CCALL_VECTOR_REG 0
92#endif
93#ifndef CCALL_ALIGN_STACKARG
94#define CCALL_ALIGN_STACKARG 1
95#endif
96
97#define CCALL_NUM_GPR \
98 (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR)
99#define CCALL_NUM_FPR \
100 (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR)
101
102#define CCALL_MAXSTACK 32
103
104/* -- C call state -------------------------------------------------------- */
105
106typedef struct CCallState {
107 void (*func)(void); /* Pointer to called function. */
108 uint32_t spadj; /* Stack pointer adjustment. */
109 uint8_t nsp; /* Number of stack slots. */
110 uint8_t retref; /* Return value by reference. */
111#if LJ_TARGET_X64
112 uint8_t nfpr; /* Number of arguments in FPRs. */
113#elif LJ_TARGET_X86
114 uint8_t resx87; /* Result on x87 stack: 1:float, 2:double. */
115#endif
116 GPRArg gpr[CCALL_NUM_GPR]; /* Arguments/results in GPRs. */
117#if CCALL_NUM_FPR
118 FPRArg fpr[CCALL_NUM_FPR]; /* Arguments/results in FPRs. */
119#endif
120 GPRArg stack[CCALL_MAXSTACK]; /* Stack slots. */
121} CCallState;
122
123/* -- C call handling ----------------------------------------------------- */
124
125/* Really belongs to lj_vm.h. */
126LJ_ASMF void LJ_FASTCALL lj_vm_ffi_call(CCallState *cc);
127LJ_FUNC int lj_ccall_func(lua_State *L, GCcdata *cd);
128
129#endif
130
131#endif
diff --git a/src/lj_ctype.h b/src/lj_ctype.h
index 826f9785..437f9ae8 100644
--- a/src/lj_ctype.h
+++ b/src/lj_ctype.h
@@ -35,6 +35,9 @@ enum {
35 CT_KW /* Keyword. */ 35 CT_KW /* Keyword. */
36}; 36};
37 37
38LJ_STATIC_ASSERT(((int)CT_PTR & (int)CT_ARRAY) == CT_PTR);
39LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT);
40
38/* 41/*
39** ---------- info ------------ 42** ---------- info ------------
40** |type flags... A cid | size | sib | next | name | 43** |type flags... A cid | size | sib | next | name |
@@ -193,9 +196,8 @@ typedef struct CTState {
193 (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0)) 196 (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0))
194#define ctype_isbool(info) \ 197#define ctype_isbool(info) \
195 (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL)) 198 (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL))
196#define ctype_isconstchar(ct) \ 199#define ctype_isfp(info) \
197 (((ct)->info & (CTMASK_NUM|CTF_CONST|CTF_BOOL|CTF_FP)) == \ 200 (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, CTF_FP))
198 CTINFO(CT_NUM, CTF_CONST) && (ct)->size == 1)
199 201
200#define ctype_ispointer(info) \ 202#define ctype_ispointer(info) \
201 ((ctype_type(info) >> 1) == (CT_PTR >> 1)) /* Pointer or array. */ 203 ((ctype_type(info) >> 1) == (CT_PTR >> 1)) /* Pointer or array. */
@@ -204,8 +206,6 @@ typedef struct CTState {
204 206
205#define ctype_isrefarray(info) \ 207#define ctype_isrefarray(info) \
206 (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0)) 208 (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0))
207#define ctype_isvalarray(info) \
208 (ctype_isarray(info) && (info & (CTF_VECTOR|CTF_COMPLEX)))
209#define ctype_isvector(info) \ 209#define ctype_isvector(info) \
210 (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR)) 210 (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR))
211#define ctype_iscomplex(info) \ 211#define ctype_iscomplex(info) \
@@ -216,16 +216,11 @@ typedef struct CTState {
216 CTINFO(CT_STRUCT, CTF_VLA)) /* VL array or VL struct. */ 216 CTINFO(CT_STRUCT, CTF_VLA)) /* VL array or VL struct. */
217#define ctype_isvlarray(info) \ 217#define ctype_isvlarray(info) \
218 (((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_ARRAY, CTF_VLA)) 218 (((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_ARRAY, CTF_VLA))
219#define ctype_isvlstruct(info) \
220 (((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_STRUCT, CTF_VLA))
221 219
222#define ctype_isxattrib(info, at) \ 220#define ctype_isxattrib(info, at) \
223 (((info) & (CTMASK_NUM|CTATTRIB(CTMASK_ATTRIB))) == \ 221 (((info) & (CTMASK_NUM|CTATTRIB(CTMASK_ATTRIB))) == \
224 CTINFO(CT_ATTRIB, CTATTRIB(at))) 222 CTINFO(CT_ATTRIB, CTATTRIB(at)))
225 223
226LJ_STATIC_ASSERT(((int)CT_PTR & (int)CT_ARRAY) == CT_PTR);
227LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT);
228
229/* Target-dependent sizes and alignments. */ 224/* Target-dependent sizes and alignments. */
230#if LJ_64 225#if LJ_64
231#define CTSIZE_PTR 8 226#define CTSIZE_PTR 8
diff --git a/src/lj_errmsg.h b/src/lj_errmsg.h
index 66062747..ae784bc9 100644
--- a/src/lj_errmsg.h
+++ b/src/lj_errmsg.h
@@ -148,11 +148,13 @@ ERRDEF(FFI_REDEF, "attempt to redefine " LUA_QS)
148ERRDEF(FFI_INITOV, "too many initializers for " LUA_QS) 148ERRDEF(FFI_INITOV, "too many initializers for " LUA_QS)
149ERRDEF(FFI_BADCONV, "cannot convert " LUA_QS " to " LUA_QS) 149ERRDEF(FFI_BADCONV, "cannot convert " LUA_QS " to " LUA_QS)
150ERRDEF(FFI_BADARITH, "attempt to perform arithmetic on " LUA_QS " and " LUA_QS) 150ERRDEF(FFI_BADARITH, "attempt to perform arithmetic on " LUA_QS " and " LUA_QS)
151ERRDEF(FFI_BADCALL, LUA_QS " is not callable")
152ERRDEF(FFI_NUMARG, "wrong number of arguments for function call")
151ERRDEF(FFI_BADMEMBER, LUA_QS " has no member named " LUA_QS) 153ERRDEF(FFI_BADMEMBER, LUA_QS " has no member named " LUA_QS)
152ERRDEF(FFI_BADIDX, LUA_QS " cannot be indexed") 154ERRDEF(FFI_BADIDX, LUA_QS " cannot be indexed")
153ERRDEF(FFI_WRCONST, "attempt to write to constant location") 155ERRDEF(FFI_WRCONST, "attempt to write to constant location")
154ERRDEF(FFI_NYIPACKBIT, "NYI: packed bit fields") 156ERRDEF(FFI_NYIPACKBIT, "NYI: packed bit fields")
155ERRDEF(FFI_NYICALL, "NYI: C function calls") 157ERRDEF(FFI_NYICALL, "NYI: cannot call this C function (yet)")
156#endif 158#endif
157 159
158#undef ERRDEF 160#undef ERRDEF
diff --git a/src/ljamalg.c b/src/ljamalg.c
index edcae247..56521752 100644
--- a/src/ljamalg.c
+++ b/src/ljamalg.c
@@ -43,6 +43,7 @@
43#include "lj_ctype.c" 43#include "lj_ctype.c"
44#include "lj_cdata.c" 44#include "lj_cdata.c"
45#include "lj_cconv.c" 45#include "lj_cconv.c"
46#include "lj_ccall.c"
46#include "lj_cparse.c" 47#include "lj_cparse.c"
47#include "lj_lib.c" 48#include "lj_lib.c"
48#include "lj_ir.c" 49#include "lj_ir.c"