Skip to content

Commit

Permalink
New implementation for constants
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
roberto-ieru committed Jul 9, 2019
1 parent e888976 commit 54f7b46
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 131 deletions.
60 changes: 13 additions & 47 deletions lcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion lcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;

#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)

LUAI_FUNC int luaK_tonumeral (FuncState *fs, const expdesc *e, TValue *v);
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx);
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A,
Expand Down
1 change: 1 addition & 0 deletions ldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ static void DumpUpvalues (const Proto *f, DumpState *D) {
for (i = 0; i < n; i++) {
DumpByte(f->upvalues[i].instack, D);
DumpByte(f->upvalues[i].idx, D);
DumpByte(f->upvalues[i].ro, D);
}
}

Expand Down
1 change: 1 addition & 0 deletions lobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ typedef struct Upvaldesc {
TString *name; /* upvalue name (for debug information) */
lu_byte instack; /* whether it is in stack (register) */
lu_byte idx; /* index of upvalue (in stack or in outer function's list) */
lu_byte ro; /* true if upvalue is read-only (const) */
} Upvaldesc;


Expand Down
Loading

0 comments on commit 54f7b46

Please sign in to comment.