aboutsummaryrefslogtreecommitdiff
path: root/lcode.c
diff options
context:
space:
mode:
Diffstat (limited to 'lcode.c')
-rw-r--r--lcode.c84
1 files changed, 52 insertions, 32 deletions
diff --git a/lcode.c b/lcode.c
index e0620791..7e573f02 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 2.149 2018/01/09 11:24:12 roberto Exp roberto $ 2** $Id: lcode.c,v 2.150 2018/01/18 16:24:31 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*/
@@ -60,27 +60,39 @@ static int tonumeral(const expdesc *e, TValue *v) {
60 60
61 61
62/* 62/*
63** Return the previous instruction of the current code. If there
64** may be a jump target between the current instruction and the
65** previous one, return an invalid instruction (to avoid wrong
66** optimizations).
67*/
68static Instruction *previousinstruction (FuncState *fs) {
69 static const Instruction invalidinstruction = -1;
70 if (fs->pc > fs->lasttarget)
71 return &fs->f->code[fs->pc - 1]; /* previous instruction */
72 else
73 return cast(Instruction*, &invalidinstruction);
74}
75
76
77/*
63** Create a OP_LOADNIL instruction, but try to optimize: if the previous 78** Create a OP_LOADNIL instruction, but try to optimize: if the previous
64** instruction is also OP_LOADNIL and ranges are compatible, adjust 79** instruction is also OP_LOADNIL and ranges are compatible, adjust
65** range of previous instruction instead of emitting a new one. (For 80** range of previous instruction instead of emitting a new one. (For
66** instance, 'local a; local b' will generate a single opcode.) 81** instance, 'local a; local b' will generate a single opcode.)
67*/ 82*/
68void luaK_nil (FuncState *fs, int from, int n) { 83void luaK_nil (FuncState *fs, int from, int n) {
69 Instruction *previous;
70 int l = from + n - 1; /* last register to set nil */ 84 int l = from + n - 1; /* last register to set nil */
71 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ 85 Instruction *previous = previousinstruction(fs);
72 previous = &fs->f->code[fs->pc-1]; 86 if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */
73 if (GET_OPCODE(*previous) == OP_LOADNIL) { /* previous is LOADNIL? */ 87 int pfrom = GETARG_A(*previous); /* get previous range */
74 int pfrom = GETARG_A(*previous); /* get previous range */ 88 int pl = pfrom + GETARG_B(*previous);
75 int pl = pfrom + GETARG_B(*previous); 89 if ((pfrom <= from && from <= pl + 1) ||
76 if ((pfrom <= from && from <= pl + 1) || 90 (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
77 (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */ 91 if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
78 if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */ 92 if (pl > l) l = pl; /* l = max(l, pl) */
79 if (pl > l) l = pl; /* l = max(l, pl) */ 93 SETARG_A(*previous, from);
80 SETARG_A(*previous, from); 94 SETARG_B(*previous, l - from);
81 SETARG_B(*previous, l - from); 95 return;
82 return;
83 }
84 } /* else go through */ 96 } /* else go through */
85 } 97 }
86 luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */ 98 luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
@@ -1432,7 +1444,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
1432 break; 1444 break;
1433 } 1445 }
1434 case OPR_CONCAT: { 1446 case OPR_CONCAT: {
1435 luaK_exp2nextreg(fs, v); /* operand must be on the 'stack' */ 1447 luaK_exp2nextreg(fs, v); /* operand must be on the stack */
1436 break; 1448 break;
1437 } 1449 }
1438 case OPR_ADD: case OPR_SUB: 1450 case OPR_ADD: case OPR_SUB:
@@ -1463,12 +1475,30 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
1463 } 1475 }
1464} 1476}
1465 1477
1478/*
1479** Create code for '(e1 .. e2)'.
1480** For '(e1 .. e2.1 .. e2.2)' (which is '(e1 .. (e2.1 .. e2.2))',
1481** because concatenation is right associative), merge both CONCATs.
1482*/
1483static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
1484 Instruction *ie2 = previousinstruction(fs);
1485 if (GET_OPCODE(*ie2) == OP_CONCAT) { /* is 'e2' a concatenation? */
1486 int n = GETARG_B(*ie2); /* # of elements concatenated in 'e2' */
1487 lua_assert(e1->u.info + 1 == GETARG_A(*ie2));
1488 freeexp(fs, e2);
1489 SETARG_A(*ie2, e1->u.info); /* correct first element ('e1') */
1490 SETARG_B(*ie2, n + 1); /* will concatenate one more element */
1491 }
1492 else { /* 'e2' is not a concatenation */
1493 luaK_codeABC(fs, OP_CONCAT, e1->u.info, 2, 0); /* new concat opcode */
1494 freeexp(fs, e2);
1495 luaK_fixline(fs, line);
1496 }
1497}
1498
1466 1499
1467/* 1500/*
1468** Finalize code for binary operation, after reading 2nd operand. 1501** Finalize code for binary operation, after reading 2nd operand.
1469** For '(a .. b .. c)' (which is '(a .. (b .. c))', because
1470** concatenation is right associative), merge second CONCAT into first
1471** one.
1472*/ 1502*/
1473void luaK_posfix (FuncState *fs, BinOpr opr, 1503void luaK_posfix (FuncState *fs, BinOpr opr,
1474 expdesc *e1, expdesc *e2, int line) { 1504 expdesc *e1, expdesc *e2, int line) {
@@ -1487,19 +1517,9 @@ void luaK_posfix (FuncState *fs, BinOpr opr,
1487 *e1 = *e2; 1517 *e1 = *e2;
1488 break; 1518 break;
1489 } 1519 }
1490 case OPR_CONCAT: { 1520 case OPR_CONCAT: { /* e1 .. e2 */
1491 luaK_exp2val(fs, e2); 1521 luaK_exp2nextreg(fs, e2);
1492 if (e2->k == VRELOC && 1522 codeconcat(fs, e1, e2, line);
1493 GET_OPCODE(getinstruction(fs, e2)) == OP_CONCAT) {
1494 lua_assert(e1->u.info == GETARG_B(getinstruction(fs, e2))-1);
1495 freeexp(fs, e1);
1496 SETARG_B(getinstruction(fs, e2), e1->u.info);
1497 e1->k = VRELOC; e1->u.info = e2->u.info;
1498 }
1499 else {
1500 luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
1501 codebinexpval(fs, OP_CONCAT, e1, e2, line);
1502 }
1503 break; 1523 break;
1504 } 1524 }
1505 case OPR_ADD: case OPR_MUL: { 1525 case OPR_ADD: case OPR_MUL: {