OSDN Git Service

* config/i386/i386.md (*float<SSEMODEI24:mode><X87MODEF:mode>2_1):
[pf3gnuchains/gcc-fork.git] / gcc / stringpool.c
1 /* String pool for GCC.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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 3, or (at your option) any later
10 version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
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 use cpplib's 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 "symtab.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
55 static void *
56 stringpool_ggc_alloc (size_t x)
57 {
58   return ggc_alloc (x);
59 }
60
61 /* Initialize the string pool.  */
62 void
63 init_stringpool (void)
64 {
65   /* Create with 16K (2^14) entries.  */
66   ident_hash = ht_create (14);
67   ident_hash->alloc_node = alloc_node;
68   ident_hash->alloc_subobject = stringpool_ggc_alloc;
69   gcc_obstack_init (&string_stack);
70 }
71
72 /* Allocate a hash node.  */
73 static hashnode
74 alloc_node (hash_table *table ATTRIBUTE_UNUSED)
75 {
76   return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
77 }
78
79 /* Allocate and return a string constant of length LENGTH, containing
80    CONTENTS.  If LENGTH is -1, CONTENTS is assumed to be a
81    nul-terminated string, and the length is calculated using strlen.
82    If the same string constant has been allocated before, that copy is
83    returned this time too.  */
84
85 const char *
86 ggc_alloc_string (const char *contents, int length)
87 {
88   if (length == -1)
89     length = strlen (contents);
90
91   if (length == 0)
92     return empty_string;
93   if (length == 1 && ISDIGIT (contents[0]))
94     return digit_string (contents[0] - '0');
95
96   obstack_grow0 (&string_stack, contents, length);
97   return XOBFINISH (&string_stack, const char *);
98 }
99
100 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
101    If an identifier with that name has previously been referred to,
102    the same node is returned this time.  */
103
104 #undef get_identifier
105
106 tree
107 get_identifier (const char *text)
108 {
109   hashnode ht_node = ht_lookup (ident_hash,
110                                 (const unsigned char *) text,
111                                 strlen (text), HT_ALLOC);
112
113   /* ht_node can't be NULL here.  */
114   return HT_IDENT_TO_GCC_IDENT (ht_node);
115 }
116
117 /* Identical to get_identifier, except that the length is assumed
118    known.  */
119
120 tree
121 get_identifier_with_length (const char *text, size_t length)
122 {
123   hashnode ht_node = ht_lookup (ident_hash,
124                                 (const unsigned char *) text,
125                                 length, HT_ALLOC);
126
127   /* ht_node can't be NULL here.  */
128   return HT_IDENT_TO_GCC_IDENT (ht_node);
129 }
130
131 /* If an identifier with the name TEXT (a null-terminated string) has
132    previously been referred to, return that node; otherwise return
133    NULL_TREE.  */
134
135 tree
136 maybe_get_identifier (const char *text)
137 {
138   hashnode ht_node;
139
140   ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
141                        strlen (text), HT_NO_INSERT);
142   if (ht_node)
143     return HT_IDENT_TO_GCC_IDENT (ht_node);
144
145   return NULL_TREE;
146 }
147
148 /* Report some basic statistics about the string pool.  */
149
150 void
151 stringpool_statistics (void)
152 {
153   ht_dump_statistics (ident_hash);
154 }
155 \f
156 /* Mark an identifier for GC.  */
157
158 static int
159 mark_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
160             const void *v ATTRIBUTE_UNUSED)
161 {
162   gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h));
163   return 1;
164 }
165
166 /* Mark the trees hanging off the identifier node for GGC.  These are
167    handled specially (not using gengtype) because of the special
168    treatment for strings.  */
169
170 void
171 ggc_mark_stringpool (void)
172 {
173   ht_forall (ident_hash, mark_ident, NULL);
174 }
175
176 /* Strings are _not_ GCed, but this routine exists so that a separate
177    roots table isn't needed for the few global variables that refer
178    to strings.  */
179
180 void
181 gt_ggc_m_S (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 (void *obj ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED,
190             gt_pointer_operator op ATTRIBUTE_UNUSED,
191             void *cookie ATTRIBUTE_UNUSED)
192 {
193 }
194
195 /* PCH pointer-walking routine for strings.  */
196
197 void
198 gt_pch_n_S (const void *x)
199 {
200   gt_pch_note_object ((void *)x, (void *)x, &gt_pch_p_S,
201                       gt_types_enum_last);
202 }
203 \f
204 /* Handle saving and restoring the string pool for PCH.  */
205
206 /* SPD is saved in the PCH file and holds the information needed
207    to restore the string pool.  */
208
209 struct string_pool_data GTY(())
210 {
211   struct ht_identifier * * 
212     GTY((length ("%h.nslots"),
213          nested_ptr (union tree_node, "%h ? GCC_IDENT_TO_HT_IDENT (%h) : NULL",
214                      "%h ? HT_IDENT_TO_GCC_IDENT (%h) : NULL")))
215     entries;
216   unsigned int nslots;
217   unsigned int nelements;
218 };
219
220 static GTY(()) struct string_pool_data * spd;
221
222 /* Save the stringpool data in SPD.  */
223
224 void
225 gt_pch_save_stringpool (void)
226 {
227   spd = ggc_alloc (sizeof (*spd));
228   spd->nslots = ident_hash->nslots;
229   spd->nelements = ident_hash->nelements;
230   spd->entries = ggc_alloc (sizeof (spd->entries[0]) * spd->nslots);
231   memcpy (spd->entries, ident_hash->entries,
232           spd->nslots * sizeof (spd->entries[0]));
233 }
234
235 /* Return the stringpool to its state before gt_pch_save_stringpool
236    was called.  */
237
238 void
239 gt_pch_fixup_stringpool (void)
240 {
241 }
242
243 /* A PCH file has been restored, which loaded SPD; fill the real hash table
244    from SPD.  */
245
246 void
247 gt_pch_restore_stringpool (void)
248 {
249   ht_load (ident_hash, spd->entries, spd->nslots, spd->nelements, false);
250   spd = NULL;
251 }
252
253 #include "gt-stringpool.h"