poke-devel
[Top][All Lists]
Advanced

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

[PATCH 5/5] pkl: Compute attributes at compile-time whenever possible


From: Mohammad-Reza Nabipoor
Subject: [PATCH 5/5] pkl: Compute attributes at compile-time whenever possible
Date: Sat, 22 Jan 2022 23:37:42 +0330

2022-01-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * libpoke/pkl-trans.c (pkl_trans2_ps_op_attr): New phase for
        computing attributes of complete type at compile-time.
        (struct pkl_phase pkl_phase_trans2): Register new phase.
        * testsuite/poke.map/map-size-2.pk: New test.
        * testsuite/poke.map/map-size-3.pk: Likewise.
        * testsuite/poke.map/maps-structs-pinned-3.pk: Likewise.
        * testsuite/poke.pkl/attr-length-10.pk: Likewise.
        * testsuite/poke.pkl/attr-length-11.pk: Likewise.
        * testsuite/poke.pkl/attr-size-15.pk: Likewise.
        * testsuite/poke.pkl/attr-size-16.pk: Likewise.
        * testsuite/poke.pkl/attr-size-17.pk: Likewise.
        * testsuite/poke.pkl/attr-size-18.pk: Likewise.
        * testsuite/poke.pkl/attr-size-19.pk: Likewise.
        * testsuite/poke.pkl/attr-size-20.pk: Likewise.
        * testsuite/poke.pkl/attr-size-21.pk: Likewise.
        * testsuite/poke.pkl/attr-unit-2.pk: Likewise.
        * testsuite/poke.pkl/attr-unit-3.pk: Likewise.
        * testsuite/Makefile.am (EXTRA_DIST): Update.
---
 ChangeLog                                   | 21 ++++++
 libpoke/pkl-trans.c                         | 77 +++++++++++++++++++++
 testsuite/Makefile.am                       | 14 ++++
 testsuite/poke.map/map-size-2.pk            | 20 ++++++
 testsuite/poke.map/map-size-3.pk            | 24 +++++++
 testsuite/poke.map/maps-structs-pinned-3.pk | 10 +++
 testsuite/poke.pkl/attr-length-10.pk        |  6 ++
 testsuite/poke.pkl/attr-length-11.pk        |  6 ++
 testsuite/poke.pkl/attr-size-15.pk          |  7 ++
 testsuite/poke.pkl/attr-size-16.pk          |  7 ++
 testsuite/poke.pkl/attr-size-17.pk          |  6 ++
 testsuite/poke.pkl/attr-size-18.pk          |  7 ++
 testsuite/poke.pkl/attr-size-19.pk          |  6 ++
 testsuite/poke.pkl/attr-size-20.pk          |  8 +++
 testsuite/poke.pkl/attr-size-21.pk          |  8 +++
 testsuite/poke.pkl/attr-unit-2.pk           |  6 ++
 testsuite/poke.pkl/attr-unit-3.pk           |  7 ++
 17 files changed, 240 insertions(+)
 create mode 100644 testsuite/poke.map/map-size-2.pk
 create mode 100644 testsuite/poke.map/map-size-3.pk
 create mode 100644 testsuite/poke.map/maps-structs-pinned-3.pk
 create mode 100644 testsuite/poke.pkl/attr-length-10.pk
 create mode 100644 testsuite/poke.pkl/attr-length-11.pk
 create mode 100644 testsuite/poke.pkl/attr-size-15.pk
 create mode 100644 testsuite/poke.pkl/attr-size-16.pk
 create mode 100644 testsuite/poke.pkl/attr-size-17.pk
 create mode 100644 testsuite/poke.pkl/attr-size-18.pk
 create mode 100644 testsuite/poke.pkl/attr-size-19.pk
 create mode 100644 testsuite/poke.pkl/attr-size-20.pk
 create mode 100644 testsuite/poke.pkl/attr-size-21.pk
 create mode 100644 testsuite/poke.pkl/attr-unit-2.pk
 create mode 100644 testsuite/poke.pkl/attr-unit-3.pk

