New implementation for constants
VLOCAL expressions keep a reference to their corresponding 'Vardesc',
and 'Upvaldesc' (for upvalues) has a field 'ro' (read-only). So, it is
easier to check whether a variable is read-only. The decoupling in
VLOCAL between 'vidx' ('Vardesc' index) and 'sidx' (stack index)
should also help the forthcoming implementation of compile-time
constant propagation.
This commit is contained in:
60
lcode.c
60
lcode.c
@@ -52,7 +52,7 @@ l_noret luaK_semerror (LexState *ls, const char *msg) {
|
||||
** If expression is a numeric constant, fills 'v' with its value
|
||||
** and returns 1. Otherwise, returns 0.
|
||||
*/
|
||||
int luaK_tonumeral (FuncState *fs, const expdesc *e, TValue *v) {
|
||||
static int tonumeral (const expdesc *e, TValue *v) {
|
||||
if (hasjumps(e))
|
||||
return 0; /* not a numeral */
|
||||
switch (e->k) {
|
||||
@@ -62,41 +62,11 @@ int luaK_tonumeral (FuncState *fs, const expdesc *e, TValue *v) {
|
||||
case VKFLT:
|
||||
if (v) setfltvalue(v, e->u.nval);
|
||||
return 1;
|
||||
case VUPVAL: { /* may be a constant */
|
||||
Vardesc *vd = luaY_getvardesc(&fs, e);
|
||||
if (v && vd && !ttisnil(&vd->val)) {
|
||||
setobj(fs->ls->L, v, &vd->val);
|
||||
return 1;
|
||||
} /* else */
|
||||
} /* FALLTHROUGH */
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** If expression 'e' is a constant, change 'e' to represent
|
||||
** the constant value.
|
||||
*/
|
||||
static int const2exp (FuncState *fs, expdesc *e) {
|
||||
Vardesc *vd = luaY_getvardesc(&fs, e);
|
||||
if (vd) {
|
||||
TValue *v = &vd->val;
|
||||
switch (ttypetag(v)) {
|
||||
case LUA_TNUMINT:
|
||||
e->k = VKINT;
|
||||
e->u.ival = ivalue(v);
|
||||
return 1;
|
||||
case LUA_TNUMFLT:
|
||||
e->k = VKFLT;
|
||||
e->u.nval = fltvalue(v);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Return the previous instruction of the current code. If there
|
||||
** may be a jump target between the current instruction and the
|
||||
@@ -708,15 +678,13 @@ void luaK_setoneret (FuncState *fs, expdesc *e) {
|
||||
void luaK_dischargevars (FuncState *fs, expdesc *e) {
|
||||
switch (e->k) {
|
||||
case VLOCAL: { /* already in a register */
|
||||
e->u.info = e->u.var.idx;
|
||||
e->u.info = e->u.var.sidx;
|
||||
e->k = VNONRELOC; /* becomes a non-relocatable value */
|
||||
break;
|
||||
}
|
||||
case VUPVAL: { /* move value to some (pending) register */
|
||||
if (!const2exp(fs, e)) {
|
||||
e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.var.idx, 0);
|
||||
e->k = VRELOC;
|
||||
}
|
||||
e->u.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
|
||||
e->k = VRELOC;
|
||||
break;
|
||||
}
|
||||
case VINDEXUP: {
|
||||
@@ -971,12 +939,12 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
|
||||
switch (var->k) {
|
||||
case VLOCAL: {
|
||||
freeexp(fs, ex);
|
||||
exp2reg(fs, ex, var->u.var.idx); /* compute 'ex' into proper place */
|
||||
exp2reg(fs, ex, var->u.var.sidx); /* compute 'ex' into proper place */
|
||||
return;
|
||||
}
|
||||
case VUPVAL: {
|
||||
int e = luaK_exp2anyreg(fs, ex);
|
||||
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.var.idx, 0);
|
||||
luaK_codeABC(fs, OP_SETUPVAL, e, var->u.info, 0);
|
||||
break;
|
||||
}
|
||||
case VINDEXUP: {
|
||||
@@ -1203,13 +1171,13 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
|
||||
if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non string? */
|
||||
luaK_exp2anyreg(fs, t); /* put it in a register */
|
||||
if (t->k == VUPVAL) {
|
||||
t->u.ind.t = t->u.var.idx; /* upvalue index */
|
||||
t->u.ind.t = t->u.info; /* upvalue index */
|
||||
t->u.ind.idx = k->u.info; /* literal string */
|
||||
t->k = VINDEXUP;
|
||||
}
|
||||
else {
|
||||
/* register index of the table */
|
||||
t->u.ind.t = (t->k == VLOCAL) ? t->u.var.idx: t->u.info;
|
||||
t->u.ind.t = (t->k == VLOCAL) ? t->u.var.sidx: t->u.info;
|
||||
if (isKstr(fs, k)) {
|
||||
t->u.ind.idx = k->u.info; /* literal string */
|
||||
t->k = VINDEXSTR;
|
||||
@@ -1252,9 +1220,7 @@ static int validop (int op, TValue *v1, TValue *v2) {
|
||||
static int constfolding (FuncState *fs, int op, expdesc *e1,
|
||||
const expdesc *e2) {
|
||||
TValue v1, v2, res;
|
||||
if (!luaK_tonumeral(fs, e1, &v1) ||
|
||||
!luaK_tonumeral(fs, e2, &v2) ||
|
||||
!validop(op, &v1, &v2))
|
||||
if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
|
||||
return 0; /* non-numeric operands or not safe to fold */
|
||||
luaO_rawarith(fs->ls->L, op, &v1, &v2, &res); /* does operation */
|
||||
if (ttisinteger(&res)) {
|
||||
@@ -1341,7 +1307,7 @@ static void codearith (FuncState *fs, OpCode op,
|
||||
expdesc *e1, expdesc *e2, int flip, int line) {
|
||||
if (isSCint(e2)) /* immediate operand? */
|
||||
codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line);
|
||||
else if (luaK_tonumeral(fs, e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */
|
||||
else if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */
|
||||
int v2 = e2->u.info; /* K index */
|
||||
op = cast(OpCode, op - OP_ADD + OP_ADDK);
|
||||
finishbinexpval(fs, e1, e2, op, v2, flip, line);
|
||||
@@ -1362,7 +1328,7 @@ static void codearith (FuncState *fs, OpCode op,
|
||||
static void codecommutative (FuncState *fs, OpCode op,
|
||||
expdesc *e1, expdesc *e2, int line) {
|
||||
int flip = 0;
|
||||
if (luaK_tonumeral(fs, e1, NULL)) { /* is first operand a numeric constant? */
|
||||
if (tonumeral(e1, NULL)) { /* is first operand a numeric constant? */
|
||||
swapexps(e1, e2); /* change order */
|
||||
flip = 1;
|
||||
}
|
||||
@@ -1519,13 +1485,13 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
|
||||
case OPR_MOD: case OPR_POW:
|
||||
case OPR_BAND: case OPR_BOR: case OPR_BXOR:
|
||||
case OPR_SHL: case OPR_SHR: {
|
||||
if (!luaK_tonumeral(fs, v, NULL))
|
||||
if (!tonumeral(v, NULL))
|
||||
luaK_exp2anyreg(fs, v);
|
||||
/* else keep numeral, which may be folded with 2nd operand */
|
||||
break;
|
||||
}
|
||||
case OPR_EQ: case OPR_NE: {
|
||||
if (!luaK_tonumeral(fs, v, NULL))
|
||||
if (!tonumeral(v, NULL))
|
||||
luaK_exp2RK(fs, v);
|
||||
/* else keep numeral, which may be an immediate operand */
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user