summaryrefslogtreecommitdiff
path: root/lparser.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-08-14 15:33:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-08-14 15:33:14 -0300
commitac65bab25f1eaaf70e1826a6937a74b3a55b521b (patch)
treea8220432c19dde69af92e720008863f821d1ce96 /lparser.c
parentf185c0132e09a5e0a32782c674de75b510f887d8 (diff)
downloadlua-github-ac65bab25f1eaaf70e1826a6937a74b3a55b521b.tar.gz
jumps in 'for' loops don't need to be signed
Diffstat (limited to 'lparser.c')
-rw-r--r--lparser.c30
1 files changed, 24 insertions, 6 deletions
diff --git a/lparser.c b/lparser.c
index 3df5a958..2eb5581f 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
/*
-** $Id: lparser.c,v 2.162 2017/06/29 15:38:41 roberto Exp roberto $
+** $Id: lparser.c,v 2.163 2017/08/12 13:12:21 roberto Exp roberto $
** Lua Parser
** See Copyright Notice in lua.h
*/
@@ -1307,6 +1307,22 @@ static int exp1 (LexState *ls) {
}
+/*
+** Fix for instruction at position 'pc' to jump to 'dest'.
+** (Jump addresses are relative in Lua). 'back' true means
+** a back jump.
+*/
+static void fixforjump (FuncState *fs, int pc, int dest, int back) {
+ Instruction *jmp = &fs->f->code[pc];
+ int offset = dest - (pc + 1);
+ if (back)
+ offset = -offset;
+ if (offset > MAXARG_Bx)
+ luaX_syntaxerror(fs->ls, "control structure too long");
+ SETARG_Bx(*jmp, offset);
+}
+
+
static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
/* forbody -> DO block */
BlockCnt bl;
@@ -1314,21 +1330,23 @@ static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
int prep, endfor;
adjustlocalvars(ls, 3); /* control variables */
checknext(ls, TK_DO);
- prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
+ prep = isnum ? luaK_codeABx(fs, OP_FORPREP, base, 0) : luaK_jump(fs);
enterblock(fs, &bl, 0); /* scope for declared variables */
adjustlocalvars(ls, nvars);
luaK_reserveregs(fs, nvars);
block(ls);
leaveblock(fs); /* end of scope for declared variables */
- luaK_patchtohere(fs, prep);
- if (isnum) /* numeric for? */
- endfor = luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP);
+ if (isnum) { /* numeric for? */
+ fixforjump(fs, prep, luaK_getlabel(fs), 0);
+ endfor = luaK_codeABx(fs, OP_FORLOOP, base, 0);
+ }
else { /* generic for */
+ luaK_patchtohere(fs, prep);
luaK_codeABC(fs, OP_TFORCALL, base, 0, nvars);
luaK_fixline(fs, line);
endfor = luaK_codeAsBx(fs, OP_TFORLOOP, base + 2, NO_JUMP);
}
- luaK_patchlist(fs, endfor, prep + 1);
+ fixforjump(fs, endfor, prep + 1, 1);
luaK_fixline(fs, line);
}