/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 | 234M | #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 | 17.0M | #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 | 17.0M | #define MAXINTFITSF ((lua_Unsigned)1 << NBM) |
73 | | |
74 | | /* check whether 'i' is in the interval [-MAXINTFITSF, MAXINTFITSF] */ |
75 | 8.52M | #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.41M | static int l_strton (const TValue *obj, TValue *result) { |
92 | 4.41M | lua_assert(obj != result); |
93 | 4.41M | if (!cvt2num(obj)) /* is object not a string? */ |
94 | 3.30M | return 0; |
95 | 1.10M | else { |
96 | 1.10M | TString *st = tsvalue(obj); |
97 | 0 | size_t stlen; |
98 | 1.10M | const char *s = getlstr(st, stlen); |
99 | 1.10M | return (luaO_str2num(s, result) == stlen + 1); |
100 | 1.10M | } |
101 | 4.41M | } |
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 | 980k | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { |
109 | 980k | TValue v; |
110 | 980k | if (ttisinteger(obj)) { |
111 | 420k | *n = cast_num(ivalue(obj)); |
112 | 0 | return 1; |
113 | 420k | } |
114 | 559k | else if (l_strton(obj, &v)) { /* string coercible to number? */ |
115 | 336k | *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ |
116 | 336k | return 1; |
117 | 336k | } |
118 | 223k | else |
119 | 223k | return 0; /* conversion failed */ |
120 | 980k | } |
121 | | |
122 | | |
123 | | /* |
124 | | ** try to convert a float to an integer, rounding according to 'mode'. |
125 | | */ |
126 | 157M | int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) { |
127 | 157M | lua_Number f = l_floor(n); |
128 | 157M | if (n != f) { /* not an integral value? */ |
129 | 13.7M | if (mode == F2Ieq) return 0; /* fails if mode demands integral value */ |
130 | 55.8k | else if (mode == F2Iceil) /* needs ceiling? */ |
131 | 49.2k | f += 1; /* convert floor to ceiling (remember: n != f) */ |
132 | 13.7M | } |
133 | 143M | return lua_numbertointeger(f, p); |
134 | 157M | } |
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 | 16.8M | int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) { |
143 | 16.8M | if (ttisfloat(obj)) |
144 | 10.2M | return luaV_flttointeger(fltvalue(obj), p, mode); |
145 | 6.62M | else if (ttisinteger(obj)) { |
146 | 6.25M | *p = ivalue(obj); |
147 | 0 | return 1; |
148 | 6.25M | } |
149 | 371k | else |
150 | 371k | return 0; |
151 | 16.8M | } |
152 | | |
153 | | |
154 | | /* |
155 | | ** try to convert a value to an integer. |
156 | | */ |
157 | 3.85M | int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) { |
158 | 3.85M | TValue v; |
159 | 3.85M | if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ |
160 | 400k | obj = &v; /* change it to point to its corresponding number */ |
161 | 3.85M | return luaV_tointegerns(obj, p, mode); |
162 | 3.85M | } |
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 | 3.14M | lua_Integer *p, lua_Integer step) { |
183 | 3.14M | if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) { |
184 | | /* not coercible to in integer */ |
185 | 17.9k | lua_Number flim; /* try to convert to float */ |
186 | 17.9k | 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.6k | if (luai_numlt(0, flim)) { /* if it is positive, it is too large */ |
190 | 5.12k | if (step < 0) return 1; /* initial value must be less than it */ |
191 | 3.46k | *p = LUA_MAXINTEGER; /* truncate */ |
192 | 3.46k | } |
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.93k | *p = LUA_MININTEGER; /* truncate */ |
196 | 8.93k | } |
197 | 15.6k | } |
198 | 3.13M | return (step > 0 ? init > *p : init < *p); /* not to run? */ |
199 | 3.14M | } |
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.50M | static int forprep (lua_State *L, StkId ra) { |
215 | 3.50M | TValue *pinit = s2v(ra); |
216 | 3.50M | TValue *plimit = s2v(ra + 1); |
217 | 3.50M | TValue *pstep = s2v(ra + 2); |
218 | 3.50M | if (ttisinteger(pinit) && ttisinteger(pstep)) { /* integer loop? */ |
219 | 3.14M | lua_Integer init = ivalue(pinit); |
220 | 3.14M | lua_Integer step = ivalue(pstep); |
221 | 0 | lua_Integer limit; |
222 | 3.14M | if (step == 0) |
223 | 100 | luaG_runerror(L, "'for' step is zero"); |
224 | 3.14M | if (forlimit(L, init, plimit, &limit, step)) |
225 | 1.80M | return 1; /* skip the loop */ |
226 | 1.33M | else { /* prepare loop counter */ |
227 | 1.33M | lua_Unsigned count; |
228 | 1.33M | if (step > 0) { /* ascending loop? */ |
229 | 1.31M | count = l_castS2U(limit) - l_castS2U(init); |
230 | 1.31M | if (step != 1) /* avoid division in the too common case */ |
231 | 6.96k | count /= l_castS2U(step); |
232 | 1.31M | } |
233 | 14.9k | else { /* step < 0; descending loop */ |
234 | 14.9k | count = l_castS2U(init) - l_castS2U(limit); |
235 | | /* 'step+1' avoids negating 'mininteger' */ |
236 | 14.9k | count /= l_castS2U(-(step + 1)) + 1u; |
237 | 14.9k | } |
238 | | /* use 'chgivalue' for places that for sure had integers */ |
239 | 1.33M | chgivalue(s2v(ra), l_castU2S(count)); /* change init to count */ |
240 | 1.33M | setivalue(s2v(ra + 1), step); /* change limit to step */ |
241 | 1.33M | chgivalue(s2v(ra + 2), init); /* change step to init */ |
242 | 1.33M | } |
243 | 3.14M | } |
244 | 360k | else { /* try making all values floats */ |
245 | 360k | lua_Number init; lua_Number limit; lua_Number step; |
246 | 360k | if (l_unlikely(!tonumber(plimit, &limit))) |
247 | 128 | luaG_forerror(L, plimit, "limit"); |
248 | 359k | if (l_unlikely(!tonumber(pstep, &step))) |
249 | 110 | luaG_forerror(L, pstep, "step"); |
250 | 359k | if (l_unlikely(!tonumber(pinit, &init))) |
251 | 4.66k | luaG_forerror(L, pinit, "initial value"); |
252 | 355k | if (step == 0) |
253 | 1.63k | luaG_runerror(L, "'for' step is zero"); |
254 | 353k | if (luai_numlt(0, step) ? luai_numlt(limit, init) |
255 | 353k | : luai_numlt(init, limit)) |
256 | 33.8k | return 1; /* skip the loop */ |
257 | 319k | else { |
258 | | /* make sure all values are floats */ |
259 | 319k | setfltvalue(s2v(ra), limit); |
260 | 319k | setfltvalue(s2v(ra + 1), step); |
261 | 319k | setfltvalue(s2v(ra + 2), init); /* control variable */ |
262 | 319k | } |
263 | 353k | } |
264 | 1.65M | return 0; |
265 | 3.50M | } |
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 | 5.22M | static int floatforloop (StkId ra) { |
274 | 5.22M | lua_Number step = fltvalue(s2v(ra + 1)); |
275 | 5.22M | lua_Number limit = fltvalue(s2v(ra)); |
276 | 5.22M | lua_Number idx = fltvalue(s2v(ra + 2)); /* control variable */ |
277 | 5.22M | idx = luai_numadd(L, idx, step); /* increment index */ |
278 | 5.22M | if (luai_numlt(0, step) ? luai_numle(idx, limit) |
279 | 5.22M | : luai_numle(limit, idx)) { |
280 | 4.90M | chgfltvalue(s2v(ra + 2), idx); /* update control variable */ |
281 | 4.90M | return 1; /* jump back */ |
282 | 4.90M | } |
283 | 319k | else |
284 | 319k | return 0; /* finish the loop */ |
285 | 5.22M | } |
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 | 153M | StkId val, lu_byte tag) { |
293 | 153M | int loop; /* counter to avoid infinite loops */ |
294 | 153M | const TValue *tm; /* metamethod */ |
295 | 168M | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
296 | 168M | if (tag == LUA_VNOTABLE) { /* 't' is not a table? */ |
297 | 110M | lua_assert(!ttistable(t)); |
298 | 110M | tm = luaT_gettmbyobj(L, t, TM_INDEX); |
299 | 110M | if (l_unlikely(notm(tm))) |
300 | 44.1k | luaG_typeerror(L, t, "index"); /* no metamethod */ |
301 | | /* else will try the metamethod */ |
302 | 110M | } |
303 | 58.5M | else { /* 't' is a table */ |
304 | 58.5M | tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ |
305 | 58.5M | if (tm == NULL) { /* no metamethod? */ |
306 | 58.5M | setnilvalue(s2v(val)); /* result is nil */ |
307 | 58.5M | return LUA_VNIL; |
308 | 58.5M | } |
309 | | /* else will try the metamethod */ |
310 | 58.5M | } |
311 | 109M | if (ttisfunction(tm)) { /* is metamethod a function? */ |
312 | 89.1M | tag = luaT_callTMres(L, tm, t, key, val); /* call it */ |
313 | 89.1M | return tag; /* return tag of the result */ |
314 | 89.1M | } |
315 | 20.8M | t = tm; /* else try to access 'tm[key]' */ |
316 | 20.8M | luaV_fastget(t, key, s2v(val), luaH_get, tag); |
317 | 20.8M | if (!tagisempty(tag)) |
318 | 6.00M | return tag; /* done */ |
319 | | /* else repeat (tail call 'luaV_finishget') */ |
320 | 20.8M | } |
321 | 0 | luaG_runerror(L, "'__index' chain too long; possible loop"); |
322 | 0 | return 0; /* to avoid warnings */ |
323 | 153M | } |
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 | 66.2M | TValue *val, int hres) { |
336 | 66.2M | int loop; /* counter to avoid infinite loops */ |
337 | 66.2M | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
338 | 66.2M | const TValue *tm; /* '__newindex' metamethod */ |
339 | 66.2M | if (hres != HNOTATABLE) { /* is 't' a table? */ |
340 | 51.0M | Table *h = hvalue(t); /* save 't' table */ |
341 | 25.5M | tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ |
342 | 51.0M | if (tm == NULL) { /* no metamethod? */ |
343 | 25.2M | sethvalue2s(L, L->top.p, h); /* anchor 't' */ |
344 | 25.2M | L->top.p++; /* assume EXTRA_STACK */ |
345 | 25.2M | luaH_finishset(L, h, key, val, hres); /* set new value */ |
346 | 25.2M | L->top.p--; |
347 | 25.2M | invalidateTMcache(h); |
348 | 25.2M | luaC_barrierback(L, obj2gco(h), val); |
349 | 25.2M | return; |
350 | 25.2M | } |
351 | | /* else will try the metamethod */ |
352 | 51.0M | } |
353 | 40.7M | else { /* not a table; check metamethod */ |
354 | 40.7M | tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); |
355 | 40.7M | if (l_unlikely(notm(tm))) |
356 | 7.29k | luaG_typeerror(L, t, "index"); |
357 | 40.7M | } |
358 | | /* try the metamethod */ |
359 | 41.0M | if (ttisfunction(tm)) { |
360 | 41.0M | luaT_callTM(L, tm, t, key, val); |
361 | 41.0M | return; |
362 | 41.0M | } |
363 | 17 | t = tm; /* else repeat assignment over 'tm' */ |
364 | 17 | luaV_fastset(t, key, val, hres, luaH_pset); |
365 | 17 | if (hres == HOK) { |
366 | 15 | luaV_finishfastset(L, t, val); |
367 | 15 | return; /* done */ |
368 | 15 | } |
369 | | /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ |
370 | 17 | } |
371 | 0 | luaG_runerror(L, "'__newindex' chain too long; possible loop"); |
372 | 66.2M | } |
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.11M | static int l_strcmp (const TString *ts1, const TString *ts2) { |
384 | 1.11M | size_t rl1; /* real length */ |
385 | 1.11M | const char *s1 = getlstr(ts1, rl1); |
386 | 1.11M | size_t rl2; |
387 | 1.11M | const char *s2 = getlstr(ts2, rl2); |
388 | 1.70M | for (;;) { /* for each segment */ |
389 | 1.70M | int temp = strcoll(s1, s2); |
390 | 1.70M | if (temp != 0) /* not equal? */ |
391 | 868k | return temp; /* done */ |
392 | 838k | else { /* strings are equal up to a '\0' */ |
393 | 838k | size_t zl1 = strlen(s1); /* index of first '\0' in 's1' */ |
394 | 838k | size_t zl2 = strlen(s2); /* index of first '\0' in 's2' */ |
395 | 838k | if (zl2 == rl2) /* 's2' is finished? */ |
396 | 228k | return (zl1 == rl1) ? 0 : 1; /* check 's1' */ |
397 | 610k | 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 | 596k | zl1++; zl2++; |
401 | 596k | s1 += zl1; rl1 -= zl1; s2 += zl2; rl2 -= zl2; |
402 | 596k | } |
403 | 1.70M | } |
404 | 1.11M | } |
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 | 844k | l_sinline int LTintfloat (lua_Integer i, lua_Number f) { |
419 | 844k | if (l_intfitsf(i)) |
420 | 735k | return luai_numlt(cast_num(i), f); /* compare them as floats */ |
421 | 108k | else { /* i < f <=> i < ceil(f) */ |
422 | 108k | lua_Integer fi; |
423 | 108k | if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ |
424 | 35.6k | 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 | 108k | } |
428 | 844k | } |
429 | | |
430 | | |
431 | | /* |
432 | | ** Check whether integer 'i' is less than or equal to float 'f'. |
433 | | ** See comments on previous function. |
434 | | */ |
435 | 84.5k | l_sinline int LEintfloat (lua_Integer i, lua_Number f) { |
436 | 84.5k | if (l_intfitsf(i)) |
437 | 74.9k | return luai_numle(cast_num(i), f); /* compare them as floats */ |
438 | 9.58k | else { /* i <= f <=> i <= floor(f) */ |
439 | 9.58k | lua_Integer fi; |
440 | 9.58k | if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */ |
441 | 8.76k | return i <= fi; /* compare them as integers */ |
442 | 821 | else /* 'f' is either greater or less than all integers */ |
443 | 821 | return f > 0; /* greater? */ |
444 | 9.58k | } |
445 | 84.5k | } |
446 | | |
447 | | |
448 | | /* |
449 | | ** Check whether float 'f' is less than integer 'i'. |
450 | | ** See comments on previous function. |
451 | | */ |
452 | 6.48M | l_sinline int LTfloatint (lua_Number f, lua_Integer i) { |
453 | 6.48M | if (l_intfitsf(i)) |
454 | 6.48M | return luai_numlt(f, cast_num(i)); /* compare them as floats */ |
455 | 5.51k | else { /* f < i <=> floor(f) < i */ |
456 | 5.51k | lua_Integer fi; |
457 | 5.51k | 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.51k | } |
462 | 6.48M | } |
463 | | |
464 | | |
465 | | /* |
466 | | ** Check whether float 'f' is less than or equal to integer 'i'. |
467 | | ** See comments on previous function. |
468 | | */ |
469 | 1.10M | l_sinline int LEfloatint (lua_Number f, lua_Integer i) { |
470 | 1.10M | if (l_intfitsf(i)) |
471 | 1.07M | return luai_numle(f, cast_num(i)); /* compare them as floats */ |
472 | 29.0k | else { /* f <= i <=> ceil(f) <= i */ |
473 | 29.0k | lua_Integer fi; |
474 | 29.0k | if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */ |
475 | 27.5k | return fi <= i; /* compare them as integers */ |
476 | 1.51k | else /* 'f' is either greater or less than all integers */ |
477 | 1.51k | return f < 0; /* less? */ |
478 | 29.0k | } |
479 | 1.10M | } |
480 | | |
481 | | |
482 | | /* |
483 | | ** Return 'l < r', for numbers. |
484 | | */ |
485 | 7.62M | l_sinline int LTnum (const TValue *l, const TValue *r) { |
486 | 7.62M | lua_assert(ttisnumber(l) && ttisnumber(r)); |
487 | 7.62M | if (ttisinteger(l)) { |
488 | 844k | lua_Integer li = ivalue(l); |
489 | 844k | if (ttisinteger(r)) |
490 | 844k | return li < ivalue(r); /* both are integers */ |
491 | 844k | else /* 'l' is int and 'r' is float */ |
492 | 844k | return LTintfloat(li, fltvalue(r)); /* l < r ? */ |
493 | 844k | } |
494 | 6.77M | else { |
495 | 6.77M | lua_Number lf = fltvalue(l); /* 'l' must be float */ |
496 | 6.77M | if (ttisfloat(r)) |
497 | 6.77M | return luai_numlt(lf, fltvalue(r)); /* both are float */ |
498 | 6.48M | else /* 'l' is float and 'r' is int */ |
499 | 6.48M | return LTfloatint(lf, ivalue(r)); |
500 | 6.77M | } |
501 | 7.62M | } |
502 | | |
503 | | |
504 | | /* |
505 | | ** Return 'l <= r', for numbers. |
506 | | */ |
507 | 1.22M | l_sinline int LEnum (const TValue *l, const TValue *r) { |
508 | 1.22M | lua_assert(ttisnumber(l) && ttisnumber(r)); |
509 | 1.22M | if (ttisinteger(l)) { |
510 | 84.5k | lua_Integer li = ivalue(l); |
511 | 84.5k | if (ttisinteger(r)) |
512 | 84.5k | return li <= ivalue(r); /* both are integers */ |
513 | 84.5k | else /* 'l' is int and 'r' is float */ |
514 | 84.5k | return LEintfloat(li, fltvalue(r)); /* l <= r ? */ |
515 | 84.5k | } |
516 | 1.13M | else { |
517 | 1.13M | lua_Number lf = fltvalue(l); /* 'l' must be float */ |
518 | 1.13M | if (ttisfloat(r)) |
519 | 1.13M | return luai_numle(lf, fltvalue(r)); /* both are float */ |
520 | 1.10M | else /* 'l' is float and 'r' is int */ |
521 | 1.10M | return LEfloatint(lf, ivalue(r)); |
522 | 1.13M | } |
523 | 1.22M | } |
524 | | |
525 | | |
526 | | /* |
527 | | ** return 'l < r' for non-numbers. |
528 | | */ |
529 | 2.77M | static int lessthanothers (lua_State *L, const TValue *l, const TValue *r) { |
530 | 2.77M | lua_assert(!ttisnumber(l) || !ttisnumber(r)); |
531 | 2.77M | if (ttisstring(l) && ttisstring(r)) /* both are strings? */ |
532 | 2.08M | return l_strcmp(tsvalue(l), tsvalue(r)) < 0; |
533 | 1.72M | else |
534 | 1.72M | return luaT_callorderTM(L, l, r, TM_LT); |
535 | 2.77M | } |
536 | | |
537 | | |
538 | | /* |
539 | | ** Main operation less than; return 'l < r'. |
540 | | */ |
541 | 956k | int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { |
542 | 956k | if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ |
543 | 882 | return LTnum(l, r); |
544 | 956k | else return lessthanothers(L, l, r); |
545 | 956k | } |
546 | | |
547 | | |
548 | | /* |
549 | | ** return 'l <= r' for non-numbers. |
550 | | */ |
551 | 79.4k | static int lessequalothers (lua_State *L, const TValue *l, const TValue *r) { |
552 | 79.4k | lua_assert(!ttisnumber(l) || !ttisnumber(r)); |
553 | 79.4k | if (ttisstring(l) && ttisstring(r)) /* both are strings? */ |
554 | 136k | return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; |
555 | 11.1k | else |
556 | 11.1k | return luaT_callorderTM(L, l, r, TM_LE); |
557 | 79.4k | } |
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 | 471M | int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { |
575 | 471M | const TValue *tm; |
576 | 471M | if (ttype(t1) != ttype(t2)) /* not the same type? */ |
577 | 44.2M | return 0; |
578 | 427M | else if (ttypetag(t1) != ttypetag(t2)) { |
579 | 34.8M | switch (ttypetag(t1)) { |
580 | 2.68M | 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 | 2.68M | lua_Integer i2; |
584 | 2.68M | return (luaV_flttointeger(fltvalue(t2), &i2, F2Ieq) && |
585 | 2.68M | ivalue(t1) == i2); |
586 | 0 | } |
587 | 182k | case LUA_VNUMFLT: { /* float == integer? */ |
588 | 182k | lua_Integer i1; /* see comment in previous case */ |
589 | 182k | return (luaV_flttointeger(fltvalue(t1), &i1, F2Ieq) && |
590 | 182k | 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 | 31.9M | default: |
599 | | /* only numbers (integer/float) and strings (long/short) can have |
600 | | equal values with different variants */ |
601 | 31.9M | return 0; |
602 | 34.8M | } |
603 | 34.8M | } |
604 | 392M | else { /* equal variants */ |
605 | 392M | switch (ttypetag(t1)) { |
606 | 1.85M | case LUA_VNIL: case LUA_VFALSE: case LUA_VTRUE: |
607 | 1.85M | return 1; |
608 | 2.74M | case LUA_VNUMINT: |
609 | 2.74M | return (ivalue(t1) == ivalue(t2)); |
610 | 5.26M | case LUA_VNUMFLT: |
611 | 5.26M | return (fltvalue(t1) == fltvalue(t2)); |
612 | 0 | case LUA_VLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); |
613 | 292M | case LUA_VSHRSTR: |
614 | 1.17G | return eqshrstr(tsvalue(t1), tsvalue(t2)); |
615 | 3.86M | case LUA_VLNGSTR: |
616 | 7.73M | return luaS_eqstr(tsvalue(t1), tsvalue(t2)); |
617 | 38.0k | case LUA_VUSERDATA: { |
618 | 114k | if (uvalue(t1) == uvalue(t2)) return 1; |
619 | 2.52k | else if (L == NULL) return 0; |
620 | 2.52k | tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); |
621 | 2.52k | if (tm == NULL) |
622 | 2.52k | tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); |
623 | 2.52k | break; /* will try TM */ |
624 | 2.52k | } |
625 | 6.18M | case LUA_VTABLE: { |
626 | 18.5M | if (hvalue(t1) == hvalue(t2)) return 1; |
627 | 97.0k | else if (L == NULL) return 0; |
628 | 95.1k | tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); |
629 | 95.1k | if (tm == NULL) |
630 | 90.4k | tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); |
631 | 95.1k | break; /* will try TM */ |
632 | 95.1k | } |
633 | 78.8M | case LUA_VLCF: |
634 | 78.8M | return (fvalue(t1) == fvalue(t2)); |
635 | 1.01M | default: /* functions and threads */ |
636 | 1.01M | return (gcvalue(t1) == gcvalue(t2)); |
637 | 392M | } |
638 | 97.7k | if (tm == NULL) /* no TM? */ |
639 | 91.6k | return 0; /* objects are different */ |
640 | 6.09k | else { |
641 | 6.09k | int tag = luaT_callTMres(L, tm, t1, t2, L->top.p); /* call TM */ |
642 | 6.09k | return !tagisfalse(tag); |
643 | 6.09k | } |
644 | 97.7k | } |
645 | 471M | } |
646 | | |
647 | | |
648 | | /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ |
649 | | #define tostring(L,o) \ |
650 | 25.4M | (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) |
651 | | |
652 | 46.3M | #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 | 10.7M | static void copy2buff (StkId top, int n, char *buff) { |
656 | 10.7M | size_t tl = 0; /* size already copied */ |
657 | 23.1M | do { |
658 | 46.3M | TString *st = tsvalue(s2v(top - n)); |
659 | 0 | size_t l; /* length of string being copied */ |
660 | 46.3M | const char *s = getlstr(st, l); |
661 | 46.3M | memcpy(buff + tl, s, l * sizeof(char)); |
662 | 46.3M | tl += l; |
663 | 46.3M | } while (--n > 0); |
664 | 10.7M | } |
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 | 14.0M | void luaV_concat (lua_State *L, int total) { |
672 | 14.0M | if (total == 1) |
673 | 6 | return; /* "all" values already concatenated */ |
674 | 14.2M | do { |
675 | 14.2M | StkId top = L->top.p; |
676 | 14.2M | int n = 2; /* number of elements handled in this pass (at least 2) */ |
677 | 14.2M | if (!(ttisstring(s2v(top - 2)) || cvt2str(s2v(top - 2))) || |
678 | 14.2M | !tostring(L, s2v(top - 1))) |
679 | 1.89M | luaT_tryconcatTM(L); /* may invalidate 'top' */ |
680 | 12.3M | else if (isemptystr(s2v(top - 1))) /* second operand is empty? */ |
681 | 12.3M | cast_void(tostring(L, s2v(top - 2))); /* result is first operand */ |
682 | 12.2M | else if (isemptystr(s2v(top - 2))) { /* first operand is empty string? */ |
683 | 1.54M | setobjs2s(L, top - 2, top - 1); /* result is second op. */ |
684 | 1.54M | } |
685 | 10.7M | else { |
686 | | /* at least two non-empty string values; get as many as possible */ |
687 | 10.7M | size_t tl = tsslen(tsvalue(s2v(top - 1))); |
688 | 10.7M | TString *ts; |
689 | | /* collect total length and number of strings */ |
690 | 23.1M | for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { |
691 | 12.4M | size_t l = tsslen(tsvalue(s2v(top - n - 1))); |
692 | 12.4M | 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 | 12.4M | tl += l; |
697 | 12.4M | } |
698 | 10.7M | if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ |
699 | 8.07M | char buff[LUAI_MAXSHORTLEN]; |
700 | 8.07M | copy2buff(top, n, buff); /* copy strings to buffer */ |
701 | 8.07M | ts = luaS_newlstr(L, buff, tl); |
702 | 8.07M | } |
703 | 2.68M | else { /* long string; copy strings directly to final result */ |
704 | 2.68M | ts = luaS_createlngstrobj(L, tl); |
705 | 2.68M | copy2buff(top, n, getlngstr(ts)); |
706 | 2.68M | } |
707 | 21.5M | setsvalue2s(L, top - n, ts); /* create result */ |
708 | 10.7M | } |
709 | 14.2M | total -= n - 1; /* got 'n' strings to create one new */ |
710 | 14.2M | L->top.p -= n - 1; /* popped 'n' strings and pushed one */ |
711 | 14.2M | } while (total > 1); /* repeat until only 1 result left */ |
712 | 14.0M | } |
713 | | |
714 | | |
715 | | /* |
716 | | ** Main operation 'ra = #rb'. |
717 | | */ |
718 | 12.0M | void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { |
719 | 12.0M | const TValue *tm; |
720 | 12.0M | switch (ttypetag(rb)) { |
721 | 8.09M | case LUA_VTABLE: { |
722 | 16.1M | Table *h = hvalue(rb); |
723 | 8.09M | tm = fasttm(L, h->metatable, TM_LEN); |
724 | 16.1M | if (tm) break; /* metamethod? break switch to call it */ |
725 | 8.08M | setivalue(s2v(ra), l_castU2S(luaH_getn(L, h))); /* else primitive len */ |
726 | 8.08M | return; |
727 | 16.1M | } |
728 | 3.51M | case LUA_VSHRSTR: { |
729 | 3.51M | setivalue(s2v(ra), tsvalue(rb)->shrlen); |
730 | 3.51M | return; |
731 | 3.51M | } |
732 | 295k | case LUA_VLNGSTR: { |
733 | 295k | setivalue(s2v(ra), cast_st2S(tsvalue(rb)->u.lnglen)); |
734 | 295k | return; |
735 | 295k | } |
736 | 122k | default: { /* try metamethod */ |
737 | 122k | tm = luaT_gettmbyobj(L, rb, TM_LEN); |
738 | 122k | if (l_unlikely(notm(tm))) /* no metamethod? */ |
739 | 1.62k | luaG_typeerror(L, rb, "get length of"); |
740 | 121k | break; |
741 | 122k | } |
742 | 12.0M | } |
743 | 123k | luaT_callTMres(L, tm, rb, rb, ra); |
744 | 123k | } |
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 | 2.01M | lua_Integer luaV_idiv (lua_State *L, lua_Integer m, lua_Integer n) { |
754 | 2.01M | if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ |
755 | 39.1k | if (n == 0) |
756 | 27 | luaG_runerror(L, "attempt to divide by zero"); |
757 | 39.1k | return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ |
758 | 39.1k | } |
759 | 1.97M | else { |
760 | 1.97M | lua_Integer q = m / n; /* perform C division */ |
761 | 1.97M | if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ |
762 | 59.5k | q -= 1; /* correct result for different rounding */ |
763 | 1.97M | return q; |
764 | 1.97M | } |
765 | 2.01M | } |
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 | 1.70M | lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { |
774 | 1.70M | if (l_unlikely(l_castS2U(n) + 1u <= 1u)) { /* special cases: -1 or 0 */ |
775 | 46.3k | if (n == 0) |
776 | 3.40k | luaG_runerror(L, "attempt to perform 'n%%0'"); |
777 | 42.9k | return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ |
778 | 46.3k | } |
779 | 1.65M | else { |
780 | 1.65M | lua_Integer r = m % n; |
781 | 1.65M | if (r != 0 && (r ^ n) < 0) /* 'm/n' would be non-integer negative? */ |
782 | 388k | r += n; /* correct result for different rounding */ |
783 | 1.65M | return r; |
784 | 1.65M | } |
785 | 1.70M | } |
786 | | |
787 | | |
788 | | /* |
789 | | ** Float modulus |
790 | | */ |
791 | 4.53M | lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) { |
792 | 4.53M | lua_Number r; |
793 | 4.53M | luai_nummod(L, m, n, r); |
794 | 4.53M | return r; |
795 | 4.53M | } |
796 | | |
797 | | |
798 | | /* number of bits in an integer */ |
799 | 2.03M | #define NBITS l_numbits(lua_Integer) |
800 | | |
801 | | |
802 | | /* |
803 | | ** Shift left operation. (Shift right just negates 'y'.) |
804 | | */ |
805 | 2.03M | lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { |
806 | 2.03M | if (y < 0) { /* shift right? */ |
807 | 764k | if (y <= -NBITS) return 0; |
808 | 560k | else return intop(>>, x, -y); |
809 | 764k | } |
810 | 1.26M | else { /* shift left */ |
811 | 1.26M | if (y >= NBITS) return 0; |
812 | 1.10M | else return intop(<<, x, y); |
813 | 1.26M | } |
814 | 2.03M | } |
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 | 24.6M | StkId ra) { |
823 | 24.6M | int nup = p->sizeupvalues; |
824 | 24.6M | Upvaldesc *uv = p->upvalues; |
825 | 24.6M | int i; |
826 | 24.6M | LClosure *ncl = luaF_newLclosure(L, nup); |
827 | 24.6M | ncl->p = p; |
828 | 24.6M | setclLvalue2s(L, ra, ncl); /* anchor new closure in stack */ |
829 | 47.0M | for (i = 0; i < nup; i++) { /* fill in its upvalues */ |
830 | 22.3M | if (uv[i].instack) /* upvalue refers to local variable? */ |
831 | 17.5M | ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); |
832 | 4.82M | else /* get upvalue from enclosing function */ |
833 | 4.82M | ncl->upvals[i] = encup[uv[i].idx]; |
834 | 22.3M | luaC_objbarrier(L, ncl, ncl->upvals[i]); |
835 | 22.3M | } |
836 | 24.6M | } |
837 | | |
838 | | |
839 | | /* |
840 | | ** finish execution of an opcode interrupted by a yield |
841 | | */ |
842 | 378 | void luaV_finishOp (lua_State *L) { |
843 | 378 | CallInfo *ci = L->ci; |
844 | 378 | StkId base = ci->func.p + 1; |
845 | 378 | Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ |
846 | 378 | OpCode op = GET_OPCODE(inst); |
847 | 378 | 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 | | #if defined(LUA_COMPAT_LT_LE) |
865 | | if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ |
866 | | ci->callstatus ^= CIST_LEQ; /* clear mark */ |
867 | | res = !res; /* negate result */ |
868 | | } |
869 | | #endif |
870 | 0 | lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); |
871 | 0 | if (res != GETARG_k(inst)) /* condition failed? */ |
872 | 0 | ci->u.l.savedpc++; /* skip jump instruction */ |
873 | 0 | break; |
874 | 0 | } |
875 | 0 | case OP_CONCAT: { |
876 | 0 | StkId top = L->top.p - 1; /* top when 'luaT_tryconcatTM' was called */ |
877 | 0 | int a = GETARG_A(inst); /* first element to concatenate */ |
878 | 0 | int total = cast_int(top - 1 - (base + a)); /* yet to concatenate */ |
879 | 0 | setobjs2s(L, top - 2, top); /* put TM result in proper position */ |
880 | 0 | L->top.p = top - 1; /* top is one after last element (at top-2) */ |
881 | 0 | luaV_concat(L, total); /* concat them (may yield again) */ |
882 | 0 | break; |
883 | 0 | } |
884 | 0 | case OP_CLOSE: { /* yielded closing variables */ |
885 | 0 | ci->u.l.savedpc--; /* repeat instruction to close other vars. */ |
886 | 0 | break; |
887 | 0 | } |
888 | 0 | case OP_RETURN: { /* yielded closing variables */ |
889 | 0 | StkId ra = base + GETARG_A(inst); |
890 | | /* adjust top to signal correct number of returns, in case the |
891 | | return is "up to top" ('isIT') */ |
892 | 0 | L->top.p = ra + ci->u2.nres; |
893 | | /* repeat instruction to close other vars. and complete the return */ |
894 | 0 | ci->u.l.savedpc--; |
895 | 0 | break; |
896 | 0 | } |
897 | 375 | default: { |
898 | | /* only these other opcodes can yield */ |
899 | 375 | lua_assert(op == OP_TFORCALL || op == OP_CALL || |
900 | 375 | op == OP_TAILCALL || op == OP_SETTABUP || op == OP_SETTABLE || |
901 | 375 | op == OP_SETI || op == OP_SETFIELD); |
902 | 375 | break; |
903 | 375 | } |
904 | 378 | } |
905 | 378 | } |
906 | | |
907 | | |
908 | | |
909 | | |
910 | | /* |
911 | | ** {================================================================== |
912 | | ** Macros for arithmetic/bitwise/comparison opcodes in 'luaV_execute' |
913 | | ** =================================================================== |
914 | | */ |
915 | | |
916 | | #define l_addi(L,a,b) intop(+, a, b) |
917 | | #define l_subi(L,a,b) intop(-, a, b) |
918 | | #define l_muli(L,a,b) intop(*, a, b) |
919 | | #define l_band(a,b) intop(&, a, b) |
920 | | #define l_bor(a,b) intop(|, a, b) |
921 | | #define l_bxor(a,b) intop(^, a, b) |
922 | | |
923 | 2.30M | #define l_lti(a,b) (a < b) |
924 | 2.44M | #define l_lei(a,b) (a <= b) |
925 | 134M | #define l_gti(a,b) (a > b) |
926 | 322k | #define l_gei(a,b) (a >= b) |
927 | | |
928 | | |
929 | | /* |
930 | | ** Arithmetic operations with immediate operands. 'iop' is the integer |
931 | | ** operation, 'fop' is the float operation. |
932 | | */ |
933 | 64.5M | #define op_arithI(L,iop,fop) { \ |
934 | 64.5M | StkId ra = RA(i); \ |
935 | 64.5M | TValue *v1 = vRB(i); \ |
936 | 64.5M | int imm = GETARG_sC(i); \ |
937 | 64.5M | if (ttisinteger(v1)) { \ |
938 | 52.7M | lua_Integer iv1 = ivalue(v1); \ |
939 | 52.7M | pc++; setivalue(s2v(ra), iop(L, iv1, imm)); \ |
940 | 52.7M | } \ |
941 | 64.5M | else if (ttisfloat(v1)) { \ |
942 | 11.2M | lua_Number nb = fltvalue(v1); \ |
943 | 11.2M | lua_Number fimm = cast_num(imm); \ |
944 | 11.2M | pc++; setfltvalue(s2v(ra), fop(L, nb, fimm)); \ |
945 | 11.2M | }} |
946 | | |
947 | | |
948 | | /* |
949 | | ** Auxiliary function for arithmetic operations over floats and others |
950 | | ** with two operands. |
951 | | */ |
952 | 63.6M | #define op_arithf_aux(L,v1,v2,fop) { \ |
953 | 63.6M | lua_Number n1; lua_Number n2; \ |
954 | 63.6M | if (tonumberns(v1, n1) && tonumberns(v2, n2)) { \ |
955 | 57.2M | pc++; setfltvalue(s2v(ra), fop(L, n1, n2)); \ |
956 | 57.2M | }} |
957 | | |
958 | | |
959 | | /* |
960 | | ** Arithmetic operations over floats and others with register operands. |
961 | | */ |
962 | 9.25M | #define op_arithf(L,fop) { \ |
963 | 9.25M | StkId ra = RA(i); \ |
964 | 9.25M | TValue *v1 = vRB(i); \ |
965 | 9.25M | TValue *v2 = vRC(i); \ |
966 | 9.25M | op_arithf_aux(L, v1, v2, fop); } |
967 | | |
968 | | |
969 | | /* |
970 | | ** Arithmetic operations with K operands for floats. |
971 | | */ |
972 | 22.2M | #define op_arithfK(L,fop) { \ |
973 | 22.2M | StkId ra = RA(i); \ |
974 | 22.2M | TValue *v1 = vRB(i); \ |
975 | 22.2M | TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ |
976 | 22.2M | op_arithf_aux(L, v1, v2, fop); } |
977 | | |
978 | | |
979 | | /* |
980 | | ** Arithmetic operations over integers and floats. |
981 | | */ |
982 | 43.3M | #define op_arith_aux(L,v1,v2,iop,fop) { \ |
983 | 43.3M | StkId ra = RA(i); \ |
984 | 43.3M | if (ttisinteger(v1) && ttisinteger(v2)) { \ |
985 | 11.1M | lua_Integer i1 = ivalue(v1); lua_Integer i2 = ivalue(v2); \ |
986 | 11.1M | pc++; setivalue(s2v(ra), iop(L, i1, i2)); \ |
987 | 11.1M | } \ |
988 | 43.3M | else op_arithf_aux(L, v1, v2, fop); } |
989 | | |
990 | | |
991 | | /* |
992 | | ** Arithmetic operations with register operands. |
993 | | */ |
994 | 22.1M | #define op_arith(L,iop,fop) { \ |
995 | 22.1M | TValue *v1 = vRB(i); \ |
996 | 22.1M | TValue *v2 = vRC(i); \ |
997 | 22.1M | op_arith_aux(L, v1, v2, iop, fop); } |
998 | | |
999 | | |
1000 | | /* |
1001 | | ** Arithmetic operations with K operands. |
1002 | | */ |
1003 | 21.1M | #define op_arithK(L,iop,fop) { \ |
1004 | 21.1M | TValue *v1 = vRB(i); \ |
1005 | 21.1M | TValue *v2 = KC(i); lua_assert(ttisnumber(v2)); \ |
1006 | 21.1M | op_arith_aux(L, v1, v2, iop, fop); } |
1007 | | |
1008 | | |
1009 | | /* |
1010 | | ** Bitwise operations with constant operand. |
1011 | | */ |
1012 | 9.53M | #define op_bitwiseK(L,op) { \ |
1013 | 9.53M | StkId ra = RA(i); \ |
1014 | 9.53M | TValue *v1 = vRB(i); \ |
1015 | 9.53M | TValue *v2 = KC(i); \ |
1016 | 9.53M | lua_Integer i1; \ |
1017 | 9.53M | lua_Integer i2 = ivalue(v2); \ |
1018 | 9.53M | if (tointegerns(v1, &i1)) { \ |
1019 | 9.51M | pc++; setivalue(s2v(ra), op(i1, i2)); \ |
1020 | 9.51M | }} |
1021 | | |
1022 | | |
1023 | | /* |
1024 | | ** Bitwise operations with register operands. |
1025 | | */ |
1026 | 4.68M | #define op_bitwise(L,op) { \ |
1027 | 4.68M | StkId ra = RA(i); \ |
1028 | 4.68M | TValue *v1 = vRB(i); \ |
1029 | 4.68M | TValue *v2 = vRC(i); \ |
1030 | 4.68M | lua_Integer i1; lua_Integer i2; \ |
1031 | 4.68M | if (tointegerns(v1, &i1) && tointegerns(v2, &i2)) { \ |
1032 | 4.53M | pc++; setivalue(s2v(ra), op(i1, i2)); \ |
1033 | 4.53M | }} |
1034 | | |
1035 | | |
1036 | | /* |
1037 | | ** Order operations with register operands. 'opn' actually works |
1038 | | ** for all numbers, but the fast track improves performance for |
1039 | | ** integers. |
1040 | | */ |
1041 | 14.9M | #define op_order(L,opi,opn,other) { \ |
1042 | 14.9M | StkId ra = RA(i); \ |
1043 | 14.9M | int cond; \ |
1044 | 14.9M | TValue *rb = vRB(i); \ |
1045 | 14.9M | if (ttisinteger(s2v(ra)) && ttisinteger(rb)) { \ |
1046 | 4.23M | lua_Integer ia = ivalue(s2v(ra)); \ |
1047 | 4.23M | lua_Integer ib = ivalue(rb); \ |
1048 | 4.23M | cond = opi(ia, ib); \ |
1049 | 4.23M | } \ |
1050 | 14.9M | else if (ttisnumber(s2v(ra)) && ttisnumber(rb)) \ |
1051 | 10.7M | cond = opn(s2v(ra), rb); \ |
1052 | 10.7M | else \ |
1053 | 10.7M | Protect(cond = other(L, s2v(ra), rb)); \ |
1054 | 14.9M | docondjump(); } |
1055 | | |
1056 | | |
1057 | | /* |
1058 | | ** Order operations with immediate operand. (Immediate operand is |
1059 | | ** always small enough to have an exact representation as a float.) |
1060 | | */ |
1061 | 39.2M | #define op_orderI(L,opi,opf,inv,tm) { \ |
1062 | 39.2M | StkId ra = RA(i); \ |
1063 | 39.2M | int cond; \ |
1064 | 39.2M | int im = GETARG_sB(i); \ |
1065 | 39.2M | if (ttisinteger(s2v(ra))) \ |
1066 | 39.2M | cond = opi(ivalue(s2v(ra)), im); \ |
1067 | 39.2M | else if (ttisfloat(s2v(ra))) { \ |
1068 | 4.41M | lua_Number fa = fltvalue(s2v(ra)); \ |
1069 | 4.41M | lua_Number fim = cast_num(im); \ |
1070 | 4.41M | cond = opf(fa, fim); \ |
1071 | 4.41M | } \ |
1072 | 4.90M | else { \ |
1073 | 489k | int isf = GETARG_C(i); \ |
1074 | 489k | Protect(cond = luaT_callorderiTM(L, s2v(ra), im, inv, isf, tm)); \ |
1075 | 489k | } \ |
1076 | 39.2M | docondjump(); } |
1077 | | |
1078 | | /* }================================================================== */ |
1079 | | |
1080 | | |
1081 | | /* |
1082 | | ** {================================================================== |
1083 | | ** Function 'luaV_execute': main interpreter loop |
1084 | | ** =================================================================== |
1085 | | */ |
1086 | | |
1087 | | /* |
1088 | | ** some macros for common tasks in 'luaV_execute' |
1089 | | */ |
1090 | | |
1091 | | |
1092 | 2.81G | #define RA(i) (base+GETARG_A(i)) |
1093 | | #define RB(i) (base+GETARG_B(i)) |
1094 | 330M | #define vRB(i) s2v(RB(i)) |
1095 | 263M | #define KB(i) (k+GETARG_B(i)) |
1096 | | #define RC(i) (base+GETARG_C(i)) |
1097 | 40.5M | #define vRC(i) s2v(RC(i)) |
1098 | 561M | #define KC(i) (k+GETARG_C(i)) |
1099 | 127M | #define RKC(i) ((TESTARG_k(i)) ? k + GETARG_C(i) : s2v(base + GETARG_C(i))) |
1100 | | |
1101 | | |
1102 | | |
1103 | 798M | #define updatetrap(ci) (trap = ci->u.l.trap) |
1104 | | |
1105 | 112M | #define updatebase(ci) (base = ci->func.p + 1) |
1106 | | |
1107 | | |
1108 | | #define updatestack(ci) \ |
1109 | 917k | { if (l_unlikely(trap)) { updatebase(ci); ra = RA(i); } } |
1110 | | |
1111 | | |
1112 | | /* |
1113 | | ** Execute a jump instruction. The 'updatetrap' allows signals to stop |
1114 | | ** tight loops. (Without it, the local copy of 'trap' could never change.) |
1115 | | */ |
1116 | 211M | #define dojump(ci,i,e) { pc += GETARG_sJ(i) + e; updatetrap(ci); } |
1117 | | |
1118 | | |
1119 | | /* for test instructions, execute the jump instruction that follows it */ |
1120 | 208M | #define donextjump(ci) { Instruction ni = *pc; dojump(ci, ni, 1); } |
1121 | | |
1122 | | /* |
1123 | | ** do a conditional jump: skip next instruction if 'cond' is not what |
1124 | | ** was expected (parameter 'k'), else do next instruction, which must |
1125 | | ** be a jump. |
1126 | | */ |
1127 | 326M | #define docondjump() if (cond != GETARG_k(i)) pc++; else donextjump(ci); |
1128 | | |
1129 | | |
1130 | | /* |
1131 | | ** Correct global 'pc'. |
1132 | | */ |
1133 | 810M | #define savepc(L) (ci->u.l.savedpc = pc) |
1134 | | |
1135 | | |
1136 | | /* |
1137 | | ** Whenever code can raise errors, the global 'pc' and the global |
1138 | | ** 'top' must be correct to report occasional errors. |
1139 | | */ |
1140 | 269M | #define savestate(L,ci) (savepc(L), L->top.p = ci->top.p) |
1141 | | |
1142 | | |
1143 | | /* |
1144 | | ** Protect code that, in general, can raise errors, reallocate the |
1145 | | ** stack, and change the hooks. |
1146 | | */ |
1147 | 225M | #define Protect(exp) (savestate(L,ci), (exp), updatetrap(ci)) |
1148 | | |
1149 | | /* special version that does not change the top */ |
1150 | 28.9M | #define ProtectNT(exp) (savepc(L), (exp), updatetrap(ci)) |
1151 | | |
1152 | | /* |
1153 | | ** Protect code that can only raise errors. (That is, it cannot change |
1154 | | ** the stack or hooks.) |
1155 | | */ |
1156 | 24.9M | #define halfProtect(exp) (savestate(L,ci), (exp)) |
1157 | | |
1158 | | /* |
1159 | | ** macro executed during Lua functions at points where the |
1160 | | ** function can yield. |
1161 | | */ |
1162 | | #if !defined(luai_threadyield) |
1163 | 56.5M | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} |
1164 | | #endif |
1165 | | |
1166 | | /* 'c' is the limit of live values in the stack */ |
1167 | | #define checkGC(L,c) \ |
1168 | 56.5M | { luaC_condGC(L, (savepc(L), L->top.p = (c)), \ |
1169 | 56.5M | updatetrap(ci)); \ |
1170 | 56.5M | luai_threadyield(L); } |
1171 | | |
1172 | | |
1173 | | /* fetch an instruction and prepare its execution */ |
1174 | 2.93G | #define vmfetch() { \ |
1175 | 2.93G | if (l_unlikely(trap)) { /* stack reallocation or hooks? */ \ |
1176 | 94.8M | trap = luaG_traceexec(L, pc); /* handle hooks */ \ |
1177 | 94.8M | updatebase(ci); /* correct stack */ \ |
1178 | 94.8M | } \ |
1179 | 2.93G | i = *(pc++); \ |
1180 | 2.93G | } |
1181 | | |
1182 | | #define vmdispatch(o) switch(o) |
1183 | | #define vmcase(l) case l: |
1184 | | #define vmbreak break |
1185 | | |
1186 | | |
1187 | 154M | void luaV_execute (lua_State *L, CallInfo *ci) { |
1188 | 154M | LClosure *cl; |
1189 | 154M | TValue *k; |
1190 | 154M | StkId base; |
1191 | 154M | const Instruction *pc; |
1192 | 154M | int trap; |
1193 | 154M | #if LUA_USE_JUMPTABLE |
1194 | 154M | #include "ljumptab.h" |
1195 | 154M | #endif |
1196 | 361M | startfunc: |
1197 | 361M | trap = L->hookmask; |
1198 | 416M | returning: /* trap already set */ |
1199 | 832M | cl = ci_func(ci); |
1200 | 0 | k = cl->p->k; |
1201 | 832M | pc = ci->u.l.savedpc; |
1202 | 832M | if (l_unlikely(trap)) |
1203 | 6.15M | trap = luaG_tracecall(L); |
1204 | 832M | base = ci->func.p + 1; |
1205 | | /* main loop of interpreter */ |
1206 | 832M | for (;;) { |
1207 | 416M | Instruction i; /* instruction being executed */ |
1208 | 416M | vmfetch(); |
1209 | | #if 0 |
1210 | | { /* low-level line tracing for debugging Lua */ |
1211 | | #include "lopnames.h" |
1212 | | int pcrel = pcRel(pc, cl->p); |
1213 | | printf("line: %d; %s (%d)\n", luaG_getfuncline(cl->p, pcrel), |
1214 | | opnames[GET_OPCODE(i)], pcrel); |
1215 | | } |
1216 | | #endif |
1217 | 416M | lua_assert(base == ci->func.p + 1); |
1218 | 416M | lua_assert(base <= L->top.p && L->top.p <= L->stack_last.p); |
1219 | | /* for tests, invalidate top for instructions not expecting it */ |
1220 | 416M | lua_assert(luaP_isIT(i) || (cast_void(L->top.p = base), 1)); |
1221 | 416M | vmdispatch (GET_OPCODE(i)) { |
1222 | 453M | vmcase(OP_MOVE) { |
1223 | 453M | StkId ra = RA(i); |
1224 | 906M | setobjs2s(L, ra, RB(i)); |
1225 | 453M | vmbreak; |
1226 | 453M | } |
1227 | 46.5M | vmcase(OP_LOADI) { |
1228 | 46.5M | StkId ra = RA(i); |
1229 | 46.5M | lua_Integer b = GETARG_sBx(i); |
1230 | 46.5M | setivalue(s2v(ra), b); |
1231 | 46.5M | vmbreak; |
1232 | 46.5M | } |
1233 | 8.20M | vmcase(OP_LOADF) { |
1234 | 8.20M | StkId ra = RA(i); |
1235 | 8.20M | int b = GETARG_sBx(i); |
1236 | 8.20M | setfltvalue(s2v(ra), cast_num(b)); |
1237 | 8.20M | vmbreak; |
1238 | 8.20M | } |
1239 | 98.7M | vmcase(OP_LOADK) { |
1240 | 98.7M | StkId ra = RA(i); |
1241 | 98.7M | TValue *rb = k + GETARG_Bx(i); |
1242 | 98.7M | setobj2s(L, ra, rb); |
1243 | 98.7M | vmbreak; |
1244 | 98.7M | } |
1245 | 0 | vmcase(OP_LOADKX) { |
1246 | 0 | StkId ra = RA(i); |
1247 | 0 | TValue *rb; |
1248 | 0 | rb = k + GETARG_Ax(*pc); pc++; |
1249 | 0 | setobj2s(L, ra, rb); |
1250 | 0 | vmbreak; |
1251 | 0 | } |
1252 | 3.99M | vmcase(OP_LOADFALSE) { |
1253 | 3.99M | StkId ra = RA(i); |
1254 | 3.99M | setbfvalue(s2v(ra)); |
1255 | 3.99M | vmbreak; |
1256 | 3.99M | } |
1257 | 6.19M | vmcase(OP_LFALSESKIP) { |
1258 | 6.19M | StkId ra = RA(i); |
1259 | 6.19M | setbfvalue(s2v(ra)); |
1260 | 6.19M | pc++; /* skip next instruction */ |
1261 | 6.19M | vmbreak; |
1262 | 6.19M | } |
1263 | 18.1M | vmcase(OP_LOADTRUE) { |
1264 | 18.1M | StkId ra = RA(i); |
1265 | 18.1M | setbtvalue(s2v(ra)); |
1266 | 18.1M | vmbreak; |
1267 | 18.1M | } |
1268 | 18.1M | vmcase(OP_LOADNIL) { |
1269 | 7.52M | StkId ra = RA(i); |
1270 | 7.52M | int b = GETARG_B(i); |
1271 | 41.2M | do { |
1272 | 41.2M | setnilvalue(s2v(ra++)); |
1273 | 41.2M | } while (b--); |
1274 | 7.52M | vmbreak; |
1275 | 7.52M | } |
1276 | 288M | vmcase(OP_GETUPVAL) { |
1277 | 288M | StkId ra = RA(i); |
1278 | 288M | int b = GETARG_B(i); |
1279 | 288M | setobj2s(L, ra, cl->upvals[b]->v.p); |
1280 | 288M | vmbreak; |
1281 | 288M | } |
1282 | 7.65M | vmcase(OP_SETUPVAL) { |
1283 | 7.65M | StkId ra = RA(i); |
1284 | 7.65M | UpVal *uv = cl->upvals[GETARG_B(i)]; |
1285 | 7.65M | setobj(L, uv->v.p, s2v(ra)); |
1286 | 7.65M | luaC_barrier(L, uv, s2v(ra)); |
1287 | 7.65M | vmbreak; |
1288 | 7.65M | } |
1289 | 392M | vmcase(OP_GETTABUP) { |
1290 | 392M | StkId ra = RA(i); |
1291 | 392M | TValue *upval = cl->upvals[GETARG_B(i)]->v.p; |
1292 | 392M | TValue *rc = KC(i); |
1293 | 785M | TString *key = tsvalue(rc); /* key must be a short string */ |
1294 | 0 | lu_byte tag; |
1295 | 785M | luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag); |
1296 | 392M | if (tagisempty(tag)) |
1297 | 47.0M | Protect(luaV_finishget(L, upval, rc, ra, tag)); |
1298 | 392M | vmbreak; |
1299 | 392M | } |
1300 | 4.49M | vmcase(OP_GETTABLE) { |
1301 | 4.49M | StkId ra = RA(i); |
1302 | 4.49M | TValue *rb = vRB(i); |
1303 | 4.49M | TValue *rc = vRC(i); |
1304 | 0 | lu_byte tag; |
1305 | 4.49M | if (ttisinteger(rc)) { /* fast track for integers? */ |
1306 | 817k | luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag); |
1307 | 817k | } |
1308 | 3.67M | else |
1309 | 3.67M | luaV_fastget(rb, rc, s2v(ra), luaH_get, tag); |
1310 | 4.49M | if (tagisempty(tag)) |
1311 | 1.83M | Protect(luaV_finishget(L, rb, rc, ra, tag)); |
1312 | 4.49M | vmbreak; |
1313 | 4.49M | } |
1314 | 609k | vmcase(OP_GETI) { |
1315 | 609k | StkId ra = RA(i); |
1316 | 609k | TValue *rb = vRB(i); |
1317 | 609k | int c = GETARG_C(i); |
1318 | 0 | lu_byte tag; |
1319 | 609k | luaV_fastgeti(rb, c, s2v(ra), tag); |
1320 | 609k | if (tagisempty(tag)) { |
1321 | 152k | TValue key; |
1322 | 152k | setivalue(&key, c); |
1323 | 152k | Protect(luaV_finishget(L, rb, &key, ra, tag)); |
1324 | 152k | } |
1325 | 609k | vmbreak; |
1326 | 609k | } |
1327 | 106M | vmcase(OP_GETFIELD) { |
1328 | 106M | StkId ra = RA(i); |
1329 | 106M | TValue *rb = vRB(i); |
1330 | 106M | TValue *rc = KC(i); |
1331 | 212M | TString *key = tsvalue(rc); /* key must be a short string */ |
1332 | 0 | lu_byte tag; |
1333 | 212M | luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); |
1334 | 106M | if (tagisempty(tag)) |
1335 | 80.0M | Protect(luaV_finishget(L, rb, rc, ra, tag)); |
1336 | 106M | vmbreak; |
1337 | 106M | } |
1338 | 57.5M | vmcase(OP_SETTABUP) { |
1339 | 57.5M | int hres; |
1340 | 57.5M | TValue *upval = cl->upvals[GETARG_A(i)]->v.p; |
1341 | 57.5M | TValue *rb = KB(i); |
1342 | 57.5M | TValue *rc = RKC(i); |
1343 | 115M | TString *key = tsvalue(rb); /* key must be a short string */ |
1344 | 57.5M | luaV_fastset(upval, key, rc, hres, luaH_psetshortstr); |
1345 | 57.5M | if (hres == HOK) |
1346 | 57.5M | luaV_finishfastset(L, upval, rc); |
1347 | 5.26M | else |
1348 | 5.26M | Protect(luaV_finishset(L, upval, rb, rc, hres)); |
1349 | 57.5M | vmbreak; |
1350 | 57.5M | } |
1351 | 15.8M | vmcase(OP_SETTABLE) { |
1352 | 15.8M | StkId ra = RA(i); |
1353 | 15.8M | int hres; |
1354 | 15.8M | TValue *rb = vRB(i); /* key (table is in 'ra') */ |
1355 | 15.8M | TValue *rc = RKC(i); /* value */ |
1356 | 15.8M | if (ttisinteger(rb)) { /* fast track for integers? */ |
1357 | 9.99M | luaV_fastseti(s2v(ra), ivalue(rb), rc, hres); |
1358 | 9.99M | } |
1359 | 5.83M | else { |
1360 | 5.83M | luaV_fastset(s2v(ra), rb, rc, hres, luaH_pset); |
1361 | 5.83M | } |
1362 | 15.8M | if (hres == HOK) |
1363 | 15.8M | luaV_finishfastset(L, s2v(ra), rc); |
1364 | 7.35M | else |
1365 | 7.35M | Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); |
1366 | 15.8M | vmbreak; |
1367 | 15.8M | } |
1368 | 2.31M | vmcase(OP_SETI) { |
1369 | 2.31M | StkId ra = RA(i); |
1370 | 2.31M | int hres; |
1371 | 2.31M | int b = GETARG_B(i); |
1372 | 2.31M | TValue *rc = RKC(i); |
1373 | 2.31M | luaV_fastseti(s2v(ra), b, rc, hres); |
1374 | 2.31M | if (hres == HOK) |
1375 | 2.31M | luaV_finishfastset(L, s2v(ra), rc); |
1376 | 351k | else { |
1377 | 351k | TValue key; |
1378 | 351k | setivalue(&key, b); |
1379 | 351k | Protect(luaV_finishset(L, s2v(ra), &key, rc, hres)); |
1380 | 351k | } |
1381 | 2.31M | vmbreak; |
1382 | 2.31M | } |
1383 | 51.8M | vmcase(OP_SETFIELD) { |
1384 | 51.8M | StkId ra = RA(i); |
1385 | 51.8M | int hres; |
1386 | 51.8M | TValue *rb = KB(i); |
1387 | 51.8M | TValue *rc = RKC(i); |
1388 | 103M | TString *key = tsvalue(rb); /* key must be a short string */ |
1389 | 51.8M | luaV_fastset(s2v(ra), key, rc, hres, luaH_psetshortstr); |
1390 | 51.8M | if (hres == HOK) |
1391 | 51.8M | luaV_finishfastset(L, s2v(ra), rc); |
1392 | 36.1M | else |
1393 | 36.1M | Protect(luaV_finishset(L, s2v(ra), rb, rc, hres)); |
1394 | 51.8M | vmbreak; |
1395 | 51.8M | } |
1396 | 21.1M | vmcase(OP_NEWTABLE) { |
1397 | 21.1M | StkId ra = RA(i); |
1398 | 21.1M | unsigned b = cast_uint(GETARG_vB(i)); /* log2(hash size) + 1 */ |
1399 | 21.1M | unsigned c = cast_uint(GETARG_vC(i)); /* array size */ |
1400 | 0 | Table *t; |
1401 | 21.1M | if (b > 0) |
1402 | 6.63M | b = 1u << (b - 1); /* hash size is 2^(b - 1) */ |
1403 | 21.1M | if (TESTARG_k(i)) { /* non-zero extra argument? */ |
1404 | 10.4k | lua_assert(GETARG_Ax(*pc) != 0); |
1405 | | /* add it to array size */ |
1406 | 10.4k | c += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); |
1407 | 10.4k | } |
1408 | 21.1M | pc++; /* skip extra argument */ |
1409 | 21.1M | L->top.p = ra + 1; /* correct top in case of emergency GC */ |
1410 | 21.1M | t = luaH_new(L); /* memory allocation */ |
1411 | 21.1M | sethvalue2s(L, ra, t); |
1412 | 21.1M | if (b != 0 || c != 0) |
1413 | 8.12M | luaH_resize(L, t, c, b); /* idem */ |
1414 | 21.1M | checkGC(L, ra + 1); |
1415 | 21.1M | vmbreak; |
1416 | 21.1M | } |
1417 | 9.95M | vmcase(OP_SELF) { |
1418 | 9.95M | StkId ra = RA(i); |
1419 | 9.95M | lu_byte tag; |
1420 | 9.95M | TValue *rb = vRB(i); |
1421 | 9.95M | TValue *rc = KC(i); |
1422 | 19.9M | TString *key = tsvalue(rc); /* key must be a short string */ |
1423 | 9.95M | setobj2s(L, ra + 1, rb); |
1424 | 9.95M | luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag); |
1425 | 9.95M | if (tagisempty(tag)) |
1426 | 9.43M | Protect(luaV_finishget(L, rb, rc, ra, tag)); |
1427 | 9.95M | vmbreak; |
1428 | 9.95M | } |
1429 | 64.5M | vmcase(OP_ADDI) { |
1430 | 129M | op_arithI(L, l_addi, luai_numadd); |
1431 | 64.5M | vmbreak; |
1432 | 64.5M | } |
1433 | 6.70M | vmcase(OP_ADDK) { |
1434 | 26.8M | op_arithK(L, l_addi, luai_numadd); |
1435 | 26.8M | vmbreak; |
1436 | 26.8M | } |
1437 | 2.61M | vmcase(OP_SUBK) { |
1438 | 10.4M | op_arithK(L, l_subi, luai_numsub); |
1439 | 10.4M | vmbreak; |
1440 | 10.4M | } |
1441 | 440k | vmcase(OP_MULK) { |
1442 | 1.76M | op_arithK(L, l_muli, luai_nummul); |
1443 | 1.76M | vmbreak; |
1444 | 1.76M | } |
1445 | 4.85M | vmcase(OP_MODK) { |
1446 | 4.85M | savestate(L, ci); /* in case of division by 0 */ |
1447 | 19.4M | op_arithK(L, luaV_mod, luaV_modf); |
1448 | 19.4M | vmbreak; |
1449 | 19.4M | } |
1450 | 6.44M | vmcase(OP_POWK) { |
1451 | 19.3M | op_arithfK(L, luai_numpow); |
1452 | 19.3M | vmbreak; |
1453 | 19.3M | } |
1454 | 15.7M | vmcase(OP_DIVK) { |
1455 | 47.3M | op_arithfK(L, luai_numdiv); |
1456 | 47.3M | vmbreak; |
1457 | 47.3M | } |
1458 | 6.54M | vmcase(OP_IDIVK) { |
1459 | 6.54M | savestate(L, ci); /* in case of division by 0 */ |
1460 | 26.1M | op_arithK(L, luaV_idiv, luai_numidiv); |
1461 | 26.1M | vmbreak; |
1462 | 26.1M | } |
1463 | 72.7k | vmcase(OP_BANDK) { |
1464 | 218k | op_bitwiseK(L, l_band); |
1465 | 218k | vmbreak; |
1466 | 218k | } |
1467 | 643k | vmcase(OP_BORK) { |
1468 | 1.92M | op_bitwiseK(L, l_bor); |
1469 | 1.92M | vmbreak; |
1470 | 1.92M | } |
1471 | 8.81M | vmcase(OP_BXORK) { |
1472 | 26.4M | op_bitwiseK(L, l_bxor); |
1473 | 26.4M | vmbreak; |
1474 | 26.4M | } |
1475 | 38.6k | vmcase(OP_SHRI) { |
1476 | 38.6k | StkId ra = RA(i); |
1477 | 38.6k | TValue *rb = vRB(i); |
1478 | 38.6k | int ic = GETARG_sC(i); |
1479 | 0 | lua_Integer ib; |
1480 | 38.6k | if (tointegerns(rb, &ib)) { |
1481 | 26.2k | pc++; setivalue(s2v(ra), luaV_shiftl(ib, -ic)); |
1482 | 26.2k | } |
1483 | 38.6k | vmbreak; |
1484 | 38.6k | } |
1485 | 196k | vmcase(OP_SHLI) { |
1486 | 196k | StkId ra = RA(i); |
1487 | 196k | TValue *rb = vRB(i); |
1488 | 196k | int ic = GETARG_sC(i); |
1489 | 0 | lua_Integer ib; |
1490 | 196k | if (tointegerns(rb, &ib)) { |
1491 | 186k | pc++; setivalue(s2v(ra), luaV_shiftl(ic, ib)); |
1492 | 186k | } |
1493 | 196k | vmbreak; |
1494 | 196k | } |
1495 | 9.05M | vmcase(OP_ADD) { |
1496 | 27.1M | op_arith(L, l_addi, luai_numadd); |
1497 | 27.1M | vmbreak; |
1498 | 27.1M | } |
1499 | 5.17M | vmcase(OP_SUB) { |
1500 | 15.5M | op_arith(L, l_subi, luai_numsub); |
1501 | 15.5M | vmbreak; |
1502 | 15.5M | } |
1503 | 4.07M | vmcase(OP_MUL) { |
1504 | 12.2M | op_arith(L, l_muli, luai_nummul); |
1505 | 12.2M | vmbreak; |
1506 | 12.2M | } |
1507 | 1.89M | vmcase(OP_MOD) { |
1508 | 1.89M | savestate(L, ci); /* in case of division by 0 */ |
1509 | 5.67M | op_arith(L, luaV_mod, luaV_modf); |
1510 | 5.67M | vmbreak; |
1511 | 5.67M | } |
1512 | 4.58M | vmcase(OP_POW) { |
1513 | 9.16M | op_arithf(L, luai_numpow); |
1514 | 9.16M | vmbreak; |
1515 | 9.16M | } |
1516 | 4.66M | vmcase(OP_DIV) { /* float division (always with floats) */ |
1517 | 9.33M | op_arithf(L, luai_numdiv); |
1518 | 9.33M | vmbreak; |
1519 | 9.33M | } |
1520 | 1.96M | vmcase(OP_IDIV) { /* floor division */ |
1521 | 1.96M | savestate(L, ci); /* in case of division by 0 */ |
1522 | 5.90M | op_arith(L, luaV_idiv, luai_numidiv); |
1523 | 5.90M | vmbreak; |
1524 | 5.90M | } |
1525 | 263k | vmcase(OP_BAND) { |
1526 | 526k | op_bitwise(L, l_band); |
1527 | 526k | vmbreak; |
1528 | 526k | } |
1529 | 2.07M | vmcase(OP_BOR) { |
1530 | 4.14M | op_bitwise(L, l_bor); |
1531 | 4.14M | vmbreak; |
1532 | 4.14M | } |
1533 | 629k | vmcase(OP_BXOR) { |
1534 | 1.25M | op_bitwise(L, l_bxor); |
1535 | 1.25M | vmbreak; |
1536 | 1.25M | } |
1537 | 1.51M | vmcase(OP_SHR) { |
1538 | 3.03M | op_bitwise(L, luaV_shiftr); |
1539 | 3.03M | vmbreak; |
1540 | 3.03M | } |
1541 | 203k | vmcase(OP_SHL) { |
1542 | 406k | op_bitwise(L, luaV_shiftl); |
1543 | 406k | vmbreak; |
1544 | 406k | } |
1545 | 6.04M | vmcase(OP_MMBIN) { |
1546 | 6.04M | StkId ra = RA(i); |
1547 | 6.04M | Instruction pi = *(pc - 2); /* original arith. expression */ |
1548 | 6.04M | TValue *rb = vRB(i); |
1549 | 6.04M | TMS tm = (TMS)GETARG_C(i); |
1550 | 6.04M | StkId result = RA(pi); |
1551 | 6.04M | lua_assert(OP_ADD <= GET_OPCODE(pi) && GET_OPCODE(pi) <= OP_SHR); |
1552 | 6.04M | Protect(luaT_trybinTM(L, s2v(ra), rb, result, tm)); |
1553 | 6.04M | vmbreak; |
1554 | 6.04M | } |
1555 | 548k | vmcase(OP_MMBINI) { |
1556 | 548k | StkId ra = RA(i); |
1557 | 548k | Instruction pi = *(pc - 2); /* original arith. expression */ |
1558 | 548k | int imm = GETARG_sB(i); |
1559 | 548k | TMS tm = (TMS)GETARG_C(i); |
1560 | 548k | int flip = GETARG_k(i); |
1561 | 548k | StkId result = RA(pi); |
1562 | 548k | Protect(luaT_trybiniTM(L, s2v(ra), imm, flip, result, tm)); |
1563 | 548k | vmbreak; |
1564 | 548k | } |
1565 | 500k | vmcase(OP_MMBINK) { |
1566 | 500k | StkId ra = RA(i); |
1567 | 500k | Instruction pi = *(pc - 2); /* original arith. expression */ |
1568 | 500k | TValue *imm = KB(i); |
1569 | 500k | TMS tm = (TMS)GETARG_C(i); |
1570 | 500k | int flip = GETARG_k(i); |
1571 | 500k | StkId result = RA(pi); |
1572 | 500k | Protect(luaT_trybinassocTM(L, s2v(ra), imm, flip, result, tm)); |
1573 | 500k | vmbreak; |
1574 | 500k | } |
1575 | 3.87M | vmcase(OP_UNM) { |
1576 | 3.87M | StkId ra = RA(i); |
1577 | 3.87M | TValue *rb = vRB(i); |
1578 | 0 | lua_Number nb; |
1579 | 3.87M | if (ttisinteger(rb)) { |
1580 | 1.48M | lua_Integer ib = ivalue(rb); |
1581 | 1.48M | setivalue(s2v(ra), intop(-, 0, ib)); |
1582 | 1.48M | } |
1583 | 2.39M | else if (tonumberns(rb, nb)) { |
1584 | 2.11M | setfltvalue(s2v(ra), luai_numunm(L, nb)); |
1585 | 2.11M | } |
1586 | 277k | else |
1587 | 277k | Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); |
1588 | 3.87M | vmbreak; |
1589 | 3.87M | } |
1590 | 7.63M | vmcase(OP_BNOT) { |
1591 | 7.63M | StkId ra = RA(i); |
1592 | 7.63M | TValue *rb = vRB(i); |
1593 | 0 | lua_Integer ib; |
1594 | 7.63M | if (tointegerns(rb, &ib)) { |
1595 | 7.60M | setivalue(s2v(ra), intop(^, ~l_castS2U(0), ib)); |
1596 | 7.60M | } |
1597 | 28.1k | else |
1598 | 28.1k | Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); |
1599 | 7.63M | vmbreak; |
1600 | 7.63M | } |
1601 | 502k | vmcase(OP_NOT) { |
1602 | 502k | StkId ra = RA(i); |
1603 | 502k | TValue *rb = vRB(i); |
1604 | 502k | if (l_isfalse(rb)) |
1605 | 502k | setbtvalue(s2v(ra)); |
1606 | 270k | else |
1607 | 502k | setbfvalue(s2v(ra)); |
1608 | 502k | vmbreak; |
1609 | 502k | } |
1610 | 11.8M | vmcase(OP_LEN) { |
1611 | 11.8M | StkId ra = RA(i); |
1612 | 11.8M | Protect(luaV_objlen(L, ra, vRB(i))); |
1613 | 11.8M | vmbreak; |
1614 | 11.8M | } |
1615 | 10.7M | vmcase(OP_CONCAT) { |
1616 | 10.7M | StkId ra = RA(i); |
1617 | 10.7M | int n = GETARG_B(i); /* number of elements to concatenate */ |
1618 | 0 | L->top.p = ra + n; /* mark the end of concat operands */ |
1619 | 10.7M | ProtectNT(luaV_concat(L, n)); |
1620 | 10.7M | checkGC(L, L->top.p); /* 'luaV_concat' ensures correct top */ |
1621 | 10.7M | vmbreak; |
1622 | 10.7M | } |
1623 | 6.66M | vmcase(OP_CLOSE) { |
1624 | 6.66M | StkId ra = RA(i); |
1625 | 6.66M | lua_assert(!GETARG_B(i)); /* 'close must be alive */ |
1626 | 6.66M | Protect(luaF_close(L, ra, LUA_OK, 1)); |
1627 | 6.66M | vmbreak; |
1628 | 6.66M | } |
1629 | 2.75k | vmcase(OP_TBC) { |
1630 | 2.75k | StkId ra = RA(i); |
1631 | | /* create new to-be-closed upvalue */ |
1632 | 2.75k | halfProtect(luaF_newtbcupval(L, ra)); |
1633 | 2.75k | vmbreak; |
1634 | 2.75k | } |
1635 | 3.06M | vmcase(OP_JMP) { |
1636 | 3.06M | dojump(ci, i, 0); |
1637 | 3.06M | vmbreak; |
1638 | 3.06M | } |
1639 | 5.42M | vmcase(OP_EQ) { |
1640 | 5.42M | StkId ra = RA(i); |
1641 | 5.42M | int cond; |
1642 | 5.42M | TValue *rb = vRB(i); |
1643 | 5.42M | Protect(cond = luaV_equalobj(L, s2v(ra), rb)); |
1644 | 5.42M | docondjump(); |
1645 | 5.42M | vmbreak; |
1646 | 5.42M | } |
1647 | 11.3M | vmcase(OP_LT) { |
1648 | 33.9M | op_order(L, l_lti, LTnum, lessthanothers); |
1649 | 33.9M | vmbreak; |
1650 | 33.9M | } |
1651 | 3.64M | vmcase(OP_LE) { |
1652 | 10.9M | op_order(L, l_lei, LEnum, lessequalothers); |
1653 | 10.9M | vmbreak; |
1654 | 10.9M | } |
1655 | 154M | vmcase(OP_EQK) { |
1656 | 154M | StkId ra = RA(i); |
1657 | 154M | TValue *rb = KB(i); |
1658 | | /* basic types do not use '__eq'; we can use raw equality */ |
1659 | 154M | int cond = luaV_rawequalobj(s2v(ra), rb); |
1660 | 154M | docondjump(); |
1661 | 154M | vmbreak; |
1662 | 154M | } |
1663 | 579k | vmcase(OP_EQI) { |
1664 | 579k | StkId ra = RA(i); |
1665 | 579k | int cond; |
1666 | 579k | int im = GETARG_sB(i); |
1667 | 579k | if (ttisinteger(s2v(ra))) |
1668 | 476k | cond = (ivalue(s2v(ra)) == im); |
1669 | 103k | else if (ttisfloat(s2v(ra))) |
1670 | 86.6k | cond = luai_numeq(fltvalue(s2v(ra)), cast_num(im)); |
1671 | 16.4k | else |
1672 | 16.4k | cond = 0; /* other types cannot be equal to a number */ |
1673 | 1.15M | docondjump(); |
1674 | 1.15M | vmbreak; |
1675 | 1.15M | } |
1676 | 463k | vmcase(OP_LTI) { |
1677 | 1.39M | op_orderI(L, l_lti, luai_numlt, 0, TM_LT); |
1678 | 1.39M | vmbreak; |
1679 | 1.39M | } |
1680 | 97.0k | vmcase(OP_LEI) { |
1681 | 290k | op_orderI(L, l_lei, luai_numle, 0, TM_LE); |
1682 | 290k | vmbreak; |
1683 | 290k | } |
1684 | 35.3M | vmcase(OP_GTI) { |
1685 | 105M | op_orderI(L, l_gti, luai_numgt, 1, TM_LT); |
1686 | 105M | vmbreak; |
1687 | 105M | } |
1688 | 3.33M | vmcase(OP_GEI) { |
1689 | 10.0M | op_orderI(L, l_gei, luai_numge, 1, TM_LE); |
1690 | 10.0M | vmbreak; |
1691 | 10.0M | } |
1692 | 112M | vmcase(OP_TEST) { |
1693 | 112M | StkId ra = RA(i); |
1694 | 112M | int cond = !l_isfalse(s2v(ra)); |
1695 | 112M | docondjump(); |
1696 | 112M | vmbreak; |
1697 | 112M | } |
1698 | 815k | vmcase(OP_TESTSET) { |
1699 | 815k | StkId ra = RA(i); |
1700 | 815k | TValue *rb = vRB(i); |
1701 | 815k | if (l_isfalse(rb) == GETARG_k(i)) |
1702 | 779k | pc++; |
1703 | 35.8k | else { |
1704 | 35.8k | setobj2s(L, ra, rb); |
1705 | 35.8k | donextjump(ci); |
1706 | 35.8k | } |
1707 | 815k | vmbreak; |
1708 | 815k | } |
1709 | 410M | vmcase(OP_CALL) { |
1710 | 410M | StkId ra = RA(i); |
1711 | 410M | CallInfo *newci; |
1712 | 410M | int b = GETARG_B(i); |
1713 | 410M | int nresults = GETARG_C(i) - 1; |
1714 | 410M | if (b != 0) /* fixed number of arguments? */ |
1715 | 396M | L->top.p = ra + b; /* top signals number of arguments */ |
1716 | | /* else previous instruction set top */ |
1717 | 410M | savepc(L); /* in case of errors */ |
1718 | 410M | if ((newci = luaD_precall(L, ra, nresults)) == NULL) |
1719 | 296M | updatetrap(ci); /* C call; nothing else to be done */ |
1720 | 114M | else { /* Lua call: run function in this same C frame */ |
1721 | 114M | ci = newci; |
1722 | 114M | goto startfunc; |
1723 | 114M | } |
1724 | 410M | vmbreak; |
1725 | 296M | } |
1726 | 92.6M | vmcase(OP_TAILCALL) { |
1727 | 92.6M | StkId ra = RA(i); |
1728 | 92.6M | int b = GETARG_B(i); /* number of arguments + 1 (function) */ |
1729 | 0 | int n; /* number of results when calling a C function */ |
1730 | 92.6M | int nparams1 = GETARG_C(i); |
1731 | | /* delta is virtual 'func' - real 'func' (vararg functions) */ |
1732 | 92.6M | int delta = (nparams1) ? ci->u.l.nextraargs + nparams1 : 0; |
1733 | 92.6M | if (b != 0) |
1734 | 92.6M | L->top.p = ra + b; |
1735 | 3.34k | else /* previous instruction set top */ |
1736 | 3.34k | b = cast_int(L->top.p - ra); |
1737 | 92.6M | savepc(ci); /* several calls here can raise errors */ |
1738 | 92.6M | if (TESTARG_k(i)) { |
1739 | 48.0k | luaF_closeupval(L, base); /* close upvalues from current call */ |
1740 | 48.0k | lua_assert(L->tbclist.p < base); /* no pending tbc variables */ |
1741 | 48.0k | lua_assert(base == ci->func.p + 1); |
1742 | 48.0k | } |
1743 | 92.6M | if ((n = luaD_pretailcall(L, ci, ra, b, delta)) < 0) /* Lua function? */ |
1744 | 92.6M | goto startfunc; /* execute the callee */ |
1745 | 17.3k | else { /* C function? */ |
1746 | 17.3k | ci->func.p -= delta; /* restore 'func' (if vararg) */ |
1747 | 17.3k | luaD_poscall(L, ci, n); /* finish caller */ |
1748 | 17.3k | updatetrap(ci); /* 'luaD_poscall' can change hooks */ |
1749 | 17.3k | goto ret; /* caller returns after the tail call */ |
1750 | 17.3k | } |
1751 | 92.6M | } |
1752 | 3.74M | vmcase(OP_RETURN) { |
1753 | 3.74M | StkId ra = RA(i); |
1754 | 3.74M | int n = GETARG_B(i) - 1; /* number of results */ |
1755 | 3.74M | int nparams1 = GETARG_C(i); |
1756 | 3.74M | if (n < 0) /* not fixed? */ |
1757 | 6.09k | n = cast_int(L->top.p - ra); /* get what is available */ |
1758 | 3.74M | savepc(ci); |
1759 | 3.74M | if (TESTARG_k(i)) { /* may there be open upvalues? */ |
1760 | 280k | ci->u2.nres = n; /* save number of returns */ |
1761 | 280k | if (L->top.p < ci->top.p) |
1762 | 56.6k | L->top.p = ci->top.p; |
1763 | 280k | luaF_close(L, base, CLOSEKTOP, 1); |
1764 | 280k | updatetrap(ci); |
1765 | 280k | updatestack(ci); |
1766 | 280k | } |
1767 | 3.74M | if (nparams1) /* vararg function? */ |
1768 | 3.59M | ci->func.p -= ci->u.l.nextraargs + nparams1; |
1769 | 3.74M | L->top.p = ra + n; /* set call for 'luaD_poscall' */ |
1770 | 3.74M | luaD_poscall(L, ci, n); |
1771 | 3.74M | updatetrap(ci); /* 'luaD_poscall' can change hooks */ |
1772 | 3.74M | goto ret; |
1773 | 3.74M | } |
1774 | 46.8M | vmcase(OP_RETURN0) { |
1775 | 46.8M | if (l_unlikely(L->hookmask)) { |
1776 | 4.83M | StkId ra = RA(i); |
1777 | 4.83M | L->top.p = ra; |
1778 | 4.83M | savepc(ci); |
1779 | 4.83M | luaD_poscall(L, ci, 0); /* no hurry... */ |
1780 | 4.83M | trap = 1; |
1781 | 4.83M | } |
1782 | 42.0M | else { /* do the 'poscall' here */ |
1783 | 42.0M | int nres = get_nresults(ci->callstatus); |
1784 | 42.0M | L->ci = ci->previous; /* back to caller */ |
1785 | 42.0M | L->top.p = base - 1; |
1786 | 42.0M | for (; l_unlikely(nres > 0); nres--) |
1787 | 42.0M | setnilvalue(s2v(L->top.p++)); /* all results are nil */ |
1788 | 42.0M | } |
1789 | 46.8M | goto ret; |
1790 | 3.74M | } |
1791 | 158M | vmcase(OP_RETURN1) { |
1792 | 158M | if (l_unlikely(L->hookmask)) { |
1793 | 56.0k | StkId ra = RA(i); |
1794 | 56.0k | L->top.p = ra + 1; |
1795 | 56.0k | savepc(ci); |
1796 | 56.0k | luaD_poscall(L, ci, 1); /* no hurry... */ |
1797 | 56.0k | trap = 1; |
1798 | 56.0k | } |
1799 | 157M | else { /* do the 'poscall' here */ |
1800 | 157M | int nres = get_nresults(ci->callstatus); |
1801 | 157M | L->ci = ci->previous; /* back to caller */ |
1802 | 157M | if (nres == 0) |
1803 | 5.01M | L->top.p = base - 1; /* asked for no results */ |
1804 | 152M | else { |
1805 | 152M | StkId ra = RA(i); |
1806 | 152M | setobjs2s(L, base - 1, ra); /* at least this result */ |
1807 | 152M | L->top.p = base; |
1808 | 152M | for (; l_unlikely(nres > 1); nres--) |
1809 | 152M | setnilvalue(s2v(L->top.p++)); /* complete missing results */ |
1810 | 152M | } |
1811 | 157M | } |
1812 | 208M | ret: /* return from a Lua function */ |
1813 | 208M | if (ci->callstatus & CIST_FRESH) |
1814 | 153M | return; /* end this frame */ |
1815 | 55.3M | else { |
1816 | 55.3M | ci = ci->previous; |
1817 | 55.3M | goto returning; /* continue running caller in this frame */ |
1818 | 55.3M | } |
1819 | 208M | } |
1820 | 31.4M | vmcase(OP_FORLOOP) { |
1821 | 31.4M | StkId ra = RA(i); |
1822 | 31.4M | if (ttisinteger(s2v(ra + 1))) { /* integer loop? */ |
1823 | 26.2M | lua_Unsigned count = l_castS2U(ivalue(s2v(ra))); |
1824 | 26.2M | if (count > 0) { /* still more iterations? */ |
1825 | 25.5M | lua_Integer step = ivalue(s2v(ra + 1)); |
1826 | 25.5M | lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */ |
1827 | 25.5M | chgivalue(s2v(ra), l_castU2S(count - 1)); /* update counter */ |
1828 | 25.5M | idx = intop(+, idx, step); /* add step to index */ |
1829 | 25.5M | chgivalue(s2v(ra + 2), idx); /* update control variable */ |
1830 | 25.5M | pc -= GETARG_Bx(i); /* jump back */ |
1831 | 25.5M | } |
1832 | 26.2M | } |
1833 | 5.22M | else if (floatforloop(ra)) /* float loop */ |
1834 | 4.90M | pc -= GETARG_Bx(i); /* jump back */ |
1835 | 31.4M | updatetrap(ci); /* allows a signal to break the loop */ |
1836 | 31.4M | vmbreak; |
1837 | 31.4M | } |
1838 | 3.50M | vmcase(OP_FORPREP) { |
1839 | 3.50M | StkId ra = RA(i); |
1840 | 3.50M | savestate(L, ci); /* in case of errors */ |
1841 | 3.50M | if (forprep(L, ra)) |
1842 | 1.83M | pc += GETARG_Bx(i) + 1; /* skip the loop */ |
1843 | 3.50M | vmbreak; |
1844 | 3.50M | } |
1845 | 289k | vmcase(OP_TFORPREP) { |
1846 | | /* before: 'ra' has the iterator function, 'ra + 1' has the state, |
1847 | | 'ra + 2' has the initial value for the control variable, and |
1848 | | 'ra + 3' has the closing variable. This opcode then swaps the |
1849 | | control and the closing variables and marks the closing variable |
1850 | | as to-be-closed. |
1851 | | */ |
1852 | 289k | StkId ra = RA(i); |
1853 | 289k | TValue temp; /* to swap control and closing variables */ |
1854 | 289k | setobj(L, &temp, s2v(ra + 3)); |
1855 | 289k | setobjs2s(L, ra + 3, ra + 2); |
1856 | 289k | setobj2s(L, ra + 2, &temp); |
1857 | | /* create to-be-closed upvalue (if closing var. is not nil) */ |
1858 | 289k | halfProtect(luaF_newtbcupval(L, ra + 2)); |
1859 | 289k | pc += GETARG_Bx(i); /* go to end of the loop */ |
1860 | 0 | i = *(pc++); /* fetch next instruction */ |
1861 | 288k | lua_assert(GET_OPCODE(i) == OP_TFORCALL && ra == RA(i)); |
1862 | 288k | goto l_tforcall; |
1863 | 288k | } |
1864 | 347k | vmcase(OP_TFORCALL) { |
1865 | 636k | l_tforcall: { |
1866 | | /* 'ra' has the iterator function, 'ra + 1' has the state, |
1867 | | 'ra + 2' has the closing variable, and 'ra + 3' has the control |
1868 | | variable. The call will use the stack starting at 'ra + 3', |
1869 | | so that it preserves the first three values, and the first |
1870 | | return will be the new value for the control variable. |
1871 | | */ |
1872 | 636k | StkId ra = RA(i); |
1873 | 636k | setobjs2s(L, ra + 5, ra + 3); /* copy the control variable */ |
1874 | 636k | setobjs2s(L, ra + 4, ra + 1); /* copy state */ |
1875 | 636k | setobjs2s(L, ra + 3, ra); /* copy function */ |
1876 | 636k | L->top.p = ra + 3 + 3; |
1877 | 636k | ProtectNT(luaD_call(L, ra + 3, GETARG_C(i))); /* do the call */ |
1878 | 636k | updatestack(ci); /* stack may have changed */ |
1879 | 636k | i = *(pc++); /* go to next instruction */ |
1880 | 636k | lua_assert(GET_OPCODE(i) == OP_TFORLOOP && ra == RA(i)); |
1881 | 632k | goto l_tforloop; |
1882 | 636k | }} |
1883 | 632k | vmcase(OP_TFORLOOP) { |
1884 | 632k | l_tforloop: { |
1885 | 632k | StkId ra = RA(i); |
1886 | 632k | if (!ttisnil(s2v(ra + 3))) /* continue loop? */ |
1887 | 533k | pc -= GETARG_Bx(i); /* jump back */ |
1888 | 632k | vmbreak; |
1889 | 632k | }} |
1890 | 2.94M | vmcase(OP_SETLIST) { |
1891 | 2.94M | StkId ra = RA(i); |
1892 | 2.94M | unsigned n = cast_uint(GETARG_vB(i)); |
1893 | 2.94M | unsigned int last = cast_uint(GETARG_vC(i)); |
1894 | 5.89M | Table *h = hvalue(s2v(ra)); |
1895 | 2.94M | if (n == 0) |
1896 | 191k | n = cast_uint(L->top.p - ra) - 1; /* get up to the top */ |
1897 | 2.75M | else |
1898 | 2.75M | L->top.p = ci->top.p; /* correct top in case of emergency GC */ |
1899 | 5.89M | last += n; |
1900 | 5.89M | if (TESTARG_k(i)) { |
1901 | 59.2k | last += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1); |
1902 | 0 | pc++; |
1903 | 59.2k | } |
1904 | | /* when 'n' is known, table should have proper size */ |
1905 | 2.94M | if (last > h->asize) { /* needs more space? */ |
1906 | | /* fixed-size sets should have space preallocated */ |
1907 | 169k | lua_assert(GETARG_vB(i) == 0); |
1908 | 169k | luaH_resizearray(L, h, last); /* preallocate it at once */ |
1909 | 169k | } |
1910 | 25.2M | for (; n > 0; n--) { |
1911 | 22.2M | TValue *val = s2v(ra + n); |
1912 | 22.2M | obj2arr(h, last - 1, val); |
1913 | 22.2M | last--; |
1914 | 22.2M | luaC_barrierback(L, obj2gco(h), val); |
1915 | 22.2M | } |
1916 | 2.94M | vmbreak; |
1917 | 2.94M | } |
1918 | 24.6M | vmcase(OP_CLOSURE) { |
1919 | 24.6M | StkId ra = RA(i); |
1920 | 24.6M | Proto *p = cl->p->p[GETARG_Bx(i)]; |
1921 | 24.6M | halfProtect(pushclosure(L, p, cl->upvals, base, ra)); |
1922 | 24.6M | checkGC(L, ra + 1); |
1923 | 24.6M | vmbreak; |
1924 | 24.6M | } |
1925 | 4.17M | vmcase(OP_VARARG) { |
1926 | 4.17M | StkId ra = RA(i); |
1927 | 4.17M | int n = GETARG_C(i) - 1; /* required results */ |
1928 | 4.17M | Protect(luaT_getvarargs(L, ci, ra, n)); |
1929 | 4.17M | vmbreak; |
1930 | 4.17M | } |
1931 | 17.5M | vmcase(OP_VARARGPREP) { |
1932 | 17.5M | ProtectNT(luaT_adjustvarargs(L, GETARG_A(i), ci, cl->p)); |
1933 | 17.5M | if (l_unlikely(trap)) { /* previous "Protect" updated trap */ |
1934 | 1.37M | luaD_hookcall(L, ci); |
1935 | 1.37M | L->oldpc = 1; /* next opcode will be seen as a "new" line */ |
1936 | 1.37M | } |
1937 | 17.5M | updatebase(ci); /* function has new base after adjustment */ |
1938 | 17.5M | vmbreak; |
1939 | 17.5M | } |
1940 | 17.5M | vmcase(OP_EXTRAARG) { |
1941 | 0 | lua_assert(0); |
1942 | 0 | vmbreak; |
1943 | 0 | } |
1944 | 0 | } |
1945 | 0 | } |
1946 | 832M | } |
1947 | | |
1948 | | /* }================================================================== */ |