OSDN Git Service

2004-06-09 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / coverage.c
index e6f4129..8e95283 100644 (file)
@@ -1,6 +1,6 @@
 /* Read and write coverage files, and associated functionality.
    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999,
-   2000, 2001, 2003  Free Software Foundation, Inc.
+   2000, 2001, 2003, 2004 Free Software Foundation, Inc.
    Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
    based on some ideas from Dain Samples of UC Berkeley.
    Further mangling by Bob Manson, Cygnus Support.
@@ -96,7 +96,11 @@ static char *da_file_name;
 /* Hash table of count data.  */
 static htab_t counts_hash = NULL;
 
-/* The names of the counter tables.  */
+/* Trees representing the counter table arrays.  */
+static GTY(()) tree tree_ctr_tables[GCOV_COUNTERS];
+
+/* The names of the counter tables.  Not used if we're
+   generating counters at tree level.  */
 static GTY(()) rtx ctr_labels[GCOV_COUNTERS];
 
 /* The names of merge functions for counters.  */
@@ -109,7 +113,7 @@ static int htab_counts_entry_eq (const void *, const void *);
 static void htab_counts_entry_del (void *);
 static void read_counts_file (void);
 static unsigned compute_checksum (void);
-static unsigned checksum_string (unsigned, const char *);
+static unsigned coverage_checksum_string (unsigned, const char *);
 static tree build_fn_info_type (unsigned);
 static tree build_fn_info_value (const struct function_list *, tree);
 static tree build_ctr_info_type (void);
@@ -154,7 +158,7 @@ read_counts_file (void)
   counts_entry_t *summaried = NULL;
   unsigned seen_summary = 0;
   gcov_unsigned_t tag;
-  int error = 0;
+  int is_error = 0;
 
   if (!gcov_open (da_file_name, 1))
     return;
@@ -172,8 +176,8 @@ read_counts_file (void)
       GCOV_UNSIGNED2STRING (v, tag);
       GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
 
-      warning ("`%s' is version `%.4s', expected version `%.4s'",
-              da_file_name, v, e);
+      warning ("`%s' is version `%.*s', expected version `%.*s'",
+              da_file_name, 4, v, 4, e);
       gcov_close ();
       return;
     }
@@ -250,17 +254,26 @@ read_counts_file (void)
              entry->summary.num = n_counts;
              entry->counts = xcalloc (n_counts, sizeof (gcov_type));
            }
-         else if (entry->checksum != checksum
-                  || entry->summary.num != n_counts)
+         else if (entry->checksum != checksum)
+           {
+             error ("coverage mismatch for function %u while reading execution counters.",
+                    fn_ident);
+             error ("checksum is %x instead of %x", entry->checksum, checksum);
+             htab_delete (counts_hash);
+             break;
+           }
+         else if (entry->summary.num != n_counts)
            {
-             warning ("coverage mismatch for function %u", fn_ident);
+             error ("coverage mismatch for function %u while reading execution counters.",
+                    fn_ident);
+             error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
              htab_delete (counts_hash);
              break;
            }
          else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
            {
-             warning ("cannot merge separate %s counters for function %u",
-                      ctr_names[elt.ctr], fn_ident);
+             error ("cannot merge separate %s counters for function %u",
+                    ctr_names[elt.ctr], fn_ident);
              goto skip_merge;
            }
 
@@ -278,14 +291,14 @@ read_counts_file (void)
        skip_merge:;
        }
       gcov_sync (offset, length);
-      if ((error = gcov_is_error ()))
+      if ((is_error = gcov_is_error ()))
        break;
     }
 
   if (!gcov_is_eof ())
     {
-      warning (error < 0 ? "`%s' has overflowed" : "`%s' is corrupted",
-              da_file_name);
+      error (is_error < 0 ? "`%s' has overflowed" : "`%s' is corrupted",
+            da_file_name);
       htab_delete (counts_hash);
     }
 
@@ -299,6 +312,7 @@ get_coverage_counts (unsigned counter, unsigned expected,
                     const struct gcov_ctr_summary **summary)
 {
   counts_entry_t *entry, elt;
+  gcov_unsigned_t checksum = -1;
 
   /* No hash table, no counts.  */
   if (!counts_hash)
@@ -306,8 +320,10 @@ get_coverage_counts (unsigned counter, unsigned expected,
       static int warned = 0;
 
       if (!warned++)
-       warning ("file %s not found, execution counts assumed to be zero",
-                da_file_name);
+       inform ((flag_guess_branch_prob
+                ? "file %s not found, execution counts estimated"
+                : "file %s not found, execution counts assumed to be zero"),
+               da_file_name);
       return NULL;
     }
 
@@ -321,12 +337,22 @@ get_coverage_counts (unsigned counter, unsigned expected,
       return 0;
     }
 
-  if (expected != entry->summary.num
-      || compute_checksum () != entry->checksum)
+  checksum = compute_checksum ();
+  if (entry->checksum != checksum)
     {
-      warning ("coverage mismatch for `%s'", IDENTIFIER_POINTER
-              (DECL_ASSEMBLER_NAME (current_function_decl)));
-      return NULL;
+      error ("coverage mismatch for function '%s' while reading counter '%s'.",
+            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)),
+            ctr_names[counter]);
+      error ("checksum is %x instead of %x", entry->checksum, checksum);
+      return 0;
+    }
+  else if (entry->summary.num != expected)
+    {
+      error ("coverage mismatch for function '%s' while reading counter '%s'.",
+            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)),
+            ctr_names[counter]);
+      error ("number of counters is %d instead of %d", entry->summary.num, expected);
+      return 0;
     }
 
   if (summary)
