# 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)
...
long quot,
long rem
])
+ PSI_STRUCT(lldiv_t, [
+ long long quot,
+ long long rem
+ ])
PSI_CONST(EXIT_FAILURE, int)
PSI_CONST(EXIT_SUCCESS, int)
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)])
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));
+}
}
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;
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:
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;
}
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;
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];
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);
}
}
#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:
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) {
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) {
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];
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);
}
}
{
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];
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) {
void *info;
size_t argc;
void **args;
- void *rval;
+ void **rval;
} decl_callinfo;
typedef struct decl {
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);
}
--- /dev/null
+--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===
--- /dev/null
+--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===
--- /dev/null
+--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===