xref: /minix3/external/mit/lua/dist/src/lvm.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: lvm.c,v 1.7 2015/10/08 13:40:16 mbalmer Exp $	*/
2 
3 /*
4 ** Id: lvm.c,v 2.245 2015/06/09 15:53:35 roberto Exp
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
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 /* limit for table tag-method chains (to avoid loops) */
39 #define MAXTAGLOOP	2000
40 
41 
42 #ifndef _KERNEL
43 /*
44 ** 'l_intfitsf' checks whether a given integer can be converted to a
45 ** float without rounding. Used in comparisons. Left undefined if
46 ** all integers fit in a float precisely.
47 */
48 #if !defined(l_intfitsf)
49 
50 /* number of bits in the mantissa of a float */
51 #define NBM		(l_mathlim(MANT_DIG))
52 
53 /*
54 ** Check whether some integers may not fit in a float, that is, whether
55 ** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger).
56 ** (The shifts are done in parts to avoid shifting by more than the size
57 ** of an integer. In a worst case, NBM == 113 for long double and
58 ** sizeof(integer) == 32.)
59 */
60 #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \
61 	>> (NBM - (3 * (NBM / 4))))  >  0
62 
63 #define l_intfitsf(i)  \
64   (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM))
65 
66 #endif
67 
68 #endif
69 #endif /*_KERNEL */
70 
71 #ifndef _KERNEL
72 /*
73 ** Try to convert a value to a float. The float case is already handled
74 ** by the macro 'tonumber'.
75 */
luaV_tonumber_(const TValue * obj,lua_Number * n)76 int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
77   TValue v;
78   if (ttisinteger(obj)) {
79     *n = cast_num(ivalue(obj));
80     return 1;
81   }
82   else if (cvt2num(obj) &&  /* string convertible to number? */
83             luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) {
84     *n = nvalue(&v);  /* convert result of 'luaO_str2num' to a float */
85     return 1;
86   }
87   else
88     return 0;  /* conversion failed */
89 }
90 #endif
91 
92 
93 /*
94 ** try to convert a value to an integer, rounding according to 'mode':
95 ** mode == 0: accepts only integral values
96 ** mode == 1: takes the floor of the number
97 ** mode == 2: takes the ceil of the number
98 */
luaV_tointeger(const TValue * obj,lua_Integer * p,int mode)99 int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) {
100   TValue v;
101  again:
102 #ifndef _KERNEL
103   if (ttisfloat(obj)) {
104     lua_Number n = fltvalue(obj);
105     lua_Number f = l_floor(n);
106     if (n != f) {  /* not an integral value? */
107       if (mode == 0) return 0;  /* fails if mode demands integral value */
108       else if (mode > 1)  /* needs ceil? */
109         f += 1;  /* convert floor to ceil (remember: n != f) */
110     }
111     return lua_numbertointeger(f, p);
112   }
113   else if (ttisinteger(obj)) {
114 #else /* _KERNEL */
115   if (ttisinteger(obj)) {
116     UNUSED(mode);
117 #endif
118     *p = ivalue(obj);
119     return 1;
120   }
121   else if (cvt2num(obj) &&
122             luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) {
123     obj = &v;
124     goto again;  /* convert result from 'luaO_str2num' to an integer */
125   }
126   return 0;  /* conversion failed */
127 }
128 
129 
130 #ifndef _KERNEL
131 /*
132 ** Try to convert a 'for' limit to an integer, preserving the
133 ** semantics of the loop.
134 ** (The following explanation assumes a non-negative step; it is valid
135 ** for negative steps mutatis mutandis.)
136 ** If the limit can be converted to an integer, rounding down, that is
137 ** it.
138 ** Otherwise, check whether the limit can be converted to a number.  If
139 ** the number is too large, it is OK to set the limit as LUA_MAXINTEGER,
140 ** which means no limit.  If the number is too negative, the loop
141 ** should not run, because any initial integer value is larger than the
142 ** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects
143 ** the extreme case when the initial value is LUA_MININTEGER, in which
144 ** case the LUA_MININTEGER limit would still run the loop once.
145 */
146 static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
147                      int *stopnow) {
148   *stopnow = 0;  /* usually, let loops run */
149   if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) {  /* not fit in integer? */
150     lua_Number n;  /* try to convert to float */
151     if (!tonumber(obj, &n)) /* cannot convert to float? */
152       return 0;  /* not a number */
153     if (luai_numlt(0, n)) {  /* if true, float is larger than max integer */
154       *p = LUA_MAXINTEGER;
155       if (step < 0) *stopnow = 1;
156     }
157     else {  /* float is smaller than min integer */
158       *p = LUA_MININTEGER;
159       if (step >= 0) *stopnow = 1;
160     }
161   }
162   return 1;
163 }
164 #endif
165 
166 
167 /*
168 ** Main function for table access (invoking metamethods if needed).
169 ** Compute 'val = t[key]'
170 */
171 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
172   int loop;  /* counter to avoid infinite loops */
173   for (loop = 0; loop < MAXTAGLOOP; loop++) {
174     const TValue *tm;
175     if (ttistable(t)) {  /* 't' is a table? */
176       Table *h = hvalue(t);
177       const TValue *res = luaH_get(h, key); /* do a primitive get */
178       if (!ttisnil(res) ||  /* result is not nil? */
179           (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
180         setobj2s(L, val, res);  /* result is the raw get */
181         return;
182       }
183       /* else will try metamethod */
184     }
185     else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
186       luaG_typeerror(L, t, "index");  /* no metamethod */
187     if (ttisfunction(tm)) {  /* metamethod is a function */
188       luaT_callTM(L, tm, t, key, val, 1);
189       return;
190     }
191     t = tm;  /* else repeat access over 'tm' */
192   }
193   luaG_runerror(L, "gettable chain too long; possible loop");
194 }
195 
196 
197 /*
198 ** Main function for table assignment (invoking metamethods if needed).
199 ** Compute 't[key] = val'
200 */
201 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
202   int loop;  /* counter to avoid infinite loops */
203   for (loop = 0; loop < MAXTAGLOOP; loop++) {
204     const TValue *tm;
205     if (ttistable(t)) {  /* 't' is a table? */
206       Table *h = hvalue(t);
207       TValue *oldval = cast(TValue *, luaH_get(h, key));
208       /* if previous value is not nil, there must be a previous entry
209          in the table; a metamethod has no relevance */
210       if (!ttisnil(oldval) ||
211          /* previous value is nil; must check the metamethod */
212          ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
213          /* no metamethod; is there a previous entry in the table? */
214          (oldval != luaO_nilobject ||
215          /* no previous entry; must create one. (The next test is
216             always true; we only need the assignment.) */
217          (oldval = luaH_newkey(L, h, key), 1)))) {
218         /* no metamethod and (now) there is an entry with given key */
219         setobj2t(L, oldval, val);  /* assign new value to that entry */
220         invalidateTMcache(h);
221         luaC_barrierback(L, h, val);
222         return;
223       }
224       /* else will try the metamethod */
225     }
226     else  /* not a table; check metamethod */
227       if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
228         luaG_typeerror(L, t, "index");
229     /* try the metamethod */
230     if (ttisfunction(tm)) {
231       luaT_callTM(L, tm, t, key, val, 0);
232       return;
233     }
234     t = tm;  /* else repeat assignment over 'tm' */
235   }
236   luaG_runerror(L, "settable chain too long; possible loop");
237 }
238 
239 
240 /*
241 ** Compare two strings 'ls' x 'rs', returning an integer smaller-equal-
242 ** -larger than zero if 'ls' is smaller-equal-larger than 'rs'.
243 ** The code is a little tricky because it allows '\0' in the strings
244 ** and it uses 'strcoll' (to respect locales) for each segments
245 ** of the strings.
246 */
247 static int l_strcmp (const TString *ls, const TString *rs) {
248   const char *l = getstr(ls);
249   size_t ll = tsslen(ls);
250   const char *r = getstr(rs);
251   size_t lr = tsslen(rs);
252   for (;;) {  /* for each segment */
253     int temp = strcoll(l, r);
254     if (temp != 0)  /* not equal? */
255       return temp;  /* done */
256     else {  /* strings are equal up to a '\0' */
257       size_t len = strlen(l);  /* index of first '\0' in both strings */
258       if (len == lr)  /* 'rs' is finished? */
259         return (len == ll) ? 0 : 1;  /* check 'ls' */
260       else if (len == ll)  /* 'ls' is finished? */
261         return -1;  /* 'ls' is smaller than 'rs' ('rs' is not finished) */
262       /* both strings longer than 'len'; go on comparing after the '\0' */
263       len++;
264       l += len; ll -= len; r += len; lr -= len;
265     }
266   }
267 }
268 
269 
270 /*
271 ** Check whether integer 'i' is less than float 'f'. If 'i' has an
272 ** exact representation as a float ('l_intfitsf'), compare numbers as
273 ** floats. Otherwise, if 'f' is outside the range for integers, result
274 ** is trivial. Otherwise, compare them as integers. (When 'i' has no
275 ** float representation, either 'f' is "far away" from 'i' or 'f' has
276 ** no precision left for a fractional part; either way, how 'f' is
277 ** truncated is irrelevant.) When 'f' is NaN, comparisons must result
278 ** in false.
279 */
280 static int LTintfloat (lua_Integer i, lua_Number f) {
281 #if defined(l_intfitsf)
282   if (!l_intfitsf(i)) {
283     if (f >= -cast_num(LUA_MININTEGER))  /* -minint == maxint + 1 */
284       return 1;  /* f >= maxint + 1 > i */
285     else if (f > cast_num(LUA_MININTEGER))  /* minint < f <= maxint ? */
286       return (i < cast(lua_Integer, f));  /* compare them as integers */
287     else  /* f <= minint <= i (or 'f' is NaN)  -->  not(i < f) */
288       return 0;
289   }
290 #endif
291   return luai_numlt(cast_num(i), f);  /* compare them as floats */
292 }
293 
294 
295 /*
296 ** Check whether integer 'i' is less than or equal to float 'f'.
297 ** See comments on previous function.
298 */
299 static int LEintfloat (lua_Integer i, lua_Number f) {
300 #if defined(l_intfitsf)
301   if (!l_intfitsf(i)) {
302     if (f >= -cast_num(LUA_MININTEGER))  /* -minint == maxint + 1 */
303       return 1;  /* f >= maxint + 1 > i */
304     else if (f >= cast_num(LUA_MININTEGER))  /* minint <= f <= maxint ? */
305       return (i <= cast(lua_Integer, f));  /* compare them as integers */
306     else  /* f < minint <= i (or 'f' is NaN)  -->  not(i <= f) */
307       return 0;
308   }
309 #endif
310   return luai_numle(cast_num(i), f);  /* compare them as floats */
311 }
312 
313 
314 /*
315 ** Return 'l < r', for numbers.
316 */
317 static int LTnum (const TValue *l, const TValue *r) {
318   if (ttisinteger(l)) {
319     lua_Integer li = ivalue(l);
320     if (ttisinteger(r))
321       return li < ivalue(r);  /* both are integers */
322 #ifndef _KERNEL
323     else  /* 'l' is int and 'r' is float */
324       return LTintfloat(li, fltvalue(r));  /* l < r ? */
325 #endif
326   }
327 #ifndef _KERNEL
328   else {
329     lua_Number lf = fltvalue(l);  /* 'l' must be float */
330     if (ttisfloat(r))
331       return luai_numlt(lf, fltvalue(r));  /* both are float */
332     else if (luai_numisnan(lf))  /* 'r' is int and 'l' is float */
333       return 0;  /* NaN < i is always false */
334     else  /* without NaN, (l < r)  <-->  not(r <= l) */
335       return !LEintfloat(ivalue(r), lf);  /* not (r <= l) ? */
336   }
337 #endif
338 }
339 
340 
341 /*
342 ** Return 'l <= r', for numbers.
343 */
344 static int LEnum (const TValue *l, const TValue *r) {
345   if (ttisinteger(l)) {
346     lua_Integer li = ivalue(l);
347     if (ttisinteger(r))
348       return li <= ivalue(r);  /* both are integers */
349 #ifndef _KERNEL
350     else  /* 'l' is int and 'r' is float */
351       return LEintfloat(li, fltvalue(r));  /* l <= r ? */
352 #endif
353   }
354 #ifndef _KERNEL
355   else {
356     lua_Number lf = fltvalue(l);  /* 'l' must be float */
357     if (ttisfloat(r))
358       return luai_numle(lf, fltvalue(r));  /* both are float */
359     else if (luai_numisnan(lf))  /* 'r' is int and 'l' is float */
360       return 0;  /*  NaN <= i is always false */
361     else  /* without NaN, (l <= r)  <-->  not(r < l) */
362       return !LTintfloat(ivalue(r), lf);  /* not (r < l) ? */
363   }
364 #endif
365 }
366 
367 
368 /*
369 ** Main operation less than; return 'l < r'.
370 */
371 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
372   int res;
373   if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
374     return LTnum(l, r);
375   else if (ttisstring(l) && ttisstring(r))  /* both are strings? */
376     return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
377   else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0)  /* no metamethod? */
378     luaG_ordererror(L, l, r);  /* error */
379   return res;
380 }
381 
382 
383 /*
384 ** Main operation less than or equal to; return 'l <= r'. If it needs
385 ** a metamethod and there is no '__le', try '__lt', based on
386 ** l <= r iff !(r < l) (assuming a total order). If the metamethod
387 ** yields during this substitution, the continuation has to know
388 ** about it (to negate the result of r<l); bit CIST_LEQ in the call
389 ** status keeps that information.
390 */
391 int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
392   int res;
393   if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
394     return LEnum(l, r);
395   else if (ttisstring(l) && ttisstring(r))  /* both are strings? */
396     return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
397   else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0)  /* try 'le' */
398     return res;
399   else {  /* try 'lt': */
400     L->ci->callstatus |= CIST_LEQ;  /* mark it is doing 'lt' for 'le' */
401     res = luaT_callorderTM(L, r, l, TM_LT);
402     L->ci->callstatus ^= CIST_LEQ;  /* clear mark */
403     if (res < 0)
404       luaG_ordererror(L, l, r);
405     return !res;  /* result is negated */
406   }
407 }
408 
409 
410 /*
411 ** Main operation for equality of Lua values; return 't1 == t2'.
412 ** L == NULL means raw equality (no metamethods)
413 */
414 int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
415   const TValue *tm;
416   if (ttype(t1) != ttype(t2)) {  /* not the same variant? */
417 #ifndef _KERNEL
418     if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER)
419       return 0;  /* only numbers can be equal with different variants */
420     else {  /* two numbers with different variants */
421       lua_Integer i1, i2;  /* compare them as integers */
422       return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2);
423     }
424 #else /* _KERNEL */
425       return 0; /* numbers have only the integer variant */
426 #endif
427   }
428   /* values have same type and same variant */
429   switch (ttype(t1)) {
430     case LUA_TNIL: return 1;
431     case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2));
432 #ifndef _KERNEL
433     case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2));
434 #endif
435     case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2);  /* true must be 1 !! */
436     case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
437     case LUA_TLCF: return fvalue(t1) == fvalue(t2);
438     case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2));
439     case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2));
440     case LUA_TUSERDATA: {
441       if (uvalue(t1) == uvalue(t2)) return 1;
442       else if (L == NULL) return 0;
443       tm = fasttm(L, uvalue(t1)->metatable, TM_EQ);
444       if (tm == NULL)
445         tm = fasttm(L, uvalue(t2)->metatable, TM_EQ);
446       break;  /* will try TM */
447     }
448     case LUA_TTABLE: {
449       if (hvalue(t1) == hvalue(t2)) return 1;
450       else if (L == NULL) return 0;
451       tm = fasttm(L, hvalue(t1)->metatable, TM_EQ);
452       if (tm == NULL)
453         tm = fasttm(L, hvalue(t2)->metatable, TM_EQ);
454       break;  /* will try TM */
455     }
456     default:
457       return gcvalue(t1) == gcvalue(t2);
458   }
459   if (tm == NULL)  /* no TM? */
460     return 0;  /* objects are different */
461   luaT_callTM(L, tm, t1, t2, L->top, 1);  /* call TM */
462   return !l_isfalse(L->top);
463 }
464 
465 
466 /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
467 #define tostring(L,o)  \
468 	(ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
469 
470 #define isemptystr(o)	(ttisshrstring(o) && tsvalue(o)->shrlen == 0)
471 
472 /*
473 ** Main operation for concatenation: concat 'total' values in the stack,
474 ** from 'L->top - total' up to 'L->top - 1'.
475 */
476 void luaV_concat (lua_State *L, int total) {
477   lua_assert(total >= 2);
478   do {
479     StkId top = L->top;
480     int n = 2;  /* number of elements handled in this pass (at least 2) */
481     if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1))
482       luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT);
483     else if (isemptystr(top - 1))  /* second operand is empty? */
484       cast_void(tostring(L, top - 2));  /* result is first operand */
485     else if (isemptystr(top - 2)) {  /* first operand is an empty string? */
486       setobjs2s(L, top - 2, top - 1);  /* result is second op. */
487     }
488     else {
489       /* at least two non-empty string values; get as many as possible */
490       size_t tl = vslen(top - 1);
491       char *buffer;
492       int i;
493       /* collect total length */
494       for (i = 1; i < total && tostring(L, top-i-1); i++) {
495         size_t l = vslen(top - i - 1);
496         if (l >= (MAX_SIZE/sizeof(char)) - tl)
497           luaG_runerror(L, "string length overflow");
498         tl += l;
499       }
500       buffer = luaZ_openspace(L, &G(L)->buff, tl);
501       tl = 0;
502       n = i;
503       do {  /* copy all strings to buffer */
504         size_t l = vslen(top - i);
505         memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
506         tl += l;
507       } while (--i > 0);
508       setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));  /* create result */
509     }
510     total -= n-1;  /* got 'n' strings to create 1 new */
511     L->top -= n-1;  /* popped 'n' strings and pushed one */
512   } while (total > 1);  /* repeat until only 1 result left */
513 }
514 
515 
516 /*
517 ** Main operation 'ra' = #rb'.
518 */
519 void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
520   const TValue *tm;
521   switch (ttype(rb)) {
522     case LUA_TTABLE: {
523       Table *h = hvalue(rb);
524       tm = fasttm(L, h->metatable, TM_LEN);
525       if (tm) break;  /* metamethod? break switch to call it */
526       setivalue(ra, luaH_getn(h));  /* else primitive len */
527       return;
528     }
529     case LUA_TSHRSTR: {
530       setivalue(ra, tsvalue(rb)->shrlen);
531       return;
532     }
533     case LUA_TLNGSTR: {
534       setivalue(ra, tsvalue(rb)->u.lnglen);
535       return;
536     }
537     default: {  /* try metamethod */
538       tm = luaT_gettmbyobj(L, rb, TM_LEN);
539       if (ttisnil(tm))  /* no metamethod? */
540         luaG_typeerror(L, rb, "get length of");
541       break;
542     }
543   }
544   luaT_callTM(L, tm, rb, rb, ra, 1);
545 }
546 
547 
548 /*
549 ** Integer division; return 'm // n', that is, floor(m/n).
550 ** C division truncates its result (rounds towards zero).
551 ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer,
552 ** otherwise 'floor(q) == trunc(q) - 1'.
553 */
554 lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) {
555   if (l_castS2U(n) + 1u <= 1u) {  /* special cases: -1 or 0 */
556     if (n == 0)
557       luaG_runerror(L, "attempt to divide by zero");
558     return intop(-, 0, m);   /* n==-1; avoid overflow with 0x80000...//-1 */
559   }
560   else {
561     lua_Integer q = m / n;  /* perform C division */
562     if ((m ^ n) < 0 && m % n != 0)  /* 'm/n' would be negative non-integer? */
563       q -= 1;  /* correct result for different rounding */
564     return q;
565   }
566 }
567 
568 
569 /*
570 ** Integer modulus; return 'm % n'. (Assume that C '%' with
571 ** negative operands follows C99 behavior. See previous comment
572 ** about luaV_div.)
573 */
574 lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) {
575   if (l_castS2U(n) + 1u <= 1u) {  /* special cases: -1 or 0 */
576     if (n == 0)
577       luaG_runerror(L, "attempt to perform 'n%%0'");
578     return 0;   /* m % -1 == 0; avoid overflow with 0x80000...%-1 */
579   }
580   else {
581     lua_Integer r = m % n;
582     if (r != 0 && (m ^ n) < 0)  /* 'm/n' would be non-integer negative? */
583       r += n;  /* correct result for different rounding */
584     return r;
585   }
586 }
587 
588 
589 /* number of bits in an integer */
590 #define NBITS	cast_int(sizeof(lua_Integer) * CHAR_BIT)
591 
592 /*
593 ** Shift left operation. (Shift right just negates 'y'.)
594 */
595 lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
596   if (y < 0) {  /* shift right? */
597     if (y <= -NBITS) return 0;
598     else return intop(>>, x, -y);
599   }
600   else {  /* shift left */
601     if (y >= NBITS) return 0;
602     else return intop(<<, x, y);
603   }
604 }
605 
606 
607 /*
608 ** check whether cached closure in prototype 'p' may be reused, that is,
609 ** whether there is a cached closure with the same upvalues needed by
610 ** new closure to be created.
611 */
612 static LClosure *getcached (Proto *p, UpVal **encup, StkId base) {
613   LClosure *c = p->cache;
614   if (c != NULL) {  /* is there a cached closure? */
615     int nup = p->sizeupvalues;
616     Upvaldesc *uv = p->upvalues;
617     int i;
618     for (i = 0; i < nup; i++) {  /* check whether it has right upvalues */
619       TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
620       if (c->upvals[i]->v != v)
621         return NULL;  /* wrong upvalue; cannot reuse closure */
622     }
623   }
624   return c;  /* return cached closure (or NULL if no cached closure) */
625 }
626 
627 
628 /*
629 ** create a new Lua closure, push it in the stack, and initialize
630 ** its upvalues. Note that the closure is not cached if prototype is
631 ** already black (which means that 'cache' was already cleared by the
632 ** GC).
633 */
634 static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
635                          StkId ra) {
636   int nup = p->sizeupvalues;
637   Upvaldesc *uv = p->upvalues;
638   int i;
639   LClosure *ncl = luaF_newLclosure(L, nup);
640   ncl->p = p;
641   setclLvalue(L, ra, ncl);  /* anchor new closure in stack */
642   for (i = 0; i < nup; i++) {  /* fill in its upvalues */
643     if (uv[i].instack)  /* upvalue refers to local variable? */
644       ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx);
645     else  /* get upvalue from enclosing function */
646       ncl->upvals[i] = encup[uv[i].idx];
647     ncl->upvals[i]->refcount++;
648     /* new closure is white, so we do not need a barrier here */
649   }
650   if (!isblack(p))  /* cache will not break GC invariant? */
651     p->cache = ncl;  /* save it on cache for reuse */
652 }
653 
654 
655 /*
656 ** finish execution of an opcode interrupted by an yield
657 */
658 void luaV_finishOp (lua_State *L) {
659   CallInfo *ci = L->ci;
660   StkId base = ci->u.l.base;
661   Instruction inst = *(ci->u.l.savedpc - 1);  /* interrupted instruction */
662   OpCode op = GET_OPCODE(inst);
663   switch (op) {  /* finish its execution */
664 #ifndef _KERNEL
665     case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV:
666 #else
667     case OP_ADD: case OP_SUB: case OP_MUL: case OP_IDIV:
668 #endif
669     case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
670 #ifndef _KERNEL
671     case OP_MOD: case OP_POW:
672 #else
673     case OP_MOD:
674 #endif
675     case OP_UNM: case OP_BNOT: case OP_LEN:
676     case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
677       setobjs2s(L, base + GETARG_A(inst), --L->top);
678       break;
679     }
680     case OP_LE: case OP_LT: case OP_EQ: {
681       int res = !l_isfalse(L->top - 1);
682       L->top--;
683       if (ci->callstatus & CIST_LEQ) {  /* "<=" using "<" instead? */
684         lua_assert(op == OP_LE);
685         ci->callstatus ^= CIST_LEQ;  /* clear mark */
686         res = !res;  /* negate result */
687       }
688       lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
689       if (res != GETARG_A(inst))  /* condition failed? */
690         ci->u.l.savedpc++;  /* skip jump instruction */
691       break;
692     }
693     case OP_CONCAT: {
694       StkId top = L->top - 1;  /* top when 'luaT_trybinTM' was called */
695       int b = GETARG_B(inst);      /* first element to concatenate */
696       int total = cast_int(top - 1 - (base + b));  /* yet to concatenate */
697       setobj2s(L, top - 2, top);  /* put TM result in proper position */
698       if (total > 1) {  /* are there elements to concat? */
699         L->top = top - 1;  /* top is one after last element (at top-2) */
700         luaV_concat(L, total);  /* concat them (may yield again) */
701       }
702       /* move final result to final position */
703       setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
704       L->top = ci->top;  /* restore top */
705       break;
706     }
707     case OP_TFORCALL: {
708       lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
709       L->top = ci->top;  /* correct top */
710       break;
711     }
712     case OP_CALL: {
713       if (GETARG_C(inst) - 1 >= 0)  /* nresults >= 0? */
714         L->top = ci->top;  /* adjust results */
715       break;
716     }
717     case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
718       break;
719     default: lua_assert(0);
720   }
721 }
722 
723 
724 
725 
726 /*
727 ** {==================================================================
728 ** Function 'luaV_execute': main interpreter loop
729 ** ===================================================================
730 */
731 
732 
733 /*
734 ** some macros for common tasks in 'luaV_execute'
735 */
736 
737 #if !defined(luai_runtimecheck)
738 #define luai_runtimecheck(L, c)		/* void */
739 #endif
740 
741 
742 #define RA(i)	(base+GETARG_A(i))
743 /* to be used after possible stack reallocation */
744 #define RB(i)	check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
745 #define RC(i)	check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
746 #define RKB(i)	check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
747 	ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
748 #define RKC(i)	check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
749 	ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
750 #define KBx(i)  \
751   (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
752 
753 
754 /* execute a jump instruction */
755 #define dojump(ci,i,e) \
756   { int a = GETARG_A(i); \
757     if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
758     ci->u.l.savedpc += GETARG_sBx(i) + e; }
759 
760 /* for test instructions, execute the jump instruction that follows it */
761 #define donextjump(ci)	{ i = *ci->u.l.savedpc; dojump(ci, i, 1); }
762 
763 
764 #define Protect(x)	{ {x;}; base = ci->u.l.base; }
765 
766 #define checkGC(L,c)  \
767   Protect( luaC_condGC(L,{L->top = (c);  /* limit of live values */ \
768                           luaC_step(L); \
769                           L->top = ci->top;})  /* restore top */ \
770            luai_threadyield(L); )
771 
772 
773 #define vmdispatch(o)	switch(o)
774 #define vmcase(l)	case l:
775 #define vmbreak		break
776 
777 void luaV_execute (lua_State *L) {
778   CallInfo *ci = L->ci;
779   LClosure *cl;
780   TValue *k;
781   StkId base;
782  newframe:  /* reentry point when frame changes (call/return) */
783   lua_assert(ci == L->ci);
784   cl = clLvalue(ci->func);
785   k = cl->p->k;
786   base = ci->u.l.base;
787   /* main loop of interpreter */
788   for (;;) {
789     Instruction i = *(ci->u.l.savedpc++);
790     StkId ra;
791     if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
792         (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
793       Protect(luaG_traceexec(L));
794     }
795     /* WARNING: several calls may realloc the stack and invalidate 'ra' */
796     ra = RA(i);
797     lua_assert(base == ci->u.l.base);
798     lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
799     vmdispatch (GET_OPCODE(i)) {
800       vmcase(OP_MOVE) {
801         setobjs2s(L, ra, RB(i));
802         vmbreak;
803       }
804       vmcase(OP_LOADK) {
805         TValue *rb = k + GETARG_Bx(i);
806         setobj2s(L, ra, rb);
807         vmbreak;
808       }
809       vmcase(OP_LOADKX) {
810         TValue *rb;
811         lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
812         rb = k + GETARG_Ax(*ci->u.l.savedpc++);
813         setobj2s(L, ra, rb);
814         vmbreak;
815       }
816       vmcase(OP_LOADBOOL) {
817         setbvalue(ra, GETARG_B(i));
818         if (GETARG_C(i)) ci->u.l.savedpc++;  /* skip next instruction (if C) */
819         vmbreak;
820       }
821       vmcase(OP_LOADNIL) {
822         int b = GETARG_B(i);
823         do {
824           setnilvalue(ra++);
825         } while (b--);
826         vmbreak;
827       }
828       vmcase(OP_GETUPVAL) {
829         int b = GETARG_B(i);
830         setobj2s(L, ra, cl->upvals[b]->v);
831         vmbreak;
832       }
833       vmcase(OP_GETTABUP) {
834         int b = GETARG_B(i);
835         Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
836         vmbreak;
837       }
838       vmcase(OP_GETTABLE) {
839         Protect(luaV_gettable(L, RB(i), RKC(i), ra));
840         vmbreak;
841       }
842       vmcase(OP_SETTABUP) {
843         int a = GETARG_A(i);
844         Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
845         vmbreak;
846       }
847       vmcase(OP_SETUPVAL) {
848         UpVal *uv = cl->upvals[GETARG_B(i)];
849         setobj(L, uv->v, ra);
850         luaC_upvalbarrier(L, uv);
851         vmbreak;
852       }
853       vmcase(OP_SETTABLE) {
854         Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
855         vmbreak;
856       }
857       vmcase(OP_NEWTABLE) {
858         int b = GETARG_B(i);
859         int c = GETARG_C(i);
860         Table *t = luaH_new(L);
861         sethvalue(L, ra, t);
862         if (b != 0 || c != 0)
863           luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
864         checkGC(L, ra + 1);
865         vmbreak;
866       }
867       vmcase(OP_SELF) {
868         StkId rb = RB(i);
869         setobjs2s(L, ra+1, rb);
870         Protect(luaV_gettable(L, rb, RKC(i), ra));
871         vmbreak;
872       }
873       vmcase(OP_ADD) {
874         TValue *rb = RKB(i);
875         TValue *rc = RKC(i);
876 #ifndef _KERNEL
877         lua_Number nb; lua_Number nc;
878         if (ttisinteger(rb) && ttisinteger(rc)) {
879           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
880           setivalue(ra, intop(+, ib, ic));
881         }
882         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
883           setfltvalue(ra, luai_numadd(L, nb, nc));
884         }
885 #else /* _KERNEL */
886         lua_Integer ib; lua_Integer ic;
887         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
888           setivalue(ra, intop(+, ib, ic));
889         }
890 #endif
891         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); }
892         vmbreak;
893       }
894       vmcase(OP_SUB) {
895         TValue *rb = RKB(i);
896         TValue *rc = RKC(i);
897 #ifndef _KERNEL
898         lua_Number nb; lua_Number nc;
899         if (ttisinteger(rb) && ttisinteger(rc)) {
900           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
901           setivalue(ra, intop(-, ib, ic));
902         }
903         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
904           setfltvalue(ra, luai_numsub(L, nb, nc));
905         }
906 #else /* _KERNEL */
907         lua_Integer ib; lua_Integer ic;
908         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
909           setivalue(ra, intop(-, ib, ic));
910         }
911 #endif
912         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); }
913         vmbreak;
914       }
915       vmcase(OP_MUL) {
916         TValue *rb = RKB(i);
917         TValue *rc = RKC(i);
918 #ifndef _KERNEL
919         lua_Number nb; lua_Number nc;
920         if (ttisinteger(rb) && ttisinteger(rc)) {
921           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
922           setivalue(ra, intop(*, ib, ic));
923         }
924         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
925           setfltvalue(ra, luai_nummul(L, nb, nc));
926         }
927 #else /* _KERNEL */
928         lua_Integer ib; lua_Integer ic;
929         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
930           setivalue(ra, intop(*, ib, ic));
931         }
932 #endif
933         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); }
934         vmbreak;
935       }
936 #ifndef _KERNEL
937       vmcase(OP_DIV) {  /* float division (always with floats) */
938         TValue *rb = RKB(i);
939         TValue *rc = RKC(i);
940         lua_Number nb; lua_Number nc;
941         if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
942           setfltvalue(ra, luai_numdiv(L, nb, nc));
943         }
944         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); }
945         vmbreak;
946       }
947 #endif
948       vmcase(OP_BAND) {
949         TValue *rb = RKB(i);
950         TValue *rc = RKC(i);
951         lua_Integer ib; lua_Integer ic;
952         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
953           setivalue(ra, intop(&, ib, ic));
954         }
955         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); }
956         vmbreak;
957       }
958       vmcase(OP_BOR) {
959         TValue *rb = RKB(i);
960         TValue *rc = RKC(i);
961         lua_Integer ib; lua_Integer ic;
962         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
963           setivalue(ra, intop(|, ib, ic));
964         }
965         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); }
966         vmbreak;
967       }
968       vmcase(OP_BXOR) {
969         TValue *rb = RKB(i);
970         TValue *rc = RKC(i);
971         lua_Integer ib; lua_Integer ic;
972         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
973           setivalue(ra, intop(^, ib, ic));
974         }
975         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); }
976         vmbreak;
977       }
978       vmcase(OP_SHL) {
979         TValue *rb = RKB(i);
980         TValue *rc = RKC(i);
981         lua_Integer ib; lua_Integer ic;
982         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
983           setivalue(ra, luaV_shiftl(ib, ic));
984         }
985         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); }
986         vmbreak;
987       }
988       vmcase(OP_SHR) {
989         TValue *rb = RKB(i);
990         TValue *rc = RKC(i);
991         lua_Integer ib; lua_Integer ic;
992         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
993           setivalue(ra, luaV_shiftl(ib, -ic));
994         }
995         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); }
996         vmbreak;
997       }
998       vmcase(OP_MOD) {
999         TValue *rb = RKB(i);
1000         TValue *rc = RKC(i);
1001 #ifndef _KERNEL
1002         lua_Number nb; lua_Number nc;
1003         if (ttisinteger(rb) && ttisinteger(rc)) {
1004           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
1005           setivalue(ra, luaV_mod(L, ib, ic));
1006         }
1007         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
1008           lua_Number m;
1009           luai_nummod(L, nb, nc, m);
1010           setfltvalue(ra, m);
1011         }
1012 #else /* _KERNEL */
1013         lua_Integer ib; lua_Integer ic;
1014         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
1015           setivalue(ra, luaV_mod(L, ib, ic));
1016         }
1017 #endif
1018         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); }
1019         vmbreak;
1020       }
1021       vmcase(OP_IDIV) {  /* floor division */
1022         TValue *rb = RKB(i);
1023         TValue *rc = RKC(i);
1024 #ifndef _KERNEL
1025         lua_Number nb; lua_Number nc;
1026         if (ttisinteger(rb) && ttisinteger(rc)) {
1027           lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
1028           setivalue(ra, luaV_div(L, ib, ic));
1029         }
1030         else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
1031           setfltvalue(ra, luai_numidiv(L, nb, nc));
1032         }
1033 #else /* _KERNEL */
1034         lua_Integer ib; lua_Integer ic;
1035         if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
1036           setivalue(ra, luaV_div(L, ib, ic));
1037         }
1038 #endif
1039         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); }
1040         vmbreak;
1041       }
1042 #ifndef _KERNEL
1043       vmcase(OP_POW) {
1044         TValue *rb = RKB(i);
1045         TValue *rc = RKC(i);
1046         lua_Number nb; lua_Number nc;
1047         if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
1048           setfltvalue(ra, luai_numpow(L, nb, nc));
1049         }
1050         else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); }
1051         vmbreak;
1052       }
1053 #endif
1054       vmcase(OP_UNM) {
1055         TValue *rb = RB(i);
1056 #ifndef _KERNEL
1057         lua_Number nb;
1058         if (ttisinteger(rb)) {
1059           lua_Integer ib = ivalue(rb);
1060           setivalue(ra, intop(-, 0, ib));
1061         }
1062         else if (tonumber(rb, &nb)) {
1063           setfltvalue(ra, luai_numunm(L, nb));
1064         }
1065 #else /* _KERNEL */
1066         lua_Integer ib;
1067         if (tointeger(rb, &ib)) {
1068           setivalue(ra, intop(-, 0, ib));
1069         }
1070 #endif
1071         else {
1072           Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM));
1073         }
1074         vmbreak;
1075       }
1076       vmcase(OP_BNOT) {
1077         TValue *rb = RB(i);
1078         lua_Integer ib;
1079         if (tointeger(rb, &ib)) {
1080           setivalue(ra, intop(^, ~l_castS2U(0), ib));
1081         }
1082         else {
1083           Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT));
1084         }
1085         vmbreak;
1086       }
1087       vmcase(OP_NOT) {
1088         TValue *rb = RB(i);
1089         int res = l_isfalse(rb);  /* next assignment may change this value */
1090         setbvalue(ra, res);
1091         vmbreak;
1092       }
1093       vmcase(OP_LEN) {
1094         Protect(luaV_objlen(L, ra, RB(i)));
1095         vmbreak;
1096       }
1097       vmcase(OP_CONCAT) {
1098         int b = GETARG_B(i);
1099         int c = GETARG_C(i);
1100         StkId rb;
1101         L->top = base + c + 1;  /* mark the end of concat operands */
1102         Protect(luaV_concat(L, c - b + 1));
1103         ra = RA(i);  /* 'luav_concat' may invoke TMs and move the stack */
1104         rb = base + b;
1105         setobjs2s(L, ra, rb);
1106         checkGC(L, (ra >= rb ? ra + 1 : rb));
1107         L->top = ci->top;  /* restore top */
1108         vmbreak;
1109       }
1110       vmcase(OP_JMP) {
1111         dojump(ci, i, 0);
1112         vmbreak;
1113       }
1114       vmcase(OP_EQ) {
1115         TValue *rb = RKB(i);
1116         TValue *rc = RKC(i);
1117         Protect(
1118           if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i))
1119             ci->u.l.savedpc++;
1120           else
1121             donextjump(ci);
1122         )
1123         vmbreak;
1124       }
1125       vmcase(OP_LT) {
1126         Protect(
1127           if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
1128             ci->u.l.savedpc++;
1129           else
1130             donextjump(ci);
1131         )
1132         vmbreak;
1133       }
1134       vmcase(OP_LE) {
1135         Protect(
1136           if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
1137             ci->u.l.savedpc++;
1138           else
1139             donextjump(ci);
1140         )
1141         vmbreak;
1142       }
1143       vmcase(OP_TEST) {
1144         if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
1145             ci->u.l.savedpc++;
1146           else
1147           donextjump(ci);
1148         vmbreak;
1149       }
1150       vmcase(OP_TESTSET) {
1151         TValue *rb = RB(i);
1152         if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
1153           ci->u.l.savedpc++;
1154         else {
1155           setobjs2s(L, ra, rb);
1156           donextjump(ci);
1157         }
1158         vmbreak;
1159       }
1160       vmcase(OP_CALL) {
1161         int b = GETARG_B(i);
1162         int nresults = GETARG_C(i) - 1;
1163         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
1164         if (luaD_precall(L, ra, nresults)) {  /* C function? */
1165           if (nresults >= 0) L->top = ci->top;  /* adjust results */
1166           base = ci->u.l.base;
1167         }
1168         else {  /* Lua function */
1169           ci = L->ci;
1170           ci->callstatus |= CIST_REENTRY;
1171           goto newframe;  /* restart luaV_execute over new Lua function */
1172         }
1173         vmbreak;
1174       }
1175       vmcase(OP_TAILCALL) {
1176         int b = GETARG_B(i);
1177         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
1178         lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
1179         if (luaD_precall(L, ra, LUA_MULTRET))  /* C function? */
1180           base = ci->u.l.base;
1181         else {
1182           /* tail call: put called frame (n) in place of caller one (o) */
1183           CallInfo *nci = L->ci;  /* called frame */
1184           CallInfo *oci = nci->previous;  /* caller frame */
1185           StkId nfunc = nci->func;  /* called function */
1186           StkId ofunc = oci->func;  /* caller function */
1187           /* last stack slot filled by 'precall' */
1188           StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
1189           int aux;
1190           /* close all upvalues from previous call */
1191           if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
1192           /* move new frame into old one */
1193           for (aux = 0; nfunc + aux < lim; aux++)
1194             setobjs2s(L, ofunc + aux, nfunc + aux);
1195           oci->u.l.base = ofunc + (nci->u.l.base - nfunc);  /* correct base */
1196           oci->top = L->top = ofunc + (L->top - nfunc);  /* correct top */
1197           oci->u.l.savedpc = nci->u.l.savedpc;
1198           oci->callstatus |= CIST_TAIL;  /* function was tail called */
1199           ci = L->ci = oci;  /* remove new frame */
1200           lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
1201           goto newframe;  /* restart luaV_execute over new Lua function */
1202         }
1203         vmbreak;
1204       }
1205       vmcase(OP_RETURN) {
1206         int b = GETARG_B(i);
1207         if (cl->p->sizep > 0) luaF_close(L, base);
1208         b = luaD_poscall(L, ra, (b != 0 ? b - 1 : L->top - ra));
1209         if (!(ci->callstatus & CIST_REENTRY))  /* 'ci' still the called one */
1210           return;  /* external invocation: return */
1211         else {  /* invocation via reentry: continue execution */
1212           ci = L->ci;
1213           if (b) L->top = ci->top;
1214           lua_assert(isLua(ci));
1215           lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
1216           goto newframe;  /* restart luaV_execute over new Lua function */
1217         }
1218       }
1219       vmcase(OP_FORLOOP) {
1220 #ifndef _KERNEL
1221         if (ttisinteger(ra)) {  /* integer loop? */
1222 #endif
1223           lua_Integer step = ivalue(ra + 2);
1224           lua_Integer idx = ivalue(ra) + step; /* increment index */
1225           lua_Integer limit = ivalue(ra + 1);
1226           if ((0 < step) ? (idx <= limit) : (limit <= idx)) {
1227             ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
1228             chgivalue(ra, idx);  /* update internal index... */
1229             setivalue(ra + 3, idx);  /* ...and external index */
1230           }
1231 #ifndef _KERNEL
1232         }
1233         else {  /* floating loop */
1234           lua_Number step = fltvalue(ra + 2);
1235           lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */
1236           lua_Number limit = fltvalue(ra + 1);
1237           if (luai_numlt(0, step) ? luai_numle(idx, limit)
1238                                   : luai_numle(limit, idx)) {
1239             ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
1240             chgfltvalue(ra, idx);  /* update internal index... */
1241             setfltvalue(ra + 3, idx);  /* ...and external index */
1242           }
1243         }
1244 #endif
1245         vmbreak;
1246       }
1247       vmcase(OP_FORPREP) {
1248         TValue *init = ra;
1249         TValue *plimit = ra + 1;
1250         TValue *pstep = ra + 2;
1251         lua_Integer ilimit;
1252 #ifndef _KERNEL
1253         int stopnow;
1254         if (ttisinteger(init) && ttisinteger(pstep) &&
1255             forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) {
1256           /* all values are integer */
1257           lua_Integer initv = (stopnow ? 0 : ivalue(init));
1258           setivalue(plimit, ilimit);
1259           setivalue(init, initv - ivalue(pstep));
1260         }
1261         else {  /* try making all values floats */
1262           lua_Number ninit; lua_Number nlimit; lua_Number nstep;
1263           if (!tonumber(plimit, &nlimit))
1264             luaG_runerror(L, "'for' limit must be a number");
1265           setfltvalue(plimit, nlimit);
1266           if (!tonumber(pstep, &nstep))
1267             luaG_runerror(L, "'for' step must be a number");
1268           setfltvalue(pstep, nstep);
1269           if (!tonumber(init, &ninit))
1270             luaG_runerror(L, "'for' initial value must be a number");
1271           setfltvalue(init, luai_numsub(L, ninit, nstep));
1272         }
1273 #else /* _KERNEL */
1274         lua_Integer initv; lua_Integer step;
1275         if (!tointeger(plimit, &ilimit))
1276           luaG_runerror(L, "'for' limit must be a number");
1277         setivalue(plimit, ilimit);
1278         if (!tointeger(pstep, &step))
1279           luaG_runerror(L, "'for' step must be a number");
1280         setivalue(pstep, step);
1281         if (!tointeger(init, &initv))
1282           luaG_runerror(L, "'for' initial value must be a number");
1283         setivalue(init, initv - step);
1284 #endif
1285         ci->u.l.savedpc += GETARG_sBx(i);
1286         vmbreak;
1287       }
1288       vmcase(OP_TFORCALL) {
1289         StkId cb = ra + 3;  /* call base */
1290         setobjs2s(L, cb+2, ra+2);
1291         setobjs2s(L, cb+1, ra+1);
1292         setobjs2s(L, cb, ra);
1293         L->top = cb + 3;  /* func. + 2 args (state and index) */
1294         Protect(luaD_call(L, cb, GETARG_C(i), 1));
1295         L->top = ci->top;
1296         i = *(ci->u.l.savedpc++);  /* go to next instruction */
1297         ra = RA(i);
1298         lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
1299         goto l_tforloop;
1300       }
1301       vmcase(OP_TFORLOOP) {
1302         l_tforloop:
1303         if (!ttisnil(ra + 1)) {  /* continue loop? */
1304           setobjs2s(L, ra, ra + 1);  /* save control variable */
1305            ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
1306         }
1307         vmbreak;
1308       }
1309       vmcase(OP_SETLIST) {
1310         int n = GETARG_B(i);
1311         int c = GETARG_C(i);
1312         unsigned int last;
1313         Table *h;
1314         if (n == 0) n = cast_int(L->top - ra) - 1;
1315         if (c == 0) {
1316           lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
1317           c = GETARG_Ax(*ci->u.l.savedpc++);
1318         }
1319         luai_runtimecheck(L, ttistable(ra));
1320         h = hvalue(ra);
1321         last = ((c-1)*LFIELDS_PER_FLUSH) + n;
1322         if (last > h->sizearray)  /* needs more space? */
1323           luaH_resizearray(L, h, last);  /* pre-allocate it at once */
1324         for (; n > 0; n--) {
1325           TValue *val = ra+n;
1326           luaH_setint(L, h, last--, val);
1327           luaC_barrierback(L, h, val);
1328         }
1329         L->top = ci->top;  /* correct top (in case of previous open call) */
1330         vmbreak;
1331       }
1332       vmcase(OP_CLOSURE) {
1333         Proto *p = cl->p->p[GETARG_Bx(i)];
1334         LClosure *ncl = getcached(p, cl->upvals, base);  /* cached closure */
1335         if (ncl == NULL)  /* no match? */
1336           pushclosure(L, p, cl->upvals, base, ra);  /* create a new one */
1337         else
1338           setclLvalue(L, ra, ncl);  /* push cashed closure */
1339         checkGC(L, ra + 1);
1340         vmbreak;
1341       }
1342       vmcase(OP_VARARG) {
1343         int b = GETARG_B(i) - 1;
1344         int j;
1345         int n = cast_int(base - ci->func) - cl->p->numparams - 1;
1346         if (b < 0) {  /* B == 0? */
1347           b = n;  /* get all var. arguments */
1348           Protect(luaD_checkstack(L, n));
1349           ra = RA(i);  /* previous call may change the stack */
1350           L->top = ra + n;
1351         }
1352         for (j = 0; j < b; j++) {
1353           if (j < n) {
1354             setobjs2s(L, ra + j, base - n + j);
1355           }
1356           else {
1357             setnilvalue(ra + j);
1358           }
1359         }
1360         vmbreak;
1361       }
1362       vmcase(OP_EXTRAARG) {
1363         lua_assert(0);
1364         vmbreak;
1365       }
1366     }
1367   }
1368 }
1369 
1370 /* }================================================================== */
1371 
1372