struct retvals
authorMichael Wallner <mike@php.net>
Wed, 20 Jan 2016 15:10:11 +0000 (16:10 +0100)
committerMichael Wallner <mike@php.net>
Wed, 20 Jan 2016 15:10:11 +0000 (16:10 +0100)
README.md
m4/stdlib.m4
psi.d/stdlib.psi
src/libffi.c
src/libjit.c
src/module.c
src/parser.h
tests/div/div001.phpt [new file with mode: 0644]
tests/div/ldiv001.phpt [new file with mode: 0644]
tests/div/lldiv001.phpt [new file with mode: 0644]

index bfdb40c67b5c8540becfb3ddaee2a3d961239c6d..035f05c965a6430c14029ecec255412b5e07569d 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,6 +1,8 @@
 # ext-psi
 
-[![Build Status](https://travis-ci.org/m6w6/psi.svg?branch=master)](https://travis-ci.org/m6w6/psi)
+THIS README IS JUST A STUB YET
+
+[![Build Status](https://travis-ci.org/m6w6/ext-psi.svg?branch=master)](https://travis-ci.org/m6w6/ext-psi)
 
 ...
 
index de4c688a60cba541b63f05e789baace4aab5476f..4ac56358288608e757f89c3a4c69019462c89fda 100644 (file)
@@ -9,6 +9,10 @@ PSI_CHECK_STDLIB() {
                long quot,
                long rem
        ])
+       PSI_STRUCT(lldiv_t, [
+               long long quot,
+               long long rem
+       ])
 
        PSI_CONST(EXIT_FAILURE, int)
        PSI_CONST(EXIT_SUCCESS, int)
@@ -33,7 +37,8 @@ PSI_CHECK_STDLIB() {
        PSI_DECL(long jrand48, [(unsigned short xsubi@<:@3@:>@)])
        PSI_DECL(long labs, [(long l)])
        PSI_DECL(void lcong48, [(unsigned short param@<:@7@:>@)])
-       dnl PSI_DECL(ldiv_t ldiv, [(long numerator, long denominator)])
+       PSI_DECL(ldiv_t ldiv, [(long numerator, long denominator)])
+       PSI_DECL(lldiv_t lldiv, [(long long numerator, long long denominator)])
        PSI_DECL(long lrand48, [()])
        PSI_DECL(int mblen, [(const char *s, size_t n)])
        PSI_DECL(size_t mbstowcs, [(wchar_t *dest, char *src, size_t n)])
index 1229c9e2d53d1e718cf1cf884ec925cfd1ca8124..8a5257205e3433e9c7b404777e7f9bc14ef04ee9 100644 (file)
@@ -7,4 +7,16 @@ function psi\div(int $numerator, int $denominator) : array {
        let numerator = intval($numerator);
        let denominator = intval($denominator);
        return to_array(div, to_int(quot), to_int(rem));
-}
\ No newline at end of file
+}
+
+function psi\ldiv(int $numerator, int $denominator) : array {
+       let numerator = intval($numerator);
+       let denominator = intval($denominator);
+       return to_array(ldiv, to_int(quot), to_int(rem));
+}
+
+function psi\lldiv(int $numerator, int $denominator) : array {
+       let numerator = intval($numerator);
+       let denominator = intval($denominator);
+       return to_array(lldiv, to_int(quot), to_int(rem));
+}
index ac018a119640d8ea0deed03ba479cf1e6c18a260..44c591bfdade94dcfb23a420413803da157308c5 100644 (file)
@@ -56,6 +56,7 @@ static void psi_ffi_closure_free(void *c)
 }
 
 static void psi_ffi_handler(ffi_cif *signature, void *_result, void **_args, void *_data);
+static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg);
 
 static inline ffi_abi psi_ffi_abi(const char *convention) {
        return FFI_DEFAULT_ABI;
@@ -87,6 +88,8 @@ static inline ffi_type *psi_ffi_token_type(token_t t) {
                return &ffi_type_uchar;
        case PSI_T_INT:
                return &ffi_type_sint;
+       case PSI_T_LONG:
+               return &ffi_type_slong;
        case PSI_T_FLOAT:
                return &ffi_type_float;
        case PSI_T_DOUBLE:
@@ -108,24 +111,70 @@ static inline ffi_type *psi_ffi_impl_type(token_t impl_type) {
                return &ffi_type_double;
        EMPTY_SWITCH_DEFAULT_CASE();
        }
+       return NULL;
 }
 static void psi_ffi_struct_type_dtor(void *type) {
        ffi_type *strct = type;
 
        if (strct->elements) {
+               ffi_type **ptr;
+
+               for (ptr = strct->elements; *ptr; ++ptr) {
+                       free(*ptr);
+               }
                free(strct->elements);
        }
        free(strct);
 }
-static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg);
+
 static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) {
-       ffi_type **els = calloc(strct->args->count + 1, sizeof(*els));
-       size_t i;
+       size_t i, j, argc = strct->args->count << 2, nels = 0, offset = 0, align, padding;
+       ffi_type **els = calloc(argc + 1, sizeof(*els));
 
        for (i = 0; i < strct->args->count; ++i) {
-               els[i] = psi_ffi_decl_arg_type(strct->args->args[i]);
+               decl_arg *darg = strct->args->args[i];
+               ffi_type *type = malloc(sizeof(*type));
+
+               memcpy(type, psi_ffi_decl_arg_type(darg), sizeof(*type));
+
+               if (darg->layout->pos > offset) {
+                       padding = darg->layout->pos - offset;
+                       align = ((padding - 1) | (type->alignment - 1)) + 1;
+                       if (align >= padding) {
+                               padding = 0;
+                       }
+               } else {
+                       align = 0;
+                       padding = 0;
+               }
+
+               if (padding) {
+                       for (j = 0; j < padding; ++j) {
+                               ffi_type *pad = malloc(sizeof(*pad));
+
+                               ZEND_ASSERT(nels + 1 < argc);
+                               memcpy(pad, &ffi_type_schar, sizeof(*pad));
+                               els[nels++] = pad;
+                       }
+               }
+
+               ZEND_ASSERT(nels + 1 < argc);
+               els[nels++] = type;
+
+               offset += MAX(align, padding) + darg->layout->len;
+       }
+
+       ZEND_ASSERT(offset <= strct->size);
+       if (offset < strct->size) {
+               padding = strct->size - offset;
+               for (j = 0; j < padding; ++j) {
+                       ffi_type *pad = malloc(sizeof(*pad));
+
+                       ZEND_ASSERT(nels + 1 < argc);
+                       memcpy(pad, &ffi_type_schar, sizeof(*pad));
+                       els[nels++] = pad;
+               }
        }
-       els[i] = NULL;
 
        return els;
 }
