OSDN Git Service

* tree-ssa-ifcombine.c (get_name_for_bit_test): Use
[pf3gnuchains/gcc-fork.git] / gcc / coverage.c
1 /* Read and write coverage files, and associated functionality.
2    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999,
3    2000, 2001, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4    Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
5    based on some ideas from Dain Samples of UC Berkeley.
6    Further mangling by Bob Manson, Cygnus Support.
7    Further mangled by Nathan Sidwell, CodeSourcery
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25
26 #define GCOV_LINKAGE
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "flags.h"
35 #include "output.h"
36 #include "regs.h"
37 #include "expr.h"
38 #include "function.h"
39 #include "toplev.h"
40 #include "ggc.h"
41 #include "coverage.h"
42 #include "langhooks.h"
43 #include "hashtab.h"
44 #include "tree-iterator.h"
45 #include "cgraph.h"
46 #include "tree-pass.h"
47
48 #include "gcov-io.c"
49
50 struct function_list
51 {
52   struct function_list *next;    /* next function */
53   unsigned ident;                /* function ident */
54   unsigned checksum;             /* function checksum */
55   unsigned n_ctrs[GCOV_COUNTERS];/* number of counters.  */
56 };
57
58 /* Counts information for a function.  */
59 typedef struct counts_entry
60 {
61   /* We hash by  */
62   unsigned ident;
63   unsigned ctr;
64
65   /* Store  */
66   unsigned checksum;
67   gcov_type *counts;
68   struct gcov_ctr_summary summary;
69
70   /* Workspace */
71   struct counts_entry *chain;
72
73 } counts_entry_t;
74
75 static struct function_list *functions_head = 0;
76 static struct function_list **functions_tail = &functions_head;
77 static unsigned no_coverage = 0;
78
79 /* Cumulative counter information for whole program.  */
80 static unsigned prg_ctr_mask; /* Mask of counter types generated.  */
81 static unsigned prg_n_ctrs[GCOV_COUNTERS]; /* Total counters allocated.  */
82
83 /* Counter information for current function.  */
84 static unsigned fn_ctr_mask; /* Mask of counters used.  */
85 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated.  */
86 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base.  */
87
88 /* Name of the output file for coverage output file.  */
89 static char *bbg_file_name;
90 static unsigned bbg_file_opened;
91 static int bbg_function_announced;
92
93 /* Name of the count data file.  */
94 static char *da_file_name;
95
96 /* Hash table of count data.  */
97 static htab_t counts_hash = NULL;
98
99 /* Trees representing the counter table arrays.  */
100 static GTY(()) tree tree_ctr_tables[GCOV_COUNTERS];
101
102 /* The names of the counter tables.  Not used if we're
103    generating counters at tree level.  */
104 static GTY(()) rtx ctr_labels[GCOV_COUNTERS];
105
106 /* The names of merge functions for counters.  */
107 static const char *const ctr_merge_functions[GCOV_COUNTERS] = GCOV_MERGE_FUNCTIONS;
108 static const char *const ctr_names[GCOV_COUNTERS] = GCOV_COUNTER_NAMES;
109
110 /* Forward declarations.  */
111 static hashval_t htab_counts_entry_hash (const void *);
112 static int htab_counts_entry_eq (const void *, const void *);
113 static void htab_counts_entry_del (void *);
114 static void read_counts_file (void);
115 static unsigned compute_checksum (void);
116 static unsigned coverage_checksum_string (unsigned, const char *);
117 static tree build_fn_info_type (unsigned);
118 static tree build_fn_info_value (const struct function_list *, tree);
119 static tree build_ctr_info_type (void);
120 static tree build_ctr_info_value (unsigned, tree);
121 static tree build_gcov_info (void);
122 static void create_coverage (void);
123 \f
124 /* Return the type node for gcov_type.  */
125
126 tree
127 get_gcov_type (void)
128 {
129   return lang_hooks.types.type_for_size (GCOV_TYPE_SIZE, false);
130 }
131
132 /* Return the type node for gcov_unsigned_t.  */
133
134 static tree
135 get_gcov_unsigned_t (void)
136 {
137   return lang_hooks.types.type_for_size (32, true);
138 }
139 \f
140 static hashval_t
141 htab_counts_entry_hash (const void *of)
142 {
143   const counts_entry_t *entry = of;
144
145   return entry->ident * GCOV_COUNTERS + entry->ctr;
146 }
147
148 static int
149 htab_counts_entry_eq (const void *of1, const void *of2)
150 {
151   const counts_entry_t *entry1 = of1;
152   const counts_entry_t *entry2 = of2;
153
154   return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
155 }
156
157 static void
158 htab_counts_entry_del (void *of)
159 {
160   counts_entry_t *entry = of;
161
162   free (entry->counts);
163   free (entry);
164 }
165
166 /* Read in the counts file, if available.  */
167
168 static void
169 read_counts_file (void)
170 {
171   gcov_unsigned_t fn_ident = 0;
172   gcov_unsigned_t checksum = -1;
173   counts_entry_t *summaried = NULL;
174   unsigned seen_summary = 0;
175   gcov_unsigned_t tag;
176   int is_error = 0;
177
178   if (!gcov_open (da_file_name, 1))
179     return;
180
181   if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
182     {
183       warning (0, "%qs is not a gcov data file", da_file_name);
184       gcov_close ();
185       return;
186     }
187   else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
188     {
189       char v[4], e[4];
190
191       GCOV_UNSIGNED2STRING (v, tag);
192       GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
193
194       warning (0, "%qs is version %q.*s, expected version %q.*s",
195                da_file_name, 4, v, 4, e);
196       gcov_close ();
197       return;
198     }
199
200   /* Read and discard the stamp.  */
201   gcov_read_unsigned ();
202   
203   counts_hash = htab_create (10,
204                              htab_counts_entry_hash, htab_counts_entry_eq,
205                              htab_counts_entry_del);
206   while ((tag = gcov_read_unsigned ()))
207     {
208       gcov_unsigned_t length;
209       gcov_position_t offset;
210
211       length = gcov_read_unsigned ();
212       offset = gcov_position ();
213       if (tag == GCOV_TAG_FUNCTION)
214         {
215           fn_ident = gcov_read_unsigned ();
216           checksum = gcov_read_unsigned ();
217           if (seen_summary)
218             {
219               /* We have already seen a summary, this means that this
220                  new function begins a new set of program runs. We
221                  must unlink the summaried chain.  */
222               counts_entry_t *entry, *chain;
223
224               for (entry = summaried; entry; entry = chain)
225                 {
226                   chain = entry->chain;
227                   entry->chain = NULL;
228                 }
229               summaried = NULL;
230               seen_summary = 0;
231             }
232         }
233       else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
234         {
235           counts_entry_t *entry;
236           struct gcov_summary summary;
237
238           gcov_read_summary (&summary);
239           seen_summary = 1;
240           for (entry = summaried; entry; entry = entry->chain)
241             {
242               struct gcov_ctr_summary *csum = &summary.ctrs[entry->ctr];
243
244               entry->summary.runs += csum->runs;
245               entry->summary.sum_all += csum->sum_all;
246               if (entry->summary.run_max < csum->run_max)
247                 entry->summary.run_max = csum->run_max;
248               entry->summary.sum_max += csum->sum_max;
249             }
250         }
251       else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
252         {
253           counts_entry_t **slot, *entry, elt;
254           unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
255           unsigned ix;
256
257           elt.ident = fn_ident;
258           elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
259
260           slot = (counts_entry_t **) htab_find_slot
261             (counts_hash, &elt, INSERT);
262           entry = *slot;
263           if (!entry)
264             {
265               *slot = entry = XCNEW (counts_entry_t);
266               entry->ident = elt.ident;
267               entry->ctr = elt.ctr;
268               entry->checksum = checksum;
269               entry->summary.num = n_counts;
270               entry->counts = XCNEWVEC (gcov_type, n_counts);
271             }
272           else if (entry->checksum != checksum)
273             {
274               error ("coverage mismatch for function %u while reading execution counters",
275                      fn_ident);
276               error ("checksum is %x instead of %x", entry->checksum, checksum);
277               htab_delete (counts_hash);
278               break;
279             }
280           else if (entry->summary.num != n_counts)
281             {
282               error ("coverage mismatch for function %u while reading execution counters",
283                      fn_ident);
284               error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
285               htab_delete (counts_hash);
286               break;
287             }
288           else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
289             {
290               error ("cannot merge separate %s counters for function %u",
291                      ctr_names[elt.ctr], fn_ident);
292               goto skip_merge;
293             }
294
295           if (elt.ctr < GCOV_COUNTERS_SUMMABLE
296               /* This should always be true for a just allocated entry,
297                  and always false for an existing one. Check this way, in
298                  case the gcov file is corrupt.  */
299               && (!entry->chain || summaried != entry))
300             {
301               entry->chain = summaried;
302               summaried = entry;
303             }
304           for (ix = 0; ix != n_counts; ix++)
305             entry->counts[ix] += gcov_read_counter ();
306         skip_merge:;
307         }
308       gcov_sync (offset, length);
309       if ((is_error = gcov_is_error ()))
310         {
311           error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
312                  da_file_name);
313           htab_delete (counts_hash);
314           break;
315         }
316     }
317
318   gcov_close ();
319 }
320
321 /* Returns the counters for a particular tag.  */
322
323 gcov_type *
324 get_coverage_counts (unsigned counter, unsigned expected,
325                      const struct gcov_ctr_summary **summary)
326 {
327   counts_entry_t *entry, elt;
328   gcov_unsigned_t checksum = -1;
329
330   /* No hash table, no counts.  */
331   if (!counts_hash)
332     {
333       static int warned = 0;
334
335       if (!warned++)
336         inform ((flag_guess_branch_prob
337                  ? "file %s not found, execution counts estimated"
338                  : "file %s not found, execution counts assumed to be zero"),
339                 da_file_name);
340       return NULL;
341     }
342
343   elt.ident = current_function_funcdef_no + 1;
344   elt.ctr = counter;
345   entry = htab_find (counts_hash, &elt);
346   if (!entry)
347     {
348       warning (0, "no coverage for function %qs found", IDENTIFIER_POINTER
349                (DECL_ASSEMBLER_NAME (current_function_decl)));
350       return NULL;
351     }
352
353   checksum = compute_checksum ();
354   if (entry->checksum != checksum
355       || entry->summary.num != expected)
356     {
357       static int warned = 0;
358       const char *id = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
359                          (current_function_decl));
360
361       if (warn_coverage_mismatch)
362         warning (OPT_Wcoverage_mismatch, "coverage mismatch for function "
363                  "%qs while reading counter %qs", id, ctr_names[counter]);
364       else
365         error ("coverage mismatch for function %qs while reading counter %qs",
366                id, ctr_names[counter]);
367
368       if (!inhibit_warnings)
369         {
370           if (entry->checksum != checksum)
371             inform ("checksum is %x instead of %x", entry->checksum, checksum);
372           else
373             inform ("number of counters is %d instead of %d",
374                     entry->summary.num, expected);
375         }
376
377       if (warn_coverage_mismatch
378           && !inhibit_warnings
379           && !warned++)
380         {
381           inform ("coverage mismatch ignored due to -Wcoverage-mismatch");
382           inform (flag_guess_branch_prob
383                   ? "execution counts estimated"
384                   : "execution counts assumed to be zero");
385           if (!flag_guess_branch_prob)
386             inform ("this can result in poorly optimized code");
387         }
388
389       return NULL;
390     }
391
392   if (summary)
393     *summary = &entry->summary;
394
395   return entry->counts;
396 }
397
398 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
399    allocation succeeded.  */
400
401 int
402 coverage_counter_alloc (unsigned counter, unsigned num)
403 {
404   if (no_coverage)
405     return 0;
406
407   if (!num)
408     return 1;
409
410   if (!tree_ctr_tables[counter])
411     {
412       /* Generate and save a copy of this so it can be shared.  Leave
413          the index type unspecified for now; it will be set after all
414          functions have been compiled.  */
415       char buf[20];
416       tree gcov_type_node = get_gcov_type ();
417       tree gcov_type_array_type
418         = build_array_type (gcov_type_node, NULL_TREE);
419       tree_ctr_tables[counter]
420         = build_decl (VAR_DECL, NULL_TREE, gcov_type_array_type);
421       TREE_STATIC (tree_ctr_tables[counter]) = 1;
422       ASM_GENERATE_INTERNAL_LABEL (buf, "LPBX", counter + 1);
423       DECL_NAME (tree_ctr_tables[counter]) = get_identifier (buf);
424       DECL_ALIGN (tree_ctr_tables[counter]) = TYPE_ALIGN (gcov_type_node);
425
426       if (dump_file)
427         fprintf (dump_file, "Using data file %s\n", da_file_name);
428     }
429   fn_b_ctrs[counter] = fn_n_ctrs[counter];
430   fn_n_ctrs[counter] += num;
431   fn_ctr_mask |= 1 << counter;
432   return 1;
433 }
434
435 /* Generate a tree to access COUNTER NO.  */
436
437 tree
438 tree_coverage_counter_ref (unsigned counter, unsigned no)
439 {
440   tree gcov_type_node = get_gcov_type ();
441
442   gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
443   no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
444
445   /* "no" here is an array index, scaled to bytes later.  */
446   return build4 (ARRAY_REF, gcov_type_node, tree_ctr_tables[counter],
447                  build_int_cst (NULL_TREE, no), NULL, NULL);
448 }
449
450 /* Generate a tree to access the address of COUNTER NO.  */
451
452 tree
453 tree_coverage_counter_addr (unsigned counter, unsigned no)
454 {
455   tree gcov_type_node = get_gcov_type ();
456
457   gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
458   no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
459
460   /* "no" here is an array index, scaled to bytes later.  */
461   return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
462                                        tree_ctr_tables[counter],
463                                        build_int_cst (NULL_TREE, no),
464                                        NULL, NULL));
465 }
466 \f
467 /* Generate a checksum for a string.  CHKSUM is the current
468    checksum.  */
469
470 static unsigned
471 coverage_checksum_string (unsigned chksum, const char *string)
472 {
473   int i;
474   char *dup = NULL;
475
476   /* Look for everything that looks if it were produced by
477      get_file_function_name and zero out the second part
478      that may result from flag_random_seed.  This is not critical
479      as the checksums are used only for sanity checking.  */
480   for (i = 0; string[i]; i++)
481     {
482       int offset = 0;
483       if (!strncmp (string + i, "_GLOBAL__N_", 11))
484       offset = 11;
485       if (!strncmp (string + i, "_GLOBAL__", 9))
486       offset = 9;
487
488       /* C++ namespaces do have scheme:
489          _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
490        since filename might contain extra underscores there seems
491        to be no better chance then walk all possible offsets looking
492        for magicnuber.  */
493       if (offset)
494         {
495           for (i = i + offset; string[i]; i++)
496             if (string[i]=='_')
497               {
498                 int y;
499
500                 for (y = 1; y < 9; y++)
501                   if (!(string[i + y] >= '0' && string[i + y] <= '9')
502                       && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
503                     break;
504                 if (y != 9 || string[i + 9] != '_')
505                   continue;
506                 for (y = 10; y < 18; y++)
507                   if (!(string[i + y] >= '0' && string[i + y] <= '9')
508                       && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
509                     break;
510                 if (y != 18)
511                   continue;
512                 if (!dup)
513                   string = dup = xstrdup (string);
514                 for (y = 10; y < 18; y++)
515                   dup[i + y] = '0';
516               }
517           break;
518         }
519     }
520
521   chksum = crc32_string (chksum, string);
522   if (dup)
523     free (dup);
524
525   return chksum;
526 }
527
528 /* Compute checksum for the current function.  We generate a CRC32.  */
529
530 static unsigned
531 compute_checksum (void)
532 {
533   expanded_location xloc
534     = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
535   unsigned chksum = xloc.line;
536
537   chksum = coverage_checksum_string (chksum, xloc.file);
538   chksum = coverage_checksum_string
539     (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
540
541   return chksum;
542 }
543 \f
544 /* Begin output to the graph file for the current function.
545    Opens the output file, if not already done. Writes the
546    function header, if not already done. Returns nonzero if data
547    should be output.  */
548
549 int
550 coverage_begin_output (void)
551 {
552   /* We don't need to output .gcno file unless we're under -ftest-coverage
553      (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
554   if (no_coverage || !flag_test_coverage)
555     return 0;
556
557   if (!bbg_function_announced)
558     {
559       expanded_location xloc
560         = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
561       unsigned long offset;
562
563       if (!bbg_file_opened)
564         {
565           if (!gcov_open (bbg_file_name, -1))
566             error ("cannot open %s", bbg_file_name);
567           else
568             {
569               gcov_write_unsigned (GCOV_NOTE_MAGIC);
570               gcov_write_unsigned (GCOV_VERSION);
571               gcov_write_unsigned (local_tick);
572             }
573           bbg_file_opened = 1;
574         }
575
576       /* Announce function */
577       offset = gcov_write_tag (GCOV_TAG_FUNCTION);
578       gcov_write_unsigned (current_function_funcdef_no + 1);
579       gcov_write_unsigned (compute_checksum ());
580       gcov_write_string (IDENTIFIER_POINTER
581                          (DECL_ASSEMBLER_NAME (current_function_decl)));
582       gcov_write_string (xloc.file);
583       gcov_write_unsigned (xloc.line);
584       gcov_write_length (offset);
585
586       bbg_function_announced = 1;
587     }
588   return !gcov_is_error ();
589 }
590
591 /* Finish coverage data for the current function. Verify no output
592    error has occurred.  Save function coverage counts.  */
593
594 void
595 coverage_end_function (void)
596 {
597   unsigned i;
598
599   if (bbg_file_opened > 1 && gcov_is_error ())
600     {
601       warning (0, "error writing %qs", bbg_file_name);
602       bbg_file_opened = -1;
603     }
604
605   if (fn_ctr_mask)
606     {
607       struct function_list *item;
608
609       item = XNEW (struct function_list);
610
611       *functions_tail = item;
612       functions_tail = &item->next;
613
614       item->next = 0;
615       item->ident = current_function_funcdef_no + 1;
616       item->checksum = compute_checksum ();
617       for (i = 0; i != GCOV_COUNTERS; i++)
618         {
619           item->n_ctrs[i] = fn_n_ctrs[i];
620           prg_n_ctrs[i] += fn_n_ctrs[i];
621           fn_n_ctrs[i] = fn_b_ctrs[i] = 0;
622         }
623       prg_ctr_mask |= fn_ctr_mask;
624       fn_ctr_mask = 0;
625     }
626   bbg_function_announced = 0;
627 }
628
629 /* Creates the gcov_fn_info RECORD_TYPE.  */
630
631 static tree
632 build_fn_info_type (unsigned int counters)
633 {
634   tree type = lang_hooks.types.make_type (RECORD_TYPE);
635   tree field, fields;
636   tree array_type;
637
638   /* ident */
639   fields = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
640
641   /* checksum */
642   field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
643   TREE_CHAIN (field) = fields;
644   fields = field;
645
646   array_type = build_int_cst (NULL_TREE, counters - 1);
647   array_type = build_index_type (array_type);
648   array_type = build_array_type (get_gcov_unsigned_t (), array_type);
649
650   /* counters */
651   field = build_decl (FIELD_DECL, NULL_TREE, array_type);
652   TREE_CHAIN (field) = fields;
653   fields = field;
654
655   finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
656
657   return type;
658 }
659
660 /* Creates a CONSTRUCTOR for a gcov_fn_info. FUNCTION is
661    the function being processed and TYPE is the gcov_fn_info
662    RECORD_TYPE.  */
663
664 static tree
665 build_fn_info_value (const struct function_list *function, tree type)
666 {
667   tree value = NULL_TREE;
668   tree fields = TYPE_FIELDS (type);
669   unsigned ix;
670   tree array_value = NULL_TREE;
671
672   /* ident */
673   value = tree_cons (fields, build_int_cstu (get_gcov_unsigned_t (),
674                                              function->ident), value);
675   fields = TREE_CHAIN (fields);
676
677   /* checksum */
678   value = tree_cons (fields, build_int_cstu (get_gcov_unsigned_t (),
679                                              function->checksum), value);
680   fields = TREE_CHAIN (fields);
681
682   /* counters */
683   for (ix = 0; ix != GCOV_COUNTERS; ix++)
684     if (prg_ctr_mask & (1 << ix))
685       {
686         tree counters = build_int_cstu (get_gcov_unsigned_t (),
687                                         function->n_ctrs[ix]);
688
689         array_value = tree_cons (NULL_TREE, counters, array_value);
690       }
691
692   /* FIXME: use build_constructor directly.  */
693   array_value = build_constructor_from_list (TREE_TYPE (fields),
694                                              nreverse (array_value));
695   value = tree_cons (fields, array_value, value);
696
697   /* FIXME: use build_constructor directly.  */
698   value = build_constructor_from_list (type, nreverse (value));
699
700   return value;
701 }
702
703 /* Creates the gcov_ctr_info RECORD_TYPE.  */
704
705 static tree
706 build_ctr_info_type (void)
707 {
708   tree type = lang_hooks.types.make_type (RECORD_TYPE);
709   tree field, fields = NULL_TREE;
710   tree gcov_ptr_type = build_pointer_type (get_gcov_type ());
711   tree gcov_merge_fn_type;
712
713   /* counters */
714   field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
715   TREE_CHAIN (field) = fields;
716   fields = field;
717
718   /* values */
719   field = build_decl (FIELD_DECL, NULL_TREE, gcov_ptr_type);
720   TREE_CHAIN (field) = fields;
721   fields = field;
722
723   /* merge */
724   gcov_merge_fn_type =
725     build_function_type_list (void_type_node,
726                               gcov_ptr_type, get_gcov_unsigned_t (),
727                               NULL_TREE);
728   field = build_decl (FIELD_DECL, NULL_TREE,
729                       build_pointer_type (gcov_merge_fn_type));
730   TREE_CHAIN (field) = fields;
731   fields = field;
732
733   finish_builtin_struct (type, "__gcov_ctr_info", fields, NULL_TREE);
734
735   return type;
736 }
737
738 /* Creates a CONSTRUCTOR for a gcov_ctr_info. COUNTER is
739    the counter being processed and TYPE is the gcov_ctr_info
740    RECORD_TYPE.  */
741
742 static tree
743 build_ctr_info_value (unsigned int counter, tree type)
744 {
745   tree value = NULL_TREE;
746   tree fields = TYPE_FIELDS (type);
747   tree fn;
748
749   /* counters */
750   value = tree_cons (fields,
751                      build_int_cstu (get_gcov_unsigned_t (),
752                                      prg_n_ctrs[counter]),
753                      value);
754   fields = TREE_CHAIN (fields);
755
756   if (prg_n_ctrs[counter])
757     {
758       tree array_type;
759
760       array_type = build_int_cstu (get_gcov_unsigned_t (),
761                                    prg_n_ctrs[counter] - 1);
762       array_type = build_index_type (array_type);
763       array_type = build_array_type (TREE_TYPE (TREE_TYPE (fields)),
764                                      array_type);
765
766       TREE_TYPE (tree_ctr_tables[counter]) = array_type;
767       DECL_SIZE (tree_ctr_tables[counter]) = TYPE_SIZE (array_type);
768       DECL_SIZE_UNIT (tree_ctr_tables[counter]) = TYPE_SIZE_UNIT (array_type);
769       assemble_variable (tree_ctr_tables[counter], 0, 0, 0);
770
771       value = tree_cons (fields,
772                          build1 (ADDR_EXPR, TREE_TYPE (fields), 
773                                             tree_ctr_tables[counter]),
774                          value);
775     }
776   else
777     value = tree_cons (fields, null_pointer_node, value);
778   fields = TREE_CHAIN (fields);
779
780   fn = build_decl (FUNCTION_DECL,
781                    get_identifier (ctr_merge_functions[counter]),
782                    TREE_TYPE (TREE_TYPE (fields)));
783   DECL_EXTERNAL (fn) = 1;
784   TREE_PUBLIC (fn) = 1;
785   DECL_ARTIFICIAL (fn) = 1;
786   TREE_NOTHROW (fn) = 1;
787   value = tree_cons (fields,
788                      build1 (ADDR_EXPR, TREE_TYPE (fields), fn),
789                      value);
790
791   /* FIXME: use build_constructor directly.  */
792   value = build_constructor_from_list (type, nreverse (value));
793
794   return value;
795 }
796
797 /* Creates the gcov_info RECORD_TYPE and initializer for it. Returns a
798    CONSTRUCTOR.  */
799
800 static tree
801 build_gcov_info (void)
802 {
803   unsigned n_ctr_types, ix;
804   tree type, const_type;
805   tree fn_info_type, fn_info_value = NULL_TREE;
806   tree fn_info_ptr_type;
807   tree ctr_info_type, ctr_info_ary_type, ctr_info_value = NULL_TREE;
808   tree field, fields = NULL_TREE;
809   tree value = NULL_TREE;
810   tree filename_string;
811   int da_file_name_len;
812   unsigned n_fns;
813   const struct function_list *fn;
814   tree string_type;
815
816   /* Count the number of active counters.  */
817   for (n_ctr_types = 0, ix = 0; ix != GCOV_COUNTERS; ix++)
818     if (prg_ctr_mask & (1 << ix))
819       n_ctr_types++;
820
821   type = lang_hooks.types.make_type (RECORD_TYPE);
822   const_type = build_qualified_type (type, TYPE_QUAL_CONST);
823
824   /* Version ident */
825   field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
826   TREE_CHAIN (field) = fields;
827   fields = field;
828   value = tree_cons (field, build_int_cstu (TREE_TYPE (field), GCOV_VERSION),
829                      value);
830
831   /* next -- NULL */
832   field = build_decl (FIELD_DECL, NULL_TREE, build_pointer_type (const_type));
833   TREE_CHAIN (field) = fields;
834   fields = field;
835   value = tree_cons (field, null_pointer_node, value);
836
837   /* stamp */
838   field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
839   TREE_CHAIN (field) = fields;
840   fields = field;
841   value = tree_cons (field, build_int_cstu (TREE_TYPE (field), local_tick),
842                      value);
843
844   /* Filename */
845   string_type = build_pointer_type (build_qualified_type (char_type_node,
846                                                     TYPE_QUAL_CONST));
847   field = build_decl (FIELD_DECL, NULL_TREE, string_type);
848   TREE_CHAIN (field) = fields;
849   fields = field;
850   da_file_name_len = strlen (da_file_name);
851   filename_string = build_string (da_file_name_len + 1, da_file_name);
852   TREE_TYPE (filename_string) = build_array_type
853     (char_type_node, build_index_type
854      (build_int_cst (NULL_TREE, da_file_name_len)));
855   value = tree_cons (field, build1 (ADDR_EXPR, string_type, filename_string),
856                      value);
857
858   /* Build the fn_info type and initializer.  */
859   fn_info_type = build_fn_info_type (n_ctr_types);
860   fn_info_ptr_type = build_pointer_type (build_qualified_type
861                                          (fn_info_type, TYPE_QUAL_CONST));
862   for (fn = functions_head, n_fns = 0; fn; fn = fn->next, n_fns++)
863     fn_info_value = tree_cons (NULL_TREE,
864                                build_fn_info_value (fn, fn_info_type),
865                                fn_info_value);
866   if (n_fns)
867     {
868       tree array_type;
869
870       array_type = build_index_type (build_int_cst (NULL_TREE, n_fns - 1));
871       array_type = build_array_type (fn_info_type, array_type);
872
873       /* FIXME: use build_constructor directly.  */
874       fn_info_value = build_constructor_from_list (array_type,
875                                                    nreverse (fn_info_value));
876       fn_info_value = build1 (ADDR_EXPR, fn_info_ptr_type, fn_info_value);
877     }
878   else
879     fn_info_value = null_pointer_node;
880
881   /* number of functions */
882   field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
883   TREE_CHAIN (field) = fields;
884   fields = field;
885   value = tree_cons (field,
886                      build_int_cstu (get_gcov_unsigned_t (), n_fns),
887                      value);
888
889   /* fn_info table */
890   field = build_decl (FIELD_DECL, NULL_TREE, fn_info_ptr_type);
891   TREE_CHAIN (field) = fields;
892   fields = field;
893   value = tree_cons (field, fn_info_value, value);
894
895   /* counter_mask */
896   field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
897   TREE_CHAIN (field) = fields;
898   fields = field;
899   value = tree_cons (field,
900                      build_int_cstu (get_gcov_unsigned_t (), prg_ctr_mask),
901                      value);
902
903   /* counters */
904   ctr_info_type = build_ctr_info_type ();
905   ctr_info_ary_type = build_index_type (build_int_cst (NULL_TREE,
906                                                        n_ctr_types));
907   ctr_info_ary_type = build_array_type (ctr_info_type, ctr_info_ary_type);
908   for (ix = 0; ix != GCOV_COUNTERS; ix++)
909     if (prg_ctr_mask & (1 << ix))
910       ctr_info_value = tree_cons (NULL_TREE,
911                                   build_ctr_info_value (ix, ctr_info_type),
912                                   ctr_info_value);
913   /* FIXME: use build_constructor directly.  */
914   ctr_info_value = build_constructor_from_list (ctr_info_ary_type,
915                                                 nreverse (ctr_info_value));
916
917   field = build_decl (FIELD_DECL, NULL_TREE, ctr_info_ary_type);
918   TREE_CHAIN (field) = fields;
919   fields = field;
920   value = tree_cons (field, ctr_info_value, value);
921
922   finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
923
924   /* FIXME: use build_constructor directly.  */
925   value = build_constructor_from_list (type, nreverse (value));
926
927   return value;
928 }
929
930 /* Write out the structure which libgcov uses to locate all the
931    counters.  The structures used here must match those defined in
932    gcov-io.h.  Write out the constructor to call __gcov_init.  */
933
934 static void
935 create_coverage (void)
936 {
937   tree gcov_info, gcov_init, body, t;
938   char name_buf[32];
939
940   no_coverage = 1; /* Disable any further coverage.  */
941
942   if (!prg_ctr_mask)
943     return;
944
945   t = build_gcov_info ();
946
947   gcov_info = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (t));
948   TREE_STATIC (gcov_info) = 1;
949   ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
950   DECL_NAME (gcov_info) = get_identifier (name_buf);
951   DECL_INITIAL (gcov_info) = t;
952
953   /* Build structure.  */
954   assemble_variable (gcov_info, 0, 0, 0);
955
956   /* Build a decl for __gcov_init.  */
957   t = build_pointer_type (TREE_TYPE (gcov_info));
958   t = build_function_type_list (void_type_node, t, NULL);
959   t = build_decl (FUNCTION_DECL, get_identifier ("__gcov_init"), t);
960   TREE_PUBLIC (t) = 1;
961   DECL_EXTERNAL (t) = 1;
962   gcov_init = t;
963
964   /* Generate a call to __gcov_init(&gcov_info).  */
965   body = NULL;
966   t = build_fold_addr_expr (gcov_info);
967   t = build_call_expr (gcov_init, 1, t);
968   append_to_statement_list (t, &body);
969
970   /* Generate a constructor to run it.  */
971   cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
972 }
973 \f
974 /* Perform file-level initialization. Read in data file, generate name
975    of graph file.  */
976
977 void
978 coverage_init (const char *filename)
979 {
980   int len = strlen (filename);
981   /* + 1 for extra '/', in case prefix doesn't end with /.  */
982   int prefix_len;
983  
984   if (profile_data_prefix == 0 && filename[0] != '/')
985     profile_data_prefix = getpwd ();
986
987   prefix_len = (profile_data_prefix) ? strlen (profile_data_prefix) + 1 : 0;
988
989   /* Name of da file.  */
990   da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX) 
991                           + prefix_len + 1);
992
993   if (profile_data_prefix)
994     {
995       strcpy (da_file_name, profile_data_prefix);
996       da_file_name[prefix_len - 1] = '/';
997       da_file_name[prefix_len] = 0;
998     }
999   else
1000     da_file_name[0] = 0;
1001   strcat (da_file_name, filename);
1002   strcat (da_file_name, GCOV_DATA_SUFFIX);
1003
1004   /* Name of bbg file.  */
1005   bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
1006   strcpy (bbg_file_name, filename);
1007   strcat (bbg_file_name, GCOV_NOTE_SUFFIX);
1008
1009   if (flag_profile_use)
1010     read_counts_file ();
1011 }
1012
1013 /* Performs file-level cleanup.  Close graph file, generate coverage
1014    variables and constructor.  */
1015
1016 void
1017 coverage_finish (void)
1018 {
1019   create_coverage ();
1020   if (bbg_file_opened)
1021     {
1022       int error = gcov_close ();
1023
1024       if (error)
1025         unlink (bbg_file_name);
1026       if (!local_tick)
1027         /* Only remove the da file, if we cannot stamp it. If we can
1028            stamp it, libgcov will DTRT.  */
1029         unlink (da_file_name);
1030     }
1031 }
1032
1033 #include "gt-coverage.h"