Drop unused function wrapper.
[luajit-2.0.git] / src / lj_bcwrite.c
blobddfa46c56d1322b2d4729cc929186f2f7d7fabe7
1 /*
2 ** Bytecode writer.
3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #define lj_bcwrite_c
7 #define LUA_CORE
9 #include "lj_obj.h"
10 #include "lj_gc.h"
11 #include "lj_buf.h"
12 #include "lj_bc.h"
13 #if LJ_HASFFI
14 #include "lj_ctype.h"
15 #endif
16 #if LJ_HASJIT
17 #include "lj_dispatch.h"
18 #include "lj_jit.h"
19 #endif
20 #include "lj_strfmt.h"
21 #include "lj_bcdump.h"
22 #include "lj_vm.h"
24 /* Context for bytecode writer. */
25 typedef struct BCWriteCtx {
26 SBuf sb; /* Output buffer. */
27 GCproto *pt; /* Root prototype. */
28 lua_Writer wfunc; /* Writer callback. */
29 void *wdata; /* Writer callback data. */
30 TValue **heap; /* Heap used for deterministic sorting. */
31 uint32_t heapsz; /* Size of heap. */
32 uint32_t flags; /* BCDUMP_F_* flags. */
33 int status; /* Status from writer callback. */
34 #ifdef LUA_USE_ASSERT
35 global_State *g;
36 #endif
37 } BCWriteCtx;
39 #ifdef LUA_USE_ASSERT
40 #define lj_assertBCW(c, ...) lj_assertG_(ctx->g, (c), __VA_ARGS__)
41 #else
42 #define lj_assertBCW(c, ...) ((void)ctx)
43 #endif
45 /* -- Bytecode writer ----------------------------------------------------- */
47 /* Write a single constant key/value of a template table. */
48 static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)
50 char *p = lj_buf_more(&ctx->sb, 1+10);
51 if (tvisstr(o)) {
52 const GCstr *str = strV(o);
53 MSize len = str->len;
54 p = lj_buf_more(&ctx->sb, 5+len);
55 p = lj_strfmt_wuleb128(p, BCDUMP_KTAB_STR+len);
56 p = lj_buf_wmem(p, strdata(str), len);
57 } else if (tvisint(o)) {
58 *p++ = BCDUMP_KTAB_INT;
59 p = lj_strfmt_wuleb128(p, intV(o));
60 } else if (tvisnum(o)) {
61 if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */
62 lua_Number num = numV(o);
63 int32_t k = lj_num2int(num);
64 if (num == (lua_Number)k) { /* -0 is never a constant. */
65 *p++ = BCDUMP_KTAB_INT;
66 p = lj_strfmt_wuleb128(p, k);
67 ctx->sb.w = p;
68 return;
71 *p++ = BCDUMP_KTAB_NUM;
72 p = lj_strfmt_wuleb128(p, o->u32.lo);
73 p = lj_strfmt_wuleb128(p, o->u32.hi);
74 } else {
75 lj_assertBCW(tvispri(o), "unhandled type %d", itype(o));
76 *p++ = BCDUMP_KTAB_NIL+~itype(o);
78 ctx->sb.w = p;
81 /* Compare two template table keys. */
82 static LJ_AINLINE int bcwrite_ktabk_lt(TValue *a, TValue *b)
84 uint32_t at = itype(a), bt = itype(b);
85 if (at != bt) { /* This also handles false and true keys. */
86 return at < bt;
87 } else if (at == LJ_TSTR) {
88 return lj_str_cmp(strV(a), strV(b)) < 0;
89 } else {
90 return a->u64 < b->u64; /* This works for numbers and integers. */
94 /* Insert key into a sorted heap. */
95 static void bcwrite_ktabk_heap_insert(TValue **heap, MSize idx, MSize end,
96 TValue *key)
98 MSize child;
99 while ((child = idx * 2 + 1) < end) {
100 /* Find lower of the two children. */
101 TValue *c0 = heap[child];
102 if (child + 1 < end) {
103 TValue *c1 = heap[child + 1];
104 if (bcwrite_ktabk_lt(c1, c0)) {
105 c0 = c1;
106 child++;
109 if (bcwrite_ktabk_lt(key, c0)) break; /* Key lower? Found our position. */
110 heap[idx] = c0; /* Move lower child up. */
111 idx = child; /* Descend. */
113 heap[idx] = key; /* Insert key here. */
116 /* Resize heap, dropping content. */
117 static void bcwrite_heap_resize(BCWriteCtx *ctx, uint32_t nsz)
119 lua_State *L = sbufL(&ctx->sb);
120 if (ctx->heapsz) {
121 lj_mem_freevec(G(L), ctx->heap, ctx->heapsz, TValue *);
122 ctx->heapsz = 0;
124 if (nsz) {
125 ctx->heap = lj_mem_newvec(L, nsz, TValue *);
126 ctx->heapsz = nsz;
130 /* Write hash part of template table in sorted order. */
131 static void bcwrite_ktab_sorted_hash(BCWriteCtx *ctx, Node *node, MSize nhash)
133 TValue **heap = ctx->heap;
134 MSize i = nhash;
135 for (;; node--) { /* Build heap. */
136 if (!tvisnil(&node->key)) {
137 bcwrite_ktabk_heap_insert(heap, --i, nhash, &node->key);
138 if (i == 0) break;
141 do { /* Drain heap. */
142 TValue *key = heap[0]; /* Output lowest key from top. */
143 bcwrite_ktabk(ctx, key, 0);
144 bcwrite_ktabk(ctx, (TValue *)((char *)key - offsetof(Node, key)), 1);
145 key = heap[--nhash]; /* Remove last key. */
146 bcwrite_ktabk_heap_insert(heap, 0, nhash, key); /* Re-insert. */
147 } while (nhash);
150 /* Write a template table. */
151 static void bcwrite_ktab(BCWriteCtx *ctx, char *p, const GCtab *t)
153 MSize narray = 0, nhash = 0;
154 if (t->asize > 0) { /* Determine max. length of array part. */
155 ptrdiff_t i;
156 TValue *array = tvref(t->array);
157 for (i = (ptrdiff_t)t->asize-1; i >= 0; i--)
158 if (!tvisnil(&array[i]))
159 break;
160 narray = (MSize)(i+1);
162 if (t->hmask > 0) { /* Count number of used hash slots. */
163 MSize i, hmask = t->hmask;
164 Node *node = noderef(t->node);
165 for (i = 0; i <= hmask; i++)
166 nhash += !tvisnil(&node[i].key);
168 /* Write number of array slots and hash slots. */
169 p = lj_strfmt_wuleb128(p, narray);
170 p = lj_strfmt_wuleb128(p, nhash);
171 ctx->sb.w = p;
172 if (narray) { /* Write array entries (may contain nil). */
173 MSize i;
174 TValue *o = tvref(t->array);
175 for (i = 0; i < narray; i++, o++)
176 bcwrite_ktabk(ctx, o, 1);
178 if (nhash) { /* Write hash entries. */
179 Node *node = noderef(t->node) + t->hmask;
180 if ((ctx->flags & BCDUMP_F_DETERMINISTIC) && nhash > 1) {
181 if (ctx->heapsz < nhash)
182 bcwrite_heap_resize(ctx, t->hmask + 1);
183 bcwrite_ktab_sorted_hash(ctx, node, nhash);
184 } else {
185 MSize i = nhash;
186 for (;; node--)
187 if (!tvisnil(&node->key)) {
188 bcwrite_ktabk(ctx, &node->key, 0);
189 bcwrite_ktabk(ctx, &node->val, 1);
190 if (--i == 0) break;
196 /* Write GC constants of a prototype. */
197 static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
199 MSize i, sizekgc = pt->sizekgc;
200 GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc;
201 for (i = 0; i < sizekgc; i++, kr++) {
202 GCobj *o = gcref(*kr);
203 MSize tp, need = 1;
204 char *p;
205 /* Determine constant type and needed size. */
206 if (o->gch.gct == ~LJ_TSTR) {
207 tp = BCDUMP_KGC_STR + gco2str(o)->len;
208 need = 5+gco2str(o)->len;
209 } else if (o->gch.gct == ~LJ_TPROTO) {
210 lj_assertBCW((pt->flags & PROTO_CHILD), "prototype has unexpected child");
211 tp = BCDUMP_KGC_CHILD;
212 #if LJ_HASFFI
213 } else if (o->gch.gct == ~LJ_TCDATA) {
214 CTypeID id = gco2cd(o)->ctypeid;
215 need = 1+4*5;
216 if (id == CTID_INT64) {
217 tp = BCDUMP_KGC_I64;
218 } else if (id == CTID_UINT64) {
219 tp = BCDUMP_KGC_U64;
220 } else {
221 lj_assertBCW(id == CTID_COMPLEX_DOUBLE,
222 "bad cdata constant CTID %d", id);
223 tp = BCDUMP_KGC_COMPLEX;
225 #endif
226 } else {
227 lj_assertBCW(o->gch.gct == ~LJ_TTAB,
228 "bad constant GC type %d", o->gch.gct);
229 tp = BCDUMP_KGC_TAB;
230 need = 1+2*5;
232 /* Write constant type. */
233 p = lj_buf_more(&ctx->sb, need);
234 p = lj_strfmt_wuleb128(p, tp);
235 /* Write constant data (if any). */
236 if (tp >= BCDUMP_KGC_STR) {
237 p = lj_buf_wmem(p, strdata(gco2str(o)), gco2str(o)->len);
238 } else if (tp == BCDUMP_KGC_TAB) {
239 bcwrite_ktab(ctx, p, gco2tab(o));
240 continue;
241 #if LJ_HASFFI
242 } else if (tp != BCDUMP_KGC_CHILD) {
243 cTValue *q = (TValue *)cdataptr(gco2cd(o));
244 p = lj_strfmt_wuleb128(p, q[0].u32.lo);
245 p = lj_strfmt_wuleb128(p, q[0].u32.hi);
246 if (tp == BCDUMP_KGC_COMPLEX) {
247 p = lj_strfmt_wuleb128(p, q[1].u32.lo);
248 p = lj_strfmt_wuleb128(p, q[1].u32.hi);
250 #endif
252 ctx->sb.w = p;
256 /* Write number constants of a prototype. */
257 static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
259 MSize i, sizekn = pt->sizekn;
260 cTValue *o = mref(pt->k, TValue);
261 char *p = lj_buf_more(&ctx->sb, 10*sizekn);
262 for (i = 0; i < sizekn; i++, o++) {
263 int32_t k;
264 if (tvisint(o)) {
265 k = intV(o);
266 goto save_int;
267 } else {
268 /* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */
269 if (!LJ_DUALNUM && o->u32.hi != LJ_KEYINDEX) {
270 /* Narrow number constants to integers. */
271 lua_Number num = numV(o);
272 k = lj_num2int(num);
273 if (num == (lua_Number)k) { /* -0 is never a constant. */
274 save_int:
275 p = lj_strfmt_wuleb128(p, 2*(uint32_t)k | ((uint32_t)k&0x80000000u));
276 if (k < 0)
277 p[-1] = (p[-1] & 7) | ((k>>27) & 0x18);
278 continue;
281 p = lj_strfmt_wuleb128(p, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));
282 if (o->u32.lo >= 0x80000000u)
283 p[-1] = (p[-1] & 7) | ((o->u32.lo>>27) & 0x18);
284 p = lj_strfmt_wuleb128(p, o->u32.hi);
287 ctx->sb.w = p;
290 /* Write bytecode instructions. */
291 static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt)
293 MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */
294 #if LJ_HASJIT
295 uint8_t *q = (uint8_t *)p;
296 #endif
297 p = lj_buf_wmem(p, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));
298 UNUSED(ctx);
299 #if LJ_HASJIT
300 /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
301 if ((pt->flags & PROTO_ILOOP) || pt->trace) {
302 jit_State *J = L2J(sbufL(&ctx->sb));
303 MSize i;
304 for (i = 0; i < nbc; i++, q += sizeof(BCIns)) {
305 BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)];
306 if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||
307 op == BC_JFORI) {
308 q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);
309 } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
310 BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8);
311 memcpy(q, &traceref(J, rd)->startins, 4);
315 #endif
316 return p;
319 /* Write prototype. */
320 static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
322 MSize sizedbg = 0;
323 char *p;
325 /* Recursively write children of prototype. */
326 if ((pt->flags & PROTO_CHILD)) {
327 ptrdiff_t i, n = pt->sizekgc;
328 GCRef *kr = mref(pt->k, GCRef) - 1;
329 for (i = 0; i < n; i++, kr--) {
330 GCobj *o = gcref(*kr);
331 if (o->gch.gct == ~LJ_TPROTO)
332 bcwrite_proto(ctx, gco2pt(o));
336 /* Start writing the prototype info to a buffer. */
337 p = lj_buf_need(&ctx->sb,
338 5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
339 p += 5; /* Leave room for final size. */
341 /* Write prototype header. */
342 *p++ = (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI));
343 *p++ = pt->numparams;
344 *p++ = pt->framesize;
345 *p++ = pt->sizeuv;
346 p = lj_strfmt_wuleb128(p, pt->sizekgc);
347 p = lj_strfmt_wuleb128(p, pt->sizekn);
348 p = lj_strfmt_wuleb128(p, pt->sizebc-1);
349 if (!(ctx->flags & BCDUMP_F_STRIP)) {
350 if (proto_lineinfo(pt))
351 sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);
352 p = lj_strfmt_wuleb128(p, sizedbg);
353 if (sizedbg) {
354 p = lj_strfmt_wuleb128(p, pt->firstline);
355 p = lj_strfmt_wuleb128(p, pt->numline);
359 /* Write bytecode instructions and upvalue refs. */
360 p = bcwrite_bytecode(ctx, p, pt);
361 p = lj_buf_wmem(p, proto_uv(pt), pt->sizeuv*2);
362 ctx->sb.w = p;
364 /* Write constants. */
365 bcwrite_kgc(ctx, pt);
366 bcwrite_knum(ctx, pt);
368 /* Write debug info, if not stripped. */
369 if (sizedbg) {
370 p = lj_buf_more(&ctx->sb, sizedbg);
371 p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg);
372 ctx->sb.w = p;
375 /* Pass buffer to writer function. */
376 if (ctx->status == 0) {
377 MSize n = sbuflen(&ctx->sb) - 5;
378 MSize nn = (lj_fls(n)+8)*9 >> 6;
379 char *q = ctx->sb.b + (5 - nn);
380 p = lj_strfmt_wuleb128(q, n); /* Fill in final size. */
381 lj_assertBCW(p == ctx->sb.b + 5, "bad ULEB128 write");
382 ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata);
386 /* Write header of bytecode dump. */
387 static void bcwrite_header(BCWriteCtx *ctx)
389 GCstr *chunkname = proto_chunkname(ctx->pt);
390 const char *name = strdata(chunkname);
391 MSize len = chunkname->len;
392 char *p = lj_buf_need(&ctx->sb, 5+5+len);
393 *p++ = BCDUMP_HEAD1;
394 *p++ = BCDUMP_HEAD2;
395 *p++ = BCDUMP_HEAD3;
396 *p++ = BCDUMP_VERSION;
397 *p++ = (ctx->flags & (BCDUMP_F_STRIP | BCDUMP_F_FR2)) +
398 LJ_BE*BCDUMP_F_BE +
399 ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0);
400 if (!(ctx->flags & BCDUMP_F_STRIP)) {
401 p = lj_strfmt_wuleb128(p, len);
402 p = lj_buf_wmem(p, name, len);
404 ctx->status = ctx->wfunc(sbufL(&ctx->sb), ctx->sb.b,
405 (MSize)(p - ctx->sb.b), ctx->wdata);
408 /* Write footer of bytecode dump. */
409 static void bcwrite_footer(BCWriteCtx *ctx)
411 if (ctx->status == 0) {
412 uint8_t zero = 0;
413 ctx->status = ctx->wfunc(sbufL(&ctx->sb), &zero, 1, ctx->wdata);
417 /* Protected callback for bytecode writer. */
418 static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)
420 BCWriteCtx *ctx = (BCWriteCtx *)ud;
421 UNUSED(L); UNUSED(dummy);
422 lj_buf_need(&ctx->sb, 1024); /* Avoids resize for most prototypes. */
423 bcwrite_header(ctx);
424 bcwrite_proto(ctx, ctx->pt);
425 bcwrite_footer(ctx);
426 return NULL;
429 /* Write bytecode for a prototype. */
430 int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data,
431 uint32_t flags)
433 BCWriteCtx ctx;
434 int status;
435 ctx.pt = pt;
436 ctx.wfunc = writer;
437 ctx.wdata = data;
438 ctx.heapsz = 0;
439 if ((bc_op(proto_bc(pt)[0]) != BC_NOT) == LJ_FR2) flags |= BCDUMP_F_FR2;
440 ctx.flags = flags;
441 ctx.status = 0;
442 #ifdef LUA_USE_ASSERT
443 ctx.g = G(L);
444 #endif
445 lj_buf_init(L, &ctx.sb);
446 status = lj_vm_cpcall(L, NULL, &ctx, cpwriter);
447 if (status == 0) status = ctx.status;
448 lj_buf_free(G(sbufL(&ctx.sb)), &ctx.sb);
449 bcwrite_heap_resize(&ctx, 0);
450 return status;