lj_ffrecord.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. /*
  2. ** Fast function call recorder.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #define lj_ffrecord_c
  6. #define LUA_CORE
  7. #include "lj_obj.h"
  8. #if LJ_HASJIT
  9. #include "lj_err.h"
  10. #include "lj_buf.h"
  11. #include "lj_str.h"
  12. #include "lj_tab.h"
  13. #include "lj_frame.h"
  14. #include "lj_bc.h"
  15. #include "lj_ff.h"
  16. #include "lj_ir.h"
  17. #include "lj_jit.h"
  18. #include "lj_ircall.h"
  19. #include "lj_iropt.h"
  20. #include "lj_trace.h"
  21. #include "lj_record.h"
  22. #include "lj_ffrecord.h"
  23. #include "lj_crecord.h"
  24. #include "lj_dispatch.h"
  25. #include "lj_vm.h"
  26. #include "lj_strscan.h"
  27. #include "lj_strfmt.h"
  28. #include "lj_serialize.h"
  29. /* Some local macros to save typing. Undef'd at the end. */
  30. #define IR(ref) (&J->cur.ir[(ref)])
  31. /* Pass IR on to next optimization in chain (FOLD). */
  32. #define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
  33. /* -- Fast function recording handlers ------------------------------------ */
  34. /* Conventions for fast function call handlers:
  35. **
  36. ** The argument slots start at J->base[0]. All of them are guaranteed to be
  37. ** valid and type-specialized references. J->base[J->maxslot] is set to 0
  38. ** as a sentinel. The runtime argument values start at rd->argv[0].
  39. **
  40. ** In general fast functions should check for presence of all of their
  41. ** arguments and for the correct argument types. Some simplifications
  42. ** are allowed if the interpreter throws instead. But even if recording
  43. ** is aborted, the generated IR must be consistent (no zero-refs).
  44. **
  45. ** The number of results in rd->nres is set to 1. Handlers that return
  46. ** a different number of results need to override it. A negative value
  47. ** prevents return processing (e.g. for pending calls).
  48. **
  49. ** Results need to be stored starting at J->base[0]. Return processing
  50. ** moves them to the right slots later.
  51. **
  52. ** The per-ffid auxiliary data is the value of the 2nd part of the
  53. ** LJLIB_REC() annotation. This allows handling similar functionality
  54. ** in a common handler.
  55. */
  56. /* Type of handler to record a fast function. */
  57. typedef void (LJ_FASTCALL *RecordFunc)(jit_State *J, RecordFFData *rd);
  58. /* Get runtime value of int argument. */
  59. static int32_t argv2int(jit_State *J, TValue *o)
  60. {
  61. if (!lj_strscan_numberobj(o))
  62. lj_trace_err(J, LJ_TRERR_BADTYPE);
  63. return tvisint(o) ? intV(o) : lj_num2int(numV(o));
  64. }
  65. /* Get runtime value of string argument. */
  66. static GCstr *argv2str(jit_State *J, TValue *o)
  67. {
  68. if (LJ_LIKELY(tvisstr(o))) {
  69. return strV(o);
  70. } else {
  71. GCstr *s;
  72. if (!tvisnumber(o))
  73. lj_trace_err(J, LJ_TRERR_BADTYPE);
  74. s = lj_strfmt_number(J->L, o);
  75. setstrV(J->L, o, s);
  76. return s;
  77. }
  78. }
  79. /* Return number of results wanted by caller. */
  80. static ptrdiff_t results_wanted(jit_State *J)
  81. {
  82. TValue *frame = J->L->base-1;
  83. if (frame_islua(frame))
  84. return (ptrdiff_t)bc_b(frame_pc(frame)[-1]) - 1;
  85. else
  86. return -1;
  87. }
  88. /* Trace stitching: add continuation below frame to start a new trace. */
  89. static void recff_stitch(jit_State *J)
  90. {
  91. ASMFunction cont = lj_cont_stitch;
  92. lua_State *L = J->L;
  93. TValue *base = L->base;
  94. BCReg nslot = J->maxslot + 1 + LJ_FR2;
  95. TValue *nframe = base + 1 + LJ_FR2;
  96. const BCIns *pc = frame_pc(base-1);
  97. TValue *pframe = frame_prevl(base-1);
  98. /* Check for this now. Throwing in lj_record_stop messes up the stack. */
  99. if (J->cur.nsnap >= (MSize)J->param[JIT_P_maxsnap])
  100. lj_trace_err(J, LJ_TRERR_SNAPOV);
  101. /* Move func + args up in Lua stack and insert continuation. */
  102. memmove(&base[1], &base[-1-LJ_FR2], sizeof(TValue)*nslot);
  103. setframe_ftsz(nframe, ((char *)nframe - (char *)pframe) + FRAME_CONT);
  104. setcont(base-LJ_FR2, cont);
  105. setframe_pc(base, pc);
  106. setnilV(base-1-LJ_FR2); /* Incorrect, but rec_check_slots() won't run anymore. */
  107. L->base += 2 + LJ_FR2;
  108. L->top += 2 + LJ_FR2;
  109. /* Ditto for the IR. */
  110. memmove(&J->base[1], &J->base[-1-LJ_FR2], sizeof(TRef)*nslot);
  111. #if LJ_FR2
  112. J->base[2] = TREF_FRAME;
  113. J->base[-1] = lj_ir_k64(J, IR_KNUM, u64ptr(contptr(cont)));
  114. J->base[0] = lj_ir_k64(J, IR_KNUM, u64ptr(pc)) | TREF_CONT;
  115. #else
  116. J->base[0] = lj_ir_kptr(J, contptr(cont)) | TREF_CONT;
  117. #endif
  118. J->ktrace = tref_ref((J->base[-1-LJ_FR2] = lj_ir_ktrace(J)));
  119. J->base += 2 + LJ_FR2;
  120. J->baseslot += 2 + LJ_FR2;
  121. J->framedepth++;
  122. lj_record_stop(J, LJ_TRLINK_STITCH, 0);
  123. /* Undo Lua stack changes. */
  124. memmove(&base[-1-LJ_FR2], &base[1], sizeof(TValue)*nslot);
  125. setframe_pc(base-1, pc);
  126. L->base -= 2 + LJ_FR2;
  127. L->top -= 2 + LJ_FR2;
  128. }
  129. /* Fallback handler for fast functions that are not recorded (yet). */
  130. static void LJ_FASTCALL recff_nyi(jit_State *J, RecordFFData *rd)
  131. {
  132. if (J->cur.nins < (IRRef)J->param[JIT_P_minstitch] + REF_BASE) {
  133. lj_trace_err_info(J, LJ_TRERR_TRACEUV);
  134. } else {
  135. /* Can only stitch from Lua call. */
  136. if (J->framedepth && frame_islua(J->L->base-1)) {
  137. BCOp op = bc_op(*frame_pc(J->L->base-1));
  138. /* Stitched trace cannot start with *M op with variable # of args. */
  139. if (!(op == BC_CALLM || op == BC_CALLMT ||
  140. op == BC_RETM || op == BC_TSETM)) {
  141. switch (J->fn->c.ffid) {
  142. case FF_error:
  143. case FF_debug_sethook:
  144. case FF_jit_flush:
  145. break; /* Don't stitch across special builtins. */
  146. default:
  147. recff_stitch(J); /* Use trace stitching. */
  148. rd->nres = -1;
  149. return;
  150. }
  151. }
  152. }
  153. /* Otherwise stop trace and return to interpreter. */
  154. lj_record_stop(J, LJ_TRLINK_RETURN, 0);
  155. rd->nres = -1;
  156. }
  157. }
  158. /* Fallback handler for unsupported variants of fast functions. */
  159. #define recff_nyiu recff_nyi
  160. /* Must stop the trace for classic C functions with arbitrary side-effects. */
  161. #define recff_c recff_nyi
  162. /* Emit BUFHDR for the global temporary buffer. */
  163. static TRef recff_bufhdr(jit_State *J)
  164. {
  165. return emitir(IRT(IR_BUFHDR, IRT_PGC),
  166. lj_ir_kptr(J, &J2G(J)->tmpbuf), IRBUFHDR_RESET);
  167. }
  168. /* Emit TMPREF. */
  169. static TRef recff_tmpref(jit_State *J, TRef tr, int mode)
  170. {
  171. if (!LJ_DUALNUM && tref_isinteger(tr))
  172. tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT);
  173. return emitir(IRT(IR_TMPREF, IRT_PGC), tr, mode);
  174. }
  175. /* -- Base library fast functions ----------------------------------------- */
  176. static void LJ_FASTCALL recff_assert(jit_State *J, RecordFFData *rd)
  177. {
  178. /* Arguments already specialized. The interpreter throws for nil/false. */
  179. rd->nres = J->maxslot; /* Pass through all arguments. */
  180. }
  181. static void LJ_FASTCALL recff_type(jit_State *J, RecordFFData *rd)
  182. {
  183. /* Arguments already specialized. Result is a constant string. Neat, huh? */
  184. uint32_t t;
  185. if (tvisnumber(&rd->argv[0]))
  186. t = ~LJ_TNUMX;
  187. else if (LJ_64 && !LJ_GC64 && tvislightud(&rd->argv[0]))
  188. t = ~LJ_TLIGHTUD;
  189. else
  190. t = ~itype(&rd->argv[0]);
  191. J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[t]));
  192. UNUSED(rd);
  193. }
  194. static void LJ_FASTCALL recff_getmetatable(jit_State *J, RecordFFData *rd)
  195. {
  196. TRef tr = J->base[0];
  197. if (tr) {
  198. RecordIndex ix;
  199. ix.tab = tr;
  200. copyTV(J->L, &ix.tabv, &rd->argv[0]);
  201. if (lj_record_mm_lookup(J, &ix, MM_metatable))
  202. J->base[0] = ix.mobj;
  203. else
  204. J->base[0] = ix.mt;
  205. } /* else: Interpreter will throw. */
  206. }
  207. static void LJ_FASTCALL recff_setmetatable(jit_State *J, RecordFFData *rd)
  208. {
  209. TRef tr = J->base[0];
  210. TRef mt = J->base[1];
  211. if (tref_istab(tr) && (tref_istab(mt) || (mt && tref_isnil(mt)))) {
  212. TRef fref, mtref;
  213. RecordIndex ix;
  214. ix.tab = tr;
  215. copyTV(J->L, &ix.tabv, &rd->argv[0]);
  216. lj_record_mm_lookup(J, &ix, MM_metatable); /* Guard for no __metatable. */
  217. fref = emitir(IRT(IR_FREF, IRT_PGC), tr, IRFL_TAB_META);
  218. mtref = tref_isnil(mt) ? lj_ir_knull(J, IRT_TAB) : mt;
  219. emitir(IRT(IR_FSTORE, IRT_TAB), fref, mtref);
  220. if (!tref_isnil(mt))
  221. emitir(IRT(IR_TBAR, IRT_TAB), tr, 0);
  222. J->base[0] = tr;
  223. J->needsnap = 1;
  224. } /* else: Interpreter will throw. */
  225. }
  226. static void LJ_FASTCALL recff_rawget(jit_State *J, RecordFFData *rd)
  227. {
  228. RecordIndex ix;
  229. ix.tab = J->base[0]; ix.key = J->base[1];
  230. if (tref_istab(ix.tab) && ix.key) {
  231. ix.val = 0; ix.idxchain = 0;
  232. settabV(J->L, &ix.tabv, tabV(&rd->argv[0]));
  233. copyTV(J->L, &ix.keyv, &rd->argv[1]);
  234. J->base[0] = lj_record_idx(J, &ix);
  235. } /* else: Interpreter will throw. */
  236. }
  237. static void LJ_FASTCALL recff_rawset(jit_State *J, RecordFFData *rd)
  238. {
  239. RecordIndex ix;
  240. ix.tab = J->base[0]; ix.key = J->base[1]; ix.val = J->base[2];
  241. if (tref_istab(ix.tab) && ix.key && ix.val) {
  242. ix.idxchain = 0;
  243. settabV(J->L, &ix.tabv, tabV(&rd->argv[0]));
  244. copyTV(J->L, &ix.keyv, &rd->argv[1]);
  245. copyTV(J->L, &ix.valv, &rd->argv[2]);
  246. lj_record_idx(J, &ix);
  247. /* Pass through table at J->base[0] as result. */
  248. } /* else: Interpreter will throw. */
  249. }
  250. static void LJ_FASTCALL recff_rawequal(jit_State *J, RecordFFData *rd)
  251. {
  252. TRef tra = J->base[0];
  253. TRef trb = J->base[1];
  254. if (tra && trb) {
  255. int diff = lj_record_objcmp(J, tra, trb, &rd->argv[0], &rd->argv[1]);
  256. J->base[0] = diff ? TREF_FALSE : TREF_TRUE;
  257. } /* else: Interpreter will throw. */
  258. }
  259. #if LJ_52
  260. static void LJ_FASTCALL recff_rawlen(jit_State *J, RecordFFData *rd)
  261. {
  262. TRef tr = J->base[0];
  263. if (tref_isstr(tr))
  264. J->base[0] = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN);
  265. else if (tref_istab(tr))
  266. J->base[0] = emitir(IRTI(IR_ALEN), tr, TREF_NIL);
  267. /* else: Interpreter will throw. */
  268. UNUSED(rd);
  269. }
  270. #endif
  271. /* Determine mode of select() call. */
  272. int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv)
  273. {
  274. if (tref_isstr(tr) && *strVdata(tv) == '#') { /* select('#', ...) */
  275. if (strV(tv)->len == 1) {
  276. emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, strV(tv)));
  277. } else {
  278. TRef trptr = emitir(IRT(IR_STRREF, IRT_PGC), tr, lj_ir_kint(J, 0));
  279. TRef trchar = emitir(IRT(IR_XLOAD, IRT_U8), trptr, IRXLOAD_READONLY);
  280. emitir(IRTGI(IR_EQ), trchar, lj_ir_kint(J, '#'));
  281. }
  282. return 0;
  283. } else { /* select(n, ...) */
  284. int32_t start = argv2int(J, tv);
  285. if (start == 0) lj_trace_err(J, LJ_TRERR_BADTYPE); /* A bit misleading. */
  286. return start;
  287. }
  288. }
  289. static void LJ_FASTCALL recff_select(jit_State *J, RecordFFData *rd)
  290. {
  291. TRef tr = J->base[0];
  292. if (tr) {
  293. ptrdiff_t start = lj_ffrecord_select_mode(J, tr, &rd->argv[0]);
  294. if (start == 0) { /* select('#', ...) */
  295. J->base[0] = lj_ir_kint(J, J->maxslot - 1);
  296. } else if (tref_isk(tr)) { /* select(k, ...) */
  297. ptrdiff_t n = (ptrdiff_t)J->maxslot;
  298. if (start < 0) start += n;
  299. else if (start > n) start = n;
  300. if (start >= 1) {
  301. ptrdiff_t i;
  302. rd->nres = n - start;
  303. for (i = 0; i < n - start; i++)
  304. J->base[i] = J->base[start+i];
  305. } /* else: Interpreter will throw. */
  306. } else {
  307. recff_nyiu(J, rd);
  308. return;
  309. }
  310. } /* else: Interpreter will throw. */
  311. }
  312. static void LJ_FASTCALL recff_tonumber(jit_State *J, RecordFFData *rd)
  313. {
  314. TRef tr = J->base[0];
  315. TRef base = J->base[1];
  316. if (tr && !tref_isnil(base)) {
  317. base = lj_opt_narrow_toint(J, base);
  318. if (!tref_isk(base) || IR(tref_ref(base))->i != 10) {
  319. recff_nyiu(J, rd);
  320. return;
  321. }
  322. }
  323. if (tref_isnumber_str(tr)) {
  324. if (tref_isstr(tr)) {
  325. TValue tmp;
  326. if (!lj_strscan_num(strV(&rd->argv[0]), &tmp)) {
  327. recff_nyiu(J, rd); /* Would need an inverted STRTO for this case. */
  328. return;
  329. }
  330. tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);
  331. }
  332. #if LJ_HASFFI
  333. } else if (tref_iscdata(tr)) {
  334. lj_crecord_tonumber(J, rd);
  335. return;
  336. #endif
  337. } else {
  338. tr = TREF_NIL;
  339. }
  340. J->base[0] = tr;
  341. UNUSED(rd);
  342. }
  343. static TValue *recff_metacall_cp(lua_State *L, lua_CFunction dummy, void *ud)
  344. {
  345. jit_State *J = (jit_State *)ud;
  346. lj_record_tailcall(J, 0, 1);
  347. UNUSED(L); UNUSED(dummy);
  348. return NULL;
  349. }
  350. static int recff_metacall(jit_State *J, RecordFFData *rd, MMS mm)
  351. {
  352. RecordIndex ix;
  353. ix.tab = J->base[0];
  354. copyTV(J->L, &ix.tabv, &rd->argv[0]);
  355. if (lj_record_mm_lookup(J, &ix, mm)) { /* Has metamethod? */
  356. int errcode;
  357. TValue argv0;
  358. /* Temporarily insert metamethod below object. */
  359. J->base[1+LJ_FR2] = J->base[0];
  360. J->base[0] = ix.mobj;
  361. copyTV(J->L, &argv0, &rd->argv[0]);
  362. copyTV(J->L, &rd->argv[1+LJ_FR2], &rd->argv[0]);
  363. copyTV(J->L, &rd->argv[0], &ix.mobjv);
  364. /* Need to protect lj_record_tailcall because it may throw. */
  365. errcode = lj_vm_cpcall(J->L, NULL, J, recff_metacall_cp);
  366. /* Always undo Lua stack changes to avoid confusing the interpreter. */
  367. copyTV(J->L, &rd->argv[0], &argv0);
  368. if (errcode)
  369. lj_err_throw(J->L, errcode); /* Propagate errors. */
  370. rd->nres = -1; /* Pending call. */
  371. return 1; /* Tailcalled to metamethod. */
  372. }
  373. return 0;
  374. }
  375. static void LJ_FASTCALL recff_tostring(jit_State *J, RecordFFData *rd)
  376. {
  377. TRef tr = J->base[0];
  378. if (tref_isstr(tr)) {
  379. /* Ignore __tostring in the string base metatable. */
  380. /* Pass on result in J->base[0]. */
  381. } else if (tr && !recff_metacall(J, rd, MM_tostring)) {
  382. if (tref_isnumber(tr)) {
  383. J->base[0] = emitir(IRT(IR_TOSTR, IRT_STR), tr,
  384. tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT);
  385. } else if (tref_ispri(tr)) {
  386. J->base[0] = lj_ir_kstr(J, lj_strfmt_obj(J->L, &rd->argv[0]));
  387. } else {
  388. recff_nyiu(J, rd);
  389. return;
  390. }
  391. }
  392. }
  393. static void LJ_FASTCALL recff_ipairs_aux(jit_State *J, RecordFFData *rd)
  394. {
  395. RecordIndex ix;
  396. ix.tab = J->base[0];
  397. if (tref_istab(ix.tab)) {
  398. if (!tvisnumber(&rd->argv[1])) /* No support for string coercion. */
  399. lj_trace_err(J, LJ_TRERR_BADTYPE);
  400. setintV(&ix.keyv, numberVint(&rd->argv[1])+1);
  401. settabV(J->L, &ix.tabv, tabV(&rd->argv[0]));
  402. ix.val = 0; ix.idxchain = 0;
  403. ix.key = lj_opt_narrow_toint(J, J->base[1]);
  404. J->base[0] = ix.key = emitir(IRTI(IR_ADD), ix.key, lj_ir_kint(J, 1));
  405. J->base[1] = lj_record_idx(J, &ix);
  406. rd->nres = tref_isnil(J->base[1]) ? 0 : 2;
  407. } /* else: Interpreter will throw. */
  408. }
  409. static void LJ_FASTCALL recff_xpairs(jit_State *J, RecordFFData *rd)
  410. {
  411. TRef tr = J->base[0];
  412. if (!((LJ_52 || (LJ_HASFFI && tref_iscdata(tr))) &&
  413. recff_metacall(J, rd, MM_pairs + rd->data))) {
  414. if (tref_istab(tr)) {
  415. J->base[0] = lj_ir_kfunc(J, funcV(&J->fn->c.upvalue[0]));
  416. J->base[1] = tr;
  417. J->base[2] = rd->data ? lj_ir_kint(J, 0) : TREF_NIL;
  418. rd->nres = 3;
  419. } /* else: Interpreter will throw. */
  420. }
  421. }
  422. static void LJ_FASTCALL recff_pcall(jit_State *J, RecordFFData *rd)
  423. {
  424. if (J->maxslot >= 1) {
  425. #if LJ_FR2
  426. /* Shift function arguments up. */
  427. memmove(J->base + 1, J->base, sizeof(TRef) * J->maxslot);
  428. #endif
  429. lj_record_call(J, 0, J->maxslot - 1);
  430. rd->nres = -1; /* Pending call. */
  431. J->needsnap = 1; /* Start catching on-trace errors. */
  432. } /* else: Interpreter will throw. */
  433. }
  434. static TValue *recff_xpcall_cp(lua_State *L, lua_CFunction dummy, void *ud)
  435. {
  436. jit_State *J = (jit_State *)ud;
  437. lj_record_call(J, 1, J->maxslot - 2);
  438. UNUSED(L); UNUSED(dummy);
  439. return NULL;
  440. }
  441. static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd)
  442. {
  443. if (J->maxslot >= 2) {
  444. TValue argv0, argv1;
  445. TRef tmp;
  446. int errcode;
  447. /* Swap function and traceback. */
  448. tmp = J->base[0]; J->base[0] = J->base[1]; J->base[1] = tmp;
  449. copyTV(J->L, &argv0, &rd->argv[0]);
  450. copyTV(J->L, &argv1, &rd->argv[1]);
  451. copyTV(J->L, &rd->argv[0], &argv1);
  452. copyTV(J->L, &rd->argv[1], &argv0);
  453. #if LJ_FR2
  454. /* Shift function arguments up. */
  455. memmove(J->base + 2, J->base + 1, sizeof(TRef) * (J->maxslot-1));
  456. #endif
  457. /* Need to protect lj_record_call because it may throw. */
  458. errcode = lj_vm_cpcall(J->L, NULL, J, recff_xpcall_cp);
  459. /* Always undo Lua stack swap to avoid confusing the interpreter. */
  460. copyTV(J->L, &rd->argv[0], &argv0);
  461. copyTV(J->L, &rd->argv[1], &argv1);
  462. if (errcode)
  463. lj_err_throw(J->L, errcode); /* Propagate errors. */
  464. rd->nres = -1; /* Pending call. */
  465. J->needsnap = 1; /* Start catching on-trace errors. */
  466. } /* else: Interpreter will throw. */
  467. }
  468. static void LJ_FASTCALL recff_getfenv(jit_State *J, RecordFFData *rd)
  469. {
  470. TRef tr = J->base[0];
  471. /* Only support getfenv(0) for now. */
  472. if (tref_isint(tr) && tref_isk(tr) && IR(tref_ref(tr))->i == 0) {
  473. TRef trl = emitir(IRT(IR_LREF, IRT_THREAD), 0, 0);
  474. J->base[0] = emitir(IRT(IR_FLOAD, IRT_TAB), trl, IRFL_THREAD_ENV);
  475. return;
  476. }
  477. recff_nyiu(J, rd);
  478. }
  479. static void LJ_FASTCALL recff_next(jit_State *J, RecordFFData *rd)
  480. {
  481. #if LJ_BE
  482. /* YAGNI: Disabled on big-endian due to issues with lj_vm_next,
  483. ** IR_HIOP, RID_RETLO/RID_RETHI and ra_destpair.
  484. */
  485. recff_nyi(J, rd);
  486. #else
  487. TRef tab = J->base[0];
  488. if (tref_istab(tab)) {
  489. RecordIndex ix;
  490. cTValue *keyv;
  491. ix.tab = tab;
  492. if (tref_isnil(J->base[1])) { /* Shortcut for start of traversal. */
  493. ix.key = lj_ir_kint(J, 0);
  494. keyv = niltvg(J2G(J));
  495. } else {
  496. TRef tmp = recff_tmpref(J, J->base[1], IRTMPREF_IN1);
  497. ix.key = lj_ir_call(J, IRCALL_lj_tab_keyindex, tab, tmp);
  498. keyv = &rd->argv[1];
  499. }
  500. copyTV(J->L, &ix.tabv, &rd->argv[0]);
  501. ix.keyv.u32.lo = lj_tab_keyindex(tabV(&ix.tabv), keyv);
  502. /* Omit the value, if not used by the caller. */
  503. ix.idxchain = (J->framedepth && frame_islua(J->L->base-1) &&
  504. bc_b(frame_pc(J->L->base-1)[-1])-1 < 2);
  505. ix.mobj = 0; /* We don't need the next index. */
  506. rd->nres = lj_record_next(J, &ix);
  507. J->base[0] = ix.key;
  508. J->base[1] = ix.val;
  509. } /* else: Interpreter will throw. */
  510. #endif
  511. }
  512. /* -- Math library fast functions ----------------------------------------- */
  513. static void LJ_FASTCALL recff_math_abs(jit_State *J, RecordFFData *rd)
  514. {
  515. TRef tr = lj_ir_tonum(J, J->base[0]);
  516. J->base[0] = emitir(IRTN(IR_ABS), tr, lj_ir_ksimd(J, LJ_KSIMD_ABS));
  517. UNUSED(rd);
  518. }
  519. /* Record rounding functions math.floor and math.ceil. */
  520. static void LJ_FASTCALL recff_math_round(jit_State *J, RecordFFData *rd)
  521. {
  522. TRef tr = J->base[0];
  523. if (!tref_isinteger(tr)) { /* Pass through integers unmodified. */
  524. tr = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, tr), rd->data);
  525. /* Result is integral (or NaN/Inf), but may not fit an int32_t. */
  526. if (LJ_DUALNUM) { /* Try to narrow using a guarded conversion to int. */
  527. lua_Number n = lj_vm_foldfpm(numberVnum(&rd->argv[0]), rd->data);
  528. if (n == (lua_Number)lj_num2int(n))
  529. tr = emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_CHECK);
  530. }
  531. J->base[0] = tr;
  532. }
  533. }
  534. /* Record unary math.* functions, mapped to IR_FPMATH opcode. */
  535. static void LJ_FASTCALL recff_math_unary(jit_State *J, RecordFFData *rd)
  536. {
  537. J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data);
  538. }
  539. /* Record math.log. */
  540. static void LJ_FASTCALL recff_math_log(jit_State *J, RecordFFData *rd)
  541. {
  542. TRef tr = lj_ir_tonum(J, J->base[0]);
  543. if (J->base[1]) {
  544. #ifdef LUAJIT_NO_LOG2
  545. uint32_t fpm = IRFPM_LOG;
  546. #else
  547. uint32_t fpm = IRFPM_LOG2;
  548. #endif
  549. TRef trb = lj_ir_tonum(J, J->base[1]);
  550. tr = emitir(IRTN(IR_FPMATH), tr, fpm);
  551. trb = emitir(IRTN(IR_FPMATH), trb, fpm);
  552. trb = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), trb);
  553. tr = emitir(IRTN(IR_MUL), tr, trb);
  554. } else {
  555. tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_LOG);
  556. }
  557. J->base[0] = tr;
  558. UNUSED(rd);
  559. }
  560. /* Record math.atan2. */
  561. static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd)
  562. {
  563. TRef tr = lj_ir_tonum(J, J->base[0]);
  564. TRef tr2 = lj_ir_tonum(J, J->base[1]);
  565. J->base[0] = lj_ir_call(J, IRCALL_atan2, tr, tr2);
  566. UNUSED(rd);
  567. }
  568. /* Record math.ldexp. */
  569. static void LJ_FASTCALL recff_math_ldexp(jit_State *J, RecordFFData *rd)
  570. {
  571. TRef tr = lj_ir_tonum(J, J->base[0]);
  572. #if LJ_TARGET_X86ORX64
  573. TRef tr2 = lj_ir_tonum(J, J->base[1]);
  574. #else
  575. TRef tr2 = lj_opt_narrow_toint(J, J->base[1]);
  576. #endif
  577. J->base[0] = emitir(IRTN(IR_LDEXP), tr, tr2);
  578. UNUSED(rd);
  579. }
  580. static void LJ_FASTCALL recff_math_call(jit_State *J, RecordFFData *rd)
  581. {
  582. TRef tr = lj_ir_tonum(J, J->base[0]);
  583. J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data);
  584. }
  585. static void LJ_FASTCALL recff_math_pow(jit_State *J, RecordFFData *rd)
  586. {
  587. J->base[0] = lj_opt_narrow_arith(J, J->base[0], J->base[1],
  588. &rd->argv[0], &rd->argv[1], IR_POW);
  589. UNUSED(rd);
  590. }
  591. static void LJ_FASTCALL recff_math_minmax(jit_State *J, RecordFFData *rd)
  592. {
  593. TRef tr = lj_ir_tonumber(J, J->base[0]);
  594. uint32_t op = rd->data;
  595. BCReg i;
  596. for (i = 1; J->base[i] != 0; i++) {
  597. TRef tr2 = lj_ir_tonumber(J, J->base[i]);
  598. IRType t = IRT_INT;
  599. if (!(tref_isinteger(tr) && tref_isinteger(tr2))) {
  600. if (tref_isinteger(tr)) tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT);
  601. if (tref_isinteger(tr2)) tr2 = emitir(IRTN(IR_CONV), tr2, IRCONV_NUM_INT);
  602. t = IRT_NUM;
  603. }
  604. tr = emitir(IRT(op, t), tr, tr2);
  605. }
  606. J->base[0] = tr;
  607. }
  608. static void LJ_FASTCALL recff_math_random(jit_State *J, RecordFFData *rd)
  609. {
  610. GCudata *ud = udataV(&J->fn->c.upvalue[0]);
  611. TRef tr, one;
  612. lj_ir_kgc(J, obj2gco(ud), IRT_UDATA); /* Prevent collection. */
  613. tr = lj_ir_call(J, IRCALL_lj_prng_u64d, lj_ir_kptr(J, uddata(ud)));
  614. one = lj_ir_knum_one(J);
  615. tr = emitir(IRTN(IR_SUB), tr, one);
  616. if (J->base[0]) {
  617. TRef tr1 = lj_ir_tonum(J, J->base[0]);
  618. if (J->base[1]) { /* d = floor(d*(r2-r1+1.0)) + r1 */
  619. TRef tr2 = lj_ir_tonum(J, J->base[1]);
  620. tr2 = emitir(IRTN(IR_SUB), tr2, tr1);
  621. tr2 = emitir(IRTN(IR_ADD), tr2, one);
  622. tr = emitir(IRTN(IR_MUL), tr, tr2);
  623. tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR);
  624. tr = emitir(IRTN(IR_ADD), tr, tr1);
  625. } else { /* d = floor(d*r1) + 1.0 */
  626. tr = emitir(IRTN(IR_MUL), tr, tr1);
  627. tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR);
  628. tr = emitir(IRTN(IR_ADD), tr, one);
  629. }
  630. }
  631. J->base[0] = tr;
  632. UNUSED(rd);
  633. }
  634. /* -- Bit library fast functions ------------------------------------------ */
  635. /* Record bit.tobit. */
  636. static void LJ_FASTCALL recff_bit_tobit(jit_State *J, RecordFFData *rd)
  637. {
  638. TRef tr = J->base[0];
  639. #if LJ_HASFFI
  640. if (tref_iscdata(tr)) { recff_bit64_tobit(J, rd); return; }
  641. #endif
  642. J->base[0] = lj_opt_narrow_tobit(J, tr);
  643. UNUSED(rd);
  644. }
  645. /* Record unary bit.bnot, bit.bswap. */
  646. static void LJ_FASTCALL recff_bit_unary(jit_State *J, RecordFFData *rd)
  647. {
  648. #if LJ_HASFFI
  649. if (recff_bit64_unary(J, rd))
  650. return;
  651. #endif
  652. J->base[0] = emitir(IRTI(rd->data), lj_opt_narrow_tobit(J, J->base[0]), 0);
  653. }
  654. /* Record N-ary bit.band, bit.bor, bit.bxor. */
  655. static void LJ_FASTCALL recff_bit_nary(jit_State *J, RecordFFData *rd)
  656. {
  657. #if LJ_HASFFI
  658. if (recff_bit64_nary(J, rd))
  659. return;
  660. #endif
  661. {
  662. TRef tr = lj_opt_narrow_tobit(J, J->base[0]);
  663. uint32_t ot = IRTI(rd->data);
  664. BCReg i;
  665. for (i = 1; J->base[i] != 0; i++)
  666. tr = emitir(ot, tr, lj_opt_narrow_tobit(J, J->base[i]));
  667. J->base[0] = tr;
  668. }
  669. }
  670. /* Record bit shifts. */
  671. static void LJ_FASTCALL recff_bit_shift(jit_State *J, RecordFFData *rd)
  672. {
  673. #if LJ_HASFFI
  674. if (recff_bit64_shift(J, rd))
  675. return;
  676. #endif
  677. {
  678. TRef tr = lj_opt_narrow_tobit(J, J->base[0]);
  679. TRef tsh = lj_opt_narrow_tobit(J, J->base[1]);
  680. IROp op = (IROp)rd->data;
  681. if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) &&
  682. !tref_isk(tsh))
  683. tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 31));
  684. #ifdef LJ_TARGET_UNIFYROT
  685. if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) {
  686. op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR;
  687. tsh = emitir(IRTI(IR_NEG), tsh, tsh);
  688. }
  689. #endif
  690. J->base[0] = emitir(IRTI(op), tr, tsh);
  691. }
  692. }
  693. static void LJ_FASTCALL recff_bit_tohex(jit_State *J, RecordFFData *rd)
  694. {
  695. #if LJ_HASFFI
  696. TRef hdr = recff_bufhdr(J);
  697. TRef tr = recff_bit64_tohex(J, rd, hdr);
  698. J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr);
  699. #else
  700. recff_nyiu(J, rd); /* Don't bother working around this NYI. */
  701. #endif
  702. }
  703. /* -- String library fast functions --------------------------------------- */
  704. /* Specialize to relative starting position for string. */
  705. static TRef recff_string_start(jit_State *J, GCstr *s, int32_t *st, TRef tr,
  706. TRef trlen, TRef tr0)
  707. {
  708. int32_t start = *st;
  709. if (start < 0) {
  710. emitir(IRTGI(IR_LT), tr, tr0);
  711. tr = emitir(IRTI(IR_ADD), trlen, tr);
  712. start = start + (int32_t)s->len;
  713. emitir(start < 0 ? IRTGI(IR_LT) : IRTGI(IR_GE), tr, tr0);
  714. if (start < 0) {
  715. tr = tr0;
  716. start = 0;
  717. }
  718. } else if (start == 0) {
  719. emitir(IRTGI(IR_EQ), tr, tr0);
  720. tr = tr0;
  721. } else {
  722. tr = emitir(IRTI(IR_ADD), tr, lj_ir_kint(J, -1));
  723. emitir(IRTGI(IR_GE), tr, tr0);
  724. start--;
  725. }
  726. *st = start;
  727. return tr;
  728. }
  729. /* Handle string.byte (rd->data = 0) and string.sub (rd->data = 1). */
  730. static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd)
  731. {
  732. TRef trstr = lj_ir_tostr(J, J->base[0]);
  733. TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN);
  734. TRef tr0 = lj_ir_kint(J, 0);
  735. TRef trstart, trend;
  736. GCstr *str = argv2str(J, &rd->argv[0]);
  737. int32_t start, end;
  738. if (rd->data) { /* string.sub(str, start [,end]) */
  739. start = argv2int(J, &rd->argv[1]);
  740. trstart = lj_opt_narrow_toint(J, J->base[1]);
  741. trend = J->base[2];
  742. if (tref_isnil(trend)) {
  743. trend = lj_ir_kint(J, -1);
  744. end = -1;
  745. } else {
  746. trend = lj_opt_narrow_toint(J, trend);
  747. end = argv2int(J, &rd->argv[2]);
  748. }
  749. } else { /* string.byte(str, [,start [,end]]) */
  750. if (tref_isnil(J->base[1])) {
  751. start = 1;
  752. trstart = lj_ir_kint(J, 1);
  753. } else {
  754. start = argv2int(J, &rd->argv[1]);
  755. trstart = lj_opt_narrow_toint(J, J->base[1]);
  756. }
  757. if (J->base[1] && !tref_isnil(J->base[2])) {
  758. trend = lj_opt_narrow_toint(J, J->base[2]);
  759. end = argv2int(J, &rd->argv[2]);
  760. } else {
  761. trend = trstart;
  762. end = start;
  763. }
  764. }
  765. if (end < 0) {
  766. emitir(IRTGI(IR_LT), trend, tr0);
  767. trend = emitir(IRTI(IR_ADD), emitir(IRTI(IR_ADD), trlen, trend),
  768. lj_ir_kint(J, 1));
  769. end = end+(int32_t)str->len+1;
  770. } else if ((MSize)end <= str->len) {
  771. emitir(IRTGI(IR_ULE), trend, trlen);
  772. } else {
  773. emitir(IRTGI(IR_UGT), trend, trlen);
  774. end = (int32_t)str->len;
  775. trend = trlen;
  776. }
  777. trstart = recff_string_start(J, str, &start, trstart, trlen, tr0);
  778. if (rd->data) { /* Return string.sub result. */
  779. if (end - start >= 0) {
  780. /* Also handle empty range here, to avoid extra traces. */
  781. TRef trptr, trslen = emitir(IRTI(IR_SUB), trend, trstart);
  782. emitir(IRTGI(IR_GE), trslen, tr0);
  783. trptr = emitir(IRT(IR_STRREF, IRT_PGC), trstr, trstart);
  784. J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), trptr, trslen);
  785. } else { /* Range underflow: return empty string. */
  786. emitir(IRTGI(IR_LT), trend, trstart);
  787. J->base[0] = lj_ir_kstr(J, &J2G(J)->strempty);
  788. }
  789. } else { /* Return string.byte result(s). */
  790. ptrdiff_t i, len = end - start;
  791. if (len > 0) {
  792. TRef trslen = emitir(IRTI(IR_SUB), trend, trstart);
  793. emitir(IRTGI(IR_EQ), trslen, lj_ir_kint(J, (int32_t)len));
  794. if (J->baseslot + len > LJ_MAX_JSLOTS)
  795. lj_trace_err_info(J, LJ_TRERR_STACKOV);
  796. rd->nres = len;
  797. for (i = 0; i < len; i++) {
  798. TRef tmp = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, (int32_t)i));
  799. tmp = emitir(IRT(IR_STRREF, IRT_PGC), trstr, tmp);
  800. J->base[i] = emitir(IRT(IR_XLOAD, IRT_U8), tmp, IRXLOAD_READONLY);
  801. }
  802. } else { /* Empty range or range underflow: return no results. */
  803. emitir(IRTGI(IR_LE), trend, trstart);
  804. rd->nres = 0;
  805. }
  806. }
  807. }
  808. static void LJ_FASTCALL recff_string_char(jit_State *J, RecordFFData *rd)
  809. {
  810. TRef k255 = lj_ir_kint(J, 255);
  811. BCReg i;
  812. for (i = 0; J->base[i] != 0; i++) { /* Convert char values to strings. */
  813. TRef tr = lj_opt_narrow_toint(J, J->base[i]);
  814. emitir(IRTGI(IR_ULE), tr, k255);
  815. J->base[i] = emitir(IRT(IR_TOSTR, IRT_STR), tr, IRTOSTR_CHAR);
  816. }
  817. if (i > 1) { /* Concatenate the strings, if there's more than one. */
  818. TRef hdr = recff_bufhdr(J), tr = hdr;
  819. for (i = 0; J->base[i] != 0; i++)
  820. tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, J->base[i]);
  821. J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr);
  822. } else if (i == 0) {
  823. J->base[0] = lj_ir_kstr(J, &J2G(J)->strempty);
  824. }
  825. UNUSED(rd);
  826. }
  827. static void LJ_FASTCALL recff_string_rep(jit_State *J, RecordFFData *rd)
  828. {
  829. TRef str = lj_ir_tostr(J, J->base[0]);
  830. TRef rep = lj_opt_narrow_toint(J, J->base[1]);
  831. TRef hdr, tr, str2 = 0;
  832. if (!tref_isnil(J->base[2])) {
  833. TRef sep = lj_ir_tostr(J, J->base[2]);
  834. int32_t vrep = argv2int(J, &rd->argv[1]);
  835. emitir(IRTGI(vrep > 1 ? IR_GT : IR_LE), rep, lj_ir_kint(J, 1));
  836. if (vrep > 1) {
  837. TRef hdr2 = recff_bufhdr(J);
  838. TRef tr2 = emitir(IRTG(IR_BUFPUT, IRT_PGC), hdr2, sep);
  839. tr2 = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr2, str);
  840. str2 = emitir(IRTG(IR_BUFSTR, IRT_STR), tr2, hdr2);
  841. }
  842. }
  843. tr = hdr = recff_bufhdr(J);
  844. if (str2) {
  845. tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, str);
  846. str = str2;
  847. rep = emitir(IRTI(IR_ADD), rep, lj_ir_kint(J, -1));
  848. }
  849. tr = lj_ir_call(J, IRCALL_lj_buf_putstr_rep, tr, str, rep);
  850. J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr);
  851. }
  852. static void LJ_FASTCALL recff_string_op(jit_State *J, RecordFFData *rd)
  853. {
  854. TRef str = lj_ir_tostr(J, J->base[0]);
  855. TRef hdr = recff_bufhdr(J);
  856. TRef tr = lj_ir_call(J, rd->data, hdr, str);
  857. J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr);
  858. }
  859. static void LJ_FASTCALL recff_string_find(jit_State *J, RecordFFData *rd)
  860. {
  861. TRef trstr = lj_ir_tostr(J, J->base[0]);
  862. TRef trpat = lj_ir_tostr(J, J->base[1]);
  863. TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN);
  864. TRef tr0 = lj_ir_kint(J, 0);
  865. TRef trstart;
  866. GCstr *str = argv2str(J, &rd->argv[0]);
  867. GCstr *pat = argv2str(J, &rd->argv[1]);
  868. int32_t start;
  869. J->needsnap = 1;
  870. if (tref_isnil(J->base[2])) {
  871. trstart = lj_ir_kint(J, 1);
  872. start = 1;
  873. } else {
  874. trstart = lj_opt_narrow_toint(J, J->base[2]);
  875. start = argv2int(J, &rd->argv[2]);
  876. }
  877. trstart = recff_string_start(J, str, &start, trstart, trlen, tr0);
  878. if ((MSize)start <= str->len) {
  879. emitir(IRTGI(IR_ULE), trstart, trlen);
  880. } else {
  881. emitir(IRTGI(IR_UGT), trstart, trlen);
  882. #if LJ_52
  883. J->base[0] = TREF_NIL;
  884. return;
  885. #else
  886. trstart = trlen;
  887. start = str->len;
  888. #endif
  889. }
  890. /* Fixed arg or no pattern matching chars? (Specialized to pattern string.) */
  891. if ((J->base[2] && tref_istruecond(J->base[3])) ||
  892. (emitir(IRTG(IR_EQ, IRT_STR), trpat, lj_ir_kstr(J, pat)),
  893. !lj_str_haspattern(pat))) { /* Search for fixed string. */
  894. TRef trsptr = emitir(IRT(IR_STRREF, IRT_PGC), trstr, trstart);
  895. TRef trpptr = emitir(IRT(IR_STRREF, IRT_PGC), trpat, tr0);
  896. TRef trslen = emitir(IRTI(IR_SUB), trlen, trstart);
  897. TRef trplen = emitir(IRTI(IR_FLOAD), trpat, IRFL_STR_LEN);
  898. TRef tr = lj_ir_call(J, IRCALL_lj_str_find, trsptr, trpptr, trslen, trplen);
  899. TRef trp0 = lj_ir_kkptr(J, NULL);
  900. if (lj_str_find(strdata(str)+(MSize)start, strdata(pat),
  901. str->len-(MSize)start, pat->len)) {
  902. TRef pos;
  903. emitir(IRTG(IR_NE, IRT_PGC), tr, trp0);
  904. /* Recompute offset. trsptr may not point into trstr after folding. */
  905. pos = emitir(IRTI(IR_ADD), emitir(IRTI(IR_SUB), tr, trsptr), trstart);
  906. J->base[0] = emitir(IRTI(IR_ADD), pos, lj_ir_kint(J, 1));
  907. J->base[1] = emitir(IRTI(IR_ADD), pos, trplen);
  908. rd->nres = 2;
  909. } else {
  910. emitir(IRTG(IR_EQ, IRT_PGC), tr, trp0);
  911. J->base[0] = TREF_NIL;
  912. }
  913. } else { /* Search for pattern. */
  914. recff_nyiu(J, rd);
  915. return;
  916. }
  917. }
  918. static void recff_format(jit_State *J, RecordFFData *rd, TRef hdr, int sbufx)
  919. {
  920. ptrdiff_t arg = sbufx;
  921. TRef tr = hdr, trfmt = lj_ir_tostr(J, J->base[arg]);
  922. GCstr *fmt = argv2str(J, &rd->argv[arg]);
  923. FormatState fs;
  924. SFormat sf;
  925. /* Specialize to the format string. */
  926. emitir(IRTG(IR_EQ, IRT_STR), trfmt, lj_ir_kstr(J, fmt));
  927. lj_strfmt_init(&fs, strdata(fmt), fmt->len);
  928. while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) { /* Parse format. */
  929. TRef tra = sf == STRFMT_LIT ? 0 : J->base[++arg];
  930. TRef trsf = lj_ir_kint(J, (int32_t)sf);
  931. IRCallID id;
  932. switch (STRFMT_TYPE(sf)) {
  933. case STRFMT_LIT:
  934. tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr,
  935. lj_ir_kstr(J, lj_str_new(J->L, fs.str, fs.len)));
  936. break;
  937. case STRFMT_INT:
  938. id = IRCALL_lj_strfmt_putfnum_int;
  939. handle_int:
  940. if (!tref_isinteger(tra)) {
  941. #if LJ_HASFFI
  942. if (tref_iscdata(tra)) {
  943. tra = lj_crecord_loadiu64(J, tra, &rd->argv[arg]);
  944. tr = lj_ir_call(J, IRCALL_lj_strfmt_putfxint, tr, trsf, tra);
  945. break;
  946. }
  947. #endif
  948. goto handle_num;
  949. }
  950. if (sf == STRFMT_INT) { /* Shortcut for plain %d. */
  951. tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr,
  952. emitir(IRT(IR_TOSTR, IRT_STR), tra, IRTOSTR_INT));
  953. } else {
  954. #if LJ_HASFFI
  955. tra = emitir(IRT(IR_CONV, IRT_U64), tra,
  956. (IRT_INT|(IRT_U64<<5)|IRCONV_SEXT));
  957. tr = lj_ir_call(J, IRCALL_lj_strfmt_putfxint, tr, trsf, tra);
  958. lj_needsplit(J);
  959. #else
  960. recff_nyiu(J, rd); /* Don't bother working around this NYI. */
  961. return;
  962. #endif
  963. }
  964. break;
  965. case STRFMT_UINT:
  966. id = IRCALL_lj_strfmt_putfnum_uint;
  967. goto handle_int;
  968. case STRFMT_NUM:
  969. id = IRCALL_lj_strfmt_putfnum;
  970. handle_num:
  971. tra = lj_ir_tonum(J, tra);
  972. tr = lj_ir_call(J, id, tr, trsf, tra);
  973. if (LJ_SOFTFP32) lj_needsplit(J);
  974. break;
  975. case STRFMT_STR:
  976. if (!tref_isstr(tra)) {
  977. recff_nyiu(J, rd); /* NYI: __tostring and non-string types for %s. */
  978. /* NYI: also buffers. */
  979. return;
  980. }
  981. if (sf == STRFMT_STR) /* Shortcut for plain %s. */
  982. tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, tra);
  983. else if ((sf & STRFMT_T_QUOTED))
  984. tr = lj_ir_call(J, IRCALL_lj_strfmt_putquoted, tr, tra);
  985. else
  986. tr = lj_ir_call(J, IRCALL_lj_strfmt_putfstr, tr, trsf, tra);
  987. break;
  988. case STRFMT_CHAR:
  989. tra = lj_opt_narrow_toint(J, tra);
  990. if (sf == STRFMT_CHAR) /* Shortcut for plain %c. */
  991. tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr,
  992. emitir(IRT(IR_TOSTR, IRT_STR), tra, IRTOSTR_CHAR));
  993. else
  994. tr = lj_ir_call(J, IRCALL_lj_strfmt_putfchar, tr, trsf, tra);
  995. break;
  996. case STRFMT_PTR: /* NYI */
  997. case STRFMT_ERR:
  998. default:
  999. recff_nyiu(J, rd);
  1000. return;
  1001. }
  1002. }
  1003. if (sbufx) {
  1004. emitir(IRT(IR_USE, IRT_NIL), tr, 0);
  1005. } else {
  1006. J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr);
  1007. }
  1008. }
  1009. static void LJ_FASTCALL recff_string_format(jit_State *J, RecordFFData *rd)
  1010. {
  1011. recff_format(J, rd, recff_bufhdr(J), 0);
  1012. }
  1013. /* -- Buffer library fast functions --------------------------------------- */
  1014. #if LJ_HASBUFFER
  1015. static LJ_AINLINE TRef recff_sbufx_get_L(jit_State *J, TRef ud)
  1016. {
  1017. return emitir(IRT(IR_FLOAD, IRT_PGC), ud, IRFL_SBUF_L);
  1018. }
  1019. static LJ_AINLINE void recff_sbufx_set_L(jit_State *J, TRef ud, TRef val)
  1020. {
  1021. TRef fref = emitir(IRT(IR_FREF, IRT_PGC), ud, IRFL_SBUF_L);
  1022. emitir(IRT(IR_FSTORE, IRT_PGC), fref, val);
  1023. }
  1024. static LJ_AINLINE TRef recff_sbufx_get_ptr(jit_State *J, TRef ud, IRFieldID fl)
  1025. {
  1026. return emitir(IRT(IR_FLOAD, IRT_PTR), ud, fl);
  1027. }
  1028. static LJ_AINLINE void recff_sbufx_set_ptr(jit_State *J, TRef ud, IRFieldID fl, TRef val)
  1029. {
  1030. TRef fref = emitir(IRT(IR_FREF, IRT_PTR), ud, fl);
  1031. emitir(IRT(IR_FSTORE, IRT_PTR), fref, val);
  1032. }
  1033. static LJ_AINLINE TRef recff_sbufx_len(jit_State *J, TRef trr, TRef trw)
  1034. {
  1035. TRef len = emitir(IRT(IR_SUB, IRT_INTP), trw, trr);
  1036. if (LJ_64)
  1037. len = emitir(IRTI(IR_CONV), len, (IRT_INT<<5)|IRT_INTP|IRCONV_NONE);
  1038. return len;
  1039. }
  1040. /* Emit typecheck for string buffer. */
  1041. static TRef recff_sbufx_check(jit_State *J, RecordFFData *rd, ptrdiff_t arg)
  1042. {
  1043. TRef trtype, ud = J->base[arg];
  1044. if (!tvisbuf(&rd->argv[arg])) lj_trace_err(J, LJ_TRERR_BADTYPE);
  1045. trtype = emitir(IRT(IR_FLOAD, IRT_U8), ud, IRFL_UDATA_UDTYPE);
  1046. emitir(IRTGI(IR_EQ), trtype, lj_ir_kint(J, UDTYPE_BUFFER));
  1047. J->needsnap = 1;
  1048. return ud;
  1049. }
  1050. /* Emit BUFHDR for write to extended string buffer. */
  1051. static TRef recff_sbufx_write(jit_State *J, TRef ud)
  1052. {
  1053. TRef trbuf = emitir(IRT(IR_ADD, IRT_PGC), ud, lj_ir_kintpgc(J, sizeof(GCudata)));
  1054. return emitir(IRT(IR_BUFHDR, IRT_PGC), trbuf, IRBUFHDR_WRITE);
  1055. }
  1056. /* Check for integer in range for the buffer API. */
  1057. static TRef recff_sbufx_checkint(jit_State *J, RecordFFData *rd, ptrdiff_t arg)
  1058. {
  1059. TRef tr = J->base[arg];
  1060. TRef trlim = lj_ir_kint(J, LJ_MAX_BUF);
  1061. if (tref_isinteger(tr)) {
  1062. emitir(IRTGI(IR_ULE), tr, trlim);
  1063. } else if (tref_isnum(tr)) {
  1064. tr = emitir(IRTI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_ANY);
  1065. emitir(IRTGI(IR_ULE), tr, trlim);
  1066. #if LJ_HASFFI
  1067. } else if (tref_iscdata(tr)) {
  1068. tr = lj_crecord_loadiu64(J, tr, &rd->argv[arg]);
  1069. emitir(IRTG(IR_ULE, IRT_U64), tr, lj_ir_kint64(J, LJ_MAX_BUF));
  1070. tr = emitir(IRTI(IR_CONV), tr, (IRT_INT<<5)|IRT_I64|IRCONV_NONE);
  1071. #else
  1072. UNUSED(rd);
  1073. #endif
  1074. } else {
  1075. lj_trace_err(J, LJ_TRERR_BADTYPE);
  1076. }
  1077. return tr;
  1078. }
  1079. static void LJ_FASTCALL recff_buffer_method_reset(jit_State *J, RecordFFData *rd)
  1080. {
  1081. TRef ud = recff_sbufx_check(J, rd, 0);
  1082. SBufExt *sbx = bufV(&rd->argv[0]);
  1083. int iscow = (int)sbufiscow(sbx);
  1084. TRef trl = recff_sbufx_get_L(J, ud);
  1085. TRef trcow = emitir(IRT(IR_BAND, IRT_IGC), trl, lj_ir_kintpgc(J, SBUF_FLAG_COW));
  1086. TRef zeropgc = lj_ir_kintpgc(J, 0);
  1087. emitir(IRTG(iscow ? IR_NE : IR_EQ, IRT_IGC), trcow, zeropgc);
  1088. if (iscow) {
  1089. TRef zerop = lj_ir_kintp(J, 0);
  1090. trl = emitir(IRT(IR_BXOR, IRT_IGC), trl, lj_ir_kintpgc(J, SBUF_FLAG_COW));
  1091. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_W, zerop);
  1092. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_E, zerop);
  1093. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_B, zerop);
  1094. recff_sbufx_set_L(J, ud, trl);
  1095. emitir(IRT(IR_FSTORE, IRT_PGC),
  1096. emitir(IRT(IR_FREF, IRT_PGC), ud, IRFL_SBUF_REF), zeropgc);
  1097. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, zerop);
  1098. } else {
  1099. TRef trb = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_B);
  1100. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_W, trb);
  1101. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trb);
  1102. }
  1103. }
  1104. static void LJ_FASTCALL recff_buffer_method_skip(jit_State *J, RecordFFData *rd)
  1105. {
  1106. TRef ud = recff_sbufx_check(J, rd, 0);
  1107. TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R);
  1108. TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W);
  1109. TRef len = recff_sbufx_len(J, trr, trw);
  1110. TRef trn = recff_sbufx_checkint(J, rd, 1);
  1111. len = emitir(IRTI(IR_MIN), len, trn);
  1112. trr = emitir(IRT(IR_ADD, IRT_PTR), trr, len);
  1113. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trr);
  1114. }
  1115. static void LJ_FASTCALL recff_buffer_method_set(jit_State *J, RecordFFData *rd)
  1116. {
  1117. TRef ud = recff_sbufx_check(J, rd, 0);
  1118. TRef trbuf = recff_sbufx_write(J, ud);
  1119. TRef tr = J->base[1];
  1120. if (tref_isstr(tr)) {
  1121. TRef trp = emitir(IRT(IR_STRREF, IRT_PGC), tr, lj_ir_kint(J, 0));
  1122. TRef len = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN);
  1123. IRIns *irp = IR(tref_ref(trp));
  1124. /* trp must point into the anchored obj, even after folding. */
  1125. if (irp->o == IR_STRREF)
  1126. tr = irp->op1;
  1127. else if (!tref_isk(tr))
  1128. trp = emitir(IRT(IR_ADD, IRT_PGC), tr, lj_ir_kintpgc(J, sizeof(GCstr)));
  1129. lj_ir_call(J, IRCALL_lj_bufx_set, trbuf, trp, len, tr);
  1130. #if LJ_HASFFI
  1131. } else if (tref_iscdata(tr)) {
  1132. TRef trp = lj_crecord_topcvoid(J, tr, &rd->argv[1]);
  1133. TRef len = recff_sbufx_checkint(J, rd, 2);
  1134. lj_ir_call(J, IRCALL_lj_bufx_set, trbuf, trp, len, tr);
  1135. #endif
  1136. } /* else: Interpreter will throw. */
  1137. }
  1138. static void LJ_FASTCALL recff_buffer_method_put(jit_State *J, RecordFFData *rd)
  1139. {
  1140. TRef ud = recff_sbufx_check(J, rd, 0);
  1141. TRef trbuf = recff_sbufx_write(J, ud);
  1142. TRef tr;
  1143. ptrdiff_t arg;
  1144. if (!J->base[1]) return;
  1145. for (arg = 1; (tr = J->base[arg]); arg++) {
  1146. if (tref_isudata(tr)) {
  1147. TRef ud2 = recff_sbufx_check(J, rd, arg);
  1148. emitir(IRTG(IR_NE, IRT_PGC), ud, ud2);
  1149. }
  1150. }
  1151. for (arg = 1; (tr = J->base[arg]); arg++) {
  1152. if (tref_isstr(tr)) {
  1153. trbuf = emitir(IRTG(IR_BUFPUT, IRT_PGC), trbuf, tr);
  1154. } else if (tref_isnumber(tr)) {
  1155. trbuf = emitir(IRTG(IR_BUFPUT, IRT_PGC), trbuf,
  1156. emitir(IRT(IR_TOSTR, IRT_STR), tr,
  1157. tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT));
  1158. } else if (tref_isudata(tr)) {
  1159. TRef trr = recff_sbufx_get_ptr(J, tr, IRFL_SBUF_R);
  1160. TRef trw = recff_sbufx_get_ptr(J, tr, IRFL_SBUF_W);
  1161. TRef len = recff_sbufx_len(J, trr, trw);
  1162. trbuf = lj_ir_call(J, IRCALL_lj_buf_putmem, trbuf, trr, len);
  1163. } else {
  1164. recff_nyiu(J, rd);
  1165. }
  1166. }
  1167. emitir(IRT(IR_USE, IRT_NIL), trbuf, 0);
  1168. }
  1169. static void LJ_FASTCALL recff_buffer_method_putf(jit_State *J, RecordFFData *rd)
  1170. {
  1171. TRef ud = recff_sbufx_check(J, rd, 0);
  1172. TRef trbuf = recff_sbufx_write(J, ud);
  1173. recff_format(J, rd, trbuf, 1);
  1174. }
  1175. static void LJ_FASTCALL recff_buffer_method_get(jit_State *J, RecordFFData *rd)
  1176. {
  1177. TRef ud = recff_sbufx_check(J, rd, 0);
  1178. TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R);
  1179. TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W);
  1180. TRef tr;
  1181. ptrdiff_t arg;
  1182. if (!J->base[1]) { J->base[1] = TREF_NIL; J->base[2] = 0; }
  1183. for (arg = 0; (tr = J->base[arg+1]); arg++) {
  1184. if (!tref_isnil(tr)) {
  1185. J->base[arg+1] = recff_sbufx_checkint(J, rd, arg+1);
  1186. }
  1187. }
  1188. for (arg = 0; (tr = J->base[arg+1]); arg++) {
  1189. TRef len = recff_sbufx_len(J, trr, trw);
  1190. if (tref_isnil(tr)) {
  1191. J->base[arg] = emitir(IRT(IR_XSNEW, IRT_STR), trr, len);
  1192. trr = trw;
  1193. } else {
  1194. TRef tru;
  1195. len = emitir(IRTI(IR_MIN), len, tr);
  1196. tru = emitir(IRT(IR_ADD, IRT_PTR), trr, len);
  1197. J->base[arg] = emitir(IRT(IR_XSNEW, IRT_STR), trr, len);
  1198. trr = tru; /* Doing the ADD before the SNEW generates better code. */
  1199. }
  1200. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trr);
  1201. }
  1202. rd->nres = arg;
  1203. }
  1204. static void LJ_FASTCALL recff_buffer_method___tostring(jit_State *J, RecordFFData *rd)
  1205. {
  1206. TRef ud = recff_sbufx_check(J, rd, 0);
  1207. TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R);
  1208. TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W);
  1209. J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), trr, recff_sbufx_len(J, trr, trw));
  1210. }
  1211. static void LJ_FASTCALL recff_buffer_method___len(jit_State *J, RecordFFData *rd)
  1212. {
  1213. TRef ud = recff_sbufx_check(J, rd, 0);
  1214. TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R);
  1215. TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W);
  1216. J->base[0] = recff_sbufx_len(J, trr, trw);
  1217. }
  1218. #if LJ_HASFFI
  1219. static void LJ_FASTCALL recff_buffer_method_putcdata(jit_State *J, RecordFFData *rd)
  1220. {
  1221. TRef ud = recff_sbufx_check(J, rd, 0);
  1222. TRef trbuf = recff_sbufx_write(J, ud);
  1223. TRef tr = lj_crecord_topcvoid(J, J->base[1], &rd->argv[1]);
  1224. TRef len = recff_sbufx_checkint(J, rd, 2);
  1225. trbuf = lj_ir_call(J, IRCALL_lj_buf_putmem, trbuf, tr, len);
  1226. emitir(IRT(IR_USE, IRT_NIL), trbuf, 0);
  1227. }
  1228. static void LJ_FASTCALL recff_buffer_method_reserve(jit_State *J, RecordFFData *rd)
  1229. {
  1230. TRef ud = recff_sbufx_check(J, rd, 0);
  1231. TRef trbuf = recff_sbufx_write(J, ud);
  1232. TRef trsz = recff_sbufx_checkint(J, rd, 1);
  1233. J->base[1] = lj_ir_call(J, IRCALL_lj_bufx_more, trbuf, trsz);
  1234. J->base[0] = lj_crecord_topuint8(J, recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W));
  1235. rd->nres = 2;
  1236. }
  1237. static void LJ_FASTCALL recff_buffer_method_commit(jit_State *J, RecordFFData *rd)
  1238. {
  1239. TRef ud = recff_sbufx_check(J, rd, 0);
  1240. TRef len = recff_sbufx_checkint(J, rd, 1);
  1241. TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W);
  1242. TRef tre = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_E);
  1243. TRef left = emitir(IRT(IR_SUB, IRT_INTP), tre, trw);
  1244. if (LJ_64)
  1245. left = emitir(IRTI(IR_CONV), left, (IRT_INT<<5)|IRT_INTP|IRCONV_NONE);
  1246. emitir(IRTGI(IR_ULE), len, left);
  1247. trw = emitir(IRT(IR_ADD, IRT_PTR), trw, len);
  1248. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_W, trw);
  1249. }
  1250. static void LJ_FASTCALL recff_buffer_method_ref(jit_State *J, RecordFFData *rd)
  1251. {
  1252. TRef ud = recff_sbufx_check(J, rd, 0);
  1253. TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R);
  1254. TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W);
  1255. J->base[0] = lj_crecord_topuint8(J, trr);
  1256. J->base[1] = recff_sbufx_len(J, trr, trw);
  1257. rd->nres = 2;
  1258. }
  1259. #endif
  1260. static void LJ_FASTCALL recff_buffer_method_encode(jit_State *J, RecordFFData *rd)
  1261. {
  1262. TRef ud = recff_sbufx_check(J, rd, 0);
  1263. TRef trbuf = recff_sbufx_write(J, ud);
  1264. TRef tmp = recff_tmpref(J, J->base[1], IRTMPREF_IN1);
  1265. lj_ir_call(J, IRCALL_lj_serialize_put, trbuf, tmp);
  1266. /* No IR_USE needed, since the call is a store. */
  1267. }
  1268. static void LJ_FASTCALL recff_buffer_method_decode(jit_State *J, RecordFFData *rd)
  1269. {
  1270. TRef ud = recff_sbufx_check(J, rd, 0);
  1271. TRef trbuf = recff_sbufx_write(J, ud);
  1272. TRef tmp = recff_tmpref(J, TREF_NIL, IRTMPREF_OUT1);
  1273. TRef trr = lj_ir_call(J, IRCALL_lj_serialize_get, trbuf, tmp);
  1274. IRType t = (IRType)lj_serialize_peektype(bufV(&rd->argv[0]));
  1275. /* No IR_USE needed, since the call is a store. */
  1276. J->base[0] = lj_record_vload(J, tmp, 0, t);
  1277. /* The sbx->r store must be after the VLOAD type check, in case it fails. */
  1278. recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trr);
  1279. }
  1280. static void LJ_FASTCALL recff_buffer_encode(jit_State *J, RecordFFData *rd)
  1281. {
  1282. TRef tmp = recff_tmpref(J, J->base[0], IRTMPREF_IN1);
  1283. J->base[0] = lj_ir_call(J, IRCALL_lj_serialize_encode, tmp);
  1284. /* IR_USE needed for IR_CALLA, because the encoder may throw non-OOM. */
  1285. emitir(IRT(IR_USE, IRT_NIL), J->base[0], 0);
  1286. UNUSED(rd);
  1287. }
  1288. static void LJ_FASTCALL recff_buffer_decode(jit_State *J, RecordFFData *rd)
  1289. {
  1290. if (tvisstr(&rd->argv[0])) {
  1291. GCstr *str = strV(&rd->argv[0]);
  1292. SBufExt sbx;
  1293. IRType t;
  1294. TRef tmp = recff_tmpref(J, TREF_NIL, IRTMPREF_OUT1);
  1295. TRef tr = lj_ir_call(J, IRCALL_lj_serialize_decode, tmp, J->base[0]);
  1296. /* IR_USE needed for IR_CALLA, because the decoder may throw non-OOM.
  1297. ** That's why IRCALL_lj_serialize_decode needs a fake INT result.
  1298. */
  1299. emitir(IRT(IR_USE, IRT_NIL), tr, 0);
  1300. memset(&sbx, 0, sizeof(SBufExt));
  1301. lj_bufx_set_cow(J->L, &sbx, strdata(str), str->len);
  1302. t = (IRType)lj_serialize_peektype(&sbx);
  1303. J->base[0] = lj_record_vload(J, tmp, 0, t);
  1304. } /* else: Interpreter will throw. */
  1305. }
  1306. #endif
  1307. /* -- Table library fast functions ---------------------------------------- */
  1308. static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd)
  1309. {
  1310. RecordIndex ix;
  1311. ix.tab = J->base[0];
  1312. ix.val = J->base[1];
  1313. rd->nres = 0;
  1314. if (tref_istab(ix.tab) && ix.val) {
  1315. if (!J->base[2]) { /* Simple push: t[#t+1] = v */
  1316. TRef trlen = emitir(IRTI(IR_ALEN), ix.tab, TREF_NIL);
  1317. GCtab *t = tabV(&rd->argv[0]);
  1318. ix.key = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1));
  1319. settabV(J->L, &ix.tabv, t);
  1320. setintV(&ix.keyv, lj_tab_len(t) + 1);
  1321. ix.idxchain = 0;
  1322. lj_record_idx(J, &ix); /* Set new value. */
  1323. } else { /* Complex case: insert in the middle. */
  1324. recff_nyiu(J, rd);
  1325. return;
  1326. }
  1327. } /* else: Interpreter will throw. */
  1328. }
  1329. static void LJ_FASTCALL recff_table_concat(jit_State *J, RecordFFData *rd)
  1330. {
  1331. TRef tab = J->base[0];
  1332. if (tref_istab(tab)) {
  1333. TRef sep = !tref_isnil(J->base[1]) ?
  1334. lj_ir_tostr(J, J->base[1]) : lj_ir_knull(J, IRT_STR);
  1335. TRef tri = (J->base[1] && !tref_isnil(J->base[2])) ?
  1336. lj_opt_narrow_toint(J, J->base[2]) : lj_ir_kint(J, 1);
  1337. TRef tre = (J->base[1] && J->base[2] && !tref_isnil(J->base[3])) ?
  1338. lj_opt_narrow_toint(J, J->base[3]) :
  1339. emitir(IRTI(IR_ALEN), tab, TREF_NIL);
  1340. TRef hdr = recff_bufhdr(J);
  1341. TRef tr = lj_ir_call(J, IRCALL_lj_buf_puttab, hdr, tab, sep, tri, tre);
  1342. emitir(IRTG(IR_NE, IRT_PTR), tr, lj_ir_kptr(J, NULL));
  1343. J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr);
  1344. } /* else: Interpreter will throw. */
  1345. UNUSED(rd);
  1346. }
  1347. static void LJ_FASTCALL recff_table_new(jit_State *J, RecordFFData *rd)
  1348. {
  1349. TRef tra = lj_opt_narrow_toint(J, J->base[0]);
  1350. TRef trh = lj_opt_narrow_toint(J, J->base[1]);
  1351. if (tref_isk(tra) && tref_isk(trh)) {
  1352. int32_t a = IR(tref_ref(tra))->i;
  1353. if (a < 0x7fff) {
  1354. uint32_t hbits = hsize2hbits(IR(tref_ref(trh))->i);
  1355. a = a > 0 ? a+1 : 0;
  1356. J->base[0] = emitir(IRTG(IR_TNEW, IRT_TAB), (uint32_t)a, hbits);
  1357. return;
  1358. }
  1359. }
  1360. J->base[0] = lj_ir_call(J, IRCALL_lj_tab_new_ah, tra, trh);
  1361. UNUSED(rd);
  1362. }
  1363. static void LJ_FASTCALL recff_table_clear(jit_State *J, RecordFFData *rd)
  1364. {
  1365. TRef tr = J->base[0];
  1366. if (tref_istab(tr)) {
  1367. rd->nres = 0;
  1368. lj_ir_call(J, IRCALL_lj_tab_clear, tr);
  1369. J->needsnap = 1;
  1370. } /* else: Interpreter will throw. */
  1371. }
  1372. /* -- I/O library fast functions ------------------------------------------ */
  1373. /* Get FILE* for I/O function. Any I/O error aborts recording, so there's
  1374. ** no need to encode the alternate cases for any of the guards.
  1375. */
  1376. static TRef recff_io_fp(jit_State *J, TRef *udp, int32_t id)
  1377. {
  1378. TRef tr, ud, fp;
  1379. if (id) { /* io.func() */
  1380. ud = lj_ir_ggfload(J, IRT_UDATA, GG_OFS(g.gcroot[id]));
  1381. } else { /* fp:method() */
  1382. ud = J->base[0];
  1383. if (!tref_isudata(ud))
  1384. lj_trace_err(J, LJ_TRERR_BADTYPE);
  1385. tr = emitir(IRT(IR_FLOAD, IRT_U8), ud, IRFL_UDATA_UDTYPE);
  1386. emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE));
  1387. }
  1388. *udp = ud;
  1389. fp = emitir(IRT(IR_FLOAD, IRT_PTR), ud, IRFL_UDATA_FILE);
  1390. emitir(IRTG(IR_NE, IRT_PTR), fp, lj_ir_knull(J, IRT_PTR));
  1391. return fp;
  1392. }
  1393. static void LJ_FASTCALL recff_io_write(jit_State *J, RecordFFData *rd)
  1394. {
  1395. TRef ud, fp = recff_io_fp(J, &ud, rd->data);
  1396. TRef zero = lj_ir_kint(J, 0);
  1397. TRef one = lj_ir_kint(J, 1);
  1398. ptrdiff_t i = rd->data == 0 ? 1 : 0;
  1399. for (; J->base[i]; i++) {
  1400. TRef str = lj_ir_tostr(J, J->base[i]);
  1401. TRef buf = emitir(IRT(IR_STRREF, IRT_PGC), str, zero);
  1402. TRef len = emitir(IRTI(IR_FLOAD), str, IRFL_STR_LEN);
  1403. if (tref_isk(len) && IR(tref_ref(len))->i == 1) {
  1404. IRIns *irs = IR(tref_ref(str));
  1405. TRef tr = (irs->o == IR_TOSTR && irs->op2 == IRTOSTR_CHAR) ?
  1406. irs->op1 :
  1407. emitir(IRT(IR_XLOAD, IRT_U8), buf, IRXLOAD_READONLY);
  1408. tr = lj_ir_call(J, IRCALL_fputc, tr, fp);
  1409. if (results_wanted(J) != 0) /* Check result only if not ignored. */
  1410. emitir(IRTGI(IR_NE), tr, lj_ir_kint(J, -1));
  1411. } else {
  1412. TRef tr = lj_ir_call(J, IRCALL_fwrite, buf, one, len, fp);
  1413. if (results_wanted(J) != 0) /* Check result only if not ignored. */
  1414. emitir(IRTGI(IR_EQ), tr, len);
  1415. }
  1416. }
  1417. J->base[0] = LJ_52 ? ud : TREF_TRUE;
  1418. }
  1419. static void LJ_FASTCALL recff_io_flush(jit_State *J, RecordFFData *rd)
  1420. {
  1421. TRef ud, fp = recff_io_fp(J, &ud, rd->data);
  1422. TRef tr = lj_ir_call(J, IRCALL_fflush, fp);
  1423. if (results_wanted(J) != 0) /* Check result only if not ignored. */
  1424. emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0));
  1425. J->base[0] = TREF_TRUE;
  1426. }
  1427. /* -- Debug library fast functions ---------------------------------------- */
  1428. static void LJ_FASTCALL recff_debug_getmetatable(jit_State *J, RecordFFData *rd)
  1429. {
  1430. GCtab *mt;
  1431. TRef mtref;
  1432. TRef tr = J->base[0];
  1433. if (tref_istab(tr)) {
  1434. mt = tabref(tabV(&rd->argv[0])->metatable);
  1435. mtref = emitir(IRT(IR_FLOAD, IRT_TAB), tr, IRFL_TAB_META);
  1436. } else if (tref_isudata(tr)) {
  1437. mt = tabref(udataV(&rd->argv[0])->metatable);
  1438. mtref = emitir(IRT(IR_FLOAD, IRT_TAB), tr, IRFL_UDATA_META);
  1439. } else {
  1440. mt = tabref(basemt_obj(J2G(J), &rd->argv[0]));
  1441. J->base[0] = mt ? lj_ir_ktab(J, mt) : TREF_NIL;
  1442. return;
  1443. }
  1444. emitir(IRTG(mt ? IR_NE : IR_EQ, IRT_TAB), mtref, lj_ir_knull(J, IRT_TAB));
  1445. J->base[0] = mt ? mtref : TREF_NIL;
  1446. }
  1447. /* -- Record calls to fast functions -------------------------------------- */
  1448. #include "lj_recdef.h"
  1449. static uint32_t recdef_lookup(GCfunc *fn)
  1450. {
  1451. if (fn->c.ffid < sizeof(recff_idmap)/sizeof(recff_idmap[0]))
  1452. return recff_idmap[fn->c.ffid];
  1453. else
  1454. return 0;
  1455. }
  1456. /* Record entry to a fast function or C function. */
  1457. void lj_ffrecord_func(jit_State *J)
  1458. {
  1459. RecordFFData rd;
  1460. uint32_t m = recdef_lookup(J->fn);
  1461. rd.data = m & 0xff;
  1462. rd.nres = 1; /* Default is one result. */
  1463. rd.argv = J->L->base;
  1464. J->base[J->maxslot] = 0; /* Mark end of arguments. */
  1465. (recff_func[m >> 8])(J, &rd); /* Call recff_* handler. */
  1466. if (rd.nres >= 0) {
  1467. if (J->postproc == LJ_POST_NONE) J->postproc = LJ_POST_FFRETRY;
  1468. lj_record_ret(J, 0, rd.nres);
  1469. }
  1470. }
  1471. #undef IR
  1472. #undef emitir
  1473. #endif