3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
17 #include "lj_dispatch.h"
20 #include "lj_strfmt.h"
21 #include "lj_bcdump.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. */
38 #define lj_assertBCW(c, ...) lj_assertG_(ctx->g, (c), __VA_ARGS__)
40 #define lj_assertBCW(c, ...) ((void)ctx)
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);
50 const GCstr
*str
= strV(o
);
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
);
69 *p
++ = BCDUMP_KTAB_NUM
;
70 p
= lj_strfmt_wuleb128(p
, o
->u32
.lo
);
71 p
= lj_strfmt_wuleb128(p
, o
->u32
.hi
);
73 lj_assertBCW(tvispri(o
), "unhandled type %d", itype(o
));
74 *p
++ = BCDUMP_KTAB_NIL
+~itype(o
);
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. */
85 TValue
*array
= tvref(t
->array
);
86 for (i
= (ptrdiff_t)t
->asize
-1; i
>= 0; i
--)
87 if (!tvisnil(&array
[i
]))
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
);
101 if (narray
) { /* Write array entries (may contain nil). */
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. */
109 Node
*node
= noderef(t
->node
) + t
->hmask
;
111 if (!tvisnil(&node
->val
)) {
112 bcwrite_ktabk(ctx
, &node
->key
, 0);
113 bcwrite_ktabk(ctx
, &node
->val
, 1);
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
);
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
;
136 } else if (o
->gch
.gct
== ~LJ_TCDATA
) {
137 CTypeID id
= gco2cd(o
)->ctypeid
;
139 if (id
== CTID_INT64
) {
141 } else if (id
== CTID_UINT64
) {
144 lj_assertBCW(id
== CTID_COMPLEX_DOUBLE
,
145 "bad cdata constant CTID %d", id
);
146 tp
= BCDUMP_KGC_COMPLEX
;
150 lj_assertBCW(o
->gch
.gct
== ~LJ_TTAB
,
151 "bad constant GC type %d", o
->gch
.gct
);
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
));
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
);
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
++) {
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
);
196 if (num
== (lua_Number
)k
) { /* -0 is never a constant. */
198 p
= lj_strfmt_wuleb128(p
, 2*(uint32_t)k
| ((uint32_t)k
&0x80000000u
));
200 p
[-1] = (p
[-1] & 7) | ((k
>>27) & 0x18);
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
);
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. */
218 uint8_t *q
= (uint8_t *)p
;
220 p
= lj_buf_wmem(p
, proto_bc(pt
)+1, nbc
*(MSize
)sizeof(BCIns
));
223 /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
224 if ((pt
->flags
& PROTO_ILOOP
) || pt
->trace
) {
225 jit_State
*J
= L2J(sbufL(&ctx
->sb
));
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
||
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);
242 /* Write prototype. */
243 static void bcwrite_proto(BCWriteCtx
*ctx
, GCproto
*pt
)
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
;
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);
273 if (proto_lineinfo(pt
))
274 sizedbg
= pt
->sizept
- (MSize
)((char *)proto_lineinfo(pt
) - (char *)pt
);
275 p
= lj_strfmt_wuleb128(p
, 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);
287 /* Write constants. */
288 bcwrite_kgc(ctx
, pt
);
289 bcwrite_knum(ctx
, pt
);
291 /* Write debug info, if not stripped. */
293 p
= lj_buf_more(&ctx
->sb
, sizedbg
);
294 p
= lj_buf_wmem(p
, proto_lineinfo(pt
), sizedbg
);
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
);
319 *p
++ = BCDUMP_VERSION
;
320 *p
++ = (ctx
->strip
? BCDUMP_F_STRIP
: 0) +
322 ((ctx
->pt
->flags
& PROTO_FFI
) ? BCDUMP_F_FFI
: 0) +
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) {
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. */
348 bcwrite_proto(ctx
, ctx
->pt
);
353 /* Write bytecode for a prototype. */
354 int lj_bcwrite(lua_State
*L
, GCproto
*pt
, lua_Writer writer
, void *data
,
364 #ifdef LUA_USE_ASSERT
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
);