369fe3367ba5d46b3c24398e19ed6168503ab8c4
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
;
92 return &ffi_type_sint
;
94 return &ffi_type_slong
;
96 return &ffi_type_float
;
98 return &ffi_type_double
;
99 #ifdef HAVE_LONG_DOUBLE
100 case PSI_T_LONG_DOUBLE
:
101 return &ffi_type_longdouble
;
104 return &ffi_type_pointer
;
107 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
110 return &ffi_type_sint8
;
112 return &ffi_type_sint64
;
114 return &ffi_type_pointer
;
117 return &ffi_type_double
;
118 EMPTY_SWITCH_DEFAULT_CASE();
122 static void psi_ffi_struct_type_dtor(void *type
) {
123 ffi_type
*strct
= type
;
125 if (strct
->elements
) {
128 for (ptr
= strct
->elements
; *ptr
; ++ptr
) {
131 free(strct
->elements
);
136 static size_t psi_ffi_struct_type_pad(ffi_type
**els
, size_t padding
) {
139 for (i
= 0; i
< padding
; ++i
) {
140 ffi_type
*pad
= malloc(sizeof(*pad
));
142 memcpy(pad
, &ffi_type_schar
, sizeof(*pad
));
149 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
150 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
= 0;
151 ffi_type
**els
= calloc(argc
+ 1, sizeof(*els
));
153 for (i
= 0; i
< strct
->args
->count
; ++i
) {
154 decl_arg
*darg
= strct
->args
->args
[i
];
155 ffi_type
*type
= malloc(sizeof(*type
));
158 memcpy(type
, psi_ffi_decl_arg_type(darg
), sizeof(*type
));
160 ZEND_ASSERT(type
->size
== darg
->layout
->len
);
162 if (type
->alignment
> maxalign
) {
163 maxalign
= type
->alignment
;
166 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, type
->alignment
))) {
167 if (nels
+ padding
+ 1 > argc
) {
169 els
= realloc(els
, (argc
+ 1) * sizeof(*els
));
172 psi_ffi_struct_type_pad(&els
[nels
], padding
);
176 ZEND_ASSERT(offset
== darg
->layout
->pos
);
178 offset
= (offset
+ darg
->layout
->len
+ type
->alignment
- 1) & ~(type
->alignment
- 1);
182 /* apply struct alignment padding */
183 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
185 ZEND_ASSERT(offset
<= strct
->size
);
186 if (offset
< strct
->size
) {
187 psi_ffi_struct_type_pad(&els
[nels
], strct
->size
- offset
);
192 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
193 decl_type
*real
= real_decl_type(type
);
195 if (real
->type
== PSI_T_STRUCT
) {
196 if (!real
->strct
->engine
.type
) {
197 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
199 strct
->type
= FFI_TYPE_STRUCT
;
201 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
203 real
->strct
->engine
.type
= strct
;
204 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
207 return real
->strct
->engine
.type
;
209 return psi_ffi_token_type(real
->type
);
211 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
212 if (darg
->var
->pointer_level
) {
213 return &ffi_type_pointer
;
215 return psi_ffi_decl_type(darg
->type
);
219 typedef struct PSI_LibffiContext
{
224 typedef struct PSI_LibffiCall
{
226 ffi_closure
*closure
;
228 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
231 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
233 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
234 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
236 for (i
= 0; i
< c
; ++i
) {
237 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
239 call
->params
[c
] = NULL
;
241 decl
->call
.info
= call
;
242 decl
->call
.rval
= &decl
->func
->ptr
;
244 decl
->call
.args
= (void **) &call
->params
[c
+1];
246 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
247 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
248 ZEND_ASSERT(FFI_OK
== rc
);
253 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
254 PSI_LibffiContext
*context
= C
->context
;
257 call
->closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), &call
->code
);
258 ZEND_ASSERT(call
->closure
!= NULL
);
260 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
261 rc
= ffi_prep_closure_loc(
268 #elif PSI_HAVE_FFI_PREP_CLOSURE
269 rc
= ffi_prep_closure(call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
271 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
273 ZEND_ASSERT(FFI_OK
== rc
);
276 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
278 psi_ffi_closure_free(call
->closure
);
283 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
287 L
= malloc(sizeof(*L
));
289 memset(L
, 0, sizeof(*L
));
291 L
->params
[0] = &ffi_type_pointer
;
292 L
->params
[1] = &ffi_type_pointer
;
293 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
294 ZEND_ASSERT(rc
== FFI_OK
);
299 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
301 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
304 static void psi_ffi_init(PSI_Context
*C
)
306 C
->context
= PSI_LibffiContextInit(NULL
);
309 static void psi_ffi_dtor(PSI_Context
*C
)
314 for (i
= 0; i
< C
->decls
->count
; ++i
) {
315 decl
*decl
= C
->decls
->list
[i
];
317 if (decl
->call
.info
) {
318 PSI_LibffiCallFree(decl
->call
.info
);
325 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
328 zend_function_entry
*zfe
;
334 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
335 for (i
= 0; i
< C
->impls
->count
; ++i
) {
336 zend_function_entry
*zf
= &zfe
[j
];
337 PSI_LibffiCall
*call
;
338 impl
*impl
= C
->impls
->list
[i
];
344 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
345 PSI_LibffiCallInitClosure(C
, call
, impl
);
347 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
348 zf
->num_args
= impl
->func
->args
->count
;
349 zf
->handler
= call
->code
;
350 zf
->arg_info
= psi_internal_arginfo(impl
);
354 for (i
= 0; i
< C
->decls
->count
; ++i
) {
355 decl
*decl
= C
->decls
->list
[i
];
361 PSI_LibffiCallAlloc(C
, decl
);
367 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
368 PSI_LibffiCall
*call
= decl_call
->info
;
373 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
374 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
376 for (i
= 0; i
< nfixedargs
; ++i
) {
377 params
[i
] = call
->params
[i
];
378 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
380 for (i
= 0; i
< va
->args
->count
; ++i
) {
381 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
382 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
384 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
385 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
386 nfixedargs
, ntotalargs
,
387 call
->signature
.rtype
, (ffi_type
**) params
);
389 /* FIXME: test in config.m4; assume we can just call anyway */
390 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
391 call
->signature
.rtype
, (ffi_type
**) params
);
393 ZEND_ASSERT(FFI_OK
== rc
);
394 ffi_call(&signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
397 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), *decl_call
->rval
, decl_call
->args
);
401 static PSI_ContextOps ops
= {
408 PSI_ContextOps
*PSI_Libffi(void)
413 #endif /* HAVE_LIBFFI */