xref: /dragonfly/contrib/gcc-4.7/gcc/stringpool.c (revision e4b17023)
1*e4b17023SJohn Marino /* String pool for GCC.
2*e4b17023SJohn Marino    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3*e4b17023SJohn Marino    Free Software Foundation, Inc.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino /* String text, identifier text and identifier node allocator.
22*e4b17023SJohn Marino    Identifiers are uniquely stored in a hash table.
23*e4b17023SJohn Marino 
24*e4b17023SJohn Marino    We use cpplib's hash table implementation.  libiberty's
25*e4b17023SJohn Marino    hashtab.c is not used because it requires 100% average space
26*e4b17023SJohn Marino    overhead per string, which is unacceptable.  Also, this algorithm
27*e4b17023SJohn Marino    is faster.  */
28*e4b17023SJohn Marino 
29*e4b17023SJohn Marino #include "config.h"
30*e4b17023SJohn Marino #include "system.h"
31*e4b17023SJohn Marino #include "coretypes.h"
32*e4b17023SJohn Marino #include "ggc.h"
33*e4b17023SJohn Marino #include "ggc-internal.h"
34*e4b17023SJohn Marino #include "tree.h"
35*e4b17023SJohn Marino #include "symtab.h"
36*e4b17023SJohn Marino #include "cpplib.h"
37*e4b17023SJohn Marino 
38*e4b17023SJohn Marino /* The "" allocated string.  */
39*e4b17023SJohn Marino const char empty_string[] = "";
40*e4b17023SJohn Marino 
41*e4b17023SJohn Marino /* Character strings, each containing a single decimal digit.
42*e4b17023SJohn Marino    Written this way to save space.  */
43*e4b17023SJohn Marino static const char digit_vector[] = {
44*e4b17023SJohn Marino   '0', 0, '1', 0, '2', 0, '3', 0, '4', 0,
45*e4b17023SJohn Marino   '5', 0, '6', 0, '7', 0, '8', 0, '9', 0
46*e4b17023SJohn Marino };
47*e4b17023SJohn Marino 
48*e4b17023SJohn Marino #define digit_string(d) (digit_vector + ((d) * 2))
49*e4b17023SJohn Marino 
50*e4b17023SJohn Marino struct ht *ident_hash;
51*e4b17023SJohn Marino 
52*e4b17023SJohn Marino static hashnode alloc_node (hash_table *);
53*e4b17023SJohn Marino static int mark_ident (struct cpp_reader *, hashnode, const void *);
54*e4b17023SJohn Marino 
55*e4b17023SJohn Marino static void *
stringpool_ggc_alloc(size_t x)56*e4b17023SJohn Marino stringpool_ggc_alloc (size_t x)
57*e4b17023SJohn Marino {
58*e4b17023SJohn Marino   return ggc_alloc_atomic (x);
59*e4b17023SJohn Marino }
60*e4b17023SJohn Marino 
61*e4b17023SJohn Marino /* Initialize the string pool.  */
62*e4b17023SJohn Marino void
init_stringpool(void)63*e4b17023SJohn Marino init_stringpool (void)
64*e4b17023SJohn Marino {
65*e4b17023SJohn Marino   /* Create with 16K (2^14) entries.  */
66*e4b17023SJohn Marino   ident_hash = ht_create (14);
67*e4b17023SJohn Marino   ident_hash->alloc_node = alloc_node;
68*e4b17023SJohn Marino   ident_hash->alloc_subobject = stringpool_ggc_alloc;
69*e4b17023SJohn Marino }
70*e4b17023SJohn Marino 
71*e4b17023SJohn Marino /* Allocate a hash node.  */
72*e4b17023SJohn Marino static hashnode
alloc_node(hash_table * table ATTRIBUTE_UNUSED)73*e4b17023SJohn Marino alloc_node (hash_table *table ATTRIBUTE_UNUSED)
74*e4b17023SJohn Marino {
75*e4b17023SJohn Marino   return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
76*e4b17023SJohn Marino }
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino /* Allocate and return a string constant of length LENGTH, containing
79*e4b17023SJohn Marino    CONTENTS.  If LENGTH is -1, CONTENTS is assumed to be a
80*e4b17023SJohn Marino    nul-terminated string, and the length is calculated using strlen.  */
81*e4b17023SJohn Marino 
82*e4b17023SJohn Marino const char *
ggc_alloc_string_stat(const char * contents,int length MEM_STAT_DECL)83*e4b17023SJohn Marino ggc_alloc_string_stat (const char *contents, int length MEM_STAT_DECL)
84*e4b17023SJohn Marino {
85*e4b17023SJohn Marino   char *result;
86*e4b17023SJohn Marino 
87*e4b17023SJohn Marino   if (length == -1)
88*e4b17023SJohn Marino     length = strlen (contents);
89*e4b17023SJohn Marino 
90*e4b17023SJohn Marino   if (length == 0)
91*e4b17023SJohn Marino     return empty_string;
92*e4b17023SJohn Marino   if (length == 1 && ISDIGIT (contents[0]))
93*e4b17023SJohn Marino     return digit_string (contents[0] - '0');
94*e4b17023SJohn Marino 
95*e4b17023SJohn Marino   result = (char *) ggc_alloc_atomic_stat (length + 1 PASS_MEM_STAT);
96*e4b17023SJohn Marino   memcpy (result, contents, length);
97*e4b17023SJohn Marino   result[length] = '\0';
98*e4b17023SJohn Marino   return (const char *) result;
99*e4b17023SJohn Marino }
100*e4b17023SJohn Marino 
101*e4b17023SJohn Marino /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
102*e4b17023SJohn Marino    If an identifier with that name has previously been referred to,
103*e4b17023SJohn Marino    the same node is returned this time.  */
104*e4b17023SJohn Marino 
105*e4b17023SJohn Marino #undef get_identifier
106*e4b17023SJohn Marino 
107*e4b17023SJohn Marino tree
get_identifier(const char * text)108*e4b17023SJohn Marino get_identifier (const char *text)
109*e4b17023SJohn Marino {
110*e4b17023SJohn Marino   hashnode ht_node = ht_lookup (ident_hash,
111*e4b17023SJohn Marino 				(const unsigned char *) text,
112*e4b17023SJohn Marino 				strlen (text), HT_ALLOC);
113*e4b17023SJohn Marino 
114*e4b17023SJohn Marino   /* ht_node can't be NULL here.  */
115*e4b17023SJohn Marino   return HT_IDENT_TO_GCC_IDENT (ht_node);
116*e4b17023SJohn Marino }
117*e4b17023SJohn Marino 
118*e4b17023SJohn Marino /* Identical to get_identifier, except that the length is assumed
119*e4b17023SJohn Marino    known.  */
120*e4b17023SJohn Marino 
121*e4b17023SJohn Marino tree
get_identifier_with_length(const char * text,size_t length)122*e4b17023SJohn Marino get_identifier_with_length (const char *text, size_t length)
123*e4b17023SJohn Marino {
124*e4b17023SJohn Marino   hashnode ht_node = ht_lookup (ident_hash,
125*e4b17023SJohn Marino 				(const unsigned char *) text,
126*e4b17023SJohn Marino 				length, HT_ALLOC);
127*e4b17023SJohn Marino 
128*e4b17023SJohn Marino   /* ht_node can't be NULL here.  */
129*e4b17023SJohn Marino   return HT_IDENT_TO_GCC_IDENT (ht_node);
130*e4b17023SJohn Marino }
131*e4b17023SJohn Marino 
132*e4b17023SJohn Marino /* If an identifier with the name TEXT (a null-terminated string) has
133*e4b17023SJohn Marino    previously been referred to, return that node; otherwise return
134*e4b17023SJohn Marino    NULL_TREE.  */
135*e4b17023SJohn Marino 
136*e4b17023SJohn Marino tree
maybe_get_identifier(const char * text)137*e4b17023SJohn Marino maybe_get_identifier (const char *text)
138*e4b17023SJohn Marino {
139*e4b17023SJohn Marino   hashnode ht_node;
140*e4b17023SJohn Marino 
141*e4b17023SJohn Marino   ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
142*e4b17023SJohn Marino 		       strlen (text), HT_NO_INSERT);
143*e4b17023SJohn Marino   if (ht_node)
144*e4b17023SJohn Marino     return HT_IDENT_TO_GCC_IDENT (ht_node);
145*e4b17023SJohn Marino 
146*e4b17023SJohn Marino   return NULL_TREE;
147*e4b17023SJohn Marino }
148*e4b17023SJohn Marino 
149*e4b17023SJohn Marino /* Report some basic statistics about the string pool.  */
150*e4b17023SJohn Marino 
151*e4b17023SJohn Marino void
stringpool_statistics(void)152*e4b17023SJohn Marino stringpool_statistics (void)
153*e4b17023SJohn Marino {
154*e4b17023SJohn Marino   ht_dump_statistics (ident_hash);
155*e4b17023SJohn Marino }
156*e4b17023SJohn Marino 
157*e4b17023SJohn Marino /* Mark an identifier for GC.  */
158*e4b17023SJohn Marino 
159*e4b17023SJohn Marino static int
mark_ident(struct cpp_reader * pfile ATTRIBUTE_UNUSED,hashnode h,const void * v ATTRIBUTE_UNUSED)160*e4b17023SJohn Marino mark_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
161*e4b17023SJohn Marino 	    const void *v ATTRIBUTE_UNUSED)
162*e4b17023SJohn Marino {
163*e4b17023SJohn Marino   gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h));
164*e4b17023SJohn Marino   return 1;
165*e4b17023SJohn Marino }
166*e4b17023SJohn Marino 
167*e4b17023SJohn Marino /* Return true if an identifier should be removed from the table.  */
168*e4b17023SJohn Marino 
169*e4b17023SJohn Marino static int
maybe_delete_ident(struct cpp_reader * pfile ATTRIBUTE_UNUSED,hashnode h,const void * v ATTRIBUTE_UNUSED)170*e4b17023SJohn Marino maybe_delete_ident (struct cpp_reader *pfile ATTRIBUTE_UNUSED, hashnode h,
171*e4b17023SJohn Marino 		    const void *v ATTRIBUTE_UNUSED)
172*e4b17023SJohn Marino {
173*e4b17023SJohn Marino   return !ggc_marked_p (HT_IDENT_TO_GCC_IDENT (h));
174*e4b17023SJohn Marino }
175*e4b17023SJohn Marino 
176*e4b17023SJohn Marino /* Mark the trees hanging off the identifier node for GGC.  These are
177*e4b17023SJohn Marino    handled specially (not using gengtype) because identifiers are only
178*e4b17023SJohn Marino    roots during one part of compilation.  */
179*e4b17023SJohn Marino 
180*e4b17023SJohn Marino void
ggc_mark_stringpool(void)181*e4b17023SJohn Marino ggc_mark_stringpool (void)
182*e4b17023SJohn Marino {
183*e4b17023SJohn Marino   ht_forall (ident_hash, mark_ident, NULL);
184*e4b17023SJohn Marino }
185*e4b17023SJohn Marino 
186*e4b17023SJohn Marino /* Purge the identifier hash of identifiers which are no longer
187*e4b17023SJohn Marino    referenced.  */
188*e4b17023SJohn Marino 
189*e4b17023SJohn Marino void
ggc_purge_stringpool(void)190*e4b17023SJohn Marino ggc_purge_stringpool (void)
191*e4b17023SJohn Marino {
192*e4b17023SJohn Marino   ht_purge (ident_hash, maybe_delete_ident, NULL);
193*e4b17023SJohn Marino }
194*e4b17023SJohn Marino 
195*e4b17023SJohn Marino /* Pointer-walking routine for strings (not very interesting, since
196*e4b17023SJohn Marino    strings don't contain pointers).  */
197*e4b17023SJohn Marino 
198*e4b17023SJohn Marino void
gt_pch_p_S(void * obj ATTRIBUTE_UNUSED,void * x ATTRIBUTE_UNUSED,gt_pointer_operator op ATTRIBUTE_UNUSED,void * cookie ATTRIBUTE_UNUSED)199*e4b17023SJohn Marino gt_pch_p_S (void *obj ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED,
200*e4b17023SJohn Marino 	    gt_pointer_operator op ATTRIBUTE_UNUSED,
201*e4b17023SJohn Marino 	    void *cookie ATTRIBUTE_UNUSED)
202*e4b17023SJohn Marino {
203*e4b17023SJohn Marino }
204*e4b17023SJohn Marino 
205*e4b17023SJohn Marino /* PCH pointer-walking routine for strings.  */
206*e4b17023SJohn Marino 
207*e4b17023SJohn Marino void
gt_pch_n_S(const void * x)208*e4b17023SJohn Marino gt_pch_n_S (const void *x)
209*e4b17023SJohn Marino {
210*e4b17023SJohn Marino   gt_pch_note_object (CONST_CAST (void *, x), CONST_CAST (void *, x),
211*e4b17023SJohn Marino 		      &gt_pch_p_S, gt_types_enum_last);
212*e4b17023SJohn Marino }
213*e4b17023SJohn Marino 
214*e4b17023SJohn Marino /* Handle saving and restoring the string pool for PCH.  */
215*e4b17023SJohn Marino 
216*e4b17023SJohn Marino /* SPD is saved in the PCH file and holds the information needed
217*e4b17023SJohn Marino    to restore the string pool.  */
218*e4b17023SJohn Marino 
219*e4b17023SJohn Marino struct GTY(()) string_pool_data {
220*e4b17023SJohn Marino   ht_identifier_ptr *
221*e4b17023SJohn Marino     GTY((length ("%h.nslots"),
222*e4b17023SJohn Marino 	 nested_ptr (union tree_node, "%h ? GCC_IDENT_TO_HT_IDENT (%h) : NULL",
223*e4b17023SJohn Marino 		     "%h ? HT_IDENT_TO_GCC_IDENT (%h) : NULL")))
224*e4b17023SJohn Marino     entries;
225*e4b17023SJohn Marino   unsigned int nslots;
226*e4b17023SJohn Marino   unsigned int nelements;
227*e4b17023SJohn Marino };
228*e4b17023SJohn Marino 
229*e4b17023SJohn Marino static GTY(()) struct string_pool_data * spd;
230*e4b17023SJohn Marino 
231*e4b17023SJohn Marino /* Save the stringpool data in SPD.  */
232*e4b17023SJohn Marino 
233*e4b17023SJohn Marino void
gt_pch_save_stringpool(void)234*e4b17023SJohn Marino gt_pch_save_stringpool (void)
235*e4b17023SJohn Marino {
236*e4b17023SJohn Marino   spd = ggc_alloc_string_pool_data ();
237*e4b17023SJohn Marino   spd->nslots = ident_hash->nslots;
238*e4b17023SJohn Marino   spd->nelements = ident_hash->nelements;
239*e4b17023SJohn Marino   spd->entries = ggc_alloc_vec_ht_identifier_ptr (spd->nslots);
240*e4b17023SJohn Marino   memcpy (spd->entries, ident_hash->entries,
241*e4b17023SJohn Marino 	  spd->nslots * sizeof (spd->entries[0]));
242*e4b17023SJohn Marino }
243*e4b17023SJohn Marino 
244*e4b17023SJohn Marino /* Return the stringpool to its state before gt_pch_save_stringpool
245*e4b17023SJohn Marino    was called.  */
246*e4b17023SJohn Marino 
247*e4b17023SJohn Marino void
gt_pch_fixup_stringpool(void)248*e4b17023SJohn Marino gt_pch_fixup_stringpool (void)
249*e4b17023SJohn Marino {
250*e4b17023SJohn Marino }
251*e4b17023SJohn Marino 
252*e4b17023SJohn Marino /* A PCH file has been restored, which loaded SPD; fill the real hash table
253*e4b17023SJohn Marino    from SPD.  */
254*e4b17023SJohn Marino 
255*e4b17023SJohn Marino void
gt_pch_restore_stringpool(void)256*e4b17023SJohn Marino gt_pch_restore_stringpool (void)
257*e4b17023SJohn Marino {
258*e4b17023SJohn Marino   ht_load (ident_hash, spd->entries, spd->nslots, spd->nelements, false);
259*e4b17023SJohn Marino   spd = NULL;
260*e4b17023SJohn Marino }
261*e4b17023SJohn Marino 
262*e4b17023SJohn Marino #include "gt-stringpool.h"
263