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