diff --git a/ChangeLog b/ChangeLog
index 82061c3f..218b299d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+2022-01-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * libpoke/pkl-trans.c (pkl_trans2_ps_op_attr): New phase for
+       computing attributes of complete type at compile-time.
+       (struct pkl_phase pkl_phase_trans2): Register new phase.
+       * testsuite/poke.map/map-size-2.pk: New test.
+       * testsuite/poke.map/map-size-3.pk: Likewise.
+       * testsuite/poke.map/maps-structs-pinned-3.pk: Likewise.
+       * testsuite/poke.pkl/attr-length-10.pk: Likewise.
+       * testsuite/poke.pkl/attr-length-11.pk: Likewise.
+       * testsuite/poke.pkl/attr-size-15.pk: Likewise.
+       * testsuite/poke.pkl/attr-size-16.pk: Likewise.
+       * testsuite/poke.pkl/attr-size-17.pk: Likewise.
+       * testsuite/poke.pkl/attr-size-18.pk: Likewise.
+       * testsuite/poke.pkl/attr-size-19.pk: Likewise.
+       * testsuite/poke.pkl/attr-size-20.pk: Likewise.
+       * testsuite/poke.pkl/attr-size-21.pk: Likewise.
+       * testsuite/poke.pkl/attr-unit-2.pk: Likewise.
+       * testsuite/poke.pkl/attr-unit-3.pk: Likewise.
+       * testsuite/Makefile.am (EXTRA_DIST): Update.
+
 2022-01-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
 
        * libpoke/pkl-fold.c (OP_BINARY_AAA_add): New macro for constant
diff --git a/libpoke/pkl-trans.c b/libpoke/pkl-trans.c
index f235a0af..0bf70e53 100644
--- a/libpoke/pkl-trans.c
+++ b/libpoke/pkl-trans.c
@@ -1435,6 +1435,82 @@ PKL_PHASE_BEGIN_HANDLER (pkl_trans2_ps_struct_type_field)
 }
 PKL_PHASE_END_HANDLER
 
+/* Compute the attributes of values with complete types.  */
+
+PKL_PHASE_BEGIN_HANDLER (pkl_trans2_ps_op_attr)
+{
+  pkl_ast_node exp = PKL_PASS_NODE;
+  pkl_ast_node exp_type = PKL_AST_TYPE (exp);
+  pkl_ast_node operand = PKL_AST_EXP_OPERAND (exp, 0);
+  pkl_ast_node operand_type = PKL_AST_TYPE (operand);
+
+  switch (PKL_AST_EXP_ATTR (exp))
+    {
+    case PKL_AST_ATTR_LENGTH:
+      {
+        size_t length = 0;
+        pkl_ast_node len;
+
+        if (PKL_AST_LITERAL_P (operand))
+          switch (PKL_AST_CODE (operand))
+            {
+            case PKL_AST_STRING:
+              length = PKL_AST_STRING_LENGTH (operand);
+              break;
+            case PKL_AST_ARRAY:
+              length = PKL_AST_ARRAY_NELEM (operand);
+              break;
+            default:
+              assert (0);
+            }
+        else if (PKL_AST_TYPE_CODE (operand_type) == PKL_TYPE_STRUCT
+                 && (PKL_AST_TYPE_COMPLETE (operand_type)
+                     == PKL_AST_TYPE_COMPLETE_YES))
+          length = PKL_AST_TYPE_S_NELEM (operand_type);
+        else
+          PKL_PASS_DONE;
+
+        /* The type of 'length is uint<64>  */
+
+        len = pkl_ast_make_integer (PKL_PASS_AST, length);
+        PKL_AST_TYPE (len) = ASTREF (exp_type);
+        PKL_PASS_NODE = ASTREF (len);
+        pkl_ast_node_free (exp);
+        PKL_PASS_RESTART = 1;
+        PKL_PASS_DONE;
+      }
+    case PKL_AST_ATTR_SIZE:
+      {
+        pkl_ast_node size, off;
+
+        if (PKL_AST_LITERAL_P (operand)
+            && PKL_AST_CODE (operand) == PKL_AST_STRING)
+          {
+            size = pkl_ast_make_integer (PKL_PASS_AST,
+                                         (PKL_AST_STRING_LENGTH (operand) + 1)
+                                           * 8);
+            PKL_AST_TYPE (size) = PKL_AST_TYPE_O_BASE_TYPE (exp_type);
+          }
+        else if (PKL_AST_TYPE_COMPLETE (operand_type)
+                   == PKL_AST_TYPE_COMPLETE_YES)
+          size = pkl_ast_sizeof_type (PKL_PASS_AST, operand_type);
+        else
+          PKL_PASS_DONE;
+
+        off = pkl_ast_make_offset (PKL_PASS_AST, size,
+                                   PKL_AST_TYPE_O_UNIT (exp_type));
+        PKL_AST_TYPE (off) = ASTREF (exp_type);
+        PKL_PASS_NODE = ASTREF (off);
+        pkl_ast_node_free (exp);
+        PKL_PASS_RESTART = 1;
+        PKL_PASS_DONE;
+      }
+    default:
+      break;
+    }
+}
+PKL_PHASE_END_HANDLER
+
 struct pkl_phase pkl_phase_trans2 =
   {
    PKL_PHASE_PS_HANDLER (PKL_AST_SRC, pkl_trans_ps_src),
@@ -1450,6 +1526,7 @@ struct pkl_phase pkl_phase_trans2 =
    PKL_PHASE_PS_HANDLER (PKL_AST_INCRDECR, pkl_trans2_ps_incrdecr),
    PKL_PHASE_PS_TYPE_HANDLER (PKL_TYPE_OFFSET, pkl_trans2_ps_type_offset),
    PKL_PHASE_PS_HANDLER (PKL_AST_STRUCT_TYPE_FIELD, 
pkl_trans2_ps_struct_type_field),
+   PKL_PHASE_PS_OP_HANDLER (PKL_AST_OP_ATTR, pkl_trans2_ps_op_attr),
   };
 
 
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 4fa4cbb3..b1c41d10 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -160,6 +160,8 @@ EXTRA_DIST = \
   poke.map/map-optcond-int-struct-1.pk \
   poke.map/map-optcond-int-struct-2.pk \
   poke.map/map-size-1.pk \
