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