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 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. */
40 #define lj_assertBCW(c, ...) lj_assertG_(ctx->g, (c), __VA_ARGS__)
42 #define lj_assertBCW(c, ...) ((void)ctx)
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);
52 const GCstr
*str
= strV(o
);
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
);
71 *p
++ = BCDUMP_KTAB_NUM
;
72 p
= lj_strfmt_wuleb128(p
, o
->u32
.lo
);
73 p
= lj_strfmt_wuleb128(p
, o
->u32
.hi
);
75 lj_assertBCW(tvispri(o
), "unhandled type %d", itype(o
));
76 *p
++ = BCDUMP_KTAB_NIL
+~itype(o
);
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. */
87 } else if (at
== LJ_TSTR
) {
88 return lj_str_cmp(strV(a
), strV(b
)) < 0;
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
,
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
)) {
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
);
121 lj_mem_freevec(G(L
), ctx
->heap
, ctx
->heapsz
, TValue
*);
125 ctx
->heap
= lj_mem_newvec(L
, nsz
, TValue
*);
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
;
135 for (;; node
--) { /* Build heap. */
136 if (!tvisnil(&node
->key
)) {
137 bcwrite_ktabk_heap_insert(heap
, --i
, nhash
, &node
->key
);
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. */
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. */
156 TValue
*array
= tvref(t
->array
);
157 for (i
= (ptrdiff_t)t
->asize
-1; i
>= 0; i
--)
158 if (!tvisnil(&array
[i
]))
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
);
172 if (narray
) { /* Write array entries (may contain nil). */
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
);
187 if (!tvisnil(&node
->key
)) {
188 bcwrite_ktabk(ctx
, &node
->key
, 0);
189 bcwrite_ktabk(ctx
, &node
->val
, 1);
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
);
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
;
213 } else if (o
->gch
.gct
== ~LJ_TCDATA
) {
214 CTypeID id
= gco2cd(o
)->ctypeid
;
216 if (id
== CTID_INT64
) {
218 } else if (id
== CTID_UINT64
) {
221 lj_assertBCW(id
== CTID_COMPLEX_DOUBLE
,
222 "bad cdata constant CTID %d", id
);
223 tp
= BCDUMP_KGC_COMPLEX
;
227 lj_assertBCW(o
->gch
.gct
== ~LJ_TTAB
,
228 "bad constant GC type %d", o
->gch
.gct
);
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
));
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
);
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
++) {
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
);
273 if (num
== (lua_Number
)k
) { /* -0 is never a constant. */
275 p
= lj_strfmt_wuleb128(p
, 2*(uint32_t)k
| ((uint32_t)k
&0x80000000u
));
277 p
[-1] = (p
[-1] & 7) | ((k
>>27) & 0x18);
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
);
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. */
295 uint8_t *q
= (uint8_t *)p
;
297 p
= lj_buf_wmem(p
, proto_bc(pt
)+1, nbc
*(MSize
)sizeof(BCIns
));
300 /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */
301 if ((pt
->flags
& PROTO_ILOOP
) || pt
->trace
) {
302 jit_State
*J
= L2J(sbufL(&ctx
->sb
));
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
||
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);
319 /* Write prototype. */
320 static void bcwrite_proto(BCWriteCtx
*ctx
, GCproto
*pt
)
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
;
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
);
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);
364 /* Write constants. */
365 bcwrite_kgc(ctx
, pt
);
366 bcwrite_knum(ctx
, pt
);
368 /* Write debug info, if not stripped. */
370 p
= lj_buf_more(&ctx
->sb
, sizedbg
);
371 p
= lj_buf_wmem(p
, proto_lineinfo(pt
), sizedbg
);
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
);
396 *p
++ = BCDUMP_VERSION
;
397 *p
++ = (ctx
->flags
& (BCDUMP_F_STRIP
| BCDUMP_F_FR2
)) +
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) {
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. */
424 bcwrite_proto(ctx
, ctx
->pt
);
429 /* Write bytecode for a prototype. */
430 int lj_bcwrite(lua_State
*L
, GCproto
*pt
, lua_Writer writer
, void *data
,
439 if ((bc_op(proto_bc(pt
)[0]) != BC_NOT
) == LJ_FR2
) flags
|= BCDUMP_F_FR2
;
442 #ifdef LUA_USE_ASSERT
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);