summaryrefslogtreecommitdiff
path: root/src/lj_parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_parse.c')
-rw-r--r--src/lj_parse.c39
1 files changed, 29 insertions, 10 deletions
diff --git a/src/lj_parse.c b/src/lj_parse.c
index 2cbfbe56..c5129ad5 100644
--- a/src/lj_parse.c
+++ b/src/lj_parse.c
@@ -39,8 +39,8 @@ typedef enum {
39 VKLAST = VKNUM, 39 VKLAST = VKNUM,
40 VKCDATA, /* nval = cdata value, not treated as a constant expression */ 40 VKCDATA, /* nval = cdata value, not treated as a constant expression */
41 /* Non-constant expressions follow: */ 41 /* Non-constant expressions follow: */
42 VLOCAL, /* info = local register */ 42 VLOCAL, /* info = local register, aux = vstack index */
43 VUPVAL, /* info = upvalue index */ 43 VUPVAL, /* info = upvalue index, aux = vstack index */
44 VGLOBAL, /* sval = string value */ 44 VGLOBAL, /* sval = string value */
45 VINDEXED, /* info = table register, aux = index reg/byte/string const */ 45 VINDEXED, /* info = table register, aux = index reg/byte/string const */
46 VJMP, /* info = instruction PC */ 46 VJMP, /* info = instruction PC */
@@ -105,6 +105,8 @@ typedef struct FuncScope {
105typedef uint16_t VarIndex; 105typedef uint16_t VarIndex;
106#define LJ_MAX_VSTACK 65536 106#define LJ_MAX_VSTACK 65536
107 107
108#define VSTACK_VAR_RW 0x80000000 /* In endpc: R/W variable. */
109
108/* Upvalue map. */ 110/* Upvalue map. */
109typedef struct UVMap { 111typedef struct UVMap {
110 VarIndex vidx; /* Varinfo index. */ 112 VarIndex vidx; /* Varinfo index. */
@@ -608,10 +610,12 @@ static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e)
608{ 610{
609 BCIns ins; 611 BCIns ins;
610 if (var->k == VLOCAL) { 612 if (var->k == VLOCAL) {
613 fs->ls->vstack[var->u.s.aux].endpc |= VSTACK_VAR_RW;
611 expr_free(fs, e); 614 expr_free(fs, e);
612 expr_toreg(fs, e, var->u.s.info); 615 expr_toreg(fs, e, var->u.s.info);
613 return; 616 return;
614 } else if (var->k == VUPVAL) { 617 } else if (var->k == VUPVAL) {
618 fs->ls->vstack[var->u.s.aux].endpc |= VSTACK_VAR_RW;
615 expr_toval(fs, e); 619 expr_toval(fs, e);
616 if (e->k <= VKTRUE) 620 if (e->k <= VKTRUE)
617 ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e)); 621 ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e));
@@ -1046,8 +1050,11 @@ static void var_add(LexState *ls, BCReg nvars)
1046{ 1050{
1047 FuncState *fs = ls->fs; 1051 FuncState *fs = ls->fs;
1048 fs->nactvar = (uint8_t)(fs->nactvar + nvars); 1052 fs->nactvar = (uint8_t)(fs->nactvar + nvars);
1049 for (; nvars; nvars--) 1053 for (; nvars; nvars--) {
1050 var_get(ls, fs, fs->nactvar - nvars).startpc = fs->pc; 1054 VarInfo *v = &var_get(ls, fs, fs->nactvar - nvars);
1055 v->startpc = fs->pc;
1056 v->endpc = 0;
1057 }
1051} 1058}
1052 1059
1053/* Remove local variables. */ 1060/* Remove local variables. */
@@ -1055,7 +1062,7 @@ static void var_remove(LexState *ls, BCReg tolevel)
1055{ 1062{
1056 FuncState *fs = ls->fs; 1063 FuncState *fs = ls->fs;
1057 while (fs->nactvar > tolevel) 1064 while (fs->nactvar > tolevel)
1058 var_get(ls, fs, --fs->nactvar).endpc = fs->pc; 1065 var_get(ls, fs, --fs->nactvar).endpc |= fs->pc;
1059} 1066}
1060 1067
1061/* Lookup local variable name. */ 1068/* Lookup local variable name. */
@@ -1080,7 +1087,8 @@ static MSize var_lookup_uv(FuncState *fs, MSize vidx, ExpDesc *e)
1080 checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues"); 1087 checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues");
1081 lua_assert(e->k == VLOCAL || e->k == VUPVAL); 1088 lua_assert(e->k == VLOCAL || e->k == VUPVAL);
1082 fs->uvloc[n].vidx = (uint16_t)vidx; 1089 fs->uvloc[n].vidx = (uint16_t)vidx;
1083 fs->uvloc[n].slot = (uint16_t)(e->u.s.info | (e->k == VLOCAL ? 0x8000 : 0)); 1090 fs->uvloc[n].slot = (uint16_t)(e->u.s.info |
1091 (e->k == VLOCAL ? PROTO_UV_LOCAL : 0));
1084 fs->nuv = n+1; 1092 fs->nuv = n+1;
1085 return n; 1093 return n;
1086} 1094}
@@ -1097,7 +1105,7 @@ static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first)
1097 expr_init(e, VLOCAL, reg); 1105 expr_init(e, VLOCAL, reg);
1098 if (!first) 1106 if (!first)
1099 scope_uvmark(fs, reg); /* Scope now has an upvalue. */ 1107 scope_uvmark(fs, reg); /* Scope now has an upvalue. */
1100 return (MSize)fs->varmap[reg]; 1108 return (MSize)(e->u.s.aux = (uint32_t)fs->varmap[reg]);
1101 } else { 1109 } else {
1102 MSize vidx = var_lookup_(fs->prev, name, e, 0); /* Var in outer func? */ 1110 MSize vidx = var_lookup_(fs->prev, name, e, 0); /* Var in outer func? */
1103 if ((int32_t)vidx >= 0) { /* Yes, make it an upvalue here. */ 1111 if ((int32_t)vidx >= 0) { /* Yes, make it an upvalue here. */
@@ -1185,11 +1193,20 @@ static void fs_fixup_k(FuncState *fs, GCproto *pt, void *kptr)
1185/* Fixup upvalues for prototype. */ 1193/* Fixup upvalues for prototype. */
1186static void fs_fixup_uv(FuncState *fs, GCproto *pt, uint16_t *uv) 1194static void fs_fixup_uv(FuncState *fs, GCproto *pt, uint16_t *uv)
1187{ 1195{
1196 VarInfo *vstack;
1197 UVMap *uvloc;
1188 MSize i, n = fs->nuv; 1198 MSize i, n = fs->nuv;
1189 setmref(pt->uv, uv); 1199 setmref(pt->uv, uv);
1190 pt->sizeuv = n; 1200 pt->sizeuv = n;
1191 for (i = 0; i < n; i++) 1201 vstack = fs->ls->vstack;
1192 uv[i] = fs->uvloc[i].slot; 1202 uvloc = fs->uvloc;
1203 for (i = 0; i < n; i++) {
1204 uint16_t slot = uvloc[i].slot;
1205 uint16_t vidx = uvloc[i].vidx;
1206 if ((slot & PROTO_UV_LOCAL) && !(vstack[vidx].endpc & VSTACK_VAR_RW))
1207 slot |= PROTO_UV_IMMUTABLE;
1208 uv[i] = slot;
1209 }
1193} 1210}
1194 1211
1195#ifndef LUAJIT_DISABLE_DEBUGINFO 1212#ifndef LUAJIT_DISABLE_DEBUGINFO
@@ -1287,7 +1304,8 @@ static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar)
1287 /* Store local variable names and compressed ranges. */ 1304 /* Store local variable names and compressed ranges. */
1288 for (i = 0, n = ls->vtop - fs->vbase; i < n; i++) { 1305 for (i = 0, n = ls->vtop - fs->vbase; i < n; i++) {
1289 GCstr *s = strref(vstack[i].name); 1306 GCstr *s = strref(vstack[i].name);
1290 BCPos startpc = vstack[i].startpc, endpc = vstack[i].endpc; 1307 BCPos startpc = vstack[i].startpc;
1308 BCPos endpc = vstack[i].endpc & ~VSTACK_VAR_RW;
1291 if ((uintptr_t)s < VARNAME__MAX) { 1309 if ((uintptr_t)s < VARNAME__MAX) {
1292 fs_buf_need(ls, 1 + 2*5); 1310 fs_buf_need(ls, 1 + 2*5);
1293 ls->sb.buf[ls->sb.n++] = (uint8_t)(uintptr_t)s; 1311 ls->sb.buf[ls->sb.n++] = (uint8_t)(uintptr_t)s;
@@ -2180,6 +2198,7 @@ static void parse_local(LexState *ls)
2180 FuncState *fs = ls->fs; 2198 FuncState *fs = ls->fs;
2181 var_new(ls, 0, lex_str(ls)); 2199 var_new(ls, 0, lex_str(ls));
2182 expr_init(&v, VLOCAL, fs->freereg); 2200 expr_init(&v, VLOCAL, fs->freereg);
2201 v.u.s.aux = fs->varmap[fs->freereg];
2183 bcreg_reserve(fs, 1); 2202 bcreg_reserve(fs, 1);
2184 var_add(ls, 1); 2203 var_add(ls, 1);
2185 parse_body(ls, &b, 0, ls->linenumber); 2204 parse_body(ls, &b, 0, ls->linenumber);