@@ -138,7 +187,6 @@ static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
 
                        strct->type = FFI_TYPE_STRUCT;
                        strct->size = real->strct->size;
-                       strct->alignment = 0;
                        strct->elements = psi_ffi_struct_type_elements(real->strct);
 
                        real->strct->engine.type = strct;
@@ -180,7 +228,7 @@ static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) {
        call->params[c] = NULL;
 
        decl->call.info = call;
-       decl->call.rval = decl->func->ptr;
+       decl->call.rval = &decl->func->ptr;
        decl->call.argc = c;
        decl->call.args = (void **) &call->params[c+1];
 
@@ -332,10 +380,10 @@ static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *
                                call->signature.rtype, (ffi_type **) params);
 #endif
                ZEND_ASSERT(FFI_OK == rc);
-               ffi_call(&signature, FFI_FN(decl_call->sym), decl_call->rval, &params[ntotalargs + 1]);
+               ffi_call(&signature, FFI_FN(decl_call->sym), *decl_call->rval, &params[ntotalargs + 1]);
                free(params);
        } else {
-               ffi_call(&call->signature, FFI_FN(decl_call->sym), decl_call->rval, decl_call->args);
+               ffi_call(&call->signature, FFI_FN(decl_call->sym), *decl_call->rval, decl_call->args);
        }
 }
 
index 273c47890818a95cf16770f5d4fd124dbc391bcf..6d55e1a772d8e2ca5cda644449c6286e2e0f3df6 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,13 +42,16 @@ 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;
        case PSI_T_POINTER:
                return jit_type_void_ptr;
-       EMPTY_SWITCH_DEFAULT_CASE();
        }
 }
 static inline jit_type_t psi_jit_impl_type(token_t impl_type) {
@@ -60,9 +67,78 @@ static inline jit_type_t psi_jit_impl_type(token_t impl_type) {
                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 unsigned psi_jit_struct_type_elements(decl_struct *strct, jit_type_t **fields) {
+       size_t i, j, argc = strct->args->count << 2, nels = 0, offset = 0, align, padding;
+       *fields = calloc(argc, 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));
+
+               if (darg->layout->pos > offset) {
+                       padding = darg->layout->pos - offset;
+                       align = ((padding - 1) | (jit_type_get_alignment(type) - 1)) + 1;
+                       if (align >= padding) {
+                               padding = 0;
+                       }
+               } else {
+                       align = 0;
+                       padding = 0;
+               }
+
+               if (padding) {
+                       for (j = 0; j < padding; ++j) {
+                               jit_type_t pad = jit_type_copy(jit_type_sys_char);
+
+                               ZEND_ASSERT(nels + 1 < argc);
+                               (*fields)[nels++] = pad;
+                       }
+               }
+
+               ZEND_ASSERT(nels + 1 < argc);
+               (*fields)[nels++] = type;
+
+               offset += MAX(align, padding) + darg->layout->len;
+       }
+
+       ZEND_ASSERT(offset <= strct->size);
+       if (offset < strct->size) {
+               padding = strct->size - offset;
+               for (j = 0; j < padding; ++j) {
+                       jit_type_t pad = jit_type_copy(jit_type_sys_char);
+
+                       ZEND_ASSERT(nels + 1 < argc);
+                       (*fields)[nels++] = pad;
+               }
+       }
+
+       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) {
@@ -103,7 +179,7 @@ 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.rval = &decl->func->ptr;
        decl->call.argc = c;
        decl->call.args = (void **) &call->params[c+1];
 
@@ -251,12 +327,12 @@ static void psi_jit_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *
                ZEND_ASSERT(signature);
 
                jit_apply(signature, decl_call->sym, &params[ntotalargs + 1],
-                                               nfixedargs, decl_call->rval);
+                                               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);
+                               decl_call->argc, *decl_call->rval);
        }
 }
 
