1/*
2** Bytecode writer.
3** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#define lj_bcwrite_c
7#define LUA_CORE
8
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"
23
24/* Context for bytecode writer. */
25typedef 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;
36
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
42
43/* -- Bytecode writer ----------------------------------------------------- */
44
45/* Write a single constant key/value of a template table. */
46static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow)
47{
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 setsbufP(&ctx->sb, p);
66 return;
67 }
68 }
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);
75 }
76 setsbufP(&ctx->sb, p);
77}
78
79/* Write a template table. */
80static void bcwrite_ktab(BCWriteCtx *ctx, char *p, const GCtab *t)
81{
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);
90 }
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);
96 }
97 /* Write number of array slots and hash slots. */
98 p = lj_strfmt_wuleb128(p, narray);
99 p = lj_strfmt_wuleb128(p, nhash);
100 setsbufP(&ctx->sb, 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);
106 }
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;
115 }
116 }
117}
118
119/* Write GC constants of a prototype. */
120static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt)
121{
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;
147 }
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;
154 }
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);
172 }
173#endif
174 }
175 setsbufP(&ctx->sb, p);
176 }
177}
178
179/* Write number constants of a prototype. */
180static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt)
181{
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) { /* Narrow number constants to integers. */
193 lua_Number num = numV(o);
194 k = lj_num2int(num);
195 if (num == (lua_Number)k) { /* -0 is never a constant. */
196 save_int:
197 p = lj_strfmt_wuleb128(p, 2*(uint32_t)k | ((uint32_t)k&0x80000000u));
198 if (k < 0)
199 p[-1] = (p[-1] & 7) | ((k>>27) & 0x18);
200 continue;
201 }
202 }
203 p = lj_strfmt_wuleb128(p, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u)));
204 if (o->u32.lo >= 0x80000000u)
205 p[-1] = (p[-1] & 7) | ((o->u32.lo>>27) & 0x18);
206 p = lj_strfmt_wuleb128(p, o->u32.hi);
207 }
208 }
209 setsbufP(&ctx->sb, p);
210}
211
212/* Write bytecode instructions. */
213static char *bcwrite_bytecode(BCWriteCtx *ctx, char *p, GCproto *pt)
214{
215 MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */
216#if LJ_HASJIT
217 uint8_t *q = (uint8_t *)p;
218#endif
219 p = lj_buf_wmem(p, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns));
220 UNUSED(ctx);
221#if LJ_HASJIT
222 /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
223 if ((pt->flags & PROTO_ILOOP) || pt->trace) {
224 jit_State *J = L2J(sbufL(&ctx->sb));
225 MSize i;
226 for (i = 0; i < nbc; i++, q += sizeof(BCIns)) {
227 BCOp op = (BCOp)q[LJ_ENDIAN_SELECT(0, 3)];
228 if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP ||
229 op == BC_JFORI) {
230 q[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL);
231 } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
232 BCReg rd = q[LJ_ENDIAN_SELECT(2, 1)] + (q[LJ_ENDIAN_SELECT(3, 0)] << 8);
233 memcpy(q, &traceref(J, rd)->startins, 4);
234 }
235 }
236 }
237#endif
238 return p;
239}
240
241/* Write prototype. */
242static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt)
243{
244 MSize sizedbg = 0;
245 char *p;
246
247 /* Recursively write children of prototype. */
248 if ((pt->flags & PROTO_CHILD)) {
249 ptrdiff_t i, n = pt->sizekgc;
250 GCRef *kr = mref(pt->k, GCRef) - 1;
251 for (i = 0; i < n; i++, kr--) {
252 GCobj *o = gcref(*kr);
253 if (o->gch.gct == ~LJ_TPROTO)
254 bcwrite_proto(ctx, gco2pt(o));
255 }
256 }
257
258 /* Start writing the prototype info to a buffer. */
259 p = lj_buf_need(&ctx->sb,
260 5+4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2);
261 p += 5; /* Leave room for final size. */
262
263 /* Write prototype header. */
264 *p++ = (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI));
265 *p++ = pt->numparams;
266 *p++ = pt->framesize;
267 *p++ = pt->sizeuv;
268 p = lj_strfmt_wuleb128(p, pt->sizekgc);
269 p = lj_strfmt_wuleb128(p, pt->sizekn);
270 p = lj_strfmt_wuleb128(p, pt->sizebc-1);
271 if (!ctx->strip) {
272 if (proto_lineinfo(pt))
273 sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt);
274 p = lj_strfmt_wuleb128(p, sizedbg);
275 if (sizedbg) {
276 p = lj_strfmt_wuleb128(p, pt->firstline);
277 p = lj_strfmt_wuleb128(p, pt->numline);
278 }
279 }
280
281 /* Write bytecode instructions and upvalue refs. */
282 p = bcwrite_bytecode(ctx, p, pt);
283 p = lj_buf_wmem(p, proto_uv(pt), pt->sizeuv*2);
284 setsbufP(&ctx->sb, p);
285
286 /* Write constants. */
287 bcwrite_kgc(ctx, pt);
288 bcwrite_knum(ctx, pt);
289
290 /* Write debug info, if not stripped. */
291 if (sizedbg) {
292 p = lj_buf_more(&ctx->sb, sizedbg);
293 p = lj_buf_wmem(p, proto_lineinfo(pt), sizedbg);
294 setsbufP(&ctx->sb, p);
295 }
296
297 /* Pass buffer to writer function. */
298 if (ctx->status == 0) {
299 MSize n = sbuflen(&ctx->sb) - 5;
300 MSize nn = (lj_fls(n)+8)*9 >> 6;
301 char *q = sbufB(&ctx->sb) + (5 - nn);
302 p = lj_strfmt_wuleb128(q, n); /* Fill in final size. */
303 lj_assertBCW(p == sbufB(&ctx->sb) + 5, "bad ULEB128 write");
304 ctx->status = ctx->wfunc(sbufL(&ctx->sb), q, nn+n, ctx->wdata);
305 }
306}
307
308/* Write header of bytecode dump. */
309static void bcwrite_header(BCWriteCtx *ctx)
310{
311 GCstr *chunkname = proto_chunkname(ctx->pt);
312 const char *name = strdata(chunkname);
313 MSize len = chunkname->len;
314 char *p = lj_buf_need(&ctx->sb, 5+5+len);
315 *p++ = BCDUMP_HEAD1;
316 *p++ = BCDUMP_HEAD2;
317 *p++ = BCDUMP_HEAD3;
318 *p++ = BCDUMP_VERSION;
319 *p++ = (ctx->strip ? BCDUMP_F_STRIP : 0) +
320 LJ_BE*BCDUMP_F_BE +
321 ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0) +
322 LJ_FR2*BCDUMP_F_FR2;
323 if (!ctx->strip) {
324 p = lj_strfmt_wuleb128(p, len);
325 p = lj_buf_wmem(p, name, len);
326 }
327 ctx->status = ctx->wfunc(sbufL(&ctx->sb), sbufB(&ctx->sb),
328 (MSize)(p - sbufB(&ctx->sb)), ctx->wdata);
329}
330
331/* Write footer of bytecode dump. */
332static void bcwrite_footer(BCWriteCtx *ctx)
333{
334 if (ctx->status == 0) {
335 uint8_t zero = 0;
336 ctx->status = ctx->wfunc(sbufL(&ctx->sb), &zero, 1, ctx->wdata);
337 }
338}
339
340/* Protected callback for bytecode writer. */
341static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud)
342{
343 BCWriteCtx *ctx = (BCWriteCtx *)ud;
344 UNUSED(L); UNUSED(dummy);
345 lj_buf_need(&ctx->sb, 1024); /* Avoids resize for most prototypes. */
346 bcwrite_header(ctx);
347 bcwrite_proto(ctx, ctx->pt);
348 bcwrite_footer(ctx);
349 return NULL;
350}
351
352/* Write bytecode for a prototype. */
353int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data,
354 int strip)
355{
356 BCWriteCtx ctx;
357 int status;
358 ctx.pt = pt;
359 ctx.wfunc = writer;
360 ctx.wdata = data;
361 ctx.strip = strip;
362 ctx.status = 0;
363#ifdef LUA_USE_ASSERT
364 ctx.g = G(L);
365#endif
366 lj_buf_init(L, &ctx.sb);
367 status = lj_vm_cpcall(L, NULL, &ctx, cpwriter);
368 if (status == 0) status = ctx.status;
369 lj_buf_free(G(sbufL(&ctx.sb)), &ctx.sb);
370 return status;
371}
372
373