/src/testdir/build/lua-master/source/lvm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lvm.c $ |
3 | | ** Lua virtual machine |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lvm_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | #include <float.h> |
13 | | #include <limits.h> |
14 | | #include <math.h> |
15 | | #include <stdio.h> |
16 | | #include <stdlib.h> |
17 | | #include <string.h> |
18 | | |
19 | | #include "lua.h" |
20 | | |
21 | | #include "lapi.h" |
22 | | #include "ldebug.h" |
23 | | #include "ldo.h" |
24 | | #include "lfunc.h" |
25 | | #include "lgc.h" |
26 | | #include "lobject.h" |
27 | | #include "lopcodes.h" |
28 | | #include "lstate.h" |
29 | | #include "lstring.h" |
30 | | #include "ltable.h" |
31 | | #include "ltm.h" |
32 | | #include "lvm.h" |
33 | | |
34 | | |
35 | | /* |
36 | | ** By default, use jump tables in the main interpreter loop on gcc |
37 | | ** and compatible compilers. |
38 | | */ |
39 | | #if !defined(LUA_USE_JUMPTABLE) |
40 | | #if defined(__GNUC__) |
41 | | #define LUA_USE_JUMPTABLE 1 |
42 | | #else |
43 | | #define LUA_USE_JUMPTABLE 0 |
44 | | #endif |
45 | | #endif |
46 | | |
47 | | |
48 | | |
49 | | /* limit for table tag-method chains (to avoid infinite loops) */ |
50 | 223M | #define MAXTAGLOOP 2000 |
51 | | |
52 | | |
53 | | /* |
54 | | ** 'l_intfitsf' checks whether a given integer is in the range that |
55 | | ** can be converted to a float without rounding. Used in comparisons. |
56 | | */ |
57 | | |
58 | | /* number of bits in the mantissa of a float */ |
59 | 13.7M | #define NBM (l_floatatt(MANT_DIG)) |
60 | | |
61 | | /* |
62 | | ** Check whether some integers may not fit in a float, testing whether |
63 | | ** (maxinteger >> NBM) > 0. (That implies (1 << NBM) <= maxinteger.) |
64 | | ** (The shifts are done in parts, to avoid shifting by more than the size |
65 | | ** of an integer. In a worst case, NBM == 113 for long double and |
66 | | ** sizeof(long) == 32.) |
67 | | */ |
68 | | #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ |
69 | | >> (NBM - (3 * (NBM / 4)))) > 0 |
70 | | |
71 | | /* limit for integers that fit in a float */ |
72 | 13.7M | #define MAXINTFITSF ((lua_Unsigned)1 << NBM) |
73 | | |
74 | | /* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */ |
75 | 6.85M | #define l_intfitsf(i) ((MAXINTFITSF + l_castS2U(i)) <= (2 * MAXINTFITSF)) |
76 | | |
77 | | #else /* all integers fit in a float precisely */ |
78 | | |
79 | | #define l_intfitsf(i) 1 |
80 | | |
81 | | #endif |
82 | | |
83 | | |
84 | | /* |
85 | | ** Try to convert a value from string to a number value. |
86 | | ** If the value is not a string or is a string not representing |
87 | | ** a valid numeral (or if coercions from strings to numbers |
88 | | ** are disabled via macro 'cvt2num'), do not modify 'result' |
89 | | ** and return 0. |
90 | | */ |
91 | 4.12M | static int l_strton (const TValue *obj, TValue *result) { |
92 | 4.12M | lua_assert(obj != result); |
93 | 4.12M | if (!cvt2num(obj)) /* is object not a string? */ |
94 | 3.04M | return 0; |
95 | 1.07M | else { |
96 | 1.07M | TString *st = tsvalue(obj); |
97 | 0 | size_t stlen; |
98 | 1.07M | const char *s = getlstr(st, stlen); |
99 | 1.07M | return (luaO_str2num(s, result) == stlen + 1); |
100 | 1.07M | } |
101 | 4.12M | } |
102 | | |
103 | | |
104 | | /* |
105 | | ** Try to convert a value to a float. The float case is already handled |
106 | | ** by the macro 'tonumber'. |
107 | | */ |
108 | 964k | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { |
109 | 964k | TValue v; |
110 | 964k | if (ttisinteger(obj)) { |
111 | 417k | *n = cast_num(ivalue(obj)); |
112 | 0 | return 1; |
113 | 417k | } |
114 | 547k | else if (l_strton(obj, &v)) { /* string coercible to number? */ |
115 | 328k | *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ |
116 | 328k | return 1; |
117 | 328k | } |
118 | 218k | else |
119 | 218k | return 0; /* conversion failed */ |
120 | 964k | } |
121 | | |
122 | | |
123 | | /* |
124 | | ** try to convert a float to an integer, rounding according to 'mode'. |
125 | | */ |
126 | 148M | int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) { |
127 | 148M | lua_Number f = l_floor(n); |
128 | 148M | if (n != f) { /* not an integral value? */ |
129 | 11.7M | if (mode == F2Ieq) return 0; /* fails if mode demands integral value */ |
130 | 56.2k | else if (mode == F2Iceil) /* needs ceiling? */ |
131 | 49.3k | f += 1; /* convert floor to ceiling (remember: n != f) */ |
132 | 11.7M | } |
133 | 136M | return lua_numbertointeger(f, p); |
134 | 148M | } |
135 | | |
136 | | |
137 | | /* |
138 | | ** try to convert a value to an integer, rounding according to 'mode', |
139 | | ** without string coercion. |
140 | | ** ("Fast track" handled by macro 'tointegerns'.) |
141 | | */ |
142 | 17.0M | int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) { |
143 | 17.0M | if (ttisfloat(obj)) |
144 | 10.8M | return luaV_flttointeger(fltvalue(obj), p, mode); |
145 | 6.16M | else if (ttisinteger(obj)) { |
146 | 5.74M | *p = ivalue(obj); |
147 | 0 | return 1; |
148 | 5.74M | } |
149 | 427k | else |
150 | 427k | return 0; |
151 | 17.0M | } |
152 | | |
153 | | |
154 | | /* |
155 | | ** try to convert a value to an integer. |
156 | | */ |
157 | 3.57M | int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) { |
158 | 3.57M | TValue v; |
159 | 3.57M | if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ |
160 | 393k | obj = &v; /* change it to point to its corresponding number */ |
161 | 3.57M | return luaV_tointegerns(obj, p, mode); |
162 | 3.57M | } |
163 | | |
164 | | |
165 | | /* |
166 | | ** Try to convert a 'for' limit to an integer, preserving the semantics |
167 | | ** of the loop. Return true if the loop must not run; otherwise, '*p' |
168 | | ** gets the integer limit. |
169 | | ** (The following explanation assumes a positive step; it is valid for |
170 | | ** negative steps mutatis mutandis.) |
171 | | ** If the limit is an integer or can be converted to an integer, |
172 | | ** rounding down, that is the limit. |
173 | | ** Otherwise, check whether the limit can be converted to a float. If |
174 | | ** the float is too large, clip it to LUA_MAXINTEGER. If the float |
175 | | ** is too negative, the loop should not run, because any initial |
176 | | ** integer value is greater than such limit; so, the function returns |
177 | | ** true to signal that. (For this latter case, no integer limit would be |
178 | | ** correct; even a limit of LUA_MININTEGER would run the loop once for |
179 | | ** an initial value equal to LUA_MININTEGER.) |
180 | | */ |
181 | | static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, |
182 | 2.87M | lua_Integer *p, lua_Integer step) { |
183 | 2.87M | if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) { |
184 | | /* not coercible to in integer */ |
185 | 17.7k | lua_Number flim; /* try to convert to float */ |
186 | 17.7k | if (!tonumber(lim, &flim)) /* cannot convert to float? */ |
187 | 2.28k | luaG_forerror(L, lim, "limit"); |
188 | | /* else 'flim' is a float out of integer bounds */ |
189 | 15.4k | if (luai_numlt(0, flim)) { /* if it is positive, it is too large */ |
190 | 4.96k | if (step < 0) return 1; /* initial value must be less than it */ |
191 | 3.31k | *p = LUA_MAXINTEGER; /* truncate */ |
192 | 3.31k | } |
193 | 10.5k | else { /* it is less than min integer */ |
194 | 10.5k | if (step > 0) return 1; /* initial value must be greater than it */ |
195 | 8.91k | *p = LUA_MININTEGER; /* truncate */ |
196 | 8.91k | } |
197 | 15.4k | } |
198 | 2.87M | return (step > 0 ? init > *p : init < *p); /* not to run? */ |
199 | 2.87M | } |
200 | | |
201 | | |
202 | | /* |
203 | | ** Prepare a numerical for loop (opcode OP_FORPREP). |
204 | | ** Before execution, stack is as follows: |
205 | | ** ra : initial value |
206 | | ** ra + 1 : limit |
207 | | ** ra + 2 : step |
208 | | ** Return true to skip the loop. Otherwise, |
209 | | ** after preparation, stack will be as follows: |
210 | | ** ra : loop counter (integer loops) or limit (float loops) |
211 | | ** ra + 1 : step |
212 | | ** ra + 2 : control variable |
213 | | */ |
214 | 3.23M | static int forprep (lua_State *L, StkId ra) { |
215 | 3.23M | TValue *pinit = s2v(ra); |
216 | 3.23M | TValue *plimit = s2v(ra + 1); |
217 | 3.23M | TValue *pstep = s2v(ra + 2); |
218 | 3.23M | if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ |
219 | 2.87M | lua_Integer init = ivalue(pinit); |
220 | 2.87M | lua_Integer step = ivalue(pstep); |
221 | 0 | lua_Integer limit; |
222 | 2.87M | if (step == 0) |
223 | 96 | luaG_runerror(L, "'for' step is zero"); |
224 | 2.87M | if (forlimit(L, init, plimit, &limit, step)) |
225 | 1.80M | return 1; /* skip the loop */ |
226 | 1.06M | else { /* prepare loop counter */ |
227 | 1.06M | lua_Unsigned count; |
228 | 1.06M | if (step > 0) { /* ascending loop? */ |
229 | 1.05M | count = l_castS2U(limit) - l_castS2U(init); |
230 | 1.05M | if (step != 1) /* avoid division in the too common case */ |
231 | 6.56k | count /= l_castS2U(step); |
232 | 1.05M | } |
233 | 14.3k | else { /* step < 0; descending loop */ |
234 | 14.3k | count = l_castS2U(init) - l_castS2U(limit); |
235 | | /* 'step+1' avoids negating 'mininteger' */ |
236 | 14.3k | count /= l_castS2U(-(step + 1)) + 1u; |
237 | 14.3k | } |
238 | | /* use 'chgivalue' for places that for sure had integers */ |
239 | 1.06M | chgivalue(s2v(ra), l_castU2S(count)); /* change init to count */ |
240 | 1.06M | setivalue(s2v(ra + 1), step); /* change limit to step */ |
241 | 1.06M | chgivalue(s2v(ra + 2), init); /* change step to init */ |
242 | 1.06M | } |
243 | 2.87M | } |
244 | 358k | else { /* try making all values floats */ |
245 | 358k | lua_Number init; lua_Number limit; lua_Number step; |
246 | 358k | if (l_unlikely(!tonumber(plimit, &limit))) |
247 | 129 | luaG_forerror(L, plimit, "limit"); |
248 | 358k | if (l_unlikely(!tonumber(pstep, &step))) |
249 | 115 | luaG_forerror(L, pstep, "step"); |
250 | 358k | if (l_unlikely(!tonumber(pinit, &init))) |
251 | 4.65k | luaG_forerror(L, pinit, "initial value"); |
252 | 353k | if (step == 0) |
253 | 1.62k | luaG_runerror(L, "'for' step is zero"); |
254 | 352k | if (luai_numlt(0, step) ? luai_numlt(limit, init) |
255 | 352k | : luai_numlt(init, limit)) |
256 | 33.8k | return 1; /* skip the loop */ |
257 | 318k | else { |
258 | | /* make sure all values are floats */ |
259 | 318k | setfltvalue(s2v(ra), limit); |
260 | 318k | setfltvalue(s2v(ra + 1), step); |
261 | 318k | setfltvalue(s2v(ra + 2), init); /* control variable */ |
262 | 318k | } |
263 | 352k | } |
264 | 1.38M | return 0; |
265 | 3.23M | } |
266 | | |
267 | | |
268 | | /* |
269 | | ** Execute a step of a float numerical for loop, returning |
270 | | ** true iff the loop must continue. (The integer case is |
271 | | ** written online with opcode OP_FORLOOP, for performance.) |
272 | | */ |
273 | 4.45M | static int floatforloop (StkId ra) { |
274 | 4.45M | lua_Number step = fltvalue(s2v(ra + 1)); |
275 | 4.45M | lua_Number limit = fltvalue(s2v(ra)); |
276 | 4.45M | lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */ |
277 | 4.45M | idx = luai_numadd(L, idx, step); /* increment index */ |
278 | 4.45M | if (luai_numlt(0, step) ? luai_numle(idx, limit) |
279 | 4.45M | : luai_numle(limit, idx)) { |
280 | 4.13M | chgfltvalue(s2v(ra + 2), idx); /* update control variable */ |
281 | 4.13M | return 1; /* jump back */ |
282 | 4.13M | } |
283 | 318k | else |
284 | 318k | return 0; /* finish the loop */ |
285 | 4.45M | } |
286 | | |
287 | | |
288 | | /* |
289 | | ** Finish the table access 'val = t[key]' and return the tag of the result. |
290 | | */ |
291 | | lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key, |
292 | 162M | StkId val, lu_byte tag) { |
293 | 162M | int loop; /* counter to avoid infinite loops */ |
294 | 162M | const TValue *tm; /* metamethod */ |
295 | 172M | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
296 | 172M | if (tag == LUA_VNOTABLE) { /* 't' is not a table? */ |
297 | 87.0M | lua_assert(!ttistable(t)); |
298 | 87.0M | tm = luaT_gettmbyobj(L, t, TM_INDEX); |
299 | 87.0M | if (l_unlikely(notm(tm))) |
300 | 33.2k | luaG_typeerror(L, t, "index"); /* no metamethod */ |
301 | | /* else will try the metamethod */ |
302 | 87.0M | } |
303 | 85.0M | else { /* 't' is a table */ |
304 | 85.0M | tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ |
305 | 85.0M | if (tm == NULL) { /* no metamethod? */ |
306 | 85.0M | setnilvalue(s2v(val)); /* result is nil */ |
307 | 85.0M | return LUA_VNIL; |
308 | 85.0M | } |
309 | | /* else will try the metamethod */ |
310 | 85.0M | } |
311 | 86.9M | if (ttisfunction(tm)) { /* is metamethod a function? */ |
312 | 70.8M | tag = luaT_callTMres(L, tm, t, key, val); /* call it */ |
313 | 70.8M | return tag; /* return tag of the result */ |
314 | 70.8M | } |
315 | 16.1M | t = tm; /* else try to access 'tm[key]' */ |
316 | 16.1M | luaV_fastget(t, key, s2v(val), luaH_get, tag); |
317 | 16.1M | if (!tagisempty(tag)) |
318 | 6.53M | return tag; /* done */ |
319 | | /* else repeat (tail call 'luaV_finishget') */ |
320 | 16.1M | } |
321 | 0 | luaG_runerror(L, "'__index' chain too long; possible loop"); |
322 | 0 | return 0; /* to avoid warnings */ |
323 | 162M | } |
324 | | |
325 | | |
326 | | /* |
327 | | ** Finish a table assignment 't[key] = val'. |
328 | | ** About anchoring the table before the call to 'luaH_finishset': |
329 | | ** This call may trigger an emergency collection. When loop>0, |
330 | | ** the table being accessed is a field in some metatable. If this |
331 | | ** metatable is weak and the table is not anchored, this collection |
332 | | ** could collect that table while it is being updated. |
333 | | */ |
334 | | void luaV_finishset (lua_State *L, const TValue *t, TValue *key, |
335 | 51.6M | TValue *val, int hres) { |
336 | 51.6M | int loop; /* counter to avoid infinite loops */ |
337 | 51.6M | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
338 | 51.6M | const TValue *tm; /* '__newindex' metamethod */ |
339 | 51.6M | if (hres != HNOTATABLE) { /* is 't' a table? */ |
340 | 44.0M | Table *h = hvalue(t); /* save 't' table */ |
341 | 22.0M | tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ |
342 | 44.0M | if (tm == NULL) { /* no metamethod? */ |
343 | 21.8M | sethvalue2s(L, L->top.p, h); /* anchor 't' */ |
344 | 21.8M | L->top.p++; /* assume EXTRA_STACK */ |
345 | 21.8M | luaH_finishset(L, h, key, val, hres); /* set new value */ |
346 | 21.8M | L->top.p--; |
347 | 21.8M | invalidateTMcache(h); |
348 | 21.8M | luaC_barrierback(L, obj2gco(h), val); |
349 | 21.8M | return; |
350 | 21.8M | } |
351 | | /* else will try the metamethod */ |
352 | 44.0M | } |
353 | 29.5M | else { /* not a table; check metamethod */ |
354 | 29.5M | tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); |
355 | 29.5M | if (l_unlikely(notm(tm))) |
356 | 7.09k | luaG_typeerror(L, t, "index"); |
357 | 29.5M | } |
358 | | /* try the metamethod */ |
359 | 29.7M | if (ttisfunction(tm)) { |
360 | 29.7M | luaT_callTM(L, tm, t, key, val); |
361 | 29.7M | return; |
362 | 29.7M | } |
363 | 16 | t = tm; /* else repeat assignment over 'tm' */ |
364 | 16 | luaV_fastset(t, key, val, hres, luaH_pset); |
365 | 16 | if (hres == HOK) { |
366 | 14 | luaV_finishfastset(L, t, val); |
367 | 14 | return; /* done */ |
368 | 14 | } |
369 | | /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ |
370 | 16 | } |
371 | 0 | luaG_runerror(L, "'__newindex' chain too long; possible loop"); |
372 | 51.6M | } |
373 | | |
374 | | |
375 | | /* |
376 | | ** Compare two strings 'ts1' x 'ts2', returning an integer less-equal- |
377 | | ** -greater than zero if 'ts1' is less-equal-greater than 'ts2'. |
378 | | ** The code is a little tricky because it allows '\0' in the strings |
379 | | ** and it uses 'strcoll' (to respect locales) for each segment |
380 | | ** of the strings. Note that segments can compare equal but still |
381 | | ** have different lengths. |
382 | | */ |
383 | 1.31M | static int l_strcmp (const TString *ts1, const TString *ts2) { |
384 | 1.31M | size_t rl1; /* real length */ |
385 | 1.31M | const char *s1 = getlstr(ts1, rl1); |
386 | 1.31M | size_t rl2; |
387 | 1.31M | const char *s2 = getlstr(ts2, rl2); |
388 | 1.91M | for (;;) { /* for each segment */ |
389 | 1.91M | int temp = strcoll(s1, s2); |
390 | 1.91M | if (temp != 0) /* not equal? */ |
391 | 1.05M | return temp; /* done */ |
392 | 860k | else { /* strings are equal up to a '\0' */ |
393 | 860k | size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */ |
394 | 860k | size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */ |
395 | 860k | if (zl2 == rl2) /* 's2' is finished? */ |
396 | 239k | return (zl1 == rl1) ? 0 : 1; /* check 's1' */ |
397 | 621k | else if (zl1 == rl1) /* 's1' is finished? */ |
398 | 13.9k | return -1; /* 's1' is less than 's2' ('s2' is not finished) */ |
399 | | /* both strings longer than 'zl'; go on comparing after the '\0' */ |
400 | 607k | zl1++; zl2++; |
401 | 607k | s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2; |
402 | 607k | } |
403 | 1.91M | } |
404 | 1.31M | } |
405 | | |
406 | | |
407 | | /* |
408 | | ** Check whether integer 'i' is less than float 'f'. If 'i' has an |
409 | | ** exact representation as a float ('l_intfitsf'), compare numbers as |
410 | | ** floats. Otherwise, use the equivalence 'i < f <=> i < ceil(f)'. |
411 | | ** If 'ceil(f)' is out of integer range, either 'f' is greater than |
412 | | ** all integers or less than all integers. |
413 | | ** (The test with 'l_intfitsf' is only for performance; the else |
414 | | ** case is correct for all values, but it is slow due to the conversion |
415 | | ** from float to int.) |
416 | | ** When 'f' is NaN, comparisons must result in false. |
417 | | */ |
418 | 308k | l_sinline int LTintfloat (lua_Integer i, lua_Number f) { |
419 | 308k | if (l_intfitsf(i)) |
420 | 213k | return luai_numlt(cast_num(i), f); /* compare them as floats */ |
421 | 95.0k | else { /* i < f <=> i < ceil(f) */ |
422 | 95.0k | lua_Integer fi; |
423 | 95.0k | if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ |
424 | 21.9k | return i < fi; /* compare them as integers */ |
425 | 73.0k | else /* 'f' is either greater or less than all integers */ |
426 | 73.0k | return f > 0; /* greater? */ |
427 | 95.0k | } |
428 | 308k | } |
429 | | |
430 | | |
431 | | /* |
432 | | ** Check whether integer 'i' is less than or equal to float 'f'. |
433 | | ** See comments on previous function. |
434 | | */ |
435 | 192k | l_sinline int LEintfloat (lua_Integer i, lua_Number f) { |
436 | 192k | if (l_intfitsf(i)) |
437 | 182k | return luai_numle(cast_num(i), f); /* compare them as floats */ |
438 | 9.78k | else { /* i <= f <=> i <= floor(f) */ |
439 | 9.78k | lua_Integer fi; |
440 | 9.78k | if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ |
441 | 8.79k | return i <= fi; /* compare them as integers */ |
442 | 993 | else /* 'f' is either greater or less than all integers */ |
443 | 993 | return f > 0; /* greater? */ |
444 | 9.78k | } |
445 | 192k | } |
446 | | |
447 | | |
448 | | /* |
449 | | ** Check whether float 'f' is less than integer 'i'. |
450 | | ** See comments on previous function. |
451 | | */ |
452 | 6.24M | l_sinline int LTfloatint (lua_Number f, lua_Integer i) { |
453 | 6.24M | if (l_intfitsf(i)) |
454 | 6.24M | return luai_numlt(f, cast_num(i)); /* compare them as floats */ |
455 | 5.52k | else { /* f < i <=> floor(f) < i */ |
456 | 5.52k | lua_Integer fi; |
457 | 5.52k | if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ |
458 | 3.31k | return fi < i; /* compare them as integers */ |
459 | 2.20k | else /* 'f' is either greater or less than all integers */ |
460 | 2.20k | return f < 0; /* less? */ |
461 | 5.52k | } |
462 | 6.24M | } |
463 | | |
464 | | |
465 | | /* |
466 | | ** Check whether float 'f' is less than or equal to integer 'i'. |
467 | | ** See comments on previous function. |
468 | | */ |
469 | 108k | l_sinline int LEfloatint (lua_Number f, lua_Integer i) { |
470 | 108k | if (l_intfitsf(i)) |
471 | 78.6k | return luai_numle(f, cast_num(i)); /* compare them as floats */ |
472 | 29.5k | else { /* f <= i <=> ceil(f) <= i */ |
473 | 29.5k | lua_Integer fi; |
474 | 29.5k | if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ |
475 | 27.6k | return fi <= i; /* compare them as integers */ |
476 | 1.89k | else /* 'f' is either greater or less than all integers */ |
477 | 1.89k | return f < 0; /* less? */ |
478 | 29.5k | } |
479 | 108k | } |
480 | | |
481 | | |
482 | | /* |
483 | | ** Return 'l < r', for numbers. |
484 | | */ |
485 | 6.84M | l_sinline int LTnum (const TValue *l, const TValue *r) { |
486 | 6.84M | lua_assert(ttisnumber(l) && ttisnumber(r)); |
487 | 6.84M | if (ttisinteger(l)) { |
488 | 308k | lua_Integer li = ivalue(l); |
489 | 308k | if (ttisinteger(r)) |
490 | 308k | return li < ivalue(r); /* both are integers */ |
491 | 308k | else /* 'l' is int and 'r' is float */ |
492 | 308k | return LTintfloat(li, fltvalue(r)); /* l < r ? */ |
493 | 308k | } |
494 | 6.54M | else { |
495 | 6.54M | lua_Number lf = fltvalue(l); /* 'l' must be float */ |
496 | 6.54M | if (ttisfloat(r)) |
497 | 6.54M | return luai_numlt(lf, fltvalue(r)); /* both are float */ |
498 | 6.24M | else /* 'l' is float and 'r' is int */ |
499 | 6.24M | return LTfloatint(lf, ivalue(r)); |
500 | 6.54M | } |
501 | 6.84M | } |
502 | | |
503 | | |
504 | | /* |
505 | | ** Return 'l <= r', for numbers. |
506 | | */ |
507 | 325k | l_sinline int LEnum (const TValue *l, const TValue *r) { |
508 | 325k | lua_assert(ttisnumber(l) && ttisnumber(r)); |
509 | 325k | if (ttisinteger(l)) { |
510 | 192k | lua_Integer li = ivalue(l); |
511 | 192k | if (ttisinteger(r)) |
512 | 192k | return li <= ivalue(r); /* both are integers */ |
513 | 192k | else /* 'l' is int and 'r' is float */ |
514 | 192k | return LEintfloat(li, fltvalue(r)); /* l <= r ? */ |
515 | 192k | } |
516 | 133k | else { |
517 | 133k | lua_Number lf = fltvalue(l); /* 'l' must be float */ |
518 | 133k | if (ttisfloat(r)) |
519 | 133k | return luai_numle(lf, fltvalue(r)); /* both are float */ |
520 | 108k | else /* 'l' is float and 'r' is int */ |
521 | 108k | return LEfloatint(lf, ivalue(r)); |
522 | 133k | } |
523 | 325k | } |
524 | | |
525 | | |
526 | | /* |
527 | | ** return 'l < r' for non-numbers. |
528 | | */ |
529 | 2.72M | static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) { |
530 | 2.72M | lua_assert(!ttisnumber(l) || !ttisnumber(r)); |
531 | 2.72M | if (ttisstring(l) && ttisstring(r)) /* both are strings? */ |
532 | 2.47M | return l_strcmp(tsvalue(l), tsvalue(r)) < 0; |
533 | 1.48M | else |
534 | 1.48M | return luaT_callorderTM(L, l, r, TM_LT); |
535 | 2.72M | } |
536 | | |
537 | | |
538 | | /* |
539 | | ** Main operation less than; return 'l < r'. |
540 | | */ |
541 | 1.17M | int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { |
542 | 1.17M | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ |
543 | 862 | return LTnum(l, r); |
544 | 1.17M | else return lessthanothers(L, l, r); |
545 | 1.17M | } |
546 | | |
547 | | |
548 | | /* |
549 | | ** return 'l <= r' for non-numbers. |
550 | | */ |
551 | 89.0k | static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) { |
552 | 89.0k | lua_assert(!ttisnumber(l) || !ttisnumber(r)); |
553 | 89.0k | if (ttisstring(l) && ttisstring(r)) /* both are strings? */ |
554 | 143k | return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; |
555 | 17.1k | else |
556 | 17.1k | return luaT_callorderTM(L, l, r, TM_LE); |
557 | 89.0k | } |
558 | | |
559 | | |
560 | | /* |
561 | | ** Main operation less than or equal to; return 'l <= r'. |
562 | | */ |
563 | 26 | int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { |
564 | 26 | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ |
565 | 26 | return LEnum(l, r); |
566 | 0 | else return lessequalothers(L, l, r); |
567 | 26 | } |
568 | | |
569 | | |
570 | | /* |
571 | | ** Main operation for equality of Lua values; return 't1 == t2'. |
572 | | ** L == NULL means raw equality (no metamethods) |
573 | | */ |
574 | 398M | int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { |
575 | 398M | const TValue *tm; |
576 | 398M | if (ttype(t1) != ttype(t2)) /* not the same type? */ |
577 | 37.6M | return 0; |
578 | 361M | else if (ttypetag(t1) != ttypetag(t2)) { |
579 | 32.8M | switch (ttypetag(t1)) { |
580 | 3.33M | case LUA_VNUMINT: { /* integer == float? */ |
581 | | /* integer and float can only be equal if float has an integer |
582 | | value equal to the integer */ |
583 | 3.33M | lua_Integer i2; |
584 | 3.33M | return (luaV_flttointeger(fltvalue(t2), &i2, F2Ieq) && |
585 | 3.33M | ivalue(t1) == i2); |
586 | 0 | } |
587 | 181k | case LUA_VNUMFLT: { /* float == integer? */ |
588 | 181k | lua_Integer i1; /* see comment in previous case */ |
589 | 181k | return (luaV_flttointeger(fltvalue(t1), &i1, F2Ieq) && |
590 | 181k | i1 == ivalue(t2)); |
591 | 0 | } |
592 | 14.9k | case LUA_VSHRSTR: case LUA_VLNGSTR: { |
593 | | /* compare two strings with different variants: they can be |
594 | | equal when one string is a short string and the other is |
595 | | an external string */ |
596 | 29.8k | return luaS_eqstr(tsvalue(t1), tsvalue(t2)); |
597 | 14.9k | } |
598 | 29.3M | default: |
599 | | /* only numbers (integer/float) and strings (long/short) can have |
600 | | equal values with different variants */ |
601 | 29.3M | return 0; |
602 | 32.8M | } |
603 | 32.8M | } |
604 | 328M | else { /* equal variants */ |
605 | 328M | switch (ttypetag(t1)) { |
606 | 1.37M | case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: |
607 | 1.37M | return 1; |
608 | 2.70M | case LUA_VNUMINT: |
609 | 2.70M | return (ivalue(t1) == ivalue(t2)); |
610 | 4.58M | case LUA_VNUMFLT: |
611 | 4.58M | return (fltvalue(t1) == fltvalue(t2)); |
612 | 0 | case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); |
613 | 243M | case LUA_VSHRSTR: |
614 | 975M | return eqshrstr(tsvalue(t1), tsvalue(t2)); |
615 | 2.06M | case LUA_VLNGSTR: |
616 | 4.13M | return luaS_eqstr(tsvalue(t1), tsvalue(t2)); |
617 | 37.8k | case LUA_VUSERDATA: { |
618 | 113k | if (uvalue(t1) == uvalue(t2)) return 1; |
619 | 2.54k | else if (L == NULL) return 0; |
620 | 2.54k | tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); |
621 | 2.54k | if (tm == NULL) |
622 | 2.54k | tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); |
623 | 2.54k | break; /* will try TM */ |
624 | 2.54k | } |
625 | 5.00M | case LUA_VTABLE: { |
626 | 15.0M | if (hvalue(t1) == hvalue(t2)) return 1; |
627 | 109k | else if (L == NULL) return 0; |
628 | 107k | tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); |
629 | 107k | if (tm == NULL) |
630 | 103k | tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); |
631 | 107k | break; /* will try TM */ |
632 | 107k | } |
633 | 68.2M | case LUA_VLCF: |
634 | 68.2M | return (fvalue(t1) == fvalue(t2)); |
635 | 349k | default: /* functions and threads */ |
636 | 349k | return (gcvalue(t1) == gcvalue(t2)); |
637 | 328M | } |
638 | 110k | if (tm == NULL) /* no TM? */ |
639 | 105k | return 0; /* objects are different */ |
640 | 4.56k | else { |
641 | 4.56k | int tag = luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ |
642 | 4.56k | return !tagisfalse(tag); |
643 | 4.56k | } |
644 | 110k | } |
645 | 398M | } |
646 | | |
647 | | |
648 | | /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ |
649 | | #define tostring(L,o) \ |
650 | 23.0M | (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) |
651 | | |
652 | 42.5M | #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) |
653 | | |
654 | | /* copy strings in stack from top - n up to top - 1 to buffer */ |
655 | 9.87M | static void copy2buff (StkId top, int n, char *buff) { |
656 | 9.87M | size_t tl = 0; /* size already copied */ |
657 | 21.1M | do { |
658 | 42.3M | TString *st = tsvalue(s2v(top - n)); |
659 | 0 | size_t l; /* length of string being copied */ |
660 | 42.3M | const char *s = getlstr(st, l); |
661 | 42.3M | memcpy(buff + tl, s, l * sizeof(char)); |
662 | 42.3M | tl += l; |
663 | 42.3M | } while (--n > 0); |
664 | 9.87M | } |
665 | | |
666 | | |
667 | | /* |
668 | | ** Main operation for concatenation: concat 'total' values in the stack, |
669 | | ** from 'L->top.p - total' up to 'L->top.p - 1'. |
670 | | */ |
671 | 12.6M | void luaV_concat (lua_State *L, int total) { |
672 | 12.6M | if (total == 1) |
673 | 7 | return; /* "all" values already concatenated */ |
674 | 12.7M | do { |
675 | 12.7M | StkId top = L->top.p; |
676 | 12.7M | int n = 2; /* number of elements handled in this pass (at least 2) */ |
677 | 12.7M | if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || |
678 | 12.7M | !tostring(L, s2v(top - 1))) |
679 | 1.50M | luaT_tryconcatTM(L); /* may invalidate 'top' */ |
680 | 11.2M | else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ |
681 | 11.2M | cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ |
682 | 11.2M | else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ |
683 | 1.34M | setobjs2s(L, top - 2, top - 1); /* result is second op. */ |
684 | 1.34M | } |
685 | 9.87M | else { |
686 | | /* at least two non-empty string values; get as many as possible */ |
687 | 9.87M | size_t tl = tsslen(tsvalue(s2v(top - 1))); |
688 | 9.87M | TString *ts; |
689 | | /* collect total length and number of strings */ |
690 | 21.1M | for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { |
691 | 11.3M | size_t l = tsslen(tsvalue(s2v(top - n - 1))); |
692 | 11.3M | if (l_unlikely(l >= MAX_SIZE - sizeof(TString) - tl)) { |
693 | 0 | L->top.p = top - total; /* pop strings to avoid wasting stack */ |
694 | 0 | luaG_runerror(L, "string length overflow"); |
695 | 0 | } |
696 | 11.3M | tl += l; |
697 | 11.3M | } |
698 | 9.87M | if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ |
699 | 7.74M | char buff[LUAI_MAXSHORTLEN]; |
700 | 7.74M | copy2buff(top, n, buff); /* copy strings to buffer */ |
701 | 7.74M | ts = luaS_newlstr(L, buff, tl); |
702 | 7.74M | } |
703 | 2.12M | else { /* long string; copy strings directly to final result */ |
704 | 2.12M | ts = luaS_createlngstrobj(L, tl); |
705 | 2.12M | copy2buff(top, n, getlngstr(ts)); |
706 | 2.12M | } |
707 | 19.7M | setsvalue2s(L, top - n, ts); /* create result */ |
708 | 9.87M | } |
709 | 12.7M | total -= n - 1; /* got 'n' strings to create one new */ |
710 | 12.7M | L->top.p -= n - 1; /* popped 'n' strings and pushed one */ |
711 | 12.7M | } while (total > 1); /* repeat until only 1 result left */ |
712 | 12.6M | } |
713 | | |
714 | | |
715 | | /* |
716 | | ** Main operation 'ra = #rb'. |
717 | | */ |
718 | 11.6M | void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { |
719 | 11.6M | const TValue *tm; |
720 | 11.6M | switch (ttypetag(rb)) { |
721 | 7.87M | case LUA_VTABLE: { |
722 | 15.7M | Table *h = hvalue(rb); |
723 | 7.87M | tm = fasttm(L, h->metatable, TM_LEN); |
724 | 15.7M | if (tm) break; /* metamethod? break switch to call it */ |
725 | 7.87M | setivalue(s2v(ra), l_castU2S(luaH_getn(L, h))); /* else primitive len */ |
726 | 7.87M | return; |
727 | 15.7M | } |
728 | 3.53M | case LUA_VSHRSTR: { |
729 | 3.53M | setivalue(s2v(ra), tsvalue(rb)->shrlen); |
730 | 3.53M | return; |
731 | 3.53M | } |
732 | 203k | case LUA_VLNGSTR: { |
733 | 203k | setivalue(s2v(ra), cast_st2S(tsvalue(rb)->u.lnglen)); |
734 | 203k | return; |
735 | 203k | } |
736 | 68.6k | default: { /* try metamethod */ |
737 | 68.6k | tm = luaT_gettmbyobj(L, rb, TM_LEN); |
738 | 68.6k | if (l_unlikely(notm(tm))) /* no metamethod? */ |
739 | 1.58k | luaG_typeerror(L, rb, "get length of"); |
740 | 67.1k | break; |
741 | 68.6k | } |
742 | 11.6M | } |
743 | 69.7k | luaT_callTMres(L, tm, rb, rb, ra); |
744 | 69.7k | } |
745 | | |
746 | | |
747 | | /* |
748 | | ** Integer division; return 'm // n', that is, floor(m/n). |
749 | | ** C division truncates its result (rounds towards zero). |
750 | | ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, |
751 | | ** otherwise 'floor(q) == trunc(q) - 1'. |
752 | | */ |
753 | 464k | lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { |
754 | 464k | if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ |
755 | 19.5k | if (n == 0) |
756 | 21 | luaG_runerror(L, "attempt to divide by zero"); |
757 | 19.5k | return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ |
758 | 19.5k | } |
759 | 444k | else { |
760 | 444k | lua_Integer q = m / n; /* perform C division */ |
761 | 444k | if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ |
762 | 59.8k | q -= 1; /* correct result for different rounding */ |
763 | 444k | return q; |
764 | 444k | } |
765 | 464k | } |
766 | | |
767 | | |
768 | | /* |
769 | | ** Integer modulus; return 'm % n'. (Assume that C '%' with |
770 | | ** negative operands follows C99 behavior. See previous comment |
771 | | ** about luaV_idiv.) |
772 | | */ |
773 | 802k | lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { |
774 | 802k | if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ |
775 | 17.5k | if (n == 0) |
776 | 3.40k | luaG_runerror(L, "attempt to perform 'n%%0'"); |
777 | 14.1k | return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ |
778 | 17.5k | } |
779 | 785k | else { |
780 | 785k | lua_Integer r = m % n; |
781 | 785k | if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */ |
782 | 82.9k | r += n; /* correct result for different rounding */ |
783 | 785k | return r; |
784 | 785k | } |
785 | 802k | } |
786 | | |
787 | | |
788 | | /* |
789 | | ** Float modulus |
790 | | */ |
791 | 5.05M | lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { |
792 | 5.05M | lua_Number r; |
793 | 5.05M | luai_nummod(L, m, n, r); |
794 | 5.05M | return r; |
795 | 5.05M | } |
796 | | |
797 | | |
798 | | /* number of bits in an integer */ |
799 | 1.47M | #define NBITS l_numbits(lua_Integer) |
800 | | |
801 | | |
802 | | /* |
803 | | ** Shift left operation. (Shift right just negates 'y'.) |
804 | | */ |
805 | 1.47M | lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { |
806 | 1.47M | if (y < 0) { /* shift right? */ |
807 | 356k | if (y <= -NBITS) return 0; |
808 | 218k | else return intop(>>, x, -y); |
809 | 356k | } |
810 | 1.11M | else { /* shift left */ |
811 | 1.11M | if (y >= NBITS) return 0; |
812 | 954k | else return intop(<<, x, y); |
813 | 1.11M | } |
814 | 1.47M | } |
815 | | |
816 | | |
817 | | /* |
818 | | ** create a new Lua closure, push it in the stack, and initialize |
819 | | ** its upvalues. |
820 | | */ |
821 | | static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, |
822 | 18.0M | StkId ra) { |
823 | 18.0M | int nup = p->sizeupvalues; |
824 | 18.0M | Upvaldesc *uv = p->upvalues; |
825 | 18.0M | int i; |
826 | 18.0M | LClosure *ncl = luaF_newLclosure(L, nup); |
827 | 18.0M | ncl->p = p; |
828 | 18.0M | setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */ |
829 | 33.8M | for (i = 0; i < nup; i++) { /* fill in its upvalues */ |
830 | 15.7M | if (uv[i].instack) /* upvalue refers to local variable? */ |
831 | 12.2M | ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); |
832 | 3.47M | else /* get upvalue from enclosing function */ |
833 | 3.47M | ncl->upvals[i] = encup[uv[i].idx]; |
834 | 15.7M | luaC_objbarrier(L, ncl, ncl->upvals[i]); |
835 | 15.7M | } |
836 | 18.0M | } |
837 | | |
838 | | |
839 | | /* |
840 | | ** finish execution of an opcode interrupted by a yield |
841 | | */ |
842 | 485 | void luaV_finishOp (lua_State *L) { |
843 | 485 | CallInfo *ci = L->ci; |
844 | 485 | StkId base = ci->func.p + 1; |
845 | 485 | Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ |
846 | 485 | OpCode op = GET_OPCODE(inst); |
847 | 485 | switch (op) { /* finish its execution */ |
848 | 3 | case OP_MMBIN: case OP_MMBINI: case OP_MMBINK: { |
849 | 3 | setobjs2s(L, base + GETARG_A(*(ci->u.l.savedpc - 2)), --L->top.p); |
850 | 3 | break; |
851 | 3 | } |
852 | 0 | case OP_UNM: case OP_BNOT: case OP_LEN: |
853 | 0 | case OP_GETTABUP: case OP_GETTABLE: case OP_GETI: |
854 | 0 | case OP_GETFIELD: case OP_SELF: { |
855 | 0 | setobjs2s(L, base + GETARG_A(inst), --L->top.p); |
856 | 0 | break; |
857 | 0 | } |
858 | 0 | case OP_LT: case OP_LE: |
859 | 0 | case OP_LTI: case OP_LEI: |
860 | 0 | case OP_GTI: case OP_GEI: |
861 | 0 | case OP_EQ: { /* note that 'OP_EQI'/'OP_EQK' cannot yield */ |
862 | 0 | int res = !l_isfalse(s2v(L->top.p - 1)); |
863 | 0 | L->top.p--; |
864 | 0 | lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); |
865 | 0 | if (res != GETARG_k(inst)) /* condition failed? */ |
866 | 0 | ci->u.l.savedpc++; /* skip jump instruction */ |
867 | 0 | break; |
868 | 0 | } |
869 | 0 | case OP_CONCAT: { |
870 | 0 | StkId top = L->top.p - 1; /* top when 'luaT_tryconcatTM' was called */ |
871 | 0 | int a = GETARG_A(inst); /* first element to concatenate */ |
872 | 0 | int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ |
873 | 0 | setobjs2s(L, top - 2, top); /* put TM result in proper position */ |
874 | 0 | L->top.p = top - 1; /* top is one after last element (at top-2) */ |
875 | 0 | luaV_concat(L, total); /* concat them (may yield again) */ |
876 | 0 | break; |
877 | 0 | } |
878 | 0 | case OP_CLOSE: { /* yielded closing variables */ |
879 | 0 | ci->u.l.savedpc--; /* repeat instruction to close other vars. */ |
880 | 0 | break; |
881 | 0 | } |
882 | 0 | case OP_RETURN: { /* yielded closing variables */ |
883 | 0 | StkId ra = base + GETARG_A(inst); |
884 | | /* adjust top to signal correct number of returns, in case the |
885 | | return is "up to top" ('isIT') */ |
886 | 0 | L->top.p = ra + ci->u2.nres; |
887 | | /* repeat instruction to close other vars. and complete the return */ |
888 | 0 | ci->u.l.savedpc--; |
889 | 0 | break; |
890 | 0 | } |
891 | 482 | default: { |
892 | | /* only these other opcodes can yield */ |
893 | 482 | lua_assert(op == OP_TFORCALL || op == OP_CALL || |
894 | 482 | op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE || |
895 | 482 | op == OP_SETI || op == OP_SETFIELD); |
896 | 482 | break; |
897 | 482 | } |
898 | 485 | } |
899 | 485 | } |
900 | | |
901 | | |
902 | | |
903 | | |
904 | | /* |
905 | | ** {================================================================== |
906 | | ** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' |
907 | | ** |
908 | | ** All these macros are to be used exclusively inside the main |
909 | | ** iterpreter loop (function luaV_execute) and may access directly |
910 | | ** the local variables of that function (L, i, pc, ci, etc.). |
911 | | ** =================================================================== |
912 | | */ |
913 | | |
914 | | #define l_addi(L,a,b) intop(+, a, b) |
915 | | #define l_subi(L,a,b) intop(-, a, b) |
916 | | #define l_muli(L,a,b) intop(*, a, b) |
917 | | #define l_band(a,b) intop(&, a, b) |
918 | | #define l_bor(a,b) intop(|, a, b) |
919 | | #define l_bxor(a,b) intop(^, a, b) |
920 | | |
921 | 2.13M | #define l_lti(a,b) (a < b) |
922 | 2.63M | #define l_lei(a,b) (a <= b) |
923 | 100M | #define l_gti(a,b) (a > b) |
924 | 322k | #define l_gei(a,b) (a >= b) |
925 | | |
926 | | |
927 | | /* |
928 | | ** Arithmetic operations with immediate operands. 'iop' is the integer |
929 | | ** operation, 'fop' is the float operation. |
930 | | */ |
931 | 50.8M | #define op_arithI(L,iop,fop) { \ |
932 | 50.8M | TValue *ra = vRA(i); \ |
933 | 50.8M | TValue *v1 = vRB(i); \ |
934 | 50.8M | int imm = GETARG_sC(i); \ |
935 | 50.8M | if (ttisinteger(v1)) { \ |
936 | 41.2M | lua_Integer iv1 = ivalue(v1); \ |
937 | 41.2M | pc++; setivalue(ra, iop(L, iv1, imm)); \ |
938 | 41.2M | } \ |
939 | 50.8M | else if (ttisfloat(v1)) { \ |
940 | 9.01M | lua_Number nb = fltvalue(v1); \ |
941 | 9.01M | lua_Number fimm = cast_num(imm); \ |
942 | 9.01M | pc++; setfltvalue(ra, fop(L, nb, fimm)); \ |
943 | 9.01M | }} |
944 | | |
945 | | |
946 | | /* |
947 | | ** Auxiliary function for arithmetic operations over floats and others |
948 | | ** with two operands. |
949 | | */ |
950 | 60.4M | #define op_arithf_aux(L,v1,v2,fop) { \ |
951 | 60.4M | lua_Number n1; lua_Number n2; \ |
952 | 60.4M | if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ |
953 | 54.7M | StkId ra = RA(i); \ |
954 | 54.7M | pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ |
955 | 54.7M | }} |
956 | | |
957 | | |
958 | | /* |
959 | | ** Arithmetic operations over floats and others with register operands. |
960 | | */ |
961 | 9.01M | #define op_arithf(L,fop) { \ |
962 | 9.01M | TValue *v1 = vRB(i); \ |
963 | 9.01M | TValue *v2 = vRC(i); \ |
964 | 9.01M | op_arithf_aux(L, v1, v2, fop); } |
965 | | |
966 | | |
967 | | /* |
968 | | ** Arithmetic operations with K operands for floats. |
969 | | */ |
970 | 21.2M | #define op_arithfK(L,fop) { \ |
971 | 21.2M | TValue *v1 = vRB(i); \ |
972 | 21.2M | TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ |
973 | 21.2M | op_arithf_aux(L, v1, v2, fop); } |
974 | | |
975 | | |
976 | | /* |
977 | | ** Arithmetic operations over integers and floats. |
978 | | */ |
979 | 34.7M | #define op_arith_aux(L,v1,v2,iop,fop) { \ |
980 | 34.7M | if (ttisinteger(v1) && ttisinteger(v2)) { \ |
981 | 4.54M | StkId ra = RA(i); \ |
982 | 4.54M | lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ |
983 | 4.54M | pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ |
984 | 4.54M | } \ |
985 | 34.7M | else op_arithf_aux(L, v1, v2, fop); } |
986 | | |
987 | | |
988 | | /* |
989 | | ** Arithmetic operations with register operands. |
990 | | */ |
991 | 14.8M | #define op_arith(L,iop,fop) { \ |
992 | 14.8M | TValue *v1 = vRB(i); \ |
993 | 14.8M | TValue *v2 = vRC(i); \ |
994 | 14.8M | op_arith_aux(L, v1, v2, iop, fop); } |
995 | | |
996 | | |
997 | | /* |
998 | | ** Arithmetic operations with K operands. |
999 | | */ |
1000 | 19.8M | #define op_arithK(L,iop,fop) { \ |
1001 | 19.8M | TValue *v1 = vRB(i); \ |
1002 | 19.8M | TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ |
1003 | 19.8M | op_arith_aux(L, v1, v2, iop, fop); } |
1004 | | |
1005 | | |
1006 | | /* |
1007 | | ** Bitwise operations with constant operand. |
1008 | | */ |
1009 | 10.2M | #define op_bitwiseK(L,op) { \ |
1010 | 10.2M | TValue *v1 = vRB(i); \ |
1011 | 10.2M | TValue *v2 = KC(i); \ |
1012 | 10.2M | lua_Integer i1; \ |
1013 | 10.2M | lua_Integer i2 = ivalue(v2); \ |
1014 | 10.2M | if (tointegerns(v1, &i1)) { \ |
1015 | 10.2M | StkId ra = RA(i); \ |
1016 | 10.2M | pc++; setivalue(s2v(ra), op(i1, i2)); \ |
1017 | 10.2M | }} |
1018 | | |
1019 | | |
1020 | | /* |
1021 | | ** Bitwise operations with register operands. |
1022 | | */ |
1023 | 3.51M | #define op_bitwise(L,op) { \ |
1024 | 3.51M | TValue *v1 = vRB(i); \ |
1025 | 3.51M | TValue *v2 = vRC(i); \ |
1026 | 3.51M | lua_Integer i1; lua_Integer i2; \ |
1027 | 3.51M | if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ |
1028 | 3.30M | StkId ra = RA(i); \ |
1029 | 3.30M | pc++; setivalue(s2v(ra), op(i1, i2)); \ |
1030 | 3.30M | }} |
1031 | | |
1032 | | |
1033 | | /* |
1034 | | ** Order operations with register operands. 'opn' actually works |
1035 | | ** for all numbers, but the fast track improves performance for |
1036 | | ** integers. |
1037 | | */ |
1038 | 13.0M | #define op_order(L,opi,opn,other) { \ |
1039 | 13.0M | TValue *ra = vRA(i); \ |
1040 | 13.0M | int cond; \ |
1041 | 13.0M | TValue *rb = vRB(i); \ |
1042 | 13.0M | if (ttisinteger(ra) && ttisinteger(rb)) { \ |
1043 | 4.21M | lua_Integer ia = ivalue(ra); \ |
1044 | 4.21M | lua_Integer ib = ivalue(rb); \ |
1045 | 4.21M | cond = opi(ia, ib); \ |
1046 | 4.21M | } \ |
1047 | 13.0M | else if (ttisnumber(ra) && ttisnumber(rb)) \ |
1048 | 8.81M | cond = opn(ra, rb); \ |
1049 | 8.81M | else \ |
1050 | 8.81M | Protect(cond = other(L, ra, rb)); \ |
1051 | 13.0M | docondjump(); } |
1052 | | |
1053 | | |
1054 | | /* |
1055 | | ** Order operations with immediate operand. (Immediate operand is |
1056 | | ** always small enough to have an exact representation as a float.) |
1057 | | */ |
1058 | 30.4M | #define op_orderI(L,opi,opf,inv,tm) { \ |
1059 | 30.4M | TValue *ra = vRA(i); \ |
1060 | 30.4M | int cond; \ |
1061 | 30.4M | int im = GETARG_sB(i); \ |
1062 | 30.4M | if (ttisinteger(ra)) \ |
1063 | 30.4M | cond = opi(ivalue(ra), im); \ |
1064 | 30.4M | else if (ttisfloat(ra)) { \ |
1065 | 4.08M | lua_Number fa = fltvalue(ra); \ |
1066 | 4.08M | lua_Number fim = cast_num(im); \ |
1067 | 4.08M | cond = opf(fa, fim); \ |
1068 | 4.08M | } \ |
1069 | 4.60M | else { \ |
1070 | 511k | int isf = GETARG_C(i); \ |
1071 | 511k | Protect(cond = luaT_callorderiTM(L, ra, im, inv, isf, tm)); \ |
1072 | 511k | } \ |
1073 | 30.4M | docondjump(); } |
1074 | | |
1075 | | /* }================================================================== */ |
1076 | | |
1077 | | |
1078 | | /* |
1079 | | ** {================================================================== |
1080 | | ** Function 'luaV_execute': main interpreter loop |
1081 | | ** =================================================================== |
1082 | | */ |
1083 | | |
1084 | | /* |
1085 | | ** some macros for common tasks in 'luaV_execute' |
1086 | | */ |
1087 | | |
1088 | | |
1089 | 2.26G | #define RA(i) (base+GETARG_A(i)) |
1090 | 94.2M | #define vRA(i) s2v(RA(i)) |
1091 | | #define RB(i) (base+GETARG_B(i)) |
1092 | 286M | #define vRB(i) s2v(RB(i)) |
1093 | 216M | #define KB(i) (k+GETARG_B(i)) |
1094 | | #define RC(i) (base+GETARG_C(i)) |
1095 | 32.3M | #define vRC(i) s2v(RC(i)) |
1096 | 494M | #define KC(i) (k+GETARG_C(i)) |
1097 | 111M | #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i))) |
1098 | | |
1099 | | |
1100 | | |
1101 | 685M | #define updatetrap(ci) (trap = ci->u.l.trap) |
1102 | | |
1103 | 98.7M | #define updatebase(ci) (base = ci->func.p + 1) |
1104 | | |
1105 | | |
1106 | | #define updatestack(ci) \ |
1107 | 593k | { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } } |
1108 | | |
1109 | | |
1110 | | /* |
1111 | | ** Execute a jump instruction. The 'updatetrap' allows signals to stop |
1112 | | ** tight loops. (Without it, the local copy of 'trap' could never change.) |
1113 | | */ |
1114 | 169M | #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); } |
1115 | | |
1116 | | |
1117 | | /* for test instructions, execute the jump instruction that follows it */ |
1118 | 166M | #define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); } |
1119 | | |
1120 | | /* |
1121 | | ** do a conditional jump: skip next instruction if 'cond' is not what |
1122 | | ** was expected (parameter 'k'), else do next instruction, which must |
1123 | | ** be a jump. |
1124 | | */ |
1125 | 261M | #define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci); |
1126 | | |
1127 | | |
1128 | | /* |
1129 | | ** Correct global 'pc'. |
1130 | | */ |
1131 | 693M | #define savepc(ci) (ci->u.l.savedpc = pc) |
1132 | | |
1133 | | |
1134 | | /* |
1135 | | ** Whenever code can raise errors, the global 'pc' and the global |
1136 | | ** 'top' must be correct to report occasional errors. |
1137 | | */ |
1138 | 255M | #define savestate(L,ci) (savepc(ci), L->top.p = ci->top.p) |
1139 | | |
1140 | | |
1141 | | /* |
1142 | | ** Protect code that, in general, can raise errors, reallocate the |
1143 | | ** stack, and change the hooks. |
1144 | | */ |
1145 | 220M | #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) |
1146 | | |
1147 | | /* special version that does not change the top */ |
1148 | 24.6M | #define ProtectNT(exp) (savepc(ci), (exp), updatetrap(ci)) |
1149 | | |
1150 | | /* |
1151 | | ** Protect code that can only raise errors. (That is, it cannot change |
1152 | | ** the stack or hooks.) |
1153 | | */ |
1154 | 18.2M | #define halfProtect(exp) (savestate(L,ci), (exp)) |
1155 | | |
1156 | | /* |
1157 | | ** macro executed during Lua functions at points where the |
1158 | | ** function can yield. |
1159 | | */ |
1160 | | #if !defined(luai_threadyield) |
1161 | 48.0M | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} |
1162 | | #endif |
1163 | | |
1164 | | /* 'c' is the limit of live values in the stack */ |
1165 | | #define checkGC(L,c) \ |
1166 | 48.0M | { luaC_condGC(L, (savepc(ci), L->top.p = (c)), \ |
1167 | 48.0M | updatetrap(ci)); \ |
1168 | 48.0M | luai_threadyield(L); } |
1169 | | |
1170 | | |
1171 | | /* fetch an instruction and prepare its execution */ |
1172 | 2.46G | #define vmfetch() { \ |
1173 | 2.46G | if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \ |
1174 | 84.0M | trap = luaG_traceexec(L, pc); /* handle hooks */ \ |
1175 | 84.0M | updatebase(ci); /* correct stack */ \ |
1176 | 84.0M | } \ |
1177 | 2.46G | i = *(pc++); \ |
1178 | 2.46G | } |
1179 | | |
1180 | | #define vmdispatch(o) switch(o) |
1181 | | #define vmcase(l) case l: |
1182 | | #define vmbreak break |
1183 | | |
1184 | | |
1185 | 122M | void luaV_execute (lua_State *L, CallInfo *ci) { |
1186 | 122M | LClosure *cl; |
1187 | 122M | TValue *k; |
1188 | 122M | StkId base; |
1189 | 122M | const Instruction *pc; |
1190 | 122M | int trap; |
1191 | 122M | #if LUA_USE_JUMPTABLE |
1192 | 122M | #include "ljumptab.h" |
1193 | 122M | #endif |
1194 | 288M | startfunc: |
1195 | 288M | trap = L->hookmask; |
1196 | 338M | returning: /* trap already set */ |
1197 | 676M | cl = ci_func(ci); |
1198 | 0 | k = cl->p->k; |
1199 | 676M | pc = ci->u.l.savedpc; |
1200 | 676M | if (l_unlikely(trap)) |
1201 | 5.32M | trap = luaG_tracecall(L); |
1202 | 676M | base = ci->func.p + 1; |
1203 | | /* main loop of interpreter */ |
1204 | 676M | for (;;) { |
1205 | 338M | Instruction i; /* instruction being executed */ |
1206 | 338M | vmfetch(); |
1207 | | #if 0 |
1208 | | { /* low-level line tracing for debugging Lua */ |
1209 | | #include "lopnames.h" |
1210 | | int pcrel = pcRel(pc, cl->p); |
1211 | | printf("line: %d; %s (%d)\n", luaG_getfuncline(cl->p, pcrel), |
1212 | | opnames[GET_OPCODE(i)], pcrel); |
1213 | | } |
1214 | | #endif |
1215 | 338M | lua_assert(base == ci->func.p + 1); |
1216 | 338M | lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); |
1217 | | /* for tests, invalidate top for instructions not expecting it */ |
1218 | 338M | lua_assert(luaP_isIT(i) || (cast_void(L->top.p = base), 1)); |
1219 | 338M | vmdispatch (GET_OPCODE(i)) { |
1220 | 373M | vmcase(OP_MOVE) { |
1221 | 373M | StkId ra = RA(i); |
1222 | 746M | setobjs2s(L, ra, RB(i)); |
1223 | 373M | vmbreak; |
1224 | 373M | } |
1225 | 47.9M | vmcase(OP_LOADI) { |
1226 | 47.9M | StkId ra = RA(i); |
1227 | 47.9M | lua_Integer b = GETARG_sBx(i); |
1228 | 47.9M | setivalue(s2v(ra), b); |
1229 | 47.9M | vmbreak; |
1230 | 47.9M | } |
1231 | 6.39M | vmcase(OP_LOADF) { |
1232 | 6.39M | StkId ra = RA(i); |
1233 | 6.39M | int b = GETARG_sBx(i); |
1234 | 6.39M | setfltvalue(s2v(ra), cast_num(b)); |
1235 | 6.39M | vmbreak; |
1236 | 6.39M | } |
1237 | 85.9M | vmcase(OP_LOADK) { |
1238 | 85.9M | StkId ra = RA(i); |
1239 | 85.9M | TValue *rb = k + GETARG_Bx(i); |
1240 | 85.9M | setobj2s(L, ra, rb); |
1241 | 85.9M | vmbreak; |
1242 | 85.9M | } |
1243 | 0 | vmcase(OP_LOADKX) { |
1244 | 0 | StkId ra = RA(i); |
1245 | 0 | TValue *rb; |
1246 | 0 | rb = k + GETARG_Ax(*pc); pc++; |
1247 | 0 | setobj2s(L, ra, rb); |
1248 | 0 | vmbreak; |
1249 | 0 | } |
1250 | 3.41M | vmcase(OP_LOADFALSE) { |
1251 | 3.41M | StkId ra = RA(i); |
1252 | 3.41M | setbfvalue(s2v(ra)); |
1253 | 3.41M | vmbreak; |
1254 | 3.41M | } |
1255 | 4.55M | vmcase(OP_LFALSESKIP) { |
1256 | 4.55M | StkId ra = RA(i); |
1257 | 4.55M | setbfvalue(s2v(ra)); |
1258 | 4.55M | pc++; /* skip next instruction */ |
1259 | 4.55M | vmbreak; |
1260 | 4.55M | } |
1261 | 17.9M | vmcase(OP_LOADTRUE) { |
1262 | 17.9M | StkId ra = RA(i); |
1263 | 17.9M | setbtvalue(s2v(ra)); |
1264 | 17.9M | vmbreak; |
1265 | 17.9M | } |
1266 | 17.9M | vmcase(OP_LOADNIL) { |
1267 | 6.53M | StkId ra = RA(i); |
1268 | 6.53M | int b = GETARG_B(i); |
1269 | 38.5M | do { |
1270 | 38.5M | setnilvalue(s2v(ra++)); |
1271 | 38.5M | } while (b--); |
1272 | 6.53M | vmbreak; |
1273 | 6.53M | } |
1274 | 242M | vmcase(OP_GETUPVAL) { |
1275 | 242M | StkId ra = RA(i); |
1276 | 242M | int b = GETARG_B(i); |
1277 | 242M | setobj2s(L, ra, cl->upvals[b]->v.p); |
1278 | 242M | vmbreak; |
1279 | 242M | } |
1280 | 6.46M | vmcase(OP_SETUPVAL) { |
1281 | 6.46M | StkId ra = RA(i); |
1282 | 6.46M | UpVal *uv = cl->upvals[GETARG_B(i)]; |
1283 | 6.46M | setobj(L, uv->v.p, s2v(ra)); |
1284 | 6.46M | luaC_barrier(L, uv, s2v(ra)); |
1285 | 6.46M | vmbreak; |
1286 | 6.46M | } |
1287 | 341M | vmcase(OP_GETTABUP) { |
1288 | 341M | StkId ra = RA(i); |
1289 | 341M | TValue *upval = cl->upvals[GETARG_B(i)]->v.p; |
1290 | 341M | TValue *rc = KC(i); |
1291 | 682M | TString *key = tsvalue(rc); /* key must be a short string */ |
1292 | 0 | lu_byte tag; |
1293 | 682M | luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag); |
1294 | 341M | if (tagisempty(tag)) |
1295 | 71.9M | Protect(luaV_finishget(L, upval, rc, ra, tag)); |
1296 | 341M | vmbreak; |
1297 | 341M | } |
1298 | 4.94M | vmcase(OP_GETTABLE) { |
1299 | 4.94M | StkId ra = RA(i); |
1300 | 4.94M | TValue *rb = vRB(i); |
1301 | 4.94M | TValue *rc = vRC(i); |
1302 | 0 | lu_byte tag; |
1303 | 4.94M | if (ttisinteger(rc)) { /* fast track for integers? */ |
1304 | 1.35M | luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag); |
1305 | 1.35M | } |
1306 | 3.59M | else |
1307 | 3.59M | luaV_fastget(rb, rc, s2v(ra), luaH_get, tag); |
1308 | 4.94M | if (tagisempty(tag)) |
1309 | 2.17M | Protect(luaV_finishget(L, rb, rc, ra, tag)); |
1310 | 4.94M | vmbreak; |
1311 | 4.94M | } |
1312 | 540k | vmcase(OP_GETI) { |
1313 | 540k | StkId ra = RA(i); |
1314 | 540k | TValue *rb = vRB(i); |
1315 | 540k | int c = GETARG_C(i); |
1316 | 0 | lu_byte tag; |
1317 | 540k | luaV_fastgeti(rb, c, s2v(ra), tag); |
1318 | 540k | if (tagisempty(tag)) { |
1319 | 138k | TValue key; |
1320 | 138k | setivalue(&key, c); |
1321 | 138k | Protect(luaV_finishget(L, rb, &key, ra, tag)); |
1322 | 138k | } |
1323 | 540k | vmbreak; |
1324 | 540k | } |
1325 | 90.7M | vmcase(OP_GETFIELD) { |
1326 | 90.7M | StkId ra = RA(i); |
1327 | 90.7M | TValue *rb = vRB(i); |
1328 | 90.7M | TValue *rc = KC(i); |
1329 | 181M | TString *key = tsvalue(rc); /* key must be a short string */ |
1330 | 0 | lu_byte tag; |
1331 | 181M | luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); |
1332 | 90.7M | if (tagisempty(tag)) |
1333 | 67.4M | Protect(luaV_finishget(L, rb, rc, ra, tag)); |
1334 | 90.7M | vmbreak; |
1335 | 90.7M | } |
1336 | 50.8M | vmcase(OP_SETTABUP) { |
1337 | 50.8M | int hres; |
1338 | 50.8M | TValue *upval = cl->upvals[GETARG_A(i)]->v.p; |
1339 | 50.8M | TValue *rb = KB(i); |
1340 | 50.8M | TValue *rc = RKC(i); |
1341 | 101M | TString *key = tsvalue(rb); /* key must be a short string */ |
1342 | 50.8M | luaV_fastset(upval, key, rc, hres, luaH_psetshortstr); |
1343 | 50.8M | if (hres == HOK) |
1344 | 50.8M | luaV_finishfastset(L, upval, rc); |
1345 | 2.01M | else |
1346 | 2.01M | Protect(luaV_finishset(L, upval, rb, rc, hres)); |
1347 | 50.8M | vmbreak; |
1348 | 50.8M | } |
1349 | 14.8M | vmcase(OP_SETTABLE) { |
1350 | 14.8M | StkId ra = RA(i); |
1351 | 14.8M | int hres; |
1352 | 14.8M | TValue *rb = vRB(i); /* key (table is in 'ra') */ |
1353 | 14.8M | TValue *rc = RKC(i); /* value */ |
1354 | 14.8M | if (ttisinteger(rb)) { /* fast track for integers? */ |
1355 | 10.5M | luaV_fastseti(s2v(ra), ivalue(rb), rc, hres); |
1356 | 10.5M | } |
1357 | 4.27M | else { |
1358 | 4.27M | luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset); |
1359 | 4.27M | } |
1360 | 14.8M | if (hres == HOK) |
1361 | 14.8M | luaV_finishfastset(L, s2v(ra), rc); |
1362 | 5.93M | else |
1363 | 5.93M | Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); |
1364 | 14.8M | vmbreak; |
1365 | 14.8M | } |
1366 | 1.73M | vmcase(OP_SETI) { |
1367 | 1.73M | StkId ra = RA(i); |
1368 | 1.73M | int hres; |
1369 | 1.73M | int b = GETARG_B(i); |
1370 | 1.73M | TValue *rc = RKC(i); |
1371 | 1.73M | luaV_fastseti(s2v(ra), b, rc, hres); |
1372 | 1.73M | if (hres == HOK) |
1373 | 1.73M | luaV_finishfastset(L, s2v(ra), rc); |
1374 | 236k | else { |
1375 | 236k | TValue key; |
1376 | 236k | setivalue(&key, b); |
1377 | 236k | Protect(luaV_finishset(L, s2v(ra), &key, rc, hres)); |
1378 | 236k | } |
1379 | 1.73M | vmbreak; |
1380 | 1.73M | } |
1381 | 43.7M | vmcase(OP_SETFIELD) { |
1382 | 43.7M | StkId ra = RA(i); |
1383 | 43.7M | int hres; |
1384 | 43.7M | TValue *rb = KB(i); |
1385 | 43.7M | TValue *rc = RKC(i); |
1386 | 87.5M | TString *key = tsvalue(rb); /* key must be a short string */ |
1387 | 43.7M | luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr); |
1388 | 43.7M | if (hres == HOK) |
1389 | 43.7M | luaV_finishfastset(L, s2v(ra), rc); |
1390 | 28.1M | else |
1391 | 28.1M | Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); |
1392 | 43.7M | vmbreak; |
1393 | 43.7M | } |
1394 | 20.2M | vmcase(OP_NEWTABLE) { |
1395 | 20.2M | StkId ra = RA(i); |
1396 | 20.2M | unsigned b = cast_uint(GETARG_vB(i)); /* log2(hash size) + 1 */ |
1397 | 20.2M | unsigned c = cast_uint(GETARG_vC(i)); /* array size */ |
1398 | 0 | Table *t; |
1399 | 20.2M | if (b > 0) |
1400 | 5.60M | b = 1u << (b - 1); /* hash size is 2^(b - 1) */ |
1401 | 20.2M | if (TESTARG_k(i)) { /* non-zero extra argument? */ |
1402 | 25.5k | lua_assert(GETARG_Ax(*pc) != 0); |
1403 | | /* add it to array size */ |
1404 | 25.5k | c += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); |
1405 | 25.5k | } |
1406 | 20.2M | pc++; /* skip extra argument */ |
1407 | 20.2M | L->top.p = ra + 1; /* correct top in case of emergency GC */ |
1408 | 20.2M | t = luaH_new(L); /* memory allocation */ |
1409 | 20.2M | sethvalue2s(L, ra, t); |
1410 | 20.2M | if (b != 0 || c != 0) |
1411 | 6.83M | luaH_resize(L, t, c, b); /* idem */ |
1412 | 20.2M | checkGC(L, ra + 1); |
1413 | 20.2M | vmbreak; |
1414 | 20.2M | } |
1415 | 10.9M | vmcase(OP_SELF) { |
1416 | 10.9M | StkId ra = RA(i); |
1417 | 10.9M | lu_byte tag; |
1418 | 10.9M | TValue *rb = vRB(i); |
1419 | 10.9M | TValue *rc = KC(i); |
1420 | 21.8M | TString *key = tsvalue(rc); /* key must be a short string */ |
1421 | 10.9M | setobj2s(L, ra + 1, rb); |
1422 | 10.9M | luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); |
1423 | 10.9M | if (tagisempty(tag)) |
1424 | 10.5M | Protect(luaV_finishget(L, rb, rc, ra, tag)); |
1425 | 10.9M | vmbreak; |
1426 | 10.9M | } |
1427 | 50.8M | vmcase(OP_ADDI) { |
1428 | 101M | op_arithI(L, l_addi, luai_numadd); |
1429 | 50.8M | vmbreak; |
1430 | 50.8M | } |
1431 | 6.04M | vmcase(OP_ADDK) { |
1432 | 24.1M | op_arithK(L, l_addi, luai_numadd); |
1433 | 24.1M | vmbreak; |
1434 | 24.1M | } |
1435 | 2.40M | vmcase(OP_SUBK) { |
1436 | 9.60M | op_arithK(L, l_subi, luai_numsub); |
1437 | 9.60M | vmbreak; |
1438 | 9.60M | } |
1439 | 411k | vmcase(OP_MULK) { |
1440 | 1.64M | op_arithK(L, l_muli, luai_nummul); |
1441 | 1.64M | vmbreak; |
1442 | 1.64M | } |
1443 | 4.67M | vmcase(OP_MODK) { |
1444 | 4.67M | savestate(L, ci); /* in case of division by 0 */ |
1445 | 18.6M | op_arithK(L, luaV_mod, luaV_modf); |
1446 | 18.6M | vmbreak; |
1447 | 18.6M | } |
1448 | 7.08M | vmcase(OP_POWK) { |
1449 | 21.2M | op_arithfK(L, luai_numpow); |
1450 | 21.2M | vmbreak; |
1451 | 21.2M | } |
1452 | 14.2M | vmcase(OP_DIVK) { |
1453 | 42.6M | op_arithfK(L, luai_numdiv); |
1454 | 42.6M | vmbreak; |
1455 | 42.6M | } |
1456 | 6.30M | vmcase(OP_IDIVK) { |
1457 | 6.30M | savestate(L, ci); /* in case of division by 0 */ |
1458 | 25.2M | op_arithK(L, luaV_idiv, luai_numidiv); |
1459 | 25.2M | vmbreak; |
1460 | 25.2M | } |
1461 | 142k | vmcase(OP_BANDK) { |
1462 | 427k | op_bitwiseK(L, l_band); |
1463 | 427k | vmbreak; |
1464 | 427k | } |
1465 | 650k | vmcase(OP_BORK) { |
1466 | 1.95M | op_bitwiseK(L, l_bor); |
1467 | 1.95M | vmbreak; |
1468 | 1.95M | } |
1469 | 9.48M | vmcase(OP_BXORK) { |
1470 | 28.4M | op_bitwiseK(L, l_bxor); |
1471 | 28.4M | vmbreak; |
1472 | 28.4M | } |
1473 | 191k | vmcase(OP_SHLI) { |
1474 | 191k | StkId ra = RA(i); |
1475 | 191k | TValue *rb = vRB(i); |
1476 | 191k | int ic = GETARG_sC(i); |
1477 | 0 | lua_Integer ib; |
1478 | 191k | if (tointegerns(rb, &ib)) { |
1479 | 182k | pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); |
1480 | 182k | } |
1481 | 191k | vmbreak; |
1482 | 191k | } |
1483 | 39.6k | vmcase(OP_SHRI) { |
1484 | 39.6k | StkId ra = RA(i); |
1485 | 39.6k | TValue *rb = vRB(i); |
1486 | 39.6k | int ic = GETARG_sC(i); |
1487 | 0 | lua_Integer ib; |
1488 | 39.6k | if (tointegerns(rb, &ib)) { |
1489 | 27.0k | pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); |
1490 | 27.0k | } |
1491 | 39.6k | vmbreak; |
1492 | 39.6k | } |
1493 | 6.41M | vmcase(OP_ADD) { |
1494 | 19.2M | op_arith(L, l_addi, luai_numadd); |
1495 | 19.2M | vmbreak; |
1496 | 19.2M | } |
1497 | 4.71M | vmcase(OP_SUB) { |
1498 | 14.1M | op_arith(L, l_subi, luai_numsub); |
1499 | 14.1M | vmbreak; |
1500 | 14.1M | } |
1501 | 1.83M | vmcase(OP_MUL) { |
1502 | 5.49M | op_arith(L, l_muli, luai_nummul); |
1503 | 5.49M | vmbreak; |
1504 | 5.49M | } |
1505 | 1.72M | vmcase(OP_MOD) { |
1506 | 1.72M | savestate(L, ci); /* in case of division by 0 */ |
1507 | 5.16M | op_arith(L, luaV_mod, luaV_modf); |
1508 | 5.16M | vmbreak; |
1509 | 5.16M | } |
1510 | 6.02M | vmcase(OP_POW) { |
1511 | 12.0M | op_arithf(L, luai_numpow); |
1512 | 12.0M | vmbreak; |
1513 | 12.0M | } |
1514 | 2.98M | vmcase(OP_DIV) { /* float division (always with floats) */ |
1515 | 5.97M | op_arithf(L, luai_numdiv); |
1516 | 5.97M | vmbreak; |
1517 | 5.97M | } |
1518 | 195k | vmcase(OP_IDIV) { /* floor division */ |
1519 | 195k | savestate(L, ci); /* in case of division by 0 */ |
1520 | 586k | op_arith(L, luaV_idiv, luai_numidiv); |
1521 | 586k | vmbreak; |
1522 | 586k | } |
1523 | 192k | vmcase(OP_BAND) { |
1524 | 384k | op_bitwise(L, l_band); |
1525 | 384k | vmbreak; |
1526 | 384k | } |
1527 | 1.45M | vmcase(OP_BOR) { |
1528 | 2.90M | op_bitwise(L, l_bor); |
1529 | 2.90M | vmbreak; |
1530 | 2.90M | } |
1531 | 645k | vmcase(OP_BXOR) { |
1532 | 1.29M | op_bitwise(L, l_bxor); |
1533 | 1.29M | vmbreak; |
1534 | 1.29M | } |
1535 | 198k | vmcase(OP_SHL) { |
1536 | 396k | op_bitwise(L, luaV_shiftl); |
1537 | 396k | vmbreak; |
1538 | 396k | } |
1539 | 1.02M | vmcase(OP_SHR) { |
1540 | 2.04M | op_bitwise(L, luaV_shiftr); |
1541 | 2.04M | vmbreak; |
1542 | 2.04M | } |
1543 | 5.31M | vmcase(OP_MMBIN) { |
1544 | 5.31M | StkId ra = RA(i); |
1545 | 5.31M | Instruction pi = *(pc - 2); /* original arith. expression */ |
1546 | 5.31M | TValue *rb = vRB(i); |
1547 | 5.31M | TMS tm = (TMS)GETARG_C(i); |
1548 | 5.31M | StkId result = RA(pi); |
1549 | 5.31M | lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR); |
1550 | 5.31M | Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm)); |
1551 | 5.31M | vmbreak; |
1552 | 5.31M | } |
1553 | 557k | vmcase(OP_MMBINI) { |
1554 | 557k | StkId ra = RA(i); |
1555 | 557k | Instruction pi = *(pc - 2); /* original arith. expression */ |
1556 | 557k | int imm = GETARG_sB(i); |
1557 | 557k | TMS tm = (TMS)GETARG_C(i); |
1558 | 557k | int flip = GETARG_k(i); |
1559 | 557k | StkId result = RA(pi); |
1560 | 557k | Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); |
1561 | 557k | vmbreak; |
1562 | 557k | } |
1563 | 591k | vmcase(OP_MMBINK) { |
1564 | 591k | StkId ra = RA(i); |
1565 | 591k | Instruction pi = *(pc - 2); /* original arith. expression */ |
1566 | 591k | TValue *imm = KB(i); |
1567 | 591k | TMS tm = (TMS)GETARG_C(i); |
1568 | 591k | int flip = GETARG_k(i); |
1569 | 591k | StkId result = RA(pi); |
1570 | 591k | Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); |
1571 | 591k | vmbreak; |
1572 | 591k | } |
1573 | 3.68M | vmcase(OP_UNM) { |
1574 | 3.68M | StkId ra = RA(i); |
1575 | 3.68M | TValue *rb = vRB(i); |
1576 | 0 | lua_Number nb; |
1577 | 3.68M | if (ttisinteger(rb)) { |
1578 | 842k | lua_Integer ib = ivalue(rb); |
1579 | 842k | setivalue(s2v(ra), intop(-, 0, ib)); |
1580 | 842k | } |
1581 | 2.84M | else if (tonumberns(rb, nb)) { |
1582 | 2.56M | setfltvalue(s2v(ra), luai_numunm(L, nb)); |
1583 | 2.56M | } |
1584 | 282k | else |
1585 | 282k | Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); |
1586 | 3.68M | vmbreak; |
1587 | 3.68M | } |
1588 | 7.14M | vmcase(OP_BNOT) { |
1589 | 7.14M | StkId ra = RA(i); |
1590 | 7.14M | TValue *rb = vRB(i); |
1591 | 0 | lua_Integer ib; |
1592 | 7.14M | if (tointegerns(rb, &ib)) { |
1593 | 7.11M | setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); |
1594 | 7.11M | } |
1595 | 30.3k | else |
1596 | 30.3k | Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); |
1597 | 7.14M | vmbreak; |
1598 | 7.14M | } |
1599 | 373k | vmcase(OP_NOT) { |
1600 | 373k | StkId ra = RA(i); |
1601 | 373k | TValue *rb = vRB(i); |
1602 | 373k | if (l_isfalse(rb)) |
1603 | 373k | setbtvalue(s2v(ra)); |
1604 | 207k | else |
1605 | 373k | setbfvalue(s2v(ra)); |
1606 | 373k | vmbreak; |
1607 | 373k | } |
1608 | 11.5M | vmcase(OP_LEN) { |
1609 | 11.5M | StkId ra = RA(i); |
1610 | 11.5M | Protect(luaV_objlen(L, ra, vRB(i))); |
1611 | 11.5M | vmbreak; |
1612 | 11.5M | } |
1613 | 9.63M | vmcase(OP_CONCAT) { |
1614 | 9.63M | StkId ra = RA(i); |
1615 | 9.63M | int n = GETARG_B(i); /* number of elements to concatenate */ |
1616 | 0 | L->top.p = ra + n; /* mark the end of concat operands */ |
1617 | 9.63M | ProtectNT(luaV_concat(L, n)); |
1618 | 9.63M | checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */ |
1619 | 9.63M | vmbreak; |
1620 | 9.63M | } |
1621 | 4.50M | vmcase(OP_CLOSE) { |
1622 | 4.50M | StkId ra = RA(i); |
1623 | 4.50M | lua_assert(!GETARG_B(i)); /* 'close must be alive */ |
1624 | 4.50M | Protect(luaF_close(L, ra, LUA_OK, 1)); |
1625 | 4.50M | vmbreak; |
1626 | 4.50M | } |
1627 | 2.61k | vmcase(OP_TBC) { |
1628 | 2.61k | StkId ra = RA(i); |
1629 | | /* create new to-be-closed upvalue */ |
1630 | 2.61k | halfProtect(luaF_newtbcupval(L, ra)); |
1631 | 2.61k | vmbreak; |
1632 | 2.61k | } |
1633 | 2.92M | vmcase(OP_JMP) { |
1634 | 2.92M | dojump(ci, i, 0); |
1635 | 2.92M | vmbreak; |
1636 | 2.92M | } |
1637 | 4.07M | vmcase(OP_EQ) { |
1638 | 4.07M | StkId ra = RA(i); |
1639 | 4.07M | int cond; |
1640 | 4.07M | TValue *rb = vRB(i); |
1641 | 4.07M | Protect(cond = luaV_equalobj(L, s2v(ra), rb)); |
1642 | 4.07M | docondjump(); |
1643 | 4.07M | vmbreak; |
1644 | 4.07M | } |
1645 | 10.1M | vmcase(OP_LT) { |
1646 | 30.3M | op_order(L, l_lti, LTnum, lessthanothers); |
1647 | 30.3M | vmbreak; |
1648 | 30.3M | } |
1649 | 2.91M | vmcase(OP_LE) { |
1650 | 8.74M | op_order(L, l_lei, LEnum, lessequalothers); |
1651 | 8.74M | vmbreak; |
1652 | 8.74M | } |
1653 | 121M | vmcase(OP_EQK) { |
1654 | 121M | StkId ra = RA(i); |
1655 | 121M | TValue *rb = KB(i); |
1656 | | /* basic types do not use '__eq'; we can use raw equality */ |
1657 | 121M | int cond = luaV_rawequalobj(s2v(ra), rb); |
1658 | 121M | docondjump(); |
1659 | 121M | vmbreak; |
1660 | 121M | } |
1661 | 508k | vmcase(OP_EQI) { |
1662 | 508k | StkId ra = RA(i); |
1663 | 508k | int cond; |
1664 | 508k | int im = GETARG_sB(i); |
1665 | 508k | if (ttisinteger(s2v(ra))) |
1666 | 402k | cond = (ivalue(s2v(ra)) == im); |
1667 | 106k | else if (ttisfloat(s2v(ra))) |
1668 | 77.7k | cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im)); |
1669 | 28.6k | else |
1670 | 28.6k | cond = 0; /* other types cannot be equal to a number */ |
1671 | 1.01M | docondjump(); |
1672 | 1.01M | vmbreak; |
1673 | 1.01M | } |
1674 | 486k | vmcase(OP_LTI) { |
1675 | 1.45M | op_orderI(L, l_lti, luai_numlt, 0, TM_LT); |
1676 | 1.45M | vmbreak; |
1677 | 1.45M | } |
1678 | 140k | vmcase(OP_LEI) { |
1679 | 420k | op_orderI(L, l_lei, luai_numle, 0, TM_LE); |
1680 | 420k | vmbreak; |
1681 | 420k | } |
1682 | 26.5M | vmcase(OP_GTI) { |
1683 | 79.7M | op_orderI(L, l_gti, luai_numgt, 1, TM_LT); |
1684 | 79.7M | vmbreak; |
1685 | 79.7M | } |
1686 | 3.22M | vmcase(OP_GEI) { |
1687 | 9.66M | op_orderI(L, l_gei, luai_numge, 1, TM_LE); |
1688 | 9.66M | vmbreak; |
1689 | 9.66M | } |
1690 | 91.5M | vmcase(OP_TEST) { |
1691 | 91.5M | StkId ra = RA(i); |
1692 | 91.5M | int cond = !l_isfalse(s2v(ra)); |
1693 | 91.5M | docondjump(); |
1694 | 91.5M | vmbreak; |
1695 | 91.5M | } |
1696 | 819k | vmcase(OP_TESTSET) { |
1697 | 819k | StkId ra = RA(i); |
1698 | 819k | TValue *rb = vRB(i); |
1699 | 819k | if (l_isfalse(rb) == GETARG_k(i)) |
1700 | 781k | pc++; |
1701 | 37.6k | else { |
1702 | 37.6k | setobj2s(L, ra, rb); |
1703 | 37.6k | donextjump(ci); |
1704 | 37.6k | } |
1705 | 819k | vmbreak; |
1706 | 819k | } |
1707 | 333M | vmcase(OP_CALL) { |
1708 | 333M | StkId ra = RA(i); |
1709 | 333M | CallInfo *newci; |
1710 | 333M | int b = GETARG_B(i); |
1711 | 333M | int nresults = GETARG_C(i) - 1; |
1712 | 333M | if (b != 0) /* fixed number of arguments? */ |
1713 | 318M | L->top.p = ra + b; /* top signals number of arguments */ |
1714 | | /* else previous instruction set top */ |
1715 | 333M | savepc(ci); /* in case of errors */ |
1716 | 333M | if ((newci = luaD_precall(L, ra, nresults)) == NULL) |
1717 | 239M | updatetrap(ci); /* C call; nothing else to be done */ |
1718 | 93.7M | else { /* Lua call: run function in this same C frame */ |
1719 | 93.7M | ci = newci; |
1720 | 93.7M | goto startfunc; |
1721 | 93.7M | } |
1722 | 333M | vmbreak; |
1723 | 239M | } |
1724 | 73.3M | vmcase(OP_TAILCALL) { |
1725 | 73.3M | StkId ra = RA(i); |
1726 | 73.3M | int b = GETARG_B(i); /* number of arguments + 1 (function) */ |
1727 | 0 | int n; /* number of results when calling a C function */ |
1728 | 73.3M | int nparams1 = GETARG_C(i); |
1729 | | /* delta is virtual 'func' - real 'func' (vararg functions) */ |
1730 | 73.3M | int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; |
1731 | 73.3M | if (b != 0) |
1732 | 73.3M | L->top.p = ra + b; |
1733 | 2.60k | else /* previous instruction set top */ |
1734 | 2.60k | b = cast_int(L->top.p - ra); |
1735 | 73.3M | savepc(ci); /* several calls here can raise errors */ |
1736 | 73.3M | if (TESTARG_k(i)) { |
1737 | 26.4k | luaF_closeupval(L, base); /* close upvalues from current call */ |
1738 | 26.4k | lua_assert(L->tbclist.p < base); /* no pending tbc variables */ |
1739 | 26.4k | lua_assert(base == ci->func.p + 1); |
1740 | 26.4k | } |
1741 | 73.3M | if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ |
1742 | 73.3M | goto startfunc; /* execute the callee */ |
1743 | 18.4k | else { /* C function? */ |
1744 | 18.4k | ci->func.p -= delta; /* restore 'func' (if vararg) */ |
1745 | 18.4k | luaD_poscall(L, ci, n); /* finish caller */ |
1746 | 18.4k | updatetrap(ci); /* 'luaD_poscall' can change hooks */ |
1747 | 18.4k | goto ret; /* caller returns after the tail call */ |
1748 | 18.4k | } |
1749 | 73.3M | } |
1750 | 2.89M | vmcase(OP_RETURN) { |
1751 | 2.89M | StkId ra = RA(i); |
1752 | 2.89M | int n = GETARG_B(i) - 1; /* number of results */ |
1753 | 2.89M | int nparams1 = GETARG_C(i); |
1754 | 2.89M | if (n < 0) /* not fixed? */ |
1755 | 6.34k | n = cast_int(L->top.p - ra); /* get what is available */ |
1756 | 2.89M | savepc(ci); |
1757 | 2.89M | if (TESTARG_k(i)) { /* may there be open upvalues? */ |
1758 | 126k | ci->u2.nres = n; /* save number of returns */ |
1759 | 126k | if (L->top.p < ci->top.p) |
1760 | 51.1k | L->top.p = ci->top.p; |
1761 | 126k | luaF_close(L, base, CLOSEKTOP, 1); |
1762 | 126k | updatetrap(ci); |
1763 | 126k | updatestack(ci); |
1764 | 126k | } |
1765 | 2.89M | if (nparams1) /* vararg function? */ |
1766 | 2.76M | ci->func.p -= ci->u.l.nextraargs + nparams1; |
1767 | 2.89M | L->top.p = ra + n; /* set call for 'luaD_poscall' */ |
1768 | 2.89M | luaD_poscall(L, ci, n); |
1769 | 2.89M | updatetrap(ci); /* 'luaD_poscall' can change hooks */ |
1770 | 2.89M | goto ret; |
1771 | 2.89M | } |
1772 | 35.6M | vmcase(OP_RETURN0) { |
1773 | 35.6M | if (l_unlikely(L->hookmask)) { |
1774 | 4.47M | StkId ra = RA(i); |
1775 | 4.47M | L->top.p = ra; |
1776 | 4.47M | savepc(ci); |
1777 | 4.47M | luaD_poscall(L, ci, 0); /* no hurry... */ |
1778 | 4.47M | trap = 1; |
1779 | 4.47M | } |
1780 | 31.2M | else { /* do the 'poscall' here */ |
1781 | 31.2M | int nres = get_nresults(ci->callstatus); |
1782 | 31.2M | L->ci = ci->previous; /* back to caller */ |
1783 | 31.2M | L->top.p = base - 1; |
1784 | 31.2M | for (; l_unlikely(nres > 0); nres--) |
1785 | 31.2M | setnilvalue(s2v(L->top.p++)); /* all results are nil */ |
1786 | 31.2M | } |
1787 | 35.6M | goto ret; |
1788 | 2.89M | } |
1789 | 132M | vmcase(OP_RETURN1) { |
1790 | 132M | if (l_unlikely(L->hookmask)) { |
1791 | 45.6k | StkId ra = RA(i); |
1792 | 45.6k | L->top.p = ra + 1; |
1793 | 45.6k | savepc(ci); |
1794 | 45.6k | luaD_poscall(L, ci, 1); /* no hurry... */ |
1795 | 45.6k | trap = 1; |
1796 | 45.6k | } |
1797 | 132M | else { /* do the 'poscall' here */ |
1798 | 132M | int nres = get_nresults(ci->callstatus); |
1799 | 132M | L->ci = ci->previous; /* back to caller */ |
1800 | 132M | if (nres == 0) |
1801 | 4.80M | L->top.p = base - 1; /* asked for no results */ |
1802 | 127M | else { |
1803 | 127M | StkId ra = RA(i); |
1804 | 127M | setobjs2s(L, base - 1, ra); /* at least this result */ |
1805 | 127M | L->top.p = base; |
1806 | 127M | for (; l_unlikely(nres > 1); nres--) |
1807 | 127M | setnilvalue(s2v(L->top.p++)); /* complete missing results */ |
1808 | 127M | } |
1809 | 132M | } |
1810 | 170M | ret: /* return from a Lua function */ |
1811 | 170M | if (ci->callstatus & CIST_FRESH) |
1812 | 121M | return; /* end this frame */ |
1813 | 49.6M | else { |
1814 | 49.6M | ci = ci->previous; |
1815 | 49.6M | goto returning; /* continue running caller in this frame */ |
1816 | 49.6M | } |
1817 | 170M | } |
1818 | 27.7M | vmcase(OP_FORLOOP) { |
1819 | 27.7M | StkId ra = RA(i); |
1820 | 27.7M | if (ttisinteger(s2v(ra + 1))) { /* integer loop? */ |
1821 | 23.3M | lua_Unsigned count = l_castS2U(ivalue(s2v(ra))); |
1822 | 23.3M | if (count > 0) { /* still more iterations? */ |
1823 | 23.0M | lua_Integer step = ivalue(s2v(ra + 1)); |
1824 | 23.0M | lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */ |
1825 | 23.0M | chgivalue(s2v(ra), l_castU2S(count - 1)); /* update counter */ |
1826 | 23.0M | idx = intop(+, idx, step); /* add step to index */ |
1827 | 23.0M | chgivalue(s2v(ra + 2), idx); /* update control variable */ |
1828 | 23.0M | pc -= GETARG_Bx(i); /* jump back */ |
1829 | 23.0M | } |
1830 | 23.3M | } |
1831 | 4.45M | else if (floatforloop(ra)) /* float loop */ |
1832 | 4.13M | pc -= GETARG_Bx(i); /* jump back */ |
1833 | 27.7M | updatetrap(ci); /* allows a signal to break the loop */ |
1834 | 27.7M | vmbreak; |
1835 | 27.7M | } |
1836 | 3.23M | vmcase(OP_FORPREP) { |
1837 | 3.23M | StkId ra = RA(i); |
1838 | 3.23M | savestate(L, ci); /* in case of errors */ |
1839 | 3.23M | if (forprep(L, ra)) |
1840 | 1.84M | pc += GETARG_Bx(i) + 1; /* skip the loop */ |
1841 | 3.23M | vmbreak; |
1842 | 3.23M | } |
1843 | 144k | vmcase(OP_TFORPREP) { |
1844 | | /* before: 'ra' has the iterator function, 'ra + 1' has the state, |
1845 | | 'ra + 2' has the initial value for the control variable, and |
1846 | | 'ra + 3' has the closing variable. This opcode then swaps the |
1847 | | control and the closing variables and marks the closing variable |
1848 | | as to-be-closed. |
1849 | | */ |
1850 | 144k | StkId ra = RA(i); |
1851 | 144k | TValue temp; /* to swap control and closing variables */ |
1852 | 144k | setobj(L, &temp, s2v(ra + 3)); |
1853 | 144k | setobjs2s(L, ra + 3, ra + 2); |
1854 | 144k | setobj2s(L, ra + 2, &temp); |
1855 | | /* create to-be-closed upvalue (if closing var. is not nil) */ |
1856 | 144k | halfProtect(luaF_newtbcupval(L, ra + 2)); |
1857 | 144k | pc += GETARG_Bx(i); /* go to end of the loop */ |
1858 | 0 | i = *(pc++); /* fetch next instruction */ |
1859 | 143k | lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); |
1860 | 143k | goto l_tforcall; |
1861 | 143k | } |
1862 | 322k | vmcase(OP_TFORCALL) { |
1863 | 466k | l_tforcall: { |
1864 | | /* 'ra' has the iterator function, 'ra + 1' has the state, |
1865 | | 'ra + 2' has the closing variable, and 'ra + 3' has the control |
1866 | | variable. The call will use the stack starting at 'ra + 3', |
1867 | | so that it preserves the first three values, and the first |
1868 | | return will be the new value for the control variable. |
1869 | | */ |
1870 | 466k | StkId ra = RA(i); |
1871 | 466k | setobjs2s(L, ra + 5, ra + 3); /* copy the control variable */ |
1872 | 466k | setobjs2s(L, ra + 4, ra + 1); /* copy state */ |
1873 | 466k | setobjs2s(L, ra + 3, ra); /* copy function */ |
1874 | 466k | L->top.p = ra + 3 + 3; |
1875 | 466k | ProtectNT(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */ |
1876 | 466k | updatestack(ci); /* stack may have changed */ |
1877 | 466k | i = *(pc++); /* go to next instruction */ |
1878 | 466k | lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); |
1879 | 463k | goto l_tforloop; |
1880 | 466k | }} |
1881 | 463k | vmcase(OP_TFORLOOP) { |
1882 | 463k | l_tforloop: { |
1883 | 463k | StkId ra = RA(i); |
1884 | 463k | if (!ttisnil(s2v(ra + 3))) /* continue loop? */ |
1885 | 378k | pc -= GETARG_Bx(i); /* jump back */ |
1886 | 463k | vmbreak; |
1887 | 463k | }} |
1888 | 3.19M | vmcase(OP_SETLIST) { |
1889 | 3.19M | StkId ra = RA(i); |
1890 | 3.19M | unsigned n = cast_uint(GETARG_vB(i)); |
1891 | 3.19M | unsigned int last = cast_uint(GETARG_vC(i)); |
1892 | 6.38M | Table *h = hvalue(s2v(ra)); |
1893 | 3.19M | if (n == 0) |
1894 | 250k | n = cast_uint(L->top.p - ra) - 1; /* get up to the top */ |
1895 | 2.94M | else |
1896 | 2.94M | L->top.p = ci->top.p; /* correct top in case of emergency GC */ |
1897 | 6.38M | last += n; |
1898 | 6.38M | if (TESTARG_k(i)) { |
1899 | 366k | last += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); |
1900 | 0 | pc++; |
1901 | 366k | } |
1902 | | /* when 'n' is known, table should have proper size */ |
1903 | 3.19M | if (last > h->asize) { /* needs more space? */ |
1904 | | /* fixed-size sets should have space preallocated */ |
1905 | 228k | lua_assert(GETARG_vB(i) == 0); |
1906 | 228k | luaH_resizearray(L, h, last); /* preallocate it at once */ |
1907 | 228k | } |
1908 | 62.7M | for (; n > 0; n--) { |
1909 | 59.5M | TValue *val = s2v(ra + n); |
1910 | 59.5M | obj2arr(h, last - 1, val); |
1911 | 59.5M | last--; |
1912 | 59.5M | luaC_barrierback(L, obj2gco(h), val); |
1913 | 59.5M | } |
1914 | 3.19M | vmbreak; |
1915 | 3.19M | } |
1916 | 18.0M | vmcase(OP_CLOSURE) { |
1917 | 18.0M | StkId ra = RA(i); |
1918 | 18.0M | Proto *p = cl->p->p[GETARG_Bx(i)]; |
1919 | 18.0M | halfProtect(pushclosure(L, p, cl->upvals, base, ra)); |
1920 | 18.0M | checkGC(L, ra + 1); |
1921 | 18.0M | vmbreak; |
1922 | 18.0M | } |
1923 | 2.89M | vmcase(OP_VARARG) { |
1924 | 2.89M | StkId ra = RA(i); |
1925 | 2.89M | int n = GETARG_C(i) - 1; /* required results */ |
1926 | 2.89M | Protect(luaT_getvarargs(L, ci, ra, n)); |
1927 | 2.89M | vmbreak; |
1928 | 2.89M | } |
1929 | 14.5M | vmcase(OP_VARARGPREP) { |
1930 | 14.5M | ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); |
1931 | 14.5M | if (l_unlikely(trap)) { /* previous "Protect" updated trap */ |
1932 | 1.31M | luaD_hookcall(L, ci); |
1933 | 1.31M | L->oldpc = 1; /* next opcode will be seen as a "new" line */ |
1934 | 1.31M | } |
1935 | 14.5M | updatebase(ci); /* function has new base after adjustment */ |
1936 | 14.5M | vmbreak; |
1937 | 14.5M | } |
1938 | 14.5M | vmcase(OP_EXTRAARG) { |
1939 | 0 | lua_assert(0); |
1940 | 0 | vmbreak; |
1941 | 0 | } |
1942 | 0 | } |
1943 | 0 | } |
1944 | 676M | } |
1945 | | |
1946 | | /* }================================================================== */ |