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_decl_type(decl_type
*type
) {
49 return psi_jit_token_type(real_decl_type(type
)->type
);
51 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
) {
52 if (darg
->var
->pointer_level
) {
53 return jit_type_void_ptr
;
55 return psi_jit_decl_type(darg
->type
);
59 typedef struct PSI_LibjitContext
{
63 struct PSI_LibjitData
**list
;
68 typedef struct PSI_LibjitCall
{
71 jit_type_t params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
74 typedef struct PSI_LibjitData
{
75 PSI_LibjitContext
*context
;
77 zend_internal_arg_info
*arginfo
;
80 static inline PSI_LibjitCall
*PSI_LibjitCallAlloc(PSI_Context
*C
, decl
*decl
) {
81 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
82 PSI_LibjitCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
84 for (i
= 0; i
< c
; ++i
) {
85 call
->params
[i
] = psi_jit_decl_arg_type(decl
->args
->args
[i
]);
87 call
->params
[c
] = NULL
;
89 decl
->call
.info
= call
;
90 decl
->call
.args
= (void **) &call
->params
[c
+1];
92 call
->signature
= jit_type_create_signature(
93 psi_jit_abi(decl
->abi
->convention
),
94 psi_jit_decl_arg_type(decl
->func
),
99 static inline void PSI_LibjitCallInitClosure(PSI_Context
*C
, PSI_LibjitCall
*call
, impl
*impl
) {
100 PSI_LibjitContext
*context
= C
->context
;
101 call
->closure
= jit_closure_create(context
->jit
, context
->signature
,
102 &psi_jit_handler
, impl
);
105 static inline void PSI_LibjitCallFree(PSI_LibjitCall
*call
) {
106 jit_type_free(call
->signature
);
109 static inline PSI_LibjitContext
*PSI_LibjitContextInit(PSI_LibjitContext
*L
) {
110 jit_type_t params
[] = {
116 L
= malloc(sizeof(*L
));
118 memset(L
, 0, sizeof(*L
));
120 L
->jit
= jit_context_create();
121 L
->signature
= jit_type_create_signature(jit_abi_cdecl
, jit_type_void
,
127 static inline void PSI_LibjitContextDtor(PSI_LibjitContext
*L
) {
128 jit_type_free(L
->signature
);
129 jit_context_destroy(L
->jit
);
132 static inline void PSI_LibjitContextFree(PSI_LibjitContext
**L
) {
134 PSI_LibjitContextDtor(*L
);
140 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
)
142 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
145 static void psi_jit_init(PSI_Context
*C
)
147 C
->context
= PSI_LibjitContextInit(NULL
);
150 static void psi_jit_dtor(PSI_Context
*C
)
155 for (i
= 0; i
< C
->decls
->count
; ++i
) {
156 decl
*decl
= C
->decls
->list
[i
];
158 PSI_LibjitCallFree(decl
->call
.info
);
161 PSI_LibjitContextFree((void *) &C
->context
);
164 static zend_function_entry
*psi_jit_compile(PSI_Context
*C
)
167 zend_function_entry
*zfe
;
168 PSI_LibjitContext
*ctx
= C
->context
;
174 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
175 jit_context_build_start(ctx
->jit
);
177 for (i
= 0; i
< C
->impls
->count
; ++i
) {
178 zend_function_entry
*zf
= &zfe
[j
];
179 PSI_LibjitCall
*call
;
180 impl
*impl
= C
->impls
->list
[i
];
186 call
= PSI_LibjitCallAlloc(C
, impl
->decl
);
187 PSI_LibjitCallInitClosure(C
, call
, impl
);
189 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
190 zf
->num_args
= impl
->func
->args
->count
;
191 zf
->handler
= call
->closure
;
192 zf
->arg_info
= psi_internal_arginfo(impl
);
196 for (i
= 0; i
< C
->decls
->count
; ++i
) {
197 decl
*decl
= C
->decls
->list
[i
];
203 PSI_LibjitCallAlloc(C
, decl
);
206 jit_context_build_end(ctx
->jit
);
211 static void psi_jit_call(PSI_Context
*C
, impl_val
*ret_val
, decl
*decl
) {
212 PSI_LibjitCall
*call
= decl
->call
.info
;
214 jit_apply(call
->signature
, decl
->call
.sym
, decl
->call
.args
,
215 decl
->args
->count
, ret_val
);
218 static PSI_ContextOps ops
= {
225 PSI_ContextOps
*PSI_Libjit(void)
230 #endif /* HAVE_LIBJIT */