aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Makefile.dep12
-rw-r--r--src/lib_ffi.c4
-rw-r--r--src/lj_cconv.c4
-rw-r--r--src/lj_cconv.h1
-rw-r--r--src/lj_crecord.c133
-rw-r--r--src/lj_crecord.h4
6 files changed, 142 insertions, 16 deletions
diff --git a/src/Makefile.dep b/src/Makefile.dep
index fd90ff4a..6a5d208a 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -50,9 +50,9 @@ lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
50 lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h \ 50 lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h \
51 lj_traceerr.h lj_vm.h lj_lex.h lj_parse.h 51 lj_traceerr.h lj_vm.h lj_lex.h lj_parse.h
52lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ 52lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
53 lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h \ 53 lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h lj_ir.h lj_jit.h \
54 lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h lj_asm.h \ 54 lj_iropt.h lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h \
55 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_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 58lj_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
@@ -64,9 +64,9 @@ lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
64 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \ 64 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \
65 lj_bc.h lj_vm.h lj_char.h 65 lj_bc.h lj_vm.h lj_char.h
66lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 66lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
67 lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cconv.h lj_ir.h \ 67 lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cparse.h lj_cconv.h \
68 lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h lj_bc.h lj_traceerr.h \ 68 lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h lj_bc.h \
69 lj_ffrecord.h lj_crecord.h 69 lj_traceerr.h lj_ffrecord.h lj_crecord.h
70lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 70lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
71 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h 71 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h
72lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 72lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index c526df40..e7108c59 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -109,7 +109,7 @@ LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
109/* Forward declaration. */ 109/* Forward declaration. */
110static int lj_cf_ffi_new(lua_State *L); 110static int lj_cf_ffi_new(lua_State *L);
111 111
112LJLIB_CF(ffi_meta___call) 112LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)
113{ 113{
114 GCcdata *cd = ffi_checkcdata(L, 1); 114 GCcdata *cd = ffi_checkcdata(L, 1);
115 if (cd->typeid == CTID_CTYPEID) 115 if (cd->typeid == CTID_CTYPEID)
@@ -163,7 +163,7 @@ LJLIB_CF(ffi_cdef)
163 return 0; 163 return 0;
164} 164}
165 165
166LJLIB_CF(ffi_new) 166LJLIB_CF(ffi_new) LJLIB_REC(.)
167{ 167{
168 CTState *cts = ctype_cts(L); 168 CTState *cts = ctype_cts(L);
169 CTypeID id = ffi_checkctype(L, cts); 169 CTypeID id = ffi_checkctype(L, cts);
diff --git a/src/lj_cconv.c b/src/lj_cconv.c
index 06e3160f..85fd57e5 100644
--- a/src/lj_cconv.c
+++ b/src/lj_cconv.c
@@ -693,7 +693,7 @@ static void cconv_struct_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,
693** This is true if an aggregate is to be initialized with a value. 693** This is true if an aggregate is to be initialized with a value.
694** Valarrays are treated as values here so ct_tv handles (V|C, I|F). 694** Valarrays are treated as values here so ct_tv handles (V|C, I|F).
695*/ 695*/
696static int cconv_multi_init(CTState *cts, CType *d, TValue *o) 696int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o)
697{ 697{
698 if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info))) 698 if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info)))
699 return 0; /* Destination is not an aggregate. */ 699 return 0; /* Destination is not an aggregate. */
@@ -713,7 +713,7 @@ void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
713{ 713{
714 if (len == 0) 714 if (len == 0)
715 memset(dp, 0, sz); 715 memset(dp, 0, sz);
716 else if (len == 1 && !cconv_multi_init(cts, d, o)) 716 else if (len == 1 && !lj_cconv_multi_init(cts, d, o))
717 lj_cconv_ct_tv(cts, d, dp, o, 0); 717 lj_cconv_ct_tv(cts, d, dp, o, 0);
718 else if (ctype_isarray(d->info)) /* Also handles valarray init with len>1. */ 718 else if (ctype_isarray(d->info)) /* Also handles valarray init with len>1. */
719 cconv_array_init(cts, d, sz, dp, o, len); 719 cconv_array_init(cts, d, sz, dp, o, len);
diff --git a/src/lj_cconv.h b/src/lj_cconv.h
index 45afe718..140ce8d6 100644
--- a/src/lj_cconv.h
+++ b/src/lj_cconv.h
@@ -56,6 +56,7 @@ LJ_FUNC void lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp);
56LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d, 56LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d,
57 uint8_t *dp, TValue *o, CTInfo flags); 57 uint8_t *dp, TValue *o, CTInfo flags);
58LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o); 58LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o);
59LJ_FUNC int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o);
59LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz, 60LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
60 uint8_t *dp, TValue *o, MSize len); 61 uint8_t *dp, TValue *o, MSize len);
61 62
diff --git a/src/lj_crecord.c b/src/lj_crecord.c
index 9a317891..50fca4cc 100644
--- a/src/lj_crecord.c
+++ b/src/lj_crecord.c
@@ -13,6 +13,7 @@
13#include "lj_err.h" 13#include "lj_err.h"
14#include "lj_str.h" 14#include "lj_str.h"
15#include "lj_ctype.h" 15#include "lj_ctype.h"
16#include "lj_cparse.h"
16#include "lj_cconv.h" 17#include "lj_cconv.h"
17#include "lj_ir.h" 18#include "lj_ir.h"
18#include "lj_jit.h" 19#include "lj_jit.h"
@@ -30,19 +31,42 @@
30 31
31/* -- C type checks ------------------------------------------------------- */ 32/* -- C type checks ------------------------------------------------------- */
32 33
33static GCcdata *argv2cdata(jit_State *J, TRef trcd, TValue *o) 34static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o)
34{ 35{
35 GCcdata *cd; 36 GCcdata *cd;
36 TRef trtypeid; 37 TRef trtypeid;
37 if (!tviscdata(o)) 38 if (!tref_iscdata(tr))
38 lj_trace_err(J, LJ_TRERR_BADTYPE); 39 lj_trace_err(J, LJ_TRERR_BADTYPE);
39 cd = cdataV(o); 40 cd = cdataV(o);
40 /* Specialize to the CTypeID. */ 41 /* Specialize to the CTypeID. */
41 trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), trcd, IRFL_CDATA_TYPEID); 42 trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_TYPEID);
42 emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->typeid)); 43 emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->typeid));
43 return cd; 44 return cd;
44} 45}
45 46
47static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o)
48{
49 if (tref_isstr(tr)) {
50 GCstr *s = strV(o);
51 CPState cp;
52 CTypeID oldtop;
53 /* Specialize to the string containing the C type declaration. */
54 emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s));
55 cp.L = J->L;
56 cp.cts = ctype_ctsG(J2G(J));
57 oldtop = cp.cts->top;
58 cp.srcname = strdata(s);
59 cp.p = strdata(s);
60 cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
61 if (lj_cparse(&cp) || cp.cts->top > oldtop) /* Avoid new struct defs. */
62 lj_trace_err(J, LJ_TRERR_BADTYPE);
63 return cp.val.id;
64 } else {
65 GCcdata *cd = argv2cdata(J, tr, o);
66 return cd->typeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->typeid;
67 }
68}
69
46/* -- Convert C type to C type -------------------------------------------- */ 70/* -- Convert C type to C type -------------------------------------------- */
47 71
48/* 72/*
@@ -172,7 +196,8 @@ static void crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp)
172 d = ctype_child(cts, d); 196 d = ctype_child(cts, d);
173 dinfo = d->info; 197 dinfo = d->info;
174 dsize = d->size; 198 dsize = d->size;
175 if (dsize != sizeof(double)) goto err_nyi; 199 dt = crec_ct2irt(d);
200 if (dt == IRT_CDATA) goto err_nyi;
176 { /* Clear im. */ 201 { /* Clear im. */
177 TRef dpim = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, dsize)); 202 TRef dpim = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, dsize));
178 emitir(IRT(IR_XSTORE, IRT_NUM), dpim, lj_ir_knum(J, 0)); 203 emitir(IRT(IR_XSTORE, IRT_NUM), dpim, lj_ir_knum(J, 0));
@@ -184,7 +209,8 @@ static void crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp)
184 d = ctype_child(cts, d); 209 d = ctype_child(cts, d);
185 dinfo = d->info; 210 dinfo = d->info;
186 dsize = d->size; 211 dsize = d->size;
187 if (dsize != sizeof(double)) goto err_nyi; 212 dt = crec_ct2irt(d);
213 if (dt == IRT_CDATA) goto err_nyi;
188 { 214 {
189 TRef spim = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, dsize)); 215 TRef spim = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, dsize));
190 TRef re = emitir(IRT(IR_XLOAD, IRT_NUM), sp, 0); 216 TRef re = emitir(IRT(IR_XLOAD, IRT_NUM), sp, 0);
@@ -306,7 +332,7 @@ static void crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, TValue *sval)
306 else 332 else
307 goto doconv; /* The pointer value was loaded, don't load number. */ 333 goto doconv; /* The pointer value was loaded, don't load number. */
308 } else { 334 } else {
309 sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCcdata))); 335 sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata)));
310 } 336 }
311 if (ctype_isenum(s->info)) s = ctype_child(cts, s); 337 if (ctype_isenum(s->info)) s = ctype_child(cts, s);
312 if (ctype_isnum(s->info)) { /* Load number value. */ 338 if (ctype_isnum(s->info)) { /* Load number value. */
@@ -436,6 +462,101 @@ index_struct:
436 } 462 }
437} 463}
438 464
465/* Record cdata allocation. */
466static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id)
467{
468 CTState *cts = ctype_ctsG(J2G(J));
469 CTSize sz;
470 CTInfo info = lj_ctype_info(cts, id, &sz);
471 TRef trid;
472 if (sz == 0 || sz > 64 || (info & CTF_VLA) || ctype_align(info) > CT_MEMALIGN)
473 lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: large/special allocations. */
474 trid = lj_ir_kint(J, id);
475 if (ctype_isptr(info)) {
476 TRef sp = J->base[1] ? J->base[1] : lj_ir_kptr(J, NULL);
477 J->base[0] = emitir(IRTG(IR_CNEWP, IRT_CDATA), trid, sp);
478 } else {
479 CType *d = ctype_raw(cts, id);
480 TRef trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, TREF_NIL);
481 J->base[0] = trcd;
482 if (J->base[1] && !J->base[2] &&
483 !lj_cconv_multi_init(cts, d, &rd->argv[1])) {
484 goto single_init;
485 } else if (ctype_isarray(d->info)) {
486 CType *dc = ctype_rawchild(cts, d); /* Array element type. */
487 CTSize ofs, esize = dc->size;
488 TRef sp = 0;
489 TValue *sval = NULL;
490 MSize i;
491 if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)))
492 lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init array of aggregates. */
493 for (i = 1, ofs = 0; ofs < sz; ofs += esize) {
494 TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
495 lj_ir_kintp(J, ofs + sizeof(GCcdata)));
496 if (J->base[i]) {
497 sp = J->base[i];
498 sval = &rd->argv[i];
499 i++;
500 } else if (i != 2) {
501 sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL;
502 }
503 crec_ct_tv(J, dc, dp, sp, sval);
504 }
505 } else if (ctype_isstruct(d->info)) {
506 CTypeID fid = d->sib;
507 MSize i = 1;
508 while (fid) {
509 CType *df = ctype_get(cts, fid);
510 fid = df->sib;
511 if (ctype_isfield(df->info)) {
512 CType *dc;
513 TRef sp, dp;
514 TValue *sval;
515 if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
516 dc = ctype_rawchild(cts, df); /* Field type. */
517 if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)))
518 lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init aggregates. */
519 if (J->base[i]) {
520 sp = J->base[i];
521 sval = &rd->argv[i];
522 i++;
523 } else {
524 sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL;
525 sval = NULL;
526 }
527 dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
528 lj_ir_kintp(J, df->size + sizeof(GCcdata)));
529 crec_ct_tv(J, dc, dp, sp, sval);
530 } else if (!ctype_isconstval(df->info)) {
531 /* NYI: init bitfields and sub-structures. */
532 lj_trace_err(J, LJ_TRERR_NYICONV);
533 }
534 }
535 } else {
536 TRef sp, dp;
537 single_init:
538 sp = J->base[1] ? J->base[1] : lj_ir_kint(J, 0);
539 dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
540 crec_ct_tv(J, d, dp, sp, &rd->argv[1]);
541 }
542 }
543}
544
545void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd)
546{
547 GCcdata *cd = argv2cdata(J, J->base[0], &rd->argv[0]);
548 if (cd->typeid == CTID_CTYPEID) {
549 crec_alloc(J, rd, *(CTypeID *)cdataptr(cd));
550 } /* else: Interpreter will throw. */
551}
552
553/* -- FFI library functions ----------------------------------------------- */
554
555void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd)
556{
557 crec_alloc(J, rd, argv2ctype(J, J->base[0], &rd->argv[0]));
558}
559
439#undef IR 560#undef IR
440#undef emitir 561#undef emitir
441 562
diff --git a/src/lj_crecord.h b/src/lj_crecord.h
index eb57cbb0..af98bde5 100644
--- a/src/lj_crecord.h
+++ b/src/lj_crecord.h
@@ -12,8 +12,12 @@
12 12
13#if LJ_HASJIT && LJ_HASFFI 13#if LJ_HASJIT && LJ_HASFFI
14LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd); 14LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd);
15LJ_FUNC void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd);
16LJ_FUNC void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd);
15#else 17#else
16#define recff_cdata_index recff_nyi 18#define recff_cdata_index recff_nyi
19#define recff_cdata_call recff_nyi
20#define recff_ffi_new recff_nyi
17#endif 21#endif
18 22
19#endif 23#endif