diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-22 13:52:53 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-12-22 13:52:53 -0300 |
commit | 7d4c7ae2af686df4a9f3cc0c6110986d886d55e6 (patch) | |
tree | 19c504432ef6a8631749783fda1b6622296a5a72 /lparser.c | |
parent | 873588dc5f04bfc37006c3dc6ceb9a495ea503f2 (diff) | |
download | lua-7d4c7ae2af686df4a9f3cc0c6110986d886d55e6.tar.gz lua-7d4c7ae2af686df4a9f3cc0c6110986d886d55e6.tar.bz2 lua-7d4c7ae2af686df4a9f3cc0c6110986d886d55e6.zip |
Changes in opcodes for generic 'for'
Again, as the control variable is read only, the code doesn't need
to keep an internal copy of it.
Diffstat (limited to 'lparser.c')
-rw-r--r-- | lparser.c | 23 |
1 files changed, 11 insertions, 12 deletions
@@ -1558,6 +1558,7 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) { | |||
1558 | int prep, endfor; | 1558 | int prep, endfor; |
1559 | checknext(ls, TK_DO); | 1559 | checknext(ls, TK_DO); |
1560 | prep = luaK_codeABx(fs, forprep[isgen], base, 0); | 1560 | prep = luaK_codeABx(fs, forprep[isgen], base, 0); |
1561 | fs->freereg--; /* both 'forprep' remove one register from the stack */ | ||
1561 | enterblock(fs, &bl, 0); /* scope for declared variables */ | 1562 | enterblock(fs, &bl, 0); /* scope for declared variables */ |
1562 | adjustlocalvars(ls, nvars); | 1563 | adjustlocalvars(ls, nvars); |
1563 | luaK_reserveregs(fs, nvars); | 1564 | luaK_reserveregs(fs, nvars); |
@@ -1591,8 +1592,7 @@ static void fornum (LexState *ls, TString *varname, int line) { | |||
1591 | luaK_int(fs, fs->freereg, 1); | 1592 | luaK_int(fs, fs->freereg, 1); |
1592 | luaK_reserveregs(fs, 1); | 1593 | luaK_reserveregs(fs, 1); |
1593 | } | 1594 | } |
1594 | adjustlocalvars(ls, 2); /* start scope for internal state variables */ | 1595 | adjustlocalvars(ls, 2); /* start scope for internal variables */ |
1595 | fs->freereg--; /* OP_FORPREP removes one register from the stack */ | ||
1596 | forbody(ls, base, line, 1, 0); | 1596 | forbody(ls, base, line, 1, 0); |
1597 | } | 1597 | } |
1598 | 1598 | ||
@@ -1601,14 +1601,13 @@ static void forlist (LexState *ls, TString *indexname) { | |||
1601 | /* forlist -> NAME {,NAME} IN explist forbody */ | 1601 | /* forlist -> NAME {,NAME} IN explist forbody */ |
1602 | FuncState *fs = ls->fs; | 1602 | FuncState *fs = ls->fs; |
1603 | expdesc e; | 1603 | expdesc e; |
1604 | int nvars = 5; /* gen, state, control, toclose, 'indexname' */ | 1604 | int nvars = 4; /* function, state, closing, control */ |
1605 | int line; | 1605 | int line; |
1606 | int base = fs->freereg; | 1606 | int base = fs->freereg; |
1607 | /* create control variables */ | 1607 | /* create internal variables */ |
1608 | new_localvarliteral(ls, "(for state)"); | 1608 | new_localvarliteral(ls, "(for state)"); /* iterator function */ |
1609 | new_localvarliteral(ls, "(for state)"); | 1609 | new_localvarliteral(ls, "(for state)"); /* state */ |
1610 | new_localvarliteral(ls, "(for state)"); | 1610 | new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */ |
1611 | new_localvarliteral(ls, "(for state)"); | ||
1612 | new_localvarkind(ls, indexname, RDKCONST); /* control variable */ | 1611 | new_localvarkind(ls, indexname, RDKCONST); /* control variable */ |
1613 | /* other declared variables */ | 1612 | /* other declared variables */ |
1614 | while (testnext(ls, ',')) { | 1613 | while (testnext(ls, ',')) { |
@@ -1618,10 +1617,10 @@ static void forlist (LexState *ls, TString *indexname) { | |||
1618 | checknext(ls, TK_IN); | 1617 | checknext(ls, TK_IN); |
1619 | line = ls->linenumber; | 1618 | line = ls->linenumber; |
1620 | adjust_assign(ls, 4, explist(ls, &e), &e); | 1619 | adjust_assign(ls, 4, explist(ls, &e), &e); |
1621 | adjustlocalvars(ls, 4); /* control variables */ | 1620 | adjustlocalvars(ls, 3); /* start scope for internal variables */ |
1622 | marktobeclosed(fs); /* last control var. must be closed */ | 1621 | marktobeclosed(fs); /* last internal var. must be closed */ |
1623 | luaK_checkstack(fs, 3); /* extra space to call generator */ | 1622 | luaK_checkstack(fs, 2); /* extra space to call iterator */ |
1624 | forbody(ls, base, line, nvars - 4, 1); | 1623 | forbody(ls, base, line, nvars - 3, 1); |
1625 | } | 1624 | } |
1626 | 1625 | ||
1627 | 1626 | ||