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