7 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
);
9 static inline jit_abi_t
psi_jit_abi(const char *convention
) {
12 static inline jit_type_t
psi_jit_token_type(token_t t
) {
17 return jit_type_sbyte
;
19 return jit_type_ubyte
;
21 return jit_type_short
;
23 return jit_type_ushort
;
31 return jit_type_ulong
;
33 return jit_type_sys_bool
;
35 return jit_type_sys_float
;
37 return jit_type_sys_double
;
38 EMPTY_SWITCH_DEFAULT_CASE();
41 static inline jit_type_t
psi_jit_decl_type(decl_type
*type
) {
42 return psi_jit_token_type(real_decl_type(type
)->type
);
44 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
) {
45 if (darg
->var
->pointer_level
) {
46 return jit_type_void_ptr
;
48 return psi_jit_decl_type(darg
->type
);
52 typedef struct PSI_LibjitContext
{
56 struct PSI_LibjitData
**list
;
61 typedef struct PSI_LibjitCall
{
64 jit_type_t params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
67 typedef struct PSI_LibjitData
{
68 PSI_LibjitContext
*context
;
70 zend_internal_arg_info
*arginfo
;
73 static inline PSI_LibjitCall
*PSI_LibjitCallAlloc(PSI_Context
*C
, decl
*decl
) {
74 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
75 PSI_LibjitCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
77 for (i
= 0; i
< c
; ++i
) {
78 call
->params
[i
] = psi_jit_decl_arg_type(decl
->args
->args
[i
]);
80 call
->params
[c
] = NULL
;
82 decl
->call
.info
= call
;
83 decl
->call
.args
= (void **) &call
->params
[c
+1];
85 call
->signature
= jit_type_create_signature(
86 psi_jit_abi(decl
->abi
->convention
),
87 psi_jit_decl_arg_type(decl
->func
),
92 static inline void PSI_LibjitCallInitClosure(PSI_Context
*C
, PSI_LibjitCall
*call
, impl
*impl
) {
93 PSI_LibjitContext
*context
= C
->context
;
94 call
->closure
= jit_closure_create(context
->jit
, context
->signature
,
95 &psi_jit_handler
, impl
);
98 static inline void PSI_LibjitCallFree(PSI_LibjitCall
*call
) {
99 jit_type_free(call
->signature
);
102 static inline PSI_LibjitContext
*PSI_LibjitContextInit(PSI_LibjitContext
*L
) {
103 jit_type_t params
[] = {
109 L
= malloc(sizeof(*L
));
111 memset(L
, 0, sizeof(*L
));
113 L
->jit
= jit_context_create();
114 L
->signature
= jit_type_create_signature(jit_abi_cdecl
, jit_type_void
,
120 static inline void PSI_LibjitContextDtor(PSI_LibjitContext
*L
) {
121 jit_type_free(L
->signature
);
122 jit_context_destroy(L
->jit
);
125 static inline void PSI_LibjitContextFree(PSI_LibjitContext
**L
) {
127 PSI_LibjitContextDtor(*L
);
133 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
)
135 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
138 static void psi_jit_init(PSI_Context
*C
)
140 C
->context
= PSI_LibjitContextInit(NULL
);
143 static void psi_jit_dtor(PSI_Context
*C
)
147 for (i
= 0; i
< C
->decls
->count
; ++i
) {
148 decl
*decl
= C
->decls
->list
[i
];
150 PSI_LibjitCallFree(decl
->call
.info
);
152 PSI_LibjitContextFree((void *) &C
->context
);
155 static zend_function_entry
*psi_jit_compile(PSI_Context
*C
)
158 zend_function_entry
*zfe
;
159 PSI_LibjitContext
*ctx
= C
->context
;
165 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
166 jit_context_build_start(ctx
->jit
);
168 for (i
= 0; i
< C
->impls
->count
; ++i
) {
169 zend_function_entry
*zf
= &zfe
[j
];
170 PSI_LibjitCall
*call
;
171 impl
*impl
= C
->impls
->list
[i
];
177 call
= PSI_LibjitCallAlloc(C
, impl
->decl
);
178 PSI_LibjitCallInitClosure(C
, call
, impl
);
180 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
181 zf
->num_args
= impl
->func
->args
->count
;
182 zf
->handler
= call
->closure
;
183 zf
->arg_info
= psi_internal_arginfo(impl
);
187 for (i
= 0; i
< C
->decls
->count
; ++i
) {
188 decl
*decl
= C
->decls
->list
[i
];
194 PSI_LibjitCallAlloc(C
, decl
);
197 jit_context_build_end(ctx
->jit
);
202 static void psi_jit_call(PSI_Context
*C
, impl_val
*ret_val
, decl
*decl
) {
203 PSI_LibjitCall
*call
= decl
->call
.info
;
205 jit_apply(call
->signature
, decl
->call
.sym
, decl
->call
.args
,
206 decl
->args
->count
, ret_val
);
209 static PSI_ContextOps ops
= {
216 PSI_ContextOps
*PSI_Libjit(void)