736b54dbbbd67e7a2a61b12b44b9cba7934d90b3
[m6w6/ext-psi] / src / libjit.c
1 #include "php.h"
2 #include "php_psi.h"
3 #include "libjit.h"
4
5 #include <jit/jit.h>
6
7 static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data);
8
9 static inline jit_abi_t psi_jit_abi(const char *convention) {
10 return jit_abi_cdecl;
11 }
12 static inline jit_type_t psi_jit_token_type(token_t t) {
13 switch (t) {
14 case PSI_T_VOID:
15 return jit_type_void;
16 case PSI_T_INT8:
17 return jit_type_sbyte;
18 case PSI_T_UINT8:
19 return jit_type_ubyte;
20 case PSI_T_INT16:
21 return jit_type_short;
22 case PSI_T_UINT16:
23 return jit_type_ushort;
24 case PSI_T_INT32:
25 return jit_type_int;
26 case PSI_T_UINT32:
27 return jit_type_uint;
28 case PSI_T_INT64:
29 return jit_type_long;
30 case PSI_T_UINT64:
31 return jit_type_ulong;
32 case PSI_T_BOOL:
33 return jit_type_sys_bool;
34 case PSI_T_FLOAT:
35 return jit_type_sys_float;
36 case PSI_T_DOUBLE:
37 return jit_type_sys_double;
38 EMPTY_SWITCH_DEFAULT_CASE();
39 }
40 }
41 static inline jit_type_t psi_jit_decl_type(decl_type *type) {
42 return psi_jit_token_type(real_decl_type(type)->type);
43 }
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;
47 } else {
48 return psi_jit_decl_type(darg->type);
49 }
50 }
51
52 typedef struct PSI_LibjitContext {
53 jit_context_t jit;
54 jit_type_t signature;
55 struct {
56 struct PSI_LibjitData **list;
57 size_t count;
58 } data;
59 } PSI_LibjitContext;
60
61 typedef struct PSI_LibjitCall {
62 void *closure;
63 jit_type_t signature;
64 jit_type_t params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
65 } PSI_LibjitCall;
66
67 typedef struct PSI_LibjitData {
68 PSI_LibjitContext *context;
69 impl *impl;
70 zend_internal_arg_info *arginfo;
71 } PSI_LibjitData;
72
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 *));
76
77 for (i = 0; i < c; ++i) {
78 call->params[i] = psi_jit_decl_arg_type(decl->args->args[i]);
79 }
80 call->params[c] = NULL;
81
82 decl->call.info = call;
83 decl->call.args = (void **) &call->params[c+1];
84
85 call->signature = jit_type_create_signature(
86 psi_jit_abi(decl->abi->convention),
87 psi_jit_decl_arg_type(decl->func),
88 call->params, c, 1);
89 return call;
90 }
91
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);
96 }
97
98 static inline void PSI_LibjitCallFree(PSI_LibjitCall *call) {
99 jit_type_free(call->signature);
100 }
101
102 static inline PSI_LibjitContext *PSI_LibjitContextInit(PSI_LibjitContext *L) {
103 jit_type_t params[] = {
104 jit_type_void_ptr,
105 jit_type_void_ptr
106 };
107
108 if (!L) {
109 L = malloc(sizeof(*L));
110 }
111 memset(L, 0, sizeof(*L));
112
113 L->jit = jit_context_create();
114 L->signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void,
115 params, 2, 1);
116
117 return L;
118 }
119
120 static inline void PSI_LibjitContextDtor(PSI_LibjitContext *L) {
121 jit_type_free(L->signature);
122 jit_context_destroy(L->jit);
123 }
124
125 static inline void PSI_LibjitContextFree(PSI_LibjitContext **L) {
126 if (*L) {
127 PSI_LibjitContextDtor(*L);
128 free(*L);
129 *L = NULL;
130 }
131 }
132
133 static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data)
134 {
135 psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
136 }
137
138 static void psi_jit_init(PSI_Context *C)
139 {
140 C->context = PSI_LibjitContextInit(NULL);
141 }
142
143 static void psi_jit_dtor(PSI_Context *C)
144 {
145 size_t i;
146
147 for (i = 0; i < C->decls->count; ++i) {
148 decl *decl = C->decls->list[i];
149
150 PSI_LibjitCallFree(decl->call.info);
151 }
152 PSI_LibjitContextFree((void *) &C->context);
153 }
154
155 static zend_function_entry *psi_jit_compile(PSI_Context *C)
156 {
157 size_t i, j = 0;
158 zend_function_entry *zfe;
159 PSI_LibjitContext *ctx = C->context;
160
161 if (!C->impls) {
162 return NULL;
163 }
164
165 zfe = calloc(C->impls->count + 1, sizeof(*zfe));
166 jit_context_build_start(ctx->jit);
167
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];
172
173 if (!impl->decl) {
174 continue;
175 }
176
177 call = PSI_LibjitCallAlloc(C, impl->decl);
178 PSI_LibjitCallInitClosure(C, call, impl);
179
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);
184 ++j;
185 }
186
187 for (i = 0; i < C->decls->count; ++i) {
188 decl *decl = C->decls->list[i];
189
190 if (decl->impl) {
191 continue;
192 }
193
194 PSI_LibjitCallAlloc(C, decl);
195 }
196
197 jit_context_build_end(ctx->jit);
198
199 return zfe;
200 }
201
202 static void psi_jit_call(PSI_Context *C, impl_val *ret_val, decl *decl) {
203 PSI_LibjitCall *call = decl->call.info;
204
205 jit_apply(call->signature, decl->call.sym, decl->call.args,
206 decl->args->count, ret_val);
207 }
208
209 static PSI_ContextOps ops = {
210 psi_jit_init,
211 psi_jit_dtor,
212 psi_jit_compile,
213 psi_jit_call,
214 };
215
216 PSI_ContextOps *PSI_Libjit(void)
217 {
218 return &ops;
219 }