1 /* $NetBSD: lcode.c,v 1.4 2015/10/08 13:21:00 mbalmer Exp $ */ 2 3 /* 4 ** Id: lcode.c,v 2.101 2015/04/29 18:24:11 roberto Exp 5 ** Code generator for Lua 6 ** See Copyright Notice in lua.h 7 */ 8 9 #define lcode_c 10 #define LUA_CORE 11 12 #include "lprefix.h" 13 14 15 #ifndef _KERNEL 16 #include <math.h> 17 #include <stdlib.h> 18 #endif 19 20 #include "lua.h" 21 22 #include "lcode.h" 23 #include "ldebug.h" 24 #include "ldo.h" 25 #include "lgc.h" 26 #include "llex.h" 27 #include "lmem.h" 28 #include "lobject.h" 29 #include "lopcodes.h" 30 #include "lparser.h" 31 #include "lstring.h" 32 #include "ltable.h" 33 #include "lvm.h" 34 35 36 /* Maximum number of registers in a Lua function (must fit in 8 bits) */ 37 #define MAXREGS 255 38 39 40 #define hasjumps(e) ((e)->t != (e)->f) 41 42 43 static int tonumeral(expdesc *e, TValue *v) { 44 if (e->t != NO_JUMP || e->f != NO_JUMP) 45 return 0; /* not a numeral */ 46 switch (e->k) { 47 case VKINT: 48 if (v) setivalue(v, e->u.ival); 49 return 1; 50 #ifndef _KERNEL 51 case VKFLT: 52 if (v) setfltvalue(v, e->u.nval); 53 return 1; 54 #endif 55 default: return 0; 56 } 57 } 58 59 60 void luaK_nil (FuncState *fs, int from, int n) { 61 Instruction *previous; 62 int l = from + n - 1; /* last register to set nil */ 63 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ 64 previous = &fs->f->code[fs->pc-1]; 65 if (GET_OPCODE(*previous) == OP_LOADNIL) { 66 int pfrom = GETARG_A(*previous); 67 int pl = pfrom + GETARG_B(*previous); 68 if ((pfrom <= from && from <= pl + 1) || 69 (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ 70 if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ 71 if (pl > l) l = pl; /* l = max(l, pl) */ 72 SETARG_A(*previous, from); 73 SETARG_B(*previous, l - from); 74 return; 75 } 76 } /* else go through */ 77 } 78 luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ 79 } 80 81 82 int luaK_jump (FuncState *fs) { 83 int jpc = fs->jpc; /* save list of jumps to here */ 84 int j; 85 fs->jpc = NO_JUMP; 86 j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); 87 luaK_concat(fs, &j, jpc); /* keep them on hold */ 88 return j; 89 } 90 91 92 void luaK_ret (FuncState *fs, int first, int nret) { 93 luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); 94 } 95 96 97 static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { 98 luaK_codeABC(fs, op, A, B, C); 99 return luaK_jump(fs); 100 } 101 102 103 static void fixjump (FuncState *fs, int pc, int dest) { 104 Instruction *jmp = &fs->f->code[pc]; 105 int offset = dest-(pc+1); 106 lua_assert(dest != NO_JUMP); 107 if (abs(offset) > MAXARG_sBx) 108 luaX_syntaxerror(fs->ls, "control structure too long"); 109 SETARG_sBx(*jmp, offset); 110 } 111 112 113 /* 114 ** returns current 'pc' and marks it as a jump target (to avoid wrong 115 ** optimizations with consecutive instructions not in the same basic block). 116 */ 117 int luaK_getlabel (FuncState *fs) { 118 fs->lasttarget = fs->pc; 119 return fs->pc; 120 } 121 122 123 static int getjump (FuncState *fs, int pc) { 124 int offset = GETARG_sBx(fs->f->code[pc]); 125 if (offset == NO_JUMP) /* point to itself represents end of list */ 126 return NO_JUMP; /* end of list */ 127 else 128 return (pc+1)+offset; /* turn offset into absolute position */ 129 } 130 131 132 static Instruction *getjumpcontrol (FuncState *fs, int pc) { 133 Instruction *pi = &fs->f->code[pc]; 134 if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) 135 return pi-1; 136 else 137 return pi; 138 } 139 140 141 /* 142 ** check whether list has any jump that do not produce a value 143 ** (or produce an inverted value) 144 */ 145 static int need_value (FuncState *fs, int list) { 146 for (; list != NO_JUMP; list = getjump(fs, list)) { 147 Instruction i = *getjumpcontrol(fs, list); 148 if (GET_OPCODE(i) != OP_TESTSET) return 1; 149 } 150 return 0; /* not found */ 151 } 152 153 154 static int patchtestreg (FuncState *fs, int node, int reg) { 155 Instruction *i = getjumpcontrol(fs, node); 156 if (GET_OPCODE(*i) != OP_TESTSET) 157 return 0; /* cannot patch other instructions */ 158 if (reg != NO_REG && reg != GETARG_B(*i)) 159 SETARG_A(*i, reg); 160 else /* no register to put value or register already has the value */ 161 *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); 162 163 return 1; 164 } 165 166 167 static void removevalues (FuncState *fs, int list) { 168 for (; list != NO_JUMP; list = getjump(fs, list)) 169 patchtestreg(fs, list, NO_REG); 170 } 171 172 173 static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, 174 int dtarget) { 175 while (list != NO_JUMP) { 176 int next = getjump(fs, list); 177 if (patchtestreg(fs, list, reg)) 178 fixjump(fs, list, vtarget); 179 else 180 fixjump(fs, list, dtarget); /* jump to default target */ 181 list = next; 182 } 183 } 184 185 186 static void dischargejpc (FuncState *fs) { 187 patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); 188 fs->jpc = NO_JUMP; 189 } 190 191 192 void luaK_patchlist (FuncState *fs, int list, int target) { 193 if (target == fs->pc) 194 luaK_patchtohere(fs, list); 195 else { 196 lua_assert(target < fs->pc); 197 patchlistaux(fs, list, target, NO_REG, target); 198 } 199 } 200 201 202 void luaK_patchclose (FuncState *fs, int list, int level) { 203 level++; /* argument is +1 to reserve 0 as non-op */ 204 while (list != NO_JUMP) { 205 int next = getjump(fs, list); 206 lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP && 207 (GETARG_A(fs->f->code[list]) == 0 || 208 GETARG_A(fs->f->code[list]) >= level)); 209 SETARG_A(fs->f->code[list], level); 210 list = next; 211 } 212 } 213 214 215 void luaK_patchtohere (FuncState *fs, int list) { 216 luaK_getlabel(fs); 217 luaK_concat(fs, &fs->jpc, list); 218 } 219 220 221 void luaK_concat (FuncState *fs, int *l1, int l2) { 222 if (l2 == NO_JUMP) return; 223 else if (*l1 == NO_JUMP) 224 *l1 = l2; 225 else { 226 int list = *l1; 227 int next; 228 while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ 229 list = next; 230 fixjump(fs, list, l2); 231 } 232 } 233 234 235 static int luaK_code (FuncState *fs, Instruction i) { 236 Proto *f = fs->f; 237 dischargejpc(fs); /* 'pc' will change */ 238 /* put new instruction in code array */ 239 luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction, 240 MAX_INT, "opcodes"); 241 f->code[fs->pc] = i; 242 /* save corresponding line information */ 243 luaM_growvector(fs->ls->L, f->lineinfo, fs->pc, f->sizelineinfo, int, 244 MAX_INT, "opcodes"); 245 f->lineinfo[fs->pc] = fs->ls->lastline; 246 return fs->pc++; 247 } 248 249 250 int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { 251 lua_assert(getOpMode(o) == iABC); 252 lua_assert(getBMode(o) != OpArgN || b == 0); 253 lua_assert(getCMode(o) != OpArgN || c == 0); 254 lua_assert(a <= MAXARG_A && b <= MAXARG_B && c <= MAXARG_C); 255 return luaK_code(fs, CREATE_ABC(o, a, b, c)); 256 } 257 258 259 int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { 260 lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); 261 lua_assert(getCMode(o) == OpArgN); 262 lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); 263 return luaK_code(fs, CREATE_ABx(o, a, bc)); 264 } 265 266 267 static int codeextraarg (FuncState *fs, int a) { 268 lua_assert(a <= MAXARG_Ax); 269 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); 270 } 271 272 273 int luaK_codek (FuncState *fs, int reg, int k) { 274 if (k <= MAXARG_Bx) 275 return luaK_codeABx(fs, OP_LOADK, reg, k); 276 else { 277 int p = luaK_codeABx(fs, OP_LOADKX, reg, 0); 278 codeextraarg(fs, k); 279 return p; 280 } 281 } 282 283 284 void luaK_checkstack (FuncState *fs, int n) { 285 int newstack = fs->freereg + n; 286 if (newstack > fs->f->maxstacksize) { 287 if (newstack >= MAXREGS) 288 luaX_syntaxerror(fs->ls, 289 "function or expression needs too many registers"); 290 fs->f->maxstacksize = cast_byte(newstack); 291 } 292 } 293 294 295 void luaK_reserveregs (FuncState *fs, int n) { 296 luaK_checkstack(fs, n); 297 fs->freereg += n; 298 } 299 300 301 static void freereg (FuncState *fs, int reg) { 302 if (!ISK(reg) && reg >= fs->nactvar) { 303 fs->freereg--; 304 lua_assert(reg == fs->freereg); 305 } 306 } 307 308 309 static void freeexp (FuncState *fs, expdesc *e) { 310 if (e->k == VNONRELOC) 311 freereg(fs, e->u.info); 312 } 313 314 315 /* 316 ** Use scanner's table to cache position of constants in constant list 317 ** and try to reuse constants 318 */ 319 static int addk (FuncState *fs, TValue *key, TValue *v) { 320 lua_State *L = fs->ls->L; 321 Proto *f = fs->f; 322 TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ 323 int k, oldsize; 324 if (ttisinteger(idx)) { /* is there an index there? */ 325 k = cast_int(ivalue(idx)); 326 /* correct value? (warning: must distinguish floats from integers!) */ 327 if (k < fs->nk && ttype(&f->k[k]) == ttype(v) && 328 luaV_rawequalobj(&f->k[k], v)) 329 return k; /* reuse index */ 330 } 331 /* constant not found; create a new entry */ 332 oldsize = f->sizek; 333 k = fs->nk; 334 /* numerical value does not need GC barrier; 335 table has no metatable, so it does not need to invalidate cache */ 336 setivalue(idx, k); 337 luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); 338 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); 339 setobj(L, &f->k[k], v); 340 fs->nk++; 341 luaC_barrier(L, f, v); 342 return k; 343 } 344 345 346 int luaK_stringK (FuncState *fs, TString *s) { 347 TValue o; 348 setsvalue(fs->ls->L, &o, s); 349 return addk(fs, &o, &o); 350 } 351 352 353 /* 354 ** Integers use userdata as keys to avoid collision with floats with same 355 ** value; conversion to 'void*' used only for hashing, no "precision" 356 ** problems 357 */ 358 int luaK_intK (FuncState *fs, lua_Integer n) { 359 TValue k, o; 360 setpvalue(&k, cast(void*, cast(size_t, n))); 361 setivalue(&o, n); 362 return addk(fs, &k, &o); 363 } 364 365 366 #ifndef _KERNEL 367 static int luaK_numberK (FuncState *fs, lua_Number r) { 368 TValue o; 369 setfltvalue(&o, r); 370 return addk(fs, &o, &o); 371 } 372 #endif 373 374 375 static int boolK (FuncState *fs, int b) { 376 TValue o; 377 setbvalue(&o, b); 378 return addk(fs, &o, &o); 379 } 380 381 382 static int nilK (FuncState *fs) { 383 TValue k, v; 384 setnilvalue(&v); 385 /* cannot use nil as key; instead use table itself to represent nil */ 386 sethvalue(fs->ls->L, &k, fs->ls->h); 387 return addk(fs, &k, &v); 388 } 389 390 391 void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { 392 if (e->k == VCALL) { /* expression is an open function call? */ 393 SETARG_C(getcode(fs, e), nresults+1); 394 } 395 else if (e->k == VVARARG) { 396 SETARG_B(getcode(fs, e), nresults+1); 397 SETARG_A(getcode(fs, e), fs->freereg); 398 luaK_reserveregs(fs, 1); 399 } 400 } 401 402 403 void luaK_setoneret (FuncState *fs, expdesc *e) { 404 if (e->k == VCALL) { /* expression is an open function call? */ 405 e->k = VNONRELOC; 406 e->u.info = GETARG_A(getcode(fs, e)); 407 } 408 else if (e->k == VVARARG) { 409 SETARG_B(getcode(fs, e), 2); 410 e->k = VRELOCABLE; /* can relocate its simple result */ 411 } 412 } 413 414 415 void luaK_dischargevars (FuncState *fs, expdesc *e) { 416 switch (e->k) { 417 case VLOCAL: { 418 e->k = VNONRELOC; 419 break; 420 } 421 case VUPVAL: { 422 e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0); 423 e->k = VRELOCABLE; 424 break; 425 } 426 case VINDEXED: { 427 OpCode op = OP_GETTABUP; /* assume 't' is in an upvalue */ 428 freereg(fs, e->u.ind.idx); 429 if (e->u.ind.vt == VLOCAL) { /* 't' is in a register? */ 430 freereg(fs, e->u.ind.t); 431 op = OP_GETTABLE; 432 } 433 e->u.info = luaK_codeABC(fs, op, 0, e->u.ind.t, e->u.ind.idx); 434 e->k = VRELOCABLE; 435 break; 436 } 437 case VVARARG: 438 case VCALL: { 439 luaK_setoneret(fs, e); 440 break; 441 } 442 default: break; /* there is one value available (somewhere) */ 443 } 444 } 445 446 447 static int code_label (FuncState *fs, int A, int b, int jump) { 448 luaK_getlabel(fs); /* those instructions may be jump targets */ 449 return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); 450 } 451 452 453 static void discharge2reg (FuncState *fs, expdesc *e, int reg) { 454 luaK_dischargevars(fs, e); 455 switch (e->k) { 456 case VNIL: { 457 luaK_nil(fs, reg, 1); 458 break; 459 } 460 case VFALSE: case VTRUE: { 461 luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); 462 break; 463 } 464 case VK: { 465 luaK_codek(fs, reg, e->u.info); 466 break; 467 } 468 #ifndef _KERNEL 469 case VKFLT: { 470 luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); 471 break; 472 } 473 #endif 474 case VKINT: { 475 luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); 476 break; 477 } 478 case VRELOCABLE: { 479 Instruction *pc = &getcode(fs, e); 480 SETARG_A(*pc, reg); 481 break; 482 } 483 case VNONRELOC: { 484 if (reg != e->u.info) 485 luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0); 486 break; 487 } 488 default: { 489 lua_assert(e->k == VVOID || e->k == VJMP); 490 return; /* nothing to do... */ 491 } 492 } 493 e->u.info = reg; 494 e->k = VNONRELOC; 495 } 496 497 498 static void discharge2anyreg (FuncState *fs, expdesc *e) { 499 if (e->k != VNONRELOC) { 500 luaK_reserveregs(fs, 1); 501 discharge2reg(fs, e, fs->freereg-1); 502 } 503 } 504 505 506 static void exp2reg (FuncState *fs, expdesc *e, int reg) { 507 discharge2reg(fs, e, reg); 508 if (e->k == VJMP) 509 luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */ 510 if (hasjumps(e)) { 511 int final; /* position after whole expression */ 512 int p_f = NO_JUMP; /* position of an eventual LOAD false */ 513 int p_t = NO_JUMP; /* position of an eventual LOAD true */ 514 if (need_value(fs, e->t) || need_value(fs, e->f)) { 515 int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); 516 p_f = code_label(fs, reg, 0, 1); 517 p_t = code_label(fs, reg, 1, 0); 518 luaK_patchtohere(fs, fj); 519 } 520 final = luaK_getlabel(fs); 521 patchlistaux(fs, e->f, final, reg, p_f); 522 patchlistaux(fs, e->t, final, reg, p_t); 523 } 524 e->f = e->t = NO_JUMP; 525 e->u.info = reg; 526 e->k = VNONRELOC; 527 } 528 529 530 void luaK_exp2nextreg (FuncState *fs, expdesc *e) { 531 luaK_dischargevars(fs, e); 532 freeexp(fs, e); 533 luaK_reserveregs(fs, 1); 534 exp2reg(fs, e, fs->freereg - 1); 535 } 536 537 538 int luaK_exp2anyreg (FuncState *fs, expdesc *e) { 539 luaK_dischargevars(fs, e); 540 if (e->k == VNONRELOC) { 541 if (!hasjumps(e)) return e->u.info; /* exp is already in a register */ 542 if (e->u.info >= fs->nactvar) { /* reg. is not a local? */ 543 exp2reg(fs, e, e->u.info); /* put value on it */ 544 return e->u.info; 545 } 546 } 547 luaK_exp2nextreg(fs, e); /* default */ 548 return e->u.info; 549 } 550 551 552 void luaK_exp2anyregup (FuncState *fs, expdesc *e) { 553 if (e->k != VUPVAL || hasjumps(e)) 554 luaK_exp2anyreg(fs, e); 555 } 556 557 558 void luaK_exp2val (FuncState *fs, expdesc *e) { 559 if (hasjumps(e)) 560 luaK_exp2anyreg(fs, e); 561 else 562 luaK_dischargevars(fs, e); 563 } 564 565 566 int luaK_exp2RK (FuncState *fs, expdesc *e) { 567 luaK_exp2val(fs, e); 568 switch (e->k) { 569 case VTRUE: 570 case VFALSE: 571 case VNIL: { 572 if (fs->nk <= MAXINDEXRK) { /* constant fits in RK operand? */ 573 e->u.info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE)); 574 e->k = VK; 575 return RKASK(e->u.info); 576 } 577 else break; 578 } 579 case VKINT: { 580 e->u.info = luaK_intK(fs, e->u.ival); 581 e->k = VK; 582 goto vk; 583 } 584 #ifndef _KERNEL 585 case VKFLT: { 586 e->u.info = luaK_numberK(fs, e->u.nval); 587 e->k = VK; 588 } 589 #endif 590 /* FALLTHROUGH */ 591 case VK: { 592 vk: 593 if (e->u.info <= MAXINDEXRK) /* constant fits in 'argC'? */ 594 return RKASK(e->u.info); 595 else break; 596 } 597 default: break; 598 } 599 /* not a constant in the right range: put it in a register */ 600 return luaK_exp2anyreg(fs, e); 601 } 602 603 604 void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { 605 switch (var->k) { 606 case VLOCAL: { 607 freeexp(fs, ex); 608 exp2reg(fs, ex, var->u.info); 609 return; 610 } 611 case VUPVAL: { 612 int e = luaK_exp2anyreg(fs, ex); 613 luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0); 614 break; 615 } 616 case VINDEXED: { 617 OpCode op = (var->u.ind.vt == VLOCAL) ? OP_SETTABLE : OP_SETTABUP; 618 int e = luaK_exp2RK(fs, ex); 619 luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e); 620 break; 621 } 622 default: { 623 lua_assert(0); /* invalid var kind to store */ 624 break; 625 } 626 } 627 freeexp(fs, ex); 628 } 629 630 631 void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { 632 int ereg; 633 luaK_exp2anyreg(fs, e); 634 ereg = e->u.info; /* register where 'e' was placed */ 635 freeexp(fs, e); 636 e->u.info = fs->freereg; /* base register for op_self */ 637 e->k = VNONRELOC; 638 luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ 639 luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key)); 640 freeexp(fs, key); 641 } 642 643 644 static void invertjump (FuncState *fs, expdesc *e) { 645 Instruction *pc = getjumpcontrol(fs, e->u.info); 646 lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && 647 GET_OPCODE(*pc) != OP_TEST); 648 SETARG_A(*pc, !(GETARG_A(*pc))); 649 } 650 651 652 static int jumponcond (FuncState *fs, expdesc *e, int cond) { 653 if (e->k == VRELOCABLE) { 654 Instruction ie = getcode(fs, e); 655 if (GET_OPCODE(ie) == OP_NOT) { 656 fs->pc--; /* remove previous OP_NOT */ 657 return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); 658 } 659 /* else go through */ 660 } 661 discharge2anyreg(fs, e); 662 freeexp(fs, e); 663 return condjump(fs, OP_TESTSET, NO_REG, e->u.info, cond); 664 } 665 666 667 void luaK_goiftrue (FuncState *fs, expdesc *e) { 668 int pc; /* pc of last jump */ 669 luaK_dischargevars(fs, e); 670 switch (e->k) { 671 case VJMP: { 672 invertjump(fs, e); 673 pc = e->u.info; 674 break; 675 } 676 #ifndef _KERNEL 677 case VK: case VKFLT: case VKINT: case VTRUE: { 678 #else 679 case VK: case VKINT: case VTRUE: { 680 #endif 681 pc = NO_JUMP; /* always true; do nothing */ 682 break; 683 } 684 default: { 685 pc = jumponcond(fs, e, 0); 686 break; 687 } 688 } 689 luaK_concat(fs, &e->f, pc); /* insert last jump in 'f' list */ 690 luaK_patchtohere(fs, e->t); 691 e->t = NO_JUMP; 692 } 693 694 695 void luaK_goiffalse (FuncState *fs, expdesc *e) { 696 int pc; /* pc of last jump */ 697 luaK_dischargevars(fs, e); 698 switch (e->k) { 699 case VJMP: { 700 pc = e->u.info; 701 break; 702 } 703 case VNIL: case VFALSE: { 704 pc = NO_JUMP; /* always false; do nothing */ 705 break; 706 } 707 default: { 708 pc = jumponcond(fs, e, 1); 709 break; 710 } 711 } 712 luaK_concat(fs, &e->t, pc); /* insert last jump in 't' list */ 713 luaK_patchtohere(fs, e->f); 714 e->f = NO_JUMP; 715 } 716 717 718 static void codenot (FuncState *fs, expdesc *e) { 719 luaK_dischargevars(fs, e); 720 switch (e->k) { 721 case VNIL: case VFALSE: { 722 e->k = VTRUE; 723 break; 724 } 725 #ifndef _KERNEL 726 case VK: case VKFLT: case VKINT: case VTRUE: { 727 #else 728 case VK: case VKINT: case VTRUE: { 729 #endif 730 e->k = VFALSE; 731 break; 732 } 733 case VJMP: { 734 invertjump(fs, e); 735 break; 736 } 737 case VRELOCABLE: 738 case VNONRELOC: { 739 discharge2anyreg(fs, e); 740 freeexp(fs, e); 741 e->u.info = luaK_codeABC(fs, OP_NOT, 0, e->u.info, 0); 742 e->k = VRELOCABLE; 743 break; 744 } 745 default: { 746 lua_assert(0); /* cannot happen */ 747 break; 748 } 749 } 750 /* interchange true and false lists */ 751 { int temp = e->f; e->f = e->t; e->t = temp; } 752 removevalues(fs, e->f); 753 removevalues(fs, e->t); 754 } 755 756 757 void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { 758 lua_assert(!hasjumps(t)); 759 t->u.ind.t = t->u.info; 760 t->u.ind.idx = luaK_exp2RK(fs, k); 761 t->u.ind.vt = (t->k == VUPVAL) ? VUPVAL 762 : check_exp(vkisinreg(t->k), VLOCAL); 763 t->k = VINDEXED; 764 } 765 766 767 /* 768 ** return false if folding can raise an error 769 */ 770 static int validop (int op, TValue *v1, TValue *v2) { 771 switch (op) { 772 case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: 773 case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ 774 lua_Integer i; 775 return (tointeger(v1, &i) && tointeger(v2, &i)); 776 } 777 #ifndef _KERNEL 778 case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ 779 #else /* _KERNEL */ 780 case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ 781 #endif 782 return (nvalue(v2) != 0); 783 default: return 1; /* everything else is valid */ 784 } 785 } 786 787 788 /* 789 ** Try to "constant-fold" an operation; return 1 iff successful 790 */ 791 static int constfolding (FuncState *fs, int op, expdesc *e1, expdesc *e2) { 792 TValue v1, v2, res; 793 if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2)) 794 return 0; /* non-numeric operands or not safe to fold */ 795 luaO_arith(fs->ls->L, op, &v1, &v2, &res); /* does operation */ 796 if (ttisinteger(&res)) { 797 e1->k = VKINT; 798 e1->u.ival = ivalue(&res); 799 } 800 else { /* folds neither NaN nor 0.0 (to avoid collapsing with -0.0) */ 801 #ifndef _KERNEL 802 lua_Number n = fltvalue(&res); 803 if (luai_numisnan(n) || n == 0) 804 return 0; 805 e1->k = VKFLT; 806 e1->u.nval = n; 807 #else /* _KERNEL */ 808 return 0; /* if it is not integer, we must fail */ 809 #endif 810 } 811 return 1; 812 } 813 814 815 /* 816 ** Code for binary and unary expressions that "produce values" 817 ** (arithmetic operations, bitwise operations, concat, length). First 818 ** try to do constant folding (only for numeric [arithmetic and 819 ** bitwise] operations, which is what 'lua_arith' accepts). 820 ** Expression to produce final result will be encoded in 'e1'. 821 */ 822 static void codeexpval (FuncState *fs, OpCode op, 823 expdesc *e1, expdesc *e2, int line) { 824 lua_assert(op >= OP_ADD); 825 if (op <= OP_BNOT && constfolding(fs, (op - OP_ADD) + LUA_OPADD, e1, e2)) 826 return; /* result has been folded */ 827 else { 828 int o1, o2; 829 /* move operands to registers (if needed) */ 830 if (op == OP_UNM || op == OP_BNOT || op == OP_LEN) { /* unary op? */ 831 o2 = 0; /* no second expression */ 832 o1 = luaK_exp2anyreg(fs, e1); /* cannot operate on constants */ 833 } 834 else { /* regular case (binary operators) */ 835 o2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ 836 o1 = luaK_exp2RK(fs, e1); 837 } 838 if (o1 > o2) { /* free registers in proper order */ 839 freeexp(fs, e1); 840 freeexp(fs, e2); 841 } 842 else { 843 freeexp(fs, e2); 844 freeexp(fs, e1); 845 } 846 e1->u.info = luaK_codeABC(fs, op, 0, o1, o2); /* generate opcode */ 847 e1->k = VRELOCABLE; /* all those operations are relocable */ 848 luaK_fixline(fs, line); 849 } 850 } 851 852 853 static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, 854 expdesc *e2) { 855 int o1 = luaK_exp2RK(fs, e1); 856 int o2 = luaK_exp2RK(fs, e2); 857 freeexp(fs, e2); 858 freeexp(fs, e1); 859 if (cond == 0 && op != OP_EQ) { 860 int temp; /* exchange args to replace by '<' or '<=' */ 861 temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ 862 cond = 1; 863 } 864 e1->u.info = condjump(fs, op, cond, o1, o2); 865 e1->k = VJMP; 866 } 867 868 869 void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { 870 expdesc e2; 871 e2.t = e2.f = NO_JUMP; e2.k = VKINT; e2.u.ival = 0; 872 switch (op) { 873 case OPR_MINUS: case OPR_BNOT: case OPR_LEN: { 874 codeexpval(fs, cast(OpCode, (op - OPR_MINUS) + OP_UNM), e, &e2, line); 875 break; 876 } 877 case OPR_NOT: codenot(fs, e); break; 878 default: lua_assert(0); 879 } 880 } 881 882 883 void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { 884 switch (op) { 885 case OPR_AND: { 886 luaK_goiftrue(fs, v); 887 break; 888 } 889 case OPR_OR: { 890 luaK_goiffalse(fs, v); 891 break; 892 } 893 case OPR_CONCAT: { 894 luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */ 895 break; 896 } 897 case OPR_ADD: case OPR_SUB: 898 #ifndef _KERNEL 899 case OPR_MUL: case OPR_DIV: case OPR_IDIV: 900 case OPR_MOD: case OPR_POW: 901 #else 902 case OPR_MUL: case OPR_IDIV: 903 case OPR_MOD: 904 #endif 905 case OPR_BAND: case OPR_BOR: case OPR_BXOR: 906 case OPR_SHL: case OPR_SHR: { 907 if (!tonumeral(v, NULL)) luaK_exp2RK(fs, v); 908 break; 909 } 910 default: { 911 luaK_exp2RK(fs, v); 912 break; 913 } 914 } 915 } 916 917 918 void luaK_posfix (FuncState *fs, BinOpr op, 919 expdesc *e1, expdesc *e2, int line) { 920 switch (op) { 921 case OPR_AND: { 922 lua_assert(e1->t == NO_JUMP); /* list must be closed */ 923 luaK_dischargevars(fs, e2); 924 luaK_concat(fs, &e2->f, e1->f); 925 *e1 = *e2; 926 break; 927 } 928 case OPR_OR: { 929 lua_assert(e1->f == NO_JUMP); /* list must be closed */ 930 luaK_dischargevars(fs, e2); 931 luaK_concat(fs, &e2->t, e1->t); 932 *e1 = *e2; 933 break; 934 } 935 case OPR_CONCAT: { 936 luaK_exp2val(fs, e2); 937 if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { 938 lua_assert(e1->u.info == GETARG_B(getcode(fs, e2))-1); 939 freeexp(fs, e1); 940 SETARG_B(getcode(fs, e2), e1->u.info); 941 e1->k = VRELOCABLE; e1->u.info = e2->u.info; 942 } 943 else { 944 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ 945 codeexpval(fs, OP_CONCAT, e1, e2, line); 946 } 947 break; 948 } 949 #ifndef _KERNEL 950 case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: 951 case OPR_IDIV: case OPR_MOD: case OPR_POW: 952 #else 953 case OPR_ADD: case OPR_SUB: case OPR_MUL: 954 case OPR_IDIV: case OPR_MOD: 955 #endif 956 case OPR_BAND: case OPR_BOR: case OPR_BXOR: 957 case OPR_SHL: case OPR_SHR: { 958 codeexpval(fs, cast(OpCode, (op - OPR_ADD) + OP_ADD), e1, e2, line); 959 break; 960 } 961 case OPR_EQ: case OPR_LT: case OPR_LE: { 962 codecomp(fs, cast(OpCode, (op - OPR_EQ) + OP_EQ), 1, e1, e2); 963 break; 964 } 965 case OPR_NE: case OPR_GT: case OPR_GE: { 966 codecomp(fs, cast(OpCode, (op - OPR_NE) + OP_EQ), 0, e1, e2); 967 break; 968 } 969 default: lua_assert(0); 970 } 971 } 972 973 974 void luaK_fixline (FuncState *fs, int line) { 975 fs->f->lineinfo[fs->pc - 1] = line; 976 } 977 978 979 void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { 980 int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; 981 int b = (tostore == LUA_MULTRET) ? 0 : tostore; 982 lua_assert(tostore != 0); 983 if (c <= MAXARG_C) 984 luaK_codeABC(fs, OP_SETLIST, base, b, c); 985 else if (c <= MAXARG_Ax) { 986 luaK_codeABC(fs, OP_SETLIST, base, b, 0); 987 codeextraarg(fs, c); 988 } 989 else 990 luaX_syntaxerror(fs->ls, "constructor too long"); 991 fs->freereg = base + 1; /* free registers with list values */ 992 } 993 994