From: Michael Wallner Date: Wed, 20 Jan 2016 15:10:11 +0000 (+0100) Subject: struct retvals X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=commitdiff_plain;h=4ecafb11128dc652013564387ab33b8b9110f039 struct retvals --- diff --git a/README.md b/README.md index bfdb40c..035f05c 100644 --- 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) ... diff --git a/m4/stdlib.m4 b/m4/stdlib.m4 index de4c688..4ac5635 100644 --- a/m4/stdlib.m4 +++ b/m4/stdlib.m4 @@ -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)]) diff --git a/psi.d/stdlib.psi b/psi.d/stdlib.psi index 1229c9e..8a52572 100644 --- a/psi.d/stdlib.psi +++ b/psi.d/stdlib.psi @@ -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)); +} diff --git a/src/libffi.c b/src/libffi.c index ac018a1..44c591b 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -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, ¶ms[ntotalargs + 1]); + ffi_call(&signature, FFI_FN(decl_call->sym), *decl_call->rval, ¶ms[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); } } diff --git a/src/libjit.c b/src/libjit.c index 273c478..6d55e1a 100644 --- a/src/libjit.c +++ b/src/libjit.c @@ -12,12 +12,16 @@ #include 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, ¶ms[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); } } diff --git a/src/module.c b/src/module.c index 2257a0d..5c8634a 100644 --- a/src/module.c +++ b/src/module.c @@ -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) { diff --git a/src/parser.h b/src/parser.h index 29bf9d3..d5ff896 100644 --- a/src/parser.h +++ b/src/parser.h @@ -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 index 0000000..ea511f8 --- /dev/null +++ b/tests/div/div001.phpt @@ -0,0 +1,23 @@ +--TEST-- +div +--INI-- +psi.directory={PWD}:{PWD}/../../psi.d +--SKIPIF-- + +--FILE-- +===TEST=== + +===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 index 0000000..7712631 --- /dev/null +++ b/tests/div/ldiv001.phpt @@ -0,0 +1,23 @@ +--TEST-- +ldiv +--INI-- +psi.directory={PWD}:{PWD}/../../psi.d +--SKIPIF-- + +--FILE-- +===TEST=== + +===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 index 0000000..f084912 --- /dev/null +++ b/tests/div/lldiv001.phpt @@ -0,0 +1,23 @@ +--TEST-- +lldiv +--INI-- +psi.directory={PWD}:{PWD}/../../psi.d +--SKIPIF-- + +--FILE-- +===TEST=== + +===DONE=== +--EXPECT-- +===TEST=== +array(2) { + ["quot"]=> + int(100) + ["rem"]=> + int(0) +} +===DONE===