hppa: Fix pr104869.C on hpux
[official-gcc.git] / gcc / stringpool.cc
blob38489677bf3463f0ac132904419548332cf0b863
1 /* String pool for GCC.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 /* String text, identifier text and identifier node allocator.
21 Identifiers are uniquely stored in a hash table.
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. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tree.h"
32 #include "cpplib.h"
34 struct ht *ident_hash;
35 struct ht *ident_hash_extra;
37 static hashnode alloc_node (cpp_hash_table *);
38 static int mark_ident (struct cpp_reader *, hashnode, const void *);
40 static void *
41 stringpool_ggc_alloc (size_t x)
43 return ggc_alloc_atomic (x);
46 /* Initialize the string pool. */
47 void
48 init_stringpool (void)
50 /* Clean up if we're called more than once.
51 (We can't make this idempotent since identifiers contain state) */
52 if (ident_hash)
53 ht_destroy (ident_hash);
54 if (ident_hash_extra)
55 ht_destroy (ident_hash_extra);
57 /* Create with 16K (2^14) entries. */
58 ident_hash = ht_create (14);
59 ident_hash->alloc_node = alloc_node;
60 ident_hash->alloc_subobject = stringpool_ggc_alloc;
62 /* Create with 64 (2^6) entries. */
63 ident_hash_extra = ht_create (6);
64 ident_hash_extra->alloc_node = [] (cpp_hash_table *)
66 return HT_NODE (ggc_cleared_alloc<cpp_hashnode_extra> ());
68 ident_hash_extra->alloc_subobject = stringpool_ggc_alloc;
71 /* Allocate a hash node. */
72 static hashnode
73 alloc_node (cpp_hash_table *table ATTRIBUTE_UNUSED)
75 return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
78 /* Allocate and return a string constant of length LENGTH, containing
79 CONTENTS. If LENGTH is -1, CONTENTS is assumed to be a
80 nul-terminated string, and the length is calculated using strlen. */
82 const char *
83 ggc_alloc_string (const char *contents, int length MEM_STAT_DECL)
85 if (length == -1)
86 length = strlen (contents);
88 if (!length)
89 return "";
91 char *result = (char *) ggc_alloc_atomic (length + 1);
92 memcpy (result, contents, length);
93 result[length] = '\0';
95 return (const char *) result;
98 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
99 If an identifier with that name has previously been referred to,
100 the same node is returned this time. */
102 #undef get_identifier
104 tree
105 get_identifier (const char *text)
107 hashnode ht_node = ht_lookup (ident_hash,
108 (const unsigned char *) text,
109 strlen (text), HT_ALLOC);
111 /* ht_node can't be NULL here. */
112 return HT_IDENT_TO_GCC_IDENT (ht_node);
115 /* Identical to get_identifier, except that the length is assumed
116 known. */
118 tree
119 get_identifier_with_length (const char *text, size_t length)
121 hashnode ht_node = ht_lookup (ident_hash,
122 (const unsigned char *) text,
123 length, HT_ALLOC);
125 /* ht_node can't be NULL here. */
126 return HT_IDENT_TO_GCC_IDENT (ht_node);
129 /* If an identifier with the name TEXT (a null-terminated string) has
130 previously been referred to, return that node; otherwise return
131 NULL_TREE. */
133 tree
134 maybe_get_identifier (const char *text)
136 hashnode ht_node;
138 ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
139 strlen (text), HT_NO_INSERT);
140 if (ht_node)
141 return HT_IDENT_TO_GCC_IDENT (ht_node);
143 return NULL_TREE;
146 /* Report some basic statistics about the string pool. */
148 void
149 stringpool_statistics (void)
151 ht_dump_statistics (ident_hash);
154 /* Mark an identifier for GC. */
156 static int
157 mark_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
158 const void *v ATTRIBUTE_UNUSED)
160 gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h));
161 return 1;
164 /* Return true if an identifier should be removed from the table. */
166 static int
167 maybe_delete_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
168 const void *v ATTRIBUTE_UNUSED)
170 return !ggc_marked_p (HT_IDENT_TO_GCC_IDENT (h));
173 /* Mark the trees hanging off the identifier node for GGC. These are
174 handled specially (not using gengtype) because identifiers are only
175 roots during one part of compilation. */
177 void
178 ggc_mark_stringpool (void)
180 ht_forall (ident_hash, mark_ident, NULL);
181 ht_forall (ident_hash_extra,
182 [] (cpp_reader *, hashnode h, const void *)
184 gt_ggc_m_18cpp_hashnode_extra (h);
185 return 1;
186 }, nullptr);
189 /* Purge the identifier hash of identifiers which are no longer
190 referenced. */
192 void
193 ggc_purge_stringpool (void)
195 ht_purge (ident_hash, maybe_delete_ident, NULL);
196 ht_purge (ident_hash_extra,
197 [] (cpp_reader *, hashnode h, const void *) -> int
199 return !ggc_marked_p (h);
200 }, nullptr);
203 /* Pointer-walking routine for strings (not very interesting, since
204 strings don't contain pointers). */
206 void
207 gt_pch_p_S (void *obj ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED,
208 gt_pointer_operator op ATTRIBUTE_UNUSED,
209 void *cookie ATTRIBUTE_UNUSED)
211 gcc_unreachable ();
214 /* PCH pointer-walking routine for strings. */
216 void
217 gt_pch_n_S (const void *x)
219 gt_pch_note_object (CONST_CAST (void *, x), CONST_CAST (void *, x),
220 &gt_pch_p_S);
223 void
224 gt_pch_n_S2 (const void *x, size_t string_len)
226 gt_pch_note_object (CONST_CAST (void *, x), CONST_CAST (void *, x),
227 &gt_pch_p_S, string_len);
231 /* User-callable entry point for marking string X. */
233 void
234 gt_pch_nx (const char *& x)
236 gt_pch_n_S (x);
239 void
240 gt_pch_nx (char *& x)
242 gt_pch_n_S (x);
245 void
246 gt_pch_nx (unsigned char *& x)
248 gt_pch_n_S (x);
251 void
252 gt_pch_nx (unsigned char& x ATTRIBUTE_UNUSED)
256 void
257 gt_pch_nx (unsigned char *x, gt_pointer_operator op, void *cookie)
259 op (x, NULL, cookie);
262 /* Handle saving and restoring the string pool for PCH. */
264 /* SPD is saved in the PCH file and holds the information needed
265 to restore the string pool. */
267 struct GTY(()) string_pool_data {
268 ht_identifier_ptr *
269 GTY((length ("%h.nslots"),
270 nested_ptr (union tree_node, "%h ? GCC_IDENT_TO_HT_IDENT (%h) : NULL",
271 "%h ? HT_IDENT_TO_GCC_IDENT (%h) : NULL")))
272 entries;
273 unsigned int nslots;
274 unsigned int nelements;
277 struct GTY (()) string_pool_data_extra
279 ht_identifier_ptr *
280 GTY((length ("%h.nslots"),
281 nested_ptr (cpp_hashnode_extra, "%h ? HT_NODE (%h) : nullptr",
282 "(cpp_hashnode_extra *)%h")))
283 entries;
284 unsigned int nslots;
285 unsigned int nelements;
288 static GTY(()) struct string_pool_data * spd;
289 static GTY(()) struct string_pool_data_extra *spd2;
291 /* Save the stringpool data in SPD. */
293 void
294 gt_pch_save_stringpool (void)
296 spd = ggc_alloc<string_pool_data> ();
297 spd->nslots = ident_hash->nslots;
298 spd->nelements = ident_hash->nelements;
299 spd->entries = ggc_vec_alloc<ht_identifier_ptr> (spd->nslots);
300 memcpy (spd->entries, ident_hash->entries,
301 spd->nslots * sizeof (spd->entries[0]));
303 spd2 = ggc_alloc<string_pool_data_extra> ();
304 spd2->nslots = ident_hash_extra->nslots;
305 spd2->nelements = ident_hash_extra->nelements;
306 spd2->entries = ggc_vec_alloc<ht_identifier_ptr> (spd2->nslots);
307 memcpy (spd2->entries, ident_hash_extra->entries,
308 spd2->nslots * sizeof (spd2->entries[0]));
311 /* Return the stringpool to its state before gt_pch_save_stringpool
312 was called. */
314 void
315 gt_pch_fixup_stringpool (void)
319 /* A PCH file has been restored, which loaded SPD; fill the real hash table
320 from SPD. */
322 void
323 gt_pch_restore_stringpool (void)
325 ht_load (ident_hash, spd->entries, spd->nslots, spd->nelements, false);
326 ht_load (ident_hash_extra, spd2->entries, spd2->nslots, spd2->nelements,
327 false);
328 spd = NULL;
329 spd2 = NULL;
332 #include "gt-stringpool.h"