Merge branch 'master' into v2.1
[luajit-2.0.git] / src / lj_bcwrite.c
blobdd9694135e53a969c2b413e238dadcbfce8132d7
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 int strip; /* Strip debug info. */
31 int status; /* Status from writer callback. */
32 #ifdef LUA_USE_ASSERT
33 global_State *g;
34 #endif
35 } BCWriteCtx;
37 #ifdef LUA_USE_ASSERT
38 #define lj_assertBCW(c, ...) lj_assertG_(ctx->g, (c), __VA_ARGS__)
39 #else
40 #define lj_assertBCW(c, ...) ((void)ctx)
41 #endif
43 /* -- Bytecode writer ----------------------------------------------------- */
45 /* Write a single constant key/value of a template table. */
46 static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)
48 char *p = lj_buf_more(&ctx->sb, 1+10);
49 if (tvisstr(o)) {
50 const GCstr *str = strV(o);
51 MSize len = str->len;
52 p = lj_buf_more(&ctx->sb, 5+len);
53 p = lj_strfmt_wuleb128(p, BCDUMP_KTAB_STR+len);
54 p = lj_buf_wmem(p, strdata(str), len);
55 } else if (tvisint(o)) {
56 *p++ = BCDUMP_KTAB_INT;
57 p = lj_strfmt_wuleb128(p, intV(o));
58 } else if (tvisnum(o)) {
59 if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */
60 lua_Number num = numV(o);
61 int32_t k = lj_num2int(num);
62 if (num == (lua_Number)k) { /* -0 is never a constant. */
63 *p++ = BCDUMP_KTAB_INT;
64 p = lj_strfmt_wuleb128(p, k);
65 ctx->sb.w = p;
66 return;
69 *p++ = BCDUMP_KTAB_NUM;
70 p = lj_strfmt_wuleb128(p, o->u32.lo);
71 p = lj_strfmt_wuleb128(p, o->u32.hi);
72 } else {
73 lj_assertBCW(tvispri(o), "unhandled type %d", itype(o));
74 *p++ = BCDUMP_KTAB_NIL+~itype(o);
76 ctx->sb.w = p;
79 /* Write a template table. */
80 static void bcwrite_ktab(BCWriteCtx *ctx, char *p, const GCtab *t)
82 MSize narray = 0, nhash = 0;
83 if (t->asize > 0) { /* Determine max. length of array part. */
84 ptrdiff_t i;
85 TValue *array = tvref(t->array);
86 for (i = (ptrdiff_t)t->asize-1; i >= 0; i--)
87 if (!tvisnil(&array[i]))
88 break;
89 narray = (MSize)(i+1);
91 if (t->hmask > 0) { /* Count number of used hash slots. */
92 MSize i, hmask = t->hmask;
93 Node *node = noderef(t->node);
94 for (i = 0; i <= hmask; i++)
95 nhash += !tvisnil(&node[i].val);
97 /* Write number of array slots and hash slots. */
98 p = lj_strfmt_wuleb128(p, narray);
99 p = lj_strfmt_wuleb128(p, nhash);
100 ctx->sb.w = p;
101 if (narray) { /* Write array entries (may contain nil). */
102 MSize i;
103 TValue *o = tvref(t->array);
104 for (i = 0; i < narray; i++, o++)
105 bcwrite_ktabk(ctx, o, 1);
107 if (nhash) { /* Write hash entries. */
108 MSize i = nhash;
109 Node *node = noderef(t->node) + t->hmask;
110 for (;; node--)
111 if (!tvisnil(&node->val)) {
112 bcwrite_ktabk(ctx, &node->key, 0);
113 bcwrite_ktabk(ctx, &node->val, 1);
114 if (--i == 0) break;
119 /* Write GC constants of a prototype. */
120 static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
122 MSize i, sizekgc = pt->sizekgc;
123 GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc;
124 for (i = 0; i < sizekgc; i++, kr++) {
125 GCobj *o = gcref(*kr);
126 MSize tp, need = 1;
127 char *p;
128 /* Determine constant type and needed size. */
129 if (o->gch.gct == ~LJ_TSTR) {
130 tp = BCDUMP_KGC_STR + gco2str(o)->len;
131 need = 5+gco2str(o)->len;
132 } else if (o->gch.gct == ~LJ_TPROTO) {
133 lj_assertBCW((pt->flags & PROTO_CHILD), "prototype has unexpected child");
134 tp = BCDUMP_KGC_CHILD;
135 #if LJ_HASFFI
136 } else if (o->gch.gct == ~LJ_TCDATA) {
137 CTypeID id = gco2cd(o)->ctypeid;
138 need = 1+4*5;
139 if (id == CTID_INT64) {
140 tp = BCDUMP_KGC_I64;
141 } else if (id == CTID_UINT64) {
142 tp = BCDUMP_KGC_U64;
143 } else {
144 lj_assertBCW(id == CTID_COMPLEX_DOUBLE,
145 "bad cdata constant CTID %d", id);
146 tp = BCDUMP_KGC_COMPLEX;
148 #endif
149 } else {
150 lj_assertBCW(o->gch.gct == ~LJ_TTAB,
151 "bad constant GC type %d", o->gch.gct);
152 tp = BCDUMP_KGC_TAB;
153 need = 1+2*5;
155 /* Write constant type. */
156 p = lj_buf_more(&ctx->sb, need);
157 p = lj_strfmt_wuleb128(p, tp);
158 /* Write constant data (if any). */
159 if (tp >= BCDUMP_KGC_STR) {
160 p = lj_buf_wmem(p, strdata(gco2str(o)), gco2str(o)->len);
161 } else if (tp == BCDUMP_KGC_TAB) {
162 bcwrite_ktab(ctx, p, gco2tab(o));
163 continue;
164 #if LJ_HASFFI
165 } else if (tp != BCDUMP_KGC_CHILD) {
166 cTValue *q = (TValue *)cdataptr(gco2cd(o));
167 p = lj_strfmt_wuleb128(p, q[0].u32.lo);
168 p = lj_strfmt_wuleb128(p, q[0].u32.hi);
169 if (tp == BCDUMP_KGC_COMPLEX) {
170 p = lj_strfmt_wuleb128(p, q[1].u32.lo);
171 p = lj_strfmt_wuleb128(p, q[1].u32.hi);
173 #endif
175 ctx->sb.w = p;
179 /* Write number constants of a prototype. */
180 static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
182 MSize i, sizekn = pt->sizekn;
183 cTValue *o = mref(pt->k, TValue);
184 char *p = lj_buf_more(&ctx->sb, 10*sizekn);
185 for (i = 0; i < sizekn; i++, o++) {
186 int32_t k;
187 if (tvisint(o)) {
188 k = intV(o);
189 goto save_int;
190 } else {
191 /* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */
192 if (!LJ_DUALNUM && o->u32.hi != LJ_KEYINDEX) {
193 /* Narrow number constants to integers. */
194 lua_Number num = numV(o);
195 k = lj_num2int(num);
196 if (num == (lua_Number)k) { /* -0 is never a constant. */
197 save_int:
198 p = lj_strfmt_wuleb128(p, 2*(uint32_t)k | ((uint32_t)k&0x80000000u));
199 if (k < 0)
200 p[-1] = (p[-1] & 7) | ((k>>27) & 0x18);
201 continue;
204 p = lj_strfmt_wuleb128(p, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));
205 if (o->u32.lo >= 0x80000000u)
206 p[-1] = (p[-1] & 7) | ((o->u32.lo>>27) & 0x18);
207 p = lj_strfmt_wuleb128(p, o->u32.hi);
210 ctx->sb.w = p;
213 /* Write bytecode instructions. */
214 static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt)
216 MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */
217 #if LJ_HASJIT
218 uint8_t *q = (uint8_t *)p;
219 #endif
220 p = lj_buf_wmem(p, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));
221 UNUSED(ctx);
222 #if LJ_HASJIT
223 /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
224 if ((pt->flags & PROTO_ILOOP) || pt->trace) {
225 jit_State *J = L2J(sbufL(&ctx->sb));
226 MSize i;
227 for (i = 0; i < nbc; i++, q += sizeof(BCIns)) {
228 BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)];
229 if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||
230 op == BC_JFORI) {
231 q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);
232 } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
233 BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8);
234 memcpy(q, &traceref(J, rd)->startins, 4);
238 #endif
239 return p;
242 /* Write prototype. */
243 static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
245 MSize sizedbg = 0;
246 char *p;
248 /* Recursively write children of prototype. */
249 if ((pt->flags & PROTO_CHILD)) {
250 ptrdiff_t i, n = pt->sizekgc;
251 GCRef *kr = mref(pt->k, GCRef) - 1;
252 for (i = 0; i < n; i++, kr--) {
253 GCobj *o = gcref(*kr);
254 if (o->gch.gct == ~LJ_TPROTO)
255 bcwrite_proto(ctx, gco2pt(o));
259 /* Start writing the prototype info to a buffer. */
260 p = lj_buf_need(&ctx->sb,
261 5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
262 p += 5; /* Leave room for final size. */
264 /* Write prototype header. */
265 *p++ = (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI));
266 *p++ = pt->numparams;
267 *p++ = pt->framesize;
268 *p++ = pt->sizeuv;
269 p = lj_strfmt_wuleb128(p, pt->sizekgc);
270 p = lj_strfmt_wuleb128(p, pt->sizekn);
271 p = lj_strfmt_wuleb128(p, pt->sizebc-1);
272 if (!ctx->strip) {
273 if (proto_lineinfo(pt))
274 sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);
275 p = lj_strfmt_wuleb128(p, sizedbg);
276 if (sizedbg) {
277 p = lj_strfmt_wuleb128(p, pt->firstline);
278 p = lj_strfmt_wuleb128(p, pt->numline);
282 /* Write bytecode instructions and upvalue refs. */
283 p = bcwrite_bytecode(ctx, p, pt);
284 p = lj_buf_wmem(p, proto_uv(pt), pt->sizeuv*2);
285 ctx->sb.w = p;
287 /* Write constants. */
288 bcwrite_kgc(ctx, pt);
289 bcwrite_knum(ctx, pt);
291 /* Write debug info, if not stripped. */
292 if (sizedbg) {
293 p = lj_buf_more(&ctx->sb, sizedbg);
294 p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg);
295 ctx->sb.w = p;
298 /* Pass buffer to writer function. */
299 if (ctx->status == 0) {
300 MSize n = sbuflen(&ctx->sb) - 5;
301 MSize nn = (lj_fls(n)+8)*9 >> 6;
302 char *q = ctx->sb.b + (5 - nn);
303 p = lj_strfmt_wuleb128(q, n); /* Fill in final size. */
304 lj_assertBCW(p == ctx->sb.b + 5, "bad ULEB128 write");
305 ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata);
309 /* Write header of bytecode dump. */
310 static void bcwrite_header(BCWriteCtx *ctx)
312 GCstr *chunkname = proto_chunkname(ctx->pt);
313 const char *name = strdata(chunkname);
314 MSize len = chunkname->len;
315 char *p = lj_buf_need(&ctx->sb, 5+5+len);
316 *p++ = BCDUMP_HEAD1;
317 *p++ = BCDUMP_HEAD2;
318 *p++ = BCDUMP_HEAD3;
319 *p++ = BCDUMP_VERSION;
320 *p++ = (ctx->strip ? BCDUMP_F_STRIP : 0) +
321 LJ_BE*BCDUMP_F_BE +
322 ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0) +
323 LJ_FR2*BCDUMP_F_FR2;
324 if (!ctx->strip) {
325 p = lj_strfmt_wuleb128(p, len);
326 p = lj_buf_wmem(p, name, len);
328 ctx->status = ctx->wfunc(sbufL(&ctx->sb), ctx->sb.b,
329 (MSize)(p - ctx->sb.b), ctx->wdata);
332 /* Write footer of bytecode dump. */
333 static void bcwrite_footer(BCWriteCtx *ctx)
335 if (ctx->status == 0) {
336 uint8_t zero = 0;
337 ctx->status = ctx->wfunc(sbufL(&ctx->sb), &zero, 1, ctx->wdata);
341 /* Protected callback for bytecode writer. */
342 static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)
344 BCWriteCtx *ctx = (BCWriteCtx *)ud;
345 UNUSED(L); UNUSED(dummy);
346 lj_buf_need(&ctx->sb, 1024); /* Avoids resize for most prototypes. */
347 bcwrite_header(ctx);
348 bcwrite_proto(ctx, ctx->pt);
349 bcwrite_footer(ctx);
350 return NULL;
353 /* Write bytecode for a prototype. */
354 int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data,
355 int strip)
357 BCWriteCtx ctx;
358 int status;
359 ctx.pt = pt;
360 ctx.wfunc = writer;
361 ctx.wdata = data;
362 ctx.strip = strip;
363 ctx.status = 0;
364 #ifdef LUA_USE_ASSERT
365 ctx.g = G(L);
366 #endif
367 lj_buf_init(L, &ctx.sb);
368 status = lj_vm_cpcall(L, NULL, &ctx, cpwriter);
369 if (status == 0) status = ctx.status;
370 lj_buf_free(G(sbufL(&ctx.sb)), &ctx.sb);
371 return status;