3 ** Copyright (C) 2005-2012 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. ORDER CC */
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)
154 /* Simplify target-specific configuration. Checked in lj_ccall.h. */
155 #define CCALL_MAX_GPR 8
156 #define CCALL_MAX_FPR 8
158 typedef LJ_ALIGN(8) union FPRCBArg
{ double d
; float f
[2]; } FPRCBArg
;
160 /* C callback state. Defined here, to avoid dragging in lj_ccall.h. */
162 typedef LJ_ALIGN(8) struct CCallback
{
163 FPRCBArg fpr
[CCALL_MAX_FPR
]; /* Arguments/results in FPRs. */
164 intptr_t gpr
[CCALL_MAX_GPR
]; /* Arguments/results in GPRs. */
165 intptr_t *stack
; /* Pointer to arguments on stack. */
166 void *mcode
; /* Machine code for callback func. pointers. */
167 CTypeID1
*cbid
; /* Callback type table. */
168 MSize sizeid
; /* Size of callback type table. */
169 MSize topid
; /* Highest unused callback type table slot. */
170 MSize slot
; /* Current callback slot. */
174 typedef struct CTState
{
175 CType
*tab
; /* C type table. */
176 CTypeID top
; /* Current top of C type table. */
177 MSize sizetab
; /* Size of C type table. */
178 lua_State
*L
; /* Lua state (needed for errors and allocations). */
179 global_State
*g
; /* Global state. */
180 GCtab
*finalizer
; /* Map of cdata to finalizer. */
181 GCtab
*miscmap
; /* Map of -CTypeID to metatable and cb slot to func. */
182 CCallback cb
; /* Temporary callback state. */
183 CTypeID1 hash
[CTHASH_SIZE
]; /* Hash anchors for C type table. */
186 #define CTINFO(ct, flags) (((CTInfo)(ct) << CTSHIFT_NUM) + (flags))
187 #define CTALIGN(al) ((CTSize)(al) << CTSHIFT_ALIGN)
188 #define CTATTRIB(at) ((CTInfo)(at) << CTSHIFT_ATTRIB)
190 #define ctype_type(info) ((info) >> CTSHIFT_NUM)
191 #define ctype_cid(info) ((CTypeID)((info) & CTMASK_CID))
192 #define ctype_align(info) (((info) >> CTSHIFT_ALIGN) & CTMASK_ALIGN)
193 #define ctype_attrib(info) (((info) >> CTSHIFT_ATTRIB) & CTMASK_ATTRIB)
194 #define ctype_bitpos(info) (((info) >> CTSHIFT_BITPOS) & CTMASK_BITPOS)
195 #define ctype_bitbsz(info) (((info) >> CTSHIFT_BITBSZ) & CTMASK_BITBSZ)
196 #define ctype_bitcsz(info) (((info) >> CTSHIFT_BITCSZ) & CTMASK_BITCSZ)
197 #define ctype_vsizeP(info) (((info) >> CTSHIFT_VSIZEP) & CTMASK_VSIZEP)
198 #define ctype_msizeP(info) (((info) >> CTSHIFT_MSIZEP) & CTMASK_MSIZEP)
199 #define ctype_cconv(info) (((info) >> CTSHIFT_CCONV) & CTMASK_CCONV)
201 /* Simple type checks. */
202 #define ctype_isnum(info) (ctype_type((info)) == CT_NUM)
203 #define ctype_isvoid(info) (ctype_type((info)) == CT_VOID)
204 #define ctype_isptr(info) (ctype_type((info)) == CT_PTR)
205 #define ctype_isarray(info) (ctype_type((info)) == CT_ARRAY)
206 #define ctype_isstruct(info) (ctype_type((info)) == CT_STRUCT)
207 #define ctype_isfunc(info) (ctype_type((info)) == CT_FUNC)
208 #define ctype_isenum(info) (ctype_type((info)) == CT_ENUM)
209 #define ctype_istypedef(info) (ctype_type((info)) == CT_TYPEDEF)
210 #define ctype_isattrib(info) (ctype_type((info)) == CT_ATTRIB)
211 #define ctype_isfield(info) (ctype_type((info)) == CT_FIELD)
212 #define ctype_isbitfield(info) (ctype_type((info)) == CT_BITFIELD)
213 #define ctype_isconstval(info) (ctype_type((info)) == CT_CONSTVAL)
214 #define ctype_isextern(info) (ctype_type((info)) == CT_EXTERN)
215 #define ctype_hassize(info) (ctype_type((info)) <= CT_HASSIZE)
217 /* Combined type and flag checks. */
218 #define ctype_isinteger(info) \
219 (((info) & (CTMASK_NUM|CTF_BOOL|CTF_FP)) == CTINFO(CT_NUM, 0))
220 #define ctype_isinteger_or_bool(info) \
221 (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0))
222 #define ctype_isbool(info) \
223 (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL))
224 #define ctype_isfp(info) \
225 (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, CTF_FP))
227 #define ctype_ispointer(info) \
228 ((ctype_type(info) >> 1) == (CT_PTR >> 1)) /* Pointer or array. */
229 #define ctype_isref(info) \
230 (((info) & (CTMASK_NUM|CTF_REF)) == CTINFO(CT_PTR, CTF_REF))
232 #define ctype_isrefarray(info) \
233 (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0))
234 #define ctype_isvector(info) \
235 (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR))
236 #define ctype_iscomplex(info) \
237 (((info) & (CTMASK_NUM|CTF_COMPLEX)) == CTINFO(CT_ARRAY, CTF_COMPLEX))
239 #define ctype_isvltype(info) \
240 (((info) & ((CTMASK_NUM|CTF_VLA) - (2u<<CTSHIFT_NUM))) == \
241 CTINFO(CT_STRUCT, CTF_VLA)) /* VL array or VL struct. */
242 #define ctype_isvlarray(info) \
243 (((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_ARRAY, CTF_VLA))
245 #define ctype_isxattrib(info, at) \
246 (((info) & (CTMASK_NUM|CTATTRIB(CTMASK_ATTRIB))) == \
247 CTINFO(CT_ATTRIB, CTATTRIB(at)))
249 /* Target-dependent sizes and alignments. */
252 #define CTALIGN_PTR CTALIGN(3)
255 #define CTALIGN_PTR CTALIGN(2)
258 #define CTINFO_REF(ref) \
259 CTINFO(CT_PTR, (CTF_CONST|CTF_REF|CTALIGN_PTR) + (ref))
261 #define CT_MEMALIGN 3 /* Alignment guaranteed by memory allocator. */
263 /* -- Predefined types ---------------------------------------------------- */
265 /* Target-dependent types. */
266 #if LJ_TARGET_PPC || LJ_TARGET_PPCSPE
267 #define CTTYDEFP(_) \
268 _(LINT32, 4, CT_NUM, CTF_LONG|CTALIGN(2))
275 _(NONE, 0, CT_ATTRIB, CTATTRIB(CTA_BAD)) \
276 _(VOID, -1, CT_VOID, CTALIGN(0)) \
277 _(CVOID, -1, CT_VOID, CTF_CONST|CTALIGN(0)) \
278 _(BOOL, 1, CT_NUM, CTF_BOOL|CTF_UNSIGNED|CTALIGN(0)) \
279 _(CCHAR, 1, CT_NUM, CTF_CONST|CTF_UCHAR|CTALIGN(0)) \
280 _(INT8, 1, CT_NUM, CTALIGN(0)) \
281 _(UINT8, 1, CT_NUM, CTF_UNSIGNED|CTALIGN(0)) \
282 _(INT16, 2, CT_NUM, CTALIGN(1)) \
283 _(UINT16, 2, CT_NUM, CTF_UNSIGNED|CTALIGN(1)) \
284 _(INT32, 4, CT_NUM, CTALIGN(2)) \
285 _(UINT32, 4, CT_NUM, CTF_UNSIGNED|CTALIGN(2)) \
286 _(INT64, 8, CT_NUM, CTF_LONG|CTALIGN(3)) \
287 _(UINT64, 8, CT_NUM, CTF_UNSIGNED|CTF_LONG|CTALIGN(3)) \
288 _(FLOAT, 4, CT_NUM, CTF_FP|CTALIGN(2)) \
289 _(DOUBLE, 8, CT_NUM, CTF_FP|CTALIGN(3)) \
290 _(COMPLEX_FLOAT, 8, CT_ARRAY, CTF_COMPLEX|CTALIGN(2)|CTID_FLOAT) \
291 _(COMPLEX_DOUBLE, 16, CT_ARRAY, CTF_COMPLEX|CTALIGN(3)|CTID_DOUBLE) \
292 _(P_VOID, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_VOID) \
293 _(P_CVOID, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_CVOID) \
294 _(P_CCHAR, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_CCHAR) \
295 _(A_CCHAR, -1, CT_ARRAY, CTF_CONST|CTALIGN(0)|CTID_CCHAR) \
296 _(CTYPEID, 4, CT_ENUM, CTALIGN(2)|CTID_INT32) \
298 /* End of type list. */
300 /* Public predefined type IDs. */
302 #define CTTYIDDEF(id, sz, ct, info) CTID_##id,
305 /* Predefined typedefs and keywords follow. */
309 /* Target-dependent type IDs. */
311 #define CTID_INT_PSZ CTID_INT64
312 #define CTID_UINT_PSZ CTID_UINT64
314 #define CTID_INT_PSZ CTID_INT32
315 #define CTID_UINT_PSZ CTID_UINT32
319 #define CTID_WCHAR CTID_UINT16
321 #define CTID_WCHAR CTID_LINT32
323 #define CTID_WCHAR CTID_INT32
326 /* -- C tokens and keywords ----------------------------------------------- */
328 /* C lexer keywords. */
330 _(IDENT, "<identifier>") _(STRING, "<string>") \
331 _(INTEGER, "<integer>") _(EOF, "<eof>") \
332 _(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \
333 _(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->")
335 /* Simple declaration specifiers. */
337 _(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \
338 _(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \
339 _(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \
340 _(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER)
344 CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \
345 _(DECLSPEC) _(CCDECL) _(PTRSZ) \
346 _(STRUCT) _(UNION) _(ENUM) \
349 /* C token numbers. */
352 #define CTOKNUM(name, sym) CTOK_##name,
353 #define CKWNUM(name) CTOK_##name,
358 CTOK_FIRSTDECL
= CTOK_VOID
,
359 CTOK_FIRSTSCL
= CTOK_TYPEDEF
,
360 CTOK_LASTDECLFLAG
= CTOK_REGISTER
,
361 CTOK_LASTDECL
= CTOK_ENUM
364 /* Declaration specifier flags. */
366 #define CDSFLAG(name) CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)),
372 #define CDF_SCL (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER)
374 /* -- C type management --------------------------------------------------- */
376 #define ctype_ctsG(g) (mref((g)->ctype_state, CTState))
378 /* Get C type state. */
379 static LJ_AINLINE CTState
*ctype_cts(lua_State
*L
)
381 CTState
*cts
= ctype_ctsG(G(L
));
382 cts
->L
= L
; /* Save L for errors and allocations. */
386 /* Save and restore state of C type table. */
387 #define LJ_CTYPE_SAVE(cts) CTState savects_ = *(cts)
388 #define LJ_CTYPE_RESTORE(cts) \
389 ((cts)->top = savects_.top, \
390 memcpy((cts)->hash, savects_.hash, sizeof(savects_.hash)))
392 /* Check C type ID for validity when assertions are enabled. */
393 static LJ_AINLINE CTypeID
ctype_check(CTState
*cts
, CTypeID id
)
395 lua_assert(id
> 0 && id
< cts
->top
); UNUSED(cts
);
399 /* Get C type for C type ID. */
400 static LJ_AINLINE CType
*ctype_get(CTState
*cts
, CTypeID id
)
402 return &cts
->tab
[ctype_check(cts
, id
)];
405 /* Get C type ID for a C type. */
406 #define ctype_typeid(cts, ct) ((CTypeID)((ct) - (cts)->tab))
408 /* Get child C type. */
409 static LJ_AINLINE CType
*ctype_child(CTState
*cts
, CType
*ct
)
411 lua_assert(!(ctype_isvoid(ct
->info
) || ctype_isstruct(ct
->info
) ||
412 ctype_isbitfield(ct
->info
))); /* These don't have children. */
413 return ctype_get(cts
, ctype_cid(ct
->info
));
416 /* Get raw type for a C type ID. */
417 static LJ_AINLINE CType
*ctype_raw(CTState
*cts
, CTypeID id
)
419 CType
*ct
= ctype_get(cts
, id
);
420 while (ctype_isattrib(ct
->info
)) ct
= ctype_child(cts
, ct
);
424 /* Get raw type of the child of a C type. */
425 static LJ_AINLINE CType
*ctype_rawchild(CTState
*cts
, CType
*ct
)
427 do { ct
= ctype_child(cts
, ct
); } while (ctype_isattrib(ct
->info
));
431 /* Set the name of a C type table element. */
432 static LJ_AINLINE
void ctype_setname(CType
*ct
, GCstr
*s
)
434 /* NOBARRIER: mark string as fixed -- the C type table is never collected. */
436 setgcref(ct
->name
, obj2gco(s
));
439 LJ_FUNC CTypeID
lj_ctype_new(CTState
*cts
, CType
**ctp
);
440 LJ_FUNC CTypeID
lj_ctype_intern(CTState
*cts
, CTInfo info
, CTSize size
);
441 LJ_FUNC
void lj_ctype_addname(CTState
*cts
, CType
*ct
, CTypeID id
);
442 LJ_FUNC CTypeID
lj_ctype_getname(CTState
*cts
, CType
**ctp
, GCstr
*name
,
444 LJ_FUNC CType
*lj_ctype_getfield(CTState
*cts
, CType
*ct
, GCstr
*name
,
446 LJ_FUNC CType
*lj_ctype_rawref(CTState
*cts
, CTypeID id
);
447 LJ_FUNC CTSize
lj_ctype_size(CTState
*cts
, CTypeID id
);
448 LJ_FUNC CTSize
lj_ctype_vlsize(CTState
*cts
, CType
*ct
, CTSize nelem
);
449 LJ_FUNC CTInfo
lj_ctype_info(CTState
*cts
, CTypeID id
, CTSize
*szp
);
450 LJ_FUNC cTValue
*lj_ctype_meta(CTState
*cts
, CTypeID id
, MMS mm
);
451 LJ_FUNC GCstr
*lj_ctype_repr(lua_State
*L
, CTypeID id
, GCstr
*name
);
452 LJ_FUNC GCstr
*lj_ctype_repr_int64(lua_State
*L
, uint64_t n
, int isunsigned
);
453 LJ_FUNC GCstr
*lj_ctype_repr_complex(lua_State
*L
, void *sp
, CTSize size
);
454 LJ_FUNC CTState
*lj_ctype_init(lua_State
*L
);
455 LJ_FUNC
void lj_ctype_freestate(global_State
*g
);