aboutsummaryrefslogtreecommitdiff
path: root/lcode.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-05-06 17:52:46 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-05-06 17:52:46 -0300
commit315639d3bbdff4f83d2ab55863141276cb882af0 (patch)
tree87ba3b6ea20508a7e21cf4fa66d714a2e3f56d9c /lcode.c
parentc764ca71a639f5585b5f466bea25dc42b855a4b0 (diff)
downloadlua-315639d3bbdff4f83d2ab55863141276cb882af0.tar.gz
lua-315639d3bbdff4f83d2ab55863141276cb882af0.tar.bz2
lua-315639d3bbdff4f83d2ab55863141276cb882af0.zip
Factoring out common parts of 'codearith' and 'codebitwise'
Diffstat (limited to 'lcode.c')
-rw-r--r--lcode.c58
1 files changed, 33 insertions, 25 deletions
diff --git a/lcode.c b/lcode.c
index cb724a09..911dbd5f 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1413,6 +1413,18 @@ static void codebini (FuncState *fs, OpCode op,
1413} 1413}
1414 1414
1415 1415
1416/*
1417** Code binary operators with K operand.
1418*/
1419static void codebinK (FuncState *fs, BinOpr opr,
1420 expdesc *e1, expdesc *e2, int flip, int line) {
1421 TMS event = cast(TMS, opr + TM_ADD);
1422 int v2 = e2->u.info; /* K index */
1423 OpCode op = cast(OpCode, opr + OP_ADDK);
1424 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1425}
1426
1427
1416/* Try to code a binary operator negating its second operand. 1428/* Try to code a binary operator negating its second operand.
1417** For the metamethod, 2nd operand must keep its original value. 1429** For the metamethod, 2nd operand must keep its original value.
1418*/ 1430*/
@@ -1441,23 +1453,27 @@ static void swapexps (expdesc *e1, expdesc *e2) {
1441 1453
1442 1454
1443/* 1455/*
1456** Code binary operators with no constant operand.
1457*/
1458static void codebinNoK (FuncState *fs, BinOpr opr,
1459 expdesc *e1, expdesc *e2, int flip, int line) {
1460 OpCode op = cast(OpCode, opr + OP_ADD);
1461 if (flip)
1462 swapexps(e1, e2); /* back to original order */
1463 codebinexpval(fs, op, e1, e2, line); /* use standard operators */
1464}
1465
1466
1467/*
1444** Code arithmetic operators ('+', '-', ...). If second operand is a 1468** Code arithmetic operators ('+', '-', ...). If second operand is a
1445** constant in the proper range, use variant opcodes with K operands. 1469** constant in the proper range, use variant opcodes with K operands.
1446*/ 1470*/
1447static void codearith (FuncState *fs, BinOpr opr, 1471static void codearith (FuncState *fs, BinOpr opr,
1448 expdesc *e1, expdesc *e2, int flip, int line) { 1472 expdesc *e1, expdesc *e2, int flip, int line) {
1449 TMS event = cast(TMS, opr + TM_ADD); 1473 if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) /* K operand? */
1450 if (tonumeral(e2, NULL) && luaK_exp2K(fs, e2)) { /* K operand? */ 1474 codebinK(fs, opr, e1, e2, flip, line);
1451 int v2 = e2->u.info; /* K index */ 1475 else /* 'e2' is neither an immediate nor a K operand */
1452 OpCode op = cast(OpCode, opr + OP_ADDK); 1476 codebinNoK(fs, opr, e1, e2, flip, line);
1453 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK, event);
1454 }
1455 else { /* 'e2' is neither an immediate nor a K operand */
1456 OpCode op = cast(OpCode, opr + OP_ADD);
1457 if (flip)
1458 swapexps(e1, e2); /* back to original order */
1459 codebinexpval(fs, op, e1, e2, line); /* use standard operators */
1460 }
1461} 1477}
1462 1478
1463 1479
@@ -1487,22 +1503,14 @@ static void codecommutative (FuncState *fs, BinOpr op,
1487static void codebitwise (FuncState *fs, BinOpr opr, 1503static void codebitwise (FuncState *fs, BinOpr opr,
1488 expdesc *e1, expdesc *e2, int line) { 1504 expdesc *e1, expdesc *e2, int line) {
1489 int flip = 0; 1505 int flip = 0;
1490 int v2; 1506 if (e1->k == VKINT) {
1491 OpCode op;
1492 if (e1->k == VKINT && luaK_exp2K(fs, e1)) {
1493 swapexps(e1, e2); /* 'e2' will be the constant operand */ 1507 swapexps(e1, e2); /* 'e2' will be the constant operand */
1494 flip = 1; 1508 flip = 1;
1495 } 1509 }
1496 else if (!(e2->k == VKINT && luaK_exp2K(fs, e2))) { /* no constants? */ 1510 if (e2->k == VKINT && luaK_exp2K(fs, e2)) /* K operand? */
1497 op = cast(OpCode, opr + OP_ADD); 1511 codebinK(fs, opr, e1, e2, flip, line);
1498 codebinexpval(fs, op, e1, e2, line); /* all-register opcodes */ 1512 else /* no constants */
1499 return; 1513 codebinNoK(fs, opr, e1, e2, flip, line);
1500 }
1501 v2 = e2->u.info; /* index in K array */
1502 op = cast(OpCode, opr + OP_ADDK);
1503 lua_assert(ttisinteger(&fs->f->k[v2]));
1504 finishbinexpval(fs, e1, e2, op, v2, flip, line, OP_MMBINK,
1505 cast(TMS, opr + TM_ADD));
1506} 1514}
1507 1515
1508 1516