coretypes.h: Include input.h and as-a.h.
[gcc.git] / gcc / stringpool.c
1 /* String pool for GCC.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* String text, identifier text and identifier node allocator.
21 Identifiers are uniquely stored in a hash table.
22
23 We use cpplib's hash table implementation. libiberty's
24 hashtab.c is not used because it requires 100% average space
25 overhead per string, which is unacceptable. Also, this algorithm
26 is faster. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "ggc-internal.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "options.h"
35 #include "tree.h"
36 #include "cpplib.h"
37
38 /* The "" allocated string. */
39 const char empty_string[] = "";
40
41 /* Character strings, each containing a single decimal digit.
42 Written this way to save space. */
43 static const char digit_vector[] = {
44 '0', 0, '1', 0, '2', 0, '3', 0, '4', 0,
45 '5', 0, '6', 0, '7', 0, '8', 0, '9', 0
46 };
47
48 #define digit_string(d) (digit_vector + ((d) * 2))
49
50 struct ht *ident_hash;
51
52 static hashnode alloc_node (cpp_hash_table *);
53 static int mark_ident (struct cpp_reader *, hashnode, const void *);
54
55 static void *
56 stringpool_ggc_alloc (size_t x)
57 {
58 return ggc_alloc_atomic (x);
59 }
60
61 /* Initialize the string pool. */
62 void
63 init_stringpool (void)
64 {
65 /* Clean up if we're called more than once.
66 (We can't make this idempotent since identifiers contain state) */
67 if (ident_hash)
68 ht_destroy (ident_hash);
69
70 /* Create with 16K (2^14) entries. */
71 ident_hash = ht_create (14);
72 ident_hash->alloc_node = alloc_node;
73 ident_hash->alloc_subobject = stringpool_ggc_alloc;
74 }
75
76 /* Allocate a hash node. */
77 static hashnode
78 alloc_node (cpp_hash_table *table ATTRIBUTE_UNUSED)
79 {
80 return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
81 }
82
83 /* Allocate and return a string constant of length LENGTH, containing
84 CONTENTS. If LENGTH is -1, CONTENTS is assumed to be a
85 nul-terminated string, and the length is calculated using strlen. */
86
87 const char *
88 ggc_alloc_string (const char *contents, int length MEM_STAT_DECL)
89 {
90 char *result;
91
92 if (length == -1)
93 length = strlen (contents);
94
95 if (length == 0)
96 return empty_string;
97 if (length == 1 && ISDIGIT (contents[0]))
98 return digit_string (contents[0] - '0');
99
100 result = (char *) ggc_internal_cleared_alloc (length + 1 PASS_MEM_STAT);
101 memcpy (result, contents, length);
102 result[length] = '\0';
103 return (const char *) result;
104 }
105
106 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
107 If an identifier with that name has previously been referred to,
108 the same node is returned this time. */
109
110 #undef get_identifier
111
112 tree
113 get_identifier (const char *text)
114 {
115 hashnode ht_node = ht_lookup (ident_hash,
116 (const unsigned char *) text,
117 strlen (text), HT_ALLOC);
118
119 /* ht_node can't be NULL here. */
120 return HT_IDENT_TO_GCC_IDENT (ht_node);
121 }
122
123 /* Identical to get_identifier, except that the length is assumed
124 known. */
125
126 tree
127 get_identifier_with_length (const char *text, size_t length)
128 {
129 hashnode ht_node = ht_lookup (ident_hash,
130 (const unsigned char *) text,
131 length, HT_ALLOC);
132
133 /* ht_node can't be NULL here. */
134 return HT_IDENT_TO_GCC_IDENT (ht_node);
135 }
136
137 /* If an identifier with the name TEXT (a null-terminated string) has
138 previously been referred to, return that node; otherwise return
139 NULL_TREE. */
140
141 tree
142 maybe_get_identifier (const char *text)
143 {
144 hashnode ht_node;
145
146 ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
147 strlen (text), HT_NO_INSERT);
148 if (ht_node)
149 return HT_IDENT_TO_GCC_IDENT (ht_node);
150
151 return NULL_TREE;
152 }
153
154 /* Report some basic statistics about the string pool. */
155
156 void
157 stringpool_statistics (void)
158 {
159 ht_dump_statistics (ident_hash);
160 }
161 \f
162 /* Mark an identifier for GC. */
163
164 static int
165 mark_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
166 const void *v ATTRIBUTE_UNUSED)
167 {
168 gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h));
169 return 1;
170 }
171
172 /* Return true if an identifier should be removed from the table. */
173
174 static int
175 maybe_delete_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
176 const void *v ATTRIBUTE_UNUSED)
177 {
178 return !ggc_marked_p (HT_IDENT_TO_GCC_IDENT (h));
179 }
180
181 /* Mark the trees hanging off the identifier node for GGC. These are
182 handled specially (not using gengtype) because identifiers are only
183 roots during one part of compilation. */
184
185 void
186 ggc_mark_stringpool (void)
187 {
188 ht_forall (ident_hash, mark_ident, NULL);
189 }
190
191 /* Purge the identifier hash of identifiers which are no longer
192 referenced. */
193
194 void
195 ggc_purge_stringpool (void)
196 {
197 ht_purge (ident_hash, maybe_delete_ident, NULL);
198 }
199
200 /* Pointer-walking routine for strings (not very interesting, since
201 strings don't contain pointers). */
202
203 void
204 gt_pch_p_S (void *obj ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED,
205 gt_pointer_operator op ATTRIBUTE_UNUSED,
206 void *cookie ATTRIBUTE_UNUSED)
207 {
208 }
209
210 /* PCH pointer-walking routine for strings. */
211
212 void
213 gt_pch_n_S (const void *x)
214 {
215 gt_pch_note_object (CONST_CAST (void *, x), CONST_CAST (void *, x),
216 &gt_pch_p_S);
217 }
218
219
220 /* User-callable entry point for marking string X. */
221
222 void
223 gt_pch_nx (const char *& x)
224 {
225 gt_pch_n_S (x);
226 }
227
228 void
229 gt_pch_nx (unsigned char *& x)
230 {
231 gt_pch_n_S (x);
232 }
233
234 void
235 gt_pch_nx (unsigned char& x ATTRIBUTE_UNUSED)
236 {
237 }
238
239 void
240 gt_pch_nx (unsigned char *x, gt_pointer_operator op, void *cookie)
241 {
242 op (x, cookie);
243 }
244 \f
245 /* Handle saving and restoring the string pool for PCH. */
246
247 /* SPD is saved in the PCH file and holds the information needed
248 to restore the string pool. */
249
250 struct GTY(()) string_pool_data {
251 ht_identifier_ptr *
252 GTY((length ("%h.nslots"),
253 nested_ptr (union tree_node, "%h ? GCC_IDENT_TO_HT_IDENT (%h) : NULL",
254 "%h ? HT_IDENT_TO_GCC_IDENT (%h) : NULL")))
255 entries;
256 unsigned int nslots;
257 unsigned int nelements;
258 };
259
260 static GTY(()) struct string_pool_data * spd;
261
262 /* Save the stringpool data in SPD. */
263
264 void
265 gt_pch_save_stringpool (void)
266 {
267 spd = ggc_alloc<string_pool_data> ();
268 spd->nslots = ident_hash->nslots;
269 spd->nelements = ident_hash->nelements;
270 spd->entries = ggc_vec_alloc<ht_identifier_ptr> (spd->nslots);
271 memcpy (spd->entries, ident_hash->entries,
272 spd->nslots * sizeof (spd->entries[0]));
273 }
274
275 /* Return the stringpool to its state before gt_pch_save_stringpool
276 was called. */
277
278 void
279 gt_pch_fixup_stringpool (void)
280 {
281 }
282
283 /* A PCH file has been restored, which loaded SPD; fill the real hash table
284 from SPD. */
285
286 void
287 gt_pch_restore_stringpool (void)
288 {
289 ht_load (ident_hash, spd->entries, spd->nslots, spd->nelements, false);
290 spd = NULL;
291 }
292
293 #include "gt-stringpool.h"