|
@@ -32,76 +32,44 @@ typedef struct BCWriteCtx {
|
|
|
int status; /* Status from writer callback. */
|
|
int status; /* Status from writer callback. */
|
|
|
} BCWriteCtx;
|
|
} BCWriteCtx;
|
|
|
|
|
|
|
|
-/* -- Output buffer handling ---------------------------------------------- */
|
|
|
|
|
-
|
|
|
|
|
-/* Ensure a certain amount of buffer space. */
|
|
|
|
|
-static LJ_AINLINE void bcwrite_need(BCWriteCtx *ctx, MSize len)
|
|
|
|
|
-{
|
|
|
|
|
- lj_buf_need(ctx->L, &ctx->sb, ctx->sb.n + len);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Add memory block to buffer. */
|
|
|
|
|
-static void bcwrite_block(BCWriteCtx *ctx, const void *p, MSize len)
|
|
|
|
|
-{
|
|
|
|
|
- uint8_t *q = (uint8_t *)(ctx->sb.buf + ctx->sb.n);
|
|
|
|
|
- MSize i;
|
|
|
|
|
- ctx->sb.n += len;
|
|
|
|
|
- for (i = 0; i < len; i++) q[i] = ((uint8_t *)p)[i];
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Add byte to buffer. */
|
|
|
|
|
-static LJ_AINLINE void bcwrite_byte(BCWriteCtx *ctx, uint8_t b)
|
|
|
|
|
-{
|
|
|
|
|
- ctx->sb.buf[ctx->sb.n++] = b;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Add ULEB128 value to buffer. */
|
|
|
|
|
-static void bcwrite_uleb128(BCWriteCtx *ctx, uint32_t v)
|
|
|
|
|
-{
|
|
|
|
|
- MSize n = ctx->sb.n;
|
|
|
|
|
- uint8_t *p = (uint8_t *)ctx->sb.buf;
|
|
|
|
|
- for (; v >= 0x80; v >>= 7)
|
|
|
|
|
- p[n++] = (uint8_t)((v & 0x7f) | 0x80);
|
|
|
|
|
- p[n++] = (uint8_t)v;
|
|
|
|
|
- ctx->sb.n = n;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
/* -- Bytecode writer ----------------------------------------------------- */
|
|
/* -- Bytecode writer ----------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Write a single constant key/value of a template table. */
|
|
/* Write a single constant key/value of a template table. */
|
|
|
static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)
|
|
static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)
|
|
|
{
|
|
{
|
|
|
- bcwrite_need(ctx, 1+10);
|
|
|
|
|
|
|
+ char *p = lj_buf_more(ctx->L, &ctx->sb, 1+10);
|
|
|
if (tvisstr(o)) {
|
|
if (tvisstr(o)) {
|
|
|
const GCstr *str = strV(o);
|
|
const GCstr *str = strV(o);
|
|
|
MSize len = str->len;
|
|
MSize len = str->len;
|
|
|
- bcwrite_need(ctx, 5+len);
|
|
|
|
|
- bcwrite_uleb128(ctx, BCDUMP_KTAB_STR+len);
|
|
|
|
|
- bcwrite_block(ctx, strdata(str), len);
|
|
|
|
|
|
|
+ p = lj_buf_more(ctx->L, &ctx->sb, 5+len);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, BCDUMP_KTAB_STR+len);
|
|
|
|
|
+ p = lj_buf_wmem(p, strdata(str), len);
|
|
|
} else if (tvisint(o)) {
|
|
} else if (tvisint(o)) {
|
|
|
- bcwrite_byte(ctx, BCDUMP_KTAB_INT);
|
|
|
|
|
- bcwrite_uleb128(ctx, intV(o));
|
|
|
|
|
|
|
+ *p++ = BCDUMP_KTAB_INT;
|
|
|
|
|
+ p = lj_buf_wuleb128(p, intV(o));
|
|
|
} else if (tvisnum(o)) {
|
|
} else if (tvisnum(o)) {
|
|
|
if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */
|
|
if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */
|
|
|
lua_Number num = numV(o);
|
|
lua_Number num = numV(o);
|
|
|
int32_t k = lj_num2int(num);
|
|
int32_t k = lj_num2int(num);
|
|
|
if (num == (lua_Number)k) { /* -0 is never a constant. */
|
|
if (num == (lua_Number)k) { /* -0 is never a constant. */
|
|
|
- bcwrite_byte(ctx, BCDUMP_KTAB_INT);
|
|
|
|
|
- bcwrite_uleb128(ctx, k);
|
|
|
|
|
|
|
+ *p++ = BCDUMP_KTAB_INT;
|
|
|
|
|
+ p = lj_buf_wuleb128(p, k);
|
|
|
|
|
+ setsbufP(&ctx->sb, p);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- bcwrite_byte(ctx, BCDUMP_KTAB_NUM);
|
|
|
|
|
- bcwrite_uleb128(ctx, o->u32.lo);
|
|
|
|
|
- bcwrite_uleb128(ctx, o->u32.hi);
|
|
|
|
|
|
|
+ *p++ = BCDUMP_KTAB_NUM;
|
|
|
|
|
+ p = lj_buf_wuleb128(p, o->u32.lo);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, o->u32.hi);
|
|
|
} else {
|
|
} else {
|
|
|
lua_assert(tvispri(o));
|
|
lua_assert(tvispri(o));
|
|
|
- bcwrite_byte(ctx, BCDUMP_KTAB_NIL+~itype(o));
|
|
|
|
|
|
|
+ *p++ = BCDUMP_KTAB_NIL+~itype(o);
|
|
|
}
|
|
}
|
|
|
|
|
+ setsbufP(&ctx->sb, p);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Write a template table. */
|
|
/* Write a template table. */
|
|
|
-static void bcwrite_ktab(BCWriteCtx *ctx, const GCtab *t)
|
|
|
|
|
|
|
+static void bcwrite_ktab(BCWriteCtx *ctx, char *p, const GCtab *t)
|
|
|
{
|
|
{
|
|
|
MSize narray = 0, nhash = 0;
|
|
MSize narray = 0, nhash = 0;
|
|
|
if (t->asize > 0) { /* Determine max. length of array part. */
|
|
if (t->asize > 0) { /* Determine max. length of array part. */
|
|
@@ -119,8 +87,9 @@ static void bcwrite_ktab(BCWriteCtx *ctx, const GCtab *t)
|
|
|
nhash += !tvisnil(&node[i].val);
|
|
nhash += !tvisnil(&node[i].val);
|
|
|
}
|
|
}
|
|
|
/* Write number of array slots and hash slots. */
|
|
/* Write number of array slots and hash slots. */
|
|
|
- bcwrite_uleb128(ctx, narray);
|
|
|
|
|
- bcwrite_uleb128(ctx, nhash);
|
|
|
|
|
|
|
+ p = lj_buf_wuleb128(p, narray);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, nhash);
|
|
|
|
|
+ setsbufP(&ctx->sb, p);
|
|
|
if (narray) { /* Write array entries (may contain nil). */
|
|
if (narray) { /* Write array entries (may contain nil). */
|
|
|
MSize i;
|
|
MSize i;
|
|
|
TValue *o = tvref(t->array);
|
|
TValue *o = tvref(t->array);
|
|
@@ -147,6 +116,7 @@ static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
|
|
|
for (i = 0; i < sizekgc; i++, kr++) {
|
|
for (i = 0; i < sizekgc; i++, kr++) {
|
|
|
GCobj *o = gcref(*kr);
|
|
GCobj *o = gcref(*kr);
|
|
|
MSize tp, need = 1;
|
|
MSize tp, need = 1;
|
|
|
|
|
+ char *p;
|
|
|
/* Determine constant type and needed size. */
|
|
/* Determine constant type and needed size. */
|
|
|
if (o->gch.gct == ~LJ_TSTR) {
|
|
if (o->gch.gct == ~LJ_TSTR) {
|
|
|
tp = BCDUMP_KGC_STR + gco2str(o)->len;
|
|
tp = BCDUMP_KGC_STR + gco2str(o)->len;
|
|
@@ -173,24 +143,26 @@ static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
|
|
|
need = 1+2*5;
|
|
need = 1+2*5;
|
|
|
}
|
|
}
|
|
|
/* Write constant type. */
|
|
/* Write constant type. */
|
|
|
- bcwrite_need(ctx, need);
|
|
|
|
|
- bcwrite_uleb128(ctx, tp);
|
|
|
|
|
|
|
+ p = lj_buf_more(ctx->L, &ctx->sb, need);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, tp);
|
|
|
/* Write constant data (if any). */
|
|
/* Write constant data (if any). */
|
|
|
if (tp >= BCDUMP_KGC_STR) {
|
|
if (tp >= BCDUMP_KGC_STR) {
|
|
|
- bcwrite_block(ctx, strdata(gco2str(o)), gco2str(o)->len);
|
|
|
|
|
|
|
+ p = lj_buf_wmem(p, strdata(gco2str(o)), gco2str(o)->len);
|
|
|
} else if (tp == BCDUMP_KGC_TAB) {
|
|
} else if (tp == BCDUMP_KGC_TAB) {
|
|
|
- bcwrite_ktab(ctx, gco2tab(o));
|
|
|
|
|
|
|
+ bcwrite_ktab(ctx, p, gco2tab(o));
|
|
|
|
|
+ continue;
|
|
|
#if LJ_HASFFI
|
|
#if LJ_HASFFI
|
|
|
} else if (tp != BCDUMP_KGC_CHILD) {
|
|
} else if (tp != BCDUMP_KGC_CHILD) {
|
|
|
- cTValue *p = (TValue *)cdataptr(gco2cd(o));
|
|
|
|
|
- bcwrite_uleb128(ctx, p[0].u32.lo);
|
|
|
|
|
- bcwrite_uleb128(ctx, p[0].u32.hi);
|
|
|
|
|
|
|
+ cTValue *q = (TValue *)cdataptr(gco2cd(o));
|
|
|
|
|
+ p = lj_buf_wuleb128(p, q[0].u32.lo);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, q[0].u32.hi);
|
|
|
if (tp == BCDUMP_KGC_COMPLEX) {
|
|
if (tp == BCDUMP_KGC_COMPLEX) {
|
|
|
- bcwrite_uleb128(ctx, p[1].u32.lo);
|
|
|
|
|
- bcwrite_uleb128(ctx, p[1].u32.hi);
|
|
|
|
|
|
|
+ p = lj_buf_wuleb128(p, q[1].u32.lo);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, q[1].u32.hi);
|
|
|
}
|
|
}
|
|
|
#endif
|
|
#endif
|
|
|
}
|
|
}
|
|
|
|
|
+ setsbufP(&ctx->sb, p);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -199,7 +171,7 @@ static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
|
|
|
{
|
|
{
|
|
|
MSize i, sizekn = pt->sizekn;
|
|
MSize i, sizekn = pt->sizekn;
|
|
|
cTValue *o = mref(pt->k, TValue);
|
|
cTValue *o = mref(pt->k, TValue);
|
|
|
- bcwrite_need(ctx, 10*sizekn);
|
|
|
|
|
|
|
+ char *p = lj_buf_more(ctx->L, &ctx->sb, 10*sizekn);
|
|
|
for (i = 0; i < sizekn; i++, o++) {
|
|
for (i = 0; i < sizekn; i++, o++) {
|
|
|
int32_t k;
|
|
int32_t k;
|
|
|
if (tvisint(o)) {
|
|
if (tvisint(o)) {
|
|
@@ -212,58 +184,58 @@ static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
|
|
|
k = lj_num2int(num);
|
|
k = lj_num2int(num);
|
|
|
if (num == (lua_Number)k) { /* -0 is never a constant. */
|
|
if (num == (lua_Number)k) { /* -0 is never a constant. */
|
|
|
save_int:
|
|
save_int:
|
|
|
- bcwrite_uleb128(ctx, 2*(uint32_t)k | ((uint32_t)k & 0x80000000u));
|
|
|
|
|
- if (k < 0) {
|
|
|
|
|
- char *p = &ctx->sb.buf[ctx->sb.n-1];
|
|
|
|
|
- *p = (*p & 7) | ((k>>27) & 0x18);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ p = lj_buf_wuleb128(p, 2*(uint32_t)k | ((uint32_t)k & 0x80000000u));
|
|
|
|
|
+ if (k < 0)
|
|
|
|
|
+ p[-1] = (p[-1] & 7) | ((k>>27) & 0x18);
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- bcwrite_uleb128(ctx, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));
|
|
|
|
|
- if (o->u32.lo >= 0x80000000u) {
|
|
|
|
|
- char *p = &ctx->sb.buf[ctx->sb.n-1];
|
|
|
|
|
- *p = (*p & 7) | ((o->u32.lo>>27) & 0x18);
|
|
|
|
|
- }
|
|
|
|
|
- bcwrite_uleb128(ctx, o->u32.hi);
|
|
|
|
|
|
|
+ p = lj_buf_wuleb128(p, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));
|
|
|
|
|
+ if (o->u32.lo >= 0x80000000u)
|
|
|
|
|
+ p[-1] = (p[-1] & 7) | ((o->u32.lo>>27) & 0x18);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, o->u32.hi);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ setsbufP(&ctx->sb, p);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Write bytecode instructions. */
|
|
/* Write bytecode instructions. */
|
|
|
-static void bcwrite_bytecode(BCWriteCtx *ctx, GCproto *pt)
|
|
|
|
|
|
|
+static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt)
|
|
|
{
|
|
{
|
|
|
MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */
|
|
MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */
|
|
|
#if LJ_HASJIT
|
|
#if LJ_HASJIT
|
|
|
- uint8_t *p = (uint8_t *)&ctx->sb.buf[ctx->sb.n];
|
|
|
|
|
|
|
+ uint8_t *q = (uint8_t *)p;
|
|
|
#endif
|
|
#endif
|
|
|
- bcwrite_block(ctx, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));
|
|
|
|
|
|
|
+ p = lj_buf_wmem(p, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));
|
|
|
|
|
+ UNUSED(ctx);
|
|
|
#if LJ_HASJIT
|
|
#if LJ_HASJIT
|
|
|
/* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
|
|
/* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
|
|
|
if ((pt->flags & PROTO_ILOOP) || pt->trace) {
|
|
if ((pt->flags & PROTO_ILOOP) || pt->trace) {
|
|
|
jit_State *J = L2J(ctx->L);
|
|
jit_State *J = L2J(ctx->L);
|
|
|
MSize i;
|
|
MSize i;
|
|
|
- for (i = 0; i < nbc; i++, p += sizeof(BCIns)) {
|
|
|
|
|
- BCOp op = (BCOp)p[LJ_ENDIAN_SELECT(0, 3)];
|
|
|
|
|
|
|
+ for (i = 0; i < nbc; i++, q += sizeof(BCIns)) {
|
|
|
|
|
+ BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)];
|
|
|
if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||
|
|
if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||
|
|
|
op == BC_JFORI) {
|
|
op == BC_JFORI) {
|
|
|
- p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);
|
|
|
|
|
|
|
+ q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);
|
|
|
} else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
|
|
} else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
|
|
|
- BCReg rd = p[LJ_ENDIAN_SELECT(2, 1)] + (p[LJ_ENDIAN_SELECT(3, 0)] << 8);
|
|
|
|
|
|
|
+ BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8);
|
|
|
BCIns ins = traceref(J, rd)->startins;
|
|
BCIns ins = traceref(J, rd)->startins;
|
|
|
- p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL);
|
|
|
|
|
- p[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins);
|
|
|
|
|
- p[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins);
|
|
|
|
|
|
|
+ q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL);
|
|
|
|
|
+ q[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins);
|
|
|
|
|
+ q[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
#endif
|
|
#endif
|
|
|
|
|
+ return p;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Write prototype. */
|
|
/* Write prototype. */
|
|
|
static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
|
|
static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
|
|
|
{
|
|
{
|
|
|
MSize sizedbg = 0;
|
|
MSize sizedbg = 0;
|
|
|
|
|
+ char *p;
|
|
|
|
|
|
|
|
/* Recursively write children of prototype. */
|
|
/* Recursively write children of prototype. */
|
|
|
if ((pt->flags & PROTO_CHILD)) {
|
|
if ((pt->flags & PROTO_CHILD)) {
|
|
@@ -277,31 +249,32 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Start writing the prototype info to a buffer. */
|
|
/* Start writing the prototype info to a buffer. */
|
|
|
- lj_buf_reset(&ctx->sb);
|
|
|
|
|
- ctx->sb.n = 5; /* Leave room for final size. */
|
|
|
|
|
- bcwrite_need(ctx, 4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
|
|
|
|
|
|
|
+ p = lj_buf_need(ctx->L, &ctx->sb,
|
|
|
|
|
+ 5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
|
|
|
|
|
+ p += 5; /* Leave room for final size. */
|
|
|
|
|
|
|
|
/* Write prototype header. */
|
|
/* Write prototype header. */
|
|
|
- bcwrite_byte(ctx, (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI)));
|
|
|
|
|
- bcwrite_byte(ctx, pt->numparams);
|
|
|
|
|
- bcwrite_byte(ctx, pt->framesize);
|
|
|
|
|
- bcwrite_byte(ctx, pt->sizeuv);
|
|
|
|
|
- bcwrite_uleb128(ctx, pt->sizekgc);
|
|
|
|
|
- bcwrite_uleb128(ctx, pt->sizekn);
|
|
|
|
|
- bcwrite_uleb128(ctx, pt->sizebc-1);
|
|
|
|
|
|
|
+ *p++ = (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI));
|
|
|
|
|
+ *p++ = pt->numparams;
|
|
|
|
|
+ *p++ = pt->framesize;
|
|
|
|
|
+ *p++ = pt->sizeuv;
|
|
|
|
|
+ p = lj_buf_wuleb128(p, pt->sizekgc);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, pt->sizekn);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, pt->sizebc-1);
|
|
|
if (!ctx->strip) {
|
|
if (!ctx->strip) {
|
|
|
if (proto_lineinfo(pt))
|
|
if (proto_lineinfo(pt))
|
|
|
sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);
|
|
sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);
|
|
|
- bcwrite_uleb128(ctx, sizedbg);
|
|
|
|
|
|
|
+ p = lj_buf_wuleb128(p, sizedbg);
|
|
|
if (sizedbg) {
|
|
if (sizedbg) {
|
|
|
- bcwrite_uleb128(ctx, pt->firstline);
|
|
|
|
|
- bcwrite_uleb128(ctx, pt->numline);
|
|
|
|
|
|
|
+ p = lj_buf_wuleb128(p, pt->firstline);
|
|
|
|
|
+ p = lj_buf_wuleb128(p, pt->numline);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Write bytecode instructions and upvalue refs. */
|
|
/* Write bytecode instructions and upvalue refs. */
|
|
|
- bcwrite_bytecode(ctx, pt);
|
|
|
|
|
- bcwrite_block(ctx, proto_uv(pt), pt->sizeuv*2);
|
|
|
|
|
|
|
+ p = bcwrite_bytecode(ctx, p, pt);
|
|
|
|
|
+ p = lj_buf_wmem(p, proto_uv(pt), pt->sizeuv*2);
|
|
|
|
|
+ setsbufP(&ctx->sb, p);
|
|
|
|
|
|
|
|
/* Write constants. */
|
|
/* Write constants. */
|
|
|
bcwrite_kgc(ctx, pt);
|
|
bcwrite_kgc(ctx, pt);
|
|
@@ -309,18 +282,19 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
|
|
|
|
|
|
|
|
/* Write debug info, if not stripped. */
|
|
/* Write debug info, if not stripped. */
|
|
|
if (sizedbg) {
|
|
if (sizedbg) {
|
|
|
- bcwrite_need(ctx, sizedbg);
|
|
|
|
|
- bcwrite_block(ctx, proto_lineinfo(pt), sizedbg);
|
|
|
|
|
|
|
+ p = lj_buf_more(ctx->L, &ctx->sb, sizedbg);
|
|
|
|
|
+ p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg);
|
|
|
|
|
+ setsbufP(&ctx->sb, p);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Pass buffer to writer function. */
|
|
/* Pass buffer to writer function. */
|
|
|
if (ctx->status == 0) {
|
|
if (ctx->status == 0) {
|
|
|
- MSize n = ctx->sb.n - 5;
|
|
|
|
|
|
|
+ MSize n = sbuflen(&ctx->sb) - 5;
|
|
|
MSize nn = (lj_fls(n)+8)*9 >> 6;
|
|
MSize nn = (lj_fls(n)+8)*9 >> 6;
|
|
|
- ctx->sb.n = 5 - nn;
|
|
|
|
|
- bcwrite_uleb128(ctx, n); /* Fill in final size. */
|
|
|
|
|
- lua_assert(ctx->sb.n == 5);
|
|
|
|
|
- ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf+5-nn, nn+n, ctx->wdata);
|
|
|
|
|
|
|
+ char *q = sbufB(&ctx->sb) + (5 - nn);
|
|
|
|
|
+ p = lj_buf_wuleb128(q, n); /* Fill in final size. */
|
|
|
|
|
+ lua_assert(p == sbufB(&ctx->sb) + 5);
|
|
|
|
|
+ ctx->status = ctx->wfunc(ctx->L, q, nn+n, ctx->wdata);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -330,20 +304,20 @@ static void bcwrite_header(BCWriteCtx *ctx)
|
|
|
GCstr *chunkname = proto_chunkname(ctx->pt);
|
|
GCstr *chunkname = proto_chunkname(ctx->pt);
|
|
|
const char *name = strdata(chunkname);
|
|
const char *name = strdata(chunkname);
|
|
|
MSize len = chunkname->len;
|
|
MSize len = chunkname->len;
|
|
|
- lj_buf_reset(&ctx->sb);
|
|
|
|
|
- bcwrite_need(ctx, 5+5+len);
|
|
|
|
|
- bcwrite_byte(ctx, BCDUMP_HEAD1);
|
|
|
|
|
- bcwrite_byte(ctx, BCDUMP_HEAD2);
|
|
|
|
|
- bcwrite_byte(ctx, BCDUMP_HEAD3);
|
|
|
|
|
- bcwrite_byte(ctx, BCDUMP_VERSION);
|
|
|
|
|
- bcwrite_byte(ctx, (ctx->strip ? BCDUMP_F_STRIP : 0) +
|
|
|
|
|
- (LJ_BE ? BCDUMP_F_BE : 0) +
|
|
|
|
|
- ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0));
|
|
|
|
|
|
|
+ char *p = lj_buf_need(ctx->L, &ctx->sb, 5+5+len);
|
|
|
|
|
+ *p++ = BCDUMP_HEAD1;
|
|
|
|
|
+ *p++ = BCDUMP_HEAD2;
|
|
|
|
|
+ *p++ = BCDUMP_HEAD3;
|
|
|
|
|
+ *p++ = BCDUMP_VERSION;
|
|
|
|
|
+ *p++ = (ctx->strip ? BCDUMP_F_STRIP : 0) +
|
|
|
|
|
+ (LJ_BE ? BCDUMP_F_BE : 0) +
|
|
|
|
|
+ ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0);
|
|
|
if (!ctx->strip) {
|
|
if (!ctx->strip) {
|
|
|
- bcwrite_uleb128(ctx, len);
|
|
|
|
|
- bcwrite_block(ctx, name, len);
|
|
|
|
|
|
|
+ p = lj_buf_wuleb128(p, len);
|
|
|
|
|
+ p = lj_buf_wmem(p, name, len);
|
|
|
}
|
|
}
|
|
|
- ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf, ctx->sb.n, ctx->wdata);
|
|
|
|
|
|
|
+ ctx->status = ctx->wfunc(ctx->L, sbufB(&ctx->sb),
|
|
|
|
|
+ (MSize)(p - sbufB(&ctx->sb)), ctx->wdata);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Write footer of bytecode dump. */
|
|
/* Write footer of bytecode dump. */
|
|
@@ -360,7 +334,7 @@ static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)
|
|
|
{
|
|
{
|
|
|
BCWriteCtx *ctx = (BCWriteCtx *)ud;
|
|
BCWriteCtx *ctx = (BCWriteCtx *)ud;
|
|
|
UNUSED(dummy);
|
|
UNUSED(dummy);
|
|
|
- lj_buf_grow(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */
|
|
|
|
|
|
|
+ lj_buf_need(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */
|
|
|
bcwrite_header(ctx);
|
|
bcwrite_header(ctx);
|
|
|
bcwrite_proto(ctx, ctx->pt);
|
|
bcwrite_proto(ctx, ctx->pt);
|
|
|
bcwrite_footer(ctx);
|
|
bcwrite_footer(ctx);
|