5962b5e7431aedaff1d362aed1b5bae373712e72
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
;
98 return &ffi_type_pointer
;
101 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
104 return &ffi_type_sint8
;
106 return &ffi_type_sint64
;
108 return &ffi_type_pointer
;
111 return &ffi_type_double
;
112 EMPTY_SWITCH_DEFAULT_CASE();
116 static void psi_ffi_struct_type_dtor(void *type
) {
117 ffi_type
*strct
= type
;
119 if (strct
->elements
) {
122 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
125 free(strct
->elements
);
130 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
133 for (i
= 0; i
< padding
; ++i
) {
134 ffi_type
*pad
= malloc(sizeof(*pad
));
136 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
143 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
144 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
145 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
147 for (i
= 0; i
< strct
->args
->count
; ++i
) {
148 decl_arg
*darg
= strct
->args
->args
[i
];
149 ffi_type
*type
= malloc(sizeof(*type
));
152 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
154 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
156 if (type
->alignment
> maxalign
) {
157 maxalign
= type
->alignment
;
160 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
161 if (nels
+ padding
+ 1 > argc
) {
163 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
166 psi_ffi_struct_type_pad(&els
[nels
], padding
);
170 ZEND_ASSERT(offset
== darg
->layout
->pos
);
172 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
176 /* apply struct alignment padding */
177 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
179 ZEND_ASSERT(offset
<= strct
->size
);
180 if (offset
< strct
->size
) {
181 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
186 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
187 decl_type
*real
= real_decl_type(type
);
189 if (real
->type
== PSI_T_STRUCT
) {
190 if (!real
->strct
->engine
.type
) {
191 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
193 strct
->type
= FFI_TYPE_STRUCT
;
195 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
197 real
->strct
->engine
.type
= strct
;
198 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
201 return real
->strct
->engine
.type
;
203 return psi_ffi_token_type(real
->type
);
205 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
206 if (darg
->var
->pointer_level
) {
207 return &ffi_type_pointer
;
209 return psi_ffi_decl_type(darg
->type
);
213 typedef struct PSI_LibffiContext
{
218 typedef struct PSI_LibffiCall
{
220 ffi_closure
*closure
;
222 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
225 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
227 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
228 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
230 for (i
= 0; i
< c
; ++i
) {
231 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
233 call
->params
[c
] = NULL
;
235 decl
->call
.info
= call
;
236 decl
->call
.rval
= &decl
->func
->ptr
;
238 decl
->call
.args
= (void **) &call
->params
[c
+1];
240 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
241 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
242 ZEND_ASSERT(FFI_OK
== rc
);
247 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
248 PSI_LibffiContext
*context
= C
->context
;
251 call
->closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), &call
->code
);
252 ZEND_ASSERT(call
->closure
!= NULL
);
254 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
255 rc
= ffi_prep_closure_loc(
262 #elif PSI_HAVE_FFI_PREP_CLOSURE
263 rc
= ffi_prep_closure(call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
265 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
267 ZEND_ASSERT(FFI_OK
== rc
);
270 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
272 psi_ffi_closure_free(call
->closure
);
277 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
281 L
= malloc(sizeof(*L
));
283 memset(L
, 0, sizeof(*L
));
285 L
->params
[0] = &ffi_type_pointer
;
286 L
->params
[1] = &ffi_type_pointer
;
287 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
288 ZEND_ASSERT(rc
== FFI_OK
);
293 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
295 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
298 static void psi_ffi_init(PSI_Context
*C
)
300 C
->context
= PSI_LibffiContextInit(NULL
);
303 static void psi_ffi_dtor(PSI_Context
*C
)
308 for (i
= 0; i
< C
->decls
->count
; ++i
) {
309 decl
*decl
= C
->decls
->list
[i
];
311 if (decl
->call
.info
) {
312 PSI_LibffiCallFree(decl
->call
.info
);
319 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
322 zend_function_entry
*zfe
;
328 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
329 for (i
= 0; i
< C
->impls
->count
; ++i
) {
330 zend_function_entry
*zf
= &zfe
[j
];
331 PSI_LibffiCall
*call
;
332 impl
*impl
= C
->impls
->list
[i
];
338 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
339 PSI_LibffiCallInitClosure(C
, call
, impl
);
341 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
342 zf
->num_args
= impl
->func
->args
->count
;
343 zf
->handler
= call
->code
;
344 zf
->arg_info
= psi_internal_arginfo(impl
);
348 for (i
= 0; i
< C
->decls
->count
; ++i
) {
349 decl
*decl
= C
->decls
->list
[i
];
355 PSI_LibffiCallAlloc(C
, decl
);
361 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
362 PSI_LibffiCall
*call
= decl_call
->info
;
367 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
368 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
370 for (i
= 0; i
< nfixedargs
; ++i
) {
371 params
[i
] = call
->params
[i
];
372 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
374 for (i
= 0; i
< va
->args
->count
; ++i
) {
375 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
376 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
378 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
379 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
380 nfixedargs
, ntotalargs
,
381 call
->signature
.rtype
, (ffi_type
**) params
);
383 /* FIXME: test in config.m4; assume we can just call anyway */
384 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
385 call
->signature
.rtype
, (ffi_type
**) params
);
387 ZEND_ASSERT(FFI_OK
== rc
);
388 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
391 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
395 static PSI_ContextOps ops
= {
402 PSI_ContextOps
*PSI_Libffi(void)
407 #endif /* HAVE_LIBFFI */