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