* cp-tree.h (struct language_function): Remove static_labelno.
[official-gcc.git] / gcc / ggc-common.c
blobcf28d5bbc0c51b0b430f80e9cd8c3188b5e06473
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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. */
21 /* Generic garbage collection (GC) functions and data, not specific to
22 any particular GC implementation. */
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"
32 static void ggc_mark_rtx_ptr PARAMS ((void *));
33 static void ggc_mark_tree_ptr PARAMS ((void *));
34 static void ggc_mark_tree_varray_ptr PARAMS ((void *));
35 static void ggc_mark_tree_hash_table_ptr PARAMS ((void *));
36 static void ggc_mark_string_ptr PARAMS ((void *));
37 static boolean ggc_mark_tree_hash_table_entry PARAMS ((struct hash_entry *,
38 hash_table_key));
40 /* Maintain global roots that are preserved during GC. */
42 /* Global roots that are preserved during calls to gc. */
44 struct ggc_root
46 struct ggc_root *next;
47 void *base;
48 int nelt;
49 int size;
50 void (*cb) PROTO ((void *));
53 static struct ggc_root *roots;
55 /* Type-correct function to pass to ggc_add_root. It just forwards
56 *ELT (which is an rtx) to ggc_mark_tree_varray. */
58 static void
59 ggc_mark_rtx_ptr (elt)
60 void *elt;
62 ggc_mark_rtx (*(rtx *)elt);
65 /* Type-correct function to pass to ggc_add_root. It just forwards
66 *ELT (which is a tree) to ggc_mark_tree. */
68 static void
69 ggc_mark_tree_ptr (elt)
70 void *elt;
72 ggc_mark_tree (*(tree *)elt);
75 /* Type-correct function to pass to ggc_add_root. It just forwards
76 ELT (which is really a varray_type *) to ggc_mark_tree_varray. */
78 static void
79 ggc_mark_tree_varray_ptr (elt)
80 void *elt;
82 ggc_mark_tree_varray (*(varray_type *)elt);
85 /* Type-correct function to pass to ggc_add_root. It just forwards
86 ELT (which is really a struct hash_table **) to
87 ggc_mark_tree_hash_table. */
89 static void
90 ggc_mark_tree_hash_table_ptr (elt)
91 void *elt;
93 ggc_mark_tree_hash_table (*(struct hash_table **) elt);
96 static void
97 ggc_mark_string_ptr (elt)
98 void *elt;
100 ggc_mark_string (*(char **)elt);
103 void
104 ggc_add_root (base, nelt, size, cb)
105 void *base;
106 int nelt, size;
107 void (*cb) PROTO ((void *));
109 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
111 x->next = roots;
112 x->base = base;
113 x->nelt = nelt;
114 x->size = size;
115 x->cb = cb;
117 roots = x;
120 void
121 ggc_add_rtx_root (base, nelt)
122 rtx *base;
123 int nelt;
125 ggc_add_root (base, nelt, sizeof(rtx), ggc_mark_rtx_ptr);
128 void
129 ggc_add_tree_root (base, nelt)
130 tree *base;
131 int nelt;
133 ggc_add_root (base, nelt, sizeof(tree), ggc_mark_tree_ptr);
136 /* Add V (a varray full of trees) to the list of GC roots. */
138 void
139 ggc_add_tree_varray_root (base, nelt)
140 varray_type *base;
141 int nelt;
143 ggc_add_root (base, nelt, sizeof (varray_type),
144 ggc_mark_tree_varray_ptr);
147 /* Add HT (a hash-table where ever key is a tree) to the list of GC
148 roots. */
150 void
151 ggc_add_tree_hash_table_root (base, nelt)
152 struct hash_table **base;
153 int nelt;
155 ggc_add_root (base, nelt, sizeof (struct hash_table *),
156 ggc_mark_tree_hash_table_ptr);
159 void
160 ggc_add_string_root (base, nelt)
161 char **base;
162 int nelt;
164 ggc_add_root (base, nelt, sizeof (char *), ggc_mark_string_ptr);
168 void
169 ggc_del_root (base)
170 void *base;
172 struct ggc_root *x, **p;
174 p = &roots, x = roots;
175 while (x)
177 if (x->base == base)
179 *p = x->next;
180 free (x);
181 return;
183 p = &x->next;
184 x = x->next;
187 abort();
190 void
191 ggc_mark_roots ()
193 struct ggc_root* x;
195 for (x = roots; x != NULL; x = x->next)
197 char *elt = x->base;
198 int s = x->size, n = x->nelt;
199 void (*cb) PROTO ((void *)) = x->cb;
200 int i;
202 for (i = 0; i < n; ++i, elt += s)
203 (*cb)(elt);
207 void
208 ggc_mark_rtx_children (r)
209 rtx r;
211 const char *fmt;
212 int i;
214 /* ??? If (some of) these are really pass-dependant info, do we have
215 any right poking our noses in? */
216 switch (GET_CODE (r))
218 case JUMP_INSN:
219 ggc_mark_rtx (JUMP_LABEL (r));
220 break;
221 case CODE_LABEL:
222 ggc_mark_rtx (LABEL_REFS (r));
223 break;
224 case LABEL_REF:
225 ggc_mark_rtx (LABEL_NEXTREF (r));
226 ggc_mark_rtx (CONTAINING_INSN (r));
227 break;
228 case ADDRESSOF:
229 ggc_mark_tree (ADDRESSOF_DECL (r));
230 break;
231 case CONST_DOUBLE:
232 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r));
233 break;
234 case NOTE:
235 switch (NOTE_LINE_NUMBER (r))
237 case NOTE_INSN_RANGE_START:
238 case NOTE_INSN_RANGE_END:
239 case NOTE_INSN_LIVE:
240 ggc_mark_rtx (NOTE_RANGE_INFO (r));
241 break;
243 case NOTE_INSN_BLOCK_BEG:
244 case NOTE_INSN_BLOCK_END:
245 ggc_mark_tree (NOTE_BLOCK (r));
246 break;
248 default:
249 if (NOTE_LINE_NUMBER (r) >= 0)
250 ggc_mark_string (NOTE_SOURCE_FILE (r));
251 break;
253 break;
255 default:
256 break;
259 for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
261 switch (*fmt)
263 case 'e': case 'u':
264 ggc_mark_rtx (XEXP (r, i));
265 break;
266 case 'V': case 'E':
267 ggc_mark_rtvec (XVEC (r, i));
268 break;
269 case 'S': case 's':
270 ggc_mark_string (XSTR (r, i));
271 break;
276 void
277 ggc_mark_rtvec (v)
278 rtvec v;
280 int i;
282 if (v == NULL || ggc_set_mark_rtvec (v))
283 return;
285 i = GET_NUM_ELEM (v);
286 while (--i >= 0)
287 ggc_mark_rtx (RTVEC_ELT (v, i));
290 void
291 ggc_mark_tree_children (t)
292 tree t;
294 /* Bits from common. */
295 ggc_mark_tree (TREE_TYPE (t));
296 ggc_mark_tree (TREE_CHAIN (t));
298 /* Some nodes require special handling. */
299 switch (TREE_CODE (t))
301 case TREE_LIST:
302 ggc_mark_tree (TREE_PURPOSE (t));
303 ggc_mark_tree (TREE_VALUE (t));
304 return;
306 case TREE_VEC:
308 int i = TREE_VEC_LENGTH (t);
309 while (--i >= 0)
310 ggc_mark_tree (TREE_VEC_ELT (t, i));
311 return;
314 case SAVE_EXPR:
315 ggc_mark_tree (TREE_OPERAND (t, 0));
316 ggc_mark_tree (SAVE_EXPR_CONTEXT (t));
317 ggc_mark_rtx (SAVE_EXPR_RTL (t));
318 return;
320 case RTL_EXPR:
321 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t));
322 ggc_mark_rtx (RTL_EXPR_RTL (t));
323 return;
325 case CALL_EXPR:
326 ggc_mark_tree (TREE_OPERAND (t, 0));
327 ggc_mark_tree (TREE_OPERAND (t, 1));
328 ggc_mark_rtx (CALL_EXPR_RTL (t));
329 return;
331 case COMPLEX_CST:
332 ggc_mark_tree (TREE_REALPART (t));
333 ggc_mark_tree (TREE_IMAGPART (t));
334 break;
336 case STRING_CST:
337 ggc_mark_string (TREE_STRING_POINTER (t));
338 break;
340 case PARM_DECL:
341 ggc_mark_rtx (DECL_INCOMING_RTL (t));
342 break;
344 case IDENTIFIER_NODE:
345 ggc_mark_string (IDENTIFIER_POINTER (t));
346 lang_mark_tree (t);
347 return;
349 default:
350 break;
353 /* But in general we can handle them by class. */
354 switch (TREE_CODE_CLASS (TREE_CODE (t)))
356 case 'd': /* A decl node. */
357 ggc_mark_string (DECL_SOURCE_FILE (t));
358 ggc_mark_tree (DECL_SIZE (t));
359 ggc_mark_tree (DECL_NAME (t));
360 ggc_mark_tree (DECL_CONTEXT (t));
361 ggc_mark_tree (DECL_ARGUMENTS (t));
362 ggc_mark_tree (DECL_RESULT (t));
363 ggc_mark_tree (DECL_INITIAL (t));
364 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t));
365 ggc_mark_tree (DECL_ASSEMBLER_NAME (t));
366 ggc_mark_tree (DECL_SECTION_NAME (t));
367 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t));
368 ggc_mark_rtx (DECL_RTL (t));
369 ggc_mark_rtx (DECL_LIVE_RANGE_RTL (t));
370 ggc_mark_tree (DECL_VINDEX (t));
371 lang_mark_tree (t);
372 break;
374 case 't': /* A type node. */
375 ggc_mark_tree (TYPE_SIZE (t));
376 ggc_mark_tree (TYPE_SIZE_UNIT (t));
377 ggc_mark_tree (TYPE_ATTRIBUTES (t));
378 ggc_mark_tree (TYPE_VALUES (t));
379 ggc_mark_tree (TYPE_POINTER_TO (t));
380 ggc_mark_tree (TYPE_REFERENCE_TO (t));
381 ggc_mark_tree (TYPE_NAME (t));
382 ggc_mark_tree (TYPE_MIN_VALUE (t));
383 ggc_mark_tree (TYPE_MAX_VALUE (t));
384 ggc_mark_tree (TYPE_NEXT_VARIANT (t));
385 ggc_mark_tree (TYPE_MAIN_VARIANT (t));
386 ggc_mark_tree (TYPE_BINFO (t));
387 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t));
388 ggc_mark_tree (TYPE_CONTEXT (t));
389 lang_mark_tree (t);
390 break;
392 case 'b': /* A lexical block. */
393 ggc_mark_tree (BLOCK_VARS (t));
394 ggc_mark_tree (BLOCK_TYPE_TAGS (t));
395 ggc_mark_tree (BLOCK_SUBBLOCKS (t));
396 ggc_mark_tree (BLOCK_SUPERCONTEXT (t));
397 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t));
398 ggc_mark_rtx (BLOCK_END_NOTE (t));
399 break;
401 case 'c': /* A constant. */
402 ggc_mark_rtx (TREE_CST_RTL (t));
403 break;
405 case 'r': case '<': case '1':
406 case '2': case 'e': case 's': /* Expressions. */
408 int i = tree_code_length[TREE_CODE (t)];
409 while (--i >= 0)
410 ggc_mark_tree (TREE_OPERAND (t, i));
411 break;
414 case 'x':
415 lang_mark_tree (t);
416 break;
420 /* Mark all the elements of the varray V, which contains trees. */
422 void
423 ggc_mark_tree_varray (v)
424 varray_type v;
426 int i;
428 if (v)
429 for (i = v->num_elements - 1; i >= 0; --i)
430 ggc_mark_tree (VARRAY_TREE (v, i));
433 /* Mark the hash table-entry HE. It's key field is really a tree. */
435 static boolean
436 ggc_mark_tree_hash_table_entry (he, k)
437 struct hash_entry *he;
438 hash_table_key k ATTRIBUTE_UNUSED;
440 ggc_mark_tree ((tree) he->key);
441 return true;
444 /* Mark all the elements of the hash-table H, which contains trees. */
446 void
447 ggc_mark_tree_hash_table (ht)
448 struct hash_table *ht;
450 hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);