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
;
77 static unsigned psi_jit_struct_type_elements(decl_struct
*strct
, jit_type_t
**fields
) {
78 size_t i
, j
, argc
= strct
->args
->count
<< 2, nels
= 0, offset
= 0, align
, padding
;
79 *fields
= calloc(argc
, sizeof(*fields
));
81 for (i
= 0; i
< strct
->args
->count
; ++i
) {
82 decl_arg
*darg
= strct
->args
->args
[i
];
83 jit_type_t type
= jit_type_copy(psi_jit_decl_arg_type(darg
));
85 if (darg
->layout
->pos
> offset
) {
86 padding
= darg
->layout
->pos
- offset
;
87 align
= ((padding
- 1) | (jit_type_get_alignment(type
) - 1)) + 1;
88 if (align
>= padding
) {
97 for (j
= 0; j
< padding
; ++j
) {
98 jit_type_t pad
= jit_type_copy(jit_type_sys_char
);
100 ZEND_ASSERT(nels
+ 1 < argc
);
101 (*fields
)[nels
++] = pad
;
105 ZEND_ASSERT(nels
+ 1 < argc
);
106 (*fields
)[nels
++] = type
;
108 offset
+= MAX(align
, padding
) + darg
->layout
->len
;
111 ZEND_ASSERT(offset
<= strct
->size
);
112 if (offset
< strct
->size
) {
113 padding
= strct
->size
- offset
;
114 for (j
= 0; j
< padding
; ++j
) {
115 jit_type_t pad
= jit_type_copy(jit_type_sys_char
);
117 ZEND_ASSERT(nels
+ 1 < argc
);
118 (*fields
)[nels
++] = pad
;
124 static inline jit_type_t
psi_jit_decl_type(decl_type
*type
) {
125 decl_type
*real
= real_decl_type(type
);
127 if (real
->type
== PSI_T_STRUCT
) {
128 if (!real
->strct
->engine
.type
) {
130 jit_type_t strct
, *fields
= NULL
;
132 count
= psi_jit_struct_type_elements(real
->strct
, &fields
);
133 strct
= jit_type_create_struct(fields
, count
, 0);
135 real
->strct
->engine
.type
= strct
;
136 real
->strct
->engine
.dtor
= psi_jit_struct_type_dtor
;
139 return real
->strct
->engine
.type
;
141 return psi_jit_token_type(real
->type
);
143 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
) {
144 if (darg
->var
->pointer_level
) {
145 return jit_type_void_ptr
;
147 return psi_jit_decl_type(darg
->type
);
151 typedef struct PSI_LibjitContext
{
153 jit_type_t signature
;
155 struct PSI_LibjitData
**list
;
160 typedef struct PSI_LibjitCall
{
162 jit_type_t signature
;
163 void *params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
166 typedef struct PSI_LibjitData
{
167 PSI_LibjitContext
*context
;
169 zend_internal_arg_info
*arginfo
;
172 static inline PSI_LibjitCall
*PSI_LibjitCallAlloc(PSI_Context
*C
, decl
*decl
) {
173 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
174 PSI_LibjitCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
176 for (i
= 0; i
< c
; ++i
) {
177 call
->params
[i
] = psi_jit_decl_arg_type(decl
->args
->args
[i
]);
179 call
->params
[c
] = NULL
;
181 decl
->call
.info
= call
;
182 decl
->call
.rval
= &decl
->func
->ptr
;
184 decl
->call
.args
= (void **) &call
->params
[c
+1];
186 call
->signature
= jit_type_create_signature(
187 psi_jit_abi(decl
->abi
->convention
),
188 psi_jit_decl_arg_type(decl
->func
),
189 (jit_type_t
*) call
->params
, c
, 1);
193 static inline void PSI_LibjitCallInitClosure(PSI_Context
*C
, PSI_LibjitCall
*call
, impl
*impl
) {
194 PSI_LibjitContext
*context
= C
->context
;
195 call
->closure
= jit_closure_create(context
->jit
, context
->signature
,
196 &psi_jit_handler
, impl
);
199 static inline void PSI_LibjitCallFree(PSI_LibjitCall
*call
) {
200 jit_type_free(call
->signature
);
204 static inline PSI_LibjitContext
*PSI_LibjitContextInit(PSI_LibjitContext
*L
) {
205 jit_type_t params
[] = {
211 L
= malloc(sizeof(*L
));
213 memset(L
, 0, sizeof(*L
));
215 L
->jit
= jit_context_create();
216 L
->signature
= jit_type_create_signature(jit_abi_cdecl
, jit_type_void
,
222 static inline void PSI_LibjitContextDtor(PSI_LibjitContext
*L
) {
223 jit_type_free(L
->signature
);
224 jit_context_destroy(L
->jit
);
227 static inline void PSI_LibjitContextFree(PSI_LibjitContext
**L
) {
229 PSI_LibjitContextDtor(*L
);
235 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
)
237 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
240 static void psi_jit_init(PSI_Context
*C
)
242 C
->context
= PSI_LibjitContextInit(NULL
);
245 static void psi_jit_dtor(PSI_Context
*C
)
250 for (i
= 0; i
< C
->decls
->count
; ++i
) {
251 decl
*decl
= C
->decls
->list
[i
];
253 PSI_LibjitCallFree(decl
->call
.info
);
256 PSI_LibjitContextFree((void *) &C
->context
);
259 static zend_function_entry
*psi_jit_compile(PSI_Context
*C
)
262 zend_function_entry
*zfe
;
263 PSI_LibjitContext
*ctx
= C
->context
;
269 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
270 jit_context_build_start(ctx
->jit
);
272 for (i
= 0; i
< C
->impls
->count
; ++i
) {
273 zend_function_entry
*zf
= &zfe
[j
];
274 PSI_LibjitCall
*call
;
275 impl
*impl
= C
->impls
->list
[i
];
281 call
= PSI_LibjitCallAlloc(C
, impl
->decl
);
282 PSI_LibjitCallInitClosure(C
, call
, impl
);
284 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
285 zf
->num_args
= impl
->func
->args
->count
;
286 zf
->handler
= call
->closure
;
287 zf
->arg_info
= psi_internal_arginfo(impl
);
291 for (i
= 0; i
< C
->decls
->count
; ++i
) {
292 decl
*decl
= C
->decls
->list
[i
];
298 PSI_LibjitCallAlloc(C
, decl
);
301 jit_context_build_end(ctx
->jit
);
306 static void psi_jit_call(PSI_Context
*C
, decl_callinfo
*decl_call
, impl_vararg
*va
) {
307 PSI_LibjitCall
*call
= decl_call
->info
;
310 jit_type_t signature
;
311 size_t i
, nfixedargs
= decl_call
->argc
, ntotalargs
= nfixedargs
+ va
->args
->count
;
312 void **params
= calloc(2 * ntotalargs
+ 2, sizeof(void *));
314 for (i
= 0; i
< nfixedargs
; ++i
) {
315 params
[i
] = call
->params
[i
];
316 params
[i
+ ntotalargs
+ 1] = call
->params
[i
+ nfixedargs
+ 1];
318 for (i
= 0; i
< va
->args
->count
; ++i
) {
319 params
[nfixedargs
+ i
] = psi_jit_impl_type(va
->types
[i
]);
320 params
[nfixedargs
+ i
+ ntotalargs
+ 1] = &va
->values
[i
];
323 signature
= jit_type_create_signature(
324 jit_type_get_abi(call
->signature
),
325 jit_type_get_return(call
->signature
),
326 (jit_type_t
*) params
, ntotalargs
, 1);
327 ZEND_ASSERT(signature
);
329 jit_apply(signature
, decl_call
->sym
, ¶ms
[ntotalargs
+ 1],
330 nfixedargs
, *decl_call
->rval
);
331 jit_type_free(signature
);
334 jit_apply(call
->signature
, decl_call
->sym
, decl_call
->args
,
335 decl_call
->argc
, *decl_call
->rval
);
339 static PSI_ContextOps ops
= {
346 PSI_ContextOps
*PSI_Libjit(void)
351 #endif /* HAVE_LIBJIT */