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