19afda09baa803e8edef148f8742cc5265905194
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 psi_callback(_data
, _result
, _sig
->nargs
, _args
);
84 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
86 typedef struct PSI_LibffiContext
{
91 typedef struct PSI_LibffiCall
{
95 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
98 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
99 return FFI_DEFAULT_ABI
;
102 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
104 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
105 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
107 for (i
= 0; i
< c
; ++i
) {
108 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
110 call
->params
[c
] = NULL
;
112 decl
->call
.info
= call
;
113 decl
->call
.rval
= &decl
->func
->ptr
;
115 decl
->call
.args
= (void **) &call
->params
[c
+1];
117 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
118 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
119 ZEND_ASSERT(FFI_OK
== rc
);
124 static inline ffi_status
PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
125 PSI_LibffiContext
*context
= C
->context
;
127 return psi_ffi_prep_closure(&call
->closure
, &call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
130 static inline ffi_status
PSI_LibffiCallInitCallbackClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, let_callback
*cb
) {
131 return psi_ffi_prep_closure(&call
->closure
, &call
->code
, &call
->signature
, psi_ffi_callback
, cb
);
134 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
136 psi_ffi_closure_free(call
->closure
);
141 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
147 return &ffi_type_void
;
149 return &ffi_type_sint8
;
151 return &ffi_type_uint8
;
153 return &ffi_type_sint16
;
155 return &ffi_type_uint16
;
157 return &ffi_type_sint32
;
159 return &ffi_type_uint32
;
161 return &ffi_type_sint64
;
163 return &ffi_type_uint64
;
165 return &ffi_type_uchar
;
168 return &ffi_type_sint
;
170 return &ffi_type_slong
;
172 return &ffi_type_float
;
174 return &ffi_type_double
;
175 #ifdef HAVE_LONG_DOUBLE
176 case PSI_T_LONG_DOUBLE
:
177 return &ffi_type_longdouble
;
181 return &ffi_type_pointer
;
184 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
187 return &ffi_type_sint8
;
189 return &ffi_type_sint64
;
191 return &ffi_type_pointer
;
194 return &ffi_type_double
;
195 EMPTY_SWITCH_DEFAULT_CASE();
199 static void psi_ffi_struct_type_dtor(void *type
) {
200 ffi_type
*strct
= type
;
202 if (strct
->elements
) {
205 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
208 free(strct
->elements
);
213 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
216 for (i
= 0; i
< padding
; ++i
) {
217 ffi_type
*pad
= malloc(sizeof(*pad
));
219 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
226 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
227 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
228 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
230 for (i
= 0; i
< strct
->args
->count
; ++i
) {
231 decl_arg
*darg
= strct
->args
->args
[i
];
232 ffi_type
*type
= malloc(sizeof(*type
));
235 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
237 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
239 if (type
->alignment
> maxalign
) {
240 maxalign
= type
->alignment
;
243 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
244 if (nels
+ padding
+ 1 > argc
) {
246 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
249 psi_ffi_struct_type_pad(&els
[nels
], padding
);
253 ZEND_ASSERT(offset
== darg
->layout
->pos
);
255 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
259 /* apply struct alignment padding */
260 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
262 ZEND_ASSERT(offset
<= strct
->size
);
263 if (offset
< strct
->size
) {
264 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
269 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
270 decl_type
*real
= real_decl_type(type
);
272 switch (real
->type
) {
274 if (!real
->strct
->engine
.type
) {
275 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
277 strct
->type
= FFI_TYPE_STRUCT
;
279 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
281 real
->strct
->engine
.type
= strct
;
282 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
285 return real
->strct
->engine
.type
;
288 return psi_ffi_decl_arg_type(real
->unn
->args
->args
[0]);
291 return psi_ffi_token_type(real
->type
);
294 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
295 if (darg
->var
->pointer_level
) {
296 return &ffi_type_pointer
;
298 return psi_ffi_decl_type(darg
->type
);
303 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
307 L
= malloc(sizeof(*L
));
309 memset(L
, 0, sizeof(*L
));
311 L
->params
[0] = &ffi_type_pointer
;
312 L
->params
[1] = &ffi_type_pointer
;
313 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
314 ZEND_ASSERT(rc
== FFI_OK
);
319 static inline void PSI_LibffiContextFree(PSI_LibffiContext
**L
) {
326 static void psi_ffi_init(PSI_Context
*C
)
328 C
->context
= PSI_LibffiContextInit(NULL
);
331 static void psi_ffi_dtor(PSI_Context
*C
)
336 for (i
= 0; i
< C
->decls
->count
; ++i
) {
337 decl
*decl
= C
->decls
->list
[i
];
339 if (decl
->call
.info
) {
340 PSI_LibffiCallFree(decl
->call
.info
);
348 for (i
= 0; i
< C
->impls
->count
; ++i
) {
349 impl
*impl
= C
->impls
->list
[i
];
351 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
352 let_stmt
*let
= impl
->stmts
->let
.list
[j
];
354 if (let
->val
&& let
->val
->kind
== PSI_LET_CALLBACK
) {
355 let_callback
*cb
= let
->val
->data
.callback
;
357 if (cb
->decl
&& cb
->decl
->call
.info
) {
358 PSI_LibffiCallFree(cb
->decl
->call
.info
);
364 PSI_LibffiContextFree((void *) &C
->context
);
367 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
370 zend_function_entry
*zfe
;
376 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
377 for (i
= 0; i
< C
->impls
->count
; ++i
) {
378 zend_function_entry
*zf
= &zfe
[j
];
379 PSI_LibffiCall
*call
;
380 impl
*impl
= C
->impls
->list
[i
];
386 if ((call
= PSI_LibffiCallAlloc(C
, impl
->decl
))) {
387 if (FFI_OK
!= PSI_LibffiCallInitClosure(C
, call
, impl
)) {
388 PSI_LibffiCallFree(call
);
393 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
394 zf
->num_args
= impl
->func
->args
->count
;
395 zf
->handler
= call
->code
;
396 zf
->arg_info
= psi_internal_arginfo(impl
);
399 for (c
= 0; c
< impl
->stmts
->let
.count
; ++c
) {
400 let_stmt
*let
= impl
->stmts
->let
.list
[c
];
402 if (let
->val
&& let
->val
->kind
== PSI_LET_CALLBACK
) {
403 let_callback
*cb
= let
->val
->data
.callback
;
405 if ((call
= PSI_LibffiCallAlloc(C
, cb
->decl
))) {
406 if (FFI_OK
!= PSI_LibffiCallInitCallbackClosure(C
, call
, cb
)) {
407 PSI_LibffiCallFree(call
);
411 cb
->decl
->call
.sym
= call
->code
;
417 for (i
= 0; i
< C
->decls
->count
; ++i
) {
418 decl
*decl
= C
->decls
->list
[i
];
420 if (decl
->call
.info
) {
424 PSI_LibffiCallAlloc(C
, decl
);
430 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
431 PSI_LibffiCall
*call
= decl_call
->info
;
436 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
437 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
439 for (i
= 0; i
< nfixedargs
; ++i
) {
440 params
[i
] = call
->params
[i
];
441 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
443 for (i
= 0; i
< va
->args
->count
; ++i
) {
444 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
445 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
447 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
448 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
449 nfixedargs
, ntotalargs
,
450 call
->signature
.rtype
, (ffi_type
**) params
);
452 /* FIXME: test in config.m4; assume we can just call anyway */
453 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
454 call
->signature
.rtype
, (ffi_type
**) params
);
456 ZEND_ASSERT(FFI_OK
== rc
);
457 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
460 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
464 static PSI_ContextOps ops
= {
471 PSI_ContextOps
*PSI_Libffi(void)
476 #endif /* HAVE_LIBFFI */