| 640 | |
| 641 | |
| 642 | static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { |
| 643 | lua_Number v1, v2, r; |
| 644 | if (!isnumeral(e1) || !isnumeral(e2)) return 0; |
| 645 | v1 = e1->u.nval; |
| 646 | v2 = e2->u.nval; |
| 647 | switch (op) { |
| 648 | case OP_ADD: r = luai_numadd(v1, v2); break; |
| 649 | case OP_SUB: r = luai_numsub(v1, v2); break; |
| 650 | case OP_MUL: r = luai_nummul(v1, v2); break; |
| 651 | case OP_DIV: |
| 652 | if (v2 == 0) return 0; /* do not attempt to divide by 0 */ |
| 653 | r = luai_numdiv(v1, v2); break; |
| 654 | case OP_MOD: |
| 655 | if (v2 == 0) return 0; /* do not attempt to divide by 0 */ |
| 656 | r = luai_nummod(v1, v2); break; |
| 657 | case OP_POW: r = luai_numpow(v1, v2); break; |
| 658 | case OP_UNM: r = luai_numunm(v1); break; |
| 659 | case OP_LEN: return 0; /* no constant folding for 'len' */ |
| 660 | default: lua_assert(0); r = 0; break; |
| 661 | } |
| 662 | if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ |
| 663 | e1->u.nval = r; |
| 664 | return 1; |
| 665 | } |
| 666 | |
| 667 | |
| 668 | void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { |