OSDN Git Service

* cpppch.c (reset_ht): Remove.
[pf3gnuchains/gcc-fork.git] / gcc / stringpool.c
1 /* String pool for GCC.
2    Copyright (C) 2000, 2001, 2002 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 2, 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 COPYING.  If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.  */
20
21 /* String text, identifier text and identifier node allocator.  Strings
22    allocated by ggc_alloc_string are stored in an obstack which is
23    never shrunk.  Identifiers are uniquely stored in a hash table.
24
25    We have our own private hash table implementation.  libiberty's
26    hashtab.c is not used because it requires 100% average space
27    overhead per string, which is unacceptable.  Also, this algorithm
28    is faster.  */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "ggc.h"
35 #include "tree.h"
36 #include "hashtable.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 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 struct ht *ident_hash;
50 static struct obstack string_stack;
51
52 static hashnode alloc_node PARAMS ((hash_table *));
53 static int mark_ident PARAMS ((struct cpp_reader *, hashnode, const PTR));
54 static int ht_copy_and_clear PARAMS ((struct cpp_reader *, hashnode, const void *));
55
56 /* Initialize the string pool.  */
57 void
58 init_stringpool ()
59 {
60   /* Create with 16K (2^14) entries.  */
61   ident_hash = ht_create (14);
62   ident_hash->alloc_node = alloc_node;
63   gcc_obstack_init (&string_stack);
64 }
65
66 /* Allocate a hash node.  */
67 static hashnode
68 alloc_node (table)
69      hash_table *table ATTRIBUTE_UNUSED;
70 {
71   return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
72 }
73
74 /* Allocate and return a string constant of length LENGTH, containing
75    CONTENTS.  If LENGTH is -1, CONTENTS is assumed to be a
76    nul-terminated string, and the length is calculated using strlen.
77    If the same string constant has been allocated before, that copy is
78    returned this time too.  */
79
80 const char *
81 ggc_alloc_string (contents, length)
82      const char *contents;
83      int length;
84 {
85   if (length == -1)
86     length = strlen (contents);
87
88   if (length == 0)
89     return empty_string;
90   if (length == 1 && ISDIGIT (contents[0]))
91     return digit_string (contents[0] - '0');
92
93   obstack_grow0 (&string_stack, contents, length);
94   return obstack_finish (&string_stack);
95 }
96
97 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
98    If an identifier with that name has previously been referred to,
99    the same node is returned this time.  */
100
101 tree
102 get_identifier (text)
103      const char *text;
104 {
105   hashnode ht_node = ht_lookup (ident_hash,
106                                 (const unsigned char *) text,
107                                 strlen (text), HT_ALLOC);
108
109   /* ht_node can't be NULL here.  */
110   return HT_IDENT_TO_GCC_IDENT (ht_node);
111 }
112
113 /* Identical to get_identifier, except that the length is assumed
114    known.  */
115
116 tree
117 get_identifier_with_length (text, length)
118      const char *text;
119      unsigned int length;
120 {
121   hashnode ht_node = ht_lookup (ident_hash,
122                                 (const unsigned char *) text,
123                                 length, HT_ALLOC);
124
125   /* ht_node can't be NULL here.  */
126   return HT_IDENT_TO_GCC_IDENT (ht_node);
127 }
128
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.  */
132
133 tree
134 maybe_get_identifier (text)
135      const char *text;
136 {
137   hashnode ht_node;
138
139   ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
140                        strlen (text), HT_NO_INSERT);
141   if (ht_node)
142     return HT_IDENT_TO_GCC_IDENT (ht_node);
143
144   return NULL_TREE;
145 }
146
147 /* Report some basic statistics about the string pool.  */
148
149 void
150 stringpool_statistics ()
151 {
152   ht_dump_statistics (ident_hash);
153 }
154
155 /* Mark an identifier for GC.  */
156
157 static int
158 mark_ident (pfile, h, v)
159      struct cpp_reader *pfile ATTRIBUTE_UNUSED;
160      hashnode h;
161      const PTR v ATTRIBUTE_UNUSED;
162 {
163   gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h));
164   return 1;
165 }
166
167 /* Mark the trees hanging off the identifier node for GGC.  These are
168    handled specially (not using gengtype) because of the special
169    treatment for strings.  */
170
171 void
172 ggc_mark_stringpool ()
173 {
174   ht_forall (ident_hash, mark_ident, NULL);
175 }
176
177 /* Strings are _not_ GCed, but this routine exists so that a separate
178    roots table isn't needed for the few global variables that refer
179    to strings.  */
180
181 void
182 gt_ggc_m_S (x)
183      void *x ATTRIBUTE_UNUSED;
184 {
185 }
186
187 /* Pointer-walking routine for strings (not very interesting, since
188    strings don't contain pointers).  */
189
190 void
191 gt_pch_p_S (obj, x, op, cookie)
192      void *obj ATTRIBUTE_UNUSED;
193      void *x ATTRIBUTE_UNUSED;
194      gt_pointer_operator op ATTRIBUTE_UNUSED;
195      void *cookie ATTRIBUTE_UNUSED;
196 {
197 }
198
199 /* PCH pointer-walking routine for strings.  */
200
201 void
202 gt_pch_n_S (x)
203      const void *x;
204 {
205   gt_pch_note_object ((void *)x, (void *)x, &gt_pch_p_S);
206 }
207
208 /* Handle saving and restoring the string pool for PCH.  */
209
210 struct string_pool_data GTY(())
211 {
212   tree * GTY((length ("%h.nslots"))) entries;
213   unsigned int nslots;
214   unsigned int nelements;
215 };
216
217 static GTY(()) struct string_pool_data * spd;
218
219 static int 
220 ht_copy_and_clear (r, hp, ht2_p)
221      cpp_reader *r ATTRIBUTE_UNUSED;
222      hashnode hp;
223      const void *ht2_p;
224 {
225   cpp_hashnode *h = CPP_HASHNODE (hp);
226   struct ht *ht2 = (struct ht *) ht2_p;
227
228   if (h->type != NT_VOID
229       && (h->flags & NODE_BUILTIN) == 0)
230     {
231       cpp_hashnode *h2 = CPP_HASHNODE (ht_lookup (ht2,
232                                                   NODE_NAME (h),
233                                                   NODE_LEN (h),
234                                                   HT_ALLOC));
235       h2->type = h->type;
236       memcpy (&h2->value, &h->value, sizeof (h->value));
237
238       h->type = NT_VOID;
239       memset (&h->value, 0, sizeof (h->value));
240     }
241   return 1;
242 }
243
244 static struct ht *saved_ident_hash;
245
246 void
247 gt_pch_save_stringpool ()
248 {
249   unsigned int i;
250   
251   spd = ggc_alloc (sizeof (*spd));
252   spd->nslots = ident_hash->nslots;
253   spd->nelements = ident_hash->nelements;
254   spd->entries = ggc_alloc (sizeof (tree *) * spd->nslots);
255   for (i = 0; i < spd->nslots; i++)
256     if (ident_hash->entries[i] != NULL)
257       spd->entries[i] = HT_IDENT_TO_GCC_IDENT (ident_hash->entries[i]);
258     else
259       spd->entries[i] = NULL;
260
261   saved_ident_hash = ht_create (14);
262   saved_ident_hash->alloc_node = alloc_node;
263   ht_forall (ident_hash, ht_copy_and_clear, saved_ident_hash);
264 }
265
266 void
267 gt_pch_fixup_stringpool ()
268 {
269   ht_forall (saved_ident_hash, ht_copy_and_clear, ident_hash);
270   ht_destroy (saved_ident_hash);
271   saved_ident_hash = 0;
272 }
273
274 void
275 gt_pch_restore_stringpool ()
276 {
277   unsigned int i;
278   
279   ident_hash->nslots = spd->nslots;
280   ident_hash->nelements = spd->nelements;
281   ident_hash->entries = xrealloc (ident_hash->entries,
282                                   sizeof (hashnode) * spd->nslots);
283   for (i = 0; i < spd->nslots; i++)
284     if (spd->entries[i] != NULL)
285       ident_hash->entries[i] = GCC_IDENT_TO_HT_IDENT (spd->entries[i]);
286     else
287       ident_hash->entries[i] = NULL;
288
289   spd = NULL;
290 }
291
292 #include "gt-stringpool.h"