Skip to content

Commit 1a21480

Browse files
committed
Initial JIT/FFI PoC
1 parent 6c9d443 commit 1a21480

File tree

9 files changed

+563
-105
lines changed

9 files changed

+563
-105
lines changed

ext/ffi/ffi.c

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -74,76 +74,8 @@ typedef struct _zend_ffi_tag {
7474
zend_ffi_type *type;
7575
} zend_ffi_tag;
7676

77-
typedef enum _zend_ffi_type_kind {
78-
ZEND_FFI_TYPE_VOID,
79-
ZEND_FFI_TYPE_FLOAT,
80-
ZEND_FFI_TYPE_DOUBLE,
81-
#ifdef HAVE_LONG_DOUBLE
82-
ZEND_FFI_TYPE_LONGDOUBLE,
83-
#endif
84-
ZEND_FFI_TYPE_UINT8,
85-
ZEND_FFI_TYPE_SINT8,
86-
ZEND_FFI_TYPE_UINT16,
87-
ZEND_FFI_TYPE_SINT16,
88-
ZEND_FFI_TYPE_UINT32,
89-
ZEND_FFI_TYPE_SINT32,
90-
ZEND_FFI_TYPE_UINT64,
91-
ZEND_FFI_TYPE_SINT64,
92-
ZEND_FFI_TYPE_ENUM,
93-
ZEND_FFI_TYPE_BOOL,
94-
ZEND_FFI_TYPE_CHAR,
95-
ZEND_FFI_TYPE_POINTER,
96-
ZEND_FFI_TYPE_FUNC,
97-
ZEND_FFI_TYPE_ARRAY,
98-
ZEND_FFI_TYPE_STRUCT,
99-
} zend_ffi_type_kind;
100-
10177
#include "ffi_arginfo.h"
10278

