2 ** Trace recorder for C data operations.
3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
11 #if LJ_HASJIT && LJ_HASFFI
19 #include "lj_cparse.h"
25 #include "lj_ircall.h"
28 #include "lj_record.h"
29 #include "lj_ffrecord.h"
31 #include "lj_crecord.h"
32 #include "lj_dispatch.h"
34 /* Some local macros to save typing. Undef'd at the end. */
35 #define IR(ref) (&J->cur.ir[(ref)])
37 /* Pass IR on to next optimization in chain (FOLD). */
38 #define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
40 #define emitconv(a, dt, st, flags) \
41 emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags))
43 /* -- C type checks ------------------------------------------------------- */
45 static GCcdata
*argv2cdata(jit_State
*J
, TRef tr
, cTValue
*o
)
49 if (!tref_iscdata(tr
))
50 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
52 /* Specialize to the CTypeID. */
53 trtypeid
= emitir(IRT(IR_FLOAD
, IRT_U16
), tr
, IRFL_CDATA_CTYPEID
);
54 emitir(IRTG(IR_EQ
, IRT_INT
), trtypeid
, lj_ir_kint(J
, (int32_t)cd
->ctypeid
));
58 /* Specialize to the CTypeID held by a cdata constructor. */
59 static CTypeID
crec_constructor(jit_State
*J
, GCcdata
*cd
, TRef tr
)
62 lua_assert(tref_iscdata(tr
) && cd
->ctypeid
== CTID_CTYPEID
);
63 id
= *(CTypeID
*)cdataptr(cd
);
64 tr
= emitir(IRT(IR_FLOAD
, IRT_INT
), tr
, IRFL_CDATA_INT
);
65 emitir(IRTG(IR_EQ
, IRT_INT
), tr
, lj_ir_kint(J
, (int32_t)id
));
69 static CTypeID
argv2ctype(jit_State
*J
, TRef tr
, cTValue
*o
)
75 /* Specialize to the string containing the C type declaration. */
76 emitir(IRTG(IR_EQ
, IRT_STR
), tr
, lj_ir_kstr(J
, s
));
78 cp
.cts
= ctype_ctsG(J2G(J
));
80 cp
.srcname
= strdata(s
);
83 cp
.mode
= CPARSE_MODE_ABSTRACT
|CPARSE_MODE_NOIMPLICIT
;
84 if (lj_cparse(&cp
) || cp
.cts
->top
> oldtop
) /* Avoid new struct defs. */
85 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
88 GCcdata
*cd
= argv2cdata(J
, tr
, o
);
89 return cd
->ctypeid
== CTID_CTYPEID
? crec_constructor(J
, cd
, tr
) :
94 /* -- Convert C type to C type -------------------------------------------- */
97 ** This code mirrors the code in lj_cconv.c. It performs the same steps
98 ** for the trace recorder that lj_cconv.c does for the interpreter.
100 ** One major difference is that we can get away with much fewer checks
101 ** here. E.g. checks for casts, constness or correct types can often be
102 ** omitted, even if they might fail. The interpreter subsequently throws
103 ** an error, which aborts the trace.
105 ** All operations are specialized to their C types, so the on-trace
106 ** outcome must be the same as the outcome in the interpreter. If the
107 ** interpreter doesn't throw an error, then the trace is correct, too.
108 ** Care must be taken not to generate invalid (temporary) IR or to
112 /* Convert CType to IRType. */
113 static IRType
crec_ct2irt(CTState
*cts
, CType
*ct
)
115 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
116 if (LJ_LIKELY(ctype_isnum(ct
->info
))) {
117 if ((ct
->info
& CTF_FP
)) {
118 if (ct
->size
== sizeof(double))
120 else if (ct
->size
== sizeof(float))
123 uint32_t b
= lj_fls(ct
->size
);
125 return IRT_I8
+ 2*b
+ ((ct
->info
& CTF_UNSIGNED
) ? 1 : 0);
127 } else if (ctype_isptr(ct
->info
)) {
128 return (LJ_64
&& ct
->size
== 8) ? IRT_P64
: IRT_P32
;
129 } else if (ctype_iscomplex(ct
->info
)) {
130 if (ct
->size
== 2*sizeof(double))
132 else if (ct
->size
== 2*sizeof(float))
138 /* Determine whether a passed number or cdata number is non-zero. */
139 static int crec_isnonzero(CType
*s
, void *p
)
145 if ((s
->info
& CTF_FP
)) {
146 if (s
->size
== sizeof(float))
147 return (*(float *)p
!= 0);
149 return (*(double *)p
!= 0);
152 return (*(uint8_t *)p
!= 0);
153 else if (s
->size
== 2)
154 return (*(uint16_t *)p
!= 0);
155 else if (s
->size
== 4)
156 return (*(uint32_t *)p
!= 0);
158 return (*(uint64_t *)p
!= 0);
162 static TRef
crec_ct_ct(jit_State
*J
, CType
*d
, CType
*s
, TRef dp
, TRef sp
,
165 IRType dt
= crec_ct2irt(ctype_ctsG(J2G(J
)), d
);
166 IRType st
= crec_ct2irt(ctype_ctsG(J2G(J
)), s
);
167 CTSize dsize
= d
->size
, ssize
= s
->size
;
168 CTInfo dinfo
= d
->info
, sinfo
= s
->info
;
170 if (ctype_type(dinfo
) > CT_MAYCONVERT
|| ctype_type(sinfo
) > CT_MAYCONVERT
)
174 ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and
175 ** numbers up to 8 bytes. Otherwise sp holds a pointer.
178 switch (cconv_idx2(dinfo
, sinfo
)) {
179 /* Destination is a bool. */
181 goto xstore
; /* Source operand is already normalized. */
184 if (st
!= IRT_CDATA
) {
185 /* Specialize to the result of a comparison against 0. */
186 TRef zero
= (st
== IRT_NUM
|| st
== IRT_FLOAT
) ? lj_ir_knum(J
, 0) :
187 (st
== IRT_I64
|| st
== IRT_U64
) ? lj_ir_kint64(J
, 0) :
189 int isnz
= crec_isnonzero(s
, svisnz
);
190 emitir(IRTG(isnz
? IR_NE
: IR_EQ
, st
), sp
, zero
);
191 sp
= lj_ir_kint(J
, isnz
);
196 /* Destination is an integer. */
200 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
201 /* Extend 32 to 64 bit integer. */
202 if (dsize
== 8 && ssize
< 8 && !(LJ_64
&& (sinfo
& CTF_UNSIGNED
)))
203 sp
= emitconv(sp
, dt
, ssize
< 4 ? IRT_INT
: st
,
204 (sinfo
& CTF_UNSIGNED
) ? 0 : IRCONV_SEXT
);
205 else if (dsize
< 8 && ssize
== 8) /* Truncate from 64 bit integer. */
206 sp
= emitconv(sp
, dsize
< 4 ? IRT_INT
: dt
, st
, 0);
207 else if (st
== IRT_INT
)
208 sp
= lj_opt_narrow_toint(J
, sp
);
210 if (dt
== IRT_I64
|| dt
== IRT_U64
) lj_needsplit(J
);
211 if (dp
== 0) return sp
;
212 emitir(IRT(IR_XSTORE
, dt
), dp
, sp
);
215 sp
= emitir(IRT(IR_XLOAD
, st
), sp
, 0); /* Load re. */
218 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
219 sp
= emitconv(sp
, dsize
< 4 ? IRT_INT
: dt
, st
, IRCONV_TRUNC
|IRCONV_ANY
);
223 sinfo
= CTINFO(CT_NUM
, CTF_UNSIGNED
);
228 /* Destination is a floating-point number. */
232 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
233 sp
= emitconv(sp
, dt
, ssize
< 4 ? IRT_INT
: st
, 0);
236 sp
= emitir(IRT(IR_XLOAD
, st
), sp
, 0); /* Load re. */
240 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
241 if (dt
!= st
) sp
= emitconv(sp
, dt
, st
, 0);
244 /* Destination is a complex number. */
248 TRef ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, (dsize
>> 1)));
249 emitir(IRT(IR_XSTORE
, dt
), ptr
, lj_ir_knum(J
, 0));
252 if ((sinfo
& CTF_FP
)) goto conv_F_F
; else goto conv_F_I
;
255 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
258 re
= emitir(IRT(IR_XLOAD
, st
), sp
, 0);
259 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, (ssize
>> 1)));
260 im
= emitir(IRT(IR_XLOAD
, st
), ptr
, 0);
262 re
= emitconv(re
, dt
, st
, 0);
263 im
= emitconv(im
, dt
, st
, 0);
265 emitir(IRT(IR_XSTORE
, dt
), dp
, re
);
266 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, (dsize
>> 1)));
267 emitir(IRT(IR_XSTORE
, dt
), ptr
, im
);
271 /* Destination is a vector. */
278 /* Destination is a pointer. */
282 /* There are only 32 bit pointers/addresses on 32 bit machines.
283 ** Also ok on x64, since all 32 bit ops clear the upper part of the reg.
287 if (st
== IRT_CDATA
) goto err_nyi
;
288 if (!LJ_64
&& ssize
== 8) /* Truncate from 64 bit integer. */
289 sp
= emitconv(sp
, IRT_U32
, st
, 0);
292 if (st
== IRT_CDATA
) goto err_nyi
;
293 /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
294 sp
= emitconv(sp
, (LJ_64
&& dsize
== 8) ? IRT_I64
: IRT_U32
,
295 st
, IRCONV_TRUNC
|IRCONV_ANY
);
298 /* Destination is an array. */
302 /* Destination is a struct/union. */
309 lj_trace_err(J
, LJ_TRERR_NYICONV
);
315 /* -- Convert C type to TValue (load) ------------------------------------- */
317 static TRef
crec_tv_ct(jit_State
*J
, CType
*s
, CTypeID sid
, TRef sp
)
319 CTState
*cts
= ctype_ctsG(J2G(J
));
320 IRType t
= crec_ct2irt(cts
, s
);
321 CTInfo sinfo
= s
->info
;
322 if (ctype_isnum(sinfo
)) {
325 goto err_nyi
; /* NYI: copyval of >64 bit integers. */
326 tr
= emitir(IRT(IR_XLOAD
, t
), sp
, 0);
327 if (t
== IRT_FLOAT
|| t
== IRT_U32
) { /* Keep uint32_t/float as numbers. */
328 return emitconv(tr
, IRT_NUM
, t
, 0);
329 } else if (t
== IRT_I64
|| t
== IRT_U64
) { /* Box 64 bit integer. */
332 } else if ((sinfo
& CTF_BOOL
)) {
333 /* Assume not equal to zero. Fixup and emit pending guard later. */
334 lj_ir_set(J
, IRTGI(IR_NE
), tr
, lj_ir_kint(J
, 0));
335 J
->postproc
= LJ_POST_FIXGUARD
;
340 } else if (ctype_isptr(sinfo
) || ctype_isenum(sinfo
)) {
341 sp
= emitir(IRT(IR_XLOAD
, t
), sp
, 0); /* Box pointers and enums. */
342 } else if (ctype_isrefarray(sinfo
) || ctype_isstruct(sinfo
)) {
344 sid
= lj_ctype_intern(cts
, CTINFO_REF(sid
), CTSIZE_PTR
); /* Create ref. */
345 } else if (ctype_iscomplex(sinfo
)) { /* Unbox/box complex. */
346 ptrdiff_t esz
= (ptrdiff_t)(s
->size
>> 1);
347 TRef ptr
, tr1
, tr2
, dp
;
348 dp
= emitir(IRTG(IR_CNEW
, IRT_CDATA
), lj_ir_kint(J
, sid
), TREF_NIL
);
349 tr1
= emitir(IRT(IR_XLOAD
, t
), sp
, 0);
350 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, esz
));
351 tr2
= emitir(IRT(IR_XLOAD
, t
), ptr
, 0);
352 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, sizeof(GCcdata
)));
353 emitir(IRT(IR_XSTORE
, t
), ptr
, tr1
);
354 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, sizeof(GCcdata
)+esz
));
355 emitir(IRT(IR_XSTORE
, t
), ptr
, tr2
);
358 /* NYI: copyval of vectors. */
360 lj_trace_err(J
, LJ_TRERR_NYICONV
);
362 /* Box pointer, ref, enum or 64 bit integer. */
363 return emitir(IRTG(IR_CNEWI
, IRT_CDATA
), lj_ir_kint(J
, sid
), sp
);
366 /* -- Convert TValue to C type (store) ------------------------------------ */
368 static TRef
crec_ct_tv(jit_State
*J
, CType
*d
, TRef dp
, TRef sp
, cTValue
*sval
)
370 CTState
*cts
= ctype_ctsG(J2G(J
));
371 CTypeID sid
= CTID_P_VOID
;
374 if (LJ_LIKELY(tref_isinteger(sp
))) {
376 svisnz
= (void *)(intptr_t)(tvisint(sval
)?(intV(sval
)!=0):!tviszero(sval
));
377 } else if (tref_isnum(sp
)) {
379 svisnz
= (void *)(intptr_t)(tvisint(sval
)?(intV(sval
)!=0):!tviszero(sval
));
380 } else if (tref_isbool(sp
)) {
381 sp
= lj_ir_kint(J
, tref_istrue(sp
) ? 1 : 0);
383 } else if (tref_isnil(sp
)) {
384 sp
= lj_ir_kptr(J
, NULL
);
385 } else if (tref_isudata(sp
)) {
386 GCudata
*ud
= udataV(sval
);
387 if (ud
->udtype
== UDTYPE_IO_FILE
) {
388 TRef tr
= emitir(IRT(IR_FLOAD
, IRT_U8
), sp
, IRFL_UDATA_UDTYPE
);
389 emitir(IRTGI(IR_EQ
), tr
, lj_ir_kint(J
, UDTYPE_IO_FILE
));
390 sp
= emitir(IRT(IR_FLOAD
, IRT_PTR
), sp
, IRFL_UDATA_FILE
);
392 sp
= emitir(IRT(IR_ADD
, IRT_P32
), sp
, lj_ir_kint(J
, sizeof(GCudata
)));
394 } else if (tref_isstr(sp
)) {
395 if (ctype_isenum(d
->info
)) { /* Match string against enum constant. */
396 GCstr
*str
= strV(sval
);
398 CType
*cct
= lj_ctype_getfield(cts
, d
, str
, &ofs
);
399 /* Specialize to the name of the enum constant. */
400 emitir(IRTG(IR_EQ
, IRT_STR
), sp
, lj_ir_kstr(J
, str
));
401 if (cct
&& ctype_isconstval(cct
->info
)) {
402 lua_assert(ctype_child(cts
, cct
)->size
== 4);
403 svisnz
= (void *)(intptr_t)(ofs
!= 0);
404 sp
= lj_ir_kint(J
, (int32_t)ofs
);
405 sid
= ctype_cid(cct
->info
);
406 } /* else: interpreter will throw. */
407 } else if (ctype_isrefarray(d
->info
)) { /* Copy string to array. */
408 lj_trace_err(J
, LJ_TRERR_BADTYPE
); /* NYI */
409 } else { /* Otherwise pass the string data as a const char[]. */
410 sp
= emitir(IRT(IR_STRREF
, IRT_P32
), sp
, lj_ir_kint(J
, 0));
413 } else { /* NYI: tref_istab(sp), tref_islightud(sp). */
415 sid
= argv2cdata(J
, sp
, sval
)->ctypeid
;
416 s
= ctype_raw(cts
, sid
);
417 svisnz
= cdataptr(cdataV(sval
));
418 t
= crec_ct2irt(cts
, s
);
419 if (ctype_isptr(s
->info
)) {
420 sp
= emitir(IRT(IR_FLOAD
, t
), sp
, IRFL_CDATA_PTR
);
421 if (ctype_isref(s
->info
)) {
422 svisnz
= *(void **)svisnz
;
423 s
= ctype_rawchild(cts
, s
);
424 if (ctype_isenum(s
->info
)) s
= ctype_child(cts
, s
);
425 t
= crec_ct2irt(cts
, s
);
429 } else if (t
== IRT_I64
|| t
== IRT_U64
) {
430 sp
= emitir(IRT(IR_FLOAD
, t
), sp
, IRFL_CDATA_INT64
);
433 } else if (t
== IRT_INT
|| t
== IRT_U32
) {
434 if (ctype_isenum(s
->info
)) s
= ctype_child(cts
, s
);
435 sp
= emitir(IRT(IR_FLOAD
, t
), sp
, IRFL_CDATA_INT
);
438 sp
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, sizeof(GCcdata
)));
440 if (ctype_isnum(s
->info
) && t
!= IRT_CDATA
)
441 sp
= emitir(IRT(IR_XLOAD
, t
), sp
, 0); /* Load number value. */
444 s
= ctype_get(cts
, sid
);
446 if (ctype_isenum(d
->info
)) d
= ctype_child(cts
, d
);
447 return crec_ct_ct(J
, d
, s
, dp
, sp
, svisnz
);
450 /* -- C data metamethods -------------------------------------------------- */
452 /* This would be rather difficult in FOLD, so do it here:
453 ** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k)
454 ** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
456 static TRef
crec_reassoc_ofs(jit_State
*J
, TRef tr
, ptrdiff_t *ofsp
, MSize sz
)
458 IRIns
*ir
= IR(tref_ref(tr
));
459 if (LJ_LIKELY(J
->flags
& JIT_F_OPT_FOLD
) && irref_isk(ir
->op2
) &&
460 (ir
->o
== IR_ADD
|| ir
->o
== IR_ADDOV
|| ir
->o
== IR_SUBOV
)) {
461 IRIns
*irk
= IR(ir
->op2
);
463 if (LJ_64
&& irk
->o
== IR_KINT64
)
464 k
= (ptrdiff_t)ir_kint64(irk
)->u64
* sz
;
466 k
= (ptrdiff_t)irk
->i
* sz
;
467 if (ir
->o
== IR_SUBOV
) *ofsp
-= k
; else *ofsp
+= k
;
468 tr
= ir
->op1
; /* Not a TRef, but the caller doesn't care. */
473 /* Record ctype __index/__newindex metamethods. */
474 static void crec_index_meta(jit_State
*J
, CTState
*cts
, CType
*ct
,
477 CTypeID id
= ctype_typeid(cts
, ct
);
478 cTValue
*tv
= lj_ctype_meta(cts
, id
, rd
->data
? MM_newindex
: MM_index
);
480 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
482 J
->base
[-1] = lj_ir_kfunc(J
, funcV(tv
)) | TREF_FRAME
;
483 rd
->nres
= -1; /* Pending tailcall. */
484 } else if (rd
->data
== 0 && tvistab(tv
) && tref_isstr(J
->base
[1])) {
485 /* Specialize to result of __index lookup. */
486 cTValue
*o
= lj_tab_get(J
->L
, tabV(tv
), &rd
->argv
[1]);
487 J
->base
[0] = lj_record_constify(J
, o
);
489 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
490 /* Always specialize to the key. */
491 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[1], lj_ir_kstr(J
, strV(&rd
->argv
[1])));
493 /* NYI: resolving of non-function metamethods. */
494 /* NYI: non-string keys for __index table. */
495 /* NYI: stores to __newindex table. */
496 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
500 void LJ_FASTCALL
recff_cdata_index(jit_State
*J
, RecordFFData
*rd
)
502 TRef idx
, ptr
= J
->base
[0];
503 ptrdiff_t ofs
= sizeof(GCcdata
);
504 GCcdata
*cd
= argv2cdata(J
, ptr
, &rd
->argv
[0]);
505 CTState
*cts
= ctype_ctsG(J2G(J
));
506 CType
*ct
= ctype_raw(cts
, cd
->ctypeid
);
509 /* Resolve pointer or reference for cdata object. */
510 if (ctype_isptr(ct
->info
)) {
511 IRType t
= (LJ_64
&& ct
->size
== 8) ? IRT_P64
: IRT_P32
;
512 if (ctype_isref(ct
->info
)) ct
= ctype_rawchild(cts
, ct
);
513 ptr
= emitir(IRT(IR_FLOAD
, t
), ptr
, IRFL_CDATA_PTR
);
515 ptr
= crec_reassoc_ofs(J
, ptr
, &ofs
, 1);
520 if (tref_isnumber(idx
)) {
521 idx
= lj_opt_narrow_cindex(J
, idx
);
522 if (ctype_ispointer(ct
->info
)) {
525 if ((ct
->info
& CTF_COMPLEX
))
526 idx
= emitir(IRT(IR_BAND
, IRT_INTP
), idx
, lj_ir_kintp(J
, 1));
527 sz
= lj_ctype_size(cts
, (sid
= ctype_cid(ct
->info
)));
528 idx
= crec_reassoc_ofs(J
, idx
, &ofs
, sz
);
529 #if LJ_TARGET_ARM || LJ_TARGET_PPC
530 /* Hoist base add to allow fusion of index/shift into operands. */
531 if (LJ_LIKELY(J
->flags
& JIT_F_OPT_LOOP
) && ofs
533 && (sz
== 1 || sz
== 4)
536 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), ptr
, lj_ir_kintp(J
, ofs
));
540 idx
= emitir(IRT(IR_MUL
, IRT_INTP
), idx
, lj_ir_kintp(J
, sz
));
541 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), idx
, ptr
);
543 } else if (tref_iscdata(idx
)) {
544 GCcdata
*cdk
= cdataV(&rd
->argv
[1]);
545 CType
*ctk
= ctype_raw(cts
, cdk
->ctypeid
);
546 IRType t
= crec_ct2irt(cts
, ctk
);
547 if (ctype_ispointer(ct
->info
) && t
>= IRT_I8
&& t
<= IRT_U64
) {
548 if (ctk
->size
== 8) {
549 idx
= emitir(IRT(IR_FLOAD
, t
), idx
, IRFL_CDATA_INT64
);
550 } else if (ctk
->size
== 4) {
551 idx
= emitir(IRT(IR_FLOAD
, t
), idx
, IRFL_CDATA_INT
);
553 idx
= emitir(IRT(IR_ADD
, IRT_PTR
), idx
,
554 lj_ir_kintp(J
, sizeof(GCcdata
)));
555 idx
= emitir(IRT(IR_XLOAD
, t
), idx
, 0);
557 if (LJ_64
&& ctk
->size
< sizeof(intptr_t) && !(ctk
->info
& CTF_UNSIGNED
))
558 idx
= emitconv(idx
, IRT_INTP
, IRT_INT
, IRCONV_SEXT
);
559 if (!LJ_64
&& ctk
->size
> sizeof(intptr_t)) {
560 idx
= emitconv(idx
, IRT_INTP
, t
, 0);
565 } else if (tref_isstr(idx
)) {
566 GCstr
*name
= strV(&rd
->argv
[1]);
567 if (cd
->ctypeid
== CTID_CTYPEID
)
568 ct
= ctype_raw(cts
, crec_constructor(J
, cd
, ptr
));
569 if (ctype_isstruct(ct
->info
)) {
572 fct
= lj_ctype_getfield(cts
, ct
, name
, &fofs
);
574 /* Always specialize to the field name. */
575 emitir(IRTG(IR_EQ
, IRT_STR
), idx
, lj_ir_kstr(J
, name
));
576 if (ctype_isconstval(fct
->info
)) {
577 if (fct
->size
>= 0x80000000u
&&
578 (ctype_child(cts
, fct
)->info
& CTF_UNSIGNED
)) {
579 J
->base
[0] = lj_ir_knum(J
, (lua_Number
)(uint32_t)fct
->size
);
582 J
->base
[0] = lj_ir_kint(J
, (int32_t)fct
->size
);
583 return; /* Interpreter will throw for newindex. */
584 } else if (ctype_isbitfield(fct
->info
)) {
585 lj_trace_err(J
, LJ_TRERR_NYICONV
);
587 lua_assert(ctype_isfield(fct
->info
));
588 sid
= ctype_cid(fct
->info
);
590 ofs
+= (ptrdiff_t)fofs
;
592 } else if (ctype_iscomplex(ct
->info
)) {
593 if (name
->len
== 2 &&
594 ((strdata(name
)[0] == 'r' && strdata(name
)[1] == 'e') ||
595 (strdata(name
)[0] == 'i' && strdata(name
)[1] == 'm'))) {
596 /* Always specialize to the field name. */
597 emitir(IRTG(IR_EQ
, IRT_STR
), idx
, lj_ir_kstr(J
, name
));
598 if (strdata(name
)[0] == 'i') ofs
+= (ct
->size
>> 1);
599 sid
= ctype_cid(ct
->info
);
604 if (ctype_isptr(ct
->info
)) { /* Automatically perform '->'. */
605 CType
*cct
= ctype_rawchild(cts
, ct
);
606 if (ctype_isstruct(cct
->info
)) {
608 if (tref_isstr(idx
)) goto again
;
611 crec_index_meta(J
, cts
, ct
, rd
);
616 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), ptr
, lj_ir_kintp(J
, ofs
));
618 /* Resolve reference for field. */
619 ct
= ctype_get(cts
, sid
);
620 if (ctype_isref(ct
->info
))
621 ptr
= emitir(IRT(IR_XLOAD
, IRT_PTR
), ptr
, 0);
623 while (ctype_isattrib(ct
->info
))
624 ct
= ctype_child(cts
, ct
); /* Skip attributes. */
626 if (rd
->data
== 0) { /* __index metamethod. */
627 J
->base
[0] = crec_tv_ct(J
, ct
, sid
, ptr
);
628 } else { /* __newindex metamethod. */
631 crec_ct_tv(J
, ct
, ptr
, J
->base
[2], &rd
->argv
[2]);
635 /* Record cdata allocation. */
636 static void crec_alloc(jit_State
*J
, RecordFFData
*rd
, CTypeID id
)
638 CTState
*cts
= ctype_ctsG(J2G(J
));
640 CTInfo info
= lj_ctype_info(cts
, id
, &sz
);
641 CType
*d
= ctype_raw(cts
, id
);
643 if (!sz
|| sz
> 128 || (info
& CTF_VLA
) || ctype_align(info
) > CT_MEMALIGN
)
644 lj_trace_err(J
, LJ_TRERR_NYICONV
); /* NYI: large/special allocations. */
645 trid
= lj_ir_kint(J
, id
);
646 /* Use special instruction to box pointer or 32/64 bit integer. */
647 if (ctype_isptr(info
) || (ctype_isinteger(info
) && (sz
== 4 || sz
== 8))) {
648 TRef sp
= J
->base
[1] ? crec_ct_tv(J
, d
, 0, J
->base
[1], &rd
->argv
[1]) :
649 ctype_isptr(info
) ? lj_ir_kptr(J
, NULL
) :
650 sz
== 4 ? lj_ir_kint(J
, 0) :
651 (lj_needsplit(J
), lj_ir_kint64(J
, 0));
652 J
->base
[0] = emitir(IRTG(IR_CNEWI
, IRT_CDATA
), trid
, sp
);
654 TRef trcd
= emitir(IRTG(IR_CNEW
, IRT_CDATA
), trid
, TREF_NIL
);
657 if (J
->base
[1] && !J
->base
[2] && !lj_cconv_multi_init(d
, &rd
->argv
[1])) {
659 } else if (ctype_isarray(d
->info
)) {
660 CType
*dc
= ctype_rawchild(cts
, d
); /* Array element type. */
661 CTSize ofs
, esize
= dc
->size
;
667 if (!(ctype_isnum(dc
->info
) || ctype_isptr(dc
->info
)))
668 lj_trace_err(J
, LJ_TRERR_NYICONV
); /* NYI: init array of aggregates. */
669 for (i
= 1, ofs
= 0; ofs
< sz
; ofs
+= esize
) {
670 TRef dp
= emitir(IRT(IR_ADD
, IRT_PTR
), trcd
,
671 lj_ir_kintp(J
, ofs
+ sizeof(GCcdata
)));
677 sp
= ctype_isnum(dc
->info
) ? lj_ir_kint(J
, 0) : TREF_NIL
;
679 crec_ct_tv(J
, dc
, dp
, sp
, sval
);
681 } else if (ctype_isstruct(d
->info
)) {
682 CTypeID fid
= d
->sib
;
685 CType
*df
= ctype_get(cts
, fid
);
687 if (ctype_isfield(df
->info
)) {
693 if (!gcref(df
->name
)) continue; /* Ignore unnamed fields. */
694 dc
= ctype_rawchild(cts
, df
); /* Field type. */
695 if (!(ctype_isnum(dc
->info
) || ctype_isptr(dc
->info
) ||
696 ctype_isenum(dc
->info
)))
697 lj_trace_err(J
, LJ_TRERR_NYICONV
); /* NYI: init aggregates. */
703 sp
= ctype_isptr(dc
->info
) ? TREF_NIL
: lj_ir_kint(J
, 0);
705 dp
= emitir(IRT(IR_ADD
, IRT_PTR
), trcd
,
706 lj_ir_kintp(J
, df
->size
+ sizeof(GCcdata
)));
707 crec_ct_tv(J
, dc
, dp
, sp
, sval
);
708 } else if (!ctype_isconstval(df
->info
)) {
709 /* NYI: init bitfields and sub-structures. */
710 lj_trace_err(J
, LJ_TRERR_NYICONV
);
716 dp
= emitir(IRT(IR_ADD
, IRT_PTR
), trcd
, lj_ir_kintp(J
, sizeof(GCcdata
)));
718 crec_ct_tv(J
, d
, dp
, J
->base
[1], &rd
->argv
[1]);
722 crec_ct_tv(J
, d
, dp
, lj_ir_kint(J
, 0), &tv
);
725 /* Handle __gc metamethod. */
726 fin
= lj_ctype_meta(cts
, id
, MM_gc
);
728 TRef trlo
= lj_ir_call(J
, IRCALL_lj_cdata_setfin
, trcd
);
729 TRef trhi
= emitir(IRT(IR_ADD
, IRT_P32
), trlo
, lj_ir_kint(J
, 4));
730 if (LJ_BE
) { TRef tmp
= trlo
; trlo
= trhi
; trhi
= tmp
; }
732 emitir(IRT(IR_XSTORE
, IRT_P32
), trlo
, lj_ir_kfunc(J
, funcV(fin
)));
733 emitir(IRTI(IR_XSTORE
), trhi
, lj_ir_kint(J
, LJ_TFUNC
));
734 } else if (tviscdata(fin
)) {
735 emitir(IRT(IR_XSTORE
, IRT_P32
), trlo
,
736 lj_ir_kgc(J
, obj2gco(cdataV(fin
)), IRT_CDATA
));
737 emitir(IRTI(IR_XSTORE
), trhi
, lj_ir_kint(J
, LJ_TCDATA
));
739 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
746 /* Record argument conversions. */
747 static TRef
crec_call_args(jit_State
*J
, RecordFFData
*rd
,
748 CTState
*cts
, CType
*ct
)
750 TRef args
[CCI_NARGS_MAX
];
757 TRef
*arg0
= NULL
, *arg1
= NULL
;
760 if (ctype_cconv(ct
->info
) == CTCC_THISCALL
)
762 else if (ctype_cconv(ct
->info
) == CTCC_FASTCALL
)
766 /* Skip initial attributes. */
769 CType
*ctf
= ctype_get(cts
, fid
);
770 if (!ctype_isattrib(ctf
->info
)) break;
774 for (n
= 0, base
= J
->base
+1, o
= rd
->argv
+1; *base
; n
++, base
++, o
++) {
778 if (n
>= CCI_NARGS_MAX
)
779 lj_trace_err(J
, LJ_TRERR_NYICALL
);
781 if (fid
) { /* Get argument type from field. */
782 CType
*ctf
= ctype_get(cts
, fid
);
784 lua_assert(ctype_isfield(ctf
->info
));
785 did
= ctype_cid(ctf
->info
);
787 if (!(ct
->info
& CTF_VARARG
))
788 lj_trace_err(J
, LJ_TRERR_NYICALL
); /* Too many arguments. */
789 did
= lj_ccall_ctid_vararg(cts
, o
); /* Infer vararg type. */
791 d
= ctype_raw(cts
, did
);
792 if (!(ctype_isnum(d
->info
) || ctype_isptr(d
->info
) ||
793 ctype_isenum(d
->info
)))
794 lj_trace_err(J
, LJ_TRERR_NYICALL
);
795 tr
= crec_ct_tv(J
, d
, 0, *base
, o
);
796 if (ctype_isinteger_or_bool(d
->info
)) {
798 if ((d
->info
& CTF_UNSIGNED
))
799 tr
= emitconv(tr
, IRT_INT
, d
->size
==1 ? IRT_U8
: IRT_U16
, 0);
801 tr
= emitconv(tr
, IRT_INT
, d
->size
==1 ? IRT_I8
: IRT_I16
,IRCONV_SEXT
);
803 } else if (LJ_SOFTFP
&& ctype_isfp(d
->info
) && d
->size
> 4) {
807 /* 64 bit args must not end up in registers for fastcall/thiscall. */
809 if (!ctype_isfp(d
->info
)) {
810 /* Sigh, the Windows/x86 ABI allows reordering across 64 bit args. */
811 if (tref_typerange(tr
, IRT_I64
, IRT_U64
)) {
813 arg0
= &args
[n
]; args
[n
++] = TREF_NIL
; ngpr
--;
815 arg1
= &args
[n
]; args
[n
++] = TREF_NIL
; ngpr
--;
819 if (arg0
) { *arg0
= tr
; arg0
= NULL
; n
--; continue; }
820 if (arg1
) { *arg1
= tr
; arg1
= NULL
; n
--; continue; }
825 if (!ctype_isfp(d
->info
) && ngpr
) {
826 if (tref_typerange(tr
, IRT_I64
, IRT_U64
)) {
827 /* No reordering for other x86 ABIs. Simply add alignment args. */
828 do { args
[n
++] = TREF_NIL
; } while (--ngpr
);
838 for (i
= 1; i
< n
; i
++)
839 tr
= emitir(IRT(IR_CARG
, IRT_NIL
), tr
, args
[i
]);
843 /* Create a snapshot for the caller, simulating a 'false' return value. */
844 static void crec_snap_caller(jit_State
*J
)
847 TValue
*base
= L
->base
, *top
= L
->top
;
848 const BCIns
*pc
= J
->pc
;
849 TRef ftr
= J
->base
[-1];
851 if (!frame_islua(base
-1))
852 lj_trace_err(J
, LJ_TRERR_NYICALL
);
853 J
->pc
= frame_pc(base
-1); delta
= 1+bc_a(J
->pc
[-1]);
854 L
->top
= base
; L
->base
= base
- delta
;
855 J
->base
[-1] = TREF_FALSE
;
856 J
->base
-= delta
; J
->baseslot
-= (BCReg
)delta
;
857 J
->maxslot
= (BCReg
)delta
; J
->framedepth
--;
859 L
->base
= base
; L
->top
= top
;
860 J
->framedepth
++; J
->maxslot
= 1;
861 J
->base
+= delta
; J
->baseslot
+= (BCReg
)delta
;
862 J
->base
[-1] = ftr
; J
->pc
= pc
;
865 /* Record function call. */
866 static int crec_call(jit_State
*J
, RecordFFData
*rd
, GCcdata
*cd
)
868 CTState
*cts
= ctype_ctsG(J2G(J
));
869 CType
*ct
= ctype_raw(cts
, cd
->ctypeid
);
871 if (ctype_isptr(ct
->info
)) {
872 tp
= (LJ_64
&& ct
->size
== 8) ? IRT_P64
: IRT_P32
;
873 ct
= ctype_rawchild(cts
, ct
);
875 if (ctype_isfunc(ct
->info
)) {
876 TRef func
= emitir(IRT(IR_FLOAD
, tp
), J
->base
[0], IRFL_CDATA_PTR
);
877 CType
*ctr
= ctype_rawchild(cts
, ct
);
878 IRType t
= crec_ct2irt(cts
, ctr
);
881 /* Check for blacklisted C functions that might call a callback. */
883 cdata_getptr(cdataptr(cd
), (LJ_64
&& tp
== IRT_P64
) ? 8 : 4));
884 if (tvistrue(lj_tab_get(J
->L
, cts
->miscmap
, &tv
)))
885 lj_trace_err(J
, LJ_TRERR_BLACKL
);
886 if (ctype_isvoid(ctr
->info
)) {
889 } else if (!(ctype_isnum(ctr
->info
) || ctype_isptr(ctr
->info
) ||
890 ctype_isenum(ctr
->info
)) || t
== IRT_CDATA
) {
891 lj_trace_err(J
, LJ_TRERR_NYICALL
);
893 if ((ct
->info
& CTF_VARARG
)
895 || ctype_cconv(ct
->info
) != CTCC_CDECL
898 func
= emitir(IRT(IR_CARG
, IRT_NIL
), func
,
899 lj_ir_kint(J
, ctype_typeid(cts
, ct
)));
900 tr
= emitir(IRT(IR_CALLXS
, t
), crec_call_args(J
, rd
, cts
, ct
), func
);
901 if (ctype_isbool(ctr
->info
)) {
902 if (frame_islua(J
->L
->base
-1) && bc_b(frame_pc(J
->L
->base
-1)[-1]) == 1) {
903 /* Don't check result if ignored. */
907 lj_ir_set(J
, IRTGI(IR_NE
), tr
, lj_ir_kint(J
, 0));
908 J
->postproc
= LJ_POST_FIXGUARDSNAP
;
911 } else if (t
== IRT_PTR
|| (LJ_64
&& t
== IRT_P32
) ||
912 t
== IRT_I64
|| t
== IRT_U64
|| ctype_isenum(ctr
->info
)) {
913 TRef trid
= lj_ir_kint(J
, ctype_cid(ct
->info
));
914 tr
= emitir(IRTG(IR_CNEWI
, IRT_CDATA
), trid
, tr
);
915 if (t
== IRT_I64
|| t
== IRT_U64
) lj_needsplit(J
);
916 } else if (t
== IRT_FLOAT
|| t
== IRT_U32
) {
917 tr
= emitconv(tr
, IRT_NUM
, t
, 0);
918 } else if (t
== IRT_I8
|| t
== IRT_I16
) {
919 tr
= emitconv(tr
, IRT_INT
, t
, IRCONV_SEXT
);
920 } else if (t
== IRT_U8
|| t
== IRT_U16
) {
921 tr
= emitconv(tr
, IRT_INT
, t
, 0);
930 void LJ_FASTCALL
recff_cdata_call(jit_State
*J
, RecordFFData
*rd
)
932 CTState
*cts
= ctype_ctsG(J2G(J
));
933 GCcdata
*cd
= argv2cdata(J
, J
->base
[0], &rd
->argv
[0]);
934 CTypeID id
= cd
->ctypeid
;
938 if (id
== CTID_CTYPEID
) {
939 id
= crec_constructor(J
, cd
, J
->base
[0]);
941 } else if (crec_call(J
, rd
, cd
)) {
944 /* Record ctype __call/__new metamethod. */
945 ct
= ctype_raw(cts
, id
);
946 tv
= lj_ctype_meta(cts
, ctype_isptr(ct
->info
) ? ctype_cid(ct
->info
) : id
, mm
);
949 J
->base
[-1] = lj_ir_kfunc(J
, funcV(tv
)) | TREF_FRAME
;
950 rd
->nres
= -1; /* Pending tailcall. */
953 } else if (mm
== MM_new
) {
954 crec_alloc(J
, rd
, id
);
957 /* No metamethod or NYI: non-function metamethods. */
958 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
961 static TRef
crec_arith_int64(jit_State
*J
, TRef
*sp
, CType
**s
, MMS mm
)
963 if (ctype_isnum(s
[0]->info
) && ctype_isnum(s
[1]->info
)) {
970 if (((s
[0]->info
& CTF_UNSIGNED
) && s
[0]->size
== 8) ||
971 ((s
[1]->info
& CTF_UNSIGNED
) && s
[1]->size
== 8)) {
972 dt
= IRT_U64
; id
= CTID_UINT64
;
974 dt
= IRT_I64
; id
= CTID_INT64
;
976 !((s
[0]->info
| s
[1]->info
) & CTF_FP
) &&
977 s
[0]->size
== 4 && s
[1]->size
== 4) { /* Try to narrow comparison. */
978 if (!((s
[0]->info
^ s
[1]->info
) & CTF_UNSIGNED
) ||
979 (tref_isk(sp
[1]) && IR(tref_ref(sp
[1]))->i
>= 0)) {
980 dt
= (s
[0]->info
& CTF_UNSIGNED
) ? IRT_U32
: IRT_INT
;
982 } else if (tref_isk(sp
[0]) && IR(tref_ref(sp
[0]))->i
>= 0) {
983 dt
= (s
[1]->info
& CTF_UNSIGNED
) ? IRT_U32
: IRT_INT
;
988 for (i
= 0; i
< 2; i
++) {
989 IRType st
= tref_type(sp
[i
]);
990 if (st
== IRT_NUM
|| st
== IRT_FLOAT
)
991 sp
[i
] = emitconv(sp
[i
], dt
, st
, IRCONV_TRUNC
|IRCONV_ANY
);
992 else if (!(st
== IRT_I64
|| st
== IRT_U64
))
993 sp
[i
] = emitconv(sp
[i
], dt
, IRT_INT
,
994 (s
[i
]->info
& CTF_UNSIGNED
) ? 0 : IRCONV_SEXT
);
998 /* Assume true comparison. Fixup and emit pending guard later. */
1002 op
= mm
== MM_lt
? IR_LT
: IR_LE
;
1003 if (dt
== IRT_U32
|| dt
== IRT_U64
)
1004 op
+= (IR_ULT
-IR_LT
);
1006 lj_ir_set(J
, IRTG(op
, dt
), sp
[0], sp
[1]);
1007 J
->postproc
= LJ_POST_FIXGUARD
;
1010 tr
= emitir(IRT(mm
+(int)IR_ADD
-(int)MM_add
, dt
), sp
[0], sp
[1]);
1012 return emitir(IRTG(IR_CNEWI
, IRT_CDATA
), lj_ir_kint(J
, id
), tr
);
1017 static TRef
crec_arith_ptr(jit_State
*J
, TRef
*sp
, CType
**s
, MMS mm
)
1019 CTState
*cts
= ctype_ctsG(J2G(J
));
1021 if (ctype_isptr(ctp
->info
) || ctype_isrefarray(ctp
->info
)) {
1022 if ((mm
== MM_sub
|| mm
== MM_eq
|| mm
== MM_lt
|| mm
== MM_le
) &&
1023 (ctype_isptr(s
[1]->info
) || ctype_isrefarray(s
[1]->info
))) {
1024 if (mm
== MM_sub
) { /* Pointer difference. */
1026 CTSize sz
= lj_ctype_size(cts
, ctype_cid(ctp
->info
));
1027 if (sz
== 0 || (sz
& (sz
-1)) != 0)
1028 return 0; /* NYI: integer division. */
1029 tr
= emitir(IRT(IR_SUB
, IRT_INTP
), sp
[0], sp
[1]);
1030 tr
= emitir(IRT(IR_BSAR
, IRT_INTP
), tr
, lj_ir_kint(J
, lj_fls(sz
)));
1032 tr
= emitconv(tr
, IRT_NUM
, IRT_INTP
, 0);
1035 } else { /* Pointer comparison (unsigned). */
1036 /* Assume true comparison. Fixup and emit pending guard later. */
1037 IROp op
= mm
== MM_eq
? IR_EQ
: mm
== MM_lt
? IR_ULT
: IR_ULE
;
1038 lj_ir_set(J
, IRTG(op
, IRT_PTR
), sp
[0], sp
[1]);
1039 J
->postproc
= LJ_POST_FIXGUARD
;
1043 if (!((mm
== MM_add
|| mm
== MM_sub
) && ctype_isnum(s
[1]->info
)))
1045 } else if (mm
== MM_add
&& ctype_isnum(ctp
->info
) &&
1046 (ctype_isptr(s
[1]->info
) || ctype_isrefarray(s
[1]->info
))) {
1047 TRef tr
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = tr
; /* Swap pointer and index. */
1054 IRType t
= tref_type(tr
);
1055 CTSize sz
= lj_ctype_size(cts
, ctype_cid(ctp
->info
));
1058 if (t
== IRT_NUM
|| t
== IRT_FLOAT
)
1059 tr
= emitconv(tr
, IRT_INTP
, t
, IRCONV_TRUNC
|IRCONV_ANY
);
1060 else if (!(t
== IRT_I64
|| t
== IRT_U64
))
1061 tr
= emitconv(tr
, IRT_INTP
, IRT_INT
,
1062 ((t
- IRT_I8
) & 1) ? 0 : IRCONV_SEXT
);
1064 if (!tref_typerange(sp
[1], IRT_I8
, IRT_U32
)) {
1065 tr
= emitconv(tr
, IRT_INTP
, t
,
1066 (t
== IRT_NUM
|| t
== IRT_FLOAT
) ?
1067 IRCONV_TRUNC
|IRCONV_ANY
: 0);
1070 tr
= emitir(IRT(IR_MUL
, IRT_INTP
), tr
, lj_ir_kintp(J
, sz
));
1071 tr
= emitir(IRT(mm
+(int)IR_ADD
-(int)MM_add
, IRT_PTR
), sp
[0], tr
);
1072 id
= lj_ctype_intern(cts
, CTINFO(CT_PTR
, CTALIGN_PTR
|ctype_cid(ctp
->info
)),
1074 return emitir(IRTG(IR_CNEWI
, IRT_CDATA
), lj_ir_kint(J
, id
), tr
);
1078 /* Record ctype arithmetic metamethods. */
1079 static void crec_arith_meta(jit_State
*J
, CTState
*cts
, RecordFFData
*rd
)
1083 if (tviscdata(&rd
->argv
[0])) {
1084 CTypeID id
= argv2cdata(J
, J
->base
[0], &rd
->argv
[0])->ctypeid
;
1085 CType
*ct
= ctype_raw(cts
, id
);
1086 if (ctype_isptr(ct
->info
)) id
= ctype_cid(ct
->info
);
1087 tv
= lj_ctype_meta(cts
, id
, (MMS
)rd
->data
);
1089 if (!tv
&& J
->base
[1] && tviscdata(&rd
->argv
[1])) {
1090 CTypeID id
= argv2cdata(J
, J
->base
[1], &rd
->argv
[1])->ctypeid
;
1091 CType
*ct
= ctype_raw(cts
, id
);
1092 if (ctype_isptr(ct
->info
)) id
= ctype_cid(ct
->info
);
1093 tv
= lj_ctype_meta(cts
, id
, (MMS
)rd
->data
);
1098 J
->base
[-1] = lj_ir_kfunc(J
, funcV(tv
)) | TREF_FRAME
;
1099 rd
->nres
= -1; /* Pending tailcall. */
1101 } /* NYI: non-function metamethods. */
1102 } else if ((MMS
)rd
->data
== MM_eq
) {
1103 J
->base
[0] = TREF_FALSE
;
1106 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
1109 void LJ_FASTCALL
recff_cdata_arith(jit_State
*J
, RecordFFData
*rd
)
1111 CTState
*cts
= ctype_ctsG(J2G(J
));
1115 for (i
= 0; i
< 2; i
++) {
1116 TRef tr
= J
->base
[i
];
1117 CType
*ct
= ctype_get(cts
, CTID_DOUBLE
);
1120 } else if (tref_iscdata(tr
)) {
1121 CTypeID id
= argv2cdata(J
, tr
, &rd
->argv
[i
])->ctypeid
;
1123 ct
= ctype_raw(cts
, id
);
1124 t
= crec_ct2irt(cts
, ct
);
1125 if (ctype_isptr(ct
->info
)) { /* Resolve pointer or reference. */
1126 tr
= emitir(IRT(IR_FLOAD
, t
), tr
, IRFL_CDATA_PTR
);
1127 if (ctype_isref(ct
->info
)) {
1128 ct
= ctype_rawchild(cts
, ct
);
1129 t
= crec_ct2irt(cts
, ct
);
1131 } else if (t
== IRT_I64
|| t
== IRT_U64
) {
1132 tr
= emitir(IRT(IR_FLOAD
, t
), tr
, IRFL_CDATA_INT64
);
1135 } else if (t
== IRT_INT
|| t
== IRT_U32
) {
1136 tr
= emitir(IRT(IR_FLOAD
, t
), tr
, IRFL_CDATA_INT
);
1137 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
1139 } else if (ctype_isfunc(ct
->info
)) {
1140 tr
= emitir(IRT(IR_FLOAD
, IRT_PTR
), tr
, IRFL_CDATA_PTR
);
1142 lj_ctype_intern(cts
, CTINFO(CT_PTR
, CTALIGN_PTR
|id
), CTSIZE_PTR
));
1145 tr
= emitir(IRT(IR_ADD
, IRT_PTR
), tr
, lj_ir_kintp(J
, sizeof(GCcdata
)));
1147 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
1148 if (ctype_isnum(ct
->info
)) {
1149 if (t
== IRT_CDATA
) goto trymeta
;
1150 if (t
== IRT_I64
|| t
== IRT_U64
) lj_needsplit(J
);
1151 tr
= emitir(IRT(IR_XLOAD
, t
), tr
, 0);
1152 } else if (!(ctype_isptr(ct
->info
) || ctype_isrefarray(ct
->info
))) {
1155 } else if (tref_isnil(tr
)) {
1156 tr
= lj_ir_kptr(J
, NULL
);
1157 ct
= ctype_get(cts
, CTID_P_VOID
);
1158 } else if (tref_isinteger(tr
)) {
1159 ct
= ctype_get(cts
, CTID_INT32
);
1160 } else if (tref_isstr(tr
)) {
1161 TRef tr2
= J
->base
[1-i
];
1162 CTypeID id
= argv2cdata(J
, tr2
, &rd
->argv
[1-i
])->ctypeid
;
1163 ct
= ctype_raw(cts
, id
);
1164 if (ctype_isenum(ct
->info
)) { /* Match string against enum constant. */
1165 GCstr
*str
= strV(&rd
->argv
[i
]);
1167 CType
*cct
= lj_ctype_getfield(cts
, ct
, str
, &ofs
);
1168 if (cct
&& ctype_isconstval(cct
->info
)) {
1169 /* Specialize to the name of the enum constant. */
1170 emitir(IRTG(IR_EQ
, IRT_STR
), tr
, lj_ir_kstr(J
, str
));
1171 ct
= ctype_child(cts
, cct
);
1172 tr
= lj_ir_kint(J
, (int32_t)ofs
);
1173 } /* else: interpreter will throw. */
1174 } /* else: interpreter will throw. */
1175 } else if (!tref_isnum(tr
)) {
1184 if ((tr
= crec_arith_int64(J
, sp
, s
, (MMS
)rd
->data
)) ||
1185 (tr
= crec_arith_ptr(J
, sp
, s
, (MMS
)rd
->data
))) {
1187 /* Fixup cdata comparisons, too. Avoids some cdata escapes. */
1188 if (J
->postproc
== LJ_POST_FIXGUARD
&& frame_iscont(J
->L
->base
-1) &&
1189 !irt_isguard(J
->guardemit
)) {
1190 const BCIns
*pc
= frame_contpc(J
->L
->base
-1) - 1;
1191 if (bc_op(*pc
) <= BC_ISNEP
) {
1192 setframe_pc(&J2G(J
)->tmptv
, pc
);
1193 J2G(J
)->tmptv
.u32
.lo
= ((tref_istrue(tr
) ^ bc_op(*pc
)) & 1);
1194 J
->postproc
= LJ_POST_FIXCOMP
;
1199 crec_arith_meta(J
, cts
, rd
);
1204 /* -- C library namespace metamethods ------------------------------------- */
1206 void LJ_FASTCALL
recff_clib_index(jit_State
*J
, RecordFFData
*rd
)
1208 CTState
*cts
= ctype_ctsG(J2G(J
));
1209 if (tref_isudata(J
->base
[0]) && tref_isstr(J
->base
[1]) &&
1210 udataV(&rd
->argv
[0])->udtype
== UDTYPE_FFI_CLIB
) {
1211 CLibrary
*cl
= (CLibrary
*)uddata(udataV(&rd
->argv
[0]));
1212 GCstr
*name
= strV(&rd
->argv
[1]);
1214 CTypeID id
= lj_ctype_getname(cts
, &ct
, name
, CLNS_INDEX
);
1215 cTValue
*tv
= lj_tab_getstr(cl
->cache
, name
);
1216 rd
->nres
= rd
->data
;
1217 if (id
&& tv
&& !tvisnil(tv
)) {
1218 /* Specialize to the symbol name and make the result a constant. */
1219 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[1], lj_ir_kstr(J
, name
));
1220 if (ctype_isconstval(ct
->info
)) {
1221 if (ct
->size
>= 0x80000000u
&&
1222 (ctype_child(cts
, ct
)->info
& CTF_UNSIGNED
))
1223 J
->base
[0] = lj_ir_knum(J
, (lua_Number
)(uint32_t)ct
->size
);
1225 J
->base
[0] = lj_ir_kint(J
, (int32_t)ct
->size
);
1226 } else if (ctype_isextern(ct
->info
)) {
1227 CTypeID sid
= ctype_cid(ct
->info
);
1228 void *sp
= *(void **)cdataptr(cdataV(tv
));
1230 ct
= ctype_raw(cts
, sid
);
1231 if (LJ_64
&& !checkptr32(sp
))
1232 ptr
= lj_ir_kintp(J
, (uintptr_t)sp
);
1234 ptr
= lj_ir_kptr(J
, sp
);
1236 J
->base
[0] = crec_tv_ct(J
, ct
, sid
, ptr
);
1239 crec_ct_tv(J
, ct
, ptr
, J
->base
[2], &rd
->argv
[2]);
1242 J
->base
[0] = lj_ir_kgc(J
, obj2gco(cdataV(tv
)), IRT_CDATA
);
1245 lj_trace_err(J
, LJ_TRERR_NOCACHE
);
1247 } /* else: interpreter will throw. */
1250 /* -- FFI library functions ----------------------------------------------- */
1252 static TRef
crec_toint(jit_State
*J
, CTState
*cts
, TRef sp
, TValue
*sval
)
1254 return crec_ct_tv(J
, ctype_get(cts
, CTID_INT32
), 0, sp
, sval
);
1257 void LJ_FASTCALL
recff_ffi_new(jit_State
*J
, RecordFFData
*rd
)
1259 crec_alloc(J
, rd
, argv2ctype(J
, J
->base
[0], &rd
->argv
[0]));
1262 void LJ_FASTCALL
recff_ffi_errno(jit_State
*J
, RecordFFData
*rd
)
1266 lj_trace_err(J
, LJ_TRERR_NYICALL
);
1267 J
->base
[0] = lj_ir_call(J
, IRCALL_lj_vm_errno
);
1270 void LJ_FASTCALL
recff_ffi_string(jit_State
*J
, RecordFFData
*rd
)
1272 CTState
*cts
= ctype_ctsG(J2G(J
));
1273 TRef tr
= J
->base
[0];
1275 TRef trlen
= J
->base
[1];
1277 trlen
= crec_toint(J
, cts
, trlen
, &rd
->argv
[1]);
1278 tr
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_CVOID
), 0, tr
, &rd
->argv
[0]);
1280 tr
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_CCHAR
), 0, tr
, &rd
->argv
[0]);
1281 trlen
= lj_ir_call(J
, IRCALL_strlen
, tr
);
1283 J
->base
[0] = emitir(IRT(IR_XSNEW
, IRT_STR
), tr
, trlen
);
1284 } /* else: interpreter will throw. */
1287 void LJ_FASTCALL
recff_ffi_copy(jit_State
*J
, RecordFFData
*rd
)
1289 CTState
*cts
= ctype_ctsG(J2G(J
));
1290 TRef trdst
= J
->base
[0], trsrc
= J
->base
[1], trlen
= J
->base
[2];
1291 if (trdst
&& trsrc
&& (trlen
|| tref_isstr(trsrc
))) {
1292 trdst
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_VOID
), 0, trdst
, &rd
->argv
[0]);
1293 trsrc
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_CVOID
), 0, trsrc
, &rd
->argv
[1]);
1295 trlen
= crec_toint(J
, cts
, trlen
, &rd
->argv
[2]);
1297 trlen
= emitir(IRTI(IR_FLOAD
), J
->base
[1], IRFL_STR_LEN
);
1298 trlen
= emitir(IRTI(IR_ADD
), trlen
, lj_ir_kint(J
, 1));
1300 lj_ir_call(J
, IRCALL_memcpy
, trdst
, trsrc
, trlen
);
1301 emitir(IRT(IR_XBAR
, IRT_NIL
), 0, 0);
1303 } /* else: interpreter will throw. */
1306 void LJ_FASTCALL
recff_ffi_fill(jit_State
*J
, RecordFFData
*rd
)
1308 CTState
*cts
= ctype_ctsG(J2G(J
));
1309 TRef tr
= J
->base
[0], trlen
= J
->base
[1], trfill
= J
->base
[2];
1311 tr
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_VOID
), 0, tr
, &rd
->argv
[0]);
1312 trlen
= crec_toint(J
, cts
, trlen
, &rd
->argv
[1]);
1314 trfill
= crec_toint(J
, cts
, trfill
, &rd
->argv
[2]);
1316 trfill
= lj_ir_kint(J
, 0);
1317 lj_ir_call(J
, IRCALL_memset
, tr
, trfill
, trlen
);
1318 emitir(IRT(IR_XBAR
, IRT_NIL
), 0, 0);
1320 } /* else: interpreter will throw. */
1323 void LJ_FASTCALL
recff_ffi_typeof(jit_State
*J
, RecordFFData
*rd
)
1325 if (tref_iscdata(J
->base
[0])) {
1326 TRef trid
= lj_ir_kint(J
, argv2ctype(J
, J
->base
[0], &rd
->argv
[0]));
1327 J
->base
[0] = emitir(IRTG(IR_CNEWI
, IRT_CDATA
),
1328 lj_ir_kint(J
, CTID_CTYPEID
), trid
);
1330 setfuncV(J
->L
, &J
->errinfo
, J
->fn
);
1331 lj_trace_err_info(J
, LJ_TRERR_NYIFFU
);
1335 void LJ_FASTCALL
recff_ffi_istype(jit_State
*J
, RecordFFData
*rd
)
1337 argv2ctype(J
, J
->base
[0], &rd
->argv
[0]);
1338 if (tref_iscdata(J
->base
[1])) {
1339 argv2ctype(J
, J
->base
[1], &rd
->argv
[1]);
1340 J
->postproc
= LJ_POST_FIXBOOL
;
1341 J
->base
[0] = TREF_TRUE
;
1343 J
->base
[0] = TREF_FALSE
;
1347 void LJ_FASTCALL
recff_ffi_abi(jit_State
*J
, RecordFFData
*rd
)
1349 if (tref_isstr(J
->base
[0])) {
1350 /* Specialize to the ABI string to make the boolean result a constant. */
1351 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[0], lj_ir_kstr(J
, strV(&rd
->argv
[0])));
1352 J
->postproc
= LJ_POST_FIXBOOL
;
1353 J
->base
[0] = TREF_TRUE
;
1354 } /* else: interpreter will throw. */
1357 /* -- Miscellaneous library functions ------------------------------------- */
1359 void LJ_FASTCALL
lj_crecord_tonumber(jit_State
*J
, RecordFFData
*rd
)
1361 CTState
*cts
= ctype_ctsG(J2G(J
));
1362 CType
*d
, *ct
= lj_ctype_rawref(cts
, cdataV(&rd
->argv
[0])->ctypeid
);
1363 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
1364 if (ctype_isnum(ct
->info
) || ctype_iscomplex(ct
->info
)) {
1365 if (ctype_isinteger_or_bool(ct
->info
) && ct
->size
<= 4 &&
1366 !(ct
->size
== 4 && (ct
->info
& CTF_UNSIGNED
)))
1367 d
= ctype_get(cts
, CTID_INT32
);
1369 d
= ctype_get(cts
, CTID_DOUBLE
);
1370 J
->base
[0] = crec_ct_tv(J
, d
, 0, J
->base
[0], &rd
->argv
[0]);
1372 J
->base
[0] = TREF_NIL
;