src/lj_carith.c - luajit-2.0-src

Data types defined

Functions defined

Macros defined

Source code

  1. /*
  2. ** C data arithmetic.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. #include "lj_obj.h"

  6. #if LJ_HASFFI

  7. #include "lj_gc.h"
  8. #include "lj_err.h"
  9. #include "lj_tab.h"
  10. #include "lj_meta.h"
  11. #include "lj_ir.h"
  12. #include "lj_ctype.h"
  13. #include "lj_cconv.h"
  14. #include "lj_cdata.h"
  15. #include "lj_carith.h"
  16. #include "lj_strscan.h"

  17. /* -- C data arithmetic --------------------------------------------------- */

  18. /* Binary operands of an operator converted to ctypes. */
  19. typedef struct CDArith {
  20.   uint8_t *p[2];
  21.   CType *ct[2];
  22. } CDArith;

  23. /* Check arguments for arithmetic metamethods. */
  24. static int carith_checkarg(lua_State *L, CTState *cts, CDArith *ca)
  25. {
  26.   TValue *o = L->base;
  27.   int ok = 1;
  28.   MSize i;
  29.   if (o+1 >= L->top)
  30.     lj_err_argt(L, 1, LUA_TCDATA);
  31.   for (i = 0; i < 2; i++, o++) {
  32.     if (tviscdata(o)) {
  33.       GCcdata *cd = cdataV(o);
  34.       CTypeID id = (CTypeID)cd->ctypeid;
  35.       CType *ct = ctype_raw(cts, id);
  36.       uint8_t *p = (uint8_t *)cdataptr(cd);
  37.       if (ctype_isptr(ct->info)) {
  38.         p = (uint8_t *)cdata_getptr(p, ct->size);
  39.         if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
  40.       } else if (ctype_isfunc(ct->info)) {
  41.         p = (uint8_t *)*(void **)p;
  42.         ct = ctype_get(cts,
  43.           lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR));
  44.       }
  45.       if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
  46.       ca->ct[i] = ct;
  47.       ca->p[i] = p;
  48.     } else if (tvisint(o)) {
  49.       ca->ct[i] = ctype_get(cts, CTID_INT32);
  50.       ca->p[i] = (uint8_t *)&o->i;
  51.     } else if (tvisnum(o)) {
  52.       ca->ct[i] = ctype_get(cts, CTID_DOUBLE);
  53.       ca->p[i] = (uint8_t *)&o->n;
  54.     } else if (tvisnil(o)) {
  55.       ca->ct[i] = ctype_get(cts, CTID_P_VOID);
  56.       ca->p[i] = (uint8_t *)0;
  57.     } else if (tvisstr(o)) {
  58.       TValue *o2 = i == 0 ? o+1 : o-1;
  59.       CType *ct = ctype_raw(cts, cdataV(o2)->ctypeid);
  60.       ca->ct[i] = NULL;
  61.       ca->p[i] = (uint8_t *)strVdata(o);
  62.       ok = 0;
  63.       if (ctype_isenum(ct->info)) {
  64.         CTSize ofs;
  65.         CType *cct = lj_ctype_getfield(cts, ct, strV(o), &ofs);
  66.         if (cct && ctype_isconstval(cct->info)) {
  67.           ca->ct[i] = ctype_child(cts, cct);
  68.           ca->p[i] = (uint8_t *)&cct->size;  /* Assumes ct does not grow. */
  69.           ok = 1;
  70.         } else {
  71.           ca->ct[1-i] = ct;  /* Use enum to improve error message. */
  72.           ca->p[1-i] = NULL;
  73.           break;
  74.         }
  75.       }
  76.     } else {
  77.       ca->ct[i] = NULL;
  78.       ca->p[i] = (void *)(intptr_t)1/* To make it unequal. */
  79.       ok = 0;
  80.     }
  81.   }
  82.   return ok;
  83. }

  84. /* Pointer arithmetic. */
  85. static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
  86. {
  87.   CType *ctp = ca->ct[0];
  88.   uint8_t *pp = ca->p[0];
  89.   ptrdiff_t idx;
  90.   CTSize sz;
  91.   CTypeID id;
  92.   GCcdata *cd;
  93.   if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
  94.     if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
  95.         (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
  96.       uint8_t *pp2 = ca->p[1];
  97.       if (mm == MM_eq) {  /* Pointer equality. Incompatible pointers are ok. */
  98.         setboolV(L->top-1, (pp == pp2));
  99.         return 1;
  100.       }
  101.       if (!lj_cconv_compatptr(cts, ctp, ca->ct[1], CCF_IGNQUAL))
  102.         return 0;
  103.       if (mm == MM_sub) {  /* Pointer difference. */
  104.         intptr_t diff;
  105.         sz = lj_ctype_size(cts, ctype_cid(ctp->info));  /* Element size. */
  106.         if (sz == 0 || sz == CTSIZE_INVALID)
  107.           return 0;
  108.         diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz;
  109.         /* All valid pointer differences on x64 are in (-2^47, +2^47),
  110.         ** which fits into a double without loss of precision.
  111.         */
  112.         setintptrV(L->top-1, (int32_t)diff);
  113.         return 1;
  114.       } else if (mm == MM_lt) {  /* Pointer comparison (unsigned). */
  115.         setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2));
  116.         return 1;
  117.       } else {
  118.         lua_assert(mm == MM_le);
  119.         setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2));
  120.         return 1;
  121.       }
  122.     }
  123.     if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(ca->ct[1]->info)))
  124.       return 0;
  125.     lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[1],
  126.                    (uint8_t *)&idx, ca->p[1], 0);
  127.     if (mm == MM_sub) idx = -idx;
  128.   } else if (mm == MM_add && ctype_isnum(ctp->info) &&
  129.       (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
  130.     /* Swap pointer and index. */
  131.     ctp = ca->ct[1]; pp = ca->p[1];
  132.     lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[0],
  133.                    (uint8_t *)&idx, ca->p[0], 0);
  134.   } else {
  135.     return 0;
  136.   }
  137.   sz = lj_ctype_size(cts, ctype_cid(ctp->info));  /* Element size. */
  138.   if (sz == CTSIZE_INVALID)
  139.     return 0;
  140.   pp += idx*(int32_t)sz;  /* Compute pointer + index. */
  141.   id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
  142.                        CTSIZE_PTR);
  143.   cd = lj_cdata_new(cts, id, CTSIZE_PTR);
  144.   *(uint8_t **)cdataptr(cd) = pp;
  145.   setcdataV(L, L->top-1, cd);
  146.   lj_gc_check(L);
  147.   return 1;
  148. }

  149. /* 64 bit integer arithmetic. */
  150. static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
  151. {
  152.   if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 &&
  153.       ctype_isnum(ca->ct[1]->info) && ca->ct[1]->size <= 8) {
  154.     CTypeID id = (((ca->ct[0]->info & CTF_UNSIGNED) && ca->ct[0]->size == 8) ||
  155.                   ((ca->ct[1]->info & CTF_UNSIGNED) && ca->ct[1]->size == 8)) ?
  156.                  CTID_UINT64 : CTID_INT64;
  157.     CType *ct = ctype_get(cts, id);
  158.     GCcdata *cd;
  159.     uint64_t u0, u1, *up;
  160.     lj_cconv_ct_ct(cts, ct, ca->ct[0], (uint8_t *)&u0, ca->p[0], 0);
  161.     if (mm != MM_unm)
  162.       lj_cconv_ct_ct(cts, ct, ca->ct[1], (uint8_t *)&u1, ca->p[1], 0);
  163.     switch (mm) {
  164.     case MM_eq:
  165.       setboolV(L->top-1, (u0 == u1));
  166.       return 1;
  167.     case MM_lt:
  168.       setboolV(L->top-1,
  169.                id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1));
  170.       return 1;
  171.     case MM_le:
  172.       setboolV(L->top-1,
  173.                id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1));
  174.       return 1;
  175.     default: break;
  176.     }
  177.     cd = lj_cdata_new(cts, id, 8);
  178.     up = (uint64_t *)cdataptr(cd);
  179.     setcdataV(L, L->top-1, cd);
  180.     switch (mm) {
  181.     case MM_add: *up = u0 + u1; break;
  182.     case MM_sub: *up = u0 - u1; break;
  183.     case MM_mul: *up = u0 * u1; break;
  184.     case MM_div:
  185.       if (id == CTID_INT64)
  186.         *up = (uint64_t)lj_carith_divi64((int64_t)u0, (int64_t)u1);
  187.       else
  188.         *up = lj_carith_divu64(u0, u1);
  189.       break;
  190.     case MM_mod:
  191.       if (id == CTID_INT64)
  192.         *up = (uint64_t)lj_carith_modi64((int64_t)u0, (int64_t)u1);
  193.       else
  194.         *up = lj_carith_modu64(u0, u1);
  195.       break;
  196.     case MM_pow:
  197.       if (id == CTID_INT64)
  198.         *up = (uint64_t)lj_carith_powi64((int64_t)u0, (int64_t)u1);
  199.       else
  200.         *up = lj_carith_powu64(u0, u1);
  201.       break;
  202.     case MM_unm: *up = (uint64_t)-(int64_t)u0; break;
  203.     default: lua_assert(0); break;
  204.     }
  205.     lj_gc_check(L);
  206.     return 1;
  207.   }
  208.   return 0;
  209. }

  210. /* Handle ctype arithmetic metamethods. */
  211. static int lj_carith_meta(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
  212. {
  213.   cTValue *tv = NULL;
  214.   if (tviscdata(L->base)) {
  215.     CTypeID id = cdataV(L->base)->ctypeid;
  216.     CType *ct = ctype_raw(cts, id);
  217.     if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  218.     tv = lj_ctype_meta(cts, id, mm);
  219.   }
  220.   if (!tv && L->base+1 < L->top && tviscdata(L->base+1)) {
  221.     CTypeID id = cdataV(L->base+1)->ctypeid;
  222.     CType *ct = ctype_raw(cts, id);
  223.     if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  224.     tv = lj_ctype_meta(cts, id, mm);
  225.   }
  226.   if (!tv) {
  227.     const char *repr[2];
  228.     int i, isenum = -1, isstr = -1;
  229.     if (mm == MM_eq) {  /* Equality checks never raise an error. */
  230.       int eq = ca->p[0] == ca->p[1];
  231.       setboolV(L->top-1, eq);
  232.       setboolV(&G(L)->tmptv2, eq);  /* Remember for trace recorder. */
  233.       return 1;
  234.     }
  235.     for (i = 0; i < 2; i++) {
  236.       if (ca->ct[i] && tviscdata(L->base+i)) {
  237.         if (ctype_isenum(ca->ct[i]->info)) isenum = i;
  238.         repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, ca->ct[i]), NULL));
  239.       } else {
  240.         if (tvisstr(&L->base[i])) isstr = i;
  241.         repr[i] = lj_typename(&L->base[i]);
  242.       }
  243.     }
  244.     if ((isenum ^ isstr) == 1)
  245.       lj_err_callerv(L, LJ_ERR_FFI_BADCONV, repr[isstr], repr[isenum]);
  246.     lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN :
  247.                       mm == MM_concat ? LJ_ERR_FFI_BADCONCAT :
  248.                       mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH,
  249.                    repr[0], repr[1]);
  250.   }
  251.   return lj_meta_tailcall(L, tv);
  252. }

  253. /* Arithmetic operators for cdata. */
  254. int lj_carith_op(lua_State *L, MMS mm)
  255. {
  256.   CTState *cts = ctype_cts(L);
  257.   CDArith ca;
  258.   if (carith_checkarg(L, cts, &ca)) {
  259.     if (carith_int64(L, cts, &ca, mm) || carith_ptr(L, cts, &ca, mm)) {
  260.       copyTV(L, &G(L)->tmptv2, L->top-1);  /* Remember for trace recorder. */
  261.       return 1;
  262.     }
  263.   }
  264.   return lj_carith_meta(L, cts, &ca, mm);
  265. }

  266. /* -- 64 bit bit operations helpers --------------------------------------- */

  267. #if LJ_64
  268. #define B64DEF(name) \
  269.   static LJ_AINLINE uint64_t lj_carith_##name(uint64_t x, int32_t sh)
  270. #else
  271. /* Not inlined on 32 bit archs, since some of these are quite lengthy. */
  272. #define B64DEF(name) \
  273.   uint64_t LJ_NOINLINE lj_carith_##name(uint64_t x, int32_t sh)
  274. #endif

  275. B64DEF(shl64) { return x << (sh&63); }
  276. B64DEF(shr64) { return x >> (sh&63); }
  277. B64DEF(sar64) { return (uint64_t)((int64_t)x >> (sh&63)); }
  278. B64DEF(rol64) { return lj_rol(x, (sh&63)); }
  279. B64DEF(ror64) { return lj_ror(x, (sh&63)); }

  280. #undef B64DEF

  281. uint64_t lj_carith_shift64(uint64_t x, int32_t sh, int op)
  282. {
  283.   switch (op) {
  284.   case IR_BSHL-IR_BSHL: x = lj_carith_shl64(x, sh); break;
  285.   case IR_BSHR-IR_BSHL: x = lj_carith_shr64(x, sh); break;
  286.   case IR_BSAR-IR_BSHL: x = lj_carith_sar64(x, sh); break;
  287.   case IR_BROL-IR_BSHL: x = lj_carith_rol64(x, sh); break;
  288.   case IR_BROR-IR_BSHL: x = lj_carith_ror64(x, sh); break;
  289.   default: lua_assert(0); break;
  290.   }
  291.   return x;
  292. }

  293. /* Equivalent to lj_lib_checkbit(), but handles cdata. */
  294. uint64_t lj_carith_check64(lua_State *L, int narg, CTypeID *id)
  295. {
  296.   TValue *o = L->base + narg-1;
  297.   if (o >= L->top) {
  298.   err:
  299.     lj_err_argt(L, narg, LUA_TNUMBER);
  300.   } else if (LJ_LIKELY(tvisnumber(o))) {
  301.     /* Handled below. */
  302.   } else if (tviscdata(o)) {
  303.     CTState *cts = ctype_cts(L);
  304.     uint8_t *sp = (uint8_t *)cdataptr(cdataV(o));
  305.     CTypeID sid = cdataV(o)->ctypeid;
  306.     CType *s = ctype_get(cts, sid);
  307.     uint64_t x;
  308.     if (ctype_isref(s->info)) {
  309.       sp = *(void **)sp;
  310.       sid = ctype_cid(s->info);
  311.     }
  312.     s = ctype_raw(cts, sid);
  313.     if (ctype_isenum(s->info)) s = ctype_child(cts, s);
  314.     if ((s->info & (CTMASK_NUM|CTF_BOOL|CTF_FP|CTF_UNSIGNED)) ==
  315.         CTINFO(CT_NUM, CTF_UNSIGNED) && s->size == 8)
  316.       *id = CTID_UINT64;  /* Use uint64_t, since it has the highest rank. */
  317.     else if (!*id)
  318.       *id = CTID_INT64;  /* Use int64_t, unless already set. */
  319.     lj_cconv_ct_ct(cts, ctype_get(cts, *id), s,
  320.                    (uint8_t *)&x, sp, CCF_ARG(narg));
  321.     return x;
  322.   } else if (!(tvisstr(o) && lj_strscan_number(strV(o), o))) {
  323.     goto err;
  324.   }
  325.   if (LJ_LIKELY(tvisint(o))) {
  326.     return (uint32_t)intV(o);
  327.   } else {
  328.     int32_t i = lj_num2bit(numV(o));
  329.     if (LJ_DUALNUM) setintV(o, i);
  330.     return (uint32_t)i;
  331.   }
  332. }


  333. /* -- 64 bit integer arithmetic helpers ----------------------------------- */

  334. #if LJ_32 && LJ_HASJIT
  335. /* Signed/unsigned 64 bit multiplication. */
  336. int64_t lj_carith_mul64(int64_t a, int64_t b)
  337. {
  338.   return a * b;
  339. }
  340. #endif

  341. /* Unsigned 64 bit division. */
  342. uint64_t lj_carith_divu64(uint64_t a, uint64_t b)
  343. {
  344.   if (b == 0) return U64x(80000000,00000000);
  345.   return a / b;
  346. }

  347. /* Signed 64 bit division. */
  348. int64_t lj_carith_divi64(int64_t a, int64_t b)
  349. {
  350.   if (b == 0 || (a == (int64_t)U64x(80000000,00000000) && b == -1))
  351.     return U64x(80000000,00000000);
  352.   return a / b;
  353. }

  354. /* Unsigned 64 bit modulo. */
  355. uint64_t lj_carith_modu64(uint64_t a, uint64_t b)
  356. {
  357.   if (b == 0) return U64x(80000000,00000000);
  358.   return a % b;
  359. }

  360. /* Signed 64 bit modulo. */
  361. int64_t lj_carith_modi64(int64_t a, int64_t b)
  362. {
  363.   if (b == 0) return U64x(80000000,00000000);
  364.   if (a == (int64_t)U64x(80000000,00000000) && b == -1) return 0;
  365.   return a % b;
  366. }

  367. /* Unsigned 64 bit x^k. */
  368. uint64_t lj_carith_powu64(uint64_t x, uint64_t k)
  369. {
  370.   uint64_t y;
  371.   if (k == 0)
  372.     return 1;
  373.   for (; (k & 1) == 0; k >>= 1) x *= x;
  374.   y = x;
  375.   if ((k >>= 1) != 0) {
  376.     for (;;) {
  377.       x *= x;
  378.       if (k == 1) break;
  379.       if (k & 1) y *= x;
  380.       k >>= 1;
  381.     }
  382.     y *= x;
  383.   }
  384.   return y;
  385. }

  386. /* Signed 64 bit x^k. */
  387. int64_t lj_carith_powi64(int64_t x, int64_t k)
  388. {
  389.   if (k == 0)
  390.     return 1;
  391.   if (k < 0) {
  392.     if (x == 0)
  393.       return U64x(7fffffff,ffffffff);
  394.     else if (x == 1)
  395.       return 1;
  396.     else if (x == -1)
  397.       return (k & 1) ? -1 : 1;
  398.     else
  399.       return 0;
  400.   }
  401.   return (int64_t)lj_carith_powu64((uint64_t)x, (uint64_t)k);
  402. }

  403. #endif