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
) {
45 # error "Neither ffi_closure_alloc() nor mmap() available"
49 static ffi_status
psi_ffi_prep_closure(ffi_closure
**closure
, void **code
, ffi_cif
*sig
, void (*handler
)(ffi_cif
*,void*,void**,void*), void *data
) {
50 *closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), code
);
51 ZEND_ASSERT(*closure
!= NULL
);
53 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
54 return ffi_prep_closure_loc(*closure
, sig
, handler
, data
, *code
);
56 #elif PSI_HAVE_FFI_PREP_CLOSURE
57 return ffi_prep_closure(*code
, sig
, handler
, data
);
59 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() is available"
64 static void psi_ffi_closure_free(void *c
)
66 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
69 munmap(c
, sizeof(ffi_closure
));
73 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
75 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
78 static void psi_ffi_callback(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
80 psi_callback(_data
, _result
, _sig
->nargs
, _args
);
83 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
85 struct psi_ffi_context
{
94 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
97 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
98 return FFI_DEFAULT_ABI
;
101 static inline struct psi_ffi_call
*psi_ffi_call_alloc(struct psi_context
*C
, decl
*decl
) {
103 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
104 struct psi_ffi_call
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
106 for (i
= 0; i
< c
; ++i
) {
107 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
109 call
->params
[c
] = NULL
;
111 decl
->call
.info
= call
;
112 decl
->call
.rval
= &decl
->func
->ptr
;
114 decl
->call
.args
= (void **) &call
->params
[c
+1];
116 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
117 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
118 ZEND_ASSERT(FFI_OK
== rc
);
123 static inline ffi_status
psi_ffi_call_init_closure(struct psi_context
*C
, struct psi_ffi_call
*call
, impl
*impl
) {
124 struct psi_ffi_context
*context
= C
->context
;
126 return psi_ffi_prep_closure(&call
->closure
, &call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
129 static inline ffi_status
psi_ffi_call_init_callback_closure(struct psi_context
*C
, struct psi_ffi_call
*call
, let_callback
*cb
) {
130 return psi_ffi_prep_closure(&call
->closure
, &call
->code
, &call
->signature
, psi_ffi_callback
, cb
);
133 static inline void psi_ffi_call_free(struct psi_ffi_call
*call
) {
135 psi_ffi_closure_free(call
->closure
);
140 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
146 return &ffi_type_void
;
148 return &ffi_type_sint8
;
150 return &ffi_type_uint8
;
152 return &ffi_type_sint16
;
154 return &ffi_type_uint16
;
156 return &ffi_type_sint32
;
158 return &ffi_type_uint32
;
160 return &ffi_type_sint64
;
162 return &ffi_type_uint64
;
164 return &ffi_type_uchar
;
167 return &ffi_type_sint
;
169 return &ffi_type_slong
;
171 return &ffi_type_float
;
173 return &ffi_type_double
;
174 #ifdef HAVE_LONG_DOUBLE
175 case PSI_T_LONG_DOUBLE
:
176 return &ffi_type_longdouble
;
180 return &ffi_type_pointer
;
183 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
186 return &ffi_type_sint8
;
188 return &ffi_type_sint64
;
190 return &ffi_type_pointer
;
193 return &ffi_type_double
;
194 EMPTY_SWITCH_DEFAULT_CASE();
198 static void psi_ffi_struct_type_dtor(void *type
) {
199 ffi_type
*strct
= type
;
201 if (strct
->elements
) {
204 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
207 free(strct
->elements
);
212 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
215 for (i
= 0; i
< padding
; ++i
) {
216 ffi_type
*pad
= malloc(sizeof(*pad
));
218 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
225 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
226 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
227 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
229 for (i
= 0; i
< strct
->args
->count
; ++i
) {
230 decl_arg
*darg
= strct
->args
->args
[i
];
231 ffi_type
*type
= malloc(sizeof(*type
));
234 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
236 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
238 if (type
->alignment
> maxalign
) {
239 maxalign
= type
->alignment
;
242 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
243 if (nels
+ padding
+ 1 > argc
) {
245 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
248 psi_ffi_struct_type_pad(&els
[nels
], padding
);
252 ZEND_ASSERT(offset
== darg
->layout
->pos
);
254 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
258 /* apply struct alignment padding */
259 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
261 ZEND_ASSERT(offset
<= strct
->size
);
262 if (offset
< strct
->size
) {
263 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
268 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
269 decl_type
*real
= real_decl_type(type
);
271 switch (real
->type
) {
273 if (!real
->real
.strct
->engine
.type
) {
274 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
276 strct
->type
= FFI_TYPE_STRUCT
;
278 strct
->elements
= psi_ffi_struct_type_elements(real
->real
.strct
);
280 real
->real
.strct
->engine
.type
= strct
;
281 real
->real
.strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
284 return real
->real
.strct
->engine
.type
;
287 return psi_ffi_decl_arg_type(real
->real
.unn
->args
->args
[0]);
290 return psi_ffi_token_type(real
->type
);
293 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
294 if (darg
->var
->pointer_level
) {
295 return &ffi_type_pointer
;
297 return psi_ffi_decl_type(darg
->type
);
302 static inline struct psi_ffi_context
*psi_ffi_context_init(struct psi_ffi_context
*L
) {
306 L
= malloc(sizeof(*L
));
308 memset(L
, 0, sizeof(*L
));
310 L
->params
[0] = &ffi_type_pointer
;
311 L
->params
[1] = &ffi_type_pointer
;
312 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
313 ZEND_ASSERT(rc
== FFI_OK
);
318 static inline void psi_ffi_context_free(struct psi_ffi_context
**L
) {
325 static void psi_ffi_init(struct psi_context
*C
)
327 C
->context
= psi_ffi_context_init(NULL
);
330 static inline void psi_ffi_destroy_callbacks(struct psi_context
*C
, let_val
*let_val
) {
334 switch (let_val
->kind
) {
335 case PSI_LET_CALLBACK
:
336 cb
= let_val
->data
.callback
;
338 if (cb
->decl
&& cb
->decl
->call
.info
) {
339 psi_ffi_call_free(cb
->decl
->call
.info
);
345 fn
= let_val
->data
.func
;
351 for (i
= 0; i
< fn
->inner
->count
; ++i
) {
352 psi_ffi_destroy_callbacks(C
, fn
->inner
->vals
[i
]);
361 static void psi_ffi_dtor(struct psi_context
*C
)
366 for (i
= 0; i
< C
->decls
->count
; ++i
) {
367 decl
*decl
= C
->decls
->list
[i
];
369 if (decl
->call
.info
) {
370 psi_ffi_call_free(decl
->call
.info
);
378 for (i
= 0; i
< C
->impls
->count
; ++i
) {
379 impl
*impl
= C
->impls
->list
[i
];
381 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
382 let_stmt
*let
= impl
->stmts
->let
.list
[j
];
384 if (let
->val
&& let
->val
->kind
== PSI_LET_CALLBACK
) {
385 let_callback
*cb
= let
->val
->data
.callback
;
387 if (cb
->decl
&& cb
->decl
->call
.info
) {
388 psi_ffi_call_free(cb
->decl
->call
.info
);
394 psi_ffi_context_free((void *) &C
->context
);
397 static inline void psi_ffi_compile_callbacks(struct psi_context
*C
, let_val
*let_val
) {
398 struct psi_ffi_call
*call
;
402 switch (let_val
->kind
) {
403 case PSI_LET_CALLBACK
:
404 cb
= let_val
->data
.callback
;
405 if ((call
= psi_ffi_call_alloc(C
, cb
->decl
))) {
406 if (FFI_OK
!= psi_ffi_call_init_callback_closure(C
, call
, cb
)) {
407 psi_ffi_call_free(call
);
411 cb
->decl
->call
.sym
= call
->code
;
417 fn
= let_val
->data
.func
;
422 for (i
= 0; i
< fn
->inner
->count
; ++i
) {
423 psi_ffi_compile_callbacks(C
, fn
->inner
->vals
[i
]);
432 static zend_function_entry
*psi_ffi_compile(struct psi_context
*C
)
435 zend_function_entry
*zfe
;
441 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
442 for (i
= 0; i
< C
->impls
->count
; ++i
) {
443 zend_function_entry
*zf
= &zfe
[j
];
444 struct psi_ffi_call
*call
;
445 impl
*impl
= C
->impls
->list
[i
];
451 if ((call
= psi_ffi_call_alloc(C
, impl
->decl
))) {
452 if (FFI_OK
!= psi_ffi_call_init_closure(C
, call
, impl
)) {
453 psi_ffi_call_free(call
);
458 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
459 zf
->num_args
= impl
->func
->args
->count
;
460 zf
->handler
= call
->code
;
461 zf
->arg_info
= psi_internal_arginfo(impl
);
464 for (c
= 0; c
< impl
->stmts
->let
.count
; ++c
) {
465 psi_ffi_compile_callbacks(C
, impl
->stmts
->let
.list
[c
]->val
);
469 for (i
= 0; i
< C
->decls
->count
; ++i
) {
470 decl
*decl
= C
->decls
->list
[i
];
472 if (decl
->call
.info
) {
476 psi_ffi_call_alloc(C
, decl
);
482 static void psi_ffi_call(struct psi_context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
483 struct psi_ffi_call
*call
= decl_call
->info
;
488 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
489 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
491 for (i
= 0; i
< nfixedargs
; ++i
) {
492 params
[i
] = call
->params
[i
];
493 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
495 for (i
= 0; i
< va
->args
->count
; ++i
) {
496 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
497 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
499 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
500 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
501 nfixedargs
, ntotalargs
,
502 call
->signature
.rtype
, (ffi_type
**) params
);
504 /* FIXME: test in config.m4; assume we can just call anyway */
505 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
506 call
->signature
.rtype
, (ffi_type
**) params
);
508 ZEND_ASSERT(FFI_OK
== rc
);
509 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
512 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
516 static struct psi_context_ops ops
= {
523 struct psi_context_ops
*psi_libffi_ops(void)
528 #endif /* HAVE_LIBFFI */