OSDN Git Service

* simplify-rtx.c (simplify_binary_operation): Revert last change.
[pf3gnuchains/gcc-fork.git] / gcc / stringpool.c
1 /* String pool for GCC.
2    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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.  */
20
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.
24
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.  */
29
30 #include "config.h"
31 #include "system.h"
32 #include "ggc.h"
33 #include "tree.h"
34 #include "hashtable.h"
35 #include "toplev.h"
36
37 /* The "" allocated string.  */
38 const char empty_string[] = "";
39
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
45 };
46
47 struct ht *ident_hash;
48 static struct obstack string_stack;
49
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 *));
53
54 /* Initialize the string pool.  */
55 void
56 init_stringpool ()
57 {
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);
63 }
64
65 /* Allocate a hash node.  */
66 static hashnode
67 alloc_node (table)
68      hash_table *table ATTRIBUTE_UNUSED;
69 {
70   return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
71 }
72
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.  */
78
79 const char *
80 ggc_alloc_string (contents, length)
81      const char *contents;
82      int length;
83 {
84   if (length == -1)
85     length = strlen (contents);
86
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');
91
92   obstack_grow0 (&string_stack, contents, length);
93   return obstack_finish (&string_stack);
94 }
95
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.  */
99
100 tree
101 get_identifier (text)
102      const char *text;
103 {
104   hashnode ht_node = ht_lookup (ident_hash,
105                                 (const unsigned char *) text,
106                                 strlen (text), HT_ALLOC);
107
108   /* ht_node can't be NULL here.  */
109   return HT_IDENT_TO_GCC_IDENT (ht_node);
110 }
111
112 /* If an identifier with the name TEXT (a null-terminated string) has
113    previously been referred to, return that node; otherwise return
114    NULL_TREE.  */
115
116 tree
117 maybe_get_identifier (text)
118      const char *text;
119 {
120   hashnode ht_node;
121
122   ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
123                        strlen (text), HT_NO_INSERT);
124   if (ht_node)
125     return HT_IDENT_TO_GCC_IDENT (ht_node);
126
127   return NULL_TREE;
128 }
129
130 /* Record the size of an identifier node for the language in use.
131    SIZE is the total size in bytes.
132    This is called by the language-specific files.  This must be
133    called before allocating any identifiers.  */
134
135 void
136 set_identifier_size (size)
137      int size;
138 {
139   tree_code_length[(int) IDENTIFIER_NODE]
140     = (size - sizeof (struct tree_common)) / sizeof (tree);
141 }
142
143 /* Report some basic statistics about the string pool.  */
144
145 void
146 stringpool_statistics ()
147 {
148   ht_dump_statistics (ident_hash);
149 }
150
151 /* Mark an identifier for GC.  */
152
153 static int
154 mark_ident (pfile, h, v)
155      struct cpp_reader *pfile ATTRIBUTE_UNUSED;
156      hashnode h;
157      const PTR v ATTRIBUTE_UNUSED;
158 {
159   ggc_mark_nonnull_tree (HT_IDENT_TO_GCC_IDENT (h));
160   return 1;
161 }
162
163 /* Mark all identifiers for GC.  */
164
165 static void
166 mark_ident_hash (arg)
167      PTR arg ATTRIBUTE_UNUSED;
168 {
169   ht_forall (ident_hash, mark_ident, NULL);
170 }