2006-10-28 Andrew Pinski <andrew_pinski@playstation.sony.com>
[official-gcc.git] / gcc / stringpool.c
blobb3cfcc45a096b82726a09a7d90bbddef2f550ff1
1 /* String pool for GCC.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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 2, or (at your option) any later
10 version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* String text, identifier text and identifier node allocator. Strings
23 allocated by ggc_alloc_string are stored in an obstack which is
24 never shrunk. Identifiers are uniquely stored in a hash table.
26 We use cpplib's hash table implementation. libiberty's
27 hashtab.c is not used because it requires 100% average space
28 overhead per string, which is unacceptable. Also, this algorithm
29 is faster. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "ggc.h"
36 #include "tree.h"
37 #include "symtab.h"
38 #include "cpplib.h"
40 /* The "" allocated string. */
41 const char empty_string[] = "";
43 /* Character strings, each containing a single decimal digit.
44 Written this way to save space. */
45 const char digit_vector[] = {
46 '0', 0, '1', 0, '2', 0, '3', 0, '4', 0,
47 '5', 0, '6', 0, '7', 0, '8', 0, '9', 0
50 struct ht *ident_hash;
51 static struct obstack string_stack;
53 static hashnode alloc_node (hash_table *);
54 static int mark_ident (struct cpp_reader *, hashnode, const void *);
56 static void *
57 stringpool_ggc_alloc (size_t x)
59 return ggc_alloc (x);
62 /* Initialize the string pool. */
63 void
64 init_stringpool (void)
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 gcc_obstack_init (&string_stack);
73 /* Allocate a hash node. */
74 static hashnode
75 alloc_node (hash_table *table ATTRIBUTE_UNUSED)
77 return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
80 /* Allocate and return a string constant of length LENGTH, containing
81 CONTENTS. If LENGTH is -1, CONTENTS is assumed to be a
82 nul-terminated string, and the length is calculated using strlen.
83 If the same string constant has been allocated before, that copy is
84 returned this time too. */
86 const char *
87 ggc_alloc_string (const char *contents, int length)
89 if (length == -1)
90 length = strlen (contents);
92 if (length == 0)
93 return empty_string;
94 if (length == 1 && ISDIGIT (contents[0]))
95 return digit_string (contents[0] - '0');
97 obstack_grow0 (&string_stack, contents, length);
98 return XOBFINISH (&string_stack, const char *);
101 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
102 If an identifier with that name has previously been referred to,
103 the same node is returned this time. */
105 #undef get_identifier
107 tree
108 get_identifier (const char *text)
110 hashnode ht_node = ht_lookup (ident_hash,
111 (const unsigned char *) text,
112 strlen (text), HT_ALLOC);
114 /* ht_node can't be NULL here. */
115 return HT_IDENT_TO_GCC_IDENT (ht_node);
118 /* Identical to get_identifier, except that the length is assumed
119 known. */
121 tree
122 get_identifier_with_length (const char *text, size_t length)
124 hashnode ht_node = ht_lookup (ident_hash,
125 (const unsigned char *) text,
126 length, HT_ALLOC);
128 /* ht_node can't be NULL here. */
129 return HT_IDENT_TO_GCC_IDENT (ht_node);
132 /* If an identifier with the name TEXT (a null-terminated string) has
133 previously been referred to, return that node; otherwise return
134 NULL_TREE. */
136 tree
137 maybe_get_identifier (const char *text)
139 hashnode ht_node;
141 ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
142 strlen (text), HT_NO_INSERT);
143 if (ht_node)
144 return HT_IDENT_TO_GCC_IDENT (ht_node);
146 return NULL_TREE;
149 /* Report some basic statistics about the string pool. */
151 void
152 stringpool_statistics (void)
154 ht_dump_statistics (ident_hash);
157 /* Mark an identifier for GC. */
159 static int
160 mark_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
161 const void *v ATTRIBUTE_UNUSED)
163 gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h));
164 return 1;
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. */
171 void
172 ggc_mark_stringpool (void)
174 ht_forall (ident_hash, mark_ident, NULL);
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. */
181 void
182 gt_ggc_m_S (void *x ATTRIBUTE_UNUSED)
186 /* Pointer-walking routine for strings (not very interesting, since
187 strings don't contain pointers). */
189 void
190 gt_pch_p_S (void *obj ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED,
191 gt_pointer_operator op ATTRIBUTE_UNUSED,
192 void *cookie ATTRIBUTE_UNUSED)
196 /* PCH pointer-walking routine for strings. */
198 void
199 gt_pch_n_S (const void *x)
201 gt_pch_note_object ((void *)x, (void *)x, &gt_pch_p_S,
202 gt_types_enum_last);
205 /* Handle saving and restoring the string pool for PCH. */
207 /* SPD is saved in the PCH file and holds the information needed
208 to restore the string pool. */
210 struct string_pool_data GTY(())
212 struct ht_identifier * *
213 GTY((length ("%h.nslots"),
214 nested_ptr (union tree_node, "%h ? GCC_IDENT_TO_HT_IDENT (%h) : NULL",
215 "%h ? HT_IDENT_TO_GCC_IDENT (%h) : NULL")))
216 entries;
217 unsigned int nslots;
218 unsigned int nelements;
221 static GTY(()) struct string_pool_data * spd;
223 /* Save the stringpool data in SPD. */
225 void
226 gt_pch_save_stringpool (void)
228 spd = ggc_alloc (sizeof (*spd));
229 spd->nslots = ident_hash->nslots;
230 spd->nelements = ident_hash->nelements;
231 spd->entries = ggc_alloc (sizeof (spd->entries[0]) * spd->nslots);
232 memcpy (spd->entries, ident_hash->entries,
233 spd->nslots * sizeof (spd->entries[0]));
236 /* Return the stringpool to its state before gt_pch_save_stringpool
237 was called. */
239 void
240 gt_pch_fixup_stringpool (void)
244 /* A PCH file has been restored, which loaded SPD; fill the real hash table
245 from SPD. */
247 void
248 gt_pch_restore_stringpool (void)
250 ht_load (ident_hash, spd->entries, spd->nslots, spd->nelements, false);
251 spd = NULL;
254 #include "gt-stringpool.h"