diff options
-rw-r--r-- | lcode.c | 32 | ||||
-rw-r--r-- | ldebug.c | 5 | ||||
-rw-r--r-- | lopcodes.c | 4 | ||||
-rw-r--r-- | lopcodes.h | 8 | ||||
-rw-r--r-- | lvm.c | 27 |
5 files changed, 65 insertions, 11 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 2.115 2017/04/25 18:28:25 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 2.116 2017/04/25 20:01:14 roberto Exp roberto $ |
3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -964,6 +964,16 @@ static int isKstr (FuncState *fs, expdesc *e) { | |||
964 | 964 | ||
965 | 965 | ||
966 | /* | 966 | /* |
967 | ** Check whether expression 'e' is a literal integer in | ||
968 | ** proper range | ||
969 | */ | ||
970 | static int isKint (expdesc *e) { | ||
971 | return (e->k == VKINT && !hasjumps(e) && | ||
972 | l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C)); | ||
973 | } | ||
974 | |||
975 | |||
976 | /* | ||
967 | ** Create expression 't[k]'. 't' must have its final result already in a | 977 | ** Create expression 't[k]'. 't' must have its final result already in a |
968 | ** register or upvalue. Upvalues can only be indexed by literal strings. | 978 | ** register or upvalue. Upvalues can only be indexed by literal strings. |
969 | */ | 979 | */ |
@@ -1047,10 +1057,24 @@ static void codeunexpval (FuncState *fs, OpCode op, expdesc *e, int line) { | |||
1047 | */ | 1057 | */ |
1048 | static void codebinexpval (FuncState *fs, OpCode op, | 1058 | static void codebinexpval (FuncState *fs, OpCode op, |
1049 | expdesc *e1, expdesc *e2, int line) { | 1059 | expdesc *e1, expdesc *e2, int line) { |
1050 | int rk2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ | 1060 | int v1, v2; |
1051 | int rk1 = luaK_exp2RK(fs, e1); | 1061 | if (op == OP_ADD && (isKint(e1) || isKint(e2))) { |
1062 | if (isKint(e2)) { | ||
1063 | v2 = cast_int(e2->u.ival); | ||
1064 | v1 = luaK_exp2anyreg(fs, e1); | ||
1065 | } | ||
1066 | else { /* exchange operands to make 2nd one a constant */ | ||
1067 | v2 = cast_int(e1->u.ival); | ||
1068 | v1 = luaK_exp2anyreg(fs, e2) | BITRK; /* K bit signal the exchange */ | ||
1069 | } | ||
1070 | op = OP_ADDI; | ||
1071 | } | ||
1072 | else { | ||
1073 | v2 = luaK_exp2RK(fs, e2); /* both operands are "RK" */ | ||
1074 | v1 = luaK_exp2RK(fs, e1); | ||
1075 | } | ||
1052 | freeexps(fs, e1, e2); | 1076 | freeexps(fs, e1, e2); |
1053 | e1->u.info = luaK_codeABC(fs, op, 0, rk1, rk2); /* generate opcode */ | 1077 | e1->u.info = luaK_codeABC(fs, op, 0, v1, v2); /* generate opcode */ |
1054 | e1->k = VRELOCABLE; /* all those operations are relocatable */ | 1078 | e1->k = VRELOCABLE; /* all those operations are relocatable */ |
1055 | luaK_fixline(fs, line); | 1079 | luaK_fixline(fs, line); |
1056 | } | 1080 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldebug.c,v 2.120 2016/03/31 19:01:21 roberto Exp roberto $ | 2 | ** $Id: ldebug.c,v 2.121 2016/10/19 12:32:10 roberto Exp roberto $ |
3 | ** Debug Interface | 3 | ** Debug Interface |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -513,6 +513,9 @@ static const char *funcnamefromcode (lua_State *L, CallInfo *ci, | |||
513 | case OP_SETTABUP: case OP_SETTABLE: | 513 | case OP_SETTABUP: case OP_SETTABLE: |
514 | tm = TM_NEWINDEX; | 514 | tm = TM_NEWINDEX; |
515 | break; | 515 | break; |
516 | case OP_ADDI: | ||
517 | tm = TM_ADD; | ||
518 | break; | ||
516 | case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: | 519 | case OP_ADD: case OP_SUB: case OP_MUL: case OP_MOD: |
517 | case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: | 520 | case OP_POW: case OP_DIV: case OP_IDIV: case OP_BAND: |
518 | case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { | 521 | case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR: { |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lopcodes.c,v 1.55 2015/01/05 13:48:33 roberto Exp roberto $ | 2 | ** $Id: lopcodes.c,v 1.56 2017/04/20 19:53:55 roberto Exp roberto $ |
3 | ** Opcodes for Lua virtual machine | 3 | ** Opcodes for Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -32,6 +32,7 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { | |||
32 | "SETTABLE", | 32 | "SETTABLE", |
33 | "NEWTABLE", | 33 | "NEWTABLE", |
34 | "SELF", | 34 | "SELF", |
35 | "ADDI", | ||
35 | "ADD", | 36 | "ADD", |
36 | "SUB", | 37 | "SUB", |
37 | "MUL", | 38 | "MUL", |
@@ -88,6 +89,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { | |||
88 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ | 89 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ |
89 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ | 90 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ |
90 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ | 91 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ |
92 | ,opmode(0, 1, OpArgR, OpArgU, iABC) /* OP_ADDI */ | ||
91 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ | 93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ |
92 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ | 94 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ |
93 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ | 95 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lopcodes.h,v 1.150 2017/04/20 19:53:55 roberto Exp roberto $ | 2 | ** $Id: lopcodes.h,v 1.151 2017/04/24 20:26:39 roberto Exp roberto $ |
3 | ** Opcodes for Lua virtual machine | 3 | ** Opcodes for Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -90,7 +90,7 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ | |||
90 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ | 90 | #define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ |
91 | ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP)))) | 91 | ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP)))) |
92 | 92 | ||
93 | #define getarg(i,pos,size) (cast(int, ((i)>>pos) & MASK1(size,0))) | 93 | #define getarg(i,pos,size) (cast(int, ((i)>>(pos)) & MASK1(size,0))) |
94 | #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ | 94 | #define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \ |
95 | ((cast(Instruction, v)<<pos)&MASK1(size,pos)))) | 95 | ((cast(Instruction, v)<<pos)&MASK1(size,pos)))) |
96 | 96 | ||
@@ -100,6 +100,9 @@ enum OpMode {iABC, iABx, iAsBx, iAx}; /* basic instruction format */ | |||
100 | #define GETARG_B(i) getarg(i, POS_B, SIZE_B) | 100 | #define GETARG_B(i) getarg(i, POS_B, SIZE_B) |
101 | #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B) | 101 | #define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B) |
102 | 102 | ||
103 | #define GETARG_Br(i) getarg(i, POS_B, SIZE_B - 1) | ||
104 | #define GETARG_Bk(i) getarg(i, (POS_B + SIZE_B - 1), 1) | ||
105 | |||
103 | #define GETARG_C(i) getarg(i, POS_C, SIZE_C) | 106 | #define GETARG_C(i) getarg(i, POS_C, SIZE_C) |
104 | #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) | 107 | #define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C) |
105 | 108 | ||
@@ -187,6 +190,7 @@ OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ | |||
187 | 190 | ||
188 | OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ | 191 | OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ |
189 | 192 | ||
193 | OP_ADDI,/* A B C R(A) := R(B) + C */ | ||
190 | OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ | 194 | OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ |
191 | OP_SUB,/* A B C R(A) := RK(B) - RK(C) */ | 195 | OP_SUB,/* A B C R(A) := RK(B) - RK(C) */ |
192 | OP_MUL,/* A B C R(A) := RK(B) * RK(C) */ | 196 | OP_MUL,/* A B C R(A) := RK(B) * RK(C) */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.271 2017/04/20 19:53:55 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.272 2017/04/24 20:26:39 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -725,10 +725,10 @@ void luaV_finishOp (lua_State *L) { | |||
725 | 725 | ||
726 | 726 | ||
727 | #define RA(i) (base+GETARG_A(i)) | 727 | #define RA(i) (base+GETARG_A(i)) |
728 | #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) | 728 | #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_Br(i)) |
729 | #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) | 729 | #define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) |
730 | #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ | 730 | #define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ |
731 | ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) | 731 | (GETARG_Bk(i)) ? k+GETARG_Br(i) : base+GETARG_Br(i)) |
732 | #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ | 732 | #define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ |
733 | ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) | 733 | ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) |
734 | 734 | ||
@@ -898,6 +898,27 @@ void luaV_execute (lua_State *L) { | |||
898 | else Protect(luaV_finishget(L, rb, rc, ra, slot)); | 898 | else Protect(luaV_finishget(L, rb, rc, ra, slot)); |
899 | vmbreak; | 899 | vmbreak; |
900 | } | 900 | } |
901 | vmcase(OP_ADDI) { | ||
902 | TValue *rb = RB(i); | ||
903 | int ic = GETARG_C(i); | ||
904 | lua_Number nb; | ||
905 | if (ttisinteger(rb)) { | ||
906 | setivalue(ra, intop(+, ivalue(rb), ic)); | ||
907 | } | ||
908 | else if (tonumber(rb, &nb)) { | ||
909 | setfltvalue(ra, luai_numadd(L, nb, cast_num(ic))); | ||
910 | } | ||
911 | else { | ||
912 | TValue aux; TValue *rc; | ||
913 | setivalue(&aux, ic); | ||
914 | if (GETARG_Bk(i)) { /* arguments were exchanged? */ | ||
915 | rc = rb; rb = &aux; /* correct them */ | ||
916 | } | ||
917 | else rc = &aux; | ||
918 | Protect(luaT_trybinTM(L, rb, rc, ra, TM_ADD)); | ||
919 | } | ||
920 | vmbreak; | ||
921 | } | ||
901 | vmcase(OP_ADD) { | 922 | vmcase(OP_ADD) { |
902 | TValue *rb = RKB(i); | 923 | TValue *rb = RKB(i); |
903 | TValue *rc = RKC(i); | 924 | TValue *rc = RKC(i); |