14 #undef PACKAGE_BUGREPORT
17 #undef PACKAGE_TARNAME
18 #undef PACKAGE_VERSION
22 #ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
27 # include <sys/mman.h>
28 # ifndef MAP_ANONYMOUS
29 # define MAP_ANONYMOUS MAP_ANON
34 static void *psi_ffi_closure_alloc(size_t s
, void **code
)
36 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
37 return ffi_closure_alloc(s
, code
);
39 *code
= mmap(NULL
, s
, PROT_EXEC
|PROT_WRITE
|PROT_READ
,
40 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
41 if (MAP_FAILED
== *code
) {
46 # error "Neither ffi_closure_alloc() nor mmap() available"
50 static ffi_status
psi_ffi_prep_closure(ffi_closure
**closure
, void **code
, ffi_cif
*sig
, void (*handler
)(ffi_cif
*,void*,void**,void*), void *data
) {
51 *closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), code
);
52 ZEND_ASSERT(*closure
!= NULL
);
54 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
55 return ffi_prep_closure_loc(*closure
, sig
, handler
, data
, *code
);
57 #elif PSI_HAVE_FFI_PREP_CLOSURE
58 return ffi_prep_closure(*code
, sig
, handler
, data
);
60 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() is available"
65 static void psi_ffi_closure_free(void *c
)
67 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
70 munmap(c
, sizeof(ffi_closure
));
74 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
76 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
79 static void psi_ffi_callback(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
81 unsigned argc
= _sig
->nargs
;
83 ffi_arg
*res
= _result
;
85 decl_arg
*darg
= let
->var
->arg
;
86 decl
*decl_cb
= darg
->type
->func
;
87 let_callback
*cb
= let
->val
->data
.callback
;
88 impl_arg
*iarg
= cb
->func
->arg
;
89 size_t i
, argc
= cb
->args
->count
;
90 zval return_value
, *argv
= calloc(argc
, sizeof(*argv
));
92 // prepare args for the userland call
93 for (i
= 0; i
< decl_cb
->args
->count
; ++i
) {
96 for (i
= 0; i
< cb
->args
->count
; ++i
) {
97 psi_do_set(&argv
[i
], cb
->args
->vals
[i
]);
99 zend_fcall_info_argp(iarg
->val
.zend
.cb
->fci
, argc
, argv
);
100 zend_fcall_info_call(&iarg
->val
.zend
.cb
->fci
, &iarg
->val
.zend
.cb
->fcc
,
101 &return_value
, NULL
);
102 // marshal return value of the userland call
103 switch (cb
->func
->type
) {
121 EMPTY_SWITCH_DEFAULT_CASE();
123 darg
->ptr
= psi_let_val(cb
->func
->type
, iarg
, darg
->ptr
, real_decl_type(darg
->type
)->strct
, &darg
->mem
);
126 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
128 typedef struct PSI_LibffiContext
{
133 typedef struct PSI_LibffiCall
{
135 ffi_closure
*closure
;
137 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
140 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
141 return FFI_DEFAULT_ABI
;
144 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
146 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
147 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
149 for (i
= 0; i
< c
; ++i
) {
150 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
152 call
->params
[c
] = NULL
;
154 decl
->call
.info
= call
;
155 decl
->call
.rval
= &decl
->func
->ptr
;
157 decl
->call
.args
= (void **) &call
->params
[c
+1];
159 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
160 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
161 ZEND_ASSERT(FFI_OK
== rc
);
166 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
167 PSI_LibffiContext
*context
= C
->context
;
170 rc
= psi_ffi_prep_closure(&call
->closure
, &call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
171 ZEND_ASSERT(FFI_OK
== rc
);
174 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
176 psi_ffi_closure_free(call
->closure
);
181 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
187 return &ffi_type_void
;
189 return &ffi_type_sint8
;
191 return &ffi_type_uint8
;
193 return &ffi_type_sint16
;
195 return &ffi_type_uint16
;
197 return &ffi_type_sint32
;
199 return &ffi_type_uint32
;
201 return &ffi_type_sint64
;
203 return &ffi_type_uint64
;
205 return &ffi_type_uchar
;
208 return &ffi_type_sint
;
210 return &ffi_type_slong
;
212 return &ffi_type_float
;
214 return &ffi_type_double
;
215 #ifdef HAVE_LONG_DOUBLE
216 case PSI_T_LONG_DOUBLE
:
217 return &ffi_type_longdouble
;
221 return &ffi_type_pointer
;
224 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
227 return &ffi_type_sint8
;
229 return &ffi_type_sint64
;
231 return &ffi_type_pointer
;
234 return &ffi_type_double
;
235 EMPTY_SWITCH_DEFAULT_CASE();
239 static void psi_ffi_struct_type_dtor(void *type
) {
240 ffi_type
*strct
= type
;
242 if (strct
->elements
) {
245 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
248 free(strct
->elements
);
253 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
256 for (i
= 0; i
< padding
; ++i
) {
257 ffi_type
*pad
= malloc(sizeof(*pad
));
259 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
266 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
267 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
268 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
270 for (i
= 0; i
< strct
->args
->count
; ++i
) {
271 decl_arg
*darg
= strct
->args
->args
[i
];
272 ffi_type
*type
= malloc(sizeof(*type
));
275 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
277 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
279 if (type
->alignment
> maxalign
) {
280 maxalign
= type
->alignment
;
283 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
284 if (nels
+ padding
+ 1 > argc
) {
286 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
289 psi_ffi_struct_type_pad(&els
[nels
], padding
);
293 ZEND_ASSERT(offset
== darg
->layout
->pos
);
295 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
299 /* apply struct alignment padding */
300 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
302 ZEND_ASSERT(offset
<= strct
->size
);
303 if (offset
< strct
->size
) {
304 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
309 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
310 decl_type
*real
= real_decl_type(type
);
312 switch (real
->type
) {
314 if (!real
->func
->call
.sym
) {
315 PSI_LibffiCall
*call
= PSI_LibffiCallAlloc(&PSI_G(context
), real
->func
);
318 rc
= psi_ffi_prep_closure(
319 (void *) &real
->func
->call
.closure
.data
,
320 &real
->func
->call
.sym
, &call
->signature
, psi_ffi_handler
, NULL
);
322 real
->func
->call
.info
= call
;
323 real
->func
->call
.closure
.dtor
= psi_ffi_closure_free
;
326 return &ffi_type_pointer
;
329 if (!real
->strct
->engine
.type
) {
330 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
332 strct
->type
= FFI_TYPE_STRUCT
;
334 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
336 real
->strct
->engine
.type
= strct
;
337 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
340 return real
->strct
->engine
.type
;
343 return psi_ffi_decl_arg_type(real
->unn
->args
->args
[0]);
346 return psi_ffi_token_type(real
->type
);
349 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
350 if (darg
->var
->pointer_level
) {
351 return &ffi_type_pointer
;
353 return psi_ffi_decl_type(darg
->type
);
358 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
362 L
= malloc(sizeof(*L
));
364 memset(L
, 0, sizeof(*L
));
366 L
->params
[0] = &ffi_type_pointer
;
367 L
->params
[1] = &ffi_type_pointer
;
368 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
369 ZEND_ASSERT(rc
== FFI_OK
);
374 static void psi_ffi_init(PSI_Context
*C
)
376 C
->context
= PSI_LibffiContextInit(NULL
);
379 static void psi_ffi_dtor(PSI_Context
*C
)
384 for (i
= 0; i
< C
->decls
->count
; ++i
) {
385 decl
*decl
= C
->decls
->list
[i
];
387 if (decl
->call
.info
) {
388 PSI_LibffiCallFree(decl
->call
.info
);
395 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
398 zend_function_entry
*zfe
;
404 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
405 for (i
= 0; i
< C
->impls
->count
; ++i
) {
406 zend_function_entry
*zf
= &zfe
[j
];
407 PSI_LibffiCall
*call
;
408 impl
*impl
= C
->impls
->list
[i
];
414 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
415 PSI_LibffiCallInitClosure(C
, call
, impl
);
417 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
418 zf
->num_args
= impl
->func
->args
->count
;
419 zf
->handler
= call
->code
;
420 zf
->arg_info
= psi_internal_arginfo(impl
);
424 for (i
= 0; i
< C
->decls
->count
; ++i
) {
425 decl
*decl
= C
->decls
->list
[i
];
431 PSI_LibffiCallAlloc(C
, decl
);
437 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
438 PSI_LibffiCall
*call
= decl_call
->info
;
443 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
444 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
446 for (i
= 0; i
< nfixedargs
; ++i
) {
447 params
[i
] = call
->params
[i
];
448 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
450 for (i
= 0; i
< va
->args
->count
; ++i
) {
451 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
452 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
454 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
455 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
456 nfixedargs
, ntotalargs
,
457 call
->signature
.rtype
, (ffi_type
**) params
);
459 /* FIXME: test in config.m4; assume we can just call anyway */
460 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
461 call
->signature
.rtype
, (ffi_type
**) params
);
463 ZEND_ASSERT(FFI_OK
== rc
);
464 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
467 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
471 static PSI_ContextOps ops
= {
478 PSI_ContextOps
*PSI_Libffi(void)
483 #endif /* HAVE_LIBFFI */