15 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
);
16 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
);
18 static inline jit_abi_t
psi_jit_abi(const char *convention
) {
21 static inline jit_type_t
psi_jit_token_type(token_t t
) {
29 return jit_type_sbyte
;
31 return jit_type_ubyte
;
33 return jit_type_short
;
35 return jit_type_ushort
;
43 return jit_type_ulong
;
45 return jit_type_sys_bool
;
47 return jit_type_sys_int
;
49 return jit_type_sys_long
;
51 return jit_type_sys_float
;
53 return jit_type_sys_double
;
54 #ifdef HAVE_LONG_DOUBLE
55 case PSI_T_LONG_DOUBLE
:
56 return jit_type_sys_long_double
;
59 return jit_type_void_ptr
;
62 static inline jit_type_t
psi_jit_impl_type(token_t impl_type
) {
65 return jit_type_sbyte
;
69 return jit_type_void_ptr
;
72 return jit_type_sys_double
;
73 EMPTY_SWITCH_DEFAULT_CASE();
78 static void psi_jit_struct_type_dtor(void *type
) {
79 jit_type_t strct
= type
;
84 static size_t psi_jit_struct_type_pad(jit_type_t
*els
, size_t padding
) {
87 for (i
= 0; i
< padding
; ++i
) {
88 *els
++ = jit_type_copy(jit_type_sys_char
);
94 static unsigned psi_jit_struct_type_elements(decl_struct
*strct
, jit_type_t
**fields
) {
95 size_t i
, argc
= strct
->args
->count
, nels
= 0, offset
= 0, maxalign
;
96 *fields
= calloc(argc
+ 1, sizeof(*fields
));
98 for (i
= 0; i
< strct
->args
->count
; ++i
) {
99 decl_arg
*darg
= strct
->args
->args
[i
];
100 jit_type_t type
= jit_type_copy(psi_jit_decl_arg_type(darg
));
101 size_t padding
, alignment
;
103 ZEND_ASSERT(jit_type_get_size(type
) == darg
->layout
->len
);
105 if ((alignment
= jit_type_get_alignment(type
)) > maxalign
) {
106 maxalign
= alignment
;
109 if ((padding
= psi_offset_padding(darg
->layout
->pos
- offset
, alignment
))) {
110 if (nels
+ padding
> argc
) {
112 *fields
= realloc(*fields
, (argc
+ 1) * sizeof(*fields
));
114 psi_jit_struct_type_pad(&(*fields
)[nels
], padding
);
118 ZEND_ASSERT(offset
== darg
->layout
->pos
);
120 offset
= (offset
+ darg
->layout
->len
+ alignment
- 1) & ~(alignment
- 1);
121 (*fields
)[nels
++] = type
;
124 /* apply struct alignment padding */
125 offset
= (offset
+ maxalign
- 1) & ~(maxalign
- 1);
127 ZEND_ASSERT(offset
<= strct
->size
);
128 if (offset
< strct
->size
) {
129 nels
+= psi_jit_struct_type_pad(&(*fields
)[nels
], strct
->size
- offset
);
134 static inline jit_type_t
psi_jit_decl_type(decl_type
*type
) {
135 decl_type
*real
= real_decl_type(type
);
137 if (real
->type
== PSI_T_STRUCT
) {
138 if (!real
->strct
->engine
.type
) {
140 jit_type_t strct
, *fields
= NULL
;
142 count
= psi_jit_struct_type_elements(real
->strct
, &fields
);
143 strct
= jit_type_create_struct(fields
, count
, 0);
145 real
->strct
->engine
.type
= strct
;
146 real
->strct
->engine
.dtor
= psi_jit_struct_type_dtor
;
149 return real
->strct
->engine
.type
;
151 return psi_jit_token_type(real
->type
);
153 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
) {
154 if (darg
->var
->pointer_level
) {
155 return jit_type_void_ptr
;
157 return psi_jit_decl_type(darg
->type
);
161 typedef struct PSI_LibjitContext
{
163 jit_type_t signature
;
165 struct PSI_LibjitData
**list
;
170 typedef struct PSI_LibjitCall
{
172 jit_type_t signature
;
173 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
176 typedef struct PSI_LibjitData
{
177 PSI_LibjitContext
*context
;
179 zend_internal_arg_info
*arginfo
;
182 static inline PSI_LibjitCall
*PSI_LibjitCallAlloc(PSI_Context
*C
, decl
*decl
) {
183 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
184 PSI_LibjitCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
186 for (i
= 0; i
< c
; ++i
) {
187 call
->params
[i
] = psi_jit_decl_arg_type(decl
->args
->args
[i
]);
189 call
->params
[c
] = NULL
;
191 decl
->call
.info
= call
;
192 decl
->call
.rval
= &decl
->func
->ptr
;
194 decl
->call
.args
= (void **) &call
->params
[c
+1];
196 call
->signature
= jit_type_create_signature(
197 psi_jit_abi(decl
->abi
->convention
),
198 psi_jit_decl_arg_type(decl
->func
),
199 (jit_type_t
*) call
->params
, c
, 1);
203 static inline void PSI_LibjitCallInitClosure(PSI_Context
*C
, PSI_LibjitCall
*call
, impl
*impl
) {
204 PSI_LibjitContext
*context
= C
->context
;
205 call
->closure
= jit_closure_create(context
->jit
, context
->signature
,
206 &psi_jit_handler
, impl
);
209 static inline void PSI_LibjitCallFree(PSI_LibjitCall
*call
) {
210 jit_type_free(call
->signature
);
214 static inline PSI_LibjitContext
*PSI_LibjitContextInit(PSI_LibjitContext
*L
) {
215 jit_type_t params
[] = {
221 L
= malloc(sizeof(*L
));
223 memset(L
, 0, sizeof(*L
));
225 L
->jit
= jit_context_create();
226 L
->signature
= jit_type_create_signature(jit_abi_cdecl
, jit_type_void
,
232 static inline void PSI_LibjitContextDtor(PSI_LibjitContext
*L
) {
233 jit_type_free(L
->signature
);
234 jit_context_destroy(L
->jit
);
237 static inline void PSI_LibjitContextFree(PSI_LibjitContext
**L
) {
239 PSI_LibjitContextDtor(*L
);
245 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
)
247 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
250 static void psi_jit_init(PSI_Context
*C
)
252 C
->context
= PSI_LibjitContextInit(NULL
);
255 static void psi_jit_dtor(PSI_Context
*C
)
260 for (i
= 0; i
< C
->decls
->count
; ++i
) {
261 decl
*decl
= C
->decls
->list
[i
];
263 PSI_LibjitCallFree(decl
->call
.info
);
266 PSI_LibjitContextFree((void *) &C
->context
);
269 static zend_function_entry
*psi_jit_compile(PSI_Context
*C
)
272 zend_function_entry
*zfe
;
273 PSI_LibjitContext
*ctx
= C
->context
;
279 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
280 jit_context_build_start(ctx
->jit
);
282 for (i
= 0; i
< C
->impls
->count
; ++i
) {
283 zend_function_entry
*zf
= &zfe
[j
];
284 PSI_LibjitCall
*call
;
285 impl
*impl
= C
->impls
->list
[i
];
291 call
= PSI_LibjitCallAlloc(C
, impl
->decl
);
292 PSI_LibjitCallInitClosure(C
, call
, impl
);
294 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
295 zf
->num_args
= impl
->func
->args
->count
;
296 zf
->handler
= call
->closure
;
297 zf
->arg_info
= psi_internal_arginfo(impl
);
301 for (i
= 0; i
< C
->decls
->count
; ++i
) {
302 decl
*decl
= C
->decls
->list
[i
];
308 PSI_LibjitCallAlloc(C
, decl
);
311 jit_context_build_end(ctx
->jit
);
316 static void psi_jit_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
317 PSI_LibjitCall
*call
= decl_call
->info
;
320 jit_type_t signature
;
321 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
322 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
324 for (i
= 0; i
< nfixedargs
; ++i
) {
325 params
[i
] = call
->params
[i
];
326 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
328 for (i
= 0; i
< va
->args
->count
; ++i
) {
329 params
[nfixedargs
+ i
] = psi_jit_impl_type(va
->types
[i
]);
330 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
333 signature
= jit_type_create_signature(
334 jit_type_get_abi(call
->signature
),
335 jit_type_get_return(call
->signature
),
336 (jit_type_t
*) params
, ntotalargs
, 1);
337 ZEND_ASSERT(signature
);
339 jit_apply(signature
, decl_call
->sym
, ¶ms
[ntotalargs
+ 1],
340 nfixedargs
, *decl_call
->rval
);
341 jit_type_free(signature
);
344 jit_apply(call
->signature
, decl_call
->sym
, decl_call
->args
,
345 decl_call
->argc
, *decl_call
->rval
);
349 static PSI_ContextOps ops
= {
356 PSI_ContextOps
*PSI_Libjit(void)
361 #endif /* HAVE_LIBJIT */