binary operators use R instead of RK

faster + nobody uses RK(B), so B can be smaller (freeing one bit
for more opcodes, soon)
This commit is contained in:
Roberto Ierusalimschy 2017-09-26 15:14:45 -03:00
parent 13256a4173
commit 00e728af88
4 changed files with 73 additions and 76 deletions

21
lcode.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 2.123 2017/09/19 18:38:14 roberto Exp roberto $
** $Id: lcode.c,v 2.124 2017/09/26 17:10:49 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -1157,7 +1157,7 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) {
** (everything but logical operators 'and'/'or' and comparison
** operators).
** Expression to produce final result will be encoded in 'e1'.
** Because 'luaK_exp2RK' can free registers, its calls must be
** Because 'luaK_exp2anyreg' can free registers, its calls must be
** in "stack order" (that is, first on 'e2', which may have more
** recent registers to be released).
*/
@ -1176,8 +1176,8 @@ static void codebinexpval (FuncState *fs, OpCode op,
op = OP_ADDI;
}
else {
v2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */
v1 = luaK_exp2RK(fs, e1);
v2 = luaK_exp2anyreg(fs, e2); /* both operands are in registers */
v1 = luaK_exp2anyreg(fs, e1);
}
freeexps(fs, e1, e2);
e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */
@ -1188,12 +1188,12 @@ static void codebinexpval (FuncState *fs, OpCode op,
/*
** Emit code for comparisons.
** 'e1' was already put in R/K form by 'luaK_infix'.
** 'e1' was already put in register by 'luaK_infix'.
*/
static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
int rk1 = (e1->k == VK) ? RKASK(e1->u.info)
: check_exp(e1->k == VNONRELOC, e1->u.info);
int rk2 = luaK_exp2RK(fs, e2);
int rk2 = luaK_exp2anyreg(fs, e2);
freeexps(fs, e1, e2);
switch (opr) {
case OPR_NE: { /* '(a ~= b)' ==> 'not (a == b)' */
@ -1258,14 +1258,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 (!tonumeral(v, NULL))
luaK_exp2RK(fs, v);
/* else keep numeral, which may be folded with 2nd operand */
break;
if (tonumeral(v, NULL))
break; /* keep numeral, which may be folded with 2nd operand */
/* else *//* FALLTHROUGH */
}
case OPR_EQ: case OPR_LT: case OPR_LE:
case OPR_NE: case OPR_GT: case OPR_GE: {
luaK_exp2RK(fs, v);
luaK_exp2anyreg(fs, v);
break;
}
default: lua_assert(0);

View File

@ -1,5 +1,5 @@
/*
** $Id: lopcodes.c,v 1.62 2017/09/15 14:19:06 roberto Exp roberto $
** $Id: lopcodes.c,v 1.63 2017/09/19 18:38:14 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -101,18 +101,18 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */
,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */
,opmode(0, 1, OpArgR, OpArgU, iABC) /* OP_ADDI */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_IDIV */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BAND */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BOR */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_BXOR */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_ADD */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_SUB */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_MUL */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_MOD */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_POW */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_DIV */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_IDIV */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_BAND */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_BOR */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_BXOR */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_SHL */
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_SHR */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */
@ -120,9 +120,9 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */
,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */
,opmode(0, 0, OpArgU, OpArgN, iAsBx) /* OP_JMP */
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */
,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */
,opmode(1, 0, OpArgR, OpArgR, iABC) /* OP_EQ */
,opmode(1, 0, OpArgR, OpArgR, iABC) /* OP_LT */
,opmode(1, 0, OpArgR, OpArgR, iABC) /* OP_LE */
,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */
,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */

View File

@ -1,5 +1,5 @@
/*
** $Id: lopcodes.h,v 1.159 2017/09/18 16:07:54 roberto Exp roberto $
** $Id: lopcodes.h,v 1.160 2017/09/19 18:38:14 roberto Exp roberto $
** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -198,18 +198,18 @@ OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */
OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C):string] */
OP_ADDI,/* A B C R(A) := R(B) + C */
OP_ADD,/* A B C R(A) := RK(B) + RK(C) */
OP_SUB,/* A B C R(A) := RK(B) - RK(C) */
OP_MUL,/* A B C R(A) := RK(B) * RK(C) */
OP_MOD,/* A B C R(A) := RK(B) % RK(C) */
OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */
OP_DIV,/* A B C R(A) := RK(B) / RK(C) */
OP_IDIV,/* A B C R(A) := RK(B) // RK(C) */
OP_BAND,/* A B C R(A) := RK(B) & RK(C) */
OP_BOR,/* A B C R(A) := RK(B) | RK(C) */
OP_BXOR,/* A B C R(A) := RK(B) ~ RK(C) */
OP_SHL,/* A B C R(A) := RK(B) << RK(C) */
OP_SHR,/* A B C R(A) := RK(B) >> RK(C) */
OP_ADD,/* A B C R(A) := R(B) + R(C) */
OP_SUB,/* A B C R(A) := R(B) - R(C) */
OP_MUL,/* A B C R(A) := R(B) * R(C) */
OP_MOD,/* A B C R(A) := R(B) % R(C) */
OP_POW,/* A B C R(A) := R(B) ^ R(C) */
OP_DIV,/* A B C R(A) := R(B) / R(C) */
OP_IDIV,/* A B C R(A) := R(B) // R(C) */
OP_BAND,/* A B C R(A) := R(B) & R(C) */
OP_BOR,/* A B C R(A) := R(B) | R(C) */
OP_BXOR,/* A B C R(A) := R(B) ~ R(C) */
OP_SHL,/* A B C R(A) := R(B) << R(C) */
OP_SHR,/* A B C R(A) := R(B) >> R(C) */
OP_UNM,/* A B R(A) := -R(B) */
OP_BNOT,/* A B R(A) := ~R(B) */
OP_NOT,/* A B R(A) := not R(B) */
@ -219,9 +219,9 @@ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
OP_CLOSE,/* A close all upvalues >= R(A) */
OP_JMP,/* sBx pc+=sBx */
OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
OP_EQ,/* A B C if ((R(B) == R(C)) ~= A) then pc++ */
OP_LT,/* A B C if ((R(B) < R(C)) ~= A) then pc++ */
OP_LE,/* A B C if ((R(B) <= R(C)) ~= A) then pc++ */
OP_TEST,/* A C if not (R(A) <=> C) then pc++ */
OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */

64
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.292 2017/09/13 19:50:08 roberto Exp roberto $
** $Id: lvm.c,v 2.293 2017/09/19 18:38:14 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -739,8 +739,6 @@ void luaV_finishOp (lua_State *L) {
#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))
#define vRC(i) s2v(RC(i))
#define KC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, k+GETARG_C(i))
#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \
(GETARG_Bk(i)) ? k + GETARG_Br(i) : s2v(base + GETARG_Br(i)))
#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \
(GETARG_Ck(i)) ? k + GETARG_Cr(i) : s2v(base + GETARG_Cr(i)))
@ -1013,8 +1011,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_ADD) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Number nb; lua_Number nc;
if (ttisinteger(rb) && ttisinteger(rc)) {
lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
@ -1027,8 +1025,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_SUB) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Number nb; lua_Number nc;
if (ttisinteger(rb) && ttisinteger(rc)) {
lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
@ -1041,8 +1039,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_MUL) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Number nb; lua_Number nc;
if (ttisinteger(rb) && ttisinteger(rc)) {
lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
@ -1055,8 +1053,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_DIV) { /* float division (always with floats) */
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Number nb; lua_Number nc;
if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
setfltvalue(s2v(ra), luai_numdiv(L, nb, nc));
@ -1065,8 +1063,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_BAND) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Integer ib; lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
setivalue(s2v(ra), intop(&, ib, ic));
@ -1075,8 +1073,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_BOR) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Integer ib; lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
setivalue(s2v(ra), intop(|, ib, ic));
@ -1085,8 +1083,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_BXOR) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Integer ib; lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
setivalue(s2v(ra), intop(^, ib, ic));
@ -1095,8 +1093,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_SHL) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Integer ib; lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
setivalue(s2v(ra), luaV_shiftl(ib, ic));
@ -1105,8 +1103,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_SHR) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Integer ib; lua_Integer ic;
if (tointeger(rb, &ib) && tointeger(rc, &ic)) {
setivalue(s2v(ra), luaV_shiftl(ib, -ic));
@ -1115,8 +1113,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_MOD) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Number nb; lua_Number nc;
if (ttisinteger(rb) && ttisinteger(rc)) {
lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
@ -1131,8 +1129,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_IDIV) { /* floor division */
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Number nb; lua_Number nc;
if (ttisinteger(rb) && ttisinteger(rc)) {
lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
@ -1145,8 +1143,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_POW) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
lua_Number nb; lua_Number nc;
if (tonumberns(rb, nb) && tonumberns(rc, nc)) {
setfltvalue(s2v(ra), luai_numpow(L, nb, nc));
@ -1212,8 +1210,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_EQ) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
Protect(
if (luaV_equalobj(L, rb, rc) != GETARG_A(i))
pc++;
@ -1223,8 +1221,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_LT) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
int res;
if (ttisinteger(rb) && ttisinteger(rc))
res = (ivalue(rb) < ivalue(rc));
@ -1238,8 +1236,8 @@ void luaV_execute (lua_State *L) {
vmbreak;
}
vmcase(OP_LE) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
int res;
if (ttisinteger(rb) && ttisinteger(rc))
res = (ivalue(rb) <= ivalue(rc));