103-
typedef enum _zend_ffi_flags {
104-
ZEND_FFI_FLAG_CONST = (1 << 0),
105-
ZEND_FFI_FLAG_OWNED = (1 << 1),
106-
ZEND_FFI_FLAG_PERSISTENT = (1 << 2),
107-
} zend_ffi_flags;
108-
109-
struct _zend_ffi_type {
110-
zend_ffi_type_kind kind;
111-
size_t size;
112-
uint32_t align;
113-
uint32_t attr;
114-
union {
115-
struct {
116-
zend_string *tag_name;
117-
zend_ffi_type_kind kind;
118-
} enumeration;
119-
struct {
120-
zend_ffi_type *type;
121-
zend_long length;
122-
} array;
123-
struct {
124-
zend_ffi_type *type;
125-
} pointer;
126-
struct {
127-
zend_string *tag_name;
128-
HashTable fields;
129-
} record;
130-
struct {
131-
zend_ffi_type *ret_type;
132-
HashTable *args;
133-
ffi_abi abi;
134-
} func;
135-
};
136-
};
137-
138-
typedef struct _zend_ffi_field {
139-
size_t offset;
140-
bool is_const;
141-
bool is_nested; /* part of nested anonymous struct */
142-
uint8_t first_bit;
143-
uint8_t bits;
144-
zend_ffi_type *type;
145-
} zend_ffi_field;
146-
14779
typedef enum _zend_ffi_symbol_kind {
14880
ZEND_FFI_SYM_TYPE,
14981
ZEND_FFI_SYM_CONST,
@@ -174,39 +106,19 @@ typedef struct _zend_ffi {
174106
bool persistent;
175107
} zend_ffi;
176108

177-
#define ZEND_FFI_TYPE_OWNED (1<<0)
178-
179-
#define ZEND_FFI_TYPE(t) \
180-
((zend_ffi_type*)(((uintptr_t)(t)) & ~ZEND_FFI_TYPE_OWNED))
181-
182-
#define ZEND_FFI_TYPE_IS_OWNED(t) \
183-
(((uintptr_t)(t)) & ZEND_FFI_TYPE_OWNED)
184-
185109
#define ZEND_FFI_TYPE_MAKE_OWNED(t) \
186110
((zend_ffi_type*)(((uintptr_t)(t)) | ZEND_FFI_TYPE_OWNED))
187111

188112
#define ZEND_FFI_SIZEOF_ARG \
189113
MAX(FFI_SIZEOF_ARG, sizeof(double))
190114

191-
typedef struct _zend_ffi_cdata {
192-
zend_object std;
193-
zend_ffi_type *type;
194-
void *ptr;
195-
void *ptr_holder;
196-
zend_ffi_flags flags;
197-
} zend_ffi_cdata;
198-
199-
typedef struct _zend_ffi_ctype {
200-
zend_object std;
201-
zend_ffi_type *type;
202-
} zend_ffi_ctype;
203-
204115
static zend_class_entry *zend_ffi_exception_ce;
205116
static zend_class_entry *zend_ffi_parser_exception_ce;
206117
static zend_class_entry *zend_ffi_ce;
207-
static zend_class_entry *zend_ffi_cdata_ce;
208118
static zend_class_entry *zend_ffi_ctype_ce;
209119

120+
ZEND_API zend_class_entry *zend_ffi_cdata_ce;
121+
210122
static zend_object_handlers zend_ffi_handlers;
211123
static zend_object_handlers zend_ffi_cdata_handlers;
212124
static zend_object_handlers zend_ffi_cdata_value_handlers;

ext/ffi/ffi.stub.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ final class FFI
1111

1212
public static function cdef(string $code = "", ?string $lib = null): FFI {}
1313

14-
public static function load(string $filename): ?FFI {}
14+
public static function load(string $filename): FFI {}
1515

1616
public static function scope(string $name): FFI {}
1717

18-
public static function new(FFI\CType|string $type, bool $owned = true, bool $persistent = false): ?FFI\CData {}
18+
public static function new(FFI\CType|string $type, bool $owned = true, bool $persistent = false): FFI\CData {}
1919

2020
/** @prefer-ref $ptr */
2121
public static function free(FFI\CData $ptr): void {}
@@ -24,9 +24,9 @@ public static function free(FFI\CData $ptr): void {}
2424
* @param FFI\CData|int|float|bool|null $ptr
2525
* @prefer-ref $ptr
2626
*/
27-
public static function cast(FFI\CType|string $type, $ptr): ?FFI\CData {}
27+
public static function cast(FFI\CType|string $type, $ptr): FFI\CData {}
2828

29-
public static function type(string $type): ?FFI\CType {}
29+
public static function type(string $type): FFI\CType {}
3030

3131
/** @prefer-ref $ptr */
3232
public static function typeof(FFI\CData $ptr): FFI\CType {}

ext/ffi/ffi_arginfo.h

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/ffi/php_ffi.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,98 @@ void zend_ffi_val_float_number(zend_ffi_val *val, const char *str, size_t str_le
276276
void zend_ffi_val_string(zend_ffi_val *val, const char *str, size_t str_len);
277277
void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len);
278278

279+
280+
/* FFI internals exported for JIT */
281+
282+
typedef enum _zend_ffi_type_kind {
283+
ZEND_FFI_TYPE_VOID,
284+
ZEND_FFI_TYPE_FLOAT,
285+
ZEND_FFI_TYPE_DOUBLE,
286+
#ifdef HAVE_LONG_DOUBLE
287+
ZEND_FFI_TYPE_LONGDOUBLE,
288+
#endif
289+
ZEND_FFI_TYPE_UINT8,
290+
ZEND_FFI_TYPE_SINT8,
291+
ZEND_FFI_TYPE_UINT16,
292+
ZEND_FFI_TYPE_SINT16,
293+
ZEND_FFI_TYPE_UINT32,
294+
ZEND_FFI_TYPE_SINT32,
295+
ZEND_FFI_TYPE_UINT64,
296+
ZEND_FFI_TYPE_SINT64,
297+
ZEND_FFI_TYPE_ENUM,
298+
ZEND_FFI_TYPE_BOOL,
299+
ZEND_FFI_TYPE_CHAR,
300+
ZEND_FFI_TYPE_POINTER,
301+
ZEND_FFI_TYPE_FUNC,
302+
ZEND_FFI_TYPE_ARRAY,
303+
ZEND_FFI_TYPE_STRUCT,
304+
} zend_ffi_type_kind;
305+
306+
typedef enum _zend_ffi_flags {
307+
ZEND_FFI_FLAG_CONST = (1 << 0),
308+
ZEND_FFI_FLAG_OWNED = (1 << 1),
309+
ZEND_FFI_FLAG_PERSISTENT = (1 << 2),
310+
} zend_ffi_flags;
311+
312+
struct _zend_ffi_type {
313+
zend_ffi_type_kind kind;
314+
size_t size;
315+
uint32_t align;
316+
uint32_t attr;
317+
union {
318+
struct {
319+
zend_string *tag_name;
320+
zend_ffi_type_kind kind;
321+
} enumeration;
322+
struct {
323+
zend_ffi_type *type;
324+
zend_long length;
325+
} array;
326+
struct {
327+
zend_ffi_type *type;
328+
} pointer;
329+
struct {
330+
zend_string *tag_name;
331+
HashTable fields;
332+
} record;
333+
struct {
334+
zend_ffi_type *ret_type;
335+
HashTable *args;
336+
int abi; /*ffi_abi*/
337+
} func;
338+
};
339+
};
340+
341+
typedef struct _zend_ffi_field {
342+
size_t offset;
343+
bool is_const;
344+
bool is_nested; /* part of nested anonymous struct */
345+
uint8_t first_bit;
346+
uint8_t bits;
347+
zend_ffi_type *type;
348+
} zend_ffi_field;
349+
350+
typedef struct _zend_ffi_cdata {
351+
zend_object std;
352+
zend_ffi_type *type;
353+
void *ptr;
354+
void *ptr_holder;
355+
zend_ffi_flags flags;
356+
} zend_ffi_cdata;
357+
358+
typedef struct _zend_ffi_ctype {
359+
zend_object std;
360+
zend_ffi_type *type;
361+
} zend_ffi_ctype;
362+
363+
extern ZEND_API zend_class_entry *zend_ffi_cdata_ce;
364+
365+
#define ZEND_FFI_TYPE_OWNED (1<<0)
366+
367+
#define ZEND_FFI_TYPE(t) \
368+
((zend_ffi_type*)(((uintptr_t)(t)) & ~ZEND_FFI_TYPE_OWNED))
369+
370+
#define ZEND_FFI_TYPE_IS_OWNED(t) \
371+
(((uintptr_t)(t)) & ZEND_FFI_TYPE_OWNED)
372+
279373
#endif /* PHP_FFI_H */

ext/opcache/jit/zend_jit.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242

4343
#include "jit/zend_jit_internal.h"
4444

45+
#if HAVE_FFI
46+
# include "ext/ffi/php_ffi.h"
47+
#endif
48+
4549
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
4650
#include <pthread.h>
4751
#endif

ext/opcache/jit/zend_jit_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ typedef enum _zend_jit_trace_op {
345345
ZEND_JIT_TRACE_VM,
346346
ZEND_JIT_TRACE_OP1_TYPE,
347347
ZEND_JIT_TRACE_OP2_TYPE,
348+
ZEND_JIT_TRACE_OP1_FFI_TYPE,
349+
ZEND_JIT_TRACE_OP2_FFI_TYPE,
348350
ZEND_JIT_TRACE_VAL_INFO,
349351
ZEND_JIT_TRACE_INIT_CALL,
350352
ZEND_JIT_TRACE_DO_ICALL,

0 commit comments

Comments
 (0)