OSDN Git Service

* xtensa-config.h: Undef all macros before defining them.
[pf3gnuchains/gcc-fork.git] / gcc / stringpool.c
1 /* String pool for GCC.
2    Copyright (C) 2000, 2001, 2002, 2003 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 (hash_table *);
53 static int mark_ident (struct cpp_reader *, hashnode, const void *);
54 static int ht_copy_and_clear (struct cpp_reader *, hashnode, const void *);
55
56 /* Initialize the string pool.  */
57 void
58 init_stringpool (void)
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 (hash_table *table ATTRIBUTE_UNUSED)
69 {
70   return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
71 }
72
73 /* Allocate and return a string constant of length LENGTH, containing
74    CONTENTS.  If LENGTH is -1, CONTENTS is assumed to be a
75    nul-terminated string, and the length is calculated using strlen.
76    If the same string constant has been allocated before, that copy is
77    returned this time too.  */
78
79 const char *
80 ggc_alloc_string (const char *contents, int length)
81 {
82   if (length == -1)
83     length = strlen (contents);
84
85   if (length == 0)
86     return empty_string;
87   if (length == 1 && ISDIGIT (contents[0]))
88     return digit_string (contents[0] - '0');
89
90   obstack_grow0 (&string_stack, contents, length);
91   return obstack_finish (&string_stack);
92 }
93
94 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
95    If an identifier with that name has previously been referred to,
96    the same node is returned this time.  */
97
98 tree
99 get_identifier (const char *text)
100 {
101   hashnode ht_node = ht_lookup (ident_hash,
102                                 (const unsigned char *) text,
103                                 strlen (text), HT_ALLOC);
104
105   /* ht_node can't be NULL here.  */
106   return HT_IDENT_TO_GCC_IDENT (ht_node);
107 }
108
109 /* Identical to get_identifier, except that the length is assumed
110    known.  */
111
112 tree
113 get_identifier_with_length (const char *text, unsigned int length)
114 {
115   hashnode ht_node = ht_lookup (ident_hash,
116                                 (const unsigned char *) text,
117                                 length, HT_ALLOC);
118
119   /* ht_node can't be NULL here.  */
120   return HT_IDENT_TO_GCC_IDENT (ht_node);
121 }
122
123 /* If an identifier with the name TEXT (a null-terminated string) has
124    previously been referred to, return that node; otherwise return
125    NULL_TREE.  */
126
127 tree
128 maybe_get_identifier (const char *text)
129 {
130   hashnode ht_node;
131
132   ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
133                        strlen (text), HT_NO_INSERT);
134   if (ht_node)
135     return HT_IDENT_TO_GCC_IDENT (ht_node);
136
137   return NULL_TREE;
138 }
139
140 /* Report some basic statistics about the string pool.  */
141
142 void
143 stringpool_statistics (void)
144 {
145   ht_dump_statistics (ident_hash);
146 }
147
148 /* Mark an identifier for GC.  */
149
150 static int
151 mark_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
152             const void *v ATTRIBUTE_UNUSED)
153 {
154   gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h));
155   return 1;
156 }
157
158 /* Mark the trees hanging off the identifier node for GGC.  These are
159    handled specially (not using gengtype) because of the special
160    treatment for strings.  */
161
162 void
163 ggc_mark_stringpool (void)
164 {
165   ht_forall (ident_hash, mark_ident, NULL);
166 }
167
168 /* Strings are _not_ GCed, but this routine exists so that a separate
169    roots table isn't needed for the few global variables that refer
170    to strings.  */
171
172 void
173 gt_ggc_m_S (void *x ATTRIBUTE_UNUSED)
174 {
175 }
176
177 /* Pointer-walking routine for strings (not very interesting, since
178    strings don't contain pointers).  */
179
180 void
181 gt_pch_p_S (void *obj ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED,
182             gt_pointer_operator op ATTRIBUTE_UNUSED,
183             void *cookie ATTRIBUTE_UNUSED)
184 {
185 }
186
187 /* PCH pointer-walking routine for strings.  */
188
189 void
190 gt_pch_n_S (const void *x)
191 {
192   gt_pch_note_object ((void *)x, (void *)x, &gt_pch_p_S);
193 }
194
195 /* Handle saving and restoring the string pool for PCH.  */
196
197 struct string_pool_data GTY(())
198 {
199   tree * GTY((length ("%h.nslots"))) entries;
200   unsigned int nslots;
201   unsigned int nelements;
202 };
203
204 static GTY(()) struct string_pool_data * spd;
205
206 static int
207 ht_copy_and_clear (cpp_reader *r ATTRIBUTE_UNUSED, hashnode hp, const void *ht2_p)
208 {
209   cpp_hashnode *h = CPP_HASHNODE (hp);
210   struct ht *ht2 = (struct ht *) ht2_p;
211
212   if (h->type != NT_VOID
213       && (h->flags & NODE_BUILTIN) == 0)
214     {
215       cpp_hashnode *h2 = CPP_HASHNODE (ht_lookup (ht2,
216                                                   NODE_NAME (h),
217                                                   NODE_LEN (h),
218                                                   HT_ALLOC));
219       h2->type = h->type;
220       memcpy (&h2->value, &h->value, sizeof (h->value));
221
222       h->type = NT_VOID;
223       memset (&h->value, 0, sizeof (h->value));
224     }
225   return 1;
226 }
227
228 static struct ht *saved_ident_hash;
229
230 void
231 gt_pch_save_stringpool (void)
232 {
233   unsigned int i;
234
235   spd = ggc_alloc (sizeof (*spd));
236   spd->nslots = ident_hash->nslots;
237   spd->nelements = ident_hash->nelements;
238   spd->entries = ggc_alloc (sizeof (tree *) * spd->nslots);
239   for (i = 0; i < spd->nslots; i++)
240     if (ident_hash->entries[i] != NULL)
241       spd->entries[i] = HT_IDENT_TO_GCC_IDENT (ident_hash->entries[i]);
242     else
243       spd->entries[i] = NULL;
244
245   saved_ident_hash = ht_create (14);
246   saved_ident_hash->alloc_node = alloc_node;
247   ht_forall (ident_hash, ht_copy_and_clear, saved_ident_hash);
248 }
249
250 void
251 gt_pch_fixup_stringpool (void)
252 {
253   ht_forall (saved_ident_hash, ht_copy_and_clear, ident_hash);
254   ht_destroy (saved_ident_hash);
255   saved_ident_hash = 0;
256 }
257
258 void
259 gt_pch_restore_stringpool (void)
260 {
261   unsigned int i;
262
263   ident_hash->nslots = spd->nslots;
264   ident_hash->nelements = spd->nelements;
265   ident_hash->entries = xrealloc (ident_hash->entries,
266                                   sizeof (hashnode) * spd->nslots);
267   for (i = 0; i < spd->nslots; i++)
268     if (spd->entries[i] != NULL)
269       ident_hash->entries[i] = GCC_IDENT_TO_HT_IDENT (spd->entries[i]);
270     else
271       ident_hash->entries[i] = NULL;
272
273   spd = NULL;
274 }
275
276 #include "gt-stringpool.h"