poke-devel
[Top][All Lists]
Advanced

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

[COMMITTED] pkl: add pkl_constant_fold and fix aoref


From: Jose E. Marchesi
Subject: [COMMITTED] pkl: add pkl_constant_fold and fix aoref
Date: Sat, 04 Feb 2023 17:54:44 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

2023-02-04  Jose E. Marchesi  <jemarch@gnu.org>

        * libpoke/pkl-ast.c: Add note to pkl_ast_sizeof_type comment.
        * libpoke/pkl.h: Prototype for pkl_constant_fold.
        * libpoke/pkl.c (pkl_constant_fold): New function.
        * testsuite/poke.pkl/arrays-index-17.pk: New test.
        * testsuite/Makefile.am (EXTRA_DIST): Add new test.
---
 ChangeLog                             |  8 ++++++++
 libpoke/pkl-ast.c                     |  7 ++++++-
 libpoke/pkl-gen.pks                   |  3 ++-
 libpoke/pkl.c                         | 16 ++++++++++++++++
 libpoke/pkl.h                         |  5 +++++
 testsuite/Makefile.am                 |  1 +
 testsuite/poke.pkl/arrays-index-17.pk |  9 +++++++++
 7 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 testsuite/poke.pkl/arrays-index-17.pk

diff --git a/ChangeLog b/ChangeLog
index 3a31f2c2..d235d854 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-02-04  Jose E. Marchesi  <jemarch@gnu.org>
+
+       * libpoke/pkl-ast.c: Add note to pkl_ast_sizeof_type comment.
+       * libpoke/pkl.h: Prototype for pkl_constant_fold.
+       * libpoke/pkl.c (pkl_constant_fold): New function.
+       * testsuite/poke.pkl/arrays-index-17.pk: New test.
+       * testsuite/Makefile.am (EXTRA_DIST): Add new test.
+
 2023-02-04  Jose E. Marchesi  <jemarch@gnu.org>
 
        * libpoke/pkl-gen.c (pkl_gen_pr_asm_stmt): Emit error whenever
diff --git a/libpoke/pkl-ast.c b/libpoke/pkl-ast.c
index e93eb6cd..804eaa34 100644
--- a/libpoke/pkl-ast.c
+++ b/libpoke/pkl-ast.c
@@ -1048,7 +1048,12 @@ pkl_ast_type_integrable_p (pkl_ast_node type)
 }
 
 /* Build and return an expression that computes the size of TYPE in
-   bits, as an unsigned 64-bit value.  */
+   bits, as an unsigned 64-bit value.
+
+   Note that it is required for the returned expression to not trigger
+   errors in the fold pass, i.e. no division by zero, no overflow, out
+   of bound indexes.  This will be detected by a run-time
+   abort/assert.  */
 
 pkl_ast_node
 pkl_ast_sizeof_type (pkl_ast ast, pkl_ast_node type)
diff --git a/libpoke/pkl-gen.pks b/libpoke/pkl-gen.pks
index 32f76218..8026db00 100644
--- a/libpoke/pkl-gen.pks
+++ b/libpoke/pkl-gen.pks
@@ -3418,7 +3418,8 @@
         ;; If the size all the array elements is constant and known
         ;; at compile-time, then we can just calculate the index
         ;; corresponding to the given offset.
-        .let @esize = pkl_ast_sizeof_type (PKL_PASS_AST, @etype)
+        .let @esize = pkl_constant_fold (PKL_PASS_COMPILER, \
+                                         pkl_ast_sizeof_type (PKL_PASS_AST, 
@etype))
   .c    assert (PKL_AST_CODE (@esize) == PKL_AST_INTEGER);
         .let #esizeval = pvm_make_ulong (PKL_AST_INTEGER_VALUE (@esize), 64);
         push #esizeval            ; ARR IDX ESIZE
diff --git a/libpoke/pkl.c b/libpoke/pkl.c
index f4fc181c..54831e6b 100644
--- a/libpoke/pkl.c
+++ b/libpoke/pkl.c
@@ -919,3 +919,19 @@ pkl_tracer_p (pkl_compiler compiler)
 
   return PVM_VAL_INT (val);
 }
+
+pkl_ast_node
+pkl_constant_fold (pkl_compiler compiler, pkl_ast_node node)
+{
+  pkl_ast ast;
+  struct pkl_fold_payload fold_payload = { 0 };
+  struct pkl_phase *fold_phases[] = { &pkl_phase_fold, NULL };
+  void *fold_payloads[] = { &fold_payload, NULL };
+
+  ast = pkl_ast_init ();
+  ast->ast = ASTREF (node);
+  if (!pkl_do_pass (compiler, ast, fold_phases, fold_payloads, 0, 1))
+    assert (0);
+
+  return ast->ast;
+}
diff --git a/libpoke/pkl.h b/libpoke/pkl.h
index 1e62fdfb..67a74422 100644
--- a/libpoke/pkl.h
+++ b/libpoke/pkl.h
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 
+#include "pkl-ast.h"
 #include "pkl-compiler.h"
 #include "pvm.h"
 
@@ -312,4 +313,8 @@ int pkl_module_loaded_p (pkl_compiler compiler, const char 
*path);
 
 void pkl_add_module (pkl_compiler compiler, const char *path);
 
+/* Given an AST node, run constant fold on it.  */
+
+pkl_ast_node pkl_constant_fold (pkl_compiler compiler, pkl_ast_node ast);
+
 #endif /* ! PKL_H */
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 2309dcc7..18eb3134 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -793,6 +793,7 @@ EXTRA_DIST = \
   poke.pkl/arrays-index-14.pk \
   poke.pkl/arrays-index-15.pk \
   poke.pkl/arrays-index-16.pk \
+  poke.pkl/arrays-index-17.pk \
   poke.pkl/arrays-index-diag-1.pk \
   poke.pkl/arrays-index-diag-2.pk \
   poke.pkl/arrays-index-diag-3.pk \
diff --git a/testsuite/poke.pkl/arrays-index-17.pk 
b/testsuite/poke.pkl/arrays-index-17.pk
new file mode 100644
index 00000000..b89bb84c
--- /dev/null
+++ b/testsuite/poke.pkl/arrays-index-17.pk
@@ -0,0 +1,9 @@
+/* { dg-do run } */
+
+type Foo = struct { uint<8> i; uint<16> l; };
+var a = Foo[24#B]();
+
+/* { dg-command {a[1].i = 10} } */
+/* { dg-command {a[1].l = 20} } */
+/* { dg-command {a[1#24]} } */
+/* { dg-output {Foo \{i=10UB,l=20UH\}} } */
-- 
2.30.2




reply via email to

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