@@ -347,13 +373,22 @@ coverage_counter_alloc (unsigned counter, unsigned num)
   if (!num)
     return 1;
 
-  if (!ctr_labels[counter])
+  if (!tree_ctr_tables[counter])
     {
       /* Generate and save a copy of this so it can be shared.  */
+      /* We don't know the size yet; make it big enough that nobody
+        will make any clever transformation on it.  */
       char buf[20];
-
+      tree domain_tree
+        = build_index_type (build_int_2 (1000, 0)); /* replaced later */
+      tree gcov_type_array_type
+        = build_array_type (GCOV_TYPE_NODE, domain_tree);
+      tree_ctr_tables[counter]
+        = build_decl (VAR_DECL, NULL_TREE, gcov_type_array_type);
+      TREE_STATIC (tree_ctr_tables[counter]) = 1;
       ASM_GENERATE_INTERNAL_LABEL (buf, "LPBX", counter + 1);
-      ctr_labels[counter] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
+      DECL_NAME (tree_ctr_tables[counter]) = get_identifier (buf);
+      DECL_ALIGN (tree_ctr_tables[counter]) = TYPE_ALIGN (GCOV_TYPE_NODE);
     }
   fn_b_ctrs[counter] = fn_n_ctrs[counter];
   fn_n_ctrs[counter] += num;
@@ -364,7 +399,7 @@ coverage_counter_alloc (unsigned counter, unsigned num)
 /* Generate a MEM rtl to access COUNTER NO.  */
 
 rtx
-coverage_counter_ref (unsigned counter, unsigned no)
+rtl_coverage_counter_ref (unsigned counter, unsigned no)
 {
   unsigned gcov_size = tree_low_cst (TYPE_SIZE (GCOV_TYPE_NODE), 1);
   enum machine_mode mode = mode_for_size (gcov_size, MODE_INT, 0);
@@ -373,34 +408,87 @@ coverage_counter_ref (unsigned counter, unsigned no)
   if (no >= fn_n_ctrs[counter] - fn_b_ctrs[counter])
     abort ();
   no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
+  if (!ctr_labels[counter])
+      {
+        ctr_labels[counter] = gen_rtx_SYMBOL_REF (Pmode,
+                              ggc_strdup (IDENTIFIER_POINTER (DECL_NAME
+                              (tree_ctr_tables[counter]))));
+        SYMBOL_REF_FLAGS (ctr_labels[counter]) = SYMBOL_FLAG_LOCAL;
+      }
   ref = plus_constant (ctr_labels[counter], gcov_size / BITS_PER_UNIT * no);
   ref = gen_rtx_MEM (mode, ref);
   set_mem_alias_set (ref, new_alias_set ());
+  MEM_NOTRAP_P (ref) = 1;
 
   return ref;
 }
+
+/* Generate a tree to access COUNTER NO.  */
+
+tree
+tree_coverage_counter_ref (unsigned counter, unsigned no)
+{
+  tree t;
+
+  if (no >= fn_n_ctrs[counter] - fn_b_ctrs[counter])
+    abort ();
+  no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
+
+  /* "no" here is an array index, scaled to bytes later.  */
+  t = build (ARRAY_REF, GCOV_TYPE_NODE, tree_ctr_tables[counter],
+            build_int_2 (no, 0));
+  return t;
+}
 \f
 /* Generate a checksum for a string.  CHKSUM is the current
    checksum.  */
 
 static unsigned
