mirror of
https://github.com/LuaJIT/LuaJIT.git
synced 2025-02-07 23:24:09 +00:00
Flatten and compress in-memory debug info (saves ~70%).
This commit is contained in:
parent
585cf05dbd
commit
8c32b38ca3
@ -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_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_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_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_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_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 \
|
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_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_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_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_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h \
|
||||||
lj_bc.h lj_ctype.h lj_lex.h lj_parse.h lj_vm.h lj_vmevent.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_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_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 \
|
lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \
|
||||||
|
@ -181,8 +181,8 @@ LJLIB_CF(jit_util_funcinfo)
|
|||||||
GCtab *t;
|
GCtab *t;
|
||||||
lua_createtable(L, 0, 16); /* Increment hash size if fields are added. */
|
lua_createtable(L, 0, 16); /* Increment hash size if fields are added. */
|
||||||
t = tabV(L->top-1);
|
t = tabV(L->top-1);
|
||||||
setintfield(L, t, "linedefined", proto_line(pt, 0));
|
setintfield(L, t, "linedefined", pt->firstline);
|
||||||
setintfield(L, t, "lastlinedefined", pt->lastlinedefined);
|
setintfield(L, t, "lastlinedefined", pt->firstline + pt->numline);
|
||||||
setintfield(L, t, "stackslots", pt->framesize);
|
setintfield(L, t, "stackslots", pt->framesize);
|
||||||
setintfield(L, t, "params", pt->numparams);
|
setintfield(L, t, "params", pt->numparams);
|
||||||
setintfield(L, t, "bytecodes", (int32_t)pt->sizebc);
|
setintfield(L, t, "bytecodes", (int32_t)pt->sizebc);
|
||||||
@ -254,7 +254,7 @@ LJLIB_CF(jit_util_funcuvname)
|
|||||||
GCproto *pt = check_Lproto(L, 0);
|
GCproto *pt = check_Lproto(L, 0);
|
||||||
uint32_t idx = (uint32_t)lj_lib_checkint(L, 2);
|
uint32_t idx = (uint32_t)lj_lib_checkint(L, 2);
|
||||||
if (idx < pt->sizeuv) {
|
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 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -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)
|
LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n)
|
||||||
{
|
{
|
||||||
TValue *val;
|
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) {
|
if (name) {
|
||||||
copyTV(L, L->top, val);
|
copyTV(L, L->top, val);
|
||||||
incr_top(L);
|
incr_top(L);
|
||||||
@ -991,7 +991,7 @@ LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n)
|
|||||||
TValue *val;
|
TValue *val;
|
||||||
const char *name;
|
const char *name;
|
||||||
api_checknelems(L, 1);
|
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) {
|
if (name) {
|
||||||
L->top--;
|
L->top--;
|
||||||
copyTV(L, val, L->top);
|
copyTV(L, val, L->top);
|
||||||
|
194
src/lj_debug.c
194
src/lj_debug.c
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "lj_obj.h"
|
#include "lj_obj.h"
|
||||||
#include "lj_err.h"
|
#include "lj_err.h"
|
||||||
|
#include "lj_debug.h"
|
||||||
#include "lj_str.h"
|
#include "lj_str.h"
|
||||||
#include "lj_tab.h"
|
#include "lj_tab.h"
|
||||||
#include "lj_state.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. */
|
/* Get line number for a bytecode position. */
|
||||||
BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc)
|
BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc)
|
||||||
{
|
{
|
||||||
BCLine *lineinfo = proto_lineinfo(pt);
|
const void *lineinfo = proto_lineinfo(pt);
|
||||||
if (pc < pt->sizebc && lineinfo)
|
if (pc < pt->sizebc && lineinfo) {
|
||||||
return lineinfo[pc];
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,14 +118,49 @@ static BCLine debug_frameline(lua_State *L, GCfunc *fn, cTValue *nextframe)
|
|||||||
|
|
||||||
/* -- Variable names ------------------------------------------------------ */
|
/* -- 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. */
|
/* Get name of a local variable from slot number and PC. */
|
||||||
static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot)
|
static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot)
|
||||||
{
|
{
|
||||||
MSize i;
|
const uint8_t *p = proto_varinfo(pt);
|
||||||
VarInfo *vi = proto_varinfo(pt);
|
if (p) {
|
||||||
for (i = 0; i < pt->sizevarinfo && vi[i].startpc <= pc; i++)
|
BCPos lastpc = 0;
|
||||||
if (pc < vi[i].endpc && slot-- == 0)
|
for (;;) {
|
||||||
return strdata(gco2str(gcref(vi[i].name)));
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,7 +185,17 @@ static TValue *debug_localname(lua_State *L, const lua_Debug *ar,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Get name of upvalue. */
|
/* 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)) {
|
if (tvisfunc(o)) {
|
||||||
GCfunc *fn = funcV(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);
|
GCproto *pt = funcproto(fn);
|
||||||
if (idx < pt->sizeuv) {
|
if (idx < pt->sizeuv) {
|
||||||
*tvp = uvval(&gcref(fn->l.uvptr[idx])->uv);
|
*tvp = uvval(&gcref(fn->l.uvptr[idx])->uv);
|
||||||
return strdata(proto_uvname(pt, idx));
|
return lj_debug_uvname(pt, idx);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (idx < fn->c.nupvalues) {
|
if (idx < fn->c.nupvalues) {
|
||||||
@ -194,7 +248,7 @@ restart:
|
|||||||
}
|
}
|
||||||
return "field";
|
return "field";
|
||||||
case BC_UGET:
|
case BC_UGET:
|
||||||
*name = strdata(proto_uvname(pt, bc_d(ins)));
|
*name = lj_debug_uvname(pt, bc_d(ins));
|
||||||
return "upvalue";
|
return "upvalue";
|
||||||
default:
|
default:
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -234,25 +288,26 @@ const char *lj_debug_funcname(lua_State *L, TValue *frame, const char **name)
|
|||||||
/* -- Source code locations ----------------------------------------------- */
|
/* -- Source code locations ----------------------------------------------- */
|
||||||
|
|
||||||
/* Generate shortened source name. */
|
/* 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 == '=') {
|
if (*src == '=') {
|
||||||
strncpy(out, src+1, LUA_IDSIZE); /* remove first char */
|
strncpy(out, src+1, LUA_IDSIZE); /* Remove first char. */
|
||||||
out[LUA_IDSIZE-1] = '\0'; /* ensures null termination */
|
out[LUA_IDSIZE-1] = '\0'; /* Ensures null termination. */
|
||||||
} else if (*src == '@') { /* out = "source", or "...source" */
|
} else if (*src == '@') { /* Output "source", or "...source". */
|
||||||
size_t l = strlen(++src); /* skip the `@' */
|
size_t len = str->len-1;
|
||||||
if (l >= LUA_IDSIZE) {
|
src++; /* Skip the `@' */
|
||||||
src += l-(LUA_IDSIZE-4); /* get last part of file name */
|
if (len >= LUA_IDSIZE) {
|
||||||
strcpy(out, "...");
|
src += len-(LUA_IDSIZE-4); /* Get last part of file name. */
|
||||||
out += 3;
|
*out++ = '.'; *out++ = '.'; *out++ = '.';
|
||||||
}
|
}
|
||||||
strcpy(out, src);
|
strcpy(out, src);
|
||||||
} else { /* out = [string "string"] */
|
} else { /* Output [string "string"]. */
|
||||||
size_t len; /* Length, up to first control char. */
|
size_t len; /* Length, up to first control char. */
|
||||||
for (len = 0; len < LUA_IDSIZE-12; len++)
|
for (len = 0; len < LUA_IDSIZE-12; len++)
|
||||||
if (((const unsigned char *)src)[len] < ' ') break;
|
if (((const unsigned char *)src)[len] < ' ') break;
|
||||||
strcpy(out, "[string \""); out += 9;
|
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;
|
if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15;
|
||||||
strncpy(out, src, len); out += len;
|
strncpy(out, src, len); out += len;
|
||||||
strcpy(out, "..."); out += 3;
|
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);
|
BCLine line = debug_frameline(L, fn, nextframe);
|
||||||
if (line >= 0) {
|
if (line >= 0) {
|
||||||
char buf[LUA_IDSIZE];
|
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);
|
lj_str_pushf(L, "%s:%d: %s", buf, line, msg);
|
||||||
return;
|
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)
|
void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc)
|
||||||
{
|
{
|
||||||
GCstr *name = proto_chunkname(pt);
|
GCstr *name = proto_chunkname(pt);
|
||||||
if (name) {
|
const char *s = strdata(name);
|
||||||
const char *s = strdata(name);
|
MSize i, len = name->len;
|
||||||
MSize i, len = name->len;
|
BCLine line = lj_debug_line(pt, pc);
|
||||||
BCLine line = lj_debug_line(pt, pc);
|
if (*s == '@') {
|
||||||
if (*s == '@') {
|
s++; len--;
|
||||||
s++; len--;
|
for (i = len; i > 0; i--)
|
||||||
for (i = len; i > 0; i--)
|
if (s[i] == '/' || s[i] == '\\') {
|
||||||
if (s[i] == '/' || s[i] == '\\') {
|
s += i+1;
|
||||||
s += i+1;
|
break;
|
||||||
break;
|
}
|
||||||
}
|
lj_str_pushf(L, "%s:%d", s, line);
|
||||||
lj_str_pushf(L, "%s:%d", s, line);
|
} else if (len > 40) {
|
||||||
} else if (len > 40) {
|
lj_str_pushf(L, "%p:%d", pt, line);
|
||||||
lj_str_pushf(L, "%p:%d", pt, line);
|
} else if (*s == '=') {
|
||||||
} else if (*s == '=') {
|
lj_str_pushf(L, "%s:%d", s+1, line);
|
||||||
lj_str_pushf(L, "%s:%d", s+1, line);
|
|
||||||
} else {
|
|
||||||
lj_str_pushf(L, "\"%s\":%d", s, line);
|
|
||||||
}
|
|
||||||
} else {
|
} 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);
|
lua_assert(fn->c.gct == ~LJ_TFUNC);
|
||||||
}
|
}
|
||||||
for (; *what; what++) {
|
for (; *what; what++) {
|
||||||
switch (*what) {
|
if (*what == 'S') {
|
||||||
case 'S':
|
|
||||||
if (isluafunc(fn)) {
|
if (isluafunc(fn)) {
|
||||||
GCproto *pt = funcproto(fn);
|
GCproto *pt = funcproto(fn);
|
||||||
ar->source = strdata(proto_chunkname(pt));
|
BCLine firstline = pt->firstline;
|
||||||
ar->linedefined = (int)proto_line(pt, 0);
|
GCstr *name = proto_chunkname(pt);
|
||||||
ar->lastlinedefined = (int)pt->lastlinedefined;
|
ar->source = strdata(name);
|
||||||
ar->what = ar->linedefined ? "Lua" : "main";
|
lj_debug_shortname(ar->short_src, name);
|
||||||
|
ar->linedefined = (int)firstline;
|
||||||
|
ar->lastlinedefined = (int)(firstline + pt->numline);
|
||||||
|
ar->what = firstline ? "Lua" : "main";
|
||||||
} else {
|
} else {
|
||||||
ar->source = "=[C]";
|
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->linedefined = -1;
|
||||||
ar->lastlinedefined = -1;
|
ar->lastlinedefined = -1;
|
||||||
ar->what = "C";
|
ar->what = "C";
|
||||||
}
|
}
|
||||||
lj_debug_shortname(ar->short_src, ar->source);
|
} else if (*what == 'l') {
|
||||||
break;
|
|
||||||
case 'l':
|
|
||||||
ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1;
|
ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1;
|
||||||
break;
|
} else if (*what == 'u') {
|
||||||
case 'u':
|
|
||||||
ar->nups = fn->c.nupvalues;
|
ar->nups = fn->c.nupvalues;
|
||||||
break;
|
} else if (*what == 'n') {
|
||||||
case 'n':
|
|
||||||
ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL;
|
ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL;
|
||||||
if (ar->namewhat == NULL) {
|
if (ar->namewhat == NULL) {
|
||||||
ar->namewhat = "";
|
ar->namewhat = "";
|
||||||
ar->name = NULL;
|
ar->name = NULL;
|
||||||
}
|
}
|
||||||
break;
|
} else if (*what == 'f') {
|
||||||
case 'f':
|
|
||||||
setfuncV(L, L->top, fn);
|
setfuncV(L, L->top, fn);
|
||||||
incr_top(L);
|
incr_top(L);
|
||||||
break;
|
} else if (*what == 'L') {
|
||||||
case 'L':
|
|
||||||
if (isluafunc(fn)) {
|
if (isluafunc(fn)) {
|
||||||
GCtab *t = lj_tab_new(L, 0, 0);
|
GCtab *t = lj_tab_new(L, 0, 0);
|
||||||
GCproto *pt = funcproto(fn);
|
GCproto *pt = funcproto(fn);
|
||||||
BCLine *lineinfo = proto_lineinfo(pt);
|
const void *lineinfo = proto_lineinfo(pt);
|
||||||
MSize i, szl = pt->sizebc;
|
if (lineinfo) {
|
||||||
for (i = 1; i < szl; i++)
|
BCLine first = pt->firstline;
|
||||||
setboolV(lj_tab_setint(L, t, lineinfo[i]), 1);
|
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);
|
settabV(L, L->top, t);
|
||||||
} else {
|
} else {
|
||||||
setnilV(L->top);
|
setnilV(L->top);
|
||||||
}
|
}
|
||||||
incr_top(L);
|
incr_top(L);
|
||||||
break;
|
} else {
|
||||||
default:
|
|
||||||
status = 0; /* Bad option. */
|
status = 0; /* Bad option. */
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
|
@ -10,14 +10,32 @@
|
|||||||
|
|
||||||
LJ_FUNC cTValue *lj_debug_frame(lua_State *L, int level, int *size);
|
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 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,
|
LJ_FUNC const char *lj_debug_slotname(GCproto *pt, const BCIns *pc,
|
||||||
BCReg slot, const char **name);
|
BCReg slot, const char **name);
|
||||||
LJ_FUNC const char *lj_debug_funcname(lua_State *L, TValue *frame,
|
LJ_FUNC const char *lj_debug_funcname(lua_State *L, TValue *frame,
|
||||||
const char **name);
|
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,
|
LJ_FUNC void lj_debug_addloc(lua_State *L, const char *msg,
|
||||||
cTValue *frame, cTValue *nextframe);
|
cTValue *frame, cTValue *nextframe);
|
||||||
LJ_FUNC void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc);
|
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
|
#endif
|
||||||
|
@ -521,7 +521,7 @@ LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Lexer error. */
|
/* 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)
|
BCLine line, ErrMsg em, va_list argp)
|
||||||
{
|
{
|
||||||
char buff[LUA_IDSIZE];
|
char buff[LUA_IDSIZE];
|
||||||
|
@ -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_mem(lua_State *L);
|
||||||
LJ_FUNC_NORET void lj_err_run(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_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);
|
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_optype(lua_State *L, cTValue *o, ErrMsg opm);
|
||||||
LJ_FUNC_NORET void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2);
|
LJ_FUNC_NORET void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2);
|
||||||
|
@ -257,10 +257,6 @@ static void gc_traverse_proto(global_State *g, GCproto *pt)
|
|||||||
gc_mark_str(proto_chunkname(pt));
|
gc_mark_str(proto_chunkname(pt));
|
||||||
for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) /* Mark collectable consts. */
|
for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) /* Mark collectable consts. */
|
||||||
gc_markobj(g, proto_kgc(pt, i));
|
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 LJ_HASJIT
|
||||||
if (pt->trace) gc_marktrace(g, pt->trace);
|
if (pt->trace) gc_marktrace(g, pt->trace);
|
||||||
#endif
|
#endif
|
||||||
|
@ -731,7 +731,7 @@ void lj_gdbjit_addtrace(jit_State *J, GCtrace *T)
|
|||||||
ctx.spadj = CFRAME_SIZE_JIT + T->spadjust;
|
ctx.spadj = CFRAME_SIZE_JIT + T->spadjust;
|
||||||
lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc);
|
lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc);
|
||||||
ctx.lineno = lj_debug_line(pt, proto_bcpos(pt, startpc));
|
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 == '=')
|
if (*ctx.filename == '@' || *ctx.filename == '=')
|
||||||
ctx.filename++;
|
ctx.filename++;
|
||||||
else
|
else
|
||||||
|
@ -494,7 +494,7 @@ void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...)
|
|||||||
tok = lj_lex_token2str(ls, token);
|
tok = lj_lex_token2str(ls, token);
|
||||||
}
|
}
|
||||||
va_start(argp, em);
|
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);
|
va_end(argp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +38,13 @@ typedef struct BCInsLine {
|
|||||||
BCLine line; /* Line number for this bytecode. */
|
BCLine line; /* Line number for this bytecode. */
|
||||||
} BCInsLine;
|
} 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. */
|
/* Lua lexer state. */
|
||||||
typedef struct LexState {
|
typedef struct LexState {
|
||||||
struct FuncState *fs; /* Current FuncState. Defined in lj_parse.c. */
|
struct FuncState *fs; /* Current FuncState. Defined in lj_parse.c. */
|
||||||
|
29
src/lj_obj.h
29
src/lj_obj.h
@ -286,12 +286,6 @@ typedef struct GCcdataVar {
|
|||||||
#define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef))
|
#define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef))
|
||||||
#define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1))
|
#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 {
|
typedef struct GCproto {
|
||||||
GCHeader;
|
GCHeader;
|
||||||
uint8_t numparams; /* Number of parameters. */
|
uint8_t numparams; /* Number of parameters. */
|
||||||
@ -308,11 +302,11 @@ typedef struct GCproto {
|
|||||||
uint16_t trace; /* Anchor for chain of root traces. */
|
uint16_t trace; /* Anchor for chain of root traces. */
|
||||||
/* ------ The following fields are for debugging/tracebacks only ------ */
|
/* ------ The following fields are for debugging/tracebacks only ------ */
|
||||||
GCRef chunkname; /* Name of the chunk this function was defined in. */
|
GCRef chunkname; /* Name of the chunk this function was defined in. */
|
||||||
BCLine lastlinedefined; /* Last line of the function definition. */
|
BCLine firstline; /* First line of the function definition. */
|
||||||
MSize sizevarinfo; /* Size of local var info array. */
|
BCLine numline; /* Number of lines for the function definition. */
|
||||||
MRef varinfo; /* Names and extents of local variables. */
|
MRef lineinfo; /* Compressed map from bytecode ins. to source line. */
|
||||||
MRef uvname; /* Array of upvalue names (GCRef of GCstr). */
|
MRef uvinfo; /* Upvalue names. */
|
||||||
MRef lineinfo; /* Map from bytecode instructions to source lines. */
|
MRef varinfo; /* Names and compressed extents of local variables. */
|
||||||
} GCproto;
|
} GCproto;
|
||||||
|
|
||||||
#define PROTO_IS_VARARG 0x01
|
#define PROTO_IS_VARARG 0x01
|
||||||
@ -331,14 +325,11 @@ typedef struct GCproto {
|
|||||||
#define proto_bcpos(pt, pc) ((BCPos)((pc) - proto_bc(pt)))
|
#define proto_bcpos(pt, pc) ((BCPos)((pc) - proto_bc(pt)))
|
||||||
#define proto_uv(pt) (mref((pt)->uv, uint16_t))
|
#define proto_uv(pt) (mref((pt)->uv, uint16_t))
|
||||||
|
|
||||||
#define proto_uvname(pt, idx) \
|
#define proto_chunkname(pt) (strref((pt)->chunkname))
|
||||||
check_exp((uintptr_t)(idx) < (pt)->sizeuv, \
|
#define proto_chunknamestr(pt) (strdata(proto_chunkname((pt))))
|
||||||
gco2str(gcref(mref((pt)->uvname, GCRef)[(idx)])))
|
#define proto_lineinfo(pt) (mref((pt)->lineinfo, const void))
|
||||||
#define proto_chunkname(pt) (gco2str(gcref((pt)->chunkname)))
|
#define proto_uvinfo(pt) (mref((pt)->uvinfo, const uint8_t))
|
||||||
#define proto_lineinfo(pt) (mref((pt)->lineinfo, BCLine))
|
#define proto_varinfo(pt) (mref((pt)->varinfo, const uint8_t))
|
||||||
#define proto_line(pt, pos) \
|
|
||||||
check_exp((uintptr_t)(pos) < (pt)->sizebc, proto_lineinfo(pt)[(pos)])
|
|
||||||
#define proto_varinfo(pt) (mref((pt)->varinfo, VarInfo))
|
|
||||||
|
|
||||||
/* -- Upvalue object ------------------------------------------------------ */
|
/* -- Upvalue object ------------------------------------------------------ */
|
||||||
|
|
||||||
|
228
src/lj_parse.c
228
src/lj_parse.c
@ -12,6 +12,7 @@
|
|||||||
#include "lj_obj.h"
|
#include "lj_obj.h"
|
||||||
#include "lj_gc.h"
|
#include "lj_gc.h"
|
||||||
#include "lj_err.h"
|
#include "lj_err.h"
|
||||||
|
#include "lj_debug.h"
|
||||||
#include "lj_str.h"
|
#include "lj_str.h"
|
||||||
#include "lj_tab.h"
|
#include "lj_tab.h"
|
||||||
#include "lj_func.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_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK);
|
||||||
lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo);
|
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. */
|
/* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */
|
||||||
setgcref(ls->vstack[vtop].name, obj2gco(name));
|
setgcref(ls->vstack[vtop].name, obj2gco(name));
|
||||||
fs->varmap[fs->nactvar+n] = (uint16_t)vtop;
|
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) \
|
#define var_new_lit(ls, n, v) \
|
||||||
var_new(ls, (n), lj_parse_keepstr(ls, "" v, sizeof(v)-1))
|
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. */
|
/* Add local variables. */
|
||||||
static void var_add(LexState *ls, BCReg nvars)
|
static void var_add(LexState *ls, BCReg nvars)
|
||||||
{
|
{
|
||||||
@ -1053,7 +1058,7 @@ static BCReg var_lookup_local(FuncState *fs, GCstr *n)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = fs->nactvar-1; i >= 0; 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)i;
|
||||||
}
|
}
|
||||||
return (BCReg)-1; /* Not found. */
|
return (BCReg)-1; /* Not found. */
|
||||||
@ -1109,23 +1114,16 @@ static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first)
|
|||||||
|
|
||||||
/* -- Function state management ------------------------------------------- */
|
/* -- Function state management ------------------------------------------- */
|
||||||
|
|
||||||
/* NYI: compress debug info. */
|
/* Fixup bytecode for prototype. */
|
||||||
|
static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, MSize n)
|
||||||
/* Fixup bytecode and lineinfo for prototype. */
|
|
||||||
static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, BCLine *lineinfo)
|
|
||||||
{
|
{
|
||||||
MSize i, n = fs->pc;
|
|
||||||
BCInsLine *base = fs->bcbase;
|
BCInsLine *base = fs->bcbase;
|
||||||
setmref(pt->lineinfo, lineinfo);
|
MSize i;
|
||||||
pt->sizebc = n;
|
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,
|
bc[0] = BCINS_AD((fs->flags & PROTO_IS_VARARG) ? BC_FUNCV : BC_FUNCF,
|
||||||
fs->framesize, 0);
|
fs->framesize, 0);
|
||||||
lineinfo[0] = fs->linedefined;
|
for (i = 1; i < n; i++)
|
||||||
for (i = 1; i < n; i++) {
|
|
||||||
bc[i] = base[i].ins;
|
bc[i] = base[i].ins;
|
||||||
lineinfo[i] = base[i].line;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fixup constants for prototype. */
|
/* 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;
|
uv[i] = fs->uvloc[i].slot;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Fixup debug info for prototype. */
|
#ifndef LUAJIT_DISABLE_DEBUGINFO
|
||||||
static void fs_fixup_dbg(FuncState *fs, GCproto *pt, VarInfo *vi, MSize sizevi)
|
/* Prepare lineinfo for prototype. */
|
||||||
|
static size_t fs_prep_line(FuncState *fs, BCLine numline)
|
||||||
{
|
{
|
||||||
MSize i, n = fs->nuv;
|
return (fs->pc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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. */
|
/* Check if bytecode op returns. */
|
||||||
static int bcopisret(BCOp op)
|
static int bcopisret(BCOp op)
|
||||||
{
|
{
|
||||||
@ -1253,8 +1369,8 @@ static GCproto *fs_finish(LexState *ls, BCLine line)
|
|||||||
{
|
{
|
||||||
lua_State *L = ls->L;
|
lua_State *L = ls->L;
|
||||||
FuncState *fs = ls->fs;
|
FuncState *fs = ls->fs;
|
||||||
MSize sizevi;
|
BCLine numline = line - fs->linedefined;
|
||||||
size_t sizept, ofsk, ofsuv, ofsdbg, ofsli;
|
size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar;
|
||||||
GCproto *pt;
|
GCproto *pt;
|
||||||
|
|
||||||
/* Apply final fixups. */
|
/* Apply final fixups. */
|
||||||
@ -1264,33 +1380,29 @@ static GCproto *fs_finish(LexState *ls, BCLine line)
|
|||||||
|
|
||||||
/* Calculate total size of prototype including all colocated arrays. */
|
/* Calculate total size of prototype including all colocated arrays. */
|
||||||
sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef);
|
sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef);
|
||||||
sizept = (sizept + sizeof(lua_Number)-1) & ~(sizeof(lua_Number)-1);
|
sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1);
|
||||||
ofsk = sizept;
|
ofsk = sizept; sizept += fs->nkn*sizeof(TValue);
|
||||||
sizept += fs->nkn*sizeof(lua_Number);
|
ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2;
|
||||||
ofsuv = sizept;
|
ofsli = sizept; sizept += fs_prep_line(fs, numline);
|
||||||
sizept += ((fs->nuv+1)&~1)*2;
|
ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar);
|
||||||
ofsdbg = sizept;
|
|
||||||
sizevi = ls->vtop - fs->vbase;
|
|
||||||
sizept += sizevi*sizeof(VarInfo) + fs->nuv*sizeof(GCRef);
|
|
||||||
ofsli = sizept;
|
|
||||||
sizept += fs->pc*sizeof(BCLine);
|
|
||||||
|
|
||||||
/* Allocate prototype and initialize its fields. */
|
/* Allocate prototype and initialize its fields. */
|
||||||
pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept);
|
pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept);
|
||||||
pt->gct = ~LJ_TPROTO;
|
pt->gct = ~LJ_TPROTO;
|
||||||
pt->sizept = (MSize)sizept;
|
pt->sizept = (MSize)sizept;
|
||||||
setgcref(pt->chunkname, obj2gco(ls->chunkname));
|
|
||||||
pt->trace = 0;
|
pt->trace = 0;
|
||||||
pt->flags = fs->flags;
|
pt->flags = fs->flags;
|
||||||
pt->numparams = fs->numparams;
|
pt->numparams = fs->numparams;
|
||||||
pt->framesize = fs->framesize;
|
pt->framesize = fs->framesize;
|
||||||
pt->lastlinedefined = line;
|
setgcref(pt->chunkname, obj2gco(ls->chunkname));
|
||||||
|
|
||||||
fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)),
|
/* Close potentially uninitialized gap between bc and kgc. */
|
||||||
(BCLine *)((char *)pt + ofsli));
|
*(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_k(fs, pt, (void *)((char *)pt + ofsk));
|
||||||
fs_fixup_uv(fs, pt, (uint16_t *)((char *)pt + ofsuv));
|
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,
|
lj_vmevent_send(L, BC,
|
||||||
setprotoV(L, L->top++, pt);
|
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->vtop = fs->vbase; /* Reset variable stack. */
|
||||||
ls->fs = fs->prev;
|
ls->fs = fs->prev;
|
||||||
lua_assert(ls->fs != NULL || ls->token == TK_eof);
|
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;
|
return pt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1530,8 +1636,6 @@ static void parse_chunk(LexState *ls);
|
|||||||
static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line)
|
static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line)
|
||||||
{
|
{
|
||||||
FuncState fs, *pfs = ls->fs;
|
FuncState fs, *pfs = ls->fs;
|
||||||
BCReg kidx;
|
|
||||||
BCLine lastline;
|
|
||||||
GCproto *pt;
|
GCproto *pt;
|
||||||
ptrdiff_t oldbase = pfs->bcbase - ls->bcstack;
|
ptrdiff_t oldbase = pfs->bcbase - ls->bcstack;
|
||||||
fs_init(ls, &fs);
|
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;
|
fs.bclim = pfs->bclim - pfs->pc;
|
||||||
bcemit_AD(&fs, BC_FUNCF, 0, 0); /* Placeholder. */
|
bcemit_AD(&fs, BC_FUNCF, 0, 0); /* Placeholder. */
|
||||||
parse_chunk(ls);
|
parse_chunk(ls);
|
||||||
lastline = ls->linenumber;
|
if (ls->token != TK_end) lex_match(ls, TK_end, TK_function, line);
|
||||||
lex_match(ls, TK_end, TK_function, line);
|
pt = fs_finish(ls, (ls->lastline = ls->linenumber));
|
||||||
pt = fs_finish(ls, lastline);
|
|
||||||
pfs->bcbase = ls->bcstack + oldbase; /* May have been reallocated. */
|
pfs->bcbase = ls->bcstack + oldbase; /* May have been reallocated. */
|
||||||
pfs->bclim = (BCPos)(ls->sizebcstack - oldbase);
|
pfs->bclim = (BCPos)(ls->sizebcstack - oldbase);
|
||||||
/* Store new prototype in the constant array of the parent. */
|
/* Store new prototype in the constant array of the parent. */
|
||||||
kidx = const_gc(pfs, obj2gco(pt), LJ_TPROTO);
|
expr_init(e, VRELOCABLE,
|
||||||
expr_init(e, VRELOCABLE, bcemit_AD(pfs, BC_FNEW, 0, kidx));
|
bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO)));
|
||||||
if (!(pfs->flags & PROTO_HAS_FNEW)) {
|
if (!(pfs->flags & PROTO_HAS_FNEW)) {
|
||||||
if (pfs->flags & PROTO_HAS_RETURN)
|
if (pfs->flags & PROTO_HAS_RETURN)
|
||||||
pfs->flags |= PROTO_FIXUP_RETURN;
|
pfs->flags |= PROTO_FIXUP_RETURN;
|
||||||
pfs->flags |= PROTO_HAS_FNEW;
|
pfs->flags |= PROTO_HAS_FNEW;
|
||||||
}
|
}
|
||||||
|
lj_lex_next(ls);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse expression list. Last expression is left open. */
|
/* 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;
|
FuncScope bl;
|
||||||
BCPos loop, loopend;
|
BCPos loop, loopend;
|
||||||
/* Hidden control variables. */
|
/* Hidden control variables. */
|
||||||
var_new_lit(ls, FORL_IDX, "(for index)");
|
var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX);
|
||||||
var_new_lit(ls, FORL_STOP, "(for limit)");
|
var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP);
|
||||||
var_new_lit(ls, FORL_STEP, "(for step)");
|
var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP);
|
||||||
/* Visible copy of index variable. */
|
/* Visible copy of index variable. */
|
||||||
var_new(ls, FORL_EXT, varname);
|
var_new(ls, FORL_EXT, varname);
|
||||||
lex_check(ls, '=');
|
lex_check(ls, '=');
|
||||||
@ -2220,9 +2324,9 @@ static void parse_for_iter(LexState *ls, GCstr *indexname)
|
|||||||
FuncScope bl;
|
FuncScope bl;
|
||||||
int isnext;
|
int isnext;
|
||||||
/* Hidden control variables. */
|
/* Hidden control variables. */
|
||||||
var_new_lit(ls, nvars++, "(for generator)");
|
var_new_fixed(ls, nvars++, VARNAME_FOR_GEN);
|
||||||
var_new_lit(ls, nvars++, "(for state)");
|
var_new_fixed(ls, nvars++, VARNAME_FOR_STATE);
|
||||||
var_new_lit(ls, nvars++, "(for control)");
|
var_new_fixed(ls, nvars++, VARNAME_FOR_CTL);
|
||||||
/* Visible variables returned from iterator. */
|
/* Visible variables returned from iterator. */
|
||||||
var_new(ls, nvars++, indexname);
|
var_new(ls, nvars++, indexname);
|
||||||
while (lex_opt(ls, ','))
|
while (lex_opt(ls, ','))
|
||||||
@ -2374,7 +2478,11 @@ GCproto *lj_parse(LexState *ls)
|
|||||||
FuncState fs;
|
FuncState fs;
|
||||||
GCproto *pt;
|
GCproto *pt;
|
||||||
lua_State *L = ls->L;
|
lua_State *L = ls->L;
|
||||||
|
#ifdef LUAJIT_DISABLE_DEBUGINFO
|
||||||
|
ls->chunkname = lj_str_newlit(L, "=");
|
||||||
|
#else
|
||||||
ls->chunkname = lj_str_newz(L, ls->chunkarg);
|
ls->chunkname = lj_str_newz(L, ls->chunkarg);
|
||||||
|
#endif
|
||||||
setstrV(L, L->top, ls->chunkname); /* Anchor chunkname string. */
|
setstrV(L, L->top, ls->chunkname); /* Anchor chunkname string. */
|
||||||
incr_top(L);
|
incr_top(L);
|
||||||
ls->level = 0;
|
ls->level = 0;
|
||||||
|
@ -98,7 +98,7 @@ static void perftools_addtrace(GCtrace *T)
|
|||||||
static FILE *fp;
|
static FILE *fp;
|
||||||
GCproto *pt = &gcref(T->startpt)->pt;
|
GCproto *pt = &gcref(T->startpt)->pt;
|
||||||
const BCIns *startpc = mref(T->startpc, const BCIns);
|
const BCIns *startpc = mref(T->startpc, const BCIns);
|
||||||
const char *name = strdata(proto_chunkname(pt));
|
const char *name = proto_chunknamestr(pt);
|
||||||
BCLine lineno;
|
BCLine lineno;
|
||||||
if (name[0] == '@' || name[0] == '=')
|
if (name[0] == '@' || name[0] == '=')
|
||||||
name++;
|
name++;
|
||||||
|
Loading…
Reference in New Issue
Block a user