lj_carith.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. ** C data arithmetic.
  3. ** Copyright (C) 2005-2014 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_ctype.h"
  12. #include "lj_cconv.h"
  13. #include "lj_cdata.h"
  14. #include "lj_carith.h"
  15. /* -- C data arithmetic --------------------------------------------------- */
  16. /* Binary operands of an operator converted to ctypes. */
  17. typedef struct CDArith {
  18. uint8_t *p[2];
  19. CType *ct[2];
  20. } CDArith;
  21. /* Check arguments for arithmetic metamethods. */
  22. static int carith_checkarg(lua_State *L, CTState *cts, CDArith *ca)
  23. {
  24. TValue *o = L->base;
  25. int ok = 1;
  26. MSize i;
  27. if (o+1 >= L->top)
  28. lj_err_argt(L, 1, LUA_TCDATA);
  29. for (i = 0; i < 2; i++, o++) {
  30. if (tviscdata(o)) {
  31. GCcdata *cd = cdataV(o);
  32. CTypeID id = (CTypeID)cd->ctypeid;
  33. CType *ct = ctype_raw(cts, id);
  34. uint8_t *p = (uint8_t *)cdataptr(cd);
  35. if (ctype_isptr(ct->info)) {
  36. p = (uint8_t *)cdata_getptr(p, ct->size);
  37. if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
  38. } else if (ctype_isfunc(ct->info)) {
  39. p = (uint8_t *)*(void **)p;
  40. ct = ctype_get(cts,
  41. lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR));
  42. }
  43. if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
  44. ca->ct[i] = ct;
  45. ca->p[i] = p;
  46. } else if (tvisint(o)) {
  47. ca->ct[i] = ctype_get(cts, CTID_INT32);
  48. ca->p[i] = (uint8_t *)&o->i;
  49. } else if (tvisnum(o)) {
  50. ca->ct[i] = ctype_get(cts, CTID_DOUBLE);
  51. ca->p[i] = (uint8_t *)&o->n;
  52. } else if (tvisnil(o)) {
  53. ca->ct[i] = ctype_get(cts, CTID_P_VOID);
  54. ca->p[i] = (uint8_t *)0;
  55. } else if (tvisstr(o)) {
  56. TValue *o2 = i == 0 ? o+1 : o-1;
  57. CType *ct = ctype_raw(cts, cdataV(o2)->ctypeid);
  58. ca->ct[i] = NULL;
  59. ca->p[i] = NULL;
  60. ok = 0;
  61. if (ctype_isenum(ct->info)) {
  62. CTSize ofs;
  63. CType *cct = lj_ctype_getfield(cts, ct, strV(o), &ofs);
  64. if (cct && ctype_isconstval(cct->info)) {
  65. ca->ct[i] = ctype_child(cts, cct);
  66. ca->p[i] = (uint8_t *)&cct->size; /* Assumes ct does not grow. */
  67. ok = 1;
  68. } else {
  69. ca->ct[1-i] = ct; /* Use enum to improve error message. */
  70. ca->p[1-i] = NULL;
  71. break;
  72. }
  73. }
  74. } else {
  75. ca->ct[i] = NULL;
  76. ca->p[i] = NULL;
  77. ok = 0;
  78. }
  79. }
  80. return ok;
  81. }
  82. /* Pointer arithmetic. */
  83. static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
  84. {
  85. CType *ctp = ca->ct[0];
  86. uint8_t *pp = ca->p[0];
  87. ptrdiff_t idx;
  88. CTSize sz;
  89. CTypeID id;
  90. GCcdata *cd;
  91. if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
  92. if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
  93. (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
  94. uint8_t *pp2 = ca->p[1];
  95. if (mm == MM_eq) { /* Pointer equality. Incompatible pointers are ok. */
  96. setboolV(L->top-1, (pp == pp2));
  97. return 1;
  98. }
  99. if (!lj_cconv_compatptr(cts, ctp, ca->ct[1], CCF_IGNQUAL))
  100. return 0;
  101. if (mm == MM_sub) { /* Pointer difference. */
  102. intptr_t diff;
  103. sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
  104. if (sz == 0 || sz == CTSIZE_INVALID)
  105. return 0;
  106. diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz;
  107. /* All valid pointer differences on x64 are in (-2^47, +2^47),
  108. ** which fits into a double without loss of precision.
  109. */
  110. setintptrV(L->top-1, (int32_t)diff);
  111. return 1;
  112. } else if (mm == MM_lt) { /* Pointer comparison (unsigned). */
  113. setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2));
  114. return 1;
  115. } else {
  116. lua_assert(mm == MM_le);
  117. setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2));
  118. return 1;
  119. }
  120. }
  121. if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(ca->ct[1]->info)))
  122. return 0;
  123. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[1],
  124. (uint8_t *)&idx, ca->p[1], 0);
  125. if (mm == MM_sub) idx = -idx;
  126. } else if (mm == MM_add && ctype_isnum(ctp->info) &&
  127. (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) {
  128. /* Swap pointer and index. */
  129. ctp = ca->ct[1]; pp = ca->p[1];
  130. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[0],
  131. (uint8_t *)&idx, ca->p[0], 0);
  132. } else {
  133. return 0;
  134. }
  135. sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */
  136. if (sz == CTSIZE_INVALID)
  137. return 0;
  138. pp += idx*(int32_t)sz; /* Compute pointer + index. */
  139. id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
  140. CTSIZE_PTR);
  141. cd = lj_cdata_new(cts, id, CTSIZE_PTR);
  142. *(uint8_t **)cdataptr(cd) = pp;
  143. setcdataV(L, L->top-1, cd);
  144. lj_gc_check(L);
  145. return 1;
  146. }
  147. /* 64 bit integer arithmetic. */
  148. static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
  149. {
  150. if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 &&
  151. ctype_isnum(ca->ct[1]->info) && ca->ct[1]->size <= 8) {
  152. CTypeID id = (((ca->ct[0]->info & CTF_UNSIGNED) && ca->ct[0]->size == 8) ||
  153. ((ca->ct[1]->info & CTF_UNSIGNED) && ca->ct[1]->size == 8)) ?
  154. CTID_UINT64 : CTID_INT64;
  155. CType *ct = ctype_get(cts, id);
  156. GCcdata *cd;
  157. uint64_t u0, u1, *up;
  158. lj_cconv_ct_ct(cts, ct, ca->ct[0], (uint8_t *)&u0, ca->p[0], 0);
  159. if (mm != MM_unm)
  160. lj_cconv_ct_ct(cts, ct, ca->ct[1], (uint8_t *)&u1, ca->p[1], 0);
  161. switch (mm) {
  162. case MM_eq:
  163. setboolV(L->top-1, (u0 == u1));
  164. return 1;
  165. case MM_lt:
  166. setboolV(L->top-1,
  167. id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1));
  168. return 1;
  169. case MM_le:
  170. setboolV(L->top-1,
  171. id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1));
  172. return 1;
  173. default: break;
  174. }
  175. cd = lj_cdata_new(cts, id, 8);
  176. up = (uint64_t *)cdataptr(cd);
  177. setcdataV(L, L->top-1, cd);
  178. switch (mm) {
  179. case MM_add: *up = u0 + u1; break;
  180. case MM_sub: *up = u0 - u1; break;
  181. case MM_mul: *up = u0 * u1; break;
  182. case MM_div:
  183. if (id == CTID_INT64)
  184. *up = (uint64_t)lj_carith_divi64((int64_t)u0, (int64_t)u1);
  185. else
  186. *up = lj_carith_divu64(u0, u1);
  187. break;
  188. case MM_mod:
  189. if (id == CTID_INT64)
  190. *up = (uint64_t)lj_carith_modi64((int64_t)u0, (int64_t)u1);
  191. else
  192. *up = lj_carith_modu64(u0, u1);
  193. break;
  194. case MM_pow:
  195. if (id == CTID_INT64)
  196. *up = (uint64_t)lj_carith_powi64((int64_t)u0, (int64_t)u1);
  197. else
  198. *up = lj_carith_powu64(u0, u1);
  199. break;
  200. case MM_unm: *up = (uint64_t)-(int64_t)u0; break;
  201. default: lua_assert(0); break;
  202. }
  203. lj_gc_check(L);
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. /* Handle ctype arithmetic metamethods. */
  209. static int lj_carith_meta(lua_State *L, CTState *cts, CDArith *ca, MMS mm)
  210. {
  211. cTValue *tv = NULL;
  212. if (tviscdata(L->base)) {
  213. CTypeID id = cdataV(L->base)->ctypeid;
  214. CType *ct = ctype_raw(cts, id);
  215. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  216. tv = lj_ctype_meta(cts, id, mm);
  217. }
  218. if (!tv && L->base+1 < L->top && tviscdata(L->base+1)) {
  219. CTypeID id = cdataV(L->base+1)->ctypeid;
  220. CType *ct = ctype_raw(cts, id);
  221. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  222. tv = lj_ctype_meta(cts, id, mm);
  223. }
  224. if (!tv) {
  225. const char *repr[2];
  226. int i, isenum = -1, isstr = -1;
  227. if (mm == MM_eq) { /* Equality checks never raise an error. */
  228. setboolV(L->top-1, 0);
  229. return 1;
  230. }
  231. for (i = 0; i < 2; i++) {
  232. if (ca->ct[i] && tviscdata(L->base+i)) {
  233. if (ctype_isenum(ca->ct[i]->info)) isenum = i;
  234. repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, ca->ct[i]), NULL));
  235. } else {
  236. if (tvisstr(&L->base[i])) isstr = i;
  237. repr[i] = lj_typename(&L->base[i]);
  238. }
  239. }
  240. if ((isenum ^ isstr) == 1)
  241. lj_err_callerv(L, LJ_ERR_FFI_BADCONV, repr[isstr], repr[isenum]);
  242. lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN :
  243. mm == MM_concat ? LJ_ERR_FFI_BADCONCAT :
  244. mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH,
  245. repr[0], repr[1]);
  246. }
  247. return lj_meta_tailcall(L, tv);
  248. }
  249. /* Arithmetic operators for cdata. */
  250. int lj_carith_op(lua_State *L, MMS mm)
  251. {
  252. CTState *cts = ctype_cts(L);
  253. CDArith ca;
  254. if (carith_checkarg(L, cts, &ca)) {
  255. if (carith_int64(L, cts, &ca, mm) || carith_ptr(L, cts, &ca, mm)) {
  256. copyTV(L, &G(L)->tmptv2, L->top-1); /* Remember for trace recorder. */
  257. return 1;
  258. }
  259. }
  260. return lj_carith_meta(L, cts, &ca, mm);
  261. }
  262. /* -- 64 bit integer arithmetic helpers ----------------------------------- */
  263. #if LJ_32 && LJ_HASJIT
  264. /* Signed/unsigned 64 bit multiplication. */
  265. int64_t lj_carith_mul64(int64_t a, int64_t b)
  266. {
  267. return a * b;
  268. }
  269. #endif
  270. /* Unsigned 64 bit division. */
  271. uint64_t lj_carith_divu64(uint64_t a, uint64_t b)
  272. {
  273. if (b == 0) return U64x(80000000,00000000);
  274. return a / b;
  275. }
  276. /* Signed 64 bit division. */
  277. int64_t lj_carith_divi64(int64_t a, int64_t b)
  278. {
  279. if (b == 0 || (a == (int64_t)U64x(80000000,00000000) && b == -1))
  280. return U64x(80000000,00000000);
  281. return a / b;
  282. }
  283. /* Unsigned 64 bit modulo. */
  284. uint64_t lj_carith_modu64(uint64_t a, uint64_t b)
  285. {
  286. if (b == 0) return U64x(80000000,00000000);
  287. return a % b;
  288. }
  289. /* Signed 64 bit modulo. */
  290. int64_t lj_carith_modi64(int64_t a, int64_t b)
  291. {
  292. if (b == 0) return U64x(80000000,00000000);
  293. if (a == (int64_t)U64x(80000000,00000000) && b == -1) return 0;
  294. return a % b;
  295. }
  296. /* Unsigned 64 bit x^k. */
  297. uint64_t lj_carith_powu64(uint64_t x, uint64_t k)
  298. {
  299. uint64_t y;
  300. if (k == 0)
  301. return 1;
  302. for (; (k & 1) == 0; k >>= 1) x *= x;
  303. y = x;
  304. if ((k >>= 1) != 0) {
  305. for (;;) {
  306. x *= x;
  307. if (k == 1) break;
  308. if (k & 1) y *= x;
  309. k >>= 1;
  310. }
  311. y *= x;
  312. }
  313. return y;
  314. }
  315. /* Signed 64 bit x^k. */
  316. int64_t lj_carith_powi64(int64_t x, int64_t k)
  317. {
  318. if (k == 0)
  319. return 1;
  320. if (k < 0) {
  321. if (x == 0)
  322. return U64x(7fffffff,ffffffff);
  323. else if (x == 1)
  324. return 1;
  325. else if (x == -1)
  326. return (k & 1) ? -1 : 1;
  327. else
  328. return 0;
  329. }
  330. return (int64_t)lj_carith_powu64((uint64_t)x, (uint64_t)k);
  331. }
  332. #endif