FFI: Fix ipairs() recording.
[luajit-2.0.git] / src / lj_ffrecord.c
blob308322517c5ced1ed4d553518e230f3835bd8741
1 /*
2 ** Fast function call recorder.
3 ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #define lj_ffrecord_c
7 #define LUA_CORE
9 #include "lj_obj.h"
11 #if LJ_HASJIT
13 #include "lj_err.h"
14 #include "lj_str.h"
15 #include "lj_tab.h"
16 #include "lj_frame.h"
17 #include "lj_bc.h"
18 #include "lj_ff.h"
19 #include "lj_ir.h"
20 #include "lj_jit.h"
21 #include "lj_ircall.h"
22 #include "lj_iropt.h"
23 #include "lj_trace.h"
24 #include "lj_record.h"
25 #include "lj_ffrecord.h"
26 #include "lj_crecord.h"
27 #include "lj_dispatch.h"
28 #include "lj_vm.h"
29 #include "lj_strscan.h"
31 /* Some local macros to save typing. Undef'd at the end. */
32 #define IR(ref) (&J->cur.ir[(ref)])
34 /* Pass IR on to next optimization in chain (FOLD). */
35 #define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
37 /* -- Fast function recording handlers ------------------------------------ */
39 /* Conventions for fast function call handlers:
41 ** The argument slots start at J->base[0]. All of them are guaranteed to be
42 ** valid and type-specialized references. J->base[J->maxslot] is set to 0
43 ** as a sentinel. The runtime argument values start at rd->argv[0].
45 ** In general fast functions should check for presence of all of their
46 ** arguments and for the correct argument types. Some simplifications
47 ** are allowed if the interpreter throws instead. But even if recording
48 ** is aborted, the generated IR must be consistent (no zero-refs).
50 ** The number of results in rd->nres is set to 1. Handlers that return
51 ** a different number of results need to override it. A negative value
52 ** prevents return processing (e.g. for pending calls).
54 ** Results need to be stored starting at J->base[0]. Return processing
55 ** moves them to the right slots later.
57 ** The per-ffid auxiliary data is the value of the 2nd part of the
58 ** LJLIB_REC() annotation. This allows handling similar functionality
59 ** in a common handler.
62 /* Type of handler to record a fast function. */
63 typedef void (LJ_FASTCALL *RecordFunc)(jit_State *J, RecordFFData *rd);
65 /* Get runtime value of int argument. */
66 static int32_t argv2int(jit_State *J, TValue *o)
68 if (!lj_strscan_numberobj(o))
69 lj_trace_err(J, LJ_TRERR_BADTYPE);
70 return tvisint(o) ? intV(o) : lj_num2int(numV(o));
73 /* Get runtime value of string argument. */
74 static GCstr *argv2str(jit_State *J, TValue *o)
76 if (LJ_LIKELY(tvisstr(o))) {
77 return strV(o);
78 } else {
79 GCstr *s;
80 if (!tvisnumber(o))
81 lj_trace_err(J, LJ_TRERR_BADTYPE);
82 if (tvisint(o))
83 s = lj_str_fromint(J->L, intV(o));
84 else
85 s = lj_str_fromnum(J->L, &o->n);
86 setstrV(J->L, o, s);
87 return s;
91 /* Return number of results wanted by caller. */
92 static ptrdiff_t results_wanted(jit_State *J)
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;
101 /* Throw error for unsupported variant of fast function. */
102 LJ_NORET static void recff_nyiu(jit_State *J)
104 setfuncV(J->L, &J->errinfo, J->fn);
105 lj_trace_err_info(J, LJ_TRERR_NYIFFU);
108 /* Fallback handler for all fast functions that are not recorded (yet). */
109 static void LJ_FASTCALL recff_nyi(jit_State *J, RecordFFData *rd)
111 setfuncV(J->L, &J->errinfo, J->fn);
112 lj_trace_err_info(J, LJ_TRERR_NYIFF);
113 UNUSED(rd);
116 /* C functions can have arbitrary side-effects and are not recorded (yet). */
117 static void LJ_FASTCALL recff_c(jit_State *J, RecordFFData *rd)
119 setfuncV(J->L, &J->errinfo, J->fn);
120 lj_trace_err_info(J, LJ_TRERR_NYICF);
121 UNUSED(rd);
124 /* -- Base library fast functions ----------------------------------------- */
126 static void LJ_FASTCALL recff_assert(jit_State *J, RecordFFData *rd)
128 /* Arguments already specialized. The interpreter throws for nil/false. */
129 rd->nres = J->maxslot; /* Pass through all arguments. */
132 static void LJ_FASTCALL recff_type(jit_State *J, RecordFFData *rd)
134 /* Arguments already specialized. Result is a constant string. Neat, huh? */
135 uint32_t t;
136 if (tvisnumber(&rd->argv[0]))
137 t = ~LJ_TNUMX;
138 else if (LJ_64 && tvislightud(&rd->argv[0]))
139 t = ~LJ_TLIGHTUD;
140 else
141 t = ~itype(&rd->argv[0]);
142 J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[t]));
143 UNUSED(rd);
146 static void LJ_FASTCALL recff_getmetatable(jit_State *J, RecordFFData *rd)
148 TRef tr = J->base[0];
149 if (tr) {
150 RecordIndex ix;
151 ix.tab = tr;
152 copyTV(J->L, &ix.tabv, &rd->argv[0]);
153 if (lj_record_mm_lookup(J, &ix, MM_metatable))
154 J->base[0] = ix.mobj;
155 else
156 J->base[0] = ix.mt;
157 } /* else: Interpreter will throw. */
160 static void LJ_FASTCALL recff_setmetatable(jit_State *J, RecordFFData *rd)
162 TRef tr = J->base[0];
163 TRef mt = J->base[1];
164 if (tref_istab(tr) && (tref_istab(mt) || (mt && tref_isnil(mt)))) {
165 TRef fref, mtref;
166 RecordIndex ix;
167 ix.tab = tr;
168 copyTV(J->L, &ix.tabv, &rd->argv[0]);
169 lj_record_mm_lookup(J, &ix, MM_metatable); /* Guard for no __metatable. */
170 fref = emitir(IRT(IR_FREF, IRT_P32), tr, IRFL_TAB_META);
171 mtref = tref_isnil(mt) ? lj_ir_knull(J, IRT_TAB) : mt;
172 emitir(IRT(IR_FSTORE, IRT_TAB), fref, mtref);
173 if (!tref_isnil(mt))
174 emitir(IRT(IR_TBAR, IRT_TAB), tr, 0);
175 J->base[0] = tr;
176 J->needsnap = 1;
177 } /* else: Interpreter will throw. */
180 static void LJ_FASTCALL recff_rawget(jit_State *J, RecordFFData *rd)
182 RecordIndex ix;
183 ix.tab = J->base[0]; ix.key = J->base[1];
184 if (tref_istab(ix.tab) && ix.key) {
185 ix.val = 0; ix.idxchain = 0;
186 settabV(J->L, &ix.tabv, tabV(&rd->argv[0]));
187 copyTV(J->L, &ix.keyv, &rd->argv[1]);
188 J->base[0] = lj_record_idx(J, &ix);
189 } /* else: Interpreter will throw. */
192 static void LJ_FASTCALL recff_rawset(jit_State *J, RecordFFData *rd)
194 RecordIndex ix;
195 ix.tab = J->base[0]; ix.key = J->base[1]; ix.val = J->base[2];
196 if (tref_istab(ix.tab) && ix.key && ix.val) {
197 ix.idxchain = 0;
198 settabV(J->L, &ix.tabv, tabV(&rd->argv[0]));
199 copyTV(J->L, &ix.keyv, &rd->argv[1]);
200 copyTV(J->L, &ix.valv, &rd->argv[2]);
201 lj_record_idx(J, &ix);
202 /* Pass through table at J->base[0] as result. */
203 } /* else: Interpreter will throw. */
206 static void LJ_FASTCALL recff_rawequal(jit_State *J, RecordFFData *rd)
208 TRef tra = J->base[0];
209 TRef trb = J->base[1];
210 if (tra && trb) {
211 int diff = lj_record_objcmp(J, tra, trb, &rd->argv[0], &rd->argv[1]);
212 J->base[0] = diff ? TREF_FALSE : TREF_TRUE;
213 } /* else: Interpreter will throw. */
216 #if LJ_52
217 static void LJ_FASTCALL recff_rawlen(jit_State *J, RecordFFData *rd)
219 TRef tr = J->base[0];
220 if (tref_isstr(tr))
221 J->base[0] = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN);
222 else if (tref_istab(tr))
223 J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, tr);
224 /* else: Interpreter will throw. */
225 UNUSED(rd);
227 #endif
229 /* Determine mode of select() call. */
230 int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv)
232 if (tref_isstr(tr) && *strVdata(tv) == '#') { /* select('#', ...) */
233 if (strV(tv)->len == 1) {
234 emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, strV(tv)));
235 } else {
236 TRef trptr = emitir(IRT(IR_STRREF, IRT_P32), tr, lj_ir_kint(J, 0));
237 TRef trchar = emitir(IRT(IR_XLOAD, IRT_U8), trptr, IRXLOAD_READONLY);
238 emitir(IRTG(IR_EQ, IRT_INT), trchar, lj_ir_kint(J, '#'));
240 return 0;
241 } else { /* select(n, ...) */
242 int32_t start = argv2int(J, tv);
243 if (start == 0) lj_trace_err(J, LJ_TRERR_BADTYPE); /* A bit misleading. */
244 return start;
248 static void LJ_FASTCALL recff_select(jit_State *J, RecordFFData *rd)
250 TRef tr = J->base[0];
251 if (tr) {
252 ptrdiff_t start = lj_ffrecord_select_mode(J, tr, &rd->argv[0]);
253 if (start == 0) { /* select('#', ...) */
254 J->base[0] = lj_ir_kint(J, J->maxslot - 1);
255 } else if (tref_isk(tr)) { /* select(k, ...) */
256 ptrdiff_t n = (ptrdiff_t)J->maxslot;
257 if (start < 0) start += n;
258 else if (start > n) start = n;
259 rd->nres = n - start;
260 if (start >= 1) {
261 ptrdiff_t i;
262 for (i = 0; i < n - start; i++)
263 J->base[i] = J->base[start+i];
264 } /* else: Interpreter will throw. */
265 } else {
266 recff_nyiu(J);
268 } /* else: Interpreter will throw. */
271 static void LJ_FASTCALL recff_tonumber(jit_State *J, RecordFFData *rd)
273 TRef tr = J->base[0];
274 TRef base = J->base[1];
275 if (tr && !tref_isnil(base)) {
276 base = lj_opt_narrow_toint(J, base);
277 if (!tref_isk(base) || IR(tref_ref(base))->i != 10)
278 recff_nyiu(J);
280 if (tref_isnumber_str(tr)) {
281 if (tref_isstr(tr)) {
282 TValue tmp;
283 if (!lj_strscan_num(strV(&rd->argv[0]), &tmp))
284 recff_nyiu(J); /* Would need an inverted STRTO for this case. */
285 tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0);
287 #if LJ_HASFFI
288 } else if (tref_iscdata(tr)) {
289 lj_crecord_tonumber(J, rd);
290 return;
291 #endif
292 } else {
293 tr = TREF_NIL;
295 J->base[0] = tr;
296 UNUSED(rd);
299 static TValue *recff_metacall_cp(lua_State *L, lua_CFunction dummy, void *ud)
301 jit_State *J = (jit_State *)ud;
302 lj_record_tailcall(J, 0, 1);
303 UNUSED(L); UNUSED(dummy);
304 return NULL;
307 static int recff_metacall(jit_State *J, RecordFFData *rd, MMS mm)
309 RecordIndex ix;
310 ix.tab = J->base[0];
311 copyTV(J->L, &ix.tabv, &rd->argv[0]);
312 if (lj_record_mm_lookup(J, &ix, mm)) { /* Has metamethod? */
313 int errcode;
314 TValue argv0;
315 /* Temporarily insert metamethod below object. */
316 J->base[1] = J->base[0];
317 J->base[0] = ix.mobj;
318 copyTV(J->L, &argv0, &rd->argv[0]);
319 copyTV(J->L, &rd->argv[1], &rd->argv[0]);
320 copyTV(J->L, &rd->argv[0], &ix.mobjv);
321 /* Need to protect lj_record_tailcall because it may throw. */
322 errcode = lj_vm_cpcall(J->L, NULL, J, recff_metacall_cp);
323 /* Always undo Lua stack changes to avoid confusing the interpreter. */
324 copyTV(J->L, &rd->argv[0], &argv0);
325 if (errcode)
326 lj_err_throw(J->L, errcode); /* Propagate errors. */
327 rd->nres = -1; /* Pending call. */
328 return 1; /* Tailcalled to metamethod. */
330 return 0;
333 static void LJ_FASTCALL recff_tostring(jit_State *J, RecordFFData *rd)
335 TRef tr = J->base[0];
336 if (tref_isstr(tr)) {
337 /* Ignore __tostring in the string base metatable. */
338 /* Pass on result in J->base[0]. */
339 } else if (!recff_metacall(J, rd, MM_tostring)) {
340 if (tref_isnumber(tr)) {
341 J->base[0] = emitir(IRT(IR_TOSTR, IRT_STR), tr, 0);
342 } else if (tref_ispri(tr)) {
343 J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[tref_type(tr)]));
344 } else {
345 recff_nyiu(J);
350 static void LJ_FASTCALL recff_ipairs_aux(jit_State *J, RecordFFData *rd)
352 RecordIndex ix;
353 ix.tab = J->base[0];
354 if (tref_istab(ix.tab)) {
355 if (!tvisnumber(&rd->argv[1])) /* No support for string coercion. */
356 lj_trace_err(J, LJ_TRERR_BADTYPE);
357 setintV(&ix.keyv, numberVint(&rd->argv[1])+1);
358 settabV(J->L, &ix.tabv, tabV(&rd->argv[0]));
359 ix.val = 0; ix.idxchain = 0;
360 ix.key = lj_opt_narrow_toint(J, J->base[1]);
361 J->base[0] = ix.key = emitir(IRTI(IR_ADD), ix.key, lj_ir_kint(J, 1));
362 J->base[1] = lj_record_idx(J, &ix);
363 rd->nres = tref_isnil(J->base[1]) ? 0 : 2;
364 } /* else: Interpreter will throw. */
367 static void LJ_FASTCALL recff_ipairs(jit_State *J, RecordFFData *rd)
369 TRef tr = J->base[0];
370 if (!((LJ_52 || (LJ_HASFFI && tref_iscdata(tr))) &&
371 recff_metacall(J, rd, MM_ipairs))) {
372 if (tref_istab(tr)) {
373 J->base[0] = lj_ir_kfunc(J, funcV(&J->fn->c.upvalue[0]));
374 J->base[1] = tr;
375 J->base[2] = lj_ir_kint(J, 0);
376 rd->nres = 3;
377 } /* else: Interpreter will throw. */
381 static void LJ_FASTCALL recff_pcall(jit_State *J, RecordFFData *rd)
383 if (J->maxslot >= 1) {
384 lj_record_call(J, 0, J->maxslot - 1);
385 rd->nres = -1; /* Pending call. */
386 } /* else: Interpreter will throw. */
389 static TValue *recff_xpcall_cp(lua_State *L, lua_CFunction dummy, void *ud)
391 jit_State *J = (jit_State *)ud;
392 lj_record_call(J, 1, J->maxslot - 2);
393 UNUSED(L); UNUSED(dummy);
394 return NULL;
397 static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd)
399 if (J->maxslot >= 2) {
400 TValue argv0, argv1;
401 TRef tmp;
402 int errcode;
403 /* Swap function and traceback. */
404 tmp = J->base[0]; J->base[0] = J->base[1]; J->base[1] = tmp;
405 copyTV(J->L, &argv0, &rd->argv[0]);
406 copyTV(J->L, &argv1, &rd->argv[1]);
407 copyTV(J->L, &rd->argv[0], &argv1);
408 copyTV(J->L, &rd->argv[1], &argv0);
409 /* Need to protect lj_record_call because it may throw. */
410 errcode = lj_vm_cpcall(J->L, NULL, J, recff_xpcall_cp);
411 /* Always undo Lua stack swap to avoid confusing the interpreter. */
412 copyTV(J->L, &rd->argv[0], &argv0);
413 copyTV(J->L, &rd->argv[1], &argv1);
414 if (errcode)
415 lj_err_throw(J->L, errcode); /* Propagate errors. */
416 rd->nres = -1; /* Pending call. */
417 } /* else: Interpreter will throw. */
420 /* -- Math library fast functions ----------------------------------------- */
422 static void LJ_FASTCALL recff_math_abs(jit_State *J, RecordFFData *rd)
424 TRef tr = lj_ir_tonum(J, J->base[0]);
425 J->base[0] = emitir(IRTN(IR_ABS), tr, lj_ir_knum_abs(J));
426 UNUSED(rd);
429 /* Record rounding functions math.floor and math.ceil. */
430 static void LJ_FASTCALL recff_math_round(jit_State *J, RecordFFData *rd)
432 TRef tr = J->base[0];
433 if (!tref_isinteger(tr)) { /* Pass through integers unmodified. */
434 tr = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, tr), rd->data);
435 /* Result is integral (or NaN/Inf), but may not fit an int32_t. */
436 if (LJ_DUALNUM) { /* Try to narrow using a guarded conversion to int. */
437 lua_Number n = lj_vm_foldfpm(numberVnum(&rd->argv[0]), rd->data);
438 if (n == (lua_Number)lj_num2int(n))
439 tr = emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_CHECK);
441 J->base[0] = tr;
445 /* Record unary math.* functions, mapped to IR_FPMATH opcode. */
446 static void LJ_FASTCALL recff_math_unary(jit_State *J, RecordFFData *rd)
448 J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data);
451 /* Record math.log. */
452 static void LJ_FASTCALL recff_math_log(jit_State *J, RecordFFData *rd)
454 TRef tr = lj_ir_tonum(J, J->base[0]);
455 if (J->base[1]) {
456 #ifdef LUAJIT_NO_LOG2
457 uint32_t fpm = IRFPM_LOG;
458 #else
459 uint32_t fpm = IRFPM_LOG2;
460 #endif
461 TRef trb = lj_ir_tonum(J, J->base[1]);
462 tr = emitir(IRTN(IR_FPMATH), tr, fpm);
463 trb = emitir(IRTN(IR_FPMATH), trb, fpm);
464 trb = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), trb);
465 tr = emitir(IRTN(IR_MUL), tr, trb);
466 } else {
467 tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_LOG);
469 J->base[0] = tr;
470 UNUSED(rd);
473 /* Record math.atan2. */
474 static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd)
476 TRef tr = lj_ir_tonum(J, J->base[0]);
477 TRef tr2 = lj_ir_tonum(J, J->base[1]);
478 J->base[0] = emitir(IRTN(IR_ATAN2), tr, tr2);
479 UNUSED(rd);
482 /* Record math.ldexp. */
483 static void LJ_FASTCALL recff_math_ldexp(jit_State *J, RecordFFData *rd)
485 TRef tr = lj_ir_tonum(J, J->base[0]);
486 #if LJ_TARGET_X86ORX64
487 TRef tr2 = lj_ir_tonum(J, J->base[1]);
488 #else
489 TRef tr2 = lj_opt_narrow_toint(J, J->base[1]);
490 #endif
491 J->base[0] = emitir(IRTN(IR_LDEXP), tr, tr2);
492 UNUSED(rd);
495 /* Record math.asin, math.acos, math.atan. */
496 static void LJ_FASTCALL recff_math_atrig(jit_State *J, RecordFFData *rd)
498 TRef y = lj_ir_tonum(J, J->base[0]);
499 TRef x = lj_ir_knum_one(J);
500 uint32_t ffid = rd->data;
501 if (ffid != FF_math_atan) {
502 TRef tmp = emitir(IRTN(IR_MUL), y, y);
503 tmp = emitir(IRTN(IR_SUB), x, tmp);
504 tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_SQRT);
505 if (ffid == FF_math_asin) { x = tmp; } else { x = y; y = tmp; }
507 J->base[0] = emitir(IRTN(IR_ATAN2), y, x);
510 static void LJ_FASTCALL recff_math_htrig(jit_State *J, RecordFFData *rd)
512 TRef tr = lj_ir_tonum(J, J->base[0]);
513 J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data);
516 static void LJ_FASTCALL recff_math_modf(jit_State *J, RecordFFData *rd)
518 TRef tr = J->base[0];
519 if (tref_isinteger(tr)) {
520 J->base[0] = tr;
521 J->base[1] = lj_ir_kint(J, 0);
522 } else {
523 TRef trt;
524 tr = lj_ir_tonum(J, tr);
525 trt = emitir(IRTN(IR_FPMATH), tr, IRFPM_TRUNC);
526 J->base[0] = trt;
527 J->base[1] = emitir(IRTN(IR_SUB), tr, trt);
529 rd->nres = 2;
532 static void LJ_FASTCALL recff_math_degrad(jit_State *J, RecordFFData *rd)
534 TRef tr = lj_ir_tonum(J, J->base[0]);
535 TRef trm = lj_ir_knum(J, numV(&J->fn->c.upvalue[0]));
536 J->base[0] = emitir(IRTN(IR_MUL), tr, trm);
537 UNUSED(rd);
540 static void LJ_FASTCALL recff_math_pow(jit_State *J, RecordFFData *rd)
542 TRef tr = lj_ir_tonum(J, J->base[0]);
543 if (!tref_isnumber_str(J->base[1]))
544 lj_trace_err(J, LJ_TRERR_BADTYPE);
545 J->base[0] = lj_opt_narrow_pow(J, tr, J->base[1], &rd->argv[1]);
546 UNUSED(rd);
549 static void LJ_FASTCALL recff_math_minmax(jit_State *J, RecordFFData *rd)
551 TRef tr = lj_ir_tonumber(J, J->base[0]);
552 uint32_t op = rd->data;
553 BCReg i;
554 for (i = 1; J->base[i] != 0; i++) {
555 TRef tr2 = lj_ir_tonumber(J, J->base[i]);
556 IRType t = IRT_INT;
557 if (!(tref_isinteger(tr) && tref_isinteger(tr2))) {
558 if (tref_isinteger(tr)) tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT);
559 if (tref_isinteger(tr2)) tr2 = emitir(IRTN(IR_CONV), tr2, IRCONV_NUM_INT);
560 t = IRT_NUM;
562 tr = emitir(IRT(op, t), tr, tr2);
564 J->base[0] = tr;
567 static void LJ_FASTCALL recff_math_random(jit_State *J, RecordFFData *rd)
569 GCudata *ud = udataV(&J->fn->c.upvalue[0]);
570 TRef tr, one;
571 lj_ir_kgc(J, obj2gco(ud), IRT_UDATA); /* Prevent collection. */
572 tr = lj_ir_call(J, IRCALL_lj_math_random_step, lj_ir_kptr(J, uddata(ud)));
573 one = lj_ir_knum_one(J);
574 tr = emitir(IRTN(IR_SUB), tr, one);
575 if (J->base[0]) {
576 TRef tr1 = lj_ir_tonum(J, J->base[0]);
577 if (J->base[1]) { /* d = floor(d*(r2-r1+1.0)) + r1 */
578 TRef tr2 = lj_ir_tonum(J, J->base[1]);
579 tr2 = emitir(IRTN(IR_SUB), tr2, tr1);
580 tr2 = emitir(IRTN(IR_ADD), tr2, one);
581 tr = emitir(IRTN(IR_MUL), tr, tr2);
582 tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR);
583 tr = emitir(IRTN(IR_ADD), tr, tr1);
584 } else { /* d = floor(d*r1) + 1.0 */
585 tr = emitir(IRTN(IR_MUL), tr, tr1);
586 tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR);
587 tr = emitir(IRTN(IR_ADD), tr, one);
590 J->base[0] = tr;
591 UNUSED(rd);
594 /* -- Bit library fast functions ------------------------------------------ */
596 /* Record unary bit.tobit, bit.bnot, bit.bswap. */
597 static void LJ_FASTCALL recff_bit_unary(jit_State *J, RecordFFData *rd)
599 TRef tr = lj_opt_narrow_tobit(J, J->base[0]);
600 J->base[0] = (rd->data == IR_TOBIT) ? tr : emitir(IRTI(rd->data), tr, 0);
603 /* Record N-ary bit.band, bit.bor, bit.bxor. */
604 static void LJ_FASTCALL recff_bit_nary(jit_State *J, RecordFFData *rd)
606 TRef tr = lj_opt_narrow_tobit(J, J->base[0]);
607 uint32_t op = rd->data;
608 BCReg i;
609 for (i = 1; J->base[i] != 0; i++)
610 tr = emitir(IRTI(op), tr, lj_opt_narrow_tobit(J, J->base[i]));
611 J->base[0] = tr;
614 /* Record bit shifts. */
615 static void LJ_FASTCALL recff_bit_shift(jit_State *J, RecordFFData *rd)
617 TRef tr = lj_opt_narrow_tobit(J, J->base[0]);
618 TRef tsh = lj_opt_narrow_tobit(J, J->base[1]);
619 IROp op = (IROp)rd->data;
620 if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) &&
621 !tref_isk(tsh))
622 tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 31));
623 #ifdef LJ_TARGET_UNIFYROT
624 if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) {
625 op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR;
626 tsh = emitir(IRTI(IR_NEG), tsh, tsh);
628 #endif
629 J->base[0] = emitir(IRTI(op), tr, tsh);
632 /* -- String library fast functions --------------------------------------- */
634 static void LJ_FASTCALL recff_string_len(jit_State *J, RecordFFData *rd)
636 J->base[0] = emitir(IRTI(IR_FLOAD), lj_ir_tostr(J, J->base[0]), IRFL_STR_LEN);
637 UNUSED(rd);
640 /* Handle string.byte (rd->data = 0) and string.sub (rd->data = 1). */
641 static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd)
643 TRef trstr = lj_ir_tostr(J, J->base[0]);
644 TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN);
645 TRef tr0 = lj_ir_kint(J, 0);
646 TRef trstart, trend;
647 GCstr *str = argv2str(J, &rd->argv[0]);
648 int32_t start, end;
649 if (rd->data) { /* string.sub(str, start [,end]) */
650 start = argv2int(J, &rd->argv[1]);
651 trstart = lj_opt_narrow_toint(J, J->base[1]);
652 trend = J->base[2];
653 if (tref_isnil(trend)) {
654 trend = lj_ir_kint(J, -1);
655 end = -1;
656 } else {
657 trend = lj_opt_narrow_toint(J, trend);
658 end = argv2int(J, &rd->argv[2]);
660 } else { /* string.byte(str, [,start [,end]]) */
661 if (tref_isnil(J->base[1])) {
662 start = 1;
663 trstart = lj_ir_kint(J, 1);
664 } else {
665 start = argv2int(J, &rd->argv[1]);
666 trstart = lj_opt_narrow_toint(J, J->base[1]);
668 if (J->base[1] && !tref_isnil(J->base[2])) {
669 trend = lj_opt_narrow_toint(J, J->base[2]);
670 end = argv2int(J, &rd->argv[2]);
671 } else {
672 trend = trstart;
673 end = start;
676 if (end < 0) {
677 emitir(IRTGI(IR_LT), trend, tr0);
678 trend = emitir(IRTI(IR_ADD), emitir(IRTI(IR_ADD), trlen, trend),
679 lj_ir_kint(J, 1));
680 end = end+(int32_t)str->len+1;
681 } else if ((MSize)end <= str->len) {
682 emitir(IRTGI(IR_ULE), trend, trlen);
683 } else {
684 emitir(IRTGI(IR_GT), trend, trlen);
685 end = (int32_t)str->len;
686 trend = trlen;
688 if (start < 0) {
689 emitir(IRTGI(IR_LT), trstart, tr0);
690 trstart = emitir(IRTI(IR_ADD), trlen, trstart);
691 start = start+(int32_t)str->len;
692 emitir(start < 0 ? IRTGI(IR_LT) : IRTGI(IR_GE), trstart, tr0);
693 if (start < 0) {
694 trstart = tr0;
695 start = 0;
697 } else {
698 if (start == 0) {
699 emitir(IRTGI(IR_EQ), trstart, tr0);
700 trstart = tr0;
701 } else {
702 trstart = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, -1));
703 emitir(IRTGI(IR_GE), trstart, tr0);
704 start--;
707 if (rd->data) { /* Return string.sub result. */
708 if (end - start >= 0) {
709 /* Also handle empty range here, to avoid extra traces. */
710 TRef trptr, trslen = emitir(IRTI(IR_SUB), trend, trstart);
711 emitir(IRTGI(IR_GE), trslen, tr0);
712 trptr = emitir(IRT(IR_STRREF, IRT_P32), trstr, trstart);
713 J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), trptr, trslen);
714 } else { /* Range underflow: return empty string. */
715 emitir(IRTGI(IR_LT), trend, trstart);
716 J->base[0] = lj_ir_kstr(J, lj_str_new(J->L, strdata(str), 0));
718 } else { /* Return string.byte result(s). */
719 ptrdiff_t i, len = end - start;
720 if (len > 0) {
721 TRef trslen = emitir(IRTI(IR_SUB), trend, trstart);
722 emitir(IRTGI(IR_EQ), trslen, lj_ir_kint(J, (int32_t)len));
723 if (J->baseslot + len > LJ_MAX_JSLOTS)
724 lj_trace_err_info(J, LJ_TRERR_STACKOV);
725 rd->nres = len;
726 for (i = 0; i < len; i++) {
727 TRef tmp = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, (int32_t)i));
728 tmp = emitir(IRT(IR_STRREF, IRT_P32), trstr, tmp);
729 J->base[i] = emitir(IRT(IR_XLOAD, IRT_U8), tmp, IRXLOAD_READONLY);
731 } else { /* Empty range or range underflow: return no results. */
732 emitir(IRTGI(IR_LE), trend, trstart);
733 rd->nres = 0;
738 /* -- Table library fast functions ---------------------------------------- */
740 static void LJ_FASTCALL recff_table_getn(jit_State *J, RecordFFData *rd)
742 if (tref_istab(J->base[0]))
743 J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, J->base[0]);
744 /* else: Interpreter will throw. */
745 UNUSED(rd);
748 static void LJ_FASTCALL recff_table_remove(jit_State *J, RecordFFData *rd)
750 TRef tab = J->base[0];
751 rd->nres = 0;
752 if (tref_istab(tab)) {
753 if (tref_isnil(J->base[1])) { /* Simple pop: t[#t] = nil */
754 TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, tab);
755 GCtab *t = tabV(&rd->argv[0]);
756 MSize len = lj_tab_len(t);
757 emitir(IRTGI(len ? IR_NE : IR_EQ), trlen, lj_ir_kint(J, 0));
758 if (len) {
759 RecordIndex ix;
760 ix.tab = tab;
761 ix.key = trlen;
762 settabV(J->L, &ix.tabv, t);
763 setintV(&ix.keyv, len);
764 ix.idxchain = 0;
765 if (results_wanted(J) != 0) { /* Specialize load only if needed. */
766 ix.val = 0;
767 J->base[0] = lj_record_idx(J, &ix); /* Load previous value. */
768 rd->nres = 1;
769 /* Assumes ix.key/ix.tab is not modified for raw lj_record_idx(). */
771 ix.val = TREF_NIL;
772 lj_record_idx(J, &ix); /* Remove value. */
774 } else { /* Complex case: remove in the middle. */
775 recff_nyiu(J);
777 } /* else: Interpreter will throw. */
780 static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd)
782 RecordIndex ix;
783 ix.tab = J->base[0];
784 ix.val = J->base[1];
785 rd->nres = 0;
786 if (tref_istab(ix.tab) && ix.val) {
787 if (!J->base[2]) { /* Simple push: t[#t+1] = v */
788 TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, ix.tab);
789 GCtab *t = tabV(&rd->argv[0]);
790 ix.key = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1));
791 settabV(J->L, &ix.tabv, t);
792 setintV(&ix.keyv, lj_tab_len(t) + 1);
793 ix.idxchain = 0;
794 lj_record_idx(J, &ix); /* Set new value. */
795 } else { /* Complex case: insert in the middle. */
796 recff_nyiu(J);
798 } /* else: Interpreter will throw. */
801 /* -- I/O library fast functions ------------------------------------------ */
803 /* Get FILE* for I/O function. Any I/O error aborts recording, so there's
804 ** no need to encode the alternate cases for any of the guards.
806 static TRef recff_io_fp(jit_State *J, TRef *udp, int32_t id)
808 TRef tr, ud, fp;
809 if (id) { /* io.func() */
810 tr = lj_ir_kptr(J, &J2G(J)->gcroot[id]);
811 ud = emitir(IRT(IR_XLOAD, IRT_UDATA), tr, 0);
812 } else { /* fp:method() */
813 ud = J->base[0];
814 if (!tref_isudata(ud))
815 lj_trace_err(J, LJ_TRERR_BADTYPE);
816 tr = emitir(IRT(IR_FLOAD, IRT_U8), ud, IRFL_UDATA_UDTYPE);
817 emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE));
819 *udp = ud;
820 fp = emitir(IRT(IR_FLOAD, IRT_PTR), ud, IRFL_UDATA_FILE);
821 emitir(IRTG(IR_NE, IRT_PTR), fp, lj_ir_knull(J, IRT_PTR));
822 return fp;
825 static void LJ_FASTCALL recff_io_write(jit_State *J, RecordFFData *rd)
827 TRef ud, fp = recff_io_fp(J, &ud, rd->data);
828 TRef zero = lj_ir_kint(J, 0);
829 TRef one = lj_ir_kint(J, 1);
830 ptrdiff_t i = rd->data == 0 ? 1 : 0;
831 for (; J->base[i]; i++) {
832 TRef str = lj_ir_tostr(J, J->base[i]);
833 TRef buf = emitir(IRT(IR_STRREF, IRT_P32), str, zero);
834 TRef len = emitir(IRTI(IR_FLOAD), str, IRFL_STR_LEN);
835 if (tref_isk(len) && IR(tref_ref(len))->i == 1) {
836 TRef tr = emitir(IRT(IR_XLOAD, IRT_U8), buf, IRXLOAD_READONLY);
837 tr = lj_ir_call(J, IRCALL_fputc, tr, fp);
838 if (results_wanted(J) != 0) /* Check result only if not ignored. */
839 emitir(IRTGI(IR_NE), tr, lj_ir_kint(J, -1));
840 } else {
841 TRef tr = lj_ir_call(J, IRCALL_fwrite, buf, one, len, fp);
842 if (results_wanted(J) != 0) /* Check result only if not ignored. */
843 emitir(IRTGI(IR_EQ), tr, len);
846 J->base[0] = LJ_52 ? ud : TREF_TRUE;
849 static void LJ_FASTCALL recff_io_flush(jit_State *J, RecordFFData *rd)
851 TRef ud, fp = recff_io_fp(J, &ud, rd->data);
852 TRef tr = lj_ir_call(J, IRCALL_fflush, fp);
853 if (results_wanted(J) != 0) /* Check result only if not ignored. */
854 emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0));
855 J->base[0] = TREF_TRUE;
858 /* -- Record calls to fast functions -------------------------------------- */
860 #include "lj_recdef.h"
862 static uint32_t recdef_lookup(GCfunc *fn)
864 if (fn->c.ffid < sizeof(recff_idmap)/sizeof(recff_idmap[0]))
865 return recff_idmap[fn->c.ffid];
866 else
867 return 0;
870 /* Record entry to a fast function or C function. */
871 void lj_ffrecord_func(jit_State *J)
873 RecordFFData rd;
874 uint32_t m = recdef_lookup(J->fn);
875 rd.data = m & 0xff;
876 rd.nres = 1; /* Default is one result. */
877 rd.argv = J->L->base;
878 J->base[J->maxslot] = 0; /* Mark end of arguments. */
879 (recff_func[m >> 8])(J, &rd); /* Call recff_* handler. */
880 if (rd.nres >= 0) {
881 if (J->postproc == LJ_POST_NONE) J->postproc = LJ_POST_FFRETRY;
882 lj_record_ret(J, 0, rd.nres);
886 #undef IR
887 #undef emitir
889 #endif