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