flush
authorMichael Wallner <mike@php.net>
Thu, 12 Nov 2015 09:50:00 +0000 (10:50 +0100)
committerMichael Wallner <mike@php.net>
Thu, 12 Nov 2015 09:50:00 +0000 (10:50 +0100)
.gitignore
php_psi.h
src/context.c
src/module.c
src/parser.h
tests/time/time.psi
tests/time/time001.phpt
tests/uname/uname.psi [new file with mode: 0644]
tests/uname/uname001.phpt [new file with mode: 0644]

index 9b68b09..dd61cef 100644 (file)
@@ -49,3 +49,4 @@ tests/*/*.sh
 /context.h
 /libffi.h
 /libjit.j
+/tests/*/*.mem
index d387a06..9ebf8e7 100644 (file)
--- a/php_psi.h
+++ b/php_psi.h
@@ -32,11 +32,11 @@ int psi_internal_type(impl_type *type);
 zend_internal_arg_info *psi_internal_arginfo(impl *impl);
 size_t psi_num_min_args(impl *impl);
 
-void psi_to_bool(zval *return_value, token_t t, impl_val *ret_val, decl_var *var);
-void psi_to_int(zval *return_value, token_t t, impl_val *ret_val, decl_var *var);
-void psi_to_double(zval *return_value, token_t t, impl_val *ret_val, decl_var *var);
-void psi_to_string(zval *return_value, token_t t, impl_val *ret_val, decl_var *var);
-void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, decl_var *var);
+void psi_to_bool(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var);
+void psi_to_int(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var);
+void psi_to_double(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var);
+void psi_to_string(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var);
+void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var);
 
 ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl);
 void *psi_do_let(decl_arg *darg);
index 3ff510f..00126c7 100644 (file)
@@ -263,7 +263,7 @@ static inline decl_arg *locate_struct_member(decl_struct *s, decl_var *var) {
                decl_arg *darg = s->args->args[i];
 
                if (!strcmp(var->name, darg->var->name)) {
-                       return darg;
+                       return var->arg = darg;
                }
        }
 
index eec8611..b8d2300 100644 (file)
@@ -32,6 +32,109 @@ void psi_error(int type, const char *msg, ...)
        php_error(type, buf);
 }
 
