lua/lvm.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lvm.c,v 2.147 2011/12/07 14:43:55 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), tsvalue(obj)->len, &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     int l = lua_number2str(s, n);
00054     setsvalue2s(L, obj, luaS_newlstr(L, s, l));
00055     return 1;
00056   }
00057 }
00058 
00059 
00060 static void traceexec (lua_State *L) {
00061   CallInfo *ci = L->ci;
00062   lu_byte mask = L->hookmask;
00063   if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
00064     resethookcount(L);
00065     luaD_hook(L, LUA_HOOKCOUNT, -1);
00066   }
00067   if (mask & LUA_MASKLINE) {
00068     Proto *p = ci_func(ci)->p;
00069     int npc = pcRel(ci->u.l.savedpc, p);
00070     int newline = getfuncline(p, npc);
00071     if (npc == 0 ||  /* call linehook when enter a new function, */
00072         ci->u.l.savedpc <= L->oldpc ||  /* when jump back (loop), or when */
00073         newline != getfuncline(p, pcRel(L->oldpc, p)))  /* enter a new line */
00074       luaD_hook(L, LUA_HOOKLINE, newline);
00075   }
00076   L->oldpc = ci->u.l.savedpc;
00077   if (L->status == LUA_YIELD) {  /* did hook yield? */
00078     ci->u.l.savedpc--;  /* undo increment (resume will increment it again) */
00079     luaD_throw(L, LUA_YIELD);
00080   }
00081 }
00082 
00083 
00084 static void callTM (lua_State *L, const TValue *f, const TValue *p1,
00085                     const TValue *p2, TValue *p3, int hasres) {
00086   ptrdiff_t result = savestack(L, p3);
00087   setobj2s(L, L->top++, f);  /* push function */
00088   setobj2s(L, L->top++, p1);  /* 1st argument */
00089   setobj2s(L, L->top++, p2);  /* 2nd argument */
00090   if (!hasres)  /* no result? 'p3' is third argument */
00091     setobj2s(L, L->top++, p3);  /* 3rd argument */
00092   luaD_checkstack(L, 0);
00093   /* metamethod may yield only when called from Lua code */
00094   luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
00095   if (hasres) {  /* if has result, move it to its place */
00096     p3 = restorestack(L, result);
00097     setobjs2s(L, p3, --L->top);
00098   }
00099 }
00100 
00101 
00102 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
00103   int loop;
00104   for (loop = 0; loop < MAXTAGLOOP; loop++) {
00105     const TValue *tm;
00106     if (ttistable(t)) {  /* `t' is a table? */
00107       Table *h = hvalue(t);
00108       const TValue *res = luaH_get(h, key); /* do a primitive get */
00109       if (!ttisnil(res) ||  /* result is not nil? */
00110           (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
00111         setobj2s(L, val, res);
00112         return;
00113       }
00114       /* else will try the tag method */
00115     }
00116     else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
00117       luaG_typeerror(L, t, "index");
00118     if (ttisfunction(tm)) {
00119       callTM(L, tm, t, key, val, 1);
00120       return;
00121     }
00122     t = tm;  /* else repeat with 'tm' */
00123   }
00124   luaG_runerror(L, "loop in gettable");
00125 }
00126 
00127 
00128 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
00129   int loop;
00130   for (loop = 0; loop < MAXTAGLOOP; loop++) {
00131     const TValue *tm;
00132     if (ttistable(t)) {  /* `t' is a table? */
00133       Table *h = hvalue(t);
00134       TValue *oldval = cast(TValue *, luaH_get(h, key));
00135       /* if previous value is not nil, there must be a previous entry
00136          in the table; moreover, a metamethod has no relevance */
00137       if (!ttisnil(oldval) ||
00138          /* previous value is nil; must check the metamethod */
00139          ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL &&
00140          /* no metamethod; is there a previous entry in the table? */
00141          (oldval != luaO_nilobject ||
00142          /* no previous entry; must create one. (The next test is
00143             always true; we only need the assignment.) */
00144          (oldval = luaH_newkey(L, h, key), 1)))) {
00145         /* no metamethod and (now) there is an entry with given key */
00146         setobj2t(L, oldval, val);  /* assign new value to that entry */
00147         invalidateTMcache(h);
00148         luaC_barrierback(L, obj2gco(h), val);
00149         return;
00150       }
00151       /* else will try the metamethod */
00152     }
00153     else  /* not a table; check metamethod */
00154       if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
00155         luaG_typeerror(L, t, "index");
00156     /* there is a metamethod */
00157     if (ttisfunction(tm)) {
00158       callTM(L, tm, t, key, val, 0);
00159       return;
00160     }
00161     t = tm;  /* else repeat with 'tm' */
00162   }
00163   luaG_runerror(L, "loop in settable");
00164 }
00165 
00166 
00167 static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
00168                        StkId res, TMS event) {
00169   const TValue *tm = luaT_gettmbyobj(L, p1, event);  /* try first operand */
00170   if (ttisnil(tm))
00171     tm = luaT_gettmbyobj(L, p2, event);  /* try second operand */
00172   if (ttisnil(tm)) return 0;
00173   callTM(L, tm, p1, p2, res, 1);
00174   return 1;
00175 }
00176 
00177 
00178 static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2,
00179                                   TMS event) {
00180   const TValue *tm1 = fasttm(L, mt1, event);
00181   const TValue *tm2;
00182   if (tm1 == NULL) return NULL;  /* no metamethod */
00183   if (mt1 == mt2) return tm1;  /* same metatables => same metamethods */
00184   tm2 = fasttm(L, mt2, event);
00185   if (tm2 == NULL) return NULL;  /* no metamethod */
00186   if (luaV_rawequalobj(tm1, tm2))  /* same metamethods? */
00187     return tm1;
00188   return NULL;
00189 }
00190 
00191 
00192 static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
00193                          TMS event) {
00194   if (!call_binTM(L, p1, p2, L->top, event))
00195     return -1;  /* no metamethod */
00196   else
00197     return !l_isfalse(L->top);
00198 }
00199 
00200 
00201 static int l_strcmp (const TString *ls, const TString *rs) {
00202   const char *l = getstr(ls);
00203   size_t ll = ls->tsv.len;
00204   const char *r = getstr(rs);
00205   size_t lr = rs->tsv.len;
00206   for (;;) {
00207     int temp = strcoll(l, r);
00208     if (temp != 0) return temp;
00209     else {  /* strings are equal up to a `\0' */
00210       size_t len = strlen(l);  /* index of first `\0' in both strings */
00211       if (len == lr)  /* r is finished? */
00212         return (len == ll) ? 0 : 1;
00213       else if (len == ll)  /* l is finished? */
00214         return -1;  /* l is smaller than r (because r is not finished) */
00215       /* both strings longer than `len'; go on comparing (after the `\0') */
00216       len++;
00217       l += len; ll -= len; r += len; lr -= len;
00218     }
00219   }
00220 }
00221 
00222 
00223 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
00224   int res;
00225   if (ttisnumber(l) && ttisnumber(r))
00226     return luai_numlt(L, nvalue(l), nvalue(r));
00227   else if (ttisstring(l) && ttisstring(r))
00228     return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
00229   else if ((res = call_orderTM(L, l, r, TM_LT)) < 0)
00230     luaG_ordererror(L, l, r);
00231   return res;
00232 }
00233 
00234 
00235 int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
00236   int res;
00237   if (ttisnumber(l) && ttisnumber(r))
00238     return luai_numle(L, nvalue(l), nvalue(r));
00239   else if (ttisstring(l) && ttisstring(r))
00240     return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
00241   else if ((res = call_orderTM(L, l, r, TM_LE)) >= 0)  /* first try `le' */
00242     return res;
00243   else if ((res = call_orderTM(L, r, l, TM_LT)) < 0)  /* else try `lt' */
00244     luaG_ordererror(L, l, r);
00245   return !res;
00246 }
00247 
00248 
00249 /*
00250 ** equality of Lua values. L == NULL means raw equality (no metamethods)
00251 */
00252 int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) {
00253   const TValue *tm;
00254   lua_assert(ttisequal(t1, t2));
00255   switch (ttype(t1)) {
00256     case LUA_TNIL: return 1;
00257     case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
00258     case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2);  /* true must be 1 !! */
00259     case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
00260     case LUA_TLCF: return fvalue(t1) == fvalue(t2);
00261     case LUA_TSTRING: return eqstr(rawtsvalue(t1), rawtsvalue(t2));
00262     case LUA_TUSERDATA: {
00263       if (uvalue(t1) == uvalue(t2)) return 1;
00264       else if (L == NULL) return 0;
00265       tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ);
00266       break;  /* will try TM */
00267     }
00268     case LUA_TTABLE: {
00269       if (hvalue(t1) == hvalue(t2)) return 1;
00270       else if (L == NULL) return 0;
00271       tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
00272       break;  /* will try TM */
00273     }
00274     default:
00275       lua_assert(iscollectable(t1));
00276       return gcvalue(t1) == gcvalue(t2);
00277   }
00278   if (tm == NULL) return 0;  /* no TM? */
00279   callTM(L, tm, t1, t2, L->top, 1);  /* call TM */
00280   return !l_isfalse(L->top);
00281 }
00282 
00283 
00284 void luaV_concat (lua_State *L, int total) {
00285   lua_assert(total >= 2);
00286   do {
00287     StkId top = L->top;
00288     int n = 2;  /* number of elements handled in this pass (at least 2) */
00289     if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
00290       if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
00291         luaG_concaterror(L, top-2, top-1);
00292     }
00293     else if (tsvalue(top-1)->len == 0)  /* second operand is empty? */
00294       (void)tostring(L, top - 2);  /* result is first operand */
00295     else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
00296       setsvalue2s(L, top-2, rawtsvalue(top-1));  /* result is second op. */
00297     }
00298     else {
00299       /* at least two non-empty string values; get as many as possible */
00300       size_t tl = tsvalue(top-1)->len;
00301       char *buffer;
00302       int i;
00303       /* collect total length */
00304       for (i = 1; i < total && tostring(L, top-i-1); i++) {
00305         size_t l = tsvalue(top-i-1)->len;
00306         if (l >= (MAX_SIZET/sizeof(char)) - tl)
00307           luaG_runerror(L, "string length overflow");
00308         tl += l;
00309       }
00310       buffer = luaZ_openspace(L, &G(L)->buff, tl);
00311       tl = 0;
00312       n = i;
00313       do {  /* concat all strings */
00314         size_t l = tsvalue(top-i)->len;
00315         memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
00316         tl += l;
00317       } while (--i > 0);
00318       setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
00319     }
00320     total -= n-1;  /* got 'n' strings to create 1 new */
00321     L->top -= n-1;  /* popped 'n' strings and pushed one */
00322   } while (total > 1);  /* repeat until only 1 result left */
00323 }
00324 
00325 
00326 void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
00327   const TValue *tm;
00328   switch (ttypenv(rb)) {
00329     case LUA_TTABLE: {
00330       Table *h = hvalue(rb);
00331       tm = fasttm(L, h->metatable, TM_LEN);
00332       if (tm) break;  /* metamethod? break switch to call it */
00333       setnvalue(ra, cast_num(luaH_getn(h)));  /* else primitive len */
00334       return;
00335     }
00336     case LUA_TSTRING: {
00337       setnvalue(ra, cast_num(tsvalue(rb)->len));
00338       return;
00339     }
00340     default: {  /* try metamethod */
00341       tm = luaT_gettmbyobj(L, rb, TM_LEN);
00342       if (ttisnil(tm))  /* no metamethod? */
00343         luaG_typeerror(L, rb, "get length of");
00344       break;
00345     }
00346   }
00347   callTM(L, tm, rb, rb, ra, 1);
00348 }
00349 
00350 
00351 void luaV_arith (lua_State *L, StkId ra, const TValue *rb,
00352                  const TValue *rc, TMS op) {
00353   TValue tempb, tempc;
00354   const TValue *b, *c;
00355   if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
00356       (c = luaV_tonumber(rc, &tempc)) != NULL) {
00357     lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c));
00358     setnvalue(ra, res);
00359   }
00360   else if (!call_binTM(L, rb, rc, ra, op))
00361     luaG_aritherror(L, rb, rc);
00362 }
00363 
00364 
00365 /*
00366 ** check whether cached closure in prototype 'p' may be reused, that is,
00367 ** whether there is a cached closure with the same upvalues needed by
00368 ** new closure to be created.
00369 */
00370 static Closure *getcached (Proto *p, UpVal **encup, StkId base) {
00371   Closure *c = p->cache;
00372   if (c != NULL) {  /* is there a cached closure? */
00373     int nup = p->sizeupvalues;
00374     Upvaldesc *uv = p->upvalues;
00375     int i;
00376     for (i = 0; i < nup; i++) {  /* check whether it has right upvalues */
00377       TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v;
00378       if (c->l.upvals[i]->v != v)
00379         return NULL;  /* wrong upvalue; cannot reuse closure */
00380     }
00381   }
00382   return c;  /* return cached closure (or NULL if no cached closure) */
00383 }
00384 
00385 
00386 /*
00387 ** create a new Lua closure, push it in the stack, and initialize
00388 ** its upvalues. Note that the call to 'luaC_barrierproto' must come
00389 ** before the assignment to 'p->cache', as the function needs the
00390 ** original value of that field.
00391 */
00392 static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base,
00393                          StkId ra) {
00394   int nup = p->sizeupvalues;
00395   Upvaldesc *uv = p->upvalues;
00396   int i;
00397   Closure *ncl = luaF_newLclosure(L, p);
00398   setclLvalue(L, ra, ncl);  /* anchor new closure in stack */
00399   for (i = 0; i < nup; i++) {  /* fill in its upvalues */
00400     if (uv[i].instack)  /* upvalue refers to local variable? */
00401       ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
00402     else  /* get upvalue from enclosing function */
00403       ncl->l.upvals[i] = encup[uv[i].idx];
00404   }
00405   luaC_barrierproto(L, p, ncl);
00406   p->cache = ncl;  /* save it on cache for reuse */
00407 }
00408 
00409 
00410 /*
00411 ** finish execution of an opcode interrupted by an yield
00412 */
00413 void luaV_finishOp (lua_State *L) {
00414   CallInfo *ci = L->ci;
00415   StkId base = ci->u.l.base;
00416   Instruction inst = *(ci->u.l.savedpc - 1);  /* interrupted instruction */
00417   OpCode op = GET_OPCODE(inst);
00418   switch (op) {  /* finish its execution */
00419     case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
00420     case OP_MOD: case OP_POW: case OP_UNM: case OP_LEN:
00421     case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
00422       setobjs2s(L, base + GETARG_A(inst), --L->top);
00423       break;
00424     }
00425     case OP_LE: case OP_LT: case OP_EQ: {
00426       int res = !l_isfalse(L->top - 1);
00427       L->top--;
00428       /* metamethod should not be called when operand is K */
00429       lua_assert(!ISK(GETARG_B(inst)));
00430       if (op == OP_LE &&  /* "<=" using "<" instead? */
00431           ttisnil(luaT_gettmbyobj(L, base + GETARG_B(inst), TM_LE)))
00432         res = !res;  /* invert result */
00433       lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
00434       if (res != GETARG_A(inst))  /* condition failed? */
00435         ci->u.l.savedpc++;  /* skip jump instruction */
00436       break;
00437     }
00438     case OP_CONCAT: {
00439       StkId top = L->top - 1;  /* top when 'call_binTM' was called */
00440       int b = GETARG_B(inst);      /* first element to concatenate */
00441       int total = cast_int(top - 1 - (base + b));  /* yet to concatenate */
00442       setobj2s(L, top - 2, top);  /* put TM result in proper position */
00443       if (total > 1) {  /* are there elements to concat? */
00444         L->top = top - 1;  /* top is one after last element (at top-2) */
00445         luaV_concat(L, total);  /* concat them (may yield again) */
00446       }
00447       /* move final result to final position */
00448       setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1);
00449       L->top = ci->top;  /* restore top */
00450       break;
00451     }
00452     case OP_TFORCALL: {
00453       lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP);
00454       L->top = ci->top;  /* correct top */
00455       break;
00456     }
00457     case OP_CALL: {
00458       if (GETARG_C(inst) - 1 >= 0)  /* nresults >= 0? */
00459         L->top = ci->top;  /* adjust results */
00460       break;
00461     }
00462     case OP_TAILCALL: case OP_SETTABUP:  case OP_SETTABLE:
00463       break;
00464     default: lua_assert(0);
00465   }
00466 }
00467 
00468 
00469 
00470 /*
00471 ** some macros for common tasks in `luaV_execute'
00472 */
00473 
00474 #if !defined luai_runtimecheck
00475 #define luai_runtimecheck(L, c)     /* void */
00476 #endif
00477 
00478 
00479 #define RA(i)   (base+GETARG_A(i))
00480 /* to be used after possible stack reallocation */
00481 #define RB(i)   check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
00482 #define RC(i)   check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
00483 #define RKB(i)  check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
00484     ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
00485 #define RKC(i)  check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
00486     ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
00487 #define KBx(i)  \
00488   (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
00489 
00490 
00491 /* execute a jump instruction */
00492 #define dojump(ci,i,e) \
00493   { int a = GETARG_A(i); \
00494     if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
00495     ci->u.l.savedpc += GETARG_sBx(i) + e; }
00496 
00497 /* for test instructions, execute the jump instruction that follows it */
00498 #define donextjump(ci)  { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
00499 
00500 
00501 #define Protect(x)  { {x;}; base = ci->u.l.base; }
00502 
00503 #define checkGC(L,c)    Protect(luaC_condGC(L, c); luai_threadyield(L);)
00504 
00505 
00506 #define arith_op(op,tm) { \
00507         TValue *rb = RKB(i); \
00508         TValue *rc = RKC(i); \
00509         if (ttisnumber(rb) && ttisnumber(rc)) { \
00510           lua_Number nb = nvalue(rb), nc = nvalue(rc); \
00511           setnvalue(ra, op(L, nb, nc)); \
00512         } \
00513         else { Protect(luaV_arith(L, ra, rb, rc, tm)); } }
00514 
00515 
00516 #define vmdispatch(o)   switch(o)
00517 #define vmcase(l,b) case l: {b}  break;
00518 #define vmcasenb(l,b)   case l: {b}     /* nb = no break */
00519 
00520 void luaV_execute (lua_State *L) {
00521   CallInfo *ci = L->ci;
00522   LClosure *cl;
00523   TValue *k;
00524   StkId base;
00525  newframe:  /* reentry point when frame changes (call/return) */
00526   lua_assert(ci == L->ci);
00527   cl = clLvalue(ci->func);
00528   k = cl->p->k;
00529   base = ci->u.l.base;
00530   /* main loop of interpreter */
00531   for (;;) {
00532     Instruction i = *(ci->u.l.savedpc++);
00533     StkId ra;
00534     if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
00535         (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
00536       Protect(traceexec(L));
00537     }
00538     /* WARNING: several calls may realloc the stack and invalidate `ra' */
00539     ra = RA(i);
00540     lua_assert(base == ci->u.l.base);
00541     lua_assert(base <= L->top && L->top < L->stack + L->stacksize);
00542     vmdispatch (GET_OPCODE(i)) {
00543       vmcase(OP_MOVE,
00544         setobjs2s(L, ra, RB(i));
00545       )
00546       vmcase(OP_LOADK,
00547         TValue *rb = k + GETARG_Bx(i);
00548         setobj2s(L, ra, rb);
00549       )
00550       vmcase(OP_LOADKX,
00551         TValue *rb;
00552         lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
00553         rb = k + GETARG_Ax(*ci->u.l.savedpc++);
00554         setobj2s(L, ra, rb);
00555       )
00556       vmcase(OP_LOADBOOL,
00557         setbvalue(ra, GETARG_B(i));
00558         if (GETARG_C(i)) ci->u.l.savedpc++;  /* skip next instruction (if C) */
00559       )
00560       vmcase(OP_LOADNIL,
00561         int b = GETARG_B(i);
00562         do {
00563           setnilvalue(ra++);
00564         } while (b--);
00565       )
00566       vmcase(OP_GETUPVAL,
00567         int b = GETARG_B(i);
00568         setobj2s(L, ra, cl->upvals[b]->v);
00569       )
00570       vmcase(OP_GETTABUP,
00571         int b = GETARG_B(i);
00572         Protect(luaV_gettable(L, cl->upvals[b]->v, RKC(i), ra));
00573       )
00574       vmcase(OP_GETTABLE,
00575         Protect(luaV_gettable(L, RB(i), RKC(i), ra));
00576       )
00577       vmcase(OP_SETTABUP,
00578         int a = GETARG_A(i);
00579         Protect(luaV_settable(L, cl->upvals[a]->v, RKB(i), RKC(i)));
00580       )
00581       vmcase(OP_SETUPVAL,
00582         UpVal *uv = cl->upvals[GETARG_B(i)];
00583         setobj(L, uv->v, ra);
00584         luaC_barrier(L, uv, ra);
00585       )
00586       vmcase(OP_SETTABLE,
00587         Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
00588       )
00589       vmcase(OP_NEWTABLE,
00590         int b = GETARG_B(i);
00591         int c = GETARG_C(i);
00592         Table *t = luaH_new(L);
00593         sethvalue(L, ra, t);
00594         if (b != 0 || c != 0)
00595           luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
00596         checkGC(L,
00597           L->top = ra + 1;  /* limit of live values */
00598           luaC_step(L);
00599           L->top = ci->top;  /* restore top */
00600         )
00601       )
00602       vmcase(OP_SELF,
00603         StkId rb = RB(i);
00604         setobjs2s(L, ra+1, rb);
00605         Protect(luaV_gettable(L, rb, RKC(i), ra));
00606       )
00607       vmcase(OP_ADD,
00608         arith_op(luai_numadd, TM_ADD);
00609       )
00610       vmcase(OP_SUB,
00611         arith_op(luai_numsub, TM_SUB);
00612       )
00613       vmcase(OP_MUL,
00614         arith_op(luai_nummul, TM_MUL);
00615       )
00616       vmcase(OP_DIV,
00617         arith_op(luai_numdiv, TM_DIV);
00618       )
00619       vmcase(OP_MOD,
00620         arith_op(luai_nummod, TM_MOD);
00621       )
00622       vmcase(OP_POW,
00623         arith_op(luai_numpow, TM_POW);
00624       )
00625       vmcase(OP_UNM,
00626         TValue *rb = RB(i);
00627         if (ttisnumber(rb)) {
00628           lua_Number nb = nvalue(rb);
00629           setnvalue(ra, luai_numunm(L, nb));
00630         }
00631         else {
00632           Protect(luaV_arith(L, ra, rb, rb, TM_UNM));
00633         }
00634       )
00635       vmcase(OP_NOT,
00636         TValue *rb = RB(i);
00637         int res = l_isfalse(rb);  /* next assignment may change this value */
00638         setbvalue(ra, res);
00639       )
00640       vmcase(OP_LEN,
00641         Protect(luaV_objlen(L, ra, RB(i)));
00642       )
00643       vmcase(OP_CONCAT,
00644         int b = GETARG_B(i);
00645         int c = GETARG_C(i);
00646         StkId rb;
00647         L->top = base + c + 1;  /* mark the end of concat operands */
00648         Protect(luaV_concat(L, c - b + 1));
00649         ra = RA(i);  /* 'luav_concat' may invoke TMs and move the stack */
00650         rb = b + base;
00651         setobjs2s(L, ra, rb);
00652         checkGC(L,
00653           L->top = (ra >= rb ? ra + 1 : rb);  /* limit of live values */
00654           luaC_step(L);
00655         )
00656         L->top = ci->top;  /* restore top */
00657       )
00658       vmcase(OP_JMP,
00659         dojump(ci, i, 0);
00660       )
00661       vmcase(OP_EQ,
00662         TValue *rb = RKB(i);
00663         TValue *rc = RKC(i);
00664         Protect(
00665           if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i))
00666             ci->u.l.savedpc++;
00667           else
00668             donextjump(ci);
00669         )
00670       )
00671       vmcase(OP_LT,
00672         Protect(
00673           if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
00674             ci->u.l.savedpc++;
00675           else
00676             donextjump(ci);
00677         )
00678       )
00679       vmcase(OP_LE,
00680         Protect(
00681           if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
00682             ci->u.l.savedpc++;
00683           else
00684             donextjump(ci);
00685         )
00686       )
00687       vmcase(OP_TEST,
00688         if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
00689             ci->u.l.savedpc++;
00690           else
00691           donextjump(ci);
00692       )
00693       vmcase(OP_TESTSET,
00694         TValue *rb = RB(i);
00695         if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
00696           ci->u.l.savedpc++;
00697         else {
00698           setobjs2s(L, ra, rb);
00699           donextjump(ci);
00700         }
00701       )
00702       vmcase(OP_CALL,
00703         int b = GETARG_B(i);
00704         int nresults = GETARG_C(i) - 1;
00705         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
00706         if (luaD_precall(L, ra, nresults)) {  /* C function? */
00707           if (nresults >= 0) L->top = ci->top;  /* adjust results */
00708           base = ci->u.l.base;
00709         }
00710         else {  /* Lua function */
00711           ci = L->ci;
00712           ci->callstatus |= CIST_REENTRY;
00713           goto newframe;  /* restart luaV_execute over new Lua function */
00714         }
00715       )
00716       vmcase(OP_TAILCALL,
00717         int b = GETARG_B(i);
00718         if (b != 0) L->top = ra+b;  /* else previous instruction set top */
00719         lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
00720         if (luaD_precall(L, ra, LUA_MULTRET))  /* C function? */
00721           base = ci->u.l.base;
00722         else {
00723           /* tail call: put called frame (n) in place of caller one (o) */
00724           CallInfo *nci = L->ci;  /* called frame */
00725           CallInfo *oci = nci->previous;  /* caller frame */
00726           StkId nfunc = nci->func;  /* called function */
00727           StkId ofunc = oci->func;  /* caller function */
00728           /* last stack slot filled by 'precall' */
00729           StkId lim = nci->u.l.base + getproto(nfunc)->numparams;
00730           int aux;
00731           /* close all upvalues from previous call */
00732           if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base);
00733           /* move new frame into old one */
00734           for (aux = 0; nfunc + aux < lim; aux++)
00735             setobjs2s(L, ofunc + aux, nfunc + aux);
00736           oci->u.l.base = ofunc + (nci->u.l.base - nfunc);  /* correct base */
00737           oci->top = L->top = ofunc + (L->top - nfunc);  /* correct top */
00738           oci->u.l.savedpc = nci->u.l.savedpc;
00739           oci->callstatus |= CIST_TAIL;  /* function was tail called */
00740           ci = L->ci = oci;  /* remove new frame */
00741           lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize);
00742           goto newframe;  /* restart luaV_execute over new Lua function */
00743         }
00744       )
00745       vmcasenb(OP_RETURN,
00746         int b = GETARG_B(i);
00747         if (b != 0) L->top = ra+b-1;
00748         if (cl->p->sizep > 0) luaF_close(L, base);
00749         b = luaD_poscall(L, ra);
00750         if (!(ci->callstatus & CIST_REENTRY))  /* 'ci' still the called one */
00751           return;  /* external invocation: return */
00752         else {  /* invocation via reentry: continue execution */
00753           ci = L->ci;
00754           if (b) L->top = ci->top;
00755           lua_assert(isLua(ci));
00756           lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL);
00757           goto newframe;  /* restart luaV_execute over new Lua function */
00758         }
00759       )
00760       vmcase(OP_FORLOOP,
00761         lua_Number step = nvalue(ra+2);
00762         lua_Number idx = luai_numadd(L, nvalue(ra), step); /* increment index */
00763         lua_Number limit = nvalue(ra+1);
00764         if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
00765                                    : luai_numle(L, limit, idx)) {
00766           ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
00767           setnvalue(ra, idx);  /* update internal index... */
00768           setnvalue(ra+3, idx);  /* ...and external index */
00769         }
00770       )
00771       vmcase(OP_FORPREP,
00772         const TValue *init = ra;
00773         const TValue *plimit = ra+1;
00774         const TValue *pstep = ra+2;
00775         if (!tonumber(init, ra))
00776           luaG_runerror(L, LUA_QL("for") " initial value must be a number");
00777         else if (!tonumber(plimit, ra+1))
00778           luaG_runerror(L, LUA_QL("for") " limit must be a number");
00779         else if (!tonumber(pstep, ra+2))
00780           luaG_runerror(L, LUA_QL("for") " step must be a number");
00781         setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
00782         ci->u.l.savedpc += GETARG_sBx(i);
00783       )
00784       vmcasenb(OP_TFORCALL,
00785         StkId cb = ra + 3;  /* call base */
00786         setobjs2s(L, cb+2, ra+2);
00787         setobjs2s(L, cb+1, ra+1);
00788         setobjs2s(L, cb, ra);
00789         L->top = cb + 3;  /* func. + 2 args (state and index) */
00790         Protect(luaD_call(L, cb, GETARG_C(i), 1));
00791         L->top = ci->top;
00792         i = *(ci->u.l.savedpc++);  /* go to next instruction */
00793         ra = RA(i);
00794         lua_assert(GET_OPCODE(i) == OP_TFORLOOP);
00795         goto l_tforloop;
00796       )
00797       vmcase(OP_TFORLOOP,
00798         l_tforloop:
00799         if (!ttisnil(ra + 1)) {  /* continue loop? */
00800           setobjs2s(L, ra, ra + 1);  /* save control variable */
00801            ci->u.l.savedpc += GETARG_sBx(i);  /* jump back */
00802         }
00803       )
00804       vmcase(OP_SETLIST,
00805         int n = GETARG_B(i);
00806         int c = GETARG_C(i);
00807         int last;
00808         Table *h;
00809         if (n == 0) n = cast_int(L->top - ra) - 1;
00810         if (c == 0) {
00811           lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
00812           c = GETARG_Ax(*ci->u.l.savedpc++);
00813         }
00814         luai_runtimecheck(L, ttistable(ra));
00815         h = hvalue(ra);
00816         last = ((c-1)*LFIELDS_PER_FLUSH) + n;
00817         if (last > h->sizearray)  /* needs more space? */
00818           luaH_resizearray(L, h, last);  /* pre-allocate it at once */
00819         for (; n > 0; n--) {
00820           TValue *val = ra+n;
00821           luaH_setint(L, h, last--, val);
00822           luaC_barrierback(L, obj2gco(h), val);
00823         }
00824         L->top = ci->top;  /* correct top (in case of previous open call) */
00825       )
00826       vmcase(OP_CLOSURE,
00827         Proto *p = cl->p->p[GETARG_Bx(i)];
00828         Closure *ncl = getcached(p, cl->upvals, base);  /* cached closure */
00829         if (ncl == NULL)  /* no match? */
00830           pushclosure(L, p, cl->upvals, base, ra);  /* create a new one */
00831         else
00832           setclLvalue(L, ra, ncl);  /* push cashed closure */
00833         checkGC(L,
00834           L->top = ra + 1;  /* limit of live values */
00835           luaC_step(L);
00836           L->top = ci->top;  /* restore top */
00837         )
00838       )
00839       vmcase(OP_VARARG,
00840         int b = GETARG_B(i) - 1;
00841         int j;
00842         int n = cast_int(base - ci->func) - cl->p->numparams - 1;
00843         if (b < 0) {  /* B == 0? */
00844           b = n;  /* get all var. arguments */
00845           Protect(luaD_checkstack(L, n));
00846           ra = RA(i);  /* previous call may change the stack */
00847           L->top = ra + n;
00848         }
00849         for (j = 0; j < b; j++) {
00850           if (j < n) {
00851             setobjs2s(L, ra + j, base - n + j);
00852           }
00853           else {
00854             setnilvalue(ra + j);
00855           }
00856         }
00857       )
00858       vmcase(OP_EXTRAARG,
00859         lua_assert(0);
00860       )
00861     }
00862   }
00863 }
00864 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:05 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs