273c47890818a95cf16770f5d4fd124dbc391bcf
[m6w6/ext-psi] / src / libjit.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include "php.h"
6
7 #ifdef HAVE_LIBJIT
8
9 #include "php_psi.h"
10 #include "libjit.h"
11
12 #include <jit/jit.h>
13
14 static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data);
15
16 static inline jit_abi_t psi_jit_abi(const char *convention) {
17 return jit_abi_cdecl;
18 }
19 static inline jit_type_t psi_jit_token_type(token_t t) {
20 switch (t) {
21 case PSI_T_VOID:
22 return jit_type_void;
23 case PSI_T_INT8:
24 return jit_type_sbyte;
25 case PSI_T_UINT8:
26 return jit_type_ubyte;
27 case PSI_T_INT16:
28 return jit_type_short;
29 case PSI_T_UINT16:
30 return jit_type_ushort;
31 case PSI_T_INT32:
32 return jit_type_int;
33 case PSI_T_UINT32:
34 return jit_type_uint;
35 case PSI_T_INT64:
36 return jit_type_long;
37 case PSI_T_UINT64:
38 return jit_type_ulong;
39 case PSI_T_BOOL:
40 return jit_type_sys_bool;
41 case PSI_T_FLOAT:
42 return jit_type_sys_float;
43 case PSI_T_DOUBLE:
44 return jit_type_sys_double;
45 case PSI_T_POINTER:
46 return jit_type_void_ptr;
47 EMPTY_SWITCH_DEFAULT_CASE();
48 }
49 }
50 static inline jit_type_t psi_jit_impl_type(token_t impl_type) {
51 switch (impl_type) {
52 case PSI_T_BOOL:
53 return jit_type_sbyte;
54 case PSI_T_INT:
55 return jit_type_long;
56 case PSI_T_STRING:
57 return jit_type_void_ptr;
58 case PSI_T_FLOAT:
59 case PSI_T_DOUBLE:
60 return jit_type_sys_double;
61 EMPTY_SWITCH_DEFAULT_CASE();
62 }
63 }
64 static inline jit_type_t psi_jit_decl_type(decl_type *type) {
65 return psi_jit_token_type(real_decl_type(type)->type);
66 }
67 static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg) {
68 if (darg->var->pointer_level) {
69 return jit_type_void_ptr;
70 } else {
71 return psi_jit_decl_type(darg->type);
72 }
73 }
74
75 typedef struct PSI_LibjitContext {
76 jit_context_t jit;
77 jit_type_t signature;
78 struct {
79 struct PSI_LibjitData **list;
80 size_t count;
81 } data;
82 } PSI_LibjitContext;
83
84 typedef struct PSI_LibjitCall {
85 void *closure;
86 jit_type_t signature;
87 void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
88 } PSI_LibjitCall;
89
90 typedef struct PSI_LibjitData {
91 PSI_LibjitContext *context;
92 impl *impl;
93 zend_internal_arg_info *arginfo;
94 } PSI_LibjitData;
95
96 static inline PSI_LibjitCall *PSI_LibjitCallAlloc(PSI_Context *C, decl *decl) {
97 size_t i, c = decl->args ? decl->args->count : 0;
98 PSI_LibjitCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
99
100 for (i = 0; i < c; ++i) {
101 call->params[i] = psi_jit_decl_arg_type(decl->args->args[i]);
102 }
103 call->params[c] = NULL;
104
105 decl->call.info = call;
106 decl->call.rval = decl->func->ptr;
107 decl->call.argc = c;
108 decl->call.args = (void **) &call->params[c+1];
109
110 call->signature = jit_type_create_signature(
111 psi_jit_abi(decl->abi->convention),
112 psi_jit_decl_arg_type(decl->func),
113 (jit_type_t *) call->params, c, 1);
114 return call;
115 }
116
117 static inline void PSI_LibjitCallInitClosure(PSI_Context *C, PSI_LibjitCall *call, impl *impl) {
118 PSI_LibjitContext *context = C->context;
119 call->closure = jit_closure_create(context->jit, context->signature,
120 &psi_jit_handler, impl);
121 }
122
123 static inline void PSI_LibjitCallFree(PSI_LibjitCall *call) {
124 jit_type_free(call->signature);
125 free(call);
126 }
127
128 static inline PSI_LibjitContext *PSI_LibjitContextInit(PSI_LibjitContext *L) {
129 jit_type_t params[] = {
130 jit_type_void_ptr,
131 jit_type_void_ptr
132 };
133
134 if (!L) {
135 L = malloc(sizeof(*L));
136 }
137 memset(L, 0, sizeof(*L));
138
139 L->jit = jit_context_create();
140 L->signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void,
141 params, 2, 1);
142
143 return L;
144 }
145
146 static inline void PSI_LibjitContextDtor(PSI_LibjitContext *L) {
147 jit_type_free(L->signature);
148 jit_context_destroy(L->jit);
149 }
150
151 static inline void PSI_LibjitContextFree(PSI_LibjitContext **L) {
152 if (*L) {
153 PSI_LibjitContextDtor(*L);
154 free(*L);
155 *L = NULL;
156 }
157 }
158
159 static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data)
160 {
161 psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
162 }
163
164 static void psi_jit_init(PSI_Context *C)
165 {
166 C->context = PSI_LibjitContextInit(NULL);
167 }
168
169 static void psi_jit_dtor(PSI_Context *C)
170 {
171 if (C->decls) {
172 size_t i;
173
174 for (i = 0; i < C->decls->count; ++i) {
175 decl *decl = C->decls->list[i];
176
177 PSI_LibjitCallFree(decl->call.info);
178 }
179 }
180 PSI_LibjitContextFree((void *) &C->context);
181 }
182
183 static zend_function_entry *psi_jit_compile(PSI_Context *C)
184 {
185 size_t i, j = 0;
186 zend_function_entry *zfe;
187 PSI_LibjitContext *ctx = C->context;
188
189 if (!C->impls) {
190 return NULL;
191 }
192
193 zfe = calloc(C->impls->count + 1, sizeof(*zfe));
194 jit_context_build_start(ctx->jit);
195
196 for (i = 0; i < C->impls->count; ++i) {
197 zend_function_entry *zf = &zfe[j];
198 PSI_LibjitCall *call;
199 impl *impl = C->impls->list[i];
200
201 if (!impl->decl) {
202 continue;
203 }
204
205 call = PSI_LibjitCallAlloc(C, impl->decl);
206 PSI_LibjitCallInitClosure(C, call, impl);
207
208 zf->fname = impl->func->name + (impl->func->name[0] == '\\');
209 zf->num_args = impl->func->args->count;
210 zf->handler = call->closure;
211 zf->arg_info = psi_internal_arginfo(impl);
212 ++j;
213 }
214
215 for (i = 0; i < C->decls->count; ++i) {
216 decl *decl = C->decls->list[i];
217
218 if (decl->impl) {
219 continue;
220 }
221
222 PSI_LibjitCallAlloc(C, decl);
223 }
224
225 jit_context_build_end(ctx->jit);
226
227 return zfe;
228 }
229
230 static void psi_jit_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
231 PSI_LibjitCall *call = decl_call->info;
232
233 if (va) {
234 jit_type_t signature;
235 size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
236 void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
237
238 for (i = 0; i < nfixedargs; ++i) {
239 params[i] = call->params[i];
240 params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
241 }
242 for (i = 0; i < va->args->count; ++i) {
243 params[nfixedargs + i] = psi_jit_impl_type(va->types[i]);
244 params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
245 }
246
247 signature = jit_type_create_signature(
248 jit_type_get_abi(call->signature),
249 jit_type_get_return(call->signature),
250 (jit_type_t *) params, ntotalargs, 1);
251 ZEND_ASSERT(signature);
252
253 jit_apply(signature, decl_call->sym, &params[ntotalargs + 1],
254 nfixedargs, decl_call->rval);
255 jit_type_free(signature);
256 free(params);
257 } else {
258 jit_apply(call->signature, decl_call->sym, decl_call->args,
259 decl_call->argc, decl_call->rval);
260 }
261 }
262
263 static PSI_ContextOps ops = {
264 psi_jit_init,
265 psi_jit_dtor,
266 psi_jit_compile,
267 psi_jit_call,
268 };
269
270 PSI_ContextOps *PSI_Libjit(void)
271 {
272 return &ops;
273 }
274
275 #endif /* HAVE_LIBJIT */