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
) {
50 static void psi_ffi_closure_free(void *c
)
52 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
55 munmap(c
, sizeof(ffi_closure
));
59 static void psi_ffi_handler(ffi_cif
*signature
, void *_result
, void **_args
, void *_data
);
60 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
62 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
63 return FFI_DEFAULT_ABI
;
65 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
71 return &ffi_type_void
;
73 return &ffi_type_sint8
;
75 return &ffi_type_uint8
;
77 return &ffi_type_sint16
;
79 return &ffi_type_uint16
;
81 return &ffi_type_sint32
;
83 return &ffi_type_uint32
;
85 return &ffi_type_sint64
;
87 return &ffi_type_uint64
;
89 return &ffi_type_uchar
;
92 return &ffi_type_sint
;
94 return &ffi_type_slong
;
96 return &ffi_type_float
;
98 return &ffi_type_double
;
99 #ifdef HAVE_LONG_DOUBLE
100 case PSI_T_LONG_DOUBLE
:
101 return &ffi_type_longdouble
;
104 return &ffi_type_pointer
;
107 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
110 return &ffi_type_sint8
;
112 return &ffi_type_sint64
;
114 return &ffi_type_pointer
;
117 return &ffi_type_double
;
118 EMPTY_SWITCH_DEFAULT_CASE();
122 static void psi_ffi_struct_type_dtor(void *type
) {
123 ffi_type
*strct
= type
;
125 if (strct
->elements
) {
128 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
131 free(strct
->elements
);
136 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
139 for (i
= 0; i
< padding
; ++i
) {
140 ffi_type
*pad
= malloc(sizeof(*pad
));
142 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
149 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
150 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
151 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
153 for (i
= 0; i
< strct
->args
->count
; ++i
) {
154 decl_arg
*darg
= strct
->args
->args
[i
];
155 ffi_type
*type
= malloc(sizeof(*type
));
158 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
160 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
162 if (type
->alignment
> maxalign
) {
163 maxalign
= type
->alignment
;
166 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
167 if (nels
+ padding
+ 1 > argc
) {
169 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
172 psi_ffi_struct_type_pad(&els
[nels
], padding
);
176 ZEND_ASSERT(offset
== darg
->layout
->pos
);
178 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
182 /* apply struct alignment padding */
183 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
185 ZEND_ASSERT(offset
<= strct
->size
);
186 if (offset
< strct
->size
) {
187 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
192 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
193 decl_type
*real
= real_decl_type(type
);
195 switch (real
->type
) {
197 if (!real
->strct
->engine
.type
) {
198 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
200 strct
->type
= FFI_TYPE_STRUCT
;
202 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
204 real
->strct
->engine
.type
= strct
;
205 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
208 return real
->strct
->engine
.type
;
211 return psi_ffi_decl_arg_type(real
->unn
->args
->args
[0]);
214 return psi_ffi_token_type(real
->type
);
217 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
218 if (darg
->var
->pointer_level
) {
219 return &ffi_type_pointer
;
221 return psi_ffi_decl_type(darg
->type
);
225 typedef struct PSI_LibffiContext
{
230 typedef struct PSI_LibffiCall
{
232 ffi_closure
*closure
;
234 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
237 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
239 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
240 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
242 for (i
= 0; i
< c
; ++i
) {
243 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
245 call
->params
[c
] = NULL
;
247 decl
->call
.info
= call
;
248 decl
->call
.rval
= &decl
->func
->ptr
;
250 decl
->call
.args
= (void **) &call
->params
[c
+1];
252 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
253 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
254 ZEND_ASSERT(FFI_OK
== rc
);
259 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
260 PSI_LibffiContext
*context
= C
->context
;
263 call
->closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), &call
->code
);
264 ZEND_ASSERT(call
->closure
!= NULL
);
266 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
267 rc
= ffi_prep_closure_loc(
274 #elif PSI_HAVE_FFI_PREP_CLOSURE
275 rc
= ffi_prep_closure(call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
277 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
279 ZEND_ASSERT(FFI_OK
== rc
);
282 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
284 psi_ffi_closure_free(call
->closure
);
289 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
293 L
= malloc(sizeof(*L
));
295 memset(L
, 0, sizeof(*L
));
297 L
->params
[0] = &ffi_type_pointer
;
298 L
->params
[1] = &ffi_type_pointer
;
299 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
300 ZEND_ASSERT(rc
== FFI_OK
);
305 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
307 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
310 static void psi_ffi_init(PSI_Context
*C
)
312 C
->context
= PSI_LibffiContextInit(NULL
);
315 static void psi_ffi_dtor(PSI_Context
*C
)
320 for (i
= 0; i
< C
->decls
->count
; ++i
) {
321 decl
*decl
= C
->decls
->list
[i
];
323 if (decl
->call
.info
) {
324 PSI_LibffiCallFree(decl
->call
.info
);
331 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
334 zend_function_entry
*zfe
;
340 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
341 for (i
= 0; i
< C
->impls
->count
; ++i
) {
342 zend_function_entry
*zf
= &zfe
[j
];
343 PSI_LibffiCall
*call
;
344 impl
*impl
= C
->impls
->list
[i
];
350 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
351 PSI_LibffiCallInitClosure(C
, call
, impl
);
353 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
354 zf
->num_args
= impl
->func
->args
->count
;
355 zf
->handler
= call
->code
;
356 zf
->arg_info
= psi_internal_arginfo(impl
);
360 for (i
= 0; i
< C
->decls
->count
; ++i
) {
361 decl
*decl
= C
->decls
->list
[i
];
367 PSI_LibffiCallAlloc(C
, decl
);
373 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
374 PSI_LibffiCall
*call
= decl_call
->info
;
379 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
380 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
382 for (i
= 0; i
< nfixedargs
; ++i
) {
383 params
[i
] = call
->params
[i
];
384 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
386 for (i
= 0; i
< va
->args
->count
; ++i
) {
387 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
388 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
390 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
391 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
392 nfixedargs
, ntotalargs
,
393 call
->signature
.rtype
, (ffi_type
**) params
);
395 /* FIXME: test in config.m4; assume we can just call anyway */
396 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
397 call
->signature
.rtype
, (ffi_type
**) params
);
399 ZEND_ASSERT(FFI_OK
== rc
);
400 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
403 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
407 static PSI_ContextOps ops
= {
414 PSI_ContextOps
*PSI_Libffi(void)
419 #endif /* HAVE_LIBFFI */