poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2] pkl: Fix bit-offset of mapped integral struct


From: Mohammad-Reza Nabipoor
Subject: [PATCH v2] pkl: Fix bit-offset of mapped integral struct
Date: Thu, 28 Jan 2021 22:01:38 +0330

2021-01-28  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>

        * libpoke/pkl-gen.pks (struct_writer): Fix bit-offset of mapped
        integral struct to use the real offset, not zero.
        * testsuite/poke.map/maps-int-structs-21.pk: New test.
        * testsuite/poke.map/maps-int-structs-22.pk: Likewise.
        * testsuite/poke.map/maps-int-structs-23.pk: Likewise.
        * testsuite/poke.map/maps-int-structs-24.pk: Likewise.
        * testsuite/poke.map/maps-int-structs-25.pk: Likewise.
        * testsuite/poke.map/maps-int-structs-26.pk: Likewise.
        * testsuite/poke.map/maps-int-structs-27.pk: Likewise.
        * testsuite/poke.map/maps-int-structs-28.pk: Likewise.
        * testsuite/Makefile.am (EXTRA_DIST): Update.
---

Hi, Jose.

The fix with tests!


## Off-topic proposal

When I was writing the function `t` in `maps-int-structs-2[5-8].pk` tests.
I thought it'd be nice to have the following syntax to access struct field
dynamically:

```
type S = struct { int a; long b; };
var s = S{};
var a = "a",
    b = "b";

s.("a") = 10;
s.("b") = 20;

s.(a) = 1;
s.(b) = 2;
```

This syntax is inspired by MATLAB programming language.

WDYT?
(Of course this is not for Poke 1.0!)


Regards,
Mohammad-Reza


 ChangeLog                                 | 14 ++++++
 libpoke/pkl-gen.pks                       |  7 +--
 testsuite/Makefile.am                     |  8 ++++
 testsuite/poke.map/maps-int-structs-21.pk | 18 ++++++++
 testsuite/poke.map/maps-int-structs-22.pk | 18 ++++++++
 testsuite/poke.map/maps-int-structs-23.pk | 22 +++++++++
 testsuite/poke.map/maps-int-structs-24.pk | 22 +++++++++
 testsuite/poke.map/maps-int-structs-25.pk | 53 ++++++++++++++++++++++
 testsuite/poke.map/maps-int-structs-26.pk | 53 ++++++++++++++++++++++
 testsuite/poke.map/maps-int-structs-27.pk | 54 +++++++++++++++++++++++
 testsuite/poke.map/maps-int-structs-28.pk | 54 +++++++++++++++++++++++
 11 files changed, 320 insertions(+), 3 deletions(-)
 create mode 100644 testsuite/poke.map/maps-int-structs-21.pk
 create mode 100644 testsuite/poke.map/maps-int-structs-22.pk
 create mode 100644 testsuite/poke.map/maps-int-structs-23.pk
 create mode 100644 testsuite/poke.map/maps-int-structs-24.pk
 create mode 100644 testsuite/poke.map/maps-int-structs-25.pk
 create mode 100644 testsuite/poke.map/maps-int-structs-26.pk
 create mode 100644 testsuite/poke.map/maps-int-structs-27.pk
 create mode 100644 testsuite/poke.map/maps-int-structs-28.pk

diff --git a/ChangeLog b/ChangeLog
index 1b84a5da..dad23d77 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2021-01-28  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>
+
+       * libpoke/pkl-gen.pks (struct_writer): Fix bit-offset of mapped
+       integral struct.
+       * testsuite/poke.map/maps-int-structs-21.pk: New test.
+       * testsuite/poke.map/maps-int-structs-22.pk: Likewise.
+       * testsuite/poke.map/maps-int-structs-23.pk: Likewise.
+       * testsuite/poke.map/maps-int-structs-24.pk: Likewise.
+       * testsuite/poke.map/maps-int-structs-25.pk: Likewise.
+       * testsuite/poke.map/maps-int-structs-26.pk: Likewise.
+       * testsuite/poke.map/maps-int-structs-27.pk: Likewise.
+       * testsuite/poke.map/maps-int-structs-28.pk: Likewise.
+       * testsuite/Makefile.am (EXTRA_DIST): Update.
+
 2021-01-26  Jose E. Marchesi  <jemarch@gnu.org>
 
        * libpoke/pkl-env.c (get_registered): The empty string is never
