fliush
authorMichael Wallner <mike@php.net>
Tue, 27 Oct 2015 11:33:52 +0000 (12:33 +0100)
committerMichael Wallner <mike@php.net>
Tue, 27 Oct 2015 11:33:52 +0000 (12:33 +0100)
src/module.c
src/parser.re
src/validator.c

index 4d6e5c9f03ca4b1c093e2f7bd51982b667003510..3e7de89e3be6d52baf9261f845b7fb3a772e0505 100644 (file)
@@ -142,6 +142,65 @@ void psi_to_string(impl_val *ret_val, decl_arg *func, zval *return_value)
        }
 }
 
        }
 }
 
+size_t psi_t_alignment(token_t t)
+{
+       size_t align;
+# define PSI_TAS_D(T) struct TAS_ ##T { \
+       char c; \
+       ##T x; \
+}
+# define PSI_TAS_C(T) align = offsetof(struct TAS_ ##T, x)
+# define PSI_TAS_CASE(T) { \
+       PSI_TAS_D(T); \
+       PSI_TAS_C(T); \
+}
+       switch (t) {
+       case PSI_T_CHAR:
+               PSI_TAS_CASE(char);
+               break;
+       case PSI_T_SINT8:
+               PSI_TAS_CASE(int8_t);
+               break;
+       case PSI_T_UINT8:
+               PSI_TAS_CASE(uint8_t);
+               break;
+       case PSI_T_SHORT:
+               PSI_TAS_CASE(short);
+               break;
+       case PSI_T_SINT16:
+               PSI_TAS_CASE(int16_t);
+               break;
+       case PSI_T_UINT16:
+               PSI_TAS_CASE(uint16_t);
+               break;
+       case PSI_T_INT:
+               PSI_TAS_CASE(int);
+               break;
+       case PSI_T_SINT32:
+               PSI_TAS_CASE(int32_t);
+               break;
+       case PSI_T_UINT32:
+               PSI_TAS_CASE(uint32_t);
+               break;
+       case PSI_T_LONG:
+               PSI_TAS_CASE(long);
+               break;
+       case PSI_T_SINT64:
+               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;
+       EMPTY_SWITCH_DEFAULT_CASE();
+       }
+}
+
 size_t psi_t_size(token_t t)
 {
        size_t size;
 size_t psi_t_size(token_t t)
 {
        size_t size;
@@ -186,6 +245,12 @@ size_t psi_t_size(token_t t)
        return size;
 }
 
        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)
 {
        size_t size = psi_t_size(t);
 static impl_val *iterate(impl_val *val, token_t t, unsigned i, impl_val *tmp)
 {
        size_t size = psi_t_size(t);
index 9cd39fac0990e70fa0abbff6078d6dd528375797..c134ffdf0e41020dbb485b6c11006beccf029fa2 100644 (file)
@@ -201,6 +201,7 @@ token_t PSI_ParserScan(PSI_Parser *P)
                ARRAY = 'array';
                FUNCTION = 'function';
                TYPEDEF = 'typedef';
                ARRAY = 'array';
                FUNCTION = 'function';
                TYPEDEF = 'typedef';
+               STRUCT = 'struct';
                CONST = 'const';
                LIB = 'lib';
                LET = 'let';
                CONST = 'const';
                LIB = 'lib';
                LET = 'let';
@@ -260,6 +261,7 @@ token_t PSI_ParserScan(PSI_Parser *P)
                ARRAY {RETURN(PSI_T_ARRAY);}
                FUNCTION {RETURN(PSI_T_FUNCTION);}
                TYPEDEF {RETURN(PSI_T_TYPEDEF);}
                ARRAY {RETURN(PSI_T_ARRAY);}
                FUNCTION {RETURN(PSI_T_FUNCTION);}
                TYPEDEF {RETURN(PSI_T_TYPEDEF);}
+               STRUCT {RETURN(PSI_T_STRUCT);}
                CONST {RETURN(PSI_T_CONST);}
                LIB {RETURN(PSI_T_LIB);}
                LET {RETURN(PSI_T_LET);}
                CONST {RETURN(PSI_T_CONST);}
                LIB {RETURN(PSI_T_LIB);}
                LET {RETURN(PSI_T_LET);}
index 44ea4ab11c2108385636c3707431dd6fa7e6aaef..19956b73f475079183c1697632062b39933abf47 100644 (file)
@@ -61,6 +61,7 @@ static int validate_lib(PSI_Validator *V) {
        }
        return 1;
 }
        }
        return 1;
 }
+
 static inline int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) {
        size_t i;
 
 static inline int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) {
        size_t i;
 
@@ -75,14 +76,38 @@ static inline int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) {
        }
        return 0;
 }
        }
        return 0;
 }
