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