diff --git a/libpoke/pkl-gen.pks b/libpoke/pkl-gen.pks
index 7b050be0..1adab82e 100644
--- a/libpoke/pkl-gen.pks
+++ b/libpoke/pkl-gen.pks
@@ -1624,9 +1624,10 @@
  .c {
         pushvar $sct            ; SCT
         mgetios                 ; SCT IOS
-        nip                     ; IOS
-        push ulong<64>0         ; IOS 0UL
-        pushvar $ivalue         ; IOS 0UL IVAL
+        swap                    ; IOS SCT
+        mgeto                   ; IOS SCT BOFF
+        nip                     ; IOS BOFF
+        pushvar $ivalue         ; IOS BOFF IVAL
  .c     PKL_PASS_SUBPASS (@struct_itype);
  .c }
         popf 1
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 15c490ce..6f92528b 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -237,6 +237,14 @@ EXTRA_DIST = \
   poke.map/maps-int-structs-18.pk \
   poke.map/maps-int-structs-19.pk \
   poke.map/maps-int-structs-20.pk \
+  poke.map/maps-int-structs-21.pk \
+  poke.map/maps-int-structs-22.pk \
+  poke.map/maps-int-structs-23.pk \
+  poke.map/maps-int-structs-24.pk \
+  poke.map/maps-int-structs-25.pk \
+  poke.map/maps-int-structs-26.pk \
+  poke.map/maps-int-structs-27.pk \
+  poke.map/maps-int-structs-28.pk \
   poke.map/maps-ios-1.pk \
   poke.map/maps-ios-2.pk \
   poke.map/maps-ios-3.pk \
