2 ** Trace recorder for C data operations.
3 ** Copyright (C) 2005-2022 Mike Pall. See Copyright Notice in luajit.h
11 #if LJ_HASJIT && LJ_HASFFI
19 #include "lj_cparse.h"
26 #include "lj_ircall.h"
29 #include "lj_record.h"
30 #include "lj_ffrecord.h"
32 #include "lj_crecord.h"
33 #include "lj_dispatch.h"
35 /* Some local macros to save typing. Undef'd at the end. */
36 #define IR(ref) (&J->cur.ir[(ref)])
38 /* Pass IR on to next optimization in chain (FOLD). */
39 #define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
41 #define emitconv(a, dt, st, flags) \
42 emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags))
44 /* -- C type checks ------------------------------------------------------- */
46 static GCcdata
*argv2cdata(jit_State
*J
, TRef tr
, cTValue
*o
)
50 if (!tref_iscdata(tr
))
51 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
53 /* Specialize to the CTypeID. */
54 trtypeid
= emitir(IRT(IR_FLOAD
, IRT_U16
), tr
, IRFL_CDATA_CTYPEID
);
55 emitir(IRTG(IR_EQ
, IRT_INT
), trtypeid
, lj_ir_kint(J
, (int32_t)cd
->ctypeid
));
59 /* Specialize to the CTypeID held by a cdata constructor. */
60 static CTypeID
crec_constructor(jit_State
*J
, GCcdata
*cd
, TRef tr
)
63 lua_assert(tref_iscdata(tr
) && cd
->ctypeid
== CTID_CTYPEID
);
64 id
= *(CTypeID
*)cdataptr(cd
);
65 tr
= emitir(IRT(IR_FLOAD
, IRT_INT
), tr
, IRFL_CDATA_INT
);
66 emitir(IRTG(IR_EQ
, IRT_INT
), tr
, lj_ir_kint(J
, (int32_t)id
));
70 static CTypeID
argv2ctype(jit_State
*J
, TRef tr
, cTValue
*o
)
76 /* Specialize to the string containing the C type declaration. */
77 emitir(IRTG(IR_EQ
, IRT_STR
), tr
, lj_ir_kstr(J
, s
));
79 cp
.cts
= ctype_cts(J
->L
);
81 cp
.srcname
= strdata(s
);
84 cp
.mode
= CPARSE_MODE_ABSTRACT
|CPARSE_MODE_NOIMPLICIT
;
85 if (lj_cparse(&cp
) || cp
.cts
->top
> oldtop
) /* Avoid new struct defs. */
86 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
89 GCcdata
*cd
= argv2cdata(J
, tr
, o
);
90 return cd
->ctypeid
== CTID_CTYPEID
? crec_constructor(J
, cd
, tr
) :
95 /* Convert CType to IRType (if possible). */
96 static IRType
crec_ct2irt(CTState
*cts
, CType
*ct
)
98 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
99 if (LJ_LIKELY(ctype_isnum(ct
->info
))) {
100 if ((ct
->info
& CTF_FP
)) {
101 if (ct
->size
== sizeof(double))
103 else if (ct
->size
== sizeof(float))
106 uint32_t b
= lj_fls(ct
->size
);
108 return IRT_I8
+ 2*b
+ ((ct
->info
& CTF_UNSIGNED
) ? 1 : 0);
110 } else if (ctype_isptr(ct
->info
)) {
111 return (LJ_64
&& ct
->size
== 8) ? IRT_P64
: IRT_P32
;
112 } else if (ctype_iscomplex(ct
->info
)) {
113 if (ct
->size
== 2*sizeof(double))
115 else if (ct
->size
== 2*sizeof(float))
121 /* -- Optimized memory fill and copy -------------------------------------- */
123 /* Maximum length and unroll of inlined copy/fill. */
124 #define CREC_COPY_MAXUNROLL 16
125 #define CREC_COPY_MAXLEN 128
127 #define CREC_FILL_MAXUNROLL 16
129 /* Number of windowed registers used for optimized memory copy. */
131 #define CREC_COPY_REGWIN 2
132 #elif LJ_TARGET_PPC || LJ_TARGET_MIPS
133 #define CREC_COPY_REGWIN 8
135 #define CREC_COPY_REGWIN 4
138 /* List of memory offsets for copy/fill. */
139 typedef struct CRecMemList
{
140 CTSize ofs
; /* Offset in bytes. */
141 IRType tp
; /* Type of load/store. */
142 TRef trofs
; /* TRef of interned offset. */
143 TRef trval
; /* TRef of load value. */
146 /* Generate copy list for element-wise struct copy. */
147 static MSize
crec_copy_struct(CRecMemList
*ml
, CTState
*cts
, CType
*ct
)
149 CTypeID fid
= ct
->sib
;
152 CType
*df
= ctype_get(cts
, fid
);
154 if (ctype_isfield(df
->info
)) {
157 if (!gcref(df
->name
)) continue; /* Ignore unnamed fields. */
158 cct
= ctype_rawchild(cts
, df
); /* Field type. */
159 tp
= crec_ct2irt(cts
, cct
);
160 if (tp
== IRT_CDATA
) return 0; /* NYI: aggregates. */
161 if (mlp
>= CREC_COPY_MAXUNROLL
) return 0;
162 ml
[mlp
].ofs
= df
->size
;
165 if (ctype_iscomplex(cct
->info
)) {
166 if (mlp
>= CREC_COPY_MAXUNROLL
) return 0;
167 ml
[mlp
].ofs
= df
->size
+ (cct
->size
>> 1);
171 } else if (!ctype_isconstval(df
->info
)) {
172 /* NYI: bitfields and sub-structures. */
179 /* Generate unrolled copy list, from highest to lowest step size/alignment. */
180 static MSize
crec_copy_unroll(CRecMemList
*ml
, CTSize len
, CTSize step
,
185 if (tp
== IRT_CDATA
) tp
= IRT_U8
+ 2*lj_fls(step
);
187 while (ofs
+ step
<= len
) {
188 if (mlp
>= CREC_COPY_MAXUNROLL
) return 0;
201 ** Emit copy list with windowed loads/stores.
202 ** LJ_TARGET_UNALIGNED: may emit unaligned loads/stores (not marked as such).
204 static void crec_copy_emit(jit_State
*J
, CRecMemList
*ml
, MSize mlp
,
205 TRef trdst
, TRef trsrc
)
207 MSize i
, j
, rwin
= 0;
208 for (i
= 0, j
= 0; i
< mlp
; ) {
209 TRef trofs
= lj_ir_kintp(J
, ml
[i
].ofs
);
210 TRef trsptr
= emitir(IRT(IR_ADD
, IRT_PTR
), trsrc
, trofs
);
211 ml
[i
].trval
= emitir(IRT(IR_XLOAD
, ml
[i
].tp
), trsptr
, 0);
214 rwin
+= (LJ_SOFTFP
&& ml
[i
].tp
== IRT_NUM
) ? 2 : 1;
215 if (rwin
>= CREC_COPY_REGWIN
|| i
>= mlp
) { /* Flush buffered stores. */
217 for ( ; j
< i
; j
++) {
218 TRef trdptr
= emitir(IRT(IR_ADD
, IRT_PTR
), trdst
, ml
[j
].trofs
);
219 emitir(IRT(IR_XSTORE
, ml
[j
].tp
), trdptr
, ml
[j
].trval
);
225 /* Optimized memory copy. */
226 static void crec_copy(jit_State
*J
, TRef trdst
, TRef trsrc
, TRef trlen
,
229 if (tref_isk(trlen
)) { /* Length must be constant. */
230 CRecMemList ml
[CREC_COPY_MAXUNROLL
];
232 CTSize step
= 1, len
= (CTSize
)IR(tref_ref(trlen
))->i
;
233 IRType tp
= IRT_CDATA
;
235 if (len
== 0) return; /* Shortcut. */
236 if (len
> CREC_COPY_MAXLEN
) goto fallback
;
238 CTState
*cts
= ctype_ctsG(J2G(J
));
239 lua_assert(ctype_isarray(ct
->info
) || ctype_isstruct(ct
->info
));
240 if (ctype_isarray(ct
->info
)) {
241 CType
*cct
= ctype_rawchild(cts
, ct
);
242 tp
= crec_ct2irt(cts
, cct
);
243 if (tp
== IRT_CDATA
) goto rawcopy
;
244 step
= lj_ir_type_size
[tp
];
245 lua_assert((len
& (step
-1)) == 0);
246 } else if ((ct
->info
& CTF_UNION
)) {
247 step
= (1u << ctype_align(ct
->info
));
250 mlp
= crec_copy_struct(ml
, cts
, ct
);
256 if (LJ_TARGET_UNALIGNED
|| step
>= CTSIZE_PTR
)
259 mlp
= crec_copy_unroll(ml
, len
, step
, tp
);
262 crec_copy_emit(J
, ml
, mlp
, trdst
, trsrc
);
264 emitir(IRT(IR_XBAR
, IRT_NIL
), 0, 0);
269 /* Call memcpy. Always needs a barrier to disable alias analysis. */
270 lj_ir_call(J
, IRCALL_memcpy
, trdst
, trsrc
, trlen
);
271 emitir(IRT(IR_XBAR
, IRT_NIL
), 0, 0);
274 /* Generate unrolled fill list, from highest to lowest step size/alignment. */
275 static MSize
crec_fill_unroll(CRecMemList
*ml
, CTSize len
, CTSize step
)
279 IRType tp
= IRT_U8
+ 2*lj_fls(step
);
281 while (ofs
+ step
<= len
) {
282 if (mlp
>= CREC_COPY_MAXUNROLL
) return 0;
295 ** Emit stores for fill list.
296 ** LJ_TARGET_UNALIGNED: may emit unaligned stores (not marked as such).
298 static void crec_fill_emit(jit_State
*J
, CRecMemList
*ml
, MSize mlp
,
299 TRef trdst
, TRef trfill
)
302 for (i
= 0; i
< mlp
; i
++) {
303 TRef trofs
= lj_ir_kintp(J
, ml
[i
].ofs
);
304 TRef trdptr
= emitir(IRT(IR_ADD
, IRT_PTR
), trdst
, trofs
);
305 emitir(IRT(IR_XSTORE
, ml
[i
].tp
), trdptr
, trfill
);
309 /* Optimized memory fill. */
310 static void crec_fill(jit_State
*J
, TRef trdst
, TRef trlen
, TRef trfill
,
313 if (tref_isk(trlen
)) { /* Length must be constant. */
314 CRecMemList ml
[CREC_FILL_MAXUNROLL
];
316 CTSize len
= (CTSize
)IR(tref_ref(trlen
))->i
;
317 if (len
== 0) return; /* Shortcut. */
318 if (LJ_TARGET_UNALIGNED
|| step
>= CTSIZE_PTR
)
320 if (step
* CREC_FILL_MAXUNROLL
< len
) goto fallback
;
321 mlp
= crec_fill_unroll(ml
, len
, step
);
322 if (!mlp
) goto fallback
;
323 if (tref_isk(trfill
) || ml
[0].tp
!= IRT_U8
)
324 trfill
= emitconv(trfill
, IRT_INT
, IRT_U8
, 0);
325 if (ml
[0].tp
!= IRT_U8
) { /* Scatter U8 to U16/U32/U64. */
326 if (CTSIZE_PTR
== 8 && ml
[0].tp
== IRT_U64
) {
327 if (tref_isk(trfill
)) /* Pointless on x64 with zero-extended regs. */
328 trfill
= emitconv(trfill
, IRT_U64
, IRT_U32
, 0);
329 trfill
= emitir(IRT(IR_MUL
, IRT_U64
), trfill
,
330 lj_ir_kint64(J
, U64x(01010101,01010101)));
332 trfill
= emitir(IRTI(IR_MUL
), trfill
,
333 lj_ir_kint(J
, ml
[0].tp
== IRT_U16
? 0x0101 : 0x01010101));
336 crec_fill_emit(J
, ml
, mlp
, trdst
, trfill
);
339 /* Call memset. Always needs a barrier to disable alias analysis. */
340 lj_ir_call(J
, IRCALL_memset
, trdst
, trfill
, trlen
); /* Note: arg order! */
342 emitir(IRT(IR_XBAR
, IRT_NIL
), 0, 0);
345 /* -- Convert C type to C type -------------------------------------------- */
348 ** This code mirrors the code in lj_cconv.c. It performs the same steps
349 ** for the trace recorder that lj_cconv.c does for the interpreter.
351 ** One major difference is that we can get away with much fewer checks
352 ** here. E.g. checks for casts, constness or correct types can often be
353 ** omitted, even if they might fail. The interpreter subsequently throws
354 ** an error, which aborts the trace.
356 ** All operations are specialized to their C types, so the on-trace
357 ** outcome must be the same as the outcome in the interpreter. If the
358 ** interpreter doesn't throw an error, then the trace is correct, too.
359 ** Care must be taken not to generate invalid (temporary) IR or to
363 /* Determine whether a passed number or cdata number is non-zero. */
364 static int crec_isnonzero(CType
*s
, void *p
)
370 if ((s
->info
& CTF_FP
)) {
371 if (s
->size
== sizeof(float))
372 return (*(float *)p
!= 0);
374 return (*(double *)p
!= 0);
377 return (*(uint8_t *)p
!= 0);
378 else if (s
->size
== 2)
379 return (*(uint16_t *)p
!= 0);
380 else if (s
->size
== 4)
381 return (*(uint32_t *)p
!= 0);
383 return (*(uint64_t *)p
!= 0);
387 static TRef
crec_ct_ct(jit_State
*J
, CType
*d
, CType
*s
, TRef dp
, TRef sp
,
390 IRType dt
= crec_ct2irt(ctype_ctsG(J2G(J
)), d
);
391 IRType st
= crec_ct2irt(ctype_ctsG(J2G(J
)), s
);
392 CTSize dsize
= d
->size
, ssize
= s
->size
;
393 CTInfo dinfo
= d
->info
, sinfo
= s
->info
;
395 if (ctype_type(dinfo
) > CT_MAYCONVERT
|| ctype_type(sinfo
) > CT_MAYCONVERT
)
399 ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and
400 ** numbers up to 8 bytes. Otherwise sp holds a pointer.
403 switch (cconv_idx2(dinfo
, sinfo
)) {
404 /* Destination is a bool. */
406 goto xstore
; /* Source operand is already normalized. */
409 if (st
!= IRT_CDATA
) {
410 /* Specialize to the result of a comparison against 0. */
411 TRef zero
= (st
== IRT_NUM
|| st
== IRT_FLOAT
) ? lj_ir_knum(J
, 0) :
412 (st
== IRT_I64
|| st
== IRT_U64
) ? lj_ir_kint64(J
, 0) :
414 int isnz
= crec_isnonzero(s
, svisnz
);
415 emitir(IRTG(isnz
? IR_NE
: IR_EQ
, st
), sp
, zero
);
416 sp
= lj_ir_kint(J
, isnz
);
421 /* Destination is an integer. */
425 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
426 /* Extend 32 to 64 bit integer. */
427 if (dsize
== 8 && ssize
< 8 && !(LJ_64
&& (sinfo
& CTF_UNSIGNED
)))
428 sp
= emitconv(sp
, dt
, ssize
< 4 ? IRT_INT
: st
,
429 (sinfo
& CTF_UNSIGNED
) ? 0 : IRCONV_SEXT
);
430 else if (dsize
< 8 && ssize
== 8) /* Truncate from 64 bit integer. */
431 sp
= emitconv(sp
, dsize
< 4 ? IRT_INT
: dt
, st
, 0);
432 else if (st
== IRT_INT
)
433 sp
= lj_opt_narrow_toint(J
, sp
);
435 if (dt
== IRT_I64
|| dt
== IRT_U64
) lj_needsplit(J
);
436 if (dp
== 0) return sp
;
437 emitir(IRT(IR_XSTORE
, dt
), dp
, sp
);
440 sp
= emitir(IRT(IR_XLOAD
, st
), sp
, 0); /* Load re. */
443 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
444 sp
= emitconv(sp
, dsize
< 4 ? IRT_INT
: dt
, st
, IRCONV_TRUNC
|IRCONV_ANY
);
448 sinfo
= CTINFO(CT_NUM
, CTF_UNSIGNED
);
451 if (((dsize
^ ssize
) & 8) == 0) { /* Must insert no-op type conversion. */
452 sp
= emitconv(sp
, dsize
< 4 ? IRT_INT
: dt
, IRT_PTR
, 0);
457 /* Destination is a floating-point number. */
461 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
462 sp
= emitconv(sp
, dt
, ssize
< 4 ? IRT_INT
: st
, 0);
465 sp
= emitir(IRT(IR_XLOAD
, st
), sp
, 0); /* Load re. */
469 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
470 if (dt
!= st
) sp
= emitconv(sp
, dt
, st
, 0);
473 /* Destination is a complex number. */
477 TRef ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, (dsize
>> 1)));
478 emitir(IRT(IR_XSTORE
, dt
), ptr
, lj_ir_knum(J
, 0));
481 if ((sinfo
& CTF_FP
)) goto conv_F_F
; else goto conv_F_I
;
484 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
487 re
= emitir(IRT(IR_XLOAD
, st
), sp
, 0);
488 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, (ssize
>> 1)));
489 im
= emitir(IRT(IR_XLOAD
, st
), ptr
, 0);
491 re
= emitconv(re
, dt
, st
, 0);
492 im
= emitconv(im
, dt
, st
, 0);
494 emitir(IRT(IR_XSTORE
, dt
), dp
, re
);
495 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, (dsize
>> 1)));
496 emitir(IRT(IR_XSTORE
, dt
), ptr
, im
);
500 /* Destination is a vector. */
507 /* Destination is a pointer. */
511 /* There are only 32 bit pointers/addresses on 32 bit machines.
512 ** Also ok on x64, since all 32 bit ops clear the upper part of the reg.
516 if (st
== IRT_CDATA
) goto err_nyi
;
517 if (!LJ_64
&& ssize
== 8) /* Truncate from 64 bit integer. */
518 sp
= emitconv(sp
, IRT_U32
, st
, 0);
521 if (st
== IRT_CDATA
) goto err_nyi
;
522 /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
523 sp
= emitconv(sp
, (LJ_64
&& dsize
== 8) ? IRT_I64
: IRT_U32
,
524 st
, IRCONV_TRUNC
|IRCONV_ANY
);
527 /* Destination is an array. */
529 /* Destination is a struct/union. */
531 if (dp
== 0) goto err_conv
;
532 crec_copy(J
, dp
, sp
, lj_ir_kint(J
, dsize
), d
);
538 lj_trace_err(J
, LJ_TRERR_NYICONV
);
544 /* -- Convert C type to TValue (load) ------------------------------------- */
546 static TRef
crec_tv_ct(jit_State
*J
, CType
*s
, CTypeID sid
, TRef sp
)
548 CTState
*cts
= ctype_ctsG(J2G(J
));
549 IRType t
= crec_ct2irt(cts
, s
);
550 CTInfo sinfo
= s
->info
;
551 if (ctype_isnum(sinfo
)) {
554 goto err_nyi
; /* NYI: copyval of >64 bit integers. */
555 tr
= emitir(IRT(IR_XLOAD
, t
), sp
, 0);
556 if (t
== IRT_FLOAT
|| t
== IRT_U32
) { /* Keep uint32_t/float as numbers. */
557 return emitconv(tr
, IRT_NUM
, t
, 0);
558 } else if (t
== IRT_I64
|| t
== IRT_U64
) { /* Box 64 bit integer. */
561 } else if ((sinfo
& CTF_BOOL
)) {
562 /* Assume not equal to zero. Fixup and emit pending guard later. */
563 lj_ir_set(J
, IRTGI(IR_NE
), tr
, lj_ir_kint(J
, 0));
564 J
->postproc
= LJ_POST_FIXGUARD
;
569 } else if (ctype_isptr(sinfo
) || ctype_isenum(sinfo
)) {
570 sp
= emitir(IRT(IR_XLOAD
, t
), sp
, 0); /* Box pointers and enums. */
571 } else if (ctype_isrefarray(sinfo
) || ctype_isstruct(sinfo
)) {
573 sid
= lj_ctype_intern(cts
, CTINFO_REF(sid
), CTSIZE_PTR
); /* Create ref. */
574 } else if (ctype_iscomplex(sinfo
)) { /* Unbox/box complex. */
575 ptrdiff_t esz
= (ptrdiff_t)(s
->size
>> 1);
576 TRef ptr
, tr1
, tr2
, dp
;
577 dp
= emitir(IRTG(IR_CNEW
, IRT_CDATA
), lj_ir_kint(J
, sid
), TREF_NIL
);
578 tr1
= emitir(IRT(IR_XLOAD
, t
), sp
, 0);
579 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, esz
));
580 tr2
= emitir(IRT(IR_XLOAD
, t
), ptr
, 0);
581 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, sizeof(GCcdata
)));
582 emitir(IRT(IR_XSTORE
, t
), ptr
, tr1
);
583 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, sizeof(GCcdata
)+esz
));
584 emitir(IRT(IR_XSTORE
, t
), ptr
, tr2
);
587 /* NYI: copyval of vectors. */
589 lj_trace_err(J
, LJ_TRERR_NYICONV
);
591 /* Box pointer, ref, enum or 64 bit integer. */
592 return emitir(IRTG(IR_CNEWI
, IRT_CDATA
), lj_ir_kint(J
, sid
), sp
);
595 /* -- Convert TValue to C type (store) ------------------------------------ */
597 static TRef
crec_ct_tv(jit_State
*J
, CType
*d
, TRef dp
, TRef sp
, cTValue
*sval
)
599 CTState
*cts
= ctype_ctsG(J2G(J
));
600 CTypeID sid
= CTID_P_VOID
;
603 if (LJ_LIKELY(tref_isinteger(sp
))) {
605 svisnz
= (void *)(intptr_t)(tvisint(sval
)?(intV(sval
)!=0):!tviszero(sval
));
606 } else if (tref_isnum(sp
)) {
608 svisnz
= (void *)(intptr_t)(tvisint(sval
)?(intV(sval
)!=0):!tviszero(sval
));
609 } else if (tref_isbool(sp
)) {
610 sp
= lj_ir_kint(J
, tref_istrue(sp
) ? 1 : 0);
612 } else if (tref_isnil(sp
)) {
613 sp
= lj_ir_kptr(J
, NULL
);
614 } else if (tref_isudata(sp
)) {
615 GCudata
*ud
= udataV(sval
);
616 if (ud
->udtype
== UDTYPE_IO_FILE
) {
617 TRef tr
= emitir(IRT(IR_FLOAD
, IRT_U8
), sp
, IRFL_UDATA_UDTYPE
);
618 emitir(IRTGI(IR_EQ
), tr
, lj_ir_kint(J
, UDTYPE_IO_FILE
));
619 sp
= emitir(IRT(IR_FLOAD
, IRT_PTR
), sp
, IRFL_UDATA_FILE
);
621 sp
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, sizeof(GCudata
)));
623 } else if (tref_isstr(sp
)) {
624 if (ctype_isenum(d
->info
)) { /* Match string against enum constant. */
625 GCstr
*str
= strV(sval
);
627 CType
*cct
= lj_ctype_getfield(cts
, d
, str
, &ofs
);
628 /* Specialize to the name of the enum constant. */
629 emitir(IRTG(IR_EQ
, IRT_STR
), sp
, lj_ir_kstr(J
, str
));
630 if (cct
&& ctype_isconstval(cct
->info
)) {
631 lua_assert(ctype_child(cts
, cct
)->size
== 4);
632 svisnz
= (void *)(intptr_t)(ofs
!= 0);
633 sp
= lj_ir_kint(J
, (int32_t)ofs
);
634 sid
= ctype_cid(cct
->info
);
635 } /* else: interpreter will throw. */
636 } else if (ctype_isrefarray(d
->info
)) { /* Copy string to array. */
637 lj_trace_err(J
, LJ_TRERR_BADTYPE
); /* NYI */
638 } else { /* Otherwise pass the string data as a const char[]. */
639 /* Don't use STRREF. It folds with SNEW, which loses the trailing NUL. */
640 sp
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, sizeof(GCstr
)));
643 } else { /* NYI: tref_istab(sp), tref_islightud(sp). */
645 sid
= argv2cdata(J
, sp
, sval
)->ctypeid
;
646 s
= ctype_raw(cts
, sid
);
647 svisnz
= cdataptr(cdataV(sval
));
648 t
= crec_ct2irt(cts
, s
);
649 if (ctype_isptr(s
->info
)) {
650 sp
= emitir(IRT(IR_FLOAD
, t
), sp
, IRFL_CDATA_PTR
);
651 if (ctype_isref(s
->info
)) {
652 svisnz
= *(void **)svisnz
;
653 s
= ctype_rawchild(cts
, s
);
654 if (ctype_isenum(s
->info
)) s
= ctype_child(cts
, s
);
655 t
= crec_ct2irt(cts
, s
);
659 } else if (t
== IRT_I64
|| t
== IRT_U64
) {
660 sp
= emitir(IRT(IR_FLOAD
, t
), sp
, IRFL_CDATA_INT64
);
663 } else if (t
== IRT_INT
|| t
== IRT_U32
) {
664 if (ctype_isenum(s
->info
)) s
= ctype_child(cts
, s
);
665 sp
= emitir(IRT(IR_FLOAD
, t
), sp
, IRFL_CDATA_INT
);
668 sp
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, sizeof(GCcdata
)));
670 if (ctype_isnum(s
->info
) && t
!= IRT_CDATA
)
671 sp
= emitir(IRT(IR_XLOAD
, t
), sp
, 0); /* Load number value. */
674 s
= ctype_get(cts
, sid
);
676 if (ctype_isenum(d
->info
)) d
= ctype_child(cts
, d
);
677 return crec_ct_ct(J
, d
, s
, dp
, sp
, svisnz
);
680 /* -- C data metamethods -------------------------------------------------- */
682 /* This would be rather difficult in FOLD, so do it here:
683 ** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k)
684 ** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
686 static TRef
crec_reassoc_ofs(jit_State
*J
, TRef tr
, ptrdiff_t *ofsp
, MSize sz
)
688 IRIns
*ir
= IR(tref_ref(tr
));
689 if (LJ_LIKELY(J
->flags
& JIT_F_OPT_FOLD
) && irref_isk(ir
->op2
) &&
690 (ir
->o
== IR_ADD
|| ir
->o
== IR_ADDOV
|| ir
->o
== IR_SUBOV
)) {
691 IRIns
*irk
= IR(ir
->op2
);
693 if (LJ_64
&& irk
->o
== IR_KINT64
)
694 k
= (ptrdiff_t)ir_kint64(irk
)->u64
* sz
;
696 k
= (ptrdiff_t)irk
->i
* sz
;
697 if (ir
->o
== IR_SUBOV
) *ofsp
-= k
; else *ofsp
+= k
;
698 tr
= ir
->op1
; /* Not a TRef, but the caller doesn't care. */
703 /* Record ctype __index/__newindex metamethods. */
704 static void crec_index_meta(jit_State
*J
, CTState
*cts
, CType
*ct
,
707 CTypeID id
= ctype_typeid(cts
, ct
);
708 cTValue
*tv
= lj_ctype_meta(cts
, id
, rd
->data
? MM_newindex
: MM_index
);
710 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
712 J
->base
[-1] = lj_ir_kfunc(J
, funcV(tv
)) | TREF_FRAME
;
713 rd
->nres
= -1; /* Pending tailcall. */
714 } else if (rd
->data
== 0 && tvistab(tv
) && tref_isstr(J
->base
[1])) {
715 /* Specialize to result of __index lookup. */
716 cTValue
*o
= lj_tab_get(J
->L
, tabV(tv
), &rd
->argv
[1]);
717 J
->base
[0] = lj_record_constify(J
, o
);
719 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
720 /* Always specialize to the key. */
721 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[1], lj_ir_kstr(J
, strV(&rd
->argv
[1])));
723 /* NYI: resolving of non-function metamethods. */
724 /* NYI: non-string keys for __index table. */
725 /* NYI: stores to __newindex table. */
726 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
730 void LJ_FASTCALL
recff_cdata_index(jit_State
*J
, RecordFFData
*rd
)
732 TRef idx
, ptr
= J
->base
[0];
733 ptrdiff_t ofs
= sizeof(GCcdata
);
734 GCcdata
*cd
= argv2cdata(J
, ptr
, &rd
->argv
[0]);
735 CTState
*cts
= ctype_ctsG(J2G(J
));
736 CType
*ct
= ctype_raw(cts
, cd
->ctypeid
);
739 /* Resolve pointer or reference for cdata object. */
740 if (ctype_isptr(ct
->info
)) {
741 IRType t
= (LJ_64
&& ct
->size
== 8) ? IRT_P64
: IRT_P32
;
742 if (ctype_isref(ct
->info
)) ct
= ctype_rawchild(cts
, ct
);
743 ptr
= emitir(IRT(IR_FLOAD
, t
), ptr
, IRFL_CDATA_PTR
);
745 ptr
= crec_reassoc_ofs(J
, ptr
, &ofs
, 1);
750 if (tref_isnumber(idx
)) {
751 idx
= lj_opt_narrow_cindex(J
, idx
);
752 if (ctype_ispointer(ct
->info
)) {
755 if ((ct
->info
& CTF_COMPLEX
))
756 idx
= emitir(IRT(IR_BAND
, IRT_INTP
), idx
, lj_ir_kintp(J
, 1));
757 sz
= lj_ctype_size(cts
, (sid
= ctype_cid(ct
->info
)));
758 idx
= crec_reassoc_ofs(J
, idx
, &ofs
, sz
);
759 #if LJ_TARGET_ARM || LJ_TARGET_PPC
760 /* Hoist base add to allow fusion of index/shift into operands. */
761 if (LJ_LIKELY(J
->flags
& JIT_F_OPT_LOOP
) && ofs
763 && (sz
== 1 || sz
== 4)
766 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), ptr
, lj_ir_kintp(J
, ofs
));
770 idx
= emitir(IRT(IR_MUL
, IRT_INTP
), idx
, lj_ir_kintp(J
, sz
));
771 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), idx
, ptr
);
773 } else if (tref_iscdata(idx
)) {
774 GCcdata
*cdk
= cdataV(&rd
->argv
[1]);
775 CType
*ctk
= ctype_raw(cts
, cdk
->ctypeid
);
776 IRType t
= crec_ct2irt(cts
, ctk
);
777 if (ctype_ispointer(ct
->info
) && t
>= IRT_I8
&& t
<= IRT_U64
) {
778 if (ctk
->size
== 8) {
779 idx
= emitir(IRT(IR_FLOAD
, t
), idx
, IRFL_CDATA_INT64
);
780 } else if (ctk
->size
== 4) {
781 idx
= emitir(IRT(IR_FLOAD
, t
), idx
, IRFL_CDATA_INT
);
783 idx
= emitir(IRT(IR_ADD
, IRT_PTR
), idx
,
784 lj_ir_kintp(J
, sizeof(GCcdata
)));
785 idx
= emitir(IRT(IR_XLOAD
, t
), idx
, 0);
787 if (LJ_64
&& ctk
->size
< sizeof(intptr_t) && !(ctk
->info
& CTF_UNSIGNED
))
788 idx
= emitconv(idx
, IRT_INTP
, IRT_INT
, IRCONV_SEXT
);
789 if (!LJ_64
&& ctk
->size
> sizeof(intptr_t)) {
790 idx
= emitconv(idx
, IRT_INTP
, t
, 0);
795 } else if (tref_isstr(idx
)) {
796 GCstr
*name
= strV(&rd
->argv
[1]);
797 if (cd
&& cd
->ctypeid
== CTID_CTYPEID
)
798 ct
= ctype_raw(cts
, crec_constructor(J
, cd
, ptr
));
799 if (ctype_isstruct(ct
->info
)) {
802 fct
= lj_ctype_getfield(cts
, ct
, name
, &fofs
);
804 /* Always specialize to the field name. */
805 emitir(IRTG(IR_EQ
, IRT_STR
), idx
, lj_ir_kstr(J
, name
));
806 if (ctype_isconstval(fct
->info
)) {
807 if (fct
->size
>= 0x80000000u
&&
808 (ctype_child(cts
, fct
)->info
& CTF_UNSIGNED
)) {
809 J
->base
[0] = lj_ir_knum(J
, (lua_Number
)(uint32_t)fct
->size
);
812 J
->base
[0] = lj_ir_kint(J
, (int32_t)fct
->size
);
813 return; /* Interpreter will throw for newindex. */
814 } else if (ctype_isbitfield(fct
->info
)) {
815 lj_trace_err(J
, LJ_TRERR_NYICONV
);
817 lua_assert(ctype_isfield(fct
->info
));
818 sid
= ctype_cid(fct
->info
);
820 ofs
+= (ptrdiff_t)fofs
;
822 } else if (ctype_iscomplex(ct
->info
)) {
823 if (name
->len
== 2 &&
824 ((strdata(name
)[0] == 'r' && strdata(name
)[1] == 'e') ||
825 (strdata(name
)[0] == 'i' && strdata(name
)[1] == 'm'))) {
826 /* Always specialize to the field name. */
827 emitir(IRTG(IR_EQ
, IRT_STR
), idx
, lj_ir_kstr(J
, name
));
828 if (strdata(name
)[0] == 'i') ofs
+= (ct
->size
>> 1);
829 sid
= ctype_cid(ct
->info
);
834 if (ctype_isptr(ct
->info
)) { /* Automatically perform '->'. */
835 CType
*cct
= ctype_rawchild(cts
, ct
);
836 if (ctype_isstruct(cct
->info
)) {
839 if (tref_isstr(idx
)) goto again
;
842 crec_index_meta(J
, cts
, ct
, rd
);
847 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), ptr
, lj_ir_kintp(J
, ofs
));
849 /* Resolve reference for field. */
850 ct
= ctype_get(cts
, sid
);
851 if (ctype_isref(ct
->info
)) {
852 ptr
= emitir(IRT(IR_XLOAD
, IRT_PTR
), ptr
, 0);
853 sid
= ctype_cid(ct
->info
);
854 ct
= ctype_get(cts
, sid
);
857 while (ctype_isattrib(ct
->info
))
858 ct
= ctype_child(cts
, ct
); /* Skip attributes. */
860 if (rd
->data
== 0) { /* __index metamethod. */
861 J
->base
[0] = crec_tv_ct(J
, ct
, sid
, ptr
);
862 } else { /* __newindex metamethod. */
865 crec_ct_tv(J
, ct
, ptr
, J
->base
[2], &rd
->argv
[2]);
869 /* Record setting a finalizer. */
870 static void crec_finalizer(jit_State
*J
, TRef trcd
, cTValue
*fin
)
872 TRef trlo
= lj_ir_call(J
, IRCALL_lj_cdata_setfin
, trcd
);
873 TRef trhi
= emitir(IRT(IR_ADD
, IRT_P32
), trlo
, lj_ir_kint(J
, 4));
874 if (LJ_BE
) { TRef tmp
= trlo
; trlo
= trhi
; trhi
= tmp
; }
876 emitir(IRT(IR_XSTORE
, IRT_P32
), trlo
, lj_ir_kfunc(J
, funcV(fin
)));
877 emitir(IRTI(IR_XSTORE
), trhi
, lj_ir_kint(J
, LJ_TFUNC
));
878 } else if (tviscdata(fin
)) {
879 emitir(IRT(IR_XSTORE
, IRT_P32
), trlo
,
880 lj_ir_kgc(J
, obj2gco(cdataV(fin
)), IRT_CDATA
));
881 emitir(IRTI(IR_XSTORE
), trhi
, lj_ir_kint(J
, LJ_TCDATA
));
883 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
888 /* Record cdata allocation. */
889 static void crec_alloc(jit_State
*J
, RecordFFData
*rd
, CTypeID id
)
891 CTState
*cts
= ctype_ctsG(J2G(J
));
893 CTInfo info
= lj_ctype_info(cts
, id
, &sz
);
894 CType
*d
= ctype_raw(cts
, id
);
896 if (!sz
|| sz
> 128 || (info
& CTF_VLA
) || ctype_align(info
) > CT_MEMALIGN
)
897 lj_trace_err(J
, LJ_TRERR_NYICONV
); /* NYI: large/special allocations. */
898 trid
= lj_ir_kint(J
, id
);
899 /* Use special instruction to box pointer or 32/64 bit integer. */
900 if (ctype_isptr(info
) || (ctype_isinteger(info
) && (sz
== 4 || sz
== 8))) {
901 TRef sp
= J
->base
[1] ? crec_ct_tv(J
, d
, 0, J
->base
[1], &rd
->argv
[1]) :
902 ctype_isptr(info
) ? lj_ir_kptr(J
, NULL
) :
903 sz
== 4 ? lj_ir_kint(J
, 0) :
904 (lj_needsplit(J
), lj_ir_kint64(J
, 0));
905 J
->base
[0] = emitir(IRTG(IR_CNEWI
, IRT_CDATA
), trid
, sp
);
907 TRef trcd
= emitir(IRTG(IR_CNEW
, IRT_CDATA
), trid
, TREF_NIL
);
910 if (J
->base
[1] && !J
->base
[2] &&
911 !lj_cconv_multi_init(cts
, d
, &rd
->argv
[1])) {
913 } else if (ctype_isarray(d
->info
)) {
914 CType
*dc
= ctype_rawchild(cts
, d
); /* Array element type. */
915 CTSize ofs
, esize
= dc
->size
;
921 if (!(ctype_isnum(dc
->info
) || ctype_isptr(dc
->info
)))
922 lj_trace_err(J
, LJ_TRERR_NYICONV
); /* NYI: init array of aggregates. */
923 for (i
= 1, ofs
= 0; ofs
< sz
; ofs
+= esize
) {
924 TRef dp
= emitir(IRT(IR_ADD
, IRT_PTR
), trcd
,
925 lj_ir_kintp(J
, ofs
+ sizeof(GCcdata
)));
931 sp
= ctype_isnum(dc
->info
) ? lj_ir_kint(J
, 0) : TREF_NIL
;
933 crec_ct_tv(J
, dc
, dp
, sp
, sval
);
935 } else if (ctype_isstruct(d
->info
)) {
936 CTypeID fid
= d
->sib
;
939 CType
*df
= ctype_get(cts
, fid
);
941 if (ctype_isfield(df
->info
)) {
947 if (!gcref(df
->name
)) continue; /* Ignore unnamed fields. */
948 dc
= ctype_rawchild(cts
, df
); /* Field type. */
949 if (!(ctype_isnum(dc
->info
) || ctype_isptr(dc
->info
) ||
950 ctype_isenum(dc
->info
)))
951 lj_trace_err(J
, LJ_TRERR_NYICONV
); /* NYI: init aggregates. */
957 sp
= ctype_isptr(dc
->info
) ? TREF_NIL
: lj_ir_kint(J
, 0);
959 dp
= emitir(IRT(IR_ADD
, IRT_PTR
), trcd
,
960 lj_ir_kintp(J
, df
->size
+ sizeof(GCcdata
)));
961 crec_ct_tv(J
, dc
, dp
, sp
, sval
);
962 if ((d
->info
& CTF_UNION
)) {
963 if (d
->size
!= dc
->size
) /* NYI: partial init of union. */
964 lj_trace_err(J
, LJ_TRERR_NYICONV
);
967 } else if (!ctype_isconstval(df
->info
)) {
968 /* NYI: init bitfields and sub-structures. */
969 lj_trace_err(J
, LJ_TRERR_NYICONV
);
975 dp
= emitir(IRT(IR_ADD
, IRT_PTR
), trcd
, lj_ir_kintp(J
, sizeof(GCcdata
)));
977 crec_ct_tv(J
, d
, dp
, J
->base
[1], &rd
->argv
[1]);
981 crec_ct_tv(J
, d
, dp
, lj_ir_kint(J
, 0), &tv
);
984 /* Handle __gc metamethod. */
985 fin
= lj_ctype_meta(cts
, id
, MM_gc
);
987 crec_finalizer(J
, trcd
, fin
);
991 /* Record argument conversions. */
992 static TRef
crec_call_args(jit_State
*J
, RecordFFData
*rd
,
993 CTState
*cts
, CType
*ct
)
995 TRef args
[CCI_NARGS_MAX
];
1002 TRef
*arg0
= NULL
, *arg1
= NULL
;
1005 if (ctype_cconv(ct
->info
) == CTCC_THISCALL
)
1007 else if (ctype_cconv(ct
->info
) == CTCC_FASTCALL
)
1011 /* Skip initial attributes. */
1014 CType
*ctf
= ctype_get(cts
, fid
);
1015 if (!ctype_isattrib(ctf
->info
)) break;
1019 for (n
= 0, base
= J
->base
+1, o
= rd
->argv
+1; *base
; n
++, base
++, o
++) {
1023 if (n
>= CCI_NARGS_MAX
)
1024 lj_trace_err(J
, LJ_TRERR_NYICALL
);
1026 if (fid
) { /* Get argument type from field. */
1027 CType
*ctf
= ctype_get(cts
, fid
);
1029 lua_assert(ctype_isfield(ctf
->info
));
1030 did
= ctype_cid(ctf
->info
);
1032 if (!(ct
->info
& CTF_VARARG
))
1033 lj_trace_err(J
, LJ_TRERR_NYICALL
); /* Too many arguments. */
1034 did
= lj_ccall_ctid_vararg(cts
, o
); /* Infer vararg type. */
1036 d
= ctype_raw(cts
, did
);
1037 if (!(ctype_isnum(d
->info
) || ctype_isptr(d
->info
) ||
1038 ctype_isenum(d
->info
)))
1039 lj_trace_err(J
, LJ_TRERR_NYICALL
);
1040 tr
= crec_ct_tv(J
, d
, 0, *base
, o
);
1041 if (ctype_isinteger_or_bool(d
->info
)) {
1043 if ((d
->info
& CTF_UNSIGNED
))
1044 tr
= emitconv(tr
, IRT_INT
, d
->size
==1 ? IRT_U8
: IRT_U16
, 0);
1046 tr
= emitconv(tr
, IRT_INT
, d
->size
==1 ? IRT_I8
: IRT_I16
,IRCONV_SEXT
);
1048 } else if (LJ_SOFTFP
&& ctype_isfp(d
->info
) && d
->size
> 4) {
1052 /* 64 bit args must not end up in registers for fastcall/thiscall. */
1054 if (!ctype_isfp(d
->info
)) {
1055 /* Sigh, the Windows/x86 ABI allows reordering across 64 bit args. */
1056 if (tref_typerange(tr
, IRT_I64
, IRT_U64
)) {
1058 arg0
= &args
[n
]; args
[n
++] = TREF_NIL
; ngpr
--;
1060 arg1
= &args
[n
]; args
[n
++] = TREF_NIL
; ngpr
--;
1064 if (arg0
) { *arg0
= tr
; arg0
= NULL
; n
--; continue; }
1065 if (arg1
) { *arg1
= tr
; arg1
= NULL
; n
--; continue; }
1070 if (!ctype_isfp(d
->info
) && ngpr
) {
1071 if (tref_typerange(tr
, IRT_I64
, IRT_U64
)) {
1072 /* No reordering for other x86 ABIs. Simply add alignment args. */
1073 do { args
[n
++] = TREF_NIL
; } while (--ngpr
);
1083 for (i
= 1; i
< n
; i
++)
1084 tr
= emitir(IRT(IR_CARG
, IRT_NIL
), tr
, args
[i
]);
1088 /* Create a snapshot for the caller, simulating a 'false' return value. */
1089 static void crec_snap_caller(jit_State
*J
)
1091 lua_State
*L
= J
->L
;
1092 TValue
*base
= L
->base
, *top
= L
->top
;
1093 const BCIns
*pc
= J
->pc
;
1094 TRef ftr
= J
->base
[-1];
1096 if (!frame_islua(base
-1) || J
->framedepth
<= 0)
1097 lj_trace_err(J
, LJ_TRERR_NYICALL
);
1098 J
->pc
= frame_pc(base
-1); delta
= 1+bc_a(J
->pc
[-1]);
1099 L
->top
= base
; L
->base
= base
- delta
;
1100 J
->base
[-1] = TREF_FALSE
;
1101 J
->base
-= delta
; J
->baseslot
-= (BCReg
)delta
;
1102 J
->maxslot
= (BCReg
)delta
; J
->framedepth
--;
1104 L
->base
= base
; L
->top
= top
;
1105 J
->framedepth
++; J
->maxslot
= 1;
1106 J
->base
+= delta
; J
->baseslot
+= (BCReg
)delta
;
1107 J
->base
[-1] = ftr
; J
->pc
= pc
;
1110 /* Record function call. */
1111 static int crec_call(jit_State
*J
, RecordFFData
*rd
, GCcdata
*cd
)
1113 CTState
*cts
= ctype_ctsG(J2G(J
));
1114 CType
*ct
= ctype_raw(cts
, cd
->ctypeid
);
1115 IRType tp
= IRT_PTR
;
1116 if (ctype_isptr(ct
->info
)) {
1117 tp
= (LJ_64
&& ct
->size
== 8) ? IRT_P64
: IRT_P32
;
1118 ct
= ctype_rawchild(cts
, ct
);
1120 if (ctype_isfunc(ct
->info
)) {
1121 TRef func
= emitir(IRT(IR_FLOAD
, tp
), J
->base
[0], IRFL_CDATA_PTR
);
1122 CType
*ctr
= ctype_rawchild(cts
, ct
);
1123 IRType t
= crec_ct2irt(cts
, ctr
);
1126 /* Check for blacklisted C functions that might call a callback. */
1128 cdata_getptr(cdataptr(cd
), (LJ_64
&& tp
== IRT_P64
) ? 8 : 4));
1129 if (tvistrue(lj_tab_get(J
->L
, cts
->miscmap
, &tv
)))
1130 lj_trace_err(J
, LJ_TRERR_BLACKL
);
1131 if (ctype_isvoid(ctr
->info
)) {
1134 } else if (!(ctype_isnum(ctr
->info
) || ctype_isptr(ctr
->info
) ||
1135 ctype_isenum(ctr
->info
)) || t
== IRT_CDATA
) {
1136 lj_trace_err(J
, LJ_TRERR_NYICALL
);
1138 if ((ct
->info
& CTF_VARARG
)
1140 || ctype_cconv(ct
->info
) != CTCC_CDECL
1143 func
= emitir(IRT(IR_CARG
, IRT_NIL
), func
,
1144 lj_ir_kint(J
, ctype_typeid(cts
, ct
)));
1145 tr
= emitir(IRT(IR_CALLXS
, t
), crec_call_args(J
, rd
, cts
, ct
), func
);
1146 if (ctype_isbool(ctr
->info
)) {
1147 if (frame_islua(J
->L
->base
-1) && bc_b(frame_pc(J
->L
->base
-1)[-1]) == 1) {
1148 /* Don't check result if ignored. */
1151 crec_snap_caller(J
);
1152 #if LJ_TARGET_X86ORX64
1153 /* Note: only the x86/x64 backend supports U8 and only for EQ(tr, 0). */
1154 lj_ir_set(J
, IRTG(IR_NE
, IRT_U8
), tr
, lj_ir_kint(J
, 0));
1156 lj_ir_set(J
, IRTGI(IR_NE
), tr
, lj_ir_kint(J
, 0));
1158 J
->postproc
= LJ_POST_FIXGUARDSNAP
;
1161 } else if (t
== IRT_PTR
|| (LJ_64
&& t
== IRT_P32
) ||
1162 t
== IRT_I64
|| t
== IRT_U64
|| ctype_isenum(ctr
->info
)) {
1163 TRef trid
= lj_ir_kint(J
, ctype_cid(ct
->info
));
1164 tr
= emitir(IRTG(IR_CNEWI
, IRT_CDATA
), trid
, tr
);
1165 if (t
== IRT_I64
|| t
== IRT_U64
) lj_needsplit(J
);
1166 } else if (t
== IRT_FLOAT
|| t
== IRT_U32
) {
1167 tr
= emitconv(tr
, IRT_NUM
, t
, 0);
1168 } else if (t
== IRT_I8
|| t
== IRT_I16
) {
1169 tr
= emitconv(tr
, IRT_INT
, t
, IRCONV_SEXT
);
1170 } else if (t
== IRT_U8
|| t
== IRT_U16
) {
1171 tr
= emitconv(tr
, IRT_INT
, t
, 0);
1180 void LJ_FASTCALL
recff_cdata_call(jit_State
*J
, RecordFFData
*rd
)
1182 CTState
*cts
= ctype_ctsG(J2G(J
));
1183 GCcdata
*cd
= argv2cdata(J
, J
->base
[0], &rd
->argv
[0]);
1184 CTypeID id
= cd
->ctypeid
;
1188 if (id
== CTID_CTYPEID
) {
1189 id
= crec_constructor(J
, cd
, J
->base
[0]);
1191 } else if (crec_call(J
, rd
, cd
)) {
1194 /* Record ctype __call/__new metamethod. */
1195 ct
= ctype_raw(cts
, id
);
1196 tv
= lj_ctype_meta(cts
, ctype_isptr(ct
->info
) ? ctype_cid(ct
->info
) : id
, mm
);
1199 J
->base
[-1] = lj_ir_kfunc(J
, funcV(tv
)) | TREF_FRAME
;
1200 rd
->nres
= -1; /* Pending tailcall. */
1203 } else if (mm
== MM_new
) {
1204 crec_alloc(J
, rd
, id
);
1207 /* No metamethod or NYI: non-function metamethods. */
1208 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
1211 static TRef
crec_arith_int64(jit_State
*J
, TRef
*sp
, CType
**s
, MMS mm
)
1213 if (sp
[0] && sp
[1] && ctype_isnum(s
[0]->info
) && ctype_isnum(s
[1]->info
)) {
1220 if (((s
[0]->info
& CTF_UNSIGNED
) && s
[0]->size
== 8) ||
1221 ((s
[1]->info
& CTF_UNSIGNED
) && s
[1]->size
== 8)) {
1222 dt
= IRT_U64
; id
= CTID_UINT64
;
1224 dt
= IRT_I64
; id
= CTID_INT64
;
1226 !((s
[0]->info
| s
[1]->info
) & CTF_FP
) &&
1227 s
[0]->size
== 4 && s
[1]->size
== 4) { /* Try to narrow comparison. */
1228 if (!((s
[0]->info
^ s
[1]->info
) & CTF_UNSIGNED
) ||
1229 (tref_isk(sp
[1]) && IR(tref_ref(sp
[1]))->i
>= 0)) {
1230 dt
= (s
[0]->info
& CTF_UNSIGNED
) ? IRT_U32
: IRT_INT
;
1232 } else if (tref_isk(sp
[0]) && IR(tref_ref(sp
[0]))->i
>= 0) {
1233 dt
= (s
[1]->info
& CTF_UNSIGNED
) ? IRT_U32
: IRT_INT
;
1238 for (i
= 0; i
< 2; i
++) {
1239 IRType st
= tref_type(sp
[i
]);
1240 if (st
== IRT_NUM
|| st
== IRT_FLOAT
)
1241 sp
[i
] = emitconv(sp
[i
], dt
, st
, IRCONV_TRUNC
|IRCONV_ANY
);
1242 else if (!(st
== IRT_I64
|| st
== IRT_U64
))
1243 sp
[i
] = emitconv(sp
[i
], dt
, IRT_INT
,
1244 (s
[i
]->info
& CTF_UNSIGNED
) ? 0 : IRCONV_SEXT
);
1248 /* Assume true comparison. Fixup and emit pending guard later. */
1252 op
= mm
== MM_lt
? IR_LT
: IR_LE
;
1253 if (dt
== IRT_U32
|| dt
== IRT_U64
)
1254 op
+= (IR_ULT
-IR_LT
);
1256 lj_ir_set(J
, IRTG(op
, dt
), sp
[0], sp
[1]);
1257 J
->postproc
= LJ_POST_FIXGUARD
;
1260 tr
= emitir(IRT(mm
+(int)IR_ADD
-(int)MM_add
, dt
), sp
[0], sp
[1]);
1262 return emitir(IRTG(IR_CNEWI
, IRT_CDATA
), lj_ir_kint(J
, id
), tr
);
1267 static TRef
crec_arith_ptr(jit_State
*J
, TRef
*sp
, CType
**s
, MMS mm
)
1269 CTState
*cts
= ctype_ctsG(J2G(J
));
1271 if (!(sp
[0] && sp
[1])) return 0;
1272 if (ctype_isptr(ctp
->info
) || ctype_isrefarray(ctp
->info
)) {
1273 if ((mm
== MM_sub
|| mm
== MM_eq
|| mm
== MM_lt
|| mm
== MM_le
) &&
1274 (ctype_isptr(s
[1]->info
) || ctype_isrefarray(s
[1]->info
))) {
1275 if (mm
== MM_sub
) { /* Pointer difference. */
1277 CTSize sz
= lj_ctype_size(cts
, ctype_cid(ctp
->info
));
1278 if (sz
== 0 || (sz
& (sz
-1)) != 0)
1279 return 0; /* NYI: integer division. */
1280 tr
= emitir(IRT(IR_SUB
, IRT_INTP
), sp
[0], sp
[1]);
1281 tr
= emitir(IRT(IR_BSAR
, IRT_INTP
), tr
, lj_ir_kint(J
, lj_fls(sz
)));
1283 tr
= emitconv(tr
, IRT_NUM
, IRT_INTP
, 0);
1286 } else { /* Pointer comparison (unsigned). */
1287 /* Assume true comparison. Fixup and emit pending guard later. */
1288 IROp op
= mm
== MM_eq
? IR_EQ
: mm
== MM_lt
? IR_ULT
: IR_ULE
;
1289 lj_ir_set(J
, IRTG(op
, IRT_PTR
), sp
[0], sp
[1]);
1290 J
->postproc
= LJ_POST_FIXGUARD
;
1294 if (!((mm
== MM_add
|| mm
== MM_sub
) && ctype_isnum(s
[1]->info
)))
1296 } else if (mm
== MM_add
&& ctype_isnum(ctp
->info
) &&
1297 (ctype_isptr(s
[1]->info
) || ctype_isrefarray(s
[1]->info
))) {
1298 TRef tr
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = tr
; /* Swap pointer and index. */
1305 IRType t
= tref_type(tr
);
1306 CTSize sz
= lj_ctype_size(cts
, ctype_cid(ctp
->info
));
1309 if (t
== IRT_NUM
|| t
== IRT_FLOAT
)
1310 tr
= emitconv(tr
, IRT_INTP
, t
, IRCONV_TRUNC
|IRCONV_ANY
);
1311 else if (!(t
== IRT_I64
|| t
== IRT_U64
))
1312 tr
= emitconv(tr
, IRT_INTP
, IRT_INT
,
1313 ((t
- IRT_I8
) & 1) ? 0 : IRCONV_SEXT
);
1315 if (!tref_typerange(sp
[1], IRT_I8
, IRT_U32
)) {
1316 tr
= emitconv(tr
, IRT_INTP
, t
,
1317 (t
== IRT_NUM
|| t
== IRT_FLOAT
) ?
1318 IRCONV_TRUNC
|IRCONV_ANY
: 0);
1321 tr
= emitir(IRT(IR_MUL
, IRT_INTP
), tr
, lj_ir_kintp(J
, sz
));
1322 tr
= emitir(IRT(mm
+(int)IR_ADD
-(int)MM_add
, IRT_PTR
), sp
[0], tr
);
1323 id
= lj_ctype_intern(cts
, CTINFO(CT_PTR
, CTALIGN_PTR
|ctype_cid(ctp
->info
)),
1325 return emitir(IRTG(IR_CNEWI
, IRT_CDATA
), lj_ir_kint(J
, id
), tr
);
1329 /* Record ctype arithmetic metamethods. */
1330 static TRef
crec_arith_meta(jit_State
*J
, TRef
*sp
, CType
**s
, CTState
*cts
,
1335 if (tviscdata(&rd
->argv
[0])) {
1336 CTypeID id
= argv2cdata(J
, J
->base
[0], &rd
->argv
[0])->ctypeid
;
1337 CType
*ct
= ctype_raw(cts
, id
);
1338 if (ctype_isptr(ct
->info
)) id
= ctype_cid(ct
->info
);
1339 tv
= lj_ctype_meta(cts
, id
, (MMS
)rd
->data
);
1341 if (!tv
&& J
->base
[1] && tviscdata(&rd
->argv
[1])) {
1342 CTypeID id
= argv2cdata(J
, J
->base
[1], &rd
->argv
[1])->ctypeid
;
1343 CType
*ct
= ctype_raw(cts
, id
);
1344 if (ctype_isptr(ct
->info
)) id
= ctype_cid(ct
->info
);
1345 tv
= lj_ctype_meta(cts
, id
, (MMS
)rd
->data
);
1350 J
->base
[-1] = lj_ir_kfunc(J
, funcV(tv
)) | TREF_FRAME
;
1351 rd
->nres
= -1; /* Pending tailcall. */
1353 } /* NYI: non-function metamethods. */
1354 } else if ((MMS
)rd
->data
== MM_eq
) { /* Fallback cdata pointer comparison. */
1355 if (sp
[0] && sp
[1] && ctype_isnum(s
[0]->info
) == ctype_isnum(s
[1]->info
)) {
1356 /* Assume true comparison. Fixup and emit pending guard later. */
1357 lj_ir_set(J
, IRTG(IR_EQ
, IRT_PTR
), sp
[0], sp
[1]);
1358 J
->postproc
= LJ_POST_FIXGUARD
;
1364 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
1368 void LJ_FASTCALL
recff_cdata_arith(jit_State
*J
, RecordFFData
*rd
)
1370 CTState
*cts
= ctype_ctsG(J2G(J
));
1374 for (i
= 0; i
< 2; i
++) {
1375 TRef tr
= J
->base
[i
];
1376 CType
*ct
= ctype_get(cts
, CTID_DOUBLE
);
1378 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
1379 } else if (tref_iscdata(tr
)) {
1380 CTypeID id
= argv2cdata(J
, tr
, &rd
->argv
[i
])->ctypeid
;
1382 ct
= ctype_raw(cts
, id
);
1383 t
= crec_ct2irt(cts
, ct
);
1384 if (ctype_isptr(ct
->info
)) { /* Resolve pointer or reference. */
1385 tr
= emitir(IRT(IR_FLOAD
, t
), tr
, IRFL_CDATA_PTR
);
1386 if (ctype_isref(ct
->info
)) {
1387 ct
= ctype_rawchild(cts
, ct
);
1388 t
= crec_ct2irt(cts
, ct
);
1390 } else if (t
== IRT_I64
|| t
== IRT_U64
) {
1391 tr
= emitir(IRT(IR_FLOAD
, t
), tr
, IRFL_CDATA_INT64
);
1394 } else if (t
== IRT_INT
|| t
== IRT_U32
) {
1395 tr
= emitir(IRT(IR_FLOAD
, t
), tr
, IRFL_CDATA_INT
);
1396 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
1398 } else if (ctype_isfunc(ct
->info
)) {
1399 tr
= emitir(IRT(IR_FLOAD
, IRT_PTR
), tr
, IRFL_CDATA_PTR
);
1401 lj_ctype_intern(cts
, CTINFO(CT_PTR
, CTALIGN_PTR
|id
), CTSIZE_PTR
));
1404 tr
= emitir(IRT(IR_ADD
, IRT_PTR
), tr
, lj_ir_kintp(J
, sizeof(GCcdata
)));
1406 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
1407 if (ctype_isnum(ct
->info
)) {
1408 if (t
== IRT_CDATA
) {
1411 if (t
== IRT_I64
|| t
== IRT_U64
) lj_needsplit(J
);
1412 tr
= emitir(IRT(IR_XLOAD
, t
), tr
, 0);
1415 } else if (tref_isnil(tr
)) {
1416 tr
= lj_ir_kptr(J
, NULL
);
1417 ct
= ctype_get(cts
, CTID_P_VOID
);
1418 } else if (tref_isinteger(tr
)) {
1419 ct
= ctype_get(cts
, CTID_INT32
);
1420 } else if (tref_isstr(tr
)) {
1421 TRef tr2
= J
->base
[1-i
];
1422 CTypeID id
= argv2cdata(J
, tr2
, &rd
->argv
[1-i
])->ctypeid
;
1423 ct
= ctype_raw(cts
, id
);
1424 if (ctype_isenum(ct
->info
)) { /* Match string against enum constant. */
1425 GCstr
*str
= strV(&rd
->argv
[i
]);
1427 CType
*cct
= lj_ctype_getfield(cts
, ct
, str
, &ofs
);
1428 if (cct
&& ctype_isconstval(cct
->info
)) {
1429 /* Specialize to the name of the enum constant. */
1430 emitir(IRTG(IR_EQ
, IRT_STR
), tr
, lj_ir_kstr(J
, str
));
1431 ct
= ctype_child(cts
, cct
);
1432 tr
= lj_ir_kint(J
, (int32_t)ofs
);
1433 } else { /* Interpreter will throw or return false. */
1434 ct
= ctype_get(cts
, CTID_P_VOID
);
1436 } else if (ctype_isptr(ct
->info
)) {
1437 tr
= emitir(IRT(IR_ADD
, IRT_PTR
), tr
, lj_ir_kintp(J
, sizeof(GCstr
)));
1439 ct
= ctype_get(cts
, CTID_P_VOID
);
1441 } else if (!tref_isnum(tr
)) {
1443 ct
= ctype_get(cts
, CTID_P_VOID
);
1451 MMS mm
= (MMS
)rd
->data
;
1452 if ((mm
== MM_len
|| mm
== MM_concat
||
1453 (!(tr
= crec_arith_int64(J
, sp
, s
, mm
)) &&
1454 !(tr
= crec_arith_ptr(J
, sp
, s
, mm
)))) &&
1455 !(tr
= crec_arith_meta(J
, sp
, s
, cts
, rd
)))
1458 /* Fixup cdata comparisons, too. Avoids some cdata escapes. */
1459 if (J
->postproc
== LJ_POST_FIXGUARD
&& frame_iscont(J
->L
->base
-1) &&
1460 !irt_isguard(J
->guardemit
)) {
1461 const BCIns
*pc
= frame_contpc(J
->L
->base
-1) - 1;
1462 if (bc_op(*pc
) <= BC_ISNEP
) {
1463 setframe_pc(&J2G(J
)->tmptv
, pc
);
1464 J2G(J
)->tmptv
.u32
.lo
= ((tref_istrue(tr
) ^ bc_op(*pc
)) & 1);
1465 J
->postproc
= LJ_POST_FIXCOMP
;
1471 /* -- C library namespace metamethods ------------------------------------- */
1473 void LJ_FASTCALL
recff_clib_index(jit_State
*J
, RecordFFData
*rd
)
1475 CTState
*cts
= ctype_ctsG(J2G(J
));
1476 if (tref_isudata(J
->base
[0]) && tref_isstr(J
->base
[1]) &&
1477 udataV(&rd
->argv
[0])->udtype
== UDTYPE_FFI_CLIB
) {
1478 CLibrary
*cl
= (CLibrary
*)uddata(udataV(&rd
->argv
[0]));
1479 GCstr
*name
= strV(&rd
->argv
[1]);
1481 CTypeID id
= lj_ctype_getname(cts
, &ct
, name
, CLNS_INDEX
);
1482 cTValue
*tv
= lj_tab_getstr(cl
->cache
, name
);
1483 rd
->nres
= rd
->data
;
1484 if (id
&& tv
&& !tvisnil(tv
)) {
1485 /* Specialize to the symbol name and make the result a constant. */
1486 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[1], lj_ir_kstr(J
, name
));
1487 if (ctype_isconstval(ct
->info
)) {
1488 if (ct
->size
>= 0x80000000u
&&
1489 (ctype_child(cts
, ct
)->info
& CTF_UNSIGNED
))
1490 J
->base
[0] = lj_ir_knum(J
, (lua_Number
)(uint32_t)ct
->size
);
1492 J
->base
[0] = lj_ir_kint(J
, (int32_t)ct
->size
);
1493 } else if (ctype_isextern(ct
->info
)) {
1494 CTypeID sid
= ctype_cid(ct
->info
);
1495 void *sp
= *(void **)cdataptr(cdataV(tv
));
1497 ct
= ctype_raw(cts
, sid
);
1498 if (LJ_64
&& !checkptr32(sp
))
1499 ptr
= lj_ir_kintp(J
, (uintptr_t)sp
);
1501 ptr
= lj_ir_kptr(J
, sp
);
1503 J
->base
[0] = crec_tv_ct(J
, ct
, sid
, ptr
);
1506 crec_ct_tv(J
, ct
, ptr
, J
->base
[2], &rd
->argv
[2]);
1509 J
->base
[0] = lj_ir_kgc(J
, obj2gco(cdataV(tv
)), IRT_CDATA
);
1512 lj_trace_err(J
, LJ_TRERR_NOCACHE
);
1514 } /* else: interpreter will throw. */
1517 /* -- FFI library functions ----------------------------------------------- */
1519 static TRef
crec_toint(jit_State
*J
, CTState
*cts
, TRef sp
, TValue
*sval
)
1521 return crec_ct_tv(J
, ctype_get(cts
, CTID_INT32
), 0, sp
, sval
);
1524 void LJ_FASTCALL
recff_ffi_new(jit_State
*J
, RecordFFData
*rd
)
1526 crec_alloc(J
, rd
, argv2ctype(J
, J
->base
[0], &rd
->argv
[0]));
1529 void LJ_FASTCALL
recff_ffi_errno(jit_State
*J
, RecordFFData
*rd
)
1533 lj_trace_err(J
, LJ_TRERR_NYICALL
);
1534 J
->base
[0] = lj_ir_call(J
, IRCALL_lj_vm_errno
);
1537 void LJ_FASTCALL
recff_ffi_string(jit_State
*J
, RecordFFData
*rd
)
1539 CTState
*cts
= ctype_ctsG(J2G(J
));
1540 TRef tr
= J
->base
[0];
1542 TRef trlen
= J
->base
[1];
1543 if (!tref_isnil(trlen
)) {
1544 trlen
= crec_toint(J
, cts
, trlen
, &rd
->argv
[1]);
1545 tr
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_CVOID
), 0, tr
, &rd
->argv
[0]);
1547 tr
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_CCHAR
), 0, tr
, &rd
->argv
[0]);
1548 trlen
= lj_ir_call(J
, IRCALL_strlen
, tr
);
1550 J
->base
[0] = emitir(IRT(IR_XSNEW
, IRT_STR
), tr
, trlen
);
1551 } /* else: interpreter will throw. */
1554 void LJ_FASTCALL
recff_ffi_copy(jit_State
*J
, RecordFFData
*rd
)
1556 CTState
*cts
= ctype_ctsG(J2G(J
));
1557 TRef trdst
= J
->base
[0], trsrc
= J
->base
[1], trlen
= J
->base
[2];
1558 if (trdst
&& trsrc
&& (trlen
|| tref_isstr(trsrc
))) {
1559 trdst
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_VOID
), 0, trdst
, &rd
->argv
[0]);
1560 trsrc
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_CVOID
), 0, trsrc
, &rd
->argv
[1]);
1562 trlen
= crec_toint(J
, cts
, trlen
, &rd
->argv
[2]);
1564 trlen
= emitir(IRTI(IR_FLOAD
), J
->base
[1], IRFL_STR_LEN
);
1565 trlen
= emitir(IRTI(IR_ADD
), trlen
, lj_ir_kint(J
, 1));
1568 crec_copy(J
, trdst
, trsrc
, trlen
, NULL
);
1569 } /* else: interpreter will throw. */
1572 void LJ_FASTCALL
recff_ffi_fill(jit_State
*J
, RecordFFData
*rd
)
1574 CTState
*cts
= ctype_ctsG(J2G(J
));
1575 TRef trdst
= J
->base
[0], trlen
= J
->base
[1], trfill
= J
->base
[2];
1576 if (trdst
&& trlen
) {
1578 if (tviscdata(&rd
->argv
[0])) { /* Get alignment of original destination. */
1580 CType
*ct
= ctype_raw(cts
, cdataV(&rd
->argv
[0])->ctypeid
);
1581 if (ctype_isptr(ct
->info
))
1582 ct
= ctype_rawchild(cts
, ct
);
1583 step
= (1u<<ctype_align(lj_ctype_info(cts
, ctype_typeid(cts
, ct
), &sz
)));
1585 trdst
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_VOID
), 0, trdst
, &rd
->argv
[0]);
1586 trlen
= crec_toint(J
, cts
, trlen
, &rd
->argv
[1]);
1588 trfill
= crec_toint(J
, cts
, trfill
, &rd
->argv
[2]);
1590 trfill
= lj_ir_kint(J
, 0);
1592 crec_fill(J
, trdst
, trlen
, trfill
, step
);
1593 } /* else: interpreter will throw. */
1596 void LJ_FASTCALL
recff_ffi_typeof(jit_State
*J
, RecordFFData
*rd
)
1598 if (tref_iscdata(J
->base
[0])) {
1599 TRef trid
= lj_ir_kint(J
, argv2ctype(J
, J
->base
[0], &rd
->argv
[0]));
1600 J
->base
[0] = emitir(IRTG(IR_CNEWI
, IRT_CDATA
),
1601 lj_ir_kint(J
, CTID_CTYPEID
), trid
);
1603 setfuncV(J
->L
, &J
->errinfo
, J
->fn
);
1604 lj_trace_err_info(J
, LJ_TRERR_NYIFFU
);
1608 void LJ_FASTCALL
recff_ffi_istype(jit_State
*J
, RecordFFData
*rd
)
1610 argv2ctype(J
, J
->base
[0], &rd
->argv
[0]);
1611 if (tref_iscdata(J
->base
[1])) {
1612 argv2ctype(J
, J
->base
[1], &rd
->argv
[1]);
1613 J
->postproc
= LJ_POST_FIXBOOL
;
1614 J
->base
[0] = TREF_TRUE
;
1616 J
->base
[0] = TREF_FALSE
;
1620 void LJ_FASTCALL
recff_ffi_abi(jit_State
*J
, RecordFFData
*rd
)
1622 if (tref_isstr(J
->base
[0])) {
1623 /* Specialize to the ABI string to make the boolean result a constant. */
1624 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[0], lj_ir_kstr(J
, strV(&rd
->argv
[0])));
1625 J
->postproc
= LJ_POST_FIXBOOL
;
1626 J
->base
[0] = TREF_TRUE
;
1628 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
1632 /* Record ffi.sizeof(), ffi.alignof(), ffi.offsetof(). */
1633 void LJ_FASTCALL
recff_ffi_xof(jit_State
*J
, RecordFFData
*rd
)
1635 CTypeID id
= argv2ctype(J
, J
->base
[0], &rd
->argv
[0]);
1636 if (rd
->data
== FF_ffi_sizeof
) {
1637 CType
*ct
= lj_ctype_rawref(ctype_ctsG(J2G(J
)), id
);
1638 if (ctype_isvltype(ct
->info
))
1639 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
1640 } else if (rd
->data
== FF_ffi_offsetof
) { /* Specialize to the field name. */
1641 if (!tref_isstr(J
->base
[1]))
1642 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
1643 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[1], lj_ir_kstr(J
, strV(&rd
->argv
[1])));
1644 rd
->nres
= 3; /* Just in case. */
1646 J
->postproc
= LJ_POST_FIXCONST
;
1647 J
->base
[0] = J
->base
[1] = J
->base
[2] = TREF_NIL
;
1650 void LJ_FASTCALL
recff_ffi_gc(jit_State
*J
, RecordFFData
*rd
)
1652 argv2cdata(J
, J
->base
[0], &rd
->argv
[0]);
1653 crec_finalizer(J
, J
->base
[0], &rd
->argv
[1]);
1656 /* -- Miscellaneous library functions ------------------------------------- */
1658 void LJ_FASTCALL
lj_crecord_tonumber(jit_State
*J
, RecordFFData
*rd
)
1660 CTState
*cts
= ctype_ctsG(J2G(J
));
1661 CType
*d
, *ct
= lj_ctype_rawref(cts
, cdataV(&rd
->argv
[0])->ctypeid
);
1662 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
1663 if (ctype_isnum(ct
->info
) || ctype_iscomplex(ct
->info
)) {
1664 if (ctype_isinteger_or_bool(ct
->info
) && ct
->size
<= 4 &&
1665 !(ct
->size
== 4 && (ct
->info
& CTF_UNSIGNED
)))
1666 d
= ctype_get(cts
, CTID_INT32
);
1668 d
= ctype_get(cts
, CTID_DOUBLE
);
1669 J
->base
[0] = crec_ct_tv(J
, d
, 0, J
->base
[0], &rd
->argv
[0]);
1671 /* Specialize to the ctype that couldn't be converted. */
1672 argv2cdata(J
, J
->base
[0], &rd
->argv
[0]);
1673 J
->base
[0] = TREF_NIL
;