13 #undef PACKAGE_BUGREPORT
16 #undef PACKAGE_TARNAME
17 #undef PACKAGE_VERSION
21 #ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
26 # include <sys/mman.h>
27 # ifndef MAP_ANONYMOUS
28 # define MAP_ANONYMOUS MAP_ANON
33 static void *psi_ffi_closure_alloc(size_t s
, void **code
)
35 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
36 return ffi_closure_alloc(s
, code
);
38 *code
= mmap(NULL
, s
, PROT_EXEC
|PROT_WRITE
|PROT_READ
,
39 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
40 if (MAP_FAILED
== *code
) {
49 static void psi_ffi_closure_free(void *c
)
51 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
54 munmap(c
, sizeof(ffi_closure
));
58 static void psi_ffi_handler(ffi_cif
*signature
, void *_result
, void **_args
, void *_data
);
60 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
61 return FFI_DEFAULT_ABI
;
63 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
69 return &ffi_type_void
;
71 return &ffi_type_sint8
;
73 return &ffi_type_uint8
;
75 return &ffi_type_sint16
;
77 return &ffi_type_uint16
;
79 return &ffi_type_sint32
;
81 return &ffi_type_uint32
;
83 return &ffi_type_sint64
;
85 return &ffi_type_uint64
;
87 return &ffi_type_uchar
;
89 return &ffi_type_sint
;
91 return &ffi_type_float
;
93 return &ffi_type_double
;
96 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
97 return psi_ffi_token_type(real_decl_type(type
)->type
);
99 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
100 if (darg
->var
->pointer_level
) {
101 return &ffi_type_pointer
;
103 return psi_ffi_decl_type(darg
->type
);
107 typedef struct PSI_LibffiContext
{
112 typedef struct PSI_LibffiCall
{
114 ffi_closure
*closure
;
116 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
119 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
121 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
122 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
124 for (i
= 0; i
< c
; ++i
) {
125 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
127 call
->params
[c
] = NULL
;
129 decl
->call
.info
= call
;
130 decl
->call
.args
= (void **) &call
->params
[c
+1];
132 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
133 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
134 ZEND_ASSERT(FFI_OK
== rc
);
139 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
140 PSI_LibffiContext
*context
= C
->context
;
143 call
->closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), &call
->code
);
144 ZEND_ASSERT(call
->closure
!= NULL
);
146 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
147 rc
= ffi_prep_closure_loc(
154 #elif PSI_HAVE_FFI_PREP_CLOSURE
155 rc
= ffi_prep_closure(call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
157 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
159 ZEND_ASSERT(FFI_OK
== rc
);
162 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
164 psi_ffi_closure_free(call
->closure
);
169 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
173 L
= malloc(sizeof(*L
));
175 memset(L
, 0, sizeof(*L
));
177 L
->params
[0] = &ffi_type_pointer
;
178 L
->params
[1] = &ffi_type_pointer
;
179 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
180 ZEND_ASSERT(rc
== FFI_OK
);
185 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
187 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
190 static void psi_ffi_init(PSI_Context
*C
)
192 C
->context
= PSI_LibffiContextInit(NULL
);
195 static void psi_ffi_dtor(PSI_Context
*C
)
200 for (i
= 0; i
< C
->decls
->count
; ++i
) {
201 decl
*decl
= C
->decls
->list
[i
];
203 if (decl
->call
.info
) {
204 PSI_LibffiCallFree(decl
->call
.info
);
211 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
214 zend_function_entry
*zfe
;
220 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
221 for (i
= 0; i
< C
->impls
->count
; ++i
) {
222 zend_function_entry
*zf
= &zfe
[j
];
223 PSI_LibffiCall
*call
;
224 impl
*impl
= C
->impls
->list
[i
];
230 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
231 PSI_LibffiCallInitClosure(C
, call
, impl
);
233 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
234 zf
->num_args
= impl
->func
->args
->count
;
235 zf
->handler
= call
->code
;
236 zf
->arg_info
= psi_internal_arginfo(impl
);
240 for (i
= 0; i
< C
->decls
->count
; ++i
) {
241 decl
*decl
= C
->decls
->list
[i
];
247 PSI_LibffiCallAlloc(C
, decl
);
253 static void psi_ffi_call(PSI_Context
*C
, impl_val
*ret_val
, decl
*decl
) {
254 PSI_LibffiCall
*call
= decl
->call
.info
;
256 ffi_call(&call
->signature
, FFI_FN(decl
->call
.sym
), ret_val
, decl
->call
.args
);
259 static PSI_ContextOps ops
= {
266 PSI_ContextOps
*PSI_Libffi(void)
271 #endif /* HAVE_LIBFFI */