diff --git a/testsuite/poke.map/maps-int-structs-21.pk 
b/testsuite/poke.map/maps-int-structs-21.pk
new file mode 100644
index 00000000..2f3132cc
--- /dev/null
+++ b/testsuite/poke.map/maps-int-structs-21.pk
@@ -0,0 +1,18 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+type Foo =
+  struct
+  {
+    uint<8> a;
+    struct uint<32>
+    {
+      uint<16> b1;
+      uint<16> b2;
+    } b;
+  };
+
+/* { dg-command { .set endian little } } */
+/* { dg-command { .set obase 16} } */
+/* { dg-command { Foo @ 0#B } } */
+/* { dg-output {Foo {a=0x10UB,b=struct {b1=0x5040UH,b2=0x3020UH}}\n} } */
diff --git a/testsuite/poke.map/maps-int-structs-22.pk 
b/testsuite/poke.map/maps-int-structs-22.pk
new file mode 100644
index 00000000..0d933e29
--- /dev/null
+++ b/testsuite/poke.map/maps-int-structs-22.pk
@@ -0,0 +1,18 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+type Foo =
+  struct
+  {
+    uint<8> a;
+    struct uint<32>
+    {
+      uint<16> b1;
+      uint<16> b2;
+    } b;
+  };
+
+/* { dg-command { .set endian little } } */
+/* { dg-command { .set obase 16} } */
+/* { dg-command { Foo @ 1#B } } */
+/* { dg-output {Foo {a=0x20UB,b=struct {b1=0x6050UH,b2=0x4030UH}}} } */
diff --git a/testsuite/poke.map/maps-int-structs-23.pk 
b/testsuite/poke.map/maps-int-structs-23.pk
new file mode 100644
index 00000000..1b8eba1f
--- /dev/null
+++ b/testsuite/poke.map/maps-int-structs-23.pk
@@ -0,0 +1,22 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+type Bar =
+  struct uint<32>
+  {
+    uint<16> a1;
+    uint<16> a2;
+  };
+
+type Foo =
+  struct
+  {
+    uint<16> a;
+    Bar b;
+    uint<8> c;
+  };
+
+/* { dg-command { .set endian little } } */
+/* { dg-command { .set obase 16} } */
+/* { dg-command { Foo @ 0#B } } */
+/* { dg-output {Foo {a=0x2010UH,b=Bar {a1=0x6050UH,a2=0x4030UH},c=0x70UB}\n} } 
*/
diff --git a/testsuite/poke.map/maps-int-structs-24.pk 
b/testsuite/poke.map/maps-int-structs-24.pk
new file mode 100644
index 00000000..024831b7
--- /dev/null
+++ b/testsuite/poke.map/maps-int-structs-24.pk
@@ -0,0 +1,22 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+type Bar =
+  struct uint<32>
+  {
+    uint<16> a1;
+    uint<16> a2;
+  };
+
+type Foo =
+  struct
+  {
+    uint<16> a;
+    Bar b;
+    uint<8> c;
+  };
+
+/* { dg-command { .set endian little } } */
+/* { dg-command { .set obase 16} } */
+/* { dg-command { Foo @ 1#B } } */
+/* { dg-output {Foo {a=0x3020UH,b=Bar {a1=0x7060UH,a2=0x5040UH},c=0x80UB}\n} } 
*/
diff --git a/testsuite/poke.map/maps-int-structs-25.pk 
b/testsuite/poke.map/maps-int-structs-25.pk
new file mode 100644
index 00000000..cb69b303
--- /dev/null
+++ b/testsuite/poke.map/maps-int-structs-25.pk
@@ -0,0 +1,53 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+type Foo =
+  struct
+  {
+    type UI32 =
+      struct uint<32>
+      {
+        uint<16> b1;
+        uint<16> b2;
+      };
+
+    uint<8> a;
+    UI32 b;
+  };
+
+fun t = (Foo foo, uint<16> old1, uint<16> new1,
+         uint<16> old2, uint<16> new2) int<32>:
+  {
+    var i = 0;
+
+    /* check current expected values */
+    if (foo.b.b1 != old1)
+      return ++i;
+    if (foo.b.b2 != old2)
+      return ++i;
+
+    /* set new values */
+    foo.b.b1 = new1;
+    if (foo.b.b1 != new1)
+      return ++i;
+    foo.b.b2 = new2;
+    if (foo.b.b2 != new2)
+      return ++i;
+
+    /* revert to old values */
+    foo.b.b1 = old1;
+    if (foo.b.b1 != old1)
+      return ++i;
+    foo.b.b2 = old2;
+    if (foo.b.b2 != old2)
+      return ++i;
+
+    return 0;
+  }
+
+/* { dg-command { .set endian little } } */
+/* { dg-command { .set obase 16} } */
+/* { dg-command { t (Foo @ 0#B, 0x5040UH, 0x4050UH, 0x3020UH, 0x2030UH);  } } 
*/
+/* { dg-output {0x0\n} } */
+/* { dg-command { Foo @ 0#B } } */
+/* { dg-output {Foo {a=0x10UB,b=UI32 {b1=0x5040UH,b2=0x3020UH}}\n} } */
diff --git a/testsuite/poke.map/maps-int-structs-26.pk 
b/testsuite/poke.map/maps-int-structs-26.pk
new file mode 100644
index 00000000..6be676ca
--- /dev/null
+++ b/testsuite/poke.map/maps-int-structs-26.pk
@@ -0,0 +1,53 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+type Foo =
+  struct
+  {
+    type UI32 =
+      struct uint<32>
+      {
+        uint<16> b1;
+        uint<16> b2;
+      };
+
+    uint<8> a;
+    UI32 b;
+  };
+
+fun t = (Foo foo, uint<16> old1, uint<16> new1,
+         uint<16> old2, uint<16> new2) int<32>:
+  {
+    var i = 0;
+
+    /* check current expected values */
+    if (foo.b.b1 != old1)
+      return ++i;
+    if (foo.b.b2 != old2)
+      return ++i;
+
+    /* set new values */
+    foo.b.b1 = new1;
+    if (foo.b.b1 != new1)
+      return ++i;
+    foo.b.b2 = new2;
+    if (foo.b.b2 != new2)
+      return ++i;
+
+    /* revert to old values */
+    foo.b.b1 = old1;
+    if (foo.b.b1 != old1)
+      return ++i;
+    foo.b.b2 = old2;
+    if (foo.b.b2 != old2)
+      return ++i;
+
+    return 0;
+  }
+
+/* { dg-command { .set endian little } } */
+/* { dg-command { .set obase 16} } */
+/* { dg-command { t (Foo @ 1#B, 0x6050UH, 0x5060UH, 0x4030UH, 0x3040UH);  } } 
*/
+/* { dg-output {0x0\n} } */
+/* { dg-command { Foo @ 1#B } } */
+/* { dg-output {Foo {a=0x20UB,b=UI32 {b1=0x6050UH,b2=0x4030UH}}} } */
diff --git a/testsuite/poke.map/maps-int-structs-27.pk 
b/testsuite/poke.map/maps-int-structs-27.pk
new file mode 100644
index 00000000..11c0912d
--- /dev/null
+++ b/testsuite/poke.map/maps-int-structs-27.pk
@@ -0,0 +1,54 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+type Bar =
+  struct uint<32>
+  {
+    uint<16> a1;
+    uint<16> a2;
+  };
+
+type Foo =
+  struct
+  {
+    uint<16> a;
+    Bar b;
+    uint<8> c;
+  };
+
+fun t = (Foo foo, uint<16> old1, uint<16> new1,
+         uint<16> old2, uint<16> new2) int<32>:
+  {
+    var i = 0;
+
+    /* check current expected values */
+    if (foo.b.a1 != old1)
+      return ++i;
+    if (foo.b.a2 != old2)
+      return ++i;
+
+    /* set new values */
+    foo.b.a1 = new1;
+    if (foo.b.a1 != new1)
+      return ++i;
+    foo.b.a2 = new2;
+    if (foo.b.a2 != new2)
+      return ++i;
+
+    /* revert to old values */
+    foo.b.a1 = old1;
+    if (foo.b.a1 != old1)
+      return ++i;
+    foo.b.a2 = old2;
+    if (foo.b.a2 != old2)
+      return ++i;
+
+    return 0;
+  }
+
+/* { dg-command { .set endian little } } */
+/* { dg-command { .set obase 16} } */
+/* { dg-command { t (Foo @ 0#B, 0x6050UH, 0xaabbUH, 0x4030UH, 0xccddUH); } } */
+/* { dg-output {0x0\n} } */
+/* { dg-command { Foo @ 0#B } } */
+/* { dg-output {Foo {a=0x2010UH,b=Bar {a1=0x6050UH,a2=0x4030UH},c=0x70UB}\n} } 
*/
diff --git a/testsuite/poke.map/maps-int-structs-28.pk 
b/testsuite/poke.map/maps-int-structs-28.pk
new file mode 100644
index 00000000..580bed89
--- /dev/null
+++ b/testsuite/poke.map/maps-int-structs-28.pk
@@ -0,0 +1,54 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+type Bar =
+  struct uint<32>
+  {
+    uint<16> a1;
+    uint<16> a2;
+  };
+
+type Foo =
+  struct
+  {
+    uint<16> a;
+    Bar b;
+    uint<8> c;
+  };
+
+fun t = (Foo foo, uint<16> old1, uint<16> new1,
+         uint<16> old2, uint<16> new2) int<32>:
+  {
+    var i = 0;
+
+    /* check current expected values */
+    if (foo.b.a1 != old1)
+      return ++i;
+    if (foo.b.a2 != old2)
+      return ++i;
+
+    /* set new values */
+    foo.b.a1 = new1;
+    if (foo.b.a1 != new1)
+      return ++i;
+    foo.b.a2 = new2;
+    if (foo.b.a2 != new2)
+      return ++i;
+
+    /* revert to old values */
+    foo.b.a1 = old1;
+    if (foo.b.a1 != old1)
+      return ++i;
+    foo.b.a2 = old2;
+    if (foo.b.a2 != old2)
+      return ++i;
+
+    return 0;
+  }
+
+/* { dg-command { .set endian little } } */
+/* { dg-command { .set obase 16} } */
+/* { dg-command { t (Foo @ 1#B, 0x7060UH, 0xaabbUH, 0x5040UH, 0xccddUH); } } */
+/* { dg-output {0x0\n} } */
+/* { dg-command { Foo @ 1#B } } */
+/* { dg-output {Foo {a=0x3020UH,b=Bar {a1=0x7060UH,a2=0x5040UH},c=0x80UB}\n} } 
*/
-- 
2.30.0



reply via email to

[Prev in Thread] Current Thread [Next in Thread]