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