| 1 |
/* |
| 2 |
** C type management. |
| 3 |
** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef _LJ_CTYPE_H |
| 7 |
#define _LJ_CTYPE_H |
| 8 |
|
| 9 |
#include "lj_obj.h" |
| 10 |
#include "lj_gc.h" |
| 11 |
|
| 12 |
#if LJ_HASFFI |
| 13 |
|
| 14 |
/* -- C type definitions -------------------------------------------------- */ |
| 15 |
|
| 16 |
/* C type numbers. Highest 4 bits of C type info. ORDER CT. */ |
| 17 |
enum { |
| 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. */ |
| 35 |
CT_KW /* Keyword. */ |
| 36 |
}; |
| 37 |
|
| 38 |
LJ_STATIC_ASSERT(((int)CT_PTR & (int)CT_ARRAY) == CT_PTR); |
| 39 |
LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT); |
| 40 |
|
| 41 |
/* |
| 42 |
** ---------- info ------------ |
| 43 |
** |type flags... A cid | size | sib | next | name | |
| 44 |
** +----------------------------+--------+-------+-------+-------+-- |
| 45 |
** |NUM BFcvUL.. A | size | | type | | |
| 46 |
** |STRUCT ..cvU..V A | size | field | name? | name? | |
| 47 |
** |PTR ..cvR... A cid | size | | type | | |
| 48 |
** |ARRAY VCcv...V A cid | size | | type | | |
| 49 |
** |VOID ..cv.... 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.cvU 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 |
| 61 |
*/ |
| 62 |
|
| 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. */ |
| 77 |
|
| 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) |
| 81 |
|
| 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 */ |
| 87 |
|
| 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 |
| 105 |
|
| 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 |
| 115 |
|
| 116 |
#define CTF_INSERT(info, field, val) \ |
| 117 |
info = (info & ~(CTMASK_##field<<CTSHIFT_##field)) | \ |
| 118 |
(((CTSize)(val) & CTMASK_##field) << CTSHIFT_##field) |
| 119 |
|
| 120 |
/* Calling conventions. ORDER CC */ |
| 121 |
enum { CTCC_CDECL, CTCC_THISCALL, CTCC_FASTCALL, CTCC_STDCALL }; |
| 122 |
|
| 123 |
/* Attribute numbers. */ |
| 124 |
enum { |
| 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. */ |
| 131 |
CTA__MAX |
| 132 |
}; |
| 133 |
|
| 134 |
/* Special sizes. */ |
| 135 |
#define CTSIZE_INVALID 0xffffffffu |
| 136 |
|
| 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. */ |
| 141 |
|
| 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). */ |
| 149 |
} CType; |
| 150 |
|
| 151 |
#define CTHASH_SIZE 128 /* Number of hash anchors. */ |
| 152 |
#define CTHASH_MASK (CTHASH_SIZE-1) |
| 153 |
|
| 154 |
/* Simplify target-specific configuration. Checked in lj_ccall.h. */ |
| 155 |
#define CCALL_MAX_GPR 8 |
| 156 |
#define CCALL_MAX_FPR 8 |
| 157 |
|
| 158 |
typedef LJ_ALIGN(8) union FPRCBArg { double d; float f[2]; } FPRCBArg; |
| 159 |
|
| 160 |
/* C callback state. Defined here, to avoid dragging in lj_ccall.h. */ |
| 161 |
|
| 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. */ |
| 171 |
} CCallback; |
| 172 |
|
| 173 |
/* C type state. */ |
| 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 *miscmap; /* Map of -CTypeID to metatable and cb slot to func. */ |
| 181 |
CCallback cb; /* Temporary callback state. */ |
| 182 |
CTypeID1 hash[CTHASH_SIZE]; /* Hash anchors for C type table. */ |
| 183 |
} CTState; |
| 184 |
|
| 185 |
#define CTINFO(ct, flags) (((CTInfo)(ct) << CTSHIFT_NUM) + (flags)) |
| 186 |
#define CTALIGN(al) ((CTSize)(al) << CTSHIFT_ALIGN) |
| 187 |
#define CTATTRIB(at) ((CTInfo)(at) << CTSHIFT_ATTRIB) |
| 188 |
|
| 189 |
#define ctype_type(info) ((info) >> CTSHIFT_NUM) |
| 190 |
#define ctype_cid(info) ((CTypeID)((info) & CTMASK_CID)) |
| 191 |
#define ctype_align(info) (((info) >> CTSHIFT_ALIGN) & CTMASK_ALIGN) |
| 192 |
#define ctype_attrib(info) (((info) >> CTSHIFT_ATTRIB) & CTMASK_ATTRIB) |
| 193 |
#define ctype_bitpos(info) (((info) >> CTSHIFT_BITPOS) & CTMASK_BITPOS) |
| 194 |
#define ctype_bitbsz(info) (((info) >> CTSHIFT_BITBSZ) & CTMASK_BITBSZ) |
| 195 |
#define ctype_bitcsz(info) (((info) >> CTSHIFT_BITCSZ) & CTMASK_BITCSZ) |
| 196 |
#define ctype_vsizeP(info) (((info) >> CTSHIFT_VSIZEP) & CTMASK_VSIZEP) |
| 197 |
#define ctype_msizeP(info) (((info) >> CTSHIFT_MSIZEP) & CTMASK_MSIZEP) |
| 198 |
#define ctype_cconv(info) (((info) >> CTSHIFT_CCONV) & CTMASK_CCONV) |
| 199 |
|
| 200 |
/* Simple type checks. */ |
| 201 |
#define ctype_isnum(info) (ctype_type((info)) == CT_NUM) |
| 202 |
#define ctype_isvoid(info) (ctype_type((info)) == CT_VOID) |
| 203 |
#define ctype_isptr(info) (ctype_type((info)) == CT_PTR) |
| 204 |
#define ctype_isarray(info) (ctype_type((info)) == CT_ARRAY) |
| 205 |
#define ctype_isstruct(info) (ctype_type((info)) == CT_STRUCT) |
| 206 |
#define ctype_isfunc(info) (ctype_type((info)) == CT_FUNC) |
| 207 |
#define ctype_isenum(info) (ctype_type((info)) == CT_ENUM) |
| 208 |
#define ctype_istypedef(info) (ctype_type((info)) == CT_TYPEDEF) |
| 209 |
#define ctype_isattrib(info) (ctype_type((info)) == CT_ATTRIB) |
| 210 |
#define ctype_isfield(info) (ctype_type((info)) == CT_FIELD) |
| 211 |
#define ctype_isbitfield(info) (ctype_type((info)) == CT_BITFIELD) |
| 212 |
#define ctype_isconstval(info) (ctype_type((info)) == CT_CONSTVAL) |
| 213 |
#define ctype_isextern(info) (ctype_type((info)) == CT_EXTERN) |
| 214 |
#define ctype_hassize(info) (ctype_type((info)) <= CT_HASSIZE) |
| 215 |
|
| 216 |
/* Combined type and flag checks. */ |
| 217 |
#define ctype_isinteger(info) \ |
| 218 |
(((info) & (CTMASK_NUM|CTF_BOOL|CTF_FP)) == CTINFO(CT_NUM, 0)) |
| 219 |
#define ctype_isinteger_or_bool(info) \ |
| 220 |
(((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0)) |
| 221 |
#define ctype_isbool(info) \ |
| 222 |
(((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL)) |
| 223 |
#define ctype_isfp(info) \ |
| 224 |
(((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, CTF_FP)) |
| 225 |
|
| 226 |
#define ctype_ispointer(info) \ |
| 227 |
((ctype_type(info) >> 1) == (CT_PTR >> 1)) /* Pointer or array. */ |
| 228 |
#define ctype_isref(info) \ |
| 229 |
(((info) & (CTMASK_NUM|CTF_REF)) == CTINFO(CT_PTR, CTF_REF)) |
| 230 |
|
| 231 |
#define ctype_isrefarray(info) \ |
| 232 |
(((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0)) |
| 233 |
#define ctype_isvector(info) \ |
| 234 |
(((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR)) |
| 235 |
#define ctype_iscomplex(info) \ |
| 236 |
(((info) & (CTMASK_NUM|CTF_COMPLEX)) == CTINFO(CT_ARRAY, CTF_COMPLEX)) |
| 237 |
|
| 238 |
#define ctype_isvltype(info) \ |
| 239 |
(((info) & ((CTMASK_NUM|CTF_VLA) - (2u<<CTSHIFT_NUM))) == \ |
| 240 |
CTINFO(CT_STRUCT, CTF_VLA)) /* VL array or VL struct. */ |
| 241 |
#define ctype_isvlarray(info) \ |
| 242 |
(((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_ARRAY, CTF_VLA)) |
| 243 |
|
| 244 |
#define ctype_isxattrib(info, at) \ |
| 245 |
(((info) & (CTMASK_NUM|CTATTRIB(CTMASK_ATTRIB))) == \ |
| 246 |
CTINFO(CT_ATTRIB, CTATTRIB(at))) |
| 247 |
|
| 248 |
/* Target-dependent sizes and alignments. */ |
| 249 |
#if LJ_64 |
| 250 |
#define CTSIZE_PTR 8 |
| 251 |
#define CTALIGN_PTR CTALIGN(3) |
| 252 |
#else |
| 253 |
#define CTSIZE_PTR 4 |
| 254 |
#define CTALIGN_PTR CTALIGN(2) |
| 255 |
#endif |
| 256 |
|
| 257 |
#define CTINFO_REF(ref) \ |
| 258 |
CTINFO(CT_PTR, (CTF_CONST|CTF_REF|CTALIGN_PTR) + (ref)) |
| 259 |
|
| 260 |
#define CT_MEMALIGN 3 /* Alignment guaranteed by memory allocator. */ |
| 261 |
|
| 262 |
#ifdef LUA_USE_ASSERT |
| 263 |
#define lj_assertCTS(c, ...) (lj_assertG_(cts->g, (c), __VA_ARGS__)) |
| 264 |
#else |
| 265 |
#define lj_assertCTS(c, ...) ((void)cts) |
| 266 |
#endif |
| 267 |
|
| 268 |
/* -- Predefined types ---------------------------------------------------- */ |
| 269 |
|
| 270 |
/* Target-dependent types. */ |
| 271 |
#if LJ_TARGET_PPC |
| 272 |
#define CTTYDEFP(_) \ |
| 273 |
_(LINT32, 4, CT_NUM, CTF_LONG|CTALIGN(2)) |
| 274 |
#else |
| 275 |
#define CTTYDEFP(_) |
| 276 |
#endif |
| 277 |
|
| 278 |
#define CTF_LONG_IF8 (CTF_LONG * (sizeof(long) == 8)) |
| 279 |
|
| 280 |
/* Common types. */ |
| 281 |
#define CTTYDEF(_) \ |
| 282 |
_(NONE, 0, CT_ATTRIB, CTATTRIB(CTA_BAD)) \ |
| 283 |
_(VOID, -1, CT_VOID, CTALIGN(0)) \ |
| 284 |
_(CVOID, -1, CT_VOID, CTF_CONST|CTALIGN(0)) \ |
| 285 |
_(BOOL, 1, CT_NUM, CTF_BOOL|CTF_UNSIGNED|CTALIGN(0)) \ |
| 286 |
_(CCHAR, 1, CT_NUM, CTF_CONST|CTF_UCHAR|CTALIGN(0)) \ |
| 287 |
_(INT8, 1, CT_NUM, CTALIGN(0)) \ |
| 288 |
_(UINT8, 1, CT_NUM, CTF_UNSIGNED|CTALIGN(0)) \ |
| 289 |
_(INT16, 2, CT_NUM, CTALIGN(1)) \ |
| 290 |
_(UINT16, 2, CT_NUM, CTF_UNSIGNED|CTALIGN(1)) \ |
| 291 |
_(INT32, 4, CT_NUM, CTALIGN(2)) \ |
| 292 |
_(UINT32, 4, CT_NUM, CTF_UNSIGNED|CTALIGN(2)) \ |
| 293 |
_(INT64, 8, CT_NUM, CTF_LONG_IF8|CTALIGN(3)) \ |
| 294 |
_(UINT64, 8, CT_NUM, CTF_UNSIGNED|CTF_LONG_IF8|CTALIGN(3)) \ |
| 295 |
_(FLOAT, 4, CT_NUM, CTF_FP|CTALIGN(2)) \ |
| 296 |
_(DOUBLE, 8, CT_NUM, CTF_FP|CTALIGN(3)) \ |
| 297 |
_(COMPLEX_FLOAT, 8, CT_ARRAY, CTF_COMPLEX|CTALIGN(2)|CTID_FLOAT) \ |
| 298 |
_(COMPLEX_DOUBLE, 16, CT_ARRAY, CTF_COMPLEX|CTALIGN(3)|CTID_DOUBLE) \ |
| 299 |
_(P_VOID, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_VOID) \ |
| 300 |
_(P_CVOID, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_CVOID) \ |
| 301 |
_(P_CCHAR, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_CCHAR) \ |
| 302 |
_(P_UINT8, CTSIZE_PTR, CT_PTR, CTALIGN_PTR|CTID_UINT8) \ |
| 303 |
_(A_CCHAR, -1, CT_ARRAY, CTF_CONST|CTALIGN(0)|CTID_CCHAR) \ |
| 304 |
_(CTYPEID, 4, CT_ENUM, CTALIGN(2)|CTID_INT32) \ |
| 305 |
CTTYDEFP(_) \ |
| 306 |
/* End of type list. */ |
| 307 |
|
| 308 |
/* Public predefined type IDs. */ |
| 309 |
enum { |
| 310 |
#define CTTYIDDEF(id, sz, ct, info) CTID_##id, |
| 311 |
CTTYDEF(CTTYIDDEF) |
| 312 |
#undef CTTYIDDEF |
| 313 |
/* Predefined typedefs and keywords follow. */ |
| 314 |
CTID_MAX = 65536 |
| 315 |
}; |
| 316 |
|
| 317 |
/* Target-dependent type IDs. */ |
| 318 |
#if LJ_64 |
| 319 |
#define CTID_INT_PSZ CTID_INT64 |
| 320 |
#define CTID_UINT_PSZ CTID_UINT64 |
| 321 |
#else |
| 322 |
#define CTID_INT_PSZ CTID_INT32 |
| 323 |
#define CTID_UINT_PSZ CTID_UINT32 |
| 324 |
#endif |
| 325 |
|
| 326 |
#if LJ_ABI_WIN |
| 327 |
#define CTID_WCHAR CTID_UINT16 |
| 328 |
#elif LJ_TARGET_PPC |
| 329 |
#define CTID_WCHAR CTID_LINT32 |
| 330 |
#else |
| 331 |
#define CTID_WCHAR CTID_INT32 |
| 332 |
#endif |
| 333 |
|
| 334 |
/* -- C tokens and keywords ----------------------------------------------- */ |
| 335 |
|
| 336 |
/* C lexer keywords. */ |
| 337 |
#define CTOKDEF(_) \ |
| 338 |
_(IDENT, "<identifier>") _(STRING, "<string>") \ |
| 339 |
_(INTEGER, "<integer>") _(EOF, "<eof>") \ |
| 340 |
_(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \ |
| 341 |
_(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->") |
| 342 |
|
| 343 |
/* Simple declaration specifiers. */ |
| 344 |
#define CDSDEF(_) \ |
| 345 |
_(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \ |
| 346 |
_(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \ |
| 347 |
_(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \ |
| 348 |
_(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER) |
| 349 |
|
| 350 |
/* C keywords. */ |
| 351 |
#define CKWDEF(_) \ |
| 352 |
CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \ |
| 353 |
_(DECLSPEC) _(CCDECL) _(PTRSZ) \ |
| 354 |
_(STRUCT) _(UNION) _(ENUM) \ |
| 355 |
_(SIZEOF) _(ALIGNOF) |
| 356 |
|
| 357 |
/* C token numbers. */ |
| 358 |
enum { |
| 359 |
CTOK_OFS = 255, |
| 360 |
#define CTOKNUM(name, sym) CTOK_##name, |
| 361 |
#define CKWNUM(name) CTOK_##name, |
| 362 |
CTOKDEF(CTOKNUM) |
| 363 |
CKWDEF(CKWNUM) |
| 364 |
#undef CTOKNUM |
| 365 |
#undef CKWNUM |
| 366 |
CTOK_FIRSTDECL = CTOK_VOID, |
| 367 |
CTOK_FIRSTSCL = CTOK_TYPEDEF, |
| 368 |
CTOK_LASTDECLFLAG = CTOK_REGISTER, |
| 369 |
CTOK_LASTDECL = CTOK_ENUM |
| 370 |
}; |
| 371 |
|
| 372 |
/* Declaration specifier flags. */ |
| 373 |
enum { |
| 374 |
#define CDSFLAG(name) CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)), |
| 375 |
CDSDEF(CDSFLAG) |
| 376 |
#undef CDSFLAG |
| 377 |
CDF__END |
| 378 |
}; |
| 379 |
|
| 380 |
#define CDF_SCL (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER) |
| 381 |
|
| 382 |
/* -- C type management --------------------------------------------------- */ |
| 383 |
|
| 384 |
#define ctype_ctsG(g) (mref((g)->ctype_state, CTState)) |
| 385 |
|
| 386 |
/* Get C type state. */ |
| 387 |
static LJ_AINLINE CTState *ctype_cts(lua_State *L) |
| 388 |
{ |
| 389 |
CTState *cts = ctype_ctsG(G(L)); |
| 390 |
cts->L = L; /* Save L for errors and allocations. */ |
| 391 |
return cts; |
| 392 |
} |
| 393 |
|
| 394 |
/* Load FFI library on-demand. */ |
| 395 |
#define ctype_loadffi(L) \ |
| 396 |
do { \ |
| 397 |
if (!ctype_ctsG(G(L))) { \ |
| 398 |
ptrdiff_t oldtop = (char *)L->top - mref(L->stack, char); \ |
| 399 |
luaopen_ffi(L); \ |
| 400 |
L->top = (TValue *)(mref(L->stack, char) + oldtop); \ |
| 401 |
} \ |
| 402 |
} while (0) |
| 403 |
|
| 404 |
/* Save and restore state of C type table. */ |
| 405 |
#define LJ_CTYPE_SAVE(cts) CTState savects_ = *(cts) |
| 406 |
#define LJ_CTYPE_RESTORE(cts) \ |
| 407 |
((cts)->top = savects_.top, \ |
| 408 |
memcpy((cts)->hash, savects_.hash, sizeof(savects_.hash))) |
| 409 |
|
| 410 |
/* Check C type ID for validity when assertions are enabled. */ |
| 411 |
static LJ_AINLINE CTypeID ctype_check(CTState *cts, CTypeID id) |
| 412 |
{ |
| 413 |
UNUSED(cts); |
| 414 |
lj_assertCTS(id > 0 && id < cts->top, "bad CTID %d", id); |
| 415 |
return id; |
| 416 |
} |
| 417 |
|
| 418 |
/* Get C type for C type ID. */ |
| 419 |
static LJ_AINLINE CType *ctype_get(CTState *cts, CTypeID id) |
| 420 |
{ |
| 421 |
return &cts->tab[ctype_check(cts, id)]; |
| 422 |
} |
| 423 |
|
| 424 |
/* Get C type ID for a C type. */ |
| 425 |
#define ctype_typeid(cts, ct) ((CTypeID)((ct) - (cts)->tab)) |
| 426 |
|
| 427 |
/* Get child C type. */ |
| 428 |
static LJ_AINLINE CType *ctype_child(CTState *cts, CType *ct) |
| 429 |
{ |
| 430 |
lj_assertCTS(!(ctype_isvoid(ct->info) || ctype_isstruct(ct->info) || |
| 431 |
ctype_isbitfield(ct->info)), |
| 432 |
"ctype %08x has no children", ct->info); |
| 433 |
return ctype_get(cts, ctype_cid(ct->info)); |
| 434 |
} |
| 435 |
|
| 436 |
/* Get raw type for a C type ID. */ |
| 437 |
static LJ_AINLINE CType *ctype_raw(CTState *cts, CTypeID id) |
| 438 |
{ |
| 439 |
CType *ct = ctype_get(cts, id); |
| 440 |
while (ctype_isattrib(ct->info)) ct = ctype_child(cts, ct); |
| 441 |
return ct; |
| 442 |
} |
| 443 |
|
| 444 |
/* Get raw type of the child of a C type. */ |
| 445 |
static LJ_AINLINE CType *ctype_rawchild(CTState *cts, CType *ct) |
| 446 |
{ |
| 447 |
do { ct = ctype_child(cts, ct); } while (ctype_isattrib(ct->info)); |
| 448 |
return ct; |
| 449 |
} |
| 450 |
|
| 451 |
/* Set the name of a C type table element. */ |
| 452 |
static LJ_AINLINE void ctype_setname(CType *ct, GCstr *s) |
| 453 |
{ |
| 454 |
/* NOBARRIER: mark string as fixed -- the C type table is never collected. */ |
| 455 |
fixstring(s); |
| 456 |
setgcref(ct->name, obj2gco(s)); |
| 457 |
} |
| 458 |
|
| 459 |
LJ_FUNC CTypeID lj_ctype_new(CTState *cts, CType **ctp); |
| 460 |
LJ_FUNC CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size); |
| 461 |
LJ_FUNC void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id); |
| 462 |
LJ_FUNC CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, |
| 463 |
uint32_t tmask); |
| 464 |
LJ_FUNC CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, |
| 465 |
CTSize *ofs, CTInfo *qual); |
| 466 |
#define lj_ctype_getfield(cts, ct, name, ofs) \ |
| 467 |
lj_ctype_getfieldq((cts), (ct), (name), (ofs), NULL) |
| 468 |
LJ_FUNC CType *lj_ctype_rawref(CTState *cts, CTypeID id); |
| 469 |
LJ_FUNC CTSize lj_ctype_size(CTState *cts, CTypeID id); |
| 470 |
LJ_FUNC CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem); |
| 471 |
LJ_FUNC CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp); |
| 472 |
LJ_FUNC CTInfo lj_ctype_info_raw(CTState *cts, CTypeID id, CTSize *szp); |
| 473 |
LJ_FUNC cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm); |
| 474 |
LJ_FUNC GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name); |
| 475 |
LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned); |
| 476 |
LJ_FUNC GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size); |
| 477 |
LJ_FUNC CTState *lj_ctype_init(lua_State *L); |
| 478 |
LJ_FUNC void lj_ctype_initfin(lua_State *L); |
| 479 |
LJ_FUNC void lj_ctype_freestate(global_State *g); |
| 480 |
|
| 481 |
#endif |
| 482 |
|
| 483 |
#endif |