diff options
author | David Walter Seikel | 2012-01-23 23:36:30 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-23 23:36:30 +1000 |
commit | 6523585c66c04cea54df50013df8886b589847d8 (patch) | |
tree | 0b22aee7064166d88595eda260ca2d17c0773da5 /libraries/luajit-2.0/src/lj_ctype.c | |
parent | Update the EFL to what I'm actually using, coz I'm using some stuff not yet r... (diff) | |
download | SledjHamr-6523585c66c04cea54df50013df8886b589847d8.zip SledjHamr-6523585c66c04cea54df50013df8886b589847d8.tar.gz SledjHamr-6523585c66c04cea54df50013df8886b589847d8.tar.bz2 SledjHamr-6523585c66c04cea54df50013df8886b589847d8.tar.xz |
Add luaproc and LuaJIT libraries.
Two versions of LuaJIT, the stable release, and the dev version. Try the dev version first, until ih fails badly.
Diffstat (limited to '')
-rw-r--r-- | libraries/luajit-2.0/src/lj_ctype.c | 607 |
1 files changed, 607 insertions, 0 deletions
diff --git a/libraries/luajit-2.0/src/lj_ctype.c b/libraries/luajit-2.0/src/lj_ctype.c new file mode 100644 index 0000000..7187d6f --- /dev/null +++ b/libraries/luajit-2.0/src/lj_ctype.c | |||
@@ -0,0 +1,607 @@ | |||
1 | /* | ||
2 | ** C type management. | ||
3 | ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | #include "lj_obj.h" | ||
7 | |||
8 | #if LJ_HASFFI | ||
9 | |||
10 | #include "lj_gc.h" | ||
11 | #include "lj_err.h" | ||
12 | #include "lj_str.h" | ||
13 | #include "lj_tab.h" | ||
14 | #include "lj_ctype.h" | ||
15 | #include "lj_ccallback.h" | ||
16 | |||
17 | /* -- C type definitions -------------------------------------------------- */ | ||
18 | |||
19 | /* Predefined typedefs. */ | ||
20 | #define CTTDDEF(_) \ | ||
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) \ | ||
28 | _("wchar_t", WCHAR) \ | ||
29 | /* Subset of stdint.h. */ \ | ||
30 | _("int8_t", INT8) \ | ||
31 | _("int16_t", INT16) \ | ||
32 | _("int32_t", INT32) \ | ||
33 | _("int64_t", INT64) \ | ||
34 | _("uint8_t", UINT8) \ | ||
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. */ | ||
41 | |||
42 | /* Keywords (only the ones we actually care for). */ | ||
43 | #define CTKWDEF(_) \ | ||
44 | /* Type specifiers. */ \ | ||
45 | _("void", -1, CTOK_VOID) \ | ||
46 | _("_Bool", 1, 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) \ | ||
104 | /* Operators. */ \ | ||
105 | _("sizeof", 0, CTOK_SIZEOF) \ | ||
106 | _("__alignof", 0, CTOK_ALIGNOF) \ | ||
107 | _("__alignof__", 0, CTOK_ALIGNOF) \ | ||
108 | /* End of keyword list. */ | ||
109 | |||
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)), | ||
115 | CTTYDEF(CTTYINFODEF) | ||
116 | CTTDDEF(CTTDINFODEF) | ||
117 | CTKWDEF(CTKWINFODEF) | ||
118 | #undef CTTYINFODEF | ||
119 | #undef CTTDINFODEF | ||
120 | #undef CTKWINFODEF | ||
121 | 0 | ||
122 | }; | ||
123 | |||
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" | ||
128 | CTTDDEF(CTTDNAMEDEF) | ||
129 | CTKWDEF(CTKWNAMEDEF) | ||
130 | #undef CTTDNAMEDEF | ||
131 | #undef CTKWNAMEDEF | ||
132 | ; | ||
133 | |||
134 | #define CTTYPEINFO_NUM (sizeof(lj_ctype_typeinfo)/sizeof(CTInfo)-1) | ||
135 | #define CTTYPETAB_MIN 128 | ||
136 | |||
137 | /* -- C type interning ---------------------------------------------------- */ | ||
138 | |||
139 | #define ct_hashtype(info, size) (hashrot(info, size) & CTHASH_MASK) | ||
140 | #define ct_hashname(name) \ | ||
141 | (hashrot(u32ptr(name), u32ptr(name) + HASH_BIAS) & CTHASH_MASK) | ||
142 | |||
143 | /* Create new type element. */ | ||
144 | CTypeID lj_ctype_new(CTState *cts, CType **ctp) | ||
145 | { | ||
146 | CTypeID id = cts->top; | ||
147 | CType *ct; | ||
148 | lua_assert(cts->L); | ||
149 | if (LJ_UNLIKELY(id >= cts->sizetab)) { | ||
150 | if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); | ||
151 | lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); | ||
152 | } | ||
153 | cts->top = id+1; | ||
154 | *ctp = ct = &cts->tab[id]; | ||
155 | ct->info = 0; | ||
156 | ct->size = 0; | ||
157 | ct->sib = 0; | ||
158 | ct->next = 0; | ||
159 | setgcrefnull(ct->name); | ||
160 | return id; | ||
161 | } | ||
162 | |||
163 | /* Intern a type element. */ | ||
164 | CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size) | ||
165 | { | ||
166 | uint32_t h = ct_hashtype(info, size); | ||
167 | CTypeID id = cts->hash[h]; | ||
168 | lua_assert(cts->L); | ||
169 | while (id) { | ||
170 | CType *ct = ctype_get(cts, id); | ||
171 | if (ct->info == info && ct->size == size) | ||
172 | return id; | ||
173 | id = ct->next; | ||
174 | } | ||
175 | id = cts->top; | ||
176 | if (LJ_UNLIKELY(id >= cts->sizetab)) { | ||
177 | if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); | ||
178 | lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); | ||
179 | } | ||
180 | cts->top = id+1; | ||
181 | cts->tab[id].info = info; | ||
182 | cts->tab[id].size = size; | ||
183 | cts->tab[id].sib = 0; | ||
184 | cts->tab[id].next = cts->hash[h]; | ||
185 | setgcrefnull(cts->tab[id].name); | ||
186 | cts->hash[h] = (CTypeID1)id; | ||
187 | return id; | ||
188 | } | ||
189 | |||
190 | /* Add type element to hash table. */ | ||
191 | static void ctype_addtype(CTState *cts, CType *ct, CTypeID id) | ||
192 | { | ||
193 | uint32_t h = ct_hashtype(ct->info, ct->size); | ||
194 | ct->next = cts->hash[h]; | ||
195 | cts->hash[h] = (CTypeID1)id; | ||
196 | } | ||
197 | |||
198 | /* Add named element to hash table. */ | ||
199 | void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id) | ||
200 | { | ||
201 | uint32_t h = ct_hashname(gcref(ct->name)); | ||
202 | ct->next = cts->hash[h]; | ||
203 | cts->hash[h] = (CTypeID1)id; | ||
204 | } | ||
205 | |||
206 | /* Get a C type by name, matching the type mask. */ | ||
207 | CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, uint32_t tmask) | ||
208 | { | ||
209 | CTypeID id = cts->hash[ct_hashname(name)]; | ||
210 | while (id) { | ||
211 | CType *ct = ctype_get(cts, id); | ||
212 | if (gcref(ct->name) == obj2gco(name) && | ||
213 | ((tmask >> ctype_type(ct->info)) & 1)) { | ||
214 | *ctp = ct; | ||
215 | return id; | ||
216 | } | ||
217 | id = ct->next; | ||
218 | } | ||
219 | *ctp = &cts->tab[0]; /* Simplify caller logic. ctype_get() would assert. */ | ||
220 | return 0; | ||
221 | } | ||
222 | |||
223 | /* Get a struct/union/enum/function field by name. */ | ||
224 | CType *lj_ctype_getfield(CTState *cts, CType *ct, GCstr *name, CTSize *ofs) | ||
225 | { | ||
226 | while (ct->sib) { | ||
227 | ct = ctype_get(cts, ct->sib); | ||
228 | if (gcref(ct->name) == obj2gco(name)) { | ||
229 | *ofs = ct->size; | ||
230 | return ct; | ||
231 | } | ||
232 | if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { | ||
233 | CType *fct = lj_ctype_getfield(cts, ctype_child(cts, ct), name, ofs); | ||
234 | if (fct) { | ||
235 | *ofs += ct->size; | ||
236 | return fct; | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | return NULL; /* Not found. */ | ||
241 | } | ||
242 | |||
243 | /* -- C type information -------------------------------------------------- */ | ||
244 | |||
245 | /* Follow references and get raw type for a C type ID. */ | ||
246 | CType *lj_ctype_rawref(CTState *cts, CTypeID id) | ||
247 | { | ||
248 | CType *ct = ctype_get(cts, id); | ||
249 | while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) | ||
250 | ct = ctype_child(cts, ct); | ||
251 | return ct; | ||
252 | } | ||
253 | |||
254 | /* Get size for a C type ID. Does NOT support VLA/VLS. */ | ||
255 | CTSize lj_ctype_size(CTState *cts, CTypeID id) | ||
256 | { | ||
257 | CType *ct = ctype_raw(cts, id); | ||
258 | return ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID; | ||
259 | } | ||
260 | |||
261 | /* Get size for a variable-length C type. Does NOT support other C types. */ | ||
262 | CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem) | ||
263 | { | ||
264 | uint64_t xsz = 0; | ||
265 | if (ctype_isstruct(ct->info)) { | ||
266 | CTypeID arrid = 0, fid = ct->sib; | ||
267 | xsz = ct->size; /* Add the struct size. */ | ||
268 | while (fid) { | ||
269 | CType *ctf = ctype_get(cts, fid); | ||
270 | if (ctype_type(ctf->info) == CT_FIELD) | ||
271 | arrid = ctype_cid(ctf->info); /* Remember last field of VLS. */ | ||
272 | fid = ctf->sib; | ||
273 | } | ||
274 | ct = ctype_raw(cts, arrid); | ||
275 | } | ||
276 | lua_assert(ctype_isvlarray(ct->info)); /* Must be a VLA. */ | ||
277 | ct = ctype_rawchild(cts, ct); /* Get array element. */ | ||
278 | lua_assert(ctype_hassize(ct->info)); | ||
279 | /* Calculate actual size of VLA and check for overflow. */ | ||
280 | xsz += (uint64_t)ct->size * nelem; | ||
281 | return xsz < 0x80000000u ? (CTSize)xsz : CTSIZE_INVALID; | ||
282 | } | ||
283 | |||
284 | /* Get type, qualifiers, size and alignment for a C type ID. */ | ||
285 | CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp) | ||
286 | { | ||
287 | CTInfo qual = 0; | ||
288 | CType *ct = ctype_get(cts, id); | ||
289 | for (;;) { | ||
290 | CTInfo info = ct->info; | ||
291 | if (ctype_isenum(info)) { | ||
292 | /* Follow child. Need to look at its attributes, too. */ | ||
293 | } else if (ctype_isattrib(info)) { | ||
294 | if (ctype_isxattrib(info, CTA_QUAL)) | ||
295 | qual |= ct->size; | ||
296 | else if (ctype_isxattrib(info, CTA_ALIGN) && !(qual & CTFP_ALIGNED)) | ||
297 | qual |= CTFP_ALIGNED + CTALIGN(ct->size); | ||
298 | } else { | ||
299 | if (!(qual & CTFP_ALIGNED)) qual |= (info & CTF_ALIGN); | ||
300 | qual |= (info & ~(CTF_ALIGN|CTMASK_CID)); | ||
301 | lua_assert(ctype_hassize(info) || ctype_isfunc(info)); | ||
302 | *szp = ctype_isfunc(info) ? CTSIZE_INVALID : ct->size; | ||
303 | break; | ||
304 | } | ||
305 | ct = ctype_get(cts, ctype_cid(info)); | ||
306 | } | ||
307 | return qual; | ||
308 | } | ||
309 | |||
310 | /* Get ctype metamethod. */ | ||
311 | cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm) | ||
312 | { | ||
313 | CType *ct = ctype_get(cts, id); | ||
314 | cTValue *tv; | ||
315 | while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) { | ||
316 | id = ctype_cid(ct->info); | ||
317 | ct = ctype_get(cts, id); | ||
318 | } | ||
319 | if (ctype_isptr(ct->info) && | ||
320 | ctype_isfunc(ctype_get(cts, ctype_cid(ct->info))->info)) | ||
321 | tv = lj_tab_getstr(cts->miscmap, &cts->g->strempty); | ||
322 | else | ||
323 | tv = lj_tab_getinth(cts->miscmap, -(int32_t)id); | ||
324 | if (tv && tvistab(tv) && | ||
325 | (tv = lj_tab_getstr(tabV(tv), mmname_str(cts->g, mm))) && !tvisnil(tv)) | ||
326 | return tv; | ||
327 | return NULL; | ||
328 | } | ||
329 | |||
330 | /* -- C type representation ----------------------------------------------- */ | ||
331 | |||
332 | /* Fixed max. length of a C type representation. */ | ||
333 | #define CTREPR_MAX 512 | ||
334 | |||
335 | typedef struct CTRepr { | ||
336 | char *pb, *pe; | ||
337 | CTState *cts; | ||
338 | lua_State *L; | ||
339 | int needsp; | ||
340 | int ok; | ||
341 | char buf[CTREPR_MAX]; | ||
342 | } CTRepr; | ||
343 | |||
344 | /* Prepend string. */ | ||
345 | static void ctype_prepstr(CTRepr *ctr, const char *str, MSize len) | ||
346 | { | ||
347 | char *p = ctr->pb; | ||
348 | if (ctr->buf + len+1 > p) { ctr->ok = 0; return; } | ||
349 | if (ctr->needsp) *--p = ' '; | ||
350 | ctr->needsp = 1; | ||
351 | p -= len; | ||
352 | while (len-- > 0) p[len] = str[len]; | ||
353 | ctr->pb = p; | ||
354 | } | ||
355 | |||
356 | #define ctype_preplit(ctr, str) ctype_prepstr((ctr), "" str, sizeof(str)-1) | ||
357 | |||
358 | /* Prepend char. */ | ||
359 | static void ctype_prepc(CTRepr *ctr, int c) | ||
360 | { | ||
361 | if (ctr->buf >= ctr->pb) { ctr->ok = 0; return; } | ||
362 | *--ctr->pb = c; | ||
363 | } | ||
364 | |||
365 | /* Prepend number. */ | ||
366 | static void ctype_prepnum(CTRepr *ctr, uint32_t n) | ||
367 | { | ||
368 | char *p = ctr->pb; | ||
369 | if (ctr->buf + 10+1 > p) { ctr->ok = 0; return; } | ||
370 | do { *--p = (char)('0' + n % 10); } while (n /= 10); | ||
371 | ctr->pb = p; | ||
372 | ctr->needsp = 0; | ||
373 | } | ||
374 | |||
375 | /* Append char. */ | ||
376 | static void ctype_appc(CTRepr *ctr, int c) | ||
377 | { | ||
378 | if (ctr->pe >= ctr->buf + CTREPR_MAX) { ctr->ok = 0; return; } | ||
379 | *ctr->pe++ = c; | ||
380 | } | ||
381 | |||
382 | /* Append number. */ | ||
383 | static void ctype_appnum(CTRepr *ctr, uint32_t n) | ||
384 | { | ||
385 | char buf[10]; | ||
386 | char *p = buf+sizeof(buf); | ||
387 | char *q = ctr->pe; | ||
388 | if (q > ctr->buf + CTREPR_MAX - 10) { ctr->ok = 0; return; } | ||
389 | do { *--p = (char)('0' + n % 10); } while (n /= 10); | ||
390 | do { *q++ = *p++; } while (p < buf+sizeof(buf)); | ||
391 | ctr->pe = q; | ||
392 | } | ||
393 | |||
394 | /* Prepend qualifiers. */ | ||
395 | static void ctype_prepqual(CTRepr *ctr, CTInfo info) | ||
396 | { | ||
397 | if ((info & CTF_VOLATILE)) ctype_preplit(ctr, "volatile"); | ||
398 | if ((info & CTF_CONST)) ctype_preplit(ctr, "const"); | ||
399 | } | ||
400 | |||
401 | /* Prepend named type. */ | ||
402 | static void ctype_preptype(CTRepr *ctr, CType *ct, CTInfo qual, const char *t) | ||
403 | { | ||
404 | if (gcref(ct->name)) { | ||
405 | GCstr *str = gco2str(gcref(ct->name)); | ||
406 | ctype_prepstr(ctr, strdata(str), str->len); | ||
407 | } else { | ||
408 | if (ctr->needsp) ctype_prepc(ctr, ' '); | ||
409 | ctype_prepnum(ctr, ctype_typeid(ctr->cts, ct)); | ||
410 | ctr->needsp = 1; | ||
411 | } | ||
412 | ctype_prepstr(ctr, t, (MSize)strlen(t)); | ||
413 | ctype_prepqual(ctr, qual); | ||
414 | } | ||
415 | |||
416 | static void ctype_repr(CTRepr *ctr, CTypeID id) | ||
417 | { | ||
418 | CType *ct = ctype_get(ctr->cts, id); | ||
419 | CTInfo qual = 0; | ||
420 | int ptrto = 0; | ||
421 | for (;;) { | ||
422 | CTInfo info = ct->info; | ||
423 | CTSize size = ct->size; | ||
424 | switch (ctype_type(info)) { | ||
425 | case CT_NUM: | ||
426 | if ((info & CTF_BOOL)) { | ||
427 | ctype_preplit(ctr, "bool"); | ||
428 | } else if ((info & CTF_FP)) { | ||
429 | if (size == sizeof(double)) ctype_preplit(ctr, "double"); | ||
430 | else if (size == sizeof(float)) ctype_preplit(ctr, "float"); | ||
431 | else ctype_preplit(ctr, "long double"); | ||
432 | } else if (size == 1) { | ||
433 | if (!((info ^ CTF_UCHAR) & CTF_UNSIGNED)) ctype_preplit(ctr, "char"); | ||
434 | else if (CTF_UCHAR) ctype_preplit(ctr, "signed char"); | ||
435 | else ctype_preplit(ctr, "unsigned char"); | ||
436 | } else if (size < 8) { | ||
437 | if (size == 4) ctype_preplit(ctr, "int"); | ||
438 | else ctype_preplit(ctr, "short"); | ||
439 | if ((info & CTF_UNSIGNED)) ctype_preplit(ctr, "unsigned"); | ||
440 | } else { | ||
441 | ctype_preplit(ctr, "_t"); | ||
442 | ctype_prepnum(ctr, size*8); | ||
443 | ctype_preplit(ctr, "int"); | ||
444 | if ((info & CTF_UNSIGNED)) ctype_prepc(ctr, 'u'); | ||
445 | } | ||
446 | ctype_prepqual(ctr, (qual|info)); | ||
447 | return; | ||
448 | case CT_VOID: | ||
449 | ctype_preplit(ctr, "void"); | ||
450 | ctype_prepqual(ctr, (qual|info)); | ||
451 | return; | ||
452 | case CT_STRUCT: | ||
453 | ctype_preptype(ctr, ct, qual, (info & CTF_UNION) ? "union" : "struct"); | ||
454 | return; | ||
455 | case CT_ENUM: | ||
456 | ctype_preptype(ctr, ct, qual, "enum"); | ||
457 | return; | ||
458 | case CT_ATTRIB: | ||
459 | if (ctype_attrib(info) == CTA_QUAL) qual |= size; | ||
460 | break; | ||
461 | case CT_PTR: | ||
462 | if ((info & CTF_REF)) { | ||
463 | ctype_prepc(ctr, '&'); | ||
464 | } else { | ||
465 | ctype_prepqual(ctr, (qual|info)); | ||
466 | if (LJ_64 && size == 4) ctype_preplit(ctr, "__ptr32"); | ||
467 | ctype_prepc(ctr, '*'); | ||
468 | } | ||
469 | qual = 0; | ||
470 | ptrto = 1; | ||
471 | ctr->needsp = 1; | ||
472 | break; | ||
473 | case CT_ARRAY: | ||
474 | if (ctype_isrefarray(info)) { | ||
475 | ctr->needsp = 1; | ||
476 | if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } | ||
477 | ctype_appc(ctr, '['); | ||
478 | if (size != CTSIZE_INVALID) { | ||
479 | CTSize csize = ctype_child(ctr->cts, ct)->size; | ||
480 | ctype_appnum(ctr, csize ? size/csize : 0); | ||
481 | } else if ((info & CTF_VLA)) { | ||
482 | ctype_appc(ctr, '?'); | ||
483 | } | ||
484 | ctype_appc(ctr, ']'); | ||
485 | } else if ((info & CTF_COMPLEX)) { | ||
486 | if (size == 2*sizeof(float)) ctype_preplit(ctr, "float"); | ||
487 | ctype_preplit(ctr, "complex"); | ||
488 | return; | ||
489 | } else { | ||
490 | ctype_preplit(ctr, ")))"); | ||
491 | ctype_prepnum(ctr, size); | ||
492 | ctype_preplit(ctr, "__attribute__((vector_size("); | ||
493 | } | ||
494 | break; | ||
495 | case CT_FUNC: | ||
496 | ctr->needsp = 1; | ||
497 | if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } | ||
498 | ctype_appc(ctr, '('); | ||
499 | ctype_appc(ctr, ')'); | ||
500 | break; | ||
501 | default: | ||
502 | lua_assert(0); | ||
503 | break; | ||
504 | } | ||
505 | ct = ctype_get(ctr->cts, ctype_cid(info)); | ||
506 | } | ||
507 | } | ||
508 | |||
509 | /* Return a printable representation of a C type. */ | ||
510 | GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name) | ||
511 | { | ||
512 | global_State *g = G(L); | ||
513 | CTRepr ctr; | ||
514 | ctr.pb = ctr.pe = &ctr.buf[CTREPR_MAX/2]; | ||
515 | ctr.cts = ctype_ctsG(g); | ||
516 | ctr.L = L; | ||
517 | ctr.ok = 1; | ||
518 | ctr.needsp = 0; | ||
519 | if (name) ctype_prepstr(&ctr, strdata(name), name->len); | ||
520 | ctype_repr(&ctr, id); | ||
521 | if (LJ_UNLIKELY(!ctr.ok)) return lj_str_newlit(L, "?"); | ||
522 | return lj_str_new(L, ctr.pb, ctr.pe - ctr.pb); | ||
523 | } | ||
524 | |||
525 | /* Convert int64_t/uint64_t to string with 'LL' or 'ULL' suffix. */ | ||
526 | GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned) | ||
527 | { | ||
528 | char buf[1+20+3]; | ||
529 | char *p = buf+sizeof(buf); | ||
530 | int sign = 0; | ||
531 | *--p = 'L'; *--p = 'L'; | ||
532 | if (isunsigned) { | ||
533 | *--p = 'U'; | ||
534 | } else if ((int64_t)n < 0) { | ||
535 | n = (uint64_t)-(int64_t)n; | ||
536 | sign = 1; | ||
537 | } | ||
538 | do { *--p = (char)('0' + n % 10); } while (n /= 10); | ||
539 | if (sign) *--p = '-'; | ||
540 | return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p)); | ||
541 | } | ||
542 | |||
543 | /* Convert complex to string with 'i' or 'I' suffix. */ | ||
544 | GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size) | ||
545 | { | ||
546 | char buf[2*LJ_STR_NUMBUF+2+1]; | ||
547 | TValue re, im; | ||
548 | size_t len; | ||
549 | if (size == 2*sizeof(double)) { | ||
550 | re.n = *(double *)sp; im.n = ((double *)sp)[1]; | ||
551 | } else { | ||
552 | re.n = (double)*(float *)sp; im.n = (double)((float *)sp)[1]; | ||
553 | } | ||
554 | len = lj_str_bufnum(buf, &re); | ||
555 | if (!(im.u32.hi & 0x80000000u) || im.n != im.n) buf[len++] = '+'; | ||
556 | len += lj_str_bufnum(buf+len, &im); | ||
557 | buf[len] = buf[len-1] >= 'a' ? 'I' : 'i'; | ||
558 | return lj_str_new(L, buf, len+1); | ||
559 | } | ||
560 | |||
561 | /* -- C type state -------------------------------------------------------- */ | ||
562 | |||
563 | /* Initialize C type table and state. */ | ||
564 | CTState *lj_ctype_init(lua_State *L) | ||
565 | { | ||
566 | CTState *cts = lj_mem_newt(L, sizeof(CTState), CTState); | ||
567 | CType *ct = lj_mem_newvec(L, CTTYPETAB_MIN, CType); | ||
568 | const char *name = lj_ctype_typenames; | ||
569 | CTypeID id; | ||
570 | memset(cts, 0, sizeof(CTState)); | ||
571 | cts->tab = ct; | ||
572 | cts->sizetab = CTTYPETAB_MIN; | ||
573 | cts->top = CTTYPEINFO_NUM; | ||
574 | cts->L = NULL; | ||
575 | cts->g = G(L); | ||
576 | for (id = 0; id < CTTYPEINFO_NUM; id++, ct++) { | ||
577 | CTInfo info = lj_ctype_typeinfo[id]; | ||
578 | ct->size = (CTSize)((int32_t)(info << 16) >> 26); | ||
579 | ct->info = info & 0xffff03ffu; | ||
580 | if (ctype_type(info) == CT_KW || ctype_istypedef(info)) { | ||
581 | size_t len = strlen(name); | ||
582 | GCstr *str = lj_str_new(L, name, len); | ||
583 | ctype_setname(ct, str); | ||
584 | name += len+1; | ||
585 | lj_ctype_addname(cts, ct, id); | ||
586 | } else { | ||
587 | setgcrefnull(ct->name); | ||
588 | if (!ctype_isenum(info)) ctype_addtype(cts, ct, id); | ||
589 | } | ||
590 | } | ||
591 | setmref(G(L)->ctype_state, cts); | ||
592 | return cts; | ||
593 | } | ||
594 | |||
595 | /* Free C type table and state. */ | ||
596 | void lj_ctype_freestate(global_State *g) | ||
597 | { | ||
598 | CTState *cts = ctype_ctsG(g); | ||
599 | if (cts) { | ||
600 | lj_ccallback_mcode_free(cts); | ||
601 | lj_mem_freevec(g, cts->tab, cts->sizetab, CType); | ||
602 | lj_mem_freevec(g, cts->cb.cbid, cts->cb.sizeid, CTypeID1); | ||
603 | lj_mem_freet(g, cts); | ||
604 | } | ||
605 | } | ||
606 | |||
607 | #endif | ||