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