3 ** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
15 #include "lj_ccallback.h"
17 /* -- Conversion errors --------------------------------------------------- */
20 LJ_NORET
static void cconv_err_conv(CTState
*cts
, CType
*d
, CType
*s
,
23 const char *dst
= strdata(lj_ctype_repr(cts
->L
, ctype_typeid(cts
, d
), NULL
));
25 if ((flags
& CCF_FROMTV
))
26 src
= lj_obj_typename
[1+(ctype_isnum(s
->info
) ? LUA_TNUMBER
:
27 ctype_isarray(s
->info
) ? LUA_TSTRING
: LUA_TNIL
)];
29 src
= strdata(lj_ctype_repr(cts
->L
, ctype_typeid(cts
, s
), NULL
));
30 if (CCF_GETARG(flags
))
31 lj_err_argv(cts
->L
, CCF_GETARG(flags
), LJ_ERR_FFI_BADCONV
, src
, dst
);
33 lj_err_callerv(cts
->L
, LJ_ERR_FFI_BADCONV
, src
, dst
);
36 /* Bad conversion from TValue. */
37 LJ_NORET
static void cconv_err_convtv(CTState
*cts
, CType
*d
, TValue
*o
,
40 const char *dst
= strdata(lj_ctype_repr(cts
->L
, ctype_typeid(cts
, d
), NULL
));
41 const char *src
= typename(o
);
42 if (CCF_GETARG(flags
))
43 lj_err_argv(cts
->L
, CCF_GETARG(flags
), LJ_ERR_FFI_BADCONV
, src
, dst
);
45 lj_err_callerv(cts
->L
, LJ_ERR_FFI_BADCONV
, src
, dst
);
48 /* Initializer overflow. */
49 LJ_NORET
static void cconv_err_initov(CTState
*cts
, CType
*d
)
51 const char *dst
= strdata(lj_ctype_repr(cts
->L
, ctype_typeid(cts
, d
), NULL
));
52 lj_err_callerv(cts
->L
, LJ_ERR_FFI_INITOV
, dst
);
55 /* -- C type compatibility checks ----------------------------------------- */
57 /* Get raw type and qualifiers for a child type. Resolves enums, too. */
58 static CType
*cconv_childqual(CTState
*cts
, CType
*ct
, CTInfo
*qual
)
60 ct
= ctype_child(cts
, ct
);
62 if (ctype_isattrib(ct
->info
)) {
63 if (ctype_attrib(ct
->info
) == CTA_QUAL
) *qual
|= ct
->size
;
64 } else if (!ctype_isenum(ct
->info
)) {
67 ct
= ctype_child(cts
, ct
);
69 *qual
|= (ct
->info
& CTF_QUAL
);
73 /* Check for compatible types when converting to a pointer.
74 ** Note: these checks are more relaxed than what C99 mandates.
76 int lj_cconv_compatptr(CTState
*cts
, CType
*d
, CType
*s
, CTInfo flags
)
78 if (!((flags
& CCF_CAST
) || d
== s
)) {
79 CTInfo dqual
= 0, squal
= 0;
80 d
= cconv_childqual(cts
, d
, &dqual
);
81 if (!ctype_isstruct(s
->info
))
82 s
= cconv_childqual(cts
, s
, &squal
);
83 if ((flags
& CCF_SAME
)) {
85 return 0; /* Different qualifiers. */
86 } else if (!(flags
& CCF_IGNQUAL
)) {
87 if ((dqual
& squal
) != squal
)
88 return 0; /* Discarded qualifiers. */
89 if (ctype_isvoid(d
->info
) || ctype_isvoid(s
->info
))
90 return 1; /* Converting to/from void * is always ok. */
92 if (ctype_type(d
->info
) != ctype_type(s
->info
) ||
94 return 0; /* Different type or different size. */
95 if (ctype_isnum(d
->info
)) {
96 if (((d
->info
^ s
->info
) & (CTF_BOOL
|CTF_FP
)))
97 return 0; /* Different numeric types. */
98 } else if (ctype_ispointer(d
->info
)) {
99 /* Check child types for compatibility. */
100 return lj_cconv_compatptr(cts
, d
, s
, flags
|CCF_SAME
);
101 } else if (ctype_isstruct(d
->info
)) {
103 return 0; /* Must be exact same type for struct/union. */
104 } else if (ctype_isfunc(d
->info
)) {
105 /* NYI: structural equality of functions. */
108 return 1; /* Types are compatible. */
111 /* -- C type to C type conversion ----------------------------------------- */
113 /* Convert C type to C type. Caveat: expects to get the raw CType!
115 ** Note: This is only used by the interpreter and not optimized at all.
116 ** The JIT compiler will do a much better job specializing for each case.
118 void lj_cconv_ct_ct(CTState
*cts
, CType
*d
, CType
*s
,
119 uint8_t *dp
, uint8_t *sp
, CTInfo flags
)
121 CTSize dsize
= d
->size
, ssize
= s
->size
;
122 CTInfo dinfo
= d
->info
, sinfo
= s
->info
;
125 lua_assert(!ctype_isenum(dinfo
) && !ctype_isenum(sinfo
));
126 lua_assert(!ctype_isattrib(dinfo
) && !ctype_isattrib(sinfo
));
128 if (ctype_type(dinfo
) > CT_MAYCONVERT
|| ctype_type(sinfo
) > CT_MAYCONVERT
)
131 /* Some basic sanity checks. */
132 lua_assert(!ctype_isnum(dinfo
) || dsize
> 0);
133 lua_assert(!ctype_isnum(sinfo
) || ssize
> 0);
134 lua_assert(!ctype_isbool(dinfo
) || dsize
== 1 || dsize
== 4);
135 lua_assert(!ctype_isbool(sinfo
) || ssize
== 1 || ssize
== 4);
136 lua_assert(!ctype_isinteger(dinfo
) || (1u<<lj_fls(dsize
)) == dsize
);
137 lua_assert(!ctype_isinteger(sinfo
) || (1u<<lj_fls(ssize
)) == ssize
);
139 switch (cconv_idx2(dinfo
, sinfo
)) {
140 /* Destination is a bool. */
142 /* Source operand is already normalized. */
143 if (dsize
== 1) *dp
= *sp
; else *(int *)dp
= *sp
;
148 for (i
= 0; i
< ssize
; i
++) b
|= sp
[i
];
150 if (dsize
== 1) *dp
= b
; else *(int *)dp
= b
;
155 if (ssize
== sizeof(double)) b
= (*(double *)sp
!= 0);
156 else if (ssize
== sizeof(float)) b
= (*(float *)sp
!= 0);
157 else goto err_conv
; /* NYI: long double. */
158 if (dsize
== 1) *dp
= b
; else *(int *)dp
= b
;
162 /* Destination is an integer. */
166 if (dsize
> ssize
) { /* Zero-extend or sign-extend LSB. */
168 uint8_t fill
= (!(sinfo
& CTF_UNSIGNED
) && (sp
[ssize
-1]&0x80)) ? 0xff : 0;
169 memcpy(dp
, sp
, ssize
);
170 memset(dp
+ ssize
, fill
, dsize
-ssize
);
172 uint8_t fill
= (!(sinfo
& CTF_UNSIGNED
) && (sp
[0]&0x80)) ? 0xff : 0;
173 memset(dp
, fill
, dsize
-ssize
);
174 memcpy(dp
+ (dsize
-ssize
), sp
, ssize
);
176 } else { /* Copy LSB. */
178 memcpy(dp
, sp
, dsize
);
180 memcpy(dp
, sp
+ (ssize
-dsize
), dsize
);
185 double n
; /* Always convert via double. */
187 /* Convert source to double. */
188 if (ssize
== sizeof(double)) n
= *(double *)sp
;
189 else if (ssize
== sizeof(float)) n
= (double)*(float *)sp
;
190 else goto err_conv
; /* NYI: long double. */
191 /* Then convert double to integer. */
192 /* The conversion must exactly match the semantics of JIT-compiled code! */
193 if (dsize
< 4 || (dsize
== 4 && !(dinfo
& CTF_UNSIGNED
))) {
194 int32_t i
= (int32_t)n
;
195 if (dsize
== 4) *(int32_t *)dp
= i
;
196 else if (dsize
== 2) *(int16_t *)dp
= (int16_t)i
;
197 else *(int8_t *)dp
= (int8_t)i
;
198 } else if (dsize
== 4) {
199 *(uint32_t *)dp
= (uint32_t)n
;
200 } else if (dsize
== 8) {
201 if (!(dinfo
& CTF_UNSIGNED
))
202 *(int64_t *)dp
= (int64_t)n
;
204 *(uint64_t *)dp
= lj_num2u64(n
);
206 goto err_conv
; /* NYI: conversion to >64 bit integers. */
211 s
= ctype_child(cts
, s
);
214 goto conv_I_F
; /* Just convert re. */
216 if (!(flags
& CCF_CAST
)) goto err_conv
;
217 sinfo
= CTINFO(CT_NUM
, CTF_UNSIGNED
);
220 if (!(flags
& CCF_CAST
)) goto err_conv
;
221 sinfo
= CTINFO(CT_NUM
, CTF_UNSIGNED
);
224 sp
= (uint8_t *)&tmpptr
;
227 /* Destination is a floating-point number. */
230 double n
; /* Always convert via double. */
232 /* First convert source to double. */
233 /* The conversion must exactly match the semantics of JIT-compiled code! */
234 if (ssize
< 4 || (ssize
== 4 && !(sinfo
& CTF_UNSIGNED
))) {
238 } else if (!(sinfo
& CTF_UNSIGNED
)) {
239 if (ssize
== 2) i
= *(int16_t *)sp
;
240 else i
= *(int8_t *)sp
;
242 if (ssize
== 2) i
= *(uint16_t *)sp
;
243 else i
= *(uint8_t *)sp
;
246 } else if (ssize
== 4) {
247 n
= (double)*(uint32_t *)sp
;
248 } else if (ssize
== 8) {
249 if (!(sinfo
& CTF_UNSIGNED
)) n
= (double)*(int64_t *)sp
;
250 else n
= (double)*(uint64_t *)sp
;
252 goto err_conv
; /* NYI: conversion from >64 bit integers. */
254 /* Convert double to destination. */
255 if (dsize
== sizeof(double)) *(double *)dp
= n
;
256 else if (dsize
== sizeof(float)) *(float *)dp
= (float)n
;
257 else goto err_conv
; /* NYI: long double. */
261 double n
; /* Always convert via double. */
263 if (ssize
== dsize
) goto copyval
;
264 /* Convert source to double. */
265 if (ssize
== sizeof(double)) n
= *(double *)sp
;
266 else if (ssize
== sizeof(float)) n
= (double)*(float *)sp
;
267 else goto err_conv
; /* NYI: long double. */
268 /* Convert double to destination. */
269 if (dsize
== sizeof(double)) *(double *)dp
= n
;
270 else if (dsize
== sizeof(float)) *(float *)dp
= (float)n
;
271 else goto err_conv
; /* NYI: long double. */
275 s
= ctype_child(cts
, s
);
278 goto conv_F_F
; /* Ignore im, and convert from re. */
280 /* Destination is a complex number. */
282 d
= ctype_child(cts
, d
);
285 memset(dp
+ dsize
, 0, dsize
); /* Clear im. */
286 goto conv_F_I
; /* Convert to re. */
288 d
= ctype_child(cts
, d
);
291 memset(dp
+ dsize
, 0, dsize
); /* Clear im. */
292 goto conv_F_F
; /* Convert to re. */
295 if (dsize
!= ssize
) { /* Different types: convert re/im separately. */
296 CType
*dc
= ctype_child(cts
, d
);
297 CType
*sc
= ctype_child(cts
, s
);
298 lj_cconv_ct_ct(cts
, dc
, sc
, dp
, sp
, flags
);
299 lj_cconv_ct_ct(cts
, dc
, sc
, dp
+ dc
->size
, sp
+ sc
->size
, flags
);
302 goto copyval
; /* Otherwise this is easy. */
304 /* Destination is a vector. */
308 CType
*dc
= ctype_child(cts
, d
);
310 /* First convert the scalar to the first element. */
311 lj_cconv_ct_ct(cts
, dc
, s
, dp
, sp
, flags
);
312 /* Then replicate it to the other elements (splat). */
313 for (sp
= dp
, esize
= dc
->size
; dsize
> esize
; dsize
-= esize
) {
315 memcpy(dp
, sp
, esize
);
321 /* Copy same-sized vectors, even for different lengths/element-types. */
322 if (dsize
!= ssize
) goto err_conv
;
325 /* Destination is a pointer. */
327 if (!(flags
& CCF_CAST
)) goto err_conv
;
328 dinfo
= CTINFO(CT_NUM
, CTF_UNSIGNED
);
332 if (!(flags
& CCF_CAST
) || !(flags
& CCF_FROMTV
)) goto err_conv
;
333 /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
334 dinfo
= CTINFO(CT_NUM
, (LJ_64
&& dsize
== 8) ? 0 : CTF_UNSIGNED
);
338 if (!lj_cconv_compatptr(cts
, d
, s
, flags
)) goto err_conv
;
339 cdata_setptr(dp
, dsize
, cdata_getptr(sp
, ssize
));
344 if (!lj_cconv_compatptr(cts
, d
, s
, flags
)) goto err_conv
;
345 cdata_setptr(dp
, dsize
, sp
);
348 /* Destination is an array. */
350 if ((flags
& CCF_CAST
) || (d
->info
& CTF_VLA
) || dsize
!= ssize
||
351 d
->size
== CTSIZE_INVALID
|| !lj_cconv_compatptr(cts
, d
, s
, flags
))
355 /* Destination is a struct/union. */
357 if ((flags
& CCF_CAST
) || (d
->info
& CTF_VLA
) || d
!= s
)
358 goto err_conv
; /* Must be exact same type. */
359 copyval
: /* Copy value. */
360 lua_assert(dsize
== ssize
);
361 memcpy(dp
, sp
, dsize
);
366 cconv_err_conv(cts
, d
, s
, flags
);
370 /* -- C type to TValue conversion ----------------------------------------- */
372 /* Convert C type to TValue. Caveat: expects to get the raw CType! */
373 int lj_cconv_tv_ct(CTState
*cts
, CType
*s
, CTypeID sid
,
374 TValue
*o
, uint8_t *sp
)
376 CTInfo sinfo
= s
->info
;
377 lua_assert(!ctype_isenum(sinfo
));
378 if (ctype_isnum(sinfo
)) {
379 if (!ctype_isbool(sinfo
)) {
380 if (ctype_isinteger(sinfo
) && s
->size
> 4) goto copyval
;
381 if (LJ_DUALNUM
&& ctype_isinteger(sinfo
)) {
383 lj_cconv_ct_ct(cts
, ctype_get(cts
, CTID_INT32
), s
,
384 (uint8_t *)&i
, sp
, 0);
385 if ((sinfo
& CTF_UNSIGNED
) && i
< 0)
386 setnumV(o
, (lua_Number
)(uint32_t)i
);
390 lj_cconv_ct_ct(cts
, ctype_get(cts
, CTID_DOUBLE
), s
,
391 (uint8_t *)&o
->n
, sp
, 0);
392 /* Numbers are NOT canonicalized here! Beware of uninitialized data. */
393 lua_assert(tvisnum(o
));
396 uint32_t b
= s
->size
== 1 ? (*sp
!= 0) : (*(int *)sp
!= 0);
398 setboolV(&cts
->g
->tmptv2
, b
); /* Remember for trace recorder. */
401 } else if (ctype_isrefarray(sinfo
) || ctype_isstruct(sinfo
)) {
402 /* Create reference. */
403 setcdataV(cts
->L
, o
, lj_cdata_newref(cts
, sp
, sid
));
404 return 1; /* Need GC step. */
408 copyval
: /* Copy value. */
410 lua_assert(sz
!= CTSIZE_INVALID
);
411 /* Attributes are stripped, qualifiers are kept (but mostly ignored). */
412 cd
= lj_cdata_new(cts
, ctype_typeid(cts
, s
), sz
);
413 setcdataV(cts
->L
, o
, cd
);
414 memcpy(cdataptr(cd
), sp
, sz
);
415 return 1; /* Need GC step. */
419 /* Convert bitfield to TValue. */
420 int lj_cconv_tv_bf(CTState
*cts
, CType
*s
, TValue
*o
, uint8_t *sp
)
422 CTInfo info
= s
->info
;
425 lua_assert(ctype_isbitfield(info
));
426 /* NYI: packed bitfields may cause misaligned reads. */
427 switch (ctype_bitcsz(info
)) {
428 case 4: val
= *(uint32_t *)sp
; break;
429 case 2: val
= *(uint16_t *)sp
; break;
430 case 1: val
= *(uint8_t *)sp
; break;
431 default: lua_assert(0); val
= 0; break;
433 /* Check if a packed bitfield crosses a container boundary. */
434 pos
= ctype_bitpos(info
);
435 bsz
= ctype_bitbsz(info
);
436 lua_assert(pos
< 8*ctype_bitcsz(info
));
437 lua_assert(bsz
> 0 && bsz
<= 8*ctype_bitcsz(info
));
438 if (pos
+ bsz
> 8*ctype_bitcsz(info
))
439 lj_err_caller(cts
->L
, LJ_ERR_FFI_NYIPACKBIT
);
440 if (!(info
& CTF_BOOL
)) {
441 CTSize shift
= 32 - bsz
;
442 if (!(info
& CTF_UNSIGNED
)) {
443 setintV(o
, (int32_t)(val
<< (shift
-pos
)) >> shift
);
445 val
= (val
<< (shift
-pos
)) >> shift
;
446 if (!LJ_DUALNUM
|| (int32_t)val
< 0)
447 setnumV(o
, (lua_Number
)(uint32_t)val
);
449 setintV(o
, (int32_t)val
);
452 lua_assert(bsz
== 1);
453 setboolV(o
, (val
>> pos
) & 1);
455 return 0; /* No GC step needed. */
458 /* -- TValue to C type conversion ----------------------------------------- */
460 /* Convert table to array. */
461 static void cconv_array_tab(CTState
*cts
, CType
*d
,
462 uint8_t *dp
, GCtab
*t
, CTInfo flags
)
465 CType
*dc
= ctype_rawchild(cts
, d
); /* Array element type. */
466 CTSize size
= d
->size
, esize
= dc
->size
, ofs
= 0;
468 TValue
*tv
= (TValue
*)lj_tab_getint(t
, i
);
469 if (!tv
|| tvisnil(tv
)) {
470 if (i
== 0) continue; /* Try again for 1-based tables. */
471 break; /* Stop at first nil. */
474 cconv_err_initov(cts
, d
);
475 lj_cconv_ct_tv(cts
, dc
, dp
+ ofs
, tv
, flags
);
478 if (size
!= CTSIZE_INVALID
) { /* Only fill up arrays with known size. */
479 if (ofs
== esize
) { /* Replicate a single element. */
480 for (; ofs
< size
; ofs
+= esize
) memcpy(dp
+ ofs
, dp
, esize
);
481 } else { /* Otherwise fill the remainder with zero. */
482 memset(dp
+ ofs
, 0, size
- ofs
);
487 /* Convert table to sub-struct/union. */
488 static void cconv_substruct_tab(CTState
*cts
, CType
*d
, uint8_t *dp
,
489 GCtab
*t
, int32_t *ip
, CTInfo flags
)
493 CType
*df
= ctype_get(cts
, id
);
495 if (ctype_isfield(df
->info
) || ctype_isbitfield(df
->info
)) {
498 if (!gcref(df
->name
)) continue; /* Ignore unnamed fields. */
501 tv
= (TValue
*)lj_tab_getint(t
, i
);
502 if (!tv
|| tvisnil(tv
)) {
503 if (i
== 0) { i
= 1; goto retry
; } /* 1-based tables. */
504 break; /* Stop at first nil. */
508 tv
= (TValue
*)lj_tab_getstr(t
, gco2str(gcref(df
->name
)));
509 if (!tv
|| tvisnil(tv
)) continue;
511 if (ctype_isfield(df
->info
))
512 lj_cconv_ct_tv(cts
, ctype_rawchild(cts
, df
), dp
+df
->size
, tv
, flags
);
514 lj_cconv_bf_tv(cts
, df
, dp
+df
->size
, tv
);
515 if ((d
->info
& CTF_UNION
)) break;
516 } else if (ctype_isxattrib(df
->info
, CTA_SUBTYPE
)) {
517 cconv_substruct_tab(cts
, ctype_child(cts
, df
), dp
+df
->size
, t
, ip
, flags
);
518 } /* Ignore all other entries in the chain. */
522 /* Convert table to struct/union. */
523 static void cconv_struct_tab(CTState
*cts
, CType
*d
,
524 uint8_t *dp
, GCtab
*t
, CTInfo flags
)
527 memset(dp
, 0, d
->size
); /* Much simpler to clear the struct first. */
528 if (t
->hmask
) i
= -1; else if (t
->asize
== 0) return; /* Fast exit. */
529 cconv_substruct_tab(cts
, d
, dp
, t
, &i
, flags
);
532 /* Convert TValue to C type. Caveat: expects to get the raw CType! */
533 void lj_cconv_ct_tv(CTState
*cts
, CType
*d
,
534 uint8_t *dp
, TValue
*o
, CTInfo flags
)
536 CTypeID sid
= CTID_P_VOID
;
539 uint8_t tmpbool
, *sp
= (uint8_t *)&tmpptr
;
540 if (LJ_LIKELY(tvisint(o
))) {
541 sp
= (uint8_t *)&o
->i
;
544 } else if (LJ_LIKELY(tvisnum(o
))) {
545 sp
= (uint8_t *)&o
->n
;
548 } else if (tviscdata(o
)) {
549 sp
= cdataptr(cdataV(o
));
550 sid
= cdataV(o
)->typeid;
551 s
= ctype_get(cts
, sid
);
552 if (ctype_isref(s
->info
)) { /* Resolve reference for value. */
553 lua_assert(s
->size
== CTSIZE_PTR
);
555 sid
= ctype_cid(s
->info
);
557 s
= ctype_raw(cts
, sid
);
558 if (ctype_isfunc(s
->info
)) {
559 sid
= lj_ctype_intern(cts
, CTINFO(CT_PTR
, CTALIGN_PTR
|sid
), CTSIZE_PTR
);
561 if (ctype_isenum(s
->info
)) s
= ctype_child(cts
, s
);
564 } else if (tvisstr(o
)) {
565 GCstr
*str
= strV(o
);
566 if (ctype_isenum(d
->info
)) { /* Match string against enum constant. */
568 CType
*cct
= lj_ctype_getfield(cts
, d
, str
, &ofs
);
569 if (!cct
|| !ctype_isconstval(cct
->info
))
571 lua_assert(d
->size
== 4);
572 sp
= (uint8_t *)&cct
->size
;
573 sid
= ctype_cid(cct
->info
);
574 } else if (ctype_isrefarray(d
->info
)) { /* Copy string to array. */
575 CType
*dc
= ctype_rawchild(cts
, d
);
576 CTSize sz
= str
->len
+1;
577 if (!ctype_isinteger(dc
->info
) || dc
->size
!= 1)
579 if (d
->size
!= 0 && d
->size
< sz
)
581 memcpy(dp
, strdata(str
), sz
);
583 } else { /* Otherwise pass it as a const char[]. */
584 sp
= (uint8_t *)strdata(str
);
588 } else if (tvistab(o
)) {
589 if (ctype_isarray(d
->info
)) {
590 cconv_array_tab(cts
, d
, dp
, tabV(o
), flags
);
592 } else if (ctype_isstruct(d
->info
)) {
593 cconv_struct_tab(cts
, d
, dp
, tabV(o
), flags
);
598 } else if (tvisbool(o
)) {
602 } else if (tvisnil(o
)) {
605 } else if (tvisudata(o
)) {
606 tmpptr
= uddata(udataV(o
));
607 } else if (tvislightud(o
)) {
608 tmpptr
= lightudV(o
);
609 } else if (tvisfunc(o
)) {
610 void *p
= lj_ccallback_new(cts
, d
, funcV(o
));
618 cconv_err_convtv(cts
, d
, o
, flags
);
620 s
= ctype_get(cts
, sid
);
622 if (ctype_isenum(d
->info
)) d
= ctype_child(cts
, d
);
623 lj_cconv_ct_ct(cts
, d
, s
, dp
, sp
, flags
);
626 /* Convert TValue to bitfield. */
627 void lj_cconv_bf_tv(CTState
*cts
, CType
*d
, uint8_t *dp
, TValue
*o
)
629 CTInfo info
= d
->info
;
632 lua_assert(ctype_isbitfield(info
));
633 if ((info
& CTF_BOOL
)) {
635 lua_assert(ctype_bitbsz(info
) == 1);
636 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_BOOL
), &tmpbool
, o
, 0);
639 CTypeID did
= (info
& CTF_UNSIGNED
) ? CTID_UINT32
: CTID_INT32
;
640 lj_cconv_ct_tv(cts
, ctype_get(cts
, did
), (uint8_t *)&val
, o
, 0);
642 pos
= ctype_bitpos(info
);
643 bsz
= ctype_bitbsz(info
);
644 lua_assert(pos
< 8*ctype_bitcsz(info
));
645 lua_assert(bsz
> 0 && bsz
<= 8*ctype_bitcsz(info
));
646 /* Check if a packed bitfield crosses a container boundary. */
647 if (pos
+ bsz
> 8*ctype_bitcsz(info
))
648 lj_err_caller(cts
->L
, LJ_ERR_FFI_NYIPACKBIT
);
649 mask
= ((1u << bsz
) - 1u) << pos
;
650 val
= (val
<< pos
) & mask
;
651 /* NYI: packed bitfields may cause misaligned reads/writes. */
652 switch (ctype_bitcsz(info
)) {
653 case 4: *(uint32_t *)dp
= (*(uint32_t *)dp
& ~mask
) | (uint32_t)val
; break;
654 case 2: *(uint16_t *)dp
= (*(uint16_t *)dp
& ~mask
) | (uint16_t)val
; break;
655 case 1: *(uint8_t *)dp
= (*(uint8_t *)dp
& ~mask
) | (uint8_t)val
; break;
656 default: lua_assert(0); break;
660 /* -- Initialize C type with TValues -------------------------------------- */
662 /* Initialize an array with TValues. */
663 static void cconv_array_init(CTState
*cts
, CType
*d
, CTSize sz
, uint8_t *dp
,
664 TValue
*o
, MSize len
)
666 CType
*dc
= ctype_rawchild(cts
, d
); /* Array element type. */
667 CTSize ofs
, esize
= dc
->size
;
670 cconv_err_initov(cts
, d
);
671 for (i
= 0, ofs
= 0; i
< len
; i
++, ofs
+= esize
)
672 lj_cconv_ct_tv(cts
, dc
, dp
+ ofs
, o
+ i
, 0);
673 if (ofs
== esize
) { /* Replicate a single element. */
674 for (; ofs
< sz
; ofs
+= esize
) memcpy(dp
+ ofs
, dp
, esize
);
675 } else { /* Otherwise fill the remainder with zero. */
676 memset(dp
+ ofs
, 0, sz
- ofs
);
680 /* Initialize a sub-struct/union with TValues. */
681 static void cconv_substruct_init(CTState
*cts
, CType
*d
, uint8_t *dp
,
682 TValue
*o
, MSize len
, MSize
*ip
)
686 CType
*df
= ctype_get(cts
, id
);
688 if (ctype_isfield(df
->info
) || ctype_isbitfield(df
->info
)) {
690 if (!gcref(df
->name
)) continue; /* Ignore unnamed fields. */
693 if (ctype_isfield(df
->info
))
694 lj_cconv_ct_tv(cts
, ctype_rawchild(cts
, df
), dp
+df
->size
, o
+ i
, 0);
696 lj_cconv_bf_tv(cts
, df
, dp
+df
->size
, o
+ i
);
697 if ((d
->info
& CTF_UNION
)) break;
698 } else if (ctype_isxattrib(df
->info
, CTA_SUBTYPE
)) {
699 cconv_substruct_init(cts
, ctype_child(cts
, df
), dp
+df
->size
, o
, len
, ip
);
700 } /* Ignore all other entries in the chain. */
704 /* Initialize a struct/union with TValues. */
705 static void cconv_struct_init(CTState
*cts
, CType
*d
, CTSize sz
, uint8_t *dp
,
706 TValue
*o
, MSize len
)
709 memset(dp
, 0, sz
); /* Much simpler to clear the struct first. */
710 cconv_substruct_init(cts
, d
, dp
, o
, len
, &i
);
712 cconv_err_initov(cts
, d
);
715 /* Check whether to use a multi-value initializer.
716 ** This is true if an aggregate is to be initialized with a value.
717 ** Valarrays are treated as values here so ct_tv handles (V|C, I|F).
719 int lj_cconv_multi_init(CType
*d
, TValue
*o
)
721 if (!(ctype_isrefarray(d
->info
) || ctype_isstruct(d
->info
)))
722 return 0; /* Destination is not an aggregate. */
723 if (tvistab(o
) || (tvisstr(o
) && !ctype_isstruct(d
->info
)))
724 return 0; /* Initializer is not a value. */
725 return 1; /* Otherwise the initializer is a value. */
728 /* Initialize C type with TValues. Caveat: expects to get the raw CType! */
729 void lj_cconv_ct_init(CTState
*cts
, CType
*d
, CTSize sz
,
730 uint8_t *dp
, TValue
*o
, MSize len
)
734 else if (len
== 1 && !lj_cconv_multi_init(d
, o
))
735 lj_cconv_ct_tv(cts
, d
, dp
, o
, 0);
736 else if (ctype_isarray(d
->info
)) /* Also handles valarray init with len>1. */
737 cconv_array_init(cts
, d
, sz
, dp
, o
, len
);
738 else if (ctype_isstruct(d
->info
))
739 cconv_struct_init(cts
, d
, sz
, dp
, o
, len
);
741 cconv_err_initov(cts
, d
);