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