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