comparison third_party/luajit/src/lj_ffrecord.c @ 178:94705b5986b3

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