This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: RFD: annotate iterator patterns with expanded forms


On 12/01/2015 04:31 PM, Bernd Schmidt wrote:
On 12/01/2015 04:23 PM, Jakub Jelinek wrote:
With the comments in the *.md file I'd worry about them getting out of
date,
or people feeling they have to edit them manually (rather than being
regenerated or whatever).

I suppose we could have a Makefile rule that checks for out-of-date
comments (by redoing the annotation and running diff). That would also
alleviate the second worry.

I'd much prefer the original source files to be searchable, because if I
want to make modifications, I can't make them in tmp-mddump.md and going
back and forth between two files is just inconvenient.

The automatic Makefile approach might look something like this. The effect is similar to what happens when you edit tm.texi.in, except the build would not be interrupted every time, only when you modify the iterator expansion of a pattern. There's a new rtx code which can be put into a machine description to enable this feature.

This could be further tweaked to make (enable_auto_annotate) push/poppable; I could imagine a world where we'd want it enabled for i386.md but not for sse.md. Another tweak might be to have every line marked as "--GEN--" both for clarity and for robustifying the part of the script that removes the previous version of the annotations.

Thoughts?


Bernd


diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index bee2879..ad4101d 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -2189,12 +2189,20 @@ s-conditions: $(MD_DEPS) build/genconditions$(build_exeext)
 	$(SHELL) $(srcdir)/../move-if-change tmp-condmd.c build/gencondmd.c
 	$(STAMP) s-conditions
 
-insn-conditions.md: s-condmd; @true
+insn-conditions.md: s-condmd s-annotations; @true
 s-condmd: build/gencondmd$(build_exeext)
 	$(RUN_GEN) build/gencondmd$(build_exeext) > tmp-cond.md
 	$(SHELL) $(srcdir)/../move-if-change tmp-cond.md insn-conditions.md
 	$(STAMP) s-condmd
 
+build/genannotation.ed: s-annotations; @true
+s-annotations: $(MD_DEPS) build/genannotations$(build_exeext)
+	$(RUN_GEN) build/genannotations$(build_exeext) $(md_file) \
+		> tmp-annotate.ed
+	$(SHELL) $(srcdir)/../move-if-change tmp-annotate.ed \
+		build/genannotation.ed
+	$(SHELL) $(srcdir)/annotate-md.sh build/genannotation.ed
+	$(STAMP) s-annotations
 
 # These files are generated by running the same generator more than
 # once with different options, so they have custom rules.  The
@@ -2577,6 +2585,9 @@ build/gengtype.o: $(BCONFIG_H)
 
 CFLAGS-errors.o += -DHOST_GENERATOR_FILE
 
+build/genannotations.o : genannotations.c $(RTL_BASE_H) $(BCONFIG_H)	\
+  $(SYSTEM_H) coretypes.h $(GTM_H) errors.h $(READ_MD_H) gensupport.h	\
+  $(OBSTACK_H)
 build/genmddeps.o: genmddeps.c $(BCONFIG_H) $(SYSTEM_H) coretypes.h	\
   errors.h $(READ_MD_H)
 build/genmodes.o : genmodes.c $(BCONFIG_H) $(SYSTEM_H) errors.h		\
@@ -2608,8 +2619,10 @@ build/gencfn-macros.o : gencfn-macros.c $(BCONFIG_H) $(SYSTEM_H)	\
 # even if GCC is being compiled to run on some other machine.
 
 # All these programs use the RTL reader ($(BUILD_RTL)).
-genprogrtl = attr attr-common attrtab automata codes conditions config emit \
-	     extract flags mddump opinit output peep preds recog target-def
+genprogrtl = annotations attr attr-common attrtab automata codes conditions \
+	     config emit extract flags mddump opinit output peep preds recog \
+	     target-def
+
 $(genprogrtl:%=build/gen%$(build_exeext)): $(BUILD_RTL)
 
 # All these programs use the MD reader ($(BUILD_MD)).
diff --git a/gcc/annotate-md.sh b/gcc/annotate-md.sh
new file mode 100644
index 0000000..7c21eec
--- /dev/null
+++ b/gcc/annotate-md.sh
@@ -0,0 +1,46 @@
+#! /bin/sh -x
+
+# Copyright (C) 2015 Free Software Foundation, Inc.
+# This file is part of GCC.
+
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.  
+
+# Take a file generated by genannotations.c and verify the annotations
+# in the .md files.
+
+mkdir -p annotated || exit 1
+fail=false
+FILES=`cat $1 |sed s,':.*$,,'|sort|uniq`
+for f in $FILES; do
+    fn=`basename $f`
+    sed '/.GEN. Expands.to/,/^[^;]/s/^;;/;; AUTOGEN-DELETE/' < $f > annotated/tmpfile
+    grep $f $1 |sed 's,^[^:]*:,,'| sort -r -n |sed 's,\\n,\n,g' >tmp-annotate
+    echo "w" >>tmp-annotate
+    echo "q" >>tmp-annotate
+    ed annotated/tmpfile < tmp-annotate >/dev/null 2>&1
+    grep -v AUTOGEN-DELETE < annotated/tmpfile >annotated/$fn
+    if ! cmp $f annotated/$fn >/dev/null 2>&1 ; then
+	fail=true
+	echo $f
+    fi
+done
+if test $fail = true; then
+    echo "Annotations mismatch in one of the machine description files."
+    echo "Automatically generated updates are available in the annotated/"
+    echo "directory. Verify that the automated annotations are correct,"
+    echo "then copy those files over."
+    exit 1
+fi
+exit 0
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index cbb9ffd..f1dc31d 100644
--- a/gcc/config/i386/i386.md
+++ b/gcc/config/i386/i386.md
@@ -65,6 +65,9 @@
 ;; ^ -- print addr32 prefix if TARGET_64BIT and Pmode != word_mode
 ;; ! -- print MPX prefix for jxx/call/ret instructions if required.
 