+  poke.map/map-size-2.pk \
+  poke.map/map-size-3.pk \
   poke.map/map-struct-fun-1.pk \
   poke.map/map-struct-fun-2.pk \
   poke.map/map-struct-fun-3.pk \
@@ -356,6 +358,7 @@ EXTRA_DIST = \
   poke.map/maps-structs-methods-11.pk \
   poke.map/maps-structs-pinned-1.pk \
   poke.map/maps-structs-pinned-2.pk \
+  poke.map/maps-structs-pinned-3.pk \
   poke.map/maps-int-struct-constraint-1.pk \
   poke.map/maps-int-struct-constraint-2.pk \
   poke.map/maps-trims-1.pk \
@@ -771,6 +774,8 @@ EXTRA_DIST = \
   poke.pkl/attr-length-7.pk \
   poke.pkl/attr-length-8.pk \
   poke.pkl/attr-length-9.pk \
+  poke.pkl/attr-length-10.pk \
+  poke.pkl/attr-length-11.pk \
   poke.pkl/attr-magnitude-1.pk \
   poke.pkl/attr-mapped-1.pk \
   poke.pkl/attr-mapped-2.pk \
@@ -798,7 +803,16 @@ EXTRA_DIST = \
   poke.pkl/attr-size-12.pk \
   poke.pkl/attr-size-13.pk \
   poke.pkl/attr-size-14.pk \
+  poke.pkl/attr-size-15.pk \
+  poke.pkl/attr-size-16.pk \
+  poke.pkl/attr-size-17.pk \
+  poke.pkl/attr-size-18.pk \
+  poke.pkl/attr-size-19.pk \
+  poke.pkl/attr-size-20.pk \
+  poke.pkl/attr-size-21.pk \
   poke.pkl/attr-unit-1.pk \
+  poke.pkl/attr-unit-2.pk \
+  poke.pkl/attr-unit-3.pk \
   poke.pkl/band-diag-1.pk \
   poke.pkl/band-diag-2.pk \
   poke.pkl/band-integers-1.pk \
