passing structs
authorMichael Wallner <mike@php.net>
Mon, 18 Jan 2016 13:56:38 +0000 (14:56 +0100)
committerMichael Wallner <mike@php.net>
Mon, 18 Jan 2016 13:56:38 +0000 (14:56 +0100)
m4/stdlib.m4
psi.d/stdlib.psi
src/libffi.c
src/parser.h

index c7654b67cf38f2b99c46a3319fdf5d2f91fa11dd..de4c688a60cba541b63f05e789baace4aab5476f 100644 (file)
@@ -22,7 +22,7 @@ PSI_CHECK_STDLIB() {
        PSI_DECL(double atof, [(char *str)])
        PSI_DECL(int atoi, [(char *str)])
        PSI_DECL(long atol, [(char *str)])
-       dnl PSI_DECL(div_t div, [(int numerator, int denominator)])
+       PSI_DECL(div_t div, [(int numerator, int denominator)])
        PSI_DECL(double drand48, [()])
        PSI_DECL(double erand48, [(unsigned short xsubi@<:@3@:>@)])
        PSI_DECL(void exit, [(int status)])
index 2400614228c4d8118effc0e658c970e4c951dcd7..1229c9e2d53d1e718cf1cf884ec925cfd1ca8124 100644 (file)
@@ -2,3 +2,9 @@ function psi\abs(int $i) : int {
        let n = intval($i);
        return to_int(abs);
 }
+
+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
index 776e98c143d0fb309c20aa3b096aa60a6a3d49a6..ac018a119640d8ea0deed03ba479cf1e6c18a260 100644 (file)
@@ -109,8 +109,45 @@ static inline ffi_type *psi_ffi_impl_type(token_t impl_type) {
        EMPTY_SWITCH_DEFAULT_CASE();
        }
 }
+static void psi_ffi_struct_type_dtor(void *type) {
+       ffi_type *strct = type;
+
+       if (strct->elements) {
+               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;
+
+       for (i = 0; i < strct->args->count; ++i) {
+               els[i] = psi_ffi_decl_arg_type(strct->args->args[i]);
+       }
+       els[i] = NULL;
+
+       return els;
+}
 static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
-       return psi_ffi_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) {
+                       ffi_type *strct = calloc(1, sizeof(ffi_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;
+                       real->strct->engine.dtor = psi_ffi_struct_type_dtor;
+               }
+
+               return real->strct->engine.type;
+       }
+       return psi_ffi_token_type(real->type);
 }
 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
        if (darg->var->pointer_level) {
index b27fdafccbe96ec22286b64f83f3d39b117267a1..29bf9d326ce17727b66bd18969c9d774110de184 100644 (file)
@@ -377,6 +377,10 @@ typedef struct decl_struct {
        char *name;
        decl_args *args;
        size_t size;
+       struct {
+               void *type;
+               void (*dtor)(void *type);
+       } engine;
 } decl_struct;
 
 static inline decl_struct *init_decl_struct(const char *name, decl_args *args) {