cvsuser     03/10/04 04:57:16

  Modified:    classes  managedstruct.pmc unmanagedstruct.pmc
               .        nci_test.c
               t/pmc    nci.t
  Log:
  float/double element access for struct
  
  Revision  Changes    Path
  1.16      +8 -3      parrot/classes/managedstruct.pmc
  
  Index: managedstruct.pmc
  ===================================================================
  RCS file: /cvs/public/parrot/classes/managedstruct.pmc,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -w -r1.15 -r1.16
  --- managedstruct.pmc 3 Oct 2003 15:17:06 -0000       1.15
  +++ managedstruct.pmc 4 Oct 2003 11:57:03 -0000       1.16
  @@ -1,7 +1,7 @@
   /*
    *  Copyright: 2001-2003 The Perl Foundation.  All Rights Reserved.
    *  CVS Info
  - *     $Id: managedstruct.pmc,v 1.15 2003/10/03 15:17:06 leo Exp $
  + *     $Id: managedstruct.pmc,v 1.16 2003/10/04 11:57:03 leo Exp $
    *  Overview:
    *     PMC class to hold structs that parrot's responsible for disposing of
    *  Data Structure and Algorithms:
  @@ -61,8 +61,13 @@
           ix = key_integer(INTERP, key);
           if (ix < 0 || ix >= SELF->cache.int_val)
               return;
  -        ((char *)PMC_data(SELF))[ix] = (0xff & value);
  +        SUPER(key, value);
  +    }
  +
  +    void set_integer_keyed_int (INTVAL ix, INTVAL value) {
  +        if (ix < 0 || ix >= SELF->cache.int_val)
           return;
  +        SUPER(ix, value);
       }
   
       INTVAL get_integer () {
  
  
  
  1.18      +111 -27   parrot/classes/unmanagedstruct.pmc
  
  Index: unmanagedstruct.pmc
  ===================================================================
  RCS file: /cvs/public/parrot/classes/unmanagedstruct.pmc,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -w -r1.17 -r1.18
  --- unmanagedstruct.pmc       4 Oct 2003 11:11:05 -0000       1.17
  +++ unmanagedstruct.pmc       4 Oct 2003 11:57:03 -0000       1.18
  @@ -1,7 +1,7 @@
   /*
    *  Copyright: 2001-2003 The Perl Foundation.  All Rights Reserved.
    *  CVS Info
  - *     $Id: unmanagedstruct.pmc,v 1.17 2003/10/04 11:11:05 leo Exp $
  + *     $Id: unmanagedstruct.pmc,v 1.18 2003/10/04 11:57:03 leo Exp $
    *  Overview:
    *     PMC class to hold structs that parrot's not responsible for
    *     disposing of.
  @@ -17,12 +17,6 @@
    */
   
   #include "parrot/parrot.h"
  -#include "parrot/method_util.h"
  -#include "parrot/pobj.h"
  -
  -enum {
  -    PObj_UnManagedStruct_my_memory_FLAG = PObj_private0_FLAG
  -};
   
   static char *
   char_offset_int(Parrot_Interp interpreter, PMC *pmc, INTVAL ix, int *type)
  @@ -64,12 +58,13 @@
   ret_int(char *p, int type)
   {
       switch (type) {
  +     case enum_type_INTVAL:
        case enum_type_int:
            return *(INTVAL*) p;
        case enum_type_short:
            return *(short*) p;
  -     case enum_type_uchar:
        case enum_type_char:
  +     case enum_type_uchar:
            return *p;
        default:
            internal_exception(1, "unhandled type in struct");
  @@ -77,6 +72,61 @@
       return -1;
   }
   
  +static FLOATVAL
  +ret_float(char *p, int type)
  +{
  +    switch (type) {
  +     case enum_type_FLOATVAL:
  +         return (FLOATVAL) *(FLOATVAL*) p;
  +     case enum_type_float:
  +         return (FLOATVAL) *(float*) p;
  +     case enum_type_double:
  +         return (FLOATVAL) *(double*) p;
  +     default:
  +         internal_exception(1, "unhandled type in struct");
  +    }
  +    return -1.0;
  +}
  +
  +static void
  +set_int(char *p, int type, INTVAL value)
  +{
  +    switch (type) {
  +     case enum_type_char:
  +     case enum_type_uchar:
  +         *(char*) p = value & 0xff;
  +         break;
  +     case enum_type_INTVAL:
  +         *(INTVAL*) p = value;
  +         break;
  +     case enum_type_int:
  +         *(int*) p = value;
  +         break;
  +     case enum_type_short:
  +         *(short*)p = (short)value;
  +         break;
  +     default:
  +         internal_exception(1, "unhandled type in struct");
  +         break;
  +    }
  +}
  +
  +static void
  +set_float(char *p, int type, FLOATVAL value)
  +{
  +    switch (type) {
  +     case enum_type_FLOATVAL:
  +         *(FLOATVAL*) p = (FLOATVAL) value;
  +         break;
  +     case enum_type_float:
  +         *(float*) p = (float) value;
  +         break;
  +     case enum_type_double:
  +         *(double*) p = (double) value;
  +         break;
  +    }
  +}
  +
   pmclass UnManagedStruct extends default need_ext {
   
       void init() {
  @@ -123,6 +173,7 @@
       }
   
       void mark() {
  +     if (PMC_ptr2p(SELF))
        pobject_lives(interpreter, (PObj *) PMC_ptr2p(SELF));
       }
   
  @@ -158,11 +209,11 @@
        int type;
        char *p;
   
  -     if (!key)
  -            return -1;
        /* assume char array */
        if (!PMC_ptr2p(SELF)) {
            ix = key_integer(INTERP, key);
  +         if (ix < 0)
  +             return -1;
            type = enum_type_char;
            p = ((char *)PMC_data(SELF)) + ix;
        }
  @@ -171,35 +222,68 @@
        return ret_int(p, type);
       }
   
  +    FLOATVAL get_number_keyed_int (INTVAL key) {
  +     int type;
  +     char *p = char_offset_int(interpreter, pmc, key, &type);
  +     return ret_float(p, type);
  +    }
  +
  +    FLOATVAL get_number_keyed (PMC* key) {
  +     int type;
  +     char *p = char_offset_key(interpreter, pmc, key, &type);
  +     return ret_float(p, type);
  +    }
  +
       INTVAL get_integer() {
          return (INTVAL)*(INTVAL *)PMC_data(SELF);
       }
   
  +    void set_integer_native(INTVAL value) {
  +       *(INTVAL *)PMC_data(SELF) = value;
  +    }
  +
  +    void set_integer_keyed_int (INTVAL ix, INTVAL value) {
  +     int type;
  +     char *p;
  +     if (!PMC_ptr2p(SELF)) {
  +         if (ix < 0)
  +             return;
  +         type = enum_type_char;
  +         p = ((char *)PMC_data(SELF)) + ix;
  +     }
  +     else
  +         p = char_offset_int(interpreter, pmc, ix, &type);
  +     set_int(p, type, value);
  +    }
  +
       /* May cause segfaults if value is out of bounds */
       void set_integer_keyed (PMC* key, INTVAL value) {
        INTVAL ix;
        int type;
        char *p;
   
  -     if (!key)
  -            return;
  +     if (!PMC_ptr2p(SELF)) {
        ix = key_integer(INTERP, key);
        if (ix < 0)
               return;
  -     if (!PMC_ptr2p(SELF)) {
  -         ((char *)PMC_data(SELF))[ix]=(0xff & value);
  -         return;
  +         type = enum_type_char;
  +         p = ((char *)PMC_data(SELF)) + ix;
        }
  +     else
        p = char_offset_key(interpreter, pmc, key, &type);
  -     switch (type) {
  -         case enum_type_int:
  -             *(INTVAL*) p = value;
  -         default:
  -             internal_exception(1, "unhandled type in struct");
  +     set_int(p, type, value);
        }
  +
  +    void set_number_keyed_int (INTVAL key, FLOATVAL value) {
  +     int type;
  +     char *p = char_offset_int(interpreter, pmc, key, &type);
  +     set_float(p, type, value);
       }
   
  -    void set_integer_native(INTVAL value) {
  -       *(INTVAL *)PMC_data(SELF) = value;
  +    void set_number_keyed (PMC *key, FLOATVAL value) {
  +     int type;
  +     char *p = char_offset_key(interpreter, pmc, key, &type);
  +     set_float(p, type, value);
       }
  +
   }
  
  
  
  1.9       +11 -0     parrot/nci_test.c
  
  Index: nci_test.c
  ===================================================================
  RCS file: /cvs/public/parrot/nci_test.c,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -w -r1.8 -r1.9
  --- nci_test.c        4 Oct 2003 11:11:17 -0000       1.8
  +++ nci_test.c        4 Oct 2003 11:57:10 -0000       1.9
  @@ -89,6 +89,17 @@
                   };
                   return &t;
               }
  +        case 1:
  +            {
  +                static struct {
  +                    float f[2];
  +                    double d;
  +                } t = {
  +                    {42.0, 100.0},
  +                    47.11
  +                };
  +                return &t;
  +            }
       }
       return NULL;
   }
  
  
  
  1.18      +32 -1     parrot/t/pmc/nci.t
  
  Index: nci.t
  ===================================================================
  RCS file: /cvs/public/parrot/t/pmc/nci.t,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -w -r1.17 -r1.18
  --- nci.t     4 Oct 2003 11:11:24 -0000       1.17
  +++ nci.t     4 Oct 2003 11:57:16 -0000       1.18
  @@ -1,4 +1,4 @@
  -use Parrot::Test tests => 15;
  +use Parrot::Test tests => 16;
   use Parrot::Config;
   
   print STDERR $PConfig{jitcpuarch}, " JIT CPU\n";
  @@ -408,6 +408,37 @@
   42
   100
   66
  +OUTPUT
  +
  +output_is(<<'CODE', <<'OUTPUT', "nci_p_i");
  +  loadlib P1, "libnci"
  +  dlfunc P0, P1, "nci_pi", "pi"
  +  # this test function returns a struct { float[2]; double }
  +  set I5, 1
  +  invoke
  +  new P2, .PerlArray
  +.include "datatypes.pasm"
  +  push P2, .DATATYPE_FLOAT
  +  push P2, 2 # 2 elem array
  +  push P2, 0
  +  push P2, .DATATYPE_DOUBLE
  +  push P2, 0
  +  push P2, 0
  +  assign P5, P2
  +  set N0, P5[0;0]
  +  print N0
  +  print "\n"
  +  set N0, P5[0;1]
  +  print N0
  +  print "\n"
  +  set N0, P5[1]
  +  print N0
  +  print "\n"
  +  end
  +CODE
  +42.000000
  +100.000000
  +47.110000
   OUTPUT
   
   } # SKIP
  
  
  

Reply via email to