3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
14 /* -- C type definitions -------------------------------------------------- */
16 /* C type numbers. Highest 4 bits of C type info. ORDER CT. */
18 /* Externally visible types. */
19 CT_NUM
, /* Integer or floating-point numbers. */
20 CT_STRUCT
, /* Struct or union. */
21 CT_PTR
, /* Pointer or reference. */
22 CT_ARRAY
, /* Array or complex type. */
23 CT_MAYCONVERT
= CT_ARRAY
,
24 CT_VOID
, /* Void type. */
25 CT_ENUM
, /* Enumeration. */
26 CT_HASSIZE
= CT_ENUM
, /* Last type where ct->size holds the actual size. */
27 CT_FUNC
, /* Function. */
28 CT_TYPEDEF
, /* Typedef. */
29 CT_ATTRIB
, /* Miscellaneous attributes. */
30 /* Internal element types. */
31 CT_FIELD
, /* Struct/union field or function parameter. */
32 CT_BITFIELD
, /* Struct/union bitfield. */
33 CT_CONSTVAL
, /* Constant value. */
34 CT_EXTERN
, /* External reference. */
38 LJ_STATIC_ASSERT(((int)CT_PTR
& (int)CT_ARRAY
) == CT_PTR
);
39 LJ_STATIC_ASSERT(((int)CT_STRUCT
& (int)CT_ARRAY
) == CT_STRUCT
);
42 ** ---------- info ------------
43 ** |type flags... A cid | size | sib | next | name |
44 ** +----------------------------+--------+-------+-------+-------+--
45 ** |NUM BFvcUL.. A | size | | type | |
46 ** |STRUCT ..vcU..V A | size | field | name? | name? |
47 ** |PTR ..vcR... A cid | size | | type | |
48 ** |ARRAY VCvc...V A cid | size | | type | |
49 ** |VOID ..vc.... A | size | | type | |
50 ** |ENUM A cid | size | const | name? | name? |
51 ** |FUNC ....VS.. cc cid | nargs | field | name? | name? |
52 ** |TYPEDEF cid | | | name | name |
53 ** |ATTRIB attrnum cid | attr | sib? | type? | |
54 ** |FIELD cid | offset | field | | name? |
55 ** |BITFIELD B.vcU csz bsz pos | offset | field | | name? |
56 ** |CONSTVAL c cid | value | const | name | name |
57 ** |EXTERN cid | | sib? | name | name |
58 ** |KW tok | size | | name | name |
59 ** +----------------------------+--------+-------+-------+-------+--
60 ** ^^ ^^--- bits used for C type conversion dispatch
63 /* C type info flags. TFFArrrr */
64 #define CTF_BOOL 0x08000000u /* Boolean: NUM, BITFIELD. */
65 #define CTF_FP 0x04000000u /* Floating-point: NUM. */
66 #define CTF_CONST 0x02000000u /* Const qualifier. */
67 #define CTF_VOLATILE 0x01000000u /* Volatile qualifier. */
68 #define CTF_UNSIGNED 0x00800000u /* Unsigned: NUM, BITFIELD. */
69 #define CTF_LONG 0x00400000u /* Long: NUM. */
70 #define CTF_VLA 0x00100000u /* Variable-length: ARRAY, STRUCT. */
71 #define CTF_REF 0x00800000u /* Reference: PTR. */
72 #define CTF_VECTOR 0x08000000u /* Vector: ARRAY. */
73 #define CTF_COMPLEX 0x04000000u /* Complex: ARRAY. */
74 #define CTF_UNION 0x00800000u /* Union: STRUCT. */
75 #define CTF_VARARG 0x00800000u /* Vararg: FUNC. */
76 #define CTF_SSEREGPARM 0x00400000u /* SSE register parameters: FUNC. */
78 #define CTF_QUAL (CTF_CONST|CTF_VOLATILE)
79 #define CTF_ALIGN (CTMASK_ALIGN<<CTSHIFT_ALIGN)
80 #define CTF_UCHAR ((char)-1 > 0 ? CTF_UNSIGNED : 0)
82 /* Flags used in parser. .F.Ammvf cp->attr */
83 #define CTFP_ALIGNED 0x00000001u /* cp->attr + ALIGN */
84 #define CTFP_PACKED 0x00000002u /* cp->attr */
85 /* ...C...f cp->fattr */
86 #define CTFP_CCONV 0x00000001u /* cp->fattr + CCONV/[SSE]REGPARM */
88 /* C type info bitfields. */
89 #define CTMASK_CID 0x0000ffffu /* Max. 65536 type IDs. */
90 #define CTMASK_NUM 0xf0000000u /* Max. 16 type numbers. */
91 #define CTSHIFT_NUM 28
92 #define CTMASK_ALIGN 15 /* Max. alignment is 2^15. */
93 #define CTSHIFT_ALIGN 16
94 #define CTMASK_ATTRIB 255 /* Max. 256 attributes. */
95 #define CTSHIFT_ATTRIB 16
96 #define CTMASK_CCONV 3 /* Max. 4 calling conventions. */
97 #define CTSHIFT_CCONV 16
98 #define CTMASK_REGPARM 3 /* Max. 0-3 regparms. */
99 #define CTSHIFT_REGPARM 18
100 /* Bitfields only used in parser. */
101 #define CTMASK_VSIZEP 15 /* Max. vector size is 2^15. */
102 #define CTSHIFT_VSIZEP 4
103 #define CTMASK_MSIZEP 255 /* Max. type size (via mode) is 128. */
104 #define CTSHIFT_MSIZEP 8
106 /* Info bits for BITFIELD. Max. size of bitfield is 64 bits. */
107 #define CTBSZ_MAX 32 /* Max. size of bitfield is 32 bit. */
108 #define CTBSZ_FIELD 127 /* Temp. marker for regular field. */
109 #define CTMASK_BITPOS 127
110 #define CTMASK_BITBSZ 127
111 #define CTMASK_BITCSZ 127
112 #define CTSHIFT_BITPOS 0
113 #define CTSHIFT_BITBSZ 8
114 #define CTSHIFT_BITCSZ 16
116 #define CTF_INSERT(info, field, val) \
117 info = (info & ~(CTMASK_##field<<CTSHIFT_##field)) | \
118 (((CTSize)(val) & CTMASK_##field) << CTSHIFT_##field)
120 /* Calling conventions. */
121 enum { CTCC_CDECL
, CTCC_THISCALL
, CTCC_FASTCALL
, CTCC_STDCALL
};
123 /* Attribute numbers. */
125 CTA_NONE
, /* Ignored attribute. Must be zero. */
126 CTA_QUAL
, /* Unmerged qualifiers. */
127 CTA_ALIGN
, /* Alignment override. */
128 CTA_SUBTYPE
, /* Transparent sub-type. */
129 CTA_REDIR
, /* Redirected symbol name. */
130 CTA_BAD
, /* To catch bad IDs. */
135 #define CTSIZE_INVALID 0xffffffffu
137 typedef uint32_t CTInfo
; /* Type info. */
138 typedef uint32_t CTSize
; /* Type size. */
139 typedef uint32_t CTypeID
; /* Type ID. */
140 typedef uint16_t CTypeID1
; /* Minimum-sized type ID. */
142 /* C type table element. */
143 typedef struct CType
{
144 CTInfo info
; /* Type info. */
145 CTSize size
; /* Type size or other info. */
146 CTypeID1 sib
; /* Sibling element. */
147 CTypeID1 next
; /* Next element in hash chain. */
148 GCRef name
; /* Element name (GCstr). */
151 #define CTHASH_SIZE 128 /* Number of hash anchors. */
152 #define CTHASH_MASK (CTHASH_SIZE-1)
155 typedef struct CTState
{
156 CType
*tab
; /* C type table. */
157 CTypeID top
; /* Current top of C type table. */
158 MSize sizetab
; /* Size of C type table. */
159 lua_State
*L
; /* Lua state (needed for errors and allocations). */
160 global_State
*g
; /* Global state. */
161 GCtab
*finalizer
; /* Map of cdata to finalizer. */
162 GCtab
*metatype
; /* Map of CTypeID to metatable. */
163 CTypeID1 hash
[CTHASH_SIZE
]; /* Hash anchors for C type table. */
166 #define CTINFO(ct, flags) (((CTInfo)(ct) << CTSHIFT_NUM) + (flags))
167 #define CTALIGN(al) ((CTSize)(al) << CTSHIFT_ALIGN)
168 #define CTATTRIB(at) ((CTInfo)(at) << CTSHIFT_ATTRIB)
170 #define ctype_type(info) ((info) >> CTSHIFT_NUM)
171 #define ctype_cid(info) ((CTypeID)((info) & CTMASK_CID))
172 #define ctype_align(info) (((info) >> CTSHIFT_ALIGN) & CTMASK_ALIGN)
173 #define ctype_attrib(info) (((info) >> CTSHIFT_ATTRIB) & CTMASK_ATTRIB)
174 #define ctype_bitpos(info) (((info) >> CTSHIFT_BITPOS) & CTMASK_BITPOS)
175 #define ctype_bitbsz(info) (((info) >> CTSHIFT_BITBSZ) & CTMASK_BITBSZ)
176 #define ctype_bitcsz(info) (((info) >> CTSHIFT_BITCSZ) & CTMASK_BITCSZ)
177 #define ctype_vsizeP(info) (((info) >> CTSHIFT_VSIZEP) & CTMASK_VSIZEP)
178 #define ctype_msizeP(info) (((info) >> CTSHIFT_MSIZEP) & CTMASK_MSIZEP)
179 #define ctype_cconv(info) (((info) >> CTSHIFT_CCONV) & CTMASK_CCONV)
181 /* Simple type checks. */
182 #define ctype_isnum(info) (ctype_type((info)) == CT_NUM)
183 #define ctype_isvoid(info) (ctype_type((info)) == CT_VOID)
184 #define ctype_isptr(info) (ctype_type((info)) == CT_PTR)
185 #define ctype_isarray(info) (ctype_type((info)) == CT_ARRAY)
186 #define ctype_isstruct(info) (ctype_type((info)) == CT_STRUCT)
187 #define ctype_isfunc(info) (ctype_type((info)) == CT_FUNC)
188 #define ctype_isenum(info) (ctype_type((info)) == CT_ENUM)
189 #define ctype_istypedef(info) (ctype_type((info)) == CT_TYPEDEF)
190 #define ctype_isattrib(info) (ctype_type((info)) == CT_ATTRIB)
191 #define ctype_isfield(info) (ctype_type((info)) == CT_FIELD)
192 #define ctype_isbitfield(info) (ctype_type((info)) == CT_BITFIELD)
193 #define ctype_isconstval(info) (ctype_type((info)) == CT_CONSTVAL)
194 #define ctype_isextern(info) (ctype_type((info)) == CT_EXTERN)
195 #define ctype_hassize(info) (ctype_type((info)) <= CT_HASSIZE)
197 /* Combined type and flag checks. */
198 #define ctype_isinteger(info) \
199 (((info) & (CTMASK_NUM|CTF_BOOL|CTF_FP)) == CTINFO(CT_NUM, 0))
200 #define ctype_isinteger_or_bool(info) \
201 (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0))
202 #define ctype_isbool(info) \
203 (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL))
204 #define ctype_isfp(info) \
205 (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, CTF_FP))
207 #define ctype_ispointer(info) \
208 ((ctype_type(info) >> 1) == (CT_PTR >> 1)) /* Pointer or array. */
209 #define ctype_isref(info) \
210 (((info) & (CTMASK_NUM|CTF_REF)) == CTINFO(CT_PTR, CTF_REF))
212 #define ctype_isrefarray(info) \
213 (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0))
214 #define ctype_isvector(info) \
215 (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR))
216 #define ctype_iscomplex(info) \
217 (((info) & (CTMASK_NUM|CTF_COMPLEX)) == CTINFO(CT_ARRAY, CTF_COMPLEX))
219 #define ctype_isvltype(info) \
220 (((info) & ((CTMASK_NUM|CTF_VLA) - (2u<<CTSHIFT_NUM))) == \
221 CTINFO(CT_STRUCT, CTF_VLA)) /* VL array or VL struct. */
222 #define ctype_isvlarray(info) \
223 (((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_ARRAY, CTF_VLA))
225 #define ctype_isxattrib(info, at) \
226 (((info) & (CTMASK_NUM|CTATTRIB(CTMASK_ATTRIB))) == \
227 CTINFO(CT_ATTRIB, CTATTRIB(at)))
229 /* Target-dependent sizes and alignments. */
232 #define CTALIGN_PTR CTALIGN(3)
235 #define CTALIGN_PTR CTALIGN(2)
238 #define CTINFO_REF(ref) \
239 CTINFO(CT_PTR, (CTF_CONST|CTF_REF|CTALIGN_PTR) + (ref))
241 #define CT_MEMALIGN 3 /* Alignment guaranteed by memory allocator. */
243 /* -- Predefined types ---------------------------------------------------- */
245 /* Target-dependent types. */
246 #if LJ_TARGET_PPC || LJ_TARGET_PPCSPE
247 #define CTTYDEFP(_) \
248 _(LINT32, 4, CT_NUM, CTF_LONG|CTALIGN(2))
255 _(NONE, 0, CT_ATTRIB, CTATTRIB(CTA_BAD)) \
256 _(VOID, -1, CT_VOID, CTALIGN(0)) \
257 _(CVOID, -1, CT_VOID, CTF_CONST|CTALIGN(0)) \
258 _(BOOL, 1, CT_NUM, CTF_BOOL|CTF_UNSIGNED|CTALIGN(0)) \
259 _(CCHAR, 1, CT_NUM, CTF_CONST|CTF_UCHAR|CTALIGN(0)) \
260 _(INT8, 1, CT_NUM, CTALIGN(0)) \
261 _(UINT8, 1, CT_NUM, CTF_UNSIGNED|CTALIGN(0)) \
262 _(INT16, 2, CT_NUM, CTALIGN(1)) \
263 _(UINT16, 2, CT_NUM, CTF_UNSIGNED|CTALIGN(1)) \
264 _(INT32, 4, CT_NUM, CTALIGN(2)) \
265 _(UINT32, 4, CT_NUM, CTF_UNSIGNED|CTALIGN(2)) \
266 _(INT64, 8, CT_NUM, CTF_LONG|CTALIGN(3)) \
267 _(UINT64, 8, CT_NUM, CTF_UNSIGNED|CTF_LONG|CTALIGN(3)) \
268 _(FLOAT, 4, CT_NUM, CTF_FP|CTALIGN(2)) \
269 _(DOUBLE, 8, CT_NUM, CTF_FP|CTALIGN(3)) \
270 _(COMPLEX_FLOAT, 8, CT_ARRAY, CTF_COMPLEX|CTALIGN(2)|CTID_FLOAT) \
271 _(COMPLEX_DOUBLE, 16, CT_ARRAY, CTF_COMPLEX|CTALIGN(3)|CTID_DOUBLE) \
272 _(P_VOID, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_VOID) \
273 _(P_CVOID, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_CVOID) \
274 _(P_CCHAR, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_CCHAR) \
275 _(A_CCHAR, -1, CT_ARRAY, CTF_CONST|CTALIGN(0)|CTID_CCHAR) \
276 _(CTYPEID, 4, CT_ENUM, CTALIGN(2)|CTID_INT32) \
278 /* End of type list. */
280 /* Public predefined type IDs. */
282 #define CTTYIDDEF(id, sz, ct, info) CTID_##id,
285 /* Predefined typedefs and keywords follow. */
289 /* Target-dependent type IDs. */
291 #define CTID_INT_PSZ CTID_INT64
292 #define CTID_UINT_PSZ CTID_UINT64
294 #define CTID_INT_PSZ CTID_INT32
295 #define CTID_UINT_PSZ CTID_UINT32
299 #define CTID_WCHAR CTID_UINT16
301 #define CTID_WCHAR CTID_LINT32
303 #define CTID_WCHAR CTID_INT32
306 /* -- C tokens and keywords ----------------------------------------------- */
308 /* C lexer keywords. */
310 _(IDENT, "<identifier>") _(STRING, "<string>") \
311 _(INTEGER, "<integer>") _(EOF, "<eof>") \
312 _(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \
313 _(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->")
315 /* Simple declaration specifiers. */
317 _(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \
318 _(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \
319 _(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \
320 _(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER)
324 CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \
325 _(DECLSPEC) _(CCDECL) _(PTRSZ) \
326 _(STRUCT) _(UNION) _(ENUM) \
329 /* C token numbers. */
332 #define CTOKNUM(name, sym) CTOK_##name,
333 #define CKWNUM(name) CTOK_##name,
338 CTOK_FIRSTDECL
= CTOK_VOID
,
339 CTOK_FIRSTSCL
= CTOK_TYPEDEF
,
340 CTOK_LASTDECLFLAG
= CTOK_REGISTER
,
341 CTOK_LASTDECL
= CTOK_ENUM
344 /* Declaration specifier flags. */
346 #define CDSFLAG(name) CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)),
352 #define CDF_SCL (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER)
354 /* -- C type management --------------------------------------------------- */
356 #define ctype_ctsG(g) (mref((g)->ctype_state, CTState))
358 /* Get C type state. */
359 static LJ_AINLINE CTState
*ctype_cts(lua_State
*L
)
361 CTState
*cts
= ctype_ctsG(G(L
));
362 cts
->L
= L
; /* Save L for errors and allocations. */
366 /* Save and restore state of C type table. */
367 #define LJ_CTYPE_SAVE(cts) CTState savects_ = *(cts)
368 #define LJ_CTYPE_RESTORE(cts) \
369 ((cts)->top = savects_.top, \
370 memcpy((cts)->hash, savects_.hash, sizeof(savects_.hash)))
372 /* Check C type ID for validity when assertions are enabled. */
373 static LJ_AINLINE CTypeID
ctype_check(CTState
*cts
, CTypeID id
)
375 lua_assert(id
> 0 && id
< cts
->top
); UNUSED(cts
);
379 /* Get C type for C type ID. */
380 static LJ_AINLINE CType
*ctype_get(CTState
*cts
, CTypeID id
)
382 return &cts
->tab
[ctype_check(cts
, id
)];
385 /* Get C type ID for a C type. */
386 #define ctype_typeid(cts, ct) ((CTypeID)((ct) - (cts)->tab))
388 /* Get child C type. */
389 static LJ_AINLINE CType
*ctype_child(CTState
*cts
, CType
*ct
)
391 lua_assert(!(ctype_isvoid(ct
->info
) || ctype_isstruct(ct
->info
) ||
392 ctype_isbitfield(ct
->info
))); /* These don't have children. */
393 return ctype_get(cts
, ctype_cid(ct
->info
));
396 /* Get raw type for a C type ID. */
397 static LJ_AINLINE CType
*ctype_raw(CTState
*cts
, CTypeID id
)
399 CType
*ct
= ctype_get(cts
, id
);
400 while (ctype_isattrib(ct
->info
)) ct
= ctype_child(cts
, ct
);
404 /* Get raw type of the child of a C type. */
405 static LJ_AINLINE CType
*ctype_rawchild(CTState
*cts
, CType
*ct
)
407 do { ct
= ctype_child(cts
, ct
); } while (ctype_isattrib(ct
->info
));
411 /* Set the name of a C type table element. */
412 static LJ_AINLINE
void ctype_setname(CType
*ct
, GCstr
*s
)
414 /* NOBARRIER: mark string as fixed -- the C type table is never collected. */
416 setgcref(ct
->name
, obj2gco(s
));
419 LJ_FUNC CTypeID
lj_ctype_new(CTState
*cts
, CType
**ctp
);
420 LJ_FUNC CTypeID
lj_ctype_intern(CTState
*cts
, CTInfo info
, CTSize size
);
421 LJ_FUNC
void lj_ctype_addname(CTState
*cts
, CType
*ct
, CTypeID id
);
422 LJ_FUNC CTypeID
lj_ctype_getname(CTState
*cts
, CType
**ctp
, GCstr
*name
,
424 LJ_FUNC CType
*lj_ctype_getfield(CTState
*cts
, CType
*ct
, GCstr
*name
,
426 LJ_FUNC CType
*lj_ctype_rawref(CTState
*cts
, CTypeID id
);
427 LJ_FUNC CTSize
lj_ctype_size(CTState
*cts
, CTypeID id
);
428 LJ_FUNC CTSize
lj_ctype_vlsize(CTState
*cts
, CType
*ct
, CTSize nelem
);
429 LJ_FUNC CTInfo
lj_ctype_info(CTState
*cts
, CTypeID id
, CTSize
*szp
);
430 LJ_FUNC cTValue
*lj_ctype_meta(CTState
*cts
, CTypeID id
, MMS mm
);
431 LJ_FUNC GCstr
*lj_ctype_repr(lua_State
*L
, CTypeID id
, GCstr
*name
);
432 LJ_FUNC GCstr
*lj_ctype_repr_int64(lua_State
*L
, uint64_t n
, int isunsigned
);
433 LJ_FUNC GCstr
*lj_ctype_repr_complex(lua_State
*L
, void *sp
, CTSize size
);
434 LJ_FUNC CTState
*lj_ctype_init(lua_State
*L
);
435 LJ_FUNC
void lj_ctype_freestate(global_State
*g
);