FFI: Extend metamethod tutorial.
[luajit-2.0.git] / src / lj_cconv.c
blob884edef1a6429f2146d7892c2ca7d06f7c3dea04
1 /*
2 ** C type conversions.
3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #include "lj_obj.h"
8 #if LJ_HASFFI
10 #include "lj_err.h"
11 #include "lj_tab.h"
12 #include "lj_ctype.h"
13 #include "lj_cdata.h"
14 #include "lj_cconv.h"
16 /* -- Conversion errors --------------------------------------------------- */
18 /* Bad conversion. */
19 LJ_NORET static void cconv_err_conv(CTState *cts, CType *d, CType *s,
20 CTInfo flags)
22 const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));
23 const char *src;
24 if ((flags & CCF_FROMTV))
25 src = lj_obj_typename[1+(ctype_isnum(s->info) ? LUA_TNUMBER :
26 ctype_isarray(s->info) ? LUA_TSTRING : LUA_TNIL)];
27 else
28 src = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, s), NULL));
29 if (CCF_GETARG(flags))
30 lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst);
31 else
32 lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst);
35 /* Bad conversion from TValue. */
36 LJ_NORET static void cconv_err_convtv(CTState *cts, CType *d, TValue *o,
37 CTInfo flags)
39 const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));
40 const char *src = typename(o);
41 if (CCF_GETARG(flags))
42 lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst);
43 else
44 lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst);
47 /* Initializer overflow. */
48 LJ_NORET static void cconv_err_initov(CTState *cts, CType *d)
50 const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));
51 lj_err_callerv(cts->L, LJ_ERR_FFI_INITOV, dst);
54 /* -- C type compatibility checks ----------------------------------------- */
56 /* Get raw type and qualifiers for a child type. Resolves enums, too. */
57 static CType *cconv_childqual(CTState *cts, CType *ct, CTInfo *qual)
59 ct = ctype_child(cts, ct);
60 for (;;) {
61 if (ctype_isattrib(ct->info)) {
62 if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size;
63 } else if (!ctype_isenum(ct->info)) {
64 break;
66 ct = ctype_child(cts, ct);
68 *qual |= (ct->info & CTF_QUAL);
69 return ct;
72 /* Check for compatible types when converting to a pointer.
73 ** Note: these checks are more relaxed than what C99 mandates.
75 int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags)
77 if (!((flags & CCF_CAST) || d == s)) {
78 CTInfo dqual = 0, squal = 0;
79 d = cconv_childqual(cts, d, &dqual);
80 if (!ctype_isstruct(s->info))
81 s = cconv_childqual(cts, s, &squal);
82 if ((flags & CCF_SAME)) {
83 if (dqual != squal)
84 return 0; /* Different qualifiers. */
85 } else if (!(flags & CCF_IGNQUAL)) {
86 if ((dqual & squal) != squal)
87 return 0; /* Discarded qualifiers. */
88 if (ctype_isvoid(d->info) || ctype_isvoid(s->info))
89 return 1; /* Converting to/from void * is always ok. */
91 if (ctype_type(d->info) != ctype_type(s->info) ||
92 d->size != s->size)
93 return 0; /* Different type or different size. */
94 if (ctype_isnum(d->info)) {
95 if (((d->info ^ s->info) & (CTF_BOOL|CTF_FP)))
96 return 0; /* Different numeric types. */
97 } else if (ctype_ispointer(d->info)) {
98 /* Check child types for compatibility. */
99 return lj_cconv_compatptr(cts, d, s, flags|CCF_SAME);
100 } else if (ctype_isstruct(d->info)) {
101 if (d != s)
102 return 0; /* Must be exact same type for struct/union. */
103 } else if (ctype_isfunc(d->info)) {
104 /* NYI: structural equality of functions. */
107 return 1; /* Types are compatible. */
110 /* -- C type to C type conversion ----------------------------------------- */
112 /* Convert C type to C type. Caveat: expects to get the raw CType!
114 ** Note: This is only used by the interpreter and not optimized at all.
115 ** The JIT compiler will do a much better job specializing for each case.
117 void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s,
118 uint8_t *dp, uint8_t *sp, CTInfo flags)
120 CTSize dsize = d->size, ssize = s->size;
121 CTInfo dinfo = d->info, sinfo = s->info;
122 void *tmpptr;
124 lua_assert(!ctype_isenum(dinfo) && !ctype_isenum(sinfo));
125 lua_assert(!ctype_isattrib(dinfo) && !ctype_isattrib(sinfo));
127 if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)
128 goto err_conv;
130 /* Some basic sanity checks. */
131 lua_assert(!ctype_isnum(dinfo) || dsize > 0);
132 lua_assert(!ctype_isnum(sinfo) || ssize > 0);
133 lua_assert(!ctype_isbool(dinfo) || dsize == 1);
134 lua_assert(!ctype_isbool(sinfo) || ssize == 1);
135 lua_assert(!ctype_isinteger(dinfo) || (1u<<lj_fls(dsize)) == dsize);
136 lua_assert(!ctype_isinteger(sinfo) || (1u<<lj_fls(ssize)) == ssize);
138 switch (cconv_idx2(dinfo, sinfo)) {
139 /* Destination is a bool. */
140 case CCX(B, B):
141 *dp = *sp; /* Source operand is already normalized. */
142 break;
143 case CCX(B, I): {
144 MSize i;
145 uint8_t b = 0;
146 for (i = 0; i < ssize; i++) b |= sp[i];
147 *dp = (b != 0);
148 break;
150 case CCX(B, F): {
151 uint8_t b;
152 if (ssize == sizeof(double)) b = (*(double *)sp != 0);
153 else if (ssize == sizeof(float)) b = (*(float *)sp != 0);
154 else goto err_conv; /* NYI: long double. */
155 *dp = b;
156 break;
159 /* Destination is an integer. */
160 case CCX(I, B):
161 case CCX(I, I):
162 conv_I_I:
163 if (dsize > ssize) { /* Zero-extend or sign-extend LSB. */
164 #if LJ_LE
165 uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[ssize-1]&0x80)) ? 0xff : 0;
166 memcpy(dp, sp, ssize);
167 memset(dp + ssize, fill, dsize-ssize);
168 #else
169 uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[0]&0x80)) ? 0xff : 0;
170 memset(dp, fill, dsize-ssize);
171 memcpy(dp + (dsize-ssize), sp, ssize);
172 #endif
173 } else { /* Copy LSB. */
174 #if LJ_LE
175 memcpy(dp, sp, dsize);
176 #else
177 memcpy(dp, sp + (ssize-dsize), dsize);
178 #endif
180 break;
181 case CCX(I, F): {
182 double n; /* Always convert via double. */
183 conv_I_F:
184 /* Convert source to double. */
185 if (ssize == sizeof(double)) n = *(double *)sp;
186 else if (ssize == sizeof(float)) n = (double)*(float *)sp;
187 else goto err_conv; /* NYI: long double. */
188 /* Then convert double to integer. */
189 /* The conversion must exactly match the semantics of JIT-compiled code! */
190 if (dsize < 4 || (dsize == 4 && !(dinfo & CTF_UNSIGNED))) {
191 int32_t i = (int32_t)n;
192 if (dsize == 4) *(int32_t *)dp = i;
193 else if (dsize == 2) *(int16_t *)dp = (int16_t)i;
194 else *(int8_t *)dp = (int8_t)i;
195 } else if (dsize == 4) {
196 *(uint32_t *)dp = (uint32_t)n;
197 } else if (dsize == 8) {
198 if (!(dinfo & CTF_UNSIGNED))
199 *(int64_t *)dp = (int64_t)n;
200 else
201 *(uint64_t *)dp = lj_num2u64(n);
202 } else {
203 goto err_conv; /* NYI: conversion to >64 bit integers. */
205 break;
207 case CCX(I, C):
208 s = ctype_child(cts, s);
209 sinfo = s->info;
210 ssize = s->size;
211 goto conv_I_F; /* Just convert re. */
212 case CCX(I, P):
213 if (!(flags & CCF_CAST)) goto err_conv;
214 sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
215 goto conv_I_I;
216 case CCX(I, A):
217 if (!(flags & CCF_CAST)) goto err_conv;
218 sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
219 ssize = CTSIZE_PTR;
220 tmpptr = sp;
221 sp = (uint8_t *)&tmpptr;
222 goto conv_I_I;
224 /* Destination is a floating-point number. */
225 case CCX(F, B):
226 case CCX(F, I): {
227 double n; /* Always convert via double. */
228 conv_F_I:
229 /* First convert source to double. */
230 /* The conversion must exactly match the semantics of JIT-compiled code! */
231 if (ssize < 4 || (ssize == 4 && !(sinfo & CTF_UNSIGNED))) {
232 int32_t i;
233 if (ssize == 4) {
234 i = *(int32_t *)sp;
235 } else if (!(sinfo & CTF_UNSIGNED)) {
236 if (ssize == 2) i = *(int16_t *)sp;
237 else i = *(int8_t *)sp;
238 } else {
239 if (ssize == 2) i = *(uint16_t *)sp;
240 else i = *(uint8_t *)sp;
242 n = (double)i;
243 } else if (ssize == 4) {
244 n = (double)*(uint32_t *)sp;
245 } else if (ssize == 8) {
246 if (!(sinfo & CTF_UNSIGNED)) n = (double)*(int64_t *)sp;
247 else n = (double)*(uint64_t *)sp;
248 } else {
249 goto err_conv; /* NYI: conversion from >64 bit integers. */
251 /* Convert double to destination. */
252 if (dsize == sizeof(double)) *(double *)dp = n;
253 else if (dsize == sizeof(float)) *(float *)dp = (float)n;
254 else goto err_conv; /* NYI: long double. */
255 break;
257 case CCX(F, F): {
258 double n; /* Always convert via double. */
259 conv_F_F:
260 if (ssize == dsize) goto copyval;
261 /* Convert source to double. */
262 if (ssize == sizeof(double)) n = *(double *)sp;
263 else if (ssize == sizeof(float)) n = (double)*(float *)sp;
264 else goto err_conv; /* NYI: long double. */
265 /* Convert double to destination. */
266 if (dsize == sizeof(double)) *(double *)dp = n;
267 else if (dsize == sizeof(float)) *(float *)dp = (float)n;
268 else goto err_conv; /* NYI: long double. */
269 break;
271 case CCX(F, C):
272 s = ctype_child(cts, s);
273 sinfo = s->info;
274 ssize = s->size;
275 goto conv_F_F; /* Ignore im, and convert from re. */
277 /* Destination is a complex number. */
278 case CCX(C, I):
279 d = ctype_child(cts, d);
280 dinfo = d->info;
281 dsize = d->size;
282 memset(dp + dsize, 0, dsize); /* Clear im. */
283 goto conv_F_I; /* Convert to re. */
284 case CCX(C, F):
285 d = ctype_child(cts, d);
286 dinfo = d->info;
287 dsize = d->size;
288 memset(dp + dsize, 0, dsize); /* Clear im. */
289 goto conv_F_F; /* Convert to re. */
291 case CCX(C, C):
292 if (dsize != ssize) { /* Different types: convert re/im separately. */
293 CType *dc = ctype_child(cts, d);
294 CType *sc = ctype_child(cts, s);
295 lj_cconv_ct_ct(cts, dc, sc, dp, sp, flags);
296 lj_cconv_ct_ct(cts, dc, sc, dp + dc->size, sp + sc->size, flags);
297 return;
299 goto copyval; /* Otherwise this is easy. */
301 /* Destination is a vector. */
302 case CCX(V, I):
303 case CCX(V, F):
304 case CCX(V, C): {
305 CType *dc = ctype_child(cts, d);
306 CTSize esize;
307 /* First convert the scalar to the first element. */
308 lj_cconv_ct_ct(cts, dc, s, dp, sp, flags);
309 /* Then replicate it to the other elements (splat). */
310 for (sp = dp, esize = dc->size; dsize > esize; dsize -= esize) {
311 dp += esize;
312 memcpy(dp, sp, esize);
314 break;
317 case CCX(V, V):
318 /* Copy same-sized vectors, even for different lengths/element-types. */
319 if (dsize != ssize) goto err_conv;
320 goto copyval;
322 /* Destination is a pointer. */
323 case CCX(P, I):
324 if (!(flags & CCF_CAST)) goto err_conv;
325 dinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
326 goto conv_I_I;
328 case CCX(P, F):
329 if (!(flags & CCF_CAST) || !(flags & CCF_FROMTV)) goto err_conv;
330 /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
331 dinfo = CTINFO(CT_NUM, (LJ_64 && dsize == 8) ? 0 : CTF_UNSIGNED);
332 goto conv_I_F;
334 case CCX(P, P):
335 if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv;
336 cdata_setptr(dp, dsize, cdata_getptr(sp, ssize));
337 break;
339 case CCX(P, A):
340 case CCX(P, S):
341 if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv;
342 cdata_setptr(dp, dsize, sp);
343 break;
345 /* Destination is an array. */
346 case CCX(A, A):
347 if ((flags & CCF_CAST) || (d->info & CTF_VLA) || dsize != ssize ||
348 d->size == CTSIZE_INVALID || !lj_cconv_compatptr(cts, d, s, flags))
349 goto err_conv;
350 goto copyval;
352 /* Destination is a struct/union. */
353 case CCX(S, S):
354 if ((flags & CCF_CAST) || (d->info & CTF_VLA) || d != s)
355 goto err_conv; /* Must be exact same type. */
356 copyval: /* Copy value. */
357 lua_assert(dsize == ssize);
358 memcpy(dp, sp, dsize);
359 break;
361 default:
362 err_conv:
363 cconv_err_conv(cts, d, s, flags);
367 /* -- C type to TValue conversion ----------------------------------------- */
369 /* Convert C type to TValue. Caveat: expects to get the raw CType! */
370 int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid,
371 TValue *o, uint8_t *sp)
373 CTInfo sinfo = s->info;
374 lua_assert(!ctype_isenum(sinfo));
375 if (ctype_isnum(sinfo)) {
376 if (!ctype_isbool(sinfo)) {
377 if (ctype_isinteger(sinfo) && s->size > 4) goto copyval;
378 if (LJ_DUALNUM && ctype_isinteger(sinfo)) {
379 int32_t i;
380 lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT32), s,
381 (uint8_t *)&i, sp, 0);
382 if ((sinfo & CTF_UNSIGNED) && i < 0)
383 setnumV(o, (lua_Number)(uint32_t)i);
384 else
385 setintV(o, i);
386 } else {
387 lj_cconv_ct_ct(cts, ctype_get(cts, CTID_DOUBLE), s,
388 (uint8_t *)&o->n, sp, 0);
389 /* Numbers are NOT canonicalized here! Beware of uninitialized data. */
390 lua_assert(tvisnum(o));
392 } else {
393 uint32_t b = ((*sp) & 1);
394 setboolV(o, b);
395 setboolV(&cts->g->tmptv2, b); /* Remember for trace recorder. */
397 return 0;
398 } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
399 /* Create reference. */
400 setcdataV(cts->L, o, lj_cdata_newref(cts, sp, sid));
401 return 1; /* Need GC step. */
402 } else {
403 GCcdata *cd;
404 CTSize sz;
405 copyval: /* Copy value. */
406 sz = s->size;
407 lua_assert(sz != CTSIZE_INVALID);
408 /* Attributes are stripped, qualifiers are kept (but mostly ignored). */
409 cd = lj_cdata_new(cts, ctype_typeid(cts, s), sz);
410 setcdataV(cts->L, o, cd);
411 memcpy(cdataptr(cd), sp, sz);
412 return 1; /* Need GC step. */
416 /* Convert bitfield to TValue. */
417 int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp)
419 CTInfo info = s->info;
420 CTSize pos, bsz;
421 uint32_t val;
422 lua_assert(ctype_isbitfield(info));
423 /* NYI: packed bitfields may cause misaligned reads. */
424 switch (ctype_bitcsz(info)) {
425 case 4: val = *(uint32_t *)sp; break;
426 case 2: val = *(uint16_t *)sp; break;
427 case 1: val = *(uint8_t *)sp; break;
428 default: lua_assert(0); val = 0; break;
430 /* Check if a packed bitfield crosses a container boundary. */
431 pos = ctype_bitpos(info);
432 bsz = ctype_bitbsz(info);
433 lua_assert(pos < 8*ctype_bitcsz(info));
434 lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info));
435 if (pos + bsz > 8*ctype_bitcsz(info))
436 lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT);
437 if (!(info & CTF_BOOL)) {
438 CTSize shift = 32 - bsz;
439 if (!(info & CTF_UNSIGNED)) {
440 setintV(o, (int32_t)(val << (shift-pos)) >> shift);
441 } else {
442 val = (val << (shift-pos)) >> shift;
443 if (!LJ_DUALNUM || (int32_t)val < 0)
444 setnumV(o, (lua_Number)(uint32_t)val);
445 else
446 setintV(o, (int32_t)val);
448 } else {
449 lua_assert(bsz == 1);
450 setboolV(o, (val >> pos) & 1);
452 return 0; /* No GC step needed. */
455 /* -- TValue to C type conversion ----------------------------------------- */
457 /* Convert table to array. */
458 static void cconv_array_tab(CTState *cts, CType *d,
459 uint8_t *dp, GCtab *t, CTInfo flags)
461 int32_t i;
462 CType *dc = ctype_rawchild(cts, d); /* Array element type. */
463 CTSize size = d->size, esize = dc->size, ofs = 0;
464 for (i = 0; ; i++) {
465 TValue *tv = (TValue *)lj_tab_getint(t, i);
466 if (!tv || tvisnil(tv)) {
467 if (i == 0) continue; /* Try again for 1-based tables. */
468 break; /* Stop at first nil. */
470 if (ofs >= size)
471 cconv_err_initov(cts, d);
472 lj_cconv_ct_tv(cts, dc, dp + ofs, tv, flags);
473 ofs += esize;
475 if (size != CTSIZE_INVALID) { /* Only fill up arrays with known size. */
476 if (ofs == esize) { /* Replicate a single element. */
477 for (; ofs < size; ofs += esize) memcpy(dp + ofs, dp, esize);
478 } else { /* Otherwise fill the remainder with zero. */
479 memset(dp + ofs, 0, size - ofs);
484 /* Convert table to sub-struct/union. */
485 static void cconv_substruct_tab(CTState *cts, CType *d, uint8_t *dp,
486 GCtab *t, int32_t *ip, CTInfo flags)
488 CTypeID id = d->sib;
489 while (id) {
490 CType *df = ctype_get(cts, id);
491 id = df->sib;
492 if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) {
493 TValue *tv;
494 int32_t i = *ip;
495 if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
496 if (i >= 0) {
497 retry:
498 tv = (TValue *)lj_tab_getint(t, i);
499 if (!tv || tvisnil(tv)) {
500 if (i == 0) { i = 1; goto retry; } /* 1-based tables. */
501 break; /* Stop at first nil. */
503 *ip = i + 1;
504 } else {
505 tv = (TValue *)lj_tab_getstr(t, gco2str(gcref(df->name)));
506 if (!tv || tvisnil(tv)) continue;
508 if (ctype_isfield(df->info))
509 lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, tv, flags);
510 else
511 lj_cconv_bf_tv(cts, df, dp+df->size, tv);
512 if ((d->info & CTF_UNION)) break;
513 } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) {
514 cconv_substruct_tab(cts, ctype_child(cts, df), dp+df->size, t, ip, flags);
515 } /* Ignore all other entries in the chain. */
519 /* Convert table to struct/union. */
520 static void cconv_struct_tab(CTState *cts, CType *d,
521 uint8_t *dp, GCtab *t, CTInfo flags)
523 int32_t i = 0;
524 memset(dp, 0, d->size); /* Much simpler to clear the struct first. */
525 if (t->hmask) i = -1; else if (t->asize == 0) return; /* Fast exit. */
526 cconv_substruct_tab(cts, d, dp, t, &i, flags);
529 /* Convert TValue to C type. Caveat: expects to get the raw CType! */
530 void lj_cconv_ct_tv(CTState *cts, CType *d,
531 uint8_t *dp, TValue *o, CTInfo flags)
533 CTypeID sid = CTID_P_VOID;
534 CType *s;
535 void *tmpptr;
536 uint8_t tmpbool, *sp = (uint8_t *)&tmpptr;
537 if (LJ_LIKELY(tvisint(o))) {
538 sp = (uint8_t *)&o->i;
539 sid = CTID_INT32;
540 flags |= CCF_FROMTV;
541 } else if (LJ_LIKELY(tvisnum(o))) {
542 sp = (uint8_t *)&o->n;
543 sid = CTID_DOUBLE;
544 flags |= CCF_FROMTV;
545 } else if (tviscdata(o)) {
546 sp = cdataptr(cdataV(o));
547 sid = cdataV(o)->typeid;
548 s = ctype_get(cts, sid);
549 if (ctype_isref(s->info)) { /* Resolve reference for value. */
550 lua_assert(s->size == CTSIZE_PTR);
551 sp = *(void **)sp;
552 sid = ctype_cid(s->info);
554 s = ctype_raw(cts, sid);
555 if (ctype_isfunc(s->info)) {
556 sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR);
557 } else {
558 if (ctype_isenum(s->info)) s = ctype_child(cts, s);
559 goto doconv;
561 } else if (tvisstr(o)) {
562 GCstr *str = strV(o);
563 if (ctype_isenum(d->info)) { /* Match string against enum constant. */
564 CTSize ofs;
565 CType *cct = lj_ctype_getfield(cts, d, str, &ofs);
566 if (!cct || !ctype_isconstval(cct->info))
567 goto err_conv;
568 lua_assert(d->size == 4);
569 sp = (uint8_t *)&cct->size;
570 sid = ctype_cid(cct->info);
571 } else if (ctype_isrefarray(d->info)) { /* Copy string to array. */
572 CType *dc = ctype_rawchild(cts, d);
573 CTSize sz = str->len+1;
574 if (!ctype_isinteger(dc->info) || dc->size != 1)
575 goto err_conv;
576 if (d->size != 0 && d->size < sz)
577 sz = d->size;
578 memcpy(dp, strdata(str), sz);
579 return;
580 } else { /* Otherwise pass it as a const char[]. */
581 sp = (uint8_t *)strdata(str);
582 sid = CTID_A_CCHAR;
583 flags |= CCF_FROMTV;
585 } else if (tvistab(o)) {
586 if (ctype_isarray(d->info)) {
587 cconv_array_tab(cts, d, dp, tabV(o), flags);
588 return;
589 } else if (ctype_isstruct(d->info)) {
590 cconv_struct_tab(cts, d, dp, tabV(o), flags);
591 return;
592 } else {
593 goto err_conv;
595 } else if (tvisbool(o)) {
596 tmpbool = boolV(o);
597 sp = &tmpbool;
598 sid = CTID_BOOL;
599 } else if (tvisnil(o)) {
600 tmpptr = (void *)0;
601 flags |= CCF_FROMTV;
602 } else if (tvisudata(o)) {
603 tmpptr = uddata(udataV(o));
604 } else if (tvislightud(o)) {
605 tmpptr = lightudV(o);
606 } else {
607 err_conv:
608 cconv_err_convtv(cts, d, o, flags);
610 s = ctype_get(cts, sid);
611 doconv:
612 if (ctype_isenum(d->info)) d = ctype_child(cts, d);
613 lj_cconv_ct_ct(cts, d, s, dp, sp, flags);
616 /* Convert TValue to bitfield. */
617 void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o)
619 CTInfo info = d->info;
620 CTSize pos, bsz;
621 uint32_t val, mask;
622 lua_assert(ctype_isbitfield(info));
623 if ((info & CTF_BOOL)) {
624 uint8_t tmpbool;
625 lua_assert(ctype_bitbsz(info) == 1);
626 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_BOOL), &tmpbool, o, 0);
627 val = tmpbool;
628 } else {
629 CTypeID did = (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32;
630 lj_cconv_ct_tv(cts, ctype_get(cts, did), (uint8_t *)&val, o, 0);
632 pos = ctype_bitpos(info);
633 bsz = ctype_bitbsz(info);
634 lua_assert(pos < 8*ctype_bitcsz(info));
635 lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info));
636 /* Check if a packed bitfield crosses a container boundary. */
637 if (pos + bsz > 8*ctype_bitcsz(info))
638 lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT);
639 mask = ((1u << bsz) - 1u) << pos;
640 val = (val << pos) & mask;
641 /* NYI: packed bitfields may cause misaligned reads/writes. */
642 switch (ctype_bitcsz(info)) {
643 case 4: *(uint32_t *)dp = (*(uint32_t *)dp & ~mask) | (uint32_t)val; break;
644 case 2: *(uint16_t *)dp = (*(uint16_t *)dp & ~mask) | (uint16_t)val; break;
645 case 1: *(uint8_t *)dp = (*(uint8_t *)dp & ~mask) | (uint8_t)val; break;
646 default: lua_assert(0); break;
650 /* -- Initialize C type with TValues -------------------------------------- */
652 /* Initialize an array with TValues. */
653 static void cconv_array_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,
654 TValue *o, MSize len)
656 CType *dc = ctype_rawchild(cts, d); /* Array element type. */
657 CTSize ofs, esize = dc->size;
658 MSize i;
659 if (len*esize > sz)
660 cconv_err_initov(cts, d);
661 for (i = 0, ofs = 0; i < len; i++, ofs += esize)
662 lj_cconv_ct_tv(cts, dc, dp + ofs, o + i, 0);
663 if (ofs == esize) { /* Replicate a single element. */
664 for (; ofs < sz; ofs += esize) memcpy(dp + ofs, dp, esize);
665 } else { /* Otherwise fill the remainder with zero. */
666 memset(dp + ofs, 0, sz - ofs);
670 /* Initialize a sub-struct/union with TValues. */
671 static void cconv_substruct_init(CTState *cts, CType *d, uint8_t *dp,
672 TValue *o, MSize len, MSize *ip)
674 CTypeID id = d->sib;
675 while (id) {
676 CType *df = ctype_get(cts, id);
677 id = df->sib;
678 if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) {
679 MSize i = *ip;
680 if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
681 if (i >= len) break;
682 *ip = i + 1;
683 if (ctype_isfield(df->info))
684 lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, o + i, 0);
685 else
686 lj_cconv_bf_tv(cts, df, dp+df->size, o + i);
687 if ((d->info & CTF_UNION)) break;
688 } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) {
689 cconv_substruct_init(cts, ctype_child(cts, df), dp+df->size, o, len, ip);
690 } /* Ignore all other entries in the chain. */
694 /* Initialize a struct/union with TValues. */
695 static void cconv_struct_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,
696 TValue *o, MSize len)
698 MSize i = 0;
699 memset(dp, 0, sz); /* Much simpler to clear the struct first. */
700 cconv_substruct_init(cts, d, dp, o, len, &i);
701 if (i < len)
702 cconv_err_initov(cts, d);
705 /* Check whether to use a multi-value initializer.
706 ** This is true if an aggregate is to be initialized with a value.
707 ** Valarrays are treated as values here so ct_tv handles (V|C, I|F).
709 int lj_cconv_multi_init(CType *d, TValue *o)
711 if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info)))
712 return 0; /* Destination is not an aggregate. */
713 if (tvistab(o) || (tvisstr(o) && !ctype_isstruct(d->info)))
714 return 0; /* Initializer is not a value. */
715 return 1; /* Otherwise the initializer is a value. */
718 /* Initialize C type with TValues. Caveat: expects to get the raw CType! */
719 void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
720 uint8_t *dp, TValue *o, MSize len)
722 if (len == 0)
723 memset(dp, 0, sz);
724 else if (len == 1 && !lj_cconv_multi_init(d, o))
725 lj_cconv_ct_tv(cts, d, dp, o, 0);
726 else if (ctype_isarray(d->info)) /* Also handles valarray init with len>1. */
727 cconv_array_init(cts, d, sz, dp, o, len);
728 else if (ctype_isstruct(d->info))
729 cconv_struct_init(cts, d, sz, dp, o, len);
730 else
731 cconv_err_initov(cts, d);
734 #endif