LCOV - code coverage report
Current view: top level - src - lvm.c Coverage Total Hit
Test: Lua 5.2.4 Lines: 86.2 % 333 287
Test Date: 2024-04-28 10:23:12
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              : ** $Id: lvm.c,v 2.155.1.1 2013/04/12 18:48:47 roberto Exp $
       3              : ** Lua virtual machine
       4              : ** See Copyright Notice in lua.h
       5              : */
       6              : 
       7              : 
       8              : #include <stdio.h>
       9              : #include <stdlib.h>
      10              : #include <string.h>
      11              : 
      12              : #define lvm_c
      13              : #define LUA_CORE
      14              : 
      15              : #include "lua.h"
      16              : 
      17              : #include "ldebug.h"
      18              : #include "ldo.h"
      19              : #include "lfunc.h"
      20              : #include "lgc.h"
      21              : #include "lobject.h"
      22              : #include "lopcodes.h"
      23              : #include "lstate.h"
      24              : #include "lstring.h"
      25              : #include "ltable.h"
      26              : #include "ltm.h"
      27              : #include "lvm.h"
      28              : 
      29              : 
      30              : 
      31              : /* limit for table tag-method chains (to avoid loops) */
      32              : #define MAXTAGLOOP      100
      33              : 
      34              : 
      35          300 : const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
      36              :   lua_Number num;
      37          300 :   if (ttisnumber(obj)) return obj;
      38          253 :   if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) {
      39           75 :     setnvalue(n, num);
      40           75 :     return n;
      41              :   }
      42              :   else
      43          178 :     return NULL;
      44              : }
      45              : 
      46              : 
      47         2307 : int luaV_tostring (lua_State *L, StkId obj) {
      48         2307 :   if (!ttisnumber(obj))
      49           21 :     return 0;
      50              :   else {
      51              :     char s[LUAI_MAXNUMBER2STR];
      52         2286 :     lua_Number n = nvalue(obj);
      53         2286 :     int l = lua_number2str(s, n);
      54         2286 :     setsvalue2s(L, obj, luaS_newlstr(L, s, l));
      55         2286 :     return 1;
      56              :   }
      57              : }
      58              : 
      59              : 
      60           32 : static void traceexec (lua_State *L) {
      61           32 :   CallInfo *ci = L->ci;
      62           32 :   lu_byte mask = L->hookmask;
      63           32 :   int counthook = ((mask & LUA_MASKCOUNT) && L->hookcount == 0);
      64           32 :   if (counthook)
      65           32 :     resethookcount(L);  /* reset count */
      66           32 :   if (ci->callstatus & CIST_HOOKYIELD) {  /* called hook last time? */
      67            0 :     ci->callstatus &= ~CIST_HOOKYIELD;  /* erase mark */
      68            0 :     return;  /* do not call hook again (VM yielded, so it did not move) */
      69              :   }
      70           32 :   if (counthook)
      71           32 :     luaD_hook(L, LUA_HOOKCOUNT, -1);  /* call count hook */
      72           32 :   if (mask & LUA_MASKLINE) {
      73            0 :     Proto *p = ci_func(ci)->p;
      74            0 :     int npc = pcRel(ci->u.l.savedpc, p);
      75            0 :     int newline = getfuncline(p, npc);
      76            0 :     if (npc == 0 ||  /* call linehook when enter a new function, */
      77            0 :         ci->u.l.savedpc <= L->oldpc ||  /* when jump back (loop), or when */
      78            0 :         newline != getfuncline(p, pcRel(L->oldpc, p)))  /* enter a new line */
      79            0 :       luaD_hook(L, LUA_HOOKLINE, newline);  /* call line hook */
      80              :   }
      81           32 :   L->oldpc = ci->u.l.savedpc;
      82           32 :   if (L->status == LUA_YIELD) {  /* did hook yield? */
      83            0 :     if (counthook)
      84            0 :       L->hookcount = 1;  /* undo decrement to zero */
      85            0 :     ci->u.l.savedpc--;  /* undo increment (resume will increment it again) */
      86            0 :     ci->callstatus |= CIST_HOOKYIELD;  /* mark that it yielded */
      87            0 :     ci->func = L->top - 1;  /* protect stack below results */
      88            0 :     luaD_throw(L, LUA_YIELD);
      89              :   }
      90              : }
      91              : 
      92              : 
      93           56 : static void callTM (lua_State *L, const TValue *f, const TValue *p1,
      94              :                     const TValue *p2, TValue *p3, int hasres) {
      95           56 :   ptrdiff_t result = savestack(L, p3);
      96           56 :   setobj2s(L, L->top++, f);  /* push function */
      97           56 :   setobj2s(L, L->top++, p1);  /* 1st argument */
      98           56 :   setobj2s(L, L->top++, p2);  /* 2nd argument */
      99           56 :   if (!hasres)  /* no result? 'p3' is third argument */
     100            4 :     setobj2s(L, L->top++, p3);  /* 3rd argument */
     101              :   /* metamethod may yield only when called from Lua code */
     102           56 :   luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
     103           51 :   if (hasres) {  /* if has result, move it to its place */
     104           49 :     p3 = restorestack(L, result);
     105           49 :     setobjs2s(L, p3, --L->top);
     106              :   }
     107           51 : }
     108              : 
     109              : 
     110       135204 : void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
     111              :   int loop;
     112       142395 :   for (loop = 0; loop < MAXTAGLOOP; loop++) {
     113              :     const TValue *tm;
     114       142395 :     if (ttistable(t)) {  /* `t' is a table? */
     115       135228 :       Table *h = hvalue(t);
     116       135228 :       const TValue *res = luaH_get(h, key); /* do a primitive get */
     117       135228 :       if (!ttisnil(res) ||  /* result is not nil? */
     118         1289 :           (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
     119       135186 :         setobj2s(L, val, res);
     120       135186 :         return;
     121              :       }
     122              :       /* else will try the tag method */
     123              :     }
     124         7167 :     else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
     125            6 :       luaG_typeerror(L, t, "index");
     126         7203 :     if (ttisfunction(tm)) {
     127           12 :       callTM(L, tm, t, key, val, 1);
     128           12 :       return;
     129              :     }
     130         7191 :     t = tm;  /* else repeat with 'tm' */
     131              :   }
     132            0 :   luaG_runerror(L, "loop in gettable");
     133              : }
     134              : 
     135              : 
     136       100472 : void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
     137              :   int loop;
     138       100473 :   for (loop = 0; loop < MAXTAGLOOP; loop++) {
     139              :     const TValue *tm;
     140       100473 :     if (ttistable(t)) {  /* `t' is a table? */
     141       100465 :       Table *h = hvalue(t);
     142       100465 :       TValue *oldval = cast(TValue *, luaH_get(h, key));
     143              :       /* if previous value is not nil, there must be a previous entry
     144              :          in the table; moreover, a metamethod has no relevance */
     145       100465 :       if (!ttisnil(oldval) ||
     146              :          /* previous value is nil; must check the metamethod */
     147        59544 :          ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
     148              :          /* no metamethod; is there a previous entry in the table? */
     149              :          (oldval != luaO_nilobject ||
     150              :          /* no previous entry; must create one. (The next test is
     151              :             always true; we only need the assignment.) */
     152        41810 :          (oldval = luaH_newkey(L, h, key), 1)))) {
     153              :         /* no metamethod and (now) there is an entry with given key */
     154       100458 :         setobj2t(L, oldval, val);  /* assign new value to that entry */
     155       100458 :         invalidateTMcache(h);
     156       100458 :         luaC_barrierback(L, obj2gco(h), val);
     157       100458 :         return;
     158              :       }
     159              :       /* else will try the metamethod */
     160              :     }
     161              :     else  /* not a table; check metamethod */
     162            8 :       if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
     163            8 :         luaG_typeerror(L, t, "index");
     164              :     /* there is a metamethod */
     165            5 :     if (ttisfunction(tm)) {
     166            4 :       callTM(L, tm, t, key, val, 0);
     167            2 :       return;
     168              :     }
     169            1 :     t = tm;  /* else repeat with 'tm' */
     170              :   }
     171            0 :   luaG_runerror(L, "loop in settable");
     172              : }
     173              : 
     174              : 
     175          225 : static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
     176              :                        StkId res, TMS event) {
     177          225 :   const TValue *tm = luaT_gettmbyobj(L, p1, event);  /* try first operand */
     178          225 :   if (ttisnil(tm))
     179          201 :     tm = luaT_gettmbyobj(L, p2, event);  /* try second operand */
     180          225 :   if (ttisnil(tm)) return 0;
     181           32 :   callTM(L, tm, p1, p2, res, 1);
     182           32 :   return 1;
     183              : }
     184              : 
     185              : 
     186           10 : static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2,
     187              :                                   TMS event) {
     188           10 :   const TValue *tm1 = fasttm(L, mt1, event);
     189              :   const TValue *tm2;
     190           10 :   if (tm1 == NULL) return NULL;  /* no metamethod */
     191            5 :   if (mt1 == mt2) return tm1;  /* same metatables => same metamethods */
     192            0 :   tm2 = fasttm(L, mt2, event);
     193            0 :   if (tm2 == NULL) return NULL;  /* no metamethod */
     194            0 :   if (luaV_rawequalobj(tm1, tm2))  /* same metamethods? */
     195            0 :     return tm1;
     196            0 :   return NULL;
     197              : }
     198              : 
     199              : 
     200          114 : static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
     201              :                          TMS event) {
     202          114 :   if (!call_binTM(L, p1, p2, L->top, event))
     203          106 :     return -1;  /* no metamethod */
     204              :   else
     205            8 :     return !l_isfalse(L->top);
     206              : }
     207              : 
     208              : 
     209        91713 : static int l_strcmp (const TString *ls, const TString *rs) {
     210        91713 :   const char *l = getstr(ls);
     211        91713 :   size_t ll = ls->tsv.len;
     212        91713 :   const char *r = getstr(rs);
     213        91713 :   size_t lr = rs->tsv.len;
     214            1 :   for (;;) {
     215        91714 :     int temp = strcoll(l, r);
     216        91714 :     if (temp != 0) return temp;
     217              :     else {  /* strings are equal up to a `\0' */
     218         2673 :       size_t len = strlen(l);  /* index of first `\0' in both strings */
     219         2673 :       if (len == lr)  /* r is finished? */
     220         2671 :         return (len == ll) ? 0 : 1;
     221            2 :       else if (len == ll)  /* l is finished? */
     222            1 :         return -1;  /* l is smaller than r (because r is not finished) */
     223              :       /* both strings longer than `len'; go on comparing (after the `\0') */
     224            1 :       len++;
     225            1 :       l += len; ll -= len; r += len; lr -= len;
     226              :     }
     227              :   }
     228              : }
     229              : 
     230              : 
     231        91726 : int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
     232              :   int res;
     233        91726 :   if (ttisnumber(l) && ttisnumber(r))
     234           54 :     return luai_numlt(L, nvalue(l), nvalue(r));
     235        91672 :   else if (ttisstring(l) && ttisstring(r))
     236        91623 :     return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
     237           49 :   else if ((res = call_orderTM(L, l, r, TM_LT)) < 0)
     238           43 :     luaG_ordererror(L, l, r);
     239            6 :   return res;
     240              : }
     241              : 
     242              : 
     243        11099 : int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
     244              :   int res;
     245        11099 :   if (ttisnumber(l) && ttisnumber(r))
     246        10976 :     return luai_numle(L, nvalue(l), nvalue(r));
     247          123 :   else if (ttisstring(l) && ttisstring(r))
     248           90 :     return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
     249           33 :   else if ((res = call_orderTM(L, l, r, TM_LE)) >= 0)  /* first try `le' */
     250            1 :     return res;
     251           32 :   else if ((res = call_orderTM(L, r, l, TM_LT)) < 0)  /* else try `lt' */
     252           31 :     luaG_ordererror(L, l, r);
     253            1 :   return !res;
     254              : }
     255              : 
     256              : 
     257              : /*
     258              : ** equality of Lua values. L == NULL means raw equality (no metamethods)
     259              : */
     260        39573 : int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) {
     261              :   const TValue *tm;
     262              :   lua_assert(ttisequal(t1, t2));
     263        39573 :   switch (ttype(t1)) {
     264          180 :     case LUA_TNIL: return 1;
     265         3017 :     case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
     266          171 :     case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2);  /* true must be 1 !! */
     267            0 :     case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
     268          653 :     case LUA_TLCF: return fvalue(t1) == fvalue(t2);
     269        33318 :     case LUA_TSHRSTR: return eqshrstr(rawtsvalue(t1), rawtsvalue(t2));
     270          576 :     case LUA_TLNGSTR: return luaS_eqlngstr(rawtsvalue(t1), rawtsvalue(t2));
     271           11 :     case LUA_TUSERDATA: {
     272           11 :       if (uvalue(t1) == uvalue(t2)) return 1;
     273            1 :       else if (L == NULL) return 0;
     274            1 :       tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ);
     275            1 :       break;  /* will try TM */
     276              :     }
     277         1411 :     case LUA_TTABLE: {
     278         1411 :       if (hvalue(t1) == hvalue(t2)) return 1;
     279           10 :       else if (L == NULL) return 0;
     280            9 :       tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
     281            9 :       break;  /* will try TM */
     282              :     }
     283          236 :     default:
     284              :       lua_assert(iscollectable(t1));
     285          236 :       return gcvalue(t1) == gcvalue(t2);
     286              :   }
     287           10 :   if (tm == NULL) return 0;  /* no TM? */
     288            5 :   callTM(L, tm, t1, t2, L->top, 1);  /* call TM */
     289            2 :   return !l_isfalse(L->top);
     290              : }
     291              : 
     292              : 
     293        10417 : void luaV_concat (lua_State *L, int total) {
     294              :   lua_assert(total >= 2);
     295              :   do {
     296        10999 :     StkId top = L->top;
     297        10999 :     int n = 2;  /* number of elements handled in this pass (at least 2) */
     298        10999 :     if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
     299           12 :       if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
     300            9 :         luaG_concaterror(L, top-2, top-1);
     301              :     }
     302        10987 :     else if (tsvalue(top-1)->len == 0)  /* second operand is empty? */
     303          579 :       (void)tostring(L, top - 2);  /* result is first operand */
     304        10408 :     else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
     305          713 :       setobjs2s(L, top - 2, top - 1);  /* result is second op. */
     306              :     }
     307              :     else {
     308              :       /* at least two non-empty string values; get as many as possible */
     309         9695 :       size_t tl = tsvalue(top-1)->len;
     310              :       char *buffer;
     311              :       int i;
     312              :       /* collect total length */
     313        26526 :       for (i = 1; i < total && tostring(L, top-i-1); i++) {
     314        16831 :         size_t l = tsvalue(top-i-1)->len;
     315        16831 :         if (l >= (MAX_SIZET/sizeof(char)) - tl)
     316            0 :           luaG_runerror(L, "string length overflow");
     317        16831 :         tl += l;
     318              :       }
     319         9695 :       buffer = luaZ_openspace(L, &G(L)->buff, tl);
     320         9695 :       tl = 0;
     321         9695 :       n = i;
     322              :       do {  /* concat all strings */
     323        26526 :         size_t l = tsvalue(top-i)->len;
     324        26526 :         memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
     325        26526 :         tl += l;
     326        26526 :       } while (--i > 0);
     327         9695 :       setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
     328              :     }
     329        10990 :     total -= n-1;  /* got 'n' strings to create 1 new */
     330        10990 :     L->top -= n-1;  /* popped 'n' strings and pushed one */
     331        10990 :   } while (total > 1);  /* repeat until only 1 result left */
     332        10408 : }
     333              : 
     334              : 
     335        18678 : void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
     336              :   const TValue *tm;
     337        18678 :   switch (ttypenv(rb)) {
     338        18670 :     case LUA_TTABLE: {
     339        18670 :       Table *h = hvalue(rb);
     340        18670 :       tm = fasttm(L, h->metatable, TM_LEN);
     341        18670 :       if (tm) break;  /* metamethod? break switch to call it */
     342        18667 :       setnvalue(ra, cast_num(luaH_getn(h)));  /* else primitive len */
     343        18667 :       return;
     344              :     }
     345            2 :     case LUA_TSTRING: {
     346            2 :       setnvalue(ra, cast_num(tsvalue(rb)->len));
     347            2 :       return;
     348              :     }
     349            6 :     default: {  /* try metamethod */
     350            6 :       tm = luaT_gettmbyobj(L, rb, TM_LEN);
     351            6 :       if (ttisnil(tm))  /* no metamethod? */
     352            6 :         luaG_typeerror(L, rb, "get length of");
     353            0 :       break;
     354              :     }
     355              :   }
     356            3 :   callTM(L, tm, rb, rb, ra, 1);
     357              : }
     358              : 
     359              : 
     360          123 : void luaV_arith (lua_State *L, StkId ra, const TValue *rb,
     361              :                  const TValue *rc, TMS op) {
     362              :   TValue tempb, tempc;
     363              :   const TValue *b, *c;
     364          182 :   if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
     365           59 :       (c = luaV_tonumber(rc, &tempc)) != NULL) {
     366           24 :     lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c));
     367           24 :     setnvalue(ra, res);
     368              :   }
     369           99 :   else if (!call_binTM(L, rb, rc, ra, op))
     370           78 :     luaG_aritherror(L, rb, rc);
     371           45 : }
     372              : 
     373              : 
     374              : /*
     375              : ** check whether cached closure in prototype 'p' may be reused, that is,
     376              : ** whether there is a cached closure with the same upvalues needed by
     377              : ** new closure to be created.
     378              : */
     379         2906 : static Closure *getcached (Proto *p, UpVal **encup, StkId base) {
     380         2906 :   Closure *c = p->cache;
     381         2906 :   if (c != NULL) {  /* is there a cached closure? */
     382         1296 :     int nup = p->sizeupvalues;
     383         1296 :     Upvaldesc *uv = p->upvalues;
     384              :     int i;
     385         1613 :     for (i = 0; i < nup; i++) {  /* check whether it has right upvalues */
     386         1611 :       TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
     387         1611 :       if (c->l.upvals[i]->v != v)
     388         1294 :         return NULL;  /* wrong upvalue; cannot reuse closure */
     389              :     }
     390              :   }
     391         1612 :   return c;  /* return cached closure (or NULL if no cached closure) */
     392              : }
     393              : 
     394              : 
     395              : /*
     396              : ** create a new Lua closure, push it in the stack, and initialize
     397              : ** its upvalues. Note that the call to 'luaC_barrierproto' must come
     398              : ** before the assignment to 'p->cache', as the function needs the
     399              : ** original value of that field.
     400              : */
     401         2904 : static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
     402              :                          StkId ra) {
     403         2904 :   int nup = p->sizeupvalues;
     404         2904 :   Upvaldesc *uv = p->upvalues;
     405              :   int i;
     406         2904 :   Closure *ncl = luaF_newLclosure(L, nup);
     407         2904 :   ncl->l.p = p;
     408         2904 :   setclLvalue(L, ra, ncl);  /* anchor new closure in stack */
     409        12795 :   for (i = 0; i < nup; i++) {  /* fill in its upvalues */
     410         9891 :     if (uv[i].instack)  /* upvalue refers to local variable? */
     411         6871 :       ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
     412              :     else  /* get upvalue from enclosing function */
     413         3020 :       ncl->l.upvals[i] = encup[uv[i].idx];
     414              :   }
     415         2904 :   luaC_barrierproto(L, p, ncl);
     416         2904 :   p->cache = ncl;  /* save it on cache for reuse */
     417         2904 : }
     418              : 
     419              : 
     420              : /*
     421              : ** finish execution of an opcode interrupted by an yield
     422              : */
     423         5954 : void luaV_finishOp (lua_State *L) {
     424         5954 :   CallInfo *ci = L->ci;
     425         5954 :   StkId base = ci->u.l.base;
     426         5954 :   Instruction inst = *(ci->u.l.savedpc - 1);  /* interrupted instruction */
     427         5954 :   OpCode op = GET_OPCODE(inst);
     428         5954 :   switch (op) {  /* finish its execution */
     429            0 :     case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
     430              :     case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN:
     431              :     case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
     432            0 :       setobjs2s(L, base + GETARG_A(inst), --L->top);
     433            0 :       break;
     434              :     }
     435            2 :     case OP_LE: case OP_LT: case OP_EQ: {
     436            2 :       int res = !l_isfalse(L->top - 1);
     437            2 :       L->top--;
     438              :       /* metamethod should not be called when operand is K */
     439              :       lua_assert(!ISK(GETARG_B(inst)));
     440            2 :       if (op == OP_LE &&  /* "<=" using "<" instead? */
     441            0 :           ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
     442            0 :         res = !res;  /* invert result */
     443              :       lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
     444            2 :       if (res != GETARG_A(inst))  /* condition failed? */
     445            1 :         ci->u.l.savedpc++;  /* skip jump instruction */
     446            2 :       break;
     447              :     }
     448            0 :     case OP_CONCAT: {
     449            0 :       StkId top = L->top - 1;  /* top when 'call_binTM' was called */
     450            0 :       int b = GETARG_B(inst);      /* first element to concatenate */
     451            0 :       int total = cast_int(top - 1 - (base + b));  /* yet to concatenate */
     452            0 :       setobj2s(L, top - 2, top);  /* put TM result in proper position */
     453            0 :       if (total > 1) {  /* are there elements to concat? */
     454            0 :         L->top = top - 1;  /* top is one after last element (at top-2) */
     455            0 :         luaV_concat(L, total);  /* concat them (may yield again) */
     456              :       }
     457              :       /* move final result to final position */
     458            0 :       setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
     459            0 :       L->top = ci->top;  /* restore top */
     460            0 :       break;
     461              :     }
     462            0 :     case OP_TFORCALL: {
     463              :       lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
     464            0 :       L->top = ci->top;  /* correct top */
     465            0 :       break;
     466              :     }
     467         5945 :     case OP_CALL: {
     468         5945 :       if (GETARG_C(inst) - 1 >= 0)  /* nresults >= 0? */
     469         5945 :         L->top = ci->top;  /* adjust results */
     470         5945 :       break;
     471              :     }
     472            7 :     case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE:
     473            7 :       break;
     474         5954 :     default: lua_assert(0);
     475              :   }
     476         5954 : }
     477              : 
     478              : 
     479              : 
     480              : /*
     481              : ** some macros for common tasks in `luaV_execute'
     482              : */
     483              : 
     484              : #if !defined luai_runtimecheck
     485              : #define luai_runtimecheck(L, c)         /* void */
     486              : #endif
     487              : 
     488              : 
     489              : #define RA(i)   (base+GETARG_A(i))
     490              : /* to be used after possible stack reallocation */
     491              : #define RB(i)   check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
     492              : #define RC(i)   check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
     493              : #define RKB(i)  check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
     494              :         ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
     495              : #define RKC(i)  check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
     496              :         ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
     497              : #define KBx(i)  \
     498              :   (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
     499              : 
     500              : 
     501              : /* execute a jump instruction */
     502              : #define dojump(ci,i,e) \
     503              :   { int a = GETARG_A(i); \
     504              :     if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
     505              :     ci->u.l.savedpc += GETARG_sBx(i) + e; }
     506              : 
     507              : /* for test instructions, execute the jump instruction that follows it */
     508              : #define donextjump(ci)  { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
     509              : 
     510              : 
     511              : #define Protect(x)      { {x;}; base = ci->u.l.base; }
     512              : 
     513              : #define checkGC(L,c)  \
     514              :   Protect( luaC_condGC(L,{L->top = (c);  /* limit of live values */ \
     515              :                           luaC_step(L); \
     516              :                           L->top = ci->top;})  /* restore top */ \
     517              :            luai_threadyield(L); )
     518              : 
     519              : 
     520              : #define arith_op(op,tm) { \
     521              :         TValue *rb = RKB(i); \
     522              :         TValue *rc = RKC(i); \
     523              :         if (ttisnumber(rb) && ttisnumber(rc)) { \
     524              :           lua_Number nb = nvalue(rb), nc = nvalue(rc); \
     525              :           setnvalue(ra, op(L, nb, nc)); \
     526              :         } \
     527              :         else { Protect(luaV_arith(L, ra, rb, rc, tm)); } }
     528              : 
     529              : 
     530              : #define vmdispatch(o)   switch(o)
     531              : #define vmcase(l,b)     case l: {b}  break;
     532              : #define vmcasenb(l,b)   case l: {b}             /* nb = no break */
     533              : 
     534        12990 : void luaV_execute (lua_State *L) {
     535        12990 :   CallInfo *ci = L->ci;
     536              :   LClosure *cl;
     537              :   TValue *k;
     538              :   StkId base;
     539        32446 :  newframe:  /* reentry point when frame changes (call/return) */
     540              :   lua_assert(ci == L->ci);
     541        45436 :   cl = clLvalue(ci->func);
     542        45436 :   k = cl->p->k;
     543        45436 :   base = ci->u.l.base;
     544              :   /* main loop of interpreter */
     545       617825 :   for (;;) {
     546       663261 :     Instruction i = *(ci->u.l.savedpc++);
     547              :     StkId ra;
     548       663261 :     if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
     549         1345 :         (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
     550           32 :       Protect(traceexec(L));
     551              :     }
     552              :     /* WARNING: several calls may realloc the stack and invalidate `ra' */
     553       663261 :     ra = RA(i);
     554              :     lua_assert(base == ci->u.l.base);
     555              :     lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
     556       663261 :     vmdispatch (GET_OPCODE(i)) {
     557        89026 :       vmcase(OP_MOVE,
     558              :         setobjs2s(L, ra, RB(i));
     559              :       )
     560        32685 :       vmcase(OP_LOADK,
     561              :         TValue *rb = k + GETARG_Bx(i);
     562              :         setobj2s(L, ra, rb);
     563              :       )
     564            0 :       vmcase(OP_LOADKX,
     565              :         TValue *rb;
     566              :         lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
     567              :         rb = k + GETARG_Ax(*ci->u.l.savedpc++);
     568              :         setobj2s(L, ra, rb);
     569              :       )
     570         7675 :       vmcase(OP_LOADBOOL,
     571              :         setbvalue(ra, GETARG_B(i));
     572              :         if (GETARG_C(i)) ci->u.l.savedpc++;  /* skip next instruction (if C) */
     573              :       )
     574          731 :       vmcase(OP_LOADNIL,
     575              :         int b = GETARG_B(i);
     576              :         do {
     577              :           setnilvalue(ra++);
     578              :         } while (b--);
     579              :       )
     580        32073 :       vmcase(OP_GETUPVAL,
     581              :         int b = GETARG_B(i);
     582              :         setobj2s(L, ra, cl->upvals[b]->v);
     583              :       )
     584        48511 :       vmcase(OP_GETTABUP,
     585              :         int b = GETARG_B(i);
     586              :         Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
     587              :       )
     588        72864 :       vmcase(OP_GETTABLE,
     589              :         Protect(luaV_gettable(L, RB(i), RKC(i), ra));
     590              :       )
     591         1518 :       vmcase(OP_SETTABUP,
     592              :         int a = GETARG_A(i);
     593              :         Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
     594              :       )
     595         2690 :       vmcase(OP_SETUPVAL,
     596              :         UpVal *uv = cl->upvals[GETARG_B(i)];
     597              :         setobj(L, uv->v, ra);
     598              :         luaC_barrier(L, uv, ra);
     599              :       )
     600        82689 :       vmcase(OP_SETTABLE,
     601              :         Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
     602              :       )
     603         6593 :       vmcase(OP_NEWTABLE,
     604              :         int b = GETARG_B(i);
     605              :         int c = GETARG_C(i);
     606              :         Table *t = luaH_new(L);
     607              :         sethvalue(L, ra, t);
     608              :         if (b != 0 || c != 0)
     609              :           luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
     610              :         checkGC(L, ra + 1);
     611              :       )
     612         7177 :       vmcase(OP_SELF,
     613              :         StkId rb = RB(i);
     614              :         setobjs2s(L, ra+1, rb);
     615              :         Protect(luaV_gettable(L, rb, RKC(i), ra));
     616              :       )
     617        14449 :       vmcase(OP_ADD,
     618              :         arith_op(luai_numadd, TM_ADD);
     619              :       )
     620        11106 :       vmcase(OP_SUB,
     621              :         arith_op(luai_numsub, TM_SUB);
     622              :       )
     623         1169 :       vmcase(OP_MUL,
     624              :         arith_op(luai_nummul, TM_MUL);
     625              :       )
     626           37 :       vmcase(OP_DIV,
     627              :         arith_op(luai_numdiv, TM_DIV);
     628              :       )
     629           16 :       vmcase(OP_MOD,
     630              :         arith_op(luai_nummod, TM_MOD);
     631              :       )
     632           20 :       vmcase(OP_POW,
     633              :         arith_op(luai_numpow, TM_POW);
     634              :       )
     635           13 :       vmcase(OP_UNM,
     636              :         TValue *rb = RB(i);
     637              :         if (ttisnumber(rb)) {
     638              :           lua_Number nb = nvalue(rb);
     639              :           setnvalue(ra, luai_numunm(L, nb));
     640              :         }
     641              :         else {
     642              :           Protect(luaV_arith(L, ra, rb, rb, TM_UNM));
     643              :         }
     644              :       )
     645            8 :       vmcase(OP_NOT,
     646              :         TValue *rb = RB(i);
     647              :         int res = l_isfalse(rb);  /* next assignment may change this value */
     648              :         setbvalue(ra, res);
     649              :       )
     650          639 :       vmcase(OP_LEN,
     651              :         Protect(luaV_objlen(L, ra, RB(i)));
     652              :       )
     653         8016 :       vmcase(OP_CONCAT,
     654              :         int b = GETARG_B(i);
     655              :         int c = GETARG_C(i);
     656              :         StkId rb;
     657              :         L->top = base + c + 1;  /* mark the end of concat operands */
     658              :         Protect(luaV_concat(L, c - b + 1));
     659              :         ra = RA(i);  /* 'luav_concat' may invoke TMs and move the stack */
     660              :         rb = b + base;
     661              :         setobjs2s(L, ra, rb);
     662              :         checkGC(L, (ra >= rb ? ra + 1 : rb));
     663              :         L->top = ci->top;  /* restore top */
     664              :       )
     665        18344 :       vmcase(OP_JMP,
     666              :         dojump(ci, i, 0);
     667              :       )
     668        25326 :       vmcase(OP_EQ,
     669              :         TValue *rb = RKB(i);
     670              :         TValue *rc = RKC(i);
     671              :         Protect(
     672              :           if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i))
     673              :             ci->u.l.savedpc++;
     674              :           else
     675              :             donextjump(ci);
     676              :         )
     677              :       )
     678          115 :       vmcase(OP_LT,
     679              :         Protect(
     680              :           if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
     681              :             ci->u.l.savedpc++;
     682              :           else
     683              :             donextjump(ci);
     684              :         )
     685              :       )
     686        11099 :       vmcase(OP_LE,
     687              :         Protect(
     688              :           if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
     689              :             ci->u.l.savedpc++;
     690              :           else
     691              :             donextjump(ci);
     692              :         )
     693              :       )
     694        18081 :       vmcase(OP_TEST,
     695              :         if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
     696              :             ci->u.l.savedpc++;
     697              :           else
     698              :           donextjump(ci);
     699              :       )
     700            5 :       vmcase(OP_TESTSET,
     701              :         TValue *rb = RB(i);
     702              :         if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
     703              :           ci->u.l.savedpc++;
     704              :         else {
     705              :           setobjs2s(L, ra, rb);
     706              :           donextjump(ci);
     707              :         }
     708              :       )
     709        70826 :       vmcase(OP_CALL,
     710              :         int b = GETARG_B(i);
     711              :         int nresults = GETARG_C(i) - 1;
     712              :         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
     713              :         if (luaD_precall(L, ra, nresults)) {  /* C function? */
     714              :           if (nresults >= 0) L->top = ci->top;  /* adjust results */
     715              :           base = ci->u.l.base;
     716              :         }
     717              :         else {  /* Lua function */
     718              :           ci = L->ci;
     719              :           ci->callstatus |= CIST_REENTRY;
     720              :           goto newframe;  /* restart luaV_execute over new Lua function */
     721              :         }
     722              :       )
     723         6775 :       vmcase(OP_TAILCALL,
     724              :         int b = GETARG_B(i);
     725              :         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
     726              :         lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
     727              :         if (luaD_precall(L, ra, LUA_MULTRET))  /* C function? */
     728              :           base = ci->u.l.base;
     729              :         else {
     730              :           /* tail call: put called frame (n) in place of caller one (o) */
     731              :           CallInfo *nci = L->ci;  /* called frame */
     732              :           CallInfo *oci = nci->previous;  /* caller frame */
     733              :           StkId nfunc = nci->func;  /* called function */
     734              :           StkId ofunc = oci->func;  /* caller function */
     735              :           /* last stack slot filled by 'precall' */
     736              :           StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
     737              :           int aux;
     738              :           /* close all upvalues from previous call */
     739              :           if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
     740              :           /* move new frame into old one */
     741              :           for (aux = 0; nfunc + aux < lim; aux++)
     742              :             setobjs2s(L, ofunc + aux, nfunc + aux);
     743              :           oci->u.l.base = ofunc + (nci->u.l.base - nfunc);  /* correct base */
     744              :           oci->top = L->top = ofunc + (L->top - nfunc);  /* correct top */
     745              :           oci->u.l.savedpc = nci->u.l.savedpc;
     746              :           oci->callstatus |= CIST_TAIL;  /* function was tail called */
     747              :           ci = L->ci = oci;  /* remove new frame */
     748              :           lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
     749              :           goto newframe;  /* restart luaV_execute over new Lua function */
     750              :         }
     751              :       )
     752        22366 :       vmcasenb(OP_RETURN,
     753              :         int b = GETARG_B(i);
     754              :         if (b != 0) L->top = ra+b-1;
     755              :         if (cl->p->sizep > 0) luaF_close(L, base);
     756              :         b = luaD_poscall(L, ra);
     757              :         if (!(ci->callstatus & CIST_REENTRY))  /* 'ci' still the called one */
     758              :           return;  /* external invocation: return */
     759              :         else {  /* invocation via reentry: continue execution */
     760              :           ci = L->ci;
     761              :           if (b) L->top = ci->top;
     762              :           lua_assert(isLua(ci));
     763              :           lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
     764              :           goto newframe;  /* restart luaV_execute over new Lua function */
     765              :         }
     766              :       )
     767        16062 :       vmcase(OP_FORLOOP,
     768              :         lua_Number step = nvalue(ra+2);
     769              :         lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */
     770              :         lua_Number limit = nvalue(ra+1);
     771              :         if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
     772              :                                    : luai_numle(L, limit, idx)) {
     773              :           ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
     774              :           setnvalue(ra, idx);  /* update internal index... */
     775              :           setnvalue(ra+3, idx);  /* ...and external index */
     776              :         }
     777              :       )
     778         4469 :       vmcase(OP_FORPREP,
     779              :         const TValue *init = ra;
     780              :         const TValue *plimit = ra+1;
     781              :         const TValue *pstep = ra+2;
     782              :         if (!tonumber(init, ra))
     783              :           luaG_runerror(L, LUA_QL("for") " initial value must be a number");
     784              :         else if (!tonumber(plimit, ra+1))
     785              :           luaG_runerror(L, LUA_QL("for") " limit must be a number");
     786              :         else if (!tonumber(pstep, ra+2))
     787              :           luaG_runerror(L, LUA_QL("for") " step must be a number");
     788              :         setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
     789              :         ci->u.l.savedpc += GETARG_sBx(i);
     790              :       )
     791        52466 :       vmcasenb(OP_TFORCALL,
     792              :         StkId cb = ra + 3;  /* call base */
     793              :         setobjs2s(L, cb+2, ra+2);
     794              :         setobjs2s(L, cb+1, ra+1);
     795              :         setobjs2s(L, cb, ra);
     796              :         L->top = cb + 3;  /* func. + 2 args (state and index) */
     797              :         Protect(luaD_call(L, cb, GETARG_C(i), 1));
     798              :         L->top = ci->top;
     799              :         i = *(ci->u.l.savedpc++);  /* go to next instruction */
     800              :         ra = RA(i);
     801              :         lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
     802              :         goto l_tforloop;
     803              :       )
     804        52466 :       vmcase(OP_TFORLOOP,
     805              :         l_tforloop:
     806              :         if (!ttisnil(ra + 1)) {  /* continue loop? */
     807              :           setobjs2s(L, ra, ra + 1);  /* save control variable */
     808              :            ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
     809              :         }
     810              :       )
     811         1386 :       vmcase(OP_SETLIST,
     812              :         int n = GETARG_B(i);
     813              :         int c = GETARG_C(i);
     814              :         int last;
     815              :         Table *h;
     816              :         if (n == 0) n = cast_int(L->top - ra) - 1;
     817              :         if (c == 0) {
     818              :           lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
     819              :           c = GETARG_Ax(*ci->u.l.savedpc++);
     820              :         }
     821              :         luai_runtimecheck(L, ttistable(ra));
     822              :         h = hvalue(ra);
     823              :         last = ((c-1)*LFIELDS_PER_FLUSH) + n;
     824              :         if (last > h->sizearray)  /* needs more space? */
     825              :           luaH_resizearray(L, h, last);  /* pre-allocate it at once */
     826              :         for (; n > 0; n--) {
     827              :           TValue *val = ra+n;
     828              :           luaH_setint(L, h, last--, val);
     829              :           luaC_barrierback(L, obj2gco(h), val);
     830              :         }
     831              :         L->top = ci->top;  /* correct top (in case of previous open call) */
     832              :       )
     833         2906 :       vmcase(OP_CLOSURE,
     834              :         Proto *p = cl->p->p[GETARG_Bx(i)];
     835              :         Closure *ncl = getcached(p, cl->upvals, base);  /* cached closure */
     836              :         if (ncl == NULL)  /* no match? */
     837              :           pushclosure(L, p, cl->upvals, base, ra);  /* create a new one */
     838              :         else
     839              :           setclLvalue(L, ra, ncl);  /* push cashed closure */
     840              :         checkGC(L, ra + 1);
     841              :       )
     842           57 :       vmcase(OP_VARARG,
     843              :         int b = GETARG_B(i) - 1;
     844              :         int j;
     845              :         int n = cast_int(base - ci->func) - cl->p->numparams - 1;
     846              :         if (b < 0) {  /* B == 0? */
     847              :           b = n;  /* get all var. arguments */
     848              :           Protect(luaD_checkstack(L, n));
     849              :           ra = RA(i);  /* previous call may change the stack */
     850              :           L->top = ra + n;
     851              :         }
     852              :         for (j = 0; j < b; j++) {
     853              :           if (j < n) {
     854              :             setobjs2s(L, ra + j, base - n + j);
     855              :           }
     856              :           else {
     857              :             setnilvalue(ra + j);
     858              :           }
     859              :         }
     860              :       )
     861            0 :       vmcase(OP_EXTRAARG,
     862              :         lua_assert(0);
     863              :       )
     864              :     }
     865              :   }
     866              : }
     867              : 
        

Generated by: LCOV version 2.0-1