| 1 | /* | 
|---|
| 2 | ** C type management. | 
|---|
| 3 | ** Copyright (C) 2005-2021 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 *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. */ | 
|---|
| 184 | } CTState; | 
|---|
| 185 |  | 
|---|
| 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) | 
|---|
| 189 |  | 
|---|
| 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) | 
|---|
| 200 |  | 
|---|
| 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) | 
|---|
| 216 |  | 
|---|
| 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)) | 
|---|
| 226 |  | 
|---|
| 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)) | 
|---|
| 231 |  | 
|---|
| 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)) | 
|---|
| 238 |  | 
|---|
| 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)) | 
|---|
| 244 |  | 
|---|
| 245 | #define ctype_isxattrib(info, at) \ | 
|---|
| 246 | (((info) & (CTMASK_NUM|CTATTRIB(CTMASK_ATTRIB))) == \ | 
|---|
| 247 | CTINFO(CT_ATTRIB, CTATTRIB(at))) | 
|---|
| 248 |  | 
|---|
| 249 | /* Target-dependent sizes and alignments. */ | 
|---|
| 250 | #if LJ_64 | 
|---|
| 251 | #define CTSIZE_PTR	8 | 
|---|
| 252 | #define CTALIGN_PTR	CTALIGN(3) | 
|---|
| 253 | #else | 
|---|
| 254 | #define CTSIZE_PTR	4 | 
|---|
| 255 | #define CTALIGN_PTR	CTALIGN(2) | 
|---|
| 256 | #endif | 
|---|
| 257 |  | 
|---|
| 258 | #define CTINFO_REF(ref) \ | 
|---|
| 259 | CTINFO(CT_PTR, (CTF_CONST|CTF_REF|CTALIGN_PTR) + (ref)) | 
|---|
| 260 |  | 
|---|
| 261 | #define CT_MEMALIGN	3	/* Alignment guaranteed by memory allocator. */ | 
|---|
| 262 |  | 
|---|
| 263 | #ifdef LUA_USE_ASSERT | 
|---|
| 264 | #define lj_assertCTS(c, ...)	(lj_assertG_(cts->g, (c), __VA_ARGS__)) | 
|---|
| 265 | #else | 
|---|
| 266 | #define lj_assertCTS(c, ...)	((void)cts) | 
|---|
| 267 | #endif | 
|---|
| 268 |  | 
|---|
| 269 | /* -- Predefined types ---------------------------------------------------- */ | 
|---|
| 270 |  | 
|---|
| 271 | /* Target-dependent types. */ | 
|---|
| 272 | #if LJ_TARGET_PPC | 
|---|
| 273 | #define CTTYDEFP(_) \ | 
|---|
| 274 | _(LINT32,		4,	CT_NUM, CTF_LONG|CTALIGN(2)) | 
|---|
| 275 | #else | 
|---|
| 276 | #define CTTYDEFP(_) | 
|---|
| 277 | #endif | 
|---|
| 278 |  | 
|---|
| 279 | /* Common types. */ | 
|---|
| 280 | #define CTTYDEF(_) \ | 
|---|
| 281 | _(NONE,		0,	CT_ATTRIB, CTATTRIB(CTA_BAD)) \ | 
|---|
| 282 | _(VOID,		-1,	CT_VOID, CTALIGN(0)) \ | 
|---|
| 283 | _(CVOID,		-1,	CT_VOID, CTF_CONST|CTALIGN(0)) \ | 
|---|
| 284 | _(BOOL,		1,	CT_NUM, CTF_BOOL|CTF_UNSIGNED|CTALIGN(0)) \ | 
|---|
| 285 | _(CCHAR,		1,	CT_NUM, CTF_CONST|CTF_UCHAR|CTALIGN(0)) \ | 
|---|
| 286 | _(INT8,		1,	CT_NUM, CTALIGN(0)) \ | 
|---|
| 287 | _(UINT8,		1,	CT_NUM, CTF_UNSIGNED|CTALIGN(0)) \ | 
|---|
| 288 | _(INT16,		2,	CT_NUM, CTALIGN(1)) \ | 
|---|
| 289 | _(UINT16,		2,	CT_NUM, CTF_UNSIGNED|CTALIGN(1)) \ | 
|---|
| 290 | _(INT32,		4,	CT_NUM, CTALIGN(2)) \ | 
|---|
| 291 | _(UINT32,		4,	CT_NUM, CTF_UNSIGNED|CTALIGN(2)) \ | 
|---|
| 292 | _(INT64,		8,	CT_NUM, CTF_LONG|CTALIGN(3)) \ | 
|---|
| 293 | _(UINT64,		8,	CT_NUM, CTF_UNSIGNED|CTF_LONG|CTALIGN(3)) \ | 
|---|
| 294 | _(FLOAT,		4,	CT_NUM, CTF_FP|CTALIGN(2)) \ | 
|---|
| 295 | _(DOUBLE,		8,	CT_NUM, CTF_FP|CTALIGN(3)) \ | 
|---|
| 296 | _(COMPLEX_FLOAT,	8,	CT_ARRAY, CTF_COMPLEX|CTALIGN(2)|CTID_FLOAT) \ | 
|---|
| 297 | _(COMPLEX_DOUBLE,	16,	CT_ARRAY, CTF_COMPLEX|CTALIGN(3)|CTID_DOUBLE) \ | 
|---|
| 298 | _(P_VOID,	CTSIZE_PTR,	CT_PTR, CTALIGN_PTR|CTID_VOID) \ | 
|---|
| 299 | _(P_CVOID,	CTSIZE_PTR,	CT_PTR, CTALIGN_PTR|CTID_CVOID) \ | 
|---|
| 300 | _(P_CCHAR,	CTSIZE_PTR,	CT_PTR, CTALIGN_PTR|CTID_CCHAR) \ | 
|---|
| 301 | _(A_CCHAR,		-1,	CT_ARRAY, CTF_CONST|CTALIGN(0)|CTID_CCHAR) \ | 
|---|
| 302 | _(CTYPEID,		4,	CT_ENUM, CTALIGN(2)|CTID_INT32) \ | 
|---|
| 303 | CTTYDEFP(_) \ | 
|---|
| 304 | /* End of type list. */ | 
|---|
| 305 |  | 
|---|
| 306 | /* Public predefined type IDs. */ | 
|---|
| 307 | enum { | 
|---|
| 308 | #define CTTYIDDEF(id, sz, ct, info)	CTID_##id, | 
|---|
| 309 | CTTYDEF(CTTYIDDEF) | 
|---|
| 310 | #undef CTTYIDDEF | 
|---|
| 311 | /* Predefined typedefs and keywords follow. */ | 
|---|
| 312 | CTID_MAX = 65536 | 
|---|
| 313 | }; | 
|---|
| 314 |  | 
|---|
| 315 | /* Target-dependent type IDs. */ | 
|---|
| 316 | #if LJ_64 | 
|---|
| 317 | #define CTID_INT_PSZ	CTID_INT64 | 
|---|
| 318 | #define CTID_UINT_PSZ	CTID_UINT64 | 
|---|
| 319 | #else | 
|---|
| 320 | #define CTID_INT_PSZ	CTID_INT32 | 
|---|
| 321 | #define CTID_UINT_PSZ	CTID_UINT32 | 
|---|
| 322 | #endif | 
|---|
| 323 |  | 
|---|
| 324 | #if LJ_ABI_WIN | 
|---|
| 325 | #define CTID_WCHAR	CTID_UINT16 | 
|---|
| 326 | #elif LJ_TARGET_PPC | 
|---|
| 327 | #define CTID_WCHAR	CTID_LINT32 | 
|---|
| 328 | #else | 
|---|
| 329 | #define CTID_WCHAR	CTID_INT32 | 
|---|
| 330 | #endif | 
|---|
| 331 |  | 
|---|
| 332 | /* -- C tokens and keywords ----------------------------------------------- */ | 
|---|
| 333 |  | 
|---|
| 334 | /* C lexer keywords. */ | 
|---|
| 335 | #define CTOKDEF(_) \ | 
|---|
| 336 | _(IDENT, "<identifier>") _(STRING, "<string>") \ | 
|---|
| 337 | _(INTEGER, "<integer>") _(EOF, "<eof>") \ | 
|---|
| 338 | _(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \ | 
|---|
| 339 | _(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->") | 
|---|
| 340 |  | 
|---|
| 341 | /* Simple declaration specifiers. */ | 
|---|
| 342 | #define CDSDEF(_) \ | 
|---|
| 343 | _(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \ | 
|---|
| 344 | _(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \ | 
|---|
| 345 | _(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \ | 
|---|
| 346 | _(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER) | 
|---|
| 347 |  | 
|---|
| 348 | /* C keywords. */ | 
|---|
| 349 | #define CKWDEF(_) \ | 
|---|
| 350 | CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \ | 
|---|
| 351 | _(DECLSPEC) _(CCDECL) _(PTRSZ) \ | 
|---|
| 352 | _(STRUCT) _(UNION) _(ENUM) \ | 
|---|
| 353 | _(SIZEOF) _(ALIGNOF) | 
|---|
| 354 |  | 
|---|
| 355 | /* C token numbers. */ | 
|---|
| 356 | enum { | 
|---|
| 357 | CTOK_OFS = 255, | 
|---|
| 358 | #define CTOKNUM(name, sym)	CTOK_##name, | 
|---|
| 359 | #define CKWNUM(name)		CTOK_##name, | 
|---|
| 360 | CTOKDEF(CTOKNUM) | 
|---|
| 361 | CKWDEF(CKWNUM) | 
|---|
| 362 | #undef CTOKNUM | 
|---|
| 363 | #undef CKWNUM | 
|---|
| 364 | CTOK_FIRSTDECL = CTOK_VOID, | 
|---|
| 365 | CTOK_FIRSTSCL = CTOK_TYPEDEF, | 
|---|
| 366 | CTOK_LASTDECLFLAG = CTOK_REGISTER, | 
|---|
| 367 | CTOK_LASTDECL = CTOK_ENUM | 
|---|
| 368 | }; | 
|---|
| 369 |  | 
|---|
| 370 | /* Declaration specifier flags. */ | 
|---|
| 371 | enum { | 
|---|
| 372 | #define CDSFLAG(name)	CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)), | 
|---|
| 373 | CDSDEF(CDSFLAG) | 
|---|
| 374 | #undef CDSFLAG | 
|---|
| 375 | CDF__END | 
|---|
| 376 | }; | 
|---|
| 377 |  | 
|---|
| 378 | #define CDF_SCL  (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER) | 
|---|
| 379 |  | 
|---|
| 380 | /* -- C type management --------------------------------------------------- */ | 
|---|
| 381 |  | 
|---|
| 382 | #define ctype_ctsG(g)		(mref((g)->ctype_state, CTState)) | 
|---|
| 383 |  | 
|---|
| 384 | /* Get C type state. */ | 
|---|
| 385 | static LJ_AINLINE CTState *ctype_cts(lua_State *L) | 
|---|
| 386 | { | 
|---|
| 387 | CTState *cts = ctype_ctsG(G(L)); | 
|---|
| 388 | cts->L = L;  /* Save L for errors and allocations. */ | 
|---|
| 389 | return cts; | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | /* Save and restore state of C type table. */ | 
|---|
| 393 | #define LJ_CTYPE_SAVE(cts)	CTState savects_ = *(cts) | 
|---|
| 394 | #define LJ_CTYPE_RESTORE(cts) \ | 
|---|
| 395 | ((cts)->top = savects_.top, \ | 
|---|
| 396 | memcpy((cts)->hash, savects_.hash, sizeof(savects_.hash))) | 
|---|
| 397 |  | 
|---|
| 398 | /* Check C type ID for validity when assertions are enabled. */ | 
|---|
| 399 | static LJ_AINLINE CTypeID ctype_check(CTState *cts, CTypeID id) | 
|---|
| 400 | { | 
|---|
| 401 | UNUSED(cts); | 
|---|
| 402 | lj_assertCTS(id > 0 && id < cts->top, "bad CTID %d", id); | 
|---|
| 403 | return id; | 
|---|
| 404 | } | 
|---|
| 405 |  | 
|---|
| 406 | /* Get C type for C type ID. */ | 
|---|
| 407 | static LJ_AINLINE CType *ctype_get(CTState *cts, CTypeID id) | 
|---|
| 408 | { | 
|---|
| 409 | return &cts->tab[ctype_check(cts, id)]; | 
|---|
| 410 | } | 
|---|
| 411 |  | 
|---|
| 412 | /* Get C type ID for a C type. */ | 
|---|
| 413 | #define ctype_typeid(cts, ct)	((CTypeID)((ct) - (cts)->tab)) | 
|---|
| 414 |  | 
|---|
| 415 | /* Get child C type. */ | 
|---|
| 416 | static LJ_AINLINE CType *ctype_child(CTState *cts, CType *ct) | 
|---|
| 417 | { | 
|---|
| 418 | lj_assertCTS(!(ctype_isvoid(ct->info) || ctype_isstruct(ct->info) || | 
|---|
| 419 | ctype_isbitfield(ct->info)), | 
|---|
| 420 | "ctype %08x has no children", ct->info); | 
|---|
| 421 | return ctype_get(cts, ctype_cid(ct->info)); | 
|---|
| 422 | } | 
|---|
| 423 |  | 
|---|
| 424 | /* Get raw type for a C type ID. */ | 
|---|
| 425 | static LJ_AINLINE CType *ctype_raw(CTState *cts, CTypeID id) | 
|---|
| 426 | { | 
|---|
| 427 | CType *ct = ctype_get(cts, id); | 
|---|
| 428 | while (ctype_isattrib(ct->info)) ct = ctype_child(cts, ct); | 
|---|
| 429 | return ct; | 
|---|
| 430 | } | 
|---|
| 431 |  | 
|---|
| 432 | /* Get raw type of the child of a C type. */ | 
|---|
| 433 | static LJ_AINLINE CType *ctype_rawchild(CTState *cts, CType *ct) | 
|---|
| 434 | { | 
|---|
| 435 | do { ct = ctype_child(cts, ct); } while (ctype_isattrib(ct->info)); | 
|---|
| 436 | return ct; | 
|---|
| 437 | } | 
|---|
| 438 |  | 
|---|
| 439 | /* Set the name of a C type table element. */ | 
|---|
| 440 | static LJ_AINLINE void ctype_setname(CType *ct, GCstr *s) | 
|---|
| 441 | { | 
|---|
| 442 | /* NOBARRIER: mark string as fixed -- the C type table is never collected. */ | 
|---|
| 443 | fixstring(s); | 
|---|
| 444 | setgcref(ct->name, obj2gco(s)); | 
|---|
| 445 | } | 
|---|
| 446 |  | 
|---|
| 447 | LJ_FUNC CTypeID lj_ctype_new(CTState *cts, CType **ctp); | 
|---|
| 448 | LJ_FUNC CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size); | 
|---|
| 449 | LJ_FUNC void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id); | 
|---|
| 450 | LJ_FUNC CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, | 
|---|
| 451 | uint32_t tmask); | 
|---|
| 452 | LJ_FUNC CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, | 
|---|
| 453 | CTSize *ofs, CTInfo *qual); | 
|---|
| 454 | #define lj_ctype_getfield(cts, ct, name, ofs) \ | 
|---|
| 455 | lj_ctype_getfieldq((cts), (ct), (name), (ofs), NULL) | 
|---|
| 456 | LJ_FUNC CType *lj_ctype_rawref(CTState *cts, CTypeID id); | 
|---|
| 457 | LJ_FUNC CTSize lj_ctype_size(CTState *cts, CTypeID id); | 
|---|
| 458 | LJ_FUNC CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem); | 
|---|
| 459 | LJ_FUNC CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp); | 
|---|
| 460 | LJ_FUNC cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm); | 
|---|
| 461 | LJ_FUNC GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name); | 
|---|
| 462 | LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned); | 
|---|
| 463 | LJ_FUNC GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size); | 
|---|
| 464 | LJ_FUNC CTState *lj_ctype_init(lua_State *L); | 
|---|
| 465 | LJ_FUNC void lj_ctype_freestate(global_State *g); | 
|---|
| 466 |  | 
|---|
| 467 | #endif | 
|---|
| 468 |  | 
|---|
| 469 | #endif | 
|---|
| 470 |  | 
|---|