From 8082906c059f2b1473de4363ca57fe19b52f281f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 17 Jul 2019 14:50:42 -0300 Subject: Fixed small issue with constant propagation Constants directly assigned to other constants were not propagating: For instance, in local k1 = 10 local k2 = k1 'k2' were not treated as a compile-time constant. --- lcode.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'lcode.c') diff --git a/lcode.c b/lcode.c index 40efcff3..c2b5fc6d 100644 --- a/lcode.c +++ b/lcode.c @@ -67,6 +67,15 @@ static int tonumeral (const expdesc *e, TValue *v) { } +/* +** Get the constant value from a constant expression +*/ +static TValue *const2val (FuncState *fs, const expdesc *e) { + lua_assert(e->k == VCONST); + return &fs->ls->dyd->actvar.arr[e->u.info].k; +} + + /* ** If expression is a constant, fills 'v' with its value ** and returns 1. Otherwise, returns 0. @@ -85,6 +94,10 @@ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) { setsvalue(fs->ls->L, v, e->u.strval); return 1; } + case VCONST: { + setobj(fs->ls->L, v, const2val(fs, e)); + return 1; + } default: return tonumeral(e, v); } } @@ -730,14 +743,13 @@ void luaK_setoneret (FuncState *fs, expdesc *e) { /* -** Ensure that expression 'e' is not a variable. +** Ensure that expression 'e' is not a variable (nor a constant). ** (Expression still may have jump lists.) */ void luaK_dischargevars (FuncState *fs, expdesc *e) { switch (e->k) { case VCONST: { - TValue *val = &fs->ls->dyd->actvar.arr[e->u.info].k; - const2exp(val, e); + const2exp(const2val(fs, e), e); break; } case VLOCAL: { /* already in a register */ -- cgit v1.2.3-55-g6feb