This restructures outer if parsing to maintain a stack of active
ifs.  This gets rid of all the reverse if stuff and fixes outer ifs
on (match ...)es.

What is left to be done is delaying 'for' lowering.

Richard.

2014-09-23  Richard Biener  <rguent...@suse.de>

        * genmatch.c 
        (dt_simplify::gen): Adjust.
        (copy_reverse): Remove.
        (parse_simplify): Adjust.
Index: match-and-simplify/gcc/genmatch.c
===================================================================
--- match-and-simplify.orig/gcc/genmatch.c      2014-09-23 13:42:38.186799538 
+0200
+++ match-and-simplify/gcc/genmatch.c   2014-09-23 13:41:43.864803278 +0200
@@ -1878,8 +1878,7 @@ dt_simplify::gen (FILE *f, bool gimple)
   unsigned n_braces = 0;
   if (s->ifexpr_vec != vNULL)
     {
-      // we add in LIFO order, so traverse backwards
-      for (int i = s->ifexpr_vec.length () - 1; i >= 0; --i)
+      for (unsigned i = 0; i < s->ifexpr_vec.length (); ++i)
        {
          if_or_with &w = s->ifexpr_vec[i];
          if (w.is_with)
@@ -1897,7 +1896,7 @@ dt_simplify::gen (FILE *f, bool gimple)
                w.cexpr->gen_transform (f, NULL, true, 1, "type");
              else
                {
-                 int j = i;
+                 unsigned j = i;
                  do
                    {
                      if (j != i)
@@ -1910,10 +1909,11 @@ dt_simplify::gen (FILE *f, bool gimple)
                      s->ifexpr_vec[j].cexpr->gen_transform (f, NULL,
                                                             true, 1, "type");
                      fprintf (f, ")");
-                     --j;
+                     ++j;
                    }
-                 while (j >= 0 && !s->ifexpr_vec[j].is_with);
-                 i = j + 1;
+                 while (j < s->ifexpr_vec.length ()
+                        && !s->ifexpr_vec[j].is_with);
+                 i = j - 1;
                }
              fprintf (f, ")\n");
            }
@@ -2511,18 +2511,6 @@ parse_op (cpp_reader *r)
   return op;
 }
 
-/* Return a reversed copy of V.  */
-
-template <class T>
-static vec<T>
-copy_reverse (vec<T> v)
-{
-  vec<T> c = vNULL;
-  for (int i = v.length ()-1; i >= 0; --i)
-    c.safe_push (v[i]);
-  return c;
-}
-
 /* Parse
      'simplify' [ <ident> ] <expr> <result-op>
    with
@@ -2557,12 +2545,12 @@ parse_simplify (cpp_reader *r, source_lo
       if (matcher->nargs == -1)
        matcher->nargs = 0;
       simplifiers.safe_push
-       (new simplify (match, match_location, NULL, token->src_loc));
+       (new simplify (match, match_location, NULL,
+                      token->src_loc, active_ifs.copy ()));
       return;
     }
 
-  auto_vec<if_or_with> ifexprs;
-  ifexprs.safe_splice (active_ifs);
+  unsigned active_ifs_len = active_ifs.length ();
   while (1)
     {
       if (token->type == CPP_OPEN_PAREN)
@@ -2572,8 +2560,8 @@ parse_simplify (cpp_reader *r, source_lo
          if (peek_ident (r, "if"))
            {
              eat_ident (r, "if");
-             ifexprs.safe_push (if_or_with (parse_c_expr (r, CPP_OPEN_PAREN),
-                                            token->src_loc, false));
+             active_ifs.safe_push (if_or_with (parse_c_expr (r, 
CPP_OPEN_PAREN),
+                                               token->src_loc, false));
              /* If this if is immediately closed then it contains a
                 manual matcher or is part of a predicate definition.
                 Push it.  */
@@ -2587,7 +2575,7 @@ parse_simplify (cpp_reader *r, source_lo
                    matcher->nargs = 0;
                  simplifiers.safe_push
                      (new simplify (match, match_location, NULL,
-                                    paren_loc, ifexprs));
+                                    paren_loc, active_ifs.copy ()));
                }
            }
          else if (peek_ident (r, "with"))
@@ -2596,7 +2584,7 @@ parse_simplify (cpp_reader *r, source_lo
              /* Parse (with c-expr expr) as (if-with (true) expr).  */
              c_expr *e = parse_c_expr (r, CPP_OPEN_BRACE);
              e->nr_stmts = 0;
-             ifexprs.safe_push (if_or_with (e, token->src_loc, true));
+             active_ifs.safe_push (if_or_with (e, token->src_loc, true));
            }
          else
            {
@@ -2614,13 +2602,13 @@ parse_simplify (cpp_reader *r, source_lo
                }
              simplifiers.safe_push
                  (new simplify (match, match_location, op,
-                                token->src_loc, ifexprs));
+                                token->src_loc, active_ifs.copy ()));
              eat_token (r, CPP_CLOSE_PAREN);
              /* A "default" result closes the enclosing scope.  */
-             if (ifexprs.length () > 0)
+             if (active_ifs.length () > active_ifs_len)
                {
                  eat_token (r, CPP_CLOSE_PAREN);
-                 ifexprs.pop ();
+                 active_ifs.pop ();
                }
              else
                return;
@@ -2629,10 +2617,10 @@ parse_simplify (cpp_reader *r, source_lo
       else if (token->type == CPP_CLOSE_PAREN)
        {
          /* Close a scope if requested.  */
-         if (ifexprs.length () > 0)
+         if (active_ifs.length () > active_ifs_len)
            {
              eat_token (r, CPP_CLOSE_PAREN);
-             ifexprs.pop ();
+             active_ifs.pop ();
              token = peek (r);
            }
          else
@@ -2644,12 +2632,12 @@ parse_simplify (cpp_reader *r, source_lo
            fatal_at (token, "expected match operand expression");
          simplifiers.safe_push
              (new simplify (match, match_location, parse_op (r),
-                            token->src_loc, ifexprs));
+                            token->src_loc, active_ifs.copy ()));
          /* A "default" result closes the enclosing scope.  */
-         if (ifexprs.length () > 0)
+         if (active_ifs.length () > active_ifs_len)
            {
              eat_token (r, CPP_CLOSE_PAREN);
-             ifexprs.pop ();
+             active_ifs.pop ();
            }
          else
            return;

Reply via email to