+;; This generates the "Expands to" annotations.
+(enable_auto_annotate)
+
 (define_c_enum "unspec" [
   ;; Relocation specifiers
   UNSPEC_GOT
diff --git a/gcc/genannotations.c b/gcc/genannotations.c
new file mode 100644
index 0000000..840c4193
--- /dev/null
+++ b/gcc/genannotations.c
@@ -0,0 +1,46 @@
+/* Generate annotations from machine description.
+   Copyright (C) 2015 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "bconfig.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "rtl.h"
+#include "errors.h"
+#include "obstack.h"
+#include "read-md.h"
+#include "gensupport.h"
+
+int
+main (int argc, char **argv)
+{
+  progname = argv[0];
+  if (argc <= 1)
+    fatal ("no input file name");
+  gen_annotations = 1;
+  if (!init_rtx_reader_args (argc, argv))
+    return FATAL_EXIT_CODE;
+
+  /* Everything happens in gensupport, we don't even need to read_md_rtx.  */
+
+  if (have_error || ferror (stdout) || fflush (stdout) || fclose (stdout))
+    return FATAL_EXIT_CODE;
+
+  return SUCCESS_EXIT_CODE;
+}
diff --git a/gcc/gensupport.c b/gcc/gensupport.c
index 484ead2..10ecbb4 100644
--- a/gcc/gensupport.c
+++ b/gcc/gensupport.c
@@ -40,6 +40,10 @@ int target_flags;
 
 int insn_elision = 1;
 
+/* Set to 1 by the driver program if annotations are wanted at all, then
+   set to 2 if a enable_auto_annotations rtx is seen.  */
+int gen_annotations = 0;
+
 static struct obstack obstack;
 struct obstack *rtl_obstack = &obstack;
 
@@ -492,6 +496,11 @@ process_rtx (rtx desc, file_location loc)
 {
   switch (GET_CODE (desc))
     {
+    case ENABLE_AUTO_ANNOTATE:
+      if (gen_annotations)
+	gen_annotations = 2;
+      break;
+
     case DEFINE_INSN:
       queue_pattern (desc, &define_insn_tail, loc);
       break;
@@ -2236,6 +2245,33 @@ rtx_handle_directive (file_location loc, const char *rtx_name)
 
   rtx x;
   unsigned int i;
+  if (gen_annotations
+      && subrtxs.length () > 1
+      && (GET_CODE (subrtxs[0]) == DEFINE_INSN
+	  || GET_CODE (subrtxs[0]) == DEFINE_EXPAND))
+    {
+      const char *p = "";
+      printf ("%s:%d\\ni\\n;; (GEN) Expands to:\\n;; ", loc.filename, loc.lineno);
+      int len = 3;
+      int p_len = 0;
+      FOR_EACH_VEC_ELT (subrtxs, i, x)
+	{
+	  int this_len = strlen (XSTR (x, 0));
+	  if (len + this_len + p_len >= 78)
+	    {
+	      printf ("\\n;; ");
+	      len = 3;
+	      p = "";
+	      p_len = 0;
+	    }
+	  printf ("%s%s", p, XSTR (x, 0));
+	  len += this_len + p_len;
+	  p = ", ";
+	  p_len = 2;
+	}
+      printf ("\\n.\n");
+    }
+
   FOR_EACH_VEC_ELT (subrtxs, i, x)
     process_rtx (x, loc);
 }
diff --git a/gcc/gensupport.h b/gcc/gensupport.h
index 0199e39..72fba10 100644
--- a/gcc/gensupport.h
+++ b/gcc/gensupport.h
@@ -50,6 +50,11 @@ extern unsigned int get_num_insn_codes ();
    Must be set before calling init_md_reader.  */
 extern int insn_elision;
 
+/* Set this to 1 to enable generation of annotations for patterns
+   using iterators.  If nonzero, it gets set to 2 if we encounter an
+   enable_auto_annotate rtx.  */
+extern int gen_annotations;
+
 /* Return the C test that says whether a definition rtx can be used,
    or "" if it can be used unconditionally.  */
 extern const char *get_c_test (rtx);
diff --git a/gcc/rtl.def b/gcc/rtl.def
index cca469d..fc85c15 100644
--- a/gcc/rtl.def
+++ b/gcc/rtl.def
@@ -1340,6 +1340,10 @@ DEF_RTL_EXPR(DEFINE_SUBST, "define_subst", "sEsE", RTX_EXTRA)
    GCC internals manual, under "RTL Templates Transformations".  */
 DEF_RTL_EXPR(DEFINE_SUBST_ATTR, "define_subst_attr", "ssss", RTX_EXTRA)
 
+/* Used by md files to enable autogeneration of annotations for
+   iterator patterns.  */
+DEF_RTL_EXPR(ENABLE_AUTO_ANNOTATE, "enable_auto_annotate", "", RTX_OBJ)
+
 #endif /* GENERATOR_FILE */
 
 /*

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]