]>
Commit | Line | Data |
---|---|---|
520a57c8 | 1 | /* String pool for GCC. |
dae4174e | 2 | Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008 |
a2130901 | 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 | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 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 | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
520a57c8 | 20 | |
d1f43464 TT |
21 | /* String text, identifier text and identifier node allocator. |
22 | Identifiers are uniquely stored in a hash table. | |
520a57c8 | 23 | |
4f4e53dd | 24 | We use cpplib's hash table implementation. libiberty's |
2a967f3d NB |
25 | hashtab.c is not used because it requires 100% average space |
26 | overhead per string, which is unacceptable. Also, this algorithm | |
27 | is faster. */ | |
520a57c8 ZW |
28 | |
29 | #include "config.h" | |
30 | #include "system.h" | |
4977bab6 ZW |
31 | #include "coretypes.h" |
32 | #include "tm.h" | |
520a57c8 ZW |
33 | #include "ggc.h" |
34 | #include "tree.h" | |
4f4e53dd | 35 | #include "symtab.h" |
d24ecd21 | 36 | #include "cpplib.h" |
520a57c8 ZW |
37 | |
38 | /* The "" allocated string. */ | |
39 | const char empty_string[] = ""; | |
40 | ||
a8a05998 ZW |
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 | ||
2a967f3d | 48 | struct ht *ident_hash; |
520a57c8 | 49 | |
46c5ad27 AJ |
50 | static hashnode alloc_node (hash_table *); |
51 | static int mark_ident (struct cpp_reader *, hashnode, const void *); | |
d8044160 GK |
52 | |
53 | static void * | |
54 | stringpool_ggc_alloc (size_t x) | |
55 | { | |
56 | return ggc_alloc (x); | |
57 | } | |
2a967f3d | 58 | |
520a57c8 ZW |
59 | /* Initialize the string pool. */ |
60 | void | |
46c5ad27 | 61 | init_stringpool (void) |
520a57c8 | 62 | { |
2a967f3d NB |
63 | /* Create with 16K (2^14) entries. */ |
64 | ident_hash = ht_create (14); | |
65 | ident_hash->alloc_node = alloc_node; | |
d8044160 | 66 | ident_hash->alloc_subobject = stringpool_ggc_alloc; |
520a57c8 ZW |
67 | } |
68 | ||
2a967f3d NB |
69 | /* Allocate a hash node. */ |
70 | static hashnode | |
46c5ad27 | 71 | alloc_node (hash_table *table ATTRIBUTE_UNUSED) |
520a57c8 | 72 | { |
2a967f3d | 73 | return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE)); |
520a57c8 ZW |
74 | } |
75 | ||
76 | /* Allocate and return a string constant of length LENGTH, containing | |
77 | CONTENTS. If LENGTH is -1, CONTENTS is assumed to be a | |
d1f43464 | 78 | nul-terminated string, and the length is calculated using strlen. */ |
520a57c8 ZW |
79 | |
80 | const char * | |
46c5ad27 | 81 | ggc_alloc_string (const char *contents, int length) |
520a57c8 | 82 | { |
dae4174e TT |
83 | char *result; |
84 | ||
520a57c8 ZW |
85 | if (length == -1) |
86 | length = strlen (contents); | |
87 | ||
88 | if (length == 0) | |
89 | return empty_string; | |
0df6c2c7 | 90 | if (length == 1 && ISDIGIT (contents[0])) |
a8a05998 | 91 | return digit_string (contents[0] - '0'); |
520a57c8 | 92 | |
1634b18f | 93 | result = GGC_NEWVAR (char, length + 1); |
d1f43464 TT |
94 | memcpy (result, contents, length); |
95 | result[length] = '\0'; | |
dae4174e | 96 | return (const char *) result; |
520a57c8 ZW |
97 | } |
98 | ||
99 | /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string). | |
100 | If an identifier with that name has previously been referred to, | |
101 | the same node is returned this time. */ | |
2a967f3d | 102 | |
7bb3fbbb RS |
103 | #undef get_identifier |
104 | ||
520a57c8 | 105 | tree |
46c5ad27 | 106 | get_identifier (const char *text) |
520a57c8 | 107 | { |
2a967f3d NB |
108 | hashnode ht_node = ht_lookup (ident_hash, |
109 | (const unsigned char *) text, | |
110 | strlen (text), HT_ALLOC); | |
4c521bad | 111 | |
2a967f3d NB |
112 | /* ht_node can't be NULL here. */ |
113 | return HT_IDENT_TO_GCC_IDENT (ht_node); | |
520a57c8 ZW |
114 | } |
115 | ||
4bad9e39 APB |
116 | /* Identical to get_identifier, except that the length is assumed |
117 | known. */ | |
786de7eb | 118 | |
4bad9e39 | 119 | tree |
7bb3fbbb | 120 | get_identifier_with_length (const char *text, size_t length) |
4bad9e39 APB |
121 | { |
122 | hashnode ht_node = ht_lookup (ident_hash, | |
123 | (const unsigned char *) text, | |
124 | length, HT_ALLOC); | |
125 | ||
126 | /* ht_node can't be NULL here. */ | |
127 | return HT_IDENT_TO_GCC_IDENT (ht_node); | |
128 | } | |
129 | ||
520a57c8 ZW |
130 | /* If an identifier with the name TEXT (a null-terminated string) has |
131 | previously been referred to, return that node; otherwise return | |
132 | NULL_TREE. */ | |
133 | ||
134 | tree | |
46c5ad27 | 135 | maybe_get_identifier (const char *text) |
520a57c8 | 136 | { |
2a967f3d | 137 | hashnode ht_node; |
520a57c8 | 138 | |
2a967f3d | 139 | ht_node = ht_lookup (ident_hash, (const unsigned char *) text, |
083e9f92 | 140 | strlen (text), HT_NO_INSERT); |
2a967f3d | 141 | if (ht_node) |
083e9f92 | 142 | return HT_IDENT_TO_GCC_IDENT (ht_node); |
520a57c8 | 143 | |
4c521bad | 144 | return NULL_TREE; |
71b7be38 ZW |
145 | } |
146 | ||
520a57c8 ZW |
147 | /* Report some basic statistics about the string pool. */ |
148 | ||
149 | void | |
46c5ad27 | 150 | stringpool_statistics (void) |
520a57c8 | 151 | { |
2a967f3d | 152 | ht_dump_statistics (ident_hash); |
520a57c8 | 153 | } |
4bb4ae96 | 154 | \f |
2a967f3d | 155 | /* Mark an identifier for GC. */ |
520a57c8 | 156 | |
2a967f3d | 157 | static int |
46c5ad27 AJ |
158 | mark_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h, |
159 | const void *v ATTRIBUTE_UNUSED) | |
520a57c8 | 160 | { |
17211ab5 | 161 | gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h)); |
2a967f3d NB |
162 | return 1; |
163 | } | |
520a57c8 | 164 | |
dae4174e TT |
165 | /* Return true if an identifier should be removed from the table. */ |
166 | ||
167 | static int | |
168 | maybe_delete_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h, | |
169 | const void *v ATTRIBUTE_UNUSED) | |
170 | { | |
171 | return !ggc_marked_p (HT_IDENT_TO_GCC_IDENT (h)); | |
172 | } | |
173 | ||
17211ab5 | 174 | /* Mark the trees hanging off the identifier node for GGC. These are |
dae4174e TT |
175 | handled specially (not using gengtype) because identifiers are only |
176 | roots during one part of compilation. */ | |
2a967f3d | 177 | |
17211ab5 | 178 | void |
46c5ad27 | 179 | ggc_mark_stringpool (void) |
2a967f3d NB |
180 | { |
181 | ht_forall (ident_hash, mark_ident, NULL); | |
520a57c8 | 182 | } |
17211ab5 | 183 | |
dae4174e TT |
184 | /* Purge the identifier hash of identifiers which are no longer |
185 | referenced. */ | |
17211ab5 GK |
186 | |
187 | void | |
dae4174e | 188 | ggc_purge_stringpool (void) |
17211ab5 | 189 | { |
dae4174e | 190 | ht_purge (ident_hash, maybe_delete_ident, NULL); |
17211ab5 GK |
191 | } |
192 | ||
193 | /* Pointer-walking routine for strings (not very interesting, since | |
194 | strings don't contain pointers). */ | |
195 | ||
196 | void | |
46c5ad27 AJ |
197 | gt_pch_p_S (void *obj ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED, |
198 | gt_pointer_operator op ATTRIBUTE_UNUSED, | |
199 | void *cookie ATTRIBUTE_UNUSED) | |
17211ab5 GK |
200 | { |
201 | } | |
202 | ||
203 | /* PCH pointer-walking routine for strings. */ | |
204 | ||
205 | void | |
46c5ad27 | 206 | gt_pch_n_S (const void *x) |
17211ab5 | 207 | { |
1634b18f KG |
208 | gt_pch_note_object (CONST_CAST (void *, x), CONST_CAST (void *, x), |
209 | >_pch_p_S, gt_types_enum_last); | |
17211ab5 | 210 | } |
4bb4ae96 | 211 | \f |
17211ab5 GK |
212 | /* Handle saving and restoring the string pool for PCH. */ |
213 | ||
4bb4ae96 GK |
214 | /* SPD is saved in the PCH file and holds the information needed |
215 | to restore the string pool. */ | |
216 | ||
d1b38208 | 217 | struct GTY(()) string_pool_data { |
b8698a0f | 218 | struct ht_identifier * * |
b453c95f GK |
219 | GTY((length ("%h.nslots"), |
220 | nested_ptr (union tree_node, "%h ? GCC_IDENT_TO_HT_IDENT (%h) : NULL", | |
221 | "%h ? HT_IDENT_TO_GCC_IDENT (%h) : NULL"))) | |
222 | entries; | |
17211ab5 GK |
223 | unsigned int nslots; |
224 | unsigned int nelements; | |
225 | }; | |
226 | ||
227 | static GTY(()) struct string_pool_data * spd; | |
228 | ||
d8044160 | 229 | /* Save the stringpool data in SPD. */ |
4bb4ae96 | 230 | |
17211ab5 | 231 | void |
46c5ad27 | 232 | gt_pch_save_stringpool (void) |
17211ab5 | 233 | { |
1634b18f | 234 | spd = GGC_NEW (struct string_pool_data); |
17211ab5 GK |
235 | spd->nslots = ident_hash->nslots; |
236 | spd->nelements = ident_hash->nelements; | |
1634b18f | 237 | spd->entries = GGC_NEWVEC (struct ht_identifier *, spd->nslots); |
b453c95f GK |
238 | memcpy (spd->entries, ident_hash->entries, |
239 | spd->nslots * sizeof (spd->entries[0])); | |
d24ecd21 MA |
240 | } |
241 | ||
4bb4ae96 GK |
242 | /* Return the stringpool to its state before gt_pch_save_stringpool |
243 | was called. */ | |
244 | ||
d24ecd21 | 245 | void |
46c5ad27 | 246 | gt_pch_fixup_stringpool (void) |
d24ecd21 | 247 | { |
17211ab5 GK |
248 | } |
249 | ||
4bb4ae96 | 250 | /* A PCH file has been restored, which loaded SPD; fill the real hash table |
b453c95f | 251 | from SPD. */ |
4bb4ae96 | 252 | |
17211ab5 | 253 | void |
46c5ad27 | 254 | gt_pch_restore_stringpool (void) |
17211ab5 | 255 | { |
b453c95f | 256 | ht_load (ident_hash, spd->entries, spd->nslots, spd->nelements, false); |
17211ab5 GK |
257 | spd = NULL; |
258 | } | |
259 | ||
260 | #include "gt-stringpool.h" |