Debian Codegraph @

libs/luajit/LuaJIT-src/src/lj_ffrecord.c

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