ac018a119640d8ea0deed03ba479cf1e6c18a260
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
);
60 static inline ffi_abi
psi_ffi_abi(const char *convention
) {
61 return FFI_DEFAULT_ABI
;
63 static inline ffi_type
*psi_ffi_token_type(token_t t
) {
69 return &ffi_type_void
;
71 return &ffi_type_sint8
;
73 return &ffi_type_uint8
;
75 return &ffi_type_sint16
;
77 return &ffi_type_uint16
;
79 return &ffi_type_sint32
;
81 return &ffi_type_uint32
;
83 return &ffi_type_sint64
;
85 return &ffi_type_uint64
;
87 return &ffi_type_uchar
;
89 return &ffi_type_sint
;
91 return &ffi_type_float
;
93 return &ffi_type_double
;
95 return &ffi_type_pointer
;
98 static inline ffi_type
*psi_ffi_impl_type(token_t impl_type
) {
101 return &ffi_type_sint8
;
103 return &ffi_type_sint64
;
105 return &ffi_type_pointer
;
108 return &ffi_type_double
;
109 EMPTY_SWITCH_DEFAULT_CASE();
112 static void psi_ffi_struct_type_dtor(void *type
) {
113 ffi_type
*strct
= type
;
115 if (strct
->elements
) {
116 free(strct
->elements
);
120 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
);
121 static ffi_type
**psi_ffi_struct_type_elements(decl_struct
*strct
) {
122 ffi_type
**els
= calloc(strct
->args
->count
+ 1, sizeof(*els
));
125 for (i
= 0; i
< strct
->args
->count
; ++i
) {
126 els
[i
] = psi_ffi_decl_arg_type(strct
->args
->args
[i
]);
132 static inline ffi_type
*psi_ffi_decl_type(decl_type
*type
) {
133 decl_type
*real
= real_decl_type(type
);
135 if (real
->type
== PSI_T_STRUCT
) {
136 if (!real
->strct
->engine
.type
) {
137 ffi_type
*strct
= calloc(1, sizeof(ffi_type
));
139 strct
->type
= FFI_TYPE_STRUCT
;
140 strct
->size
= real
->strct
->size
;
141 strct
->alignment
= 0;
142 strct
->elements
= psi_ffi_struct_type_elements(real
->strct
);
144 real
->strct
->engine
.type
= strct
;
145 real
->strct
->engine
.dtor
= psi_ffi_struct_type_dtor
;
148 return real
->strct
->engine
.type
;
150 return psi_ffi_token_type(real
->type
);
152 static inline ffi_type
*psi_ffi_decl_arg_type(decl_arg
*darg
) {
153 if (darg
->var
->pointer_level
) {
154 return &ffi_type_pointer
;
156 return psi_ffi_decl_type(darg
->type
);
160 typedef struct PSI_LibffiContext
{
165 typedef struct PSI_LibffiCall
{
167 ffi_closure
*closure
;
169 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
172 static inline PSI_LibffiCall
*PSI_LibffiCallAlloc(PSI_Context
*C
, decl
*decl
) {
174 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
175 PSI_LibffiCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
177 for (i
= 0; i
< c
; ++i
) {
178 call
->params
[i
] = psi_ffi_decl_arg_type(decl
->args
->args
[i
]);
180 call
->params
[c
] = NULL
;
182 decl
->call
.info
= call
;
183 decl
->call
.rval
= decl
->func
->ptr
;
185 decl
->call
.args
= (void **) &call
->params
[c
+1];
187 rc
= ffi_prep_cif(&call
->signature
, psi_ffi_abi(decl
->abi
->convention
),
188 c
, psi_ffi_decl_arg_type(decl
->func
), (ffi_type
**) call
->params
);
189 ZEND_ASSERT(FFI_OK
== rc
);
194 static inline void PSI_LibffiCallInitClosure(PSI_Context
*C
, PSI_LibffiCall
*call
, impl
*impl
) {
195 PSI_LibffiContext
*context
= C
->context
;
198 call
->closure
= psi_ffi_closure_alloc(sizeof(ffi_closure
), &call
->code
);
199 ZEND_ASSERT(call
->closure
!= NULL
);
201 #if PSI_HAVE_FFI_PREP_CLOSURE_LOC
202 rc
= ffi_prep_closure_loc(
209 #elif PSI_HAVE_FFI_PREP_CLOSURE
210 rc
= ffi_prep_closure(call
->code
, &context
->signature
, psi_ffi_handler
, impl
);
212 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
214 ZEND_ASSERT(FFI_OK
== rc
);
217 static inline void PSI_LibffiCallFree(PSI_LibffiCall
*call
) {
219 psi_ffi_closure_free(call
->closure
);
224 static inline PSI_LibffiContext
*PSI_LibffiContextInit(PSI_LibffiContext
*L
) {
228 L
= malloc(sizeof(*L
));
230 memset(L
, 0, sizeof(*L
));
232 L
->params
[0] = &ffi_type_pointer
;
233 L
->params
[1] = &ffi_type_pointer
;
234 rc
= ffi_prep_cif(&L
->signature
, FFI_DEFAULT_ABI
, 2, &ffi_type_void
, L
->params
);
235 ZEND_ASSERT(rc
== FFI_OK
);
240 static void psi_ffi_handler(ffi_cif
*_sig
, void *_result
, void **_args
, void *_data
)
242 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
245 static void psi_ffi_init(PSI_Context
*C
)
247 C
->context
= PSI_LibffiContextInit(NULL
);
250 static void psi_ffi_dtor(PSI_Context
*C
)
255 for (i
= 0; i
< C
->decls
->count
; ++i
) {
256 decl
*decl
= C
->decls
->list
[i
];
258 if (decl
->call
.info
) {
259 PSI_LibffiCallFree(decl
->call
.info
);
266 static zend_function_entry
*psi_ffi_compile(PSI_Context
*C
)
269 zend_function_entry
*zfe
;
275 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
276 for (i
= 0; i
< C
->impls
->count
; ++i
) {
277 zend_function_entry
*zf
= &zfe
[j
];
278 PSI_LibffiCall
*call
;
279 impl
*impl
= C
->impls
->list
[i
];
285 call
= PSI_LibffiCallAlloc(C
, impl
->decl
);
286 PSI_LibffiCallInitClosure(C
, call
, impl
);
288 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
289 zf
->num_args
= impl
->func
->args
->count
;
290 zf
->handler
= call
->code
;
291 zf
->arg_info
= psi_internal_arginfo(impl
);
295 for (i
= 0; i
< C
->decls
->count
; ++i
) {
296 decl
*decl
= C
->decls
->list
[i
];
302 PSI_LibffiCallAlloc(C
, decl
);
308 static void psi_ffi_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
309 PSI_LibffiCall
*call
= decl_call
->info
;
314 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
315 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
317 for (i
= 0; i
< nfixedargs
; ++i
) {
318 params
[i
] = call
->params
[i
];
319 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
321 for (i
= 0; i
< va
->args
->count
; ++i
) {
322 params
[nfixedargs
+ i
] = psi_ffi_impl_type(va
->types
[i
]);
323 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
325 #ifdef PSI_HAVE_FFI_PREP_CIF_VAR
326 rc
= ffi_prep_cif_var(&signature
, call
->signature
.abi
,
327 nfixedargs
, ntotalargs
,
328 call
->signature
.rtype
, (ffi_type
**) params
);
330 /* FIXME: test in config.m4; assume we can just call anyway */
331 rc
= ffi_prep_cif(&signature
, call
->signature
.abi
, ntotalargs
,
332 call
->signature
.rtype
, (ffi_type
**) params
);
334 ZEND_ASSERT(FFI_OK
== rc
);
335 ffi_call(&signature
, FFI_FN(decl_call
->sym
), decl_call
->rval
, ¶ms
[ntotalargs
+ 1]);
338 ffi_call(&call
->signature
, FFI_FN(decl_call
->sym
), decl_call
->rval
, decl_call
->args
);
342 static PSI_ContextOps ops
= {
349 PSI_ContextOps
*PSI_Libffi(void)
354 #endif /* HAVE_LIBFFI */