OSDN Git Service

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