* Makefile.in (rtlanal.o): Depend on $(TM_P_H).
[official-gcc.git] / gcc / stringpool.c
blobc28156caaeeb65b8b8eee0b760c62dfbb20efc1b
1 /* String pool for GCC.
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 /* String text, identifer 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.
25 We have our own private 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. */
30 #include "config.h"
31 #include "system.h"
32 #include "ggc.h"
33 #include "tree.h"
34 #include "hashtable.h"
35 #include "toplev.h"
37 /* The "" allocated string. */
38 const char empty_string[] = "";
40 /* Character strings, each containing a single decimal digit.
41 Written this way to save space. */
42 const char digit_vector[] = {
43 '0', 0, '1', 0, '2', 0, '3', 0, '4', 0,
44 '5', 0, '6', 0, '7', 0, '8', 0, '9', 0
47 struct ht *ident_hash;
48 static struct obstack string_stack;
50 static hashnode alloc_node PARAMS ((hash_table *));
51 static int mark_ident PARAMS ((struct cpp_reader *, hashnode, const PTR));
52 static void mark_ident_hash PARAMS ((void *));
54 /* Initialize the string pool. */
55 void
56 init_stringpool ()
58 /* Create with 16K (2^14) entries. */
59 ident_hash = ht_create (14);
60 ident_hash->alloc_node = alloc_node;
61 gcc_obstack_init (&string_stack);
62 ggc_add_root (&ident_hash, 1, sizeof ident_hash, mark_ident_hash);
65 /* Allocate a hash node. */
66 static hashnode
67 alloc_node (table)
68 hash_table *table ATTRIBUTE_UNUSED;
70 return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
73 /* Allocate and return a string constant of length LENGTH, containing
74 CONTENTS. If LENGTH is -1, CONTENTS is assumed to be a
75 nul-terminated string, and the length is calculated using strlen.
76 If the same string constant has been allocated before, that copy is
77 returned this time too. */
79 const char *
80 ggc_alloc_string (contents, length)
81 const char *contents;
82 int length;
84 if (length == -1)
85 length = strlen (contents);
87 if (length == 0)
88 return empty_string;
89 if (length == 1 && contents[0] >= '0' && contents[0] <= '9')
90 return digit_string (contents[0] - '0');
92 obstack_grow0 (&string_stack, contents, length);
93 return obstack_finish (&string_stack);
96 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
97 If an identifier with that name has previously been referred to,
98 the same node is returned this time. */
100 tree
101 get_identifier (text)
102 const char *text;
104 hashnode ht_node = ht_lookup (ident_hash,
105 (const unsigned char *) text,
106 strlen (text), HT_ALLOC);
108 /* ht_node can't be NULL here. */
109 return HT_IDENT_TO_GCC_IDENT (ht_node);
112 /* Identical to get_identifier, except that the length is assumed
113 known. */
115 tree
116 get_identifier_with_length (text, length)
117 const char *text;
118 unsigned int length;
120 hashnode ht_node = ht_lookup (ident_hash,
121 (const unsigned char *) text,
122 length, HT_ALLOC);
124 /* ht_node can't be NULL here. */
125 return HT_IDENT_TO_GCC_IDENT (ht_node);
128 /* If an identifier with the name TEXT (a null-terminated string) has
129 previously been referred to, return that node; otherwise return
130 NULL_TREE. */
132 tree
133 maybe_get_identifier (text)
134 const char *text;
136 hashnode ht_node;
138 ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
139 strlen (text), HT_NO_INSERT);
140 if (ht_node)
141 return HT_IDENT_TO_GCC_IDENT (ht_node);
143 return NULL_TREE;
146 /* Record the size of an identifier node for the language in use.
147 SIZE is the total size in bytes.
148 This is called by the language-specific files. This must be
149 called before allocating any identifiers. */
151 void
152 set_identifier_size (size)
153 int size;
155 tree_code_length[(int) IDENTIFIER_NODE]
156 = (size - sizeof (struct tree_common)) / sizeof (tree);
159 /* Report some basic statistics about the string pool. */
161 void
162 stringpool_statistics ()
164 ht_dump_statistics (ident_hash);
167 /* Mark an identifier for GC. */
169 static int
170 mark_ident (pfile, h, v)
171 struct cpp_reader *pfile ATTRIBUTE_UNUSED;
172 hashnode h;
173 const PTR v ATTRIBUTE_UNUSED;
175 ggc_mark_nonnull_tree (HT_IDENT_TO_GCC_IDENT (h));
176 return 1;
179 /* Mark all identifiers for GC. */
181 static void
182 mark_ident_hash (arg)
183 PTR arg ATTRIBUTE_UNUSED;
185 ht_forall (ident_hash, mark_ident, NULL);