2 ** Trace recorder for C data operations.
3 ** Copyright (C) 2005-2011 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_TYPEID
);
54 emitir(IRTG(IR_EQ
, IRT_INT
), trtypeid
, lj_ir_kint(J
, (int32_t)cd
->typeid));
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
->typeid == CTID_CTYPEID
);
63 id
= *(CTypeID
*)cdataptr(cd
);
64 tr
= emitir(IRT(IR_ADD
, IRT_PTR
), tr
, lj_ir_kintp(J
, sizeof(GCcdata
)));
65 tr
= emitir(IRT(IR_XLOAD
, IRT_INT
), tr
, 0);
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_ctsG(J2G(J
));
81 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
->typeid == 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(CType
*ct
)
115 if (LJ_LIKELY(ctype_isnum(ct
->info
))) {
116 if ((ct
->info
& CTF_FP
)) {
117 if (ct
->size
== sizeof(double))
119 else if (ct
->size
== sizeof(float))
122 uint32_t b
= lj_fls(ct
->size
);
124 return IRT_I8
+ 2*b
+ ((ct
->info
& CTF_UNSIGNED
) ? 1 : 0);
126 } else if (ctype_isptr(ct
->info
)) {
127 return (LJ_64
&& ct
->size
== 8) ? IRT_P64
: IRT_P32
;
128 } else if (ctype_iscomplex(ct
->info
)) {
129 if (ct
->size
== 2*sizeof(double))
131 else if (ct
->size
== 2*sizeof(float))
137 /* Determine whether a passed number or cdata number is non-zero. */
138 static int crec_isnonzero(CType
*s
, void *p
)
144 if ((s
->info
& CTF_FP
)) {
145 if (s
->size
== sizeof(float))
146 return (*(float *)p
!= 0);
148 return (*(double *)p
!= 0);
151 return (*(uint8_t *)p
!= 0);
152 else if (s
->size
== 2)
153 return (*(uint16_t *)p
!= 0);
154 else if (s
->size
== 4)
155 return (*(uint32_t *)p
!= 0);
157 return (*(uint64_t *)p
!= 0);
161 static TRef
crec_ct_ct(jit_State
*J
, CType
*d
, CType
*s
, TRef dp
, TRef sp
,
164 CTSize dsize
= d
->size
, ssize
= s
->size
;
165 CTInfo dinfo
= d
->info
, sinfo
= s
->info
;
166 IRType dt
= crec_ct2irt(d
);
167 IRType st
= crec_ct2irt(s
);
169 if (ctype_type(dinfo
) > CT_MAYCONVERT
|| ctype_type(sinfo
) > CT_MAYCONVERT
)
173 ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and
174 ** numbers up to 8 bytes. Otherwise sp holds a pointer.
177 switch (cconv_idx2(dinfo
, sinfo
)) {
178 /* Destination is a bool. */
180 goto xstore
; /* Source operand is already normalized. */
183 if (st
!= IRT_CDATA
) {
184 /* Specialize to the result of a comparison against 0. */
185 TRef zero
= (st
== IRT_NUM
|| st
== IRT_FLOAT
) ? lj_ir_knum(J
, 0) :
186 (st
== IRT_I64
|| st
== IRT_U64
) ? lj_ir_kint64(J
, 0) :
188 int isnz
= crec_isnonzero(s
, svisnz
);
189 emitir(IRTG(isnz
? IR_NE
: IR_EQ
, st
), sp
, zero
);
190 sp
= lj_ir_kint(J
, isnz
);
195 /* Destination is an integer. */
199 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
200 /* Extend 32 to 64 bit integer. */
201 if (dsize
== 8 && ssize
< 8 && !(LJ_64
&& (sinfo
& CTF_UNSIGNED
)))
202 sp
= emitconv(sp
, dt
, ssize
< 4 ? IRT_INT
: st
,
203 (sinfo
& CTF_UNSIGNED
) ? 0 : IRCONV_SEXT
);
204 else if (dsize
< 8 && ssize
== 8) /* Truncate from 64 bit integer. */
205 sp
= emitconv(sp
, dsize
< 4 ? IRT_INT
: dt
, st
, 0);
206 else if (st
== IRT_INT
)
207 sp
= lj_opt_narrow_toint(J
, sp
);
209 if (dt
== IRT_I64
|| dt
== IRT_U64
) lj_needsplit(J
);
210 if (dp
== 0) return sp
;
211 emitir(IRT(IR_XSTORE
, dt
), dp
, sp
);
214 sp
= emitir(IRT(IR_XLOAD
, st
), sp
, 0); /* Load re. */
217 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
218 sp
= emitconv(sp
, dsize
< 4 ? IRT_INT
: dt
, st
, IRCONV_TRUNC
|IRCONV_ANY
);
222 sinfo
= CTINFO(CT_NUM
, CTF_UNSIGNED
);
227 /* Destination is a floating-point number. */
231 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
232 sp
= emitconv(sp
, dt
, ssize
< 4 ? IRT_INT
: st
, 0);
235 sp
= emitir(IRT(IR_XLOAD
, st
), sp
, 0); /* Load re. */
239 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
240 if (dt
!= st
) sp
= emitconv(sp
, dt
, st
, 0);
243 /* Destination is a complex number. */
247 TRef ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, (dsize
>> 1)));
248 emitir(IRT(IR_XSTORE
, dt
), ptr
, lj_ir_knum(J
, 0));
251 if ((sinfo
& CTF_FP
)) goto conv_F_F
; else goto conv_F_I
;
254 if (dt
== IRT_CDATA
|| st
== IRT_CDATA
) goto err_nyi
;
257 re
= emitir(IRT(IR_XLOAD
, st
), sp
, 0);
258 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, (ssize
>> 1)));
259 im
= emitir(IRT(IR_XLOAD
, st
), ptr
, 0);
261 re
= emitconv(re
, dt
, st
, 0);
262 im
= emitconv(im
, dt
, st
, 0);
264 emitir(IRT(IR_XSTORE
, dt
), dp
, re
);
265 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, (dsize
>> 1)));
266 emitir(IRT(IR_XSTORE
, dt
), ptr
, im
);
270 /* Destination is a vector. */
277 /* Destination is a pointer. */
281 /* There are only 32 bit pointers/addresses on 32 bit machines.
282 ** Also ok on x64, since all 32 bit ops clear the upper part of the reg.
286 if (st
== IRT_CDATA
) goto err_nyi
;
287 if (!LJ_64
&& ssize
== 8) /* Truncate from 64 bit integer. */
288 sp
= emitconv(sp
, IRT_U32
, st
, 0);
291 if (st
== IRT_CDATA
) goto err_nyi
;
292 /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
293 sp
= emitconv(sp
, (LJ_64
&& dsize
== 8) ? IRT_I64
: IRT_U32
,
294 st
, IRCONV_TRUNC
|IRCONV_ANY
);
297 /* Destination is an array. */
301 /* Destination is a struct/union. */
308 lj_trace_err(J
, LJ_TRERR_NYICONV
);
314 /* -- Convert C type to TValue (load) ------------------------------------- */
316 static TRef
crec_tv_ct(jit_State
*J
, CType
*s
, CTypeID sid
, TRef sp
)
318 CTState
*cts
= ctype_ctsG(J2G(J
));
319 CTInfo sinfo
= s
->info
;
320 lua_assert(!ctype_isenum(sinfo
));
321 if (ctype_isnum(sinfo
)) {
322 IRType t
= crec_ct2irt(s
);
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
)) {
341 IRType t
= (LJ_64
&& s
->size
== 8) ? IRT_P64
: IRT_P32
;
342 sp
= emitir(IRT(IR_XLOAD
, t
), sp
, 0);
343 } else if (ctype_isrefarray(sinfo
) || ctype_isstruct(sinfo
)) {
345 sid
= lj_ctype_intern(cts
, CTINFO_REF(sid
), CTSIZE_PTR
); /* Create ref. */
346 } else if (ctype_iscomplex(sinfo
)) { /* Unbox/box complex. */
347 IRType t
= s
->size
== 2*sizeof(double) ? IRT_NUM
: IRT_FLOAT
;
348 ptrdiff_t esz
= (ptrdiff_t)(s
->size
>> 1);
349 TRef ptr
, tr1
, tr2
, dp
;
350 dp
= emitir(IRTG(IR_CNEW
, IRT_CDATA
), lj_ir_kint(J
, sid
), TREF_NIL
);
351 tr1
= emitir(IRT(IR_XLOAD
, t
), sp
, 0);
352 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, esz
));
353 tr2
= emitir(IRT(IR_XLOAD
, t
), ptr
, 0);
354 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, sizeof(GCcdata
)));
355 emitir(IRT(IR_XSTORE
, t
), ptr
, tr1
);
356 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), dp
, lj_ir_kintp(J
, sizeof(GCcdata
)+esz
));
357 emitir(IRT(IR_XSTORE
, t
), ptr
, tr2
);
360 /* NYI: copyval of vectors. */
362 lj_trace_err(J
, LJ_TRERR_NYICONV
);
364 /* Box pointer, ref or 64 bit integer. */
365 return emitir(IRTG(IR_CNEWI
, IRT_CDATA
), lj_ir_kint(J
, sid
), sp
);
368 /* -- Convert TValue to C type (store) ------------------------------------ */
370 static TRef
crec_ct_tv(jit_State
*J
, CType
*d
, TRef dp
, TRef sp
, cTValue
*sval
)
372 CTState
*cts
= ctype_ctsG(J2G(J
));
373 CTypeID sid
= CTID_P_VOID
;
376 if (LJ_LIKELY(tref_isinteger(sp
))) {
378 svisnz
= (void *)(intptr_t)(tvisint(sval
)?(intV(sval
)!=0):!tviszero(sval
));
379 } else if (tref_isnum(sp
)) {
381 svisnz
= (void *)(intptr_t)(tvisint(sval
)?(intV(sval
)!=0):!tviszero(sval
));
382 } else if (tref_isbool(sp
)) {
383 sp
= lj_ir_kint(J
, tref_istrue(sp
) ? 1 : 0);
385 } else if (tref_isnil(sp
)) {
386 sp
= lj_ir_kptr(J
, NULL
);
387 } else if (tref_isudata(sp
)) {
388 sp
= emitir(IRT(IR_ADD
, IRT_P32
), sp
, lj_ir_kint(J
, sizeof(GCudata
)));
389 } else if (tref_isstr(sp
)) {
390 if (ctype_isenum(d
->info
)) { /* Match string against enum constant. */
391 GCstr
*str
= strV(sval
);
393 CType
*cct
= lj_ctype_getfield(cts
, d
, str
, &ofs
);
394 /* Specialize to the name of the enum constant. */
395 emitir(IRTG(IR_EQ
, IRT_STR
), sp
, lj_ir_kstr(J
, str
));
396 if (cct
&& ctype_isconstval(cct
->info
)) {
397 lua_assert(ctype_child(cts
, cct
)->size
== 4);
398 svisnz
= (void *)(intptr_t)(cct
->size
!= 0);
399 sp
= lj_ir_kint(J
, (int32_t)cct
->size
);
400 sid
= ctype_cid(cct
->info
);
401 } /* else: interpreter will throw. */
402 } else if (ctype_isrefarray(d
->info
)) { /* Copy string to array. */
403 lj_trace_err(J
, LJ_TRERR_BADTYPE
); /* NYI */
404 } else { /* Otherwise pass the string data as a const char[]. */
405 sp
= emitir(IRT(IR_STRREF
, IRT_P32
), sp
, lj_ir_kint(J
, 0));
408 } else { /* NYI: tref_istab(sp), tref_islightud(sp). */
409 sid
= argv2cdata(J
, sp
, sval
)->typeid;
410 s
= ctype_raw(cts
, sid
);
411 svisnz
= cdataptr(cdataV(sval
));
412 if (ctype_isptr(s
->info
)) {
413 IRType t
= (LJ_64
&& s
->size
== 8) ? IRT_P64
: IRT_P32
;
414 sp
= emitir(IRT(IR_FLOAD
, t
), sp
, IRFL_CDATA_PTR
);
415 if (ctype_isref(s
->info
)) {
416 svisnz
= *(void **)svisnz
;
417 s
= ctype_rawchild(cts
, s
);
419 goto doconv
; /* The pointer value was loaded, don't load number. */
421 } else if (ctype_isinteger(s
->info
) && s
->size
== 8) {
422 IRType t
= (s
->info
& CTF_UNSIGNED
) ? IRT_U64
: IRT_I64
;
423 sp
= emitir(IRT(IR_FLOAD
, t
), sp
, IRFL_CDATA_INT64
);
427 sp
= emitir(IRT(IR_ADD
, IRT_PTR
), sp
, lj_ir_kintp(J
, sizeof(GCcdata
)));
429 if (ctype_isenum(s
->info
)) s
= ctype_child(cts
, s
);
430 if (ctype_isnum(s
->info
)) { /* Load number value. */
431 IRType t
= crec_ct2irt(s
);
432 if (t
!= IRT_CDATA
) {
433 sp
= emitir(IRT(IR_XLOAD
, t
), sp
, 0);
434 if (t
== IRT_I64
|| t
== IRT_U64
) lj_needsplit(J
);
439 s
= ctype_get(cts
, sid
);
441 if (ctype_isenum(d
->info
)) d
= ctype_child(cts
, d
);
442 return crec_ct_ct(J
, d
, s
, dp
, sp
, svisnz
);
445 /* -- C data metamethods -------------------------------------------------- */
447 /* This would be rather difficult in FOLD, so do it here:
448 ** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k)
449 ** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
451 static TRef
crec_reassoc_ofs(jit_State
*J
, TRef tr
, ptrdiff_t *ofsp
, MSize sz
)
453 IRIns
*ir
= IR(tref_ref(tr
));
454 if (LJ_LIKELY(J
->flags
& JIT_F_OPT_FOLD
) && irref_isk(ir
->op2
) &&
455 (ir
->o
== IR_ADD
|| ir
->o
== IR_ADDOV
|| ir
->o
== IR_SUBOV
)) {
456 IRIns
*irk
= IR(ir
->op2
);
458 if (LJ_64
&& irk
->o
== IR_KINT64
)
459 k
= (ptrdiff_t)ir_kint64(irk
)->u64
* sz
;
461 k
= (ptrdiff_t)irk
->i
* sz
;
462 if (ir
->o
== IR_SUBOV
) *ofsp
-= k
; else *ofsp
+= k
;
463 tr
= ir
->op1
; /* Not a TRef, but the caller doesn't care. */
468 /* Record ctype __index/__newindex metamethods. */
469 static void crec_index_meta(jit_State
*J
, CTState
*cts
, CType
*ct
,
472 CTypeID id
= ctype_typeid(cts
, ct
);
473 cTValue
*tv
= lj_ctype_meta(cts
, id
, rd
->data
? MM_newindex
: MM_index
);
475 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
477 J
->base
[-1] = lj_ir_kfunc(J
, funcV(tv
)) | TREF_FRAME
;
478 rd
->nres
= -1; /* Pending tailcall. */
479 } else if (rd
->data
== 0 && tvistab(tv
) && tref_isstr(J
->base
[1])) {
480 /* Specialize to result of __index lookup. */
481 cTValue
*o
= lj_tab_get(J
->L
, tabV(tv
), &rd
->argv
[1]);
482 IRType t
= itype2irt(o
);
484 J
->base
[0] = lj_ir_kgc(J
, gcV(o
), t
);
486 J
->base
[0] = lj_ir_kint(J
, intV(o
));
488 J
->base
[0] = lj_ir_knumint(J
, numV(o
));
489 else if (tvisbool(o
))
490 J
->base
[0] = TREF_PRI(t
);
492 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
493 /* Always specialize to the key. */
494 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[1], lj_ir_kstr(J
, strV(&rd
->argv
[1])));
496 /* NYI: resolving of non-function metamethods. */
497 /* NYI: non-string keys for __index table. */
498 /* NYI: stores to __newindex table. */
499 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
503 void LJ_FASTCALL
recff_cdata_index(jit_State
*J
, RecordFFData
*rd
)
505 TRef idx
, ptr
= J
->base
[0];
506 ptrdiff_t ofs
= sizeof(GCcdata
);
507 GCcdata
*cd
= argv2cdata(J
, ptr
, &rd
->argv
[0]);
508 CTState
*cts
= ctype_ctsG(J2G(J
));
509 CType
*ct
= ctype_raw(cts
, cd
->typeid);
512 /* Resolve pointer or reference for cdata object. */
513 if (ctype_isptr(ct
->info
)) {
514 IRType t
= (LJ_64
&& ct
->size
== 8) ? IRT_P64
: IRT_P32
;
515 if (ctype_isref(ct
->info
)) ct
= ctype_rawchild(cts
, ct
);
516 ptr
= emitir(IRT(IR_FLOAD
, t
), ptr
, IRFL_CDATA_PTR
);
518 ptr
= crec_reassoc_ofs(J
, ptr
, &ofs
, 1);
523 if (tref_isnumber(idx
)) {
524 idx
= lj_opt_narrow_cindex(J
, idx
);
525 if (ctype_ispointer(ct
->info
)) {
528 if ((ct
->info
& CTF_COMPLEX
))
529 idx
= emitir(IRT(IR_BAND
, IRT_INTP
), idx
, lj_ir_kintp(J
, 1));
530 sz
= lj_ctype_size(cts
, (sid
= ctype_cid(ct
->info
)));
531 idx
= crec_reassoc_ofs(J
, idx
, &ofs
, sz
);
532 #if LJ_TARGET_ARM || LJ_TARGET_PPC
533 /* Hoist base add to allow fusion of index/shift into operands. */
534 if (LJ_LIKELY(J
->flags
& JIT_F_OPT_LOOP
) && ofs
536 && (sz
== 1 || sz
== 4)
539 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), ptr
, lj_ir_kintp(J
, ofs
));
543 idx
= emitir(IRT(IR_MUL
, IRT_INTP
), idx
, lj_ir_kintp(J
, sz
));
544 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), idx
, ptr
);
546 } else if (tref_iscdata(idx
)) {
547 GCcdata
*cdk
= cdataV(&rd
->argv
[1]);
548 CType
*ctk
= ctype_raw(cts
, cdk
->typeid);
550 if (ctype_isenum(ctk
->info
)) ctk
= ctype_child(cts
, ctk
);
551 if (ctype_ispointer(ct
->info
) &&
552 ctype_isinteger(ctk
->info
) && (t
= crec_ct2irt(ctk
)) != IRT_CDATA
) {
553 if (ctk
->size
== 8) {
554 idx
= emitir(IRT(IR_FLOAD
, t
), idx
, IRFL_CDATA_INT64
);
556 idx
= emitir(IRT(IR_ADD
, IRT_PTR
), idx
,
557 lj_ir_kintp(J
, sizeof(GCcdata
)));
558 idx
= emitir(IRT(IR_XLOAD
, t
), idx
, 0);
560 if (LJ_64
&& ctk
->size
< sizeof(intptr_t) && !(ctk
->info
& CTF_UNSIGNED
))
561 idx
= emitconv(idx
, IRT_INTP
, IRT_INT
, IRCONV_SEXT
);
562 if (!LJ_64
&& ctk
->size
> sizeof(intptr_t)) {
563 idx
= emitconv(idx
, IRT_INTP
, t
, 0);
568 } else if (tref_isstr(idx
)) {
569 GCstr
*name
= strV(&rd
->argv
[1]);
570 if (cd
->typeid == CTID_CTYPEID
)
571 ct
= ctype_raw(cts
, crec_constructor(J
, cd
, ptr
));
572 if (ctype_isstruct(ct
->info
)) {
575 fct
= lj_ctype_getfield(cts
, ct
, name
, &fofs
);
577 /* Always specialize to the field name. */
578 emitir(IRTG(IR_EQ
, IRT_STR
), idx
, lj_ir_kstr(J
, name
));
579 if (ctype_isconstval(fct
->info
)) {
580 if (fct
->size
>= 0x80000000u
&&
581 (ctype_child(cts
, fct
)->info
& CTF_UNSIGNED
)) {
582 J
->base
[0] = lj_ir_knum(J
, (lua_Number
)(uint32_t)fct
->size
);
585 J
->base
[0] = lj_ir_kint(J
, (int32_t)fct
->size
);
586 return; /* Interpreter will throw for newindex. */
587 } else if (ctype_isbitfield(fct
->info
)) {
588 lj_trace_err(J
, LJ_TRERR_NYICONV
);
590 lua_assert(ctype_isfield(fct
->info
));
591 sid
= ctype_cid(fct
->info
);
593 ofs
+= (ptrdiff_t)fofs
;
595 } else if (ctype_iscomplex(ct
->info
)) {
596 if (name
->len
== 2 &&
597 ((strdata(name
)[0] == 'r' && strdata(name
)[1] == 'e') ||
598 (strdata(name
)[0] == 'i' && strdata(name
)[1] == 'm'))) {
599 /* Always specialize to the field name. */
600 emitir(IRTG(IR_EQ
, IRT_STR
), idx
, lj_ir_kstr(J
, name
));
601 if (strdata(name
)[0] == 'i') ofs
+= (ct
->size
>> 1);
602 sid
= ctype_cid(ct
->info
);
607 if (ctype_isptr(ct
->info
)) { /* Automatically perform '->'. */
608 CType
*cct
= ctype_rawchild(cts
, ct
);
609 if (ctype_isstruct(cct
->info
)) {
611 if (tref_isstr(idx
)) goto again
;
614 crec_index_meta(J
, cts
, ct
, rd
);
619 ptr
= emitir(IRT(IR_ADD
, IRT_PTR
), ptr
, lj_ir_kintp(J
, ofs
));
621 /* Resolve reference for field. */
622 ct
= ctype_get(cts
, sid
);
623 if (ctype_isref(ct
->info
))
624 ptr
= emitir(IRT(IR_XLOAD
, IRT_PTR
), ptr
, 0);
626 while (ctype_isattrib(ct
->info
))
627 ct
= ctype_child(cts
, ct
); /* Skip attributes. */
629 if (rd
->data
== 0) { /* __index metamethod. */
630 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
); /* Skip enums. */
631 J
->base
[0] = crec_tv_ct(J
, ct
, sid
, ptr
);
632 } else { /* __newindex metamethod. */
635 crec_ct_tv(J
, ct
, ptr
, J
->base
[2], &rd
->argv
[2]);
639 /* Record cdata allocation. */
640 static void crec_alloc(jit_State
*J
, RecordFFData
*rd
, CTypeID id
)
642 CTState
*cts
= ctype_ctsG(J2G(J
));
644 CTInfo info
= lj_ctype_info(cts
, id
, &sz
);
645 CType
*d
= ctype_raw(cts
, id
);
647 if (sz
== 0 || sz
> 64 || (info
& CTF_VLA
) || ctype_align(info
) > CT_MEMALIGN
)
648 lj_trace_err(J
, LJ_TRERR_NYICONV
); /* NYI: large/special allocations. */
649 trid
= lj_ir_kint(J
, id
);
650 /* Use special instruction to box pointer or 64 bit integer. */
651 if (ctype_isptr(info
) || (ctype_isinteger(info
) && sz
== 8)) {
652 TRef sp
= J
->base
[1] ? crec_ct_tv(J
, d
, 0, J
->base
[1], &rd
->argv
[1]) :
653 ctype_isptr(info
) ? lj_ir_kptr(J
, NULL
) :
654 (lj_needsplit(J
), lj_ir_kint64(J
, 0));
655 J
->base
[0] = emitir(IRTG(IR_CNEWI
, IRT_CDATA
), trid
, sp
);
657 TRef trcd
= emitir(IRTG(IR_CNEW
, IRT_CDATA
), trid
, TREF_NIL
);
660 if (J
->base
[1] && !J
->base
[2] && !lj_cconv_multi_init(d
, &rd
->argv
[1])) {
662 } else if (ctype_isarray(d
->info
)) {
663 CType
*dc
= ctype_rawchild(cts
, d
); /* Array element type. */
664 CTSize ofs
, esize
= dc
->size
;
670 if (!(ctype_isnum(dc
->info
) || ctype_isptr(dc
->info
)))
671 lj_trace_err(J
, LJ_TRERR_NYICONV
); /* NYI: init array of aggregates. */
672 for (i
= 1, ofs
= 0; ofs
< sz
; ofs
+= esize
) {
673 TRef dp
= emitir(IRT(IR_ADD
, IRT_PTR
), trcd
,
674 lj_ir_kintp(J
, ofs
+ sizeof(GCcdata
)));
680 sp
= ctype_isnum(dc
->info
) ? lj_ir_kint(J
, 0) : TREF_NIL
;
682 crec_ct_tv(J
, dc
, dp
, sp
, sval
);
684 } else if (ctype_isstruct(d
->info
)) {
685 CTypeID fid
= d
->sib
;
688 CType
*df
= ctype_get(cts
, fid
);
690 if (ctype_isfield(df
->info
)) {
696 if (!gcref(df
->name
)) continue; /* Ignore unnamed fields. */
697 dc
= ctype_rawchild(cts
, df
); /* Field type. */
698 if (!(ctype_isnum(dc
->info
) || ctype_isptr(dc
->info
)))
699 lj_trace_err(J
, LJ_TRERR_NYICONV
); /* NYI: init aggregates. */
705 sp
= ctype_isnum(dc
->info
) ? lj_ir_kint(J
, 0) : TREF_NIL
;
707 dp
= emitir(IRT(IR_ADD
, IRT_PTR
), trcd
,
708 lj_ir_kintp(J
, df
->size
+ sizeof(GCcdata
)));
709 crec_ct_tv(J
, dc
, dp
, sp
, sval
);
710 } else if (!ctype_isconstval(df
->info
)) {
711 /* NYI: init bitfields and sub-structures. */
712 lj_trace_err(J
, LJ_TRERR_NYICONV
);
718 dp
= emitir(IRT(IR_ADD
, IRT_PTR
), trcd
, lj_ir_kintp(J
, sizeof(GCcdata
)));
720 crec_ct_tv(J
, d
, dp
, J
->base
[1], &rd
->argv
[1]);
724 crec_ct_tv(J
, d
, dp
, lj_ir_kint(J
, 0), &tv
);
727 /* Handle __gc metamethod. */
728 fin
= lj_ctype_meta(cts
, id
, MM_gc
);
730 TRef trlo
= lj_ir_call(J
, IRCALL_lj_cdata_setfin
, trcd
);
731 TRef trhi
= emitir(IRT(IR_ADD
, IRT_P32
), trlo
, lj_ir_kint(J
, 4));
732 if (LJ_BE
) { TRef tmp
= trlo
; trlo
= trhi
; trhi
= tmp
; }
734 emitir(IRT(IR_XSTORE
, IRT_P32
), trlo
, lj_ir_kfunc(J
, funcV(fin
)));
735 emitir(IRTI(IR_XSTORE
), trhi
, lj_ir_kint(J
, LJ_TFUNC
));
736 } else if (tviscdata(fin
)) {
737 emitir(IRT(IR_XSTORE
, IRT_P32
), trlo
,
738 lj_ir_kgc(J
, obj2gco(cdataV(fin
)), IRT_CDATA
));
739 emitir(IRTI(IR_XSTORE
), trhi
, lj_ir_kint(J
, LJ_TCDATA
));
741 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
748 /* Record argument conversions. */
749 static TRef
crec_call_args(jit_State
*J
, RecordFFData
*rd
,
750 CTState
*cts
, CType
*ct
)
752 TRef args
[CCI_NARGS_MAX
];
759 TRef
*arg0
= NULL
, *arg1
= NULL
;
762 if (ctype_cconv(ct
->info
) == CTCC_THISCALL
)
764 else if (ctype_cconv(ct
->info
) == CTCC_FASTCALL
)
768 /* Skip initial attributes. */
771 CType
*ctf
= ctype_get(cts
, fid
);
772 if (!ctype_isattrib(ctf
->info
)) break;
776 for (n
= 0, base
= J
->base
+1, o
= rd
->argv
+1; *base
; n
++, base
++, o
++) {
780 if (n
>= CCI_NARGS_MAX
)
781 lj_trace_err(J
, LJ_TRERR_NYICALL
);
783 if (fid
) { /* Get argument type from field. */
784 CType
*ctf
= ctype_get(cts
, fid
);
786 lua_assert(ctype_isfield(ctf
->info
));
787 did
= ctype_cid(ctf
->info
);
789 if (!(ct
->info
& CTF_VARARG
))
790 lj_trace_err(J
, LJ_TRERR_NYICALL
); /* Too many arguments. */
791 did
= lj_ccall_ctid_vararg(cts
, o
); /* Infer vararg type. */
793 d
= ctype_raw(cts
, did
);
794 if (!(ctype_isnum(d
->info
) || ctype_isptr(d
->info
) ||
795 ctype_isenum(d
->info
)))
796 lj_trace_err(J
, LJ_TRERR_NYICALL
);
797 tr
= crec_ct_tv(J
, d
, 0, *base
, o
);
798 if (ctype_isinteger_or_bool(d
->info
)) {
800 if ((d
->info
& CTF_UNSIGNED
))
801 tr
= emitconv(tr
, IRT_INT
, d
->size
==1 ? IRT_U8
: IRT_U16
, 0);
803 tr
= emitconv(tr
, IRT_INT
, d
->size
==1 ? IRT_I8
: IRT_I16
,IRCONV_SEXT
);
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
->typeid);
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(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_isenum(ctr
->info
)) {
890 ctr
= ctype_child(cts
, ctr
);
892 if (!(ctype_isnum(ctr
->info
) || ctype_isptr(ctr
->info
) ||
893 ctype_isvoid(ctr
->info
)) || t
== IRT_CDATA
)
894 lj_trace_err(J
, LJ_TRERR_NYICALL
);
895 if ((ct
->info
& CTF_VARARG
)
897 || ctype_cconv(ct
->info
) != CTCC_CDECL
900 func
= emitir(IRT(IR_CARG
, IRT_NIL
), func
,
901 lj_ir_kint(J
, ctype_typeid(cts
, ct
)));
902 tr
= emitir(IRT(IR_CALLXS
, t
), crec_call_args(J
, rd
, cts
, ct
), func
);
903 if (ctype_isbool(ctr
->info
)) {
905 lj_ir_set(J
, IRTGI(IR_NE
), tr
, lj_ir_kint(J
, 0));
906 J
->postproc
= LJ_POST_FIXGUARDSNAP
;
908 } else if (t
== IRT_FLOAT
|| t
== IRT_U32
) {
909 tr
= emitconv(tr
, IRT_NUM
, t
, 0);
910 } else if (t
== IRT_I8
|| t
== IRT_I16
) {
911 tr
= emitconv(tr
, IRT_INT
, t
, IRCONV_SEXT
);
912 } else if (t
== IRT_U8
|| t
== IRT_U16
) {
913 tr
= emitconv(tr
, IRT_INT
, t
, 0);
914 } else if (t
== IRT_PTR
|| (LJ_64
&& t
== IRT_P32
) ||
915 (t
== IRT_I64
|| t
== IRT_U64
)) {
916 TRef trid
= lj_ir_kint(J
, ctype_cid(ct
->info
));
917 tr
= emitir(IRTG(IR_CNEWI
, IRT_CDATA
), trid
, tr
);
918 if (t
== IRT_I64
|| t
== IRT_U64
) lj_needsplit(J
);
927 /* Record ctype call metamethod. */
928 static void crec_call_meta(jit_State
*J
, RecordFFData
*rd
, CTypeID id
)
930 CTState
*cts
= ctype_ctsG(J2G(J
));
931 CType
*ct
= ctype_raw(cts
, id
);
933 if (ctype_isptr(ct
->info
)) id
= ctype_cid(ct
->info
);
934 tv
= lj_ctype_meta(cts
, id
, MM_call
);
935 if (tv
&& tvisfunc(tv
)) {
936 J
->base
[-1] = lj_ir_kfunc(J
, funcV(tv
)) | TREF_FRAME
;
937 rd
->nres
= -1; /* Pending tailcall. */
939 /* NYI: non-function metamethods. */
940 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
944 void LJ_FASTCALL
recff_cdata_call(jit_State
*J
, RecordFFData
*rd
)
946 GCcdata
*cd
= argv2cdata(J
, J
->base
[0], &rd
->argv
[0]);
947 if (cd
->typeid == CTID_CTYPEID
)
948 crec_alloc(J
, rd
, crec_constructor(J
, cd
, J
->base
[0]));
949 else if (!crec_call(J
, rd
, cd
))
950 crec_call_meta(J
, rd
, cd
->typeid);
953 static TRef
crec_arith_int64(jit_State
*J
, TRef
*sp
, CType
**s
, MMS mm
)
955 if (ctype_isnum(s
[0]->info
) && ctype_isnum(s
[1]->info
)) {
961 if (((s
[0]->info
& CTF_UNSIGNED
) && s
[0]->size
== 8) ||
962 ((s
[1]->info
& CTF_UNSIGNED
) && s
[1]->size
== 8)) {
963 dt
= IRT_U64
; id
= CTID_UINT64
;
965 dt
= IRT_I64
; id
= CTID_INT64
;
967 for (i
= 0; i
< 2; i
++) {
968 IRType st
= tref_type(sp
[i
]);
969 if (st
== IRT_NUM
|| st
== IRT_FLOAT
)
970 sp
[i
] = emitconv(sp
[i
], dt
, st
, IRCONV_TRUNC
|IRCONV_ANY
);
971 else if (!(st
== IRT_I64
|| st
== IRT_U64
))
972 sp
[i
] = emitconv(sp
[i
], dt
, IRT_INT
,
973 ((st
- IRT_I8
) & 1) ? 0 : IRCONV_SEXT
);
976 /* Assume true comparison. Fixup and emit pending guard later. */
981 op
= mm
== MM_lt
? IR_LT
: IR_LE
;
983 op
+= (IR_ULT
-IR_LT
);
985 lj_ir_set(J
, IRTG(op
, dt
), sp
[0], sp
[1]);
986 J
->postproc
= LJ_POST_FIXGUARD
;
989 tr
= emitir(IRT(mm
+(int)IR_ADD
-(int)MM_add
, dt
), sp
[0], sp
[1]);
991 return emitir(IRTG(IR_CNEWI
, IRT_CDATA
), lj_ir_kint(J
, id
), tr
);
996 static TRef
crec_arith_ptr(jit_State
*J
, TRef
*sp
, CType
**s
, MMS mm
)
998 CTState
*cts
= ctype_ctsG(J2G(J
));
1000 if (ctype_isptr(ctp
->info
) || ctype_isrefarray(ctp
->info
)) {
1001 if ((mm
== MM_sub
|| mm
== MM_eq
|| mm
== MM_lt
|| mm
== MM_le
) &&
1002 (ctype_isptr(s
[1]->info
) || ctype_isrefarray(s
[1]->info
))) {
1003 if (mm
== MM_sub
) { /* Pointer difference. */
1005 CTSize sz
= lj_ctype_size(cts
, ctype_cid(ctp
->info
));
1006 if (sz
== 0 || (sz
& (sz
-1)) != 0)
1007 return 0; /* NYI: integer division. */
1008 tr
= emitir(IRT(IR_SUB
, IRT_PTR
), sp
[0], sp
[1]);
1009 tr
= emitir(IRT(IR_BSAR
, IRT_INTP
), tr
, lj_ir_kint(J
, lj_fls(sz
)));
1011 tr
= emitconv(tr
, IRT_NUM
, IRT_INTP
, 0);
1014 } else { /* Pointer comparison (unsigned). */
1015 /* Assume true comparison. Fixup and emit pending guard later. */
1016 IROp op
= mm
== MM_eq
? IR_EQ
: mm
== MM_lt
? IR_ULT
: IR_ULE
;
1017 lj_ir_set(J
, IRTG(op
, IRT_PTR
), sp
[0], sp
[1]);
1018 J
->postproc
= LJ_POST_FIXGUARD
;
1022 if (!((mm
== MM_add
|| mm
== MM_sub
) && ctype_isnum(s
[1]->info
)))
1024 } else if (mm
== MM_add
&& ctype_isnum(ctp
->info
) &&
1025 (ctype_isptr(s
[1]->info
) || ctype_isrefarray(s
[1]->info
))) {
1026 TRef tr
= sp
[0]; sp
[0] = sp
[1]; sp
[1] = tr
; /* Swap pointer and index. */
1033 IRType t
= tref_type(tr
);
1034 CTSize sz
= lj_ctype_size(cts
, ctype_cid(ctp
->info
));
1037 if (t
== IRT_NUM
|| t
== IRT_FLOAT
)
1038 tr
= emitconv(tr
, IRT_INTP
, t
, IRCONV_TRUNC
|IRCONV_ANY
);
1039 else if (!(t
== IRT_I64
|| t
== IRT_U64
))
1040 tr
= emitconv(tr
, IRT_INTP
, IRT_INT
,
1041 ((t
- IRT_I8
) & 1) ? 0 : IRCONV_SEXT
);
1043 if (!tref_typerange(sp
[1], IRT_I8
, IRT_U32
)) {
1044 tr
= emitconv(tr
, IRT_INTP
, t
,
1045 (t
== IRT_NUM
|| t
== IRT_FLOAT
) ?
1046 IRCONV_TRUNC
|IRCONV_ANY
: 0);
1049 tr
= emitir(IRT(IR_MUL
, IRT_INTP
), tr
, lj_ir_kintp(J
, sz
));
1050 tr
= emitir(IRT(mm
+(int)IR_ADD
-(int)MM_add
, IRT_PTR
), sp
[0], tr
);
1051 id
= lj_ctype_intern(cts
, CTINFO(CT_PTR
, CTALIGN_PTR
|ctype_cid(ctp
->info
)),
1053 return emitir(IRTG(IR_CNEWI
, IRT_CDATA
), lj_ir_kint(J
, id
), tr
);
1057 /* Record ctype arithmetic metamethods. */
1058 static void crec_arith_meta(jit_State
*J
, CTState
*cts
, RecordFFData
*rd
)
1062 if (tviscdata(&rd
->argv
[0]))
1063 tv
= lj_ctype_meta(cts
, argv2cdata(J
, J
->base
[0], &rd
->argv
[0])->typeid,
1065 if (!tv
&& J
->base
[1] && tviscdata(&rd
->argv
[1]))
1066 tv
= lj_ctype_meta(cts
, argv2cdata(J
, J
->base
[1], &rd
->argv
[1])->typeid,
1069 if (tv
&& tvisfunc(tv
)) {
1070 J
->base
[-1] = lj_ir_kfunc(J
, funcV(tv
)) | TREF_FRAME
;
1071 rd
->nres
= -1; /* Pending tailcall. */
1073 /* NYI: non-function metamethods. */
1074 lj_trace_err(J
, LJ_TRERR_BADTYPE
);
1078 void LJ_FASTCALL
recff_cdata_arith(jit_State
*J
, RecordFFData
*rd
)
1080 CTState
*cts
= ctype_ctsG(J2G(J
));
1084 for (i
= 0; i
< 2; i
++) {
1085 TRef tr
= J
->base
[i
];
1086 CType
*ct
= ctype_get(cts
, CTID_DOUBLE
);
1089 } else if (tref_iscdata(tr
)) {
1090 CTypeID id
= argv2cdata(J
, tr
, &rd
->argv
[i
])->typeid;
1091 ct
= ctype_raw(cts
, id
);
1092 if (ctype_isptr(ct
->info
)) { /* Resolve pointer or reference. */
1093 IRType t
= (LJ_64
&& ct
->size
== 8) ? IRT_P64
: IRT_P32
;
1094 if (ctype_isref(ct
->info
)) ct
= ctype_rawchild(cts
, ct
);
1095 tr
= emitir(IRT(IR_FLOAD
, t
), tr
, IRFL_CDATA_PTR
);
1096 } else if (ctype_isinteger(ct
->info
) && ct
->size
== 8) {
1097 IRType t
= (ct
->info
& CTF_UNSIGNED
) ? IRT_U64
: IRT_I64
;
1098 tr
= emitir(IRT(IR_FLOAD
, t
), tr
, IRFL_CDATA_INT64
);
1101 } else if (ctype_isfunc(ct
->info
)) {
1102 tr
= emitir(IRT(IR_FLOAD
, IRT_PTR
), tr
, IRFL_CDATA_PTR
);
1104 lj_ctype_intern(cts
, CTINFO(CT_PTR
, CTALIGN_PTR
|id
), CTSIZE_PTR
));
1106 tr
= emitir(IRT(IR_ADD
, IRT_PTR
), tr
, lj_ir_kintp(J
, sizeof(GCcdata
)));
1108 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
1109 if (ctype_isnum(ct
->info
)) {
1110 IRType t
= crec_ct2irt(ct
);
1111 if (t
== IRT_CDATA
) goto trymeta
;
1112 if (t
== IRT_I64
|| t
== IRT_U64
) lj_needsplit(J
);
1113 tr
= emitir(IRT(IR_XLOAD
, t
), tr
, 0);
1114 } else if (!(ctype_isptr(ct
->info
) || ctype_isrefarray(ct
->info
))) {
1117 } else if (tref_isnil(tr
)) {
1118 tr
= lj_ir_kptr(J
, NULL
);
1119 ct
= ctype_get(cts
, CTID_P_VOID
);
1120 } else if (tref_isinteger(tr
)) {
1121 ct
= ctype_get(cts
, CTID_INT32
);
1122 } else if (!tref_isnum(tr
)) {
1131 if ((tr
= crec_arith_int64(J
, sp
, s
, (MMS
)rd
->data
)) ||
1132 (tr
= crec_arith_ptr(J
, sp
, s
, (MMS
)rd
->data
))) {
1134 /* Fixup cdata comparisons, too. Avoids some cdata escapes. */
1135 if (J
->postproc
== LJ_POST_FIXGUARD
&& frame_iscont(J
->L
->base
-1) &&
1136 !irt_isguard(J
->guardemit
)) {
1137 const BCIns
*pc
= frame_contpc(J
->L
->base
-1) - 1;
1138 if (bc_op(*pc
) <= BC_ISNEP
) {
1139 setframe_pc(&J2G(J
)->tmptv
, pc
);
1140 J2G(J
)->tmptv
.u32
.lo
= ((tref_istrue(tr
) ^ bc_op(*pc
)) & 1);
1141 J
->postproc
= LJ_POST_FIXCOMP
;
1146 crec_arith_meta(J
, cts
, rd
);
1151 /* -- C library namespace metamethods ------------------------------------- */
1153 void LJ_FASTCALL
recff_clib_index(jit_State
*J
, RecordFFData
*rd
)
1155 CTState
*cts
= ctype_ctsG(J2G(J
));
1156 if (tref_isudata(J
->base
[0]) && tref_isstr(J
->base
[1]) &&
1157 udataV(&rd
->argv
[0])->udtype
== UDTYPE_FFI_CLIB
) {
1158 CLibrary
*cl
= (CLibrary
*)uddata(udataV(&rd
->argv
[0]));
1159 GCstr
*name
= strV(&rd
->argv
[1]);
1161 CTypeID id
= lj_ctype_getname(cts
, &ct
, name
, CLNS_INDEX
);
1162 cTValue
*tv
= lj_tab_getstr(cl
->cache
, name
);
1163 rd
->nres
= rd
->data
;
1164 if (id
&& tv
&& !tvisnil(tv
)) {
1165 /* Specialize to the symbol name and make the result a constant. */
1166 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[1], lj_ir_kstr(J
, name
));
1167 if (ctype_isconstval(ct
->info
)) {
1168 if (ct
->size
>= 0x80000000u
&&
1169 (ctype_child(cts
, ct
)->info
& CTF_UNSIGNED
))
1170 J
->base
[0] = lj_ir_knum(J
, (lua_Number
)(uint32_t)ct
->size
);
1172 J
->base
[0] = lj_ir_kint(J
, (int32_t)ct
->size
);
1173 } else if (ctype_isextern(ct
->info
)) {
1174 CTypeID sid
= ctype_cid(ct
->info
);
1175 void *sp
= *(void **)cdataptr(cdataV(tv
));
1177 ct
= ctype_raw(cts
, sid
);
1178 if (rd
->data
&& ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
1179 if (LJ_64
&& !checkptr32(sp
))
1180 ptr
= lj_ir_kintp(J
, (uintptr_t)sp
);
1182 ptr
= lj_ir_kptr(J
, sp
);
1184 J
->base
[0] = crec_tv_ct(J
, ct
, sid
, ptr
);
1187 crec_ct_tv(J
, ct
, ptr
, J
->base
[2], &rd
->argv
[2]);
1190 J
->base
[0] = lj_ir_kgc(J
, obj2gco(cdataV(tv
)), IRT_CDATA
);
1193 lj_trace_err(J
, LJ_TRERR_NOCACHE
);
1195 } /* else: interpreter will throw. */
1198 /* -- FFI library functions ----------------------------------------------- */
1200 static TRef
crec_toint(jit_State
*J
, CTState
*cts
, TRef sp
, TValue
*sval
)
1202 return crec_ct_tv(J
, ctype_get(cts
, CTID_INT32
), 0, sp
, sval
);
1205 void LJ_FASTCALL
recff_ffi_new(jit_State
*J
, RecordFFData
*rd
)
1207 crec_alloc(J
, rd
, argv2ctype(J
, J
->base
[0], &rd
->argv
[0]));
1210 void LJ_FASTCALL
recff_ffi_errno(jit_State
*J
, RecordFFData
*rd
)
1214 lj_trace_err(J
, LJ_TRERR_NYICALL
);
1215 J
->base
[0] = lj_ir_call(J
, IRCALL_lj_vm_errno
);
1218 void LJ_FASTCALL
recff_ffi_string(jit_State
*J
, RecordFFData
*rd
)
1220 CTState
*cts
= ctype_ctsG(J2G(J
));
1221 TRef tr
= J
->base
[0];
1223 TRef trlen
= J
->base
[1];
1225 trlen
= crec_toint(J
, cts
, trlen
, &rd
->argv
[1]);
1226 tr
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_CVOID
), 0, tr
, &rd
->argv
[0]);
1228 tr
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_CCHAR
), 0, tr
, &rd
->argv
[0]);
1229 trlen
= lj_ir_call(J
, IRCALL_strlen
, tr
);
1231 J
->base
[0] = emitir(IRT(IR_XSNEW
, IRT_STR
), tr
, trlen
);
1232 } /* else: interpreter will throw. */
1235 void LJ_FASTCALL
recff_ffi_copy(jit_State
*J
, RecordFFData
*rd
)
1237 CTState
*cts
= ctype_ctsG(J2G(J
));
1238 TRef trdst
= J
->base
[0], trsrc
= J
->base
[1], trlen
= J
->base
[2];
1239 if (trdst
&& trsrc
&& (trlen
|| tref_isstr(trsrc
))) {
1240 trdst
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_VOID
), 0, trdst
, &rd
->argv
[0]);
1241 trsrc
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_CVOID
), 0, trsrc
, &rd
->argv
[1]);
1243 trlen
= crec_toint(J
, cts
, trlen
, &rd
->argv
[2]);
1245 trlen
= emitir(IRTI(IR_FLOAD
), J
->base
[1], IRFL_STR_LEN
);
1246 trlen
= emitir(IRTI(IR_ADD
), trlen
, lj_ir_kint(J
, 1));
1248 lj_ir_call(J
, IRCALL_memcpy
, trdst
, trsrc
, trlen
);
1249 emitir(IRT(IR_XBAR
, IRT_NIL
), 0, 0);
1251 } /* else: interpreter will throw. */
1254 void LJ_FASTCALL
recff_ffi_fill(jit_State
*J
, RecordFFData
*rd
)
1256 CTState
*cts
= ctype_ctsG(J2G(J
));
1257 TRef tr
= J
->base
[0], trlen
= J
->base
[1], trfill
= J
->base
[2];
1259 tr
= crec_ct_tv(J
, ctype_get(cts
, CTID_P_VOID
), 0, tr
, &rd
->argv
[0]);
1260 trlen
= crec_toint(J
, cts
, trlen
, &rd
->argv
[1]);
1262 trfill
= crec_toint(J
, cts
, trfill
, &rd
->argv
[2]);
1264 trfill
= lj_ir_kint(J
, 0);
1265 lj_ir_call(J
, IRCALL_memset
, tr
, trfill
, trlen
);
1266 emitir(IRT(IR_XBAR
, IRT_NIL
), 0, 0);
1268 } /* else: interpreter will throw. */
1271 void LJ_FASTCALL
recff_ffi_istype(jit_State
*J
, RecordFFData
*rd
)
1273 argv2ctype(J
, J
->base
[0], &rd
->argv
[0]);
1274 if (tref_iscdata(J
->base
[1])) {
1275 argv2ctype(J
, J
->base
[1], &rd
->argv
[1]);
1276 J
->postproc
= LJ_POST_FIXBOOL
;
1277 J
->base
[0] = TREF_TRUE
;
1279 J
->base
[0] = TREF_FALSE
;
1283 void LJ_FASTCALL
recff_ffi_abi(jit_State
*J
, RecordFFData
*rd
)
1285 if (tref_isstr(J
->base
[0])) {
1286 /* Specialize to the ABI string to make the boolean result a constant. */
1287 emitir(IRTG(IR_EQ
, IRT_STR
), J
->base
[0], lj_ir_kstr(J
, strV(&rd
->argv
[0])));
1288 J
->postproc
= LJ_POST_FIXBOOL
;
1289 J
->base
[0] = TREF_TRUE
;
1290 } /* else: interpreter will throw. */
1293 /* -- Miscellaneous library functions ------------------------------------- */
1295 void LJ_FASTCALL
lj_crecord_tonumber(jit_State
*J
, RecordFFData
*rd
)
1297 CTState
*cts
= ctype_ctsG(J2G(J
));
1298 CType
*d
, *ct
= lj_ctype_rawref(cts
, cdataV(&rd
->argv
[0])->typeid);
1299 if (ctype_isenum(ct
->info
)) ct
= ctype_child(cts
, ct
);
1300 if (ctype_isnum(ct
->info
) || ctype_iscomplex(ct
->info
)) {
1301 if (ctype_isinteger_or_bool(ct
->info
) && ct
->size
<= 4 &&
1302 !(ct
->size
== 4 && (ct
->info
& CTF_UNSIGNED
)))
1303 d
= ctype_get(cts
, CTID_INT32
);
1305 d
= ctype_get(cts
, CTID_DOUBLE
);
1306 J
->base
[0] = crec_ct_tv(J
, d
, 0, J
->base
[0], &rd
->argv
[0]);
1308 J
->base
[0] = TREF_NIL
;