bug-gnu-utils
[Top][All Lists]
Advanced

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

gawk 3.1.0 doesn't correctly optimize for ... delete loop


From: Aharon Robbins
Subject: gawk 3.1.0 doesn't correctly optimize for ... delete loop
Date: Fri, 8 Feb 2002 16:06:23 +0200

Gawk 3.1 has some code that is supposed to notice the case of

        for (iggy in foo)
                delete foo[iggy]

and optimize it internally into

        delete foo

This code didn't actually work.  Below is an unofficial patch.

Enjoy,

Arnold Robbins

Fri Feb  8 16:01:11 2002  Arnold D. Robbins  <address@hidden>

        * awkgram.y (LEX_FOR): Fix case of array loop with body of single
          delete statement to actually check the right things to make the
          optimization.
        * profile.c (tree_eval): Add case for Node_K_delete_loop.
          (prec_level): Ditto.

diff -cbr gawk-3.1.0/awkgram.y gawk-fpat/awkgram.y
*** gawk-3.1.0/awkgram.y        Mon Apr 23 10:25:58 2001
--- gawk-fpat/awkgram.y Fri Feb  8 15:54:10 2002
***************
*** 406,419 ****
                 * Check that the body is a `delete a[i]' statement,
                 * and that both the loop var and array names match.
                 */
!               if ($8 != NULL && $8->type == Node_K_delete
!                   && $8->rnode != NULL
!                   && ($8->rnode->type == Node_var || $8->rnode->type == 
Node_param_list)
!                   && strcmp($3, $8->rnode->var_value->vname) == 0
!                   && strcmp($5, $8->lnode->vname) == 0) {
                        $8->type = Node_K_delete_loop;
                        $$ = $8;
                } else {
                        $$ = node($8, Node_K_arrayfor,
                                make_for_loop(variable($3, CAN_FREE, Node_var),
                                (NODE *) NULL, variable($5, CAN_FREE, 
Node_var_array)));
--- 406,431 ----
                 * Check that the body is a `delete a[i]' statement,
                 * and that both the loop var and array names match.
                 */
!               if ($8 != NULL && $8->type == Node_K_delete) {
!                       NODE *arr, *sub;
! 
!                       assert($8->rnode->type == Node_expression_list);
!                       arr = $8->lnode;        /* array var */
!                       sub = $8->rnode->lnode; /* index var */
! 
!                       if (   (arr->type == Node_var
!                               || arr->type == Node_var_array
!                               || arr->type == Node_param_list)
!                           && (sub->type == Node_var || sub->type == 
Node_param_list)
!                           && strcmp($3, sub->vname) == 0
!                           && strcmp($5, arr->vname) == 0) {
                                $8->type = Node_K_delete_loop;
                                $$ = $8;
+                       }
+                       else
+                               goto regular_loop;
                } else {
+       regular_loop:
                        $$ = node($8, Node_K_arrayfor,
                                make_for_loop(variable($3, CAN_FREE, Node_var),
                                (NODE *) NULL, variable($5, CAN_FREE, 
Node_var_array)));
diff -cbr gawk-3.1.0/profile.c gawk-fpat/profile.c
*** gawk-3.1.0/profile.c        Tue Feb 27 12:05:01 2001
--- gawk-fpat/profile.c Fri Feb  8 15:30:30 2002
***************
*** 447,452 ****
--- 447,457 ----
                pp_getline(tree);
                return;
  
+       case Node_K_delete_loop:
+               fprintf(prof_fp, "delete ");
+               tree_eval(tree->lnode);
+               return;
+ 
                /* unary operations */
        case Node_NR:
                fprintf(prof_fp, "NR");
***************
*** 1240,1245 ****
--- 1245,1251 ----
        case Node_param_list:
        case Node_subscript:
        case Node_func_call:
+       case Node_K_delete_loop:
        case Node_val:
        case Node_builtin:
        case Node_BINMODE:



reply via email to

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