-checksum_string (unsigned chksum, const char *string)
+coverage_checksum_string (unsigned chksum, const char *string)
 {
-  do
+  int i;
+  char *dup = NULL;
+
+  /* Look for everything that looks if it were produced by
+     get_file_function_name_long and zero out the second part
+     that may result from flag_random_seed.  This is not critical
+     as the checksums are used only for sanity checking.  */
+  for (i = 0; string[i]; i++)
     {
-      unsigned value = *string << 24;
-      unsigned ix;
-
-      for (ix = 8; ix--; value <<= 1)
-       {
-         unsigned feedback;
-
-         feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
-         chksum <<= 1;
-         chksum ^= feedback;
-       }
+      if (!strncmp (string + i, "_GLOBAL__", 9))
+       for (i = i + 9; string[i]; i++)
+         if (string[i]=='_')
+           {
+             int y;
+             unsigned seed;
+
+             for (y = 1; y < 9; y++)
+               if (!(string[i + y] >= '0' && string[i + y] <= '9')
+                   && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
+                 break;
+             if (y != 9 || string[i + 9] != '_')
+               continue;
+             for (y = 10; y < 18; y++)
+               if (!(string[i + y] >= '0' && string[i + y] <= '9')
+                   && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
+                 break;
+             if (y != 18)
+               continue;
+             if (!sscanf (string + i + 10, "%X", &seed))
+               abort ();
+             if (seed != crc32_string (0, flag_random_seed))
+               continue;
+             string = dup = xstrdup (string);
+             for (y = 10; y < 18; y++)
+               dup[i + y] = '0';
+             break;
+           }
+      break;
     }
-  while (*string++);
+
+  chksum = crc32_string (chksum, string);
+  if (dup)
+    free (dup);
 
   return chksum;
 }
@@ -412,8 +500,9 @@ compute_checksum (void)
 {
   unsigned chksum = DECL_SOURCE_LINE (current_function_decl);
 
-  chksum = checksum_string (chksum, DECL_SOURCE_FILE (current_function_decl));
-  chksum = checksum_string
+  chksum = coverage_checksum_string (chksum,
+                                    DECL_SOURCE_FILE (current_function_decl));
+  chksum = coverage_checksum_string
     (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
 
   return chksum;
@@ -507,7 +596,7 @@ coverage_end_function (void)
 static tree
 build_fn_info_type (unsigned int counters)
 {
-  tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
+  tree type = lang_hooks.types.make_type (RECORD_TYPE);
   tree field, fields;
   tree array_type;
 
@@ -581,7 +670,7 @@ build_fn_info_value (const struct function_list *function, tree type)
 static tree
 build_ctr_info_type (void)
 {
-  tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
+  tree type = lang_hooks.types.make_type (RECORD_TYPE);
   tree field, fields = NULL_TREE;
   tree gcov_ptr_type = build_pointer_type (GCOV_TYPE_NODE);
   tree gcov_merge_fn_type;
@@ -631,19 +720,20 @@ build_ctr_info_value (unsigned int counter, tree type)
 
   if (prg_n_ctrs[counter])
     {
-      tree array_type, array;
+      tree array_type;
 
       array_type = build_index_type (build_int_2 (prg_n_ctrs[counter] - 1, 0));
       array_type = build_array_type (TREE_TYPE (TREE_TYPE (fields)),
                                     array_type);
 
-      array = build (VAR_DECL, array_type, NULL_TREE, NULL_TREE);
-      TREE_STATIC (array) = 1;
-      DECL_NAME (array) = get_identifier (XSTR (ctr_labels[counter], 0));
-      assemble_variable (array, 0, 0, 0);
+      TREE_TYPE (tree_ctr_tables[counter]) = array_type;
+      DECL_SIZE (tree_ctr_tables[counter]) = TYPE_SIZE (array_type);
+      DECL_SIZE_UNIT (tree_ctr_tables[counter]) = TYPE_SIZE_UNIT (array_type);
+      assemble_variable (tree_ctr_tables[counter], 0, 0, 0);
 
       value = tree_cons (fields,
-                        build1 (ADDR_EXPR, TREE_TYPE (fields), array),
+                        build1 (ADDR_EXPR, TREE_TYPE (fields), 
+                                           tree_ctr_tables[counter]),
                         value);
     }
   else
@@ -691,7 +781,7 @@ build_gcov_info (void)
     if (prg_ctr_mask & (1 << ix))
       n_ctr_types++;
 
-  type = (*lang_hooks.types.make_type) (RECORD_TYPE);
+  type = lang_hooks.types.make_type (RECORD_TYPE);
   const_type = build_qualified_type (type, TYPE_QUAL_CONST);
 
   /* Version ident */
@@ -824,8 +914,7 @@ create_coverage (void)
 
   gcov_info_value = build_gcov_info ();
 
-  gcov_info = build (VAR_DECL, TREE_TYPE (gcov_info_value),
-                    NULL_TREE, NULL_TREE);
+  gcov_info = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (gcov_info_value));
   DECL_INITIAL (gcov_info) = gcov_info_value;
 
   TREE_STATIC (gcov_info) = 1;
@@ -850,7 +939,6 @@ create_coverage (void)
   DECL_RESULT (ctor) = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
   DECL_UNINLINABLE (ctor) = 1;
 
-  ctor = (*lang_hooks.decls.pushdecl) (ctor);
   rest_of_decl_compilation (ctor, 0, 1, 0);
   announce_function (ctor);
   current_function_decl = ctor;
@@ -874,8 +962,8 @@ create_coverage (void)
   current_function_decl = NULL_TREE;
 
   if (targetm.have_ctors_dtors)
-    (* targetm.asm_out.constructor) (XEXP (DECL_RTL (ctor), 0),
-                                    DEFAULT_INIT_PRIORITY);
+    targetm.asm_out.constructor (XEXP (DECL_RTL (ctor), 0),
+                                DEFAULT_INIT_PRIORITY);
 }
 \f
 /* Perform file-level initialization. Read in data file, generate name