aboutsummaryrefslogtreecommitdiff
path: root/src/lj_bcwrite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_bcwrite.c')
-rw-r--r--src/lj_bcwrite.c204
1 files changed, 89 insertions, 115 deletions
diff --git a/src/lj_bcwrite.c b/src/lj_bcwrite.c
index 474234c5..71c86581 100644
--- a/src/lj_bcwrite.c
+++ b/src/lj_bcwrite.c
@@ -32,76 +32,44 @@ typedef struct BCWriteCtx {
32 int status; /* Status from writer callback. */ 32 int status; /* Status from writer callback. */
33} BCWriteCtx; 33} BCWriteCtx;
34 34
35/* -- Output buffer handling ---------------------------------------------- */
36
37/* Ensure a certain amount of buffer space. */
38static LJ_AINLINE void bcwrite_need(BCWriteCtx *ctx, MSize len)
39{
40 lj_buf_need(ctx->L, &ctx->sb, ctx->sb.n + len);
41}
42
43/* Add memory block to buffer. */
44static void bcwrite_block(BCWriteCtx *ctx, const void *p, MSize len)
45{
46 uint8_t *q = (uint8_t *)(ctx->sb.buf + ctx->sb.n);
47 MSize i;
48 ctx->sb.n += len;
49 for (i = 0; i < len; i++) q[i] = ((uint8_t *)p)[i];
50}
51
52/* Add byte to buffer. */
53static LJ_AINLINE void bcwrite_byte(BCWriteCtx *ctx, uint8_t b)
54{
55 ctx->sb.buf[ctx->sb.n++] = b;
56}
57
58/* Add ULEB128 value to buffer. */
59static void bcwrite_uleb128(BCWriteCtx *ctx, uint32_t v)
60{
61 MSize n = ctx->sb.n;
62 uint8_t *p = (uint8_t *)ctx->sb.buf;
63 for (; v >= 0x80; v >>= 7)
64 p[n++] = (uint8_t)((v & 0x7f) | 0x80);
65 p[n++] = (uint8_t)v;
66 ctx->sb.n = n;
67}
68
69/* -- Bytecode writer ----------------------------------------------------- */ 35/* -- Bytecode writer ----------------------------------------------------- */
70 36
71/* Write a single constant key/value of a template table. */ 37/* Write a single constant key/value of a template table. */
72static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow) 38static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)
73{ 39{
74 bcwrite_need(ctx, 1+10); 40 char *p = lj_buf_more(ctx->L, &ctx->sb, 1+10);
75 if (tvisstr(o)) { 41 if (tvisstr(o)) {
76 const GCstr *str = strV(o); 42 const GCstr *str = strV(o);
77 MSize len = str->len; 43 MSize len = str->len;
78 bcwrite_need(ctx, 5+len); 44 p = lj_buf_more(ctx->L, &ctx->sb, 5+len);
79 bcwrite_uleb128(ctx, BCDUMP_KTAB_STR+len); 45 p = lj_buf_wuleb128(p, BCDUMP_KTAB_STR+len);
80 bcwrite_block(ctx, strdata(str), len); 46 p = lj_buf_wmem(p, strdata(str), len);
81 } else if (tvisint(o)) { 47 } else if (tvisint(o)) {
82 bcwrite_byte(ctx, BCDUMP_KTAB_INT); 48 *p++ = BCDUMP_KTAB_INT;
83 bcwrite_uleb128(ctx, intV(o)); 49 p = lj_buf_wuleb128(p, intV(o));
84 } else if (tvisnum(o)) { 50 } else if (tvisnum(o)) {
85 if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */ 51 if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */
86 lua_Number num = numV(o); 52 lua_Number num = numV(o);
87 int32_t k = lj_num2int(num); 53 int32_t k = lj_num2int(num);
88 if (num == (lua_Number)k) { /* -0 is never a constant. */ 54 if (num == (lua_Number)k) { /* -0 is never a constant. */
89 bcwrite_byte(ctx, BCDUMP_KTAB_INT); 55 *p++ = BCDUMP_KTAB_INT;
90 bcwrite_uleb128(ctx, k); 56 p = lj_buf_wuleb128(p, k);
57 setsbufP(&ctx->sb, p);
91 return; 58 return;
92 } 59 }
93 } 60 }
94 bcwrite_byte(ctx, BCDUMP_KTAB_NUM); 61 *p++ = BCDUMP_KTAB_NUM;
95 bcwrite_uleb128(ctx, o->u32.lo); 62 p = lj_buf_wuleb128(p, o->u32.lo);
96 bcwrite_uleb128(ctx, o->u32.hi); 63 p = lj_buf_wuleb128(p, o->u32.hi);
97 } else { 64 } else {
98 lua_assert(tvispri(o)); 65 lua_assert(tvispri(o));
99 bcwrite_byte(ctx, BCDUMP_KTAB_NIL+~itype(o)); 66 *p++ = BCDUMP_KTAB_NIL+~itype(o);
100 } 67 }
68 setsbufP(&ctx->sb, p);
101} 69}
102 70
103/* Write a template table. */ 71/* Write a template table. */
104static void bcwrite_ktab(BCWriteCtx *ctx, const GCtab *t) 72static void bcwrite_ktab(BCWriteCtx *ctx, char *p, const GCtab *t)
105{ 73{
106 MSize narray = 0, nhash = 0; 74 MSize narray = 0, nhash = 0;
107 if (t->asize > 0) { /* Determine max. length of array part. */ 75 if (t->asize > 0) { /* Determine max. length of array part. */
@@ -119,8 +87,9 @@ static void bcwrite_ktab(BCWriteCtx *ctx, const GCtab *t)
119 nhash += !tvisnil(&node[i].val); 87 nhash += !tvisnil(&node[i].val);
120 } 88 }
121 /* Write number of array slots and hash slots. */ 89 /* Write number of array slots and hash slots. */
122 bcwrite_uleb128(ctx, narray); 90 p = lj_buf_wuleb128(p, narray);
123 bcwrite_uleb128(ctx, nhash); 91 p = lj_buf_wuleb128(p, nhash);
92 setsbufP(&ctx->sb, p);
124 if (narray) { /* Write array entries (may contain nil). */ 93 if (narray) { /* Write array entries (may contain nil). */
125 MSize i; 94 MSize i;
126 TValue *o = tvref(t->array); 95 TValue *o = tvref(t->array);
@@ -147,6 +116,7 @@ static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
147 for (i = 0; i < sizekgc; i++, kr++) { 116 for (i = 0; i < sizekgc; i++, kr++) {
148 GCobj *o = gcref(*kr); 117 GCobj *o = gcref(*kr);
149 MSize tp, need = 1; 118 MSize tp, need = 1;
119 char *p;
150 /* Determine constant type and needed size. */ 120 /* Determine constant type and needed size. */
151 if (o->gch.gct == ~LJ_TSTR) { 121 if (o->gch.gct == ~LJ_TSTR) {
152 tp = BCDUMP_KGC_STR + gco2str(o)->len; 122 tp = BCDUMP_KGC_STR + gco2str(o)->len;
@@ -173,24 +143,26 @@ static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
173 need = 1+2*5; 143 need = 1+2*5;
174 } 144 }
175 /* Write constant type. */ 145 /* Write constant type. */
176 bcwrite_need(ctx, need); 146 p = lj_buf_more(ctx->L, &ctx->sb, need);
177 bcwrite_uleb128(ctx, tp); 147 p = lj_buf_wuleb128(p, tp);
178 /* Write constant data (if any). */ 148 /* Write constant data (if any). */
179 if (tp >= BCDUMP_KGC_STR) { 149 if (tp >= BCDUMP_KGC_STR) {
180 bcwrite_block(ctx, strdata(gco2str(o)), gco2str(o)->len); 150 p = lj_buf_wmem(p, strdata(gco2str(o)), gco2str(o)->len);
181 } else if (tp == BCDUMP_KGC_TAB) { 151 } else if (tp == BCDUMP_KGC_TAB) {
182 bcwrite_ktab(ctx, gco2tab(o)); 152 bcwrite_ktab(ctx, p, gco2tab(o));
153 continue;
183#if LJ_HASFFI 154#if LJ_HASFFI
184 } else if (tp != BCDUMP_KGC_CHILD) { 155 } else if (tp != BCDUMP_KGC_CHILD) {
185 cTValue *p = (TValue *)cdataptr(gco2cd(o)); 156 cTValue *q = (TValue *)cdataptr(gco2cd(o));
186 bcwrite_uleb128(ctx, p[0].u32.lo); 157 p = lj_buf_wuleb128(p, q[0].u32.lo);
187 bcwrite_uleb128(ctx, p[0].u32.hi); 158 p = lj_buf_wuleb128(p, q[0].u32.hi);
188 if (tp == BCDUMP_KGC_COMPLEX) { 159 if (tp == BCDUMP_KGC_COMPLEX) {
189 bcwrite_uleb128(ctx, p[1].u32.lo); 160 p = lj_buf_wuleb128(p, q[1].u32.lo);
190 bcwrite_uleb128(ctx, p[1].u32.hi); 161 p = lj_buf_wuleb128(p, q[1].u32.hi);
191 } 162 }
192#endif 163#endif
193 } 164 }
165 setsbufP(&ctx->sb, p);
194 } 166 }
195} 167}
196 168
@@ -199,7 +171,7 @@ static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
199{ 171{
200 MSize i, sizekn = pt->sizekn; 172 MSize i, sizekn = pt->sizekn;
201 cTValue *o = mref(pt->k, TValue); 173 cTValue *o = mref(pt->k, TValue);
202 bcwrite_need(ctx, 10*sizekn); 174 char *p = lj_buf_more(ctx->L, &ctx->sb, 10*sizekn);
203 for (i = 0; i < sizekn; i++, o++) { 175 for (i = 0; i < sizekn; i++, o++) {
204 int32_t k; 176 int32_t k;
205 if (tvisint(o)) { 177 if (tvisint(o)) {
@@ -212,58 +184,58 @@ static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
212 k = lj_num2int(num); 184 k = lj_num2int(num);
213 if (num == (lua_Number)k) { /* -0 is never a constant. */ 185 if (num == (lua_Number)k) { /* -0 is never a constant. */
214 save_int: 186 save_int:
215 bcwrite_uleb128(ctx, 2*(uint32_t)k | ((uint32_t)k & 0x80000000u)); 187 p = lj_buf_wuleb128(p, 2*(uint32_t)k | ((uint32_t)k & 0x80000000u));
216 if (k < 0) { 188 if (k < 0)
217 char *p = &ctx->sb.buf[ctx->sb.n-1]; 189 p[-1] = (p[-1] & 7) | ((k>>27) & 0x18);
218 *p = (*p & 7) | ((k>>27) & 0x18);
219 }
220 continue; 190 continue;
221 } 191 }
222 } 192 }
223 bcwrite_uleb128(ctx, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u))); 193 p = lj_buf_wuleb128(p, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));
224 if (o->u32.lo >= 0x80000000u) { 194 if (o->u32.lo >= 0x80000000u)
225 char *p = &ctx->sb.buf[ctx->sb.n-1]; 195 p[-1] = (p[-1] & 7) | ((o->u32.lo>>27) & 0x18);
226 *p = (*p & 7) | ((o->u32.lo>>27) & 0x18); 196 p = lj_buf_wuleb128(p, o->u32.hi);
227 }
228 bcwrite_uleb128(ctx, o->u32.hi);
229 } 197 }
230 } 198 }
199 setsbufP(&ctx->sb, p);
231} 200}
232 201
233/* Write bytecode instructions. */ 202/* Write bytecode instructions. */
234static void bcwrite_bytecode(BCWriteCtx *ctx, GCproto *pt) 203static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt)
235{ 204{
236 MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */ 205 MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */
237#if LJ_HASJIT 206#if LJ_HASJIT
238 uint8_t *p = (uint8_t *)&ctx->sb.buf[ctx->sb.n]; 207 uint8_t *q = (uint8_t *)p;
239#endif 208#endif
240 bcwrite_block(ctx, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns)); 209 p = lj_buf_wmem(p, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));
210 UNUSED(ctx);
241#if LJ_HASJIT 211#if LJ_HASJIT
242 /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */ 212 /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
243 if ((pt->flags & PROTO_ILOOP) || pt->trace) { 213 if ((pt->flags & PROTO_ILOOP) || pt->trace) {
244 jit_State *J = L2J(ctx->L); 214 jit_State *J = L2J(ctx->L);
245 MSize i; 215 MSize i;
246 for (i = 0; i < nbc; i++, p += sizeof(BCIns)) { 216 for (i = 0; i < nbc; i++, q += sizeof(BCIns)) {
247 BCOp op = (BCOp)p[LJ_ENDIAN_SELECT(0, 3)]; 217 BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)];
248 if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP || 218 if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||
249 op == BC_JFORI) { 219 op == BC_JFORI) {
250 p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL); 220 q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);
251 } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) { 221 } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
252 BCReg rd = p[LJ_ENDIAN_SELECT(2, 1)] + (p[LJ_ENDIAN_SELECT(3, 0)] << 8); 222 BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8);
253 BCIns ins = traceref(J, rd)->startins; 223 BCIns ins = traceref(J, rd)->startins;
254 p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL); 224 q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL);
255 p[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins); 225 q[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins);
256 p[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins); 226 q[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins);
257 } 227 }
258 } 228 }
259 } 229 }
260#endif 230#endif
231 return p;
261} 232}
262 233
263/* Write prototype. */ 234/* Write prototype. */
264static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) 235static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
265{ 236{
266 MSize sizedbg = 0; 237 MSize sizedbg = 0;
238 char *p;
267 239
268 /* Recursively write children of prototype. */ 240 /* Recursively write children of prototype. */
269 if ((pt->flags & PROTO_CHILD)) { 241 if ((pt->flags & PROTO_CHILD)) {
@@ -277,31 +249,32 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
277 } 249 }
278 250
279 /* Start writing the prototype info to a buffer. */ 251 /* Start writing the prototype info to a buffer. */
280 lj_buf_reset(&ctx->sb); 252 p = lj_buf_need(ctx->L, &ctx->sb,
281 ctx->sb.n = 5; /* Leave room for final size. */ 253 5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
282 bcwrite_need(ctx, 4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2); 254 p += 5; /* Leave room for final size. */
283 255
284 /* Write prototype header. */ 256 /* Write prototype header. */
285 bcwrite_byte(ctx, (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI))); 257 *p++ = (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI));
286 bcwrite_byte(ctx, pt->numparams); 258 *p++ = pt->numparams;
287 bcwrite_byte(ctx, pt->framesize); 259 *p++ = pt->framesize;
288 bcwrite_byte(ctx, pt->sizeuv); 260 *p++ = pt->sizeuv;
289 bcwrite_uleb128(ctx, pt->sizekgc); 261 p = lj_buf_wuleb128(p, pt->sizekgc);
290 bcwrite_uleb128(ctx, pt->sizekn); 262 p = lj_buf_wuleb128(p, pt->sizekn);
291 bcwrite_uleb128(ctx, pt->sizebc-1); 263 p = lj_buf_wuleb128(p, pt->sizebc-1);
292 if (!ctx->strip) { 264 if (!ctx->strip) {
293 if (proto_lineinfo(pt)) 265 if (proto_lineinfo(pt))
294 sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt); 266 sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);
295 bcwrite_uleb128(ctx, sizedbg); 267 p = lj_buf_wuleb128(p, sizedbg);
296 if (sizedbg) { 268 if (sizedbg) {
297 bcwrite_uleb128(ctx, pt->firstline); 269 p = lj_buf_wuleb128(p, pt->firstline);
298 bcwrite_uleb128(ctx, pt->numline); 270 p = lj_buf_wuleb128(p, pt->numline);
299 } 271 }
300 } 272 }
301 273
302 /* Write bytecode instructions and upvalue refs. */ 274 /* Write bytecode instructions and upvalue refs. */
303 bcwrite_bytecode(ctx, pt); 275 p = bcwrite_bytecode(ctx, p, pt);
304 bcwrite_block(ctx, proto_uv(pt), pt->sizeuv*2); 276 p = lj_buf_wmem(p, proto_uv(pt), pt->sizeuv*2);
277 setsbufP(&ctx->sb, p);
305 278
306 /* Write constants. */ 279 /* Write constants. */
307 bcwrite_kgc(ctx, pt); 280 bcwrite_kgc(ctx, pt);
@@ -309,18 +282,19 @@ static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
309 282
310 /* Write debug info, if not stripped. */ 283 /* Write debug info, if not stripped. */
311 if (sizedbg) { 284 if (sizedbg) {
312 bcwrite_need(ctx, sizedbg); 285 p = lj_buf_more(ctx->L, &ctx->sb, sizedbg);
313 bcwrite_block(ctx, proto_lineinfo(pt), sizedbg); 286 p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg);
287 setsbufP(&ctx->sb, p);
314 } 288 }
315 289
316 /* Pass buffer to writer function. */ 290 /* Pass buffer to writer function. */
317 if (ctx->status == 0) { 291 if (ctx->status == 0) {
318 MSize n = ctx->sb.n - 5; 292 MSize n = sbuflen(&ctx->sb) - 5;
319 MSize nn = (lj_fls(n)+8)*9 >> 6; 293 MSize nn = (lj_fls(n)+8)*9 >> 6;
320 ctx->sb.n = 5 - nn; 294 char *q = sbufB(&ctx->sb) + (5 - nn);
321 bcwrite_uleb128(ctx, n); /* Fill in final size. */ 295 p = lj_buf_wuleb128(q, n); /* Fill in final size. */
322 lua_assert(ctx->sb.n == 5); 296 lua_assert(p == sbufB(&ctx->sb) + 5);
323 ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf+5-nn, nn+n, ctx->wdata); 297 ctx->status = ctx->wfunc(ctx->L, q, nn+n, ctx->wdata);
324 } 298 }
325} 299}
326 300
@@ -330,20 +304,20 @@ static void bcwrite_header(BCWriteCtx *ctx)
330 GCstr *chunkname = proto_chunkname(ctx->pt); 304 GCstr *chunkname = proto_chunkname(ctx->pt);
331 const char *name = strdata(chunkname); 305 const char *name = strdata(chunkname);
332 MSize len = chunkname->len; 306 MSize len = chunkname->len;
333 lj_buf_reset(&ctx->sb); 307 char *p = lj_buf_need(ctx->L, &ctx->sb, 5+5+len);
334 bcwrite_need(ctx, 5+5+len); 308 *p++ = BCDUMP_HEAD1;
335 bcwrite_byte(ctx, BCDUMP_HEAD1); 309 *p++ = BCDUMP_HEAD2;
336 bcwrite_byte(ctx, BCDUMP_HEAD2); 310 *p++ = BCDUMP_HEAD3;
337 bcwrite_byte(ctx, BCDUMP_HEAD3); 311 *p++ = BCDUMP_VERSION;
338 bcwrite_byte(ctx, BCDUMP_VERSION); 312 *p++ = (ctx->strip ? BCDUMP_F_STRIP : 0) +
339 bcwrite_byte(ctx, (ctx->strip ? BCDUMP_F_STRIP : 0) + 313 (LJ_BE ? BCDUMP_F_BE : 0) +
340 (LJ_BE ? BCDUMP_F_BE : 0) + 314 ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0);
341 ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0));
342 if (!ctx->strip) { 315 if (!ctx->strip) {
343 bcwrite_uleb128(ctx, len); 316 p = lj_buf_wuleb128(p, len);
344 bcwrite_block(ctx, name, len); 317 p = lj_buf_wmem(p, name, len);
345 } 318 }
346 ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf, ctx->sb.n, ctx->wdata); 319 ctx->status = ctx->wfunc(ctx->L, sbufB(&ctx->sb),
320 (MSize)(p - sbufB(&ctx->sb)), ctx->wdata);
347} 321}
348 322
349/* Write footer of bytecode dump. */ 323/* Write footer of bytecode dump. */
@@ -360,7 +334,7 @@ static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)
360{ 334{
361 BCWriteCtx *ctx = (BCWriteCtx *)ud; 335 BCWriteCtx *ctx = (BCWriteCtx *)ud;
362 UNUSED(dummy); 336 UNUSED(dummy);
363 lj_buf_grow(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */ 337 lj_buf_need(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */
364 bcwrite_header(ctx); 338 bcwrite_header(ctx);
365 bcwrite_proto(ctx, ctx->pt); 339 bcwrite_proto(ctx, ctx->pt);
366 bcwrite_footer(ctx); 340 bcwrite_footer(ctx);