Debian Codegraph @

libs/luajit/LuaJIT-src/src/lj_crecord.c

raw
1 /*
2 ** Trace recorder for C data operations.
3 ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h
4 */
5
6 #define lj_ffrecord_c
7 #define LUA_CORE
8
9 #include "lj_obj.h"
10
11 #if LJ_HASJIT && LJ_HASFFI
12
13 #include "lj_err.h"
14 #include "lj_tab.h"
15 #include "lj_frame.h"
16 #include "lj_ctype.h"
17 #include "lj_cdata.h"
18 #include "lj_cparse.h"
19 #include "lj_cconv.h"
20 #include "lj_carith.h"
21 #include "lj_clib.h"
22 #include "lj_ccall.h"
23 #include "lj_ff.h"
24 #include "lj_ir.h"
25 #include "lj_jit.h"
26 #include "lj_ircall.h"
27 #include "lj_iropt.h"
28 #include "lj_trace.h"
29 #include "lj_record.h"
30 #include "lj_ffrecord.h"
31 #include "lj_snap.h"
32 #include "lj_crecord.h"
33 #include "lj_dispatch.h"
34 #include "lj_strfmt.h"
35 #include "lj_strscan.h"
36
37 /* Some local macros to save typing. Undef'd at the end. */
38 #define IR(ref) (&J->cur.ir[(ref)])
39
40 /* Pass IR on to next optimization in chain (FOLD). */
41 #define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
42
43 #define emitconv(a, dt, st, flags) \
44 emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags))
45
46 /* -- C type checks ------------------------------------------------------- */
47
48 static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o)
49 {
50 GCcdata *cd;
51 TRef trtypeid;
52 if (!tref_iscdata(tr))
53 lj_trace_err(J, LJ_TRERR_BADTYPE);
54 cd = cdataV(o);
55 /* Specialize to the CTypeID. */
56 trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_CTYPEID);
57 emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->ctypeid));
58 return cd;
59 }
60
61 /* Specialize to the CTypeID held by a cdata constructor. */
62 static CTypeID crec_constructor(jit_State *J, GCcdata *cd, TRef tr)
63 {
64 CTypeID id;
65 lj_assertJ(tref_iscdata(tr) && cd->ctypeid == CTID_CTYPEID,
66 "expected CTypeID cdata");
67 id = *(CTypeID *)cdataptr(cd);
68 tr = emitir(IRT(IR_FLOAD, IRT_INT), tr, IRFL_CDATA_INT);
69 emitir(IRTG(IR_EQ, IRT_INT), tr, lj_ir_kint(J, (int32_t)id));
70 return id;
71 }
72
73 static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o)
74 {
75 if (tref_isstr(tr)) {
76 GCstr *s = strV(o);
77 CPState cp;
78 CTypeID oldtop;
79 /* Specialize to the string containing the C type declaration. */
80 emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s));
81 cp.L = J->L;
82 cp.cts = ctype_cts(J->L);
83 oldtop = cp.cts->top;
84 cp.srcname = strdata(s);
85 cp.p = strdata(s);
86 cp.param = NULL;
87 cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
88 if (lj_cparse(&cp) || cp.cts->top > oldtop) /* Avoid new struct defs. */
89 lj_trace_err(J, LJ_TRERR_BADTYPE);
90 return cp.val.id;
91 } else {
92 GCcdata *cd = argv2cdata(J, tr, o);
93 return cd->ctypeid == CTID_CTYPEID ? crec_constructor(J, cd, tr) :
94 cd->ctypeid;
95 }
96 }
97
98 /* Convert CType to IRType (if possible). */
99 static IRType crec_ct2irt(CTState *cts, CType *ct)
100 {
101 if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
102 if (LJ_LIKELY(ctype_isnum(ct->info))) {
103 if ((ct->info & CTF_FP)) {
104 if (ct->size == sizeof(double))
105 return IRT_NUM;
106 else if (ct->size == sizeof(float))
107 return IRT_FLOAT;
108 } else {
109 uint32_t b = lj_fls(ct->size);
110 if (b <= 3)
111 return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0);
112 }
113 } else if (ctype_isptr(ct->info)) {
114 return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
115 } else if (ctype_iscomplex(ct->info)) {
116 if (ct->size == 2*sizeof(double))
117 return IRT_NUM;
118 else if (ct->size == 2*sizeof(float))
119 return IRT_FLOAT;
120 }
121 return IRT_CDATA;
122 }
123
124 /* -- Optimized memory fill and copy -------------------------------------- */
125
126 /* Maximum length and unroll of inlined copy/fill. */
127 #define CREC_COPY_MAXUNROLL 16
128 #define CREC_COPY_MAXLEN 128
129
130 #define CREC_FILL_MAXUNROLL 16
131
132 /* Number of windowed registers used for optimized memory copy. */
133 #if LJ_TARGET_X86
134 #define CREC_COPY_REGWIN 2
135 #elif LJ_TARGET_PPC || LJ_TARGET_MIPS
136 #define CREC_COPY_REGWIN 8
137 #else
138 #define CREC_COPY_REGWIN 4
139 #endif
140
141 /* List of memory offsets for copy/fill. */
142 typedef struct CRecMemList {
143 CTSize ofs; /* Offset in bytes. */
144 IRType tp; /* Type of load/store. */
145 TRef trofs; /* TRef of interned offset. */
146 TRef trval; /* TRef of load value. */
147 } CRecMemList;
148
149 /* Generate copy list for element-wise struct copy. */
150 static MSize crec_copy_struct(CRecMemList *ml, CTState *cts, CType *ct)
151 {
152 CTypeID fid = ct->sib;
153 MSize mlp = 0;
154 while (fid) {
155 CType *df = ctype_get(cts, fid);
156 fid = df->sib;
157 if (ctype_isfield(df->info)) {
158 CType *cct;
159 IRType tp;
160 if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
161 cct = ctype_rawchild(cts, df); /* Field type. */
162 tp = crec_ct2irt(cts, cct);
163 if (tp == IRT_CDATA) return 0; /* NYI: aggregates. */
164 if (mlp >= CREC_COPY_MAXUNROLL) return 0;
165 ml[mlp].ofs = df->size;
166 ml[mlp].tp = tp;
167 mlp++;
168 if (ctype_iscomplex(cct->info)) {
169 if (mlp >= CREC_COPY_MAXUNROLL) return 0;
170 ml[mlp].ofs = df->size + (cct->size >> 1);
171 ml[mlp].tp = tp;
172 mlp++;
173 }
174 } else if (!ctype_isconstval(df->info)) {
175 /* NYI: bitfields and sub-structures. */
176 return 0;
177 }
178 }
179 return mlp;
180 }
181
182 /* Generate unrolled copy list, from highest to lowest step size/alignment. */
183 static MSize crec_copy_unroll(CRecMemList *ml, CTSize len, CTSize step,
184 IRType tp)
185 {
186 CTSize ofs = 0;
187 MSize mlp = 0;
188 if (tp == IRT_CDATA) tp = IRT_U8 + 2*lj_fls(step);
189 do {
190 while (ofs + step <= len) {
191 if (mlp >= CREC_COPY_MAXUNROLL) return 0;
192 ml[mlp].ofs = ofs;
193 ml[mlp].tp = tp;
194 mlp++;
195 ofs += step;
196 }
197 step >>= 1;
198 tp -= 2;
199 } while (ofs < len);
200 return mlp;
201 }
202
203 /*
204 ** Emit copy list with windowed loads/stores.
205 ** LJ_TARGET_UNALIGNED: may emit unaligned loads/stores (not marked as such).
206 */
207 static void crec_copy_emit(jit_State *J, CRecMemList *ml, MSize mlp,
208 TRef trdst, TRef trsrc)
209 {
210 MSize i, j, rwin = 0;
211 for (i = 0, j = 0; i < mlp; ) {
212 TRef trofs = lj_ir_kintp(J, ml[i].ofs);
213 TRef trsptr = emitir(IRT(IR_ADD, IRT_PTR), trsrc, trofs);
214 ml[i].trval = emitir(IRT(IR_XLOAD, ml[i].tp), trsptr, 0);
215 ml[i].trofs = trofs;
216 i++;
217 rwin += (LJ_SOFTFP32 && ml[i].tp == IRT_NUM) ? 2 : 1;
218 if (rwin >= CREC_COPY_REGWIN || i >= mlp) { /* Flush buffered stores. */
219 rwin = 0;
220 for ( ; j < i; j++) {
221 TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, ml[j].trofs);
222 emitir(IRT(IR_XSTORE, ml[j].tp), trdptr, ml[j].trval);
223 }
224 }
225 }
226 }
227
228 /* Optimized memory copy. */
229 static void crec_copy(jit_State *J, TRef trdst, TRef trsrc, TRef trlen,
230 CType *ct)
231 {
232 if (tref_isk(trlen)) { /* Length must be constant. */
233 CRecMemList ml[CREC_COPY_MAXUNROLL];
234 MSize mlp = 0;
235 CTSize step = 1, len = (CTSize)IR(tref_ref(trlen))->i;
236 IRType tp = IRT_CDATA;
237 int needxbar = 0;
238 if (len == 0) return; /* Shortcut. */
239 if (len > CREC_COPY_MAXLEN) goto fallback;
240 if (ct) {
241 CTState *cts = ctype_ctsG(J2G(J));
242 lj_assertJ(ctype_isarray(ct->info) || ctype_isstruct(ct->info),
243 "copy of non-aggregate");
244 if (ctype_isarray(ct->info)) {
245 CType *cct = ctype_rawchild(cts, ct);
246 tp = crec_ct2irt(cts, cct);
247 if (tp == IRT_CDATA) goto rawcopy;
248 step = lj_ir_type_size[tp];
249 lj_assertJ((len & (step-1)) == 0, "copy of fractional size");
250 } else if ((ct->info & CTF_UNION)) {
251 step = (1u << ctype_align(ct->info));
252 goto rawcopy;
253 } else {
254 mlp = crec_copy_struct(ml, cts, ct);
255 goto emitcopy;
256 }
257 } else {
258 rawcopy:
259 needxbar = 1;
260 if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)
261 step = CTSIZE_PTR;
262 }
263 mlp = crec_copy_unroll(ml, len, step, tp);
264 emitcopy:
265 if (mlp) {
266 crec_copy_emit(J, ml, mlp, trdst, trsrc);
267 if (needxbar)
268 emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
269 return;
270 }
271 }
272 fallback:
273 /* Call memcpy. Always needs a barrier to disable alias analysis. */
274 lj_ir_call(J, IRCALL_memcpy, trdst, trsrc, trlen);
275 emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
276 }
277
278 /* Generate unrolled fill list, from highest to lowest step size/alignment. */
279 static MSize crec_fill_unroll(CRecMemList *ml, CTSize len, CTSize step)
280 {
281 CTSize ofs = 0;
282 MSize mlp = 0;
283 IRType tp = IRT_U8 + 2*lj_fls(step);
284 do {
285 while (ofs + step <= len) {
286 if (mlp >= CREC_COPY_MAXUNROLL) return 0;
287 ml[mlp].ofs = ofs;
288 ml[mlp].tp = tp;
289 mlp++;
290 ofs += step;
291 }
292 step >>= 1;
293 tp -= 2;
294 } while (ofs < len);
295 return mlp;
296 }
297
298 /*
299 ** Emit stores for fill list.
300 ** LJ_TARGET_UNALIGNED: may emit unaligned stores (not marked as such).
301 */
302 static void crec_fill_emit(jit_State *J, CRecMemList *ml, MSize mlp,
303 TRef trdst, TRef trfill)
304 {
305 MSize i;
306 for (i = 0; i < mlp; i++) {
307 TRef trofs = lj_ir_kintp(J, ml[i].ofs);
308 TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, trofs);
309 emitir(IRT(IR_XSTORE, ml[i].tp), trdptr, trfill);
310 }
311 }
312
313 /* Optimized memory fill. */
314 static void crec_fill(jit_State *J, TRef trdst, TRef trlen, TRef trfill,
315 CTSize step)
316 {
317 if (tref_isk(trlen)) { /* Length must be constant. */
318 CRecMemList ml[CREC_FILL_MAXUNROLL];
319 MSize mlp;
320 CTSize len = (CTSize)IR(tref_ref(trlen))->i;
321 if (len == 0) return; /* Shortcut. */
322 if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)
323 step = CTSIZE_PTR;
324 if (step * CREC_FILL_MAXUNROLL < len) goto fallback;
325 mlp = crec_fill_unroll(ml, len, step);
326 if (!mlp) goto fallback;
327 if (tref_isk(trfill) || ml[0].tp != IRT_U8)
328 trfill = emitconv(trfill, IRT_INT, IRT_U8, 0);
329 if (ml[0].tp != IRT_U8) { /* Scatter U8 to U16/U32/U64. */
330 if (CTSIZE_PTR == 8 && ml[0].tp == IRT_U64) {
331 if (tref_isk(trfill)) /* Pointless on x64 with zero-extended regs. */
332 trfill = emitconv(trfill, IRT_U64, IRT_U32, 0);
333 trfill = emitir(IRT(IR_MUL, IRT_U64), trfill,
334 lj_ir_kint64(J, U64x(01010101,01010101)));
335 } else {
336 trfill = emitir(IRTI(IR_MUL), trfill,
337 lj_ir_kint(J, ml[0].tp == IRT_U16 ? 0x0101 : 0x01010101));
338 }
339 }
340 crec_fill_emit(J, ml, mlp, trdst, trfill);
341 } else {
342 fallback:
343 /* Call memset. Always needs a barrier to disable alias analysis. */
344 lj_ir_call(J, IRCALL_memset, trdst, trfill, trlen); /* Note: arg order! */
345 }
346 emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
347 }
348
349 /* -- Convert C type to C type -------------------------------------------- */
350
351 /*
352 ** This code mirrors the code in lj_cconv.c. It performs the same steps
353 ** for the trace recorder that lj_cconv.c does for the interpreter.
354 **
355 ** One major difference is that we can get away with much fewer checks
356 ** here. E.g. checks for casts, constness or correct types can often be
357 ** omitted, even if they might fail. The interpreter subsequently throws
358 ** an error, which aborts the trace.
359 **
360 ** All operations are specialized to their C types, so the on-trace
361 ** outcome must be the same as the outcome in the interpreter. If the
362 ** interpreter doesn't throw an error, then the trace is correct, too.
363 ** Care must be taken not to generate invalid (temporary) IR or to
364 ** trigger asserts.
365 */
366
367 /* Determine whether a passed number or cdata number is non-zero. */
368 static int crec_isnonzero(CType *s, void *p)
369 {
370 if (p == (void *)0)
371 return 0;
372 if (p == (void *)1)
373 return 1;
374 if ((s->info & CTF_FP)) {
375 if (s->size == sizeof(float))
376 return (*(float *)p != 0);
377 else
378 return (*(double *)p != 0);
379 } else {
380 if (s->size == 1)
381 return (*(uint8_t *)p != 0);
382 else if (s->size == 2)
383 return (*(uint16_t *)p != 0);
384 else if (s->size == 4)
385 return (*(uint32_t *)p != 0);
386 else
387 return (*(uint64_t *)p != 0);
388 }
389 }
390
391 static TRef crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp,
392 void *svisnz)
393 {
394 IRType dt = crec_ct2irt(ctype_ctsG(J2G(J)), d);
395 IRType st = crec_ct2irt(ctype_ctsG(J2G(J)), s);
396 CTSize dsize = d->size, ssize = s->size;
397 CTInfo dinfo = d->info, sinfo = s->info;
398
399 if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)
400 goto err_conv;
401
402 /*
403 ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and
404 ** numbers up to 8 bytes. Otherwise sp holds a pointer.
405 */
406
407 switch (cconv_idx2(dinfo, sinfo)) {
408 /* Destination is a bool. */
409 case CCX(B, B):
410 goto xstore; /* Source operand is already normalized. */
411 case CCX(B, I):
412 case CCX(B, F):
413 if (st != IRT_CDATA) {
414 /* Specialize to the result of a comparison against 0. */
415 TRef zero = (st == IRT_NUM || st == IRT_FLOAT) ? lj_ir_knum(J, 0) :
416 (st == IRT_I64 || st == IRT_U64) ? lj_ir_kint64(J, 0) :
417 lj_ir_kint(J, 0);
418 int isnz = crec_isnonzero(s, svisnz);
419 emitir(IRTG(isnz ? IR_NE : IR_EQ, st), sp, zero);
420 sp = lj_ir_kint(J, isnz);
421 goto xstore;
422 }
423 goto err_nyi;
424
425 /* Destination is an integer. */
426 case CCX(I, B):
427 case CCX(I, I):
428 conv_I_I:
429 if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
430 /* Extend 32 to 64 bit integer. */
431 if (dsize == 8 && ssize < 8 && !(LJ_64 && (sinfo & CTF_UNSIGNED)))
432 sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st,
433 (sinfo & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);
434 else if (dsize < 8 && ssize == 8) /* Truncate from 64 bit integer. */
435 sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, 0);
436 else if (st == IRT_INT)
437 sp = lj_opt_narrow_toint(J, sp);
438 xstore:
439 if (dt == IRT_I64 || dt == IRT_U64) lj_needsplit(J);
440 if (dp == 0) return sp;
441 emitir(IRT(IR_XSTORE, dt), dp, sp);
442 break;
443 case CCX(I, C):
444 sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */
445 /* fallthrough */
446 case CCX(I, F):
447 if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
448 sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, IRCONV_ANY);
449 goto xstore;
450 case CCX(I, P):
451 case CCX(I, A):
452 sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
453 ssize = CTSIZE_PTR;
454 st = IRT_UINTP;
455 if (((dsize ^ ssize) & 8) == 0) { /* Must insert no-op type conversion. */
456 sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, IRT_PTR, 0);
457 goto xstore;
458 }
459 goto conv_I_I;
460
461 /* Destination is a floating-point number. */
462 case CCX(F, B):
463 case CCX(F, I):
464 conv_F_I:
465 if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
466 sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, 0);
467 goto xstore;
468 case CCX(F, C):
469 sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */
470 /* fallthrough */
471 case CCX(F, F):
472 conv_F_F:
473 if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
474 if (dt != st) sp = emitconv(sp, dt, st, 0);
475 goto xstore;
476
477 /* Destination is a complex number. */
478 case CCX(C, I):
479 case CCX(C, F):
480 { /* Clear im. */
481 TRef ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
482 emitir(IRT(IR_XSTORE, dt), ptr, lj_ir_knum(J, 0));
483 }
484 /* Convert to re. */
485 if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I;
486
487 case CCX(C, C):
488 if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
489 {
490 TRef re, im, ptr;
491 re = emitir(IRT(IR_XLOAD, st), sp, 0);
492 ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, (ssize >> 1)));
493 im = emitir(IRT(IR_XLOAD, st), ptr, 0);
494 if (dt != st) {
495 re = emitconv(re, dt, st, 0);
496 im = emitconv(im, dt, st, 0);
497 }
498 emitir(IRT(IR_XSTORE, dt), dp, re);
499 ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
500 emitir(IRT(IR_XSTORE, dt), ptr, im);
501 }
502 break;
503
504 /* Destination is a vector. */
505 case CCX(V, I):
506 case CCX(V, F):
507 case CCX(V, C):
508 case CCX(V, V):
509 goto err_nyi;
510
511 /* Destination is a pointer. */
512 case CCX(P, P):
513 case CCX(P, A):
514 case CCX(P, S):
515 /* There are only 32 bit pointers/addresses on 32 bit machines.
516 ** Also ok on x64, since all 32 bit ops clear the upper part of the reg.
517 */
518 goto xstore;
519 case CCX(P, I):
520 if (st == IRT_CDATA) goto err_nyi;
521 if (!LJ_64 && ssize == 8) /* Truncate from 64 bit integer. */
522 sp = emitconv(sp, IRT_U32, st, 0);
523 goto xstore;
524 case CCX(P, F):
525 if (st == IRT_CDATA) goto err_nyi;
526 /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
527 sp = emitconv(sp, (LJ_64 && dsize == 8) ? IRT_I64 : IRT_U32,
528 st, IRCONV_ANY);
529 goto xstore;
530
531 /* Destination is an array. */
532 case CCX(A, A):
533 /* Destination is a struct/union. */
534 case CCX(S, S):
535 if (dp == 0) goto err_conv;
536 crec_copy(J, dp, sp, lj_ir_kint(J, dsize), d);
537 break;
538
539 default:
540 err_conv:
541 err_nyi:
542 lj_trace_err(J, LJ_TRERR_NYICONV);
543 break;
544 }
545 return 0;
546 }
547
548 /* -- Convert C type to TValue (load) ------------------------------------- */
549
550 static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp)
551 {
552 CTState *cts = ctype_ctsG(J2G(J));
553 IRType t = crec_ct2irt(cts, s);
554 CTInfo sinfo = s->info;
555 if (ctype_isnum(sinfo)) {
556 TRef tr;
557 if (t == IRT_CDATA)
558 goto err_nyi; /* NYI: copyval of >64 bit integers. */
559 tr = emitir(IRT(IR_XLOAD, t), sp, 0);
560 if (t == IRT_FLOAT || t == IRT_U32) { /* Keep uint32_t/float as numbers. */
561 return emitconv(tr, IRT_NUM, t, 0);
562 } else if (t == IRT_I64 || t == IRT_U64) { /* Box 64 bit integer. */
563 sp = tr;
564 lj_needsplit(J);
565 } else if ((sinfo & CTF_BOOL)) {
566 /* Assume not equal to zero. Fixup and emit pending guard later. */
567 lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
568 J->postproc = LJ_POST_FIXGUARD;
569 return TREF_TRUE;
570 } else {
571 return tr;
572 }
573 } else if (ctype_isptr(sinfo) || ctype_isenum(sinfo)) {
574 sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Box pointers and enums. */
575 } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
576 cts->L = J->L;
577 sid = lj_ctype_intern(cts, CTINFO_REF(sid), CTSIZE_PTR); /* Create ref. */
578 } else if (ctype_iscomplex(sinfo)) { /* Unbox/box complex. */
579 ptrdiff_t esz = (ptrdiff_t)(s->size >> 1);
580 TRef ptr, tr1, tr2, dp;
581 dp = emitir(IRTG(IR_CNEW, IRT_CDATA), lj_ir_kint(J, sid), TREF_NIL);
582 tr1 = emitir(IRT(IR_XLOAD, t), sp, 0);
583 ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, esz));
584 tr2 = emitir(IRT(IR_XLOAD, t), ptr, 0);
585 ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)));
586 emitir(IRT(IR_XSTORE, t), ptr, tr1);
587 ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)+esz));
588 emitir(IRT(IR_XSTORE, t), ptr, tr2);
589 return dp;
590 } else {
591 /* NYI: copyval of vectors. */
592 err_nyi:
593 lj_trace_err(J, LJ_TRERR_NYICONV);
594 }
595 /* Box pointer, ref, enum or 64 bit integer. */
596 return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, sid), sp);
597 }
598
599 /* -- Convert TValue to C type (store) ------------------------------------ */
600
601 static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, cTValue *sval)
602 {
603 CTState *cts = ctype_ctsG(J2G(J));
604 CTypeID sid = CTID_P_VOID;
605 void *svisnz = 0;
606 CType *s;
607 if (LJ_LIKELY(tref_isinteger(sp))) {
608 sid = CTID_INT32;
609 svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));
610 } else if (tref_isnum(sp)) {
611 sid = CTID_DOUBLE;
612 svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));
613 } else if (tref_isbool(sp)) {
614 sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0);
615 sid = CTID_BOOL;
616 } else if (tref_isnil(sp)) {
617 sp = lj_ir_kptr(J, NULL);
618 } else if (tref_isudata(sp)) {
619 GCudata *ud = udataV(sval);
620 if (ud->udtype == UDTYPE_IO_FILE || ud->udtype == UDTYPE_BUFFER) {
621 TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), sp, IRFL_UDATA_UDTYPE);
622 emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, ud->udtype));
623 sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp,
624 ud->udtype == UDTYPE_IO_FILE ? IRFL_UDATA_FILE :
625 IRFL_SBUF_R);
626 } else {
627 sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCudata)));
628 }
629 } else if (tref_isstr(sp)) {
630 if (ctype_isenum(d->info)) { /* Match string against enum constant. */
631 GCstr *str = strV(sval);
632 CTSize ofs;
633 CType *cct = lj_ctype_getfield(cts, d, str, &ofs);
634 /* Specialize to the name of the enum constant. */
635 emitir(IRTG(IR_EQ, IRT_STR), sp, lj_ir_kstr(J, str));
636 if (cct && ctype_isconstval(cct->info)) {
637 lj_assertJ(ctype_child(cts, cct)->size == 4,
638 "only 32 bit const supported"); /* NYI */
639 svisnz = (void *)(intptr_t)(ofs != 0);
640 sp = lj_ir_kint(J, (int32_t)ofs);
641 sid = ctype_cid(cct->info);
642 } /* else: interpreter will throw. */
643 } else if (ctype_isrefarray(d->info)) { /* Copy string to array. */
644 lj_trace_err(J, LJ_TRERR_BADTYPE); /* NYI */
645 } else { /* Otherwise pass the string data as a const char[]. */
646 /* Don't use STRREF. It folds with SNEW, which loses the trailing NUL. */
647 sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCstr)));
648 sid = CTID_A_CCHAR;
649 }
650 } else if (tref_islightud(sp)) {
651 #if LJ_64
652 lj_trace_err(J, LJ_TRERR_NYICONV);
653 #endif
654 } else { /* NYI: tref_istab(sp). */
655 IRType t;
656 sid = argv2cdata(J, sp, sval)->ctypeid;
657 s = ctype_raw(cts, sid);
658 svisnz = cdataptr(cdataV(sval));
659 if (ctype_isfunc(s->info)) {
660 sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR);
661 s = ctype_get(cts, sid);
662 t = IRT_PTR;
663 } else {
664 t = crec_ct2irt(cts, s);
665 }
666 if (ctype_isptr(s->info)) {
667 sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_PTR);
668 if (ctype_isref(s->info)) {
669 svisnz = *(void **)svisnz;
670 s = ctype_rawchild(cts, s);
671 if (ctype_isenum(s->info)) s = ctype_child(cts, s);
672 t = crec_ct2irt(cts, s);
673 } else {
674 goto doconv;
675 }
676 } else if (t == IRT_I64 || t == IRT_U64) {
677 sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64);
678 lj_needsplit(J);
679 goto doconv;
680 } else if (t == IRT_INT || t == IRT_U32) {
681 if (ctype_isenum(s->info)) s = ctype_child(cts, s);
682 sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT);
683 goto doconv;
684 } else {
685 sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata)));
686 }
687 if (ctype_isnum(s->info) && t != IRT_CDATA)
688 sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Load number value. */
689 goto doconv;
690 }
691 s = ctype_get(cts, sid);
692 doconv:
693 if (ctype_isenum(d->info)) d = ctype_child(cts, d);
694 return crec_ct_ct(J, d, s, dp, sp, svisnz);
695 }
696
697 /* -- C data metamethods -------------------------------------------------- */
698
699 /* This would be rather difficult in FOLD, so do it here:
700 ** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k)
701 ** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
702 */
703 static TRef crec_reassoc_ofs(jit_State *J, TRef tr, ptrdiff_t *ofsp, MSize sz)
704 {
705 IRIns *ir = IR(tref_ref(tr));
706 if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && irref_isk(ir->op2) &&
707 (ir->o == IR_ADD || ir->o == IR_ADDOV || ir->o == IR_SUBOV)) {
708 IRIns *irk = IR(ir->op2);
709 ptrdiff_t k;
710 if (LJ_64 && irk->o == IR_KINT64)
711 k = (ptrdiff_t)ir_kint64(irk)->u64 * sz;
712 else
713 k = (ptrdiff_t)irk->i * sz;
714 if (ir->o == IR_SUBOV) *ofsp -= k; else *ofsp += k;
715 tr = ir->op1; /* Not a TRef, but the caller doesn't care. */
716 }
717 return tr;
718 }
719
720 /* Tailcall to function. */
721 static void crec_tailcall(jit_State *J, RecordFFData *rd, cTValue *tv)
722 {
723 TRef kfunc = lj_ir_kfunc(J, funcV(tv));
724 #if LJ_FR2
725 J->base[-2] = kfunc;
726 J->base[-1] = TREF_FRAME;
727 #else
728 J->base[-1] = kfunc | TREF_FRAME;
729 #endif
730 rd->nres = -1; /* Pending tailcall. */
731 }
732
733 /* Record ctype __index/__newindex metamethods. */
734 static void crec_index_meta(jit_State *J, CTState *cts, CType *ct,
735 RecordFFData *rd)
736 {
737 CTypeID id = ctype_typeid(cts, ct);
738 cTValue *tv = lj_ctype_meta(cts, id, rd->data ? MM_newindex : MM_index);
739 if (!tv)
740 lj_trace_err(J, LJ_TRERR_BADTYPE);
741 if (tvisfunc(tv)) {
742 crec_tailcall(J, rd, tv);
743 } else if (rd->data == 0 && tvistab(tv) && tref_isstr(J->base[1])) {
744 /* Specialize to result of __index lookup. */
745 cTValue *o = lj_tab_get(J->L, tabV(tv), &rd->argv[1]);
746 J->base[0] = lj_record_constify(J, o);
747 if (!J->base[0])
748 lj_trace_err(J, LJ_TRERR_BADTYPE);
749 /* Always specialize to the key. */
750 emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1])));
751 } else {
752 /* NYI: resolving of non-function metamethods. */
753 /* NYI: non-string keys for __index table. */
754 /* NYI: stores to __newindex table. */
755 lj_trace_err(J, LJ_TRERR_BADTYPE);
756 }
757 }
758
759 /* Record bitfield load/store. */
760 static void crec_index_bf(jit_State *J, RecordFFData *rd, TRef ptr, CTInfo info)
761 {
762 IRType t = IRT_I8 + 2*lj_fls(ctype_bitcsz(info)) + ((info&CTF_UNSIGNED)?1:0);
763 TRef tr = emitir(IRT(IR_XLOAD, t), ptr, 0);
764 CTSize pos = ctype_bitpos(info), bsz = ctype_bitbsz(info), shift = 32 - bsz;
765 lj_assertJ(t <= IRT_U32, "only 32 bit bitfields supported"); /* NYI */
766 if (rd->data == 0) { /* __index metamethod. */
767 if ((info & CTF_BOOL)) {
768 tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << pos))));
769 /* Assume not equal to zero. Fixup and emit pending guard later. */
770 lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
771 J->postproc = LJ_POST_FIXGUARD;
772 tr = TREF_TRUE;
773 } else if (!(info & CTF_UNSIGNED)) {
774 tr = emitir(IRTI(IR_BSHL), tr, lj_ir_kint(J, shift - pos));
775 tr = emitir(IRTI(IR_BSAR), tr, lj_ir_kint(J, shift));
776 } else {
777 lj_assertJ(bsz < 32, "unexpected full bitfield index");
778 tr = emitir(IRTI(IR_BSHR), tr, lj_ir_kint(J, pos));
779 tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << bsz)-1)));
780 /* We can omit the U32 to NUM conversion, since bsz < 32. */
781 }
782 J->base[0] = tr;
783 } else { /* __newindex metamethod. */
784 CTState *cts = ctype_ctsG(J2G(J));
785 CType *ct = ctype_get(cts,
786 (info & CTF_BOOL) ? CTID_BOOL :
787 (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32);
788 int32_t mask = (int32_t)(((1u << bsz)-1) << pos);
789 TRef sp = crec_ct_tv(J, ct, 0, J->base[2], &rd->argv[2]);
790 sp = emitir(IRTI(IR_BSHL), sp, lj_ir_kint(J, pos));
791 /* Use of the target type avoids forwarding conversions. */
792 sp = emitir(IRT(IR_BAND, t), sp, lj_ir_kint(J, mask));
793 tr = emitir(IRT(IR_BAND, t), tr, lj_ir_kint(J, (int32_t)~mask));
794 tr = emitir(IRT(IR_BOR, t), tr, sp);
795 emitir(IRT(IR_XSTORE, t), ptr, tr);
796 rd->nres = 0;
797 J->needsnap = 1;
798 }
799 }
800
801 void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)
802 {
803 TRef idx, ptr = J->base[0];
804 ptrdiff_t ofs = sizeof(GCcdata);
805 GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]);
806 CTState *cts = ctype_ctsG(J2G(J));
807 CType *ct = ctype_raw(cts, cd->ctypeid);
808 CTypeID sid = 0;
809
810 /* Resolve pointer or reference for cdata object. */
811 if (ctype_isptr(ct->info)) {
812 IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
813 if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
814 ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_PTR);
815 ofs = 0;
816 ptr = crec_reassoc_ofs(J, ptr, &ofs, 1);
817 }
818
819 again:
820 idx = J->base[1];
821 if (tref_isnumber(idx)) {
822 idx = lj_opt_narrow_cindex(J, idx);
823 if (ctype_ispointer(ct->info)) {
824 CTSize sz;
825 integer_key:
826 if ((ct->info & CTF_COMPLEX))
827 idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1));
828 sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info)));
829 idx = crec_reassoc_ofs(J, idx, &ofs, sz);
830 #if LJ_TARGET_ARM || LJ_TARGET_PPC
831 /* Hoist base add to allow fusion of index/shift into operands. */
832 if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs
833 #if LJ_TARGET_ARM
834 && (sz == 1 || sz == 4)
835 #endif
836 ) {
837 ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
838 ofs = 0;
839 }
840 #endif
841 idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz));
842 ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);
843 }
844 } else if (tref_iscdata(idx)) {
845 GCcdata *cdk = cdataV(&rd->argv[1]);
846 CType *ctk = ctype_raw(cts, cdk->ctypeid);
847 IRType t = crec_ct2irt(cts, ctk);
848 if (ctype_ispointer(ct->info) && t >= IRT_I8 && t <= IRT_U64) {
849 if (ctk->size == 8) {
850 idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT64);
851 } else if (ctk->size == 4) {
852 idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT);
853 } else {
854 idx = emitir(IRT(IR_ADD, IRT_PTR), idx,
855 lj_ir_kintp(J, sizeof(GCcdata)));
856 idx = emitir(IRT(IR_XLOAD, t), idx, 0);
857 }
858 if (LJ_64 && ctk->size < sizeof(intptr_t) && !(ctk->info & CTF_UNSIGNED))
859 idx = emitconv(idx, IRT_INTP, IRT_INT, IRCONV_SEXT);
860 if (!LJ_64 && ctk->size > sizeof(intptr_t)) {
861 idx = emitconv(idx, IRT_INTP, t, 0);
862 lj_needsplit(J);
863 }
864 goto integer_key;
865 }
866 } else if (tref_isstr(idx)) {
867 GCstr *name = strV(&rd->argv[1]);
868 if (cd && cd->ctypeid == CTID_CTYPEID)
869 ct = ctype_raw(cts, crec_constructor(J, cd, ptr));
870 if (ctype_isstruct(ct->info)) {
871 CTSize fofs;
872 CType *fct;
873 fct = lj_ctype_getfield(cts, ct, name, &fofs);
874 if (fct) {
875 ofs += (ptrdiff_t)fofs;
876 /* Always specialize to the field name. */
877 emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));
878 if (ctype_isconstval(fct->info)) {
879 if (fct->size >= 0x80000000u &&
880 (ctype_child(cts, fct)->info & CTF_UNSIGNED)) {
881 J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size);
882 return;
883 }
884 J->base[0] = lj_ir_kint(J, (int32_t)fct->size);
885 return; /* Interpreter will throw for newindex. */
886 } else if (ctype_isbitfield(fct->info)) {
887 if (ofs)
888 ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
889 crec_index_bf(J, rd, ptr, fct->info);
890 return;
891 } else {
892 lj_assertJ(ctype_isfield(fct->info), "field expected");
893 sid = ctype_cid(fct->info);
894 }
895 }
896 } else if (ctype_iscomplex(ct->info)) {
897 if (name->len == 2 &&
898 ((strdata(name)[0] == 'r' && strdata(name)[1] == 'e') ||
899 (strdata(name)[0] == 'i' && strdata(name)[1] == 'm'))) {
900 /* Always specialize to the field name. */
901 emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));
902 if (strdata(name)[0] == 'i') ofs += (ct->size >> 1);
903 sid = ctype_cid(ct->info);
904 }
905 }
906 }
907 if (!sid) {
908 if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */
909 CType *cct = ctype_rawchild(cts, ct);
910 if (ctype_isstruct(cct->info)) {
911 ct = cct;
912 cd = NULL;
913 if (tref_isstr(idx)) goto again;
914 }
915 }
916 crec_index_meta(J, cts, ct, rd);
917 return;
918 }
919
920 if (ofs)
921 ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
922
923 /* Resolve reference for field. */
924 ct = ctype_get(cts, sid);
925 if (ctype_isref(ct->info)) {
926 ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0);
927 sid = ctype_cid(ct->info);
928 ct = ctype_get(cts, sid);
929 }
930
931 while (ctype_isattrib(ct->info))
932 ct = ctype_child(cts, ct); /* Skip attributes. */
933
934 if (rd->data == 0) { /* __index metamethod. */
935 J->base[0] = crec_tv_ct(J, ct, sid, ptr);
936 } else { /* __newindex metamethod. */
937 rd->nres = 0;
938 J->needsnap = 1;
939 crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
940 }
941 }
942
943 /* Record setting a finalizer. */
944 static void crec_finalizer(jit_State *J, TRef trcd, TRef trfin, cTValue *fin)
945 {
946 if (tvisgcv(fin)) {
947 if (!trfin) trfin = lj_ir_kptr(J, gcval(fin));
948 } else if (tvisnil(fin)) {
949 trfin = lj_ir_kptr(J, NULL);
950 } else {
951 lj_trace_err(J, LJ_TRERR_BADTYPE);
952 }
953 lj_ir_call(J, IRCALL_lj_cdata_setfin, trcd,
954 trfin, lj_ir_kint(J, (int32_t)itype(fin)));
955 J->needsnap = 1;
956 }
957
958 /* Record cdata allocation. */
959 static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id)
960 {
961 CTState *cts = ctype_ctsG(J2G(J));
962 CTSize sz;
963 CTInfo info = lj_ctype_info(cts, id, &sz);
964 CType *d = ctype_raw(cts, id);
965 TRef trcd, trid = lj_ir_kint(J, id);
966 cTValue *fin;
967 /* Use special instruction to box pointer or 32/64 bit integer. */
968 if (ctype_isptr(info) || (ctype_isinteger(info) && (sz == 4 || sz == 8))) {
969 TRef sp = J->base[1] ? crec_ct_tv(J, d, 0, J->base[1], &rd->argv[1]) :
970 ctype_isptr(info) ? lj_ir_kptr(J, NULL) :
971 sz == 4 ? lj_ir_kint(J, 0) :
972 (lj_needsplit(J), lj_ir_kint64(J, 0));
973 J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, sp);
974 return;
975 } else {
976 TRef trsz = TREF_NIL;
977 if ((info & CTF_VLA)) { /* Calculate VLA/VLS size at runtime. */
978 CTSize sz0, sz1;
979 if (!J->base[1] || J->base[2])
980 lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init VLA/VLS. */
981 trsz = crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0,
982 J->base[1], &rd->argv[1]);
983 sz0 = lj_ctype_vlsize(cts, d, 0);
984 sz1 = lj_ctype_vlsize(cts, d, 1);
985 trsz = emitir(IRTGI(IR_MULOV), trsz, lj_ir_kint(J, (int32_t)(sz1-sz0)));
986 trsz = emitir(IRTGI(IR_ADDOV), trsz, lj_ir_kint(J, (int32_t)sz0));
987 J->base[1] = 0; /* Simplify logic below. */
988 } else if (ctype_align(info) > CT_MEMALIGN) {
989 trsz = lj_ir_kint(J, sz);
990 }
991 trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, trsz);
992 if (sz > 128 || (info & CTF_VLA)) {
993 TRef dp;
994 CTSize align;
995 special: /* Only handle bulk zero-fill for large/VLA/VLS types. */
996 if (J->base[1])
997 lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init large/VLA/VLS types. */
998 dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
999 if (trsz == TREF_NIL) trsz = lj_ir_kint(J, sz);
1000 align = ctype_align(info);
1001 if (align < CT_MEMALIGN) align = CT_MEMALIGN;
1002 crec_fill(J, dp, trsz, lj_ir_kint(J, 0), (1u << align));
1003 } else if (J->base[1] && !J->base[2] &&
1004 !lj_cconv_multi_init(cts, d, &rd->argv[1])) {
1005 goto single_init;
1006 } else if (ctype_isarray(d->info)) {
1007 CType *dc = ctype_rawchild(cts, d); /* Array element type. */
1008 CTSize ofs, esize = dc->size;
1009 TRef sp = 0;
1010 TValue tv;
1011 TValue *sval = &tv;
1012 MSize i;
1013 tv.u64 = 0;
1014 if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)) ||
1015 esize * CREC_FILL_MAXUNROLL < sz)
1016 goto special;
1017 for (i = 1, ofs = 0; ofs < sz; ofs += esize) {
1018 TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
1019 lj_ir_kintp(J, ofs + sizeof(GCcdata)));
1020 if (J->base[i]) {
1021 sp = J->base[i];
1022 sval = &rd->argv[i];
1023 i++;
1024 } else if (i != 2) {
1025 sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL;
1026 }
1027 crec_ct_tv(J, dc, dp, sp, sval);
1028 }
1029 } else if (ctype_isstruct(d->info)) {
1030 CTypeID fid;
1031 MSize i = 1;
1032 if (!J->base[1]) { /* Handle zero-fill of struct-of-NYI. */
1033 fid = d->sib;
1034 while (fid) {
1035 CType *df = ctype_get(cts, fid);
1036 fid = df->sib;
1037 if (ctype_isfield(df->info)) {
1038 CType *dc;
1039 if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
1040 dc = ctype_rawchild(cts, df); /* Field type. */
1041 if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) ||
1042 ctype_isenum(dc->info)))
1043 goto special;
1044 } else if (!ctype_isconstval(df->info)) {
1045 goto special;
1046 }
1047 }
1048 }
1049 fid = d->sib;
1050 while (fid) {
1051 CType *df = ctype_get(cts, fid);
1052 fid = df->sib;
1053 if (ctype_isfield(df->info)) {
1054 CType *dc;
1055 TRef sp, dp;
1056 TValue tv;
1057 TValue *sval = &tv;
1058 setintV(&tv, 0);
1059 if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
1060 dc = ctype_rawchild(cts, df); /* Field type. */
1061 if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) ||
1062 ctype_isenum(dc->info)))
1063 lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init aggregates. */
1064 if (J->base[i]) {
1065 sp = J->base[i];
1066 sval = &rd->argv[i];
1067 i++;
1068 } else {
1069 sp = ctype_isptr(dc->info) ? TREF_NIL : lj_ir_kint(J, 0);
1070 }
1071 dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
1072 lj_ir_kintp(J, df->size + sizeof(GCcdata)));
1073 crec_ct_tv(J, dc, dp, sp, sval);
1074 if ((d->info & CTF_UNION)) {
1075 if (d->size != dc->size) /* NYI: partial init of union. */
1076 lj_trace_err(J, LJ_TRERR_NYICONV);
1077 break;
1078 }
1079 } else if (!ctype_isconstval(df->info)) {
1080 /* NYI: init bitfields and sub-structures. */
1081 lj_trace_err(J, LJ_TRERR_NYICONV);
1082 }
1083 }
1084 } else {
1085 TRef dp;
1086 single_init:
1087 dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
1088 if (J->base[1]) {
1089 crec_ct_tv(J, d, dp, J->base[1], &rd->argv[1]);
1090 } else {
1091 TValue tv;
1092 tv.u64 = 0;
1093 crec_ct_tv(J, d, dp, lj_ir_kint(J, 0), &tv);
1094 }
1095 }
1096 }
1097 J->base[0] = trcd;
1098 /* Handle __gc metamethod. */
1099 fin = lj_ctype_meta(cts, id, MM_gc);
1100 if (fin)
1101 crec_finalizer(J, trcd, 0, fin);
1102 }
1103
1104 /* Record argument conversions. */
1105 static TRef crec_call_args(jit_State *J, RecordFFData *rd,
1106 CTState *cts, CType *ct)
1107 {
1108 TRef args[CCI_NARGS_MAX];
1109 CTypeID fid;
1110 MSize i, n;
1111 TRef tr, *base;
1112 cTValue *o;
1113 #if LJ_TARGET_X86
1114 #if LJ_ABI_WIN
1115 TRef *arg0 = NULL, *arg1 = NULL;
1116 #endif
1117 int ngpr = 0;
1118 if (ctype_cconv(ct->info) == CTCC_THISCALL)
1119 ngpr = 1;
1120 else if (ctype_cconv(ct->info) == CTCC_FASTCALL)
1121 ngpr = 2;
1122 #elif LJ_TARGET_ARM64 && LJ_TARGET_OSX
1123 int ngpr = CCALL_NARG_GPR;
1124 #endif
1125
1126 /* Skip initial attributes. */
1127 fid = ct->sib;
1128 while (fid) {
1129 CType *ctf = ctype_get(cts, fid);
1130 if (!ctype_isattrib(ctf->info)) break;
1131 fid = ctf->sib;
1132 }
1133 args[0] = TREF_NIL;
1134 for (n = 0, base = J->base+1, o = rd->argv+1; *base; n++, base++, o++) {
1135 CTypeID did;
1136 CType *d;
1137
1138 if (n >= CCI_NARGS_MAX)
1139 lj_trace_err(J, LJ_TRERR_NYICALL);
1140
1141 if (fid) { /* Get argument type from field. */
1142 CType *ctf = ctype_get(cts, fid);
1143 fid = ctf->sib;
1144 lj_assertJ(ctype_isfield(ctf->info), "field expected");
1145 did = ctype_cid(ctf->info);
1146 } else {
1147 if (!(ct->info & CTF_VARARG))
1148 lj_trace_err(J, LJ_TRERR_NYICALL); /* Too many arguments. */
1149 #if LJ_TARGET_ARM64 && LJ_TARGET_OSX
1150 if (ngpr >= 0) {
1151 ngpr = -1;
1152 args[n++] = TREF_NIL; /* Marker for start of varargs. */
1153 if (n >= CCI_NARGS_MAX)
1154 lj_trace_err(J, LJ_TRERR_NYICALL);
1155 }
1156 #endif
1157 did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */
1158 }
1159 d = ctype_raw(cts, did);
1160 if (!(ctype_isnum(d->info) || ctype_isptr(d->info) ||
1161 ctype_isenum(d->info)))
1162 lj_trace_err(J, LJ_TRERR_NYICALL);
1163 tr = crec_ct_tv(J, d, 0, *base, o);
1164 if (ctype_isinteger_or_bool(d->info)) {
1165 #if LJ_TARGET_ARM64 && LJ_TARGET_OSX
1166 if (!ngpr) {
1167 /* Fixed args passed on the stack use their unpromoted size. */
1168 if (d->size != lj_ir_type_size[tref_type(tr)]) {
1169 lj_assertJ(d->size == 1 || d->size==2, "unexpected size %d", d->size);
1170 tr = emitconv(tr, d->size==1 ? IRT_U8 : IRT_U16, tref_type(tr), 0);
1171 }
1172 } else
1173 #endif
1174 if (d->size < 4) {
1175 if ((d->info & CTF_UNSIGNED))
1176 tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_U8 : IRT_U16, 0);
1177 else
1178 tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_I8 : IRT_I16,IRCONV_SEXT);
1179 }
1180 } else if (LJ_SOFTFP32 && ctype_isfp(d->info) && d->size > 4) {
1181 lj_needsplit(J);
1182 }
1183 #if LJ_TARGET_X86
1184 /* 64 bit args must not end up in registers for fastcall/thiscall. */
1185 #if LJ_ABI_WIN
1186 if (!ctype_isfp(d->info)) {
1187 /* Sigh, the Windows/x86 ABI allows reordering across 64 bit args. */
1188 if (tref_typerange(tr, IRT_I64, IRT_U64)) {
1189 if (ngpr) {
1190 arg0 = &args[n]; args[n++] = TREF_NIL; ngpr--;
1191 if (ngpr) {
1192 arg1 = &args[n]; args[n++] = TREF_NIL; ngpr--;
1193 }
1194 }
1195 } else {
1196 if (arg0) { *arg0 = tr; arg0 = NULL; n--; continue; }
1197 if (arg1) { *arg1 = tr; arg1 = NULL; n--; continue; }
1198 if (ngpr) ngpr--;
1199 }
1200 }
1201 #else
1202 if (!ctype_isfp(d->info) && ngpr) {
1203 if (tref_typerange(tr, IRT_I64, IRT_U64)) {
1204 /* No reordering for other x86 ABIs. Simply add alignment args. */
1205 do { args[n++] = TREF_NIL; } while (--ngpr);
1206 } else {
1207 ngpr--;
1208 }
1209 }
1210 #endif
1211 #elif LJ_TARGET_ARM64 && LJ_TARGET_OSX
1212 if (!ctype_isfp(d->info) && ngpr) {
1213 ngpr--;
1214 }
1215 #endif
1216 args[n] = tr;
1217 }
1218 tr = args[0];
1219 for (i = 1; i < n; i++)
1220 tr = emitir(IRT(IR_CARG, IRT_NIL), tr, args[i]);
1221 return tr;
1222 }
1223
1224 /* Create a snapshot for the caller, simulating a 'false' return value. */
1225 static void crec_snap_caller(jit_State *J)
1226 {
1227 lua_State *L = J->L;
1228 TValue *base = L->base, *top = L->top;
1229 const BCIns *pc = J->pc;
1230 TRef ftr = J->base[-1-LJ_FR2];
1231 ptrdiff_t delta;
1232 if (!frame_islua(base-1) || J->framedepth <= 0)
1233 lj_trace_err(J, LJ_TRERR_NYICALL);
1234 J->pc = frame_pc(base-1); delta = 1+LJ_FR2+bc_a(J->pc[-1]);
1235 L->top = base; L->base = base - delta;
1236 J->base[-1-LJ_FR2] = TREF_FALSE;
1237 J->base -= delta; J->baseslot -= (BCReg)delta;
1238 J->maxslot = (BCReg)delta-LJ_FR2; J->framedepth--;
1239 lj_snap_add(J);
1240 L->base = base; L->top = top;
1241 J->framedepth++; J->maxslot = 1;
1242 J->base += delta; J->baseslot += (BCReg)delta;
1243 J->base[-1-LJ_FR2] = ftr; J->pc = pc;
1244 }
1245
1246 /* Record function call. */
1247 static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd)
1248 {
1249 CTState *cts = ctype_ctsG(J2G(J));
1250 CType *ct = ctype_raw(cts, cd->ctypeid);
1251 IRType tp = IRT_PTR;
1252 if (ctype_isptr(ct->info)) {
1253 tp = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
1254 ct = ctype_rawchild(cts, ct);
1255 }
1256 if (ctype_isfunc(ct->info)) {
1257 TRef func = emitir(IRT(IR_FLOAD, tp), J->base[0], IRFL_CDATA_PTR);
1258 CType *ctr = ctype_rawchild(cts, ct);
1259 IRType t = crec_ct2irt(cts, ctr);
1260 TRef tr;
1261 TValue tv;
1262 /* Check for blacklisted C functions that might call a callback. */
1263 tv.u64 = ((uintptr_t)cdata_getptr(cdataptr(cd), (LJ_64 && tp == IRT_P64) ? 8 : 4) >> 2) | U64x(800000000, 00000000);
1264 if (tvistrue(lj_tab_get(J->L, cts->miscmap, &tv)))
1265 lj_trace_err(J, LJ_TRERR_BLACKL);
1266 if (ctype_isvoid(ctr->info)) {
1267 t = IRT_NIL;
1268 rd->nres = 0;
1269 } else if (!(ctype_isnum(ctr->info) || ctype_isptr(ctr->info) ||
1270 ctype_isenum(ctr->info)) || t == IRT_CDATA) {
1271 lj_trace_err(J, LJ_TRERR_NYICALL);
1272 }
1273 if ((ct->info & CTF_VARARG)
1274 #if LJ_TARGET_X86
1275 || ctype_cconv(ct->info) != CTCC_CDECL
1276 #endif
1277 )
1278 func = emitir(IRT(IR_CARG, IRT_NIL), func,
1279 lj_ir_kint(J, ctype_typeid(cts, ct)));
1280 tr = emitir(IRT(IR_CALLXS, t), crec_call_args(J, rd, cts, ct), func);
1281 if (ctype_isbool(ctr->info)) {
1282 if (frame_islua(J->L->base-1) && bc_b(frame_pc(J->L->base-1)[-1]) == 1) {
1283 /* Don't check result if ignored. */
1284 tr = TREF_NIL;
1285 } else {
1286 crec_snap_caller(J);
1287 #if LJ_TARGET_X86ORX64
1288 /* Note: only the x86/x64 backend supports U8 and only for EQ(tr, 0). */
1289 lj_ir_set(J, IRTG(IR_NE, IRT_U8), tr, lj_ir_kint(J, 0));
1290 #else
1291 lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
1292 #endif
1293 J->postproc = LJ_POST_FIXGUARDSNAP;
1294 tr = TREF_TRUE;
1295 }
1296 } else if (t == IRT_PTR || (LJ_64 && t == IRT_P32) ||
1297 t == IRT_I64 || t == IRT_U64 || ctype_isenum(ctr->info)) {
1298 TRef trid = lj_ir_kint(J, ctype_cid(ct->info));
1299 tr = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, tr);
1300 if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
1301 } else if (t == IRT_FLOAT || t == IRT_U32) {
1302 tr = emitconv(tr, IRT_NUM, t, 0);
1303 } else if (t == IRT_I8 || t == IRT_I16) {
1304 tr = emitconv(tr, IRT_INT, t, IRCONV_SEXT);
1305 } else if (t == IRT_U8 || t == IRT_U16) {
1306 tr = emitconv(tr, IRT_INT, t, 0);
1307 }
1308 J->base[0] = tr;
1309 J->needsnap = 1;
1310 return 1;
1311 }
1312 return 0;
1313 }
1314
1315 void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd)
1316 {
1317 CTState *cts = ctype_ctsG(J2G(J));
1318 GCcdata *cd = argv2cdata(J, J->base[0], &rd->argv[0]);
1319 CTypeID id = cd->ctypeid;
1320 CType *ct;
1321 cTValue *tv;
1322 MMS mm = MM_call;
1323 if (id == CTID_CTYPEID) {
1324 id = crec_constructor(J, cd, J->base[0]);
1325 mm = MM_new;
1326 } else if (crec_call(J, rd, cd)) {
1327 return;
1328 }
1329 /* Record ctype __call/__new metamethod. */
1330 ct = ctype_raw(cts, id);
1331 tv = lj_ctype_meta(cts, ctype_isptr(ct->info) ? ctype_cid(ct->info) : id, mm);
1332 if (tv) {
1333 if (tvisfunc(tv)) {
1334 crec_tailcall(J, rd, tv);
1335 return;
1336 }
1337 } else if (mm == MM_new) {
1338 crec_alloc(J, rd, id);
1339 return;
1340 }
1341 /* No metamethod or NYI: non-function metamethods. */
1342 lj_trace_err(J, LJ_TRERR_BADTYPE);
1343 }
1344
1345 static TRef crec_arith_int64(jit_State *J, TRef *sp, CType **s, MMS mm)
1346 {
1347 if (sp[0] && sp[1] && ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) {
1348 IRType dt;
1349 CTypeID id;
1350 TRef tr;
1351 MSize i;
1352 IROp op;
1353 lj_needsplit(J);
1354 if (((s[0]->info & CTF_UNSIGNED) && s[0]->size == 8) ||
1355 ((s[1]->info & CTF_UNSIGNED) && s[1]->size == 8)) {
1356 dt = IRT_U64; id = CTID_UINT64;
1357 } else {
1358 dt = IRT_I64; id = CTID_INT64;
1359 if (mm < MM_add &&
1360 !((s[0]->info | s[1]->info) & CTF_FP) &&
1361 s[0]->size == 4 && s[1]->size == 4) { /* Try to narrow comparison. */
1362 if (!((s[0]->info ^ s[1]->info) & CTF_UNSIGNED) ||
1363 (tref_isk(sp[1]) && IR(tref_ref(sp[1]))->i >= 0)) {
1364 dt = (s[0]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT;
1365 goto comp;
1366 } else if (tref_isk(sp[0]) && IR(tref_ref(sp[0]))->i >= 0) {
1367 dt = (s[1]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT;
1368 goto comp;
1369 }
1370 }
1371 }
1372 for (i = 0; i < 2; i++) {
1373 IRType st = tref_type(sp[i]);
1374 if (st == IRT_NUM || st == IRT_FLOAT)
1375 sp[i] = emitconv(sp[i], dt, st, IRCONV_ANY);
1376 else if (!(st == IRT_I64 || st == IRT_U64))
1377 sp[i] = emitconv(sp[i], dt, IRT_INT,
1378 (s[i]->info & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);
1379 }
1380 if (mm < MM_add) {
1381 comp:
1382 /* Assume true comparison. Fixup and emit pending guard later. */
1383 if (mm == MM_eq) {
1384 op = IR_EQ;
1385 } else {
1386 op = mm == MM_lt ? IR_LT : IR_LE;
1387 if (dt == IRT_U32 || dt == IRT_U64)
1388 op += (IR_ULT-IR_LT);
1389 }
1390 lj_ir_set(J, IRTG(op, dt), sp[0], sp[1]);
1391 J->postproc = LJ_POST_FIXGUARD;
1392 return TREF_TRUE;
1393 } else {
1394 tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, dt), sp[0], sp[1]);
1395 }
1396 return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
1397 }
1398 return 0;
1399 }
1400
1401 static TRef crec_arith_ptr(jit_State *J, TRef *sp, CType **s, MMS mm)
1402 {
1403 CTState *cts = ctype_ctsG(J2G(J));
1404 CType *ctp = s[0];
1405 if (!(sp[0] && sp[1])) return 0;
1406 if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
1407 if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
1408 (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {
1409 if (mm == MM_sub) { /* Pointer difference. */
1410 TRef tr;
1411 CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));
1412 if (sz == 0 || (sz & (sz-1)) != 0)
1413 return 0; /* NYI: integer division. */
1414 tr = emitir(IRT(IR_SUB, IRT_INTP), sp[0], sp[1]);
1415 tr = emitir(IRT(IR_BSAR, IRT_INTP), tr, lj_ir_kint(J, lj_fls(sz)));
1416 #if LJ_64
1417 tr = emitconv(tr, IRT_NUM, IRT_INTP, 0);
1418 #endif
1419 return tr;
1420 } else { /* Pointer comparison (unsigned). */
1421 /* Assume true comparison. Fixup and emit pending guard later. */
1422 IROp op = mm == MM_eq ? IR_EQ : mm == MM_lt ? IR_ULT : IR_ULE;
1423 lj_ir_set(J, IRTG(op, IRT_PTR), sp[0], sp[1]);
1424 J->postproc = LJ_POST_FIXGUARD;
1425 return TREF_TRUE;
1426 }
1427 }
1428 if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(s[1]->info)))
1429 return 0;
1430 } else if (mm == MM_add && ctype_isnum(ctp->info) &&
1431 (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {
1432 TRef tr = sp[0]; sp[0] = sp[1]; sp[1] = tr; /* Swap pointer and index. */
1433 ctp = s[1];
1434 } else {
1435 return 0;
1436 }
1437 {
1438 TRef tr = sp[1];
1439 IRType t = tref_type(tr);
1440 CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));
1441 CTypeID id;
1442 #if LJ_64
1443 if (t == IRT_NUM || t == IRT_FLOAT)
1444 tr = emitconv(tr, IRT_INTP, t, IRCONV_ANY);
1445 else if (!(t == IRT_I64 || t == IRT_U64))
1446 tr = emitconv(tr, IRT_INTP, IRT_INT,
1447 ((t - IRT_I8) & 1) ? 0 : IRCONV_SEXT);
1448 #else
1449 if (!tref_typerange(sp[1], IRT_I8, IRT_U32)) {
1450 tr = emitconv(tr, IRT_INTP, t,
1451 (t == IRT_NUM || t == IRT_FLOAT) ? IRCONV_ANY : 0);
1452 }
1453 #endif
1454 tr = emitir(IRT(IR_MUL, IRT_INTP), tr, lj_ir_kintp(J, sz));
1455 tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, IRT_PTR), sp[0], tr);
1456 id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
1457 CTSIZE_PTR);
1458 return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
1459 }
1460 }
1461
1462 /* Record ctype arithmetic metamethods. */
1463 static TRef crec_arith_meta(jit_State *J, TRef *sp, CType **s, CTState *cts,
1464 RecordFFData *rd)
1465 {
1466 cTValue *tv = NULL;
1467 if (J->base[0]) {
1468 if (tviscdata(&rd->argv[0])) {
1469 CTypeID id = argv2cdata(J, J->base[0], &rd->argv[0])->ctypeid;
1470 CType *ct = ctype_raw(cts, id);
1471 if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
1472 tv = lj_ctype_meta(cts, id, (MMS)rd->data);
1473 }
1474 if (!tv && J->base[1] && tviscdata(&rd->argv[1])) {
1475 CTypeID id = argv2cdata(J, J->base[1], &rd->argv[1])->ctypeid;
1476 CType *ct = ctype_raw(cts, id);
1477 if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
1478 tv = lj_ctype_meta(cts, id, (MMS)rd->data);
1479 }
1480 }
1481 if (tv) {
1482 if (tvisfunc(tv)) {
1483 crec_tailcall(J, rd, tv);
1484 return 0;
1485 } /* NYI: non-function metamethods. */
1486 } else if ((MMS)rd->data == MM_eq) { /* Fallback cdata pointer comparison. */
1487 if (sp[0] && sp[1] && ctype_isnum(s[0]->info) == ctype_isnum(s[1]->info)) {
1488 /* Assume true comparison. Fixup and emit pending guard later. */
1489 lj_ir_set(J, IRTG(IR_EQ, IRT_PTR), sp[0], sp[1]);
1490 J->postproc = LJ_POST_FIXGUARD;
1491 return TREF_TRUE;
1492 } else {
1493 return TREF_FALSE;
1494 }
1495 }
1496 lj_trace_err(J, LJ_TRERR_BADTYPE);
1497 return 0;
1498 }
1499
1500 void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd)
1501 {
1502 CTState *cts = ctype_cts(J->L);
1503 MMS mm = (MMS)rd->data;
1504 TRef sp[2];
1505 CType *s[2];
1506 MSize i;
1507 for (i = 0; i < 2; i++) {
1508 TRef tr = J->base[i];
1509 CType *ct = ctype_get(cts, CTID_DOUBLE);
1510 if (!tr) {
1511 lj_trace_err(J, LJ_TRERR_BADTYPE);
1512 } else if (tref_iscdata(tr)) {
1513 CTypeID id = argv2cdata(J, tr, &rd->argv[i])->ctypeid;
1514 IRType t;
1515 ct = ctype_raw(cts, id);
1516 t = crec_ct2irt(cts, ct);
1517 if (ctype_isptr(ct->info)) { /* Resolve pointer or reference. */
1518 tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_PTR);
1519 if (ctype_isref(ct->info)) {
1520 ct = ctype_rawchild(cts, ct);
1521 t = crec_ct2irt(cts, ct);
1522 }
1523 } else if (t == IRT_I64 || t == IRT_U64) {
1524 tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT64);
1525 lj_needsplit(J);
1526 goto ok;
1527 } else if (t == IRT_INT || t == IRT_U32) {
1528 tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT);
1529 if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
1530 goto ok;
1531 } else if (ctype_isfunc(ct->info)) {
1532 CTypeID id0 = i ? ctype_typeid(cts, s[0]) : 0;
1533 tr = emitir(IRT(IR_FLOAD, IRT_PTR), tr, IRFL_CDATA_PTR);
1534 ct = ctype_get(cts,
1535 lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR));
1536 if (i) {
1537 s[0] = ctype_get(cts, id0); /* cts->tab may have been reallocated. */
1538 }
1539 goto ok;
1540 } else {
1541 tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata)));
1542 }
1543 if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
1544 if (ctype_isnum(ct->info)) {
1545 if (t == IRT_CDATA) {
1546 tr = 0;
1547 } else {
1548 if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
1549 tr = emitir(IRT(IR_XLOAD, t), tr, 0);
1550 }
1551 }
1552 } else if (tref_isnil(tr)) {
1553 if (!(mm == MM_len || mm == MM_eq || mm == MM_lt || mm == MM_le))
1554 lj_trace_err(J, LJ_TRERR_BADTYPE);
1555 tr = lj_ir_kptr(J, NULL);
1556 ct = ctype_get(cts, CTID_P_VOID);
1557 } else if (tref_isinteger(tr)) {
1558 ct = ctype_get(cts, CTID_INT32);
1559 } else if (tref_isstr(tr)) {
1560 TRef tr2 = J->base[1-i];
1561 CTypeID id = argv2cdata(J, tr2, &rd->argv[1-i])->ctypeid;
1562 ct = ctype_raw(cts, id);
1563 if (ctype_isenum(ct->info)) { /* Match string against enum constant. */
1564 GCstr *str = strV(&rd->argv[i]);
1565 CTSize ofs;
1566 CType *cct = lj_ctype_getfield(cts, ct, str, &ofs);
1567 if (cct && ctype_isconstval(cct->info)) {
1568 /* Specialize to the name of the enum constant. */
1569 emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, str));
1570 ct = ctype_child(cts, cct);
1571 tr = lj_ir_kint(J, (int32_t)ofs);
1572 } else { /* Interpreter will throw or return false. */
1573 lj_trace_err(J, LJ_TRERR_BADTYPE);
1574 }
1575 } else if (ctype_isptr(ct->info)) {
1576 tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCstr)));
1577 } else {
1578 lj_trace_err(J, LJ_TRERR_BADTYPE);
1579 }
1580 } else if (!tref_isnum(tr)) {
1581 tr = 0;
1582 ct = ctype_get(cts, CTID_P_VOID);
1583 }
1584 ok:
1585 s[i] = ct;
1586 sp[i] = tr;
1587 }
1588 {
1589 TRef tr;
1590 if ((mm == MM_len || mm == MM_concat ||
1591 (!(tr = crec_arith_int64(J, sp, s, mm)) &&
1592 !(tr = crec_arith_ptr(J, sp, s, mm)))) &&
1593 !(tr = crec_arith_meta(J, sp, s, cts, rd)))
1594 return;
1595 J->base[0] = tr;
1596 /* Fixup cdata comparisons, too. Avoids some cdata escapes. */
1597 if (J->postproc == LJ_POST_FIXGUARD && frame_iscont(J->L->base-1) &&
1598 !irt_isguard(J->guardemit)) {
1599 const BCIns *pc = frame_contpc(J->L->base-1) - 1;
1600 if (bc_op(*pc) <= BC_ISNEP) {
1601 J2G(J)->tmptv.u64 = (uint64_t)(uintptr_t)pc;
1602 J->postproc = LJ_POST_FIXCOMP;
1603 }
1604 }
1605 }
1606 }
1607
1608 /* -- C library namespace metamethods ------------------------------------- */
1609
1610 void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd)
1611 {
1612 CTState *cts = ctype_ctsG(J2G(J));
1613 if (tref_isudata(J->base[0]) && tref_isstr(J->base[1]) &&
1614 udataV(&rd->argv[0])->udtype == UDTYPE_FFI_CLIB) {
1615 CLibrary *cl = (CLibrary *)uddata(udataV(&rd->argv[0]));
1616 GCstr *name = strV(&rd->argv[1]);
1617 CType *ct;
1618 CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX);
1619 cTValue *tv = lj_tab_getstr(cl->cache, name);
1620 rd->nres = rd->data;
1621 if (id && tv && !tvisnil(tv)) {
1622 /* Specialize to the symbol name and make the result a constant. */
1623 emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, name));
1624 if (ctype_isconstval(ct->info)) {
1625 if (ct->size >= 0x80000000u &&
1626 (ctype_child(cts, ct)->info & CTF_UNSIGNED))
1627 J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)ct->size);
1628 else
1629 J->base[0] = lj_ir_kint(J, (int32_t)ct->size);
1630 } else if (ctype_isextern(ct->info)) {
1631 CTypeID sid = ctype_cid(ct->info);
1632 void *sp = *(void **)cdataptr(cdataV(tv));
1633 TRef ptr;
1634 ct = ctype_raw(cts, sid);
1635 if (LJ_64 && !checkptr32(sp))
1636 ptr = lj_ir_kintp(J, (uintptr_t)sp);
1637 else
1638 ptr = lj_ir_kptr(J, sp);
1639 if (rd->data) {
1640 J->base[0] = crec_tv_ct(J, ct, sid, ptr);
1641 } else {
1642 J->needsnap = 1;
1643 crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
1644 }
1645 } else {
1646 J->base[0] = lj_ir_kgc(J, obj2gco(cdataV(tv)), IRT_CDATA);
1647 }
1648 } else {
1649 lj_trace_err(J, LJ_TRERR_NOCACHE);
1650 }
1651 } /* else: interpreter will throw. */
1652 }
1653
1654 /* -- FFI library functions ----------------------------------------------- */
1655
1656 static TRef crec_toint(jit_State *J, CTState *cts, TRef sp, TValue *sval)
1657 {
1658 return crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, sp, sval);
1659 }
1660
1661 void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd)
1662 {
1663 crec_alloc(J, rd, argv2ctype(J, J->base[0], &rd->argv[0]));
1664 }
1665
1666 void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd)
1667 {
1668 UNUSED(rd);
1669 if (J->base[0])
1670 lj_trace_err(J, LJ_TRERR_NYICALL);
1671 J->base[0] = lj_ir_call(J, IRCALL_lj_vm_errno);
1672 }
1673
1674 void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd)
1675 {
1676 CTState *cts = ctype_ctsG(J2G(J));
1677 TRef tr = J->base[0];
1678 if (tr) {
1679 TRef trlen = J->base[1];
1680 if (!tref_isnil(trlen)) {
1681 trlen = crec_toint(J, cts, trlen, &rd->argv[1]);
1682 tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, &rd->argv[0]);
1683 } else {
1684 tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CCHAR), 0, tr, &rd->argv[0]);
1685 trlen = lj_ir_call(J, IRCALL_strlen, tr);
1686 }
1687 J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), tr, trlen);
1688 } /* else: interpreter will throw. */
1689 }
1690
1691 void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd)
1692 {
1693 CTState *cts = ctype_ctsG(J2G(J));
1694 TRef trdst = J->base[0], trsrc = J->base[1], trlen = J->base[2];
1695 if (trdst && trsrc && (trlen || tref_isstr(trsrc))) {
1696 trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]);
1697 trsrc = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, trsrc, &rd->argv[1]);
1698 if (trlen) {
1699 trlen = crec_toint(J, cts, trlen, &rd->argv[2]);
1700 } else {
1701 trlen = emitir(IRTI(IR_FLOAD), J->base[1], IRFL_STR_LEN);
1702 trlen = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1));
1703 }
1704 rd->nres = 0;
1705 crec_copy(J, trdst, trsrc, trlen, NULL);
1706 } /* else: interpreter will throw. */
1707 }
1708
1709 void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd)
1710 {
1711 CTState *cts = ctype_ctsG(J2G(J));
1712 TRef trdst = J->base[0], trlen = J->base[1], trfill = J->base[2];
1713 if (trdst && trlen) {
1714 CTSize step = 1;
1715 if (tviscdata(&rd->argv[0])) { /* Get alignment of original destination. */
1716 CTSize sz;
1717 CType *ct = ctype_raw(cts, cdataV(&rd->argv[0])->ctypeid);
1718 if (ctype_isptr(ct->info))
1719 ct = ctype_rawchild(cts, ct);
1720 step = (1u<<ctype_align(lj_ctype_info(cts, ctype_typeid(cts, ct), &sz)));
1721 }
1722 trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]);
1723 trlen = crec_toint(J, cts, trlen, &rd->argv[1]);
1724 if (trfill)
1725 trfill = crec_toint(J, cts, trfill, &rd->argv[2]);
1726 else
1727 trfill = lj_ir_kint(J, 0);
1728 rd->nres = 0;
1729 crec_fill(J, trdst, trlen, trfill, step);
1730 } /* else: interpreter will throw. */
1731 }
1732
1733 void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd)
1734 {
1735 if (tref_iscdata(J->base[0])) {
1736 TRef trid = lj_ir_kint(J, argv2ctype(J, J->base[0], &rd->argv[0]));
1737 J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA),
1738 lj_ir_kint(J, CTID_CTYPEID), trid);
1739 } else {
1740 setfuncV(J->L, &J->errinfo, J->fn);
1741 lj_trace_err_info(J, LJ_TRERR_NYIFFU);
1742 }
1743 }
1744
1745 void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd)
1746 {
1747 argv2ctype(J, J->base[0], &rd->argv[0]);
1748 if (tref_iscdata(J->base[1])) {
1749 argv2ctype(J, J->base[1], &rd->argv[1]);
1750 J->postproc = LJ_POST_FIXBOOL;
1751 J->base[0] = TREF_TRUE;
1752 } else {
1753 J->base[0] = TREF_FALSE;
1754 }
1755 }
1756
1757 void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd)
1758 {
1759 if (tref_isstr(J->base[0])) {
1760 /* Specialize to the ABI string to make the boolean result a constant. */
1761 emitir(IRTG(IR_EQ, IRT_STR), J->base[0], lj_ir_kstr(J, strV(&rd->argv[0])));
1762 J->postproc = LJ_POST_FIXBOOL;
1763 J->base[0] = TREF_TRUE;
1764 } else {
1765 lj_trace_err(J, LJ_TRERR_BADTYPE);
1766 }
1767 }
1768
1769 /* Record ffi.sizeof(), ffi.alignof(), ffi.offsetof(). */
1770 void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd)
1771 {
1772 CTypeID id = argv2ctype(J, J->base[0], &rd->argv[0]);
1773 if (rd->data == FF_ffi_sizeof) {
1774 CType *ct = lj_ctype_rawref(ctype_ctsG(J2G(J)), id);
1775 if (ctype_isvltype(ct->info))
1776 lj_trace_err(J, LJ_TRERR_BADTYPE);
1777 } else if (rd->data == FF_ffi_offsetof) { /* Specialize to the field name. */
1778 if (!tref_isstr(J->base[1]))
1779 lj_trace_err(J, LJ_TRERR_BADTYPE);
1780 emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1])));
1781 rd->nres = 3; /* Just in case. */
1782 }
1783 J->postproc = LJ_POST_FIXCONST;
1784 J->base[0] = J->base[1] = J->base[2] = TREF_NIL;
1785 }
1786
1787 void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd)
1788 {
1789 argv2cdata(J, J->base[0], &rd->argv[0]);
1790 if (!J->base[1])
1791 lj_trace_err(J, LJ_TRERR_BADTYPE);
1792 crec_finalizer(J, J->base[0], J->base[1], &rd->argv[1]);
1793 }
1794
1795 /* -- 64 bit bit.* library functions -------------------------------------- */
1796
1797 /* Determine bit operation type from argument type. */
1798 static CTypeID crec_bit64_type(CTState *cts, cTValue *tv)
1799 {
1800 if (tviscdata(tv)) {
1801 CType *ct = lj_ctype_rawref(cts, cdataV(tv)->ctypeid);
1802 if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
1803 if ((ct->info & (CTMASK_NUM|CTF_BOOL|CTF_FP|CTF_UNSIGNED)) ==
1804 CTINFO(CT_NUM, CTF_UNSIGNED) && ct->size == 8)
1805 return CTID_UINT64; /* Use uint64_t, since it has the highest rank. */
1806 return CTID_INT64; /* Otherwise use int64_t. */
1807 }
1808 return 0; /* Use regular 32 bit ops. */
1809 }
1810
1811 static TRef crec_bit64_arg(jit_State *J, CType *d, TRef sp, TValue *sval)
1812 {
1813 if (LJ_UNLIKELY(tref_isstr(sp))) {
1814 if (lj_strscan_num(strV(sval), sval)) {
1815 sp = emitir(IRTG(IR_STRTO, IRT_NUM), sp, 0);
1816 } /* else: interpreter will throw. */
1817 }
1818 return crec_ct_tv(J, d, 0, sp, sval);
1819 }
1820
1821 void LJ_FASTCALL recff_bit64_tobit(jit_State *J, RecordFFData *rd)
1822 {
1823 CTState *cts = ctype_ctsG(J2G(J));
1824 TRef tr = crec_bit64_arg(J, ctype_get(cts, CTID_INT64),
1825 J->base[0], &rd->argv[0]);
1826 if (!tref_isinteger(tr))
1827 tr = emitconv(tr, IRT_INT, tref_type(tr), 0);
1828 J->base[0] = tr;
1829 }
1830
1831 int LJ_FASTCALL recff_bit64_unary(jit_State *J, RecordFFData *rd)
1832 {
1833 CTState *cts = ctype_ctsG(J2G(J));
1834 CTypeID id = crec_bit64_type(cts, &rd->argv[0]);
1835 if (id) {
1836 TRef tr = crec_bit64_arg(J, ctype_get(cts, id), J->base[0], &rd->argv[0]);
1837 tr = emitir(IRT(rd->data, id-CTID_INT64+IRT_I64), tr, 0);
1838 J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
1839 return 1;
1840 }
1841 return 0;
1842 }
1843
1844 int LJ_FASTCALL recff_bit64_nary(jit_State *J, RecordFFData *rd)
1845 {
1846 CTState *cts = ctype_ctsG(J2G(J));
1847 CTypeID id = 0;
1848 MSize i;
1849 for (i = 0; J->base[i] != 0; i++) {
1850 CTypeID aid = crec_bit64_type(cts, &rd->argv[i]);
1851 if (id < aid) id = aid; /* Determine highest type rank of all arguments. */
1852 }
1853 if (id) {
1854 CType *ct = ctype_get(cts, id);
1855 uint32_t ot = IRT(rd->data, id-CTID_INT64+IRT_I64);
1856 TRef tr = crec_bit64_arg(J, ct, J->base[0], &rd->argv[0]);
1857 for (i = 1; J->base[i] != 0; i++) {
1858 TRef tr2 = crec_bit64_arg(J, ct, J->base[i], &rd->argv[i]);
1859 tr = emitir(ot, tr, tr2);
1860 }
1861 J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
1862 return 1;
1863 }
1864 return 0;
1865 }
1866
1867 int LJ_FASTCALL recff_bit64_shift(jit_State *J, RecordFFData *rd)
1868 {
1869 CTState *cts = ctype_ctsG(J2G(J));
1870 CTypeID id;
1871 TRef tsh = 0;
1872 if (J->base[0] && tref_iscdata(J->base[1])) {
1873 tsh = crec_bit64_arg(J, ctype_get(cts, CTID_INT64),
1874 J->base[1], &rd->argv[1]);
1875 if (!tref_isinteger(tsh))
1876 tsh = emitconv(tsh, IRT_INT, tref_type(tsh), 0);
1877 J->base[1] = tsh;
1878 }
1879 id = crec_bit64_type(cts, &rd->argv[0]);
1880 if (id) {
1881 TRef tr = crec_bit64_arg(J, ctype_get(cts, id), J->base[0], &rd->argv[0]);
1882 uint32_t op = rd->data;
1883 if (!tsh) tsh = lj_opt_narrow_tobit(J, J->base[1]);
1884 if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) &&
1885 !tref_isk(tsh))
1886 tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 63));
1887 #ifdef LJ_TARGET_UNIFYROT
1888 if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) {
1889 op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR;
1890 tsh = emitir(IRTI(IR_NEG), tsh, tsh);
1891 }
1892 #endif
1893 tr = emitir(IRT(op, id-CTID_INT64+IRT_I64), tr, tsh);
1894 J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
1895 return 1;
1896 }
1897 return 0;
1898 }
1899
1900 TRef recff_bit64_tohex(jit_State *J, RecordFFData *rd, TRef hdr)
1901 {
1902 CTState *cts = ctype_ctsG(J2G(J));
1903 CTypeID id = crec_bit64_type(cts, &rd->argv[0]);
1904 TRef tr, trsf = J->base[1];
1905 SFormat sf = (STRFMT_UINT|STRFMT_T_HEX);
1906 int32_t n;
1907 if (trsf) {
1908 CTypeID id2 = 0;
1909 n = (int32_t)lj_carith_check64(J->L, 2, &id2);
1910 if (id2)
1911 trsf = crec_bit64_arg(J, ctype_get(cts, CTID_INT32), trsf, &rd->argv[1]);
1912 else
1913 trsf = lj_opt_narrow_tobit(J, trsf);
1914 emitir(IRTGI(IR_EQ), trsf, lj_ir_kint(J, n)); /* Specialize to n. */
1915 } else {
1916 n = id ? 16 : 8;
1917 }
1918 if (n < 0) { n = (int32_t)(~n+1u); sf |= STRFMT_F_UPPER; }
1919 if ((uint32_t)n > 254) n = 254;
1920 sf |= ((SFormat)((n+1)&255) << STRFMT_SH_PREC);
1921 if (id) {
1922 tr = crec_bit64_arg(J, ctype_get(cts, id), J->base[0], &rd->argv[0]);
1923 if (n < 16)
1924 tr = emitir(IRT(IR_BAND, IRT_U64), tr,
1925 lj_ir_kint64(J, ((uint64_t)1 << 4*n)-1));
1926 } else {
1927 tr = lj_opt_narrow_tobit(J, J->base[0]);
1928 if (n < 8)
1929 tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << 4*n)-1)));
1930 tr = emitconv(tr, IRT_U64, IRT_INT, 0); /* No sign-extension. */
1931 lj_needsplit(J);
1932 }
1933 return lj_ir_call(J, IRCALL_lj_strfmt_putfxint, hdr, lj_ir_kint(J, sf), tr);
1934 }
1935
1936 /* -- Miscellaneous library functions ------------------------------------- */
1937
1938 void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd)
1939 {
1940 CTState *cts = ctype_ctsG(J2G(J));
1941 CType *d, *ct = lj_ctype_rawref(cts, cdataV(&rd->argv[0])->ctypeid);
1942 if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
1943 if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {
1944 if (ctype_isinteger_or_bool(ct->info) && ct->size <= 4 &&
1945 !(ct->size == 4 && (ct->info & CTF_UNSIGNED)))
1946 d = ctype_get(cts, CTID_INT32);
1947 else
1948 d = ctype_get(cts, CTID_DOUBLE);
1949 J->base[0] = crec_ct_tv(J, d, 0, J->base[0], &rd->argv[0]);
1950 } else {
1951 /* Specialize to the ctype that couldn't be converted. */
1952 argv2cdata(J, J->base[0], &rd->argv[0]);
1953 J->base[0] = TREF_NIL;
1954 }
1955 }
1956
1957 TRef lj_crecord_loadiu64(jit_State *J, TRef tr, cTValue *o)
1958 {
1959 CTypeID id = argv2cdata(J, tr, o)->ctypeid;
1960 if (!(id == CTID_INT64 || id == CTID_UINT64))
1961 lj_trace_err(J, LJ_TRERR_BADTYPE);
1962 lj_needsplit(J);
1963 return emitir(IRT(IR_FLOAD, id == CTID_INT64 ? IRT_I64 : IRT_U64), tr,
1964 IRFL_CDATA_INT64);
1965 }
1966
1967 #if LJ_HASBUFFER
1968 TRef lj_crecord_topcvoid(jit_State *J, TRef tr, cTValue *o)
1969 {
1970 CTState *cts = ctype_ctsG(J2G(J));
1971 if (!tref_iscdata(tr)) lj_trace_err(J, LJ_TRERR_BADTYPE);
1972 return crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, o);
1973 }
1974
1975 TRef lj_crecord_topuint8(jit_State *J, TRef tr)
1976 {
1977 return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, CTID_P_UINT8), tr);
1978 }
1979 #endif
1980
1981 #undef IR
1982 #undef emitir
1983 #undef emitconv
1984
1985 #endif