+static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type) {
+       size_t i;
+
+       if (type->strct) {
+               return 1;
+       }
+       for (i = 0; i < structs->count; ++i) {
+               if (!strcmp(structs->list[i]->name, type->name)) {
+                       type->strct = structs->list[i];
+                       return 1;
+               }
+       }
+       return 0;
+}
 static inline int validate_decl_type(PSI_Validator *V, decl_arg *arg, decl_type *type) {
 static inline int validate_decl_type(PSI_Validator *V, decl_arg *arg, decl_type *type) {
-       if (type->type == PSI_T_NAME) {
+       switch (type->type) {
+       case PSI_T_NAME:
                if (!V->defs || !locate_decl_type_alias(V->defs, type)) {
                        V->error(PSI_WARNING, "Cannot use '%s' as type for '%s';"
                if (!V->defs || !locate_decl_type_alias(V->defs, type)) {
                        V->error(PSI_WARNING, "Cannot use '%s' as type for '%s';"
-                               " Use 'typedef <type> <basic_type>;' statement",
+                               " Use 'typedef <type> <alias>;' statement",
+                               type->name, arg->var->name);
+                       return 0;
+               }
+               return validate_decl_type(V, arg, type->real);
+       case PSI_T_STRUCT:
+               if (!V->structs || !locate_decl_type_struct(V->structs, type)) {
+                       V->error(PSI_WARNING, "Cannot use '%s' as type for '%s';"
+                               " Use 'typedef struct <name> <alias>;' statement",
                                type->name, arg->var->name);
                        return 0;
                }
                                type->name, arg->var->name);
                        return 0;
                }
+               break;
        }
        return 1;
 }
        }
        return 1;
 }
@@ -142,17 +167,17 @@ static inline int validate_decl_abi(PSI_Validator *V, decl_abi *abi) {
        V->error(PSI_WARNING, "Invalid calling convention: '%s'", abi->convention);
        return 0;
 }
        V->error(PSI_WARNING, "Invalid calling convention: '%s'", abi->convention);
        return 0;
 }
-static inline int validate_decl_arg(PSI_Validator *V, decl *decl, decl_arg *arg) {
+static inline int validate_decl_arg(PSI_Validator *V, decl_arg *arg) {
        if (!validate_decl_type(V, arg, arg->type)) {
                return 0;
        }
        return 1;
 }
        if (!validate_decl_type(V, arg, arg->type)) {
                return 0;
        }
        return 1;
 }
-static inline int validate_decl_args(PSI_Validator *V, decl *decl, decl_args *args) {
+static inline int validate_decl_args(PSI_Validator *V, decl_args *args) {
        size_t i;
 
        for (i = 0; i < args->count; ++i) {
        size_t i;
 
        for (i = 0; i < args->count; ++i) {
-               if (!validate_decl_arg(V, decl, args->args[i])) {
+               if (!validate_decl_arg(V, args->args[i])) {
                        return 0;
                }
        }
                        return 0;
                }
        }
@@ -165,7 +190,7 @@ static inline int validate_decl(PSI_Validator *V, decl *decl) {
        if (!validate_decl_func(V, decl, decl->func)) {
                return 0;
        }
        if (!validate_decl_func(V, decl, decl->func)) {
                return 0;
        }
-       if (decl->args && !validate_decl_args(V, decl, decl->args)) {
+       if (decl->args && !validate_decl_args(V, decl->args)) {
                return 0;
        }
        return 1;
                return 0;
        }
        return 1;
@@ -180,6 +205,37 @@ static inline int validate_decls(PSI_Validator *V) {
        }
        return 1;
 }
        }
        return 1;
 }
+static inline int validate_struct(PSI_Validator *V, decl_struct *s) {
+       size_t i;
+
+       if (!validate_decl_args(V, s->args)) {
+               return 0;
+       }
+
+       s->layout = calloc(s->args->count, sizeof(*s->layout));
+       for (i = 0; i < s->args->count; ++i) {
+               decl_type *t = real_decl_type(s->args->args[i]->type);
+
+               if (i) {
+                       decl_struct_layout *l = &s->layout[i-1];
+                       s->layout[i].pos = psi_t_align(t->type, l->pos + l->size);
+               } else {
+                       s->layout[i].pos = 0;
+               }
+               s->layout[i].len = psi_t_size(t->type);
+       }
+       return 1;
+}
+static inline int validate_structs(PSI_Validator *V) {
+       size_t i;
+
+       for (i = 0; i < V->structs->count; ++i) {
+               if (!validate_struct(V->structs->list[i])) {
+                       return 0;
+               }
+       }
+       return 1;
+}
 
 static inline int validate_impl_type(PSI_Validator *V, impl *impl, impl_type *type) {
        /* FIXME */
 
 static inline int validate_impl_type(PSI_Validator *V, impl *impl, impl_type *type) {
        /* FIXME */
@@ -406,6 +462,9 @@ int PSI_ValidatorValidate(PSI_Validator *V)
        if (V->defs && !validate_typedefs(V)) {
                return 0;
        }
        if (V->defs && !validate_typedefs(V)) {
                return 0;
        }
+       if (V->structs && !validate_structs(V)) {
+               return 0;
+       }
        if (V->decls && !validate_decls(V)) {
                return 0;
        }
        if (V->decls && !validate_decls(V)) {
                return 0;
        }