lj_carith.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. ** C data arithmetic.
  3. ** Copyright (C) 2005-2023 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. CTypeID id0 = i ? ctype_typeid(cts, ca->ct[0]) : 0;
  42. p = (uint8_t *)*(void **)p;
  43. ct = ctype_get(cts,
  44. lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR));
  45. if (i) { /* cts->tab may have been reallocated. */
  46. ca->ct[0] = ctype_get(cts, id0);
  47. }
  48. }
  49. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
  50. ca->ct[i] = ct;
  51. ca->p[i] = p;
  52. } else if (tvisint(o)) {
  53. ca->ct[i] = ctype_get(cts, CTID_INT32);
  54. ca->p[i] = (uint8_t *)&o->i;
  55. } else if (tvisnum(o)) {
  56. ca->ct[i] = ctype_get(cts, CTID_DOUBLE);
  57. ca->p[i] = (uint8_t *)&o->n;
  58. } else if (tvisnil(o)) {
  59. ca->ct[i] = ctype_get(cts, CTID_P_VOID);
  60. ca->p[i] = (uint8_t *)0;
  61. } else if (tvisstr(o)) {
  62. TValue *o2 = i == 0 ? o+1 : o-1;
  63. CType *ct = ctype_raw(cts, cdataV(o2)->ctypeid);
  64. ca->ct[i] = NULL;
  65. ca->p[i] = (uint8_t *)strVdata(o);
  66. ok = 0;
  67. if (ctype_isenum(ct->info)) {
  68. CTSize ofs;
  69. CType *cct = lj_ctype_getfield(cts, ct, strV(o), &ofs);
  70. if (cct && ctype_isconstval(cct->info)) {
  71. ca->ct[i] = ctype_child(cts, cct);
  72. ca->p[i] = (uint8_t *)&cct->size; /* Assumes ct does not grow. */
  73. ok = 1;
  74. } else {
  75. ca->ct[1-i] = ct; /* Use enum to improve error message. */
  76. ca->p[1-i] = NULL;
  77. break;
  78. }
  79. }
  80. } else {
  81. ca->ct[i] = NULL;
  82. ca->p[i] = (void *)(intptr_t)1; /* To make it unequal. */
  83. ok = 0;
  84. }
  85. }
  86. return ok;
  87. }
  88. /* Pointer arithmetic. */
  89. static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
  90. {
  91. CType *ctp = ca->ct[0];
  92. uint8_t *pp = ca->p[0];
  93. ptrdiff_t idx;
  94. CTSize sz;
  95. CTypeID id;
  96. GCcdata *cd;
  97. if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
  98. if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
  99. (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
  100. uint8_t *pp2 = ca->p[1];
  101. if (mm == MM_eq) { /* Pointer equality. Incompatible pointers are ok. */
  102. setboolV(L->top-1, (pp == pp2));
  103. return 1;
  104. }
  105. if (!lj_cconv_compatptr(cts, ctp, ca->ct[1], CCF_IGNQUAL))
  106. return 0;
  107. if (mm == MM_sub) { /* Pointer difference. */
  108. intptr_t diff;
  109. sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
  110. if (sz == 0 || sz == CTSIZE_INVALID)
  111. return 0;
  112. diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz;
  113. /* All valid pointer differences on x64 are in (-2^47, +2^47),
  114. ** which fits into a double without loss of precision.
  115. */
  116. setintptrV(L->top-1, (int32_t)diff);
  117. return 1;
  118. } else if (mm == MM_lt) { /* Pointer comparison (unsigned). */
  119. setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2));
  120. return 1;
  121. } else {
  122. lj_assertL(mm == MM_le, "bad metamethod %d", mm);
  123. setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2));
  124. return 1;
  125. }
  126. }
  127. if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(ca->ct[1]->info)))
  128. return 0;
  129. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[1],
  130. (uint8_t *)&idx, ca->p[1], 0);
  131. if (mm == MM_sub) idx = -idx;
  132. } else if (mm == MM_add && ctype_isnum(ctp->info) &&
  133. (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
  134. /* Swap pointer and index. */
  135. ctp = ca->ct[1]; pp = ca->p[1];
  136. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[0],
  137. (uint8_t *)&idx, ca->p[0], 0);
  138. } else {
  139. return 0;
  140. }
  141. sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
  142. if (sz == CTSIZE_INVALID)
  143. return 0;
  144. pp += idx*(int32_t)sz; /* Compute pointer + index. */
  145. id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
  146. CTSIZE_PTR);
  147. cd = lj_cdata_new(cts, id, CTSIZE_PTR);
  148. *(uint8_t **)cdataptr(cd) = pp;
  149. setcdataV(L, L->top-1, cd);
  150. lj_gc_check(L);
  151. return 1;
  152. }
  153. /* 64 bit integer arithmetic. */
  154. static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
  155. {
  156. if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 &&
  157. ctype_isnum(ca->ct[1]->info) && ca->ct[1]->size <= 8) {
  158. CTypeID id = (((ca->ct[0]->info & CTF_UNSIGNED) && ca->ct[0]->size == 8) ||
  159. ((ca->ct[1]->info & CTF_UNSIGNED) && ca->ct[1]->size == 8)) ?
  160. CTID_UINT64 : CTID_INT64;
  161. CType *ct = ctype_get(cts, id);
  162. GCcdata *cd;
  163. uint64_t u0, u1, *up;
  164. lj_cconv_ct_ct(cts, ct, ca->ct[0], (uint8_t *)&u0, ca->p[0], 0);
  165. if (mm != MM_unm)
  166. lj_cconv_ct_ct(cts, ct, ca->ct[1], (uint8_t *)&u1, ca->p[1], 0);
  167. switch (mm) {
  168. case MM_eq:
  169. setboolV(L->top-1, (u0 == u1));
  170. return 1;
  171. case MM_lt:
  172. setboolV(L->top-1,
  173. id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1));
  174. return 1;
  175. case MM_le:
  176. setboolV(L->top-1,
  177. id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1));
  178. return 1;
  179. default: break;
  180. }
  181. cd = lj_cdata_new(cts, id, 8);
  182. up = (uint64_t *)cdataptr(cd);
  183. setcdataV(L, L->top-1, cd);
  184. switch (mm) {
  185. case MM_add: *up = u0 + u1; break;
  186. case MM_sub: *up = u0 - u1; break;
  187. case MM_mul: *up = u0 * u1; break;
  188. case MM_div:
  189. if (id == CTID_INT64)
  190. *up = (uint64_t)lj_carith_divi64((int64_t)u0, (int64_t)u1);
  191. else
  192. *up = lj_carith_divu64(u0, u1);
  193. break;
  194. case MM_mod:
  195. if (id == CTID_INT64)
  196. *up = (uint64_t)lj_carith_modi64((int64_t)u0, (int64_t)u1);
  197. else
  198. *up = lj_carith_modu64(u0, u1);
  199. break;
  200. case MM_pow:
  201. if (id == CTID_INT64)
  202. *up = (uint64_t)lj_carith_powi64((int64_t)u0, (int64_t)u1);
  203. else
  204. *up = lj_carith_powu64(u0, u1);
  205. break;
  206. case MM_unm: *up = ~u0+1u; break;
  207. default:
  208. lj_assertL(0, "bad metamethod %d", mm);
  209. break;
  210. }
  211. lj_gc_check(L);
  212. return 1;
  213. }
  214. return 0;
  215. }
  216. /* Handle ctype arithmetic metamethods. */
  217. static int lj_carith_meta(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
  218. {
  219. cTValue *tv = NULL;
  220. if (tviscdata(L->base)) {
  221. CTypeID id = cdataV(L->base)->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 && L->base+1 < L->top && tviscdata(L->base+1)) {
  227. CTypeID id = cdataV(L->base+1)->ctypeid;
  228. CType *ct = ctype_raw(cts, id);
  229. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  230. tv = lj_ctype_meta(cts, id, mm);
  231. }
  232. if (!tv) {
  233. const char *repr[2];
  234. int i, isenum = -1, isstr = -1;
  235. if (mm == MM_eq) { /* Equality checks never raise an error. */
  236. int eq = ca->p[0] == ca->p[1];
  237. setboolV(L->top-1, eq);
  238. setboolV(&G(L)->tmptv2, eq); /* Remember for trace recorder. */
  239. return 1;
  240. }
  241. for (i = 0; i < 2; i++) {
  242. if (ca->ct[i] && tviscdata(L->base+i)) {
  243. if (ctype_isenum(ca->ct[i]->info)) isenum = i;
  244. repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, ca->ct[i]), NULL));
  245. } else {
  246. if (tvisstr(&L->base[i])) isstr = i;
  247. repr[i] = lj_typename(&L->base[i]);
  248. }
  249. }
  250. if ((isenum ^ isstr) == 1)
  251. lj_err_callerv(L, LJ_ERR_FFI_BADCONV, repr[isstr], repr[isenum]);
  252. lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN :
  253. mm == MM_concat ? LJ_ERR_FFI_BADCONCAT :
  254. mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH,
  255. repr[0], repr[1]);
  256. }
  257. return lj_meta_tailcall(L, tv);
  258. }
  259. /* Arithmetic operators for cdata. */
  260. int lj_carith_op(lua_State *L, MMS mm)
  261. {
  262. CTState *cts = ctype_cts(L);
  263. CDArith ca;
  264. if (carith_checkarg(L, cts, &ca) && mm != MM_len && mm != MM_concat) {
  265. if (carith_int64(L, cts, &ca, mm) || carith_ptr(L, cts, &ca, mm)) {
  266. copyTV(L, &G(L)->tmptv2, L->top-1); /* Remember for trace recorder. */
  267. return 1;
  268. }
  269. }
  270. return lj_carith_meta(L, cts, &ca, mm);
  271. }
  272. /* -- 64 bit bit operations helpers --------------------------------------- */
  273. #if LJ_64
  274. #define B64DEF(name) \
  275. static LJ_AINLINE uint64_t lj_carith_##name(uint64_t x, int32_t sh)
  276. #else
  277. /* Not inlined on 32 bit archs, since some of these are quite lengthy. */
  278. #define B64DEF(name) \
  279. uint64_t LJ_NOINLINE lj_carith_##name(uint64_t x, int32_t sh)
  280. #endif
  281. B64DEF(shl64) { return x << (sh&63); }
  282. B64DEF(shr64) { return x >> (sh&63); }
  283. B64DEF(sar64) { return (uint64_t)((int64_t)x >> (sh&63)); }
  284. B64DEF(rol64) { return lj_rol(x, (sh&63)); }
  285. B64DEF(ror64) { return lj_ror(x, (sh&63)); }
  286. #undef B64DEF
  287. uint64_t lj_carith_shift64(uint64_t x, int32_t sh, int op)
  288. {
  289. switch (op) {
  290. case IR_BSHL-IR_BSHL: x = lj_carith_shl64(x, sh); break;
  291. case IR_BSHR-IR_BSHL: x = lj_carith_shr64(x, sh); break;
  292. case IR_BSAR-IR_BSHL: x = lj_carith_sar64(x, sh); break;
  293. case IR_BROL-IR_BSHL: x = lj_carith_rol64(x, sh); break;
  294. case IR_BROR-IR_BSHL: x = lj_carith_ror64(x, sh); break;
  295. default:
  296. lj_assertX(0, "bad shift op %d", op);
  297. break;
  298. }
  299. return x;
  300. }
  301. /* Equivalent to lj_lib_checkbit(), but handles cdata. */
  302. uint64_t lj_carith_check64(lua_State *L, int narg, CTypeID *id)
  303. {
  304. TValue *o = L->base + narg-1;
  305. if (o >= L->top) {
  306. err:
  307. lj_err_argt(L, narg, LUA_TNUMBER);
  308. } else if (LJ_LIKELY(tvisnumber(o))) {
  309. /* Handled below. */
  310. } else if (tviscdata(o)) {
  311. CTState *cts = ctype_cts(L);
  312. uint8_t *sp = (uint8_t *)cdataptr(cdataV(o));
  313. CTypeID sid = cdataV(o)->ctypeid;
  314. CType *s = ctype_get(cts, sid);
  315. uint64_t x;
  316. if (ctype_isref(s->info)) {
  317. sp = *(void **)sp;
  318. sid = ctype_cid(s->info);
  319. }
  320. s = ctype_raw(cts, sid);
  321. if (ctype_isenum(s->info)) s = ctype_child(cts, s);
  322. if ((s->info & (CTMASK_NUM|CTF_BOOL|CTF_FP|CTF_UNSIGNED)) ==
  323. CTINFO(CT_NUM, CTF_UNSIGNED) && s->size == 8)
  324. *id = CTID_UINT64; /* Use uint64_t, since it has the highest rank. */
  325. else if (!*id)
  326. *id = CTID_INT64; /* Use int64_t, unless already set. */
  327. lj_cconv_ct_ct(cts, ctype_get(cts, *id), s,
  328. (uint8_t *)&x, sp, CCF_ARG(narg));
  329. return x;
  330. } else if (!(tvisstr(o) && lj_strscan_number(strV(o), o))) {
  331. goto err;
  332. }
  333. if (LJ_LIKELY(tvisint(o))) {
  334. return (uint32_t)intV(o);
  335. } else {
  336. int32_t i = lj_num2bit(numV(o));
  337. if (LJ_DUALNUM) setintV(o, i);
  338. return (uint32_t)i;
  339. }
  340. }
  341. /* -- 64 bit integer arithmetic helpers ----------------------------------- */
  342. #if LJ_32 && LJ_HASJIT
  343. /* Signed/unsigned 64 bit multiplication. */
  344. int64_t lj_carith_mul64(int64_t a, int64_t b)
  345. {
  346. return a * b;
  347. }
  348. #endif
  349. /* Unsigned 64 bit division. */
  350. uint64_t lj_carith_divu64(uint64_t a, uint64_t b)
  351. {
  352. if (b == 0) return U64x(80000000,00000000);
  353. return a / b;
  354. }
  355. /* Signed 64 bit division. */
  356. int64_t lj_carith_divi64(int64_t a, int64_t b)
  357. {
  358. if (b == 0 || (a == (int64_t)U64x(80000000,00000000) && b == -1))
  359. return U64x(80000000,00000000);
  360. return a / b;
  361. }
  362. /* Unsigned 64 bit modulo. */
  363. uint64_t lj_carith_modu64(uint64_t a, uint64_t b)
  364. {
  365. if (b == 0) return U64x(80000000,00000000);
  366. return a % b;
  367. }
  368. /* Signed 64 bit modulo. */
  369. int64_t lj_carith_modi64(int64_t a, int64_t b)
  370. {
  371. if (b == 0) return U64x(80000000,00000000);
  372. if (a == (int64_t)U64x(80000000,00000000) && b == -1) return 0;
  373. return a % b;
  374. }
  375. /* Unsigned 64 bit x^k. */
  376. uint64_t lj_carith_powu64(uint64_t x, uint64_t k)
  377. {
  378. uint64_t y;
  379. if (k == 0)
  380. return 1;
  381. for (; (k & 1) == 0; k >>= 1) x *= x;
  382. y = x;
  383. if ((k >>= 1) != 0) {
  384. for (;;) {
  385. x *= x;
  386. if (k == 1) break;
  387. if (k & 1) y *= x;
  388. k >>= 1;
  389. }
  390. y *= x;
  391. }
  392. return y;
  393. }
  394. /* Signed 64 bit x^k. */
  395. int64_t lj_carith_powi64(int64_t x, int64_t k)
  396. {
  397. if (k == 0)
  398. return 1;
  399. if (k < 0) {
  400. if (x == 0)
  401. return U64x(7fffffff,ffffffff);
  402. else if (x == 1)
  403. return 1;
  404. else if (x == -1)
  405. return (k & 1) ? -1 : 1;
  406. else
  407. return 0;
  408. }
  409. return (int64_t)lj_carith_powu64((uint64_t)x, (uint64_t)k);
  410. }
  411. #endif