4 # include "php_config.h"
17 static void psi_jit_handler(jit_type_t _sig
, void *_result
, void **_args
, void *_data
)
19 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
22 static void psi_jit_callback(jit_type_t _sig
, void *_result
, void **_args
, void *_data
)
24 psi_callback(_data
, _result
, jit_type_num_params(_sig
), _args
);
27 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
);
29 static inline jit_abi_t
psi_jit_abi(const char *convention
) {
32 static inline jit_type_t
psi_jit_token_type(token_t t
) {
40 return jit_type_sbyte
;
42 return jit_type_ubyte
;
44 return jit_type_short
;
46 return jit_type_ushort
;
54 return jit_type_ulong
;
56 return jit_type_sys_bool
;
59 return jit_type_sys_int
;
61 return jit_type_sys_long
;
63 return jit_type_sys_float
;
65 return jit_type_sys_double
;
66 #ifdef HAVE_LONG_DOUBLE
67 case PSI_T_LONG_DOUBLE
:
68 return jit_type_sys_long_double
;
72 return jit_type_void_ptr
;
75 static inline jit_type_t
psi_jit_impl_type(token_t impl_type
) {
78 return jit_type_sbyte
;
82 return jit_type_void_ptr
;
85 return jit_type_sys_double
;
86 EMPTY_SWITCH_DEFAULT_CASE();
91 static void psi_jit_struct_type_dtor(void *type
) {
92 jit_type_t strct
= type
;
97 static size_t psi_jit_struct_type_pad(jit_type_t
*els
, size_t padding
) {
100 for (i
= 0; i
< padding
; ++i
) {
101 *els
++ = jit_type_copy(jit_type_sys_char
);
107 static unsigned psi_jit_struct_type_elements(decl_struct
*strct
, jit_type_t
**fields
) {
108 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
;
109 *fields
= calloc(argc
+ 1, sizeof(*fields
));
111 for (i
= 0; i
< strct
->args
->count
; ++i
) {
112 decl_arg
*darg
= strct
->args
->args
[i
];
113 jit_type_t type
= jit_type_copy(psi_jit_decl_arg_type(darg
));
114 size_t padding
, alignment
;
116 ZEND_ASSERT(jit_type_get_size(type
) == darg
->layout
->len
);
118 if ((alignment
= jit_type_get_alignment(type
)) > maxalign
) {
119 maxalign
= alignment
;
122 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, alignment
))) {
123 if (nels
+ padding
> argc
) {
125 *fields
= realloc(*fields
, (argc
+ 1) * sizeof(*fields
));
127 psi_jit_struct_type_pad(&(*fields
)[nels
], padding
);
131 ZEND_ASSERT(offset
== darg
->layout
->pos
);
133 offset
= (offset
+ darg
->layout
->len
+ alignment
- 1) & ~(alignment
- 1);
134 (*fields
)[nels
++] = type
;
137 /* apply struct alignment padding */
138 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
140 ZEND_ASSERT(offset
<= strct
->size
);
141 if (offset
< strct
->size
) {
142 nels
+= psi_jit_struct_type_pad(&(*fields
)[nels
], strct
->size
- offset
);
147 static inline jit_type_t
psi_jit_decl_type(decl_type
*type
) {
148 decl_type
*real
= real_decl_type(type
);
150 switch (real
->type
) {
152 if (!real
->real
.strct
->engine
.type
) {
154 jit_type_t strct
, *fields
= NULL
;
156 count
= psi_jit_struct_type_elements(real
->real
.strct
, &fields
);
157 strct
= jit_type_create_struct(fields
, count
, 0);
159 real
->real
.strct
->engine
.type
= strct
;
160 real
->real
.strct
->engine
.dtor
= psi_jit_struct_type_dtor
;
163 return real
->real
.strct
->engine
.type
;
166 return psi_jit_decl_arg_type(real
->real
.unn
->args
->args
[0]);
169 return psi_jit_token_type(real
->type
);
172 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
) {
173 if (darg
->var
->pointer_level
) {
174 return jit_type_void_ptr
;
176 return psi_jit_decl_type(darg
->type
);
180 struct psi_jit_context
{
182 jit_type_t signature
;
185 struct psi_jit_call
{
187 jit_type_t signature
;
188 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
191 static inline struct psi_jit_call
*psi_jit_call_alloc(struct psi_context
*C
, decl
*decl
) {
192 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
193 struct psi_jit_call
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
195 for (i
= 0; i
< c
; ++i
) {
196 call
->params
[i
] = psi_jit_decl_arg_type(decl
->args
->args
[i
]);
198 call
->params
[c
] = NULL
;
200 decl
->call
.info
= call
;
201 decl
->call
.rval
= &decl
->func
->ptr
;
203 decl
->call
.args
= (void **) &call
->params
[c
+1];
205 call
->signature
= jit_type_create_signature(
206 psi_jit_abi(decl
->abi
->convention
),
207 psi_jit_decl_arg_type(decl
->func
),
208 (jit_type_t
*) call
->params
, c
, 1);
209 ZEND_ASSERT(call
->signature
);
214 static inline void *psi_jit_call_init_closure(struct psi_context
*C
, struct psi_jit_call
*call
, impl
*impl
) {
215 struct psi_jit_context
*context
= C
->context
;
216 return call
->closure
= jit_closure_create(context
->jit
, context
->signature
,
217 &psi_jit_handler
, impl
);
220 static inline void *psi_jit_call_init_callback_closure(struct psi_context
*C
, struct psi_jit_call
*call
, let_callback
*cb
) {
221 struct psi_jit_context
*context
= C
->context
;
222 return call
->closure
= jit_closure_create(context
->jit
, call
->signature
,
223 &psi_jit_callback
, cb
);
226 static inline void psi_jit_call_free(struct psi_jit_call
*call
) {
227 jit_type_free(call
->signature
);
231 static inline struct psi_jit_context
*psi_jit_context_init(struct psi_jit_context
*L
) {
232 jit_type_t params
[] = {
238 L
= malloc(sizeof(*L
));
240 memset(L
, 0, sizeof(*L
));
242 L
->jit
= jit_context_create();
243 L
->signature
= jit_type_create_signature(jit_abi_cdecl
, jit_type_void
,
249 static inline void psi_jit_context_dtor(struct psi_jit_context
*L
) {
250 jit_type_free(L
->signature
);
251 jit_context_destroy(L
->jit
);
254 static inline void psi_jit_context_free(struct psi_jit_context
**L
) {
256 psi_jit_context_dtor(*L
);
262 static void psi_jit_init(struct psi_context
*C
)
264 C
->context
= psi_jit_context_init(NULL
);
267 static inline void psi_jit_destroy_callbacks(struct psi_context
*C
, let_val
*let_val
) {
271 switch (let_val
->kind
) {
272 case PSI_LET_CALLBACK
:
273 cb
= let_val
->data
.callback
;
275 if (cb
->decl
&& cb
->decl
->call
.info
) {
276 psi_jit_call_free(cb
->decl
->call
.info
);
282 fn
= let_val
->data
.func
;
288 for (i
= 0; i
< fn
->inner
->count
; ++i
) {
289 psi_jit_destroy_callbacks(C
, fn
->inner
->vals
[i
]);
298 static void psi_jit_dtor(struct psi_context
*C
)
303 for (i
= 0; i
< C
->decls
->count
; ++i
) {
304 decl
*decl
= C
->decls
->list
[i
];
306 if (decl
->call
.info
) {
307 psi_jit_call_free(decl
->call
.info
);
314 for (i
= 0; i
< C
->impls
->count
; ++i
) {
315 impl
*impl
= C
->impls
->list
[i
];
317 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
318 psi_jit_destroy_callbacks(C
, impl
->stmts
->let
.list
[j
]->val
);
322 psi_jit_context_free((void *) &C
->context
);
325 static inline void psi_jit_compile_callbacks(struct psi_context
*C
, let_val
*let_val
) {
326 struct psi_jit_call
*call
;
330 switch (let_val
->kind
) {
331 case PSI_LET_CALLBACK
:
332 cb
= let_val
->data
.callback
;
333 if ((call
= psi_jit_call_alloc(C
, cb
->decl
))) {
334 if (!psi_jit_call_init_callback_closure(C
, call
, cb
)) {
335 psi_jit_call_free(call
);
339 cb
->decl
->call
.sym
= call
->closure
;
345 fn
= let_val
->data
.func
;
350 for (i
= 0; i
< fn
->inner
->count
; ++i
) {
351 psi_jit_compile_callbacks(C
, fn
->inner
->vals
[i
]);
360 static zend_function_entry
*psi_jit_compile(struct psi_context
*C
)
363 zend_function_entry
*zfe
;
364 struct psi_jit_context
*ctx
= C
->context
;
370 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
371 jit_context_build_start(ctx
->jit
);
373 for (i
= 0; i
< C
->impls
->count
; ++i
) {
374 zend_function_entry
*zf
= &zfe
[j
];
375 struct psi_jit_call
*call
;
376 impl
*impl
= C
->impls
->list
[i
];
381 if (!(call
= psi_jit_call_alloc(C
, impl
->decl
))) {
384 if (!psi_jit_call_init_closure(C
, call
, impl
)) {
385 psi_jit_call_free(call
);
389 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
390 zf
->num_args
= impl
->func
->args
->count
;
391 zf
->handler
= call
->closure
;
392 zf
->arg_info
= psi_internal_arginfo(impl
);
395 for (c
= 0; c
< impl
->stmts
->let
.count
; ++c
) {
396 psi_jit_compile_callbacks(C
, impl
->stmts
->let
.list
[c
]->val
);
400 for (i
= 0; i
< C
->decls
->count
; ++i
) {
401 decl
*decl
= C
->decls
->list
[i
];
403 if (decl
->call
.info
) {
407 psi_jit_call_alloc(C
, decl
);
410 jit_context_build_end(ctx
->jit
);
415 static void psi_jit_call(struct psi_context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
416 struct psi_jit_call
*call
= decl_call
->info
;
419 jit_type_t signature
;
420 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
421 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
423 for (i
= 0; i
< nfixedargs
; ++i
) {
424 params
[i
] = call
->params
[i
];
425 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
427 for (i
= 0; i
< va
->args
->count
; ++i
) {
428 params
[nfixedargs
+ i
] = psi_jit_impl_type(va
->types
[i
]);
429 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
432 signature
= jit_type_create_signature(
433 jit_type_get_abi(call
->signature
),
434 jit_type_get_return(call
->signature
),
435 (jit_type_t
*) params
, ntotalargs
, 1);
436 ZEND_ASSERT(signature
);
438 jit_apply(signature
, decl_call
->sym
, ¶ms
[ntotalargs
+ 1],
439 nfixedargs
, *decl_call
->rval
);
440 jit_type_free(signature
);
443 jit_apply(call
->signature
, decl_call
->sym
, decl_call
->args
,
444 decl_call
->argc
, *decl_call
->rval
);
448 static struct psi_context_ops ops
= {
455 struct psi_context_ops
*psi_libjit_ops(void)
460 #endif /* HAVE_LIBJIT */