index 2257a0d9d68a5256f2758cc7a6d9c823a57a0600..5c8634ac5d9864f79b903191ea2f1a1135d3a258 100644 (file)
@@ -788,6 +788,10 @@ static inline void psi_do_clean(impl *impl)
 {
        size_t i;
 
+       if (impl->decl->func->ptr != &impl->decl->func->val) {
+               efree(impl->decl->func->ptr);
+               impl->decl->func->ptr = &impl->decl->func->val;
+       }
        for (i = 0; i < impl->func->args->count; ++i ) {
                impl_arg *iarg = impl->func->args->args[i];
 
@@ -1068,6 +1072,16 @@ static inline void psi_do_args(impl *impl) {
        for (i = 0; i < impl->decl->args->count; ++i) {
                impl->decl->call.args[i] = impl->decl->args->args[i]->let->ptr;
        }
+
+       if (!impl->decl->func->var->pointer_level) {
+               decl_type *real = real_decl_type(impl->decl->func->type);
+
+               switch (real->type) {
+               case PSI_T_STRUCT:
+                       impl->decl->func->ptr = psi_array_to_struct(real->strct, NULL);
+                       break;
+               }
+       }
 }
 
 static inline impl_vararg *psi_do_varargs(impl *impl) {
index 29bf9d326ce17727b66bd18969c9d774110de184..d5ff8967b4ae2d343bfee026e9e1d7748896179a 100644 (file)
@@ -320,7 +320,7 @@ typedef struct decl_callinfo {
        void *info;
        size_t argc;
        void **args;
-       void *rval;
+       void **rval;
 } decl_callinfo;
 
 typedef struct decl {
@@ -397,6 +397,9 @@ static inline void free_decl_struct(decl_struct *s) {
        if (s->args) {
                free_decl_args(s->args);
        }
+       if (s->engine.type && s->engine.dtor) {
+               s->engine.dtor(s->engine.type);
+       }
        free(s->name);
        free(s);
 }
diff --git a/tests/div/div001.phpt b/tests/div/div001.phpt
new file mode 100644 (file)
index 0000000..ea511f8
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+div
+--INI--
+psi.directory={PWD}:{PWD}/../../psi.d
+--SKIPIF--
+<?php 
+extension_loaded("psi") or die("skip - need ext/psi");
+?>
+--FILE--
+===TEST===
+<?php
+var_dump(psi\div(1000,10));
+?>
+===DONE===
+--EXPECT--
+===TEST===
+array(2) {
+  ["quot"]=>
+  int(100)
+  ["rem"]=>
+  int(0)
+}
+===DONE===
diff --git a/tests/div/ldiv001.phpt b/tests/div/ldiv001.phpt
new file mode 100644 (file)
index 0000000..7712631
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+ldiv
+--INI--
+psi.directory={PWD}:{PWD}/../../psi.d
+--SKIPIF--
+<?php 
+extension_loaded("psi") or die("skip - need ext/psi");
+?>
+--FILE--
+===TEST===
+<?php
+var_dump(psi\ldiv(1000,10));
+?>
+===DONE===
+--EXPECT--
+===TEST===
+array(2) {
+  ["quot"]=>
+  int(100)
+  ["rem"]=>
+  int(0)
+}
+===DONE===
diff --git a/tests/div/lldiv001.phpt b/tests/div/lldiv001.phpt
new file mode 100644 (file)
index 0000000..f084912
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+lldiv
+--INI--
+psi.directory={PWD}:{PWD}/../../psi.d
+--SKIPIF--
+<?php 
+extension_loaded("psi") or die("skip - need ext/psi");
+?>
+--FILE--
+===TEST===
+<?php
+var_dump(psi\lldiv(1000,10));
+?>
+===DONE===
+--EXPECT--
+===TEST===
+array(2) {
+  ["quot"]=>
+  int(100)
+  ["rem"]=>
+  int(0)
+}
+===DONE===