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
)
82 unsigned argc
= _sig
->nargs
;
84 let_callback
*cb
= _data
;
85 decl
*decl_cb
= cb
->decl
;
86 impl_arg
*iarg
= cb
->func
->var
->arg
;
87 zval return_value
, *zargv
= calloc(argc
, sizeof(*zargv
));
88 void *result
, *to_free
= NULL
;
90 ZEND_ASSERT(argc
== cb
->decl
->args
->count
);
92 /* prepare args for the userland call */
93 for (i
= 0; i
< argc
; ++i
) {
94 cb
->decl
->args
->args
[i
]->ptr
= argv
[i
];
96 for (i
= 0; i
< cb
->args
->count
; ++i
) {
97 psi_do_set(&zargv
[i
], cb
->args
->vals
[i
]);
99 zend_fcall_info_argp(&iarg
->val
.zend
.cb
->fci
, cb
->args
->count
, zargv
);
101 /* callback into userland */
102 ZVAL_UNDEF(&return_value
);
103 iarg
->_zv
= &return_value
;
104 zend_fcall_info_call(&iarg
->val
.zend
.cb
->fci
, &iarg
->val
.zend
.cb
->fcc
, iarg
->_zv
, NULL
);
106 /* marshal return value of the userland call */
107 switch (iarg
->type
->type
) {
108 case PSI_T_BOOL
: zend_parse_arg_bool(iarg
->_zv
, &iarg
->val
.zend
.bval
, NULL
, 0); break;
109 case PSI_T_LONG
: zend_parse_arg_long(iarg
->_zv
, &iarg
->val
.zend
.lval
, NULL
, 0, 1); break;
111 case PSI_T_DOUBLE
: zend_parse_arg_double(iarg
->_zv
, &iarg
->val
.dval
, NULL
, 0); break;
112 case PSI_T_STRING
: zend_parse_arg_str(iarg
->_zv
, &iarg
->val
.zend
.str
, 0); break;
114 result
= cb
->func
->handler(_result
, decl_cb
->func
->type
, iarg
, &to_free
);
116 if (result
!= _result
) {
117 *(void **)_result
= result
;
121 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
123 typedef struct PSI_LibffiContext
{
128 typedef struct PSI_LibffiCall
{
130 ffi_closure
*closure
;
132 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
135 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
136 return FFI_DEFAULT_ABI
;
139 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
141 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
142 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
144 for (i
= 0; i
< c
; ++i
) {
145 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
147 call
->params
[c
] = NULL
;
149 decl
->call
.info
= call
;
150 decl
->call
.rval
= &decl
->func
->ptr
;
152 decl
->call
.args
= (void **) &call
->params
[c
+1];
154 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
155 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
156 ZEND_ASSERT(FFI_OK
== rc
);
161 static inline ffi_status
PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
162 PSI_LibffiContext
*context
= C
->context
;
164 return psi_ffi_prep_closure(&call
->closure
, &call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
167 static inline ffi_status
PSI_LibffiCallInitCallbackClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, let_callback
*cb
) {
168 return psi_ffi_prep_closure(&call
->closure
, &call
->code
, &call
->signature
, psi_ffi_callback
, cb
);
171 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
173 psi_ffi_closure_free(call
->closure
);
178 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
184 return &ffi_type_void
;
186 return &ffi_type_sint8
;
188 return &ffi_type_uint8
;
190 return &ffi_type_sint16
;
192 return &ffi_type_uint16
;
194 return &ffi_type_sint32
;
196 return &ffi_type_uint32
;
198 return &ffi_type_sint64
;
200 return &ffi_type_uint64
;
202 return &ffi_type_uchar
;
205 return &ffi_type_sint
;
207 return &ffi_type_slong
;
209 return &ffi_type_float
;
211 return &ffi_type_double
;
212 #ifdef HAVE_LONG_DOUBLE
213 case PSI_T_LONG_DOUBLE
:
214 return &ffi_type_longdouble
;
218 return &ffi_type_pointer
;
221 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
224 return &ffi_type_sint8
;
226 return &ffi_type_sint64
;
228 return &ffi_type_pointer
;
231 return &ffi_type_double
;
232 EMPTY_SWITCH_DEFAULT_CASE();
236 static void psi_ffi_struct_type_dtor(void *type
) {
237 ffi_type
*strct
= type
;
239 if (strct
->elements
) {
242 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
245 free(strct
->elements
);
250 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
253 for (i
= 0; i
< padding
; ++i
) {
254 ffi_type
*pad
= malloc(sizeof(*pad
));
256 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
263 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
264 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
265 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
267 for (i
= 0; i
< strct
->args
->count
; ++i
) {
268 decl_arg
*darg
= strct
->args
->args
[i
];
269 ffi_type
*type
= malloc(sizeof(*type
));
272 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
274 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
276 if (type
->alignment
> maxalign
) {
277 maxalign
= type
->alignment
;
280 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
281 if (nels
+ padding
+ 1 > argc
) {
283 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
286 psi_ffi_struct_type_pad(&els
[nels
], padding
);
290 ZEND_ASSERT(offset
== darg
->layout
->pos
);
292 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
296 /* apply struct alignment padding */
297 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
299 ZEND_ASSERT(offset
<= strct
->size
);
300 if (offset
< strct
->size
) {
301 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
306 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
307 decl_type
*real
= real_decl_type(type
);
309 switch (real
->type
) {
311 return &ffi_type_pointer
;
314 if (!real
->strct
->engine
.type
) {
315 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
317 strct
->type
= FFI_TYPE_STRUCT
;
319 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
321 real
->strct
->engine
.type
= strct
;
322 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
325 return real
->strct
->engine
.type
;
328 return psi_ffi_decl_arg_type(real
->unn
->args
->args
[0]);
331 return psi_ffi_token_type(real
->type
);
334 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
335 if (darg
->var
->pointer_level
) {
336 return &ffi_type_pointer
;
338 return psi_ffi_decl_type(darg
->type
);
343 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
347 L
= malloc(sizeof(*L
));
349 memset(L
, 0, sizeof(*L
));
351 L
->params
[0] = &ffi_type_pointer
;
352 L
->params
[1] = &ffi_type_pointer
;
353 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
354 ZEND_ASSERT(rc
== FFI_OK
);
359 static void psi_ffi_init(PSI_Context
*C
)
361 C
->context
= PSI_LibffiContextInit(NULL
);
364 static void psi_ffi_dtor(PSI_Context
*C
)
369 for (i
= 0; i
< C
->decls
->count
; ++i
) {
370 decl
*decl
= C
->decls
->list
[i
];
372 if (decl
->call
.info
) {
373 PSI_LibffiCallFree(decl
->call
.info
);
380 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
383 zend_function_entry
*zfe
;
389 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
390 for (i
= 0; i
< C
->impls
->count
; ++i
) {
391 zend_function_entry
*zf
= &zfe
[j
];
392 PSI_LibffiCall
*call
;
393 impl
*impl
= C
->impls
->list
[i
];
399 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
400 if (FFI_OK
!= PSI_LibffiCallInitClosure(C
, call
, impl
)) {
401 PSI_LibffiCallFree(call
);
405 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
406 zf
->num_args
= impl
->func
->args
->count
;
407 zf
->handler
= call
->code
;
408 zf
->arg_info
= psi_internal_arginfo(impl
);
411 for (c
= 0; c
< impl
->stmts
->let
.count
; ++c
) {
412 let_stmt
*let
= impl
->stmts
->let
.list
[c
];
414 if (let
->val
->kind
== PSI_LET_CALLBACK
) {
415 let_callback
*cb
= let
->val
->data
.callback
;
417 call
= PSI_LibffiCallAlloc(C
, cb
->decl
);
418 if (FFI_OK
!= PSI_LibffiCallInitCallbackClosure(C
, call
, cb
)) {
419 PSI_LibffiCallFree(call
);
423 cb
->decl
->call
.sym
= call
->code
;
428 for (i
= 0; i
< C
->decls
->count
; ++i
) {
429 decl
*decl
= C
->decls
->list
[i
];
434 if (decl
->call
.info
) {
438 PSI_LibffiCallAlloc(C
, decl
);
444 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
445 PSI_LibffiCall
*call
= decl_call
->info
;
450 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
451 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
453 for (i
= 0; i
< nfixedargs
; ++i
) {
454 params
[i
] = call
->params
[i
];
455 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
457 for (i
= 0; i
< va
->args
->count
; ++i
) {
458 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
459 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
461 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
462 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
463 nfixedargs
, ntotalargs
,
464 call
->signature
.rtype
, (ffi_type
**) params
);
466 /* FIXME: test in config.m4; assume we can just call anyway */
467 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
468 call
->signature
.rtype
, (ffi_type
**) params
);
470 ZEND_ASSERT(FFI_OK
== rc
);
471 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
474 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
478 static PSI_ContextOps ops
= {
485 PSI_ContextOps
*PSI_Libffi(void)
490 #endif /* HAVE_LIBFFI */