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