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
) {
20 return jit_type_sbyte
;
22 return jit_type_ubyte
;
24 return jit_type_short
;
26 return jit_type_ushort
;
34 return jit_type_ulong
;
36 return jit_type_sys_bool
;
38 return jit_type_sys_int
;
40 return jit_type_sys_float
;
42 return jit_type_sys_double
;
45 static inline jit_type_t
psi_jit_decl_type(decl_type
*type
) {
46 return psi_jit_token_type(real_decl_type(type
)->type
);
48 static inline jit_type_t
psi_jit_decl_arg_type(decl_arg
*darg
) {
49 if (darg
->var
->pointer_level
) {
50 return jit_type_void_ptr
;
52 return psi_jit_decl_type(darg
->type
);
56 typedef struct PSI_LibjitContext
{
60 struct PSI_LibjitData
**list
;
65 typedef struct PSI_LibjitCall
{
68 jit_type_t params
[1]; /* [type1, type2, NULL, arg1, arg2] ... */
71 typedef struct PSI_LibjitData
{
72 PSI_LibjitContext
*context
;
74 zend_internal_arg_info
*arginfo
;
77 static inline PSI_LibjitCall
*PSI_LibjitCallAlloc(PSI_Context
*C
, decl
*decl
) {
78 size_t i
, c
= decl
->args
? decl
->args
->count
: 0;
79 PSI_LibjitCall
*call
= calloc(1, sizeof(*call
) + 2 * c
* sizeof(void *));
81 for (i
= 0; i
< c
; ++i
) {
82 call
->params
[i
] = psi_jit_decl_arg_type(decl
->args
->args
[i
]);
84 call
->params
[c
] = NULL
;
86 decl
->call
.info
= call
;
87 decl
->call
.args
= (void **) &call
->params
[c
+1];
89 call
->signature
= jit_type_create_signature(
90 psi_jit_abi(decl
->abi
->convention
),
91 psi_jit_decl_arg_type(decl
->func
),
96 static inline void PSI_LibjitCallInitClosure(PSI_Context
*C
, PSI_LibjitCall
*call
, impl
*impl
) {
97 PSI_LibjitContext
*context
= C
->context
;
98 call
->closure
= jit_closure_create(context
->jit
, context
->signature
,
99 &psi_jit_handler
, impl
);
102 static inline void PSI_LibjitCallFree(PSI_LibjitCall
*call
) {
103 jit_type_free(call
->signature
);
106 static inline PSI_LibjitContext
*PSI_LibjitContextInit(PSI_LibjitContext
*L
) {
107 jit_type_t params
[] = {
113 L
= malloc(sizeof(*L
));
115 memset(L
, 0, sizeof(*L
));
117 L
->jit
= jit_context_create();
118 L
->signature
= jit_type_create_signature(jit_abi_cdecl
, jit_type_void
,
124 static inline void PSI_LibjitContextDtor(PSI_LibjitContext
*L
) {
125 jit_type_free(L
->signature
);
126 jit_context_destroy(L
->jit
);
129 static inline void PSI_LibjitContextFree(PSI_LibjitContext
**L
) {
131 PSI_LibjitContextDtor(*L
);
137 static void psi_jit_handler(jit_type_t _sig
, void *result
, void **_args
, void *_data
)
139 psi_call(*(zend_execute_data
**)_args
[0], *(zval
**)_args
[1], _data
);
142 static void psi_jit_init(PSI_Context
*C
)
144 C
->context
= PSI_LibjitContextInit(NULL
);
147 static void psi_jit_dtor(PSI_Context
*C
)
151 for (i
= 0; i
< C
->decls
->count
; ++i
) {
152 decl
*decl
= C
->decls
->list
[i
];
154 PSI_LibjitCallFree(decl
->call
.info
);
156 PSI_LibjitContextFree((void *) &C
->context
);
159 static zend_function_entry
*psi_jit_compile(PSI_Context
*C
)
162 zend_function_entry
*zfe
;
163 PSI_LibjitContext
*ctx
= C
->context
;
169 zfe
= calloc(C
->impls
->count
+ 1, sizeof(*zfe
));
170 jit_context_build_start(ctx
->jit
);
172 for (i
= 0; i
< C
->impls
->count
; ++i
) {
173 zend_function_entry
*zf
= &zfe
[j
];
174 PSI_LibjitCall
*call
;
175 impl
*impl
= C
->impls
->list
[i
];
181 call
= PSI_LibjitCallAlloc(C
, impl
->decl
);
182 PSI_LibjitCallInitClosure(C
, call
, impl
);
184 zf
->fname
= impl
->func
->name
+ (impl
->func
->name
[0] == '\\');
185 zf
->num_args
= impl
->func
->args
->count
;
186 zf
->handler
= call
->closure
;
187 zf
->arg_info
= psi_internal_arginfo(impl
);
191 for (i
= 0; i
< C
->decls
->count
; ++i
) {
192 decl
*decl
= C
->decls
->list
[i
];
198 PSI_LibjitCallAlloc(C
, decl
);
201 jit_context_build_end(ctx
->jit
);
206 static void psi_jit_call(PSI_Context
*C
, impl_val
*ret_val
, decl
*decl
) {
207 PSI_LibjitCall
*call
= decl
->call
.info
;
209 jit_apply(call
->signature
, decl
->call
.sym
, decl
->call
.args
,
210 decl
->args
->count
, ret_val
);
213 static PSI_ContextOps ops
= {
220 PSI_ContextOps
*PSI_Libjit(void)