3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
15 #include "lj_ccallback.h"
17 /* -- C type definitions -------------------------------------------------- */
19 /* Predefined typedefs. */
21 /* Vararg handling. */ \
22 _("va_list", P_VOID) \
23 _("__builtin_va_list", P_VOID) \
24 _("__gnuc_va_list", P_VOID) \
25 /* From stddef.h. */ \
26 _("ptrdiff_t", INT_PSZ) \
27 _("size_t", UINT_PSZ) \
29 /* Subset of stdint.h. */ \
35 _("uint16_t", UINT16) \
36 _("uint32_t", UINT32) \
37 _("uint64_t", UINT64) \
38 _("intptr_t", INT_PSZ) \
39 _("uintptr_t", UINT_PSZ) \
40 /* End of typedef list. */
42 /* Keywords (only the ones we actually care for). */
44 /* Type specifiers. */ \
45 _("void", -1, CTOK_VOID) \
46 _("_Bool", 0, CTOK_BOOL) \
47 _("bool", 1, CTOK_BOOL) \
48 _("char", 1, CTOK_CHAR) \
49 _("int", 4, CTOK_INT) \
50 _("__int8", 1, CTOK_INT) \
51 _("__int16", 2, CTOK_INT) \
52 _("__int32", 4, CTOK_INT) \
53 _("__int64", 8, CTOK_INT) \
54 _("float", 4, CTOK_FP) \
55 _("double", 8, CTOK_FP) \
56 _("long", 0, CTOK_LONG) \
57 _("short", 0, CTOK_SHORT) \
58 _("_Complex", 0, CTOK_COMPLEX) \
59 _("complex", 0, CTOK_COMPLEX) \
60 _("__complex", 0, CTOK_COMPLEX) \
61 _("__complex__", 0, CTOK_COMPLEX) \
62 _("signed", 0, CTOK_SIGNED) \
63 _("__signed", 0, CTOK_SIGNED) \
64 _("__signed__", 0, CTOK_SIGNED) \
65 _("unsigned", 0, CTOK_UNSIGNED) \
66 /* Type qualifiers. */ \
67 _("const", 0, CTOK_CONST) \
68 _("__const", 0, CTOK_CONST) \
69 _("__const__", 0, CTOK_CONST) \
70 _("volatile", 0, CTOK_VOLATILE) \
71 _("__volatile", 0, CTOK_VOLATILE) \
72 _("__volatile__", 0, CTOK_VOLATILE) \
73 _("restrict", 0, CTOK_RESTRICT) \
74 _("__restrict", 0, CTOK_RESTRICT) \
75 _("__restrict__", 0, CTOK_RESTRICT) \
76 _("inline", 0, CTOK_INLINE) \
77 _("__inline", 0, CTOK_INLINE) \
78 _("__inline__", 0, CTOK_INLINE) \
79 /* Storage class specifiers. */ \
80 _("typedef", 0, CTOK_TYPEDEF) \
81 _("extern", 0, CTOK_EXTERN) \
82 _("static", 0, CTOK_STATIC) \
83 _("auto", 0, CTOK_AUTO) \
84 _("register", 0, CTOK_REGISTER) \
85 /* GCC Attributes. */ \
86 _("__extension__", 0, CTOK_EXTENSION) \
87 _("__attribute", 0, CTOK_ATTRIBUTE) \
88 _("__attribute__", 0, CTOK_ATTRIBUTE) \
89 _("asm", 0, CTOK_ASM) \
90 _("__asm", 0, CTOK_ASM) \
91 _("__asm__", 0, CTOK_ASM) \
92 /* MSVC Attributes. */ \
93 _("__declspec", 0, CTOK_DECLSPEC) \
94 _("__cdecl", CTCC_CDECL, CTOK_CCDECL) \
95 _("__thiscall", CTCC_THISCALL, CTOK_CCDECL) \
96 _("__fastcall", CTCC_FASTCALL, CTOK_CCDECL) \
97 _("__stdcall", CTCC_STDCALL, CTOK_CCDECL) \
98 _("__ptr32", 4, CTOK_PTRSZ) \
99 _("__ptr64", 8, CTOK_PTRSZ) \
100 /* Other type specifiers. */ \
101 _("struct", 0, CTOK_STRUCT) \
102 _("union", 0, CTOK_UNION) \
103 _("enum", 0, CTOK_ENUM) \
105 _("sizeof", 0, CTOK_SIZEOF) \
106 _("__alignof", 0, CTOK_ALIGNOF) \
107 _("__alignof__", 0, CTOK_ALIGNOF) \
108 /* End of keyword list. */
110 /* Type info for predefined types. Size merged in. */
111 static CTInfo lj_ctype_typeinfo
[] = {
112 #define CTTYINFODEF(id, sz, ct, info) CTINFO((ct),(((sz)&0x3fu)<<10)+(info)),
113 #define CTTDINFODEF(name, id) CTINFO(CT_TYPEDEF, CTID_##id),
114 #define CTKWINFODEF(name, sz, kw) CTINFO(CT_KW,(((sz)&0x3fu)<<10)+(kw)),
124 /* Predefined type names collected in a single string. */
125 static const char * const lj_ctype_typenames
=
126 #define CTTDNAMEDEF(name, id) name "\0"
127 #define CTKWNAMEDEF(name, sz, cds) name "\0"
134 #define CTTYPEINFO_NUM (sizeof(lj_ctype_typeinfo)/sizeof(CTInfo)-1)
135 #ifdef LUAJIT_CTYPE_CHECK_ANCHOR
136 #define CTTYPETAB_MIN CTTYPEINFO_NUM
138 #define CTTYPETAB_MIN 128
141 /* -- C type interning ---------------------------------------------------- */
143 #define ct_hashtype(info, size) (hashrot(info, size) & CTHASH_MASK)
144 #define ct_hashname(name) \
145 (hashrot(u32ptr(name), u32ptr(name) + HASH_BIAS) & CTHASH_MASK)
147 /* Create new type element. */
148 CTypeID
lj_ctype_new(CTState
*cts
, CType
**ctp
)
150 CTypeID id
= cts
->top
;
153 if (LJ_UNLIKELY(id
>= cts
->sizetab
)) {
154 if (id
>= CTID_MAX
) lj_err_msg(cts
->L
, LJ_ERR_TABOV
);
155 #ifdef LUAJIT_CTYPE_CHECK_ANCHOR
156 ct
= lj_mem_newvec(cts
->L
, id
+1, CType
);
157 memcpy(ct
, cts
->tab
, id
*sizeof(CType
));
158 memset(cts
->tab
, 0, id
*sizeof(CType
));
159 lj_mem_freevec(cts
->g
, cts
->tab
, cts
->sizetab
, CType
);
163 lj_mem_growvec(cts
->L
, cts
->tab
, cts
->sizetab
, CTID_MAX
, CType
);
167 *ctp
= ct
= &cts
->tab
[id
];
172 setgcrefnull(ct
->name
);
176 /* Intern a type element. */
177 CTypeID
lj_ctype_intern(CTState
*cts
, CTInfo info
, CTSize size
)
179 uint32_t h
= ct_hashtype(info
, size
);
180 CTypeID id
= cts
->hash
[h
];
183 CType
*ct
= ctype_get(cts
, id
);
184 if (ct
->info
== info
&& ct
->size
== size
)
189 if (LJ_UNLIKELY(id
>= cts
->sizetab
)) {
190 #ifdef LUAJIT_CTYPE_CHECK_ANCHOR
193 if (id
>= CTID_MAX
) lj_err_msg(cts
->L
, LJ_ERR_TABOV
);
194 #ifdef LUAJIT_CTYPE_CHECK_ANCHOR
195 ct
= lj_mem_newvec(cts
->L
, id
+1, CType
);
196 memcpy(ct
, cts
->tab
, id
*sizeof(CType
));
197 memset(cts
->tab
, 0, id
*sizeof(CType
));
198 lj_mem_freevec(cts
->g
, cts
->tab
, cts
->sizetab
, CType
);
202 lj_mem_growvec(cts
->L
, cts
->tab
, cts
->sizetab
, CTID_MAX
, CType
);
206 cts
->tab
[id
].info
= info
;
207 cts
->tab
[id
].size
= size
;
208 cts
->tab
[id
].sib
= 0;
209 cts
->tab
[id
].next
= cts
->hash
[h
];
210 setgcrefnull(cts
->tab
[id
].name
);
211 cts
->hash
[h
] = (CTypeID1
)id
;
215 /* Add type element to hash table. */
216 static void ctype_addtype(CTState
*cts
, CType
*ct
, CTypeID id
)
218 uint32_t h
= ct_hashtype(ct
->info
, ct
->size
);
219 ct
->next
= cts
->hash
[h
];
220 cts
->hash
[h
] = (CTypeID1
)id
;
223 /* Add named element to hash table. */
224 void lj_ctype_addname(CTState
*cts
, CType
*ct
, CTypeID id
)
226 uint32_t h
= ct_hashname(gcref(ct
->name
));
227 ct
->next
= cts
->hash
[h
];
228 cts
->hash
[h
] = (CTypeID1
)id
;
231 /* Get a C type by name, matching the type mask. */
232 CTypeID
lj_ctype_getname(CTState
*cts
, CType
**ctp
, GCstr
*name
, uint32_t tmask
)
234 CTypeID id
= cts
->hash
[ct_hashname(name
)];
236 CType
*ct
= ctype_get(cts
, id
);
237 if (gcref(ct
->name
) == obj2gco(name
) &&
238 ((tmask
>> ctype_type(ct
->info
)) & 1)) {
244 *ctp
= &cts
->tab
[0]; /* Simplify caller logic. ctype_get() would assert. */
248 /* Get a struct/union/enum/function field by name. */
249 CType
*lj_ctype_getfieldq(CTState
*cts
, CType
*ct
, GCstr
*name
, CTSize
*ofs
,
253 ct
= ctype_get(cts
, ct
->sib
);
254 if (gcref(ct
->name
) == obj2gco(name
)) {
258 if (ctype_isxattrib(ct
->info
, CTA_SUBTYPE
)) {
259 CType
*fct
, *cct
= ctype_child(cts
, ct
);
261 while (ctype_isattrib(cct
->info
)) {
262 if (ctype_attrib(cct
->info
) == CTA_QUAL
) q
|= cct
->size
;
263 cct
= ctype_child(cts
, cct
);
265 fct
= lj_ctype_getfieldq(cts
, cct
, name
, ofs
, qual
);
267 if (qual
) *qual
|= q
;
273 return NULL
; /* Not found. */
276 /* -- C type information -------------------------------------------------- */
278 /* Follow references and get raw type for a C type ID. */
279 CType
*lj_ctype_rawref(CTState
*cts
, CTypeID id
)
281 CType
*ct
= ctype_get(cts
, id
);
282 while (ctype_isattrib(ct
->info
) || ctype_isref(ct
->info
))
283 ct
= ctype_child(cts
, ct
);
287 /* Get size for a C type ID. Does NOT support VLA/VLS. */
288 CTSize
lj_ctype_size(CTState
*cts
, CTypeID id
)
290 CType
*ct
= ctype_raw(cts
, id
);
291 return ctype_hassize(ct
->info
) ? ct
->size
: CTSIZE_INVALID
;
294 /* Get size for a variable-length C type. Does NOT support other C types. */
295 CTSize
lj_ctype_vlsize(CTState
*cts
, CType
*ct
, CTSize nelem
)
298 if (ctype_isstruct(ct
->info
)) {
299 CTypeID arrid
= 0, fid
= ct
->sib
;
300 xsz
= ct
->size
; /* Add the struct size. */
302 CType
*ctf
= ctype_get(cts
, fid
);
303 if (ctype_type(ctf
->info
) == CT_FIELD
)
304 arrid
= ctype_cid(ctf
->info
); /* Remember last field of VLS. */
307 ct
= ctype_raw(cts
, arrid
);
309 lua_assert(ctype_isvlarray(ct
->info
)); /* Must be a VLA. */
310 ct
= ctype_rawchild(cts
, ct
); /* Get array element. */
311 lua_assert(ctype_hassize(ct
->info
));
312 /* Calculate actual size of VLA and check for overflow. */
313 xsz
+= (uint64_t)ct
->size
* nelem
;
314 return xsz
< 0x80000000u
? (CTSize
)xsz
: CTSIZE_INVALID
;
317 /* Get type, qualifiers, size and alignment for a C type ID. */
318 CTInfo
lj_ctype_info(CTState
*cts
, CTypeID id
, CTSize
*szp
)
321 CType
*ct
= ctype_get(cts
, id
);
323 CTInfo info
= ct
->info
;
324 if (ctype_isenum(info
)) {
325 /* Follow child. Need to look at its attributes, too. */
326 } else if (ctype_isattrib(info
)) {
327 if (ctype_isxattrib(info
, CTA_QUAL
))
329 else if (ctype_isxattrib(info
, CTA_ALIGN
) && !(qual
& CTFP_ALIGNED
))
330 qual
|= CTFP_ALIGNED
+ CTALIGN(ct
->size
);
332 if (!(qual
& CTFP_ALIGNED
)) qual
|= (info
& CTF_ALIGN
);
333 qual
|= (info
& ~(CTF_ALIGN
|CTMASK_CID
));
334 lua_assert(ctype_hassize(info
) || ctype_isfunc(info
));
335 *szp
= ctype_isfunc(info
) ? CTSIZE_INVALID
: ct
->size
;
338 ct
= ctype_get(cts
, ctype_cid(info
));
343 /* Ditto, but follow a reference. */
344 CTInfo
lj_ctype_info_raw(CTState
*cts
, CTypeID id
, CTSize
*szp
)
346 CType
*ct
= ctype_get(cts
, id
);
347 if (ctype_isref(ct
->info
)) id
= ctype_cid(ct
->info
);
348 return lj_ctype_info(cts
, id
, szp
);
351 /* Get ctype metamethod. */
352 cTValue
*lj_ctype_meta(CTState
*cts
, CTypeID id
, MMS mm
)
354 CType
*ct
= ctype_get(cts
, id
);
356 while (ctype_isattrib(ct
->info
) || ctype_isref(ct
->info
)) {
357 id
= ctype_cid(ct
->info
);
358 ct
= ctype_get(cts
, id
);
360 if (ctype_isptr(ct
->info
) &&
361 ctype_isfunc(ctype_get(cts
, ctype_cid(ct
->info
))->info
))
362 tv
= lj_tab_getstr(cts
->miscmap
, &cts
->g
->strempty
);
364 tv
= lj_tab_getinth(cts
->miscmap
, -(int32_t)id
);
365 if (tv
&& tvistab(tv
) &&
366 (tv
= lj_tab_getstr(tabV(tv
), mmname_str(cts
->g
, mm
))) && !tvisnil(tv
))
371 /* -- C type representation ----------------------------------------------- */
373 /* Fixed max. length of a C type representation. */
374 #define CTREPR_MAX 512
376 typedef struct CTRepr
{
382 char buf
[CTREPR_MAX
];
385 /* Prepend string. */
386 static void ctype_prepstr(CTRepr
*ctr
, const char *str
, MSize len
)
389 if (ctr
->buf
+ len
+1 > p
) { ctr
->ok
= 0; return; }
390 if (ctr
->needsp
) *--p
= ' ';
393 while (len
-- > 0) p
[len
] = str
[len
];
397 #define ctype_preplit(ctr, str) ctype_prepstr((ctr), "" str, sizeof(str)-1)
400 static void ctype_prepc(CTRepr
*ctr
, int c
)
402 if (ctr
->buf
>= ctr
->pb
) { ctr
->ok
= 0; return; }
406 /* Prepend number. */
407 static void ctype_prepnum(CTRepr
*ctr
, uint32_t n
)
410 if (ctr
->buf
+ 10+1 > p
) { ctr
->ok
= 0; return; }
411 do { *--p
= (char)('0' + n
% 10); } while (n
/= 10);
417 static void ctype_appc(CTRepr
*ctr
, int c
)
419 if (ctr
->pe
>= ctr
->buf
+ CTREPR_MAX
) { ctr
->ok
= 0; return; }
424 static void ctype_appnum(CTRepr
*ctr
, uint32_t n
)
427 char *p
= buf
+sizeof(buf
);
429 if (q
> ctr
->buf
+ CTREPR_MAX
- 10) { ctr
->ok
= 0; return; }
430 do { *--p
= (char)('0' + n
% 10); } while (n
/= 10);
431 do { *q
++ = *p
++; } while (p
< buf
+sizeof(buf
));
435 /* Prepend qualifiers. */
436 static void ctype_prepqual(CTRepr
*ctr
, CTInfo info
)
438 if ((info
& CTF_VOLATILE
)) ctype_preplit(ctr
, "volatile");
439 if ((info
& CTF_CONST
)) ctype_preplit(ctr
, "const");
442 /* Prepend named type. */
443 static void ctype_preptype(CTRepr
*ctr
, CType
*ct
, CTInfo qual
, const char *t
)
445 if (gcref(ct
->name
)) {
446 GCstr
*str
= gco2str(gcref(ct
->name
));
447 ctype_prepstr(ctr
, strdata(str
), str
->len
);
449 if (ctr
->needsp
) ctype_prepc(ctr
, ' ');
450 ctype_prepnum(ctr
, ctype_typeid(ctr
->cts
, ct
));
453 ctype_prepstr(ctr
, t
, (MSize
)strlen(t
));
454 ctype_prepqual(ctr
, qual
);
457 static void ctype_repr(CTRepr
*ctr
, CTypeID id
)
459 CType
*ct
= ctype_get(ctr
->cts
, id
);
463 CTInfo info
= ct
->info
;
464 CTSize size
= ct
->size
;
465 switch (ctype_type(info
)) {
467 if ((info
& CTF_BOOL
)) {
468 ctype_preplit(ctr
, "bool");
469 } else if ((info
& CTF_FP
)) {
470 if (size
== sizeof(double)) ctype_preplit(ctr
, "double");
471 else if (size
== sizeof(float)) ctype_preplit(ctr
, "float");
472 else ctype_preplit(ctr
, "long double");
473 } else if (size
== 1) {
474 if (!((info
^ CTF_UCHAR
) & CTF_UNSIGNED
)) ctype_preplit(ctr
, "char");
475 else if (CTF_UCHAR
) ctype_preplit(ctr
, "signed char");
476 else ctype_preplit(ctr
, "unsigned char");
477 } else if (size
< 8) {
478 if (size
== 4) ctype_preplit(ctr
, "int");
479 else ctype_preplit(ctr
, "short");
480 if ((info
& CTF_UNSIGNED
)) ctype_preplit(ctr
, "unsigned");
482 ctype_preplit(ctr
, "_t");
483 ctype_prepnum(ctr
, size
*8);
484 ctype_preplit(ctr
, "int");
485 if ((info
& CTF_UNSIGNED
)) ctype_prepc(ctr
, 'u');
487 ctype_prepqual(ctr
, (qual
|info
));
490 ctype_preplit(ctr
, "void");
491 ctype_prepqual(ctr
, (qual
|info
));
494 ctype_preptype(ctr
, ct
, qual
, (info
& CTF_UNION
) ? "union" : "struct");
497 if (id
== CTID_CTYPEID
) {
498 ctype_preplit(ctr
, "ctype");
501 ctype_preptype(ctr
, ct
, qual
, "enum");
504 if (ctype_attrib(info
) == CTA_QUAL
) qual
|= size
;
507 if ((info
& CTF_REF
)) {
508 ctype_prepc(ctr
, '&');
510 ctype_prepqual(ctr
, (qual
|info
));
511 if (LJ_64
&& size
== 4) ctype_preplit(ctr
, "__ptr32");
512 ctype_prepc(ctr
, '*');
519 if (ctype_isrefarray(info
)) {
521 if (ptrto
) { ptrto
= 0; ctype_prepc(ctr
, '('); ctype_appc(ctr
, ')'); }
522 ctype_appc(ctr
, '[');
523 if (size
!= CTSIZE_INVALID
) {
524 CTSize csize
= ctype_child(ctr
->cts
, ct
)->size
;
525 ctype_appnum(ctr
, csize
? size
/csize
: 0);
526 } else if ((info
& CTF_VLA
)) {
527 ctype_appc(ctr
, '?');
529 ctype_appc(ctr
, ']');
530 } else if ((info
& CTF_COMPLEX
)) {
531 if (size
== 2*sizeof(float)) ctype_preplit(ctr
, "float");
532 ctype_preplit(ctr
, "complex");
535 ctype_preplit(ctr
, ")))");
536 ctype_prepnum(ctr
, size
);
537 ctype_preplit(ctr
, "__attribute__((vector_size(");
542 if (ptrto
) { ptrto
= 0; ctype_prepc(ctr
, '('); ctype_appc(ctr
, ')'); }
543 ctype_appc(ctr
, '(');
544 ctype_appc(ctr
, ')');
550 ct
= ctype_get(ctr
->cts
, ctype_cid(info
));
554 /* Return a printable representation of a C type. */
555 GCstr
*lj_ctype_repr(lua_State
*L
, CTypeID id
, GCstr
*name
)
557 global_State
*g
= G(L
);
559 ctr
.pb
= ctr
.pe
= &ctr
.buf
[CTREPR_MAX
/2];
560 ctr
.cts
= ctype_ctsG(g
);
564 if (name
) ctype_prepstr(&ctr
, strdata(name
), name
->len
);
565 ctype_repr(&ctr
, id
);
566 if (LJ_UNLIKELY(!ctr
.ok
)) return lj_str_newlit(L
, "?");
567 return lj_str_new(L
, ctr
.pb
, ctr
.pe
- ctr
.pb
);
570 /* Convert int64_t/uint64_t to string with 'LL' or 'ULL' suffix. */
571 GCstr
*lj_ctype_repr_int64(lua_State
*L
, uint64_t n
, int isunsigned
)
574 char *p
= buf
+sizeof(buf
);
576 *--p
= 'L'; *--p
= 'L';
579 } else if ((int64_t)n
< 0) {
583 do { *--p
= (char)('0' + n
% 10); } while (n
/= 10);
584 if (sign
) *--p
= '-';
585 return lj_str_new(L
, p
, (size_t)(buf
+sizeof(buf
)-p
));
588 /* Convert complex to string with 'i' or 'I' suffix. */
589 GCstr
*lj_ctype_repr_complex(lua_State
*L
, void *sp
, CTSize size
)
591 char buf
[2*LJ_STR_NUMBUF
+2+1];
594 if (size
== 2*sizeof(double)) {
595 re
.n
= *(double *)sp
; im
.n
= ((double *)sp
)[1];
597 re
.n
= (double)*(float *)sp
; im
.n
= (double)((float *)sp
)[1];
599 len
= lj_str_bufnum(buf
, &re
);
600 if (!(im
.u32
.hi
& 0x80000000u
) || im
.n
!= im
.n
) buf
[len
++] = '+';
601 len
+= lj_str_bufnum(buf
+len
, &im
);
602 buf
[len
] = buf
[len
-1] >= 'a' ? 'I' : 'i';
603 return lj_str_new(L
, buf
, len
+1);
606 /* -- C type state -------------------------------------------------------- */
608 /* Initialize C type table and state. */
609 CTState
*lj_ctype_init(lua_State
*L
)
611 CTState
*cts
= lj_mem_newt(L
, sizeof(CTState
), CTState
);
612 CType
*ct
= lj_mem_newvec(L
, CTTYPETAB_MIN
, CType
);
613 const char *name
= lj_ctype_typenames
;
615 memset(cts
, 0, sizeof(CTState
));
617 cts
->sizetab
= CTTYPETAB_MIN
;
618 cts
->top
= CTTYPEINFO_NUM
;
621 for (id
= 0; id
< CTTYPEINFO_NUM
; id
++, ct
++) {
622 CTInfo info
= lj_ctype_typeinfo
[id
];
623 ct
->size
= (CTSize
)((int32_t)(info
<< 16) >> 26);
624 ct
->info
= info
& 0xffff03ffu
;
626 if (ctype_type(info
) == CT_KW
|| ctype_istypedef(info
)) {
627 size_t len
= strlen(name
);
628 GCstr
*str
= lj_str_new(L
, name
, len
);
629 ctype_setname(ct
, str
);
631 lj_ctype_addname(cts
, ct
, id
);
633 setgcrefnull(ct
->name
);
635 if (!ctype_isenum(info
)) ctype_addtype(cts
, ct
, id
);
638 setmref(G(L
)->ctype_state
, cts
);
642 /* Free C type table and state. */
643 void lj_ctype_freestate(global_State
*g
)
645 CTState
*cts
= ctype_ctsG(g
);
647 lj_ccallback_mcode_free(cts
);
648 lj_mem_freevec(g
, cts
->tab
, cts
->sizetab
, CType
);
649 lj_mem_freevec(g
, cts
->cb
.cbid
, cts
->cb
.sizeid
, CTypeID1
);
650 lj_mem_freet(g
, cts
);