2010-12-08 01:11:18 +00:00
|
|
|
/*
|
|
|
|
** Trace recorder for C data operations.
|
2011-01-09 16:12:53 +00:00
|
|
|
** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
|
2010-12-08 01:11:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define lj_ffrecord_c
|
|
|
|
#define LUA_CORE
|
|
|
|
|
|
|
|
#include "lj_obj.h"
|
|
|
|
|
|
|
|
#if LJ_HASJIT && LJ_HASFFI
|
|
|
|
|
|
|
|
#include "lj_err.h"
|
|
|
|
#include "lj_str.h"
|
|
|
|
#include "lj_ctype.h"
|
2010-12-22 22:21:38 +00:00
|
|
|
#include "lj_cparse.h"
|
2010-12-08 01:11:18 +00:00
|
|
|
#include "lj_cconv.h"
|
|
|
|
#include "lj_ir.h"
|
|
|
|
#include "lj_jit.h"
|
|
|
|
#include "lj_iropt.h"
|
|
|
|
#include "lj_trace.h"
|
|
|
|
#include "lj_ffrecord.h"
|
|
|
|
#include "lj_crecord.h"
|
|
|
|
#include "lj_dispatch.h"
|
|
|
|
|
|
|
|
/* Some local macros to save typing. Undef'd at the end. */
|
|
|
|
#define IR(ref) (&J->cur.ir[(ref)])
|
|
|
|
|
|
|
|
/* Pass IR on to next optimization in chain (FOLD). */
|
|
|
|
#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
|
|
|
|
|
2010-12-31 00:00:54 +00:00
|
|
|
#define emitconv(a, dt, st, flags) \
|
|
|
|
emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags))
|
|
|
|
|
2010-12-08 01:11:18 +00:00
|
|
|
/* -- C type checks ------------------------------------------------------- */
|
|
|
|
|
2010-12-22 22:21:38 +00:00
|
|
|
static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o)
|
2010-12-08 01:11:18 +00:00
|
|
|
{
|
|
|
|
GCcdata *cd;
|
|
|
|
TRef trtypeid;
|
2010-12-22 22:21:38 +00:00
|
|
|
if (!tref_iscdata(tr))
|
2010-12-08 01:11:18 +00:00
|
|
|
lj_trace_err(J, LJ_TRERR_BADTYPE);
|
|
|
|
cd = cdataV(o);
|
|
|
|
/* Specialize to the CTypeID. */
|
2010-12-22 22:21:38 +00:00
|
|
|
trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_TYPEID);
|
2010-12-08 01:11:18 +00:00
|
|
|
emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->typeid));
|
|
|
|
return cd;
|
|
|
|
}
|
|
|
|
|
2010-12-22 22:21:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-08 01:11:18 +00:00
|
|
|
/* -- Convert C type to C type -------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This code mirrors the code in lj_cconv.c. It performs the same steps
|
|
|
|
** for the trace recorder that lj_cconv.c does for the interpreter.
|
|
|
|
**
|
|
|
|
** One major difference is that we can get away with much fewer checks
|
|
|
|
** here. E.g. checks for casts, constness or correct types can often be
|
|
|
|
** omitted, even if they might fail. The interpreter subsequently throws
|
|
|
|
** an error, which aborts the trace.
|
|
|
|
**
|
|
|
|
** All operations are specialized to their C types, so the on-trace
|
|
|
|
** outcome must be the same as the outcome in the interpreter. If the
|
|
|
|
** interpreter doesn't throw an error, then the trace is correct, too.
|
|
|
|
** Care must be taken not to generate invalid (temporary) IR or to
|
|
|
|
** trigger asserts.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Convert CType to IRType. */
|
|
|
|
static IRType crec_ct2irt(CType *ct)
|
|
|
|
{
|
|
|
|
if (LJ_LIKELY(ctype_isnum(ct->info))) {
|
|
|
|
if ((ct->info & CTF_FP)) {
|
2010-12-29 23:42:00 +00:00
|
|
|
if (ct->size == sizeof(double))
|
2010-12-08 01:11:18 +00:00
|
|
|
return IRT_NUM;
|
2010-12-29 23:42:00 +00:00
|
|
|
else if (ct->size == sizeof(float))
|
|
|
|
return IRT_FLOAT;
|
2010-12-08 01:11:18 +00:00
|
|
|
} else {
|
|
|
|
uint32_t b = lj_fls(ct->size);
|
|
|
|
if (b <= 3)
|
|
|
|
return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0);
|
|
|
|
}
|
|
|
|
} else if (ctype_isptr(ct->info)) {
|
|
|
|
return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
|
2010-12-29 23:42:00 +00:00
|
|
|
} else if (ctype_iscomplex(ct->info)) {
|
|
|
|
if (ct->size == 2*sizeof(double))
|
|
|
|
return IRT_NUM;
|
|
|
|
else if (ct->size == 2*sizeof(float))
|
|
|
|
return IRT_FLOAT;
|
2010-12-08 01:11:18 +00:00
|
|
|
}
|
|
|
|
return IRT_CDATA;
|
|
|
|
}
|
|
|
|
|
2011-01-16 18:42:53 +00:00
|
|
|
/* Determine whether a passed number or cdata number is non-zero. */
|
|
|
|
static int crec_isnonzero(CType *s, void *p)
|
|
|
|
{
|
|
|
|
if (p == (void *)0)
|
|
|
|
return 0;
|
|
|
|
if (p == (void *)1)
|
|
|
|
return 1;
|
|
|
|
if ((s->info & CTF_FP)) {
|
|
|
|
if (s->size == sizeof(float))
|
|
|
|
return (*(float *)p != 0);
|
|
|
|
else
|
|
|
|
return (*(double *)p != 0);
|
|
|
|
} else {
|
|
|
|
if (s->size == 1)
|
|
|
|
return (*(uint8_t *)p != 0);
|
|
|
|
else if (s->size == 2)
|
|
|
|
return (*(uint16_t *)p != 0);
|
|
|
|
else if (s->size == 4)
|
|
|
|
return (*(uint32_t *)p != 0);
|
|
|
|
else
|
|
|
|
return (*(uint64_t *)p != 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-03 03:13:51 +00:00
|
|
|
static TRef crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp,
|
2011-01-16 18:42:53 +00:00
|
|
|
void *svisnz)
|
2010-12-08 01:11:18 +00:00
|
|
|
{
|
|
|
|
CTSize dsize = d->size, ssize = s->size;
|
|
|
|
CTInfo dinfo = d->info, sinfo = s->info;
|
|
|
|
IRType dt = crec_ct2irt(d);
|
2010-12-29 23:42:00 +00:00
|
|
|
IRType st = crec_ct2irt(s);
|
2010-12-08 01:11:18 +00:00
|
|
|
|
|
|
|
if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)
|
|
|
|
goto err_conv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and
|
|
|
|
** numbers up to 8 bytes. Otherwise sp holds a pointer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (cconv_idx2(dinfo, sinfo)) {
|
|
|
|
/* Destination is a bool. */
|
|
|
|
case CCX(B, B):
|
|
|
|
goto xstore; /* Source operand is already normalized. */
|
|
|
|
case CCX(B, I):
|
|
|
|
case CCX(B, F):
|
2011-01-16 18:42:53 +00:00
|
|
|
if (st != IRT_CDATA) {
|
|
|
|
/* Specialize to the result of a comparison against 0. */
|
|
|
|
TRef zero = (st == IRT_NUM || st == IRT_FLOAT) ? lj_ir_knum(J, 0) :
|
|
|
|
(st == IRT_I64 || st == IRT_U64) ? lj_ir_kint64(J, 0) :
|
|
|
|
lj_ir_kint(J, 0);
|
|
|
|
int isnz = crec_isnonzero(s, svisnz);
|
|
|
|
emitir(IRTG(isnz ? IR_NE : IR_EQ, st), sp, zero);
|
|
|
|
sp = lj_ir_kint(J, isnz);
|
|
|
|
goto xstore;
|
|
|
|
}
|
2010-12-08 01:11:18 +00:00
|
|
|
goto err_nyi;
|
|
|
|
|
|
|
|
/* Destination is an integer. */
|
|
|
|
case CCX(I, B):
|
|
|
|
case CCX(I, I):
|
|
|
|
conv_I_I:
|
2010-12-29 23:42:00 +00:00
|
|
|
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
|
|
|
|
#if LJ_64
|
|
|
|
/* Sign-extend 32 to 64 bit integer. */
|
|
|
|
if (dsize == 8 && ssize < 8 && !(sinfo & CTF_UNSIGNED))
|
2010-12-31 00:00:54 +00:00
|
|
|
sp = emitconv(sp, dt, IRT_INT, IRCONV_SEXT);
|
2010-12-29 23:42:00 +00:00
|
|
|
/* All other conversions are no-ops on x64. */
|
|
|
|
#else
|
|
|
|
if (dsize == 8 && ssize < 8) /* Extend to 64 bit integer. */
|
2010-12-31 00:00:54 +00:00
|
|
|
sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st,
|
|
|
|
(sinfo & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);
|
2010-12-29 23:42:00 +00:00
|
|
|
else if (dsize < 8 && ssize == 8) /* Truncate from 64 bit integer. */
|
2010-12-31 00:00:54 +00:00
|
|
|
sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, 0);
|
2010-12-29 23:42:00 +00:00
|
|
|
#endif
|
2010-12-08 01:11:18 +00:00
|
|
|
xstore:
|
2011-02-02 01:29:37 +00:00
|
|
|
if (dt == IRT_I64 || dt == IRT_U64) lj_needsplit(J);
|
2011-02-03 03:13:51 +00:00
|
|
|
if (dp == 0) return sp;
|
2010-12-08 01:11:18 +00:00
|
|
|
emitir(IRT(IR_XSTORE, dt), dp, sp);
|
|
|
|
break;
|
2010-12-29 23:42:00 +00:00
|
|
|
case CCX(I, C):
|
|
|
|
sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */
|
|
|
|
/* fallthrough */
|
2010-12-08 01:11:18 +00:00
|
|
|
case CCX(I, F):
|
2010-12-29 23:42:00 +00:00
|
|
|
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
|
2010-12-31 02:56:30 +00:00
|
|
|
sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, IRCONV_TRUNC|IRCONV_ANY);
|
2010-12-08 01:11:18 +00:00
|
|
|
goto xstore;
|
|
|
|
case CCX(I, P):
|
|
|
|
case CCX(I, A):
|
|
|
|
sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
|
|
|
|
ssize = CTSIZE_PTR;
|
2010-12-29 23:42:00 +00:00
|
|
|
st = IRT_UINTP;
|
2010-12-08 01:11:18 +00:00
|
|
|
goto conv_I_I;
|
|
|
|
|
|
|
|
/* Destination is a floating-point number. */
|
|
|
|
case CCX(F, B):
|
|
|
|
case CCX(F, I):
|
|
|
|
conv_F_I:
|
2010-12-29 23:42:00 +00:00
|
|
|
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
|
2010-12-31 00:00:54 +00:00
|
|
|
sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, 0);
|
2010-12-08 01:11:18 +00:00
|
|
|
goto xstore;
|
2010-12-29 23:42:00 +00:00
|
|
|
case CCX(F, C):
|
|
|
|
sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */
|
|
|
|
/* fallthrough */
|
2010-12-08 01:11:18 +00:00
|
|
|
case CCX(F, F):
|
|
|
|
conv_F_F:
|
2010-12-29 23:42:00 +00:00
|
|
|
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
|
2010-12-31 00:00:54 +00:00
|
|
|
if (dt != st) sp = emitconv(sp, dt, st, 0);
|
2010-12-08 01:11:18 +00:00
|
|
|
goto xstore;
|
|
|
|
|
|
|
|
/* Destination is a complex number. */
|
|
|
|
case CCX(C, I):
|
|
|
|
case CCX(C, F):
|
|
|
|
{ /* Clear im. */
|
2010-12-29 23:42:00 +00:00
|
|
|
TRef ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
|
|
|
|
emitir(IRT(IR_XSTORE, dt), ptr, lj_ir_knum(J, 0));
|
2010-12-08 01:11:18 +00:00
|
|
|
}
|
|
|
|
/* Convert to re. */
|
|
|
|
if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I;
|
|
|
|
|
|
|
|
case CCX(C, C):
|
2010-12-29 23:42:00 +00:00
|
|
|
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
|
2010-12-08 01:11:18 +00:00
|
|
|
{
|
2010-12-29 23:42:00 +00:00
|
|
|
TRef re, im, ptr;
|
|
|
|
re = emitir(IRT(IR_XLOAD, st), sp, 0);
|
|
|
|
ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, (ssize >> 1)));
|
|
|
|
im = emitir(IRT(IR_XLOAD, st), ptr, 0);
|
|
|
|
if (dt != st) {
|
2010-12-31 00:00:54 +00:00
|
|
|
re = emitconv(re, dt, st, 0);
|
|
|
|
im = emitconv(im, dt, st, 0);
|
2010-12-29 23:42:00 +00:00
|
|
|
}
|
|
|
|
emitir(IRT(IR_XSTORE, dt), dp, re);
|
|
|
|
ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
|
|
|
|
emitir(IRT(IR_XSTORE, dt), ptr, im);
|
2010-12-08 01:11:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Destination is a vector. */
|
|
|
|
case CCX(V, I):
|
|
|
|
case CCX(V, F):
|
|
|
|
case CCX(V, C):
|
|
|
|
case CCX(V, V):
|
|
|
|
goto err_nyi;
|
|
|
|
|
|
|
|
/* Destination is a pointer. */
|
|
|
|
case CCX(P, P):
|
|
|
|
case CCX(P, A):
|
|
|
|
case CCX(P, S):
|
2010-12-29 23:42:00 +00:00
|
|
|
/* There are only 32 bit pointers/addresses on 32 bit machines.
|
|
|
|
** Also ok on x64, since all 32 bit ops clear the upper part of the reg.
|
|
|
|
*/
|
|
|
|
goto xstore;
|
2010-12-08 01:11:18 +00:00
|
|
|
case CCX(P, I):
|
2010-12-29 23:42:00 +00:00
|
|
|
if (st == IRT_CDATA) goto err_nyi;
|
|
|
|
if (!LJ_64 && ssize == 8) /* Truncate from 64 bit integer. */
|
2010-12-31 00:00:54 +00:00
|
|
|
sp = emitconv(sp, IRT_U32, st, 0);
|
2010-12-29 23:42:00 +00:00
|
|
|
goto xstore;
|
2010-12-08 01:11:18 +00:00
|
|
|
case CCX(P, F):
|
2010-12-29 23:42:00 +00:00
|
|
|
if (st == IRT_CDATA) goto err_nyi;
|
|
|
|
/* The signed conversion is cheaper. x64 really has 47 bit pointers. */
|
2010-12-31 00:00:54 +00:00
|
|
|
sp = emitconv(sp, (LJ_64 && dsize == 8) ? IRT_I64 : IRT_U32,
|
2010-12-31 02:56:30 +00:00
|
|
|
st, IRCONV_TRUNC|IRCONV_ANY);
|
2010-12-29 23:42:00 +00:00
|
|
|
goto xstore;
|
2010-12-08 01:11:18 +00:00
|
|
|
|
|
|
|
/* Destination is an array. */
|
|
|
|
case CCX(A, A):
|
|
|
|
goto err_nyi;
|
|
|
|
|
|
|
|
/* Destination is a struct/union. */
|
|
|
|
case CCX(S, S):
|
|
|
|
goto err_nyi;
|
|
|
|
|
|
|
|
default:
|
|
|
|
err_conv:
|
|
|
|
err_nyi:
|
|
|
|
lj_trace_err(J, LJ_TRERR_NYICONV);
|
|
|
|
break;
|
|
|
|
}
|
2011-02-03 03:13:51 +00:00
|
|
|
return 0;
|
2010-12-08 01:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -- Convert C type to TValue (load) ------------------------------------- */
|
|
|
|
|
|
|
|
static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp)
|
|
|
|
{
|
2010-12-11 18:32:12 +00:00
|
|
|
CTState *cts = ctype_ctsG(J2G(J));
|
2010-12-08 01:11:18 +00:00
|
|
|
CTInfo sinfo = s->info;
|
|
|
|
lua_assert(!ctype_isenum(sinfo));
|
|
|
|
if (ctype_isnum(sinfo)) {
|
|
|
|
IRType t = crec_ct2irt(s);
|
2010-12-29 23:42:00 +00:00
|
|
|
TRef tr;
|
2010-12-15 20:49:40 +00:00
|
|
|
if (t == IRT_CDATA)
|
|
|
|
goto err_nyi; /* NYI: copyval of >64 bit integers. */
|
2010-12-29 23:42:00 +00:00
|
|
|
tr = emitir(IRT(IR_XLOAD, t), sp, 0);
|
|
|
|
if (t == IRT_FLOAT || t == IRT_U32) { /* Keep uint32_t/float as numbers. */
|
2011-02-03 03:13:51 +00:00
|
|
|
return emitconv(tr, IRT_NUM, t, 0);
|
2010-12-29 23:42:00 +00:00
|
|
|
} else if (t == IRT_I64 || t == IRT_U64) { /* Box 64 bit integer. */
|
2011-02-03 03:13:51 +00:00
|
|
|
sp = tr;
|
2011-02-02 01:29:37 +00:00
|
|
|
lj_needsplit(J);
|
2011-01-17 00:21:57 +00:00
|
|
|
} else if ((sinfo & CTF_BOOL)) {
|
|
|
|
/* Assume not equal to zero. Fixup and emit pending guard later. */
|
|
|
|
lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
|
|
|
|
J->postproc = LJ_POST_FIXGUARD;
|
2011-02-03 03:13:51 +00:00
|
|
|
return TREF_TRUE;
|
|
|
|
} else {
|
|
|
|
return tr;
|
2010-12-29 23:42:00 +00:00
|
|
|
}
|
2010-12-15 20:49:40 +00:00
|
|
|
} else if (ctype_isptr(sinfo)) {
|
|
|
|
IRType t = (LJ_64 && s->size == 8) ? IRT_P64 : IRT_P32;
|
|
|
|
sp = emitir(IRT(IR_XLOAD, t), sp, 0);
|
2010-12-08 01:11:18 +00:00
|
|
|
} else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
|
2010-12-28 18:09:01 +00:00
|
|
|
cts->L = J->L;
|
2010-12-15 20:49:40 +00:00
|
|
|
sid = lj_ctype_intern(cts, CTINFO_REF(sid), CTSIZE_PTR); /* Create ref. */
|
2010-12-29 23:42:00 +00:00
|
|
|
} else if (ctype_iscomplex(sinfo)) { /* Unbox/box complex. */
|
|
|
|
IRType t = s->size == 2*sizeof(double) ? IRT_NUM : IRT_FLOAT;
|
2010-12-15 20:49:40 +00:00
|
|
|
ptrdiff_t esz = (ptrdiff_t)(s->size >> 1);
|
|
|
|
TRef ptr, tr1, tr2, dp;
|
2010-12-17 16:20:04 +00:00
|
|
|
dp = emitir(IRTG(IR_CNEW, IRT_CDATA), lj_ir_kint(J, sid), TREF_NIL);
|
2010-12-15 20:49:40 +00:00
|
|
|
tr1 = emitir(IRT(IR_XLOAD, t), sp, 0);
|
|
|
|
ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, esz));
|
|
|
|
tr2 = emitir(IRT(IR_XLOAD, t), ptr, 0);
|
|
|
|
ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)));
|
|
|
|
emitir(IRT(IR_XSTORE, t), ptr, tr1);
|
|
|
|
ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)+esz));
|
|
|
|
emitir(IRT(IR_XSTORE, t), ptr, tr2);
|
|
|
|
return dp;
|
2010-12-08 01:11:18 +00:00
|
|
|
} else {
|
2010-12-15 20:49:40 +00:00
|
|
|
/* NYI: copyval of vectors. */
|
|
|
|
err_nyi:
|
2010-12-08 01:11:18 +00:00
|
|
|
lj_trace_err(J, LJ_TRERR_NYICONV);
|
|
|
|
}
|
2011-02-03 03:13:51 +00:00
|
|
|
/* Box pointer, ref or 64 bit integer. */
|
|
|
|
return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, sid), sp);
|
2010-12-08 01:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -- Convert TValue to C type (store) ------------------------------------ */
|
|
|
|
|
2011-02-03 03:13:51 +00:00
|
|
|
static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, TValue *sval)
|
2010-12-08 01:11:18 +00:00
|
|
|
{
|
|
|
|
CTState *cts = ctype_ctsG(J2G(J));
|
|
|
|
CTypeID sid = CTID_P_VOID;
|
2011-01-16 18:42:53 +00:00
|
|
|
void *svisnz = 0;
|
2010-12-08 01:11:18 +00:00
|
|
|
CType *s;
|
|
|
|
if (LJ_LIKELY(tref_isinteger(sp))) {
|
|
|
|
sid = CTID_INT32;
|
2011-01-16 18:42:53 +00:00
|
|
|
svisnz = (void *)(intptr_t)(numV(sval) != 0);
|
2010-12-08 01:11:18 +00:00
|
|
|
} else if (tref_isnum(sp)) {
|
|
|
|
sid = CTID_DOUBLE;
|
2011-01-16 18:42:53 +00:00
|
|
|
svisnz = (void *)(intptr_t)(numV(sval) != 0);
|
2010-12-08 01:11:18 +00:00
|
|
|
} else if (tref_isbool(sp)) {
|
|
|
|
sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0);
|
|
|
|
sid = CTID_BOOL;
|
|
|
|
} else if (tref_isnil(sp)) {
|
2010-12-11 19:10:52 +00:00
|
|
|
sp = lj_ir_kptr(J, NULL);
|
2010-12-08 01:11:18 +00:00
|
|
|
} else if (tref_isudata(sp)) {
|
|
|
|
sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCcdata)));
|
2011-01-13 16:20:29 +00:00
|
|
|
} else if (tref_isstr(sp)) {
|
|
|
|
if (ctype_isenum(d->info)) { /* Match string against enum constant. */
|
|
|
|
GCstr *str = strV(sval);
|
|
|
|
CTSize ofs;
|
|
|
|
CType *cct = lj_ctype_getfield(cts, d, str, &ofs);
|
|
|
|
/* Specialize to the name of the enum constant. */
|
|
|
|
emitir(IRTG(IR_EQ, IRT_STR), sp, lj_ir_kstr(J, str));
|
|
|
|
if (cct && ctype_isconstval(cct->info)) {
|
|
|
|
lua_assert(ctype_child(cts, cct)->size == 4);
|
2011-01-16 18:42:53 +00:00
|
|
|
svisnz = (void *)(intptr_t)(cct->size != 0);
|
2011-01-13 16:20:29 +00:00
|
|
|
sp = lj_ir_kint(J, (int32_t)cct->size);
|
|
|
|
sid = ctype_cid(cct->info);
|
|
|
|
} /* else: interpreter will throw. */
|
|
|
|
} else if (ctype_isrefarray(d->info)) { /* Copy string to array. */
|
|
|
|
lj_trace_err(J, LJ_TRERR_BADTYPE); /* NYI */
|
|
|
|
} else { /* Otherwise pass the string data as a const char[]. */
|
|
|
|
sp = emitir(IRT(IR_STRREF, IRT_P32), sp, lj_ir_kint(J, 0));
|
|
|
|
sid = CTID_A_CCHAR;
|
|
|
|
}
|
2011-01-16 18:42:53 +00:00
|
|
|
} else { /* NYI: tref_istab(sp), tref_islightud(sp). */
|
2010-12-08 01:11:18 +00:00
|
|
|
sid = argv2cdata(J, sp, sval)->typeid;
|
|
|
|
s = ctype_raw(cts, sid);
|
2011-01-16 18:42:53 +00:00
|
|
|
svisnz = cdataptr(cdataV(sval));
|
2010-12-08 01:11:18 +00:00
|
|
|
if (ctype_isptr(s->info)) {
|
|
|
|
IRType t = (LJ_64 && s->size == 8) ? IRT_P64 : IRT_P32;
|
2010-12-17 16:20:04 +00:00
|
|
|
sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_PTR);
|
2011-01-16 18:42:53 +00:00
|
|
|
if (ctype_isref(s->info)) {
|
|
|
|
svisnz = *(void **)svisnz;
|
2010-12-08 01:11:18 +00:00
|
|
|
s = ctype_rawchild(cts, s);
|
2011-01-16 18:42:53 +00:00
|
|
|
} else {
|
2010-12-08 01:11:18 +00:00
|
|
|
goto doconv; /* The pointer value was loaded, don't load number. */
|
2011-01-16 18:42:53 +00:00
|
|
|
}
|
2011-02-03 03:13:51 +00:00
|
|
|
|
|
|
|
} else if (ctype_isnum(s->info) && s->size == 8) {
|
|
|
|
IRType t = (s->info & CTF_UNSIGNED) ? IRT_U64 : IRT_I64;
|
|
|
|
sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64);
|
|
|
|
lj_needsplit(J);
|
|
|
|
goto doconv;
|
2010-12-08 01:11:18 +00:00
|
|
|
} else {
|
2010-12-22 22:21:38 +00:00
|
|
|
sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata)));
|
2010-12-08 01:11:18 +00:00
|
|
|
}
|
|
|
|
if (ctype_isenum(s->info)) s = ctype_child(cts, s);
|
|
|
|
if (ctype_isnum(s->info)) { /* Load number value. */
|
|
|
|
IRType t = crec_ct2irt(s);
|
2011-02-02 01:29:37 +00:00
|
|
|
if (t != IRT_CDATA) {
|
|
|
|
sp = emitir(IRT(IR_XLOAD, t), sp, 0);
|
|
|
|
if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
|
|
|
|
}
|
2010-12-08 01:11:18 +00:00
|
|
|
}
|
|
|
|
goto doconv;
|
|
|
|
}
|
|
|
|
s = ctype_get(cts, sid);
|
|
|
|
doconv:
|
2011-01-13 16:20:29 +00:00
|
|
|
if (ctype_isenum(d->info)) d = ctype_child(cts, d);
|
2011-02-03 03:13:51 +00:00
|
|
|
return crec_ct_ct(J, d, s, dp, sp, svisnz);
|
2010-12-08 01:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -- C data metamethods -------------------------------------------------- */
|
|
|
|
|
2011-01-16 17:32:33 +00:00
|
|
|
/* Specialize to the CTypeID held by a cdata constructor. */
|
|
|
|
static CTypeID crec_constructor(jit_State *J, GCcdata *cd, TRef tr)
|
|
|
|
{
|
|
|
|
CTypeID id;
|
|
|
|
lua_assert(tref_iscdata(tr) && cd->typeid == CTID_CTYPEID);
|
|
|
|
id = *(CTypeID *)cdataptr(cd);
|
|
|
|
tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata)));
|
|
|
|
tr = emitir(IRT(IR_XLOAD, IRT_INT), tr, 0);
|
|
|
|
emitir(IRTG(IR_EQ, IRT_INT), tr, lj_ir_kint(J, (int32_t)id));
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2010-12-12 20:07:23 +00:00
|
|
|
/* This would be rather difficult in FOLD, so do it here:
|
|
|
|
** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k)
|
|
|
|
** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
|
|
|
|
*/
|
|
|
|
static TRef crec_reassoc_ofs(jit_State *J, TRef tr, ptrdiff_t *ofsp, MSize sz)
|
|
|
|
{
|
|
|
|
IRIns *ir = IR(tref_ref(tr));
|
|
|
|
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) &&
|
|
|
|
ir->o == IR_ADD && irref_isk(ir->op2)) {
|
|
|
|
IRIns *irk = IR(ir->op2);
|
|
|
|
tr = ir->op1;
|
|
|
|
#if LJ_64
|
|
|
|
if (irk->o == IR_KINT64)
|
|
|
|
*ofsp += (ptrdiff_t)ir_kint64(irk)->u64 * sz;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
*ofsp += (ptrdiff_t)irk->i * sz;
|
|
|
|
}
|
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
2010-12-08 01:11:18 +00:00
|
|
|
void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)
|
|
|
|
{
|
|
|
|
TRef idx, ptr = J->base[0];
|
|
|
|
ptrdiff_t ofs = sizeof(GCcdata);
|
|
|
|
GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]);
|
|
|
|
CTState *cts = ctype_ctsG(J2G(J));
|
|
|
|
CType *ct = ctype_raw(cts, cd->typeid);
|
|
|
|
CTypeID sid = 0;
|
|
|
|
|
|
|
|
/* Resolve pointer or reference for cdata object. */
|
|
|
|
if (ctype_isptr(ct->info)) {
|
|
|
|
IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
|
|
|
|
if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
|
2010-12-17 16:20:04 +00:00
|
|
|
ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_PTR);
|
2010-12-08 01:11:18 +00:00
|
|
|
ofs = 0;
|
2010-12-15 20:49:40 +00:00
|
|
|
ptr = crec_reassoc_ofs(J, ptr, &ofs, 1);
|
2010-12-08 01:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
idx = J->base[1];
|
|
|
|
if (tref_isnumber(idx)) {
|
|
|
|
/* The size of a ptrdiff_t is target-specific. */
|
|
|
|
#if LJ_64
|
2010-12-31 02:56:30 +00:00
|
|
|
if (tref_isnum(idx))
|
|
|
|
idx = emitconv(idx, IRT_I64, IRT_NUM, IRCONV_TRUNC|IRCONV_ANY);
|
|
|
|
else
|
|
|
|
idx = emitconv(idx, IRT_I64, IRT_INT, IRCONV_SEXT);
|
2010-12-08 01:11:18 +00:00
|
|
|
#else
|
2010-12-31 02:56:30 +00:00
|
|
|
if (tref_isnum(idx))
|
|
|
|
idx = emitconv(idx, IRT_INT, IRT_NUM, IRCONV_TRUNC|IRCONV_ANY);
|
2010-12-08 01:11:18 +00:00
|
|
|
#endif
|
2011-01-26 20:14:58 +00:00
|
|
|
integer_key:
|
2010-12-08 01:11:18 +00:00
|
|
|
if (ctype_ispointer(ct->info)) {
|
2011-01-29 18:47:37 +00:00
|
|
|
CTSize sz;
|
|
|
|
if ((ct->info & CTF_COMPLEX))
|
|
|
|
idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1));
|
|
|
|
sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info)));
|
2010-12-12 20:07:23 +00:00
|
|
|
idx = crec_reassoc_ofs(J, idx, &ofs, sz);
|
2010-12-08 02:33:48 +00:00
|
|
|
idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz));
|
2010-12-08 01:11:18 +00:00
|
|
|
ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);
|
|
|
|
}
|
2011-01-26 20:14:58 +00:00
|
|
|
} else if (tref_iscdata(idx)) {
|
|
|
|
GCcdata *cdk = cdataV(&rd->argv[1]);
|
|
|
|
CType *ctk = ctype_raw(cts, cdk->typeid);
|
|
|
|
IRType t;
|
|
|
|
if (ctype_isenum(ctk->info)) ctk = ctype_child(cts, ctk);
|
|
|
|
if (ctype_isinteger(ctk->info) && (t = crec_ct2irt(ctk)) != IRT_CDATA) {
|
|
|
|
idx = emitir(IRT(IR_ADD, IRT_PTR), idx, lj_ir_kintp(J, sizeof(GCcdata)));
|
|
|
|
idx = emitir(IRT(IR_XLOAD, t), idx, 0);
|
2011-02-02 01:29:37 +00:00
|
|
|
if (!LJ_64 && (t == IRT_I64 || t == IRT_U64)) {
|
2011-01-26 20:14:58 +00:00
|
|
|
idx = emitconv(idx, IRT_INT, t, 0);
|
2011-02-02 01:29:37 +00:00
|
|
|
lj_needsplit(J);
|
|
|
|
}
|
2011-01-26 20:14:58 +00:00
|
|
|
goto integer_key;
|
|
|
|
}
|
2010-12-08 01:11:18 +00:00
|
|
|
} else if (tref_isstr(idx)) {
|
|
|
|
GCstr *name = strV(&rd->argv[1]);
|
2010-12-09 21:48:01 +00:00
|
|
|
/* Always specialize to the field name. */
|
|
|
|
emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));
|
2011-01-16 17:32:33 +00:00
|
|
|
if (cd->typeid == CTID_CTYPEID)
|
|
|
|
ct = ctype_raw(cts, crec_constructor(J, cd, ptr));
|
2010-12-15 18:47:01 +00:00
|
|
|
if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */
|
|
|
|
CType *cct = ctype_rawchild(cts, ct);
|
|
|
|
if (ctype_isstruct(cct->info)) {
|
2010-12-15 20:49:40 +00:00
|
|
|
ct = cct;
|
|
|
|
goto index_struct;
|
2010-12-15 18:47:01 +00:00
|
|
|
}
|
|
|
|
} else if (ctype_isstruct(ct->info)) {
|
2010-12-08 01:11:18 +00:00
|
|
|
CTSize fofs;
|
2010-12-15 18:47:01 +00:00
|
|
|
CType *fct;
|
|
|
|
index_struct:
|
|
|
|
fct = lj_ctype_getfield(cts, ct, name, &fofs);
|
2010-12-08 01:11:18 +00:00
|
|
|
if (fct) {
|
|
|
|
if (ctype_isconstval(fct->info)) {
|
|
|
|
if (fct->size >= 0x80000000u &&
|
|
|
|
(ctype_child(cts, fct)->info & CTF_UNSIGNED)) {
|
|
|
|
J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
J->base[0] = lj_ir_kint(J, (int32_t)fct->size);
|
|
|
|
return; /* Interpreter will throw for newindex. */
|
|
|
|
} else if (ctype_isbitfield(fct->info)) {
|
|
|
|
lj_trace_err(J, LJ_TRERR_NYICONV);
|
|
|
|
} else {
|
|
|
|
lua_assert(ctype_isfield(fct->info));
|
|
|
|
sid = ctype_cid(fct->info);
|
|
|
|
}
|
|
|
|
ofs += (ptrdiff_t)fofs;
|
|
|
|
}
|
|
|
|
} else if (ctype_iscomplex(ct->info)) {
|
|
|
|
if (strdata(name)[0] == 'i') ofs += (ct->size >> 1);
|
|
|
|
sid = ctype_cid(ct->info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!sid) lj_trace_err(J, LJ_TRERR_BADTYPE);
|
|
|
|
|
|
|
|
if (ofs)
|
|
|
|
ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
|
|
|
|
|
|
|
|
/* Resolve reference for field. */
|
|
|
|
ct = ctype_get(cts, sid);
|
|
|
|
if (ctype_isref(ct->info))
|
|
|
|
ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0);
|
|
|
|
|
2011-01-13 16:20:29 +00:00
|
|
|
while (ctype_isattrib(ct->info))
|
|
|
|
ct = ctype_child(cts, ct); /* Skip attributes. */
|
2010-12-08 01:11:18 +00:00
|
|
|
|
|
|
|
if (rd->data == 0) { /* __index metamethod. */
|
2011-01-13 16:20:29 +00:00
|
|
|
if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); /* Skip enums. */
|
2010-12-08 01:11:18 +00:00
|
|
|
J->base[0] = crec_tv_ct(J, ct, sid, ptr);
|
|
|
|
} else { /* __newindex metamethod. */
|
|
|
|
rd->nres = 0;
|
2011-01-05 19:28:57 +00:00
|
|
|
J->needsnap = 1;
|
2010-12-08 01:11:18 +00:00
|
|
|
crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-22 22:21:38 +00:00
|
|
|
/* 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);
|
2011-02-03 03:13:51 +00:00
|
|
|
CType *d = ctype_raw(cts, id);
|
2010-12-22 22:21:38 +00:00
|
|
|
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);
|
2011-02-03 03:13:51 +00:00
|
|
|
/* Use special instruction to box pointer or 64 bit integer. */
|
|
|
|
if (ctype_isptr(info) || (ctype_isnum(info) && sz == 8)) {
|
|
|
|
TRef sp = J->base[1] ? crec_ct_tv(J, d, 0, J->base[1], &rd->argv[1]) :
|
|
|
|
ctype_isptr(info) ? lj_ir_kptr(J, NULL) :
|
|
|
|
(lj_needsplit(J), lj_ir_kint64(J, 0));
|
|
|
|
J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, sp);
|
2010-12-22 22:21:38 +00:00
|
|
|
} else {
|
|
|
|
TRef trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, TREF_NIL);
|
|
|
|
J->base[0] = trcd;
|
2011-01-23 13:23:21 +00:00
|
|
|
if (J->base[1] && !J->base[2] && !lj_cconv_multi_init(d, &rd->argv[1])) {
|
2010-12-22 22:21:38 +00:00
|
|
|
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;
|
2011-01-16 18:42:53 +00:00
|
|
|
TValue tv;
|
|
|
|
TValue *sval = &tv;
|
2010-12-22 22:21:38 +00:00
|
|
|
MSize i;
|
2011-02-03 03:13:51 +00:00
|
|
|
tv.u64 = 0;
|
2010-12-22 22:21:38 +00:00
|
|
|
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;
|
2011-01-16 18:42:53 +00:00
|
|
|
TValue tv;
|
|
|
|
TValue *sval = &tv;
|
|
|
|
setnumV(&tv, 0);
|
2010-12-22 22:21:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
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 {
|
2011-02-03 03:13:51 +00:00
|
|
|
TRef dp;
|
2010-12-22 22:21:38 +00:00
|
|
|
single_init:
|
|
|
|
dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
|
2011-02-03 03:13:51 +00:00
|
|
|
if (J->base[1]) {
|
|
|
|
crec_ct_tv(J, d, dp, J->base[1], &rd->argv[1]);
|
|
|
|
} else {
|
|
|
|
TValue tv;
|
|
|
|
tv.u64 = 0;
|
|
|
|
crec_ct_tv(J, d, dp, lj_ir_kint(J, 0), &tv);
|
|
|
|
}
|
2010-12-22 22:21:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2011-01-16 17:32:33 +00:00
|
|
|
crec_alloc(J, rd, crec_constructor(J, cd, J->base[0]));
|
2011-01-09 15:48:37 +00:00
|
|
|
} else {
|
|
|
|
lj_trace_err(J, LJ_TRERR_BADTYPE);
|
|
|
|
}
|
2010-12-22 22:21:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-03 00:30:58 +00:00
|
|
|
static TRef crec_arith_int64(jit_State *J, TRef *sp, CType **s, MMS mm)
|
|
|
|
{
|
2011-01-03 02:35:35 +00:00
|
|
|
if (ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) {
|
|
|
|
IRType dt;
|
|
|
|
CTypeID id;
|
2011-02-03 03:13:51 +00:00
|
|
|
TRef tr;
|
2011-01-03 02:35:35 +00:00
|
|
|
MSize i;
|
2011-02-02 01:29:37 +00:00
|
|
|
lj_needsplit(J);
|
2011-01-03 02:35:35 +00:00
|
|
|
if (((s[0]->info & CTF_UNSIGNED) && s[0]->size == 8) ||
|
|
|
|
((s[1]->info & CTF_UNSIGNED) && s[1]->size == 8)) {
|
|
|
|
dt = IRT_U64; id = CTID_UINT64;
|
|
|
|
} else {
|
|
|
|
dt = IRT_I64; id = CTID_INT64;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
IRType st = tref_type(sp[i]);
|
|
|
|
if (st == IRT_NUM || st == IRT_FLOAT)
|
|
|
|
sp[i] = emitconv(sp[i], dt, st, IRCONV_TRUNC|IRCONV_ANY);
|
|
|
|
else if (!(st == IRT_I64 || st == IRT_U64))
|
|
|
|
sp[i] = emitconv(sp[i], dt, IRT_INT,
|
|
|
|
((st - IRT_I8) & 1) ? 0 : IRCONV_SEXT);
|
|
|
|
}
|
2011-01-17 00:23:04 +00:00
|
|
|
if (mm < MM_add) {
|
|
|
|
/* Assume true comparison. Fixup and emit pending guard later. */
|
|
|
|
IROp op;
|
|
|
|
if (mm == MM_eq) {
|
|
|
|
op = IR_EQ;
|
|
|
|
} else {
|
|
|
|
op = mm == MM_lt ? IR_LT : IR_LE;
|
|
|
|
if (dt == IRT_U64)
|
|
|
|
op += (IR_ULT-IR_LT);
|
|
|
|
}
|
|
|
|
lj_ir_set(J, IRTG(op, dt), sp[0], sp[1]);
|
|
|
|
J->postproc = LJ_POST_FIXGUARD;
|
|
|
|
return TREF_TRUE;
|
2011-01-03 02:35:35 +00:00
|
|
|
} else {
|
|
|
|
tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, dt), sp[0], sp[1]);
|
|
|
|
}
|
2011-02-03 03:13:51 +00:00
|
|
|
return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
|
2011-01-03 02:35:35 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2011-01-03 00:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static TRef crec_arith_ptr(jit_State *J, TRef *sp, CType **s, MMS mm)
|
|
|
|
{
|
|
|
|
CTState *cts = ctype_ctsG(J2G(J));
|
|
|
|
CType *ctp = s[0];
|
2011-01-17 00:23:04 +00:00
|
|
|
if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
|
|
|
|
if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
|
|
|
|
(ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {
|
|
|
|
if (mm == MM_sub) { /* Pointer difference. */
|
|
|
|
TRef tr;
|
|
|
|
CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));
|
|
|
|
if (sz == 0 || (sz & (sz-1)) != 0)
|
|
|
|
return 0; /* NYI: integer division. */
|
|
|
|
tr = emitir(IRT(IR_SUB, IRT_PTR), sp[0], sp[1]);
|
|
|
|
tr = emitir(IRT(IR_BSAR, IRT_INTP), tr, lj_ir_kint(J, lj_fls(sz)));
|
2011-01-03 00:30:58 +00:00
|
|
|
#if LJ_64
|
2011-01-17 00:23:04 +00:00
|
|
|
tr = emitconv(tr, IRT_NUM, IRT_INTP, 0);
|
2011-01-03 00:30:58 +00:00
|
|
|
#endif
|
2011-01-17 00:23:04 +00:00
|
|
|
return tr;
|
|
|
|
} else { /* Pointer comparison (unsigned). */
|
|
|
|
/* Assume true comparison. Fixup and emit pending guard later. */
|
|
|
|
IROp op = mm == MM_eq ? IR_EQ : mm == MM_lt ? IR_ULT : IR_ULE;
|
|
|
|
lj_ir_set(J, IRTG(op, IRT_PTR), sp[0], sp[1]);
|
|
|
|
J->postproc = LJ_POST_FIXGUARD;
|
|
|
|
return TREF_TRUE;
|
|
|
|
}
|
2011-01-03 00:30:58 +00:00
|
|
|
}
|
2011-01-17 00:23:04 +00:00
|
|
|
if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(s[1]->info)))
|
|
|
|
return 0;
|
|
|
|
} else if (mm == MM_add && ctype_isnum(ctp->info) &&
|
|
|
|
(ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {
|
2011-01-03 00:30:58 +00:00
|
|
|
TRef tr = sp[0]; sp[0] = sp[1]; sp[1] = tr; /* Swap pointer and index. */
|
|
|
|
ctp = s[1];
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TRef tr = sp[1];
|
|
|
|
IRType t = tref_type(tr);
|
2011-01-17 00:23:04 +00:00
|
|
|
CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));
|
2011-01-03 00:30:58 +00:00
|
|
|
CTypeID id;
|
|
|
|
#if LJ_64
|
|
|
|
if (t == IRT_NUM || t == IRT_FLOAT)
|
|
|
|
tr = emitconv(tr, IRT_INTP, t, IRCONV_TRUNC|IRCONV_ANY);
|
|
|
|
else if (!(t == IRT_I64 || t == IRT_U64))
|
|
|
|
tr = emitconv(tr, IRT_INTP, IRT_INT,
|
|
|
|
((t - IRT_I8) & 1) ? 0 : IRCONV_SEXT);
|
|
|
|
#else
|
2011-02-02 01:29:37 +00:00
|
|
|
if (!tref_typerange(sp[1], IRT_I8, IRT_U32)) {
|
2011-01-03 00:30:58 +00:00
|
|
|
tr = emitconv(tr, IRT_INTP, t,
|
|
|
|
(t == IRT_NUM || t == IRT_FLOAT) ?
|
|
|
|
IRCONV_TRUNC|IRCONV_ANY : 0);
|
2011-02-02 01:29:37 +00:00
|
|
|
}
|
2011-01-03 00:30:58 +00:00
|
|
|
#endif
|
|
|
|
tr = emitir(IRT(IR_MUL, IRT_INTP), tr, lj_ir_kintp(J, sz));
|
|
|
|
tr = emitir(IRT(IR_ADD, IRT_PTR), sp[0], tr);
|
|
|
|
id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
|
|
|
|
CTSIZE_PTR);
|
2011-02-03 03:13:51 +00:00
|
|
|
return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
|
2011-01-03 00:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd)
|
|
|
|
{
|
|
|
|
CTState *cts = ctype_ctsG(J2G(J));
|
|
|
|
TRef sp[2];
|
|
|
|
CType *s[2];
|
|
|
|
MSize i;
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
TRef tr = J->base[i];
|
|
|
|
CType *ct = ctype_get(cts, CTID_DOUBLE);
|
|
|
|
if (tref_iscdata(tr)) {
|
|
|
|
CTypeID id = argv2cdata(J, tr, &rd->argv[i])->typeid;
|
|
|
|
ct = ctype_raw(cts, id);
|
|
|
|
if (ctype_isptr(ct->info)) { /* Resolve pointer or reference. */
|
|
|
|
IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
|
|
|
|
if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
|
|
|
|
tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_PTR);
|
2011-02-03 03:13:51 +00:00
|
|
|
} else if (ctype_isnum(ct->info) && ct->size == 8) {
|
|
|
|
IRType t = (ct->info & CTF_UNSIGNED) ? IRT_U64 : IRT_I64;
|
|
|
|
tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT64);
|
|
|
|
lj_needsplit(J);
|
|
|
|
goto ok;
|
2011-01-03 00:30:58 +00:00
|
|
|
} else {
|
|
|
|
tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata)));
|
|
|
|
}
|
|
|
|
if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
|
|
|
|
if (ctype_isnum(ct->info)) {
|
|
|
|
IRType t = crec_ct2irt(ct);
|
|
|
|
if (t == IRT_CDATA) goto err_type;
|
2011-02-02 01:29:37 +00:00
|
|
|
if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
|
2011-01-03 00:30:58 +00:00
|
|
|
tr = emitir(IRT(IR_XLOAD, t), tr, 0);
|
|
|
|
} else if (!(ctype_isptr(ct->info) || ctype_isrefarray(ct->info))) {
|
|
|
|
goto err_type;
|
|
|
|
}
|
2011-01-13 15:39:42 +00:00
|
|
|
} else if (tref_isnil(tr)) {
|
|
|
|
tr = lj_ir_kptr(J, NULL);
|
|
|
|
ct = ctype_get(cts, CTID_P_VOID);
|
2011-01-03 00:30:58 +00:00
|
|
|
} else if (tref_isinteger(tr)) {
|
|
|
|
ct = ctype_get(cts, CTID_INT32);
|
|
|
|
} else if (!tref_isnum(tr)) {
|
|
|
|
goto err_type;
|
|
|
|
}
|
2011-02-03 03:13:51 +00:00
|
|
|
ok:
|
2011-01-03 00:30:58 +00:00
|
|
|
s[i] = ct;
|
|
|
|
sp[i] = tr;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TRef tr;
|
|
|
|
if (!(tr = crec_arith_int64(J, sp, s, (MMS)rd->data)) &&
|
|
|
|
!(tr = crec_arith_ptr(J, sp, s, (MMS)rd->data))) {
|
|
|
|
err_type:
|
|
|
|
lj_trace_err(J, LJ_TRERR_BADTYPE);
|
|
|
|
}
|
|
|
|
J->base[0] = tr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-22 22:21:38 +00:00
|
|
|
/* -- 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]));
|
|
|
|
}
|
|
|
|
|
2011-02-05 00:05:56 +00:00
|
|
|
void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd)
|
|
|
|
{
|
|
|
|
CTState *cts = ctype_ctsG(J2G(J));
|
|
|
|
TRef tr = J->base[0];
|
|
|
|
if (tr) {
|
|
|
|
TRef trlen = J->base[1];
|
|
|
|
if (trlen) {
|
|
|
|
trlen = lj_ir_toint(J, trlen);
|
|
|
|
tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, &rd->argv[0]);
|
|
|
|
} else {
|
|
|
|
tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CCHAR), 0, tr, &rd->argv[0]);
|
|
|
|
trlen = lj_ir_call(J, IRCALL_strlen, tr);
|
|
|
|
}
|
|
|
|
J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), tr, trlen);
|
|
|
|
} /* else: interpreter will throw. */
|
|
|
|
}
|
|
|
|
|
2011-01-02 17:30:02 +00:00
|
|
|
/* -- Miscellaneous library functions ------------------------------------- */
|
|
|
|
|
|
|
|
void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd)
|
|
|
|
{
|
|
|
|
CTypeID sid = argv2cdata(J, J->base[0], &rd->argv[0])->typeid;
|
|
|
|
CTState *cts = ctype_ctsG(J2G(J));
|
|
|
|
CType *s = ctype_raw(cts, sid);
|
|
|
|
TRef sp = J->base[0];
|
|
|
|
if (ctype_isref(s->info)) {
|
|
|
|
sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp, IRFL_CDATA_PTR);
|
|
|
|
s = ctype_rawchild(cts, s);
|
2011-02-05 00:04:41 +00:00
|
|
|
} else if (ctype_isnum(s->info) && s->size == 8) {
|
|
|
|
IRType t = (s->info & CTF_UNSIGNED) ? IRT_U64 : IRT_I64;
|
|
|
|
TRef tr = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64);
|
|
|
|
J->base[0] = emitconv(tr, IRT_NUM, t, 0);
|
|
|
|
lj_needsplit(J);
|
|
|
|
return;
|
2011-01-02 17:30:02 +00:00
|
|
|
} else {
|
|
|
|
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_isnum(s->info) || ctype_iscomplex(s->info)) {
|
|
|
|
IRType t = crec_ct2irt(s);
|
|
|
|
if (t != IRT_CDATA) {
|
|
|
|
TRef tr = emitir(IRT(IR_XLOAD, t), sp, 0); /* Load number value. */
|
2011-02-02 01:29:37 +00:00
|
|
|
if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
|
2011-01-02 17:30:02 +00:00
|
|
|
if (t == IRT_FLOAT || t == IRT_U32 || t == IRT_I64 || t == IRT_U64)
|
|
|
|
tr = emitconv(tr, IRT_NUM, t, 0);
|
|
|
|
J->base[0] = tr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lj_trace_err(J, LJ_TRERR_BADTYPE);
|
|
|
|
}
|
|
|
|
|
2010-12-08 01:11:18 +00:00
|
|
|
#undef IR
|
|
|
|
#undef emitir
|
2010-12-31 00:00:54 +00:00
|
|
|
#undef emitconv
|
2010-12-08 01:11:18 +00:00
|
|
|
|
|
|
|
#endif
|