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
*signature
, void *_result
, void **_args
, void *_data
);
75 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
77 typedef struct PSI_LibffiContext
{
82 typedef struct PSI_LibffiCall
{
86 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
89 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
90 return FFI_DEFAULT_ABI
;
93 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
95 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
96 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
98 for (i
= 0; i
< c
; ++i
) {
99 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
101 call
->params
[c
] = NULL
;
103 decl
->call
.info
= call
;
104 decl
->call
.rval
= &decl
->func
->ptr
;
106 decl
->call
.args
= (void **) &call
->params
[c
+1];
108 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
109 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
110 ZEND_ASSERT(FFI_OK
== rc
);
115 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
116 PSI_LibffiContext
*context
= C
->context
;
119 rc
= psi_ffi_prep_closure(&call
->closure
, &call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
120 ZEND_ASSERT(FFI_OK
== rc
);
123 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
125 psi_ffi_closure_free(call
->closure
);
130 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
136 return &ffi_type_void
;
138 return &ffi_type_sint8
;
140 return &ffi_type_uint8
;
142 return &ffi_type_sint16
;
144 return &ffi_type_uint16
;
146 return &ffi_type_sint32
;
148 return &ffi_type_uint32
;
150 return &ffi_type_sint64
;
152 return &ffi_type_uint64
;
154 return &ffi_type_uchar
;
157 return &ffi_type_sint
;
159 return &ffi_type_slong
;
161 return &ffi_type_float
;
163 return &ffi_type_double
;
164 #ifdef HAVE_LONG_DOUBLE
165 case PSI_T_LONG_DOUBLE
:
166 return &ffi_type_longdouble
;
170 return &ffi_type_pointer
;
173 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
176 return &ffi_type_sint8
;
178 return &ffi_type_sint64
;
180 return &ffi_type_pointer
;
183 return &ffi_type_double
;
184 EMPTY_SWITCH_DEFAULT_CASE();
188 static void psi_ffi_struct_type_dtor(void *type
) {
189 ffi_type
*strct
= type
;
191 if (strct
->elements
) {
194 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
197 free(strct
->elements
);
202 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
205 for (i
= 0; i
< padding
; ++i
) {
206 ffi_type
*pad
= malloc(sizeof(*pad
));
208 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
215 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
216 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
217 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
219 for (i
= 0; i
< strct
->args
->count
; ++i
) {
220 decl_arg
*darg
= strct
->args
->args
[i
];
221 ffi_type
*type
= malloc(sizeof(*type
));
224 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
226 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
228 if (type
->alignment
> maxalign
) {
229 maxalign
= type
->alignment
;
232 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
233 if (nels
+ padding
+ 1 > argc
) {
235 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
238 psi_ffi_struct_type_pad(&els
[nels
], padding
);
242 ZEND_ASSERT(offset
== darg
->layout
->pos
);
244 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
248 /* apply struct alignment padding */
249 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
251 ZEND_ASSERT(offset
<= strct
->size
);
252 if (offset
< strct
->size
) {
253 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
258 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
259 decl_type
*real
= real_decl_type(type
);
261 switch (real
->type
) {
263 if (!real
->func
->call
.sym
) {
264 PSI_LibffiCall
*call
= PSI_LibffiCallAlloc(&PSI_G(context
), real
->func
);
267 rc
= psi_ffi_prep_closure(&real
->func
->call
.closure
.data
, &real
->func
->call
.sym
,
268 &call
->signature
, psi_ffi_handler
, NULL
);
270 real
->func
->call
.info
= call
;
271 real
->func
->call
.closure
.dtor
= psi_ffi_closure_free
;
274 return &ffi_type_pointer
;
277 if (!real
->strct
->engine
.type
) {
278 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
280 strct
->type
= FFI_TYPE_STRUCT
;
282 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
284 real
->strct
->engine
.type
= strct
;
285 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
288 return real
->strct
->engine
.type
;
291 return psi_ffi_decl_arg_type(real
->unn
->args
->args
[0]);
294 return psi_ffi_token_type(real
->type
);
297 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
298 if (darg
->var
->pointer_level
) {
299 return &ffi_type_pointer
;
301 return psi_ffi_decl_type(darg
->type
);
306 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
310 L
= malloc(sizeof(*L
));
312 memset(L
, 0, sizeof(*L
));
314 L
->params
[0] = &ffi_type_pointer
;
315 L
->params
[1] = &ffi_type_pointer
;
316 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
317 ZEND_ASSERT(rc
== FFI_OK
);
322 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
324 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
327 static void psi_ffi_init(PSI_Context
*C
)
329 C
->context
= PSI_LibffiContextInit(NULL
);
332 static void psi_ffi_dtor(PSI_Context
*C
)
337 for (i
= 0; i
< C
->decls
->count
; ++i
) {
338 decl
*decl
= C
->decls
->list
[i
];
340 if (decl
->call
.info
) {
341 PSI_LibffiCallFree(decl
->call
.info
);
348 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
351 zend_function_entry
*zfe
;
357 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
358 for (i
= 0; i
< C
->impls
->count
; ++i
) {
359 zend_function_entry
*zf
= &zfe
[j
];
360 PSI_LibffiCall
*call
;
361 impl
*impl
= C
->impls
->list
[i
];
367 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
368 PSI_LibffiCallInitClosure(C
, call
, impl
);
370 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
371 zf
->num_args
= impl
->func
->args
->count
;
372 zf
->handler
= call
->code
;
373 zf
->arg_info
= psi_internal_arginfo(impl
);
377 for (i
= 0; i
< C
->decls
->count
; ++i
) {
378 decl
*decl
= C
->decls
->list
[i
];
384 PSI_LibffiCallAlloc(C
, decl
);
390 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
391 PSI_LibffiCall
*call
= decl_call
->info
;
396 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
397 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
399 for (i
= 0; i
< nfixedargs
; ++i
) {
400 params
[i
] = call
->params
[i
];
401 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
403 for (i
= 0; i
< va
->args
->count
; ++i
) {
404 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
405 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
407 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
408 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
409 nfixedargs
, ntotalargs
,
410 call
->signature
.rtype
, (ffi_type
**) params
);
412 /* FIXME: test in config.m4; assume we can just call anyway */
413 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
414 call
->signature
.rtype
, (ffi_type
**) params
);
416 ZEND_ASSERT(FFI_OK
== rc
);
417 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
420 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
424 static PSI_ContextOps ops
= {
431 PSI_ContextOps
*PSI_Libffi(void)
436 #endif /* HAVE_LIBFFI */