new opcode OP_LOADF (load immediate float)

This commit is contained in:
Roberto Ierusalimschy
2017-09-19 15:38:14 -03:00
parent e0c0e2ee14
commit abb17cf19b
4 changed files with 25 additions and 5 deletions

16
lcode.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.121 2017/06/29 15:06:44 roberto Exp roberto $ ** $Id: lcode.c,v 2.122 2017/09/13 19:50:08 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -579,6 +579,18 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
} }
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
TValue v;
lua_Integer fi;
setfltvalue(&v, f);
if (luaV_tointeger(&v, &fi, 0) &&
l_castS2U(fi) + MAXARG_sBx <= l_castS2U(MAXARG_Bx))
luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
else
luaK_codek(fs, reg, luaK_numberK(fs, f));
}
/* /*
** Fix an expression to return the number of results 'nresults'. ** Fix an expression to return the number of results 'nresults'.
** Either 'e' is a multi-ret expression (function call or vararg) ** Either 'e' is a multi-ret expression (function call or vararg)
@@ -688,7 +700,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
break; break;
} }
case VKFLT: { case VKFLT: {
luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); luaK_float(fs, reg, e->u.nval);
break; break;
} }
case VKINT: { case VKINT: {

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.c,v 1.61 2017/09/13 19:50:08 roberto Exp roberto $ ** $Id: lopcodes.c,v 1.62 2017/09/15 14:19:06 roberto Exp roberto $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -20,6 +20,7 @@
LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
"MOVE", "MOVE",
"LOADI", "LOADI",
"LOADF",
"LOADK", "LOADK",
"LOADKX", "LOADKX",
"LOADBOOL", "LOADBOOL",
@@ -82,6 +83,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
/* T A B C mode opcode */ /* T A B C mode opcode */
opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */
,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADI */ ,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADI */
,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADF */
,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */
,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */
,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lopcodes.h,v 1.158 2017/09/15 14:19:06 roberto Exp roberto $ ** $Id: lopcodes.h,v 1.159 2017/09/18 16:07:54 roberto Exp roberto $
** Opcodes for Lua virtual machine ** Opcodes for Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -175,6 +175,7 @@ name args description
------------------------------------------------------------------------*/ ------------------------------------------------------------------------*/
OP_MOVE,/* A B R(A) := R(B) */ OP_MOVE,/* A B R(A) := R(B) */
OP_LOADI,/* A sBx R(A) := sBx */ OP_LOADI,/* A sBx R(A) := sBx */
OP_LOADF,/* A sBx R(A) := (lua_Number)sBx */
OP_LOADK,/* A Bx R(A) := Kst(Bx) */ OP_LOADK,/* A Bx R(A) := Kst(Bx) */
OP_LOADKX,/* A R(A) := Kst(extra arg) */ OP_LOADKX,/* A R(A) := Kst(extra arg) */
OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */

7
lvm.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.291 2017/08/14 18:33:14 roberto Exp roberto $ ** $Id: lvm.c,v 2.292 2017/09/13 19:50:08 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -829,6 +829,11 @@ void luaV_execute (lua_State *L) {
setivalue(s2v(ra), b); setivalue(s2v(ra), b);
vmbreak; vmbreak;
} }
vmcase(OP_LOADF) {
int b = GETARG_sBx(i);
setfltvalue(s2v(ra), cast_num(b));
vmbreak;
}
vmcase(OP_LOADKX) { vmcase(OP_LOADKX) {
TValue *rb; TValue *rb;
lua_assert(GET_OPCODE(*pc) == OP_EXTRAARG); lua_assert(GET_OPCODE(*pc) == OP_EXTRAARG);