+size_t psi_t_alignment(token_t t)
+{
+       size_t align;
+#define PSI_TAS_D(T) struct PSI_TAS_ ##T { \
+       char c; \
+       T x; \
+}
+#define PSI_TAS_P(T) struct PSI_TAS_ ## T ## _pointer { \
+       char c; \
+       T *x; \
+}
+#define PSI_TAS_C(T) align = offsetof(struct PSI_TAS_ ##T, x)
+#define PSI_TAS_CASE(T) { \
+       PSI_TAS_D(T); \
+       PSI_TAS_C(T); \
+}
+       switch (t) {
+       case PSI_T_INT8:
+               PSI_TAS_CASE(int8_t);
+               break;
+       case PSI_T_UINT8:
+               PSI_TAS_CASE(uint8_t);
+               break;
+       case PSI_T_INT16:
+               PSI_TAS_CASE(int16_t);
+               break;
+       case PSI_T_UINT16:
+               PSI_TAS_CASE(uint16_t);
+               break;
+       case PSI_T_INT32:
+               PSI_TAS_CASE(int32_t);
+               break;
+       case PSI_T_UINT32:
+               PSI_TAS_CASE(uint32_t);
+               break;
+       case PSI_T_INT64:
+               PSI_TAS_CASE(int64_t);
+               break;
+       case PSI_T_UINT64:
+               PSI_TAS_CASE(uint64_t);
+               break;
+       case PSI_T_FLOAT:
+               PSI_TAS_CASE(float);
+               break;
+       case PSI_T_DOUBLE:
+               PSI_TAS_CASE(double);
+               break;
+       case PSI_T_POINTER:
+               {
+                       PSI_TAS_P(char);
+                       PSI_TAS_C(char_pointer);
+               }
+               break;
+       EMPTY_SWITCH_DEFAULT_CASE();
+       }
+
+       return align;
+}
+
+size_t psi_t_size(token_t t)
+{
+       size_t size;
+
+       switch (t) {
+       case PSI_T_INT8:
+       case PSI_T_UINT8:
+               size = 1;
+               break;
+       case PSI_T_INT16:
+       case PSI_T_UINT16:
+               size = 2;
+               break;
+       case PSI_T_INT:
+               size = sizeof(int);
+               break;
+       case PSI_T_INT32:
+       case PSI_T_UINT32:
+               size = 4;
+               break;
+       case PSI_T_INT64:
+       case PSI_T_UINT64:
+               size = 8;
+               break;
+       case PSI_T_FLOAT:
+               size = sizeof(float);
+               break;
+       case PSI_T_DOUBLE:
+               size = sizeof(double);
+               break;
+       case PSI_T_POINTER:
+               size = sizeof(char *);
+               break;
+       EMPTY_SWITCH_DEFAULT_CASE();
+       }
+       return size;
+}
+
+size_t psi_t_align(token_t t, size_t s)
+{
+       size_t a = psi_t_alignment(t);
+       return ((s - 1) | (a - 1)) + 1;
+}
+
 int psi_internal_type(impl_type *type)
 {
        switch (type->type) {
@@ -93,56 +196,60 @@ size_t psi_num_min_args(impl *impl)
        return n;
 }
 
-void psi_to_bool(zval *return_value, token_t t, impl_val *ret_val, decl_var *var)
+void psi_to_bool(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var)
 {
+       impl_val *v = deref_impl_val(ret_val, var);
+
        switch (t) {
        case PSI_T_FLOAT:
-               RETVAL_DOUBLE((double) deref_impl_val(ret_val, var)->fval);
-               convert_to_boolean(return_value);
+               RETVAL_DOUBLE((double) v->fval);
                break;
        case PSI_T_DOUBLE:
-               RETVAL_DOUBLE(deref_impl_val(ret_val, var)->dval);
-               convert_to_boolean(return_value);
+               RETVAL_DOUBLE(v->dval);
                break;
        default:
-               RETVAL_LONG(deref_impl_val(ret_val, var)->lval);
-               convert_to_boolean(return_value);
+               RETVAL_LONG(v->lval);
                break;
        }
+       convert_to_boolean(return_value);
 }
 
-void psi_to_int(zval *return_value, token_t t, impl_val *ret_val, decl_var *var)
+void psi_to_int(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var)
 {
+       impl_val *v = deref_impl_val(ret_val, var);
+
        switch (t) {
        case PSI_T_FLOAT:
-               RETVAL_DOUBLE((double) deref_impl_val(ret_val, var)->fval);
-               convert_to_long(return_value);
+               RETVAL_DOUBLE((double) v->fval);
                break;
        case PSI_T_DOUBLE:
-               RETVAL_DOUBLE(deref_impl_val(ret_val, var)->dval);
-               convert_to_long(return_value);
+               RETVAL_DOUBLE(v->dval);
                break;
        default:
-               RETVAL_LONG(deref_impl_val(ret_val, var)->lval);
+               RETVAL_LONG(v->lval);
+               return;
        }
+       convert_to_long(return_value);
 }
 
-void psi_to_double(zval *return_value, token_t t, impl_val *ret_val, decl_var *var)
+void psi_to_double(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var)
 {
+       impl_val *v = deref_impl_val(ret_val, var);
+
        switch (t) {
        case PSI_T_FLOAT:
-               RETVAL_DOUBLE((double) deref_impl_val(ret_val, var)->fval);
+               RETVAL_DOUBLE((double) v->fval);
                break;
        case PSI_T_DOUBLE:
-               RETVAL_DOUBLE(deref_impl_val(ret_val, var)->dval);
+               RETVAL_DOUBLE(v->dval);
                break;
        default:
-               RETVAL_DOUBLE((double) deref_impl_val(ret_val, var)->lval);
+               RETVAL_DOUBLE((double) v->lval);
                break;
        }
 }
 
-void psi_to_string(zval *return_value, token_t t, impl_val *ret_val, decl_var *var)
+void psi_to_string(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var)
 {
        switch (t) {
        case PSI_T_INT8:
@@ -157,124 +264,20 @@ void psi_to_string(zval *return_value, token_t t, impl_val *ret_val, decl_var *v
                                RETVAL_EMPTY_STRING();
                        }
                }
-               break;
+               return;
        case PSI_T_FLOAT:
                RETVAL_DOUBLE((double) deref_impl_val(ret_val, var)->fval);
-               convert_to_string(return_value);
                break;
        case PSI_T_DOUBLE:
                RETVAL_DOUBLE(deref_impl_val(ret_val, var)->dval);
-               convert_to_string(return_value);
                break;
        default:
                RETVAL_LONG(deref_impl_val(ret_val, var)->lval);
-               convert_to_string(return_value);
                break;
        }
+       convert_to_string(return_value);
 }
 
