| 625 | |
| 626 | |
| 627 | static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { |
| 628 | lua_Number v1, v2, r; |
| 629 | if (!isnumeral(e1) || !isnumeral(e2)) return 0; |
| 630 | v1 = e1->u.nval; |
| 631 | v2 = e2->u.nval; |
| 632 | switch (op) { |
| 633 | case OP_ADD: r = luai_numadd(v1, v2); break; |
| 634 | case OP_SUB: r = luai_numsub(v1, v2); break; |
| 635 | case OP_MUL: r = luai_nummul(v1, v2); break; |
| 636 | case OP_DIV: |
| 637 | if (v2 == 0) return 0; /* do not attempt to divide by 0 */ |
| 638 | r = luai_numdiv(v1, v2); break; |
| 639 | case OP_MOD: |
| 640 | if (v2 == 0) return 0; /* do not attempt to divide by 0 */ |
| 641 | r = luai_nummod(v1, v2); break; |
| 642 | case OP_POW: r = luai_numpow(v1, v2); break; |
| 643 | case OP_UNM: r = luai_numunm(v1); break; |
| 644 | case OP_LEN: return 0; /* no constant folding for 'len' */ |
| 645 | default: lua_assert(0); r = 0; break; |
| 646 | } |
| 647 | if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ |
| 648 | e1->u.nval = r; |
| 649 | return 1; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { |