From 6a98aa0bb0426acdf4a9ef75b1d3200fd972e77d Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 20 Apr 2017 16:53:55 -0300 Subject: [PATCH] new opcode LOADI (for loading immediate integers) --- lcode.c | 16 ++++++++++++---- lcode.h | 5 ++--- lopcodes.c | 4 +++- lopcodes.h | 3 ++- lparser.c | 4 ++-- lvm.c | 7 ++++++- 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lcode.c b/lcode.c index aca0256d..f48221cf 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.111 2016/07/19 17:12:07 roberto Exp roberto $ +** $Id: lcode.c,v 2.112 2016/12/22 13:08:50 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -343,7 +343,7 @@ static int codeextraarg (FuncState *fs, int a) { ** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX' ** instruction with "extra argument". */ -int luaK_codek (FuncState *fs, int reg, int k) { +static int luaK_codek (FuncState *fs, int reg, int k) { if (k <= MAXARG_Bx) return luaK_codeABx(fs, OP_LOADK, reg, k); else { @@ -468,7 +468,7 @@ int luaK_stringK (FuncState *fs, TString *s) { ** same value; conversion to 'void*' is used only for hashing, so there ** are no "precision" problems. */ -int luaK_intK (FuncState *fs, lua_Integer n) { +static int luaK_intK (FuncState *fs, lua_Integer n) { TValue k, o; setpvalue(&k, cast(void*, cast(size_t, n))); setivalue(&o, n); @@ -507,6 +507,14 @@ static int nilK (FuncState *fs) { } +void luaK_int (FuncState *fs, int reg, lua_Integer i) { + if (-MAXARG_sBx <= i && i <= MAXARG_sBx) + luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i)); + else + luaK_codek(fs, reg, luaK_intK(fs, i)); +} + + /* ** Fix an expression to return the number of results 'nresults'. ** Either 'e' is a multi-ret expression (function call or vararg) @@ -612,7 +620,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { break; } case VKINT: { - luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); + luaK_int(fs, reg, e->u.ival); break; } case VRELOCABLE: { diff --git a/lcode.h b/lcode.h index d2b5100e..e7ff3522 100644 --- a/lcode.h +++ b/lcode.h @@ -1,5 +1,5 @@ /* -** $Id: lcode.h,v 1.63 2013/12/30 20:47:58 roberto Exp roberto $ +** $Id: lcode.h,v 1.64 2016/01/05 16:22:37 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -51,13 +51,12 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); -LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); LUAI_FUNC void luaK_fixline (FuncState *fs, int line); LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); -LUAI_FUNC int luaK_intK (FuncState *fs, lua_Integer n); +LUAI_FUNC void luaK_int (FuncState *fs, int reg, lua_Integer n); LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); diff --git a/lopcodes.c b/lopcodes.c index 10d4bbce..18199764 100644 --- a/lopcodes.c +++ b/lopcodes.c @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.c,v 1.54 2014/11/02 19:19:04 roberto Exp roberto $ +** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -20,6 +20,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { "MOVE", "LOADK", + "LOADI", "LOADKX", "LOADBOOL", "LOADNIL", @@ -75,6 +76,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { /* T A B C mode opcode */ opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ + ,opmode(0, 1, OpArgU, OpArgN, iAsBx) /* OP_LOADI */ ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ diff --git a/lopcodes.h b/lopcodes.h index dd2a7571..afb9205a 100644 --- a/lopcodes.h +++ b/lopcodes.h @@ -1,5 +1,5 @@ /* -** $Id: lopcodes.h,v 1.148 2014/10/25 11:50:46 roberto Exp roberto $ +** $Id: lopcodes.h,v 1.149 2016/07/19 17:12:21 roberto Exp roberto $ ** Opcodes for Lua virtual machine ** See Copyright Notice in lua.h */ @@ -170,6 +170,7 @@ name args description ------------------------------------------------------------------------*/ OP_MOVE,/* A B R(A) := R(B) */ OP_LOADK,/* A Bx R(A) := Kst(Bx) */ +OP_LOADI,/* A sBx R(A) := sBx */ OP_LOADKX,/* A R(A) := Kst(extra arg) */ OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */ diff --git a/lparser.c b/lparser.c index 5894d8ff..25134f11 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.154 2016/06/22 15:48:25 roberto Exp roberto $ +** $Id: lparser.c,v 2.155 2016/08/01 19:51:24 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -1329,7 +1329,7 @@ static void fornum (LexState *ls, TString *varname, int line) { if (testnext(ls, ',')) exp1(ls); /* optional step */ else { /* default step = 1 */ - luaK_codek(fs, fs->freereg, luaK_intK(fs, 1)); + luaK_int(fs, fs->freereg, 1); luaK_reserveregs(fs, 1); } forbody(ls, base, line, 1, 1); diff --git a/lvm.c b/lvm.c index 32958665..a15ebe56 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.269 2017/04/06 13:08:56 roberto Exp roberto $ +** $Id: lvm.c,v 2.270 2017/04/11 18:41:09 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -808,6 +808,11 @@ void luaV_execute (lua_State *L) { setobj2s(L, ra, rb); vmbreak; } + vmcase(OP_LOADI) { + lua_Integer b = GETARG_sBx(i); + setivalue(ra, b); + vmbreak; + } vmcase(OP_LOADKX) { TValue *rb; lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);