]>
Commit | Line | Data |
---|---|---|
520a57c8 | 1 | /* String pool for GCC. |
a2130901 KH |
2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 |
3 | Free Software Foundation, Inc. | |
520a57c8 | 4 | |
1322177d | 5 | This file is part of GCC. |
520a57c8 | 6 | |
1322177d LB |
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. | |
520a57c8 | 11 | |
1322177d LB |
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 | |
520a57c8 ZW |
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 | |
1322177d | 18 | along with GCC; see the file COPYING. If not, write to the Free |
520a57c8 ZW |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
20 | 02111-1307, USA. */ | |
21 | ||
ff7cc307 | 22 | /* String text, identifier text and identifier node allocator. Strings |
2a967f3d NB |
23 | allocated by ggc_alloc_string are stored in an obstack which is |
24 | never shrunk. Identifiers are uniquely stored in a hash table. | |
520a57c8 | 25 | |
4f4e53dd | 26 | We use cpplib's hash table implementation. libiberty's |
2a967f3d NB |
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. */ | |
520a57c8 ZW |
30 | |
31 | #include "config.h" | |
32 | #include "system.h" | |
4977bab6 ZW |
33 | #include "coretypes.h" |
34 | #include "tm.h" | |
520a57c8 ZW |
35 | #include "ggc.h" |
36 | #include "tree.h" | |
4f4e53dd | 37 | #include "symtab.h" |
d24ecd21 | 38 | #include "cpplib.h" |
520a57c8 ZW |
39 | |
40 | /* The "" allocated string. */ | |
41 | const char empty_string[] = ""; | |
42 | ||
a8a05998 ZW |
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 | |
48 | }; | |
49 | ||
2a967f3d | 50 | struct ht *ident_hash; |
520a57c8 | 51 | static struct obstack string_stack; |
520a57c8 | 52 | |
46c5ad27 AJ |
53 | static hashnode alloc_node (hash_table *); |
54 | static int mark_ident (struct cpp_reader *, hashnode, const void *); | |
d8044160 GK |
55 | |
56 | static void * | |
57 | stringpool_ggc_alloc (size_t x) | |
58 | { | |
59 | return ggc_alloc (x); | |
60 | } | |
2a967f3d | 61 | |
520a57c8 ZW |
62 | /* Initialize the string pool. */ |
63 | void | |
46c5ad27 | 64 | init_stringpool (void) |
520a57c8 | 65 | { |
2a967f3d NB |
66 | /* Create with 16K (2^14) entries. */ |
67 | ident_hash = ht_create (14); | |
68 | ident_hash->alloc_node = alloc_node; | |
d8044160 | 69 | ident_hash->alloc_subobject = stringpool_ggc_alloc; |
520a57c8 | 70 | gcc_obstack_init (&string_stack); |
520a57c8 ZW |
71 | } |
72 | ||
2a967f3d NB |
73 | /* Allocate a hash node. */ |
74 | static hashnode | |
46c5ad27 | 75 | alloc_node (hash_table *table ATTRIBUTE_UNUSED) |
520a57c8 | 76 | { |
2a967f3d | 77 | return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE)); |
520a57c8 ZW |
78 | } |
79 | ||
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. */ | |
85 | ||
86 | const char * | |
46c5ad27 | 87 | ggc_alloc_string (const char *contents, int length) |
520a57c8 | 88 | { |
520a57c8 ZW |
89 | if (length == -1) |
90 | length = strlen (contents); | |
91 | ||
92 | if (length == 0) | |
93 | return empty_string; | |
0df6c2c7 | 94 | if (length == 1 && ISDIGIT (contents[0])) |
a8a05998 | 95 | return digit_string (contents[0] - '0'); |
520a57c8 | 96 | |
4c521bad NB |
97 | obstack_grow0 (&string_stack, contents, length); |
98 | return obstack_finish (&string_stack); | |
520a57c8 ZW |
99 | } |
100 | ||
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. */ | |
2a967f3d | 104 | |
7bb3fbbb RS |
105 | #undef get_identifier |
106 | ||
520a57c8 | 107 | tree |
46c5ad27 | 108 | get_identifier (const char *text) |
520a57c8 | 109 | { |
2a967f3d NB |
110 | hashnode ht_node = ht_lookup (ident_hash, |
111 | (const unsigned char *) text, | |
112 | strlen (text), HT_ALLOC); | |
4c521bad | 113 | |
2a967f3d NB |
114 | /* ht_node can't be NULL here. */ |
115 | return HT_IDENT_TO_GCC_IDENT (ht_node); | |
520a57c8 ZW |
116 | } |
117 | ||
4bad9e39 APB |
118 | /* Identical to get_identifier, except that the length is assumed |
119 | known. */ | |
786de7eb | 120 | |
4bad9e39 | 121 | tree |
7bb3fbbb | 122 | get_identifier_with_length (const char *text, size_t length) |
4bad9e39 APB |
123 | { |
124 | hashnode ht_node = ht_lookup (ident_hash, | |
125 | (const unsigned char *) text, | |
126 | length, HT_ALLOC); | |
127 | ||
128 | /* ht_node can't be NULL here. */ | |
129 | return HT_IDENT_TO_GCC_IDENT (ht_node); | |
130 | } | |
131 | ||
520a57c8 ZW |
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. */ | |
135 | ||
136 | tree | |
46c5ad27 | 137 | maybe_get_identifier (const char *text) |
520a57c8 | 138 | { |
2a967f3d | 139 | hashnode ht_node; |
520a57c8 | 140 | |
2a967f3d | 141 | ht_node = ht_lookup (ident_hash, (const unsigned char *) text, |
083e9f92 | 142 | strlen (text), HT_NO_INSERT); |
2a967f3d | 143 | if (ht_node) |
083e9f92 | 144 | return HT_IDENT_TO_GCC_IDENT (ht_node); |
520a57c8 | 145 | |
4c521bad | 146 | return NULL_TREE; |
71b7be38 ZW |
147 | } |
148 | ||
520a57c8 ZW |
149 | /* Report some basic statistics about the string pool. */ |
150 | ||
151 | void | |
46c5ad27 | 152 | stringpool_statistics (void) |
520a57c8 | 153 | { |
2a967f3d | 154 | ht_dump_statistics (ident_hash); |
520a57c8 | 155 | } |
4bb4ae96 | 156 | \f |
2a967f3d | 157 | /* Mark an identifier for GC. */ |
520a57c8 | 158 | |
2a967f3d | 159 | static int |
46c5ad27 AJ |
160 | mark_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h, |
161 | const void *v ATTRIBUTE_UNUSED) | |
520a57c8 | 162 | { |
17211ab5 | 163 | gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h)); |
2a967f3d NB |
164 | return 1; |
165 | } | |
520a57c8 | 166 | |
17211ab5 GK |
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. */ | |
2a967f3d | 170 | |
17211ab5 | 171 | void |
46c5ad27 | 172 | ggc_mark_stringpool (void) |
2a967f3d NB |
173 | { |
174 | ht_forall (ident_hash, mark_ident, NULL); | |
520a57c8 | 175 | } |
17211ab5 GK |
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 | |
46c5ad27 | 182 | gt_ggc_m_S (void *x ATTRIBUTE_UNUSED) |
17211ab5 GK |
183 | { |
184 | } | |
185 | ||
186 | /* Pointer-walking routine for strings (not very interesting, since | |
187 | strings don't contain pointers). */ | |
188 | ||
189 | void | |
46c5ad27 AJ |
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) | |
17211ab5 GK |
193 | { |
194 | } | |
195 | ||
196 | /* PCH pointer-walking routine for strings. */ | |
197 | ||
198 | void | |
46c5ad27 | 199 | gt_pch_n_S (const void *x) |
17211ab5 | 200 | { |
08cee789 DJ |
201 | gt_pch_note_object ((void *)x, (void *)x, >_pch_p_S, |
202 | gt_types_enum_last); | |
17211ab5 | 203 | } |
4bb4ae96 | 204 | \f |
17211ab5 GK |
205 | /* Handle saving and restoring the string pool for PCH. */ |
206 | ||
4bb4ae96 GK |
207 | /* SPD is saved in the PCH file and holds the information needed |
208 | to restore the string pool. */ | |
209 | ||
17211ab5 GK |
210 | struct string_pool_data GTY(()) |
211 | { | |
b453c95f GK |
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; | |
17211ab5 GK |
217 | unsigned int nslots; |
218 | unsigned int nelements; | |
219 | }; | |
220 | ||
221 | static GTY(()) struct string_pool_data * spd; | |
222 | ||
d8044160 | 223 | /* Save the stringpool data in SPD. */ |
4bb4ae96 | 224 | |
17211ab5 | 225 | void |
46c5ad27 | 226 | gt_pch_save_stringpool (void) |
17211ab5 | 227 | { |
17211ab5 GK |
228 | spd = ggc_alloc (sizeof (*spd)); |
229 | spd->nslots = ident_hash->nslots; | |
230 | spd->nelements = ident_hash->nelements; | |
b453c95f GK |
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])); | |
d24ecd21 MA |
234 | } |
235 | ||
4bb4ae96 GK |
236 | /* Return the stringpool to its state before gt_pch_save_stringpool |
237 | was called. */ | |
238 | ||
d24ecd21 | 239 | void |
46c5ad27 | 240 | gt_pch_fixup_stringpool (void) |
d24ecd21 | 241 | { |
17211ab5 GK |
242 | } |
243 | ||
4bb4ae96 | 244 | /* A PCH file has been restored, which loaded SPD; fill the real hash table |
b453c95f | 245 | from SPD. */ |
4bb4ae96 | 246 | |
17211ab5 | 247 | void |
46c5ad27 | 248 | gt_pch_restore_stringpool (void) |
17211ab5 | 249 | { |
b453c95f | 250 | ht_load (ident_hash, spd->entries, spd->nslots, spd->nelements, false); |
17211ab5 GK |
251 | spd = NULL; |
252 | } | |
253 | ||
254 | #include "gt-stringpool.h" |