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