diff options
author | Mike Pall <mike> | 2010-12-05 00:11:35 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2010-12-05 00:11:35 +0100 |
commit | 05973ee44075698e6c578cfb0fa72cfccdf7b742 (patch) | |
tree | 81e337225e44c799b58e1e76aeea91ff684c2158 | |
parent | 8679ca5792e9d69b35246196af93cadaad016c6f (diff) | |
download | luajit-05973ee44075698e6c578cfb0fa72cfccdf7b742.tar.gz luajit-05973ee44075698e6c578cfb0fa72cfccdf7b742.tar.bz2 luajit-05973ee44075698e6c578cfb0fa72cfccdf7b742.zip |
FFI: Add C type management.
-rw-r--r-- | src/Makefile | 1 | ||||
-rw-r--r-- | src/Makefile.dep | 23 | ||||
-rw-r--r-- | src/lj_ctype.c | 583 | ||||
-rw-r--r-- | src/lj_ctype.h | 438 | ||||
-rw-r--r-- | src/lj_state.c | 7 | ||||
-rw-r--r-- | src/ljamalg.c | 1 |
6 files changed, 1041 insertions, 12 deletions
diff --git a/src/Makefile b/src/Makefile index 3b9d202a..563c6c08 100644 --- a/src/Makefile +++ b/src/Makefile | |||
@@ -327,6 +327,7 @@ LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o \ | |||
327 | lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \ | 327 | lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \ |
328 | lj_opt_dce.o lj_opt_loop.o \ | 328 | lj_opt_dce.o lj_opt_loop.o \ |
329 | lj_mcode.o lj_snap.o lj_record.o lj_asm.o lj_trace.o lj_gdbjit.o \ | 329 | lj_mcode.o lj_snap.o lj_record.o lj_asm.o lj_trace.o lj_gdbjit.o \ |
330 | lj_ctype.o \ | ||
330 | lj_lib.o lj_alloc.o lib_aux.o \ | 331 | lj_lib.o lj_alloc.o lib_aux.o \ |
331 | $(LJLIB_O) lib_init.o | 332 | $(LJLIB_O) lib_init.o |
332 | 333 | ||
diff --git a/src/Makefile.dep b/src/Makefile.dep index b30086d9..761fd9fa 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep | |||
@@ -53,6 +53,8 @@ lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ | |||
53 | lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \ | 53 | lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \ |
54 | lj_bcdef.h | 54 | lj_bcdef.h |
55 | lj_char.o: lj_char.c lj_char.h lj_def.h lua.h luaconf.h | 55 | lj_char.o: lj_char.c lj_char.h lj_def.h lua.h luaconf.h |
56 | lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ | ||
57 | lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h | ||
56 | lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ | 58 | lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ |
57 | lj_err.h lj_errmsg.h lj_state.h lj_frame.h lj_bc.h lj_ff.h lj_ffdef.h \ | 59 | lj_err.h lj_errmsg.h lj_state.h lj_frame.h lj_bc.h lj_ff.h lj_ffdef.h \ |
58 | lj_jit.h lj_ir.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_vm.h luajit.h | 60 | lj_jit.h lj_ir.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_vm.h luajit.h |
@@ -109,7 +111,7 @@ lj_snap.o: lj_snap.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ | |||
109 | lj_dispatch.h lj_traceerr.h lj_snap.h lj_target.h lj_target_*.h | 111 | lj_dispatch.h lj_traceerr.h lj_snap.h lj_target.h lj_target_*.h |
110 | lj_state.o: lj_state.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ | 112 | lj_state.o: lj_state.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ |
111 | lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_meta.h \ | 113 | lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_meta.h \ |
112 | lj_state.h lj_frame.h lj_bc.h lj_mcode.h lj_jit.h lj_ir.h lj_trace.h \ | 114 | lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_trace.h lj_jit.h lj_ir.h \ |
113 | lj_dispatch.h lj_traceerr.h lj_vm.h lj_lex.h lj_alloc.h | 115 | lj_dispatch.h lj_traceerr.h lj_vm.h lj_lex.h lj_alloc.h |
114 | lj_str.o: lj_str.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ | 116 | lj_str.o: lj_str.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ |
115 | lj_err.h lj_errmsg.h lj_str.h lj_state.h lj_char.h | 117 | lj_err.h lj_errmsg.h lj_str.h lj_state.h lj_char.h |
@@ -130,13 +132,14 @@ ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \ | |||
130 | lj_udata.h lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_trace.h lj_jit.h \ | 132 | lj_udata.h lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_trace.h lj_jit.h \ |
131 | lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h lj_err.c lj_char.c lj_char.h \ | 133 | lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h lj_err.c lj_char.c lj_char.h \ |
132 | lj_bc.c lj_bcdef.h lj_obj.c lj_str.c lj_tab.c lj_func.c lj_udata.c \ | 134 | lj_bc.c lj_bcdef.h lj_obj.c lj_str.c lj_tab.c lj_func.c lj_udata.c \ |
133 | lj_meta.c lj_state.c lj_mcode.h lj_lex.h lj_alloc.h lj_dispatch.c \ | 135 | lj_meta.c lj_state.c lj_ctype.h lj_lex.h lj_alloc.h lj_dispatch.c \ |
134 | lj_ff.h lj_ffdef.h luajit.h lj_vmevent.c lj_vmevent.h lj_api.c \ | 136 | lj_ff.h lj_ffdef.h luajit.h lj_vmevent.c lj_vmevent.h lj_api.c \ |
135 | lj_parse.h lj_lex.c lj_parse.c lj_lib.c lj_lib.h lj_ir.c lj_iropt.h \ | 137 | lj_parse.h lj_lex.c lj_parse.c lj_ctype.c lj_lib.c lj_lib.h lj_ir.c \ |
136 | lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c \ | 138 | lj_iropt.h lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c \ |
137 | lj_opt_loop.c lj_snap.h lj_mcode.c lj_snap.c lj_target.h lj_target_*.h \ | 139 | lj_opt_dce.c lj_opt_loop.c lj_snap.h lj_mcode.c lj_mcode.h lj_snap.c \ |
138 | lj_record.c lj_record.h lj_asm.h lj_recdef.h lj_asm.c lj_trace.c \ | 140 | lj_target.h lj_target_*.h lj_record.c lj_record.h lj_asm.h lj_recdef.h \ |
139 | lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c lib_base.c lualib.h \ | 141 | lj_asm.c lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c \ |
140 | lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c lib_os.c \ | 142 | lib_base.c lualib.h lj_libdef.h lib_math.c lib_string.c lib_table.c \ |
141 | lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_init.c | 143 | lib_io.c lib_os.c lib_package.c lib_debug.c lib_bit.c lib_jit.c \ |
142 | luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h | 144 | lib_init.c |
145 | luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h | ||
diff --git a/src/lj_ctype.c b/src/lj_ctype.c new file mode 100644 index 00000000..001d6f5a --- /dev/null +++ b/src/lj_ctype.c | |||
@@ -0,0 +1,583 @@ | |||
1 | /* | ||
2 | ** C type management. | ||
3 | ** Copyright (C) 2005-2010 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 | |||
16 | /* -- C type definitions -------------------------------------------------- */ | ||
17 | |||
18 | /* Predefined typedefs. */ | ||
19 | #define CTTDDEF(_) \ | ||
20 | /* Vararg handling. */ \ | ||
21 | _("va_list", P_VOID) \ | ||
22 | _("__builtin_va_list", P_VOID) \ | ||
23 | _("__gnuc_va_list", P_VOID) \ | ||
24 | /* From stddef.h. */ \ | ||
25 | _("ptrdiff_t", INT_PSZ) \ | ||
26 | _("size_t", UINT_PSZ) \ | ||
27 | _("wchar_t", WCHAR) \ | ||
28 | /* Subset of stdint.h. */ \ | ||
29 | _("int8_t", INT8) \ | ||
30 | _("int16_t", INT16) \ | ||
31 | _("int32_t", INT32) \ | ||
32 | _("int64_t", INT64) \ | ||
33 | _("uint8_t", UINT8) \ | ||
34 | _("uint16_t", UINT16) \ | ||
35 | _("uint32_t", UINT32) \ | ||
36 | _("uint64_t", UINT64) \ | ||
37 | _("intptr_t", INT_PSZ) \ | ||
38 | _("uintptr_t", UINT_PSZ) \ | ||
39 | /* End of typedef list. */ | ||
40 | |||
41 | /* Keywords (only the ones we actually care for). */ | ||
42 | #define CTKWDEF(_) \ | ||
43 | /* Type specifiers. */ \ | ||
44 | _("void", -1, CTOK_VOID) \ | ||
45 | _("_Bool", 1, CTOK_BOOL) \ | ||
46 | _("bool", 1, CTOK_BOOL) \ | ||
47 | _("char", 1, CTOK_CHAR) \ | ||
48 | _("int", 4, CTOK_INT) \ | ||
49 | _("__int8", 1, CTOK_INT) \ | ||
50 | _("__int16", 2, CTOK_INT) \ | ||
51 | _("__int32", 4, CTOK_INT) \ | ||
52 | _("__int64", 8, CTOK_INT) \ | ||
53 | _("float", 4, CTOK_FP) \ | ||
54 | _("double", 8, CTOK_FP) \ | ||
55 | _("long", 0, CTOK_LONG) \ | ||
56 | _("short", 0, CTOK_SHORT) \ | ||
57 | _("_Complex", 0, CTOK_COMPLEX) \ | ||
58 | _("complex", 0, CTOK_COMPLEX) \ | ||
59 | _("__complex", 0, CTOK_COMPLEX) \ | ||
60 | _("__complex__", 0, CTOK_COMPLEX) \ | ||
61 | _("signed", 0, CTOK_SIGNED) \ | ||
62 | _("__signed", 0, CTOK_SIGNED) \ | ||
63 | _("__signed__", 0, CTOK_SIGNED) \ | ||
64 | _("unsigned", 0, CTOK_UNSIGNED) \ | ||
65 | /* Type qualifiers. */ \ | ||
66 | _("const", 0, CTOK_CONST) \ | ||
67 | _("__const", 0, CTOK_CONST) \ | ||
68 | _("__const__", 0, CTOK_CONST) \ | ||
69 | _("volatile", 0, CTOK_VOLATILE) \ | ||
70 | _("__volatile", 0, CTOK_VOLATILE) \ | ||
71 | _("__volatile__", 0, CTOK_VOLATILE) \ | ||
72 | _("restrict", 0, CTOK_RESTRICT) \ | ||
73 | _("__restrict", 0, CTOK_RESTRICT) \ | ||
74 | _("__restrict__", 0, CTOK_RESTRICT) \ | ||
75 | _("inline", 0, CTOK_INLINE) \ | ||
76 | _("__inline", 0, CTOK_INLINE) \ | ||
77 | _("__inline__", 0, CTOK_INLINE) \ | ||
78 | /* Storage class specifiers. */ \ | ||
79 | _("typedef", 0, CTOK_TYPEDEF) \ | ||
80 | _("extern", 0, CTOK_EXTERN) \ | ||
81 | _("static", 0, CTOK_STATIC) \ | ||
82 | _("auto", 0, CTOK_AUTO) \ | ||
83 | _("register", 0, CTOK_REGISTER) \ | ||
84 | /* GCC Attributes. */ \ | ||
85 | _("__extension__", 0, CTOK_EXTENSION) \ | ||
86 | _("__attribute", 0, CTOK_ATTRIBUTE) \ | ||
87 | _("__attribute__", 0, CTOK_ATTRIBUTE) \ | ||
88 | _("asm", 0, CTOK_ASM) \ | ||
89 | _("__asm", 0, CTOK_ASM) \ | ||
90 | _("__asm__", 0, CTOK_ASM) \ | ||
91 | /* MSVC Attributes. */ \ | ||
92 | _("__declspec", 0, CTOK_DECLSPEC) \ | ||
93 | _("__cdecl", CTCC_CDECL, CTOK_CCDECL) \ | ||
94 | _("__thiscall", CTCC_THISCALL, CTOK_CCDECL) \ | ||
95 | _("__fastcall", CTCC_FASTCALL, CTOK_CCDECL) \ | ||
96 | _("__stdcall", CTCC_STDCALL, CTOK_CCDECL) \ | ||
97 | _("__ptr32", 4, CTOK_PTRSZ) \ | ||
98 | _("__ptr64", 8, CTOK_PTRSZ) \ | ||
99 | /* Other type specifiers. */ \ | ||
100 | _("struct", 0, CTOK_STRUCT) \ | ||
101 | _("union", 0, CTOK_UNION) \ | ||
102 | _("enum", 0, CTOK_ENUM) \ | ||
103 | /* Operators. */ \ | ||
104 | _("sizeof", 0, CTOK_SIZEOF) \ | ||
105 | _("__alignof", 0, CTOK_ALIGNOF) \ | ||
106 | _("__alignof__", 0, CTOK_ALIGNOF) \ | ||
107 | /* End of keyword list. */ | ||
108 | |||
109 | /* Type info for predefined types. Size merged in. */ | ||
110 | static CTInfo lj_ctype_typeinfo[] = { | ||
111 | #define CTTYINFODEF(id, sz, ct, info) CTINFO((ct),(((sz)&0x3fu)<<10)+(info)), | ||
112 | #define CTTDINFODEF(name, id) CTINFO(CT_TYPEDEF, CTID_##id), | ||
113 | #define CTKWINFODEF(name, sz, kw) CTINFO(CT_KW,(((sz)&0x3fu)<<10)+(kw)), | ||
114 | CTTYDEF(CTTYINFODEF) | ||
115 | CTTDDEF(CTTDINFODEF) | ||
116 | CTKWDEF(CTKWINFODEF) | ||
117 | #undef CTTYINFODEF | ||
118 | #undef CTTDINFODEF | ||
119 | #undef CTKWINFODEF | ||
120 | 0 | ||
121 | }; | ||
122 | |||
123 | /* Predefined type names collected in a single string. */ | ||
124 | static const char * const lj_ctype_typenames = | ||
125 | #define CTTDNAMEDEF(name, id) name "\0" | ||
126 | #define CTKWNAMEDEF(name, sz, cds) name "\0" | ||
127 | CTTDDEF(CTTDNAMEDEF) | ||
128 | CTKWDEF(CTKWNAMEDEF) | ||
129 | #undef CTTDNAMEDEF | ||
130 | #undef CTKWNAMEDEF | ||
131 | ; | ||
132 | |||
133 | #define CTTYPEINFO_NUM (sizeof(lj_ctype_typeinfo)/sizeof(CTInfo)-1) | ||
134 | #define CTTYPETAB_MIN 128 | ||
135 | |||
136 | /* -- C type interning ---------------------------------------------------- */ | ||
137 | |||
138 | #define ct_hashtype(info, size) (hashrot(info, size) & CTHASH_MASK) | ||
139 | #define ct_hashname(name) \ | ||
140 | (hashrot(u32ptr(name), u32ptr(name) + HASH_BIAS) & CTHASH_MASK) | ||
141 | |||
142 | /* Create new type element. */ | ||
143 | CTypeID lj_ctype_new(CTState *cts, CType **ctp) | ||
144 | { | ||
145 | CTypeID id = cts->top; | ||
146 | CType *ct; | ||
147 | lua_assert(cts->L); | ||
148 | if (LJ_UNLIKELY(id >= cts->sizetab)) { | ||
149 | if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); | ||
150 | lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); | ||
151 | } | ||
152 | cts->top = id+1; | ||
153 | *ctp = ct = &cts->tab[id]; | ||
154 | ct->info = 0; | ||
155 | ct->size = 0; | ||
156 | ct->sib = 0; | ||
157 | ct->next = 0; | ||
158 | setgcrefnull(ct->name); | ||
159 | return id; | ||
160 | } | ||
161 | |||
162 | /* Intern a type element. */ | ||
163 | CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size) | ||
164 | { | ||
165 | uint32_t h = ct_hashtype(info, size); | ||
166 | CTypeID id = cts->hash[h]; | ||
167 | lua_assert(cts->L); | ||
168 | while (id) { | ||
169 | CType *ct = ctype_get(cts, id); | ||
170 | if (ct->info == info && ct->size == size) | ||
171 | return id; | ||
172 | id = ct->next; | ||
173 | } | ||
174 | id = cts->top; | ||
175 | if (LJ_UNLIKELY(id >= cts->sizetab)) { | ||
176 | if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); | ||
177 | lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); | ||
178 | } | ||
179 | cts->top = id+1; | ||
180 | cts->tab[id].info = info; | ||
181 | cts->tab[id].size = size; | ||
182 | cts->tab[id].sib = 0; | ||
183 | cts->tab[id].next = cts->hash[h]; | ||
184 | setgcrefnull(cts->tab[id].name); | ||
185 | cts->hash[h] = (CTypeID1)id; | ||
186 | return id; | ||
187 | } | ||
188 | |||
189 | /* Add type element to hash table. */ | ||
190 | static void ctype_addtype(CTState *cts, CType *ct, CTypeID id) | ||
191 | { | ||
192 | uint32_t h = ct_hashtype(ct->info, ct->size); | ||
193 | ct->next = cts->hash[h]; | ||
194 | cts->hash[h] = (CTypeID1)id; | ||
195 | } | ||
196 | |||
197 | /* Add named element to hash table. */ | ||
198 | void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id) | ||
199 | { | ||
200 | uint32_t h = ct_hashname(gcref(ct->name)); | ||
201 | ct->next = cts->hash[h]; | ||
202 | cts->hash[h] = (CTypeID1)id; | ||
203 | } | ||
204 | |||
205 | /* Get a C type by name, matching the type mask. */ | ||
206 | CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, uint32_t tmask) | ||
207 | { | ||
208 | CTypeID id = cts->hash[ct_hashname(name)]; | ||
209 | while (id) { | ||
210 | CType *ct = ctype_get(cts, id); | ||
211 | if (gcref(ct->name) == obj2gco(name) && | ||
212 | ((tmask >> ctype_type(ct->info)) & 1)) { | ||
213 | *ctp = ct; | ||
214 | return id; | ||
215 | } | ||
216 | id = ct->next; | ||
217 | } | ||
218 | *ctp = &cts->tab[0]; /* Simplify caller logic. ctype_get() would assert. */ | ||
219 | return 0; | ||
220 | } | ||
221 | |||
222 | /* Get a struct/union/enum/function field by name. */ | ||
223 | CType *lj_ctype_getfield(CTState *cts, CType *ct, GCstr *name, CTSize *ofs) | ||
224 | { | ||
225 | while (ct->sib) { | ||
226 | ct = ctype_get(cts, ct->sib); | ||
227 | if (gcref(ct->name) == obj2gco(name)) { | ||
228 | *ofs = ct->size; | ||
229 | return ct; | ||
230 | } | ||
231 | if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { | ||
232 | CType *fct = lj_ctype_getfield(cts, ctype_child(cts, ct), name, ofs); | ||
233 | if (fct) { | ||
234 | *ofs += ct->size; | ||
235 | return fct; | ||
236 | } | ||
237 | } | ||
238 | } | ||
239 | return NULL; /* Not found. */ | ||
240 | } | ||
241 | |||
242 | /* -- C type information -------------------------------------------------- */ | ||
243 | |||
244 | /* Follow references and get raw type for a C type ID. */ | ||
245 | CType *lj_ctype_rawref(CTState *cts, CTypeID id) | ||
246 | { | ||
247 | CType *ct = ctype_get(cts, id); | ||
248 | while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) | ||
249 | ct = ctype_child(cts, ct); | ||
250 | return ct; | ||
251 | } | ||
252 | |||
253 | /* Get size for a C type ID. Does NOT support VLA/VLS. */ | ||
254 | CTSize lj_ctype_size(CTState *cts, CTypeID id) | ||
255 | { | ||
256 | CType *ct = ctype_raw(cts, id); | ||
257 | return ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID; | ||
258 | } | ||
259 | |||
260 | /* Get size for a variable-length C type. Does NOT support other C types. */ | ||
261 | CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem) | ||
262 | { | ||
263 | uint64_t xsz = 0; | ||
264 | if (ctype_isstruct(ct->info)) { | ||
265 | CTypeID arrid = 0, fid = ct->sib; | ||
266 | xsz = ct->size; /* Add the struct size. */ | ||
267 | while (fid) { | ||
268 | CType *ctf = ctype_get(cts, fid); | ||
269 | if (ctype_type(ctf->info) == CT_FIELD) | ||
270 | arrid = ctype_cid(ctf->info); /* Remember last field of VLS. */ | ||
271 | fid = ctf->sib; | ||
272 | } | ||
273 | ct = ctype_raw(cts, arrid); | ||
274 | } | ||
275 | lua_assert(ctype_isvlarray(ct->info)); /* Must be a VLA. */ | ||
276 | ct = ctype_rawchild(cts, ct); /* Get array element. */ | ||
277 | lua_assert(ctype_hassize(ct->info)); | ||
278 | /* Calculate actual size of VLA and check for overflow. */ | ||
279 | xsz += (uint64_t)ct->size * nelem; | ||
280 | return xsz < 0x80000000u ? (CTSize)xsz : CTSIZE_INVALID; | ||
281 | } | ||
282 | |||
283 | /* Get type, qualifiers, size and alignment for a C type ID. */ | ||
284 | CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp) | ||
285 | { | ||
286 | CTInfo qual = 0; | ||
287 | CType *ct = ctype_get(cts, id); | ||
288 | for (;;) { | ||
289 | CTInfo info = ct->info; | ||
290 | if (ctype_isenum(info)) { | ||
291 | /* Follow child. Need to look at its attributes, too. */ | ||
292 | } else if (ctype_isattrib(info)) { | ||
293 | if (ctype_isxattrib(info, CTA_QUAL)) | ||
294 | qual |= ct->size; | ||
295 | else if (ctype_isxattrib(info, CTA_ALIGN) && !(qual & CTFP_ALIGNED)) | ||
296 | qual |= CTFP_ALIGNED + CTALIGN(ct->size); | ||
297 | } else { | ||
298 | if (!(qual & CTFP_ALIGNED)) qual |= (info & CTF_ALIGN); | ||
299 | qual |= (info & ~(CTF_ALIGN|CTMASK_CID)); | ||
300 | lua_assert(ctype_hassize(info) || ctype_isfunc(info)); | ||
301 | *szp = ctype_isfunc(info) ? CTSIZE_INVALID : ct->size; | ||
302 | break; | ||
303 | } | ||
304 | ct = ctype_get(cts, ctype_cid(info)); | ||
305 | } | ||
306 | return qual; | ||
307 | } | ||
308 | |||
309 | /* -- C type representation ----------------------------------------------- */ | ||
310 | |||
311 | /* Fixed max. length of a C type representation. */ | ||
312 | #define CTREPR_MAX 512 | ||
313 | |||
314 | typedef struct CTRepr { | ||
315 | char *pb, *pe; | ||
316 | CTState *cts; | ||
317 | lua_State *L; | ||
318 | int needsp; | ||
319 | int ok; | ||
320 | char buf[CTREPR_MAX]; | ||
321 | } CTRepr; | ||
322 | |||
323 | /* Prepend string. */ | ||
324 | static void ctype_prepstr(CTRepr *ctr, const char *str, MSize len) | ||
325 | { | ||
326 | char *p = ctr->pb; | ||
327 | if (ctr->buf + len+1 > p) { ctr->ok = 0; return; } | ||
328 | if (ctr->needsp) *--p = ' '; | ||
329 | ctr->needsp = 1; | ||
330 | p -= len; | ||
331 | while (len-- > 0) p[len] = str[len]; | ||
332 | ctr->pb = p; | ||
333 | } | ||
334 | |||
335 | #define ctype_preplit(ctr, str) ctype_prepstr((ctr), "" str, sizeof(str)-1) | ||
336 | |||
337 | /* Prepend char. */ | ||
338 | static void ctype_prepc(CTRepr *ctr, int c) | ||
339 | { | ||
340 | if (ctr->buf >= ctr->pb) { ctr->ok = 0; return; } | ||
341 | *--ctr->pb = c; | ||
342 | } | ||
343 | |||
344 | /* Prepend number. */ | ||
345 | static void ctype_prepnum(CTRepr *ctr, uint32_t n) | ||
346 | { | ||
347 | char *p = ctr->pb; | ||
348 | if (ctr->buf + 10+1 > p) { ctr->ok = 0; return; } | ||
349 | do { *--p = (char)('0' + n % 10); } while (n /= 10); | ||
350 | ctr->pb = p; | ||
351 | ctr->needsp = 0; | ||
352 | } | ||
353 | |||
354 | /* Append char. */ | ||
355 | static void ctype_appc(CTRepr *ctr, int c) | ||
356 | { | ||
357 | if (ctr->pe >= ctr->buf + CTREPR_MAX) { ctr->ok = 0; return; } | ||
358 | *ctr->pe++ = c; | ||
359 | } | ||
360 | |||
361 | /* Append number. */ | ||
362 | static void ctype_appnum(CTRepr *ctr, uint32_t n) | ||
363 | { | ||
364 | char buf[10]; | ||
365 | char *p = buf+sizeof(buf); | ||
366 | char *q = ctr->pe; | ||
367 | if (q > ctr->buf + CTREPR_MAX - 10) { ctr->ok = 0; return; } | ||
368 | do { *--p = (char)('0' + n % 10); } while (n /= 10); | ||
369 | do { *q++ = *p++; } while (p < buf+sizeof(buf)); | ||
370 | ctr->pe = q; | ||
371 | } | ||
372 | |||
373 | /* Prepend qualifiers. */ | ||
374 | static void ctype_prepqual(CTRepr *ctr, CTInfo info) | ||
375 | { | ||
376 | if ((info & CTF_VOLATILE)) ctype_preplit(ctr, "volatile"); | ||
377 | if ((info & CTF_CONST)) ctype_preplit(ctr, "const"); | ||
378 | } | ||
379 | |||
380 | /* Prepend named type. */ | ||
381 | static void ctype_preptype(CTRepr *ctr, CType *ct, CTInfo qual, const char *t) | ||
382 | { | ||
383 | if (gcref(ct->name)) { | ||
384 | GCstr *str = gco2str(gcref(ct->name)); | ||
385 | ctype_prepstr(ctr, strdata(str), str->len); | ||
386 | } else { | ||
387 | if (ctr->needsp) ctype_prepc(ctr, ' '); | ||
388 | ctype_prepnum(ctr, ctype_typeid(ctr->cts, ct)); | ||
389 | ctr->needsp = 1; | ||
390 | } | ||
391 | ctype_prepstr(ctr, t, (MSize)strlen(t)); | ||
392 | ctype_prepqual(ctr, qual); | ||
393 | } | ||
394 | |||
395 | void ctype_repr(CTRepr *ctr, CTypeID id) | ||
396 | { | ||
397 | CType *ct = ctype_get(ctr->cts, id); | ||
398 | CTInfo qual = 0; | ||
399 | int ptrto = 0; | ||
400 | for (;;) { | ||
401 | CTInfo info = ct->info; | ||
402 | CTSize size = ct->size; | ||
403 | switch (ctype_type(info)) { | ||
404 | case CT_NUM: | ||
405 | if ((info & CTF_BOOL)) { | ||
406 | ctype_preplit(ctr, "bool"); | ||
407 | } else if ((info & CTF_FP)) { | ||
408 | if (size == sizeof(double)) ctype_preplit(ctr, "double"); | ||
409 | else if (size == sizeof(float)) ctype_preplit(ctr, "float"); | ||
410 | else ctype_preplit(ctr, "long double"); | ||
411 | } else if (size == 1) { | ||
412 | if (!((info ^ CTF_UCHAR) & CTF_UNSIGNED)) ctype_preplit(ctr, "char"); | ||
413 | else if (CTF_UCHAR) ctype_preplit(ctr, "signed char"); | ||
414 | else ctype_preplit(ctr, "unsigned char"); | ||
415 | } else if (size < 8) { | ||
416 | if (size == 4) ctype_preplit(ctr, "int"); | ||
417 | else ctype_preplit(ctr, "short"); | ||
418 | if ((info & CTF_UNSIGNED)) ctype_preplit(ctr, "unsigned"); | ||
419 | } else { | ||
420 | ctype_preplit(ctr, "_t"); | ||
421 | ctype_prepnum(ctr, size*8); | ||
422 | ctype_preplit(ctr, "int"); | ||
423 | if ((info & CTF_UNSIGNED)) ctype_prepc(ctr, 'u'); | ||
424 | } | ||
425 | ctype_prepqual(ctr, (qual|info)); | ||
426 | return; | ||
427 | case CT_VOID: | ||
428 | ctype_preplit(ctr, "void"); | ||
429 | ctype_prepqual(ctr, (qual|info)); | ||
430 | return; | ||
431 | case CT_STRUCT: | ||
432 | ctype_preptype(ctr, ct, qual, (info & CTF_UNION) ? "union" : "struct"); | ||
433 | return; | ||
434 | case CT_ENUM: | ||
435 | ctype_preptype(ctr, ct, qual, "enum"); | ||
436 | return; | ||
437 | case CT_ATTRIB: | ||
438 | if (ctype_attrib(info) == CTA_QUAL) qual |= size; | ||
439 | break; | ||
440 | case CT_PTR: | ||
441 | if ((info & CTF_REF)) { | ||
442 | ctype_prepc(ctr, '&'); | ||
443 | } else { | ||
444 | ctype_prepqual(ctr, (qual|info)); | ||
445 | if (LJ_64 && size == 4) ctype_preplit(ctr, "__ptr32"); | ||
446 | ctype_prepc(ctr, '*'); | ||
447 | } | ||
448 | qual = 0; | ||
449 | ptrto = 1; | ||
450 | ctr->needsp = 1; | ||
451 | break; | ||
452 | case CT_ARRAY: | ||
453 | ctr->needsp = 1; | ||
454 | if (ctype_isrefarray(info)) { | ||
455 | if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } | ||
456 | ctype_appc(ctr, '['); | ||
457 | if (size != CTSIZE_INVALID) { | ||
458 | CTSize csize = ctype_child(ctr->cts, ct)->size; | ||
459 | ctype_appnum(ctr, csize ? size/csize : 0); | ||
460 | } else if ((info & CTF_VLA)) { | ||
461 | ctype_appc(ctr, '?'); | ||
462 | } | ||
463 | ctype_appc(ctr, ']'); | ||
464 | } else if ((info & CTF_COMPLEX)) { | ||
465 | if (size == 2*sizeof(float)) ctype_preplit(ctr, "float"); | ||
466 | ctype_preplit(ctr, "complex"); | ||
467 | return; | ||
468 | } else { | ||
469 | ctype_preplit(ctr, ")))"); | ||
470 | ctype_prepnum(ctr, size); | ||
471 | ctype_preplit(ctr, "__attribute__((vector_size("); | ||
472 | } | ||
473 | break; | ||
474 | case CT_FUNC: | ||
475 | ctr->needsp = 1; | ||
476 | if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } | ||
477 | ctype_appc(ctr, '('); | ||
478 | ctype_appc(ctr, ')'); | ||
479 | break; | ||
480 | default: | ||
481 | lua_assert(0); | ||
482 | break; | ||
483 | } | ||
484 | ct = ctype_get(ctr->cts, ctype_cid(info)); | ||
485 | } | ||
486 | } | ||
487 | |||
488 | /* Return a printable representation of a C type. */ | ||
489 | GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name) | ||
490 | { | ||
491 | global_State *g = G(L); | ||
492 | CTRepr ctr; | ||
493 | ctr.pb = ctr.pe = &ctr.buf[CTREPR_MAX/2]; | ||
494 | ctr.cts = ctype_ctsG(g); | ||
495 | ctr.L = L; | ||
496 | ctr.ok = 1; | ||
497 | ctr.needsp = 0; | ||
498 | if (name) ctype_prepstr(&ctr, strdata(name), name->len); | ||
499 | ctype_repr(&ctr, id); | ||
500 | if (LJ_UNLIKELY(!ctr.ok)) return lj_str_newlit(L, "?"); | ||
501 | return lj_str_new(L, ctr.pb, ctr.pe - ctr.pb); | ||
502 | } | ||
503 | |||
504 | /* Convert int64_t/uint64_t to string with 'LL' or 'ULL' suffix. */ | ||
505 | GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned) | ||
506 | { | ||
507 | char buf[1+20+3]; | ||
508 | char *p = buf+sizeof(buf); | ||
509 | int sign = 0; | ||
510 | *--p = 'L'; *--p = 'L'; | ||
511 | if (isunsigned) { | ||
512 | *--p = 'U'; | ||
513 | } else if ((int64_t)n < 0) { | ||
514 | n = (uint64_t)-(int64_t)n; | ||
515 | sign = 1; | ||
516 | } | ||
517 | do { *--p = (char)('0' + n % 10); } while (n /= 10); | ||
518 | if (sign) *--p = '-'; | ||
519 | return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p)); | ||
520 | } | ||
521 | |||
522 | /* Convert complex to string with 'i' or 'I' suffix. */ | ||
523 | GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size) | ||
524 | { | ||
525 | char buf[2*LUAI_MAXNUMBER2STR+2+1]; | ||
526 | TValue re, im; | ||
527 | size_t len; | ||
528 | if (size == 2*sizeof(double)) { | ||
529 | re.n = *(double *)sp; im.n = ((double *)sp)[1]; | ||
530 | } else { | ||
531 | re.n = (double)*(float *)sp; im.n = (double)((float *)sp)[1]; | ||
532 | } | ||
533 | len = lj_str_bufnum(buf, &re); | ||
534 | if (!(im.u32.hi & 0x80000000u) || im.n != im.n) buf[len++] = '+'; | ||
535 | len += lj_str_bufnum(buf+len, &im); | ||
536 | buf[len] = buf[len-1] >= 'a' ? 'I' : 'i'; | ||
537 | return lj_str_new(L, buf, len+1); | ||
538 | } | ||
539 | |||
540 | /* -- C type state -------------------------------------------------------- */ | ||
541 | |||
542 | /* Initialize C type table and state. */ | ||
543 | void lj_ctype_init(lua_State *L) | ||
544 | { | ||
545 | CTState *cts = lj_mem_newt(L, sizeof(CTState), CTState); | ||
546 | CType *ct = lj_mem_newvec(L, CTTYPETAB_MIN, CType); | ||
547 | const char *name = lj_ctype_typenames; | ||
548 | CTypeID id; | ||
549 | memset(cts, 0, sizeof(CTState)); | ||
550 | cts->tab = ct; | ||
551 | cts->sizetab = CTTYPETAB_MIN; | ||
552 | cts->top = CTTYPEINFO_NUM; | ||
553 | cts->L = NULL; | ||
554 | cts->g = G(L); | ||
555 | for (id = 0; id < CTTYPEINFO_NUM; id++, ct++) { | ||
556 | CTInfo info = lj_ctype_typeinfo[id]; | ||
557 | ct->size = (CTSize)((int32_t)(info << 16) >> 26); | ||
558 | ct->info = info & 0xffff03ffu; | ||
559 | if (ctype_type(info) == CT_KW || ctype_istypedef(info)) { | ||
560 | size_t len = strlen(name); | ||
561 | GCstr *str = lj_str_new(L, name, len); | ||
562 | ctype_setname(ct, str); | ||
563 | name += len+1; | ||
564 | lj_ctype_addname(cts, ct, id); | ||
565 | } else { | ||
566 | setgcrefnull(ct->name); | ||
567 | if (!ctype_isenum(info)) ctype_addtype(cts, ct, id); | ||
568 | } | ||
569 | } | ||
570 | setmref(G(L)->ctype_state, cts); | ||
571 | } | ||
572 | |||
573 | /* Free C type table and state. */ | ||
574 | void lj_ctype_freestate(global_State *g) | ||
575 | { | ||
576 | CTState *cts = ctype_ctsG(g); | ||
577 | if (cts) { | ||
578 | lj_mem_freevec(g, cts->tab, cts->sizetab, CType); | ||
579 | lj_mem_freet(g, cts); | ||
580 | } | ||
581 | } | ||
582 | |||
583 | #endif | ||
diff --git a/src/lj_ctype.h b/src/lj_ctype.h new file mode 100644 index 00000000..826f9785 --- /dev/null +++ b/src/lj_ctype.h | |||
@@ -0,0 +1,438 @@ | |||
1 | /* | ||
2 | ** C type management. | ||
3 | ** Copyright (C) 2005-2010 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 | /* | ||
39 | ** ---------- info ------------ | ||
40 | ** |type flags... A cid | size | sib | next | name | | ||
41 | ** +----------------------------+--------+-------+-------+-------+-- | ||
42 | ** |NUM BFvcUL.. A | size | | type | | | ||
43 | ** |STRUCT ..vcU..V A | size | field | name? | name? | | ||
44 | ** |PTR ..vcR... A cid | size | | type | | | ||
45 | ** |ARRAY VCvc...V A cid | size | | type | | | ||
46 | ** |VOID ..vc.... A | size | | type | | | ||
47 | ** |ENUM A cid | size | const | name? | name? | | ||
48 | ** |FUNC ....VS.. cc cid | nargs | field | name? | name? | | ||
49 | ** |TYPEDEF cid | | | name | name | | ||
50 | ** |ATTRIB attrnum cid | attr | sib? | type? | | | ||
51 | ** |FIELD cid | offset | field | | name? | | ||
52 | ** |BITFIELD B.vcU csz bsz pos | offset | field | | name? | | ||
53 | ** |CONSTVAL c cid | value | const | name | name | | ||
54 | ** |EXTERN cid | | | name | name | | ||
55 | ** |KW tok | size | | name | name | | ||
56 | ** +----------------------------+--------+-------+-------+-------+-- | ||
57 | ** ^^ ^^--- bits used for C type conversion dispatch | ||
58 | */ | ||
59 | |||
60 | /* C type info flags. TFFArrrr */ | ||
61 | #define CTF_BOOL 0x08000000u /* Boolean: NUM, BITFIELD. */ | ||
62 | #define CTF_FP 0x04000000u /* Floating-point: NUM. */ | ||
63 | #define CTF_CONST 0x02000000u /* Const qualifier. */ | ||
64 | #define CTF_VOLATILE 0x01000000u /* Volatile qualifier. */ | ||
65 | #define CTF_UNSIGNED 0x00800000u /* Unsigned: NUM, BITFIELD. */ | ||
66 | #define CTF_LONG 0x00400000u /* Long: NUM. */ | ||
67 | #define CTF_VLA 0x00100000u /* Variable-length: ARRAY, STRUCT. */ | ||
68 | #define CTF_REF 0x00800000u /* Reference: PTR. */ | ||
69 | #define CTF_VECTOR 0x08000000u /* Vector: ARRAY. */ | ||
70 | #define CTF_COMPLEX 0x04000000u /* Complex: ARRAY. */ | ||
71 | #define CTF_UNION 0x00800000u /* Union: STRUCT. */ | ||
72 | #define CTF_VARARG 0x00800000u /* Vararg: FUNC. */ | ||
73 | #define CTF_SSEREGPARM 0x00400000u /* SSE register parameters: FUNC. */ | ||
74 | |||
75 | #define CTF_QUAL (CTF_CONST|CTF_VOLATILE) | ||
76 | #define CTF_ALIGN (CTMASK_ALIGN<<CTSHIFT_ALIGN) | ||
77 | #define CTF_UCHAR ((char)-1 > 0 ? CTF_UNSIGNED : 0) | ||
78 | |||
79 | /* Flags used in parser. .F.Ammvf cp->attr */ | ||
80 | #define CTFP_ALIGNED 0x00000001u /* cp->attr + ALIGN */ | ||
81 | #define CTFP_PACKED 0x00000002u /* cp->attr */ | ||
82 | /* ...C...f cp->fattr */ | ||
83 | #define CTFP_CCONV 0x00000001u /* cp->fattr + CCONV/[SSE]REGPARM */ | ||
84 | |||
85 | /* C type info bitfields. */ | ||
86 | #define CTMASK_CID 0x0000ffffu /* Max. 65536 type IDs. */ | ||
87 | #define CTMASK_NUM 0xf0000000u /* Max. 16 type numbers. */ | ||
88 | #define CTSHIFT_NUM 28 | ||
89 | #define CTMASK_ALIGN 15 /* Max. alignment is 2^15. */ | ||
90 | #define CTSHIFT_ALIGN 16 | ||
91 | #define CTMASK_ATTRIB 255 /* Max. 256 attributes. */ | ||
92 | #define CTSHIFT_ATTRIB 16 | ||
93 | #define CTMASK_CCONV 3 /* Max. 4 calling conventions. */ | ||
94 | #define CTSHIFT_CCONV 16 | ||
95 | #define CTMASK_REGPARM 3 /* Max. 0-3 regparms. */ | ||
96 | #define CTSHIFT_REGPARM 18 | ||
97 | /* Bitfields only used in parser. */ | ||
98 | #define CTMASK_VSIZEP 15 /* Max. vector size is 2^15. */ | ||
99 | #define CTSHIFT_VSIZEP 4 | ||
100 | #define CTMASK_MSIZEP 255 /* Max. type size (via mode) is 128. */ | ||
101 | #define CTSHIFT_MSIZEP 8 | ||
102 | |||
103 | /* Info bits for BITFIELD. Max. size of bitfield is 64 bits. */ | ||
104 | #define CTBSZ_MAX 32 /* Max. size of bitfield is 32 bit. */ | ||
105 | #define CTBSZ_FIELD 127 /* Temp. marker for regular field. */ | ||
106 | #define CTMASK_BITPOS 127 | ||
107 | #define CTMASK_BITBSZ 127 | ||
108 | #define CTMASK_BITCSZ 127 | ||
109 | #define CTSHIFT_BITPOS 0 | ||
110 | #define CTSHIFT_BITBSZ 8 | ||
111 | #define CTSHIFT_BITCSZ 16 | ||
112 | |||
113 | #define CTF_INSERT(info, field, val) \ | ||
114 | info = (info & ~(CTMASK_##field<<CTSHIFT_##field)) | \ | ||
115 | (((CTSize)(val) & CTMASK_##field) << CTSHIFT_##field) | ||
116 | |||
117 | /* Calling conventions. */ | ||
118 | enum { CTCC_CDECL, CTCC_THISCALL, CTCC_FASTCALL, CTCC_STDCALL }; | ||
119 | |||
120 | /* Attribute numbers. */ | ||
121 | enum { | ||
122 | CTA_NONE, /* Ignored attribute. Must be zero. */ | ||
123 | CTA_QUAL, /* Unmerged qualifiers. */ | ||
124 | CTA_ALIGN, /* Alignment override. */ | ||
125 | CTA_SUBTYPE, /* Transparent sub-type. */ | ||
126 | CTA_BAD, /* To catch bad IDs. */ | ||
127 | CTA__MAX | ||
128 | }; | ||
129 | |||
130 | /* Special sizes. */ | ||
131 | #define CTSIZE_INVALID 0xffffffffu | ||
132 | |||
133 | typedef uint32_t CTInfo; /* Type info. */ | ||
134 | typedef uint32_t CTSize; /* Type size. */ | ||
135 | typedef uint32_t CTypeID; /* Type ID. */ | ||
136 | typedef uint16_t CTypeID1; /* Minimum-sized type ID. */ | ||
137 | |||
138 | /* C type table element. */ | ||
139 | typedef struct CType { | ||
140 | CTInfo info; /* Type info. */ | ||
141 | CTSize size; /* Type size or other info. */ | ||
142 | CTypeID1 sib; /* Sibling element. */ | ||
143 | CTypeID1 next; /* Next element in hash chain. */ | ||
144 | GCRef name; /* Element name (GCstr). */ | ||
145 | } CType; | ||
146 | |||
147 | #define CTHASH_SIZE 128 /* Number of hash anchors. */ | ||
148 | #define CTHASH_MASK (CTHASH_SIZE-1) | ||
149 | |||
150 | /* C type state. */ | ||
151 | typedef struct CTState { | ||
152 | CType *tab; /* C type table. */ | ||
153 | CTypeID top; /* Current top of C type table. */ | ||
154 | MSize sizetab; /* Size of C type table. */ | ||
155 | lua_State *L; /* Lua state (needed for errors and allocations). */ | ||
156 | global_State *g; /* Global state. */ | ||
157 | CTypeID1 hash[CTHASH_SIZE]; /* Hash anchors for C type table. */ | ||
158 | } CTState; | ||
159 | |||
160 | #define CTINFO(ct, flags) (((CTInfo)(ct) << CTSHIFT_NUM) + (flags)) | ||
161 | #define CTALIGN(al) ((CTSize)(al) << CTSHIFT_ALIGN) | ||
162 | #define CTATTRIB(at) ((CTInfo)(at) << CTSHIFT_ATTRIB) | ||
163 | |||
164 | #define ctype_type(info) ((info) >> CTSHIFT_NUM) | ||
165 | #define ctype_cid(info) ((CTypeID)((info) & CTMASK_CID)) | ||
166 | #define ctype_align(info) (((info) >> CTSHIFT_ALIGN) & CTMASK_ALIGN) | ||
167 | #define ctype_attrib(info) (((info) >> CTSHIFT_ATTRIB) & CTMASK_ATTRIB) | ||
168 | #define ctype_bitpos(info) (((info) >> CTSHIFT_BITPOS) & CTMASK_BITPOS) | ||
169 | #define ctype_bitbsz(info) (((info) >> CTSHIFT_BITBSZ) & CTMASK_BITBSZ) | ||
170 | #define ctype_bitcsz(info) (((info) >> CTSHIFT_BITCSZ) & CTMASK_BITCSZ) | ||
171 | #define ctype_vsizeP(info) (((info) >> CTSHIFT_VSIZEP) & CTMASK_VSIZEP) | ||
172 | #define ctype_msizeP(info) (((info) >> CTSHIFT_MSIZEP) & CTMASK_MSIZEP) | ||
173 | |||
174 | /* Simple type checks. */ | ||
175 | #define ctype_isnum(info) (ctype_type((info)) == CT_NUM) | ||
176 | #define ctype_isvoid(info) (ctype_type((info)) == CT_VOID) | ||
177 | #define ctype_isptr(info) (ctype_type((info)) == CT_PTR) | ||
178 | #define ctype_isarray(info) (ctype_type((info)) == CT_ARRAY) | ||
179 | #define ctype_isstruct(info) (ctype_type((info)) == CT_STRUCT) | ||
180 | #define ctype_isfunc(info) (ctype_type((info)) == CT_FUNC) | ||
181 | #define ctype_isenum(info) (ctype_type((info)) == CT_ENUM) | ||
182 | #define ctype_istypedef(info) (ctype_type((info)) == CT_TYPEDEF) | ||
183 | #define ctype_isattrib(info) (ctype_type((info)) == CT_ATTRIB) | ||
184 | #define ctype_isfield(info) (ctype_type((info)) == CT_FIELD) | ||
185 | #define ctype_isbitfield(info) (ctype_type((info)) == CT_BITFIELD) | ||
186 | #define ctype_isconstval(info) (ctype_type((info)) == CT_CONSTVAL) | ||
187 | #define ctype_hassize(info) (ctype_type((info)) <= CT_HASSIZE) | ||
188 | |||
189 | /* Combined type and flag checks. */ | ||
190 | #define ctype_isinteger(info) \ | ||
191 | (((info) & (CTMASK_NUM|CTF_BOOL|CTF_FP)) == CTINFO(CT_NUM, 0)) | ||
192 | #define ctype_isinteger_or_bool(info) \ | ||
193 | (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0)) | ||
194 | #define ctype_isbool(info) \ | ||
195 | (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL)) | ||
196 | #define ctype_isconstchar(ct) \ | ||
197 | (((ct)->info & (CTMASK_NUM|CTF_CONST|CTF_BOOL|CTF_FP)) == \ | ||
198 | CTINFO(CT_NUM, CTF_CONST) && (ct)->size == 1) | ||
199 | |||
200 | #define ctype_ispointer(info) \ | ||
201 | ((ctype_type(info) >> 1) == (CT_PTR >> 1)) /* Pointer or array. */ | ||
202 | #define ctype_isref(info) \ | ||
203 | (((info) & (CTMASK_NUM|CTF_REF)) == CTINFO(CT_PTR, CTF_REF)) | ||
204 | |||
205 | #define ctype_isrefarray(info) \ | ||
206 | (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0)) | ||
207 | #define ctype_isvalarray(info) \ | ||
208 | (ctype_isarray(info) && (info & (CTF_VECTOR|CTF_COMPLEX))) | ||
209 | #define ctype_isvector(info) \ | ||
210 | (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR)) | ||
211 | #define ctype_iscomplex(info) \ | ||
212 | (((info) & (CTMASK_NUM|CTF_COMPLEX)) == CTINFO(CT_ARRAY, CTF_COMPLEX)) | ||
213 | |||
214 | #define ctype_isvltype(info) \ | ||
215 | (((info) & ((CTMASK_NUM|CTF_VLA) - (2u<<CTSHIFT_NUM))) == \ | ||
216 | CTINFO(CT_STRUCT, CTF_VLA)) /* VL array or VL struct. */ | ||
217 | #define ctype_isvlarray(info) \ | ||
218 | (((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_ARRAY, CTF_VLA)) | ||
219 | #define ctype_isvlstruct(info) \ | ||
220 | (((info) & (CTMASK_NUM|CTF_VLA)) == CTINFO(CT_STRUCT, CTF_VLA)) | ||
221 | |||
222 | #define ctype_isxattrib(info, at) \ | ||
223 | (((info) & (CTMASK_NUM|CTATTRIB(CTMASK_ATTRIB))) == \ | ||
224 | CTINFO(CT_ATTRIB, CTATTRIB(at))) | ||
225 | |||
226 | LJ_STATIC_ASSERT(((int)CT_PTR & (int)CT_ARRAY) == CT_PTR); | ||
227 | LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT); | ||
228 | |||
229 | /* Target-dependent sizes and alignments. */ | ||
230 | #if LJ_64 | ||
231 | #define CTSIZE_PTR 8 | ||
232 | #define CTALIGN_PTR CTALIGN(3) | ||
233 | #else | ||
234 | #define CTSIZE_PTR 4 | ||
235 | #define CTALIGN_PTR CTALIGN(2) | ||
236 | #endif | ||
237 | |||
238 | #define CTINFO_REF(ref) \ | ||
239 | CTINFO(CT_PTR, (CTF_CONST|CTF_REF|CTALIGN_PTR) + (ref)) | ||
240 | |||
241 | #define CT_MEMALIGN 3 /* Alignment guaranteed by memory allocator. */ | ||
242 | |||
243 | /* -- Predefined types ---------------------------------------------------- */ | ||
244 | |||
245 | /* Target-dependent types. */ | ||
246 | #if LJ_TARGET_PPC | ||
247 | #define CTTYDEFP(_) \ | ||
248 | _(LINT32, 4, CT_NUM, CTF_LONG|CTALIGN(2)) | ||
249 | #else | ||
250 | #define CTTYDEFP(_) | ||
251 | #endif | ||
252 | |||
253 | /* Common types. */ | ||
254 | #define CTTYDEF(_) \ | ||
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) \ | ||
277 | CTTYDEFP(_) \ | ||
278 | /* End of type list. */ | ||
279 | |||
280 | /* Public predefined type IDs. */ | ||
281 | enum { | ||
282 | #define CTTYIDDEF(id, sz, ct, info) CTID_##id, | ||
283 | CTTYDEF(CTTYIDDEF) | ||
284 | #undef CTTYIDDEF | ||
285 | /* Predefined typedefs and keywords follow. */ | ||
286 | CTID_MAX = 65536 | ||
287 | }; | ||
288 | |||
289 | /* Target-dependent type IDs. */ | ||
290 | #if LJ_64 | ||
291 | #define CTID_INT_PSZ CTID_INT64 | ||
292 | #define CTID_UINT_PSZ CTID_UINT64 | ||
293 | #else | ||
294 | #define CTID_INT_PSZ CTID_INT32 | ||
295 | #define CTID_UINT_PSZ CTID_UINT32 | ||
296 | #endif | ||
297 | |||
298 | #if LJ_ABI_WIN | ||
299 | #define CTID_WCHAR CTID_UINT16 | ||
300 | #elif LJ_TARGET_PPC | ||
301 | #define CTID_WCHAR CTID_LINT32 | ||
302 | #else | ||
303 | #define CTID_WCHAR CTID_INT32 | ||
304 | #endif | ||
305 | |||
306 | /* -- C tokens and keywords ----------------------------------------------- */ | ||
307 | |||
308 | /* C lexer keywords. */ | ||
309 | #define CTOKDEF(_) \ | ||
310 | _(IDENT, "<identifier>") _(STRING, "<string>") \ | ||
311 | _(INTEGER, "<integer>") _(EOF, "<eof>") \ | ||
312 | _(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \ | ||
313 | _(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->") | ||
314 | |||
315 | /* Simple declaration specifiers. */ | ||
316 | #define CDSDEF(_) \ | ||
317 | _(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \ | ||
318 | _(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \ | ||
319 | _(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \ | ||
320 | _(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER) | ||
321 | |||
322 | /* C keywords. */ | ||
323 | #define CKWDEF(_) \ | ||
324 | CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \ | ||
325 | _(DECLSPEC) _(CCDECL) _(PTRSZ) \ | ||
326 | _(STRUCT) _(UNION) _(ENUM) \ | ||
327 | _(SIZEOF) _(ALIGNOF) | ||
328 | |||
329 | /* C token numbers. */ | ||
330 | enum { | ||
331 | CTOK_OFS = 255, | ||
332 | #define CTOKNUM(name, sym) CTOK_##name, | ||
333 | #define CKWNUM(name) CTOK_##name, | ||
334 | CTOKDEF(CTOKNUM) | ||
335 | CKWDEF(CKWNUM) | ||
336 | #undef CTOKNUM | ||
337 | #undef CKWNUM | ||
338 | CTOK_FIRSTDECL = CTOK_VOID, | ||
339 | CTOK_FIRSTSCL = CTOK_TYPEDEF, | ||
340 | CTOK_LASTDECLFLAG = CTOK_REGISTER, | ||
341 | CTOK_LASTDECL = CTOK_ENUM | ||
342 | }; | ||
343 | |||
344 | /* Declaration specifier flags. */ | ||
345 | enum { | ||
346 | #define CDSFLAG(name) CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)), | ||
347 | CDSDEF(CDSFLAG) | ||
348 | #undef CDSFLAG | ||
349 | CDF__END | ||
350 | }; | ||
351 | |||
352 | #define CDF_SCL (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER) | ||
353 | |||
354 | /* -- C type management --------------------------------------------------- */ | ||
355 | |||
356 | #define ctype_ctsG(g) (mref((g)->ctype_state, CTState)) | ||
357 | |||
358 | /* Get C type state. */ | ||
359 | static LJ_AINLINE CTState *ctype_cts(lua_State *L) | ||
360 | { | ||
361 | CTState *cts = ctype_ctsG(G(L)); | ||
362 | cts->L = L; /* Save L for errors and allocations. */ | ||
363 | return cts; | ||
364 | } | ||
365 | |||
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))) | ||
371 | |||
372 | /* Check C type ID for validity when assertions are enabled. */ | ||
373 | static LJ_AINLINE CTypeID ctype_check(CTState *cts, CTypeID id) | ||
374 | { | ||
375 | lua_assert(id > 0 && id < cts->top); UNUSED(cts); | ||
376 | return id; | ||
377 | } | ||
378 | |||
379 | /* Get C type for C type ID. */ | ||
380 | static LJ_AINLINE CType *ctype_get(CTState *cts, CTypeID id) | ||
381 | { | ||
382 | return &cts->tab[ctype_check(cts, id)]; | ||
383 | } | ||
384 | |||
385 | /* Get C type ID for a C type. */ | ||
386 | #define ctype_typeid(cts, ct) ((CTypeID)((ct) - (cts)->tab)) | ||
387 | |||
388 | /* Get child C type. */ | ||
389 | static LJ_AINLINE CType *ctype_child(CTState *cts, CType *ct) | ||
390 | { | ||
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)); | ||
394 | } | ||
395 | |||
396 | /* Get raw type for a C type ID. */ | ||
397 | static LJ_AINLINE CType *ctype_raw(CTState *cts, CTypeID id) | ||
398 | { | ||
399 | CType *ct = ctype_get(cts, id); | ||
400 | while (ctype_isattrib(ct->info)) ct = ctype_child(cts, ct); | ||
401 | return ct; | ||
402 | } | ||
403 | |||
404 | /* Get raw type of the child of a C type. */ | ||
405 | static LJ_AINLINE CType *ctype_rawchild(CTState *cts, CType *ct) | ||
406 | { | ||
407 | do { ct = ctype_child(cts, ct); } while (ctype_isattrib(ct->info)); | ||
408 | return ct; | ||
409 | } | ||
410 | |||
411 | /* Set the name of a C type table element. */ | ||
412 | static LJ_AINLINE void ctype_setname(CType *ct, GCstr *s) | ||
413 | { | ||
414 | /* NOBARRIER: mark string as fixed -- the C type table is never collected. */ | ||
415 | fixstring(s); | ||
416 | setgcref(ct->name, obj2gco(s)); | ||
417 | } | ||
418 | |||
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, | ||
423 | uint32_t tmask); | ||
424 | LJ_FUNC CType *lj_ctype_getfield(CTState *cts, CType *ct, GCstr *name, | ||
425 | CTSize *ofs); | ||
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 GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name); | ||
431 | LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned); | ||
432 | LJ_FUNC GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size); | ||
433 | LJ_FUNC void lj_ctype_init(lua_State *L); | ||
434 | LJ_FUNC void lj_ctype_freestate(global_State *g); | ||
435 | |||
436 | #endif | ||
437 | |||
438 | #endif | ||
diff --git a/src/lj_state.c b/src/lj_state.c index 91f35638..79ab34e3 100644 --- a/src/lj_state.c +++ b/src/lj_state.c | |||
@@ -18,8 +18,8 @@ | |||
18 | #include "lj_meta.h" | 18 | #include "lj_meta.h" |
19 | #include "lj_state.h" | 19 | #include "lj_state.h" |
20 | #include "lj_frame.h" | 20 | #include "lj_frame.h" |
21 | #if LJ_HASJIT | 21 | #if LJ_HASFFI |
22 | #include "lj_mcode.h" | 22 | #include "lj_ctype.h" |
23 | #endif | 23 | #endif |
24 | #include "lj_trace.h" | 24 | #include "lj_trace.h" |
25 | #include "lj_dispatch.h" | 25 | #include "lj_dispatch.h" |
@@ -160,6 +160,9 @@ static void close_state(lua_State *L) | |||
160 | lua_assert(gcref(g->gc.root) == obj2gco(L)); | 160 | lua_assert(gcref(g->gc.root) == obj2gco(L)); |
161 | lua_assert(g->strnum == 0); | 161 | lua_assert(g->strnum == 0); |
162 | lj_trace_freestate(g); | 162 | lj_trace_freestate(g); |
163 | #if LJ_HASFFI | ||
164 | lj_ctype_freestate(g); | ||
165 | #endif | ||
163 | lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef); | 166 | lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef); |
164 | lj_str_freebuf(g, &g->tmpbuf); | 167 | lj_str_freebuf(g, &g->tmpbuf); |
165 | lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); | 168 | lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); |
diff --git a/src/ljamalg.c b/src/ljamalg.c index 4f3b5025..d37daa2f 100644 --- a/src/ljamalg.c +++ b/src/ljamalg.c | |||
@@ -40,6 +40,7 @@ | |||
40 | #include "lj_api.c" | 40 | #include "lj_api.c" |
41 | #include "lj_lex.c" | 41 | #include "lj_lex.c" |
42 | #include "lj_parse.c" | 42 | #include "lj_parse.c" |
43 | #include "lj_ctype.c" | ||
43 | #include "lj_lib.c" | 44 | #include "lj_lib.c" |
44 | #include "lj_ir.c" | 45 | #include "lj_ir.c" |
45 | #include "lj_opt_mem.c" | 46 | #include "lj_opt_mem.c" |