13 #undef PACKAGE_BUGREPORT
16 #undef PACKAGE_TARNAME
17 #undef PACKAGE_VERSION
21 #ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
26 # include <sys/mman.h>
27 # ifndef MAP_ANONYMOUS
28 # define MAP_ANONYMOUS MAP_ANON
33 static void *psi_ffi_closure_alloc(size_t s
, void **code
)
35 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
36 return ffi_closure_alloc(s
, code
);
38 *code
= mmap(NULL
, s
, PROT_EXEC
|PROT_WRITE
|PROT_READ
,
39 MAP_PRIVATE
|MAP_ANONYMOUS
, -1, 0);
40 if (MAP_FAILED
== *code
) {
49 static void psi_ffi_closure_free(void *c
)
51 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
54 munmap(c
, sizeof(ffi_closure
));
58 static void psi_ffi_handler(ffi_cif
*signature
, void *_result
, void **_args
, void *_data
);
59 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
61 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
62 return FFI_DEFAULT_ABI
;
64 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
70 return &ffi_type_void
;
72 return &ffi_type_sint8
;
74 return &ffi_type_uint8
;
76 return &ffi_type_sint16
;
78 return &ffi_type_uint16
;
80 return &ffi_type_sint32
;
82 return &ffi_type_uint32
;
84 return &ffi_type_sint64
;
86 return &ffi_type_uint64
;
88 return &ffi_type_uchar
;
90 return &ffi_type_sint
;
92 return &ffi_type_slong
;
94 return &ffi_type_float
;
96 return &ffi_type_double
;
97 #ifdef HAVE_LONG_DOUBLE
98 case PSI_T_LONG_DOUBLE
:
99 return &ffi_type_longdouble
;
102 return &ffi_type_pointer
;
105 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
108 return &ffi_type_sint8
;
110 return &ffi_type_sint64
;
112 return &ffi_type_pointer
;
115 return &ffi_type_double
;
116 EMPTY_SWITCH_DEFAULT_CASE();
120 static void psi_ffi_struct_type_dtor(void *type
) {
121 ffi_type
*strct
= type
;
123 if (strct
->elements
) {
126 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
129 free(strct
->elements
);
134 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
137 for (i
= 0; i
< padding
; ++i
) {
138 ffi_type
*pad
= malloc(sizeof(*pad
));
140 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
147 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
148 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
149 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
151 for (i
= 0; i
< strct
->args
->count
; ++i
) {
152 decl_arg
*darg
= strct
->args
->args
[i
];
153 ffi_type
*type
= malloc(sizeof(*type
));
156 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
158 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
160 if (type
->alignment
> maxalign
) {
161 maxalign
= type
->alignment
;
164 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
165 if (nels
+ padding
+ 1 > argc
) {
167 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
170 psi_ffi_struct_type_pad(&els
[nels
], padding
);
174 ZEND_ASSERT(offset
== darg
->layout
->pos
);
176 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
180 /* apply struct alignment padding */
181 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
183 ZEND_ASSERT(offset
<= strct
->size
);
184 if (offset
< strct
->size
) {
185 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
190 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
191 decl_type
*real
= real_decl_type(type
);
193 if (real
->type
== PSI_T_STRUCT
) {
194 if (!real
->strct
->engine
.type
) {
195 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
197 strct
->type
= FFI_TYPE_STRUCT
;
199 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
201 real
->strct
->engine
.type
= strct
;
202 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
205 return real
->strct
->engine
.type
;
207 return psi_ffi_token_type(real
->type
);
209 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
210 if (darg
->var
->pointer_level
) {
211 return &ffi_type_pointer
;
213 return psi_ffi_decl_type(darg
->type
);
217 typedef struct PSI_LibffiContext
{
222 typedef struct PSI_LibffiCall
{
224 ffi_closure
*closure
;
226 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
229 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
231 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
232 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
234 for (i
= 0; i
< c
; ++i
) {
235 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
237 call
->params
[c
] = NULL
;
239 decl
->call
.info
= call
;
240 decl
->call
.rval
= &decl
->func
->ptr
;
242 decl
->call
.args
= (void **) &call
->params
[c
+1];
244 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
245 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
246 ZEND_ASSERT(FFI_OK
== rc
);
251 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
252 PSI_LibffiContext
*context
= C
->context
;
255 call
->closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), &call
->code
);
256 ZEND_ASSERT(call
->closure
!= NULL
);
258 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
259 rc
= ffi_prep_closure_loc(
266 #elif PSI_HAVE_FFI_PREP_CLOSURE
267 rc
= ffi_prep_closure(call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
269 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
271 ZEND_ASSERT(FFI_OK
== rc
);
274 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
276 psi_ffi_closure_free(call
->closure
);
281 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
285 L
= malloc(sizeof(*L
));
287 memset(L
, 0, sizeof(*L
));
289 L
->params
[0] = &ffi_type_pointer
;
290 L
->params
[1] = &ffi_type_pointer
;
291 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
292 ZEND_ASSERT(rc
== FFI_OK
);
297 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
299 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
302 static void psi_ffi_init(PSI_Context
*C
)
304 C
->context
= PSI_LibffiContextInit(NULL
);
307 static void psi_ffi_dtor(PSI_Context
*C
)
312 for (i
= 0; i
< C
->decls
->count
; ++i
) {
313 decl
*decl
= C
->decls
->list
[i
];
315 if (decl
->call
.info
) {
316 PSI_LibffiCallFree(decl
->call
.info
);
323 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
326 zend_function_entry
*zfe
;
332 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
333 for (i
= 0; i
< C
->impls
->count
; ++i
) {
334 zend_function_entry
*zf
= &zfe
[j
];
335 PSI_LibffiCall
*call
;
336 impl
*impl
= C
->impls
->list
[i
];
342 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
343 PSI_LibffiCallInitClosure(C
, call
, impl
);
345 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
346 zf
->num_args
= impl
->func
->args
->count
;
347 zf
->handler
= call
->code
;
348 zf
->arg_info
= psi_internal_arginfo(impl
);
352 for (i
= 0; i
< C
->decls
->count
; ++i
) {
353 decl
*decl
= C
->decls
->list
[i
];
359 PSI_LibffiCallAlloc(C
, decl
);
365 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
366 PSI_LibffiCall
*call
= decl_call
->info
;
371 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
372 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
374 for (i
= 0; i
< nfixedargs
; ++i
) {
375 params
[i
] = call
->params
[i
];
376 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
378 for (i
= 0; i
< va
->args
->count
; ++i
) {
379 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
380 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
382 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
383 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
384 nfixedargs
, ntotalargs
,
385 call
->signature
.rtype
, (ffi_type
**) params
);
387 /* FIXME: test in config.m4; assume we can just call anyway */
388 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
389 call
->signature
.rtype
, (ffi_type
**) params
);
391 ZEND_ASSERT(FFI_OK
== rc
);
392 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
395 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
399 static PSI_ContextOps ops
= {
406 PSI_ContextOps
*PSI_Libffi(void)
411 #endif /* HAVE_LIBFFI */