1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Dmitry Stogov <dmitry@zend.com> |
16 +----------------------------------------------------------------------+
17 */
18
19 #ifndef PHP_FFI_H
20 #define PHP_FFI_H
21
22 extern zend_module_entry ffi_module_entry;
23 #define phpext_ffi_ptr &ffi_module_entry
24
25 typedef enum _zend_ffi_api_restriction {
26 ZEND_FFI_DISABLED = 0, /* completely disabled */
27 ZEND_FFI_ENABLED = 1, /* enabled everywhere */
28 ZEND_FFI_PRELOAD = 2, /* enabled only in preloaded scripts and CLI */
29 } zend_ffi_api_restriction;
30
31 typedef struct _zend_ffi_type zend_ffi_type;
32
33 ZEND_BEGIN_MODULE_GLOBALS(ffi)
34 zend_ffi_api_restriction restriction;
35 zend_bool is_cli;
36
37 /* predefined ffi_types */
38 HashTable types;
39
40 /* preloading */
41 char *preload;
42 HashTable *scopes; /* list of preloaded scopes */
43
44 /* callbacks */
45 HashTable *callbacks;
46
47 /* weak type references */
48 HashTable *weak_types;
49
50 /* ffi_parser */
51 JMP_BUF bailout;
52 unsigned const char *buf;
53 unsigned const char *end;
54 unsigned const char *pos;
55 unsigned const char *text;
56 int line;
57 HashTable *symbols;
58 HashTable *tags;
59 zend_bool allow_vla;
60 zend_bool attribute_parsing;
61 zend_bool persistent;
62 uint32_t default_type_attr;
63 ZEND_END_MODULE_GLOBALS(ffi)
64
65 ZEND_EXTERN_MODULE_GLOBALS(ffi)
66
67 #ifdef PHP_WIN32
68 # define PHP_FFI_API __declspec(dllexport)
69 #elif defined(__GNUC__) && __GNUC__ >= 4
70 # define PHP_FFI_API __attribute__ ((visibility("default")))
71 #else
72 # define PHP_FFI_API
73 #endif
74
75 #define FFI_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(ffi, v)
76
77 #define ZEND_FFI_DCL_VOID (1<<0)
78 #define ZEND_FFI_DCL_CHAR (1<<1)
79 #define ZEND_FFI_DCL_SHORT (1<<2)
80 #define ZEND_FFI_DCL_INT (1<<3)
81 #define ZEND_FFI_DCL_LONG (1<<4)
82 #define ZEND_FFI_DCL_LONG_LONG (1<<5)
83 #define ZEND_FFI_DCL_FLOAT (1<<6)
84 #define ZEND_FFI_DCL_DOUBLE (1<<7)
85 #define ZEND_FFI_DCL_SIGNED (1<<8)
86 #define ZEND_FFI_DCL_UNSIGNED (1<<9)
87 #define ZEND_FFI_DCL_BOOL (1<<10)
88 #define ZEND_FFI_DCL_COMPLEX (1<<11)
89
90 #define ZEND_FFI_DCL_STRUCT (1<<12)
91 #define ZEND_FFI_DCL_UNION (1<<13)
92 #define ZEND_FFI_DCL_ENUM (1<<14)
93 #define ZEND_FFI_DCL_TYPEDEF_NAME (1<<15)
94
95 #define ZEND_FFI_DCL_TYPE_SPECIFIERS \
96 (ZEND_FFI_DCL_VOID|ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_SHORT \
97 |ZEND_FFI_DCL_INT|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_LONG_LONG \
98 |ZEND_FFI_DCL_FLOAT|ZEND_FFI_DCL_DOUBLE|ZEND_FFI_DCL_SIGNED \
99 |ZEND_FFI_DCL_UNSIGNED|ZEND_FFI_DCL_BOOL|ZEND_FFI_DCL_COMPLEX \
100 |ZEND_FFI_DCL_STRUCT|ZEND_FFI_DCL_UNION|ZEND_FFI_DCL_ENUM \
101 |ZEND_FFI_DCL_TYPEDEF_NAME)
102
103 #define ZEND_FFI_DCL_TYPEDEF (1<<16)
104 #define ZEND_FFI_DCL_EXTERN (1<<17)
105 #define ZEND_FFI_DCL_STATIC (1<<18)
106 #define ZEND_FFI_DCL_AUTO (1<<19)
107 #define ZEND_FFI_DCL_REGISTER (1<<20)
108
109 #define ZEND_FFI_DCL_STORAGE_CLASS \
110 (ZEND_FFI_DCL_TYPEDEF|ZEND_FFI_DCL_EXTERN|ZEND_FFI_DCL_STATIC \
111 |ZEND_FFI_DCL_AUTO|ZEND_FFI_DCL_REGISTER)
112
113 #define ZEND_FFI_DCL_CONST (1<<21)
114 #define ZEND_FFI_DCL_RESTRICT (1<<22)
115 #define ZEND_FFI_DCL_VOLATILE (1<<23)
116 #define ZEND_FFI_DCL_ATOMIC (1<<24)
117
118 #define ZEND_FFI_DCL_TYPE_QUALIFIERS \
119 (ZEND_FFI_DCL_CONST|ZEND_FFI_DCL_RESTRICT|ZEND_FFI_DCL_VOLATILE \
120 |ZEND_FFI_DCL_ATOMIC)
121
122 #define ZEND_FFI_DCL_INLINE (1<<25)
123 #define ZEND_FFI_DCL_NO_RETURN (1<<26)
124
125 #define ZEND_FFI_ABI_DEFAULT 0
126
127 #define ZEND_FFI_ABI_CDECL 1 // FFI_DEFAULT_ABI
128 #define ZEND_FFI_ABI_FASTCALL 2 // FFI_FASTCALL
129 #define ZEND_FFI_ABI_THISCALL 3 // FFI_THISCALL
130 #define ZEND_FFI_ABI_STDCALL 4 // FFI_STDCALL
131 #define ZEND_FFI_ABI_PASCAL 5 // FFI_PASCAL
132 #define ZEND_FFI_ABI_REGISTER 6 // FFI_REGISTER
133 #define ZEND_FFI_ABI_MS 7 // FFI_MS_CDECL
134 #define ZEND_FFI_ABI_SYSV 8 // FFI_SYSV
135 #define ZEND_FFI_ABI_VECTORCALL 9 // FFI_VECTORCALL
136
137 #define ZEND_FFI_ATTR_CONST (1<<0)
138 #define ZEND_FFI_ATTR_INCOMPLETE_TAG (1<<1)
139 #define ZEND_FFI_ATTR_VARIADIC (1<<2)
140 #define ZEND_FFI_ATTR_INCOMPLETE_ARRAY (1<<3)
141 #define ZEND_FFI_ATTR_VLA (1<<4)
142 #define ZEND_FFI_ATTR_UNION (1<<5)
143 #define ZEND_FFI_ATTR_PACKED (1<<6)
144 #define ZEND_FFI_ATTR_MS_STRUCT (1<<7)
145 #define ZEND_FFI_ATTR_GCC_STRUCT (1<<8)
146
147 #define ZEND_FFI_ATTR_PERSISTENT (1<<9)
148 #define ZEND_FFI_ATTR_STORED (1<<10)
149
150 #define ZEND_FFI_STRUCT_ATTRS \
151 (ZEND_FFI_ATTR_UNION|ZEND_FFI_ATTR_PACKED|ZEND_FFI_ATTR_MS_STRUCT \
152 |ZEND_FFI_ATTR_GCC_STRUCT)
153
154 #define ZEND_FFI_ENUM_ATTRS \
155 (ZEND_FFI_ATTR_PACKED)
156
157 #define ZEND_FFI_ARRAY_ATTRS \
158 (ZEND_FFI_ATTR_CONST|ZEND_FFI_ATTR_VLA|ZEND_FFI_ATTR_INCOMPLETE_ARRAY)
159
160 #define ZEND_FFI_FUNC_ATTRS \
161 (ZEND_FFI_ATTR_VARIADIC)
162
163 #define ZEND_FFI_POINTER_ATTRS \
164 (ZEND_FFI_ATTR_CONST)
165
166 typedef struct _zend_ffi_dcl {
167 uint32_t flags;
168 uint32_t align;
169 uint16_t attr;
170 uint16_t abi;
171 zend_ffi_type *type;
172 } zend_ffi_dcl;
173
174 #define ZEND_FFI_ATTR_INIT {0, 0, 0, 0, NULL}
175
176 typedef enum _zend_ffi_val_kind {
177 ZEND_FFI_VAL_EMPTY,
178 ZEND_FFI_VAL_ERROR,
179 ZEND_FFI_VAL_INT32,
180 ZEND_FFI_VAL_INT64,
181 ZEND_FFI_VAL_UINT32,
182 ZEND_FFI_VAL_UINT64,
183 ZEND_FFI_VAL_FLOAT,
184 ZEND_FFI_VAL_DOUBLE,
185 ZEND_FFI_VAL_LONG_DOUBLE,
186 ZEND_FFI_VAL_CHAR,
187 ZEND_FFI_VAL_STRING,
188 ZEND_FFI_VAL_NAME, /* attribute value */
189 } zend_ffi_val_kind;
190
191 #ifdef HAVE_LONG_DOUBLE
192 typedef long double zend_ffi_double;
193 #else
194 typedef double zend_ffi_double;
195 #endif
196
197 typedef struct _zend_ffi_val {
198 zend_ffi_val_kind kind;
199 union {
200 uint64_t u64;
201 int64_t i64;
202 zend_ffi_double d;
203 signed char ch;
204 struct {
205 const char *str;
206 size_t len;
207 };
208 };
209 } zend_ffi_val;
210
211 int zend_ffi_parse_decl(const char *str, size_t len);
212 int zend_ffi_parse_type(const char *str, size_t len, zend_ffi_dcl *dcl);
213
214 /* parser callbacks */
215 void ZEND_NORETURN zend_ffi_parser_error(const char *msg, ...);
216 int zend_ffi_is_typedef_name(const char *name, size_t name_len);
217 void zend_ffi_resolve_typedef(const char *name, size_t name_len, zend_ffi_dcl *dcl);
218 void zend_ffi_resolve_const(const char *name, size_t name_len, zend_ffi_val *val);
219 void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl *dcl, zend_bool incomplete);
220 void zend_ffi_make_enum_type(zend_ffi_dcl *dcl);
221 void zend_ffi_add_enum_val(zend_ffi_dcl *enum_dcl, const char *name, size_t name_len, zend_ffi_val *val, int64_t *min, int64_t *max, int64_t *last);
222 void zend_ffi_make_struct_type(zend_ffi_dcl *dcl);
223 void zend_ffi_add_field(zend_ffi_dcl *struct_dcl, const char *name, size_t name_len, zend_ffi_dcl *field_dcl);
224 void zend_ffi_add_anonymous_field(zend_ffi_dcl *struct_dcl, zend_ffi_dcl *field_dcl);
225 void zend_ffi_add_bit_field(zend_ffi_dcl *struct_dcl, const char *name, size_t name_len, zend_ffi_dcl *field_dcl, zend_ffi_val *bits);
226 void zend_ffi_adjust_struct_size(zend_ffi_dcl *dcl);
227 void zend_ffi_make_pointer_type(zend_ffi_dcl *dcl);
228 void zend_ffi_make_array_type(zend_ffi_dcl *dcl, zend_ffi_val *len);
229 void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args, zend_ffi_dcl *nested_dcl);
230 void zend_ffi_add_arg(HashTable **args, const char *name, size_t name_len, zend_ffi_dcl *arg_dcl);
231 void zend_ffi_declare(const char *name, size_t name_len, zend_ffi_dcl *dcl);
232 void zend_ffi_add_attribute(zend_ffi_dcl *dcl, const char *name, size_t name_len);
233 void zend_ffi_add_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, int n, zend_ffi_val *val);
234 void zend_ffi_add_msvc_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, zend_ffi_val *val);
235 void zend_ffi_set_abi(zend_ffi_dcl *dcl, uint16_t abi);
236 void zend_ffi_nested_declaration(zend_ffi_dcl *dcl, zend_ffi_dcl *nested_dcl);
237 void zend_ffi_align_as_type(zend_ffi_dcl *dcl, zend_ffi_dcl *align_dcl);
238 void zend_ffi_align_as_val(zend_ffi_dcl *dcl, zend_ffi_val *align_val);
239 void zend_ffi_validate_type_name(zend_ffi_dcl *dcl);
240
241 void zend_ffi_expr_conditional(zend_ffi_val *val, zend_ffi_val *op2, zend_ffi_val *op3);
242 void zend_ffi_expr_bool_or(zend_ffi_val *val, zend_ffi_val *op2);
243 void zend_ffi_expr_bool_and(zend_ffi_val *val, zend_ffi_val *op2);
244 void zend_ffi_expr_bw_or(zend_ffi_val *val, zend_ffi_val *op2);
245 void zend_ffi_expr_bw_xor(zend_ffi_val *val, zend_ffi_val *op2);
246 void zend_ffi_expr_bw_and(zend_ffi_val *val, zend_ffi_val *op2);
247 void zend_ffi_expr_is_equal(zend_ffi_val *val, zend_ffi_val *op2);
248 void zend_ffi_expr_is_not_equal(zend_ffi_val *val, zend_ffi_val *op2);
249 void zend_ffi_expr_is_less(zend_ffi_val *val, zend_ffi_val *op2);
250 void zend_ffi_expr_is_greater(zend_ffi_val *val, zend_ffi_val *op2);
251 void zend_ffi_expr_is_less_or_equal(zend_ffi_val *val, zend_ffi_val *op2);
252 void zend_ffi_expr_is_greater_or_equal(zend_ffi_val *val, zend_ffi_val *op2);
253 void zend_ffi_expr_shift_left(zend_ffi_val *val, zend_ffi_val *op2);
254 void zend_ffi_expr_shift_right(zend_ffi_val *val, zend_ffi_val *op2);
255 void zend_ffi_expr_add(zend_ffi_val *val, zend_ffi_val *op2);
256 void zend_ffi_expr_sub(zend_ffi_val *val, zend_ffi_val *op2);
257 void zend_ffi_expr_mul(zend_ffi_val *val, zend_ffi_val *op2);
258 void zend_ffi_expr_div(zend_ffi_val *val, zend_ffi_val *op2);
259 void zend_ffi_expr_mod(zend_ffi_val *val, zend_ffi_val *op2);
260 void zend_ffi_expr_cast(zend_ffi_val *val, zend_ffi_dcl *dcl);
261 void zend_ffi_expr_plus(zend_ffi_val *val);
262 void zend_ffi_expr_neg(zend_ffi_val *val);
263 void zend_ffi_expr_bw_not(zend_ffi_val *val);
264 void zend_ffi_expr_bool_not(zend_ffi_val *val);
265 void zend_ffi_expr_sizeof_val(zend_ffi_val *val);
266 void zend_ffi_expr_sizeof_type(zend_ffi_val *val, zend_ffi_dcl *dcl);
267 void zend_ffi_expr_alignof_val(zend_ffi_val *val);
268 void zend_ffi_expr_alignof_type(zend_ffi_val *val, zend_ffi_dcl *dcl);
269
zend_ffi_val_error(zend_ffi_val * val)270 static zend_always_inline void zend_ffi_val_error(zend_ffi_val *val) /* {{{ */
271 {
272 val->kind = ZEND_FFI_VAL_ERROR;
273 }
274 /* }}} */
275
276 void zend_ffi_val_number(zend_ffi_val *val, int base, const char *str, size_t str_len);
277 void zend_ffi_val_float_number(zend_ffi_val *val, const char *str, size_t str_len);
278 void zend_ffi_val_string(zend_ffi_val *val, const char *str, size_t str_len);
279 void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len);
280
281 #endif /* PHP_FFI_H */
282