diff options
author | Mike Pall <mike> | 2010-12-05 17:12:34 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2010-12-05 17:12:34 +0100 |
commit | e7f8cc964e036147f0303c2653a77dfe24473dcc (patch) | |
tree | 04710210a3280557a0528208440ba4b45341bebb /src/lj_ffrecord.c | |
parent | 559545eb6dc098594f957b09217ac3507b56eb3c (diff) | |
download | luajit-e7f8cc964e036147f0303c2653a77dfe24473dcc.tar.gz luajit-e7f8cc964e036147f0303c2653a77dfe24473dcc.tar.bz2 luajit-e7f8cc964e036147f0303c2653a77dfe24473dcc.zip |
Split off fast function recording to lj_ffrecord.c.
Diffstat (limited to 'src/lj_ffrecord.c')
-rw-r--r-- | src/lj_ffrecord.c | 803 |
1 files changed, 803 insertions, 0 deletions
diff --git a/src/lj_ffrecord.c b/src/lj_ffrecord.c new file mode 100644 index 00000000..0c8a41cd --- /dev/null +++ b/src/lj_ffrecord.c | |||
@@ -0,0 +1,803 @@ | |||
1 | /* | ||
2 | ** Fast function call recorder. | ||
3 | ** Copyright (C) 2005-2010 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | #define lj_ffrecord_c | ||
7 | #define LUA_CORE | ||
8 | |||
9 | #include "lj_obj.h" | ||
10 | |||
11 | #if LJ_HASJIT | ||
12 | |||
13 | #include "lj_err.h" | ||
14 | #include "lj_str.h" | ||
15 | #include "lj_tab.h" | ||
16 | #include "lj_frame.h" | ||
17 | #include "lj_bc.h" | ||
18 | #include "lj_ff.h" | ||
19 | #include "lj_ir.h" | ||
20 | #include "lj_jit.h" | ||
21 | #include "lj_iropt.h" | ||
22 | #include "lj_trace.h" | ||
23 | #include "lj_record.h" | ||
24 | #include "lj_dispatch.h" | ||
25 | #include "lj_vm.h" | ||
26 | |||
27 | /* Some local macros to save typing. Undef'd at the end. */ | ||
28 | #define IR(ref) (&J->cur.ir[(ref)]) | ||
29 | |||
30 | /* Pass IR on to next optimization in chain (FOLD). */ | ||
31 | #define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) | ||
32 | |||
33 | /* -- Fast function recording handlers ------------------------------------ */ | ||
34 | |||
35 | /* Conventions for fast function call handlers: | ||
36 | ** | ||
37 | ** The argument slots start at J->base[0]. All of them are guaranteed to be | ||
38 | ** valid and type-specialized references. J->base[J->maxslot] is set to 0 | ||
39 | ** as a sentinel. The runtime argument values start at rd->argv[0]. | ||
40 | ** | ||
41 | ** In general fast functions should check for presence of all of their | ||
42 | ** arguments and for the correct argument types. Some simplifications | ||
43 | ** are allowed if the interpreter throws instead. But even if recording | ||
44 | ** is aborted, the generated IR must be consistent (no zero-refs). | ||
45 | ** | ||
46 | ** The number of results in rd->nres is set to 1. Handlers that return | ||
47 | ** a different number of results need to override it. A negative value | ||
48 | ** prevents return processing (e.g. for pending calls). | ||
49 | ** | ||
50 | ** Results need to be stored starting at J->base[0]. Return processing | ||
51 | ** moves them to the right slots later. | ||
52 | ** | ||
53 | ** The per-ffid auxiliary data is the value of the 2nd part of the | ||
54 | ** LJLIB_REC() annotation. This allows handling similar functionality | ||
55 | ** in a common handler. | ||
56 | */ | ||
57 | |||
58 | /* Data used by handlers to record a fast function. */ | ||
59 | typedef struct RecordFFData { | ||
60 | TValue *argv; /* Runtime argument values. */ | ||
61 | ptrdiff_t nres; /* Number of returned results (defaults to 1). */ | ||
62 | uint32_t data; /* Per-ffid auxiliary data (opcode, literal etc.). */ | ||
63 | } RecordFFData; | ||
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 (!tvisnum(o) && !(tvisstr(o) && lj_str_tonum(strV(o), o))) | ||
72 | lj_trace_err(J, LJ_TRERR_BADTYPE); | ||
73 | return lj_num2bit(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 (!tvisnum(o)) | ||
84 | lj_trace_err(J, LJ_TRERR_BADTYPE); | ||
85 | s = lj_str_fromnum(J->L, &o->n); | ||
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 | /* Throw error for unsupported variant of fast function. */ | ||
102 | LJ_NORET static void recff_nyiu(jit_State *J) | ||
103 | { | ||
104 | setfuncV(J->L, &J->errinfo, J->fn); | ||
105 | lj_trace_err_info(J, LJ_TRERR_NYIFFU); | ||
106 | } | ||
107 | |||
108 | /* Fallback handler for all fast functions that are not recorded (yet). */ | ||
109 | static void LJ_FASTCALL recff_nyi(jit_State *J, RecordFFData *rd) | ||
110 | { | ||
111 | setfuncV(J->L, &J->errinfo, J->fn); | ||
112 | lj_trace_err_info(J, LJ_TRERR_NYIFF); | ||
113 | UNUSED(rd); | ||
114 | } | ||
115 | |||
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) | ||
118 | { | ||
119 | setfuncV(J->L, &J->errinfo, J->fn); | ||
120 | lj_trace_err_info(J, LJ_TRERR_NYICF); | ||
121 | UNUSED(rd); | ||
122 | } | ||
123 | |||
124 | /* -- Base library fast functions ----------------------------------------- */ | ||
125 | |||
126 | static void LJ_FASTCALL recff_assert(jit_State *J, RecordFFData *rd) | ||
127 | { | ||
128 | /* Arguments already specialized. The interpreter throws for nil/false. */ | ||
129 | rd->nres = J->maxslot; /* Pass through all arguments. */ | ||
130 | } | ||
131 | |||
132 | static void LJ_FASTCALL recff_type(jit_State *J, RecordFFData *rd) | ||
133 | { | ||
134 | /* Arguments already specialized. Result is a constant string. Neat, huh? */ | ||
135 | IRType t = tref_isinteger(J->base[0]) ? IRT_NUM : tref_type(J->base[0]); | ||
136 | J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[t])); | ||
137 | UNUSED(rd); | ||
138 | } | ||
139 | |||
140 | static void LJ_FASTCALL recff_getmetatable(jit_State *J, RecordFFData *rd) | ||
141 | { | ||
142 | TRef tr = J->base[0]; | ||
143 | if (tr) { | ||
144 | RecordIndex ix; | ||
145 | ix.tab = tr; | ||
146 | copyTV(J->L, &ix.tabv, &rd->argv[0]); | ||
147 | if (lj_record_mm_lookup(J, &ix, MM_metatable)) | ||
148 | J->base[0] = ix.mobj; | ||
149 | else | ||
150 | J->base[0] = ix.mt; | ||
151 | } /* else: Interpreter will throw. */ | ||
152 | } | ||
153 | |||
154 | static void LJ_FASTCALL recff_setmetatable(jit_State *J, RecordFFData *rd) | ||
155 | { | ||
156 | TRef tr = J->base[0]; | ||
157 | TRef mt = J->base[1]; | ||
158 | if (tref_istab(tr) && (tref_istab(mt) || (mt && tref_isnil(mt)))) { | ||
159 | TRef fref, mtref; | ||
160 | RecordIndex ix; | ||
161 | ix.tab = tr; | ||
162 | copyTV(J->L, &ix.tabv, &rd->argv[0]); | ||
163 | lj_record_mm_lookup(J, &ix, MM_metatable); /* Guard for no __metatable. */ | ||
164 | fref = emitir(IRT(IR_FREF, IRT_PTR), tr, IRFL_TAB_META); | ||
165 | mtref = tref_isnil(mt) ? lj_ir_knull(J, IRT_TAB) : mt; | ||
166 | emitir(IRT(IR_FSTORE, IRT_TAB), fref, mtref); | ||
167 | if (!tref_isnil(mt)) | ||
168 | emitir(IRT(IR_TBAR, IRT_TAB), tr, 0); | ||
169 | J->base[0] = tr; | ||
170 | J->needsnap = 1; | ||
171 | } /* else: Interpreter will throw. */ | ||
172 | } | ||
173 | |||
174 | static void LJ_FASTCALL recff_rawget(jit_State *J, RecordFFData *rd) | ||
175 | { | ||
176 | RecordIndex ix; | ||
177 | ix.tab = J->base[0]; ix.key = J->base[1]; | ||
178 | if (tref_istab(ix.tab) && ix.key) { | ||
179 | ix.val = 0; ix.idxchain = 0; | ||
180 | settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); | ||
181 | copyTV(J->L, &ix.keyv, &rd->argv[1]); | ||
182 | J->base[0] = lj_record_idx(J, &ix); | ||
183 | } /* else: Interpreter will throw. */ | ||
184 | } | ||
185 | |||
186 | static void LJ_FASTCALL recff_rawset(jit_State *J, RecordFFData *rd) | ||
187 | { | ||
188 | RecordIndex ix; | ||
189 | ix.tab = J->base[0]; ix.key = J->base[1]; ix.val = J->base[2]; | ||
190 | if (tref_istab(ix.tab) && ix.key && ix.val) { | ||
191 | ix.idxchain = 0; | ||
192 | settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); | ||
193 | copyTV(J->L, &ix.keyv, &rd->argv[1]); | ||
194 | copyTV(J->L, &ix.valv, &rd->argv[2]); | ||
195 | lj_record_idx(J, &ix); | ||
196 | /* Pass through table at J->base[0] as result. */ | ||
197 | } /* else: Interpreter will throw. */ | ||
198 | } | ||
199 | |||
200 | static void LJ_FASTCALL recff_rawequal(jit_State *J, RecordFFData *rd) | ||
201 | { | ||
202 | TRef tra = J->base[0]; | ||
203 | TRef trb = J->base[1]; | ||
204 | if (tra && trb) { | ||
205 | int diff = lj_record_objcmp(J, tra, trb, &rd->argv[0], &rd->argv[1]); | ||
206 | J->base[0] = diff ? TREF_FALSE : TREF_TRUE; | ||
207 | } /* else: Interpreter will throw. */ | ||
208 | } | ||
209 | |||
210 | /* Determine mode of select() call. */ | ||
211 | int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv) | ||
212 | { | ||
213 | if (tref_isstr(tr) && *strVdata(tv) == '#') { /* select('#', ...) */ | ||
214 | if (strV(tv)->len == 1) { | ||
215 | emitir(IRT(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, strV(tv))); | ||
216 | } else { | ||
217 | TRef trptr = emitir(IRT(IR_STRREF, IRT_PTR), tr, 0); | ||
218 | TRef trchar = emitir(IRT(IR_XLOAD, IRT_U8), trptr, IRXLOAD_READONLY); | ||
219 | emitir(IRT(IR_EQ, IRT_INT), trchar, lj_ir_kint(J, '#')); | ||
220 | } | ||
221 | return 0; | ||
222 | } else { /* select(n, ...) */ | ||
223 | int32_t start = argv2int(J, tv); | ||
224 | if (start == 0) lj_trace_err(J, LJ_TRERR_BADTYPE); /* A bit misleading. */ | ||
225 | return start; | ||
226 | } | ||
227 | } | ||
228 | |||
229 | static void LJ_FASTCALL recff_select(jit_State *J, RecordFFData *rd) | ||
230 | { | ||
231 | TRef tr = J->base[0]; | ||
232 | if (tr) { | ||
233 | ptrdiff_t start = lj_ffrecord_select_mode(J, tr, &rd->argv[0]); | ||
234 | if (start == 0) { /* select('#', ...) */ | ||
235 | J->base[0] = lj_ir_kint(J, J->maxslot - 1); | ||
236 | } else if (tref_isk(tr)) { /* select(k, ...) */ | ||
237 | ptrdiff_t n = (ptrdiff_t)J->maxslot; | ||
238 | if (start < 0) start += n; | ||
239 | else if (start > n) start = n; | ||
240 | rd->nres = n - start; | ||
241 | if (start >= 1) { | ||
242 | ptrdiff_t i; | ||
243 | for (i = 0; i < n - start; i++) | ||
244 | J->base[i] = J->base[start+i]; | ||
245 | } /* else: Interpreter will throw. */ | ||
246 | } else { | ||
247 | recff_nyiu(J); | ||
248 | } | ||
249 | } /* else: Interpreter will throw. */ | ||
250 | } | ||
251 | |||
252 | static void LJ_FASTCALL recff_tonumber(jit_State *J, RecordFFData *rd) | ||
253 | { | ||
254 | TRef tr = J->base[0]; | ||
255 | if (tref_isnumber_str(tr)) { | ||
256 | TRef base = J->base[1]; | ||
257 | if (base) { | ||
258 | base = lj_ir_toint(J, base); | ||
259 | if (!tref_isk(base) || IR(tref_ref(base))->i != 10) | ||
260 | recff_nyiu(J); | ||
261 | } | ||
262 | if (tref_isstr(tr)) { | ||
263 | TValue tmp; | ||
264 | if (!lj_str_tonum(strV(&rd->argv[0]), &tmp)) | ||
265 | recff_nyiu(J); /* Would need an inverted STRTO for this case. */ | ||
266 | tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); | ||
267 | } | ||
268 | } else { | ||
269 | tr = TREF_NIL; | ||
270 | } | ||
271 | J->base[0] = tr; | ||
272 | UNUSED(rd); | ||
273 | } | ||
274 | |||
275 | static TValue *recff_metacall_cp(lua_State *L, lua_CFunction dummy, void *ud) | ||
276 | { | ||
277 | jit_State *J = (jit_State *)ud; | ||
278 | lj_record_tailcall(J, 0, 1); | ||
279 | UNUSED(L); UNUSED(dummy); | ||
280 | return NULL; | ||
281 | } | ||
282 | |||
283 | static int recff_metacall(jit_State *J, RecordFFData *rd, MMS mm) | ||
284 | { | ||
285 | RecordIndex ix; | ||
286 | ix.tab = J->base[0]; | ||
287 | copyTV(J->L, &ix.tabv, &rd->argv[0]); | ||
288 | if (lj_record_mm_lookup(J, &ix, mm)) { /* Has metamethod? */ | ||
289 | int errcode; | ||
290 | /* Temporarily insert metamethod below object. */ | ||
291 | J->base[1] = J->base[0]; | ||
292 | J->base[0] = ix.mobj; | ||
293 | copyTV(J->L, &rd->argv[1], &rd->argv[0]); | ||
294 | copyTV(J->L, &rd->argv[0], &ix.mobjv); | ||
295 | /* Need to protect lj_record_tailcall because it may throw. */ | ||
296 | errcode = lj_vm_cpcall(J->L, NULL, J, recff_metacall_cp); | ||
297 | /* Always undo Lua stack changes to avoid confusing the interpreter. */ | ||
298 | copyTV(J->L, &rd->argv[0], &rd->argv[1]); | ||
299 | if (errcode) | ||
300 | lj_err_throw(J->L, errcode); /* Propagate errors. */ | ||
301 | rd->nres = -1; /* Pending call. */ | ||
302 | return 1; /* Tailcalled to metamethod. */ | ||
303 | } | ||
304 | return 0; | ||
305 | } | ||
306 | |||
307 | static void LJ_FASTCALL recff_tostring(jit_State *J, RecordFFData *rd) | ||
308 | { | ||
309 | TRef tr = J->base[0]; | ||
310 | if (tref_isstr(tr)) { | ||
311 | /* Ignore __tostring in the string base metatable. */ | ||
312 | /* Pass on result in J->base[0]. */ | ||
313 | } else if (!recff_metacall(J, rd, MM_tostring)) { | ||
314 | if (tref_isnumber(tr)) { | ||
315 | J->base[0] = emitir(IRT(IR_TOSTR, IRT_STR), tr, 0); | ||
316 | } else if (tref_ispri(tr)) { | ||
317 | J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[tref_type(tr)])); | ||
318 | } else { | ||
319 | recff_nyiu(J); | ||
320 | } | ||
321 | } | ||
322 | } | ||
323 | |||
324 | static void LJ_FASTCALL recff_ipairs_aux(jit_State *J, RecordFFData *rd) | ||
325 | { | ||
326 | RecordIndex ix; | ||
327 | ix.tab = J->base[0]; | ||
328 | if (tref_istab(ix.tab)) { | ||
329 | if (!tvisnum(&rd->argv[1])) /* No support for string coercion. */ | ||
330 | lj_trace_err(J, LJ_TRERR_BADTYPE); | ||
331 | setnumV(&ix.keyv, numV(&rd->argv[1])+(lua_Number)1); | ||
332 | settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); | ||
333 | ix.val = 0; ix.idxchain = 0; | ||
334 | ix.key = lj_ir_toint(J, J->base[1]); | ||
335 | J->base[0] = ix.key = emitir(IRTI(IR_ADD), ix.key, lj_ir_kint(J, 1)); | ||
336 | J->base[1] = lj_record_idx(J, &ix); | ||
337 | rd->nres = tref_isnil(J->base[1]) ? 0 : 2; | ||
338 | } /* else: Interpreter will throw. */ | ||
339 | } | ||
340 | |||
341 | static void LJ_FASTCALL recff_ipairs(jit_State *J, RecordFFData *rd) | ||
342 | { | ||
343 | #ifdef LUAJIT_ENABLE_LUA52COMPAT | ||
344 | if (!recff_metacall(J, rd, MM_ipairs)) | ||
345 | #endif | ||
346 | { | ||
347 | TRef tab = J->base[0]; | ||
348 | if (tref_istab(tab)) { | ||
349 | J->base[0] = lj_ir_kfunc(J, funcV(&J->fn->c.upvalue[0])); | ||
350 | J->base[1] = tab; | ||
351 | J->base[2] = lj_ir_kint(J, 0); | ||
352 | rd->nres = 3; | ||
353 | } /* else: Interpreter will throw. */ | ||
354 | } | ||
355 | } | ||
356 | |||
357 | static void LJ_FASTCALL recff_pcall(jit_State *J, RecordFFData *rd) | ||
358 | { | ||
359 | if (J->maxslot >= 1) { | ||
360 | lj_record_call(J, 0, J->maxslot - 1); | ||
361 | rd->nres = -1; /* Pending call. */ | ||
362 | } /* else: Interpreter will throw. */ | ||
363 | } | ||
364 | |||
365 | static TValue *recff_xpcall_cp(lua_State *L, lua_CFunction dummy, void *ud) | ||
366 | { | ||
367 | jit_State *J = (jit_State *)ud; | ||
368 | lj_record_call(J, 1, J->maxslot - 2); | ||
369 | UNUSED(L); UNUSED(dummy); | ||
370 | return NULL; | ||
371 | } | ||
372 | |||
373 | static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd) | ||
374 | { | ||
375 | if (J->maxslot >= 2) { | ||
376 | TValue argv0, argv1; | ||
377 | TRef tmp; | ||
378 | int errcode; | ||
379 | /* Swap function and traceback. */ | ||
380 | tmp = J->base[0]; J->base[0] = J->base[1]; J->base[1] = tmp; | ||
381 | copyTV(J->L, &argv0, &rd->argv[0]); | ||
382 | copyTV(J->L, &argv1, &rd->argv[1]); | ||
383 | copyTV(J->L, &rd->argv[0], &argv1); | ||
384 | copyTV(J->L, &rd->argv[1], &argv0); | ||
385 | /* Need to protect lj_record_call because it may throw. */ | ||
386 | errcode = lj_vm_cpcall(J->L, NULL, J, recff_xpcall_cp); | ||
387 | /* Always undo Lua stack swap to avoid confusing the interpreter. */ | ||
388 | copyTV(J->L, &rd->argv[0], &argv0); | ||
389 | copyTV(J->L, &rd->argv[1], &argv1); | ||
390 | if (errcode) | ||
391 | lj_err_throw(J->L, errcode); /* Propagate errors. */ | ||
392 | rd->nres = -1; /* Pending call. */ | ||
393 | } /* else: Interpreter will throw. */ | ||
394 | } | ||
395 | |||
396 | /* -- Math library fast functions ----------------------------------------- */ | ||
397 | |||
398 | static void LJ_FASTCALL recff_math_abs(jit_State *J, RecordFFData *rd) | ||
399 | { | ||
400 | TRef tr = lj_ir_tonum(J, J->base[0]); | ||
401 | J->base[0] = emitir(IRTN(IR_ABS), tr, lj_ir_knum_abs(J)); | ||
402 | UNUSED(rd); | ||
403 | } | ||
404 | |||
405 | /* Record rounding functions math.floor and math.ceil. */ | ||
406 | static void LJ_FASTCALL recff_math_round(jit_State *J, RecordFFData *rd) | ||
407 | { | ||
408 | if (!tref_isinteger(J->base[0])) /* Pass through integers unmodified. */ | ||
409 | J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data); | ||
410 | /* Note: result is integral (or NaN/Inf), but may not fit into an integer. */ | ||
411 | } | ||
412 | |||
413 | /* Record unary math.* functions, mapped to IR_FPMATH opcode. */ | ||
414 | static void LJ_FASTCALL recff_math_unary(jit_State *J, RecordFFData *rd) | ||
415 | { | ||
416 | J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data); | ||
417 | } | ||
418 | |||
419 | /* Record binary math.* functions math.atan2 and math.ldexp. */ | ||
420 | static void LJ_FASTCALL recff_math_binary(jit_State *J, RecordFFData *rd) | ||
421 | { | ||
422 | TRef tr = lj_ir_tonum(J, J->base[0]); | ||
423 | J->base[0] = emitir(IRTN(rd->data), tr, lj_ir_tonum(J, J->base[1])); | ||
424 | } | ||
425 | |||
426 | /* Record math.asin, math.acos, math.atan. */ | ||
427 | static void LJ_FASTCALL recff_math_atrig(jit_State *J, RecordFFData *rd) | ||
428 | { | ||
429 | TRef y = lj_ir_tonum(J, J->base[0]); | ||
430 | TRef x = lj_ir_knum_one(J); | ||
431 | uint32_t ffid = rd->data; | ||
432 | if (ffid != FF_math_atan) { | ||
433 | TRef tmp = emitir(IRTN(IR_MUL), y, y); | ||
434 | tmp = emitir(IRTN(IR_SUB), x, tmp); | ||
435 | tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_SQRT); | ||
436 | if (ffid == FF_math_asin) { x = tmp; } else { x = y; y = tmp; } | ||
437 | } | ||
438 | J->base[0] = emitir(IRTN(IR_ATAN2), y, x); | ||
439 | } | ||
440 | |||
441 | static void LJ_FASTCALL recff_math_htrig(jit_State *J, RecordFFData *rd) | ||
442 | { | ||
443 | TRef tr = lj_ir_tonum(J, J->base[0]); | ||
444 | J->base[0] = lj_ir_call(J, rd->data, tr); | ||
445 | } | ||
446 | |||
447 | static void LJ_FASTCALL recff_math_modf(jit_State *J, RecordFFData *rd) | ||
448 | { | ||
449 | TRef tr = J->base[0]; | ||
450 | if (tref_isinteger(tr)) { | ||
451 | J->base[0] = tr; | ||
452 | J->base[1] = lj_ir_kint(J, 0); | ||
453 | } else { | ||
454 | TRef trt; | ||
455 | tr = lj_ir_tonum(J, tr); | ||
456 | trt = emitir(IRTN(IR_FPMATH), tr, IRFPM_TRUNC); | ||
457 | J->base[0] = trt; | ||
458 | J->base[1] = emitir(IRTN(IR_SUB), tr, trt); | ||
459 | } | ||
460 | rd->nres = 2; | ||
461 | } | ||
462 | |||
463 | static void LJ_FASTCALL recff_math_degrad(jit_State *J, RecordFFData *rd) | ||
464 | { | ||
465 | TRef tr = lj_ir_tonum(J, J->base[0]); | ||
466 | TRef trm = lj_ir_knum(J, numV(&J->fn->c.upvalue[0])); | ||
467 | J->base[0] = emitir(IRTN(IR_MUL), tr, trm); | ||
468 | UNUSED(rd); | ||
469 | } | ||
470 | |||
471 | static void LJ_FASTCALL recff_math_pow(jit_State *J, RecordFFData *rd) | ||
472 | { | ||
473 | TRef tr = lj_ir_tonum(J, J->base[0]); | ||
474 | if (!tref_isnumber_str(J->base[1])) | ||
475 | lj_trace_err(J, LJ_TRERR_BADTYPE); | ||
476 | J->base[0] = lj_opt_narrow_pow(J, tr, J->base[1], &rd->argv[1]); | ||
477 | UNUSED(rd); | ||
478 | } | ||
479 | |||
480 | static void LJ_FASTCALL recff_math_minmax(jit_State *J, RecordFFData *rd) | ||
481 | { | ||
482 | TRef tr = lj_ir_tonum(J, J->base[0]); | ||
483 | uint32_t op = rd->data; | ||
484 | BCReg i; | ||
485 | for (i = 1; J->base[i] != 0; i++) | ||
486 | tr = emitir(IRTN(op), tr, lj_ir_tonum(J, J->base[i])); | ||
487 | J->base[0] = tr; | ||
488 | } | ||
489 | |||
490 | static void LJ_FASTCALL recff_math_random(jit_State *J, RecordFFData *rd) | ||
491 | { | ||
492 | GCudata *ud = udataV(&J->fn->c.upvalue[0]); | ||
493 | TRef tr, one; | ||
494 | lj_ir_kgc(J, obj2gco(ud), IRT_UDATA); /* Prevent collection. */ | ||
495 | tr = lj_ir_call(J, IRCALL_lj_math_random_step, lj_ir_kptr(J, uddata(ud))); | ||
496 | one = lj_ir_knum_one(J); | ||
497 | tr = emitir(IRTN(IR_SUB), tr, one); | ||
498 | if (J->base[0]) { | ||
499 | TRef tr1 = lj_ir_tonum(J, J->base[0]); | ||
500 | if (J->base[1]) { /* d = floor(d*(r2-r1+1.0)) + r1 */ | ||
501 | TRef tr2 = lj_ir_tonum(J, J->base[1]); | ||
502 | tr2 = emitir(IRTN(IR_SUB), tr2, tr1); | ||
503 | tr2 = emitir(IRTN(IR_ADD), tr2, one); | ||
504 | tr = emitir(IRTN(IR_MUL), tr, tr2); | ||
505 | tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR); | ||
506 | tr = emitir(IRTN(IR_ADD), tr, tr1); | ||
507 | } else { /* d = floor(d*r1) + 1.0 */ | ||
508 | tr = emitir(IRTN(IR_MUL), tr, tr1); | ||
509 | tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR); | ||
510 | tr = emitir(IRTN(IR_ADD), tr, one); | ||
511 | } | ||
512 | } | ||
513 | J->base[0] = tr; | ||
514 | UNUSED(rd); | ||
515 | } | ||
516 | |||
517 | /* -- Bit library fast functions ------------------------------------------ */ | ||
518 | |||
519 | /* Record unary bit.tobit, bit.bnot, bit.bswap. */ | ||
520 | static void LJ_FASTCALL recff_bit_unary(jit_State *J, RecordFFData *rd) | ||
521 | { | ||
522 | TRef tr = lj_ir_tobit(J, J->base[0]); | ||
523 | J->base[0] = (rd->data == IR_TOBIT) ? tr : emitir(IRTI(rd->data), tr, 0); | ||
524 | } | ||
525 | |||
526 | /* Record N-ary bit.band, bit.bor, bit.bxor. */ | ||
527 | static void LJ_FASTCALL recff_bit_nary(jit_State *J, RecordFFData *rd) | ||
528 | { | ||
529 | TRef tr = lj_ir_tobit(J, J->base[0]); | ||
530 | uint32_t op = rd->data; | ||
531 | BCReg i; | ||
532 | for (i = 1; J->base[i] != 0; i++) | ||
533 | tr = emitir(IRTI(op), tr, lj_ir_tobit(J, J->base[i])); | ||
534 | J->base[0] = tr; | ||
535 | } | ||
536 | |||
537 | /* Record bit shifts. */ | ||
538 | static void LJ_FASTCALL recff_bit_shift(jit_State *J, RecordFFData *rd) | ||
539 | { | ||
540 | TRef tr = lj_ir_tobit(J, J->base[0]); | ||
541 | TRef tsh = lj_ir_tobit(J, J->base[1]); | ||
542 | if (!(rd->data < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) && | ||
543 | !tref_isk(tsh)) | ||
544 | tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 31)); | ||
545 | J->base[0] = emitir(IRTI(rd->data), tr, tsh); | ||
546 | } | ||
547 | |||
548 | /* -- String library fast functions --------------------------------------- */ | ||
549 | |||
550 | static void LJ_FASTCALL recff_string_len(jit_State *J, RecordFFData *rd) | ||
551 | { | ||
552 | J->base[0] = emitir(IRTI(IR_FLOAD), lj_ir_tostr(J, J->base[0]), IRFL_STR_LEN); | ||
553 | UNUSED(rd); | ||
554 | } | ||
555 | |||
556 | /* Handle string.byte (rd->data = 0) and string.sub (rd->data = 1). */ | ||
557 | static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd) | ||
558 | { | ||
559 | TRef trstr = lj_ir_tostr(J, J->base[0]); | ||
560 | TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN); | ||
561 | TRef tr0 = lj_ir_kint(J, 0); | ||
562 | TRef trstart, trend; | ||
563 | GCstr *str = argv2str(J, &rd->argv[0]); | ||
564 | int32_t start, end; | ||
565 | if (rd->data) { /* string.sub(str, start [,end]) */ | ||
566 | start = argv2int(J, &rd->argv[1]); | ||
567 | trstart = lj_ir_toint(J, J->base[1]); | ||
568 | trend = J->base[2]; | ||
569 | if (tref_isnil(trend)) { | ||
570 | trend = lj_ir_kint(J, -1); | ||
571 | end = -1; | ||
572 | } else { | ||
573 | trend = lj_ir_toint(J, trend); | ||
574 | end = argv2int(J, &rd->argv[2]); | ||
575 | } | ||
576 | } else { /* string.byte(str, [,start [,end]]) */ | ||
577 | if (J->base[1]) { | ||
578 | start = argv2int(J, &rd->argv[1]); | ||
579 | trstart = lj_ir_toint(J, J->base[1]); | ||
580 | trend = J->base[2]; | ||
581 | if (tref_isnil(trend)) { | ||
582 | trend = trstart; | ||
583 | end = start; | ||
584 | } else { | ||
585 | trend = lj_ir_toint(J, trend); | ||
586 | end = argv2int(J, &rd->argv[2]); | ||
587 | } | ||
588 | } else { | ||
589 | trend = trstart = lj_ir_kint(J, 1); | ||
590 | end = start = 1; | ||
591 | } | ||
592 | } | ||
593 | if (end < 0) { | ||
594 | emitir(IRTGI(IR_LT), trend, tr0); | ||
595 | trend = emitir(IRTI(IR_ADD), emitir(IRTI(IR_ADD), trlen, trend), | ||
596 | lj_ir_kint(J, 1)); | ||
597 | end = end+(int32_t)str->len+1; | ||
598 | } else if ((MSize)end <= str->len) { | ||
599 | emitir(IRTGI(IR_ULE), trend, trlen); | ||
600 | } else { | ||
601 | emitir(IRTGI(IR_GT), trend, trlen); | ||
602 | end = (int32_t)str->len; | ||
603 | trend = trlen; | ||
604 | } | ||
605 | if (start < 0) { | ||
606 | emitir(IRTGI(IR_LT), trstart, tr0); | ||
607 | trstart = emitir(IRTI(IR_ADD), trlen, trstart); | ||
608 | start = start+(int32_t)str->len; | ||
609 | emitir(start < 0 ? IRTGI(IR_LT) : IRTGI(IR_GE), trstart, tr0); | ||
610 | if (start < 0) { | ||
611 | trstart = tr0; | ||
612 | start = 0; | ||
613 | } | ||
614 | } else { | ||
615 | if (start == 0) { | ||
616 | emitir(IRTGI(IR_EQ), trstart, tr0); | ||
617 | trstart = tr0; | ||
618 | } else { | ||
619 | trstart = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, -1)); | ||
620 | emitir(IRTGI(IR_GE), trstart, tr0); | ||
621 | start--; | ||
622 | } | ||
623 | } | ||
624 | if (rd->data) { /* Return string.sub result. */ | ||
625 | if (end - start >= 0) { | ||
626 | /* Also handle empty range here, to avoid extra traces. */ | ||
627 | TRef trptr, trslen = emitir(IRTI(IR_SUB), trend, trstart); | ||
628 | emitir(IRTGI(IR_GE), trslen, tr0); | ||
629 | trptr = emitir(IRT(IR_STRREF, IRT_PTR), trstr, trstart); | ||
630 | J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), trptr, trslen); | ||
631 | } else { /* Range underflow: return empty string. */ | ||
632 | emitir(IRTGI(IR_LT), trend, trstart); | ||
633 | J->base[0] = lj_ir_kstr(J, lj_str_new(J->L, strdata(str), 0)); | ||
634 | } | ||
635 | } else { /* Return string.byte result(s). */ | ||
636 | ptrdiff_t i, len = end - start; | ||
637 | if (len > 0) { | ||
638 | TRef trslen = emitir(IRTI(IR_SUB), trend, trstart); | ||
639 | emitir(IRTGI(IR_EQ), trslen, lj_ir_kint(J, (int32_t)len)); | ||
640 | if (J->baseslot + len > LJ_MAX_JSLOTS) | ||
641 | lj_trace_err_info(J, LJ_TRERR_STACKOV); | ||
642 | rd->nres = len; | ||
643 | for (i = 0; i < len; i++) { | ||
644 | TRef tmp = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, (int32_t)i)); | ||
645 | tmp = emitir(IRT(IR_STRREF, IRT_PTR), trstr, tmp); | ||
646 | J->base[i] = emitir(IRT(IR_XLOAD, IRT_U8), tmp, IRXLOAD_READONLY); | ||
647 | } | ||
648 | } else { /* Empty range or range underflow: return no results. */ | ||
649 | emitir(IRTGI(IR_LE), trend, trstart); | ||
650 | rd->nres = 0; | ||
651 | } | ||
652 | } | ||
653 | } | ||
654 | |||
655 | /* -- Table library fast functions ---------------------------------------- */ | ||
656 | |||
657 | static void LJ_FASTCALL recff_table_getn(jit_State *J, RecordFFData *rd) | ||
658 | { | ||
659 | if (tref_istab(J->base[0])) | ||
660 | J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, J->base[0]); | ||
661 | /* else: Interpreter will throw. */ | ||
662 | UNUSED(rd); | ||
663 | } | ||
664 | |||
665 | static void LJ_FASTCALL recff_table_remove(jit_State *J, RecordFFData *rd) | ||
666 | { | ||
667 | TRef tab = J->base[0]; | ||
668 | rd->nres = 0; | ||
669 | if (tref_istab(tab)) { | ||
670 | if (!J->base[1] || tref_isnil(J->base[1])) { /* Simple pop: t[#t] = nil */ | ||
671 | TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, tab); | ||
672 | GCtab *t = tabV(&rd->argv[0]); | ||
673 | MSize len = lj_tab_len(t); | ||
674 | emitir(IRTGI(len ? IR_NE : IR_EQ), trlen, lj_ir_kint(J, 0)); | ||
675 | if (len) { | ||
676 | RecordIndex ix; | ||
677 | ix.tab = tab; | ||
678 | ix.key = trlen; | ||
679 | settabV(J->L, &ix.tabv, t); | ||
680 | setintV(&ix.keyv, len); | ||
681 | ix.idxchain = 0; | ||
682 | if (results_wanted(J) != 0) { /* Specialize load only if needed. */ | ||
683 | ix.val = 0; | ||
684 | J->base[0] = lj_record_idx(J, &ix); /* Load previous value. */ | ||
685 | rd->nres = 1; | ||
686 | /* Assumes ix.key/ix.tab is not modified for raw lj_record_idx(). */ | ||
687 | } | ||
688 | ix.val = TREF_NIL; | ||
689 | lj_record_idx(J, &ix); /* Remove value. */ | ||
690 | } | ||
691 | } else { /* Complex case: remove in the middle. */ | ||
692 | recff_nyiu(J); | ||
693 | } | ||
694 | } /* else: Interpreter will throw. */ | ||
695 | } | ||
696 | |||
697 | static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd) | ||
698 | { | ||
699 | RecordIndex ix; | ||
700 | ix.tab = J->base[0]; | ||
701 | ix.val = J->base[1]; | ||
702 | rd->nres = 0; | ||
703 | if (tref_istab(ix.tab) && ix.val) { | ||
704 | if (!J->base[2]) { /* Simple push: t[#t+1] = v */ | ||
705 | TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, ix.tab); | ||
706 | GCtab *t = tabV(&rd->argv[0]); | ||
707 | ix.key = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1)); | ||
708 | settabV(J->L, &ix.tabv, t); | ||
709 | setintV(&ix.keyv, lj_tab_len(t) + 1); | ||
710 | ix.idxchain = 0; | ||
711 | lj_record_idx(J, &ix); /* Set new value. */ | ||
712 | } else { /* Complex case: insert in the middle. */ | ||
713 | recff_nyiu(J); | ||
714 | } | ||
715 | } /* else: Interpreter will throw. */ | ||
716 | } | ||
717 | |||
718 | /* -- I/O library fast functions ------------------------------------------ */ | ||
719 | |||
720 | /* Get FILE* for I/O function. Any I/O error aborts recording, so there's | ||
721 | ** no need to encode the alternate cases for any of the guards. | ||
722 | */ | ||
723 | static TRef recff_io_fp(jit_State *J, uint32_t id) | ||
724 | { | ||
725 | TRef tr, ud, fp; | ||
726 | if (id) { /* io.func() */ | ||
727 | tr = lj_ir_kptr(J, &J2G(J)->gcroot[id]); | ||
728 | ud = emitir(IRT(IR_XLOAD, IRT_UDATA), tr, 0); | ||
729 | } else { /* fp:method() */ | ||
730 | ud = J->base[0]; | ||
731 | if (!tref_isudata(ud)) | ||
732 | lj_trace_err(J, LJ_TRERR_BADTYPE); | ||
733 | tr = emitir(IRT(IR_FLOAD, IRT_U8), ud, IRFL_UDATA_UDTYPE); | ||
734 | emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE)); | ||
735 | } | ||
736 | fp = emitir(IRT(IR_FLOAD, IRT_LIGHTUD), ud, IRFL_UDATA_FILE); | ||
737 | emitir(IRTG(IR_NE, IRT_LIGHTUD), fp, lj_ir_knull(J, IRT_LIGHTUD)); | ||
738 | return fp; | ||
739 | } | ||
740 | |||
741 | static void LJ_FASTCALL recff_io_write(jit_State *J, RecordFFData *rd) | ||
742 | { | ||
743 | TRef fp = recff_io_fp(J, rd->data); | ||
744 | TRef zero = lj_ir_kint(J, 0); | ||
745 | TRef one = lj_ir_kint(J, 1); | ||
746 | ptrdiff_t i = rd->data == 0 ? 1 : 0; | ||
747 | for (; J->base[i]; i++) { | ||
748 | TRef str = lj_ir_tostr(J, J->base[i]); | ||
749 | TRef buf = emitir(IRT(IR_STRREF, IRT_PTR), str, zero); | ||
750 | TRef len = emitir(IRTI(IR_FLOAD), str, IRFL_STR_LEN); | ||
751 | if (tref_isk(len) && IR(tref_ref(len))->i == 1) { | ||
752 | TRef tr = emitir(IRT(IR_XLOAD, IRT_U8), buf, IRXLOAD_READONLY); | ||
753 | tr = lj_ir_call(J, IRCALL_fputc, tr, fp); | ||
754 | if (results_wanted(J) != 0) /* Check result only if not ignored. */ | ||
755 | emitir(IRTGI(IR_NE), tr, lj_ir_kint(J, -1)); | ||
756 | } else { | ||
757 | TRef tr = lj_ir_call(J, IRCALL_fwrite, buf, one, len, fp); | ||
758 | if (results_wanted(J) != 0) /* Check result only if not ignored. */ | ||
759 | emitir(IRTGI(IR_EQ), tr, len); | ||
760 | } | ||
761 | } | ||
762 | J->base[0] = TREF_TRUE; | ||
763 | } | ||
764 | |||
765 | static void LJ_FASTCALL recff_io_flush(jit_State *J, RecordFFData *rd) | ||
766 | { | ||
767 | TRef fp = recff_io_fp(J, rd->data); | ||
768 | TRef tr = lj_ir_call(J, IRCALL_fflush, fp); | ||
769 | if (results_wanted(J) != 0) /* Check result only if not ignored. */ | ||
770 | emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0)); | ||
771 | J->base[0] = TREF_TRUE; | ||
772 | } | ||
773 | |||
774 | /* -- Record calls to fast functions -------------------------------------- */ | ||
775 | |||
776 | #include "lj_recdef.h" | ||
777 | |||
778 | static uint32_t recdef_lookup(GCfunc *fn) | ||
779 | { | ||
780 | if (fn->c.ffid < sizeof(recff_idmap)/sizeof(recff_idmap[0])) | ||
781 | return recff_idmap[fn->c.ffid]; | ||
782 | else | ||
783 | return 0; | ||
784 | } | ||
785 | |||
786 | /* Record entry to a fast function or C function. */ | ||
787 | void lj_ffrecord_func(jit_State *J) | ||
788 | { | ||
789 | RecordFFData rd; | ||
790 | uint32_t m = recdef_lookup(J->fn); | ||
791 | rd.data = m & 0xff; | ||
792 | rd.nres = 1; /* Default is one result. */ | ||
793 | rd.argv = J->L->base; | ||
794 | J->base[J->maxslot] = 0; /* Mark end of arguments. */ | ||
795 | (recff_func[m >> 8])(J, &rd); /* Call recff_* handler. */ | ||
796 | if (rd.nres >= 0) | ||
797 | lj_record_ret(J, 0, rd.nres); | ||
798 | } | ||
799 | |||
800 | #undef IR | ||
801 | #undef emitir | ||
802 | |||
803 | #endif | ||