mirror of
https://github.com/LuaJIT/LuaJIT.git
synced 2025-02-07 23:24:09 +00:00
FFI: Record cdata allocations.
This commit is contained in:
parent
4f2f44dd97
commit
c0ffe7d394
@ -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 \
|
|||||||
lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h \
|
lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h \
|
||||||
lj_traceerr.h lj_vm.h lj_lex.h lj_parse.h
|
lj_traceerr.h lj_vm.h lj_lex.h lj_parse.h
|
||||||
lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
|
lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
|
||||||
lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h \
|
lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h lj_ir.h lj_jit.h \
|
||||||
lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h lj_asm.h \
|
lj_iropt.h lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h \
|
||||||
lj_vm.h lj_target.h lj_target_*.h
|
lj_asm.h lj_vm.h lj_target.h lj_target_*.h
|
||||||
lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \
|
lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \
|
||||||
lj_bcdef.h
|
lj_bcdef.h
|
||||||
lj_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
|
lj_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 \
|
|||||||
lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \
|
lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \
|
||||||
lj_bc.h lj_vm.h lj_char.h
|
lj_bc.h lj_vm.h lj_char.h
|
||||||
lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
|
lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
|
||||||
lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cconv.h lj_ir.h \
|
lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cparse.h lj_cconv.h \
|
||||||
lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h lj_bc.h lj_traceerr.h \
|
lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h lj_bc.h \
|
||||||
lj_ffrecord.h lj_crecord.h
|
lj_traceerr.h lj_ffrecord.h lj_crecord.h
|
||||||
lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
|
lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
|
||||||
lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h
|
lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h
|
||||||
lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
|
lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
|
||||||
|
@ -109,7 +109,7 @@ LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
|
|||||||
/* Forward declaration. */
|
/* Forward declaration. */
|
||||||
static int lj_cf_ffi_new(lua_State *L);
|
static int lj_cf_ffi_new(lua_State *L);
|
||||||
|
|
||||||
LJLIB_CF(ffi_meta___call)
|
LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)
|
||||||
{
|
{
|
||||||
GCcdata *cd = ffi_checkcdata(L, 1);
|
GCcdata *cd = ffi_checkcdata(L, 1);
|
||||||
if (cd->typeid == CTID_CTYPEID)
|
if (cd->typeid == CTID_CTYPEID)
|
||||||
@ -163,7 +163,7 @@ LJLIB_CF(ffi_cdef)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
LJLIB_CF(ffi_new)
|
LJLIB_CF(ffi_new) LJLIB_REC(.)
|
||||||
{
|
{
|
||||||
CTState *cts = ctype_cts(L);
|
CTState *cts = ctype_cts(L);
|
||||||
CTypeID id = ffi_checkctype(L, cts);
|
CTypeID id = ffi_checkctype(L, cts);
|
||||||
|
@ -693,7 +693,7 @@ static void cconv_struct_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,
|
|||||||
** This is true if an aggregate is to be initialized with a value.
|
** This is true if an aggregate is to be initialized with a value.
|
||||||
** Valarrays are treated as values here so ct_tv handles (V|C, I|F).
|
** Valarrays are treated as values here so ct_tv handles (V|C, I|F).
|
||||||
*/
|
*/
|
||||||
static int cconv_multi_init(CTState *cts, CType *d, TValue *o)
|
int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o)
|
||||||
{
|
{
|
||||||
if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info)))
|
if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info)))
|
||||||
return 0; /* Destination is not an aggregate. */
|
return 0; /* Destination is not an aggregate. */
|
||||||
@ -713,7 +713,7 @@ void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
|
|||||||
{
|
{
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
memset(dp, 0, sz);
|
memset(dp, 0, sz);
|
||||||
else if (len == 1 && !cconv_multi_init(cts, d, o))
|
else if (len == 1 && !lj_cconv_multi_init(cts, d, o))
|
||||||
lj_cconv_ct_tv(cts, d, dp, o, 0);
|
lj_cconv_ct_tv(cts, d, dp, o, 0);
|
||||||
else if (ctype_isarray(d->info)) /* Also handles valarray init with len>1. */
|
else if (ctype_isarray(d->info)) /* Also handles valarray init with len>1. */
|
||||||
cconv_array_init(cts, d, sz, dp, o, len);
|
cconv_array_init(cts, d, sz, dp, o, len);
|
||||||
|
@ -56,6 +56,7 @@ LJ_FUNC void lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp);
|
|||||||
LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d,
|
LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d,
|
||||||
uint8_t *dp, TValue *o, CTInfo flags);
|
uint8_t *dp, TValue *o, CTInfo flags);
|
||||||
LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o);
|
LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o);
|
||||||
|
LJ_FUNC int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o);
|
||||||
LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
|
LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
|
||||||
uint8_t *dp, TValue *o, MSize len);
|
uint8_t *dp, TValue *o, MSize len);
|
||||||
|
|
||||||
|
133
src/lj_crecord.c
133
src/lj_crecord.c
@ -13,6 +13,7 @@
|
|||||||
#include "lj_err.h"
|
#include "lj_err.h"
|
||||||
#include "lj_str.h"
|
#include "lj_str.h"
|
||||||
#include "lj_ctype.h"
|
#include "lj_ctype.h"
|
||||||
|
#include "lj_cparse.h"
|
||||||
#include "lj_cconv.h"
|
#include "lj_cconv.h"
|
||||||
#include "lj_ir.h"
|
#include "lj_ir.h"
|
||||||
#include "lj_jit.h"
|
#include "lj_jit.h"
|
||||||
@ -30,19 +31,42 @@
|
|||||||
|
|
||||||
/* -- C type checks ------------------------------------------------------- */
|
/* -- C type checks ------------------------------------------------------- */
|
||||||
|
|
||||||
static GCcdata *argv2cdata(jit_State *J, TRef trcd, TValue *o)
|
static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o)
|
||||||
{
|
{
|
||||||
GCcdata *cd;
|
GCcdata *cd;
|
||||||
TRef trtypeid;
|
TRef trtypeid;
|
||||||
if (!tviscdata(o))
|
if (!tref_iscdata(tr))
|
||||||
lj_trace_err(J, LJ_TRERR_BADTYPE);
|
lj_trace_err(J, LJ_TRERR_BADTYPE);
|
||||||
cd = cdataV(o);
|
cd = cdataV(o);
|
||||||
/* Specialize to the CTypeID. */
|
/* Specialize to the CTypeID. */
|
||||||
trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), trcd, IRFL_CDATA_TYPEID);
|
trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_TYPEID);
|
||||||
emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->typeid));
|
emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->typeid));
|
||||||
return cd;
|
return cd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o)
|
||||||
|
{
|
||||||
|
if (tref_isstr(tr)) {
|
||||||
|
GCstr *s = strV(o);
|
||||||
|
CPState cp;
|
||||||
|
CTypeID oldtop;
|
||||||
|
/* Specialize to the string containing the C type declaration. */
|
||||||
|
emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s));
|
||||||
|
cp.L = J->L;
|
||||||
|
cp.cts = ctype_ctsG(J2G(J));
|
||||||
|
oldtop = cp.cts->top;
|
||||||
|
cp.srcname = strdata(s);
|
||||||
|
cp.p = strdata(s);
|
||||||
|
cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
|
||||||
|
if (lj_cparse(&cp) || cp.cts->top > oldtop) /* Avoid new struct defs. */
|
||||||
|
lj_trace_err(J, LJ_TRERR_BADTYPE);
|
||||||
|
return cp.val.id;
|
||||||
|
} else {
|
||||||
|
GCcdata *cd = argv2cdata(J, tr, o);
|
||||||
|
return cd->typeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->typeid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* -- Convert C type to C type -------------------------------------------- */
|
/* -- Convert C type to C type -------------------------------------------- */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -172,7 +196,8 @@ static void crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp)
|
|||||||
d = ctype_child(cts, d);
|
d = ctype_child(cts, d);
|
||||||
dinfo = d->info;
|
dinfo = d->info;
|
||||||
dsize = d->size;
|
dsize = d->size;
|
||||||
if (dsize != sizeof(double)) goto err_nyi;
|
dt = crec_ct2irt(d);
|
||||||
|
if (dt == IRT_CDATA) goto err_nyi;
|
||||||
{ /* Clear im. */
|
{ /* Clear im. */
|
||||||
TRef dpim = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, dsize));
|
TRef dpim = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, dsize));
|
||||||
emitir(IRT(IR_XSTORE, IRT_NUM), dpim, lj_ir_knum(J, 0));
|
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)
|
|||||||
d = ctype_child(cts, d);
|
d = ctype_child(cts, d);
|
||||||
dinfo = d->info;
|
dinfo = d->info;
|
||||||
dsize = d->size;
|
dsize = d->size;
|
||||||
if (dsize != sizeof(double)) goto err_nyi;
|
dt = crec_ct2irt(d);
|
||||||
|
if (dt == IRT_CDATA) goto err_nyi;
|
||||||
{
|
{
|
||||||
TRef spim = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, dsize));
|
TRef spim = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, dsize));
|
||||||
TRef re = emitir(IRT(IR_XLOAD, IRT_NUM), sp, 0);
|
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)
|
|||||||
else
|
else
|
||||||
goto doconv; /* The pointer value was loaded, don't load number. */
|
goto doconv; /* The pointer value was loaded, don't load number. */
|
||||||
} else {
|
} else {
|
||||||
sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCcdata)));
|
sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata)));
|
||||||
}
|
}
|
||||||
if (ctype_isenum(s->info)) s = ctype_child(cts, s);
|
if (ctype_isenum(s->info)) s = ctype_child(cts, s);
|
||||||
if (ctype_isnum(s->info)) { /* Load number value. */
|
if (ctype_isnum(s->info)) { /* Load number value. */
|
||||||
@ -436,6 +462,101 @@ index_struct:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Record cdata allocation. */
|
||||||
|
static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id)
|
||||||
|
{
|
||||||
|
CTState *cts = ctype_ctsG(J2G(J));
|
||||||
|
CTSize sz;
|
||||||
|
CTInfo info = lj_ctype_info(cts, id, &sz);
|
||||||
|
TRef trid;
|
||||||
|
if (sz == 0 || sz > 64 || (info & CTF_VLA) || ctype_align(info) > CT_MEMALIGN)
|
||||||
|
lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: large/special allocations. */
|
||||||
|
trid = lj_ir_kint(J, id);
|
||||||
|
if (ctype_isptr(info)) {
|
||||||
|
TRef sp = J->base[1] ? J->base[1] : lj_ir_kptr(J, NULL);
|
||||||
|
J->base[0] = emitir(IRTG(IR_CNEWP, IRT_CDATA), trid, sp);
|
||||||
|
} else {
|
||||||
|
CType *d = ctype_raw(cts, id);
|
||||||
|
TRef trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, TREF_NIL);
|
||||||
|
J->base[0] = trcd;
|
||||||
|
if (J->base[1] && !J->base[2] &&
|
||||||
|
!lj_cconv_multi_init(cts, d, &rd->argv[1])) {
|
||||||
|
goto single_init;
|
||||||
|
} else if (ctype_isarray(d->info)) {
|
||||||
|
CType *dc = ctype_rawchild(cts, d); /* Array element type. */
|
||||||
|
CTSize ofs, esize = dc->size;
|
||||||
|
TRef sp = 0;
|
||||||
|
TValue *sval = NULL;
|
||||||
|
MSize i;
|
||||||
|
if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)))
|
||||||
|
lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init array of aggregates. */
|
||||||
|
for (i = 1, ofs = 0; ofs < sz; ofs += esize) {
|
||||||
|
TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
|
||||||
|
lj_ir_kintp(J, ofs + sizeof(GCcdata)));
|
||||||
|
if (J->base[i]) {
|
||||||
|
sp = J->base[i];
|
||||||
|
sval = &rd->argv[i];
|
||||||
|
i++;
|
||||||
|
} else if (i != 2) {
|
||||||
|
sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL;
|
||||||
|
}
|
||||||
|
crec_ct_tv(J, dc, dp, sp, sval);
|
||||||
|
}
|
||||||
|
} else if (ctype_isstruct(d->info)) {
|
||||||
|
CTypeID fid = d->sib;
|
||||||
|
MSize i = 1;
|
||||||
|
while (fid) {
|
||||||
|
CType *df = ctype_get(cts, fid);
|
||||||
|
fid = df->sib;
|
||||||
|
if (ctype_isfield(df->info)) {
|
||||||
|
CType *dc;
|
||||||
|
TRef sp, dp;
|
||||||
|
TValue *sval;
|
||||||
|
if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
|
||||||
|
dc = ctype_rawchild(cts, df); /* Field type. */
|
||||||
|
if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)))
|
||||||
|
lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init aggregates. */
|
||||||
|
if (J->base[i]) {
|
||||||
|
sp = J->base[i];
|
||||||
|
sval = &rd->argv[i];
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL;
|
||||||
|
sval = NULL;
|
||||||
|
}
|
||||||
|
dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
|
||||||
|
lj_ir_kintp(J, df->size + sizeof(GCcdata)));
|
||||||
|
crec_ct_tv(J, dc, dp, sp, sval);
|
||||||
|
} else if (!ctype_isconstval(df->info)) {
|
||||||
|
/* NYI: init bitfields and sub-structures. */
|
||||||
|
lj_trace_err(J, LJ_TRERR_NYICONV);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
TRef sp, dp;
|
||||||
|
single_init:
|
||||||
|
sp = J->base[1] ? J->base[1] : lj_ir_kint(J, 0);
|
||||||
|
dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
|
||||||
|
crec_ct_tv(J, d, dp, sp, &rd->argv[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd)
|
||||||
|
{
|
||||||
|
GCcdata *cd = argv2cdata(J, J->base[0], &rd->argv[0]);
|
||||||
|
if (cd->typeid == CTID_CTYPEID) {
|
||||||
|
crec_alloc(J, rd, *(CTypeID *)cdataptr(cd));
|
||||||
|
} /* else: Interpreter will throw. */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -- FFI library functions ----------------------------------------------- */
|
||||||
|
|
||||||
|
void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd)
|
||||||
|
{
|
||||||
|
crec_alloc(J, rd, argv2ctype(J, J->base[0], &rd->argv[0]));
|
||||||
|
}
|
||||||
|
|
||||||
#undef IR
|
#undef IR
|
||||||
#undef emitir
|
#undef emitir
|
||||||
|
|
||||||
|
@ -12,8 +12,12 @@
|
|||||||
|
|
||||||
#if LJ_HASJIT && LJ_HASFFI
|
#if LJ_HASJIT && LJ_HASFFI
|
||||||
LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd);
|
LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd);
|
||||||
|
LJ_FUNC void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd);
|
||||||
|
LJ_FUNC void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd);
|
||||||
#else
|
#else
|
||||||
#define recff_cdata_index recff_nyi
|
#define recff_cdata_index recff_nyi
|
||||||
|
#define recff_cdata_call recff_nyi
|
||||||
|
#define recff_ffi_new recff_nyi
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user