1*e4b17023SJohn Marino /* Data and functions related to line maps and input files.
2*e4b17023SJohn Marino Copyright (C) 2004, 2007, 2008, 2009, 2010, 2011
3*e4b17023SJohn Marino Free Software Foundation, Inc.
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino #include "config.h"
22*e4b17023SJohn Marino #include "system.h"
23*e4b17023SJohn Marino #include "coretypes.h"
24*e4b17023SJohn Marino #include "intl.h"
25*e4b17023SJohn Marino #include "input.h"
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino /* Current position in real source file. */
28*e4b17023SJohn Marino
29*e4b17023SJohn Marino location_t input_location;
30*e4b17023SJohn Marino
31*e4b17023SJohn Marino struct line_maps *line_table;
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino /* Expand the source location LOC into a human readable location. If
34*e4b17023SJohn Marino LOC resolves to a builtin location, the file name of the readable
35*e4b17023SJohn Marino location is set to the string "<built-in>". */
36*e4b17023SJohn Marino
37*e4b17023SJohn Marino expanded_location
expand_location(source_location loc)38*e4b17023SJohn Marino expand_location (source_location loc)
39*e4b17023SJohn Marino {
40*e4b17023SJohn Marino expanded_location xloc;
41*e4b17023SJohn Marino const struct line_map *map;
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino loc = linemap_resolve_location (line_table, loc,
44*e4b17023SJohn Marino LRK_SPELLING_LOCATION, &map);
45*e4b17023SJohn Marino xloc = linemap_expand_location (line_table, map, loc);
46*e4b17023SJohn Marino
47*e4b17023SJohn Marino if (loc <= BUILTINS_LOCATION)
48*e4b17023SJohn Marino xloc.file = loc == UNKNOWN_LOCATION ? NULL : _("<built-in>");
49*e4b17023SJohn Marino
50*e4b17023SJohn Marino return xloc;
51*e4b17023SJohn Marino }
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino #define ONE_K 1024
54*e4b17023SJohn Marino #define ONE_M (ONE_K * ONE_K)
55*e4b17023SJohn Marino
56*e4b17023SJohn Marino /* Display a number as an integer multiple of either:
57*e4b17023SJohn Marino - 1024, if said integer is >= to 10 K (in base 2)
58*e4b17023SJohn Marino - 1024 * 1024, if said integer is >= 10 M in (base 2)
59*e4b17023SJohn Marino */
60*e4b17023SJohn Marino #define SCALE(x) ((unsigned long) ((x) < 10 * ONE_K \
61*e4b17023SJohn Marino ? (x) \
62*e4b17023SJohn Marino : ((x) < 10 * ONE_M \
63*e4b17023SJohn Marino ? (x) / ONE_K \
64*e4b17023SJohn Marino : (x) / ONE_M)))
65*e4b17023SJohn Marino
66*e4b17023SJohn Marino /* For a given integer, display either:
67*e4b17023SJohn Marino - the character 'k', if the number is higher than 10 K (in base 2)
68*e4b17023SJohn Marino but strictly lower than 10 M (in base 2)
69*e4b17023SJohn Marino - the character 'M' if the number is higher than 10 M (in base2)
70*e4b17023SJohn Marino - the charcter ' ' if the number is strictly lower than 10 K */
71*e4b17023SJohn Marino #define STAT_LABEL(x) ((x) < 10 * ONE_K ? ' ' : ((x) < 10 * ONE_M ? 'k' : 'M'))
72*e4b17023SJohn Marino
73*e4b17023SJohn Marino /* Display an integer amount as multiple of 1K or 1M (in base 2).
74*e4b17023SJohn Marino Display the correct unit (either k, M, or ' ') after the amout, as
75*e4b17023SJohn Marino well. */
76*e4b17023SJohn Marino #define FORMAT_AMOUNT(size) SCALE (size), STAT_LABEL (size)
77*e4b17023SJohn Marino
78*e4b17023SJohn Marino /* Dump statistics to stderr about the memory usage of the line_table
79*e4b17023SJohn Marino set of line maps. This also displays some statistics about macro
80*e4b17023SJohn Marino expansion. */
81*e4b17023SJohn Marino
82*e4b17023SJohn Marino void
dump_line_table_statistics(void)83*e4b17023SJohn Marino dump_line_table_statistics (void)
84*e4b17023SJohn Marino {
85*e4b17023SJohn Marino struct linemap_stats s;
86*e4b17023SJohn Marino long total_used_map_size,
87*e4b17023SJohn Marino macro_maps_size,
88*e4b17023SJohn Marino total_allocated_map_size;
89*e4b17023SJohn Marino
90*e4b17023SJohn Marino memset (&s, 0, sizeof (s));
91*e4b17023SJohn Marino
92*e4b17023SJohn Marino linemap_get_statistics (line_table, &s);
93*e4b17023SJohn Marino
94*e4b17023SJohn Marino macro_maps_size = s.macro_maps_used_size
95*e4b17023SJohn Marino + s.macro_maps_locations_size;
96*e4b17023SJohn Marino
97*e4b17023SJohn Marino total_allocated_map_size = s.ordinary_maps_allocated_size
98*e4b17023SJohn Marino + s.macro_maps_allocated_size
99*e4b17023SJohn Marino + s.macro_maps_locations_size;
100*e4b17023SJohn Marino
101*e4b17023SJohn Marino total_used_map_size = s.ordinary_maps_used_size
102*e4b17023SJohn Marino + s.macro_maps_used_size
103*e4b17023SJohn Marino + s.macro_maps_locations_size;
104*e4b17023SJohn Marino
105*e4b17023SJohn Marino fprintf (stderr, "Number of expanded macros: %5ld\n",
106*e4b17023SJohn Marino s.num_expanded_macros);
107*e4b17023SJohn Marino if (s.num_expanded_macros != 0)
108*e4b17023SJohn Marino fprintf (stderr, "Average number of tokens per macro expansion: %5ld\n",
109*e4b17023SJohn Marino s.num_macro_tokens / s.num_expanded_macros);
110*e4b17023SJohn Marino fprintf (stderr,
111*e4b17023SJohn Marino "\nLine Table allocations during the "
112*e4b17023SJohn Marino "compilation process\n");
113*e4b17023SJohn Marino fprintf (stderr, "Number of ordinary maps used: %5ld%c\n",
114*e4b17023SJohn Marino SCALE (s.num_ordinary_maps_used),
115*e4b17023SJohn Marino STAT_LABEL (s.num_ordinary_maps_used));
116*e4b17023SJohn Marino fprintf (stderr, "Ordinary map used size: %5ld%c\n",
117*e4b17023SJohn Marino SCALE (s.ordinary_maps_used_size),
118*e4b17023SJohn Marino STAT_LABEL (s.ordinary_maps_used_size));
119*e4b17023SJohn Marino fprintf (stderr, "Number of ordinary maps allocated: %5ld%c\n",
120*e4b17023SJohn Marino SCALE (s.num_ordinary_maps_allocated),
121*e4b17023SJohn Marino STAT_LABEL (s.num_ordinary_maps_allocated));
122*e4b17023SJohn Marino fprintf (stderr, "Ordinary maps allocated size: %5ld%c\n",
123*e4b17023SJohn Marino SCALE (s.ordinary_maps_allocated_size),
124*e4b17023SJohn Marino STAT_LABEL (s.ordinary_maps_allocated_size));
125*e4b17023SJohn Marino fprintf (stderr, "Number of macro maps used: %5ld%c\n",
126*e4b17023SJohn Marino SCALE (s.num_macro_maps_used),
127*e4b17023SJohn Marino STAT_LABEL (s.num_macro_maps_used));
128*e4b17023SJohn Marino fprintf (stderr, "Macro maps used size: %5ld%c\n",
129*e4b17023SJohn Marino SCALE (s.macro_maps_used_size),
130*e4b17023SJohn Marino STAT_LABEL (s.macro_maps_used_size));
131*e4b17023SJohn Marino fprintf (stderr, "Macro maps locations size: %5ld%c\n",
132*e4b17023SJohn Marino SCALE (s.macro_maps_locations_size),
133*e4b17023SJohn Marino STAT_LABEL (s.macro_maps_locations_size));
134*e4b17023SJohn Marino fprintf (stderr, "Macro maps size: %5ld%c\n",
135*e4b17023SJohn Marino SCALE (macro_maps_size),
136*e4b17023SJohn Marino STAT_LABEL (macro_maps_size));
137*e4b17023SJohn Marino fprintf (stderr, "Duplicated maps locations size: %5ld%c\n",
138*e4b17023SJohn Marino SCALE (s.duplicated_macro_maps_locations_size),
139*e4b17023SJohn Marino STAT_LABEL (s.duplicated_macro_maps_locations_size));
140*e4b17023SJohn Marino fprintf (stderr, "Total allocated maps size: %5ld%c\n",
141*e4b17023SJohn Marino SCALE (total_allocated_map_size),
142*e4b17023SJohn Marino STAT_LABEL (total_allocated_map_size));
143*e4b17023SJohn Marino fprintf (stderr, "Total used maps size: %5ld%c\n",
144*e4b17023SJohn Marino SCALE (total_used_map_size),
145*e4b17023SJohn Marino STAT_LABEL (total_used_map_size));
146*e4b17023SJohn Marino fprintf (stderr, "\n");
147*e4b17023SJohn Marino }
148