Do not generate error message about unrecognised command line switches of
[official-gcc.git] / gcc / ggc-common.c
blob3aa0d998ceb525bf05c2891fd4f1f797a2b75491
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 "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "hash.h"
30 #include "varray.h"
31 #include "ggc.h"
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));
41 /* Maintain global roots that are preserved during GC. */
43 /* Global roots that are preserved during calls to gc. */
45 struct ggc_root
47 struct ggc_root *next;
48 void *base;
49 int nelt;
50 int size;
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. */
59 static void
60 ggc_mark_rtx_ptr (elt)
61 void *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. */
69 static void
70 ggc_mark_tree_ptr (elt)
71 void *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. */
79 static void
80 ggc_mark_tree_varray_ptr (elt)
81 void *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. */
90 static void
91 ggc_mark_tree_hash_table_ptr (elt)
92 void *elt;
94 ggc_mark_tree_hash_table (*(struct hash_table **) elt);
97 static void
98 ggc_mark_string_ptr (elt)
99 void *elt;
101 ggc_mark_string (*(char **)elt);
104 void
105 ggc_add_root (base, nelt, size, cb)
106 void *base;
107 int nelt, size;
108 void (*cb) PROTO ((void *));
110 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
112 x->next = roots;
113 x->base = base;
114 x->nelt = nelt;
115 x->size = size;
116 x->cb = cb;
118 roots = x;
121 void
122 ggc_add_rtx_root (base, nelt)
123 rtx *base;
124 int nelt;
126 ggc_add_root (base, nelt, sizeof(rtx), ggc_mark_rtx_ptr);
129 void
130 ggc_add_tree_root (base, nelt)
131 tree *base;
132 int 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. */
139 void
140 ggc_add_tree_varray_root (base, nelt)
141 varray_type *base;
142 int 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
149 roots. */
151 void
152 ggc_add_tree_hash_table_root (base, nelt)
153 struct hash_table **base;
154 int nelt;
156 ggc_add_root (base, nelt, sizeof (struct hash_table *),
157 ggc_mark_tree_hash_table_ptr);
160 void
161 ggc_add_string_root (base, nelt)
162 char **base;
163 int nelt;
165 ggc_add_root (base, nelt, sizeof (char *), ggc_mark_string_ptr);
169 void
170 ggc_del_root (base)
171 void *base;
173 struct ggc_root *x, **p;
175 p = &roots, x = roots;
176 while (x)
178 if (x->base == base)
180 *p = x->next;
181 free (x);
182 return;
184 p = &x->next;
185 x = x->next;
188 abort();
191 void
192 ggc_mark_roots ()
194 struct ggc_root* x;
196 for (x = roots; x != NULL; x = x->next)
198 char *elt = x->base;
199 int s = x->size, n = x->nelt;
200 void (*cb) PROTO ((void *)) = x->cb;
201 int i;
203 for (i = 0; i < n; ++i, elt += s)
204 (*cb)(elt);
208 void
209 ggc_mark_rtx_children (r)
210 rtx r;
212 const char *fmt;
213 int i;
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))
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))
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;
244 case NOTE_INSN_BLOCK_BEG:
245 case NOTE_INSN_BLOCK_END:
246 ggc_mark_tree (NOTE_BLOCK (r));
247 break;
249 default:
250 if (NOTE_LINE_NUMBER (r) >= 0)
251 ggc_mark_string (NOTE_SOURCE_FILE (r));
252 break;
254 break;
256 default:
257 break;
260 for (fmt = GET_RTX_FORMAT (GET_CODE (r)), i = 0; *fmt ; ++fmt, ++i)
262 switch (*fmt)
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_if_gcable (XSTR (r, i));
272 break;
277 void
278 ggc_mark_rtvec_children (v)
279 rtvec v;
281 int i;
283 i = GET_NUM_ELEM (v);
284 while (--i >= 0)
285 ggc_mark_rtx (RTVEC_ELT (v, i));
288 void
289 ggc_mark_tree_children (t)
290 tree 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))
299 case TREE_LIST:
300 ggc_mark_tree (TREE_PURPOSE (t));
301 ggc_mark_tree (TREE_VALUE (t));
302 return;
304 case TREE_VEC:
306 int i = TREE_VEC_LENGTH (t);
307 while (--i >= 0)
308 ggc_mark_tree (TREE_VEC_ELT (t, i));
309 return;
312 case SAVE_EXPR:
313 ggc_mark_tree (TREE_OPERAND (t, 0));
314 ggc_mark_tree (SAVE_EXPR_CONTEXT (t));
315 ggc_mark_rtx (SAVE_EXPR_RTL (t));
316 return;
318 case RTL_EXPR:
319 ggc_mark_rtx (RTL_EXPR_SEQUENCE (t));
320 ggc_mark_rtx (RTL_EXPR_RTL (t));
321 return;
323 case CALL_EXPR:
324 ggc_mark_tree (TREE_OPERAND (t, 0));
325 ggc_mark_tree (TREE_OPERAND (t, 1));
326 ggc_mark_rtx (CALL_EXPR_RTL (t));
327 return;
329 case COMPLEX_CST:
330 ggc_mark_tree (TREE_REALPART (t));
331 ggc_mark_tree (TREE_IMAGPART (t));
332 break;
334 case STRING_CST:
335 ggc_mark_string (TREE_STRING_POINTER (t));
336 break;
338 case PARM_DECL:
339 ggc_mark_rtx (DECL_INCOMING_RTL (t));
340 break;
342 case IDENTIFIER_NODE:
343 ggc_mark_string (IDENTIFIER_POINTER (t));
344 lang_mark_tree (t);
345 return;
347 default:
348 break;
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));
369 lang_mark_tree (t);
370 break;
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));
387 lang_mark_tree (t);
388 break;
390 case 'b': /* A lexical block. */
391 ggc_mark_tree (BLOCK_VARS (t));
392 ggc_mark_tree (BLOCK_TYPE_TAGS (t));
393 ggc_mark_tree (BLOCK_SUBBLOCKS (t));
394 ggc_mark_tree (BLOCK_SUPERCONTEXT (t));
395 ggc_mark_tree (BLOCK_ABSTRACT_ORIGIN (t));
396 ggc_mark_rtx (BLOCK_END_NOTE (t));
397 break;
399 case 'c': /* A constant. */
400 ggc_mark_rtx (TREE_CST_RTL (t));
401 break;
403 case 'r': case '<': case '1':
404 case '2': case 'e': case 's': /* Expressions. */
406 int i = tree_code_length[TREE_CODE (t)];
407 while (--i >= 0)
408 ggc_mark_tree (TREE_OPERAND (t, i));
409 break;
412 case 'x':
413 lang_mark_tree (t);
414 break;
418 /* Mark all the elements of the varray V, which contains trees. */
420 void
421 ggc_mark_tree_varray (v)
422 varray_type v;
424 int i;
426 if (v)
427 for (i = v->num_elements - 1; i >= 0; --i)
428 ggc_mark_tree (VARRAY_TREE (v, i));
431 /* Mark the hash table-entry HE. It's key field is really a tree. */
433 static boolean
434 ggc_mark_tree_hash_table_entry (he, k)
435 struct hash_entry *he;
436 hash_table_key k ATTRIBUTE_UNUSED;
438 ggc_mark_tree ((tree) he->key);
439 return true;
442 /* Mark all the elements of the hash-table H, which contains trees. */
444 void
445 ggc_mark_tree_hash_table (ht)
446 struct hash_table *ht;
448 hash_traverse (ht, ggc_mark_tree_hash_table_entry, /*info=*/0);
451 /* Allocation wrappers. */
453 char *
454 ggc_alloc_string (contents, length)
455 const char *contents;
456 int length;
458 char *string;
460 if (length < 0)
462 if (contents == NULL)
463 return NULL;
464 length = strlen (contents);
467 string = (char *) ggc_alloc_obj (length + 1, 0);
468 if (contents != NULL)
469 memcpy (string, contents, length);
470 string[length] = 0;
472 return string;