annotate src/lua/lvm.c @ 14:32bb141d34de

cleaning up lua stuff
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 11:29:11 -0600
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: lvm.c,v 2.63.1.3 2007/12/28 15:32:23 roberto Exp $
rlm@1 3 ** Lua virtual machine
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #include <stdio.h>
rlm@1 9 #include <stdlib.h>
rlm@1 10 #include <string.h>
rlm@1 11
rlm@1 12 #define lvm_c
rlm@1 13 #define LUA_CORE
rlm@1 14
rlm@1 15 #include "lua.h"
rlm@1 16
rlm@1 17 #include "ldebug.h"
rlm@1 18 #include "ldo.h"
rlm@1 19 #include "lfunc.h"
rlm@1 20 #include "lgc.h"
rlm@1 21 #include "lobject.h"
rlm@1 22 #include "lopcodes.h"
rlm@1 23 #include "lstate.h"
rlm@1 24 #include "lstring.h"
rlm@1 25 #include "ltable.h"
rlm@1 26 #include "ltm.h"
rlm@1 27 #include "lvm.h"
rlm@1 28
rlm@1 29
rlm@1 30
rlm@1 31 /* limit for table tag-method chains (to avoid loops) */
rlm@1 32 #define MAXTAGLOOP 100
rlm@1 33
rlm@1 34
rlm@1 35 const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
rlm@1 36 lua_Number num;
rlm@1 37 if (ttisnumber(obj)) return obj;
rlm@1 38 if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
rlm@1 39 setnvalue(n, num);
rlm@1 40 return n;
rlm@1 41 }
rlm@1 42 else
rlm@1 43 return NULL;
rlm@1 44 }
rlm@1 45
rlm@1 46
rlm@1 47 int luaV_tostring (lua_State *L, StkId obj) {
rlm@1 48 if (!ttisnumber(obj))
rlm@1 49 return 0;
rlm@1 50 else {
rlm@1 51 char s[LUAI_MAXNUMBER2STR];
rlm@1 52 lua_Number n = nvalue(obj);
rlm@1 53 lua_number2str(s, n);
rlm@1 54 setsvalue2s(L, obj, luaS_new(L, s));
rlm@1 55 return 1;
rlm@1 56 }
rlm@1 57 }
rlm@1 58
rlm@1 59
rlm@1 60 static void traceexec (lua_State *L, const Instruction *pc) {
rlm@1 61 lu_byte mask = L->hookmask;
rlm@1 62 const Instruction *oldpc = L->savedpc;
rlm@1 63 L->savedpc = pc;
rlm@1 64 if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {
rlm@1 65 resethookcount(L);
rlm@1 66 luaD_callhook(L, LUA_HOOKCOUNT, -1);
rlm@1 67 }
rlm@1 68 if (mask & LUA_MASKLINE) {
rlm@1 69 Proto *p = ci_func(L->ci)->l.p;
rlm@1 70 int npc = pcRel(pc, p);
rlm@1 71 int newline = getline(p, npc);
rlm@1 72 /* call linehook when enter a new function, when jump back (loop),
rlm@1 73 or when enter a new line */
rlm@1 74 if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))
rlm@1 75 luaD_callhook(L, LUA_HOOKLINE, newline);
rlm@1 76 }
rlm@1 77 }
rlm@1 78
rlm@1 79
rlm@1 80 static void callTMres (lua_State *L, StkId res, const TValue *f,
rlm@1 81 const TValue *p1, const TValue *p2) {
rlm@1 82 ptrdiff_t result = savestack(L, res);
rlm@1 83 setobj2s(L, L->top, f); /* push function */
rlm@1 84 setobj2s(L, L->top+1, p1); /* 1st argument */
rlm@1 85 setobj2s(L, L->top+2, p2); /* 2nd argument */
rlm@1 86 luaD_checkstack(L, 3);
rlm@1 87 L->top += 3;
rlm@1 88 luaD_call(L, L->top - 3, 1);
rlm@1 89 res = restorestack(L, result);
rlm@1 90 L->top--;
rlm@1 91 setobjs2s(L, res, L->top);
rlm@1 92 }
rlm@1 93
rlm@1 94
rlm@1 95
rlm@1 96 static void callTM (lua_State *L, const TValue *f, const TValue *p1,
rlm@1 97 const TValue *p2, const TValue *p3) {
rlm@1 98 setobj2s(L, L->top, f); /* push function */
rlm@1 99 setobj2s(L, L->top+1, p1); /* 1st argument */
rlm@1 100 setobj2s(L, L->top+2, p2); /* 2nd argument */
rlm@1 101 setobj2s(L, L->top+3, p3); /* 3th argument */
rlm@1 102 luaD_checkstack(L, 4);
rlm@1 103 L->top += 4;
rlm@1 104 luaD_call(L, L->top - 4, 0);
rlm@1 105 }
rlm@1 106
rlm@1 107
rlm@1 108 void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
rlm@1 109 int loop;
rlm@1 110 for (loop = 0; loop < MAXTAGLOOP; loop++) {
rlm@1 111 const TValue *tm;
rlm@1 112 if (ttistable(t)) { /* `t' is a table? */
rlm@1 113 Table *h = hvalue(t);
rlm@1 114 const TValue *res = luaH_get(h, key); /* do a primitive get */
rlm@1 115 if (!ttisnil(res) || /* result is no nil? */
rlm@1 116 (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
rlm@1 117 setobj2s(L, val, res);
rlm@1 118 return;
rlm@1 119 }
rlm@1 120 /* else will try the tag method */
rlm@1 121 }
rlm@1 122 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
rlm@1 123 luaG_typeerror(L, t, "index");
rlm@1 124 if (ttisfunction(tm)) {
rlm@1 125 callTMres(L, val, tm, t, key);
rlm@1 126 return;
rlm@1 127 }
rlm@1 128 t = tm; /* else repeat with `tm' */
rlm@1 129 }
rlm@1 130 luaG_runerror(L, "loop in gettable");
rlm@1 131 }
rlm@1 132
rlm@1 133
rlm@1 134 void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {
rlm@1 135 int loop;
rlm@1 136 for (loop = 0; loop < MAXTAGLOOP; loop++) {
rlm@1 137 const TValue *tm;
rlm@1 138 if (ttistable(t)) { /* `t' is a table? */
rlm@1 139 Table *h = hvalue(t);
rlm@1 140 TValue *oldval = luaH_set(L, h, key); /* do a primitive set */
rlm@1 141 if (!ttisnil(oldval) || /* result is no nil? */
rlm@1 142 (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */
rlm@1 143 setobj2t(L, oldval, val);
rlm@1 144 luaC_barriert(L, h, val);
rlm@1 145 return;
rlm@1 146 }
rlm@1 147 /* else will try the tag method */
rlm@1 148 }
rlm@1 149 else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))
rlm@1 150 luaG_typeerror(L, t, "index");
rlm@1 151 if (ttisfunction(tm)) {
rlm@1 152 callTM(L, tm, t, key, val);
rlm@1 153 return;
rlm@1 154 }
rlm@1 155 t = tm; /* else repeat with `tm' */
rlm@1 156 }
rlm@1 157 luaG_runerror(L, "loop in settable");
rlm@1 158 }
rlm@1 159
rlm@1 160
rlm@1 161 static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,
rlm@1 162 StkId res, TMS event) {
rlm@1 163 const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */
rlm@1 164 if (ttisnil(tm))
rlm@1 165 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
rlm@1 166 if (ttisnil(tm)) return 0;
rlm@1 167 callTMres(L, res, tm, p1, p2);
rlm@1 168 return 1;
rlm@1 169 }
rlm@1 170
rlm@1 171
rlm@1 172 static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,
rlm@1 173 TMS event) {
rlm@1 174 const TValue *tm1 = fasttm(L, mt1, event);
rlm@1 175 const TValue *tm2;
rlm@1 176 if (tm1 == NULL) return NULL; /* no metamethod */
rlm@1 177 if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
rlm@1 178 tm2 = fasttm(L, mt2, event);
rlm@1 179 if (tm2 == NULL) return NULL; /* no metamethod */
rlm@1 180 if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
rlm@1 181 return tm1;
rlm@1 182 return NULL;
rlm@1 183 }
rlm@1 184
rlm@1 185
rlm@1 186 static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
rlm@1 187 TMS event) {
rlm@1 188 const TValue *tm1 = luaT_gettmbyobj(L, p1, event);
rlm@1 189 const TValue *tm2;
rlm@1 190 if (ttisnil(tm1)) return -1; /* no metamethod? */
rlm@1 191 tm2 = luaT_gettmbyobj(L, p2, event);
rlm@1 192 if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */
rlm@1 193 return -1;
rlm@1 194 callTMres(L, L->top, tm1, p1, p2);
rlm@1 195 return !l_isfalse(L->top);
rlm@1 196 }
rlm@1 197
rlm@1 198
rlm@1 199 static int l_strcmp (const TString *ls, const TString *rs) {
rlm@1 200 const char *l = getstr(ls);
rlm@1 201 size_t ll = ls->tsv.len;
rlm@1 202 const char *r = getstr(rs);
rlm@1 203 size_t lr = rs->tsv.len;
rlm@1 204 for (;;) {
rlm@1 205 int temp = strcoll(l, r);
rlm@1 206 if (temp != 0) return temp;
rlm@1 207 else { /* strings are equal up to a `\0' */
rlm@1 208 size_t len = strlen(l); /* index of first `\0' in both strings */
rlm@1 209 if (len == lr) /* r is finished? */
rlm@1 210 return (len == ll) ? 0 : 1;
rlm@1 211 else if (len == ll) /* l is finished? */
rlm@1 212 return -1; /* l is smaller than r (because r is not finished) */
rlm@1 213 /* both strings longer than `len'; go on comparing (after the `\0') */
rlm@1 214 len++;
rlm@1 215 l += len; ll -= len; r += len; lr -= len;
rlm@1 216 }
rlm@1 217 }
rlm@1 218 }
rlm@1 219
rlm@1 220
rlm@1 221 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
rlm@1 222 int res;
rlm@1 223 if (ttype(l) != ttype(r))
rlm@1 224 return luaG_ordererror(L, l, r);
rlm@1 225 else if (ttisnumber(l))
rlm@1 226 return luai_numlt(nvalue(l), nvalue(r));
rlm@1 227 else if (ttisstring(l))
rlm@1 228 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
rlm@1 229 else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
rlm@1 230 return res;
rlm@1 231 return luaG_ordererror(L, l, r);
rlm@1 232 }
rlm@1 233
rlm@1 234
rlm@1 235 static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
rlm@1 236 int res;
rlm@1 237 if (ttype(l) != ttype(r))
rlm@1 238 return luaG_ordererror(L, l, r);
rlm@1 239 else if (ttisnumber(l))
rlm@1 240 return luai_numle(nvalue(l), nvalue(r));
rlm@1 241 else if (ttisstring(l))
rlm@1 242 return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
rlm@1 243 else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */
rlm@1 244 return res;
rlm@1 245 else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */
rlm@1 246 return !res;
rlm@1 247 return luaG_ordererror(L, l, r);
rlm@1 248 }
rlm@1 249
rlm@1 250
rlm@1 251 int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {
rlm@1 252 const TValue *tm;
rlm@1 253 lua_assert(ttype(t1) == ttype(t2));
rlm@1 254 switch (ttype(t1)) {
rlm@1 255 case LUA_TNIL: return 1;
rlm@1 256 case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
rlm@1 257 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */
rlm@1 258 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);
rlm@1 259 case LUA_TUSERDATA: {
rlm@1 260 if (uvalue(t1) == uvalue(t2)) return 1;
rlm@1 261 tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,
rlm@1 262 TM_EQ);
rlm@1 263 break; /* will try TM */
rlm@1 264 }
rlm@1 265 case LUA_TTABLE: {
rlm@1 266 if (hvalue(t1) == hvalue(t2)) return 1;
rlm@1 267 tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
rlm@1 268 break; /* will try TM */
rlm@1 269 }
rlm@1 270 default: return gcvalue(t1) == gcvalue(t2);
rlm@1 271 }
rlm@1 272 if (tm == NULL) return 0; /* no TM? */
rlm@1 273 callTMres(L, L->top, tm, t1, t2); /* call TM */
rlm@1 274 return !l_isfalse(L->top);
rlm@1 275 }
rlm@1 276
rlm@1 277
rlm@1 278 void luaV_concat (lua_State *L, int total, int last) {
rlm@1 279 do {
rlm@1 280 StkId top = L->base + last + 1;
rlm@1 281 int n = 2; /* number of elements handled in this pass (at least 2) */
rlm@1 282 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
rlm@1 283 if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
rlm@1 284 luaG_concaterror(L, top-2, top-1);
rlm@1 285 } else if (tsvalue(top-1)->len == 0) /* second op is empty? */
rlm@1 286 (void)tostring(L, top - 2); /* result is first op (as string) */
rlm@1 287 else {
rlm@1 288 /* at least two string values; get as many as possible */
rlm@1 289 size_t tl = tsvalue(top-1)->len;
rlm@1 290 char *buffer;
rlm@1 291 int i;
rlm@1 292 /* collect total length */
rlm@1 293 for (n = 1; n < total && tostring(L, top-n-1); n++) {
rlm@1 294 size_t l = tsvalue(top-n-1)->len;
rlm@1 295 if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
rlm@1 296 tl += l;
rlm@1 297 }
rlm@1 298 buffer = luaZ_openspace(L, &G(L)->buff, tl);
rlm@1 299 tl = 0;
rlm@1 300 for (i=n; i>0; i--) { /* concat all strings */
rlm@1 301 size_t l = tsvalue(top-i)->len;
rlm@1 302 memcpy(buffer+tl, svalue(top-i), l);
rlm@1 303 tl += l;
rlm@1 304 }
rlm@1 305 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
rlm@1 306 }
rlm@1 307 total -= n-1; /* got `n' strings to create 1 new */
rlm@1 308 last -= n-1;
rlm@1 309 } while (total > 1); /* repeat until only 1 result left */
rlm@1 310 }
rlm@1 311
rlm@1 312
rlm@1 313 static void Arith (lua_State *L, StkId ra, const TValue *rb,
rlm@1 314 const TValue *rc, TMS op) {
rlm@1 315 TValue tempb, tempc;
rlm@1 316 const TValue *b, *c;
rlm@1 317 if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
rlm@1 318 (c = luaV_tonumber(rc, &tempc)) != NULL) {
rlm@1 319 lua_Number nb = nvalue(b), nc = nvalue(c);
rlm@1 320 switch (op) {
rlm@1 321 case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;
rlm@1 322 case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;
rlm@1 323 case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;
rlm@1 324 case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;
rlm@1 325 case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;
rlm@1 326 case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;
rlm@1 327 case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;
rlm@1 328 default: lua_assert(0); break;
rlm@1 329 }
rlm@1 330 }
rlm@1 331 else if (!call_binTM(L, rb, rc, ra, op))
rlm@1 332 luaG_aritherror(L, rb, rc);
rlm@1 333 }
rlm@1 334
rlm@1 335
rlm@1 336
rlm@1 337 /*
rlm@1 338 ** some macros for common tasks in `luaV_execute'
rlm@1 339 */
rlm@1 340
rlm@1 341 #define runtime_check(L, c) { if (!(c)) break; }
rlm@1 342
rlm@1 343 #define RA(i) (base+GETARG_A(i))
rlm@1 344 /* to be used after possible stack reallocation */
rlm@1 345 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
rlm@1 346 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
rlm@1 347 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
rlm@1 348 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))
rlm@1 349 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
rlm@1 350 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))
rlm@1 351 #define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))
rlm@1 352
rlm@1 353
rlm@1 354 #define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);}
rlm@1 355
rlm@1 356
rlm@1 357 #define Protect(x) { L->savedpc = pc; {x;}; base = L->base; }
rlm@1 358
rlm@1 359
rlm@1 360 #define arith_op(op,tm) { \
rlm@1 361 TValue *rb = RKB(i); \
rlm@1 362 TValue *rc = RKC(i); \
rlm@1 363 if (ttisnumber(rb) && ttisnumber(rc)) { \
rlm@1 364 lua_Number nb = nvalue(rb), nc = nvalue(rc); \
rlm@1 365 setnvalue(ra, op(nb, nc)); \
rlm@1 366 } \
rlm@1 367 else \
rlm@1 368 Protect(Arith(L, ra, rb, rc, tm)); \
rlm@1 369 }
rlm@1 370
rlm@1 371
rlm@1 372
rlm@1 373 void luaV_execute (lua_State *L, int nexeccalls) {
rlm@1 374 LClosure *cl;
rlm@1 375 StkId base;
rlm@1 376 TValue *k;
rlm@1 377 const Instruction *pc;
rlm@1 378 reentry: /* entry point */
rlm@1 379 lua_assert(isLua(L->ci));
rlm@1 380 pc = L->savedpc;
rlm@1 381 cl = &clvalue(L->ci->func)->l;
rlm@1 382 base = L->base;
rlm@1 383 k = cl->p->k;
rlm@1 384 /* main loop of interpreter */
rlm@1 385 for (;;) {
rlm@1 386 const Instruction i = *pc++;
rlm@1 387 StkId ra;
rlm@1 388 if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
rlm@1 389 (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
rlm@1 390 traceexec(L, pc);
rlm@1 391 if (L->status == LUA_YIELD) { /* did hook yield? */
rlm@1 392 L->savedpc = pc - 1;
rlm@1 393 return;
rlm@1 394 }
rlm@1 395 base = L->base;
rlm@1 396 }
rlm@1 397 /* warning!! several calls may realloc the stack and invalidate `ra' */
rlm@1 398 ra = RA(i);
rlm@1 399 lua_assert(base == L->base && L->base == L->ci->base);
rlm@1 400 lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);
rlm@1 401 lua_assert(L->top == L->ci->top || luaG_checkopenop(i));
rlm@1 402 switch (GET_OPCODE(i)) {
rlm@1 403 case OP_MOVE: {
rlm@1 404 setobjs2s(L, ra, RB(i));
rlm@1 405 continue;
rlm@1 406 }
rlm@1 407 case OP_LOADK: {
rlm@1 408 setobj2s(L, ra, KBx(i));
rlm@1 409 continue;
rlm@1 410 }
rlm@1 411 case OP_LOADBOOL: {
rlm@1 412 setbvalue(ra, GETARG_B(i));
rlm@1 413 if (GETARG_C(i)) pc++; /* skip next instruction (if C) */
rlm@1 414 continue;
rlm@1 415 }
rlm@1 416 case OP_LOADNIL: {
rlm@1 417 TValue *rb = RB(i);
rlm@1 418 do {
rlm@1 419 setnilvalue(rb--);
rlm@1 420 } while (rb >= ra);
rlm@1 421 continue;
rlm@1 422 }
rlm@1 423 case OP_GETUPVAL: {
rlm@1 424 int b = GETARG_B(i);
rlm@1 425 setobj2s(L, ra, cl->upvals[b]->v);
rlm@1 426 continue;
rlm@1 427 }
rlm@1 428 case OP_GETGLOBAL: {
rlm@1 429 TValue g;
rlm@1 430 TValue *rb = KBx(i);
rlm@1 431 sethvalue(L, &g, cl->env);
rlm@1 432 lua_assert(ttisstring(rb));
rlm@1 433 Protect(luaV_gettable(L, &g, rb, ra));
rlm@1 434 continue;
rlm@1 435 }
rlm@1 436 case OP_GETTABLE: {
rlm@1 437 Protect(luaV_gettable(L, RB(i), RKC(i), ra));
rlm@1 438 continue;
rlm@1 439 }
rlm@1 440 case OP_SETGLOBAL: {
rlm@1 441 TValue g;
rlm@1 442 sethvalue(L, &g, cl->env);
rlm@1 443 lua_assert(ttisstring(KBx(i)));
rlm@1 444 Protect(luaV_settable(L, &g, KBx(i), ra));
rlm@1 445 continue;
rlm@1 446 }
rlm@1 447 case OP_SETUPVAL: {
rlm@1 448 UpVal *uv = cl->upvals[GETARG_B(i)];
rlm@1 449 setobj(L, uv->v, ra);
rlm@1 450 luaC_barrier(L, uv, ra);
rlm@1 451 continue;
rlm@1 452 }
rlm@1 453 case OP_SETTABLE: {
rlm@1 454 Protect(luaV_settable(L, ra, RKB(i), RKC(i)));
rlm@1 455 continue;
rlm@1 456 }
rlm@1 457 case OP_NEWTABLE: {
rlm@1 458 int b = GETARG_B(i);
rlm@1 459 int c = GETARG_C(i);
rlm@1 460 sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));
rlm@1 461 Protect(luaC_checkGC(L));
rlm@1 462 continue;
rlm@1 463 }
rlm@1 464 case OP_SELF: {
rlm@1 465 StkId rb = RB(i);
rlm@1 466 setobjs2s(L, ra+1, rb);
rlm@1 467 Protect(luaV_gettable(L, rb, RKC(i), ra));
rlm@1 468 continue;
rlm@1 469 }
rlm@1 470 case OP_ADD: {
rlm@1 471 arith_op(luai_numadd, TM_ADD);
rlm@1 472 continue;
rlm@1 473 }
rlm@1 474 case OP_SUB: {
rlm@1 475 arith_op(luai_numsub, TM_SUB);
rlm@1 476 continue;
rlm@1 477 }
rlm@1 478 case OP_MUL: {
rlm@1 479 arith_op(luai_nummul, TM_MUL);
rlm@1 480 continue;
rlm@1 481 }
rlm@1 482 case OP_DIV: {
rlm@1 483 arith_op(luai_numdiv, TM_DIV);
rlm@1 484 continue;
rlm@1 485 }
rlm@1 486 case OP_MOD: {
rlm@1 487 arith_op(luai_nummod, TM_MOD);
rlm@1 488 continue;
rlm@1 489 }
rlm@1 490 case OP_POW: {
rlm@1 491 arith_op(luai_numpow, TM_POW);
rlm@1 492 continue;
rlm@1 493 }
rlm@1 494 case OP_UNM: {
rlm@1 495 TValue *rb = RB(i);
rlm@1 496 if (ttisnumber(rb)) {
rlm@1 497 lua_Number nb = nvalue(rb);
rlm@1 498 setnvalue(ra, luai_numunm(nb));
rlm@1 499 }
rlm@1 500 else {
rlm@1 501 Protect(Arith(L, ra, rb, rb, TM_UNM));
rlm@1 502 }
rlm@1 503 continue;
rlm@1 504 }
rlm@1 505 case OP_NOT: {
rlm@1 506 int res = l_isfalse(RB(i)); /* next assignment may change this value */
rlm@1 507 setbvalue(ra, res);
rlm@1 508 continue;
rlm@1 509 }
rlm@1 510 case OP_LEN: {
rlm@1 511 const TValue *rb = RB(i);
rlm@1 512 switch (ttype(rb)) {
rlm@1 513 case LUA_TTABLE: {
rlm@1 514 setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));
rlm@1 515 break;
rlm@1 516 }
rlm@1 517 case LUA_TSTRING: {
rlm@1 518 setnvalue(ra, cast_num(tsvalue(rb)->len));
rlm@1 519 break;
rlm@1 520 }
rlm@1 521 default: { /* try metamethod */
rlm@1 522 Protect(
rlm@1 523 if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))
rlm@1 524 luaG_typeerror(L, rb, "get length of");
rlm@1 525 )
rlm@1 526 }
rlm@1 527 }
rlm@1 528 continue;
rlm@1 529 }
rlm@1 530 case OP_CONCAT: {
rlm@1 531 int b = GETARG_B(i);
rlm@1 532 int c = GETARG_C(i);
rlm@1 533 Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));
rlm@1 534 setobjs2s(L, RA(i), base+b);
rlm@1 535 continue;
rlm@1 536 }
rlm@1 537 case OP_JMP: {
rlm@1 538 dojump(L, pc, GETARG_sBx(i));
rlm@1 539 continue;
rlm@1 540 }
rlm@1 541 case OP_EQ: {
rlm@1 542 TValue *rb = RKB(i);
rlm@1 543 TValue *rc = RKC(i);
rlm@1 544 Protect(
rlm@1 545 if (equalobj(L, rb, rc) == GETARG_A(i))
rlm@1 546 dojump(L, pc, GETARG_sBx(*pc));
rlm@1 547 )
rlm@1 548 pc++;
rlm@1 549 continue;
rlm@1 550 }
rlm@1 551 case OP_LT: {
rlm@1 552 Protect(
rlm@1 553 if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
rlm@1 554 dojump(L, pc, GETARG_sBx(*pc));
rlm@1 555 )
rlm@1 556 pc++;
rlm@1 557 continue;
rlm@1 558 }
rlm@1 559 case OP_LE: {
rlm@1 560 Protect(
rlm@1 561 if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
rlm@1 562 dojump(L, pc, GETARG_sBx(*pc));
rlm@1 563 )
rlm@1 564 pc++;
rlm@1 565 continue;
rlm@1 566 }
rlm@1 567 case OP_TEST: {
rlm@1 568 if (l_isfalse(ra) != GETARG_C(i))
rlm@1 569 dojump(L, pc, GETARG_sBx(*pc));
rlm@1 570 pc++;
rlm@1 571 continue;
rlm@1 572 }
rlm@1 573 case OP_TESTSET: {
rlm@1 574 TValue *rb = RB(i);
rlm@1 575 if (l_isfalse(rb) != GETARG_C(i)) {
rlm@1 576 setobjs2s(L, ra, rb);
rlm@1 577 dojump(L, pc, GETARG_sBx(*pc));
rlm@1 578 }
rlm@1 579 pc++;
rlm@1 580 continue;
rlm@1 581 }
rlm@1 582 case OP_CALL: {
rlm@1 583 int b = GETARG_B(i);
rlm@1 584 int nresults = GETARG_C(i) - 1;
rlm@1 585 if (b != 0) L->top = ra+b; /* else previous instruction set top */
rlm@1 586 L->savedpc = pc;
rlm@1 587 switch (luaD_precall(L, ra, nresults)) {
rlm@1 588 case PCRLUA: {
rlm@1 589 nexeccalls++;
rlm@1 590 goto reentry; /* restart luaV_execute over new Lua function */
rlm@1 591 }
rlm@1 592 case PCRC: {
rlm@1 593 /* it was a C function (`precall' called it); adjust results */
rlm@1 594 if (nresults >= 0) L->top = L->ci->top;
rlm@1 595 base = L->base;
rlm@1 596 continue;
rlm@1 597 }
rlm@1 598 default: {
rlm@1 599 return; /* yield */
rlm@1 600 }
rlm@1 601 }
rlm@1 602 }
rlm@1 603 case OP_TAILCALL: {
rlm@1 604 int b = GETARG_B(i);
rlm@1 605 if (b != 0) L->top = ra+b; /* else previous instruction set top */
rlm@1 606 L->savedpc = pc;
rlm@1 607 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);
rlm@1 608 switch (luaD_precall(L, ra, LUA_MULTRET)) {
rlm@1 609 case PCRLUA: {
rlm@1 610 /* tail call: put new frame in place of previous one */
rlm@1 611 CallInfo *ci = L->ci - 1; /* previous frame */
rlm@1 612 int aux;
rlm@1 613 StkId func = ci->func;
rlm@1 614 StkId pfunc = (ci+1)->func; /* previous function index */
rlm@1 615 if (L->openupval) luaF_close(L, ci->base);
rlm@1 616 L->base = ci->base = ci->func + ((ci+1)->base - pfunc);
rlm@1 617 for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */
rlm@1 618 setobjs2s(L, func+aux, pfunc+aux);
rlm@1 619 ci->top = L->top = func+aux; /* correct top */
rlm@1 620 lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);
rlm@1 621 ci->savedpc = L->savedpc;
rlm@1 622 ci->tailcalls++; /* one more call lost */
rlm@1 623 L->ci--; /* remove new frame */
rlm@1 624 goto reentry;
rlm@1 625 }
rlm@1 626 case PCRC: { /* it was a C function (`precall' called it) */
rlm@1 627 base = L->base;
rlm@1 628 continue;
rlm@1 629 }
rlm@1 630 default: {
rlm@1 631 return; /* yield */
rlm@1 632 }
rlm@1 633 }
rlm@1 634 }
rlm@1 635 case OP_RETURN: {
rlm@1 636 int b = GETARG_B(i);
rlm@1 637 if (b != 0) L->top = ra+b-1;
rlm@1 638 if (L->openupval) luaF_close(L, base);
rlm@1 639 L->savedpc = pc;
rlm@1 640 b = luaD_poscall(L, ra);
rlm@1 641 if (--nexeccalls == 0) /* was previous function running `here'? */
rlm@1 642 return; /* no: return */
rlm@1 643 else { /* yes: continue its execution */
rlm@1 644 if (b) L->top = L->ci->top;
rlm@1 645 lua_assert(isLua(L->ci));
rlm@1 646 lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);
rlm@1 647 goto reentry;
rlm@1 648 }
rlm@1 649 }
rlm@1 650 case OP_FORLOOP: {
rlm@1 651 lua_Number step = nvalue(ra+2);
rlm@1 652 lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */
rlm@1 653 lua_Number limit = nvalue(ra+1);
rlm@1 654 if (luai_numlt(0, step) ? luai_numle(idx, limit)
rlm@1 655 : luai_numle(limit, idx)) {
rlm@1 656 dojump(L, pc, GETARG_sBx(i)); /* jump back */
rlm@1 657 setnvalue(ra, idx); /* update internal index... */
rlm@1 658 setnvalue(ra+3, idx); /* ...and external index */
rlm@1 659 }
rlm@1 660 continue;
rlm@1 661 }
rlm@1 662 case OP_FORPREP: {
rlm@1 663 const TValue *init = ra;
rlm@1 664 const TValue *plimit = ra+1;
rlm@1 665 const TValue *pstep = ra+2;
rlm@1 666 L->savedpc = pc; /* next steps may throw errors */
rlm@1 667 if (!tonumber(init, ra))
rlm@1 668 luaG_runerror(L, LUA_QL("for") " initial value must be a number");
rlm@1 669 else if (!tonumber(plimit, ra+1))
rlm@1 670 luaG_runerror(L, LUA_QL("for") " limit must be a number");
rlm@1 671 else if (!tonumber(pstep, ra+2))
rlm@1 672 luaG_runerror(L, LUA_QL("for") " step must be a number");
rlm@1 673 setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));
rlm@1 674 dojump(L, pc, GETARG_sBx(i));
rlm@1 675 continue;
rlm@1 676 }
rlm@1 677 case OP_TFORLOOP: {
rlm@1 678 StkId cb = ra + 3; /* call base */
rlm@1 679 setobjs2s(L, cb+2, ra+2);
rlm@1 680 setobjs2s(L, cb+1, ra+1);
rlm@1 681 setobjs2s(L, cb, ra);
rlm@1 682 L->top = cb+3; /* func. + 2 args (state and index) */
rlm@1 683 Protect(luaD_call(L, cb, GETARG_C(i)));
rlm@1 684 L->top = L->ci->top;
rlm@1 685 cb = RA(i) + 3; /* previous call may change the stack */
rlm@1 686 if (!ttisnil(cb)) { /* continue loop? */
rlm@1 687 setobjs2s(L, cb-1, cb); /* save control variable */
rlm@1 688 dojump(L, pc, GETARG_sBx(*pc)); /* jump back */
rlm@1 689 }
rlm@1 690 pc++;
rlm@1 691 continue;
rlm@1 692 }
rlm@1 693 case OP_SETLIST: {
rlm@1 694 int n = GETARG_B(i);
rlm@1 695 int c = GETARG_C(i);
rlm@1 696 int last;
rlm@1 697 Table *h;
rlm@1 698 if (n == 0) {
rlm@1 699 n = cast_int(L->top - ra) - 1;
rlm@1 700 L->top = L->ci->top;
rlm@1 701 }
rlm@1 702 if (c == 0) c = cast_int(*pc++);
rlm@1 703 runtime_check(L, ttistable(ra));
rlm@1 704 h = hvalue(ra);
rlm@1 705 last = ((c-1)*LFIELDS_PER_FLUSH) + n;
rlm@1 706 if (last > h->sizearray) /* needs more space? */
rlm@1 707 luaH_resizearray(L, h, last); /* pre-alloc it at once */
rlm@1 708 for (; n > 0; n--) {
rlm@1 709 TValue *val = ra+n;
rlm@1 710 setobj2t(L, luaH_setnum(L, h, last--), val);
rlm@1 711 luaC_barriert(L, h, val);
rlm@1 712 }
rlm@1 713 continue;
rlm@1 714 }
rlm@1 715 case OP_CLOSE: {
rlm@1 716 luaF_close(L, ra);
rlm@1 717 continue;
rlm@1 718 }
rlm@1 719 case OP_CLOSURE: {
rlm@1 720 Proto *p;
rlm@1 721 Closure *ncl;
rlm@1 722 int nup, j;
rlm@1 723 p = cl->p->p[GETARG_Bx(i)];
rlm@1 724 nup = p->nups;
rlm@1 725 ncl = luaF_newLclosure(L, nup, cl->env);
rlm@1 726 ncl->l.p = p;
rlm@1 727 for (j=0; j<nup; j++, pc++) {
rlm@1 728 if (GET_OPCODE(*pc) == OP_GETUPVAL)
rlm@1 729 ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];
rlm@1 730 else {
rlm@1 731 lua_assert(GET_OPCODE(*pc) == OP_MOVE);
rlm@1 732 ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));
rlm@1 733 }
rlm@1 734 }
rlm@1 735 setclvalue(L, ra, ncl);
rlm@1 736 Protect(luaC_checkGC(L));
rlm@1 737 continue;
rlm@1 738 }
rlm@1 739 case OP_VARARG: {
rlm@1 740 int b = GETARG_B(i) - 1;
rlm@1 741 int j;
rlm@1 742 CallInfo *ci = L->ci;
rlm@1 743 int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;
rlm@1 744 if (b == LUA_MULTRET) {
rlm@1 745 Protect(luaD_checkstack(L, n));
rlm@1 746 ra = RA(i); /* previous call may change the stack */
rlm@1 747 b = n;
rlm@1 748 L->top = ra + n;
rlm@1 749 }
rlm@1 750 for (j = 0; j < b; j++) {
rlm@1 751 if (j < n) {
rlm@1 752 setobjs2s(L, ra + j, ci->base - n + j);
rlm@1 753 }
rlm@1 754 else {
rlm@1 755 setnilvalue(ra + j);
rlm@1 756 }
rlm@1 757 }
rlm@1 758 continue;
rlm@1 759 }
rlm@1 760 }
rlm@1 761 }
rlm@1 762 }
rlm@1 763