diff options
author | Mike Pall <mike> | 2010-12-22 23:21:38 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2010-12-22 23:21:38 +0100 |
commit | c0ffe7d39409b403b8e24484bd4f5ef67afa4507 (patch) | |
tree | 7b3a08212b51b031e130c8120e7635731f78a601 /src/lj_crecord.c | |
parent | 4f2f44dd97b56cd7ba07c3a9a08fcdd78913805e (diff) | |
download | luajit-c0ffe7d39409b403b8e24484bd4f5ef67afa4507.tar.gz luajit-c0ffe7d39409b403b8e24484bd4f5ef67afa4507.tar.bz2 luajit-c0ffe7d39409b403b8e24484bd4f5ef67afa4507.zip |
FFI: Record cdata allocations.
Diffstat (limited to 'src/lj_crecord.c')
-rw-r--r-- | src/lj_crecord.c | 133 |
1 files changed, 127 insertions, 6 deletions
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 | ||
33 | static GCcdata *argv2cdata(jit_State *J, TRef trcd, TValue *o) | 34 | static 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 | ||
47 | static 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. */ | ||
466 | static 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 | |||
545 | void 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 | |||
555 | void 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 | ||