14 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
);
16 static inline jit_abi_t
psi_jit_abi(const char *convention
) {
19 static inline jit_type_t
psi_jit_token_type(token_t t
) {
24 return jit_type_sbyte
;
26 return jit_type_ubyte
;
28 return jit_type_short
;
30 return jit_type_ushort
;
38 return jit_type_ulong
;
40 return jit_type_sys_bool
;
42 return jit_type_sys_float
;
44 return jit_type_sys_double
;
45 EMPTY_SWITCH_DEFAULT_CASE();
48 static inline jit_type_t
psi_jit_impl_type(token_t impl_type
) {
51 return jit_type_sbyte
;
55 return jit_type_void_ptr
;
58 return jit_type_sys_double
;
59 EMPTY_SWITCH_DEFAULT_CASE();
62 static inline jit_type_t
psi_jit_decl_type(decl_type
*type
) {
63 return psi_jit_token_type(real_decl_type(type
)->type
);
65 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
) {
66 if (darg
->var
->pointer_level
) {
67 return jit_type_void_ptr
;
69 return psi_jit_decl_type(darg
->type
);
73 typedef struct PSI_LibjitContext
{
77 struct PSI_LibjitData
**list
;
82 typedef struct PSI_LibjitCall
{
85 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
88 typedef struct PSI_LibjitData
{
89 PSI_LibjitContext
*context
;
91 zend_internal_arg_info
*arginfo
;
94 static inline PSI_LibjitCall
*PSI_LibjitCallAlloc(PSI_Context
*C
, decl
*decl
) {
95 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
96 PSI_LibjitCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
98 for (i
= 0; i
< c
; ++i
) {
99 call
->params
[i
] = psi_jit_decl_arg_type(decl
->args
->args
[i
]);
101 call
->params
[c
] = NULL
;
103 decl
->call
.info
= call
;
104 decl
->call
.rval
= decl
->func
->ptr
;
106 decl
->call
.args
= (void **) &call
->params
[c
+1];
108 call
->signature
= jit_type_create_signature(
109 psi_jit_abi(decl
->abi
->convention
),
110 psi_jit_decl_arg_type(decl
->func
),
111 (jit_type_t
*) call
->params
, c
, 1);
115 static inline void PSI_LibjitCallInitClosure(PSI_Context
*C
, PSI_LibjitCall
*call
, impl
*impl
) {
116 PSI_LibjitContext
*context
= C
->context
;
117 call
->closure
= jit_closure_create(context
->jit
, context
->signature
,
118 &psi_jit_handler
, impl
);
121 static inline void PSI_LibjitCallFree(PSI_LibjitCall
*call
) {
122 jit_type_free(call
->signature
);
126 static inline PSI_LibjitContext
*PSI_LibjitContextInit(PSI_LibjitContext
*L
) {
127 jit_type_t params
[] = {
133 L
= malloc(sizeof(*L
));
135 memset(L
, 0, sizeof(*L
));
137 L
->jit
= jit_context_create();
138 L
->signature
= jit_type_create_signature(jit_abi_cdecl
, jit_type_void
,
144 static inline void PSI_LibjitContextDtor(PSI_LibjitContext
*L
) {
145 jit_type_free(L
->signature
);
146 jit_context_destroy(L
->jit
);
149 static inline void PSI_LibjitContextFree(PSI_LibjitContext
**L
) {
151 PSI_LibjitContextDtor(*L
);
157 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
)
159 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
162 static void psi_jit_init(PSI_Context
*C
)
164 C
->context
= PSI_LibjitContextInit(NULL
);
167 static void psi_jit_dtor(PSI_Context
*C
)
172 for (i
= 0; i
< C
->decls
->count
; ++i
) {
173 decl
*decl
= C
->decls
->list
[i
];
175 PSI_LibjitCallFree(decl
->call
.info
);
178 PSI_LibjitContextFree((void *) &C
->context
);
181 static zend_function_entry
*psi_jit_compile(PSI_Context
*C
)
184 zend_function_entry
*zfe
;
185 PSI_LibjitContext
*ctx
= C
->context
;
191 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
192 jit_context_build_start(ctx
->jit
);
194 for (i
= 0; i
< C
->impls
->count
; ++i
) {
195 zend_function_entry
*zf
= &zfe
[j
];
196 PSI_LibjitCall
*call
;
197 impl
*impl
= C
->impls
->list
[i
];
203 call
= PSI_LibjitCallAlloc(C
, impl
->decl
);
204 PSI_LibjitCallInitClosure(C
, call
, impl
);
206 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
207 zf
->num_args
= impl
->func
->args
->count
;
208 zf
->handler
= call
->closure
;
209 zf
->arg_info
= psi_internal_arginfo(impl
);
213 for (i
= 0; i
< C
->decls
->count
; ++i
) {
214 decl
*decl
= C
->decls
->list
[i
];
220 PSI_LibjitCallAlloc(C
, decl
);
223 jit_context_build_end(ctx
->jit
);
228 static void psi_jit_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
229 PSI_LibjitCall
*call
= decl_call
->info
;
232 jit_type_t signature
;
233 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
234 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
236 for (i
= 0; i
< nfixedargs
; ++i
) {
237 params
[i
] = call
->params
[i
];
238 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
240 for (i
= 0; i
< va
->args
->count
; ++i
) {
241 params
[nfixedargs
+ i
] = psi_jit_impl_type(va
->types
[i
]);
242 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
245 signature
= jit_type_create_signature(
246 jit_type_get_abi(call
->signature
),
247 jit_type_get_return(call
->signature
),
248 (jit_type_t
*) params
, ntotalargs
, 1);
249 ZEND_ASSERT(signature
);
251 jit_apply(signature
, decl_call
->sym
, ¶ms
[ntotalargs
+ 1],
252 nfixedargs
, decl_call
->rval
);
253 jit_type_free(signature
);
256 jit_apply(call
->signature
, decl_call
->sym
, decl_call
->args
,
257 decl_call
->argc
, decl_call
->rval
);
261 static PSI_ContextOps ops
= {
268 PSI_ContextOps
*PSI_Libjit(void)
273 #endif /* HAVE_LIBJIT */