aboutsummaryrefslogtreecommitdiff
path: root/src/lj_parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_parse.c')
-rw-r--r--src/lj_parse.c114
1 files changed, 65 insertions, 49 deletions
diff --git a/src/lj_parse.c b/src/lj_parse.c
index 33955ab8..3ae05446 100644
--- a/src/lj_parse.c
+++ b/src/lj_parse.c
@@ -163,6 +163,12 @@ LJ_STATIC_ASSERT((int)BC_MULVV-(int)BC_ADDVV == (int)OPR_MUL-(int)OPR_ADD);
163LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD); 163LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD);
164LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD); 164LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD);
165 165
166#ifdef LUA_USE_ASSERT
167#define lj_assertFS(c, ...) (lj_assertG_(G(fs->L), (c), __VA_ARGS__))
168#else
169#define lj_assertFS(c, ...) ((void)fs)
170#endif
171
166/* -- Error handling ------------------------------------------------------ */ 172/* -- Error handling ------------------------------------------------------ */
167 173
168LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em) 174LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em)
@@ -200,7 +206,7 @@ static BCReg const_num(FuncState *fs, ExpDesc *e)
200{ 206{
201 lua_State *L = fs->L; 207 lua_State *L = fs->L;
202 TValue *o; 208 TValue *o;
203 lua_assert(expr_isnumk(e)); 209 lj_assertFS(expr_isnumk(e), "bad usage");
204 o = lj_tab_set(L, fs->kt, &e->u.nval); 210 o = lj_tab_set(L, fs->kt, &e->u.nval);
205 if (tvhaskslot(o)) 211 if (tvhaskslot(o))
206 return tvkslot(o); 212 return tvkslot(o);
@@ -225,7 +231,7 @@ static BCReg const_gc(FuncState *fs, GCobj *gc, uint32_t itype)
225/* Add a string constant. */ 231/* Add a string constant. */
226static BCReg const_str(FuncState *fs, ExpDesc *e) 232static BCReg const_str(FuncState *fs, ExpDesc *e)
227{ 233{
228 lua_assert(expr_isstrk(e) || e->k == VGLOBAL); 234 lj_assertFS(expr_isstrk(e) || e->k == VGLOBAL, "bad usage");
229 return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR); 235 return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR);
230} 236}
231 237
@@ -313,7 +319,7 @@ static void jmp_patchins(FuncState *fs, BCPos pc, BCPos dest)
313{ 319{
314 BCIns *jmp = &fs->bcbase[pc].ins; 320 BCIns *jmp = &fs->bcbase[pc].ins;
315 BCPos offset = dest-(pc+1)+BCBIAS_J; 321 BCPos offset = dest-(pc+1)+BCBIAS_J;
316 lua_assert(dest != NO_JMP); 322 lj_assertFS(dest != NO_JMP, "uninitialized jump target");
317 if (offset > BCMAX_D) 323 if (offset > BCMAX_D)
318 err_syntax(fs->ls, LJ_ERR_XJUMP); 324 err_syntax(fs->ls, LJ_ERR_XJUMP);
319 setbc_d(jmp, offset); 325 setbc_d(jmp, offset);
@@ -362,7 +368,7 @@ static void jmp_patch(FuncState *fs, BCPos list, BCPos target)
362 if (target == fs->pc) { 368 if (target == fs->pc) {
363 jmp_tohere(fs, list); 369 jmp_tohere(fs, list);
364 } else { 370 } else {
365 lua_assert(target < fs->pc); 371 lj_assertFS(target < fs->pc, "bad jump target");
366 jmp_patchval(fs, list, target, NO_REG, target); 372 jmp_patchval(fs, list, target, NO_REG, target);
367 } 373 }
368} 374}
@@ -392,7 +398,7 @@ static void bcreg_free(FuncState *fs, BCReg reg)
392{ 398{
393 if (reg >= fs->nactvar) { 399 if (reg >= fs->nactvar) {
394 fs->freereg--; 400 fs->freereg--;
395 lua_assert(reg == fs->freereg); 401 lj_assertFS(reg == fs->freereg, "bad regfree");
396 } 402 }
397} 403}
398 404
@@ -542,7 +548,7 @@ static void expr_toreg_nobranch(FuncState *fs, ExpDesc *e, BCReg reg)
542 } else if (e->k <= VKTRUE) { 548 } else if (e->k <= VKTRUE) {
543 ins = BCINS_AD(BC_KPRI, reg, const_pri(e)); 549 ins = BCINS_AD(BC_KPRI, reg, const_pri(e));
544 } else { 550 } else {
545 lua_assert(e->k == VVOID || e->k == VJMP); 551 lj_assertFS(e->k == VVOID || e->k == VJMP, "bad expr type %d", e->k);
546 return; 552 return;
547 } 553 }
548 bcemit_INS(fs, ins); 554 bcemit_INS(fs, ins);
@@ -637,7 +643,7 @@ static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e)
637 ins = BCINS_AD(BC_GSET, ra, const_str(fs, var)); 643 ins = BCINS_AD(BC_GSET, ra, const_str(fs, var));
638 } else { 644 } else {
639 BCReg ra, rc; 645 BCReg ra, rc;
640 lua_assert(var->k == VINDEXED); 646 lj_assertFS(var->k == VINDEXED, "bad expr type %d", var->k);
641 ra = expr_toanyreg(fs, e); 647 ra = expr_toanyreg(fs, e);
642 rc = var->u.s.aux; 648 rc = var->u.s.aux;
643 if ((int32_t)rc < 0) { 649 if ((int32_t)rc < 0) {
@@ -645,10 +651,12 @@ static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e)
645 } else if (rc > BCMAX_C) { 651 } else if (rc > BCMAX_C) {
646 ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1)); 652 ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1));
647 } else { 653 } else {
654#ifdef LUA_USE_ASSERT
648 /* Free late alloced key reg to avoid assert on free of value reg. */ 655 /* Free late alloced key reg to avoid assert on free of value reg. */
649 /* This can only happen when called from expr_table(). */ 656 /* This can only happen when called from expr_table(). */
650 lua_assert(e->k != VNONRELOC || ra < fs->nactvar || 657 if (e->k == VNONRELOC && ra >= fs->nactvar && rc >= ra)
651 rc < ra || (bcreg_free(fs, rc),1)); 658 bcreg_free(fs, rc);
659#endif
652 ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc); 660 ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc);
653 } 661 }
654 } 662 }
@@ -663,7 +671,7 @@ static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key)
663 expr_free(fs, e); 671 expr_free(fs, e);
664 func = fs->freereg; 672 func = fs->freereg;
665 bcemit_AD(fs, BC_MOV, func+1+LJ_FR2, obj); /* Copy object to 1st argument. */ 673 bcemit_AD(fs, BC_MOV, func+1+LJ_FR2, obj); /* Copy object to 1st argument. */
666 lua_assert(expr_isstrk(key)); 674 lj_assertFS(expr_isstrk(key), "bad usage");
667 idx = const_str(fs, key); 675 idx = const_str(fs, key);
668 if (idx <= BCMAX_C) { 676 if (idx <= BCMAX_C) {
669 bcreg_reserve(fs, 2+LJ_FR2); 677 bcreg_reserve(fs, 2+LJ_FR2);
@@ -803,7 +811,8 @@ static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
803 else 811 else
804 rc = expr_toanyreg(fs, e2); 812 rc = expr_toanyreg(fs, e2);
805 /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */ 813 /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */
806 lua_assert(expr_isnumk(e1) || e1->k == VNONRELOC); 814 lj_assertFS(expr_isnumk(e1) || e1->k == VNONRELOC,
815 "bad expr type %d", e1->k);
807 expr_toval(fs, e1); 816 expr_toval(fs, e1);
808 /* Avoid two consts to satisfy bytecode constraints. */ 817 /* Avoid two consts to satisfy bytecode constraints. */
809 if (expr_isnumk(e1) && !expr_isnumk(e2) && 818 if (expr_isnumk(e1) && !expr_isnumk(e2) &&
@@ -891,19 +900,20 @@ static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2)
891 if (op <= OPR_POW) { 900 if (op <= OPR_POW) {
892 bcemit_arith(fs, op, e1, e2); 901 bcemit_arith(fs, op, e1, e2);
893 } else if (op == OPR_AND) { 902 } else if (op == OPR_AND) {
894 lua_assert(e1->t == NO_JMP); /* List must be closed. */ 903 lj_assertFS(e1->t == NO_JMP, "jump list not closed");
895 expr_discharge(fs, e2); 904 expr_discharge(fs, e2);
896 jmp_append(fs, &e2->f, e1->f); 905 jmp_append(fs, &e2->f, e1->f);
897 *e1 = *e2; 906 *e1 = *e2;
898 } else if (op == OPR_OR) { 907 } else if (op == OPR_OR) {
899 lua_assert(e1->f == NO_JMP); /* List must be closed. */ 908 lj_assertFS(e1->f == NO_JMP, "jump list not closed");
900 expr_discharge(fs, e2); 909 expr_discharge(fs, e2);
901 jmp_append(fs, &e2->t, e1->t); 910 jmp_append(fs, &e2->t, e1->t);
902 *e1 = *e2; 911 *e1 = *e2;
903 } else if (op == OPR_CONCAT) { 912 } else if (op == OPR_CONCAT) {
904 expr_toval(fs, e2); 913 expr_toval(fs, e2);
905 if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) { 914 if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) {
906 lua_assert(e1->u.s.info == bc_b(*bcptr(fs, e2))-1); 915 lj_assertFS(e1->u.s.info == bc_b(*bcptr(fs, e2))-1,
916 "bad CAT stack layout");
907 expr_free(fs, e1); 917 expr_free(fs, e1);
908 setbc_b(bcptr(fs, e2), e1->u.s.info); 918 setbc_b(bcptr(fs, e2), e1->u.s.info);
909 e1->u.s.info = e2->u.s.info; 919 e1->u.s.info = e2->u.s.info;
@@ -915,8 +925,9 @@ static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2)
915 } 925 }
916 e1->k = VRELOCABLE; 926 e1->k = VRELOCABLE;
917 } else { 927 } else {
918 lua_assert(op == OPR_NE || op == OPR_EQ || 928 lj_assertFS(op == OPR_NE || op == OPR_EQ ||
919 op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT); 929 op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT,
930 "bad binop %d", op);
920 bcemit_comp(fs, op, e1, e2); 931 bcemit_comp(fs, op, e1, e2);
921 } 932 }
922} 933}
@@ -945,10 +956,10 @@ static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e)
945 e->u.s.info = fs->freereg-1; 956 e->u.s.info = fs->freereg-1;
946 e->k = VNONRELOC; 957 e->k = VNONRELOC;
947 } else { 958 } else {
948 lua_assert(e->k == VNONRELOC); 959 lj_assertFS(e->k == VNONRELOC, "bad expr type %d", e->k);
949 } 960 }
950 } else { 961 } else {
951 lua_assert(op == BC_UNM || op == BC_LEN); 962 lj_assertFS(op == BC_UNM || op == BC_LEN, "bad unop %d", op);
952 if (op == BC_UNM && !expr_hasjump(e)) { /* Constant-fold negations. */ 963 if (op == BC_UNM && !expr_hasjump(e)) { /* Constant-fold negations. */
953#if LJ_HASFFI 964#if LJ_HASFFI
954 if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */ 965 if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */
@@ -1043,8 +1054,9 @@ static void var_new(LexState *ls, BCReg n, GCstr *name)
1043 lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); 1054 lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);
1044 lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo); 1055 lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);
1045 } 1056 }
1046 lua_assert((uintptr_t)name < VARNAME__MAX || 1057 lj_assertFS((uintptr_t)name < VARNAME__MAX ||
1047 lj_tab_getstr(fs->kt, name) != NULL); 1058 lj_tab_getstr(fs->kt, name) != NULL,
1059 "unanchored variable name");
1048 /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */ 1060 /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */
1049 setgcref(ls->vstack[vtop].name, obj2gco(name)); 1061 setgcref(ls->vstack[vtop].name, obj2gco(name));
1050 fs->varmap[fs->nactvar+n] = (uint16_t)vtop; 1062 fs->varmap[fs->nactvar+n] = (uint16_t)vtop;
@@ -1099,7 +1111,7 @@ static MSize var_lookup_uv(FuncState *fs, MSize vidx, ExpDesc *e)
1099 return i; /* Already exists. */ 1111 return i; /* Already exists. */
1100 /* Otherwise create a new one. */ 1112 /* Otherwise create a new one. */
1101 checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues"); 1113 checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues");
1102 lua_assert(e->k == VLOCAL || e->k == VUPVAL); 1114 lj_assertFS(e->k == VLOCAL || e->k == VUPVAL, "bad expr type %d", e->k);
1103 fs->uvmap[n] = (uint16_t)vidx; 1115 fs->uvmap[n] = (uint16_t)vidx;
1104 fs->uvtmp[n] = (uint16_t)(e->k == VLOCAL ? vidx : LJ_MAX_VSTACK+e->u.s.info); 1116 fs->uvtmp[n] = (uint16_t)(e->k == VLOCAL ? vidx : LJ_MAX_VSTACK+e->u.s.info);
1105 fs->nuv = n+1; 1117 fs->nuv = n+1;
@@ -1150,7 +1162,8 @@ static MSize gola_new(LexState *ls, GCstr *name, uint8_t info, BCPos pc)
1150 lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); 1162 lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);
1151 lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo); 1163 lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);
1152 } 1164 }
1153 lua_assert(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL); 1165 lj_assertFS(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL,
1166 "unanchored label name");
1154 /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */ 1167 /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */
1155 setgcref(ls->vstack[vtop].name, obj2gco(name)); 1168 setgcref(ls->vstack[vtop].name, obj2gco(name));
1156 ls->vstack[vtop].startpc = pc; 1169 ls->vstack[vtop].startpc = pc;
@@ -1180,8 +1193,9 @@ static void gola_close(LexState *ls, VarInfo *vg)
1180 FuncState *fs = ls->fs; 1193 FuncState *fs = ls->fs;
1181 BCPos pc = vg->startpc; 1194 BCPos pc = vg->startpc;
1182 BCIns *ip = &fs->bcbase[pc].ins; 1195 BCIns *ip = &fs->bcbase[pc].ins;
1183 lua_assert(gola_isgoto(vg)); 1196 lj_assertFS(gola_isgoto(vg), "expected goto");
1184 lua_assert(bc_op(*ip) == BC_JMP || bc_op(*ip) == BC_UCLO); 1197 lj_assertFS(bc_op(*ip) == BC_JMP || bc_op(*ip) == BC_UCLO,
1198 "bad bytecode op %d", bc_op(*ip));
1185 setbc_a(ip, vg->slot); 1199 setbc_a(ip, vg->slot);
1186 if (bc_op(*ip) == BC_JMP) { 1200 if (bc_op(*ip) == BC_JMP) {
1187 BCPos next = jmp_next(fs, pc); 1201 BCPos next = jmp_next(fs, pc);
@@ -1200,9 +1214,9 @@ static void gola_resolve(LexState *ls, FuncScope *bl, MSize idx)
1200 if (gcrefeq(vg->name, vl->name) && gola_isgoto(vg)) { 1214 if (gcrefeq(vg->name, vl->name) && gola_isgoto(vg)) {
1201 if (vg->slot < vl->slot) { 1215 if (vg->slot < vl->slot) {
1202 GCstr *name = strref(var_get(ls, ls->fs, vg->slot).name); 1216 GCstr *name = strref(var_get(ls, ls->fs, vg->slot).name);
1203 lua_assert((uintptr_t)name >= VARNAME__MAX); 1217 lj_assertLS((uintptr_t)name >= VARNAME__MAX, "expected goto name");
1204 ls->linenumber = ls->fs->bcbase[vg->startpc].line; 1218 ls->linenumber = ls->fs->bcbase[vg->startpc].line;
1205 lua_assert(strref(vg->name) != NAME_BREAK); 1219 lj_assertLS(strref(vg->name) != NAME_BREAK, "unexpected break");
1206 lj_lex_error(ls, 0, LJ_ERR_XGSCOPE, 1220 lj_lex_error(ls, 0, LJ_ERR_XGSCOPE,
1207 strdata(strref(vg->name)), strdata(name)); 1221 strdata(strref(vg->name)), strdata(name));
1208 } 1222 }
@@ -1266,7 +1280,7 @@ static void fscope_begin(FuncState *fs, FuncScope *bl, int flags)
1266 bl->vstart = fs->ls->vtop; 1280 bl->vstart = fs->ls->vtop;
1267 bl->prev = fs->bl; 1281 bl->prev = fs->bl;
1268 fs->bl = bl; 1282 fs->bl = bl;
1269 lua_assert(fs->freereg == fs->nactvar); 1283 lj_assertFS(fs->freereg == fs->nactvar, "bad regalloc");
1270} 1284}
1271 1285
1272/* End a scope. */ 1286/* End a scope. */
@@ -1277,7 +1291,7 @@ static void fscope_end(FuncState *fs)
1277 fs->bl = bl->prev; 1291 fs->bl = bl->prev;
1278 var_remove(ls, bl->nactvar); 1292 var_remove(ls, bl->nactvar);
1279 fs->freereg = fs->nactvar; 1293 fs->freereg = fs->nactvar;
1280 lua_assert(bl->nactvar == fs->nactvar); 1294 lj_assertFS(bl->nactvar == fs->nactvar, "bad regalloc");
1281 if ((bl->flags & (FSCOPE_UPVAL|FSCOPE_NOCLOSE)) == FSCOPE_UPVAL) 1295 if ((bl->flags & (FSCOPE_UPVAL|FSCOPE_NOCLOSE)) == FSCOPE_UPVAL)
1282 bcemit_AJ(fs, BC_UCLO, bl->nactvar, 0); 1296 bcemit_AJ(fs, BC_UCLO, bl->nactvar, 0);
1283 if ((bl->flags & FSCOPE_BREAK)) { 1297 if ((bl->flags & FSCOPE_BREAK)) {
@@ -1364,13 +1378,13 @@ static void fs_fixup_k(FuncState *fs, GCproto *pt, void *kptr)
1364 Node *n = &node[i]; 1378 Node *n = &node[i];
1365 if (tvhaskslot(&n->val)) { 1379 if (tvhaskslot(&n->val)) {
1366 ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val); 1380 ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val);
1367 lua_assert(!tvisint(&n->key)); 1381 lj_assertFS(!tvisint(&n->key), "unexpected integer key");
1368 if (tvisnum(&n->key)) { 1382 if (tvisnum(&n->key)) {
1369 TValue *tv = &((TValue *)kptr)[kidx]; 1383 TValue *tv = &((TValue *)kptr)[kidx];
1370 if (LJ_DUALNUM) { 1384 if (LJ_DUALNUM) {
1371 lua_Number nn = numV(&n->key); 1385 lua_Number nn = numV(&n->key);
1372 int32_t k = lj_num2int(nn); 1386 int32_t k = lj_num2int(nn);
1373 lua_assert(!tvismzero(&n->key)); 1387 lj_assertFS(!tvismzero(&n->key), "unexpected -0 key");
1374 if ((lua_Number)k == nn) 1388 if ((lua_Number)k == nn)
1375 setintV(tv, k); 1389 setintV(tv, k);
1376 else 1390 else
@@ -1418,21 +1432,21 @@ static void fs_fixup_line(FuncState *fs, GCproto *pt,
1418 uint8_t *li = (uint8_t *)lineinfo; 1432 uint8_t *li = (uint8_t *)lineinfo;
1419 do { 1433 do {
1420 BCLine delta = base[i].line - first; 1434 BCLine delta = base[i].line - first;
1421 lua_assert(delta >= 0 && delta < 256); 1435 lj_assertFS(delta >= 0 && delta < 256, "bad line delta");
1422 li[i] = (uint8_t)delta; 1436 li[i] = (uint8_t)delta;
1423 } while (++i < n); 1437 } while (++i < n);
1424 } else if (LJ_LIKELY(numline < 65536)) { 1438 } else if (LJ_LIKELY(numline < 65536)) {
1425 uint16_t *li = (uint16_t *)lineinfo; 1439 uint16_t *li = (uint16_t *)lineinfo;
1426 do { 1440 do {
1427 BCLine delta = base[i].line - first; 1441 BCLine delta = base[i].line - first;
1428 lua_assert(delta >= 0 && delta < 65536); 1442 lj_assertFS(delta >= 0 && delta < 65536, "bad line delta");
1429 li[i] = (uint16_t)delta; 1443 li[i] = (uint16_t)delta;
1430 } while (++i < n); 1444 } while (++i < n);
1431 } else { 1445 } else {
1432 uint32_t *li = (uint32_t *)lineinfo; 1446 uint32_t *li = (uint32_t *)lineinfo;
1433 do { 1447 do {
1434 BCLine delta = base[i].line - first; 1448 BCLine delta = base[i].line - first;
1435 lua_assert(delta >= 0); 1449 lj_assertFS(delta >= 0, "bad line delta");
1436 li[i] = (uint32_t)delta; 1450 li[i] = (uint32_t)delta;
1437 } while (++i < n); 1451 } while (++i < n);
1438 } 1452 }
@@ -1522,7 +1536,7 @@ static void fs_fixup_ret(FuncState *fs)
1522 } 1536 }
1523 fs->bl->flags |= FSCOPE_NOCLOSE; /* Handled above. */ 1537 fs->bl->flags |= FSCOPE_NOCLOSE; /* Handled above. */
1524 fscope_end(fs); 1538 fscope_end(fs);
1525 lua_assert(fs->bl == NULL); 1539 lj_assertFS(fs->bl == NULL, "bad scope nesting");
1526 /* May need to fixup returns encoded before first function was created. */ 1540 /* May need to fixup returns encoded before first function was created. */
1527 if (fs->flags & PROTO_FIXUP_RETURN) { 1541 if (fs->flags & PROTO_FIXUP_RETURN) {
1528 BCPos pc; 1542 BCPos pc;
@@ -1594,7 +1608,7 @@ static GCproto *fs_finish(LexState *ls, BCLine line)
1594 L->top--; /* Pop table of constants. */ 1608 L->top--; /* Pop table of constants. */
1595 ls->vtop = fs->vbase; /* Reset variable stack. */ 1609 ls->vtop = fs->vbase; /* Reset variable stack. */
1596 ls->fs = fs->prev; 1610 ls->fs = fs->prev;
1597 lua_assert(ls->fs != NULL || ls->tok == TK_eof); 1611 lj_assertL(ls->fs != NULL || ls->tok == TK_eof, "bad parser state");
1598 return pt; 1612 return pt;
1599} 1613}
1600 1614
@@ -1688,14 +1702,15 @@ static void expr_bracket(LexState *ls, ExpDesc *v)
1688} 1702}
1689 1703
1690/* Get value of constant expression. */ 1704/* Get value of constant expression. */
1691static void expr_kvalue(TValue *v, ExpDesc *e) 1705static void expr_kvalue(FuncState *fs, TValue *v, ExpDesc *e)
1692{ 1706{
1707 UNUSED(fs);
1693 if (e->k <= VKTRUE) { 1708 if (e->k <= VKTRUE) {
1694 setpriV(v, ~(uint32_t)e->k); 1709 setpriV(v, ~(uint32_t)e->k);
1695 } else if (e->k == VKSTR) { 1710 } else if (e->k == VKSTR) {
1696 setgcVraw(v, obj2gco(e->u.sval), LJ_TSTR); 1711 setgcVraw(v, obj2gco(e->u.sval), LJ_TSTR);
1697 } else { 1712 } else {
1698 lua_assert(tvisnumber(expr_numtv(e))); 1713 lj_assertFS(tvisnumber(expr_numtv(e)), "bad number constant");
1699 *v = *expr_numtv(e); 1714 *v = *expr_numtv(e);
1700 } 1715 }
1701} 1716}
@@ -1745,11 +1760,11 @@ static void expr_table(LexState *ls, ExpDesc *e)
1745 fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx); 1760 fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx);
1746 } 1761 }
1747 vcall = 0; 1762 vcall = 0;
1748 expr_kvalue(&k, &key); 1763 expr_kvalue(fs, &k, &key);
1749 v = lj_tab_set(fs->L, t, &k); 1764 v = lj_tab_set(fs->L, t, &k);
1750 lj_gc_anybarriert(fs->L, t); 1765 lj_gc_anybarriert(fs->L, t);
1751 if (expr_isk_nojump(&val)) { /* Add const key/value to template table. */ 1766 if (expr_isk_nojump(&val)) { /* Add const key/value to template table. */
1752 expr_kvalue(v, &val); 1767 expr_kvalue(fs, v, &val);
1753 } else { /* Otherwise create dummy string key (avoids lj_tab_newkey). */ 1768 } else { /* Otherwise create dummy string key (avoids lj_tab_newkey). */
1754 settabV(fs->L, v, t); /* Preserve key with table itself as value. */ 1769 settabV(fs->L, v, t); /* Preserve key with table itself as value. */
1755 fixt = 1; /* Fix this later, after all resizes. */ 1770 fixt = 1; /* Fix this later, after all resizes. */
@@ -1768,8 +1783,9 @@ static void expr_table(LexState *ls, ExpDesc *e)
1768 if (vcall) { 1783 if (vcall) {
1769 BCInsLine *ilp = &fs->bcbase[fs->pc-1]; 1784 BCInsLine *ilp = &fs->bcbase[fs->pc-1];
1770 ExpDesc en; 1785 ExpDesc en;
1771 lua_assert(bc_a(ilp->ins) == freg && 1786 lj_assertFS(bc_a(ilp->ins) == freg &&
1772 bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB)); 1787 bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB),
1788 "bad CALL code generation");
1773 expr_init(&en, VKNUM, 0); 1789 expr_init(&en, VKNUM, 0);
1774 en.u.nval.u32.lo = narr-1; 1790 en.u.nval.u32.lo = narr-1;
1775 en.u.nval.u32.hi = 0x43300000; /* Biased integer to avoid denormals. */ 1791 en.u.nval.u32.hi = 0x43300000; /* Biased integer to avoid denormals. */
@@ -1799,7 +1815,7 @@ static void expr_table(LexState *ls, ExpDesc *e)
1799 for (i = 0; i <= hmask; i++) { 1815 for (i = 0; i <= hmask; i++) {
1800 Node *n = &node[i]; 1816 Node *n = &node[i];
1801 if (tvistab(&n->val)) { 1817 if (tvistab(&n->val)) {
1802 lua_assert(tabV(&n->val) == t); 1818 lj_assertFS(tabV(&n->val) == t, "bad dummy key in template table");
1803 setnilV(&n->val); /* Turn value into nil. */ 1819 setnilV(&n->val); /* Turn value into nil. */
1804 } 1820 }
1805 } 1821 }
@@ -1830,7 +1846,7 @@ static BCReg parse_params(LexState *ls, int needself)
1830 } while (lex_opt(ls, ',')); 1846 } while (lex_opt(ls, ','));
1831 } 1847 }
1832 var_add(ls, nparams); 1848 var_add(ls, nparams);
1833 lua_assert(fs->nactvar == nparams); 1849 lj_assertFS(fs->nactvar == nparams, "bad regalloc");
1834 bcreg_reserve(fs, nparams); 1850 bcreg_reserve(fs, nparams);
1835 lex_check(ls, ')'); 1851 lex_check(ls, ')');
1836 return nparams; 1852 return nparams;
@@ -1917,7 +1933,7 @@ static void parse_args(LexState *ls, ExpDesc *e)
1917 err_syntax(ls, LJ_ERR_XFUNARG); 1933 err_syntax(ls, LJ_ERR_XFUNARG);
1918 return; /* Silence compiler. */ 1934 return; /* Silence compiler. */
1919 } 1935 }
1920 lua_assert(e->k == VNONRELOC); 1936 lj_assertFS(e->k == VNONRELOC, "bad expr type %d", e->k);
1921 base = e->u.s.info; /* Base register for call. */ 1937 base = e->u.s.info; /* Base register for call. */
1922 if (args.k == VCALL) { 1938 if (args.k == VCALL) {
1923 ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1 - LJ_FR2); 1939 ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1 - LJ_FR2);
@@ -2687,8 +2703,9 @@ static void parse_chunk(LexState *ls)
2687 while (!islast && !parse_isend(ls->tok)) { 2703 while (!islast && !parse_isend(ls->tok)) {
2688 islast = parse_stmt(ls); 2704 islast = parse_stmt(ls);
2689 lex_opt(ls, ';'); 2705 lex_opt(ls, ';');
2690 lua_assert(ls->fs->framesize >= ls->fs->freereg && 2706 lj_assertLS(ls->fs->framesize >= ls->fs->freereg &&
2691 ls->fs->freereg >= ls->fs->nactvar); 2707 ls->fs->freereg >= ls->fs->nactvar,
2708 "bad regalloc");
2692 ls->fs->freereg = ls->fs->nactvar; /* Free registers after each stmt. */ 2709 ls->fs->freereg = ls->fs->nactvar; /* Free registers after each stmt. */
2693 } 2710 }
2694 synlevel_end(ls); 2711 synlevel_end(ls);
@@ -2723,9 +2740,8 @@ GCproto *lj_parse(LexState *ls)
2723 err_token(ls, TK_eof); 2740 err_token(ls, TK_eof);
2724 pt = fs_finish(ls, ls->linenumber); 2741 pt = fs_finish(ls, ls->linenumber);
2725 L->top--; /* Drop chunkname. */ 2742 L->top--; /* Drop chunkname. */
2726 lua_assert(fs.prev == NULL); 2743 lj_assertL(fs.prev == NULL && ls->fs == NULL, "mismatched frame nesting");
2727 lua_assert(ls->fs == NULL); 2744 lj_assertL(pt->sizeuv == 0, "toplevel proto has upvalues");
2728 lua_assert(pt->sizeuv == 0);
2729 return pt; 2745 return pt;
2730} 2746}
2731 2747