fix build with long double
[m6w6/ext-psi] / src / libjit.c
index 45ee8e766c34a8153439cee6351cb4617c4782ee..7b0a49a5cadd6748edd886ba5345570c9d292ddf 100644 (file)
 #include <jit/jit.h>
 
 static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data);
+static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg);
 
 static inline jit_abi_t psi_jit_abi(const char *convention) {
        return jit_abi_cdecl;
 }
 static inline jit_type_t psi_jit_token_type(token_t t) {
        switch (t) {
+       default:
+               ZEND_ASSERT(0);
+               /* no break */
        case PSI_T_VOID:
                return jit_type_void;
        case PSI_T_INT8:
@@ -38,15 +42,111 @@ static inline jit_type_t psi_jit_token_type(token_t t) {
                return jit_type_ulong;
        case PSI_T_BOOL:
                return jit_type_sys_bool;
+       case PSI_T_INT:
+               return jit_type_sys_int;
+       case PSI_T_LONG:
+               return jit_type_sys_long;
        case PSI_T_FLOAT:
                return jit_type_sys_float;
        case PSI_T_DOUBLE:
                return jit_type_sys_double;
+#ifdef HAVE_LONG_DOUBLE
+       case PSI_T_LONG_DOUBLE:
+               return jit_type_sys_long_double;
+#endif
+       case PSI_T_POINTER:
+               return jit_type_void_ptr;
+       }
+}
+static inline jit_type_t psi_jit_impl_type(token_t impl_type) {
+       switch (impl_type) {
+       case PSI_T_BOOL:
+               return jit_type_sbyte;
+       case PSI_T_INT:
+               return jit_type_long;
+       case PSI_T_STRING:
+               return jit_type_void_ptr;
+       case PSI_T_FLOAT:
+       case PSI_T_DOUBLE:
+               return jit_type_sys_double;
        EMPTY_SWITCH_DEFAULT_CASE();
        }
+       return NULL;
+}
+static void psi_jit_struct_type_dtor(void *type) {
+       jit_type_t strct = type;
+
+       jit_type_free(strct);
+}
+
+static size_t psi_jit_struct_type_pad(jit_type_t **els, size_t padding) {
+       size_t i;
+
+       for (i = 0; i < padding; ++i) {
+               *els++ = jit_type_copy(jit_type_sys_char);
+       }
+
+       return padding;
+}
+
+static unsigned psi_jit_struct_type_elements(decl_struct *strct, jit_type_t **fields) {
+       size_t i, j, argc = strct->args->count, nels = 0, offset = 0, maxalign;
+       *fields = calloc(argc + 1, sizeof(*fields));
+
+       for (i = 0; i < strct->args->count; ++i) {
+               decl_arg *darg = strct->args->args[i];
+               jit_type_t type = jit_type_copy(psi_jit_decl_arg_type(darg));
+               size_t padding, alignment;
+
+               ZEND_ASSERT(jit_type_get_size(type) == darg->layout->len);
+
+               if ((alignment = jit_type_get_alignment(type)) > maxalign) {
+                       maxalign = alignment;
+               }
+
+               if ((padding = psi_offset_padding(darg->layout->pos - offset, alignment))) {
+                       if (nels + padding > argc) {
+                               argc += padding;
+                               *fields = realloc(*fields, (argc + 1) * sizeof(*fields));
+                       }
+                       psi_jit_struct_type_pad(&(*fields)[nels], padding);
+                       nels += padding;
+                       offset += padding;
+               }
+               ZEND_ASSERT(offset == darg->layout->pos);
+
+               offset = (offset + darg->layout->len + alignment - 1) & ~(alignment - 1);
+               (*fields)[nels++] = type;
+       }
+
+       /* apply struct alignment padding */
+       offset = (offset + maxalign - 1) & ~(maxalign - 1);
+
+       ZEND_ASSERT(offset <= strct->size);
+       if (offset < strct->size) {
+               nels += psi_jit_struct_type_pad(&(*fields)[nels], strct->size - offset);
+       }
+
+       return nels;
 }
 static inline jit_type_t psi_jit_decl_type(decl_type *type) {
-       return psi_jit_token_type(real_decl_type(type)->type);
+       decl_type *real = real_decl_type(type);
+
+       if (real->type == PSI_T_STRUCT) {
+               if (!real->strct->engine.type) {
+                       unsigned count;
+                       jit_type_t strct, *fields = NULL;
+
+                       count = psi_jit_struct_type_elements(real->strct, &fields);
+                       strct = jit_type_create_struct(fields, count, 0);
+
+                       real->strct->engine.type = strct;
+                       real->strct->engine.dtor = psi_jit_struct_type_dtor;
+               }
+
+               return real->strct->engine.type;
+       }
+       return psi_jit_token_type(real->type);
 }
 static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg) {
        if (darg->var->pointer_level) {
@@ -68,7 +168,7 @@ typedef struct PSI_LibjitContext {
 typedef struct PSI_LibjitCall {
        void *closure;
        jit_type_t signature;
-       jit_type_t params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
+       void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
 } PSI_LibjitCall;
 
 typedef struct PSI_LibjitData {
@@ -87,12 +187,14 @@ static inline PSI_LibjitCall *PSI_LibjitCallAlloc(PSI_Context *C, decl *decl) {
        call->params[c] = NULL;
 
        decl->call.info = call;
+       decl->call.rval = &decl->func->ptr;
+       decl->call.argc = c;
        decl->call.args = (void **) &call->params[c+1];
 
        call->signature = jit_type_create_signature(
                        psi_jit_abi(decl->abi->convention),
                        psi_jit_decl_arg_type(decl->func),
-                       call->params, c, 1);
+                       (jit_type_t *) call->params, c, 1);
        return call;
 }
 
@@ -104,6 +206,7 @@ static inline void PSI_LibjitCallInitClosure(PSI_Context *C, PSI_LibjitCall *cal
 
 static inline void PSI_LibjitCallFree(PSI_LibjitCall *call) {
        jit_type_free(call->signature);
+       free(call);
 }
 
 static inline PSI_LibjitContext *PSI_LibjitContextInit(PSI_LibjitContext *L) {
@@ -208,11 +311,37 @@ static zend_function_entry *psi_jit_compile(PSI_Context *C)
        return zfe;
 }
 
-static void psi_jit_call(PSI_Context *C, impl_val *ret_val, decl *decl) {
-       PSI_LibjitCall *call = decl->call.info;
+static void psi_jit_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
+       PSI_LibjitCall *call = decl_call->info;
+
+       if (va) {
+               jit_type_t signature;
+               size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
+               void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
 
-       jit_apply(call->signature, decl->call.sym, decl->call.args,
-                       decl->args->count, ret_val);
+               for (i = 0; i < nfixedargs; ++i) {
+                       params[i] = call->params[i];
+                       params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
+               }
+               for (i = 0; i < va->args->count; ++i) {
+                       params[nfixedargs + i] = psi_jit_impl_type(va->types[i]);
+                       params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
+               }
+
+               signature = jit_type_create_signature(
+                               jit_type_get_abi(call->signature),
+                               jit_type_get_return(call->signature),
+                               (jit_type_t *) params, ntotalargs, 1);
+               ZEND_ASSERT(signature);
+
+               jit_apply(signature, decl_call->sym, &params[ntotalargs + 1],
+                                               nfixedargs, *decl_call->rval);
+               jit_type_free(signature);
+               free(params);
+       } else {
+               jit_apply(call->signature, decl_call->sym, decl_call->args,
+                               decl_call->argc, *decl_call->rval);
+       }
 }
 
 static PSI_ContextOps ops = {