PR rtl-optimization/20370
[official-gcc.git] / gcc / coverage.c
blob5c1d0922aa5fbb81ce6e424964a5462532bc6871
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 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
9 This file is part of GCC.
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 2, or (at your option) any later
14 version.
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.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING. If not, write to the Free
23 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 02110-1301, USA. */
27 #define GCOV_LINKAGE
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "regs.h"
38 #include "expr.h"
39 #include "function.h"
40 #include "toplev.h"
41 #include "ggc.h"
42 #include "coverage.h"
43 #include "langhooks.h"
44 #include "hashtab.h"
45 #include "tree-iterator.h"
46 #include "cgraph.h"
48 #include "gcov-io.c"
50 struct function_list
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. */
58 /* Counts information for a function. */
59 typedef struct counts_entry
61 /* We hash by */
62 unsigned ident;
63 unsigned ctr;
65 /* Store */
66 unsigned checksum;
67 gcov_type *counts;
68 struct gcov_ctr_summary summary;
70 /* Workspace */
71 struct counts_entry *chain;
73 } counts_entry_t;
75 static struct function_list *functions_head = 0;
76 static struct function_list **functions_tail = &functions_head;
77 static unsigned no_coverage = 0;
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. */
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. */
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;
93 /* Name of the count data file. */
94 static char *da_file_name;
96 /* Hash table of count data. */
97 static htab_t counts_hash = NULL;
99 /* Trees representing the counter table arrays. */
100 static GTY(()) tree tree_ctr_tables[GCOV_COUNTERS];
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];
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;
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);
124 /* Return the type node for gcov_type. */
126 tree
127 get_gcov_type (void)
129 return lang_hooks.types.type_for_size (GCOV_TYPE_SIZE, false);
132 /* Return the type node for gcov_unsigned_t. */
134 static tree
135 get_gcov_unsigned_t (void)
137 return lang_hooks.types.type_for_size (32, true);
140 static hashval_t
141 htab_counts_entry_hash (const void *of)
143 const counts_entry_t *entry = of;
145 return entry->ident * GCOV_COUNTERS + entry->ctr;
148 static int
149 htab_counts_entry_eq (const void *of1, const void *of2)
151 const counts_entry_t *entry1 = of1;
152 const counts_entry_t *entry2 = of2;
154 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
157 static void
158 htab_counts_entry_del (void *of)
160 counts_entry_t *entry = of;
162 free (entry->counts);
163 free (entry);
166 /* Read in the counts file, if available. */
168 static void
169 read_counts_file (void)
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;
178 if (!gcov_open (da_file_name, 1))
179 return;
181 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
183 warning (0, "%qs is not a gcov data file", da_file_name);
184 gcov_close ();
185 return;
187 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
189 char v[4], e[4];
191 GCOV_UNSIGNED2STRING (v, tag);
192 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
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;
200 /* Read and discard the stamp. */
201 gcov_read_unsigned ();
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 ()))
208 gcov_unsigned_t length;
209 gcov_position_t offset;
211 length = gcov_read_unsigned ();
212 offset = gcov_position ();
213 if (tag == GCOV_TAG_FUNCTION)
215 fn_ident = gcov_read_unsigned ();
216 checksum = gcov_read_unsigned ();
217 if (seen_summary)
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;
224 for (entry = summaried; entry; entry = chain)
226 chain = entry->chain;
227 entry->chain = NULL;
229 summaried = NULL;
230 seen_summary = 0;
233 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
235 counts_entry_t *entry;
236 struct gcov_summary summary;
238 gcov_read_summary (&summary);
239 seen_summary = 1;
240 for (entry = summaried; entry; entry = entry->chain)
242 struct gcov_ctr_summary *csum = &summary.ctrs[entry->ctr];
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;
251 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
253 counts_entry_t **slot, *entry, elt;
254 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
255 unsigned ix;
257 elt.ident = fn_ident;
258 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
260 slot = (counts_entry_t **) htab_find_slot
261 (counts_hash, &elt, INSERT);
262 entry = *slot;
263 if (!entry)
265 *slot = entry = xcalloc (1, sizeof (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 = xcalloc (n_counts, sizeof (gcov_type));
272 else if (entry->checksum != checksum)
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;
280 else if (entry->summary.num != n_counts)
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;
288 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
290 error ("cannot merge separate %s counters for function %u",
291 ctr_names[elt.ctr], fn_ident);
292 goto skip_merge;
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))
301 entry->chain = summaried;
302 summaried = entry;
304 for (ix = 0; ix != n_counts; ix++)
305 entry->counts[ix] += gcov_read_counter ();
306 skip_merge:;
308 gcov_sync (offset, length);
309 if ((is_error = gcov_is_error ()))
311 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
312 da_file_name);
313 htab_delete (counts_hash);
314 break;
318 gcov_close ();
321 /* Returns the counters for a particular tag. */
323 gcov_type *
324 get_coverage_counts (unsigned counter, unsigned expected,
325 const struct gcov_ctr_summary **summary)
327 counts_entry_t *entry, elt;
328 gcov_unsigned_t checksum = -1;
330 /* No hash table, no counts. */
331 if (!counts_hash)
333 static int warned = 0;
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;
343 elt.ident = current_function_funcdef_no + 1;
344 elt.ctr = counter;
345 entry = htab_find (counts_hash, &elt);
346 if (!entry)
348 warning (0, "no coverage for function %qs found", IDENTIFIER_POINTER
349 (DECL_ASSEMBLER_NAME (current_function_decl)));
350 return 0;
353 checksum = compute_checksum ();
354 if (entry->checksum != checksum)
356 error ("coverage mismatch for function %qs while reading counter %qs",
357 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)),
358 ctr_names[counter]);
359 error ("checksum is %x instead of %x", entry->checksum, checksum);
360 return 0;
362 else if (entry->summary.num != expected)
364 error ("coverage mismatch for function %qs while reading counter %qs",
365 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)),
366 ctr_names[counter]);
367 error ("number of counters is %d instead of %d", entry->summary.num, expected);
368 return 0;
371 if (summary)
372 *summary = &entry->summary;
374 return entry->counts;
377 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
378 allocation succeeded. */
381 coverage_counter_alloc (unsigned counter, unsigned num)
383 if (no_coverage)
384 return 0;
386 if (!num)
387 return 1;
389 if (!tree_ctr_tables[counter])
391 /* Generate and save a copy of this so it can be shared. */
392 /* We don't know the size yet; make it big enough that nobody
393 will make any clever transformation on it. */
394 char buf[20];
395 tree gcov_type_node = get_gcov_type ();
396 tree domain_tree
397 = build_index_type (build_int_cst (NULL_TREE, 1000)); /* replaced later */
398 tree gcov_type_array_type
399 = build_array_type (gcov_type_node, domain_tree);
400 tree_ctr_tables[counter]
401 = build_decl (VAR_DECL, NULL_TREE, gcov_type_array_type);
402 TREE_STATIC (tree_ctr_tables[counter]) = 1;
403 ASM_GENERATE_INTERNAL_LABEL (buf, "LPBX", counter + 1);
404 DECL_NAME (tree_ctr_tables[counter]) = get_identifier (buf);
405 DECL_ALIGN (tree_ctr_tables[counter]) = TYPE_ALIGN (gcov_type_node);
407 fn_b_ctrs[counter] = fn_n_ctrs[counter];
408 fn_n_ctrs[counter] += num;
409 fn_ctr_mask |= 1 << counter;
410 return 1;
413 /* Generate a MEM rtl to access COUNTER NO. */
416 rtl_coverage_counter_ref (unsigned counter, unsigned no)
418 enum machine_mode mode = mode_for_size (GCOV_TYPE_SIZE, MODE_INT, 0);
419 rtx ref;
421 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
422 no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
423 if (!ctr_labels[counter])
425 ctr_labels[counter] = gen_rtx_SYMBOL_REF (Pmode,
426 ggc_strdup (IDENTIFIER_POINTER (DECL_NAME
427 (tree_ctr_tables[counter]))));
428 SYMBOL_REF_FLAGS (ctr_labels[counter]) = SYMBOL_FLAG_LOCAL;
430 ref = plus_constant (ctr_labels[counter],
431 GCOV_TYPE_SIZE / BITS_PER_UNIT * no);
432 ref = gen_rtx_MEM (mode, ref);
433 set_mem_alias_set (ref, new_alias_set ());
434 MEM_NOTRAP_P (ref) = 1;
436 return ref;
439 /* Generate a tree to access COUNTER NO. */
441 tree
442 tree_coverage_counter_ref (unsigned counter, unsigned no)
444 tree gcov_type_node = get_gcov_type ();
445 tree domain_type = TYPE_DOMAIN (TREE_TYPE (tree_ctr_tables[counter]));
447 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
448 no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
450 /* "no" here is an array index, scaled to bytes later. */
451 return build4 (ARRAY_REF, gcov_type_node, tree_ctr_tables[counter],
452 fold_convert (domain_type,
453 build_int_cst (NULL_TREE, no)),
454 TYPE_MIN_VALUE (domain_type),
455 size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (gcov_type_node),
456 size_int (TYPE_ALIGN_UNIT (gcov_type_node))));
459 /* Generate a checksum for a string. CHKSUM is the current
460 checksum. */
462 static unsigned
463 coverage_checksum_string (unsigned chksum, const char *string)
465 int i;
466 char *dup = NULL;
468 /* Look for everything that looks if it were produced by
469 get_file_function_name_long and zero out the second part
470 that may result from flag_random_seed. This is not critical
471 as the checksums are used only for sanity checking. */
472 for (i = 0; string[i]; i++)
474 if (!strncmp (string + i, "_GLOBAL__", 9))
475 for (i = i + 9; string[i]; i++)
476 if (string[i]=='_')
478 int y;
479 unsigned seed;
480 int scan;
482 for (y = 1; y < 9; y++)
483 if (!(string[i + y] >= '0' && string[i + y] <= '9')
484 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
485 break;
486 if (y != 9 || string[i + 9] != '_')
487 continue;
488 for (y = 10; y < 18; y++)
489 if (!(string[i + y] >= '0' && string[i + y] <= '9')
490 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
491 break;
492 if (y != 18)
493 continue;
494 scan = sscanf (string + i + 10, "%X", &seed);
495 gcc_assert (scan);
496 if (seed != crc32_string (0, flag_random_seed))
497 continue;
498 string = dup = xstrdup (string);
499 for (y = 10; y < 18; y++)
500 dup[i + y] = '0';
501 break;
503 break;
506 chksum = crc32_string (chksum, string);
507 if (dup)
508 free (dup);
510 return chksum;
513 /* Compute checksum for the current function. We generate a CRC32. */
515 static unsigned
516 compute_checksum (void)
518 expanded_location xloc
519 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
520 unsigned chksum = xloc.line;
522 chksum = coverage_checksum_string (chksum, xloc.file);
523 chksum = coverage_checksum_string
524 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
526 return chksum;
529 /* Begin output to the graph file for the current function.
530 Opens the output file, if not already done. Writes the
531 function header, if not already done. Returns nonzero if data
532 should be output. */
535 coverage_begin_output (void)
537 if (no_coverage)
538 return 0;
540 if (!bbg_function_announced)
542 expanded_location xloc
543 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
544 unsigned long offset;
546 if (!bbg_file_opened)
548 if (!gcov_open (bbg_file_name, -1))
549 error ("cannot open %s", bbg_file_name);
550 else
552 gcov_write_unsigned (GCOV_NOTE_MAGIC);
553 gcov_write_unsigned (GCOV_VERSION);
554 gcov_write_unsigned (local_tick);
556 bbg_file_opened = 1;
559 /* Announce function */
560 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
561 gcov_write_unsigned (current_function_funcdef_no + 1);
562 gcov_write_unsigned (compute_checksum ());
563 gcov_write_string (IDENTIFIER_POINTER
564 (DECL_ASSEMBLER_NAME (current_function_decl)));
565 gcov_write_string (xloc.file);
566 gcov_write_unsigned (xloc.line);
567 gcov_write_length (offset);
569 bbg_function_announced = 1;
571 return !gcov_is_error ();
574 /* Finish coverage data for the current function. Verify no output
575 error has occurred. Save function coverage counts. */
577 void
578 coverage_end_function (void)
580 unsigned i;
582 if (bbg_file_opened > 1 && gcov_is_error ())
584 warning (0, "error writing %qs", bbg_file_name);
585 bbg_file_opened = -1;
588 if (fn_ctr_mask)
590 struct function_list *item;
592 item = xmalloc (sizeof (struct function_list));
594 *functions_tail = item;
595 functions_tail = &item->next;
597 item->next = 0;
598 item->ident = current_function_funcdef_no + 1;
599 item->checksum = compute_checksum ();
600 for (i = 0; i != GCOV_COUNTERS; i++)
602 item->n_ctrs[i] = fn_n_ctrs[i];
603 prg_n_ctrs[i] += fn_n_ctrs[i];
604 fn_n_ctrs[i] = fn_b_ctrs[i] = 0;
606 prg_ctr_mask |= fn_ctr_mask;
607 fn_ctr_mask = 0;
609 bbg_function_announced = 0;
612 /* Creates the gcov_fn_info RECORD_TYPE. */
614 static tree
615 build_fn_info_type (unsigned int counters)
617 tree type = lang_hooks.types.make_type (RECORD_TYPE);
618 tree field, fields;
619 tree array_type;
621 /* ident */
622 fields = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
624 /* checksum */
625 field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
626 TREE_CHAIN (field) = fields;
627 fields = field;
629 array_type = build_int_cst (NULL_TREE, counters - 1);
630 array_type = build_index_type (array_type);
631 array_type = build_array_type (unsigned_type_node, array_type);
633 /* counters */
634 field = build_decl (FIELD_DECL, NULL_TREE, array_type);
635 TREE_CHAIN (field) = fields;
636 fields = field;
638 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
640 return type;
643 /* Creates a CONSTRUCTOR for a gcov_fn_info. FUNCTION is
644 the function being processed and TYPE is the gcov_fn_info
645 RECORD_TYPE. */
647 static tree
648 build_fn_info_value (const struct function_list *function, tree type)
650 tree value = NULL_TREE;
651 tree fields = TYPE_FIELDS (type);
652 unsigned ix;
653 tree array_value = NULL_TREE;
655 /* ident */
656 value = tree_cons (fields, build_int_cstu (get_gcov_unsigned_t (),
657 function->ident), value);
658 fields = TREE_CHAIN (fields);
660 /* checksum */
661 value = tree_cons (fields, build_int_cstu (get_gcov_unsigned_t (),
662 function->checksum), value);
663 fields = TREE_CHAIN (fields);
665 /* counters */
666 for (ix = 0; ix != GCOV_COUNTERS; ix++)
667 if (prg_ctr_mask & (1 << ix))
669 tree counters = build_int_cstu (unsigned_type_node,
670 function->n_ctrs[ix]);
672 array_value = tree_cons (NULL_TREE, counters, array_value);
675 /* FIXME: use build_constructor directly. */
676 array_value = build_constructor_from_list (TREE_TYPE (fields),
677 nreverse (array_value));
678 value = tree_cons (fields, array_value, value);
680 /* FIXME: use build_constructor directly. */
681 value = build_constructor_from_list (type, nreverse (value));
683 return value;
686 /* Creates the gcov_ctr_info RECORD_TYPE. */
688 static tree
689 build_ctr_info_type (void)
691 tree type = lang_hooks.types.make_type (RECORD_TYPE);
692 tree field, fields = NULL_TREE;
693 tree gcov_ptr_type = build_pointer_type (get_gcov_type ());
694 tree gcov_merge_fn_type;
696 /* counters */
697 field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
698 TREE_CHAIN (field) = fields;
699 fields = field;
701 /* values */
702 field = build_decl (FIELD_DECL, NULL_TREE, gcov_ptr_type);
703 TREE_CHAIN (field) = fields;
704 fields = field;
706 /* merge */
707 gcov_merge_fn_type =
708 build_function_type_list (void_type_node,
709 gcov_ptr_type, unsigned_type_node,
710 NULL_TREE);
711 field = build_decl (FIELD_DECL, NULL_TREE,
712 build_pointer_type (gcov_merge_fn_type));
713 TREE_CHAIN (field) = fields;
714 fields = field;
716 finish_builtin_struct (type, "__gcov_ctr_info", fields, NULL_TREE);
718 return type;
721 /* Creates a CONSTRUCTOR for a gcov_ctr_info. COUNTER is
722 the counter being processed and TYPE is the gcov_ctr_info
723 RECORD_TYPE. */
725 static tree
726 build_ctr_info_value (unsigned int counter, tree type)
728 tree value = NULL_TREE;
729 tree fields = TYPE_FIELDS (type);
730 tree fn;
732 /* counters */
733 value = tree_cons (fields,
734 build_int_cstu (get_gcov_unsigned_t (),
735 prg_n_ctrs[counter]),
736 value);
737 fields = TREE_CHAIN (fields);
739 if (prg_n_ctrs[counter])
741 tree array_type;
743 array_type = build_int_cstu (unsigned_type_node,
744 prg_n_ctrs[counter] - 1);
745 array_type = build_index_type (array_type);
746 array_type = build_array_type (TREE_TYPE (TREE_TYPE (fields)),
747 array_type);
749 TREE_TYPE (tree_ctr_tables[counter]) = array_type;
750 DECL_SIZE (tree_ctr_tables[counter]) = TYPE_SIZE (array_type);
751 DECL_SIZE_UNIT (tree_ctr_tables[counter]) = TYPE_SIZE_UNIT (array_type);
752 assemble_variable (tree_ctr_tables[counter], 0, 0, 0);
754 value = tree_cons (fields,
755 build1 (ADDR_EXPR, TREE_TYPE (fields),
756 tree_ctr_tables[counter]),
757 value);
759 else
760 value = tree_cons (fields, null_pointer_node, value);
761 fields = TREE_CHAIN (fields);
763 fn = build_decl (FUNCTION_DECL,
764 get_identifier (ctr_merge_functions[counter]),
765 TREE_TYPE (TREE_TYPE (fields)));
766 DECL_EXTERNAL (fn) = 1;
767 TREE_PUBLIC (fn) = 1;
768 DECL_ARTIFICIAL (fn) = 1;
769 TREE_NOTHROW (fn) = 1;
770 value = tree_cons (fields,
771 build1 (ADDR_EXPR, TREE_TYPE (fields), fn),
772 value);
774 /* FIXME: use build_constructor directly. */
775 value = build_constructor_from_list (type, nreverse (value));
777 return value;
780 /* Creates the gcov_info RECORD_TYPE and initializer for it. Returns a
781 CONSTRUCTOR. */
783 static tree
784 build_gcov_info (void)
786 unsigned n_ctr_types, ix;
787 tree type, const_type;
788 tree fn_info_type, fn_info_value = NULL_TREE;
789 tree fn_info_ptr_type;
790 tree ctr_info_type, ctr_info_ary_type, ctr_info_value = NULL_TREE;
791 tree field, fields = NULL_TREE;
792 tree value = NULL_TREE;
793 tree filename_string;
794 char *filename;
795 int filename_len;
796 unsigned n_fns;
797 const struct function_list *fn;
798 tree string_type;
800 /* Count the number of active counters. */
801 for (n_ctr_types = 0, ix = 0; ix != GCOV_COUNTERS; ix++)
802 if (prg_ctr_mask & (1 << ix))
803 n_ctr_types++;
805 type = lang_hooks.types.make_type (RECORD_TYPE);
806 const_type = build_qualified_type (type, TYPE_QUAL_CONST);
808 /* Version ident */
809 field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
810 TREE_CHAIN (field) = fields;
811 fields = field;
812 value = tree_cons (field, build_int_cstu (TREE_TYPE (field), GCOV_VERSION),
813 value);
815 /* next -- NULL */
816 field = build_decl (FIELD_DECL, NULL_TREE, build_pointer_type (const_type));
817 TREE_CHAIN (field) = fields;
818 fields = field;
819 value = tree_cons (field, null_pointer_node, value);
821 /* stamp */
822 field = build_decl (FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
823 TREE_CHAIN (field) = fields;
824 fields = field;
825 value = tree_cons (field, build_int_cstu (TREE_TYPE (field), local_tick),
826 value);
828 /* Filename */
829 string_type = build_pointer_type (build_qualified_type (char_type_node,
830 TYPE_QUAL_CONST));
831 field = build_decl (FIELD_DECL, NULL_TREE, string_type);
832 TREE_CHAIN (field) = fields;
833 fields = field;
834 filename = getpwd ();
835 filename = (filename && da_file_name[0] != '/'
836 ? concat (filename, "/", da_file_name, NULL)
837 : da_file_name);
838 filename_len = strlen (filename);
839 filename_string = build_string (filename_len + 1, filename);
840 if (filename != da_file_name)
841 free (filename);
842 TREE_TYPE (filename_string) = build_array_type
843 (char_type_node, build_index_type
844 (build_int_cst (NULL_TREE, filename_len)));
845 value = tree_cons (field, build1 (ADDR_EXPR, string_type, filename_string),
846 value);
848 /* Build the fn_info type and initializer. */
849 fn_info_type = build_fn_info_type (n_ctr_types);
850 fn_info_ptr_type = build_pointer_type (build_qualified_type
851 (fn_info_type, TYPE_QUAL_CONST));
852 for (fn = functions_head, n_fns = 0; fn; fn = fn->next, n_fns++)
853 fn_info_value = tree_cons (NULL_TREE,
854 build_fn_info_value (fn, fn_info_type),
855 fn_info_value);
856 if (n_fns)
858 tree array_type;
860 array_type = build_index_type (build_int_cst (NULL_TREE, n_fns - 1));
861 array_type = build_array_type (fn_info_type, array_type);
863 /* FIXME: use build_constructor directly. */
864 fn_info_value = build_constructor_from_list (array_type,
865 nreverse (fn_info_value));
866 fn_info_value = build1 (ADDR_EXPR, fn_info_ptr_type, fn_info_value);
868 else
869 fn_info_value = null_pointer_node;
871 /* number of functions */
872 field = build_decl (FIELD_DECL, NULL_TREE, unsigned_type_node);
873 TREE_CHAIN (field) = fields;
874 fields = field;
875 value = tree_cons (field,
876 build_int_cstu (unsigned_type_node, n_fns),
877 value);
879 /* fn_info table */
880 field = build_decl (FIELD_DECL, NULL_TREE, fn_info_ptr_type);
881 TREE_CHAIN (field) = fields;
882 fields = field;
883 value = tree_cons (field, fn_info_value, value);
885 /* counter_mask */
886 field = build_decl (FIELD_DECL, NULL_TREE, unsigned_type_node);
887 TREE_CHAIN (field) = fields;
888 fields = field;
889 value = tree_cons (field,
890 build_int_cstu (unsigned_type_node, prg_ctr_mask),
891 value);
893 /* counters */
894 ctr_info_type = build_ctr_info_type ();
895 ctr_info_ary_type = build_index_type (build_int_cst (NULL_TREE,
896 n_ctr_types));
897 ctr_info_ary_type = build_array_type (ctr_info_type, ctr_info_ary_type);
898 for (ix = 0; ix != GCOV_COUNTERS; ix++)
899 if (prg_ctr_mask & (1 << ix))
900 ctr_info_value = tree_cons (NULL_TREE,
901 build_ctr_info_value (ix, ctr_info_type),
902 ctr_info_value);
903 /* FIXME: use build_constructor directly. */
904 ctr_info_value = build_constructor_from_list (ctr_info_ary_type,
905 nreverse (ctr_info_value));
907 field = build_decl (FIELD_DECL, NULL_TREE, ctr_info_ary_type);
908 TREE_CHAIN (field) = fields;
909 fields = field;
910 value = tree_cons (field, ctr_info_value, value);
912 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
914 /* FIXME: use build_constructor directly. */
915 value = build_constructor_from_list (type, nreverse (value));
917 return value;
920 /* Write out the structure which libgcov uses to locate all the
921 counters. The structures used here must match those defined in
922 gcov-io.h. Write out the constructor to call __gcov_init. */
924 static void
925 create_coverage (void)
927 tree gcov_info, gcov_init, body, t;
928 char name_buf[32];
930 no_coverage = 1; /* Disable any further coverage. */
932 if (!prg_ctr_mask)
933 return;
935 t = build_gcov_info ();
937 gcov_info = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (t));
938 TREE_STATIC (gcov_info) = 1;
939 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
940 DECL_NAME (gcov_info) = get_identifier (name_buf);
941 DECL_INITIAL (gcov_info) = t;
943 /* Build structure. */
944 assemble_variable (gcov_info, 0, 0, 0);
946 /* Build a decl for __gcov_init. */
947 t = build_pointer_type (TREE_TYPE (gcov_info));
948 t = build_function_type_list (void_type_node, t, NULL);
949 t = build_decl (FUNCTION_DECL, get_identifier ("__gcov_init"), t);
950 TREE_PUBLIC (t) = 1;
951 DECL_EXTERNAL (t) = 1;
952 gcov_init = t;
954 /* Generate a call to __gcov_init(&gcov_info). */
955 body = NULL;
956 t = build_fold_addr_expr (gcov_info);
957 t = tree_cons (NULL, t, NULL);
958 t = build_function_call_expr (gcov_init, t);
959 append_to_statement_list (t, &body);
961 /* Generate a constructor to run it. */
962 cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
965 /* Perform file-level initialization. Read in data file, generate name
966 of graph file. */
968 void
969 coverage_init (const char *filename)
971 int len = strlen (filename);
973 /* Name of da file. */
974 da_file_name = xmalloc (len + strlen (GCOV_DATA_SUFFIX) + 1);
975 strcpy (da_file_name, filename);
976 strcat (da_file_name, GCOV_DATA_SUFFIX);
978 /* Name of bbg file. */
979 bbg_file_name = xmalloc (len + strlen (GCOV_NOTE_SUFFIX) + 1);
980 strcpy (bbg_file_name, filename);
981 strcat (bbg_file_name, GCOV_NOTE_SUFFIX);
983 read_counts_file ();
986 /* Performs file-level cleanup. Close graph file, generate coverage
987 variables and constructor. */
989 void
990 coverage_finish (void)
992 create_coverage ();
993 if (bbg_file_opened)
995 int error = gcov_close ();
997 if (error)
998 unlink (bbg_file_name);
999 if (!local_tick)
1000 /* Only remove the da file, if we cannot stamp it. If we can
1001 stamp it, libgcov will DTRT. */
1002 unlink (da_file_name);
1006 #include "gt-coverage.h"