diff options
Diffstat (limited to 'src/lj_ffrecord.c')
-rw-r--r-- | src/lj_ffrecord.c | 1001 |
1 files changed, 840 insertions, 161 deletions
diff --git a/src/lj_ffrecord.c b/src/lj_ffrecord.c index f833bc16..13f91333 100644 --- a/src/lj_ffrecord.c +++ b/src/lj_ffrecord.c | |||
@@ -11,6 +11,7 @@ | |||
11 | #if LJ_HASJIT | 11 | #if LJ_HASJIT |
12 | 12 | ||
13 | #include "lj_err.h" | 13 | #include "lj_err.h" |
14 | #include "lj_buf.h" | ||
14 | #include "lj_str.h" | 15 | #include "lj_str.h" |
15 | #include "lj_tab.h" | 16 | #include "lj_tab.h" |
16 | #include "lj_frame.h" | 17 | #include "lj_frame.h" |
@@ -27,6 +28,8 @@ | |||
27 | #include "lj_dispatch.h" | 28 | #include "lj_dispatch.h" |
28 | #include "lj_vm.h" | 29 | #include "lj_vm.h" |
29 | #include "lj_strscan.h" | 30 | #include "lj_strscan.h" |
31 | #include "lj_strfmt.h" | ||
32 | #include "lj_serialize.h" | ||
30 | 33 | ||
31 | /* Some local macros to save typing. Undef'd at the end. */ | 34 | /* Some local macros to save typing. Undef'd at the end. */ |
32 | #define IR(ref) (&J->cur.ir[(ref)]) | 35 | #define IR(ref) (&J->cur.ir[(ref)]) |
@@ -79,10 +82,7 @@ static GCstr *argv2str(jit_State *J, TValue *o) | |||
79 | GCstr *s; | 82 | GCstr *s; |
80 | if (!tvisnumber(o)) | 83 | if (!tvisnumber(o)) |
81 | lj_trace_err(J, LJ_TRERR_BADTYPE); | 84 | lj_trace_err(J, LJ_TRERR_BADTYPE); |
82 | if (tvisint(o)) | 85 | s = lj_strfmt_number(J->L, 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); | 86 | setstrV(J->L, o, s); |
87 | return s; | 87 | return s; |
88 | } | 88 | } |
@@ -98,27 +98,102 @@ static ptrdiff_t results_wanted(jit_State *J) | |||
98 | return -1; | 98 | return -1; |
99 | } | 99 | } |
100 | 100 | ||
101 | /* Throw error for unsupported variant of fast function. */ | 101 | /* Trace stitching: add continuation below frame to start a new trace. */ |
102 | LJ_NORET static void recff_nyiu(jit_State *J) | 102 | static void recff_stitch(jit_State *J) |
103 | { | 103 | { |
104 | setfuncV(J->L, &J->errinfo, J->fn); | 104 | ASMFunction cont = lj_cont_stitch; |
105 | lj_trace_err_info(J, LJ_TRERR_NYIFFU); | 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; | ||
106 | } | 146 | } |
107 | 147 | ||
108 | /* Fallback handler for all fast functions that are not recorded (yet). */ | 148 | /* Fallback handler for fast functions that are not recorded (yet). */ |
109 | static void LJ_FASTCALL recff_nyi(jit_State *J, RecordFFData *rd) | 149 | static void LJ_FASTCALL recff_nyi(jit_State *J, RecordFFData *rd) |
110 | { | 150 | { |
111 | setfuncV(J->L, &J->errinfo, J->fn); | 151 | if (J->cur.nins < (IRRef)J->param[JIT_P_minstitch] + REF_BASE) { |
112 | lj_trace_err_info(J, LJ_TRERR_NYIFF); | 152 | lj_trace_err_info(J, LJ_TRERR_TRACEUV); |
113 | UNUSED(rd); | 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 | } | ||
114 | } | 176 | } |
115 | 177 | ||
116 | /* C functions can have arbitrary side-effects and are not recorded (yet). */ | 178 | /* Fallback handler for unsupported variants of fast functions. */ |
117 | static void LJ_FASTCALL recff_c(jit_State *J, RecordFFData *rd) | 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) | ||
118 | { | 186 | { |
119 | setfuncV(J->L, &J->errinfo, J->fn); | 187 | return emitir(IRT(IR_BUFHDR, IRT_PGC), |
120 | lj_trace_err_info(J, LJ_TRERR_NYICF); | 188 | lj_ir_kptr(J, &J2G(J)->tmpbuf), IRBUFHDR_RESET); |
121 | UNUSED(rd); | 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); | ||
122 | } | 197 | } |
123 | 198 | ||
124 | /* -- Base library fast functions ----------------------------------------- */ | 199 | /* -- Base library fast functions ----------------------------------------- */ |
@@ -135,7 +210,7 @@ static void LJ_FASTCALL recff_type(jit_State *J, RecordFFData *rd) | |||
135 | uint32_t t; | 210 | uint32_t t; |
136 | if (tvisnumber(&rd->argv[0])) | 211 | if (tvisnumber(&rd->argv[0])) |
137 | t = ~LJ_TNUMX; | 212 | t = ~LJ_TNUMX; |
138 | else if (LJ_64 && tvislightud(&rd->argv[0])) | 213 | else if (LJ_64 && !LJ_GC64 && tvislightud(&rd->argv[0])) |
139 | t = ~LJ_TLIGHTUD; | 214 | t = ~LJ_TLIGHTUD; |
140 | else | 215 | else |
141 | t = ~itype(&rd->argv[0]); | 216 | t = ~itype(&rd->argv[0]); |
@@ -167,7 +242,7 @@ static void LJ_FASTCALL recff_setmetatable(jit_State *J, RecordFFData *rd) | |||
167 | ix.tab = tr; | 242 | ix.tab = tr; |
168 | copyTV(J->L, &ix.tabv, &rd->argv[0]); | 243 | copyTV(J->L, &ix.tabv, &rd->argv[0]); |
169 | lj_record_mm_lookup(J, &ix, MM_metatable); /* Guard for no __metatable. */ | 244 | lj_record_mm_lookup(J, &ix, MM_metatable); /* Guard for no __metatable. */ |
170 | fref = emitir(IRT(IR_FREF, IRT_P32), tr, IRFL_TAB_META); | 245 | fref = emitir(IRT(IR_FREF, IRT_PGC), tr, IRFL_TAB_META); |
171 | mtref = tref_isnil(mt) ? lj_ir_knull(J, IRT_TAB) : mt; | 246 | mtref = tref_isnil(mt) ? lj_ir_knull(J, IRT_TAB) : mt; |
172 | emitir(IRT(IR_FSTORE, IRT_TAB), fref, mtref); | 247 | emitir(IRT(IR_FSTORE, IRT_TAB), fref, mtref); |
173 | if (!tref_isnil(mt)) | 248 | if (!tref_isnil(mt)) |
@@ -220,7 +295,7 @@ static void LJ_FASTCALL recff_rawlen(jit_State *J, RecordFFData *rd) | |||
220 | if (tref_isstr(tr)) | 295 | if (tref_isstr(tr)) |
221 | J->base[0] = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN); | 296 | J->base[0] = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN); |
222 | else if (tref_istab(tr)) | 297 | else if (tref_istab(tr)) |
223 | J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, tr); | 298 | J->base[0] = emitir(IRTI(IR_ALEN), tr, TREF_NIL); |
224 | /* else: Interpreter will throw. */ | 299 | /* else: Interpreter will throw. */ |
225 | UNUSED(rd); | 300 | UNUSED(rd); |
226 | } | 301 | } |
@@ -233,9 +308,9 @@ int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv) | |||
233 | if (strV(tv)->len == 1) { | 308 | if (strV(tv)->len == 1) { |
234 | emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, strV(tv))); | 309 | emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, strV(tv))); |
235 | } else { | 310 | } else { |
236 | TRef trptr = emitir(IRT(IR_STRREF, IRT_P32), tr, lj_ir_kint(J, 0)); | 311 | TRef trptr = emitir(IRT(IR_STRREF, IRT_PGC), tr, lj_ir_kint(J, 0)); |
237 | TRef trchar = emitir(IRT(IR_XLOAD, IRT_U8), trptr, IRXLOAD_READONLY); | 312 | TRef trchar = emitir(IRT(IR_XLOAD, IRT_U8), trptr, IRXLOAD_READONLY); |
238 | emitir(IRTG(IR_EQ, IRT_INT), trchar, lj_ir_kint(J, '#')); | 313 | emitir(IRTGI(IR_EQ), trchar, lj_ir_kint(J, '#')); |
239 | } | 314 | } |
240 | return 0; | 315 | return 0; |
241 | } else { /* select(n, ...) */ | 316 | } else { /* select(n, ...) */ |
@@ -263,7 +338,8 @@ static void LJ_FASTCALL recff_select(jit_State *J, RecordFFData *rd) | |||
263 | J->base[i] = J->base[start+i]; | 338 | J->base[i] = J->base[start+i]; |
264 | } /* else: Interpreter will throw. */ | 339 | } /* else: Interpreter will throw. */ |
265 | } else { | 340 | } else { |
266 | recff_nyiu(J); | 341 | recff_nyiu(J, rd); |
342 | return; | ||
267 | } | 343 | } |
268 | } /* else: Interpreter will throw. */ | 344 | } /* else: Interpreter will throw. */ |
269 | } | 345 | } |
@@ -274,14 +350,18 @@ static void LJ_FASTCALL recff_tonumber(jit_State *J, RecordFFData *rd) | |||
274 | TRef base = J->base[1]; | 350 | TRef base = J->base[1]; |
275 | if (tr && !tref_isnil(base)) { | 351 | if (tr && !tref_isnil(base)) { |
276 | base = lj_opt_narrow_toint(J, base); | 352 | base = lj_opt_narrow_toint(J, base); |
277 | if (!tref_isk(base) || IR(tref_ref(base))->i != 10) | 353 | if (!tref_isk(base) || IR(tref_ref(base))->i != 10) { |
278 | recff_nyiu(J); | 354 | recff_nyiu(J, rd); |
355 | return; | ||
356 | } | ||
279 | } | 357 | } |
280 | if (tref_isnumber_str(tr)) { | 358 | if (tref_isnumber_str(tr)) { |
281 | if (tref_isstr(tr)) { | 359 | if (tref_isstr(tr)) { |
282 | TValue tmp; | 360 | TValue tmp; |
283 | if (!lj_strscan_num(strV(&rd->argv[0]), &tmp)) | 361 | if (!lj_strscan_num(strV(&rd->argv[0]), &tmp)) { |
284 | recff_nyiu(J); /* Would need an inverted STRTO for this case. */ | 362 | recff_nyiu(J, rd); /* Would need an inverted STRTO for this case. */ |
363 | return; | ||
364 | } | ||
285 | tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); | 365 | tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); |
286 | } | 366 | } |
287 | #if LJ_HASFFI | 367 | #if LJ_HASFFI |
@@ -313,10 +393,10 @@ static int recff_metacall(jit_State *J, RecordFFData *rd, MMS mm) | |||
313 | int errcode; | 393 | int errcode; |
314 | TValue argv0; | 394 | TValue argv0; |
315 | /* Temporarily insert metamethod below object. */ | 395 | /* Temporarily insert metamethod below object. */ |
316 | J->base[1] = J->base[0]; | 396 | J->base[1+LJ_FR2] = J->base[0]; |
317 | J->base[0] = ix.mobj; | 397 | J->base[0] = ix.mobj; |
318 | copyTV(J->L, &argv0, &rd->argv[0]); | 398 | copyTV(J->L, &argv0, &rd->argv[0]); |
319 | copyTV(J->L, &rd->argv[1], &rd->argv[0]); | 399 | copyTV(J->L, &rd->argv[1+LJ_FR2], &rd->argv[0]); |
320 | copyTV(J->L, &rd->argv[0], &ix.mobjv); | 400 | copyTV(J->L, &rd->argv[0], &ix.mobjv); |
321 | /* Need to protect lj_record_tailcall because it may throw. */ | 401 | /* Need to protect lj_record_tailcall because it may throw. */ |
322 | errcode = lj_vm_cpcall(J->L, NULL, J, recff_metacall_cp); | 402 | errcode = lj_vm_cpcall(J->L, NULL, J, recff_metacall_cp); |
@@ -336,13 +416,15 @@ static void LJ_FASTCALL recff_tostring(jit_State *J, RecordFFData *rd) | |||
336 | if (tref_isstr(tr)) { | 416 | if (tref_isstr(tr)) { |
337 | /* Ignore __tostring in the string base metatable. */ | 417 | /* Ignore __tostring in the string base metatable. */ |
338 | /* Pass on result in J->base[0]. */ | 418 | /* Pass on result in J->base[0]. */ |
339 | } else if (!recff_metacall(J, rd, MM_tostring)) { | 419 | } else if (tr && !recff_metacall(J, rd, MM_tostring)) { |
340 | if (tref_isnumber(tr)) { | 420 | if (tref_isnumber(tr)) { |
341 | J->base[0] = emitir(IRT(IR_TOSTR, IRT_STR), tr, 0); | 421 | J->base[0] = emitir(IRT(IR_TOSTR, IRT_STR), tr, |
422 | tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT); | ||
342 | } else if (tref_ispri(tr)) { | 423 | } else if (tref_ispri(tr)) { |
343 | J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[tref_type(tr)])); | 424 | J->base[0] = lj_ir_kstr(J, lj_strfmt_obj(J->L, &rd->argv[0])); |
344 | } else { | 425 | } else { |
345 | recff_nyiu(J); | 426 | recff_nyiu(J, rd); |
427 | return; | ||
346 | } | 428 | } |
347 | } | 429 | } |
348 | } | 430 | } |
@@ -364,15 +446,15 @@ static void LJ_FASTCALL recff_ipairs_aux(jit_State *J, RecordFFData *rd) | |||
364 | } /* else: Interpreter will throw. */ | 446 | } /* else: Interpreter will throw. */ |
365 | } | 447 | } |
366 | 448 | ||
367 | static void LJ_FASTCALL recff_ipairs(jit_State *J, RecordFFData *rd) | 449 | static void LJ_FASTCALL recff_xpairs(jit_State *J, RecordFFData *rd) |
368 | { | 450 | { |
369 | TRef tr = J->base[0]; | 451 | TRef tr = J->base[0]; |
370 | if (!((LJ_52 || (LJ_HASFFI && tref_iscdata(tr))) && | 452 | if (!((LJ_52 || (LJ_HASFFI && tref_iscdata(tr))) && |
371 | recff_metacall(J, rd, MM_ipairs))) { | 453 | recff_metacall(J, rd, MM_pairs + rd->data))) { |
372 | if (tref_istab(tr)) { | 454 | if (tref_istab(tr)) { |
373 | J->base[0] = lj_ir_kfunc(J, funcV(&J->fn->c.upvalue[0])); | 455 | J->base[0] = lj_ir_kfunc(J, funcV(&J->fn->c.upvalue[0])); |
374 | J->base[1] = tr; | 456 | J->base[1] = tr; |
375 | J->base[2] = lj_ir_kint(J, 0); | 457 | J->base[2] = rd->data ? lj_ir_kint(J, 0) : TREF_NIL; |
376 | rd->nres = 3; | 458 | rd->nres = 3; |
377 | } /* else: Interpreter will throw. */ | 459 | } /* else: Interpreter will throw. */ |
378 | } | 460 | } |
@@ -381,8 +463,13 @@ static void LJ_FASTCALL recff_ipairs(jit_State *J, RecordFFData *rd) | |||
381 | static void LJ_FASTCALL recff_pcall(jit_State *J, RecordFFData *rd) | 463 | static void LJ_FASTCALL recff_pcall(jit_State *J, RecordFFData *rd) |
382 | { | 464 | { |
383 | if (J->maxslot >= 1) { | 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 | ||
384 | lj_record_call(J, 0, J->maxslot - 1); | 470 | lj_record_call(J, 0, J->maxslot - 1); |
385 | rd->nres = -1; /* Pending call. */ | 471 | rd->nres = -1; /* Pending call. */ |
472 | J->needsnap = 1; /* Start catching on-trace errors. */ | ||
386 | } /* else: Interpreter will throw. */ | 473 | } /* else: Interpreter will throw. */ |
387 | } | 474 | } |
388 | 475 | ||
@@ -406,6 +493,10 @@ static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd) | |||
406 | copyTV(J->L, &argv1, &rd->argv[1]); | 493 | copyTV(J->L, &argv1, &rd->argv[1]); |
407 | copyTV(J->L, &rd->argv[0], &argv1); | 494 | copyTV(J->L, &rd->argv[0], &argv1); |
408 | copyTV(J->L, &rd->argv[1], &argv0); | 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 | ||
409 | /* Need to protect lj_record_call because it may throw. */ | 500 | /* Need to protect lj_record_call because it may throw. */ |
410 | errcode = lj_vm_cpcall(J->L, NULL, J, recff_xpcall_cp); | 501 | errcode = lj_vm_cpcall(J->L, NULL, J, recff_xpcall_cp); |
411 | /* Always undo Lua stack swap to avoid confusing the interpreter. */ | 502 | /* Always undo Lua stack swap to avoid confusing the interpreter. */ |
@@ -414,7 +505,54 @@ static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd) | |||
414 | if (errcode) | 505 | if (errcode) |
415 | lj_err_throw(J->L, errcode); /* Propagate errors. */ | 506 | lj_err_throw(J->L, errcode); /* Propagate errors. */ |
416 | rd->nres = -1; /* Pending call. */ | 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; | ||
417 | } /* else: Interpreter will throw. */ | 554 | } /* else: Interpreter will throw. */ |
555 | #endif | ||
418 | } | 556 | } |
419 | 557 | ||
420 | /* -- Math library fast functions ----------------------------------------- */ | 558 | /* -- Math library fast functions ----------------------------------------- */ |
@@ -422,7 +560,7 @@ static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd) | |||
422 | static void LJ_FASTCALL recff_math_abs(jit_State *J, RecordFFData *rd) | 560 | static void LJ_FASTCALL recff_math_abs(jit_State *J, RecordFFData *rd) |
423 | { | 561 | { |
424 | TRef tr = lj_ir_tonum(J, J->base[0]); | 562 | TRef tr = lj_ir_tonum(J, J->base[0]); |
425 | J->base[0] = emitir(IRTN(IR_ABS), tr, lj_ir_knum_abs(J)); | 563 | J->base[0] = emitir(IRTN(IR_ABS), tr, lj_ir_ksimd(J, LJ_KSIMD_ABS)); |
426 | UNUSED(rd); | 564 | UNUSED(rd); |
427 | } | 565 | } |
428 | 566 | ||
@@ -475,7 +613,7 @@ static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd) | |||
475 | { | 613 | { |
476 | TRef tr = lj_ir_tonum(J, J->base[0]); | 614 | TRef tr = lj_ir_tonum(J, J->base[0]); |
477 | TRef tr2 = lj_ir_tonum(J, J->base[1]); | 615 | TRef tr2 = lj_ir_tonum(J, J->base[1]); |
478 | J->base[0] = emitir(IRTN(IR_ATAN2), tr, tr2); | 616 | J->base[0] = lj_ir_call(J, IRCALL_atan2, tr, tr2); |
479 | UNUSED(rd); | 617 | UNUSED(rd); |
480 | } | 618 | } |
481 | 619 | ||
@@ -492,51 +630,12 @@ static void LJ_FASTCALL recff_math_ldexp(jit_State *J, RecordFFData *rd) | |||
492 | UNUSED(rd); | 630 | UNUSED(rd); |
493 | } | 631 | } |
494 | 632 | ||
495 | /* Record math.asin, math.acos, math.atan. */ | 633 | static void LJ_FASTCALL recff_math_call(jit_State *J, RecordFFData *rd) |
496 | static void LJ_FASTCALL recff_math_atrig(jit_State *J, RecordFFData *rd) | ||
497 | { | ||
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; } | ||
506 | } | ||
507 | J->base[0] = emitir(IRTN(IR_ATAN2), y, x); | ||
508 | } | ||
509 | |||
510 | static void LJ_FASTCALL recff_math_htrig(jit_State *J, RecordFFData *rd) | ||
511 | { | 634 | { |
512 | TRef tr = lj_ir_tonum(J, J->base[0]); | 635 | TRef tr = lj_ir_tonum(J, J->base[0]); |
513 | J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data); | 636 | J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data); |
514 | } | 637 | } |
515 | 638 | ||
516 | static void LJ_FASTCALL recff_math_modf(jit_State *J, RecordFFData *rd) | ||
517 | { | ||
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); | ||
528 | } | ||
529 | rd->nres = 2; | ||
530 | } | ||
531 | |||
532 | static void LJ_FASTCALL recff_math_degrad(jit_State *J, RecordFFData *rd) | ||
533 | { | ||
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); | ||
538 | } | ||
539 | |||
540 | static void LJ_FASTCALL recff_math_pow(jit_State *J, RecordFFData *rd) | 639 | static void LJ_FASTCALL recff_math_pow(jit_State *J, RecordFFData *rd) |
541 | { | 640 | { |
542 | J->base[0] = lj_opt_narrow_pow(J, J->base[0], J->base[1], | 641 | J->base[0] = lj_opt_narrow_pow(J, J->base[0], J->base[1], |
@@ -567,7 +666,7 @@ static void LJ_FASTCALL recff_math_random(jit_State *J, RecordFFData *rd) | |||
567 | GCudata *ud = udataV(&J->fn->c.upvalue[0]); | 666 | GCudata *ud = udataV(&J->fn->c.upvalue[0]); |
568 | TRef tr, one; | 667 | TRef tr, one; |
569 | lj_ir_kgc(J, obj2gco(ud), IRT_UDATA); /* Prevent collection. */ | 668 | lj_ir_kgc(J, obj2gco(ud), IRT_UDATA); /* Prevent collection. */ |
570 | tr = lj_ir_call(J, IRCALL_lj_math_random_step, lj_ir_kptr(J, uddata(ud))); | 669 | tr = lj_ir_call(J, IRCALL_lj_prng_u64d, lj_ir_kptr(J, uddata(ud))); |
571 | one = lj_ir_knum_one(J); | 670 | one = lj_ir_knum_one(J); |
572 | tr = emitir(IRTN(IR_SUB), tr, one); | 671 | tr = emitir(IRTN(IR_SUB), tr, one); |
573 | if (J->base[0]) { | 672 | if (J->base[0]) { |
@@ -591,48 +690,105 @@ static void LJ_FASTCALL recff_math_random(jit_State *J, RecordFFData *rd) | |||
591 | 690 | ||
592 | /* -- Bit library fast functions ------------------------------------------ */ | 691 | /* -- Bit library fast functions ------------------------------------------ */ |
593 | 692 | ||
594 | /* Record unary bit.tobit, bit.bnot, bit.bswap. */ | 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. */ | ||
595 | static void LJ_FASTCALL recff_bit_unary(jit_State *J, RecordFFData *rd) | 705 | static void LJ_FASTCALL recff_bit_unary(jit_State *J, RecordFFData *rd) |
596 | { | 706 | { |
597 | TRef tr = lj_opt_narrow_tobit(J, J->base[0]); | 707 | #if LJ_HASFFI |
598 | J->base[0] = (rd->data == IR_TOBIT) ? tr : emitir(IRTI(rd->data), tr, 0); | 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); | ||
599 | } | 712 | } |
600 | 713 | ||
601 | /* Record N-ary bit.band, bit.bor, bit.bxor. */ | 714 | /* Record N-ary bit.band, bit.bor, bit.bxor. */ |
602 | static void LJ_FASTCALL recff_bit_nary(jit_State *J, RecordFFData *rd) | 715 | static void LJ_FASTCALL recff_bit_nary(jit_State *J, RecordFFData *rd) |
603 | { | 716 | { |
604 | TRef tr = lj_opt_narrow_tobit(J, J->base[0]); | 717 | #if LJ_HASFFI |
605 | uint32_t op = rd->data; | 718 | if (recff_bit64_nary(J, rd)) |
606 | BCReg i; | 719 | return; |
607 | for (i = 1; J->base[i] != 0; i++) | 720 | #endif |
608 | tr = emitir(IRTI(op), tr, lj_opt_narrow_tobit(J, J->base[i])); | 721 | { |
609 | J->base[0] = tr; | 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 | } | ||
610 | } | 729 | } |
611 | 730 | ||
612 | /* Record bit shifts. */ | 731 | /* Record bit shifts. */ |
613 | static void LJ_FASTCALL recff_bit_shift(jit_State *J, RecordFFData *rd) | 732 | static void LJ_FASTCALL recff_bit_shift(jit_State *J, RecordFFData *rd) |
614 | { | 733 | { |
615 | TRef tr = lj_opt_narrow_tobit(J, J->base[0]); | 734 | #if LJ_HASFFI |
616 | TRef tsh = lj_opt_narrow_tobit(J, J->base[1]); | 735 | if (recff_bit64_shift(J, rd)) |
617 | IROp op = (IROp)rd->data; | 736 | return; |
618 | if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) && | 737 | #endif |
619 | !tref_isk(tsh)) | 738 | { |
620 | tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 31)); | 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)); | ||
621 | #ifdef LJ_TARGET_UNIFYROT | 745 | #ifdef LJ_TARGET_UNIFYROT |
622 | if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) { | 746 | if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) { |
623 | op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR; | 747 | op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR; |
624 | tsh = emitir(IRTI(IR_NEG), tsh, tsh); | 748 | tsh = emitir(IRTI(IR_NEG), tsh, tsh); |
749 | } | ||
750 | #endif | ||
751 | J->base[0] = emitir(IRTI(op), tr, tsh); | ||
625 | } | 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. */ | ||
626 | #endif | 763 | #endif |
627 | J->base[0] = emitir(IRTI(op), tr, tsh); | ||
628 | } | 764 | } |
629 | 765 | ||
630 | /* -- String library fast functions --------------------------------------- */ | 766 | /* -- String library fast functions --------------------------------------- */ |
631 | 767 | ||
632 | static void LJ_FASTCALL recff_string_len(jit_State *J, RecordFFData *rd) | 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) | ||
633 | { | 771 | { |
634 | J->base[0] = emitir(IRTI(IR_FLOAD), lj_ir_tostr(J, J->base[0]), IRFL_STR_LEN); | 772 | int32_t start = *st; |
635 | UNUSED(rd); | 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; | ||
636 | } | 792 | } |
637 | 793 | ||
638 | /* Handle string.byte (rd->data = 0) and string.sub (rd->data = 1). */ | 794 | /* Handle string.byte (rd->data = 0) and string.sub (rd->data = 1). */ |
@@ -679,39 +835,21 @@ static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd) | |||
679 | } else if ((MSize)end <= str->len) { | 835 | } else if ((MSize)end <= str->len) { |
680 | emitir(IRTGI(IR_ULE), trend, trlen); | 836 | emitir(IRTGI(IR_ULE), trend, trlen); |
681 | } else { | 837 | } else { |
682 | emitir(IRTGI(IR_GT), trend, trlen); | 838 | emitir(IRTGI(IR_UGT), trend, trlen); |
683 | end = (int32_t)str->len; | 839 | end = (int32_t)str->len; |
684 | trend = trlen; | 840 | trend = trlen; |
685 | } | 841 | } |
686 | if (start < 0) { | 842 | trstart = recff_string_start(J, str, &start, trstart, trlen, tr0); |
687 | emitir(IRTGI(IR_LT), trstart, tr0); | ||
688 | trstart = emitir(IRTI(IR_ADD), trlen, trstart); | ||
689 | start = start+(int32_t)str->len; | ||
690 | emitir(start < 0 ? IRTGI(IR_LT) : IRTGI(IR_GE), trstart, tr0); | ||
691 | if (start < 0) { | ||
692 | trstart = tr0; | ||
693 | start = 0; | ||
694 | } | ||
695 | } else { | ||
696 | if (start == 0) { | ||
697 | emitir(IRTGI(IR_EQ), trstart, tr0); | ||
698 | trstart = tr0; | ||
699 | } else { | ||
700 | trstart = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, -1)); | ||
701 | emitir(IRTGI(IR_GE), trstart, tr0); | ||
702 | start--; | ||
703 | } | ||
704 | } | ||
705 | if (rd->data) { /* Return string.sub result. */ | 843 | if (rd->data) { /* Return string.sub result. */ |
706 | if (end - start >= 0) { | 844 | if (end - start >= 0) { |
707 | /* Also handle empty range here, to avoid extra traces. */ | 845 | /* Also handle empty range here, to avoid extra traces. */ |
708 | TRef trptr, trslen = emitir(IRTI(IR_SUB), trend, trstart); | 846 | TRef trptr, trslen = emitir(IRTI(IR_SUB), trend, trstart); |
709 | emitir(IRTGI(IR_GE), trslen, tr0); | 847 | emitir(IRTGI(IR_GE), trslen, tr0); |
710 | trptr = emitir(IRT(IR_STRREF, IRT_P32), trstr, trstart); | 848 | trptr = emitir(IRT(IR_STRREF, IRT_PGC), trstr, trstart); |
711 | J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), trptr, trslen); | 849 | J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), trptr, trslen); |
712 | } else { /* Range underflow: return empty string. */ | 850 | } else { /* Range underflow: return empty string. */ |
713 | emitir(IRTGI(IR_LT), trend, trstart); | 851 | emitir(IRTGI(IR_LT), trend, trstart); |
714 | J->base[0] = lj_ir_kstr(J, lj_str_new(J->L, strdata(str), 0)); | 852 | J->base[0] = lj_ir_kstr(J, &J2G(J)->strempty); |
715 | } | 853 | } |
716 | } else { /* Return string.byte result(s). */ | 854 | } else { /* Return string.byte result(s). */ |
717 | ptrdiff_t i, len = end - start; | 855 | ptrdiff_t i, len = end - start; |
@@ -723,7 +861,7 @@ static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd) | |||
723 | rd->nres = len; | 861 | rd->nres = len; |
724 | for (i = 0; i < len; i++) { | 862 | for (i = 0; i < len; i++) { |
725 | TRef tmp = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, (int32_t)i)); | 863 | TRef tmp = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, (int32_t)i)); |
726 | tmp = emitir(IRT(IR_STRREF, IRT_P32), trstr, tmp); | 864 | tmp = emitir(IRT(IR_STRREF, IRT_PGC), trstr, tmp); |
727 | J->base[i] = emitir(IRT(IR_XLOAD, IRT_U8), tmp, IRXLOAD_READONLY); | 865 | J->base[i] = emitir(IRT(IR_XLOAD, IRT_U8), tmp, IRXLOAD_READONLY); |
728 | } | 866 | } |
729 | } else { /* Empty range or range underflow: return no results. */ | 867 | } else { /* Empty range or range underflow: return no results. */ |
@@ -733,48 +871,527 @@ static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd) | |||
733 | } | 871 | } |
734 | } | 872 | } |
735 | 873 | ||
736 | /* -- Table library fast functions ---------------------------------------- */ | 874 | static void LJ_FASTCALL recff_string_char(jit_State *J, RecordFFData *rd) |
737 | |||
738 | static void LJ_FASTCALL recff_table_getn(jit_State *J, RecordFFData *rd) | ||
739 | { | 875 | { |
740 | if (tref_istab(J->base[0])) | 876 | TRef k255 = lj_ir_kint(J, 255); |
741 | J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, J->base[0]); | 877 | BCReg i; |
742 | /* else: Interpreter will throw. */ | 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 | } | ||
743 | UNUSED(rd); | 891 | UNUSED(rd); |
744 | } | 892 | } |
745 | 893 | ||
746 | static void LJ_FASTCALL recff_table_remove(jit_State *J, RecordFFData *rd) | 894 | static void LJ_FASTCALL recff_string_rep(jit_State *J, RecordFFData *rd) |
747 | { | 895 | { |
748 | TRef tab = J->base[0]; | 896 | TRef str = lj_ir_tostr(J, J->base[0]); |
749 | rd->nres = 0; | 897 | TRef rep = lj_opt_narrow_toint(J, J->base[1]); |
750 | if (tref_istab(tab)) { | 898 | TRef hdr, tr, str2 = 0; |
751 | if (tref_isnil(J->base[1])) { /* Simple pop: t[#t] = nil */ | 899 | if (!tref_isnil(J->base[2])) { |
752 | TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, tab); | 900 | TRef sep = lj_ir_tostr(J, J->base[2]); |
753 | GCtab *t = tabV(&rd->argv[0]); | 901 | int32_t vrep = argv2int(J, &rd->argv[1]); |
754 | MSize len = lj_tab_len(t); | 902 | emitir(IRTGI(vrep > 1 ? IR_GT : IR_LE), rep, lj_ir_kint(J, 1)); |
755 | emitir(IRTGI(len ? IR_NE : IR_EQ), trlen, lj_ir_kint(J, 0)); | 903 | if (vrep > 1) { |
756 | if (len) { | 904 | TRef hdr2 = recff_bufhdr(J); |
757 | RecordIndex ix; | 905 | TRef tr2 = emitir(IRTG(IR_BUFPUT, IRT_PGC), hdr2, sep); |
758 | ix.tab = tab; | 906 | tr2 = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr2, str); |
759 | ix.key = trlen; | 907 | str2 = emitir(IRTG(IR_BUFSTR, IRT_STR), tr2, hdr2); |
760 | settabV(J->L, &ix.tabv, t); | 908 | } |
761 | setintV(&ix.keyv, len); | 909 | } |
762 | ix.idxchain = 0; | 910 | tr = hdr = recff_bufhdr(J); |
763 | if (results_wanted(J) != 0) { /* Specialize load only if needed. */ | 911 | if (str2) { |
764 | ix.val = 0; | 912 | tr = emitir(IRTG(IR_BUFPUT, IRT_PGC), tr, str); |
765 | J->base[0] = lj_record_idx(J, &ix); /* Load previous value. */ | 913 | str = str2; |
766 | rd->nres = 1; | 914 | rep = emitir(IRTI(IR_ADD), rep, lj_ir_kint(J, -1)); |
767 | /* Assumes ix.key/ix.tab is not modified for raw lj_record_idx(). */ | 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; | ||
768 | } | 1016 | } |
769 | ix.val = TREF_NIL; | 1017 | #endif |
770 | lj_record_idx(J, &ix); /* Remove value. */ | 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; | ||
771 | } | 1050 | } |
772 | } else { /* Complex case: remove in the middle. */ | 1051 | if (sf == STRFMT_STR) /* Shortcut for plain %s. */ |
773 | recff_nyiu(J); | 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, int 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, int 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_isstr(tr)) { | ||
1227 | trbuf = emitir(IRTG(IR_BUFPUT, IRT_PGC), trbuf, tr); | ||
1228 | } else if (tref_isnumber(tr)) { | ||
1229 | trbuf = emitir(IRTG(IR_BUFPUT, IRT_PGC), trbuf, | ||
1230 | emitir(IRT(IR_TOSTR, IRT_STR), tr, | ||
1231 | tref_isnum(tr) ? IRTOSTR_NUM : IRTOSTR_INT)); | ||
1232 | } else if (tref_isudata(tr)) { | ||
1233 | TRef ud2 = recff_sbufx_check(J, rd, arg); | ||
1234 | TRef trr = recff_sbufx_get_ptr(J, ud2, IRFL_SBUF_R); | ||
1235 | TRef trw = recff_sbufx_get_ptr(J, ud2, IRFL_SBUF_W); | ||
1236 | TRef len = recff_sbufx_len(J, trr, trw); | ||
1237 | emitir(IRTG(IR_NE, IRT_PGC), ud, ud2); | ||
1238 | trbuf = lj_ir_call(J, IRCALL_lj_buf_putmem, trbuf, trr, len); | ||
1239 | } else { | ||
1240 | recff_nyiu(J, rd); | ||
1241 | } | ||
1242 | } | ||
1243 | emitir(IRT(IR_USE, IRT_NIL), trbuf, 0); | ||
1244 | } | ||
1245 | |||
1246 | static void LJ_FASTCALL recff_buffer_method_putf(jit_State *J, RecordFFData *rd) | ||
1247 | { | ||
1248 | TRef ud = recff_sbufx_check(J, rd, 0); | ||
1249 | TRef trbuf = recff_sbufx_write(J, ud); | ||
1250 | recff_format(J, rd, trbuf, 1); | ||
1251 | } | ||
1252 | |||
1253 | static void LJ_FASTCALL recff_buffer_method_get(jit_State *J, RecordFFData *rd) | ||
1254 | { | ||
1255 | TRef ud = recff_sbufx_check(J, rd, 0); | ||
1256 | TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R); | ||
1257 | TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); | ||
1258 | TRef tr; | ||
1259 | ptrdiff_t arg; | ||
1260 | if (!J->base[1]) { J->base[1] = TREF_NIL; J->base[2] = 0; } | ||
1261 | for (arg = 0; (tr = J->base[arg+1]); arg++) { | ||
1262 | TRef len = recff_sbufx_len(J, trr, trw); | ||
1263 | if (tref_isnil(tr)) { | ||
1264 | J->base[arg] = emitir(IRT(IR_XSNEW, IRT_STR), trr, len); | ||
1265 | trr = trw; | ||
1266 | } else { | ||
1267 | TRef trn = recff_sbufx_checkint(J, rd, arg+1); | ||
1268 | TRef tru; | ||
1269 | len = emitir(IRTI(IR_MIN), len, trn); | ||
1270 | tru = emitir(IRT(IR_ADD, IRT_PTR), trr, len); | ||
1271 | J->base[arg] = emitir(IRT(IR_XSNEW, IRT_STR), trr, len); | ||
1272 | trr = tru; /* Doing the ADD before the SNEW generates better code. */ | ||
774 | } | 1273 | } |
1274 | recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trr); | ||
1275 | } | ||
1276 | rd->nres = arg; | ||
1277 | } | ||
1278 | |||
1279 | static void LJ_FASTCALL recff_buffer_method___tostring(jit_State *J, RecordFFData *rd) | ||
1280 | { | ||
1281 | TRef ud = recff_sbufx_check(J, rd, 0); | ||
1282 | TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R); | ||
1283 | TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); | ||
1284 | J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), trr, recff_sbufx_len(J, trr, trw)); | ||
1285 | } | ||
1286 | |||
1287 | static void LJ_FASTCALL recff_buffer_method___len(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] = recff_sbufx_len(J, trr, trw); | ||
1293 | } | ||
1294 | |||
1295 | #if LJ_HASFFI | ||
1296 | static void LJ_FASTCALL recff_buffer_method_putcdata(jit_State *J, RecordFFData *rd) | ||
1297 | { | ||
1298 | TRef ud = recff_sbufx_check(J, rd, 0); | ||
1299 | TRef trbuf = recff_sbufx_write(J, ud); | ||
1300 | TRef tr = lj_crecord_topcvoid(J, J->base[1], &rd->argv[1]); | ||
1301 | TRef len = recff_sbufx_checkint(J, rd, 2); | ||
1302 | trbuf = lj_ir_call(J, IRCALL_lj_buf_putmem, trbuf, tr, len); | ||
1303 | emitir(IRT(IR_USE, IRT_NIL), trbuf, 0); | ||
1304 | } | ||
1305 | |||
1306 | static void LJ_FASTCALL recff_buffer_method_reserve(jit_State *J, RecordFFData *rd) | ||
1307 | { | ||
1308 | TRef ud = recff_sbufx_check(J, rd, 0); | ||
1309 | TRef trbuf = recff_sbufx_write(J, ud); | ||
1310 | TRef trsz = recff_sbufx_checkint(J, rd, 1); | ||
1311 | J->base[1] = lj_ir_call(J, IRCALL_lj_bufx_more, trbuf, trsz); | ||
1312 | J->base[0] = lj_crecord_topuint8(J, recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W)); | ||
1313 | rd->nres = 2; | ||
1314 | } | ||
1315 | |||
1316 | static void LJ_FASTCALL recff_buffer_method_commit(jit_State *J, RecordFFData *rd) | ||
1317 | { | ||
1318 | TRef ud = recff_sbufx_check(J, rd, 0); | ||
1319 | TRef len = recff_sbufx_checkint(J, rd, 1); | ||
1320 | TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); | ||
1321 | TRef tre = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_E); | ||
1322 | TRef left = emitir(IRT(IR_SUB, IRT_INTP), tre, trw); | ||
1323 | if (LJ_64) | ||
1324 | left = emitir(IRTI(IR_CONV), left, (IRT_INT<<5)|IRT_INTP|IRCONV_NONE); | ||
1325 | emitir(IRTGI(IR_ULE), len, left); | ||
1326 | trw = emitir(IRT(IR_ADD, IRT_PTR), trw, len); | ||
1327 | recff_sbufx_set_ptr(J, ud, IRFL_SBUF_W, trw); | ||
1328 | } | ||
1329 | |||
1330 | static void LJ_FASTCALL recff_buffer_method_ref(jit_State *J, RecordFFData *rd) | ||
1331 | { | ||
1332 | TRef ud = recff_sbufx_check(J, rd, 0); | ||
1333 | TRef trr = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_R); | ||
1334 | TRef trw = recff_sbufx_get_ptr(J, ud, IRFL_SBUF_W); | ||
1335 | J->base[0] = lj_crecord_topuint8(J, trr); | ||
1336 | J->base[1] = recff_sbufx_len(J, trr, trw); | ||
1337 | rd->nres = 2; | ||
1338 | } | ||
1339 | #endif | ||
1340 | |||
1341 | static void LJ_FASTCALL recff_buffer_method_encode(jit_State *J, RecordFFData *rd) | ||
1342 | { | ||
1343 | TRef ud = recff_sbufx_check(J, rd, 0); | ||
1344 | TRef trbuf = recff_sbufx_write(J, ud); | ||
1345 | TRef tmp = recff_tmpref(J, J->base[1], IRTMPREF_IN1); | ||
1346 | lj_ir_call(J, IRCALL_lj_serialize_put, trbuf, tmp); | ||
1347 | /* No IR_USE needed, since the call is a store. */ | ||
1348 | } | ||
1349 | |||
1350 | static void LJ_FASTCALL recff_buffer_method_decode(jit_State *J, RecordFFData *rd) | ||
1351 | { | ||
1352 | TRef ud = recff_sbufx_check(J, rd, 0); | ||
1353 | TRef trbuf = recff_sbufx_write(J, ud); | ||
1354 | TRef tmp = recff_tmpref(J, TREF_NIL, IRTMPREF_OUT1); | ||
1355 | TRef trr = lj_ir_call(J, IRCALL_lj_serialize_get, trbuf, tmp); | ||
1356 | IRType t = (IRType)lj_serialize_peektype(bufV(&rd->argv[0])); | ||
1357 | /* No IR_USE needed, since the call is a store. */ | ||
1358 | J->base[0] = lj_record_vload(J, tmp, 0, t); | ||
1359 | /* The sbx->r store must be after the VLOAD type check, in case it fails. */ | ||
1360 | recff_sbufx_set_ptr(J, ud, IRFL_SBUF_R, trr); | ||
1361 | } | ||
1362 | |||
1363 | static void LJ_FASTCALL recff_buffer_encode(jit_State *J, RecordFFData *rd) | ||
1364 | { | ||
1365 | TRef tmp = recff_tmpref(J, J->base[0], IRTMPREF_IN1); | ||
1366 | J->base[0] = lj_ir_call(J, IRCALL_lj_serialize_encode, tmp); | ||
1367 | /* IR_USE needed for IR_CALLA, because the encoder may throw non-OOM. */ | ||
1368 | emitir(IRT(IR_USE, IRT_NIL), J->base[0], 0); | ||
1369 | UNUSED(rd); | ||
1370 | } | ||
1371 | |||
1372 | static void LJ_FASTCALL recff_buffer_decode(jit_State *J, RecordFFData *rd) | ||
1373 | { | ||
1374 | if (tvisstr(&rd->argv[0])) { | ||
1375 | GCstr *str = strV(&rd->argv[0]); | ||
1376 | SBufExt sbx; | ||
1377 | IRType t; | ||
1378 | TRef tmp = recff_tmpref(J, TREF_NIL, IRTMPREF_OUT1); | ||
1379 | TRef tr = lj_ir_call(J, IRCALL_lj_serialize_decode, tmp, J->base[0]); | ||
1380 | /* IR_USE needed for IR_CALLA, because the decoder may throw non-OOM. | ||
1381 | ** That's why IRCALL_lj_serialize_decode needs a fake INT result. | ||
1382 | */ | ||
1383 | emitir(IRT(IR_USE, IRT_NIL), tr, 0); | ||
1384 | memset(&sbx, 0, sizeof(SBufExt)); | ||
1385 | lj_bufx_set_cow(J->L, &sbx, strdata(str), str->len); | ||
1386 | t = (IRType)lj_serialize_peektype(&sbx); | ||
1387 | J->base[0] = lj_record_vload(J, tmp, 0, t); | ||
775 | } /* else: Interpreter will throw. */ | 1388 | } /* else: Interpreter will throw. */ |
776 | } | 1389 | } |
777 | 1390 | ||
1391 | #endif | ||
1392 | |||
1393 | /* -- Table library fast functions ---------------------------------------- */ | ||
1394 | |||
778 | static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd) | 1395 | static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd) |
779 | { | 1396 | { |
780 | RecordIndex ix; | 1397 | RecordIndex ix; |
@@ -783,7 +1400,7 @@ static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd) | |||
783 | rd->nres = 0; | 1400 | rd->nres = 0; |
784 | if (tref_istab(ix.tab) && ix.val) { | 1401 | if (tref_istab(ix.tab) && ix.val) { |
785 | if (!J->base[2]) { /* Simple push: t[#t+1] = v */ | 1402 | if (!J->base[2]) { /* Simple push: t[#t+1] = v */ |
786 | TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, ix.tab); | 1403 | TRef trlen = emitir(IRTI(IR_ALEN), ix.tab, TREF_NIL); |
787 | GCtab *t = tabV(&rd->argv[0]); | 1404 | GCtab *t = tabV(&rd->argv[0]); |
788 | ix.key = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1)); | 1405 | ix.key = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1)); |
789 | settabV(J->L, &ix.tabv, t); | 1406 | settabV(J->L, &ix.tabv, t); |
@@ -791,11 +1408,49 @@ static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd) | |||
791 | ix.idxchain = 0; | 1408 | ix.idxchain = 0; |
792 | lj_record_idx(J, &ix); /* Set new value. */ | 1409 | lj_record_idx(J, &ix); /* Set new value. */ |
793 | } else { /* Complex case: insert in the middle. */ | 1410 | } else { /* Complex case: insert in the middle. */ |
794 | recff_nyiu(J); | 1411 | recff_nyiu(J, rd); |
1412 | return; | ||
795 | } | 1413 | } |
796 | } /* else: Interpreter will throw. */ | 1414 | } /* else: Interpreter will throw. */ |
797 | } | 1415 | } |
798 | 1416 | ||
1417 | static void LJ_FASTCALL recff_table_concat(jit_State *J, RecordFFData *rd) | ||
1418 | { | ||
1419 | TRef tab = J->base[0]; | ||
1420 | if (tref_istab(tab)) { | ||
1421 | TRef sep = !tref_isnil(J->base[1]) ? | ||
1422 | lj_ir_tostr(J, J->base[1]) : lj_ir_knull(J, IRT_STR); | ||
1423 | TRef tri = (J->base[1] && !tref_isnil(J->base[2])) ? | ||
1424 | lj_opt_narrow_toint(J, J->base[2]) : lj_ir_kint(J, 1); | ||
1425 | TRef tre = (J->base[1] && J->base[2] && !tref_isnil(J->base[3])) ? | ||
1426 | lj_opt_narrow_toint(J, J->base[3]) : | ||
1427 | emitir(IRTI(IR_ALEN), tab, TREF_NIL); | ||
1428 | TRef hdr = recff_bufhdr(J); | ||
1429 | TRef tr = lj_ir_call(J, IRCALL_lj_buf_puttab, hdr, tab, sep, tri, tre); | ||
1430 | emitir(IRTG(IR_NE, IRT_PTR), tr, lj_ir_kptr(J, NULL)); | ||
1431 | J->base[0] = emitir(IRTG(IR_BUFSTR, IRT_STR), tr, hdr); | ||
1432 | } /* else: Interpreter will throw. */ | ||
1433 | UNUSED(rd); | ||
1434 | } | ||
1435 | |||
1436 | static void LJ_FASTCALL recff_table_new(jit_State *J, RecordFFData *rd) | ||
1437 | { | ||
1438 | TRef tra = lj_opt_narrow_toint(J, J->base[0]); | ||
1439 | TRef trh = lj_opt_narrow_toint(J, J->base[1]); | ||
1440 | J->base[0] = lj_ir_call(J, IRCALL_lj_tab_new_ah, tra, trh); | ||
1441 | UNUSED(rd); | ||
1442 | } | ||
1443 | |||
1444 | static void LJ_FASTCALL recff_table_clear(jit_State *J, RecordFFData *rd) | ||
1445 | { | ||
1446 | TRef tr = J->base[0]; | ||
1447 | if (tref_istab(tr)) { | ||
1448 | rd->nres = 0; | ||
1449 | lj_ir_call(J, IRCALL_lj_tab_clear, tr); | ||
1450 | J->needsnap = 1; | ||
1451 | } /* else: Interpreter will throw. */ | ||
1452 | } | ||
1453 | |||
799 | /* -- I/O library fast functions ------------------------------------------ */ | 1454 | /* -- I/O library fast functions ------------------------------------------ */ |
800 | 1455 | ||
801 | /* Get FILE* for I/O function. Any I/O error aborts recording, so there's | 1456 | /* Get FILE* for I/O function. Any I/O error aborts recording, so there's |
@@ -805,8 +1460,7 @@ static TRef recff_io_fp(jit_State *J, TRef *udp, int32_t id) | |||
805 | { | 1460 | { |
806 | TRef tr, ud, fp; | 1461 | TRef tr, ud, fp; |
807 | if (id) { /* io.func() */ | 1462 | if (id) { /* io.func() */ |
808 | tr = lj_ir_kptr(J, &J2G(J)->gcroot[id]); | 1463 | ud = lj_ir_ggfload(J, IRT_UDATA, GG_OFS(g.gcroot[id])); |
809 | ud = emitir(IRT(IR_XLOAD, IRT_UDATA), tr, 0); | ||
810 | } else { /* fp:method() */ | 1464 | } else { /* fp:method() */ |
811 | ud = J->base[0]; | 1465 | ud = J->base[0]; |
812 | if (!tref_isudata(ud)) | 1466 | if (!tref_isudata(ud)) |
@@ -828,10 +1482,13 @@ static void LJ_FASTCALL recff_io_write(jit_State *J, RecordFFData *rd) | |||
828 | ptrdiff_t i = rd->data == 0 ? 1 : 0; | 1482 | ptrdiff_t i = rd->data == 0 ? 1 : 0; |
829 | for (; J->base[i]; i++) { | 1483 | for (; J->base[i]; i++) { |
830 | TRef str = lj_ir_tostr(J, J->base[i]); | 1484 | TRef str = lj_ir_tostr(J, J->base[i]); |
831 | TRef buf = emitir(IRT(IR_STRREF, IRT_P32), str, zero); | 1485 | TRef buf = emitir(IRT(IR_STRREF, IRT_PGC), str, zero); |
832 | TRef len = emitir(IRTI(IR_FLOAD), str, IRFL_STR_LEN); | 1486 | TRef len = emitir(IRTI(IR_FLOAD), str, IRFL_STR_LEN); |
833 | if (tref_isk(len) && IR(tref_ref(len))->i == 1) { | 1487 | if (tref_isk(len) && IR(tref_ref(len))->i == 1) { |
834 | TRef tr = emitir(IRT(IR_XLOAD, IRT_U8), buf, IRXLOAD_READONLY); | 1488 | IRIns *irs = IR(tref_ref(str)); |
1489 | TRef tr = (irs->o == IR_TOSTR && irs->op2 == IRTOSTR_CHAR) ? | ||
1490 | irs->op1 : | ||
1491 | emitir(IRT(IR_XLOAD, IRT_U8), buf, IRXLOAD_READONLY); | ||
835 | tr = lj_ir_call(J, IRCALL_fputc, tr, fp); | 1492 | tr = lj_ir_call(J, IRCALL_fputc, tr, fp); |
836 | if (results_wanted(J) != 0) /* Check result only if not ignored. */ | 1493 | if (results_wanted(J) != 0) /* Check result only if not ignored. */ |
837 | emitir(IRTGI(IR_NE), tr, lj_ir_kint(J, -1)); | 1494 | emitir(IRTGI(IR_NE), tr, lj_ir_kint(J, -1)); |
@@ -853,6 +1510,28 @@ static void LJ_FASTCALL recff_io_flush(jit_State *J, RecordFFData *rd) | |||
853 | J->base[0] = TREF_TRUE; | 1510 | J->base[0] = TREF_TRUE; |
854 | } | 1511 | } |
855 | 1512 | ||
1513 | /* -- Debug library fast functions ---------------------------------------- */ | ||
1514 | |||
1515 | static void LJ_FASTCALL recff_debug_getmetatable(jit_State *J, RecordFFData *rd) | ||
1516 | { | ||
1517 | GCtab *mt; | ||
1518 | TRef mtref; | ||
1519 | TRef tr = J->base[0]; | ||
1520 | if (tref_istab(tr)) { | ||
1521 | mt = tabref(tabV(&rd->argv[0])->metatable); | ||
1522 | mtref = emitir(IRT(IR_FLOAD, IRT_TAB), tr, IRFL_TAB_META); | ||
1523 | } else if (tref_isudata(tr)) { | ||
1524 | mt = tabref(udataV(&rd->argv[0])->metatable); | ||
1525 | mtref = emitir(IRT(IR_FLOAD, IRT_TAB), tr, IRFL_UDATA_META); | ||
1526 | } else { | ||
1527 | mt = tabref(basemt_obj(J2G(J), &rd->argv[0])); | ||
1528 | J->base[0] = mt ? lj_ir_ktab(J, mt) : TREF_NIL; | ||
1529 | return; | ||
1530 | } | ||
1531 | emitir(IRTG(mt ? IR_NE : IR_EQ, IRT_TAB), mtref, lj_ir_knull(J, IRT_TAB)); | ||
1532 | J->base[0] = mt ? mtref : TREF_NIL; | ||
1533 | } | ||
1534 | |||
856 | /* -- Record calls to fast functions -------------------------------------- */ | 1535 | /* -- Record calls to fast functions -------------------------------------- */ |
857 | 1536 | ||
858 | #include "lj_recdef.h" | 1537 | #include "lj_recdef.h" |