OSDN Git Service

* Fix for g++/15861
[pf3gnuchains/gcc-fork.git] / gcc / coverage.c
index 88d677c..888d5cd 100644 (file)
@@ -39,11 +39,11 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include "function.h"
 #include "toplev.h"
 #include "ggc.h"
-#include "target.h"
 #include "coverage.h"
-#include "libfuncs.h"
 #include "langhooks.h"
 #include "hashtab.h"
+#include "tree-iterator.h"
+#include "cgraph.h"
 
 #include "gcov-io.c"
 
@@ -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.  */
@@ -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;
     }
@@ -369,14 +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));
-      SYMBOL_REF_FLAGS (ctr_labels[counter]) = SYMBOL_FLAG_LOCAL;
+      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;
@@ -387,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);
@@ -396,6 +408,13 @@ 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 ());
@@ -403,6 +422,25 @@ coverage_counter_ref (unsigned counter, unsigned no)
 
   return ref;
 }
+
+/* Generate a tree to access COUNTER NO.  */
+
+tree
+tree_coverage_counter_ref (unsigned counter, unsigned no)
+{
+  tree domain_type = TYPE_DOMAIN (TREE_TYPE (tree_ctr_tables[counter]));
+
+  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.  */
+  return build (ARRAY_REF, GCOV_TYPE_NODE, tree_ctr_tables[counter],
+               fold_convert (domain_type, build_int_2 (no, 0)),
+               TYPE_MIN_VALUE (domain_type),
+               size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (GCOV_TYPE_NODE),
+                           size_int (TYPE_ALIGN (GCOV_TYPE_NODE))));
+}
 \f
 /* Generate a checksum for a string.  CHKSUM is the current
    checksum.  */
@@ -462,10 +500,11 @@ coverage_checksum_string (unsigned chksum, const char *string)
 static unsigned
 compute_checksum (void)
 {
-  unsigned chksum = DECL_SOURCE_LINE (current_function_decl);
+  expanded_location xloc
+    = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
+  unsigned chksum = xloc.line;
 
-  chksum = coverage_checksum_string (chksum,
-                                    DECL_SOURCE_FILE (current_function_decl));
+  chksum = coverage_checksum_string (chksum, xloc.file);
   chksum = coverage_checksum_string
     (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
 
@@ -485,8 +524,8 @@ coverage_begin_output (void)
 
   if (!bbg_function_announced)
     {
-      const char *file = DECL_SOURCE_FILE (current_function_decl);
-      unsigned line = DECL_SOURCE_LINE (current_function_decl);
+      expanded_location xloc
+       = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
       unsigned long offset;
 
       if (!bbg_file_opened)
@@ -508,8 +547,8 @@ coverage_begin_output (void)
       gcov_write_unsigned (compute_checksum ());
       gcov_write_string (IDENTIFIER_POINTER
                         (DECL_ASSEMBLER_NAME (current_function_decl)));
-      gcov_write_string (file);
-      gcov_write_unsigned (line);
+      gcov_write_string (xloc.file);
+      gcov_write_unsigned (xloc.line);
       gcov_write_length (offset);
 
       bbg_function_announced = 1;
@@ -684,19 +723,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_decl (VAR_DECL, NULL_TREE, array_type);
-      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
@@ -864,70 +904,42 @@ build_gcov_info (void)
 static void
 create_coverage (void)
 {
-  tree gcov_info, gcov_info_value;
-  char name[20];
-  char *ctor_name;
-  tree ctor;
-  rtx gcov_info_address;
+  tree gcov_info, gcov_init, body, t;
+  char name_buf[32];
 
   no_coverage = 1; /* Disable any further coverage.  */
 
   if (!prg_ctr_mask)
     return;
 
-  gcov_info_value = build_gcov_info ();
-
-  gcov_info = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (gcov_info_value));
-  DECL_INITIAL (gcov_info) = gcov_info_value;
+  t = build_gcov_info ();
 
+  gcov_info = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (t));
   TREE_STATIC (gcov_info) = 1;
-  ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 0);
-  DECL_NAME (gcov_info) = get_identifier (name);
+  ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
+  DECL_NAME (gcov_info) = get_identifier (name_buf);
+  DECL_INITIAL (gcov_info) = t;
 
   /* Build structure.  */
   assemble_variable (gcov_info, 0, 0, 0);
 
-  /* Build the constructor function to invoke __gcov_init.  */
-  ctor_name = concat (IDENTIFIER_POINTER (get_file_function_name ('I')),
-                     "_GCOV", NULL);
-  ctor = build_decl (FUNCTION_DECL, get_identifier (ctor_name),
-                    build_function_type (void_type_node, NULL_TREE));
-  free (ctor_name);
-  DECL_EXTERNAL (ctor) = 0;
-
-  /* It can be a static function as long as collect2 does not have
-     to scan the object file to find its ctor/dtor routine.  */
-  TREE_PUBLIC (ctor) = ! targetm.have_ctors_dtors;
-  TREE_USED (ctor) = 1;
-  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;
-  make_decl_rtl (ctor, NULL);
-  init_function_start (ctor);
-  expand_function_start (ctor, 0);
-  /* Actually generate the code to call __gcov_init.  */
-  gcov_info_address = force_reg (Pmode, XEXP (DECL_RTL (gcov_info), 0));
-  emit_library_call (gcov_init_libfunc, LCT_NORMAL, VOIDmode, 1,
-                    gcov_info_address, Pmode);
-
-  expand_function_end ();
-  /* Create a dummy BLOCK.  */
-  DECL_INITIAL (ctor) = make_node (BLOCK);
-  TREE_USED (DECL_INITIAL (ctor)) = 1;
-
-  rest_of_compilation (ctor);
-
-  if (! quiet_flag)
-    fflush (asm_out_file);
-  current_function_decl = NULL_TREE;
-
-  if (targetm.have_ctors_dtors)
-    targetm.asm_out.constructor (XEXP (DECL_RTL (ctor), 0),
-                                DEFAULT_INIT_PRIORITY);
+  /* Build a decl for __gcov_init.  */
+  t = build_pointer_type (TREE_TYPE (gcov_info));
+  t = build_function_type_list (void_type_node, t, NULL);
+  t = build_decl (FUNCTION_DECL, get_identifier ("__gcov_init"), t);
+  TREE_PUBLIC (t) = 1;
+  DECL_EXTERNAL (t) = 1;
+  gcov_init = t;
+
+  /* Generate a call to __gcov_init(&gcov_info).  */
+  body = NULL;
+  t = build_fold_addr_expr (gcov_info);
+  t = tree_cons (NULL, t, NULL);
+  t = build_function_call_expr (gcov_init, t);
+  append_to_statement_list (t, &body);
+
+  /* Generate a constructor to run it.  */
+  cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
 }
 \f
 /* Perform file-level initialization. Read in data file, generate name