Changed internal representation of booleans

Instead of an explicit value (field 'b'), true and false use different
tag variants. This avoids reading an extra field and results in more
direct code. (Most code that uses booleans needs to distinguish between
true and false anyway.)
This commit is contained in:
Roberto Ierusalimschy
2020-01-06 11:38:31 -03:00
parent e3c83835e7
commit 5ff408d218
14 changed files with 87 additions and 52 deletions

50
lcode.c
View File

@@ -84,8 +84,11 @@ int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v) {
if (hasjumps(e))
return 0; /* not a constant */
switch (e->k) {
case VFALSE: case VTRUE:
setbvalue(v, e->k == VTRUE);
case VFALSE:
setbfvalue(v);
return 1;
case VTRUE:
setbtvalue(v);
return 1;
case VNIL:
setnilvalue(v);
@@ -604,11 +607,21 @@ static int luaK_numberK (FuncState *fs, lua_Number r) {
/*
** Add a boolean to list of constants and return its index.
** Add a false to list of constants and return its index.
*/
static int boolK (FuncState *fs, int b) {
static int boolF (FuncState *fs) {
TValue o;
setbvalue(&o, b);
setbfvalue(&o);
return addk(fs, &o, &o); /* use boolean itself as key */
}
/*
** Add a true to list of constants and return its index.
*/
static int boolT (FuncState *fs) {
TValue o;
setbtvalue(&o);
return addk(fs, &o, &o); /* use boolean itself as key */
}
@@ -671,8 +684,11 @@ static void const2exp (TValue *v, expdesc *e) {
case LUA_TNUMFLT:
e->k = VKFLT; e->u.nval = fltvalue(v);
break;
case LUA_TBOOLEAN:
e->k = bvalue(v) ? VTRUE : VFALSE;
case LUA_TFALSE:
e->k = VFALSE;
break;
case LUA_TTRUE:
e->k = VTRUE;
break;
case LUA_TNIL:
e->k = VNIL;
@@ -801,8 +817,12 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
luaK_nil(fs, reg, 1);
break;
}
case VFALSE: case VTRUE: {
luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);
case VFALSE: {
luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
break;
}
case VTRUE: {
luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
break;
}
case VKSTR: {
@@ -852,9 +872,9 @@ static void discharge2anyreg (FuncState *fs, expdesc *e) {
}
static int code_loadbool (FuncState *fs, int A, int b, int jump) {
static int code_loadbool (FuncState *fs, int A, OpCode op, int jump) {
luaK_getlabel(fs); /* those instructions may be jump targets */
return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
return luaK_codeABC(fs, op, A, jump, 0);
}
@@ -888,8 +908,8 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) {
int p_t = NO_JUMP; /* position of an eventual LOAD true */
if (need_value(fs, e->t) || need_value(fs, e->f)) {
int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);
p_f = code_loadbool(fs, reg, 0, 1); /* load false and skip next i. */
p_t = code_loadbool(fs, reg, 1, 0); /* load true */
p_f = code_loadbool(fs, reg, OP_LOADFALSE, 1); /* skip next inst. */
p_t = code_loadbool(fs, reg, OP_LOADTRUE, 0);
/* jump around these booleans if 'e' is not a test */
luaK_patchtohere(fs, fj);
}
@@ -963,8 +983,8 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) {
if (!hasjumps(e)) {
int info;
switch (e->k) { /* move constants to 'k' */
case VTRUE: info = boolK(fs, 1); break;
case VFALSE: info = boolK(fs, 0); break;
case VTRUE: info = boolT(fs); break;
case VFALSE: info = boolF(fs); break;
case VNIL: info = nilK(fs); break;
case VKINT: info = luaK_intK(fs, e->u.ival); break;
case VKFLT: info = luaK_numberK(fs, e->u.nval); break;