poke-devel
[Top][All Lists]
Advanced

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

Re: [PATCH 4/5] pkl: Add folding for array add operation


From: Jose E. Marchesi
Subject: Re: [PATCH 4/5] pkl: Add folding for array add operation
Date: Sun, 23 Jan 2022 15:27:10 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hi Mohammad.
This is OK for master.
Thanks for doing this :)


> 2022-01-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
>
>       * libpoke/pkl-fold.c (OP_BINARY_AAA_add): New macro for constant
>       folding ADD of array literals.
>       (OP_BINARY_AAA_sub): Add dummy macro.
>       (PKL_PHASE_HANDLER_BIN_ARITH): Add macros for array folding.
>       * libpoke/pkl-ast.h (pkl_ast_array_initializers_concat): New
>       function to concat two array literals.
>       * libpoke/pkl-ast.c (pkl_ast_array_initializers_concat): Likewise.
>       * testsuite/poke.pkl/arrays-16.pk: New test.
>       * testsuite/Makefile.am (EXTRA_DIST): Update.
> ---
>  ChangeLog                       | 12 ++++++
>  libpoke/pkl-ast.c               | 44 +++++++++++++++++++++
>  libpoke/pkl-ast.h               |  7 ++++
>  libpoke/pkl-fold.c              | 70 +++++++++++++++++++++++++++++++++
>  testsuite/Makefile.am           |  1 +
>  testsuite/poke.pkl/arrays-16.pk |  9 +++++
>  6 files changed, 143 insertions(+)
>  create mode 100644 testsuite/poke.pkl/arrays-16.pk
>
> diff --git a/ChangeLog b/ChangeLog
> index b3c9f9bd..82061c3f 100644
> --- a/ChangeLog
> +++ b/ChangeLog
> @@ -1,3 +1,15 @@
> +2022-01-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
> +
> +     * libpoke/pkl-fold.c (OP_BINARY_AAA_add): New macro for constant
> +     folding ADD of array literals.
> +     (OP_BINARY_AAA_sub): Add dummy macro.
> +     (PKL_PHASE_HANDLER_BIN_ARITH): Add macros for array folding.
> +     * libpoke/pkl-ast.h (pkl_ast_array_initializers_concat): New
> +     function to concat two array literals.
> +     * libpoke/pkl-ast.c (pkl_ast_array_initializers_concat): Likewise.
> +     * testsuite/poke.pkl/arrays-16.pk: New test.
> +     * testsuite/Makefile.am (EXTRA_DIST): Update.
> +
>  2022-01-22  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
>  
>       * libpoke/pkl-ast.h (pkl_ast_struct_type_field): Add `size` field.
> diff --git a/libpoke/pkl-ast.c b/libpoke/pkl-ast.c
> index 41097826..9308bbab 100644
> --- a/libpoke/pkl-ast.c
> +++ b/libpoke/pkl-ast.c
> @@ -1560,6 +1560,50 @@ pkl_ast_make_array (pkl_ast ast,
>    return array;
>  }
>  
> +/* Concatenates two chain of array initializers and fixes the indices of
> +   second initializer accordingly.  */
> +
> +pkl_ast_node
> +pkl_ast_array_initializers_concat (pkl_ast ast,
> +                                   pkl_ast_node init1,
> +                                   pkl_ast_node init2)
> +{
> +  pkl_ast_node init, cursor, tmp;
> +  pkl_ast_node index, exp;
> +  size_t idx;
> +
> +  assert (init1);
> +  assert (init2);
> +
> +  index = PKL_AST_ARRAY_INITIALIZER_INDEX (init1);
> +  exp = PKL_AST_ARRAY_INITIALIZER_EXP (init1);
> +  init = pkl_ast_make_array_initializer (ast, index, exp);
> +  idx = 1;
> +  cursor = init;
> +  for (tmp = PKL_AST_CHAIN (init1); tmp; tmp = PKL_AST_CHAIN (tmp), idx++)
> +    {
> +      index = PKL_AST_ARRAY_INITIALIZER_INDEX (tmp);
> +      exp = PKL_AST_ARRAY_INITIALIZER_EXP (tmp);
> +      PKL_AST_CHAIN (cursor)
> +          = ASTREF (pkl_ast_make_array_initializer (ast, index, exp));
> +      cursor = PKL_AST_CHAIN (cursor);
> +    }
> +  for (tmp = init2; tmp; tmp = PKL_AST_CHAIN (tmp), idx++)
> +    {
> +      pkl_ast_node index_type
> +          = PKL_AST_TYPE (PKL_AST_ARRAY_INITIALIZER_INDEX (tmp));
> +
> +      index = pkl_ast_make_integer (ast, idx);
> +      PKL_AST_TYPE (index) = ASTREF (index_type);
> +      exp = PKL_AST_ARRAY_INITIALIZER_EXP (tmp);
> +      PKL_AST_CHAIN (cursor)
> +          = ASTREF (pkl_ast_make_array_initializer (ast, index, exp));
> +      cursor = PKL_AST_CHAIN (cursor);
> +    }
> +
> +  return init;
> +}
> +
>  /* Build and return an AST node for an array element.  */
>  
>  pkl_ast_node
> diff --git a/libpoke/pkl-ast.h b/libpoke/pkl-ast.h
> index be28ba49..cb94fc4d 100644
> --- a/libpoke/pkl-ast.h
> +++ b/libpoke/pkl-ast.h
> @@ -404,6 +404,13 @@ pkl_ast_node pkl_ast_make_array_initializer (pkl_ast ast,
>                                               pkl_ast_node index,
>                                               pkl_ast_node exp);
>  
> +/* Concatenates two chain of array initializers and fixes the indices of
> +   second initializer accordingly.  */
> +
> +pkl_ast_node
> +pkl_ast_array_initializers_concat (pkl_ast ast,
> +                                   pkl_ast_node init1,
> +                                   pkl_ast_node init2);
>  
>  /* PKL_AST_STRUCT nodes represent struct literals.  */
>  
> diff --git a/libpoke/pkl-fold.c b/libpoke/pkl-fold.c
> index 767aecc4..93e92860 100644
> --- a/libpoke/pkl-fold.c
> +++ b/libpoke/pkl-fold.c
> @@ -715,6 +715,75 @@ EMUL_UU (bnoto) { return ~op; }
>      }                                                                   \
>    while (0)
>  
> +/* Fold binary ADD for arrays.  */
> +
> +#define OP_BINARY_AAA_add()                                                  
>  \
> +  do                                                                         
>  \
> +    {                                                                        
>  \
> +      pkl_ast_node op1 = PKL_AST_EXP_OPERAND (PKL_PASS_NODE, 0);             
>  \
> +      pkl_ast_node op2 = PKL_AST_EXP_OPERAND (PKL_PASS_NODE, 1);             
>  \
> +      pkl_ast_node op1_type = PKL_AST_TYPE (op1);                            
>  \
> +      pkl_ast_node op2_type = PKL_AST_TYPE (op2);                            
>  \
> +                                                                             
>  \
> +      if (PKL_AST_TYPE_CODE (op1_type) == PKL_TYPE_ARRAY                     
>  \
> +          && PKL_AST_TYPE_CODE (op2_type) == PKL_TYPE_ARRAY                  
>  \
> +          && PKL_AST_TYPE_COMPLETE (op1_type) == PKL_AST_TYPE_COMPLETE_YES   
>  \
> +          && PKL_AST_TYPE_COMPLETE (op2_type) == PKL_AST_TYPE_COMPLETE_YES)  
>  \
> +        {                                                                    
>  \
> +          pkl_ast_node new, bound1, bound2, btype1, btype2, bound, init;     
>  \
> +                                                                             
>  \
> +          if (!(PKL_AST_CODE (op1) == PKL_AST_ARRAY                          
>  \
> +                && PKL_AST_CODE (op2) == PKL_AST_ARRAY                       
>  \
> +                && PKL_AST_LITERAL_P (op1) && PKL_AST_LITERAL_P (op2)))      
>  \
> +            /* We cannot fold this expression.  */                           
>  \
> +            PKL_PASS_DONE;                                                   
>  \
> +                                                                             
>  \
> +          /* For simplicity.  */                                             
>  \
> +          bound1 = PKL_AST_TYPE_A_BOUND (op1_type);                          
>  \
> +          bound2 = PKL_AST_TYPE_A_BOUND (op2_type);                          
>  \
> +          btype1 = PKL_AST_TYPE (bound1);                                    
>  \
> +          btype2 = PKL_AST_TYPE (bound2);                                    
>  \
> +          if (!pkl_ast_type_equal_p (btype1, btype2))                        
>  \
> +            /* We cannot fold this expression.  */                           
>  \
> +            PKL_PASS_DONE;                                                   
>  \
> +                                                                             
>  \
> +          init = pkl_ast_array_initializers_concat(                          
>  \
> +              PKL_PASS_AST,                                                  
>  \
> +              PKL_AST_ARRAY_INITIALIZERS (op1),                              
>  \
> +              PKL_AST_ARRAY_INITIALIZERS (op2));                             
>  \
> +          new = pkl_ast_make_array (PKL_PASS_AST,                            
>  \
> +                                    PKL_AST_ARRAY_NELEM (op1)                
>  \
> +                                        + PKL_AST_ARRAY_NELEM (op2),         
>  \
> +                                    PKL_AST_ARRAY_NINITIALIZER (op1)         
>  \
> +                                        + PKL_AST_ARRAY_NINITIALIZER (op2),  
>  \
> +                                    init);                                   
>  \
> +                                                                             
>  \
> +          bound = pkl_ast_make_binary_exp (PKL_PASS_AST, PKL_AST_OP_ADD,     
>  \
> +                                           bound1, bound2);                  
>  \
> +          PKL_AST_TYPE (bound) = btype1;                                     
>  \
> +          PKL_AST_TYPE (new) = ASTREF (pkl_ast_make_array_type (             
>  \
> +              PKL_PASS_AST, PKL_AST_TYPE_A_ETYPE (op1_type), bound));        
>  \
> +          PKL_AST_TYPE_COMPLETE (PKL_AST_TYPE (new))                         
>  \
> +              = PKL_AST_TYPE_COMPLETE_YES;                                   
>  \
> +          PKL_AST_LITERAL_P (new) = 1;                                       
>  \
> +          PKL_AST_LOC (new) = PKL_AST_LOC (PKL_PASS_NODE);                   
>  \
> +          pkl_ast_node_free (PKL_PASS_NODE);                                 
>  \
> +          PKL_PASS_NODE = new;                                               
>  \
> +          PKL_PASS_RESTART = 1;                                              
>  \
> +          PKL_PASS_DONE;                                                     
>  \
> +        }                                                                    
>  \
> +    }                                                                        
>  \
> +  while (0)
> +
> +/* Fold binary SUB for arrays.  */
> +
> +#define OP_BINARY_AAA_sub()                                                  
>  \
> +  do                                                                         
>  \
> +    {                                                                        
>  \
> +      /* This is just a placeholder.  */                                     
>  \
> +    }                                                                        
>  \
> +  while (0)
> +
>  /* Handlers for the several expression codes.  */
>  
>  #define PKL_PHASE_HANDLER_UNA_INT(OP)           \
> @@ -859,6 +928,7 @@ PKL_PHASE_HANDLER_BIN_RELA (ge);
>      OP_BINARY_III (OP);                              \
>      OP_BINARY_OOO (OP##o);                           \
>      OP_BINARY_SSS (OP);                              \
> +    OP_BINARY_AAA_##OP ();                           \
>    }                                                  \
>    PKL_PHASE_END_HANDLER
>  
> diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
> index 7037fc75..4fa4cbb3 100644
> --- a/testsuite/Makefile.am
> +++ b/testsuite/Makefile.am
> @@ -671,6 +671,7 @@ EXTRA_DIST = \
>    poke.pkl/arrays-12.pk \
>    poke.pkl/arrays-13.pk \
>    poke.pkl/arrays-15.pk \
> +  poke.pkl/arrays-16.pk \
>    poke.pkl/arrays-diag-1.pk \
>    poke.pkl/arrays-diag-2.pk \
>    poke.pkl/arrays-diag-3.pk \
> diff --git a/testsuite/poke.pkl/arrays-16.pk b/testsuite/poke.pkl/arrays-16.pk
> new file mode 100644
> index 00000000..2ddd5780
> --- /dev/null
> +++ b/testsuite/poke.pkl/arrays-16.pk
> @@ -0,0 +1,9 @@
> +/* { dg-do run } */
> +
> +var typof = typeof ([1] + [2]);
> +
> +/* { dg-command { .set obase 10 } } */
> +/* { dg-command {  [1,2]+[3,4,5] } } */
> +/* { dg-output "\\\[1,2,3,4,5\\\]\n" } */
> +/* { dg-command {  typof.complete_p  } } */
> +/* { dg-output "1" } */



reply via email to

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