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)
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. */
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
*,
41 /* Maintain global roots that are preserved during GC. */
43 /* Global roots that are preserved during calls to gc. */
47 struct ggc_root
*next
;
51 void (*cb
) PROTO ((void *));
54 static struct ggc_root
*roots
;
56 /* Type-correct function to pass to ggc_add_root. It just forwards
57 *ELT (which is an rtx) to ggc_mark_tree_varray. */
60 ggc_mark_rtx_ptr (elt
)
63 ggc_mark_rtx (*(rtx
*)elt
);
66 /* Type-correct function to pass to ggc_add_root. It just forwards
67 *ELT (which is a tree) to ggc_mark_tree. */
70 ggc_mark_tree_ptr (elt
)
73 ggc_mark_tree (*(tree
*)elt
);
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. */
80 ggc_mark_tree_varray_ptr (elt
)
83 ggc_mark_tree_varray (*(varray_type
*)elt
);
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. */
91 ggc_mark_tree_hash_table_ptr (elt
)
94 ggc_mark_tree_hash_table (*(struct hash_table
**) elt
);
98 ggc_mark_string_ptr (elt
)
101 ggc_mark_string (*(char **)elt
);
105 ggc_add_root (base
, nelt
, size
, cb
)
108 void (*cb
) PROTO ((void *));
110 struct ggc_root
*x
= (struct ggc_root
*) xmalloc (sizeof (*x
));
122 ggc_add_rtx_root (base
, nelt
)
126 ggc_add_root (base
, nelt
, sizeof(rtx
), ggc_mark_rtx_ptr
);
130 ggc_add_tree_root (base
, nelt
)
134 ggc_add_root (base
, nelt
, sizeof(tree
), ggc_mark_tree_ptr
);
137 /* Add V (a varray full of trees) to the list of GC roots. */
140 ggc_add_tree_varray_root (base
, nelt
)
144 ggc_add_root (base
, nelt
, sizeof (varray_type
),
145 ggc_mark_tree_varray_ptr
);
148 /* Add HT (a hash-table where ever key is a tree) to the list of GC
152 ggc_add_tree_hash_table_root (base
, nelt
)
153 struct hash_table
**base
;
156 ggc_add_root (base
, nelt
, sizeof (struct hash_table
*),
157 ggc_mark_tree_hash_table_ptr
);
161 ggc_add_string_root (base
, nelt
)
165 ggc_add_root (base
, nelt
, sizeof (char *), ggc_mark_string_ptr
);
173 struct ggc_root
*x
, **p
;
175 p
= &roots
, x
= roots
;
196 for (x
= roots
; x
!= NULL
; x
= x
->next
)
199 int s
= x
->size
, n
= x
->nelt
;
200 void (*cb
) PROTO ((void *)) = x
->cb
;
203 for (i
= 0; i
< n
; ++i
, elt
+= s
)
209 ggc_mark_rtx_children (r
)
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
))
220 ggc_mark_rtx (JUMP_LABEL (r
));
223 ggc_mark_rtx (LABEL_REFS (r
));
226 ggc_mark_rtx (LABEL_NEXTREF (r
));
227 ggc_mark_rtx (CONTAINING_INSN (r
));
230 ggc_mark_tree (ADDRESSOF_DECL (r
));
233 ggc_mark_rtx (CONST_DOUBLE_CHAIN (r
));
236 switch (NOTE_LINE_NUMBER (r
))
238 case NOTE_INSN_RANGE_START
:
239 case NOTE_INSN_RANGE_END
:
241 ggc_mark_rtx (NOTE_RANGE_INFO (r
));
244 case NOTE_INSN_BLOCK_BEG
:
245 case NOTE_INSN_BLOCK_END
:
246 ggc_mark_tree (NOTE_BLOCK (r
));
250 if (NOTE_LINE_NUMBER (r
) >= 0)
251 ggc_mark_string (NOTE_SOURCE_FILE (r
));
260 for (fmt
= GET_RTX_FORMAT (GET_CODE (r
)), i
= 0; *fmt
; ++fmt
, ++i
)
265 ggc_mark_rtx (XEXP (r
, i
));
268 ggc_mark_rtvec (XVEC (r
, i
));
271 ggc_mark_if_gcable (XSTR (r
, i
));
278 ggc_mark_rtvec_children (v
)
283 i
= GET_NUM_ELEM (v
);
285 ggc_mark_rtx (RTVEC_ELT (v
, i
));
289 ggc_mark_tree_children (t
)
292 /* Bits from common. */
293 ggc_mark_tree (TREE_TYPE (t
));
294 ggc_mark_tree (TREE_CHAIN (t
));
296 /* Some nodes require special handling. */
297 switch (TREE_CODE (t
))
300 ggc_mark_tree (TREE_PURPOSE (t
));
301 ggc_mark_tree (TREE_VALUE (t
));
306 int i
= TREE_VEC_LENGTH (t
);
308 ggc_mark_tree (TREE_VEC_ELT (t
, i
));
313 ggc_mark_tree (TREE_OPERAND (t
, 0));
314 ggc_mark_tree (SAVE_EXPR_CONTEXT (t
));
315 ggc_mark_rtx (SAVE_EXPR_RTL (t
));
319 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t
));
320 ggc_mark_rtx (RTL_EXPR_RTL (t
));
324 ggc_mark_tree (TREE_OPERAND (t
, 0));
325 ggc_mark_tree (TREE_OPERAND (t
, 1));
326 ggc_mark_rtx (CALL_EXPR_RTL (t
));
330 ggc_mark_tree (TREE_REALPART (t
));
331 ggc_mark_tree (TREE_IMAGPART (t
));
335 ggc_mark_string (TREE_STRING_POINTER (t
));
339 ggc_mark_rtx (DECL_INCOMING_RTL (t
));
342 case IDENTIFIER_NODE
:
343 ggc_mark_string (IDENTIFIER_POINTER (t
));
351 /* But in general we can handle them by class. */
352 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
354 case 'd': /* A decl node. */
355 ggc_mark_string (DECL_SOURCE_FILE (t
));
356 ggc_mark_tree (DECL_SIZE (t
));
357 ggc_mark_tree (DECL_NAME (t
));
358 ggc_mark_tree (DECL_CONTEXT (t
));
359 ggc_mark_tree (DECL_ARGUMENTS (t
));
360 ggc_mark_tree (DECL_RESULT (t
));
361 ggc_mark_tree (DECL_INITIAL (t
));
362 ggc_mark_tree (DECL_ABSTRACT_ORIGIN (t
));
363 ggc_mark_tree (DECL_ASSEMBLER_NAME (t
));
364 ggc_mark_tree (DECL_SECTION_NAME (t
));
365 ggc_mark_tree (DECL_MACHINE_ATTRIBUTES (t
));
366 ggc_mark_rtx (DECL_RTL (t
));
367 ggc_mark_rtx (DECL_LIVE_RANGE_RTL (t
));
368 ggc_mark_tree (DECL_VINDEX (t
));
372 case 't': /* A type node. */
373 ggc_mark_tree (TYPE_SIZE (t
));
374 ggc_mark_tree (TYPE_SIZE_UNIT (t
));
375 ggc_mark_tree (TYPE_ATTRIBUTES (t
));
376 ggc_mark_tree (TYPE_VALUES (t
));
377 ggc_mark_tree (TYPE_POINTER_TO (t
));
378 ggc_mark_tree (TYPE_REFERENCE_TO (t
));
379 ggc_mark_tree (TYPE_NAME (t
));
380 ggc_mark_tree (TYPE_MIN_VALUE (t
));
381 ggc_mark_tree (TYPE_MAX_VALUE (t
));
382 ggc_mark_tree (TYPE_NEXT_VARIANT (t
));
383 ggc_mark_tree (TYPE_MAIN_VARIANT (t
));
384 ggc_mark_tree (TYPE_BINFO (t
));
385 ggc_mark_tree (TYPE_NONCOPIED_PARTS (t
));
386 ggc_mark_tree (TYPE_CONTEXT (t
));
390 case 'b': /* A lexical block. */
391 ggc_mark_tree (BLOCK_VARS (t
));
392 ggc_mark_tree (BLOCK_SUBBLOCKS (t
));
393 ggc_mark_tree (BLOCK_SUPERCONTEXT (t
));
394 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t
));
397 case 'c': /* A constant. */
398 ggc_mark_rtx (TREE_CST_RTL (t
));
401 case 'r': case '<': case '1':
402 case '2': case 'e': case 's': /* Expressions. */
404 int i
= tree_code_length
[TREE_CODE (t
)];
406 ggc_mark_tree (TREE_OPERAND (t
, i
));
416 /* Mark all the elements of the varray V, which contains trees. */
419 ggc_mark_tree_varray (v
)
425 for (i
= v
->num_elements
- 1; i
>= 0; --i
)
426 ggc_mark_tree (VARRAY_TREE (v
, i
));
429 /* Mark the hash table-entry HE. It's key field is really a tree. */
432 ggc_mark_tree_hash_table_entry (he
, k
)
433 struct hash_entry
*he
;
434 hash_table_key k ATTRIBUTE_UNUSED
;
436 ggc_mark_tree ((tree
) he
->key
);
440 /* Mark all the elements of the hash-table H, which contains trees. */
443 ggc_mark_tree_hash_table (ht
)
444 struct hash_table
*ht
;
446 hash_traverse (ht
, ggc_mark_tree_hash_table_entry
, /*info=*/0);
449 /* Allocation wrappers. */
452 ggc_alloc_string (contents
, length
)
453 const char *contents
;
460 if (contents
== NULL
)
462 length
= strlen (contents
);
465 string
= (char *) ggc_alloc_obj (length
+ 1, 0);
466 if (contents
!= NULL
)
467 memcpy (string
, contents
, length
);