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.
This commit is contained in:
Roberto Ierusalimschy
2022-12-22 13:52:53 -03:00
parent 873588dc5f
commit 7d4c7ae2af
3 changed files with 38 additions and 29 deletions

View File

@@ -1558,6 +1558,7 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isgen) {
int prep, endfor;
checknext(ls, TK_DO);
prep = luaK_codeABx(fs, forprep[isgen], base, 0);
fs->freereg--; /* both 'forprep' remove one register from the stack */
enterblock(fs, &bl, 0); /* scope for declared variables */
adjustlocalvars(ls, nvars);
luaK_reserveregs(fs, nvars);
@@ -1591,8 +1592,7 @@ static void fornum (LexState *ls, TString *varname, int line) {
luaK_int(fs, fs->freereg, 1);
luaK_reserveregs(fs, 1);
}
adjustlocalvars(ls, 2); /* start scope for internal state variables */
fs->freereg--; /* OP_FORPREP removes one register from the stack */
adjustlocalvars(ls, 2); /* start scope for internal variables */
forbody(ls, base, line, 1, 0);
}
@@ -1601,14 +1601,13 @@ static void forlist (LexState *ls, TString *indexname) {
/* forlist -> NAME {,NAME} IN explist forbody */
FuncState *fs = ls->fs;
expdesc e;
int nvars = 5; /* gen, state, control, toclose, 'indexname' */
int nvars = 4; /* function, state, closing, control */
int line;
int base = fs->freereg;
/* create control variables */
new_localvarliteral(ls, "(for state)");
new_localvarliteral(ls, "(for state)");
new_localvarliteral(ls, "(for state)");
new_localvarliteral(ls, "(for state)");
/* create internal variables */
new_localvarliteral(ls, "(for state)"); /* iterator function */
new_localvarliteral(ls, "(for state)"); /* state */
new_localvarliteral(ls, "(for state)"); /* closing var. (after swap) */
new_localvarkind(ls, indexname, RDKCONST); /* control variable */
/* other declared variables */
while (testnext(ls, ',')) {
@@ -1618,10 +1617,10 @@ static void forlist (LexState *ls, TString *indexname) {
checknext(ls, TK_IN);
line = ls->linenumber;
adjust_assign(ls, 4, explist(ls, &e), &e);
adjustlocalvars(ls, 4); /* control variables */
marktobeclosed(fs); /* last control var. must be closed */
luaK_checkstack(fs, 3); /* extra space to call generator */
forbody(ls, base, line, nvars - 4, 1);
adjustlocalvars(ls, 3); /* start scope for internal variables */
marktobeclosed(fs); /* last internal var. must be closed */
luaK_checkstack(fs, 2); /* extra space to call iterator */
forbody(ls, base, line, nvars - 3, 1);
}