diff options
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 32 |
1 files changed, 28 insertions, 4 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 | } |