3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
16 #include "lj_ccallback.h"
18 /* -- Conversion errors --------------------------------------------------- */
21 LJ_NORET
static void cconv_err_conv(CTState
*cts
, CType
*d
, CType
*s
,
24 const char *dst
= strdata(lj_ctype_repr(cts
->L
, ctype_typeid(cts
, d
), NULL
));
26 if ((flags
& CCF_FROMTV
))
27 src
= lj_obj_typename
[1+(ctype_isnum(s
->info
) ? LUA_TNUMBER
:
28 ctype_isarray(s
->info
) ? LUA_TSTRING
: LUA_TNIL
)];
30 src
= strdata(lj_ctype_repr(cts
->L
, ctype_typeid(cts
, s
), NULL
));
31 if (CCF_GETARG(flags
))
32 lj_err_argv(cts
->L
, CCF_GETARG(flags
), LJ_ERR_FFI_BADCONV
, src
, dst
);
34 lj_err_callerv(cts
->L
, LJ_ERR_FFI_BADCONV
, src
, dst
);
37 /* Bad conversion from TValue. */
38 LJ_NORET
static void cconv_err_convtv(CTState
*cts
, CType
*d
, TValue
*o
,
41 const char *dst
= strdata(lj_ctype_repr(cts
->L
, ctype_typeid(cts
, d
), NULL
));
42 const char *src
= lj_typename(o
);
43 if (CCF_GETARG(flags
))
44 lj_err_argv(cts
->L
, CCF_GETARG(flags
), LJ_ERR_FFI_BADCONV
, src
, dst
);
46 lj_err_callerv(cts
->L
, LJ_ERR_FFI_BADCONV
, src
, dst
);
49 /* Initializer overflow. */
50 LJ_NORET
static void cconv_err_initov(CTState
*cts
, CType
*d
)
52 const char *dst
= strdata(lj_ctype_repr(cts
->L
, ctype_typeid(cts
, d
), NULL
));
53 lj_err_callerv(cts
->L
, LJ_ERR_FFI_INITOV
, dst
);
56 /* -- C type compatibility checks ----------------------------------------- */
58 /* Get raw type and qualifiers for a child type. Resolves enums, too. */
59 static CType
*cconv_childqual(CTState
*cts
, CType
*ct
, CTInfo
*qual
)
61 ct
= ctype_child(cts
, ct
);
63 if (ctype_isattrib(ct
->info
)) {
64 if (ctype_attrib(ct
->info
) == CTA_QUAL
) *qual
|= ct
->size
;
65 } else if (!ctype_isenum(ct
->info
)) {
68 ct
= ctype_child(cts
, ct
);
70 *qual
|= (ct
->info
& CTF_QUAL
);
74 /* Check for compatible types when converting to a pointer.
75 ** Note: these checks are more relaxed than what C99 mandates.
77 int lj_cconv_compatptr(CTState
*cts
, CType
*d
, CType
*s
, CTInfo flags
)
79 if (!((flags
& CCF_CAST
) || d
== s
)) {
80 CTInfo dqual
= 0, squal
= 0;
81 d
= cconv_childqual(cts
, d
, &dqual
);
82 if (!ctype_isstruct(s
->info
))
83 s
= cconv_childqual(cts
, s
, &squal
);
84 if ((flags
& CCF_SAME
)) {
86 return 0; /* Different qualifiers. */
87 } else if (!(flags
& CCF_IGNQUAL
)) {
88 if ((dqual
& squal
) != squal
)
89 return 0; /* Discarded qualifiers. */
90 if (ctype_isvoid(d
->info
) || ctype_isvoid(s
->info
))
91 return 1; /* Converting to/from void * is always ok. */
93 if (ctype_type(d
->info
) != ctype_type(s
->info
) ||
95 return 0; /* Different type or different size. */
96 if (ctype_isnum(d
->info
)) {
97 if (((d
->info
^ s
->info
) & (CTF_BOOL
|CTF_FP
)))
98 return 0; /* Different numeric types. */
99 } else if (ctype_ispointer(d
->info
)) {
100 /* Check child types for compatibility. */
101 return lj_cconv_compatptr(cts
, d
, s
, flags
|CCF_SAME
);
102 } else if (ctype_isstruct(d
->info
)) {
104 return 0; /* Must be exact same type for struct/union. */
105 } else if (ctype_isfunc(d
->info
)) {
106 /* NYI: structural equality of functions. */
109 return 1; /* Types are compatible. */
112 /* -- C type to C type conversion ----------------------------------------- */
114 /* Convert C type to C type. Caveat: expects to get the raw CType!
116 ** Note: This is only used by the interpreter and not optimized at all.
117 ** The JIT compiler will do a much better job specializing for each case.
119 void lj_cconv_ct_ct(CTState
*cts
, CType
*d
, CType
*s
,
120 uint8_t *dp
, uint8_t *sp
, CTInfo flags
)
122 CTSize dsize
= d
->size
, ssize
= s
->size
;
123 CTInfo dinfo
= d
->info
, sinfo
= s
->info
;
126 lj_assertCTS(!ctype_isenum(dinfo
) && !ctype_isenum(sinfo
),
128 lj_assertCTS(!ctype_isattrib(dinfo
) && !ctype_isattrib(sinfo
),
129 "unstripped attribute");
131 if (ctype_type(dinfo
) > CT_MAYCONVERT
|| ctype_type(sinfo
) > CT_MAYCONVERT
)
134 /* Some basic sanity checks. */
135 lj_assertCTS(!ctype_isnum(dinfo
) || dsize
> 0, "bad size for number type");
136 lj_assertCTS(!ctype_isnum(sinfo
) || ssize
> 0, "bad size for number type");
137 lj_assertCTS(!ctype_isbool(dinfo
) || dsize
== 1 || dsize
== 4,
138 "bad size for bool type");
139 lj_assertCTS(!ctype_isbool(sinfo
) || ssize
== 1 || ssize
== 4,
140 "bad size for bool type");
141 lj_assertCTS(!ctype_isinteger(dinfo
) || (1u<<lj_fls(dsize
)) == dsize
,
142 "bad size for integer type");
143 lj_assertCTS(!ctype_isinteger(sinfo
) || (1u<<lj_fls(ssize
)) == ssize
,
144 "bad size for integer type");
146 switch (cconv_idx2(dinfo
, sinfo
)) {
147 /* Destination is a bool. */
149 /* Source operand is already normalized. */
150 if (dsize
== 1) *dp
= *sp
; else *(int *)dp
= *sp
;
155 for (i
= 0; i
< ssize
; i
++) b
|= sp
[i
];
157 if (dsize
== 1) *dp
= b
; else *(int *)dp
= b
;
162 if (ssize
== sizeof(double)) b
= (*(double *)sp
!= 0);
163 else if (ssize
== sizeof(float)) b
= (*(float *)sp
!= 0);
164 else goto err_conv
; /* NYI: long double. */
165 if (dsize
== 1) *dp
= b
; else *(int *)dp
= b
;
169 /* Destination is an integer. */
173 if (dsize
> ssize
) { /* Zero-extend or sign-extend LSB. */
175 uint8_t fill
= (!(sinfo
& CTF_UNSIGNED
) && (sp
[ssize
-1]&0x80)) ? 0xff : 0;
176 memcpy(dp
, sp
, ssize
);
177 memset(dp
+ ssize
, fill
, dsize
-ssize
);
179 uint8_t fill
= (!(sinfo
& CTF_UNSIGNED
) && (sp
[0]&0x80)) ? 0xff : 0;
180 memset(dp
, fill
, dsize
-ssize
);
181 memcpy(dp
+ (dsize
-ssize
), sp
, ssize
);
183 } else { /* Copy LSB. */
185 memcpy(dp
, sp
, dsize
);
187 memcpy(dp
, sp
+ (ssize
-dsize
), dsize
);
192 double n
; /* Always convert via double. */
194 /* Convert source to double. */
195 if (ssize
== sizeof(double)) n
= *(double *)sp
;
196 else if (ssize
== sizeof(float)) n
= (double)*(float *)sp
;
197 else goto err_conv
; /* NYI: long double. */
198 /* Then convert double to integer. */
199 /* The conversion must exactly match the semantics of JIT-compiled code! */
200 if (dsize
< 4 || (dsize
== 4 && !(dinfo
& CTF_UNSIGNED
))) {
201 int32_t i
= (int32_t)n
;
202 if (dsize
== 4) *(int32_t *)dp
= i
;
203 else if (dsize
== 2) *(int16_t *)dp
= (int16_t)i
;
204 else *(int8_t *)dp
= (int8_t)i
;
205 } else if (dsize
== 4) {
206 *(uint32_t *)dp
= (uint32_t)n
;
207 } else if (dsize
== 8) {
208 if (!(dinfo
& CTF_UNSIGNED
))
209 *(int64_t *)dp
= (int64_t)n
;
211 *(uint64_t *)dp
= lj_num2u64(n
);
213 goto err_conv
; /* NYI: conversion to >64 bit integers. */
218 s
= ctype_child(cts
, s
);
221 goto conv_I_F
; /* Just convert re. */
223 if (!(flags
& CCF_CAST
)) goto err_conv
;
224 sinfo
= CTINFO(CT_NUM
, CTF_UNSIGNED
);
227 if (!(flags
& CCF_CAST
)) goto err_conv
;
228 sinfo
= CTINFO(CT_NUM
, CTF_UNSIGNED
);
231 sp
= (uint8_t *)&tmpptr
;
234 /* Destination is a floating-point number. */
237 double n
; /* Always convert via double. */
239 /* First convert source to double. */
240 /* The conversion must exactly match the semantics of JIT-compiled code! */
241 if (ssize
< 4 || (ssize
== 4 && !(sinfo
& CTF_UNSIGNED
))) {
245 } else if (!(sinfo
& CTF_UNSIGNED
)) {
246 if (ssize
== 2) i
= *(int16_t *)sp
;
247 else i
= *(int8_t *)sp
;
249 if (ssize
== 2) i
= *(uint16_t *)sp
;
250 else i
= *(uint8_t *)sp
;
253 } else if (ssize
== 4) {
254 n
= (double)*(uint32_t *)sp
;
255 } else if (ssize
== 8) {
256 if (!(sinfo
& CTF_UNSIGNED
)) n
= (double)*(int64_t *)sp
;
257 else n
= (double)*(uint64_t *)sp
;
259 goto err_conv
; /* NYI: conversion from >64 bit integers. */
261 /* Convert double to destination. */
262 if (dsize
== sizeof(double)) *(double *)dp
= n
;
263 else if (dsize
== sizeof(float)) *(float *)dp
= (float)n
;
264 else goto err_conv
; /* NYI: long double. */
268 double n
; /* Always convert via double. */
270 if (ssize
== dsize
) goto copyval
;
271 /* Convert source to double. */
272 if (ssize
== sizeof(double)) n
= *(double *)sp
;
273 else if (ssize
== sizeof(float)) n
= (double)*(float *)sp
;
274 else goto err_conv
; /* NYI: long double. */
275 /* Convert double to destination. */
276 if (dsize
== sizeof(double)) *(double *)dp
= n
;
277 else if (dsize
== sizeof(float)) *(float *)dp
= (float)n
;
278 else goto err_conv
; /* NYI: long double. */
282 s
= ctype_child(cts
, s
);
285 goto conv_F_F
; /* Ignore im, and convert from re. */
287 /* Destination is a complex number. */
289 d
= ctype_child(cts
, d
);
292 memset(dp
+ dsize
, 0, dsize
); /* Clear im. */
293 goto conv_F_I
; /* Convert to re. */
295 d
= ctype_child(cts
, d
);
298 memset(dp
+ dsize
, 0, dsize
); /* Clear im. */
299 goto conv_F_F
; /* Convert to re. */
302 if (dsize
!= ssize
) { /* Different types: convert re/im separately. */
303 CType
*dc
= ctype_child(cts
, d
);
304 CType
*sc
= ctype_child(cts
, s
);
305 lj_cconv_ct_ct(cts
, dc
, sc
, dp
, sp
, flags
);
306 lj_cconv_ct_ct(cts
, dc
, sc
, dp
+ dc
->size
, sp
+ sc
->size
, flags
);
309 goto copyval
; /* Otherwise this is easy. */
311 /* Destination is a vector. */
315 CType
*dc
= ctype_child(cts
, d
);
317 /* First convert the scalar to the first element. */
318 lj_cconv_ct_ct(cts
, dc
, s
, dp
, sp
, flags
);
319 /* Then replicate it to the other elements (splat). */
320 for (sp
= dp
, esize
= dc
->size
; dsize
> esize
; dsize
-= esize
) {
322 memcpy(dp
, sp
, esize
);
328 /* Copy same-sized vectors, even for different lengths/element-types. */
329 if (dsize
!= ssize
) goto err_conv
;
332 /* Destination is a pointer. */
334 if (!(flags
& CCF_CAST
)) goto err_conv
;
335 dinfo
= CTINFO(CT_NUM
, CTF_UNSIGNED
);
339 if (!(flags
& CCF_CAST
) || !(flags
& CCF_FROMTV
)) goto err_conv
;
340 /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
341 dinfo
= CTINFO(CT_NUM
, (LJ_64
&& dsize
== 8) ? 0 : CTF_UNSIGNED
);
345 if (!lj_cconv_compatptr(cts
, d
, s
, flags
)) goto err_conv
;
346 cdata_setptr(dp
, dsize
, cdata_getptr(sp
, ssize
));
351 if (!lj_cconv_compatptr(cts
, d
, s
, flags
)) goto err_conv
;
352 cdata_setptr(dp
, dsize
, sp
);
355 /* Destination is an array. */
357 if ((flags
& CCF_CAST
) || (d
->info
& CTF_VLA
) || dsize
!= ssize
||
358 d
->size
== CTSIZE_INVALID
|| !lj_cconv_compatptr(cts
, d
, s
, flags
))
362 /* Destination is a struct/union. */
364 if ((flags
& CCF_CAST
) || (d
->info
& CTF_VLA
) || d
!= s
)
365 goto err_conv
; /* Must be exact same type. */
366 copyval
: /* Copy value. */
367 lj_assertCTS(dsize
== ssize
, "value copy with different sizes");
368 memcpy(dp
, sp
, dsize
);
373 cconv_err_conv(cts
, d
, s
, flags
);
377 /* -- C type to TValue conversion ----------------------------------------- */
379 /* Convert C type to TValue. Caveat: expects to get the raw CType! */
380 int lj_cconv_tv_ct(CTState
*cts
, CType
*s
, CTypeID sid
,
381 TValue
*o
, uint8_t *sp
)
383 CTInfo sinfo
= s
->info
;
384 if (ctype_isnum(sinfo
)) {
385 if (!ctype_isbool(sinfo
)) {
386 if (ctype_isinteger(sinfo
) && s
->size
> 4) goto copyval
;
387 if (LJ_DUALNUM
&& ctype_isinteger(sinfo
)) {
389 lj_cconv_ct_ct(cts
, ctype_get(cts
, CTID_INT32
), s
,
390 (uint8_t *)&i
, sp
, 0);
391 if ((sinfo
& CTF_UNSIGNED
) && i
< 0)
392 setnumV(o
, (lua_Number
)(uint32_t)i
);
396 lj_cconv_ct_ct(cts
, ctype_get(cts
, CTID_DOUBLE
), s
,
397 (uint8_t *)&o
->n
, sp
, 0);
398 /* Numbers are NOT canonicalized here! Beware of uninitialized data. */
399 lj_assertCTS(tvisnum(o
), "non-canonical NaN passed");
402 uint32_t b
= s
->size
== 1 ? (*sp
!= 0) : (*(int *)sp
!= 0);
404 setboolV(&cts
->g
->tmptv2
, b
); /* Remember for trace recorder. */
407 } else if (ctype_isrefarray(sinfo
) || ctype_isstruct(sinfo
)) {
408 /* Create reference. */
409 setcdataV(cts
->L
, o
, lj_cdata_newref(cts
, sp
, sid
));
410 return 1; /* Need GC step. */
414 copyval
: /* Copy value. */
416 lj_assertCTS(sz
!= CTSIZE_INVALID
, "value copy with invalid size");
417 /* Attributes are stripped, qualifiers are kept (but mostly ignored). */
418 cd
= lj_cdata_new(cts
, ctype_typeid(cts
, s
), sz
);
419 setcdataV(cts
->L
, o
, cd
);
420 memcpy(cdataptr(cd
), sp
, sz
);
421 return 1; /* Need GC step. */
425 /* Convert bitfield to TValue. */
426 int lj_cconv_tv_bf(CTState
*cts
, CType
*s
, TValue
*o
, uint8_t *sp
)
428 CTInfo info
= s
->info
;
431 lj_assertCTS(ctype_isbitfield(info
), "bitfield expected");
432 /* NYI: packed bitfields may cause misaligned reads. */
433 switch (ctype_bitcsz(info
)) {
434 case 4: val
= *(uint32_t *)sp
; break;
435 case 2: val
= *(uint16_t *)sp
; break;
436 case 1: val
= *(uint8_t *)sp
; break;
438 lj_assertCTS(0, "bad bitfield container size %d", ctype_bitcsz(info
));
442 /* Check if a packed bitfield crosses a container boundary. */
443 pos
= ctype_bitpos(info
);
444 bsz
= ctype_bitbsz(info
);
445 lj_assertCTS(pos
< 8*ctype_bitcsz(info
), "bad bitfield position");
446 lj_assertCTS(bsz
> 0 && bsz
<= 8*ctype_bitcsz(info
), "bad bitfield size");
447 if (pos
+ bsz
> 8*ctype_bitcsz(info
))
448 lj_err_caller(cts
->L
, LJ_ERR_FFI_NYIPACKBIT
);
449 if (!(info
& CTF_BOOL
)) {
450 CTSize shift
= 32 - bsz
;
451 if (!(info
& CTF_UNSIGNED
)) {
452 setintV(o
, (int32_t)(val
<< (shift
-pos
)) >> shift
);
454 val
= (val
<< (shift
-pos
)) >> shift
;
455 if (!LJ_DUALNUM
|| (int32_t)val
< 0)
456 setnumV(o
, (lua_Number
)(uint32_t)val
);
458 setintV(o
, (int32_t)val
);
461 uint32_t b
= (val
>> pos
) & 1;
462 lj_assertCTS(bsz
== 1, "bad bool bitfield size");
464 setboolV(&cts
->g
->tmptv2
, b
); /* Remember for trace recorder. */
466 return 0; /* No GC step needed. */
469 /* -- TValue to C type conversion ----------------------------------------- */
471 /* Convert table to array. */
472 static void cconv_array_tab(CTState
*cts
, CType
*d
,
473 uint8_t *dp
, GCtab
*t
, CTInfo flags
)
476 CType
*dc
= ctype_rawchild(cts
, d
); /* Array element type. */
477 CTSize size
= d
->size
, esize
= dc
->size
, ofs
= 0;
479 TValue
*tv
= (TValue
*)lj_tab_getint(t
, i
);
480 if (!tv
|| tvisnil(tv
)) {
481 if (i
== 0) continue; /* Try again for 1-based tables. */
482 break; /* Stop at first nil. */
485 cconv_err_initov(cts
, d
);
486 lj_cconv_ct_tv(cts
, dc
, dp
+ ofs
, tv
, flags
);
489 if (size
!= CTSIZE_INVALID
) { /* Only fill up arrays with known size. */
490 if (ofs
== esize
) { /* Replicate a single element. */
491 for (; ofs
< size
; ofs
+= esize
) memcpy(dp
+ ofs
, dp
, esize
);
492 } else { /* Otherwise fill the remainder with zero. */
493 memset(dp
+ ofs
, 0, size
- ofs
);
498 /* Convert table to sub-struct/union. */
499 static void cconv_substruct_tab(CTState
*cts
, CType
*d
, uint8_t *dp
,
500 GCtab
*t
, int32_t *ip
, CTInfo flags
)
504 CType
*df
= ctype_get(cts
, id
);
506 if (ctype_isfield(df
->info
) || ctype_isbitfield(df
->info
)) {
508 int32_t i
= *ip
, iz
= i
;
509 if (!gcref(df
->name
)) continue; /* Ignore unnamed fields. */
512 tv
= (TValue
*)lj_tab_getint(t
, i
);
513 if (!tv
|| tvisnil(tv
)) {
514 if (i
== 0) { i
= 1; goto retry
; } /* 1-based tables. */
515 if (iz
== 0) { *ip
= i
= -1; goto tryname
; } /* Init named fields. */
516 break; /* Stop at first nil. */
521 tv
= (TValue
*)lj_tab_getstr(t
, gco2str(gcref(df
->name
)));
522 if (!tv
|| tvisnil(tv
)) continue;
524 if (ctype_isfield(df
->info
))
525 lj_cconv_ct_tv(cts
, ctype_rawchild(cts
, df
), dp
+df
->size
, tv
, flags
);
527 lj_cconv_bf_tv(cts
, df
, dp
+df
->size
, tv
);
528 if ((d
->info
& CTF_UNION
)) break;
529 } else if (ctype_isxattrib(df
->info
, CTA_SUBTYPE
)) {
530 cconv_substruct_tab(cts
, ctype_rawchild(cts
, df
),
531 dp
+df
->size
, t
, ip
, flags
);
532 } /* Ignore all other entries in the chain. */
536 /* Convert table to struct/union. */
537 static void cconv_struct_tab(CTState
*cts
, CType
*d
,
538 uint8_t *dp
, GCtab
*t
, CTInfo flags
)
541 memset(dp
, 0, d
->size
); /* Much simpler to clear the struct first. */
542 cconv_substruct_tab(cts
, d
, dp
, t
, &i
, flags
);
545 /* Convert TValue to C type. Caveat: expects to get the raw CType! */
546 void lj_cconv_ct_tv(CTState
*cts
, CType
*d
,
547 uint8_t *dp
, TValue
*o
, CTInfo flags
)
549 CTypeID sid
= CTID_P_VOID
;
552 uint8_t tmpbool
, *sp
= (uint8_t *)&tmpptr
;
553 if (LJ_LIKELY(tvisint(o
))) {
554 sp
= (uint8_t *)&o
->i
;
557 } else if (LJ_LIKELY(tvisnum(o
))) {
558 sp
= (uint8_t *)&o
->n
;
561 } else if (tviscdata(o
)) {
562 sp
= cdataptr(cdataV(o
));
563 sid
= cdataV(o
)->ctypeid
;
564 s
= ctype_get(cts
, sid
);
565 if (ctype_isref(s
->info
)) { /* Resolve reference for value. */
566 lj_assertCTS(s
->size
== CTSIZE_PTR
, "ref is not pointer-sized");
568 sid
= ctype_cid(s
->info
);
570 s
= ctype_raw(cts
, sid
);
571 if (ctype_isfunc(s
->info
)) {
572 CTypeID did
= ctype_typeid(cts
, d
);
573 sid
= lj_ctype_intern(cts
, CTINFO(CT_PTR
, CTALIGN_PTR
|sid
), CTSIZE_PTR
);
574 d
= ctype_get(cts
, did
); /* cts->tab may have been reallocated. */
576 if (ctype_isenum(s
->info
)) s
= ctype_child(cts
, s
);
579 } else if (tvisstr(o
)) {
580 GCstr
*str
= strV(o
);
581 if (ctype_isenum(d
->info
)) { /* Match string against enum constant. */
583 CType
*cct
= lj_ctype_getfield(cts
, d
, str
, &ofs
);
584 if (!cct
|| !ctype_isconstval(cct
->info
))
586 lj_assertCTS(d
->size
== 4, "only 32 bit enum supported"); /* NYI */
587 sp
= (uint8_t *)&cct
->size
;
588 sid
= ctype_cid(cct
->info
);
589 } else if (ctype_isrefarray(d
->info
)) { /* Copy string to array. */
590 CType
*dc
= ctype_rawchild(cts
, d
);
591 CTSize sz
= str
->len
+1;
592 if (!ctype_isinteger(dc
->info
) || dc
->size
!= 1)
594 if (d
->size
!= 0 && d
->size
< sz
)
596 memcpy(dp
, strdata(str
), sz
);
598 } else { /* Otherwise pass it as a const char[]. */
599 sp
= (uint8_t *)strdata(str
);
603 } else if (tvistab(o
)) {
604 if (ctype_isarray(d
->info
)) {
605 cconv_array_tab(cts
, d
, dp
, tabV(o
), flags
);
607 } else if (ctype_isstruct(d
->info
)) {
608 cconv_struct_tab(cts
, d
, dp
, tabV(o
), flags
);
613 } else if (tvisbool(o
)) {
617 } else if (tvisnil(o
)) {
620 } else if (tvisudata(o
)) {
621 GCudata
*ud
= udataV(o
);
623 if (ud
->udtype
== UDTYPE_IO_FILE
)
624 tmpptr
= *(void **)tmpptr
;
625 else if (ud
->udtype
== UDTYPE_BUFFER
)
626 tmpptr
= ((SBufExt
*)tmpptr
)->r
;
627 } else if (tvislightud(o
)) {
628 tmpptr
= lightudV(cts
->g
, o
);
629 } else if (tvisfunc(o
)) {
630 void *p
= lj_ccallback_new(cts
, d
, funcV(o
));
638 cconv_err_convtv(cts
, d
, o
, flags
);
640 s
= ctype_get(cts
, sid
);
642 if (ctype_isenum(d
->info
)) d
= ctype_child(cts
, d
);
643 lj_cconv_ct_ct(cts
, d
, s
, dp
, sp
, flags
);
646 /* Convert TValue to bitfield. */
647 void lj_cconv_bf_tv(CTState
*cts
, CType
*d
, uint8_t *dp
, TValue
*o
)
649 CTInfo info
= d
->info
;
652 lj_assertCTS(ctype_isbitfield(info
), "bitfield expected");
653 if ((info
& CTF_BOOL
)) {
655 lj_assertCTS(ctype_bitbsz(info
) == 1, "bad bool bitfield size");
656 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_BOOL
), &tmpbool
, o
, 0);
659 CTypeID did
= (info
& CTF_UNSIGNED
) ? CTID_UINT32
: CTID_INT32
;
660 lj_cconv_ct_tv(cts
, ctype_get(cts
, did
), (uint8_t *)&val
, o
, 0);
662 pos
= ctype_bitpos(info
);
663 bsz
= ctype_bitbsz(info
);
664 lj_assertCTS(pos
< 8*ctype_bitcsz(info
), "bad bitfield position");
665 lj_assertCTS(bsz
> 0 && bsz
<= 8*ctype_bitcsz(info
), "bad bitfield size");
666 /* Check if a packed bitfield crosses a container boundary. */
667 if (pos
+ bsz
> 8*ctype_bitcsz(info
))
668 lj_err_caller(cts
->L
, LJ_ERR_FFI_NYIPACKBIT
);
669 mask
= ((1u << bsz
) - 1u) << pos
;
670 val
= (val
<< pos
) & mask
;
671 /* NYI: packed bitfields may cause misaligned reads/writes. */
672 switch (ctype_bitcsz(info
)) {
673 case 4: *(uint32_t *)dp
= (*(uint32_t *)dp
& ~mask
) | (uint32_t)val
; break;
674 case 2: *(uint16_t *)dp
= (*(uint16_t *)dp
& ~mask
) | (uint16_t)val
; break;
675 case 1: *(uint8_t *)dp
= (*(uint8_t *)dp
& ~mask
) | (uint8_t)val
; break;
677 lj_assertCTS(0, "bad bitfield container size %d", ctype_bitcsz(info
));
682 /* -- Initialize C type with TValues -------------------------------------- */
684 /* Initialize an array with TValues. */
685 static void cconv_array_init(CTState
*cts
, CType
*d
, CTSize sz
, uint8_t *dp
,
686 TValue
*o
, MSize len
)
688 CType
*dc
= ctype_rawchild(cts
, d
); /* Array element type. */
689 CTSize ofs
, esize
= dc
->size
;
692 cconv_err_initov(cts
, d
);
693 for (i
= 0, ofs
= 0; i
< len
; i
++, ofs
+= esize
)
694 lj_cconv_ct_tv(cts
, dc
, dp
+ ofs
, o
+ i
, 0);
695 if (ofs
== esize
) { /* Replicate a single element. */
696 for (; ofs
< sz
; ofs
+= esize
) memcpy(dp
+ ofs
, dp
, esize
);
697 } else { /* Otherwise fill the remainder with zero. */
698 memset(dp
+ ofs
, 0, sz
- ofs
);
702 /* Initialize a sub-struct/union with TValues. */
703 static void cconv_substruct_init(CTState
*cts
, CType
*d
, uint8_t *dp
,
704 TValue
*o
, MSize len
, MSize
*ip
)
708 CType
*df
= ctype_get(cts
, id
);
710 if (ctype_isfield(df
->info
) || ctype_isbitfield(df
->info
)) {
712 if (!gcref(df
->name
)) continue; /* Ignore unnamed fields. */
715 if (ctype_isfield(df
->info
))
716 lj_cconv_ct_tv(cts
, ctype_rawchild(cts
, df
), dp
+df
->size
, o
+ i
, 0);
718 lj_cconv_bf_tv(cts
, df
, dp
+df
->size
, o
+ i
);
719 if ((d
->info
& CTF_UNION
)) break;
720 } else if (ctype_isxattrib(df
->info
, CTA_SUBTYPE
)) {
721 cconv_substruct_init(cts
, ctype_rawchild(cts
, df
),
722 dp
+df
->size
, o
, len
, ip
);
723 if ((d
->info
& CTF_UNION
)) break;
724 } /* Ignore all other entries in the chain. */
728 /* Initialize a struct/union with TValues. */
729 static void cconv_struct_init(CTState
*cts
, CType
*d
, CTSize sz
, uint8_t *dp
,
730 TValue
*o
, MSize len
)
733 memset(dp
, 0, sz
); /* Much simpler to clear the struct first. */
734 cconv_substruct_init(cts
, d
, dp
, o
, len
, &i
);
736 cconv_err_initov(cts
, d
);
739 /* Check whether to use a multi-value initializer.
740 ** This is true if an aggregate is to be initialized with a value.
741 ** Valarrays are treated as values here so ct_tv handles (V|C, I|F).
743 int lj_cconv_multi_init(CTState
*cts
, CType
*d
, TValue
*o
)
745 if (!(ctype_isrefarray(d
->info
) || ctype_isstruct(d
->info
)))
746 return 0; /* Destination is not an aggregate. */
747 if (tvistab(o
) || (tvisstr(o
) && !ctype_isstruct(d
->info
)))
748 return 0; /* Initializer is not a value. */
749 if (tviscdata(o
) && lj_ctype_rawref(cts
, cdataV(o
)->ctypeid
) == d
)
750 return 0; /* Source and destination are identical aggregates. */
751 return 1; /* Otherwise the initializer is a value. */
754 /* Initialize C type with TValues. Caveat: expects to get the raw CType! */
755 void lj_cconv_ct_init(CTState
*cts
, CType
*d
, CTSize sz
,
756 uint8_t *dp
, TValue
*o
, MSize len
)
760 else if (len
== 1 && !lj_cconv_multi_init(cts
, d
, o
))
761 lj_cconv_ct_tv(cts
, d
, dp
, o
, 0);
762 else if (ctype_isarray(d
->info
)) /* Also handles valarray init with len>1. */
763 cconv_array_init(cts
, d
, sz
, dp
, o
, len
);
764 else if (ctype_isstruct(d
->info
))
765 cconv_struct_init(cts
, d
, sz
, dp
, o
, len
);
767 cconv_err_initov(cts
, d
);