MCPcopy Create free account
hub / github.com/defold/defold / constfolding

Function constfolding

engine/lua/src/lua/lcode.c:635–658  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

codearithFunction · 0.85

Calls 1

isnumeralFunction · 0.85

Tested by

no test coverage detected