-size_t psi_t_alignment(token_t t)
-{
-       size_t align;
-#define PSI_TAS_D(T) struct PSI_TAS_ ##T { \
-       char c; \
-       T x; \
-}
-#define PSI_TAS_P(T) struct PSI_TAS_ ## T ## _pointer { \
-       char c; \
-       T *x; \
-}
-#define PSI_TAS_C(T) align = offsetof(struct PSI_TAS_ ##T, x)
-#define PSI_TAS_CASE(T) { \
-       PSI_TAS_D(T); \
-       PSI_TAS_C(T); \
-}
-       switch (t) {
-       case PSI_T_INT8:
-               PSI_TAS_CASE(int8_t);
-               break;
-       case PSI_T_UINT8:
-               PSI_TAS_CASE(uint8_t);
-               break;
-       case PSI_T_INT16:
-               PSI_TAS_CASE(int16_t);
-               break;
-       case PSI_T_UINT16:
-               PSI_TAS_CASE(uint16_t);
-               break;
-       case PSI_T_INT32:
-               PSI_TAS_CASE(int32_t);
-               break;
-       case PSI_T_UINT32:
-               PSI_TAS_CASE(uint32_t);
-               break;
-       case PSI_T_INT64:
-               PSI_TAS_CASE(int64_t);
-               break;
-       case PSI_T_UINT64:
-               PSI_TAS_CASE(uint64_t);
-               break;
-       case PSI_T_FLOAT:
-               PSI_TAS_CASE(float);
-               break;
-       case PSI_T_DOUBLE:
-               PSI_TAS_CASE(double);
-               break;
-       case PSI_T_POINTER:
-               {
-                       PSI_TAS_P(char);
-                       PSI_TAS_C(char_pointer);
-               }
-               break;
-       EMPTY_SWITCH_DEFAULT_CASE();
-       }
-
-       return align;
-}
-
-size_t psi_t_size(token_t t)
-{
-       size_t size;
-
-       switch (t) {
-       case PSI_T_INT8:
-       case PSI_T_UINT8:
-               size = 1;
-               break;
-       case PSI_T_INT16:
-       case PSI_T_UINT16:
-               size = 2;
-               break;
-       case PSI_T_INT:
-               size = sizeof(int);
-               break;
-       case PSI_T_INT32:
-       case PSI_T_UINT32:
-               size = 4;
-               break;
-       case PSI_T_INT64:
-       case PSI_T_UINT64:
-               size = 8;
-               break;
-       case PSI_T_FLOAT:
-               size = sizeof(float);
-               break;
-       case PSI_T_DOUBLE:
-               size = sizeof(double);
-               break;
-       case PSI_T_POINTER:
-               size = sizeof(char *);
-               break;
-       EMPTY_SWITCH_DEFAULT_CASE();
-       }
-       return size;
-}
-
-size_t psi_t_align(token_t t, size_t s)
-{
-       size_t a = psi_t_alignment(t);
-       return ((s - 1) | (a - 1)) + 1;
-}
 
 static impl_val *iterate(impl_val *val, token_t t, unsigned i, impl_val *tmp)
 {
@@ -335,7 +338,7 @@ void *psi_array_to_struct(decl_struct *s, HashTable *arr)
        return mem;
 }
 
