php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
php_ffi.h
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Dmitry Stogov <dmitry@zend.com> |
14 +----------------------------------------------------------------------+
15 */
16
17#ifndef PHP_FFI_H
18#define PHP_FFI_H
19
21#define phpext_ffi_ptr &ffi_module_entry
22
24 ZEND_FFI_DISABLED = 0, /* completely disabled */
25 ZEND_FFI_ENABLED = 1, /* enabled everywhere */
26 ZEND_FFI_PRELOAD = 2, /* enabled only in preloaded scripts and CLI */
28
30
33 bool is_cli;
34
35 /* predefined ffi_types */
37
38 /* preloading */
39 char *preload;
40 HashTable *scopes; /* list of preloaded scopes */
41
42 /* callbacks */
44
45 /* weak type references */
47
48 /* ffi_parser */
50 unsigned const char *buf;
51 unsigned const char *end;
52 unsigned const char *pos;
53 unsigned const char *text;
54 int line;
62
64
65#ifdef PHP_WIN32
66# define PHP_FFI_API __declspec(dllexport)
67#elif defined(__GNUC__) && __GNUC__ >= 4
68# define PHP_FFI_API __attribute__ ((visibility("default")))
69#else
70# define PHP_FFI_API
71#endif
72
73#define FFI_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(ffi, v)
74
75#define ZEND_FFI_DCL_VOID (1<<0)
76#define ZEND_FFI_DCL_CHAR (1<<1)
77#define ZEND_FFI_DCL_SHORT (1<<2)
78#define ZEND_FFI_DCL_INT (1<<3)
79#define ZEND_FFI_DCL_LONG (1<<4)
80#define ZEND_FFI_DCL_LONG_LONG (1<<5)
81#define ZEND_FFI_DCL_FLOAT (1<<6)
82#define ZEND_FFI_DCL_DOUBLE (1<<7)
83#define ZEND_FFI_DCL_SIGNED (1<<8)
84#define ZEND_FFI_DCL_UNSIGNED (1<<9)
85#define ZEND_FFI_DCL_BOOL (1<<10)
86#define ZEND_FFI_DCL_COMPLEX (1<<11)
87
88#define ZEND_FFI_DCL_STRUCT (1<<12)
89#define ZEND_FFI_DCL_UNION (1<<13)
90#define ZEND_FFI_DCL_ENUM (1<<14)
91#define ZEND_FFI_DCL_TYPEDEF_NAME (1<<15)
92
93#define ZEND_FFI_DCL_TYPE_SPECIFIERS \
94 (ZEND_FFI_DCL_VOID|ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_SHORT \
95 |ZEND_FFI_DCL_INT|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_LONG_LONG \
96 |ZEND_FFI_DCL_FLOAT|ZEND_FFI_DCL_DOUBLE|ZEND_FFI_DCL_SIGNED \
97 |ZEND_FFI_DCL_UNSIGNED|ZEND_FFI_DCL_BOOL|ZEND_FFI_DCL_COMPLEX \
98 |ZEND_FFI_DCL_STRUCT|ZEND_FFI_DCL_UNION|ZEND_FFI_DCL_ENUM \
99 |ZEND_FFI_DCL_TYPEDEF_NAME)
100
101#define ZEND_FFI_DCL_TYPEDEF (1<<16)
102#define ZEND_FFI_DCL_EXTERN (1<<17)
103#define ZEND_FFI_DCL_STATIC (1<<18)
104#define ZEND_FFI_DCL_AUTO (1<<19)
105#define ZEND_FFI_DCL_REGISTER (1<<20)
106
107#define ZEND_FFI_DCL_STORAGE_CLASS \
108 (ZEND_FFI_DCL_TYPEDEF|ZEND_FFI_DCL_EXTERN|ZEND_FFI_DCL_STATIC \
109 |ZEND_FFI_DCL_AUTO|ZEND_FFI_DCL_REGISTER)
110
111#define ZEND_FFI_DCL_CONST (1<<21)
112#define ZEND_FFI_DCL_RESTRICT (1<<22)
113#define ZEND_FFI_DCL_VOLATILE (1<<23)
114#define ZEND_FFI_DCL_ATOMIC (1<<24)
115
116#define ZEND_FFI_DCL_TYPE_QUALIFIERS \
117 (ZEND_FFI_DCL_CONST|ZEND_FFI_DCL_RESTRICT|ZEND_FFI_DCL_VOLATILE \
118 |ZEND_FFI_DCL_ATOMIC)
119
120#define ZEND_FFI_DCL_INLINE (1<<25)
121#define ZEND_FFI_DCL_NO_RETURN (1<<26)
122
123#define ZEND_FFI_ABI_DEFAULT 0
124
125#define ZEND_FFI_ABI_CDECL 1 // FFI_DEFAULT_ABI
126#define ZEND_FFI_ABI_FASTCALL 2 // FFI_FASTCALL
127#define ZEND_FFI_ABI_THISCALL 3 // FFI_THISCALL
128#define ZEND_FFI_ABI_STDCALL 4 // FFI_STDCALL
129#define ZEND_FFI_ABI_PASCAL 5 // FFI_PASCAL
130#define ZEND_FFI_ABI_REGISTER 6 // FFI_REGISTER
131#define ZEND_FFI_ABI_MS 7 // FFI_MS_CDECL
132#define ZEND_FFI_ABI_SYSV 8 // FFI_SYSV
133#define ZEND_FFI_ABI_VECTORCALL 9 // FFI_VECTORCALL
134
135#define ZEND_FFI_ATTR_CONST (1<<0)
136#define ZEND_FFI_ATTR_INCOMPLETE_TAG (1<<1)
137#define ZEND_FFI_ATTR_VARIADIC (1<<2)
138#define ZEND_FFI_ATTR_INCOMPLETE_ARRAY (1<<3)
139#define ZEND_FFI_ATTR_VLA (1<<4)
140#define ZEND_FFI_ATTR_UNION (1<<5)
141#define ZEND_FFI_ATTR_PACKED (1<<6)
142#define ZEND_FFI_ATTR_MS_STRUCT (1<<7)
143#define ZEND_FFI_ATTR_GCC_STRUCT (1<<8)
144
145#define ZEND_FFI_ATTR_PERSISTENT (1<<9)
146#define ZEND_FFI_ATTR_STORED (1<<10)
147
148#define ZEND_FFI_STRUCT_ATTRS \
149 (ZEND_FFI_ATTR_UNION|ZEND_FFI_ATTR_PACKED|ZEND_FFI_ATTR_MS_STRUCT \
150 |ZEND_FFI_ATTR_GCC_STRUCT)
151
152#define ZEND_FFI_ENUM_ATTRS \
153 (ZEND_FFI_ATTR_PACKED)
154
155#define ZEND_FFI_ARRAY_ATTRS \
156 (ZEND_FFI_ATTR_CONST|ZEND_FFI_ATTR_VLA|ZEND_FFI_ATTR_INCOMPLETE_ARRAY)
157
158#define ZEND_FFI_FUNC_ATTRS \
159 (ZEND_FFI_ATTR_VARIADIC)
160
161#define ZEND_FFI_POINTER_ATTRS \
162 (ZEND_FFI_ATTR_CONST)
163
164typedef struct _zend_ffi_dcl {
165 uint32_t flags;
166 uint32_t align;
167 uint16_t attr;
168 uint16_t abi;
171
172#define ZEND_FFI_ATTR_INIT {0, 0, 0, 0, NULL}
173
188
189#ifdef HAVE_LONG_DOUBLE
190typedef long double zend_ffi_double;
191#else
192typedef double zend_ffi_double;
193#endif
194
195typedef struct _zend_ffi_val {
197 union {
198 uint64_t u64;
199 int64_t i64;
201 signed char ch;
202 struct {
203 const char *str;
204 size_t len;
205 };
206 };
208
209zend_result zend_ffi_parse_decl(const char *str, size_t len);
210zend_result zend_ffi_parse_type(const char *str, size_t len, zend_ffi_dcl *dcl);
212
213/* parser callbacks */
214void ZEND_NORETURN zend_ffi_parser_error(const char *msg, ...);
215bool zend_ffi_is_typedef_name(const char *name, size_t name_len);
216void zend_ffi_resolve_typedef(const char *name, size_t name_len, zend_ffi_dcl *dcl);
217void zend_ffi_resolve_const(const char *name, size_t name_len, zend_ffi_val *val);
218void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl *dcl, bool incomplete);
220void 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);
222void zend_ffi_add_field(zend_ffi_dcl *struct_dcl, const char *name, size_t name_len, zend_ffi_dcl *field_dcl);
223void zend_ffi_add_anonymous_field(zend_ffi_dcl *struct_dcl, zend_ffi_dcl *field_dcl);
224void 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);
229void zend_ffi_add_arg(HashTable **args, const char *name, size_t name_len, zend_ffi_dcl *arg_dcl);
230void zend_ffi_declare(const char *name, size_t name_len, zend_ffi_dcl *dcl);
231void zend_ffi_add_attribute(zend_ffi_dcl *dcl, const char *name, size_t name_len);
232void zend_ffi_add_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, int n, zend_ffi_val *val);
233void zend_ffi_add_msvc_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, zend_ffi_val *val);
234void zend_ffi_set_abi(zend_ffi_dcl *dcl, uint16_t abi);
239
268
269static zend_always_inline void zend_ffi_val_error(zend_ffi_val *val) /* {{{ */
270{
271 val->kind = ZEND_FFI_VAL_ERROR;
272}
273/* }}} */
274
275void zend_ffi_val_number(zend_ffi_val *val, int base, const char *str, size_t str_len);
276void zend_ffi_val_float_number(zend_ffi_val *val, const char *str, size_t str_len);
277void zend_ffi_val_string(zend_ffi_val *val, const char *str, size_t str_len);
278void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len);
279
280#endif /* PHP_FFI_H */
size_t len
Definition apprentice.c:174
#define max(a, b)
Definition exif.c:60
ffi tags
Definition ffi.c:3115
zend_long n
Definition ffi.c:4979
zend_module_entry ffi_module_entry
Definition ffi.c:5733
zend_ffi * ffi
Definition ffi.c:3027
ffi symbols
Definition ffi.c:3114
zend_ffi_dcl dcl
Definition ffi.c:4153
zval * val
Definition ffi.c:4262
ffi persistent
Definition ffi.c:3633
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
void zend_ffi_resolve_typedef(const char *name, size_t name_len, zend_ffi_dcl *dcl)
Definition ffi.c:5891
void zend_ffi_add_arg(HashTable **args, const char *name, size_t name_len, zend_ffi_dcl *arg_dcl)
Definition ffi.c:6597
void zend_ffi_expr_cast(zend_ffi_val *val, zend_ffi_dcl *dcl)
Definition ffi.c:7498
HashTable * callbacks
Definition php_ffi.h:43
bool zend_ffi_is_typedef_name(const char *name, size_t name_len)
Definition ffi.c:5872
void zend_ffi_expr_plus(zend_ffi_val *val)
Definition ffi.c:7632
void zend_ffi_expr_sizeof_val(zend_ffi_val *val)
Definition ffi.c:7683
uint32_t default_type_attr
Definition php_ffi.h:60
void zend_ffi_cleanup_dcl(zend_ffi_dcl *dcl)
Definition ffi.c:3639
void zend_ffi_align_as_val(zend_ffi_dcl *dcl, zend_ffi_val *align_val)
Definition ffi.c:7121
_zend_ffi_val_kind
Definition php_ffi.h:174
@ ZEND_FFI_VAL_FLOAT
Definition php_ffi.h:181
@ ZEND_FFI_VAL_INT32
Definition php_ffi.h:177
@ ZEND_FFI_VAL_LONG_DOUBLE
Definition php_ffi.h:183
@ ZEND_FFI_VAL_NAME
Definition php_ffi.h:186
@ ZEND_FFI_VAL_ERROR
Definition php_ffi.h:176
@ ZEND_FFI_VAL_EMPTY
Definition php_ffi.h:175
@ ZEND_FFI_VAL_UINT32
Definition php_ffi.h:179
@ ZEND_FFI_VAL_STRING
Definition php_ffi.h:185
@ ZEND_FFI_VAL_UINT64
Definition php_ffi.h:180
@ ZEND_FFI_VAL_INT64
Definition php_ffi.h:178
@ ZEND_FFI_VAL_DOUBLE
Definition php_ffi.h:182
@ ZEND_FFI_VAL_CHAR
Definition php_ffi.h:184
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)
Definition ffi.c:5975
zend_result zend_ffi_parse_decl(const char *str, size_t len)
enum _zend_ffi_val_kind zend_ffi_val_kind
void zend_ffi_expr_alignof_val(zend_ffi_val *val)
Definition ffi.c:7733
zend_result zend_ffi_parse_type(const char *str, size_t len, zend_ffi_dcl *dcl)
void zend_ffi_expr_neg(zend_ffi_val *val)
Definition ffi.c:7644
void zend_ffi_expr_alignof_type(zend_ffi_val *val, zend_ffi_dcl *dcl)
Definition ffi.c:7764
_zend_ffi_api_restriction
Definition php_ffi.h:23
@ ZEND_FFI_ENABLED
Definition php_ffi.h:25
@ ZEND_FFI_DISABLED
Definition php_ffi.h:24
@ ZEND_FFI_PRELOAD
Definition php_ffi.h:26
void zend_ffi_expr_bool_or(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7378
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)
Definition ffi.c:6249
void zend_ffi_adjust_struct_size(zend_ffi_dcl *dcl)
Definition ffi.c:6351
void zend_ffi_expr_div(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7486
void zend_ffi_expr_shift_left(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7456
void zend_ffi_expr_bw_or(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7402
int line
Definition php_ffi.h:54
void zend_ffi_expr_is_less_or_equal(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7444
void zend_ffi_make_pointer_type(zend_ffi_dcl *dcl)
Definition ffi.c:6366
HashTable types
Definition php_ffi.h:36
struct _zend_ffi_val zend_ffi_val
unsigned const char * end
Definition php_ffi.h:51
void ZEND_NORETURN zend_ffi_parser_error(const char *msg,...)
Definition ffi.c:5759
void zend_ffi_add_anonymous_field(zend_ffi_dcl *struct_dcl, zend_ffi_dcl *field_dcl)
Definition ffi.c:6181
void zend_ffi_expr_is_not_equal(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7426
void zend_ffi_expr_bw_not(zend_ffi_val *val)
Definition ffi.c:7660
bool is_cli
Definition php_ffi.h:33
void zend_ffi_declare(const char *name, size_t name_len, zend_ffi_dcl *dcl)
Definition ffi.c:6640
unsigned const char * pos
Definition php_ffi.h:52
void zend_ffi_nested_declaration(zend_ffi_dcl *dcl, zend_ffi_dcl *nested_dcl)
Definition ffi.c:7098
void zend_ffi_expr_is_greater_or_equal(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7450
void zend_ffi_expr_sizeof_type(zend_ffi_val *val, zend_ffi_dcl *dcl)
Definition ffi.c:7721
void zend_ffi_expr_mul(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7480
struct _zend_ffi_type zend_ffi_type
Definition php_ffi.h:29
void zend_ffi_add_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, int n, zend_ffi_val *val)
Definition ffi.c:6924
HashTable * weak_types
Definition php_ffi.h:46
void zend_ffi_expr_mod(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7492
void zend_ffi_expr_bool_and(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7390
void zend_ffi_expr_shift_right(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7462
struct _zend_ffi_dcl zend_ffi_dcl
char * preload
Definition php_ffi.h:39
void zend_ffi_make_struct_type(zend_ffi_dcl *dcl)
Definition ffi.c:6085
void zend_ffi_expr_is_less(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7432
unsigned const char * text
Definition php_ffi.h:53
void zend_ffi_expr_bw_xor(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7408
void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args, zend_ffi_dcl *nested_dcl)
Definition ffi.c:6459
void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len)
Definition ffi.c:7844
void zend_ffi_validate_type_name(zend_ffi_dcl *dcl)
Definition ffi.c:3741
void zend_ffi_expr_bw_and(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7414
void zend_ffi_set_abi(zend_ffi_dcl *dcl, uint16_t abi)
Definition ffi.c:6807
void zend_ffi_make_enum_type(zend_ffi_dcl *dcl)
Definition ffi.c:5955
void zend_ffi_resolve_const(const char *name, size_t name_len, zend_ffi_val *val)
Definition ffi.c:5915
void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl *dcl, bool incomplete)
Definition ffi.c:6729
void zend_ffi_val_number(zend_ffi_val *val, int base, const char *str, size_t str_len)
Definition ffi.c:7773
void zend_ffi_add_field(zend_ffi_dcl *struct_dcl, const char *name, size_t name_len, zend_ffi_dcl *field_dcl)
Definition ffi.c:6137
void zend_ffi_val_float_number(zend_ffi_val *val, const char *str, size_t str_len)
Definition ffi.c:7819
void zend_ffi_expr_is_greater(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7438
HashTable * scopes
Definition php_ffi.h:40
JMP_BUF bailout
Definition php_ffi.h:49
void zend_ffi_expr_bool_not(zend_ffi_val *val)
Definition ffi.c:7674
void zend_ffi_val_string(zend_ffi_val *val, const char *str, size_t str_len)
Definition ffi.c:7832
void zend_ffi_add_msvc_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, zend_ffi_val *val)
Definition ffi.c:7039
bool attribute_parsing
Definition php_ffi.h:58
void zend_ffi_expr_sub(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7474
zend_ffi_api_restriction restriction
Definition php_ffi.h:32
void zend_ffi_add_attribute(zend_ffi_dcl *dcl, const char *name, size_t name_len)
Definition ffi.c:6841
bool allow_vla
Definition php_ffi.h:57
void zend_ffi_make_array_type(zend_ffi_dcl *dcl, zend_ffi_val *len)
Definition ffi.c:6399
void zend_ffi_expr_conditional(zend_ffi_val *val, zend_ffi_val *op2, zend_ffi_val *op3)
Definition ffi.c:7365
enum _zend_ffi_api_restriction zend_ffi_api_restriction
double zend_ffi_double
Definition php_ffi.h:192
void zend_ffi_expr_is_equal(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7420
void zend_ffi_align_as_type(zend_ffi_dcl *dcl, zend_ffi_dcl *align_dcl)
Definition ffi.c:7114
void zend_ffi_expr_add(zend_ffi_val *val, zend_ffi_val *op2)
Definition ffi.c:7468
#define min(a, b)
char * msg
Definition phpdbg.h:289
uint16_t abi
Definition php_ffi.h:168
uint32_t align
Definition php_ffi.h:166
zend_ffi_type * type
Definition php_ffi.h:169
uint32_t flags
Definition php_ffi.h:165
uint16_t attr
Definition php_ffi.h:167
zend_ffi_val_kind kind
Definition php_ffi.h:196
signed char ch
Definition php_ffi.h:201
int64_t i64
Definition php_ffi.h:199
zend_ffi_double d
Definition php_ffi.h:200
const char * str
Definition php_ffi.h:203
uint64_t u64
Definition php_ffi.h:198
size_t len
Definition php_ffi.h:204
#define ZEND_END_MODULE_GLOBALS(module_name)
Definition zend_API.h:248
#define ZEND_EXTERN_MODULE_GLOBALS(module_name)
Definition zend_API.h:270
#define ZEND_BEGIN_MODULE_GLOBALS(module_name)
Definition zend_API.h:246
zval * args
struct _zend_module_entry zend_module_entry
int last
#define zend_always_inline
#define JMP_BUF
#define ZEND_NORETURN
struct _zend_array HashTable
Definition zend_types.h:386
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
zend_string * name
op2