14 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
);
15 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
);
17 static inline jit_abi_t
psi_jit_abi(const char *convention
) {
20 static inline jit_type_t
psi_jit_token_type(token_t t
) {
28 return jit_type_sbyte
;
30 return jit_type_ubyte
;
32 return jit_type_short
;
34 return jit_type_ushort
;
42 return jit_type_ulong
;
44 return jit_type_sys_bool
;
46 return jit_type_sys_int
;
48 return jit_type_sys_long
;
50 return jit_type_sys_float
;
52 return jit_type_sys_double
;
54 return jit_type_void_ptr
;
57 static inline jit_type_t
psi_jit_impl_type(token_t impl_type
) {
60 return jit_type_sbyte
;
64 return jit_type_void_ptr
;
67 return jit_type_sys_double
;
68 EMPTY_SWITCH_DEFAULT_CASE();
72 static void psi_jit_struct_type_dtor(void *type
) {
73 jit_type_t strct
= type
;
78 static size_t psi_jit_struct_type_pad(jit_type_t
**els
, size_t padding
) {
81 for (i
= 0; i
< padding
; ++i
) {
82 *els
++ = jit_type_copy(jit_type_sys_char
);
88 static unsigned psi_jit_struct_type_elements(decl_struct
*strct
, jit_type_t
**fields
) {
89 size_t i
, j
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
;
90 *fields
= calloc(argc
+ 1, sizeof(*fields
));
92 for (i
= 0; i
< strct
->args
->count
; ++i
) {
93 decl_arg
*darg
= strct
->args
->args
[i
];
94 jit_type_t type
= jit_type_copy(psi_jit_decl_arg_type(darg
));
95 size_t padding
, alignment
;
97 ZEND_ASSERT(jit_type_get_size(type
) == darg
->layout
->len
);
99 if ((alignment
= jit_type_get_alignment(type
)) > maxalign
) {
100 maxalign
= alignment
;
103 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, alignment
))) {
104 if (nels
+ padding
> argc
) {
106 *fields
= realloc(*fields
, (argc
+ 1) * sizeof(*fields
));
108 psi_jit_struct_type_pad(&(*fields
)[nels
], padding
);
112 ZEND_ASSERT(offset
== darg
->layout
->pos
);
114 offset
= (offset
+ darg
->layout
->len
+ alignment
- 1) & ~(alignment
- 1);
115 (*fields
)[nels
++] = type
;
118 /* apply struct alignment padding */
119 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
121 ZEND_ASSERT(offset
<= strct
->size
);
122 if (offset
< strct
->size
) {
123 nels
+= psi_jit_struct_type_pad(&(*fields
)[nels
], strct
->size
- offset
);
128 static inline jit_type_t
psi_jit_decl_type(decl_type
*type
) {
129 decl_type
*real
= real_decl_type(type
);
131 if (real
->type
== PSI_T_STRUCT
) {
132 if (!real
->strct
->engine
.type
) {
134 jit_type_t strct
, *fields
= NULL
;
136 count
= psi_jit_struct_type_elements(real
->strct
, &fields
);
137 strct
= jit_type_create_struct(fields
, count
, 0);
139 real
->strct
->engine
.type
= strct
;
140 real
->strct
->engine
.dtor
= psi_jit_struct_type_dtor
;
143 return real
->strct
->engine
.type
;
145 return psi_jit_token_type(real
->type
);
147 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
) {
148 if (darg
->var
->pointer_level
) {
149 return jit_type_void_ptr
;
151 return psi_jit_decl_type(darg
->type
);
155 typedef struct PSI_LibjitContext
{
157 jit_type_t signature
;
159 struct PSI_LibjitData
**list
;
164 typedef struct PSI_LibjitCall
{
166 jit_type_t signature
;
167 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
170 typedef struct PSI_LibjitData
{
171 PSI_LibjitContext
*context
;
173 zend_internal_arg_info
*arginfo
;
176 static inline PSI_LibjitCall
*PSI_LibjitCallAlloc(PSI_Context
*C
, decl
*decl
) {
177 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
178 PSI_LibjitCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
180 for (i
= 0; i
< c
; ++i
) {
181 call
->params
[i
] = psi_jit_decl_arg_type(decl
->args
->args
[i
]);
183 call
->params
[c
] = NULL
;
185 decl
->call
.info
= call
;
186 decl
->call
.rval
= &decl
->func
->ptr
;
188 decl
->call
.args
= (void **) &call
->params
[c
+1];
190 call
->signature
= jit_type_create_signature(
191 psi_jit_abi(decl
->abi
->convention
),
192 psi_jit_decl_arg_type(decl
->func
),
193 (jit_type_t
*) call
->params
, c
, 1);
197 static inline void PSI_LibjitCallInitClosure(PSI_Context
*C
, PSI_LibjitCall
*call
, impl
*impl
) {
198 PSI_LibjitContext
*context
= C
->context
;
199 call
->closure
= jit_closure_create(context
->jit
, context
->signature
,
200 &psi_jit_handler
, impl
);
203 static inline void PSI_LibjitCallFree(PSI_LibjitCall
*call
) {
204 jit_type_free(call
->signature
);
208 static inline PSI_LibjitContext
*PSI_LibjitContextInit(PSI_LibjitContext
*L
) {
209 jit_type_t params
[] = {
215 L
= malloc(sizeof(*L
));
217 memset(L
, 0, sizeof(*L
));
219 L
->jit
= jit_context_create();
220 L
->signature
= jit_type_create_signature(jit_abi_cdecl
, jit_type_void
,
226 static inline void PSI_LibjitContextDtor(PSI_LibjitContext
*L
) {
227 jit_type_free(L
->signature
);
228 jit_context_destroy(L
->jit
);
231 static inline void PSI_LibjitContextFree(PSI_LibjitContext
**L
) {
233 PSI_LibjitContextDtor(*L
);
239 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
)
241 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
244 static void psi_jit_init(PSI_Context
*C
)
246 C
->context
= PSI_LibjitContextInit(NULL
);
249 static void psi_jit_dtor(PSI_Context
*C
)
254 for (i
= 0; i
< C
->decls
->count
; ++i
) {
255 decl
*decl
= C
->decls
->list
[i
];
257 PSI_LibjitCallFree(decl
->call
.info
);
260 PSI_LibjitContextFree((void *) &C
->context
);
263 static zend_function_entry
*psi_jit_compile(PSI_Context
*C
)
266 zend_function_entry
*zfe
;
267 PSI_LibjitContext
*ctx
= C
->context
;
273 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
274 jit_context_build_start(ctx
->jit
);
276 for (i
= 0; i
< C
->impls
->count
; ++i
) {
277 zend_function_entry
*zf
= &zfe
[j
];
278 PSI_LibjitCall
*call
;
279 impl
*impl
= C
->impls
->list
[i
];
285 call
= PSI_LibjitCallAlloc(C
, impl
->decl
);
286 PSI_LibjitCallInitClosure(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
->closure
;
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_LibjitCallAlloc(C
, decl
);
305 jit_context_build_end(ctx
->jit
);
310 static void psi_jit_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
311 PSI_LibjitCall
*call
= decl_call
->info
;
314 jit_type_t signature
;
315 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
316 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
318 for (i
= 0; i
< nfixedargs
; ++i
) {
319 params
[i
] = call
->params
[i
];
320 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
322 for (i
= 0; i
< va
->args
->count
; ++i
) {
323 params
[nfixedargs
+ i
] = psi_jit_impl_type(va
->types
[i
]);
324 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
327 signature
= jit_type_create_signature(
328 jit_type_get_abi(call
->signature
),
329 jit_type_get_return(call
->signature
),
330 (jit_type_t
*) params
, ntotalargs
, 1);
331 ZEND_ASSERT(signature
);
333 jit_apply(signature
, decl_call
->sym
, ¶ms
[ntotalargs
+ 1],
334 nfixedargs
, *decl_call
->rval
);
335 jit_type_free(signature
);
338 jit_apply(call
->signature
, decl_call
->sym
, decl_call
->args
,
339 decl_call
->argc
, *decl_call
->rval
);
343 static PSI_ContextOps ops
= {
350 PSI_ContextOps
*PSI_Libjit(void)
355 #endif /* HAVE_LIBJIT */