OSDN Git Service

* configure.in: Delete three unused variables. Move a variable
[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
38 /* The "" allocated string.  */
39 const char empty_string[] = "";
40
41 /* Character strings, each containing a single decimal digit.
42    Written this way to save space.  */
43 const char digit_vector[] = {
44   '0', 0, '1', 0, '2', 0, '3', 0, '4', 0,
45   '5', 0, '6', 0, '7', 0, '8', 0, '9', 0
46 };
47
48 struct ht *ident_hash;
49 static struct obstack string_stack;
50
51 static hashnode alloc_node PARAMS ((hash_table *));
52 static int mark_ident PARAMS ((struct cpp_reader *, hashnode, const PTR));
53
54 /* Initialize the string pool.  */
55 void
56 init_stringpool ()
57 {
58   /* Create with 16K (2^14) entries.  */
59   ident_hash = ht_create (14);
60   ident_hash->alloc_node = alloc_node;
61   gcc_obstack_init (&string_stack);
62 }
63
64 /* Allocate a hash node.  */
65 static hashnode
66 alloc_node (table)
67      hash_table *table ATTRIBUTE_UNUSED;
68 {
69   return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
70 }
71
72 /* Allocate and return a string constant of length LENGTH, containing
73    CONTENTS.  If LENGTH is -1, CONTENTS is assumed to be a
74    nul-terminated string, and the length is calculated using strlen.
75    If the same string constant has been allocated before, that copy is
76    returned this time too.  */
77
78 const char *
79 ggc_alloc_string (contents, length)
80      const char *contents;
81      int length;
82 {
83   if (length == -1)
84     length = strlen (contents);
85
86   if (length == 0)
87     return empty_string;
88   if (length == 1 && ISDIGIT (contents[0]))
89     return digit_string (contents[0] - '0');
90
91   obstack_grow0 (&string_stack, contents, length);
92   return obstack_finish (&string_stack);
93 }
94
95 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
96    If an identifier with that name has previously been referred to,
97    the same node is returned this time.  */
98
99 tree
100 get_identifier (text)
101      const char *text;
102 {
103   hashnode ht_node = ht_lookup (ident_hash,
104                                 (const unsigned char *) text,
105                                 strlen (text), HT_ALLOC);
106
107   /* ht_node can't be NULL here.  */
108   return HT_IDENT_TO_GCC_IDENT (ht_node);
109 }
110
111 /* Identical to get_identifier, except that the length is assumed
112    known.  */
113
114 tree
115 get_identifier_with_length (text, length)
116      const char *text;
117      unsigned int length;
118 {
119   hashnode ht_node = ht_lookup (ident_hash,
120                                 (const unsigned char *) text,
121                                 length, HT_ALLOC);
122
123   /* ht_node can't be NULL here.  */
124   return HT_IDENT_TO_GCC_IDENT (ht_node);
125 }
126
127 /* If an identifier with the name TEXT (a null-terminated string) has
128    previously been referred to, return that node; otherwise return
129    NULL_TREE.  */
130
131 tree
132 maybe_get_identifier (text)
133      const char *text;
134 {
135   hashnode ht_node;
136
137   ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
138                        strlen (text), HT_NO_INSERT);
139   if (ht_node)
140     return HT_IDENT_TO_GCC_IDENT (ht_node);
141
142   return NULL_TREE;
143 }
144
145 /* Report some basic statistics about the string pool.  */
146
147 void
148 stringpool_statistics ()
149 {
150   ht_dump_statistics (ident_hash);
151 }
152
153 /* Mark an identifier for GC.  */
154
155 static int
156 mark_ident (pfile, h, v)
157      struct cpp_reader *pfile ATTRIBUTE_UNUSED;
158      hashnode h;
159      const PTR v ATTRIBUTE_UNUSED;
160 {
161   gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h));
162   return 1;
163 }
164
165 /* Mark the trees hanging off the identifier node for GGC.  These are
166    handled specially (not using gengtype) because of the special
167    treatment for strings.  */
168
169 void
170 ggc_mark_stringpool ()
171 {
172   ht_forall (ident_hash, mark_ident, NULL);
173 }
174
175 /* Strings are _not_ GCed, but this routine exists so that a separate
176    roots table isn't needed for the few global variables that refer
177    to strings.  */
178
179 void
180 gt_ggc_m_S (x)
181      void *x ATTRIBUTE_UNUSED;
182 {
183 }
184
185 /* Pointer-walking routine for strings (not very interesting, since
186    strings don't contain pointers).  */
187
188 void
189 gt_pch_p_S (obj, x, op, cookie)
190      void *obj ATTRIBUTE_UNUSED;
191      void *x ATTRIBUTE_UNUSED;
192      gt_pointer_operator op ATTRIBUTE_UNUSED;
193      void *cookie ATTRIBUTE_UNUSED;
194 {
195 }
196
197 /* PCH pointer-walking routine for strings.  */
198
199 void
200 gt_pch_n_S (x)
201      const void *x;
202 {
203   gt_pch_note_object ((void *)x, (void *)x, &gt_pch_p_S);
204 }
205
206 /* Handle saving and restoring the string pool for PCH.  */
207
208 struct string_pool_data GTY(())
209 {
210   tree * GTY((length ("%h.nslots"))) entries;
211   unsigned int nslots;
212   unsigned int nelements;
213 };
214
215 static GTY(()) struct string_pool_data * spd;
216
217 void
218 gt_pch_save_stringpool ()
219 {
220   unsigned int i;
221   
222   spd = ggc_alloc (sizeof (*spd));
223   spd->nslots = ident_hash->nslots;
224   spd->nelements = ident_hash->nelements;
225   spd->entries = ggc_alloc (sizeof (tree *) * spd->nslots);
226   for (i = 0; i < spd->nslots; i++)
227     if (ident_hash->entries[i] != NULL)
228       spd->entries[i] = HT_IDENT_TO_GCC_IDENT (ident_hash->entries[i]);
229     else
230       spd->entries[i] = NULL;
231 }
232
233 void
234 gt_pch_restore_stringpool ()
235 {
236   unsigned int i;
237   
238   ident_hash->nslots = spd->nslots;
239   ident_hash->nelements = spd->nelements;
240   ident_hash->entries = xrealloc (ident_hash->entries,
241                                   sizeof (hashnode) * spd->nslots);
242   for (i = 0; i < spd->nslots; i++)
243     if (spd->entries[i] != NULL)
244       ident_hash->entries[i] = GCC_IDENT_TO_HT_IDENT (spd->entries[i]);
245     else
246       ident_hash->entries[i] = NULL;
247
248   spd = NULL;
249 }
250
251 #include "gt-stringpool.h"