diff --git a/src/Makefile.dep b/src/Makefile.dep index 5e7f11dc..937fd67f 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -85,7 +85,8 @@ lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_debug.o: lj_debug.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_state.h lj_frame.h lj_bc.h + lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_state.h lj_frame.h \ + lj_bc.h lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ lj_err.h lj_errmsg.h lj_debug.h lj_state.h lj_frame.h lj_bc.h lj_ff.h \ lj_ffdef.h lj_jit.h lj_ir.h lj_trace.h lj_dispatch.h lj_traceerr.h \ @@ -143,8 +144,8 @@ lj_opt_split.o: lj_opt_split.c lj_obj.h lua.h luaconf.h lj_def.h \ lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_ir.h lj_jit.h lj_ircall.h \ lj_iropt.h lj_vm.h lj_parse.o: lj_parse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ - lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_state.h \ - lj_bc.h lj_ctype.h lj_lex.h lj_parse.h lj_vm.h lj_vmevent.h + lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h \ + lj_state.h lj_bc.h lj_ctype.h lj_lex.h lj_parse.h lj_vm.h lj_vmevent.h lj_record.o: lj_record.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \ lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \ diff --git a/src/lib_jit.c b/src/lib_jit.c index 758835aa..818b8787 100644 --- a/src/lib_jit.c +++ b/src/lib_jit.c @@ -181,8 +181,8 @@ LJLIB_CF(jit_util_funcinfo) GCtab *t; lua_createtable(L, 0, 16); /* Increment hash size if fields are added. */ t = tabV(L->top-1); - setintfield(L, t, "linedefined", proto_line(pt, 0)); - setintfield(L, t, "lastlinedefined", pt->lastlinedefined); + setintfield(L, t, "linedefined", pt->firstline); + setintfield(L, t, "lastlinedefined", pt->firstline + pt->numline); setintfield(L, t, "stackslots", pt->framesize); setintfield(L, t, "params", pt->numparams); setintfield(L, t, "bytecodes", (int32_t)pt->sizebc); @@ -254,7 +254,7 @@ LJLIB_CF(jit_util_funcuvname) GCproto *pt = check_Lproto(L, 0); uint32_t idx = (uint32_t)lj_lib_checkint(L, 2); if (idx < pt->sizeuv) { - setstrV(L, L->top-1, proto_uvname(pt, idx)); + setstrV(L, L->top-1, lj_str_newz(L, lj_debug_uvname(pt, idx))); return 1; } return 0; diff --git a/src/lj_api.c b/src/lj_api.c index 4e41ccc0..6a68cd84 100644 --- a/src/lj_api.c +++ b/src/lj_api.c @@ -836,7 +836,7 @@ LUA_API int lua_next(lua_State *L, int idx) LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n) { TValue *val; - const char *name = lj_debug_uvname(index2adr(L, idx), (uint32_t)(n-1), &val); + const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val); if (name) { copyTV(L, L->top, val); incr_top(L); @@ -991,7 +991,7 @@ LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n) TValue *val; const char *name; api_checknelems(L, 1); - name = lj_debug_uvname(f, (uint32_t)(n-1), &val); + name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val); if (name) { L->top--; copyTV(L, val, L->top); diff --git a/src/lj_debug.c b/src/lj_debug.c index a5ffff74..1c4bac43 100644 --- a/src/lj_debug.c +++ b/src/lj_debug.c @@ -8,6 +8,7 @@ #include "lj_obj.h" #include "lj_err.h" +#include "lj_debug.h" #include "lj_str.h" #include "lj_tab.h" #include "lj_state.h" @@ -89,9 +90,17 @@ static BCPos debug_framepc(lua_State *L, GCfunc *fn, cTValue *nextframe) /* Get line number for a bytecode position. */ BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc) { - BCLine *lineinfo = proto_lineinfo(pt); - if (pc < pt->sizebc && lineinfo) - return lineinfo[pc]; + const void *lineinfo = proto_lineinfo(pt); + if (pc < pt->sizebc && lineinfo) { + BCLine first = pt->firstline; + if (pc-- == 0) return first; + if (pt->numline < 256) + return first + (BCLine)((const uint8_t *)lineinfo)[pc]; + else if (pt->numline < 65536) + return first + (BCLine)((const uint16_t *)lineinfo)[pc]; + else + return first + (BCLine)((const uint32_t *)lineinfo)[pc]; + } return 0; } @@ -109,14 +118,49 @@ static BCLine debug_frameline(lua_State *L, GCfunc *fn, cTValue *nextframe) /* -- Variable names ------------------------------------------------------ */ +/* Read ULEB128 value. */ +static uint32_t debug_read_uleb128(const uint8_t **pp) +{ + const uint8_t *p = *pp; + uint32_t v = *p++; + if (LJ_UNLIKELY(v >= 0x80)) { + int sh = 0; + v &= 0x7f; + do { v |= ((*p & 0x7f) << (sh += 7)); } while (*p++ >= 0x80); + } + *pp = p; + return v; +} + /* Get name of a local variable from slot number and PC. */ static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot) { - MSize i; - VarInfo *vi = proto_varinfo(pt); - for (i = 0; i < pt->sizevarinfo && vi[i].startpc <= pc; i++) - if (pc < vi[i].endpc && slot-- == 0) - return strdata(gco2str(gcref(vi[i].name))); + const uint8_t *p = proto_varinfo(pt); + if (p) { + BCPos lastpc = 0; + for (;;) { + const char *name = (const char *)p; + uint32_t vn = *p++; + BCPos startpc, endpc; + if (vn < VARNAME__MAX) { + if (vn == VARNAME_END) break; /* End of varinfo. */ + } else { + while (*p++) ; /* Skip over variable name string. */ + } + lastpc = startpc = lastpc + debug_read_uleb128(&p); + if (startpc > pc) break; + endpc = startpc + debug_read_uleb128(&p); + if (pc < endpc && slot-- == 0) { + if (vn < VARNAME__MAX) { +#define VARNAMESTR(name, str) str "\0" + name = VARNAMEDEF(VARNAMESTR); +#undef VARNAMESTR + if (--vn) while (*name++ || --vn) ; + } + return name; + } + } + } return NULL; } @@ -141,7 +185,17 @@ static TValue *debug_localname(lua_State *L, const lua_Debug *ar, } /* Get name of upvalue. */ -const char *lj_debug_uvname(cTValue *o, uint32_t idx, TValue **tvp) +const char *lj_debug_uvname(GCproto *pt, uint32_t idx) +{ + const uint8_t *p = proto_uvinfo(pt); + lua_assert(idx < pt->sizeuv); + if (!p) return ""; + if (idx) while (*p++ || --idx) ; + return (const char *)p; +} + +/* Get name and value of upvalue. */ +const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp) { if (tvisfunc(o)) { GCfunc *fn = funcV(o); @@ -149,7 +203,7 @@ const char *lj_debug_uvname(cTValue *o, uint32_t idx, TValue **tvp) GCproto *pt = funcproto(fn); if (idx < pt->sizeuv) { *tvp = uvval(&gcref(fn->l.uvptr[idx])->uv); - return strdata(proto_uvname(pt, idx)); + return lj_debug_uvname(pt, idx); } } else { if (idx < fn->c.nupvalues) { @@ -194,7 +248,7 @@ restart: } return "field"; case BC_UGET: - *name = strdata(proto_uvname(pt, bc_d(ins))); + *name = lj_debug_uvname(pt, bc_d(ins)); return "upvalue"; default: return NULL; @@ -234,25 +288,26 @@ const char *lj_debug_funcname(lua_State *L, TValue *frame, const char **name) /* -- Source code locations ----------------------------------------------- */ /* Generate shortened source name. */ -void lj_debug_shortname(char *out, const char *src) +void lj_debug_shortname(char *out, GCstr *str) { + const char *src = strdata(str); if (*src == '=') { - strncpy(out, src+1, LUA_IDSIZE); /* remove first char */ - out[LUA_IDSIZE-1] = '\0'; /* ensures null termination */ - } else if (*src == '@') { /* out = "source", or "...source" */ - size_t l = strlen(++src); /* skip the `@' */ - if (l >= LUA_IDSIZE) { - src += l-(LUA_IDSIZE-4); /* get last part of file name */ - strcpy(out, "..."); - out += 3; + strncpy(out, src+1, LUA_IDSIZE); /* Remove first char. */ + out[LUA_IDSIZE-1] = '\0'; /* Ensures null termination. */ + } else if (*src == '@') { /* Output "source", or "...source". */ + size_t len = str->len-1; + src++; /* Skip the `@' */ + if (len >= LUA_IDSIZE) { + src += len-(LUA_IDSIZE-4); /* Get last part of file name. */ + *out++ = '.'; *out++ = '.'; *out++ = '.'; } strcpy(out, src); - } else { /* out = [string "string"] */ - size_t len; /* Length, up to first control char. */ + } else { /* Output [string "string"]. */ + size_t len; /* Length, up to first control char. */ for (len = 0; len < LUA_IDSIZE-12; len++) if (((const unsigned char *)src)[len] < ' ') break; strcpy(out, "[string \""); out += 9; - if (src[len] != '\0') { /* must truncate? */ + if (src[len] != '\0') { /* Must truncate? */ if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15; strncpy(out, src, len); out += len; strcpy(out, "..."); out += 3; @@ -273,7 +328,7 @@ void lj_debug_addloc(lua_State *L, const char *msg, BCLine line = debug_frameline(L, fn, nextframe); if (line >= 0) { char buf[LUA_IDSIZE]; - lj_debug_shortname(buf, strdata(proto_chunkname(funcproto(fn)))); + lj_debug_shortname(buf, proto_chunkname(funcproto(fn))); lj_str_pushf(L, "%s:%d: %s", buf, line, msg); return; } @@ -286,27 +341,23 @@ void lj_debug_addloc(lua_State *L, const char *msg, void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc) { GCstr *name = proto_chunkname(pt); - if (name) { - const char *s = strdata(name); - MSize i, len = name->len; - BCLine line = lj_debug_line(pt, pc); - if (*s == '@') { - s++; len--; - for (i = len; i > 0; i--) - if (s[i] == '/' || s[i] == '\\') { - s += i+1; - break; - } - lj_str_pushf(L, "%s:%d", s, line); - } else if (len > 40) { - lj_str_pushf(L, "%p:%d", pt, line); - } else if (*s == '=') { - lj_str_pushf(L, "%s:%d", s+1, line); - } else { - lj_str_pushf(L, "\"%s\":%d", s, line); - } + const char *s = strdata(name); + MSize i, len = name->len; + BCLine line = lj_debug_line(pt, pc); + if (*s == '@') { + s++; len--; + for (i = len; i > 0; i--) + if (s[i] == '/' || s[i] == '\\') { + s += i+1; + break; + } + lj_str_pushf(L, "%s:%d", s, line); + } else if (len > 40) { + lj_str_pushf(L, "%p:%d", pt, line); + } else if (*s == '=') { + lj_str_pushf(L, "%s:%d", s+1, line); } else { - lj_str_pushf(L, "%p:%u", pt, pc); + lj_str_pushf(L, "\"%s\":%d", s, line); } } @@ -359,56 +410,63 @@ LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) lua_assert(fn->c.gct == ~LJ_TFUNC); } for (; *what; what++) { - switch (*what) { - case 'S': + if (*what == 'S') { if (isluafunc(fn)) { GCproto *pt = funcproto(fn); - ar->source = strdata(proto_chunkname(pt)); - ar->linedefined = (int)proto_line(pt, 0); - ar->lastlinedefined = (int)pt->lastlinedefined; - ar->what = ar->linedefined ? "Lua" : "main"; + BCLine firstline = pt->firstline; + GCstr *name = proto_chunkname(pt); + ar->source = strdata(name); + lj_debug_shortname(ar->short_src, name); + ar->linedefined = (int)firstline; + ar->lastlinedefined = (int)(firstline + pt->numline); + ar->what = firstline ? "Lua" : "main"; } else { ar->source = "=[C]"; + ar->short_src[0] = '['; + ar->short_src[1] = 'C'; + ar->short_src[2] = ']'; + ar->short_src[3] = '\0'; ar->linedefined = -1; ar->lastlinedefined = -1; ar->what = "C"; } - lj_debug_shortname(ar->short_src, ar->source); - break; - case 'l': + } else if (*what == 'l') { ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1; - break; - case 'u': + } else if (*what == 'u') { ar->nups = fn->c.nupvalues; - break; - case 'n': + } else if (*what == 'n') { ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL; if (ar->namewhat == NULL) { ar->namewhat = ""; ar->name = NULL; } - break; - case 'f': + } else if (*what == 'f') { setfuncV(L, L->top, fn); incr_top(L); - break; - case 'L': + } else if (*what == 'L') { if (isluafunc(fn)) { GCtab *t = lj_tab_new(L, 0, 0); GCproto *pt = funcproto(fn); - BCLine *lineinfo = proto_lineinfo(pt); - MSize i, szl = pt->sizebc; - for (i = 1; i < szl; i++) - setboolV(lj_tab_setint(L, t, lineinfo[i]), 1); + const void *lineinfo = proto_lineinfo(pt); + if (lineinfo) { + BCLine first = pt->firstline; + int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4; + MSize i, szl = pt->sizebc-1; + for (i = 0; i < szl; i++) { + BCLine line = first + + (sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] : + sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] : + (BCLine)((const uint32_t *)lineinfo)[i]); + setboolV(lj_tab_setint(L, t, line), 1); + } + } settabV(L, L->top, t); } else { setnilV(L->top); } incr_top(L); - break; - default: + } else { status = 0; /* Bad option. */ - break; } } return status; diff --git a/src/lj_debug.h b/src/lj_debug.h index 5a1ddbec..f82fdfed 100644 --- a/src/lj_debug.h +++ b/src/lj_debug.h @@ -10,14 +10,32 @@ LJ_FUNC cTValue *lj_debug_frame(lua_State *L, int level, int *size); LJ_FUNC BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc); -LJ_FUNC const char *lj_debug_uvname(cTValue *o, uint32_t idx, TValue **tvp); +LJ_FUNC const char *lj_debug_uvname(GCproto *pt, uint32_t idx); +LJ_FUNC const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp); LJ_FUNC const char *lj_debug_slotname(GCproto *pt, const BCIns *pc, BCReg slot, const char **name); LJ_FUNC const char *lj_debug_funcname(lua_State *L, TValue *frame, const char **name); -LJ_FUNC void lj_debug_shortname(char *out, const char *src); +LJ_FUNC void lj_debug_shortname(char *out, GCstr *str); LJ_FUNC void lj_debug_addloc(lua_State *L, const char *msg, cTValue *frame, cTValue *nextframe); LJ_FUNC void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc); +/* Fixed internal variable names. */ +#define VARNAMEDEF(_) \ + _(FOR_IDX, "(for index)") \ + _(FOR_STOP, "(for limit)") \ + _(FOR_STEP, "(for step)") \ + _(FOR_GEN, "(for generator)") \ + _(FOR_STATE, "(for state)") \ + _(FOR_CTL, "(for control)") + +enum { + VARNAME_END, +#define VARNAMEENUM(name, str) VARNAME_##name, + VARNAMEDEF(VARNAMEENUM) +#undef VARNAMEENUM + VARNAME__MAX +}; + #endif diff --git a/src/lj_err.c b/src/lj_err.c index 8d37af6a..42bb87fc 100644 --- a/src/lj_err.c +++ b/src/lj_err.c @@ -521,7 +521,7 @@ LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em) } /* Lexer error. */ -LJ_NOINLINE void lj_err_lex(lua_State *L, const char *src, const char *tok, +LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok, BCLine line, ErrMsg em, va_list argp) { char buff[LUA_IDSIZE]; diff --git a/src/lj_err.h b/src/lj_err.h index 69ede6d6..c65f484e 100644 --- a/src/lj_err.h +++ b/src/lj_err.h @@ -25,7 +25,7 @@ LJ_FUNCA_NORET void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode); LJ_FUNC_NORET void lj_err_mem(lua_State *L); LJ_FUNC_NORET void lj_err_run(lua_State *L); LJ_FUNC_NORET void lj_err_msg(lua_State *L, ErrMsg em); -LJ_FUNC_NORET void lj_err_lex(lua_State *L, const char *src, const char *tok, +LJ_FUNC_NORET void lj_err_lex(lua_State *L, GCstr *src, const char *tok, BCLine line, ErrMsg em, va_list argp); LJ_FUNC_NORET void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm); LJ_FUNC_NORET void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2); diff --git a/src/lj_gc.c b/src/lj_gc.c index e65f7ffa..37d42467 100644 --- a/src/lj_gc.c +++ b/src/lj_gc.c @@ -257,10 +257,6 @@ static void gc_traverse_proto(global_State *g, GCproto *pt) gc_mark_str(proto_chunkname(pt)); for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) /* Mark collectable consts. */ gc_markobj(g, proto_kgc(pt, i)); - for (i = 0; i < (ptrdiff_t)pt->sizeuv; i++) /* Mark upvalue names. */ - gc_mark_str(proto_uvname(pt, i)); - for (i = 0; i < (ptrdiff_t)pt->sizevarinfo; i++) /* Mark names of locals. */ - gc_mark_str(gco2str(gcref(proto_varinfo(pt)[i].name))); #if LJ_HASJIT if (pt->trace) gc_marktrace(g, pt->trace); #endif diff --git a/src/lj_gdbjit.c b/src/lj_gdbjit.c index 28e063fb..fbaafc04 100644 --- a/src/lj_gdbjit.c +++ b/src/lj_gdbjit.c @@ -731,7 +731,7 @@ void lj_gdbjit_addtrace(jit_State *J, GCtrace *T) ctx.spadj = CFRAME_SIZE_JIT + T->spadjust; lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc); ctx.lineno = lj_debug_line(pt, proto_bcpos(pt, startpc)); - ctx.filename = strdata(proto_chunkname(pt)); + ctx.filename = proto_chunknamestr(pt); if (*ctx.filename == '@' || *ctx.filename == '=') ctx.filename++; else diff --git a/src/lj_lex.c b/src/lj_lex.c index acfcb921..3411aa56 100644 --- a/src/lj_lex.c +++ b/src/lj_lex.c @@ -494,7 +494,7 @@ void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...) tok = lj_lex_token2str(ls, token); } va_start(argp, em); - lj_err_lex(ls->L, strdata(ls->chunkname), tok, ls->linenumber, em, argp); + lj_err_lex(ls->L, ls->chunkname, tok, ls->linenumber, em, argp); va_end(argp); } diff --git a/src/lj_lex.h b/src/lj_lex.h index 0718f5a6..78a7e1a9 100644 --- a/src/lj_lex.h +++ b/src/lj_lex.h @@ -38,6 +38,13 @@ typedef struct BCInsLine { BCLine line; /* Line number for this bytecode. */ } BCInsLine; +/* Info for local variables. Only used during bytecode generation. */ +typedef struct VarInfo { + GCRef name; /* Local variable name. */ + BCPos startpc; /* First point where the local variable is active. */ + BCPos endpc; /* First point where the local variable is dead. */ +} VarInfo; + /* Lua lexer state. */ typedef struct LexState { struct FuncState *fs; /* Current FuncState. Defined in lj_parse.c. */ diff --git a/src/lj_obj.h b/src/lj_obj.h index 6d6782b4..e85cf703 100644 --- a/src/lj_obj.h +++ b/src/lj_obj.h @@ -286,12 +286,6 @@ typedef struct GCcdataVar { #define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef)) #define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1)) -typedef struct VarInfo { - GCRef name; /* Local variable name. */ - BCPos startpc; /* First point where the local variable is active. */ - BCPos endpc; /* First point where the local variable is dead. */ -} VarInfo; - typedef struct GCproto { GCHeader; uint8_t numparams; /* Number of parameters. */ @@ -308,11 +302,11 @@ typedef struct GCproto { uint16_t trace; /* Anchor for chain of root traces. */ /* ------ The following fields are for debugging/tracebacks only ------ */ GCRef chunkname; /* Name of the chunk this function was defined in. */ - BCLine lastlinedefined; /* Last line of the function definition. */ - MSize sizevarinfo; /* Size of local var info array. */ - MRef varinfo; /* Names and extents of local variables. */ - MRef uvname; /* Array of upvalue names (GCRef of GCstr). */ - MRef lineinfo; /* Map from bytecode instructions to source lines. */ + BCLine firstline; /* First line of the function definition. */ + BCLine numline; /* Number of lines for the function definition. */ + MRef lineinfo; /* Compressed map from bytecode ins. to source line. */ + MRef uvinfo; /* Upvalue names. */ + MRef varinfo; /* Names and compressed extents of local variables. */ } GCproto; #define PROTO_IS_VARARG 0x01 @@ -331,14 +325,11 @@ typedef struct GCproto { #define proto_bcpos(pt, pc) ((BCPos)((pc) - proto_bc(pt))) #define proto_uv(pt) (mref((pt)->uv, uint16_t)) -#define proto_uvname(pt, idx) \ - check_exp((uintptr_t)(idx) < (pt)->sizeuv, \ - gco2str(gcref(mref((pt)->uvname, GCRef)[(idx)]))) -#define proto_chunkname(pt) (gco2str(gcref((pt)->chunkname))) -#define proto_lineinfo(pt) (mref((pt)->lineinfo, BCLine)) -#define proto_line(pt, pos) \ - check_exp((uintptr_t)(pos) < (pt)->sizebc, proto_lineinfo(pt)[(pos)]) -#define proto_varinfo(pt) (mref((pt)->varinfo, VarInfo)) +#define proto_chunkname(pt) (strref((pt)->chunkname)) +#define proto_chunknamestr(pt) (strdata(proto_chunkname((pt)))) +#define proto_lineinfo(pt) (mref((pt)->lineinfo, const void)) +#define proto_uvinfo(pt) (mref((pt)->uvinfo, const uint8_t)) +#define proto_varinfo(pt) (mref((pt)->varinfo, const uint8_t)) /* -- Upvalue object ------------------------------------------------------ */ diff --git a/src/lj_parse.c b/src/lj_parse.c index 992034eb..bd9d9a7a 100644 --- a/src/lj_parse.c +++ b/src/lj_parse.c @@ -12,6 +12,7 @@ #include "lj_obj.h" #include "lj_gc.h" #include "lj_err.h" +#include "lj_debug.h" #include "lj_str.h" #include "lj_tab.h" #include "lj_func.h" @@ -1021,7 +1022,8 @@ static void var_new(LexState *ls, BCReg n, GCstr *name) lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo); } - lua_assert(lj_tab_getstr(fs->kt, name) != NULL); + lua_assert((uintptr_t)name < VARNAME__MAX || + lj_tab_getstr(fs->kt, name) != NULL); /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */ setgcref(ls->vstack[vtop].name, obj2gco(name)); fs->varmap[fs->nactvar+n] = (uint16_t)vtop; @@ -1031,6 +1033,9 @@ static void var_new(LexState *ls, BCReg n, GCstr *name) #define var_new_lit(ls, n, v) \ var_new(ls, (n), lj_parse_keepstr(ls, "" v, sizeof(v)-1)) +#define var_new_fixed(ls, n, vn) \ + var_new(ls, (n), (GCstr *)(uintptr_t)(vn)) + /* Add local variables. */ static void var_add(LexState *ls, BCReg nvars) { @@ -1053,7 +1058,7 @@ static BCReg var_lookup_local(FuncState *fs, GCstr *n) { int i; for (i = fs->nactvar-1; i >= 0; i--) { - if (n == gco2str(gcref(var_get(fs->ls, fs, i).name))) + if (n == strref(var_get(fs->ls, fs, i).name)) return (BCReg)i; } return (BCReg)-1; /* Not found. */ @@ -1109,23 +1114,16 @@ static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first) /* -- Function state management ------------------------------------------- */ -/* NYI: compress debug info. */ - -/* Fixup bytecode and lineinfo for prototype. */ -static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, BCLine *lineinfo) +/* Fixup bytecode for prototype. */ +static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, MSize n) { - MSize i, n = fs->pc; BCInsLine *base = fs->bcbase; - setmref(pt->lineinfo, lineinfo); + MSize i; pt->sizebc = n; - bc[n] = ~0u; /* Close potentially uninitialized gap between bc and kgc. */ bc[0] = BCINS_AD((fs->flags & PROTO_IS_VARARG) ? BC_FUNCV : BC_FUNCF, fs->framesize, 0); - lineinfo[0] = fs->linedefined; - for (i = 1; i < n; i++) { + for (i = 1; i < n; i++) bc[i] = base[i].ins; - lineinfo[i] = base[i].line; - } } /* Fixup constants for prototype. */ @@ -1189,20 +1187,138 @@ static void fs_fixup_uv(FuncState *fs, GCproto *pt, uint16_t *uv) uv[i] = fs->uvloc[i].slot; } -/* Fixup debug info for prototype. */ -static void fs_fixup_dbg(FuncState *fs, GCproto *pt, VarInfo *vi, MSize sizevi) +#ifndef LUAJIT_DISABLE_DEBUGINFO +/* Prepare lineinfo for prototype. */ +static size_t fs_prep_line(FuncState *fs, BCLine numline) { - MSize i, n = fs->nuv; - GCRef *uvname = (GCRef *)((char *)vi + sizevi*sizeof(VarInfo)); - VarInfo *vstack = fs->ls->vstack; - setmref(pt->varinfo, vi); - setmref(pt->uvname, uvname); - pt->sizevarinfo = sizevi; - memcpy(vi, &vstack[fs->vbase], sizevi*sizeof(VarInfo)); - for (i = 0; i < n; i++) - setgcref(uvname[i], gcref(vstack[fs->uvloc[i].vidx].name)); + return (fs->pc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2); } +/* Fixup lineinfo for prototype. */ +static void fs_fixup_line(FuncState *fs, GCproto *pt, + void *lineinfo, BCLine numline) +{ + BCInsLine *base = fs->bcbase + 1; + BCLine first = fs->linedefined; + MSize i = 0, n = fs->pc-1; + pt->firstline = fs->linedefined; + pt->numline = numline; + setmref(pt->lineinfo, lineinfo); + if (LJ_LIKELY(numline < 256)) { + uint8_t *li = (uint8_t *)lineinfo; + do { + BCLine delta = base[i].line - first; + lua_assert(delta >= 0 && delta < 256); + li[i] = (uint8_t)delta; + } while (++i < n); + } else if (LJ_LIKELY(numline < 65536)) { + uint16_t *li = (uint16_t *)lineinfo; + do { + BCLine delta = base[i].line - first; + lua_assert(delta >= 0 && delta < 65536); + li[i] = (uint16_t)delta; + } while (++i < n); + } else { + uint32_t *li = (uint32_t *)lineinfo; + do { + BCLine delta = base[i].line - first; + lua_assert(delta >= 0); + li[i] = (uint32_t)delta; + } while (++i < n); + } +} + +/* Resize buffer if needed. */ +static LJ_NOINLINE void fs_buf_resize(LexState *ls, MSize len) +{ + MSize sz = ls->sb.sz * 2; + while (ls->sb.n + len > sz) sz = sz * 2; + lj_str_resizebuf(ls->L, &ls->sb, sz); +} + +static LJ_AINLINE void fs_buf_need(LexState *ls, MSize len) +{ + if (LJ_UNLIKELY(ls->sb.n + len > ls->sb.sz)) + fs_buf_resize(ls, len); +} + +/* Add string to buffer. */ +static void fs_buf_str(LexState *ls, const char *str, MSize len) +{ + char *p = ls->sb.buf + ls->sb.n; + MSize i; + ls->sb.n += len; + for (i = 0; i < len; i++) p[i] = str[i]; +} + +/* Add ULEB128 value to buffer. */ +static void fs_buf_uleb128(LexState *ls, uint32_t v) +{ + MSize n = ls->sb.n; + uint8_t *p = (uint8_t *)ls->sb.buf; + for (; v >= 0x80; v >>= 7) + p[n++] = (uint8_t)((v & 0x7f) | 0x80); + p[n++] = (uint8_t)v; + ls->sb.n = n; +} + +/* Prepare variable info for prototype. */ +static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar) +{ + VarInfo *vstack = fs->ls->vstack; + MSize i, n; + BCPos lastpc; + lj_str_resetbuf(&ls->sb); /* Copy to temp. string buffer. */ + /* Store upvalue names. */ + for (i = 0, n = fs->nuv; i < n; i++) { + GCstr *s = strref(vstack[fs->uvloc[i].vidx].name); + MSize len = s->len+1; + fs_buf_need(ls, len); + fs_buf_str(ls, strdata(s), len); + } + *ofsvar = ls->sb.n; + vstack += fs->vbase; + lastpc = 0; + /* Store local variable names and compressed ranges. */ + for (i = 0, n = ls->vtop - fs->vbase; i < n; i++) { + GCstr *s = strref(vstack[i].name); + BCPos startpc = vstack[i].startpc, endpc = vstack[i].endpc; + if ((uintptr_t)s < VARNAME__MAX) { + fs_buf_need(ls, 1 + 2*5); + ls->sb.buf[ls->sb.n++] = (uint8_t)(uintptr_t)s; + } else { + MSize len = s->len+1; + fs_buf_need(ls, len + 2*5); + fs_buf_str(ls, strdata(s), len); + } + fs_buf_uleb128(ls, startpc-lastpc); + fs_buf_uleb128(ls, endpc-startpc); + lastpc = startpc; + } + fs_buf_need(ls, 1); + ls->sb.buf[ls->sb.n++] = '\0'; /* Terminator for varinfo. */ + return ls->sb.n; +} + +/* Fixup variable info for prototype. */ +static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar) +{ + setmref(pt->uvinfo, p); + setmref(pt->varinfo, (char *)p + ofsvar); + memcpy(p, ls->sb.buf, ls->sb.n); /* Copy from temp. string buffer. */ +} +#else + +/* Initialize with empty debug info, if disabled. */ +#define fs_prep_line(fs, numline) (UNUSED(numline), 0) +#define fs_fixup_line(fs, pt, li, numline) \ + pt->firstline = pt->numline = 0, setmref((pt)->lineinfo, NULL) +#define fs_prep_var(ls, fs, ofsvar) (UNUSED(ofsvar), 0) +#define fs_fixup_var(ls, pt, p, ofsvar) \ + setmref((pt)->uvinfo, NULL), setmref((pt)->varinfo, NULL) + +#endif + /* Check if bytecode op returns. */ static int bcopisret(BCOp op) { @@ -1253,8 +1369,8 @@ static GCproto *fs_finish(LexState *ls, BCLine line) { lua_State *L = ls->L; FuncState *fs = ls->fs; - MSize sizevi; - size_t sizept, ofsk, ofsuv, ofsdbg, ofsli; + BCLine numline = line - fs->linedefined; + size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar; GCproto *pt; /* Apply final fixups. */ @@ -1264,33 +1380,29 @@ static GCproto *fs_finish(LexState *ls, BCLine line) /* Calculate total size of prototype including all colocated arrays. */ sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef); - sizept = (sizept + sizeof(lua_Number)-1) & ~(sizeof(lua_Number)-1); - ofsk = sizept; - sizept += fs->nkn*sizeof(lua_Number); - ofsuv = sizept; - sizept += ((fs->nuv+1)&~1)*2; - ofsdbg = sizept; - sizevi = ls->vtop - fs->vbase; - sizept += sizevi*sizeof(VarInfo) + fs->nuv*sizeof(GCRef); - ofsli = sizept; - sizept += fs->pc*sizeof(BCLine); + sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1); + ofsk = sizept; sizept += fs->nkn*sizeof(TValue); + ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2; + ofsli = sizept; sizept += fs_prep_line(fs, numline); + ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar); /* Allocate prototype and initialize its fields. */ pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept); pt->gct = ~LJ_TPROTO; pt->sizept = (MSize)sizept; - setgcref(pt->chunkname, obj2gco(ls->chunkname)); pt->trace = 0; pt->flags = fs->flags; pt->numparams = fs->numparams; pt->framesize = fs->framesize; - pt->lastlinedefined = line; + setgcref(pt->chunkname, obj2gco(ls->chunkname)); - fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), - (BCLine *)((char *)pt + ofsli)); + /* Close potentially uninitialized gap between bc and kgc. */ + *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(fs->nkgc+1)) = 0; + fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), fs->pc); fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk)); fs_fixup_uv(fs, pt, (uint16_t *)((char *)pt + ofsuv)); - fs_fixup_dbg(fs, pt, (VarInfo *)((char *)pt + ofsdbg), sizevi); + fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline); + fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar); lj_vmevent_send(L, BC, setprotoV(L, L->top++, pt); @@ -1300,12 +1412,6 @@ static GCproto *fs_finish(LexState *ls, BCLine line) ls->vtop = fs->vbase; /* Reset variable stack. */ ls->fs = fs->prev; lua_assert(ls->fs != NULL || ls->token == TK_eof); - /* Re-anchor last string token to avoid GC. */ - if (ls->token == TK_name || ls->token == TK_string) { - /* NOBARRIER: the key is new or kept alive. */ - TValue *tv = lj_tab_setstr(ls->L, ls->fs->kt, strV(&ls->tokenval)); - if (tvisnil(tv)) setboolV(tv, 1); - } return pt; } @@ -1530,8 +1636,6 @@ static void parse_chunk(LexState *ls); static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line) { FuncState fs, *pfs = ls->fs; - BCReg kidx; - BCLine lastline; GCproto *pt; ptrdiff_t oldbase = pfs->bcbase - ls->bcstack; fs_init(ls, &fs); @@ -1541,19 +1645,19 @@ static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line) fs.bclim = pfs->bclim - pfs->pc; bcemit_AD(&fs, BC_FUNCF, 0, 0); /* Placeholder. */ parse_chunk(ls); - lastline = ls->linenumber; - lex_match(ls, TK_end, TK_function, line); - pt = fs_finish(ls, lastline); + if (ls->token != TK_end) lex_match(ls, TK_end, TK_function, line); + pt = fs_finish(ls, (ls->lastline = ls->linenumber)); pfs->bcbase = ls->bcstack + oldbase; /* May have been reallocated. */ pfs->bclim = (BCPos)(ls->sizebcstack - oldbase); /* Store new prototype in the constant array of the parent. */ - kidx = const_gc(pfs, obj2gco(pt), LJ_TPROTO); - expr_init(e, VRELOCABLE, bcemit_AD(pfs, BC_FNEW, 0, kidx)); + expr_init(e, VRELOCABLE, + bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO))); if (!(pfs->flags & PROTO_HAS_FNEW)) { if (pfs->flags & PROTO_HAS_RETURN) pfs->flags |= PROTO_FIXUP_RETURN; pfs->flags |= PROTO_HAS_FNEW; } + lj_lex_next(ls); } /* Parse expression list. Last expression is left open. */ @@ -2146,9 +2250,9 @@ static void parse_for_num(LexState *ls, GCstr *varname, BCLine line) FuncScope bl; BCPos loop, loopend; /* Hidden control variables. */ - var_new_lit(ls, FORL_IDX, "(for index)"); - var_new_lit(ls, FORL_STOP, "(for limit)"); - var_new_lit(ls, FORL_STEP, "(for step)"); + var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX); + var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP); + var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP); /* Visible copy of index variable. */ var_new(ls, FORL_EXT, varname); lex_check(ls, '='); @@ -2220,9 +2324,9 @@ static void parse_for_iter(LexState *ls, GCstr *indexname) FuncScope bl; int isnext; /* Hidden control variables. */ - var_new_lit(ls, nvars++, "(for generator)"); - var_new_lit(ls, nvars++, "(for state)"); - var_new_lit(ls, nvars++, "(for control)"); + var_new_fixed(ls, nvars++, VARNAME_FOR_GEN); + var_new_fixed(ls, nvars++, VARNAME_FOR_STATE); + var_new_fixed(ls, nvars++, VARNAME_FOR_CTL); /* Visible variables returned from iterator. */ var_new(ls, nvars++, indexname); while (lex_opt(ls, ',')) @@ -2374,7 +2478,11 @@ GCproto *lj_parse(LexState *ls) FuncState fs; GCproto *pt; lua_State *L = ls->L; +#ifdef LUAJIT_DISABLE_DEBUGINFO + ls->chunkname = lj_str_newlit(L, "="); +#else ls->chunkname = lj_str_newz(L, ls->chunkarg); +#endif setstrV(L, L->top, ls->chunkname); /* Anchor chunkname string. */ incr_top(L); ls->level = 0; diff --git a/src/lj_trace.c b/src/lj_trace.c index 6b1dd2f6..fbd1b45a 100644 --- a/src/lj_trace.c +++ b/src/lj_trace.c @@ -98,7 +98,7 @@ static void perftools_addtrace(GCtrace *T) static FILE *fp; GCproto *pt = &gcref(T->startpt)->pt; const BCIns *startpc = mref(T->startpc, const BCIns); - const char *name = strdata(proto_chunkname(pt)); + const char *name = proto_chunknamestr(pt); BCLine lineno; if (name[0] == '@' || name[0] == '=') name++;