|
@@ -21,6 +21,7 @@
|
|
|
#include "lj_cparse.h"
|
|
|
#include "lj_cdata.h"
|
|
|
#include "lj_cconv.h"
|
|
|
+#include "lj_carith.h"
|
|
|
#include "lj_ccall.h"
|
|
|
#include "lj_clib.h"
|
|
|
#include "lj_ff.h"
|
|
@@ -77,215 +78,6 @@ static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)
|
|
|
return p;
|
|
|
}
|
|
|
|
|
|
-/* -- C data arithmetic --------------------------------------------------- */
|
|
|
-
|
|
|
-typedef struct FFIArith {
|
|
|
- uint8_t *p[2];
|
|
|
- CType *ct[2];
|
|
|
-} FFIArith;
|
|
|
-
|
|
|
-/* Check arguments for arithmetic metamethods. */
|
|
|
-static int ffi_checkarith(lua_State *L, CTState *cts, FFIArith *fa)
|
|
|
-{
|
|
|
- TValue *o = L->base;
|
|
|
- int ok = 1;
|
|
|
- MSize i;
|
|
|
- if (o+1 >= L->top)
|
|
|
- lj_err_argt(L, 1, LUA_TCDATA);
|
|
|
- for (i = 0; i < 2; i++, o++) {
|
|
|
- if (tviscdata(o)) {
|
|
|
- GCcdata *cd = cdataV(o);
|
|
|
- CType *ct = ctype_raw(cts, (CTypeID)cd->typeid);
|
|
|
- uint8_t *p = (uint8_t *)cdataptr(cd);
|
|
|
- if (ctype_isptr(ct->info)) {
|
|
|
- p = (uint8_t *)cdata_getptr(p, ct->size);
|
|
|
- if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
|
|
|
- }
|
|
|
- fa->ct[i] = ct;
|
|
|
- fa->p[i] = p;
|
|
|
- } else if (tvisnum(o)) {
|
|
|
- fa->ct[i] = ctype_get(cts, CTID_DOUBLE);
|
|
|
- fa->p[i] = (uint8_t *)&o->n;
|
|
|
- } else if (tvisnil(o)) {
|
|
|
- fa->ct[i] = ctype_get(cts, CTID_P_VOID);
|
|
|
- fa->p[i] = (uint8_t *)0;
|
|
|
- } else {
|
|
|
- fa->ct[i] = NULL;
|
|
|
- fa->p[i] = NULL;
|
|
|
- ok = 0;
|
|
|
- }
|
|
|
- }
|
|
|
- return ok;
|
|
|
-}
|
|
|
-
|
|
|
-/* Pointer arithmetic. */
|
|
|
-static int ffi_arith_ptr(lua_State *L, CTState *cts, FFIArith *fa, MMS mm)
|
|
|
-{
|
|
|
- CType *ctp = fa->ct[0];
|
|
|
- uint8_t *pp = fa->p[0];
|
|
|
- ptrdiff_t idx;
|
|
|
- CTSize sz;
|
|
|
- CTypeID id;
|
|
|
- GCcdata *cd;
|
|
|
- if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
|
|
|
- if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
|
|
|
- (ctype_isptr(fa->ct[1]->info) || ctype_isrefarray(fa->ct[1]->info))) {
|
|
|
- uint8_t *pp2 = fa->p[1];
|
|
|
- if (mm == MM_eq) { /* Pointer equality. Incompatible pointers are ok. */
|
|
|
- setboolV(L->top-1, (pp == pp2));
|
|
|
- return 1;
|
|
|
- }
|
|
|
- if (!lj_cconv_compatptr(cts, ctp, fa->ct[1], CCF_IGNQUAL))
|
|
|
- return 0;
|
|
|
- if (mm == MM_sub) { /* Pointer difference. */
|
|
|
- intptr_t diff;
|
|
|
- sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
|
|
|
- if (sz == 0 || sz == CTSIZE_INVALID)
|
|
|
- return 0;
|
|
|
- diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz;
|
|
|
- /* All valid pointer differences on x64 are in (-2^47, +2^47),
|
|
|
- ** which fits into a double without loss of precision.
|
|
|
- */
|
|
|
- setnumV(L->top-1, (lua_Number)diff);
|
|
|
- return 1;
|
|
|
- } else if (mm == MM_lt) { /* Pointer comparison (unsigned). */
|
|
|
- setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2));
|
|
|
- return 1;
|
|
|
- } else {
|
|
|
- lua_assert(mm == MM_le);
|
|
|
- setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2));
|
|
|
- return 1;
|
|
|
- }
|
|
|
- }
|
|
|
- if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(fa->ct[1]->info)))
|
|
|
- return 0;
|
|
|
- lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), fa->ct[1],
|
|
|
- (uint8_t *)&idx, fa->p[1], 0);
|
|
|
- if (mm == MM_sub) idx = -idx;
|
|
|
- } else if (mm == MM_add && ctype_isnum(ctp->info) &&
|
|
|
- (ctype_isptr(fa->ct[1]->info) || ctype_isrefarray(fa->ct[1]->info))) {
|
|
|
- /* Swap pointer and index. */
|
|
|
- ctp = fa->ct[1]; pp = fa->p[1];
|
|
|
- lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), fa->ct[0],
|
|
|
- (uint8_t *)&idx, fa->p[0], 0);
|
|
|
- } else {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
|
|
|
- if (sz == CTSIZE_INVALID)
|
|
|
- return 0;
|
|
|
- pp += idx*(int32_t)sz; /* Compute pointer + index. */
|
|
|
- id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
|
|
|
- CTSIZE_PTR);
|
|
|
- cd = lj_cdata_new(cts, id, CTSIZE_PTR);
|
|
|
- *(uint8_t **)cdataptr(cd) = pp;
|
|
|
- setcdataV(L, L->top-1, cd);
|
|
|
- lj_gc_check(L);
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-/* 64 bit integer arithmetic. */
|
|
|
-static int ffi_arith_int64(lua_State *L, CTState *cts, FFIArith *fa, MMS mm)
|
|
|
-{
|
|
|
- if (ctype_isnum(fa->ct[0]->info) && fa->ct[0]->size <= 8 &&
|
|
|
- ctype_isnum(fa->ct[1]->info) && fa->ct[1]->size <= 8) {
|
|
|
- CTypeID id = (((fa->ct[0]->info & CTF_UNSIGNED) && fa->ct[0]->size == 8) ||
|
|
|
- ((fa->ct[1]->info & CTF_UNSIGNED) && fa->ct[1]->size == 8)) ?
|
|
|
- CTID_UINT64 : CTID_INT64;
|
|
|
- CType *ct = ctype_get(cts, id);
|
|
|
- GCcdata *cd;
|
|
|
- uint64_t u0, u1, *up;
|
|
|
- lj_cconv_ct_ct(cts, ct, fa->ct[0], (uint8_t *)&u0, fa->p[0], 0);
|
|
|
- if (mm != MM_unm)
|
|
|
- lj_cconv_ct_ct(cts, ct, fa->ct[1], (uint8_t *)&u1, fa->p[1], 0);
|
|
|
- switch (mm) {
|
|
|
- case MM_eq:
|
|
|
- setboolV(L->top-1, (u0 == u1));
|
|
|
- return 1;
|
|
|
- case MM_lt:
|
|
|
- setboolV(L->top-1,
|
|
|
- id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1));
|
|
|
- return 1;
|
|
|
- case MM_le:
|
|
|
- setboolV(L->top-1,
|
|
|
- id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1));
|
|
|
- return 1;
|
|
|
- case MM_div: case MM_mod:
|
|
|
- if (u1 == 0) { /* Division by zero. */
|
|
|
- if (u0 == 0)
|
|
|
- setnanV(L->top-1);
|
|
|
- else if (id == CTID_INT64 && (int64_t)u0 < 0)
|
|
|
- setminfV(L->top-1);
|
|
|
- else
|
|
|
- setpinfV(L->top-1);
|
|
|
- return 1;
|
|
|
- } else if (id == CTID_INT64 && (int64_t)u1 == -1 &&
|
|
|
- u0 == U64x(80000000,00000000)) { /* MIN64 / -1. */
|
|
|
- if (mm == MM_div) id = CTID_UINT64; else u0 = 0;
|
|
|
- mm = MM_unm; /* Result is 0x8000000000000000ULL or 0LL. */
|
|
|
- }
|
|
|
- break;
|
|
|
- default: break;
|
|
|
- }
|
|
|
- cd = lj_cdata_new(cts, id, 8);
|
|
|
- up = (uint64_t *)cdataptr(cd);
|
|
|
- setcdataV(L, L->top-1, cd);
|
|
|
- switch (mm) {
|
|
|
- case MM_add: *up = u0 + u1; break;
|
|
|
- case MM_sub: *up = u0 - u1; break;
|
|
|
- case MM_mul: *up = u0 * u1; break;
|
|
|
- case MM_div:
|
|
|
- if (id == CTID_INT64)
|
|
|
- *up = (uint64_t)((int64_t)u0 / (int64_t)u1);
|
|
|
- else
|
|
|
- *up = u0 / u1;
|
|
|
- break;
|
|
|
- case MM_mod:
|
|
|
- if (id == CTID_INT64)
|
|
|
- *up = (uint64_t)((int64_t)u0 % (int64_t)u1);
|
|
|
- else
|
|
|
- *up = u0 % u1;
|
|
|
- break;
|
|
|
- case MM_pow: *up = lj_cdata_powi64(u0, u1, (id == CTID_UINT64)); break;
|
|
|
- case MM_unm: *up = (uint64_t)-(int64_t)u0; break;
|
|
|
- default: lua_assert(0); break;
|
|
|
- }
|
|
|
- lj_gc_check(L);
|
|
|
- return 1;
|
|
|
- }
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-/* cdata arithmetic. */
|
|
|
-static int ffi_arith(lua_State *L)
|
|
|
-{
|
|
|
- CTState *cts = ctype_cts(L);
|
|
|
- FFIArith fa;
|
|
|
- MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq);
|
|
|
- if (ffi_checkarith(L, cts, &fa)) {
|
|
|
- if (ffi_arith_int64(L, cts, &fa, mm) || ffi_arith_ptr(L, cts, &fa, mm)) {
|
|
|
- copyTV(L, &G(L)->tmptv2, L->top-1); /* Remember for trace recorder. */
|
|
|
- return 1;
|
|
|
- }
|
|
|
- }
|
|
|
- /* NYI: per-cdata metamethods. */
|
|
|
- {
|
|
|
- const char *repr[2];
|
|
|
- int i;
|
|
|
- for (i = 0; i < 2; i++) {
|
|
|
- if (fa.ct[i])
|
|
|
- repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, fa.ct[i]), NULL));
|
|
|
- else
|
|
|
- repr[i] = typename(&L->base[i]);
|
|
|
- }
|
|
|
- lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN :
|
|
|
- mm == MM_concat ? LJ_ERR_FFI_BADCONCAT :
|
|
|
- mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH,
|
|
|
- repr[0], repr[1]);
|
|
|
- }
|
|
|
- return 0; /* unreachable */
|
|
|
-}
|
|
|
-
|
|
|
/* -- C type metamethods -------------------------------------------------- */
|
|
|
|
|
|
#define LJLIB_MODULE_ffi_meta
|
|
@@ -319,6 +111,13 @@ LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+/* Common handler for cdata arithmetic. */
|
|
|
+static int ffi_arith(lua_State *L)
|
|
|
+{
|
|
|
+ MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq);
|
|
|
+ return lj_carith_op(L, mm);
|
|
|
+}
|
|
|
+
|
|
|
/* The following functions must be in contiguous ORDER MM. */
|
|
|
LJLIB_CF(ffi_meta___eq) LJLIB_REC(cdata_arith MM_eq)
|
|
|
{
|