diff --git a/testsuite/poke.map/map-size-2.pk b/testsuite/poke.map/map-size-2.pk
new file mode 100644
index 00000000..4e5ea6c4
--- /dev/null
+++ b/testsuite/poke.map/map-size-2.pk
@@ -0,0 +1,20 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+/* { dg-command {.set obase 16} } */
+
+type Foo = struct { byte i; byte j @ 3#B; byte k @ 2#B; };
+
+/* { dg-command { var f = Foo @ 1#B } } */
+/* { dg-command { f } } */
+/* { dg-output "Foo \\{i=0x20UB,j=0x50UB,k=0x40UB\\}" } */
+/* { dg-command { f'size } } */
+/* { dg-output "\n0x20UL#b" } */
+
+type Bar = struct { byte i; byte j; byte k @ 1#B; };
+
+/* { dg-command { var b = Bar @ 1#B } } */
+/* { dg-command { b } } */
+/* { dg-output "\nBar \\{i=0x20UB,j=0x30UB,k=0x30UB\\}" } */
+/* { dg-command { b'size } } */
+/* { dg-output "\n0x10UL#b" } */
diff --git a/testsuite/poke.map/map-size-3.pk b/testsuite/poke.map/map-size-3.pk
new file mode 100644
index 00000000..e3b2440a
--- /dev/null
+++ b/testsuite/poke.map/map-size-3.pk
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x50 0x60 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+/* { dg-command {.set obase 16} } */
+
+/* FOO and BAR are no longer known at compile-time.  */
+
+var n = 1;
+type Foo = struct { byte[n] i; byte j @ 3#B; byte k @ 2#B; };
+
+/* { dg-command { var f = Foo @ 1#B } } */
+/* { dg-command { f } } */
+/* { dg-output "Foo \\{i=\\\[0x20UB\\\],j=0x50UB,k=0x40UB\\}" } */
+/* { dg-command { f'size } } */
+/* { dg-output "\n0x20UL#b" } */
+
+var n = 1;
+type Bar = struct { byte[n] i; byte j; byte k @ 1#B; };
+
+/* { dg-command { var b = Bar @ 1#B } } */
+/* { dg-command { b } } */
+/* { dg-output "\nBar \\{i=\\\[0x20UB\\\],j=0x30UB,k=0x30UB\\}" } */
+/* { dg-command { b'size } } */
+/* { dg-output "\n0x10UL#b" } */
diff --git a/testsuite/poke.map/maps-structs-pinned-3.pk 
b/testsuite/poke.map/maps-structs-pinned-3.pk
new file mode 100644
index 00000000..946cd747
--- /dev/null
+++ b/testsuite/poke.map/maps-structs-pinned-3.pk
@@ -0,0 +1,10 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40  0x60 0x50 0x70 0x80   0x90 0xa0 0xb0 
0xc0} } */
+
+/* Size of Foo is not known at compile-time.  */
+var n = 1;
+type Foo = pinned struct { int[n] i; long j; };
+
+/* { dg-command { var f = Foo @ 1#B } } */
+/* { dg-command { f'size } } */
+/* { dg-output "64UL#b" } */
diff --git a/testsuite/poke.pkl/attr-length-10.pk 
b/testsuite/poke.pkl/attr-length-10.pk
new file mode 100644
index 00000000..02ffcc6a
--- /dev/null
+++ b/testsuite/poke.pkl/attr-length-10.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+var s = "foo";
+
+/* { dg-command {  s'length } } */
+/* { dg-output "3UL" } */
diff --git a/testsuite/poke.pkl/attr-length-11.pk 
b/testsuite/poke.pkl/attr-length-11.pk
new file mode 100644
index 00000000..d2b4f2ea
--- /dev/null
+++ b/testsuite/poke.pkl/attr-length-11.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+var a = [1,2,3];
+
+/* { dg-command { a'length } } */
+/* { dg-output "3UL" } */
diff --git a/testsuite/poke.pkl/attr-size-15.pk 
b/testsuite/poke.pkl/attr-size-15.pk
new file mode 100644
index 00000000..f378fc04
--- /dev/null
+++ b/testsuite/poke.pkl/attr-size-15.pk
@@ -0,0 +1,7 @@
+/* { dg-do run } */
+
+var a = 1 as int<3>;
+var b = [a] as int<3>[];  /* Incomplete type.  */
+
+/* { dg-command {  b'size } } */
+/* { dg-output "3UL#b" } */
diff --git a/testsuite/poke.pkl/attr-size-16.pk 
b/testsuite/poke.pkl/attr-size-16.pk
new file mode 100644
index 00000000..22e3c02d
--- /dev/null
+++ b/testsuite/poke.pkl/attr-size-16.pk
@@ -0,0 +1,7 @@
+/* { dg-do run } */
+
+var a = 1 as uint<33>;
+var b = [a] as uint<33>[];
+
+/* { dg-command {  b'size } } */
+/* { dg-output "33UL#b" } */
diff --git a/testsuite/poke.pkl/attr-size-17.pk 
b/testsuite/poke.pkl/attr-size-17.pk
new file mode 100644
index 00000000..ae5d9f3b
--- /dev/null
+++ b/testsuite/poke.pkl/attr-size-17.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+var a = [1] as int[];  /* Incomplete type.  */
+
+/* { dg-command {  a'size } } */
+/* { dg-output "32UL#b" } */
diff --git a/testsuite/poke.pkl/attr-size-18.pk 
b/testsuite/poke.pkl/attr-size-18.pk
new file mode 100644
index 00000000..96a309b2
--- /dev/null
+++ b/testsuite/poke.pkl/attr-size-18.pk
@@ -0,0 +1,7 @@
+/* { dg-do run } */
+
+var a = [1,2,3];
+var b = a as int[];
+
+/* { dg-command {  b'size } } */
+/* { dg-output "96UL#b" } */
diff --git a/testsuite/poke.pkl/attr-size-19.pk 
b/testsuite/poke.pkl/attr-size-19.pk
new file mode 100644
index 00000000..c6dc83ec
--- /dev/null
+++ b/testsuite/poke.pkl/attr-size-19.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+var a = [1UL] as uint<64>[];
+
+/* { dg-command {  a'size } } */
+/* { dg-output "64UL#b" } */
diff --git a/testsuite/poke.pkl/attr-size-20.pk 
b/testsuite/poke.pkl/attr-size-20.pk
new file mode 100644
index 00000000..85551a71
--- /dev/null
+++ b/testsuite/poke.pkl/attr-size-20.pk
@@ -0,0 +1,8 @@
+/* { dg-do run } */
+
+type Struct = struct { };
+var s = Struct {};
+var x = [s] as Struct[];  /* Incomplete type.  */
+
+/* { dg-command {  x'size } } */
+/* { dg-output "0UL#b" } */
diff --git a/testsuite/poke.pkl/attr-size-21.pk 
b/testsuite/poke.pkl/attr-size-21.pk
new file mode 100644
index 00000000..d34cbedf
--- /dev/null
+++ b/testsuite/poke.pkl/attr-size-21.pk
@@ -0,0 +1,8 @@
+/* { dg-do run } */
+
+type Struct = struct { int i; long l; string s; };
+var s = Struct {i=1, l=2UL, s="foo"};
+var x = [s] as Struct[];  /* Incomplete type.  */
+
+/* { dg-command { x'size } } */
+/* { dg-output "128UL#b" } */
diff --git a/testsuite/poke.pkl/attr-unit-2.pk 
b/testsuite/poke.pkl/attr-unit-2.pk
new file mode 100644
index 00000000..81af4049
--- /dev/null
+++ b/testsuite/poke.pkl/attr-unit-2.pk
@@ -0,0 +1,6 @@
+/* { dg-do run } */
+
+var a = 10;
+
+/* { dg-command {  a'size'unit } } */
+/* { dg-output "1UL" } */
diff --git a/testsuite/poke.pkl/attr-unit-3.pk 
b/testsuite/poke.pkl/attr-unit-3.pk
new file mode 100644
index 00000000..73d48832
--- /dev/null
+++ b/testsuite/poke.pkl/attr-unit-3.pk
@@ -0,0 +1,7 @@
+/* { dg-do run } */
+
+var a = [10];
+var b = a as int[]; /* Type of `b' is no longer complete.  */
+
+/* { dg-command {  b'size'unit } } */
+/* { dg-output "1UL" } */
-- 
2.34.1




reply via email to

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