OSDN Git Service

2010-07-24 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / stringpool.c
1 /* String pool for GCC.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 /* String text, identifier text and identifier node allocator.
22    Identifiers are uniquely stored in a hash table.
23
24    We use cpplib's hash table implementation.  libiberty's
25    hashtab.c is not used because it requires 100% average space
26    overhead per string, which is unacceptable.  Also, this algorithm
27    is faster.  */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "ggc.h"
34 #include "ggc-internal.h"
35 #include "tree.h"
36 #include "symtab.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 (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   /* Create with 16K (2^14) entries.  */
67   ident_hash = ht_create (14);
68   ident_hash->alloc_node = alloc_node;
69   ident_hash->alloc_subobject = stringpool_ggc_alloc;
70 }
71
72 /* Allocate a hash node.  */
73 static hashnode
74 alloc_node (hash_table *table ATTRIBUTE_UNUSED)
75 {
76   return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
77 }
78
79 /* Allocate and return a string constant of length LENGTH, containing
80    CONTENTS.  If LENGTH is -1, CONTENTS is assumed to be a
81    nul-terminated string, and the length is calculated using strlen.  */
82
83 const char *
84 ggc_alloc_string_stat (const char *contents, int length MEM_STAT_DECL)
85 {
86   char *result;
87
88   if (length == -1)
89     length = strlen (contents);
90
91   if (length == 0)
92     return empty_string;
93   if (length == 1 && ISDIGIT (contents[0]))
94     return digit_string (contents[0] - '0');
95
96   result = (char *) ggc_alloc_atomic_stat (length + 1 PASS_MEM_STAT);
97   memcpy (result, contents, length);
98   result[length] = '\0';
99   return (const char *) result;
100 }
101
102 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
103    If an identifier with that name has previously been referred to,
104    the same node is returned this time.  */
105
106 #undef get_identifier
107
108 tree
109 get_identifier (const char *text)
110 {
111   hashnode ht_node = ht_lookup (ident_hash,
112                                 (const unsigned char *) text,
113                                 strlen (text), HT_ALLOC);
114
115   /* ht_node can't be NULL here.  */
116   return HT_IDENT_TO_GCC_IDENT (ht_node);
117 }
118
119 /* Identical to get_identifier, except that the length is assumed
120    known.  */
121
122 tree
123 get_identifier_with_length (const char *text, size_t length)
124 {
125   hashnode ht_node = ht_lookup (ident_hash,
126                                 (const unsigned char *) text,
127                                 length, HT_ALLOC);
128
129   /* ht_node can't be NULL here.  */
130   return HT_IDENT_TO_GCC_IDENT (ht_node);
131 }
132
133 /* If an identifier with the name TEXT (a null-terminated string) has
134    previously been referred to, return that node; otherwise return
135    NULL_TREE.  */
136
137 tree
138 maybe_get_identifier (const char *text)
139 {
140   hashnode ht_node;
141
142   ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
143                        strlen (text), HT_NO_INSERT);
144   if (ht_node)
145     return HT_IDENT_TO_GCC_IDENT (ht_node);
146
147   return NULL_TREE;
148 }
149
150 /* Report some basic statistics about the string pool.  */
151
152 void
153 stringpool_statistics (void)
154 {
155   ht_dump_statistics (ident_hash);
156 }
157 \f
158 /* Mark an identifier for GC.  */
159
160 static int
161 mark_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
162             const void *v ATTRIBUTE_UNUSED)
163 {
164   gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h));
165   return 1;
166 }
167
168 /* Return true if an identifier should be removed from the table.  */
169
170 static int
171 maybe_delete_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
172                     const void *v ATTRIBUTE_UNUSED)
173 {
174   return !ggc_marked_p (HT_IDENT_TO_GCC_IDENT (h));
175 }
176
177 /* Mark the trees hanging off the identifier node for GGC.  These are
178    handled specially (not using gengtype) because identifiers are only
179    roots during one part of compilation.  */
180
181 void
182 ggc_mark_stringpool (void)
183 {
184   ht_forall (ident_hash, mark_ident, NULL);
185 }
186
187 /* Purge the identifier hash of identifiers which are no longer
188    referenced.  */
189
190 void
191 ggc_purge_stringpool (void)
192 {
193   ht_purge (ident_hash, maybe_delete_ident, NULL);
194 }
195
196 /* Pointer-walking routine for strings (not very interesting, since
197    strings don't contain pointers).  */
198
199 void
200 gt_pch_p_S (void *obj ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED,
201             gt_pointer_operator op ATTRIBUTE_UNUSED,
202             void *cookie ATTRIBUTE_UNUSED)
203 {
204 }
205
206 /* PCH pointer-walking routine for strings.  */
207
208 void
209 gt_pch_n_S (const void *x)
210 {
211   gt_pch_note_object (CONST_CAST (void *, x), CONST_CAST (void *, x),
212                       &gt_pch_p_S, gt_types_enum_last);
213 }
214 \f
215 /* Handle saving and restoring the string pool for PCH.  */
216
217 /* SPD is saved in the PCH file and holds the information needed
218    to restore the string pool.  */
219
220 struct GTY(()) string_pool_data {
221   ht_identifier_ptr *
222     GTY((length ("%h.nslots"),
223          nested_ptr (union tree_node, "%h ? GCC_IDENT_TO_HT_IDENT (%h) : NULL",
224                      "%h ? HT_IDENT_TO_GCC_IDENT (%h) : NULL")))
225     entries;
226   unsigned int nslots;
227   unsigned int nelements;
228 };
229
230 static GTY(()) struct string_pool_data * spd;
231
232 /* Save the stringpool data in SPD.  */
233
234 void
235 gt_pch_save_stringpool (void)
236 {
237   spd = ggc_alloc_string_pool_data ();
238   spd->nslots = ident_hash->nslots;
239   spd->nelements = ident_hash->nelements;
240   spd->entries = ggc_alloc_vec_ht_identifier_ptr (spd->nslots);
241   memcpy (spd->entries, ident_hash->entries,
242           spd->nslots * sizeof (spd->entries[0]));
243 }
244
245 /* Return the stringpool to its state before gt_pch_save_stringpool
246    was called.  */
247
248 void
249 gt_pch_fixup_stringpool (void)
250 {
251 }
252
253 /* A PCH file has been restored, which loaded SPD; fill the real hash table
254    from SPD.  */
255
256 void
257 gt_pch_restore_stringpool (void)
258 {
259   ht_load (ident_hash, spd->entries, spd->nslots, spd->nelements, false);
260   spd = NULL;
261 }
262
263 #include "gt-stringpool.h"