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
;
91 return &ffi_type_sint
;
93 return &ffi_type_slong
;
95 return &ffi_type_float
;
97 return &ffi_type_double
;
98 #ifdef HAVE_LONG_DOUBLE
99 case PSI_T_LONG_DOUBLE
:
100 return &ffi_type_longdouble
;
103 return &ffi_type_pointer
;
106 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
109 return &ffi_type_sint8
;
111 return &ffi_type_sint64
;
113 return &ffi_type_pointer
;
116 return &ffi_type_double
;
117 EMPTY_SWITCH_DEFAULT_CASE();
121 static void psi_ffi_struct_type_dtor(void *type
) {
122 ffi_type
*strct
= type
;
124 if (strct
->elements
) {
127 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
130 free(strct
->elements
);
135 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
138 for (i
= 0; i
< padding
; ++i
) {
139 ffi_type
*pad
= malloc(sizeof(*pad
));
141 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
148 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
149 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
150 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
152 for (i
= 0; i
< strct
->args
->count
; ++i
) {
153 decl_arg
*darg
= strct
->args
->args
[i
];
154 ffi_type
*type
= malloc(sizeof(*type
));
157 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
159 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
161 if (type
->alignment
> maxalign
) {
162 maxalign
= type
->alignment
;
165 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
166 if (nels
+ padding
+ 1 > argc
) {
168 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
171 psi_ffi_struct_type_pad(&els
[nels
], padding
);
175 ZEND_ASSERT(offset
== darg
->layout
->pos
);
177 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
181 /* apply struct alignment padding */
182 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
184 ZEND_ASSERT(offset
<= strct
->size
);
185 if (offset
< strct
->size
) {
186 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
191 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
192 decl_type
*real
= real_decl_type(type
);
194 if (real
->type
== PSI_T_STRUCT
) {
195 if (!real
->strct
->engine
.type
) {
196 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
198 strct
->type
= FFI_TYPE_STRUCT
;
200 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
202 real
->strct
->engine
.type
= strct
;
203 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
206 return real
->strct
->engine
.type
;
208 return psi_ffi_token_type(real
->type
);
210 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
211 if (darg
->var
->pointer_level
) {
212 return &ffi_type_pointer
;
214 return psi_ffi_decl_type(darg
->type
);
218 typedef struct PSI_LibffiContext
{
223 typedef struct PSI_LibffiCall
{
225 ffi_closure
*closure
;
227 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
230 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
232 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
233 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
235 for (i
= 0; i
< c
; ++i
) {
236 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
238 call
->params
[c
] = NULL
;
240 decl
->call
.info
= call
;
241 decl
->call
.rval
= &decl
->func
->ptr
;
243 decl
->call
.args
= (void **) &call
->params
[c
+1];
245 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
246 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
247 ZEND_ASSERT(FFI_OK
== rc
);
252 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
253 PSI_LibffiContext
*context
= C
->context
;
256 call
->closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), &call
->code
);
257 ZEND_ASSERT(call
->closure
!= NULL
);
259 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
260 rc
= ffi_prep_closure_loc(
267 #elif PSI_HAVE_FFI_PREP_CLOSURE
268 rc
= ffi_prep_closure(call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
270 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
272 ZEND_ASSERT(FFI_OK
== rc
);
275 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
277 psi_ffi_closure_free(call
->closure
);
282 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
286 L
= malloc(sizeof(*L
));
288 memset(L
, 0, sizeof(*L
));
290 L
->params
[0] = &ffi_type_pointer
;
291 L
->params
[1] = &ffi_type_pointer
;
292 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
293 ZEND_ASSERT(rc
== FFI_OK
);
298 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
300 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
303 static void psi_ffi_init(PSI_Context
*C
)
305 C
->context
= PSI_LibffiContextInit(NULL
);
308 static void psi_ffi_dtor(PSI_Context
*C
)
313 for (i
= 0; i
< C
->decls
->count
; ++i
) {
314 decl
*decl
= C
->decls
->list
[i
];
316 if (decl
->call
.info
) {
317 PSI_LibffiCallFree(decl
->call
.info
);
324 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
327 zend_function_entry
*zfe
;
333 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
334 for (i
= 0; i
< C
->impls
->count
; ++i
) {
335 zend_function_entry
*zf
= &zfe
[j
];
336 PSI_LibffiCall
*call
;
337 impl
*impl
= C
->impls
->list
[i
];
343 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
344 PSI_LibffiCallInitClosure(C
, call
, impl
);
346 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
347 zf
->num_args
= impl
->func
->args
->count
;
348 zf
->handler
= call
->code
;
349 zf
->arg_info
= psi_internal_arginfo(impl
);
353 for (i
= 0; i
< C
->decls
->count
; ++i
) {
354 decl
*decl
= C
->decls
->list
[i
];
360 PSI_LibffiCallAlloc(C
, decl
);
366 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
367 PSI_LibffiCall
*call
= decl_call
->info
;
372 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
373 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
375 for (i
= 0; i
< nfixedargs
; ++i
) {
376 params
[i
] = call
->params
[i
];
377 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
379 for (i
= 0; i
< va
->args
->count
; ++i
) {
380 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
381 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
383 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
384 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
385 nfixedargs
, ntotalargs
,
386 call
->signature
.rtype
, (ffi_type
**) params
);
388 /* FIXME: test in config.m4; assume we can just call anyway */
389 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
390 call
->signature
.rtype
, (ffi_type
**) params
);
392 ZEND_ASSERT(FFI_OK
== rc
);
393 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
396 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
400 static PSI_ContextOps ops
= {
407 PSI_ContextOps
*PSI_Libffi(void)
412 #endif /* HAVE_LIBFFI */