source: trunk/src/lua/lvm.c @ 2540

Last change on this file since 2540 was 2540, checked in by sam, 10 years ago

core: import pristine lua 5.2.1 in the engine code.

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