OSDN Git Service

* config/i386/sol2.h (PREFERRED_DEBUGGING_TYPE): Use stabs.
[pf3gnuchains/gcc-fork.git] / gcc / ggc-common.c
1 /* Simple garbage collection for the GNU compiler.
2    Copyright (C) 1999 Free Software Foundation, Inc.
3
4    This file is part of GNU CC.
5
6    GNU CC is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GNU CC is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GNU CC; see the file COPYING.  If not, write to
18    the Free Software Foundation, 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 /* Generic garbage collection (GC) functions and data, not specific to
22    any particular GC implementation.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "ggc.h"
27 #include "hash.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "varray.h"
31
32
33 /* Maintain global roots that are preserved during GC.  */
34
35 /* Global roots that are preserved during calls to gc.  */
36
37 struct ggc_root
38 {
39   struct ggc_root *next;
40   void *base;
41   int nelt;
42   int size;
43   void (*cb) PROTO ((void *));
44 };
45
46 static struct ggc_root *roots;
47
48 /* Type-correct function to pass to ggc_add_root.  It just forwards
49    *ELT (which is an rtx) to ggc_mark_tree_varray.  */
50
51 static void
52 ggc_mark_rtx_ptr (elt)
53      void *elt;
54 {
55   ggc_mark_rtx (*(rtx *)elt);
56 }
57
58 /* Type-correct function to pass to ggc_add_root.  It just forwards
59    *ELT (which is a tree) to ggc_mark_tree.  */
60
61 static void
62 ggc_mark_tree_ptr (elt)
63      void *elt;
64 {
65   ggc_mark_tree (*(tree *)elt);
66 }
67
68 /* Type-correct function to pass to ggc_add_root.  It just forwards
69    ELT (which is really a varray_type *) to ggc_mark_tree_varray.  */
70
71 static void
72 ggc_mark_tree_varray_ptr (elt)
73      void *elt;
74 {
75   ggc_mark_tree_varray (*(varray_type *)elt);
76 }
77
78 /* Type-correct function to pass to ggc_add_root.  It just forwards
79    ELT (which is really a struct hash_table **) to
80    ggc_mark_tree_hash_table.  */
81
82 static void
83 ggc_mark_tree_hash_table_ptr (elt)
84      void *elt;
85 {
86   ggc_mark_tree_hash_table (*(struct hash_table **) elt);
87 }
88
89 static void
90 ggc_mark_string_ptr (elt)
91      void *elt;
92 {
93   ggc_mark_string (*(char **)elt);
94 }
95
96 void
97 ggc_add_root (base, nelt, size, cb)
98      void *base;
99      int nelt, size;
100      void (*cb) PROTO ((void *));
101 {
102   struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
103
104   x->next = roots;
105   x->base = base;
106   x->nelt = nelt;
107   x->size = size;
108   x->cb = cb;
109
110   roots = x;
111 }
112
113 void
114 ggc_add_rtx_root (base, nelt)
115      rtx *base;
116      int nelt;
117 {
118   ggc_add_root (base, nelt, sizeof(rtx), ggc_mark_rtx_ptr);
119 }
120
121 void
122 ggc_add_tree_root (base, nelt)
123      tree *base;
124      int nelt;
125 {
126   ggc_add_root (base, nelt, sizeof(tree), ggc_mark_tree_ptr);
127 }
128
129 /* Add V (a varray full of trees) to the list of GC roots.  */
130
131 void
132 ggc_add_tree_varray_root (base, nelt)
133      varray_type *base;
134      int nelt;
135 {
136   ggc_add_root (base, nelt, sizeof (varray_type), 
137                 ggc_mark_tree_varray_ptr);
138 }
139
140 /* Add HT (a hash-table where ever key is a tree) to the list of GC
141    roots.  */
142
143 void
144 ggc_add_tree_hash_table_root (base, nelt)
145      struct hash_table **base;
146      int nelt;
147 {
148   ggc_add_root (base, nelt, sizeof (struct hash_table *), 
149                 ggc_mark_tree_hash_table_ptr);
150 }
151
152 void
153 ggc_add_string_root (base, nelt)
154      char **base;
155      int nelt;
156 {
157   ggc_add_root (base, nelt, sizeof (char *), ggc_mark_string_ptr);
158 }
159
160
161 void
162 ggc_del_root (base)
163      void *base;
164 {
165   struct ggc_root *x, **p;
166
167   p = &roots, x = roots;
168   while (x)
169     {
170       if (x->base == base)
171         {
172           *p = x->next;
173           free (x);
174           return;
175         }
176       p = &x->next;
177       x = x->next;
178     }
179
180   abort();
181 }
182
183 void
184 ggc_mark_roots ()
185 {
186   struct ggc_root* x;
187   
188   for (x = roots; x != NULL; x = x->next)
189     {
190       char *elt = x->base;
191       int s = x->size, n = x->nelt;
192       void (*cb) PROTO ((void *)) = x->cb;
193       int i;
194
195       for (i = 0; i < n; ++i, elt += s)
196         (*cb)(elt);
197     }
198 }
199
200 void
201 ggc_mark_rtx_children (r)
202      rtx r;
203 {
204   const char *fmt;
205   int i;
206
207   /* ??? If (some of) these are really pass-dependant info, do we have
208      any right poking our noses in?  */
209   switch (GET_CODE (r))
210     {
211     case JUMP_INSN:
212       ggc_mark_rtx (JUMP_LABEL (r));
213       break;
214     case CODE_LABEL:
215       ggc_mark_rtx (LABEL_REFS (r));
216       break;
217     case LABEL_REF:
218       ggc_mark_rtx (LABEL_NEXTREF (r));
219       ggc_mark_rtx (CONTAINING_INSN (r));
220       break;
221     case ADDRESSOF:
222       ggc_mark_tree (ADDRESSOF_DECL (r));
223       break;
224     case CONST_DOUBLE:
225       ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
226       break;
227     case NOTE:
228       switch (NOTE_LINE_NUMBER (r))
229         {
230         case NOTE_INSN_RANGE_START:
231         case NOTE_INSN_RANGE_END:
232         case NOTE_INSN_LIVE:
233           ggc_mark_rtx (NOTE_RANGE_INFO (r));
234           break;
235
236         case NOTE_INSN_BLOCK_BEG:
237         case NOTE_INSN_BLOCK_END:
238           ggc_mark_tree (NOTE_BLOCK (r));
239           break;
240
241         default:
242           if (NOTE_LINE_NUMBER (r) >= 0)
243             ggc_mark_string (NOTE_SOURCE_FILE (r));
244           break;
245         }
246       break;
247
248     default:
249       break;
250     }
251
252   for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
253     {
254       switch (*fmt)
255         {
256         case 'e': case 'u':
257           ggc_mark_rtx (XEXP (r, i));
258           break;
259         case 'V': case 'E':
260           ggc_mark_rtvec (XVEC (r, i));
261           break;
262         case 'S': case 's':
263           ggc_mark_string (XSTR (r, i));
264           break;
265         }
266     }
267 }
268
269 void
270 ggc_mark_rtvec (v)
271      rtvec v;
272 {
273   int i;
274
275   if (v == NULL || ggc_set_mark_rtvec (v))
276     return;
277
278   i = GET_NUM_ELEM (v);
279   while (--i >= 0)
280     ggc_mark_rtx (RTVEC_ELT (v, i));
281 }
282
283 void
284 ggc_mark_tree_children (t)
285      tree t;
286 {
287   /* Bits from common.  */
288   ggc_mark_tree (TREE_TYPE (t));
289   ggc_mark_tree (TREE_CHAIN (t));
290
291   /* Some nodes require special handling.  */
292   switch (TREE_CODE (t))
293     {
294     case TREE_LIST:
295       ggc_mark_tree (TREE_PURPOSE (t));
296       ggc_mark_tree (TREE_VALUE (t));
297       return;
298
299     case TREE_VEC:
300       {
301         int i = TREE_VEC_LENGTH (t);
302         while (--i >= 0)
303           ggc_mark_tree (TREE_VEC_ELT (t, i));
304         return;
305       }
306
307     case SAVE_EXPR:
308       ggc_mark_tree (TREE_OPERAND (t, 0));
309       ggc_mark_tree (SAVE_EXPR_CONTEXT (t));
310       ggc_mark_rtx (SAVE_EXPR_RTL (t));
311       return;
312
313     case RTL_EXPR:
314       ggc_mark_rtx (RTL_EXPR_SEQUENCE (t));
315       ggc_mark_rtx (RTL_EXPR_RTL (t));
316       return;
317
318     case CALL_EXPR:
319       ggc_mark_tree (TREE_OPERAND (t, 0));
320       ggc_mark_tree (TREE_OPERAND (t, 1));
321       ggc_mark_rtx (CALL_EXPR_RTL (t));
322       return;
323
324     case COMPLEX_CST:
325       ggc_mark_tree (TREE_REALPART (t));
326       ggc_mark_tree (TREE_IMAGPART (t));
327       break;
328
329     case STRING_CST:
330       ggc_mark_string (TREE_STRING_POINTER (t));
331       break;
332
333     case PARM_DECL:
334       ggc_mark_rtx (DECL_INCOMING_RTL (t));
335       break;
336
337     case IDENTIFIER_NODE:
338       ggc_mark_string (IDENTIFIER_POINTER (t));
339       lang_mark_tree (t);
340       return;
341
342     default:
343       break;
344     }
345   
346   /* But in general we can handle them by class.  */
347   switch (TREE_CODE_CLASS (TREE_CODE (t)))
348     {
349     case 'd': /* A decl node.  */
350       ggc_mark_string (DECL_SOURCE_FILE (t));
351       ggc_mark_tree (DECL_SIZE (t));
352       ggc_mark_tree (DECL_NAME (t));
353       ggc_mark_tree (DECL_CONTEXT (t));
354       ggc_mark_tree (DECL_ARGUMENTS (t));
355       ggc_mark_tree (DECL_RESULT (t));
356       ggc_mark_tree (DECL_INITIAL (t));
357       ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t));
358       ggc_mark_tree (DECL_ASSEMBLER_NAME (t));
359       ggc_mark_tree (DECL_SECTION_NAME (t));
360       ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t));
361       ggc_mark_rtx (DECL_RTL (t));
362       ggc_mark_rtx (DECL_LIVE_RANGE_RTL (t));
363       ggc_mark_tree (DECL_VINDEX (t));
364       lang_mark_tree (t);
365       break;
366
367     case 't': /* A type node.  */
368       ggc_mark_tree (TYPE_SIZE (t));
369       ggc_mark_tree (TYPE_SIZE_UNIT (t));
370       ggc_mark_tree (TYPE_ATTRIBUTES (t));
371       ggc_mark_tree (TYPE_VALUES (t));
372       ggc_mark_tree (TYPE_POINTER_TO (t));
373       ggc_mark_tree (TYPE_REFERENCE_TO (t));
374       ggc_mark_tree (TYPE_NAME (t));
375       ggc_mark_tree (TYPE_MIN_VALUE (t));
376       ggc_mark_tree (TYPE_MAX_VALUE (t));
377       ggc_mark_tree (TYPE_NEXT_VARIANT (t));
378       ggc_mark_tree (TYPE_MAIN_VARIANT (t));
379       ggc_mark_tree (TYPE_BINFO (t));
380       ggc_mark_tree (TYPE_NONCOPIED_PARTS (t));
381       ggc_mark_tree (TYPE_CONTEXT (t));
382       lang_mark_tree (t);
383       break;
384
385     case 'b': /* A lexical block.  */
386       ggc_mark_tree (BLOCK_VARS (t));
387       ggc_mark_tree (BLOCK_TYPE_TAGS (t));
388       ggc_mark_tree (BLOCK_SUBBLOCKS (t));
389       ggc_mark_tree (BLOCK_SUPERCONTEXT (t));
390       ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t));
391       ggc_mark_rtx (BLOCK_END_NOTE (t));
392       break;
393
394     case 'c': /* A constant.  */
395       ggc_mark_rtx (TREE_CST_RTL (t));
396       break;
397
398     case 'r': case '<': case '1':
399     case '2': case 'e': case 's': /* Expressions.  */
400       {
401         int i = tree_code_length[TREE_CODE (t)];
402         while (--i >= 0)
403           ggc_mark_tree (TREE_OPERAND (t, i));
404         break;
405       }
406
407     case 'x':
408       lang_mark_tree (t);
409       break;
410     }
411 }
412
413 /* Mark all the elements of the varray V, which contains trees.  */
414
415 void
416 ggc_mark_tree_varray (v)
417      varray_type v;
418 {
419   int i;
420
421   if (v)
422     for (i = v->num_elements - 1; i >= 0; --i) 
423       ggc_mark_tree (VARRAY_TREE (v, i));
424 }
425
426 /* Mark the hash table-entry HE.  It's key field is really a tree.  */
427
428 static boolean
429 ggc_mark_tree_hash_table_entry (he, k)
430      struct hash_entry *he;
431      hash_table_key k ATTRIBUTE_UNUSED;
432 {
433   ggc_mark_tree ((tree) he->key);
434   return true;
435 }
436
437 /* Mark all the elements of the hash-table H, which contains trees.  */
438
439 void
440 ggc_mark_tree_hash_table (ht)
441      struct hash_table *ht;
442 {
443   hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
444 }
445