dd843949cd90afc92c9bc3ca8ee721dedae7fc63
[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 static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg);
16
17 static inline jit_abi_t psi_jit_abi(const char *convention) {
18 return jit_abi_cdecl;
19 }
20 static inline jit_type_t psi_jit_token_type(token_t t) {
21 switch (t) {
22 default:
23 ZEND_ASSERT(0);
24 /* no break */
25 case PSI_T_VOID:
26 return jit_type_void;
27 case PSI_T_INT8:
28 return jit_type_sbyte;
29 case PSI_T_UINT8:
30 return jit_type_ubyte;
31 case PSI_T_INT16:
32 return jit_type_short;
33 case PSI_T_UINT16:
34 return jit_type_ushort;
35 case PSI_T_INT32:
36 return jit_type_int;
37 case PSI_T_UINT32:
38 return jit_type_uint;
39 case PSI_T_INT64:
40 return jit_type_long;
41 case PSI_T_UINT64:
42 return jit_type_ulong;
43 case PSI_T_BOOL:
44 return jit_type_sys_bool;
45 case PSI_T_INT:
46 return jit_type_sys_int;
47 case PSI_T_LONG:
48 return jit_type_sys_long;
49 case PSI_T_FLOAT:
50 return jit_type_sys_float;
51 case PSI_T_DOUBLE:
52 return jit_type_sys_double;
53 case PSI_T_POINTER:
54 return jit_type_void_ptr;
55 }
56 }
57 static inline jit_type_t psi_jit_impl_type(token_t impl_type) {
58 switch (impl_type) {
59 case PSI_T_BOOL:
60 return jit_type_sbyte;
61 case PSI_T_INT:
62 return jit_type_long;
63 case PSI_T_STRING:
64 return jit_type_void_ptr;
65 case PSI_T_FLOAT:
66 case PSI_T_DOUBLE:
67 return jit_type_sys_double;
68 EMPTY_SWITCH_DEFAULT_CASE();
69 }
70 return NULL;
71 }
72 static void psi_jit_struct_type_dtor(void *type) {
73 jit_type_t strct = type;
74
75 jit_type_free(strct);
76 }
77
78 static size_t psi_jit_struct_type_pad(jit_type_t **els, size_t padding) {
79 size_t i;
80
81 for (i = 0; i < padding; ++i) {
82 *els++ = jit_type_copy(jit_type_sys_char);
83 }
84
85 return padding;
86 }
87
88 static unsigned psi_jit_struct_type_elements(decl_struct *strct, jit_type_t **fields) {
89 size_t i, j, argc = strct->args->count, nels = 0, offset = 0, maxalign;
90 *fields = calloc(argc + 1, sizeof(*fields));
91
92 for (i = 0; i < strct->args->count; ++i) {
93 decl_arg *darg = strct->args->args[i];
94 jit_type_t type = jit_type_copy(psi_jit_decl_arg_type(darg));
95 size_t padding, alignment;
96
97 ZEND_ASSERT(jit_type_get_size(type) == darg->layout->len);
98
99 if ((alignment = jit_type_get_alignment(type)) > maxalign) {
100 maxalign = alignment;
101 }
102
103 if ((padding = psi_offset_padding(darg->layout->pos - offset, alignment))) {
104 if (nels + padding > argc) {
105 argc += padding;
106 *fields = realloc(*fields, (argc + 1) * sizeof(*fields));
107 }
108 psi_jit_struct_type_pad(&(*fields)[nels], padding);
109 nels += padding;
110 offset += padding;
111 }
112 ZEND_ASSERT(offset == darg->layout->pos);
113
114 offset = (offset + darg->layout->len + alignment - 1) & ~(alignment - 1);
115 (*fields)[nels++] = type;
116 }
117
118 /* apply struct alignment padding */
119 offset = (offset + maxalign - 1) & ~(maxalign - 1);
120
121 ZEND_ASSERT(offset <= strct->size);
122 if (offset < strct->size) {
123 nels += psi_jit_struct_type_pad(&(*fields)[nels], strct->size - offset);
124 }
125
126 return nels;
127 }
128 static inline jit_type_t psi_jit_decl_type(decl_type *type) {
129 decl_type *real = real_decl_type(type);
130
131 if (real->type == PSI_T_STRUCT) {
132 if (!real->strct->engine.type) {
133 unsigned count;
134 jit_type_t strct, *fields = NULL;
135
136 count = psi_jit_struct_type_elements(real->strct, &fields);
137 strct = jit_type_create_struct(fields, count, 0);
138
139 real->strct->engine.type = strct;
140 real->strct->engine.dtor = psi_jit_struct_type_dtor;
141 }
142
143 return real->strct->engine.type;
144 }
145 return psi_jit_token_type(real->type);
146 }
147 static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg) {
148 if (darg->var->pointer_level) {
149 return jit_type_void_ptr;
150 } else {
151 return psi_jit_decl_type(darg->type);
152 }
153 }
154
155 typedef struct PSI_LibjitContext {
156 jit_context_t jit;
157 jit_type_t signature;
158 struct {
159 struct PSI_LibjitData **list;
160 size_t count;
161 } data;
162 } PSI_LibjitContext;
163
164 typedef struct PSI_LibjitCall {
165 void *closure;
166 jit_type_t signature;
167 void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
168 } PSI_LibjitCall;
169
170 typedef struct PSI_LibjitData {
171 PSI_LibjitContext *context;
172 impl *impl;
173 zend_internal_arg_info *arginfo;
174 } PSI_LibjitData;
175
176 static inline PSI_LibjitCall *PSI_LibjitCallAlloc(PSI_Context *C, decl *decl) {
177 size_t i, c = decl->args ? decl->args->count : 0;
178 PSI_LibjitCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
179
180 for (i = 0; i < c; ++i) {
181 call->params[i] = psi_jit_decl_arg_type(decl->args->args[i]);
182 }
183 call->params[c] = NULL;
184
185 decl->call.info = call;
186 decl->call.rval = &decl->func->ptr;
187 decl->call.argc = c;
188 decl->call.args = (void **) &call->params[c+1];
189
190 call->signature = jit_type_create_signature(
191 psi_jit_abi(decl->abi->convention),
192 psi_jit_decl_arg_type(decl->func),
193 (jit_type_t *) call->params, c, 1);
194 return call;
195 }
196
197 static inline void PSI_LibjitCallInitClosure(PSI_Context *C, PSI_LibjitCall *call, impl *impl) {
198 PSI_LibjitContext *context = C->context;
199 call->closure = jit_closure_create(context->jit, context->signature,
200 &psi_jit_handler, impl);
201 }
202
203 static inline void PSI_LibjitCallFree(PSI_LibjitCall *call) {
204 jit_type_free(call->signature);
205 free(call);
206 }
207
208 static inline PSI_LibjitContext *PSI_LibjitContextInit(PSI_LibjitContext *L) {
209 jit_type_t params[] = {
210 jit_type_void_ptr,
211 jit_type_void_ptr
212 };
213
214 if (!L) {
215 L = malloc(sizeof(*L));
216 }
217 memset(L, 0, sizeof(*L));
218
219 L->jit = jit_context_create();
220 L->signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void,
221 params, 2, 1);
222
223 return L;
224 }
225
226 static inline void PSI_LibjitContextDtor(PSI_LibjitContext *L) {
227 jit_type_free(L->signature);
228 jit_context_destroy(L->jit);
229 }
230
231 static inline void PSI_LibjitContextFree(PSI_LibjitContext **L) {
232 if (*L) {
233 PSI_LibjitContextDtor(*L);
234 free(*L);
235 *L = NULL;
236 }
237 }
238
239 static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data)
240 {
241 psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
242 }
243
244 static void psi_jit_init(PSI_Context *C)
245 {
246 C->context = PSI_LibjitContextInit(NULL);
247 }
248
249 static void psi_jit_dtor(PSI_Context *C)
250 {
251 if (C->decls) {
252 size_t i;
253
254 for (i = 0; i < C->decls->count; ++i) {
255 decl *decl = C->decls->list[i];
256
257 PSI_LibjitCallFree(decl->call.info);
258 }
259 }
260 PSI_LibjitContextFree((void *) &C->context);
261 }
262
263 static zend_function_entry *psi_jit_compile(PSI_Context *C)
264 {
265 size_t i, j = 0;
266 zend_function_entry *zfe;
267 PSI_LibjitContext *ctx = C->context;
268
269 if (!C->impls) {
270 return NULL;
271 }
272
273 zfe = calloc(C->impls->count + 1, sizeof(*zfe));
274 jit_context_build_start(ctx->jit);
275
276 for (i = 0; i < C->impls->count; ++i) {
277 zend_function_entry *zf = &zfe[j];
278 PSI_LibjitCall *call;
279 impl *impl = C->impls->list[i];
280
281 if (!impl->decl) {
282 continue;
283 }
284
285 call = PSI_LibjitCallAlloc(C, impl->decl);
286 PSI_LibjitCallInitClosure(C, call, impl);
287
288 zf->fname = impl->func->name + (impl->func->name[0] == '\\');
289 zf->num_args = impl->func->args->count;
290 zf->handler = call->closure;
291 zf->arg_info = psi_internal_arginfo(impl);
292 ++j;
293 }
294
295 for (i = 0; i < C->decls->count; ++i) {
296 decl *decl = C->decls->list[i];
297
298 if (decl->impl) {
299 continue;
300 }
301
302 PSI_LibjitCallAlloc(C, decl);
303 }
304
305 jit_context_build_end(ctx->jit);
306
307 return zfe;
308 }
309
310 static void psi_jit_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
311 PSI_LibjitCall *call = decl_call->info;
312
313 if (va) {
314 jit_type_t signature;
315 size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
316 void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
317
318 for (i = 0; i < nfixedargs; ++i) {
319 params[i] = call->params[i];
320 params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
321 }
322 for (i = 0; i < va->args->count; ++i) {
323 params[nfixedargs + i] = psi_jit_impl_type(va->types[i]);
324 params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
325 }
326
327 signature = jit_type_create_signature(
328 jit_type_get_abi(call->signature),
329 jit_type_get_return(call->signature),
330 (jit_type_t *) params, ntotalargs, 1);
331 ZEND_ASSERT(signature);
332
333 jit_apply(signature, decl_call->sym, &params[ntotalargs + 1],
334 nfixedargs, *decl_call->rval);
335 jit_type_free(signature);
336 free(params);
337 } else {
338 jit_apply(call->signature, decl_call->sym, decl_call->args,
339 decl_call->argc, *decl_call->rval);
340 }
341 }
342
343 static PSI_ContextOps ops = {
344 psi_jit_init,
345 psi_jit_dtor,
346 psi_jit_compile,
347 psi_jit_call,
348 };
349
350 PSI_ContextOps *PSI_Libjit(void)
351 {
352 return &ops;
353 }
354
355 #endif /* HAVE_LIBJIT */