-void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, decl_var *var)
+void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var)
 {
        zval ele;
        unsigned i;
@@ -348,45 +351,70 @@ void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, decl_var *va
                ret_val = deref_impl_val(ret_val, var);
 
                ZEND_ASSERT(s);
-               for (i = 0; i < s->args->count; ++i) {
-                       decl_arg *darg = s->args->args[i];
-                       impl_val tmp, tmp_ptr;
-                       zval ztmp;
-                       char *ptr = (char *) ret_val->ptr + darg->layout->pos;
 
-                       tmp_ptr.ptr = &tmp;
-                       memset(&tmp, 0, sizeof(tmp));
-                       memcpy(&tmp, ptr, darg->layout->len);
-                       switch (real_decl_type(darg->type)->type) {
-                       case PSI_T_FLOAT:
-                       case PSI_T_DOUBLE:
-                               psi_to_double(&ztmp, real_decl_type(darg->type)->type, &tmp, darg->var);
-                               break;
-                       case PSI_T_INT8:
-                       case PSI_T_UINT8:
-                               if (darg->var->pointer_level) {
-                                       psi_to_string(&ztmp, real_decl_type(darg->type)->type, &tmp_ptr, darg->var);
-                                       break;
+               if (set->count) {
+                       /* explicit member casts */
+                       for (i = 0; i < set->count; ++i) {
+                               zval ztmp;
+                               impl_val *tmp_ptr;
+                               set_value *sub_set = set->inner[i];
+                               decl_var *sub_var = sub_set->vars->vars[0];
+                               decl_arg *sub_arg = sub_var->arg;
+                               token_t t = real_decl_type(sub_arg->type)->type;
+                               void *ptr = malloc(sub_arg->layout->len);
+
+                               memcpy(ptr, (char *) ret_val->ptr + sub_arg->layout->pos,
+                                               sub_arg->layout->len);
+                               tmp_ptr = enref_impl_val(ptr, sub_arg->var);
+                               sub_set->func->handler(&ztmp, t, tmp_ptr, sub_set, sub_var);
+                               add_assoc_zval(return_value, sub_var->name, &ztmp);
+                               free(tmp_ptr);
+                               if (tmp_ptr != ptr) {
+                                       free(ptr);
                                }
-                               /* no break */
-                       case PSI_T_INT16:
-                       case PSI_T_UINT16:
-                       case PSI_T_INT32:
-                       case PSI_T_UINT32:
-                       case PSI_T_INT64:
-                       case PSI_T_UINT64:
-                               psi_to_int(&ztmp, real_decl_type(darg->type)->type, &tmp, darg->var);
-                               break;
-                       case PSI_T_STRUCT:
-                               psi_to_array(&ztmp, real_decl_type(darg->type)->type, &tmp_ptr, darg->var);
-                               break;
-                       default:
-                               printf("t=%d\n", real_decl_type(darg->type)->type);
-                               abort();
                        }
-                       add_assoc_zval(return_value, darg->var->name, &ztmp);
                }
                return;
+
+//             for (i = 0; i < s->args->count; ++i) {
+//                     decl_arg *darg = s->args->args[i];
+//                     impl_val tmp, tmp_ptr;
+//                     zval ztmp;
+//                     char *ptr = (char *) ret_val->ptr + darg->layout->pos;
+//
+//                     tmp_ptr.ptr = &tmp;
+//                     memset(&tmp, 0, sizeof(tmp));
+//                     memcpy(&tmp, ptr, darg->layout->len);
+//                     switch (real_decl_type(darg->type)->type) {
+//                     case PSI_T_FLOAT:
+//                     case PSI_T_DOUBLE:
+//                             psi_to_double(&ztmp, real_decl_type(darg->type)->type, &tmp, darg->var);
+//                             break;
+//                     case PSI_T_INT8:
+//                     case PSI_T_UINT8:
+//                             if (darg->var->pointer_level) {
+//                                     psi_to_string(&ztmp, real_decl_type(darg->type)->type, &tmp_ptr, darg->var);
+//                                     break;
+//                             }
+//                             /* no break */
+//                     case PSI_T_INT16:
+//                     case PSI_T_UINT16:
+//                     case PSI_T_INT32:
+//                     case PSI_T_UINT32:
+//                     case PSI_T_INT64:
+//                     case PSI_T_UINT64:
+//                             psi_to_int(&ztmp, real_decl_type(darg->type)->type, &tmp, darg->var);
+//                             break;
+//                     case PSI_T_STRUCT:
+//                             psi_to_array(&ztmp, real_decl_type(darg->type)->type, &tmp_ptr, darg->var);
+//                             break;
+//                     default:
+//                             printf("t=%d\n", real_decl_type(darg->type)->type);
+//                             abort();
+//                     }
+//                     add_assoc_zval(return_value, darg->var->name, &ztmp);
+//             }
+               return;
        }
        ret_val = deref_impl_val(ret_val, var);
        for (i = 0; i < var->arg->var->array_size; ++i) {
@@ -573,14 +601,14 @@ void psi_do_set(zval *return_value, set_value *set)
        ZVAL_DEREF(return_value);
        zval_dtor(return_value);
 
-       set->func->handler(return_value, t, val, set->vars->vars[0]);
+       set->func->handler(return_value, t, val, set, set->vars->vars[0]);
 }
 
 void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val)
 {
        token_t t = real_decl_type(ret->decl->type)->type;
 
-       ret->set->func->handler(return_value, t, ret_val, ret->decl->var);
+       ret->set->func->handler(return_value, t, ret_val, ret->set, ret->decl->var);
 }
 
 void psi_do_free(free_stmt *fre)
index a9060bd..3b4c77f 100644 (file)
@@ -352,6 +352,21 @@ static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) {
        return ret_val;
 }
 
+static inline impl_val *enref_impl_val(void *ptr, decl_var *var) {
+       impl_val *val, *val_ptr;
+       unsigned i;
+
+       if (!var->pointer_level) {
+               return ptr;
+       }
+       val = val_ptr = calloc(var->pointer_level, sizeof(void *));
+       for (i = 1; i < var->pointer_level; ++i) {
+               val_ptr->ptr = (void **) val_ptr + 1;
+               val_ptr = val_ptr->ptr;
+       }
+       val_ptr->ptr = ptr;
+       return val;
+}
 typedef struct impl_type {
        char *name;
        token_t type;
@@ -570,10 +585,12 @@ static inline void free_let_stmt(let_stmt *stmt) {
        free(stmt);
 }
 
+struct set_value;
+
 typedef struct set_func {
        token_t type;
        char *name;
-       void (*handler)(zval *, token_t, impl_val *, decl_var *);
+       void (*handler)(zval *, token_t, impl_val *, struct set_value *set, decl_var *);
 } set_func;
 
 static inline set_func *init_set_func(token_t type, const char *name) {
@@ -615,6 +632,7 @@ static inline void free_set_value(set_value *val) {
                for (i = 0; i < val->count; ++i) {
                        free_set_value(val->inner[i]);
                }
+               free(val->inner);
        }
        free(val);
 }
index 514d2b3..e491185 100644 (file)
@@ -3,8 +3,12 @@ function psi\gettimeofday(array &$tv = NULL, array &$tz = NULL) : int {
        let tv = calloc(1, struct timeval);
        let tz = calloc(1, struct timezone);
        return to_int(gettimeofday);
-       set $tv = to_array(*tv);
-       set $tz = to_array(*tz);
+       set $tv = to_array(*tv, 
+               to_int(tv_sec), 
+               to_int(tv_usec));
+       set $tz = to_array(*tz, 
+               to_int(tz_minuteswest), 
+               to_int(tz_dsttime));
 }
 
 extern char *asctime(struct tm *tm);
@@ -23,14 +27,34 @@ function psi\asctime_r(array $tm = NULL) : string {
 extern struct tm *gmtime(time_t *tp);
 function psi\gmtime(int $ts) : array {
        let tp = &intval($ts);
-       return to_array(gmtime);
+       return to_array(*gmtime,
+               to_int(tm_sec),
+               to_int(tm_min),
+               to_int(tm_hour),
+               to_int(tm_mday),
+               to_int(tm_mon),
+               to_int(tm_year),
+               to_int(tm_wday),
+               to_int(tm_yday),
+               to_int(tm_isdst)
+       );
 }
 
 extern struct tm *gmtime_r(time_t *tp, struct tm *buf);
 function psi\gmtime_r(int $ts) : array {
        let tp = &intval($ts);
        let buf = calloc(1, struct tm);
-       return to_array(gmtime_r);
+       return to_array(*gmtime_r,
+               to_int(tm_sec),
+               to_int(tm_min),
+               to_int(tm_hour),
+               to_int(tm_mday),
+               to_int(tm_mon),
+               to_int(tm_year),
+               to_int(tm_wday),
+               to_int(tm_yday),
+               to_int(tm_isdst)
+       );
 }
 
 extern int nanosleep(struct timespec *rqtp, struct timespec *rmtp);
@@ -38,12 +62,20 @@ function psi\nanosleep(array $rq = NULL, array &$rm = NULL) : int {
        let rqtp = arrval($rq);
        let rmtp = calloc(1, struct timespec);
        return to_int(nanosleep);
-       set $rm = to_array(*rmtp);
+       set $rm = to_array(*rmtp,
+               to_int(tv_sec),
+               to_int(tv_nsec)
+       );
 }
 
 extern clock_t times(struct tms *buf);
 function psi\times(array &$tms = NULL) : int {
        let buf = calloc(1, struct tms);
        return to_int(times);
-       set $tms = to_array(*buf);
+       set $tms = to_array(*buf,
+               to_int(tms_utime),
+               to_int(tms_stime),
+               to_int(tms_cutime),
+               to_int(tms_cstime)
+       );
 }
index 8c35eb9..b372836 100644 (file)
@@ -17,6 +17,7 @@ var_dump(psi\gettimeofday($tv), $tv);
 var_dump(psi\gettimeofday($tv, $tz), $tv, $tz);
 
 var_dump(abs($ar["sec"] - $tv["tv_sec"]) <= 1);
+
 ?>
 ===DONE===
 --EXPECTF--
diff --git a/tests/uname/uname.psi b/tests/uname/uname.psi
new file mode 100644 (file)
index 0000000..25abfcb
--- /dev/null
@@ -0,0 +1,12 @@
+extern int uname(struct utsname *u);
+function psi\uname(array &$u = NULL) : int {
+       let u = calloc(1, struct utsname);
+       return to_int(uname);
+       set $u = to_array(*u,
+               to_string(sysname),
+               to_string(nodename),
+               to_string(release),
+               to_string(version), 
+               to_string(machine),
+               to_string(domainname));
+}
\ No newline at end of file
diff --git a/tests/uname/uname001.phpt b/tests/uname/uname001.phpt
new file mode 100644 (file)
index 0000000..7f46723
--- /dev/null
@@ -0,0 +1,32 @@
+--TEST--
+uname
+--INI--
+psi.directory={PWD}
+--SKIPIF--
+<?php
+extension_loaded("psi") or die("skip - need ext/psi");
+?>
+--FILE--
+===TEST===
+<?php
+var_dump(psi\uname($uname), $uname);
+?>
+===DONE===
+--EXPECTF--
+===TEST===
+int(0)
+array(6) {
+  ["sysname"]=>
+  string(5) "Linux"
+  ["nodename"]=>
+  string(%d) "%s"
+  ["release"]=>
+  string(%d) "%s"
+  ["version"]=>
+  string(%d) "%s"
+  ["machine"]=>
+  string(%d) "%s"
+  ["domainname"]=>
+  string(%d) "%s"
+}
+===DONE===