lvm.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lvm.c,v 2.63.1.4 2009/07/01 21:10:33 roberto Exp $
00003 ** Lua virtual machine
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 
00012 #define lvm_c
00013 #define LUA_CORE
00014 
00015 #include "lua.h"
00016 
00017 #include "ldebug.h"
00018 #include "ldo.h"
00019 #include "lfunc.h"
00020 #include "lgc.h"
00021 #include "lobject.h"
00022 #include "lopcodes.h"
00023 #include "lstate.h"
00024 #include "lstring.h"
00025 #include "ltable.h"
00026 #include "ltm.h"
00027 #include "lvm.h"
00028 
00029 
00030 
00031 /* limit for table tag-method chains (to avoid loops) */
00032 #define MAXTAGLOOP  100
00033 
00034 
00035 const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
00036   lua_Number num;
00037   if (ttisnumber(obj)) return obj;
00038   if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
00039     setnvalue(n, num);
00040     return n;
00041   }
00042   else
00043     return NULL;
00044 }
00045 
00046 
00047 int luaV_tostring (lua_State *L, StkId obj) {
00048   if (!ttisnumber(obj))
00049     return 0;
00050   else {
00051     char s[LUAI_MAXNUMBER2STR];
00052     lua_Number n = nvalue(obj);
00053     lua_number2str(s, n);
00054     setsvalue2s(L, obj, luaS_new(L, s));
00055     return 1;
00056   }
00057 }
00058 
00059 
00060 static void traceexec (lua_State *L, const Instruction *pc) {
00061   lu_byte mask = L->hookmask;
00062   const Instruction *oldpc = L->savedpc;
00063   L->savedpc = pc;
00064   if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
00065     resethookcount(L);
00066     luaD_callhook(L, LUA_HOOKCOUNT, -1);
00067   }
00068   if (mask & LUA_MASKLINE) {
00069     Proto *p = ci_func(L->ci)->l.p;
00070     int npc = pcRel(pc, p);
00071     int newline = getline(p, npc);
00072     /* call linehook when enter a new function, when jump back (loop),
00073        or when enter a new line */
00074     if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
00075       luaD_callhook(L, LUA_HOOKLINE, newline);
00076   }
00077 }
00078 
00079 
00080 static void callTMres (lua_State *L, StkId res, const TValue *f,
00081                         const TValue *p1, const TValue *p2) {
00082   ptrdiff_t result = savestack(L, res);
00083   setobj2s(L, L->top, f);  /* push function */
00084   setobj2s(L, L->top+1, p1);  /* 1st argument */
00085   setobj2s(L, L->top+2, p2);  /* 2nd argument */
00086   luaD_checkstack(L, 3);
00087   L->top += 3;
00088   luaD_call(L, L->top - 3, 1);
00089   res = restorestack(L, result);
00090   L->top--;
00091   setobjs2s(L, res, L->top);
00092 }
00093 
00094 
00095 
00096 static void callTM (lua_State *L, const TValue *f, const TValue *p1,
00097                     const TValue *p2, const TValue *p3) {
00098   setobj2s(L, L->top, f);  /* push function */
00099   setobj2s(L, L->top+1, p1);  /* 1st argument */
00100   setobj2s(L, L->top+2, p2);  /* 2nd argument */
00101   setobj2s(L, L->top+3, p3);  /* 3th argument */
00102   luaD_checkstack(L, 4);
00103   L->top += 4;
00104   luaD_call(L, L->top - 4, 0);
00105 }
00106 
00107 
00108 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
00109   int loop;
00110   for (loop = 0; loop < MAXTAGLOOP; loop++) {
00111     const TValue *tm;
00112     if (ttistable(t)) {  /* `t' is a table? */
00113       Table *h = hvalue(t);
00114       const TValue *res = luaH_get(h, key); /* do a primitive get */
00115       if (!ttisnil(res) ||  /* result is no nil? */
00116           (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
00117         setobj2s(L, val, res);
00118         return;
00119       }
00120       /* else will try the tag method */
00121     }
00122     else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
00123       luaG_typeerror(L, t, "index");
00124     if (ttisfunction(tm)) {
00125       callTMres(L, val, tm, t, key);
00126       return;
00127     }
00128     t = tm;  /* else repeat with `tm' */
00129   }
00130   luaG_runerror(L, "loop in gettable");
00131 }
00132 
00133 
00134 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
00135   int loop;
00136   TValue temp;
00137   for (loop = 0; loop < MAXTAGLOOP; loop++) {
00138     const TValue *tm;
00139     if (ttistable(t)) {  /* `t' is a table? */
00140       Table *h = hvalue(t);
00141       TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
00142       if (!ttisnil(oldval) ||  /* result is no nil? */
00143           (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
00144         setobj2t(L, oldval, val);
00145         luaC_barriert(L, h, val);
00146         return;
00147       }
00148       /* else will try the tag method */
00149     }
00150     else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
00151       luaG_typeerror(L, t, "index");
00152     if (ttisfunction(tm)) {
00153       callTM(L, tm, t, key, val);
00154       return;
00155     }
00156     /* else repeat with `tm' */
00157     setobj(L, &temp, tm);  /* avoid pointing inside table (may rehash) */
00158     t = &temp;
00159     t = tm;
00160   }
00161   luaG_runerror(L, "loop in settable");
00162 }
00163 
00164 
00165 static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
00166                        StkId res, TMS event) {
00167   const TValue *tm = luaT_gettmbyobj(L, p1, event);  /* try first operand */
00168   if (ttisnil(tm))
00169     tm = luaT_gettmbyobj(L, p2, event);  /* try second operand */
00170   if (ttisnil(tm)) return 0;
00171   callTMres(L, res, tm, p1, p2);
00172   return 1;
00173 }
00174 
00175 
00176 static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
00177                                   TMS event) {
00178   const TValue *tm1 = fasttm(L, mt1, event);
00179   const TValue *tm2;
00180   if (tm1 == NULL) return NULL;  /* no metamethod */
00181   if (mt1 == mt2) return tm1;  /* same metatables => same metamethods */
00182   tm2 = fasttm(L, mt2, event);
00183   if (tm2 == NULL) return NULL;  /* no metamethod */
00184   if (luaO_rawequalObj(tm1, tm2))  /* same metamethods? */
00185     return tm1;
00186   return NULL;
00187 }
00188 
00189 
00190 static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
00191                          TMS event) {
00192   const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
00193   const TValue *tm2;
00194   if (ttisnil(tm1)) return -1;  /* no metamethod? */
00195   tm2 = luaT_gettmbyobj(L, p2, event);
00196   if (!luaO_rawequalObj(tm1, tm2))  /* different metamethods? */
00197     return -1;
00198   callTMres(L, L->top, tm1, p1, p2);
00199   return !l_isfalse(L->top);
00200 }
00201 
00202 
00203 static int l_strcmp (const TString *ls, const TString *rs) {
00204   const char *l = getstr(ls);
00205   size_t ll = ls->tsv.len;
00206   const char *r = getstr(rs);
00207   size_t lr = rs->tsv.len;
00208   for (;;) {
00209     int temp = strcoll(l, r);
00210     if (temp != 0) return temp;
00211     else {  /* strings are equal up to a `\0' */
00212       size_t len = strlen(l);  /* index of first `\0' in both strings */
00213       if (len == lr)  /* r is finished? */
00214         return (len == ll) ? 0 : 1;
00215       else if (len == ll)  /* l is finished? */
00216         return -1;  /* l is smaller than r (because r is not finished) */
00217       /* both strings longer than `len'; go on comparing (after the `\0') */
00218       len++;
00219       l += len; ll -= len; r += len; lr -= len;
00220     }
00221   }
00222 }
00223 
00224 
00225 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
00226   int res;
00227   if (ttype(l) != ttype(r))
00228     return luaG_ordererror(L, l, r);
00229   else if (ttisnumber(l))
00230     return luai_numlt(nvalue(l), nvalue(r));
00231   else if (ttisstring(l))
00232     return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
00233   else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
00234     return res;
00235   return luaG_ordererror(L, l, r);
00236 }
00237 
00238 
00239 static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
00240   int res;
00241   if (ttype(l) != ttype(r))
00242     return luaG_ordererror(L, l, r);
00243   else if (ttisnumber(l))
00244     return luai_numle(nvalue(l), nvalue(r));
00245   else if (ttisstring(l))
00246     return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
00247   else if ((res = call_orderTM(L, l, r, TM_LE)) != -1)  /* first try `le' */
00248     return res;
00249   else if ((res = call_orderTM(L, r, l, TM_LT)) != -1)  /* else try `lt' */
00250     return !res;
00251   return luaG_ordererror(L, l, r);
00252 }
00253 
00254 
00255 int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
00256   const TValue *tm;
00257   lua_assert(ttype(t1) == ttype(t2));
00258   switch (ttype(t1)) {
00259     case LUA_TNIL: return 1;
00260     case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
00261     case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2);  /* true must be 1 !! */
00262     case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
00263     case LUA_TUSERDATA: {
00264       if (uvalue(t1) == uvalue(t2)) return 1;
00265       tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
00266                          TM_EQ);
00267       break;  /* will try TM */
00268     }
00269     case LUA_TTABLE: {
00270       if (hvalue(t1) == hvalue(t2)) return 1;
00271       tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
00272       break;  /* will try TM */
00273     }
00274     default: return gcvalue(t1) == gcvalue(t2);
00275   }
00276   if (tm == NULL) return 0;  /* no TM? */
00277   callTMres(L, L->top, tm, t1, t2);  /* call TM */
00278   return !l_isfalse(L->top);
00279 }
00280 
00281 
00282 void luaV_concat (lua_State *L, int total, int last) {
00283   do {
00284     StkId top = L->base + last + 1;
00285     int n = 2;  /* number of elements handled in this pass (at least 2) */
00286     if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
00287       if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
00288         luaG_concaterror(L, top-2, top-1);
00289     } else if (tsvalue(top-1)->len == 0)  /* second op is empty? */
00290       (void)tostring(L, top - 2);  /* result is first op (as string) */
00291     else {
00292       /* at least two string values; get as many as possible */
00293       size_t tl = tsvalue(top-1)->len;
00294       char *buffer;
00295       int i;
00296       /* collect total length */
00297       for (n = 1; n < total && tostring(L, top-n-1); n++) {
00298         size_t l = tsvalue(top-n-1)->len;
00299         if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
00300         tl += l;
00301       }
00302       buffer = luaZ_openspace(L, &G(L)->buff, tl);
00303       tl = 0;
00304       for (i=n; i>0; i--) {  /* concat all strings */
00305         size_t l = tsvalue(top-i)->len;
00306         memcpy(buffer+tl, svalue(top-i), l);
00307         tl += l;
00308       }
00309       setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
00310     }
00311     total -= n-1;  /* got `n' strings to create 1 new */
00312     last -= n-1;
00313   } while (total > 1);  /* repeat until only 1 result left */
00314 }
00315 
00316 
00317 static void Arith (lua_State *L, StkId ra, const TValue *rb,
00318                    const TValue *rc, TMS op) {
00319   TValue tempb, tempc;
00320   const TValue *b, *c;
00321   if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
00322       (c = luaV_tonumber(rc, &tempc)) != NULL) {
00323     lua_Number nb = nvalue(b), nc = nvalue(c);
00324     switch (op) {
00325       case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
00326       case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
00327       case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
00328       case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
00329       case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
00330       case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
00331       case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
00332       default: lua_assert(0); break;
00333     }
00334   }
00335   else if (!call_binTM(L, rb, rc, ra, op))
00336     luaG_aritherror(L, rb, rc);
00337 }
00338 
00339 
00340 
00341 /*
00342 ** some macros for common tasks in `luaV_execute'
00343 */
00344 
00345 #define runtime_check(L, c) { if (!(c)) break; }
00346 
00347 #define RA(i)   (base+GETARG_A(i))
00348 /* to be used after possible stack reallocation */
00349 #define RB(i)   check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
00350 #define RC(i)   check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
00351 #define RKB(i)  check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
00352     ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
00353 #define RKC(i)  check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
00354     ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
00355 #define KBx(i)  check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
00356 
00357 
00358 #define dojump(L,pc,i)  {(pc) += (i); luai_threadyield(L);}
00359 
00360 
00361 #define Protect(x)  { L->savedpc = pc; {x;}; base = L->base; }
00362 
00363 
00364 #define arith_op(op,tm) { \
00365         TValue *rb = RKB(i); \
00366         TValue *rc = RKC(i); \
00367         if (ttisnumber(rb) && ttisnumber(rc)) { \
00368           lua_Number nb = nvalue(rb), nc = nvalue(rc); \
00369           setnvalue(ra, op(nb, nc)); \
00370         } \
00371         else \
00372           Protect(Arith(L, ra, rb, rc, tm)); \
00373       }
00374 
00375 
00376 
00377 void luaV_execute (lua_State *L, int nexeccalls) {
00378   LClosure *cl;
00379   StkId base;
00380   TValue *k;
00381   const Instruction *pc;
00382  reentry:  /* entry point */
00383   lua_assert(isLua(L->ci));
00384   pc = L->savedpc;
00385   cl = &clvalue(L->ci->func)->l;
00386   base = L->base;
00387   k = cl->p->k;
00388   /* main loop of interpreter */
00389   for (;;) {
00390     const Instruction i = *pc++;
00391     StkId ra;
00392     if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
00393         (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
00394       traceexec(L, pc);
00395       if (L->status == LUA_YIELD) {  /* did hook yield? */
00396         L->savedpc = pc - 1;
00397         return;
00398       }
00399       base = L->base;
00400     }
00401     /* warning!! several calls may realloc the stack and invalidate `ra' */
00402     ra = RA(i);
00403     lua_assert(base == L->base && L->base == L->ci->base);
00404     lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
00405     lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
00406     switch (GET_OPCODE(i)) {
00407       case OP_MOVE: {
00408         setobjs2s(L, ra, RB(i));
00409         continue;
00410       }
00411       case OP_LOADK: {
00412         setobj2s(L, ra, KBx(i));
00413         continue;
00414       }
00415       case OP_LOADBOOL: {
00416         setbvalue(ra, GETARG_B(i));
00417         if (GETARG_C(i)) pc++;  /* skip next instruction (if C) */
00418         continue;
00419       }
00420       case OP_LOADNIL: {
00421         TValue *rb = RB(i);
00422         do {
00423           setnilvalue(rb--);
00424         } while (rb >= ra);
00425         continue;
00426       }
00427       case OP_GETUPVAL: {
00428         int b = GETARG_B(i);
00429         setobj2s(L, ra, cl->upvals[b]->v);
00430         continue;
00431       }
00432       case OP_GETGLOBAL: {
00433         TValue g;
00434         TValue *rb = KBx(i);
00435         sethvalue(L, &g, cl->env);
00436         lua_assert(ttisstring(rb));
00437         Protect(luaV_gettable(L, &g, rb, ra));
00438         continue;
00439       }
00440       case OP_GETTABLE: {
00441         Protect(luaV_gettable(L, RB(i), RKC(i), ra));
00442         continue;
00443       }
00444       case OP_SETGLOBAL: {
00445         TValue g;
00446         sethvalue(L, &g, cl->env);
00447         lua_assert(ttisstring(KBx(i)));
00448         Protect(luaV_settable(L, &g, KBx(i), ra));
00449         continue;
00450       }
00451       case OP_SETUPVAL: {
00452         UpVal *uv = cl->upvals[GETARG_B(i)];
00453         setobj(L, uv->v, ra);
00454         luaC_barrier(L, uv, ra);
00455         continue;
00456       }
00457       case OP_SETTABLE: {
00458         Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
00459         continue;
00460       }
00461       case OP_NEWTABLE: {
00462         int b = GETARG_B(i);
00463         int c = GETARG_C(i);
00464         sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
00465         Protect(luaC_checkGC(L));
00466         continue;
00467       }
00468       case OP_SELF: {
00469         StkId rb = RB(i);
00470         setobjs2s(L, ra+1, rb);
00471         Protect(luaV_gettable(L, rb, RKC(i), ra));
00472         continue;
00473       }
00474       case OP_ADD: {
00475         arith_op(luai_numadd, TM_ADD);
00476         continue;
00477       }
00478       case OP_SUB: {
00479         arith_op(luai_numsub, TM_SUB);
00480         continue;
00481       }
00482       case OP_MUL: {
00483         arith_op(luai_nummul, TM_MUL);
00484         continue;
00485       }
00486       case OP_DIV: {
00487         arith_op(luai_numdiv, TM_DIV);
00488         continue;
00489       }
00490       case OP_MOD: {
00491         arith_op(luai_nummod, TM_MOD);
00492         continue;
00493       }
00494       case OP_POW: {
00495         arith_op(luai_numpow, TM_POW);
00496         continue;
00497       }
00498       case OP_UNM: {
00499         TValue *rb = RB(i);
00500         if (ttisnumber(rb)) {
00501           lua_Number nb = nvalue(rb);
00502           setnvalue(ra, luai_numunm(nb));
00503         }
00504         else {
00505           Protect(Arith(L, ra, rb, rb, TM_UNM));
00506         }
00507         continue;
00508       }
00509       case OP_NOT: {
00510         int res = l_isfalse(RB(i));  /* next assignment may change this value */
00511         setbvalue(ra, res);
00512         continue;
00513       }
00514       case OP_LEN: {
00515         const TValue *rb = RB(i);
00516         switch (ttype(rb)) {
00517           case LUA_TTABLE: {
00518             setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
00519             break;
00520           }
00521           case LUA_TSTRING: {
00522             setnvalue(ra, cast_num(tsvalue(rb)->len));
00523             break;
00524           }
00525           default: {  /* try metamethod */
00526             Protect(
00527               if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
00528                 luaG_typeerror(L, rb, "get length of");
00529             )
00530           }
00531         }
00532         continue;
00533       }
00534       case OP_CONCAT: {
00535         int b = GETARG_B(i);
00536         int c = GETARG_C(i);
00537         Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
00538         setobjs2s(L, RA(i), base+b);
00539         continue;
00540       }
00541       case OP_JMP: {
00542         dojump(L, pc, GETARG_sBx(i));
00543         continue;
00544       }
00545       case OP_EQ: {
00546         TValue *rb = RKB(i);
00547         TValue *rc = RKC(i);
00548         Protect(
00549           if (equalobj(L, rb, rc) == GETARG_A(i))
00550             dojump(L, pc, GETARG_sBx(*pc));
00551         )
00552         pc++;
00553         continue;
00554       }
00555       case OP_LT: {
00556         Protect(
00557           if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
00558             dojump(L, pc, GETARG_sBx(*pc));
00559         )
00560         pc++;
00561         continue;
00562       }
00563       case OP_LE: {
00564         Protect(
00565           if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
00566             dojump(L, pc, GETARG_sBx(*pc));
00567         )
00568         pc++;
00569         continue;
00570       }
00571       case OP_TEST: {
00572         if (l_isfalse(ra) != GETARG_C(i))
00573           dojump(L, pc, GETARG_sBx(*pc));
00574         pc++;
00575         continue;
00576       }
00577       case OP_TESTSET: {
00578         TValue *rb = RB(i);
00579         if (l_isfalse(rb) != GETARG_C(i)) {
00580           setobjs2s(L, ra, rb);
00581           dojump(L, pc, GETARG_sBx(*pc));
00582         }
00583         pc++;
00584         continue;
00585       }
00586       case OP_CALL: {
00587         int b = GETARG_B(i);
00588         int nresults = GETARG_C(i) - 1;
00589         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
00590         L->savedpc = pc;
00591         switch (luaD_precall(L, ra, nresults)) {
00592           case PCRLUA: {
00593             nexeccalls++;
00594             goto reentry;  /* restart luaV_execute over new Lua function */
00595           }
00596           case PCRC: {
00597             /* it was a C function (`precall' called it); adjust results */
00598             if (nresults >= 0) L->top = L->ci->top;
00599             base = L->base;
00600             continue;
00601           }
00602           default: {
00603             return;  /* yield */
00604           }
00605         }
00606       }
00607       case OP_TAILCALL: {
00608         int b = GETARG_B(i);
00609         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
00610         L->savedpc = pc;
00611         lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
00612         switch (luaD_precall(L, ra, LUA_MULTRET)) {
00613           case PCRLUA: {
00614             /* tail call: put new frame in place of previous one */
00615             CallInfo *ci = L->ci - 1;  /* previous frame */
00616             int aux;
00617             StkId func = ci->func;
00618             StkId pfunc = (ci+1)->func;  /* previous function index */
00619             if (L->openupval) luaF_close(L, ci->base);
00620             L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
00621             for (aux = 0; pfunc+aux < L->top; aux++)  /* move frame down */
00622               setobjs2s(L, func+aux, pfunc+aux);
00623             ci->top = L->top = func+aux;  /* correct top */
00624             lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
00625             ci->savedpc = L->savedpc;
00626             ci->tailcalls++;  /* one more call lost */
00627             L->ci--;  /* remove new frame */
00628             goto reentry;
00629           }
00630           case PCRC: {  /* it was a C function (`precall' called it) */
00631             base = L->base;
00632             continue;
00633           }
00634           default: {
00635             return;  /* yield */
00636           }
00637         }
00638       }
00639       case OP_RETURN: {
00640         int b = GETARG_B(i);
00641         if (b != 0) L->top = ra+b-1;
00642         if (L->openupval) luaF_close(L, base);
00643         L->savedpc = pc;
00644         b = luaD_poscall(L, ra);
00645         if (--nexeccalls == 0)  /* was previous function running `here'? */
00646           return;  /* no: return */
00647         else {  /* yes: continue its execution */
00648           if (b) L->top = L->ci->top;
00649           lua_assert(isLua(L->ci));
00650           lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
00651           goto reentry;
00652         }
00653       }
00654       case OP_FORLOOP: {
00655         lua_Number step = nvalue(ra+2);
00656         lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
00657         lua_Number limit = nvalue(ra+1);
00658         if (luai_numlt(0, step) ? luai_numle(idx, limit)
00659                                 : luai_numle(limit, idx)) {
00660           dojump(L, pc, GETARG_sBx(i));  /* jump back */
00661           setnvalue(ra, idx);  /* update internal index... */
00662           setnvalue(ra+3, idx);  /* ...and external index */
00663         }
00664         continue;
00665       }
00666       case OP_FORPREP: {
00667         const TValue *init = ra;
00668         const TValue *plimit = ra+1;
00669         const TValue *pstep = ra+2;
00670         L->savedpc = pc;  /* next steps may throw errors */
00671         if (!tonumber(init, ra))
00672           luaG_runerror(L, LUA_QL("for") " initial value must be a number");
00673         else if (!tonumber(plimit, ra+1))
00674           luaG_runerror(L, LUA_QL("for") " limit must be a number");
00675         else if (!tonumber(pstep, ra+2))
00676           luaG_runerror(L, LUA_QL("for") " step must be a number");
00677         setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
00678         dojump(L, pc, GETARG_sBx(i));
00679         continue;
00680       }
00681       case OP_TFORLOOP: {
00682         StkId cb = ra + 3;  /* call base */
00683         setobjs2s(L, cb+2, ra+2);
00684         setobjs2s(L, cb+1, ra+1);
00685         setobjs2s(L, cb, ra);
00686         L->top = cb+3;  /* func. + 2 args (state and index) */
00687         Protect(luaD_call(L, cb, GETARG_C(i)));
00688         L->top = L->ci->top;
00689         cb = RA(i) + 3;  /* previous call may change the stack */
00690         if (!ttisnil(cb)) {  /* continue loop? */
00691           setobjs2s(L, cb-1, cb);  /* save control variable */
00692           dojump(L, pc, GETARG_sBx(*pc));  /* jump back */
00693         }
00694         pc++;
00695         continue;
00696       }
00697       case OP_SETLIST: {
00698         int n = GETARG_B(i);
00699         int c = GETARG_C(i);
00700         int last;
00701         Table *h;
00702         if (n == 0) {
00703           n = cast_int(L->top - ra) - 1;
00704           L->top = L->ci->top;
00705         }
00706         if (c == 0) c = cast_int(*pc++);
00707         runtime_check(L, ttistable(ra));
00708         h = hvalue(ra);
00709         last = ((c-1)*LFIELDS_PER_FLUSH) + n;
00710         if (last > h->sizearray)  /* needs more space? */
00711           luaH_resizearray(L, h, last);  /* pre-alloc it at once */
00712         for (; n > 0; n--) {
00713           TValue *val = ra+n;
00714           setobj2t(L, luaH_setnum(L, h, last--), val);
00715           luaC_barriert(L, h, val);
00716         }
00717         continue;
00718       }
00719       case OP_CLOSE: {
00720         luaF_close(L, ra);
00721         continue;
00722       }
00723       case OP_CLOSURE: {
00724         Proto *p;
00725         Closure *ncl;
00726         int nup, j;
00727         p = cl->p->p[GETARG_Bx(i)];
00728         nup = p->nups;
00729         ncl = luaF_newLclosure(L, nup, cl->env);
00730         ncl->l.p = p;
00731         for (j=0; j<nup; j++, pc++) {
00732           if (GET_OPCODE(*pc) == OP_GETUPVAL)
00733             ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
00734           else {
00735             lua_assert(GET_OPCODE(*pc) == OP_MOVE);
00736             ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
00737           }
00738         }
00739         setclvalue(L, ra, ncl);
00740         Protect(luaC_checkGC(L));
00741         continue;
00742       }
00743       case OP_VARARG: {
00744         int b = GETARG_B(i) - 1;
00745         int j;
00746         CallInfo *ci = L->ci;
00747         int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
00748         if (b == LUA_MULTRET) {
00749           Protect(luaD_checkstack(L, n));
00750           ra = RA(i);  /* previous call may change the stack */
00751           b = n;
00752           L->top = ra + n;
00753         }
00754         for (j = 0; j < b; j++) {
00755           if (j < n) {
00756             setobjs2s(L, ra + j, ci->base - n + j);
00757           }
00758           else {
00759             setnilvalue(ra + j);
00760           }
00761         }
00762         continue;
00763       }
00764     }
00765   }
00766 }

Generated by  doxygen 1.6.2