diff options
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 69 |
1 files changed, 55 insertions, 14 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 2.140 2017/11/30 13:29:18 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 2.141 2017/11/30 15:37:16 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 | */ |
@@ -1217,6 +1217,20 @@ static void codebinexpval (FuncState *fs, OpCode op, | |||
1217 | 1217 | ||
1218 | 1218 | ||
1219 | /* | 1219 | /* |
1220 | ** Code binary operators ('+', '-', ...) with immediate operands. | ||
1221 | */ | ||
1222 | static void codebini (FuncState *fs, OpCode op, | ||
1223 | expdesc *e1, expdesc *e2, int k, int line) { | ||
1224 | int v2 = cast_int(e2->u.ival); /* immediate operand */ | ||
1225 | int v1 = luaK_exp2anyreg(fs, e1); | ||
1226 | freeexp(fs, e1); | ||
1227 | e1->u.info = codeABsC(fs, op, 0, v1, v2, k); /* generate opcode */ | ||
1228 | e1->k = VRELOCABLE; /* all those operations are relocatable */ | ||
1229 | luaK_fixline(fs, line); | ||
1230 | } | ||
1231 | |||
1232 | |||
1233 | /* | ||
1220 | ** Code arithmetic operators ('+', '-', ...). If second operand is a | 1234 | ** Code arithmetic operators ('+', '-', ...). If second operand is a |
1221 | ** constant in the proper range, use variant opcodes with immediate | 1235 | ** constant in the proper range, use variant opcodes with immediate |
1222 | ** operands. | 1236 | ** operands. |
@@ -1225,15 +1239,8 @@ static void codearith (FuncState *fs, OpCode op, | |||
1225 | expdesc *e1, expdesc *e2, int flip, int line) { | 1239 | expdesc *e1, expdesc *e2, int flip, int line) { |
1226 | if (!isSCint(e2)) | 1240 | if (!isSCint(e2)) |
1227 | codebinexpval(fs, op, e1, e2, line); /* use standard operators */ | 1241 | codebinexpval(fs, op, e1, e2, line); /* use standard operators */ |
1228 | else { /* use immediate operators */ | 1242 | else /* use immediate operators */ |
1229 | int v2 = cast_int(e2->u.ival); /* immediate operand */ | 1243 | codebini(fs, cast(OpCode, op - OP_ADD + OP_ADDI), e1, e2, flip, line); |
1230 | int v1 = luaK_exp2anyreg(fs, e1); | ||
1231 | op = cast(OpCode, op - OP_ADD + OP_ADDI); | ||
1232 | freeexp(fs, e1); | ||
1233 | e1->u.info = codeABsC(fs, op, 0, v1, v2, flip); /* generate opcode */ | ||
1234 | e1->k = VRELOCABLE; /* all those operations are relocatable */ | ||
1235 | luaK_fixline(fs, line); | ||
1236 | } | ||
1237 | } | 1244 | } |
1238 | 1245 | ||
1239 | 1246 | ||
@@ -1258,11 +1265,30 @@ static void codecommutative (FuncState *fs, OpCode op, | |||
1258 | 1265 | ||
1259 | 1266 | ||
1260 | /* | 1267 | /* |
1268 | ** Code shift operators. If second operand is constant, use immediate | ||
1269 | ** operand (negating it if shift is in the other direction). | ||
1270 | */ | ||
1271 | static void codeshift (FuncState *fs, OpCode op, | ||
1272 | expdesc *e1, expdesc *e2, int line) { | ||
1273 | if (isSCint(e2)) { | ||
1274 | int changedir = 0; | ||
1275 | if (op == OP_SHL) { | ||
1276 | changedir = 1; | ||
1277 | e2->u.ival = -(e2->u.ival); | ||
1278 | } | ||
1279 | codebini(fs, OP_SHRI, e1, e2, changedir, line); | ||
1280 | } | ||
1281 | else | ||
1282 | codebinexpval(fs, op, e1, e2, line); | ||
1283 | } | ||
1284 | |||
1285 | |||
1286 | /* | ||
1261 | ** Emit code for order comparisons. | 1287 | ** Emit code for order comparisons. |
1262 | ** When the first operand is an integral value in the proper range, | 1288 | ** When the first operand is an integral value in the proper range, |
1263 | ** change (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) so that | 1289 | ** change (A < B) to (!(B <= A)) and (A <= B) to (!(B < A)) so that |
1264 | ** it can use an immediate operand. In this case, C indicates this | 1290 | ** it can use an immediate operand. In this case, C indicates this |
1265 | ** change, for cases that cannot assume a total order (NaN and | 1291 | ** change, for cases that cannot assume a total order (NaN and |
1266 | ** metamethods). | 1292 | ** metamethods). |
1267 | */ | 1293 | */ |
1268 | static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { | 1294 | static void codeorder (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { |
@@ -1440,12 +1466,27 @@ void luaK_posfix (FuncState *fs, BinOpr opr, | |||
1440 | codearith(fs, cast(OpCode, opr + OP_ADD), e1, e2, 0, line); | 1466 | codearith(fs, cast(OpCode, opr + OP_ADD), e1, e2, 0, line); |
1441 | break; | 1467 | break; |
1442 | } | 1468 | } |
1443 | case OPR_BAND: case OPR_BOR: case OPR_BXOR: | 1469 | case OPR_BAND: case OPR_BOR: case OPR_BXOR: { |
1444 | case OPR_SHL: case OPR_SHR: { | ||
1445 | if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) | 1470 | if (!constfolding(fs, opr + LUA_OPADD, e1, e2)) |
1446 | codebinexpval(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); | 1471 | codebinexpval(fs, cast(OpCode, opr + OP_ADD), e1, e2, line); |
1447 | break; | 1472 | break; |
1448 | } | 1473 | } |
1474 | case OPR_SHL: { | ||
1475 | if (!constfolding(fs, LUA_OPSHL, e1, e2)) { | ||
1476 | if (isSCint(e1)) { | ||
1477 | swapexps(e1, e2); | ||
1478 | codebini(fs, OP_SHLI, e1, e2, 1, line); | ||
1479 | } | ||
1480 | else | ||
1481 | codeshift(fs, OP_SHL, e1, e2, line); | ||
1482 | } | ||
1483 | break; | ||
1484 | } | ||
1485 | case OPR_SHR: { | ||
1486 | if (!constfolding(fs, LUA_OPSHR, e1, e2)) | ||
1487 | codeshift(fs, OP_SHR, e1, e2, line); | ||
1488 | break; | ||
1489 | } | ||
1449 | case OPR_EQ: case OPR_NE: { | 1490 | case OPR_EQ: case OPR_NE: { |
1450 | codeeq(fs, opr, e1, e2); | 1491 | codeeq(fs, opr, e1, e2); |
1451 | break; | 1492 | break; |
@@ -1483,7 +1524,7 @@ void luaK_fixline (FuncState *fs, int line) { | |||
1483 | } | 1524 | } |
1484 | else { | 1525 | else { |
1485 | fs->previousline -= f->lineinfo[fs->pc - 1]; /* undo previous info. */ | 1526 | fs->previousline -= f->lineinfo[fs->pc - 1]; /* undo previous info. */ |
1486 | savelineinfo(fs, f, fs->pc - 1, line); /* redo it */ | 1527 | savelineinfo(fs, f, fs->pc - 1, line); /* redo it */ |
1487 | } | 1528 | } |
1488 | } | 1529 | } |
1489 | 1530 | ||