1 /* $NetBSD: lvm.c,v 1.14 2018/08/04 17:30:01 alnsn Exp $ */ 2 3 /* 4 ** Id: lvm.c,v 2.268.1.1 2017/04/19 17:39:34 roberto Exp 5 ** Lua virtual machine 6 ** See Copyright Notice in lua.h 7 */ 8 9 #define lvm_c 10 #define LUA_CORE 11 12 #include "lprefix.h" 13 14 #ifndef _KERNEL 15 #include <float.h> 16 #include <limits.h> 17 #include <math.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #endif /* _KERNEL */ 22 23 #include "lua.h" 24 25 #include "ldebug.h" 26 #include "ldo.h" 27 #include "lfunc.h" 28 #include "lgc.h" 29 #include "lobject.h" 30 #include "lopcodes.h" 31 #include "lstate.h" 32 #include "lstring.h" 33 #include "ltable.h" 34 #include "ltm.h" 35 #include "lvm.h" 36 37 38 /* limit for table tag-method chains (to avoid loops) */ 39 #define MAXTAGLOOP 2000 40 41 42 43 #ifndef _KERNEL 44 /* 45 ** 'l_intfitsf' checks whether a given integer can be converted to a 46 ** float without rounding. Used in comparisons. Left undefined if 47 ** all integers fit in a float precisely. 48 */ 49 #if !defined(l_intfitsf) 50 51 /* number of bits in the mantissa of a float */ 52 #define NBM (l_mathlim(MANT_DIG)) 53 54 /* 55 ** Check whether some integers may not fit in a float, that is, whether 56 ** (maxinteger >> NBM) > 0 (that implies (1 << NBM) <= maxinteger). 57 ** (The shifts are done in parts to avoid shifting by more than the size 58 ** of an integer. In a worst case, NBM == 113 for long double and 59 ** sizeof(integer) == 32.) 60 */ 61 #if ((((LUA_MAXINTEGER >> (NBM / 4)) >> (NBM / 4)) >> (NBM / 4)) \ 62 >> (NBM - (3 * (NBM / 4)))) > 0 63 64 #define l_intfitsf(i) \ 65 (-((lua_Integer)1 << NBM) <= (i) && (i) <= ((lua_Integer)1 << NBM)) 66 67 #endif 68 69 #endif 70 71 72 73 /* 74 ** Try to convert a value to a float. The float case is already handled 75 ** by the macro 'tonumber'. 76 */ 77 int luaV_tonumber_ (const TValue *obj, lua_Number *n) { 78 TValue v; 79 if (ttisinteger(obj)) { 80 *n = cast_num(ivalue(obj)); 81 return 1; 82 } 83 else if (cvt2num(obj) && /* string convertible to number? */ 84 luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { 85 *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ 86 return 1; 87 } 88 else 89 return 0; /* conversion failed */ 90 } 91 #endif /* _KERNEL */ 92 93 94 /* 95 ** try to convert a value to an integer, rounding according to 'mode': 96 ** mode == 0: accepts only integral values 97 ** mode == 1: takes the floor of the number 98 ** mode == 2: takes the ceil of the number 99 */ 100 int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { 101 TValue v; 102 again: 103 #ifndef _KERNEL 104 if (ttisfloat(obj)) { 105 lua_Number n = fltvalue(obj); 106 lua_Number f = l_floor(n); 107 if (n != f) { /* not an integral value? */ 108 if (mode == 0) return 0; /* fails if mode demands integral value */ 109 else if (mode > 1) /* needs ceil? */ 110 f += 1; /* convert floor to ceil (remember: n != f) */ 111 } 112 return lua_numbertointeger(f, p); 113 } 114 else if (ttisinteger(obj)) { 115 #else /* _KERNEL */ 116 if (ttisinteger(obj)) { 117 UNUSED(mode); 118 #endif /* _KERNEL */ 119 *p = ivalue(obj); 120 return 1; 121 } 122 else if (cvt2num(obj) && 123 luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { 124 obj = &v; 125 goto again; /* convert result from 'luaO_str2num' to an integer */ 126 } 127 return 0; /* conversion failed */ 128 } 129 130 131 #ifndef _KERNEL 132 /* 133 ** Try to convert a 'for' limit to an integer, preserving the 134 ** semantics of the loop. 135 ** (The following explanation assumes a non-negative step; it is valid 136 ** for negative steps mutatis mutandis.) 137 ** If the limit can be converted to an integer, rounding down, that is 138 ** it. 139 ** Otherwise, check whether the limit can be converted to a number. If 140 ** the number is too large, it is OK to set the limit as LUA_MAXINTEGER, 141 ** which means no limit. If the number is too negative, the loop 142 ** should not run, because any initial integer value is larger than the 143 ** limit. So, it sets the limit to LUA_MININTEGER. 'stopnow' corrects 144 ** the extreme case when the initial value is LUA_MININTEGER, in which 145 ** case the LUA_MININTEGER limit would still run the loop once. 146 */ 147 static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, 148 int *stopnow) { 149 *stopnow = 0; /* usually, let loops run */ 150 if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ 151 lua_Number n; /* try to convert to float */ 152 if (!tonumber(obj, &n)) /* cannot convert to float? */ 153 return 0; /* not a number */ 154 if (luai_numlt(0, n)) { /* if true, float is larger than max integer */ 155 *p = LUA_MAXINTEGER; 156 if (step < 0) *stopnow = 1; 157 } 158 else { /* float is smaller than min integer */ 159 *p = LUA_MININTEGER; 160 if (step >= 0) *stopnow = 1; 161 } 162 } 163 return 1; 164 } 165 #endif /* _KERNEL */ 166 167 168 /* 169 ** Finish the table access 'val = t[key]'. 170 ** if 'slot' is NULL, 't' is not a table; otherwise, 'slot' points to 171 ** t[k] entry (which must be nil). 172 */ 173 void luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val, 174 const TValue *slot) { 175 int loop; /* counter to avoid infinite loops */ 176 const TValue *tm; /* metamethod */ 177 for (loop = 0; loop < MAXTAGLOOP; loop++) { 178 if (slot == NULL) { /* 't' is not a table? */ 179 lua_assert(!ttistable(t)); 180 tm = luaT_gettmbyobj(L, t, TM_INDEX); 181 if (ttisnil(tm)) 182 luaG_typeerror(L, t, "index"); /* no metamethod */ 183 /* else will try the metamethod */ 184 } 185 else { /* 't' is a table */ 186 lua_assert(ttisnil(slot)); 187 tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); /* table's metamethod */ 188 if (tm == NULL) { /* no metamethod? */ 189 setnilvalue(val); /* result is nil */ 190 return; 191 } 192 /* else will try the metamethod */ 193 } 194 if (ttisfunction(tm)) { /* is metamethod a function? */ 195 luaT_callTM(L, tm, t, key, val, 1); /* call it */ 196 return; 197 } 198 t = tm; /* else try to access 'tm[key]' */ 199 if (luaV_fastget(L,t,key,slot,luaH_get)) { /* fast track? */ 200 setobj2s(L, val, slot); /* done */ 201 return; 202 } 203 /* else repeat (tail call 'luaV_finishget') */ 204 } 205 luaG_runerror(L, "'__index' chain too long; possible loop"); 206 } 207 208 209 /* 210 ** Finish a table assignment 't[key] = val'. 211 ** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points 212 ** to the entry 't[key]', or to 'luaO_nilobject' if there is no such 213 ** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastset' 214 ** would have done the job.) 215 */ 216 void luaV_finishset (lua_State *L, const TValue *t, TValue *key, 217 StkId val, const TValue *slot) { 218 int loop; /* counter to avoid infinite loops */ 219 for (loop = 0; loop < MAXTAGLOOP; loop++) { 220 const TValue *tm; /* '__newindex' metamethod */ 221 if (slot != NULL) { /* is 't' a table? */ 222 Table *h = hvalue(t); /* save 't' table */ 223 lua_assert(ttisnil(slot)); /* old value must be nil */ 224 tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ 225 if (tm == NULL) { /* no metamethod? */ 226 if (slot == luaO_nilobject) /* no previous entry? */ 227 slot = luaH_newkey(L, h, key); /* create one */ 228 /* no metamethod and (now) there is an entry with given key */ 229 setobj2t(L, cast(TValue *, slot), val); /* set its new value */ 230 invalidateTMcache(h); 231 luaC_barrierback(L, h, val); 232 return; 233 } 234 /* else will try the metamethod */ 235 } 236 else { /* not a table; check metamethod */ 237 if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) 238 luaG_typeerror(L, t, "index"); 239 } 240 /* try the metamethod */ 241 if (ttisfunction(tm)) { 242 luaT_callTM(L, tm, t, key, val, 0); 243 return; 244 } 245 t = tm; /* else repeat assignment over 'tm' */ 246 if (luaV_fastset(L, t, key, slot, luaH_get, val)) 247 return; /* done */ 248 /* else loop */ 249 } 250 luaG_runerror(L, "'__newindex' chain too long; possible loop"); 251 } 252 253 254 /* 255 ** Compare two strings 'ls' x 'rs', returning an integer smaller-equal- 256 ** -larger than zero if 'ls' is smaller-equal-larger than 'rs'. 257 ** The code is a little tricky because it allows '\0' in the strings 258 ** and it uses 'strcoll' (to respect locales) for each segments 259 ** of the strings. 260 */ 261 static int l_strcmp (const TString *ls, const TString *rs) { 262 const char *l = getstr(ls); 263 size_t ll = tsslen(ls); 264 const char *r = getstr(rs); 265 size_t lr = tsslen(rs); 266 for (;;) { /* for each segment */ 267 int temp = strcoll(l, r); 268 if (temp != 0) /* not equal? */ 269 return temp; /* done */ 270 else { /* strings are equal up to a '\0' */ 271 size_t len = strlen(l); /* index of first '\0' in both strings */ 272 if (len == lr) /* 'rs' is finished? */ 273 return (len == ll) ? 0 : 1; /* check 'ls' */ 274 else if (len == ll) /* 'ls' is finished? */ 275 return -1; /* 'ls' is smaller than 'rs' ('rs' is not finished) */ 276 /* both strings longer than 'len'; go on comparing after the '\0' */ 277 len++; 278 l += len; ll -= len; r += len; lr -= len; 279 } 280 } 281 } 282 283 284 #ifndef _KERNEL 285 /* 286 ** Check whether integer 'i' is less than float 'f'. If 'i' has an 287 ** exact representation as a float ('l_intfitsf'), compare numbers as 288 ** floats. Otherwise, if 'f' is outside the range for integers, result 289 ** is trivial. Otherwise, compare them as integers. (When 'i' has no 290 ** float representation, either 'f' is "far away" from 'i' or 'f' has 291 ** no precision left for a fractional part; either way, how 'f' is 292 ** truncated is irrelevant.) When 'f' is NaN, comparisons must result 293 ** in false. 294 */ 295 static int LTintfloat (lua_Integer i, lua_Number f) { 296 #if defined(l_intfitsf) 297 if (!l_intfitsf(i)) { 298 if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ 299 return 1; /* f >= maxint + 1 > i */ 300 else if (f > cast_num(LUA_MININTEGER)) /* minint < f <= maxint ? */ 301 return (i < cast(lua_Integer, f)); /* compare them as integers */ 302 else /* f <= minint <= i (or 'f' is NaN) --> not(i < f) */ 303 return 0; 304 } 305 #endif 306 return luai_numlt(cast_num(i), f); /* compare them as floats */ 307 } 308 309 310 /* 311 ** Check whether integer 'i' is less than or equal to float 'f'. 312 ** See comments on previous function. 313 */ 314 static int LEintfloat (lua_Integer i, lua_Number f) { 315 #if defined(l_intfitsf) 316 if (!l_intfitsf(i)) { 317 if (f >= -cast_num(LUA_MININTEGER)) /* -minint == maxint + 1 */ 318 return 1; /* f >= maxint + 1 > i */ 319 else if (f >= cast_num(LUA_MININTEGER)) /* minint <= f <= maxint ? */ 320 return (i <= cast(lua_Integer, f)); /* compare them as integers */ 321 else /* f < minint <= i (or 'f' is NaN) --> not(i <= f) */ 322 return 0; 323 } 324 #endif 325 return luai_numle(cast_num(i), f); /* compare them as floats */ 326 } 327 328 329 /* 330 ** Return 'l < r', for numbers. 331 */ 332 static int LTnum (const TValue *l, const TValue *r) { 333 if (ttisinteger(l)) { 334 lua_Integer li = ivalue(l); 335 if (ttisinteger(r)) 336 return li < ivalue(r); /* both are integers */ 337 else /* 'l' is int and 'r' is float */ 338 return LTintfloat(li, fltvalue(r)); /* l < r ? */ 339 } 340 else { 341 lua_Number lf = fltvalue(l); /* 'l' must be float */ 342 if (ttisfloat(r)) 343 return luai_numlt(lf, fltvalue(r)); /* both are float */ 344 else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ 345 return 0; /* NaN < i is always false */ 346 else /* without NaN, (l < r) <--> not(r <= l) */ 347 return !LEintfloat(ivalue(r), lf); /* not (r <= l) ? */ 348 } 349 } 350 351 352 /* 353 ** Return 'l <= r', for numbers. 354 */ 355 static int LEnum (const TValue *l, const TValue *r) { 356 if (ttisinteger(l)) { 357 lua_Integer li = ivalue(l); 358 if (ttisinteger(r)) 359 return li <= ivalue(r); /* both are integers */ 360 else /* 'l' is int and 'r' is float */ 361 return LEintfloat(li, fltvalue(r)); /* l <= r ? */ 362 } 363 else { 364 lua_Number lf = fltvalue(l); /* 'l' must be float */ 365 if (ttisfloat(r)) 366 return luai_numle(lf, fltvalue(r)); /* both are float */ 367 else if (luai_numisnan(lf)) /* 'r' is int and 'l' is float */ 368 return 0; /* NaN <= i is always false */ 369 else /* without NaN, (l <= r) <--> not(r < l) */ 370 return !LTintfloat(ivalue(r), lf); /* not (r < l) ? */ 371 } 372 } 373 #endif /* _KERNEL */ 374 375 376 /* 377 ** Main operation less than; return 'l < r'. 378 */ 379 int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { 380 int res; 381 #ifndef _KERNEL 382 if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ 383 return LTnum(l, r); 384 #else /* _KERNEL */ 385 if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */ 386 return (ivalue(l) < ivalue(r)); 387 #endif /* _KERNEL */ 388 else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ 389 return l_strcmp(tsvalue(l), tsvalue(r)) < 0; 390 else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0) /* no metamethod? */ 391 luaG_ordererror(L, l, r); /* error */ 392 return res; 393 } 394 395 396 /* 397 ** Main operation less than or equal to; return 'l <= r'. If it needs 398 ** a metamethod and there is no '__le', try '__lt', based on 399 ** l <= r iff !(r < l) (assuming a total order). If the metamethod 400 ** yields during this substitution, the continuation has to know 401 ** about it (to negate the result of r<l); bit CIST_LEQ in the call 402 ** status keeps that information. 403 */ 404 int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { 405 int res; 406 #ifndef _KERNEL 407 if (ttisnumber(l) && ttisnumber(r)) /* both operands are numbers? */ 408 return LEnum(l, r); 409 #else /* _KERNEL */ 410 if (ttisinteger(l) && ttisinteger(r)) /* both operands are integers? */ 411 return (ivalue(l) <= ivalue(r)); 412 #endif /* _KERNEL */ 413 else if (ttisstring(l) && ttisstring(r)) /* both are strings? */ 414 return l_strcmp(tsvalue(l), tsvalue(r)) <= 0; 415 else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0) /* try 'le' */ 416 return res; 417 else { /* try 'lt': */ 418 L->ci->callstatus |= CIST_LEQ; /* mark it is doing 'lt' for 'le' */ 419 res = luaT_callorderTM(L, r, l, TM_LT); 420 L->ci->callstatus ^= CIST_LEQ; /* clear mark */ 421 if (res < 0) 422 luaG_ordererror(L, l, r); 423 return !res; /* result is negated */ 424 } 425 } 426 427 428 /* 429 ** Main operation for equality of Lua values; return 't1 == t2'. 430 ** L == NULL means raw equality (no metamethods) 431 */ 432 int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { 433 const TValue *tm; 434 if (ttype(t1) != ttype(t2)) { /* not the same variant? */ 435 #ifndef _KERNEL 436 if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) 437 return 0; /* only numbers can be equal with different variants */ 438 else { /* two numbers with different variants */ 439 lua_Integer i1, i2; /* compare them as integers */ 440 return (tointeger(t1, &i1) && tointeger(t2, &i2) && i1 == i2); 441 } 442 #else /* _KERNEL */ 443 return 0; /* numbers have only the integer variant */ 444 #endif /* _KERNEL */ 445 } 446 /* values have same type and same variant */ 447 switch (ttype(t1)) { 448 case LUA_TNIL: return 1; 449 case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); 450 #ifndef _KERNEL 451 case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); 452 #endif /* _KERNEL */ 453 case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ 454 case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); 455 case LUA_TLCF: return fvalue(t1) == fvalue(t2); 456 case LUA_TSHRSTR: return eqshrstr(tsvalue(t1), tsvalue(t2)); 457 case LUA_TLNGSTR: return luaS_eqlngstr(tsvalue(t1), tsvalue(t2)); 458 case LUA_TUSERDATA: { 459 if (uvalue(t1) == uvalue(t2)) return 1; 460 else if (L == NULL) return 0; 461 tm = fasttm(L, uvalue(t1)->metatable, TM_EQ); 462 if (tm == NULL) 463 tm = fasttm(L, uvalue(t2)->metatable, TM_EQ); 464 break; /* will try TM */ 465 } 466 case LUA_TTABLE: { 467 if (hvalue(t1) == hvalue(t2)) return 1; 468 else if (L == NULL) return 0; 469 tm = fasttm(L, hvalue(t1)->metatable, TM_EQ); 470 if (tm == NULL) 471 tm = fasttm(L, hvalue(t2)->metatable, TM_EQ); 472 break; /* will try TM */ 473 } 474 default: 475 return gcvalue(t1) == gcvalue(t2); 476 } 477 if (tm == NULL) /* no TM? */ 478 return 0; /* objects are different */ 479 luaT_callTM(L, tm, t1, t2, L->top, 1); /* call TM */ 480 return !l_isfalse(L->top); 481 } 482 483 484 /* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ 485 #define tostring(L,o) \ 486 (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) 487 488 #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) 489 490 /* copy strings in stack from top - n up to top - 1 to buffer */ 491 static void copy2buff (StkId top, int n, char *buff) { 492 size_t tl = 0; /* size already copied */ 493 do { 494 size_t l = vslen(top - n); /* length of string being copied */ 495 memcpy(buff + tl, svalue(top - n), l * sizeof(char)); 496 tl += l; 497 } while (--n > 0); 498 } 499 500 501 /* 502 ** Main operation for concatenation: concat 'total' values in the stack, 503 ** from 'L->top - total' up to 'L->top - 1'. 504 */ 505 void luaV_concat (lua_State *L, int total) { 506 lua_assert(total >= 2); 507 do { 508 StkId top = L->top; 509 int n = 2; /* number of elements handled in this pass (at least 2) */ 510 if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1)) 511 luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); 512 else if (isemptystr(top - 1)) /* second operand is empty? */ 513 cast_void(tostring(L, top - 2)); /* result is first operand */ 514 else if (isemptystr(top - 2)) { /* first operand is an empty string? */ 515 setobjs2s(L, top - 2, top - 1); /* result is second op. */ 516 } 517 else { 518 /* at least two non-empty string values; get as many as possible */ 519 size_t tl = vslen(top - 1); 520 TString *ts; 521 /* collect total length and number of strings */ 522 for (n = 1; n < total && tostring(L, top - n - 1); n++) { 523 size_t l = vslen(top - n - 1); 524 if (l >= (MAX_SIZE/sizeof(char)) - tl) 525 luaG_runerror(L, "string length overflow"); 526 tl += l; 527 } 528 if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ 529 char buff[LUAI_MAXSHORTLEN]; 530 copy2buff(top, n, buff); /* copy strings to buffer */ 531 ts = luaS_newlstr(L, buff, tl); 532 } 533 else { /* long string; copy strings directly to final result */ 534 ts = luaS_createlngstrobj(L, tl); 535 copy2buff(top, n, getstr(ts)); 536 } 537 setsvalue2s(L, top - n, ts); /* create result */ 538 } 539 total -= n-1; /* got 'n' strings to create 1 new */ 540 L->top -= n-1; /* popped 'n' strings and pushed one */ 541 } while (total > 1); /* repeat until only 1 result left */ 542 } 543 544 545 /* 546 ** Main operation 'ra' = #rb'. 547 */ 548 void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { 549 const TValue *tm; 550 switch (ttype(rb)) { 551 case LUA_TTABLE: { 552 Table *h = hvalue(rb); 553 tm = fasttm(L, h->metatable, TM_LEN); 554 if (tm) break; /* metamethod? break switch to call it */ 555 setivalue(ra, luaH_getn(h)); /* else primitive len */ 556 return; 557 } 558 case LUA_TSHRSTR: { 559 setivalue(ra, tsvalue(rb)->shrlen); 560 return; 561 } 562 case LUA_TLNGSTR: { 563 setivalue(ra, tsvalue(rb)->u.lnglen); 564 return; 565 } 566 default: { /* try metamethod */ 567 tm = luaT_gettmbyobj(L, rb, TM_LEN); 568 if (ttisnil(tm)) /* no metamethod? */ 569 luaG_typeerror(L, rb, "get length of"); 570 break; 571 } 572 } 573 luaT_callTM(L, tm, rb, rb, ra, 1); 574 } 575 576 577 /* 578 ** Integer division; return 'm // n', that is, floor(m/n). 579 ** C division truncates its result (rounds towards zero). 580 ** 'floor(q) == trunc(q)' when 'q >= 0' or when 'q' is integer, 581 ** otherwise 'floor(q) == trunc(q) - 1'. 582 */ 583 lua_Integer luaV_div (lua_State *L, lua_Integer m, lua_Integer n) { 584 if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ 585 if (n == 0) 586 luaG_runerror(L, "attempt to divide by zero"); 587 return intop(-, 0, m); /* n==-1; avoid overflow with 0x80000...//-1 */ 588 } 589 else { 590 lua_Integer q = m / n; /* perform C division */ 591 if ((m ^ n) < 0 && m % n != 0) /* 'm/n' would be negative non-integer? */ 592 q -= 1; /* correct result for different rounding */ 593 return q; 594 } 595 } 596 597 598 /* 599 ** Integer modulus; return 'm % n'. (Assume that C '%' with 600 ** negative operands follows C99 behavior. See previous comment 601 ** about luaV_div.) 602 */ 603 lua_Integer luaV_mod (lua_State *L, lua_Integer m, lua_Integer n) { 604 if (l_castS2U(n) + 1u <= 1u) { /* special cases: -1 or 0 */ 605 if (n == 0) 606 luaG_runerror(L, "attempt to perform 'n%%0'"); 607 return 0; /* m % -1 == 0; avoid overflow with 0x80000...%-1 */ 608 } 609 else { 610 lua_Integer r = m % n; 611 if (r != 0 && (m ^ n) < 0) /* 'm/n' would be non-integer negative? */ 612 r += n; /* correct result for different rounding */ 613 return r; 614 } 615 } 616 617 618 /* number of bits in an integer */ 619 #define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) 620 621 /* 622 ** Shift left operation. (Shift right just negates 'y'.) 623 */ 624 lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { 625 if (y < 0) { /* shift right? */ 626 if (y <= -NBITS) return 0; 627 else return intop(>>, x, -y); 628 } 629 else { /* shift left */ 630 if (y >= NBITS) return 0; 631 else return intop(<<, x, y); 632 } 633 } 634 635 636 /* 637 ** check whether cached closure in prototype 'p' may be reused, that is, 638 ** whether there is a cached closure with the same upvalues needed by 639 ** new closure to be created. 640 */ 641 static LClosure *getcached (Proto *p, UpVal **encup, StkId base) { 642 LClosure *c = p->cache; 643 if (c != NULL) { /* is there a cached closure? */ 644 int nup = p->sizeupvalues; 645 Upvaldesc *uv = p->upvalues; 646 int i; 647 for (i = 0; i < nup; i++) { /* check whether it has right upvalues */ 648 TValue *v = uv[i].instack ? base + uv[i].idx : encup[uv[i].idx]->v; 649 if (c->upvals[i]->v != v) 650 return NULL; /* wrong upvalue; cannot reuse closure */ 651 } 652 } 653 return c; /* return cached closure (or NULL if no cached closure) */ 654 } 655 656 657 /* 658 ** create a new Lua closure, push it in the stack, and initialize 659 ** its upvalues. Note that the closure is not cached if prototype is 660 ** already black (which means that 'cache' was already cleared by the 661 ** GC). 662 */ 663 static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, 664 StkId ra) { 665 int nup = p->sizeupvalues; 666 Upvaldesc *uv = p->upvalues; 667 int i; 668 LClosure *ncl = luaF_newLclosure(L, nup); 669 ncl->p = p; 670 setclLvalue(L, ra, ncl); /* anchor new closure in stack */ 671 for (i = 0; i < nup; i++) { /* fill in its upvalues */ 672 if (uv[i].instack) /* upvalue refers to local variable? */ 673 ncl->upvals[i] = luaF_findupval(L, base + uv[i].idx); 674 else /* get upvalue from enclosing function */ 675 ncl->upvals[i] = encup[uv[i].idx]; 676 ncl->upvals[i]->refcount++; 677 /* new closure is white, so we do not need a barrier here */ 678 } 679 if (!isblack(p)) /* cache will not break GC invariant? */ 680 p->cache = ncl; /* save it on cache for reuse */ 681 } 682 683 684 /* 685 ** finish execution of an opcode interrupted by an yield 686 */ 687 void luaV_finishOp (lua_State *L) { 688 CallInfo *ci = L->ci; 689 StkId base = ci->u.l.base; 690 Instruction inst = *(ci->u.l.savedpc - 1); /* interrupted instruction */ 691 OpCode op = GET_OPCODE(inst); 692 switch (op) { /* finish its execution */ 693 #ifndef _KERNEL 694 case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV: case OP_IDIV: 695 #else /* _KERNEL */ 696 case OP_ADD: case OP_SUB: case OP_MUL: case OP_IDIV: 697 #endif /* _KERNEL */ 698 case OP_BAND: case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: 699 #ifndef _KERNEL 700 case OP_MOD: case OP_POW: 701 #else /* _KERNEL */ 702 case OP_MOD: 703 #endif /* _KERNEL */ 704 case OP_UNM: case OP_BNOT: case OP_LEN: 705 case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: { 706 setobjs2s(L, base + GETARG_A(inst), --L->top); 707 break; 708 } 709 case OP_LE: case OP_LT: case OP_EQ: { 710 int res = !l_isfalse(L->top - 1); 711 L->top--; 712 if (ci->callstatus & CIST_LEQ) { /* "<=" using "<" instead? */ 713 lua_assert(op == OP_LE); 714 ci->callstatus ^= CIST_LEQ; /* clear mark */ 715 res = !res; /* negate result */ 716 } 717 lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP); 718 if (res != GETARG_A(inst)) /* condition failed? */ 719 ci->u.l.savedpc++; /* skip jump instruction */ 720 break; 721 } 722 case OP_CONCAT: { 723 StkId top = L->top - 1; /* top when 'luaT_trybinTM' was called */ 724 int b = GETARG_B(inst); /* first element to concatenate */ 725 int total = cast_int(top - 1 - (base + b)); /* yet to concatenate */ 726 setobj2s(L, top - 2, top); /* put TM result in proper position */ 727 if (total > 1) { /* are there elements to concat? */ 728 L->top = top - 1; /* top is one after last element (at top-2) */ 729 luaV_concat(L, total); /* concat them (may yield again) */ 730 } 731 /* move final result to final position */ 732 setobj2s(L, ci->u.l.base + GETARG_A(inst), L->top - 1); 733 L->top = ci->top; /* restore top */ 734 break; 735 } 736 case OP_TFORCALL: { 737 lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_TFORLOOP); 738 L->top = ci->top; /* correct top */ 739 break; 740 } 741 case OP_CALL: { 742 if (GETARG_C(inst) - 1 >= 0) /* nresults >= 0? */ 743 L->top = ci->top; /* adjust results */ 744 break; 745 } 746 case OP_TAILCALL: case OP_SETTABUP: case OP_SETTABLE: 747 break; 748 default: lua_assert(0); 749 } 750 } 751 752 753 754 755 /* 756 ** {================================================================== 757 ** Function 'luaV_execute': main interpreter loop 758 ** =================================================================== 759 */ 760 761 762 /* 763 ** some macros for common tasks in 'luaV_execute' 764 */ 765 766 767 #define RA(i) (base+GETARG_A(i)) 768 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) 769 #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) 770 #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ 771 ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) 772 #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ 773 ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) 774 775 776 /* execute a jump instruction */ 777 #define dojump(ci,i,e) \ 778 { int a = GETARG_A(i); \ 779 if (a != 0) luaF_close(L, ci->u.l.base + a - 1); \ 780 ci->u.l.savedpc += GETARG_sBx(i) + e; } 781 782 /* for test instructions, execute the jump instruction that follows it */ 783 #define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); } 784 785 786 #define Protect(x) { {x;}; base = ci->u.l.base; } 787 788 #define checkGC(L,c) \ 789 { luaC_condGC(L, L->top = (c), /* limit of live values */ \ 790 Protect(L->top = ci->top)); /* restore top */ \ 791 luai_threadyield(L); } 792 793 794 /* fetch an instruction and prepare its execution */ 795 #define vmfetch() { \ 796 i = *(ci->u.l.savedpc++); \ 797 if (L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) \ 798 Protect(luaG_traceexec(L)); \ 799 ra = RA(i); /* WARNING: any stack reallocation invalidates 'ra' */ \ 800 lua_assert(base == ci->u.l.base); \ 801 lua_assert(base <= L->top && L->top < L->stack + L->stacksize); \ 802 } 803 804 #define vmdispatch(o) switch(o) 805 #define vmcase(l) case l: 806 #define vmbreak break 807 808 809 /* 810 ** copy of 'luaV_gettable', but protecting the call to potential 811 ** metamethod (which can reallocate the stack) 812 */ 813 #define gettableProtected(L,t,k,v) { const TValue *slot; \ 814 if (luaV_fastget(L,t,k,slot,luaH_get)) { setobj2s(L, v, slot); } \ 815 else Protect(luaV_finishget(L,t,k,v,slot)); } 816 817 818 /* same for 'luaV_settable' */ 819 #define settableProtected(L,t,k,v) { const TValue *slot; \ 820 if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ 821 Protect(luaV_finishset(L,t,k,v,slot)); } 822 823 824 825 void luaV_execute (lua_State *L) { 826 CallInfo *ci = L->ci; 827 LClosure *cl; 828 TValue *k; 829 StkId base; 830 ci->callstatus |= CIST_FRESH; /* fresh invocation of 'luaV_execute" */ 831 newframe: /* reentry point when frame changes (call/return) */ 832 lua_assert(ci == L->ci); 833 cl = clLvalue(ci->func); /* local reference to function's closure */ 834 k = cl->p->k; /* local reference to function's constant table */ 835 base = ci->u.l.base; /* local copy of function's base */ 836 /* main loop of interpreter */ 837 for (;;) { 838 Instruction i; 839 StkId ra; 840 vmfetch(); 841 vmdispatch (GET_OPCODE(i)) { 842 vmcase(OP_MOVE) { 843 setobjs2s(L, ra, RB(i)); 844 vmbreak; 845 } 846 vmcase(OP_LOADK) { 847 TValue *rb = k + GETARG_Bx(i); 848 setobj2s(L, ra, rb); 849 vmbreak; 850 } 851 vmcase(OP_LOADKX) { 852 TValue *rb; 853 lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); 854 rb = k + GETARG_Ax(*ci->u.l.savedpc++); 855 setobj2s(L, ra, rb); 856 vmbreak; 857 } 858 vmcase(OP_LOADBOOL) { 859 setbvalue(ra, GETARG_B(i)); 860 if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */ 861 vmbreak; 862 } 863 vmcase(OP_LOADNIL) { 864 int b = GETARG_B(i); 865 do { 866 setnilvalue(ra++); 867 } while (b--); 868 vmbreak; 869 } 870 vmcase(OP_GETUPVAL) { 871 int b = GETARG_B(i); 872 setobj2s(L, ra, cl->upvals[b]->v); 873 vmbreak; 874 } 875 vmcase(OP_GETTABUP) { 876 TValue *upval = cl->upvals[GETARG_B(i)]->v; 877 TValue *rc = RKC(i); 878 gettableProtected(L, upval, rc, ra); 879 vmbreak; 880 } 881 vmcase(OP_GETTABLE) { 882 StkId rb = RB(i); 883 TValue *rc = RKC(i); 884 gettableProtected(L, rb, rc, ra); 885 vmbreak; 886 } 887 vmcase(OP_SETTABUP) { 888 TValue *upval = cl->upvals[GETARG_A(i)]->v; 889 TValue *rb = RKB(i); 890 TValue *rc = RKC(i); 891 settableProtected(L, upval, rb, rc); 892 vmbreak; 893 } 894 vmcase(OP_SETUPVAL) { 895 UpVal *uv = cl->upvals[GETARG_B(i)]; 896 setobj(L, uv->v, ra); 897 luaC_upvalbarrier(L, uv); 898 vmbreak; 899 } 900 vmcase(OP_SETTABLE) { 901 TValue *rb = RKB(i); 902 TValue *rc = RKC(i); 903 settableProtected(L, ra, rb, rc); 904 vmbreak; 905 } 906 vmcase(OP_NEWTABLE) { 907 int b = GETARG_B(i); 908 int c = GETARG_C(i); 909 Table *t = luaH_new(L); 910 sethvalue(L, ra, t); 911 if (b != 0 || c != 0) 912 luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c)); 913 checkGC(L, ra + 1); 914 vmbreak; 915 } 916 vmcase(OP_SELF) { 917 const TValue *aux; 918 StkId rb = RB(i); 919 TValue *rc = RKC(i); 920 TString *key = tsvalue(rc); /* key must be a string */ 921 setobjs2s(L, ra + 1, rb); 922 if (luaV_fastget(L, rb, key, aux, luaH_getstr)) { 923 setobj2s(L, ra, aux); 924 } 925 else Protect(luaV_finishget(L, rb, rc, ra, aux)); 926 vmbreak; 927 } 928 vmcase(OP_ADD) { 929 TValue *rb = RKB(i); 930 TValue *rc = RKC(i); 931 #ifndef _KERNEL 932 lua_Number nb; lua_Number nc; 933 if (ttisinteger(rb) && ttisinteger(rc)) { 934 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 935 setivalue(ra, intop(+, ib, ic)); 936 } 937 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { 938 setfltvalue(ra, luai_numadd(L, nb, nc)); 939 } 940 #else /* _KERNEL */ 941 lua_Integer ib; lua_Integer ic; 942 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 943 setivalue(ra, intop(+, ib, ic)); 944 } 945 #endif /* _KERNEL */ 946 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); } 947 vmbreak; 948 } 949 vmcase(OP_SUB) { 950 TValue *rb = RKB(i); 951 TValue *rc = RKC(i); 952 #ifndef _KERNEL 953 lua_Number nb; lua_Number nc; 954 if (ttisinteger(rb) && ttisinteger(rc)) { 955 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 956 setivalue(ra, intop(-, ib, ic)); 957 } 958 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { 959 setfltvalue(ra, luai_numsub(L, nb, nc)); 960 } 961 #else /* _KERNEL */ 962 lua_Integer ib; lua_Integer ic; 963 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 964 setivalue(ra, intop(-, ib, ic)); 965 } 966 #endif /* _KERNEL */ 967 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SUB)); } 968 vmbreak; 969 } 970 vmcase(OP_MUL) { 971 TValue *rb = RKB(i); 972 TValue *rc = RKC(i); 973 #ifndef _KERNEL 974 lua_Number nb; lua_Number nc; 975 if (ttisinteger(rb) && ttisinteger(rc)) { 976 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 977 setivalue(ra, intop(*, ib, ic)); 978 } 979 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { 980 setfltvalue(ra, luai_nummul(L, nb, nc)); 981 } 982 #else /* _KERNEL */ 983 lua_Integer ib; lua_Integer ic; 984 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 985 setivalue(ra, intop(*, ib, ic)); 986 } 987 #endif /* _KERNEL */ 988 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MUL)); } 989 vmbreak; 990 } 991 #ifndef _KERNEL 992 vmcase(OP_DIV) { /* float division (always with floats) */ 993 TValue *rb = RKB(i); 994 TValue *rc = RKC(i); 995 lua_Number nb; lua_Number nc; 996 if (tonumber(rb, &nb) && tonumber(rc, &nc)) { 997 setfltvalue(ra, luai_numdiv(L, nb, nc)); 998 } 999 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_DIV)); } 1000 vmbreak; 1001 } 1002 #endif /* _KERNEL */ 1003 vmcase(OP_BAND) { 1004 TValue *rb = RKB(i); 1005 TValue *rc = RKC(i); 1006 lua_Integer ib; lua_Integer ic; 1007 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 1008 setivalue(ra, intop(&, ib, ic)); 1009 } 1010 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BAND)); } 1011 vmbreak; 1012 } 1013 vmcase(OP_BOR) { 1014 TValue *rb = RKB(i); 1015 TValue *rc = RKC(i); 1016 lua_Integer ib; lua_Integer ic; 1017 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 1018 setivalue(ra, intop(|, ib, ic)); 1019 } 1020 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BOR)); } 1021 vmbreak; 1022 } 1023 vmcase(OP_BXOR) { 1024 TValue *rb = RKB(i); 1025 TValue *rc = RKC(i); 1026 lua_Integer ib; lua_Integer ic; 1027 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 1028 setivalue(ra, intop(^, ib, ic)); 1029 } 1030 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_BXOR)); } 1031 vmbreak; 1032 } 1033 vmcase(OP_SHL) { 1034 TValue *rb = RKB(i); 1035 TValue *rc = RKC(i); 1036 lua_Integer ib; lua_Integer ic; 1037 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 1038 setivalue(ra, luaV_shiftl(ib, ic)); 1039 } 1040 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHL)); } 1041 vmbreak; 1042 } 1043 vmcase(OP_SHR) { 1044 TValue *rb = RKB(i); 1045 TValue *rc = RKC(i); 1046 lua_Integer ib; lua_Integer ic; 1047 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 1048 setivalue(ra, luaV_shiftl(ib, -ic)); 1049 } 1050 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_SHR)); } 1051 vmbreak; 1052 } 1053 vmcase(OP_MOD) { 1054 TValue *rb = RKB(i); 1055 TValue *rc = RKC(i); 1056 #ifndef _KERNEL 1057 lua_Number nb; lua_Number nc; 1058 if (ttisinteger(rb) && ttisinteger(rc)) { 1059 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 1060 setivalue(ra, luaV_mod(L, ib, ic)); 1061 } 1062 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { 1063 lua_Number m; 1064 luai_nummod(L, nb, nc, m); 1065 setfltvalue(ra, m); 1066 } 1067 #else /* _KERNEL */ 1068 lua_Integer ib; lua_Integer ic; 1069 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 1070 setivalue(ra, luaV_mod(L, ib, ic)); 1071 } 1072 #endif /* _KERNEL */ 1073 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_MOD)); } 1074 vmbreak; 1075 } 1076 vmcase(OP_IDIV) { /* floor division */ 1077 TValue *rb = RKB(i); 1078 TValue *rc = RKC(i); 1079 #ifndef _KERNEL 1080 lua_Number nb; lua_Number nc; 1081 if (ttisinteger(rb) && ttisinteger(rc)) { 1082 lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc); 1083 setivalue(ra, luaV_div(L, ib, ic)); 1084 } 1085 else if (tonumber(rb, &nb) && tonumber(rc, &nc)) { 1086 setfltvalue(ra, luai_numidiv(L, nb, nc)); 1087 } 1088 #else /* _KERNEL */ 1089 lua_Integer ib; lua_Integer ic; 1090 if (tointeger(rb, &ib) && tointeger(rc, &ic)) { 1091 setivalue(ra, luaV_div(L, ib, ic)); 1092 } 1093 #endif /* _KERNEL */ 1094 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_IDIV)); } 1095 vmbreak; 1096 } 1097 #ifndef _KERNEL 1098 vmcase(OP_POW) { 1099 TValue *rb = RKB(i); 1100 TValue *rc = RKC(i); 1101 lua_Number nb; lua_Number nc; 1102 if (tonumber(rb, &nb) && tonumber(rc, &nc)) { 1103 setfltvalue(ra, luai_numpow(L, nb, nc)); 1104 } 1105 else { Protect(luaT_trybinTM(L, rb, rc, ra, TM_POW)); } 1106 vmbreak; 1107 } 1108 #endif /* _KERNEL */ 1109 vmcase(OP_UNM) { 1110 TValue *rb = RB(i); 1111 #ifndef _KERNEL 1112 lua_Number nb; 1113 if (ttisinteger(rb)) { 1114 lua_Integer ib = ivalue(rb); 1115 setivalue(ra, intop(-, 0, ib)); 1116 } 1117 else if (tonumber(rb, &nb)) { 1118 setfltvalue(ra, luai_numunm(L, nb)); 1119 } 1120 #else /* _KERNEL */ 1121 lua_Integer ib; 1122 if (tointeger(rb, &ib)) { 1123 setivalue(ra, intop(-, 0, ib)); 1124 } 1125 #endif /* _KERNEL */ 1126 else { 1127 Protect(luaT_trybinTM(L, rb, rb, ra, TM_UNM)); 1128 } 1129 vmbreak; 1130 } 1131 vmcase(OP_BNOT) { 1132 TValue *rb = RB(i); 1133 lua_Integer ib; 1134 if (tointeger(rb, &ib)) { 1135 setivalue(ra, intop(^, ~l_castS2U(0), ib)); 1136 } 1137 else { 1138 Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); 1139 } 1140 vmbreak; 1141 } 1142 vmcase(OP_NOT) { 1143 TValue *rb = RB(i); 1144 int res = l_isfalse(rb); /* next assignment may change this value */ 1145 setbvalue(ra, res); 1146 vmbreak; 1147 } 1148 vmcase(OP_LEN) { 1149 Protect(luaV_objlen(L, ra, RB(i))); 1150 vmbreak; 1151 } 1152 vmcase(OP_CONCAT) { 1153 int b = GETARG_B(i); 1154 int c = GETARG_C(i); 1155 StkId rb; 1156 L->top = base + c + 1; /* mark the end of concat operands */ 1157 Protect(luaV_concat(L, c - b + 1)); 1158 ra = RA(i); /* 'luaV_concat' may invoke TMs and move the stack */ 1159 rb = base + b; 1160 setobjs2s(L, ra, rb); 1161 checkGC(L, (ra >= rb ? ra + 1 : rb)); 1162 L->top = ci->top; /* restore top */ 1163 vmbreak; 1164 } 1165 vmcase(OP_JMP) { 1166 dojump(ci, i, 0); 1167 vmbreak; 1168 } 1169 vmcase(OP_EQ) { 1170 TValue *rb = RKB(i); 1171 TValue *rc = RKC(i); 1172 Protect( 1173 if (luaV_equalobj(L, rb, rc) != GETARG_A(i)) 1174 ci->u.l.savedpc++; 1175 else 1176 donextjump(ci); 1177 ) 1178 vmbreak; 1179 } 1180 vmcase(OP_LT) { 1181 Protect( 1182 if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i)) 1183 ci->u.l.savedpc++; 1184 else 1185 donextjump(ci); 1186 ) 1187 vmbreak; 1188 } 1189 vmcase(OP_LE) { 1190 Protect( 1191 if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i)) 1192 ci->u.l.savedpc++; 1193 else 1194 donextjump(ci); 1195 ) 1196 vmbreak; 1197 } 1198 vmcase(OP_TEST) { 1199 if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra)) 1200 ci->u.l.savedpc++; 1201 else 1202 donextjump(ci); 1203 vmbreak; 1204 } 1205 vmcase(OP_TESTSET) { 1206 TValue *rb = RB(i); 1207 if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb)) 1208 ci->u.l.savedpc++; 1209 else { 1210 setobjs2s(L, ra, rb); 1211 donextjump(ci); 1212 } 1213 vmbreak; 1214 } 1215 vmcase(OP_CALL) { 1216 int b = GETARG_B(i); 1217 int nresults = GETARG_C(i) - 1; 1218 if (b != 0) L->top = ra+b; /* else previous instruction set top */ 1219 if (luaD_precall(L, ra, nresults)) { /* C function? */ 1220 if (nresults >= 0) 1221 L->top = ci->top; /* adjust results */ 1222 Protect((void)0); /* update 'base' */ 1223 } 1224 else { /* Lua function */ 1225 ci = L->ci; 1226 goto newframe; /* restart luaV_execute over new Lua function */ 1227 } 1228 vmbreak; 1229 } 1230 vmcase(OP_TAILCALL) { 1231 int b = GETARG_B(i); 1232 if (b != 0) L->top = ra+b; /* else previous instruction set top */ 1233 lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); 1234 if (luaD_precall(L, ra, LUA_MULTRET)) { /* C function? */ 1235 Protect((void)0); /* update 'base' */ 1236 } 1237 else { 1238 /* tail call: put called frame (n) in place of caller one (o) */ 1239 CallInfo *nci = L->ci; /* called frame */ 1240 CallInfo *oci = nci->previous; /* caller frame */ 1241 StkId nfunc = nci->func; /* called function */ 1242 StkId ofunc = oci->func; /* caller function */ 1243 /* last stack slot filled by 'precall' */ 1244 StkId lim = nci->u.l.base + getproto(nfunc)->numparams; 1245 int aux; 1246 /* close all upvalues from previous call */ 1247 if (cl->p->sizep > 0) luaF_close(L, oci->u.l.base); 1248 /* move new frame into old one */ 1249 for (aux = 0; nfunc + aux < lim; aux++) 1250 setobjs2s(L, ofunc + aux, nfunc + aux); 1251 oci->u.l.base = ofunc + (nci->u.l.base - nfunc); /* correct base */ 1252 oci->top = L->top = ofunc + (L->top - nfunc); /* correct top */ 1253 oci->u.l.savedpc = nci->u.l.savedpc; 1254 oci->callstatus |= CIST_TAIL; /* function was tail called */ 1255 ci = L->ci = oci; /* remove new frame */ 1256 lua_assert(L->top == oci->u.l.base + getproto(ofunc)->maxstacksize); 1257 goto newframe; /* restart luaV_execute over new Lua function */ 1258 } 1259 vmbreak; 1260 } 1261 vmcase(OP_RETURN) { 1262 int b = GETARG_B(i); 1263 if (cl->p->sizep > 0) luaF_close(L, base); 1264 b = luaD_poscall(L, ci, ra, (b != 0 ? b - 1 : cast_int(L->top - ra))); 1265 if (ci->callstatus & CIST_FRESH) /* local 'ci' still from callee */ 1266 return; /* external invocation: return */ 1267 else { /* invocation via reentry: continue execution */ 1268 ci = L->ci; 1269 if (b) L->top = ci->top; 1270 lua_assert(isLua(ci)); 1271 lua_assert(GET_OPCODE(*((ci)->u.l.savedpc - 1)) == OP_CALL); 1272 goto newframe; /* restart luaV_execute over new Lua function */ 1273 } 1274 } 1275 vmcase(OP_FORLOOP) { 1276 #ifndef _KERNEL 1277 if (ttisinteger(ra)) { /* integer loop? */ 1278 #endif /* _KERNEL */ 1279 lua_Integer step = ivalue(ra + 2); 1280 lua_Integer idx = intop(+, ivalue(ra), step); /* increment index */ 1281 lua_Integer limit = ivalue(ra + 1); 1282 if ((0 < step) ? (idx <= limit) : (limit <= idx)) { 1283 ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ 1284 chgivalue(ra, idx); /* update internal index... */ 1285 setivalue(ra + 3, idx); /* ...and external index */ 1286 } 1287 #ifndef _KERNEL 1288 } 1289 else { /* floating loop */ 1290 lua_Number step = fltvalue(ra + 2); 1291 lua_Number idx = luai_numadd(L, fltvalue(ra), step); /* inc. index */ 1292 lua_Number limit = fltvalue(ra + 1); 1293 if (luai_numlt(0, step) ? luai_numle(idx, limit) 1294 : luai_numle(limit, idx)) { 1295 ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ 1296 chgfltvalue(ra, idx); /* update internal index... */ 1297 setfltvalue(ra + 3, idx); /* ...and external index */ 1298 } 1299 } 1300 #endif /* _KERNEL */ 1301 vmbreak; 1302 } 1303 vmcase(OP_FORPREP) { 1304 TValue *init = ra; 1305 TValue *plimit = ra + 1; 1306 TValue *pstep = ra + 2; 1307 lua_Integer ilimit; 1308 #ifndef _KERNEL 1309 int stopnow; 1310 if (ttisinteger(init) && ttisinteger(pstep) && 1311 forlimit(plimit, &ilimit, ivalue(pstep), &stopnow)) { 1312 /* all values are integer */ 1313 lua_Integer initv = (stopnow ? 0 : ivalue(init)); 1314 setivalue(plimit, ilimit); 1315 setivalue(init, intop(-, initv, ivalue(pstep))); 1316 } 1317 else { /* try making all values floats */ 1318 lua_Number ninit; lua_Number nlimit; lua_Number nstep; 1319 if (!tonumber(plimit, &nlimit)) 1320 luaG_runerror(L, "'for' limit must be a number"); 1321 setfltvalue(plimit, nlimit); 1322 if (!tonumber(pstep, &nstep)) 1323 luaG_runerror(L, "'for' step must be a number"); 1324 setfltvalue(pstep, nstep); 1325 if (!tonumber(init, &ninit)) 1326 luaG_runerror(L, "'for' initial value must be a number"); 1327 setfltvalue(init, luai_numsub(L, ninit, nstep)); 1328 } 1329 #else /* _KERNEL */ 1330 lua_Integer initv; lua_Integer step; 1331 if (!tointeger(plimit, &ilimit)) 1332 luaG_runerror(L, "'for' limit must be a number"); 1333 setivalue(plimit, ilimit); 1334 if (!tointeger(pstep, &step)) 1335 luaG_runerror(L, "'for' step must be a number"); 1336 setivalue(pstep, step); 1337 if (!tointeger(init, &initv)) 1338 luaG_runerror(L, "'for' initial value must be a number"); 1339 setivalue(init, initv - step); 1340 #endif /* _KERNEL */ 1341 ci->u.l.savedpc += GETARG_sBx(i); 1342 vmbreak; 1343 } 1344 vmcase(OP_TFORCALL) { 1345 StkId cb = ra + 3; /* call base */ 1346 setobjs2s(L, cb+2, ra+2); 1347 setobjs2s(L, cb+1, ra+1); 1348 setobjs2s(L, cb, ra); 1349 L->top = cb + 3; /* func. + 2 args (state and index) */ 1350 Protect(luaD_call(L, cb, GETARG_C(i))); 1351 L->top = ci->top; 1352 i = *(ci->u.l.savedpc++); /* go to next instruction */ 1353 ra = RA(i); 1354 lua_assert(GET_OPCODE(i) == OP_TFORLOOP); 1355 goto l_tforloop; 1356 } 1357 vmcase(OP_TFORLOOP) { 1358 l_tforloop: 1359 if (!ttisnil(ra + 1)) { /* continue loop? */ 1360 setobjs2s(L, ra, ra + 1); /* save control variable */ 1361 ci->u.l.savedpc += GETARG_sBx(i); /* jump back */ 1362 } 1363 vmbreak; 1364 } 1365 vmcase(OP_SETLIST) { 1366 int n = GETARG_B(i); 1367 int c = GETARG_C(i); 1368 unsigned int last; 1369 Table *h; 1370 if (n == 0) n = cast_int(L->top - ra) - 1; 1371 if (c == 0) { 1372 lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG); 1373 c = GETARG_Ax(*ci->u.l.savedpc++); 1374 } 1375 h = hvalue(ra); 1376 last = ((c-1)*LFIELDS_PER_FLUSH) + n; 1377 if (last > h->sizearray) /* needs more space? */ 1378 luaH_resizearray(L, h, last); /* preallocate it at once */ 1379 for (; n > 0; n--) { 1380 TValue *val = ra+n; 1381 luaH_setint(L, h, last--, val); 1382 luaC_barrierback(L, h, val); 1383 } 1384 L->top = ci->top; /* correct top (in case of previous open call) */ 1385 vmbreak; 1386 } 1387 vmcase(OP_CLOSURE) { 1388 Proto *p = cl->p->p[GETARG_Bx(i)]; 1389 LClosure *ncl = getcached(p, cl->upvals, base); /* cached closure */ 1390 if (ncl == NULL) /* no match? */ 1391 pushclosure(L, p, cl->upvals, base, ra); /* create a new one */ 1392 else 1393 setclLvalue(L, ra, ncl); /* push cashed closure */ 1394 checkGC(L, ra + 1); 1395 vmbreak; 1396 } 1397 vmcase(OP_VARARG) { 1398 int b = GETARG_B(i) - 1; /* required results */ 1399 int j; 1400 int n = cast_int(base - ci->func) - cl->p->numparams - 1; 1401 if (n < 0) /* less arguments than parameters? */ 1402 n = 0; /* no vararg arguments */ 1403 if (b < 0) { /* B == 0? */ 1404 b = n; /* get all var. arguments */ 1405 Protect(luaD_checkstack(L, n)); 1406 ra = RA(i); /* previous call may change the stack */ 1407 L->top = ra + n; 1408 } 1409 for (j = 0; j < b && j < n; j++) 1410 setobjs2s(L, ra + j, base - n + j); 1411 for (; j < b; j++) /* complete required results with nil */ 1412 setnilvalue(ra + j); 1413 vmbreak; 1414 } 1415 vmcase(OP_EXTRAARG) { 1416 lua_assert(0); 1417 vmbreak; 1418 } 1419 } 1420 } 1421 } 1422 1423 /* }================================================================== */ 1424 1425