1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
29 /* Nonzero iff an error has occurred. */
30 static int hit_error
= 0;
32 static void gen_rtx_next (void);
33 static void write_rtx_next (void);
34 static void open_base_files (void);
35 static void close_output_files (void);
37 /* Report an error at POS, printing MSG. */
40 error_at_line (struct fileloc
*pos
, const char *msg
, ...)
46 fprintf (stderr
, "%s:%d: ", pos
->file
, pos
->line
);
47 vfprintf (stderr
, msg
, ap
);
54 /* vasprintf, but produces fatal message on out-of-memory. */
56 xvasprintf (char **result
, const char *format
, va_list args
)
58 int ret
= vasprintf (result
, format
, args
);
59 if (*result
== NULL
|| ret
< 0)
61 fputs ("gengtype: out of memory", stderr
);
67 /* Wrapper for xvasprintf. */
69 xasprintf (const char *format
, ...)
74 va_start (ap
, format
);
75 xvasprintf (&result
, format
, ap
);
80 /* The one and only TYPE_STRING. */
82 struct type string_type
= {
83 TYPE_STRING
, NULL
, NULL
, GC_USED
, {0}
86 /* Lists of various things. */
88 static pair_p typedefs
;
89 static type_p structures
;
90 static type_p param_structs
;
91 static pair_p variables
;
93 static void do_scalar_typedef (const char *, struct fileloc
*);
94 static type_p find_param_structure
95 (type_p t
, type_p param
[NUM_PARAM
]);
96 static type_p
adjust_field_tree_exp (type_p t
, options_p opt
);
97 static type_p
adjust_field_rtx_def (type_p t
, options_p opt
);
99 /* Define S as a typedef to T at POS. */
102 do_typedef (const char *s
, type_p t
, struct fileloc
*pos
)
106 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
107 if (strcmp (p
->name
, s
) == 0)
111 error_at_line (pos
, "type `%s' previously defined", s
);
112 error_at_line (&p
->line
, "previously defined here");
117 p
= XNEW (struct pair
);
125 /* Define S as a typename of a scalar. */
128 do_scalar_typedef (const char *s
, struct fileloc
*pos
)
130 do_typedef (s
, create_scalar_type (s
, strlen (s
)), pos
);
133 /* Return the type previously defined for S. Use POS to report errors. */
136 resolve_typedef (const char *s
, struct fileloc
*pos
)
139 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
140 if (strcmp (p
->name
, s
) == 0)
142 error_at_line (pos
, "unidentified type `%s'", s
);
143 return create_scalar_type ("char", 4);
146 /* Create a new structure with tag NAME (or a union iff ISUNION is nonzero),
147 at POS with fields FIELDS and options O. */
150 new_structure (const char *name
, int isunion
, struct fileloc
*pos
,
151 pair_p fields
, options_p o
)
155 lang_bitmap bitmap
= get_base_file_bitmap (pos
->file
);
157 for (si
= structures
; si
!= NULL
; si
= si
->next
)
158 if (strcmp (name
, si
->u
.s
.tag
) == 0
159 && UNION_P (si
) == isunion
)
162 if (si
->kind
== TYPE_LANG_STRUCT
)
166 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
167 if (si
->u
.s
.bitmap
== bitmap
)
170 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
173 si
= XCNEW (struct type
);
174 memcpy (si
, ls
, sizeof (struct type
));
175 ls
->kind
= TYPE_LANG_STRUCT
;
176 ls
->u
.s
.lang_struct
= si
;
177 ls
->u
.s
.fields
= NULL
;
179 si
->pointer_to
= NULL
;
180 si
->u
.s
.lang_struct
= ls
;
185 if (ls
!= NULL
&& s
== NULL
)
187 s
= XCNEW (struct type
);
188 s
->next
= ls
->u
.s
.lang_struct
;
189 ls
->u
.s
.lang_struct
= s
;
190 s
->u
.s
.lang_struct
= ls
;
197 s
= XCNEW (struct type
);
198 s
->next
= structures
;
202 if (s
->u
.s
.line
.file
!= NULL
203 || (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
)))
205 error_at_line (pos
, "duplicate structure definition");
206 error_at_line (&s
->u
.s
.line
, "previous definition here");
209 s
->kind
= isunion
? TYPE_UNION
: TYPE_STRUCT
;
212 s
->u
.s
.fields
= fields
;
214 s
->u
.s
.bitmap
= bitmap
;
215 if (s
->u
.s
.lang_struct
)
216 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
219 /* Return the previously-defined structure with tag NAME (or a union
220 iff ISUNION is nonzero), or a new empty structure or union if none
221 was defined previously. */
224 find_structure (const char *name
, int isunion
)
228 for (s
= structures
; s
!= NULL
; s
= s
->next
)
229 if (strcmp (name
, s
->u
.s
.tag
) == 0
230 && UNION_P (s
) == isunion
)
233 s
= XCNEW (struct type
);
234 s
->next
= structures
;
236 s
->kind
= isunion
? TYPE_UNION
: TYPE_STRUCT
;
242 /* Return the previously-defined parameterized structure for structure
243 T and parameters PARAM, or a new parameterized empty structure or
244 union if none was defined previously. */
247 find_param_structure (type_p t
, type_p param
[NUM_PARAM
])
251 for (res
= param_structs
; res
; res
= res
->next
)
252 if (res
->u
.param_struct
.stru
== t
253 && memcmp (res
->u
.param_struct
.param
, param
,
254 sizeof (type_p
) * NUM_PARAM
) == 0)
258 res
= XCNEW (struct type
);
259 res
->kind
= TYPE_PARAM_STRUCT
;
260 res
->next
= param_structs
;
262 res
->u
.param_struct
.stru
= t
;
263 memcpy (res
->u
.param_struct
.param
, param
, sizeof (type_p
) * NUM_PARAM
);
268 /* Return a scalar type with name NAME. */
271 create_scalar_type (const char *name
, size_t name_len
)
273 type_p r
= XCNEW (struct type
);
274 r
->kind
= TYPE_SCALAR
;
275 r
->u
.sc
= (char *) xmemdup (name
, name_len
, name_len
+ 1);
279 /* Return a pointer to T. */
282 create_pointer (type_p t
)
286 type_p r
= XCNEW (struct type
);
287 r
->kind
= TYPE_POINTER
;
291 return t
->pointer_to
;
294 /* Return an array of length LEN. */
297 create_array (type_p t
, const char *len
)
301 v
= XCNEW (struct type
);
302 v
->kind
= TYPE_ARRAY
;
308 /* Return an options structure with name NAME and info INFO. */
310 create_option (const char *name
, void *info
)
312 options_p o
= XNEW (struct options
);
314 o
->info
= (const char*) info
;
318 /* Add a variable named S of type T with options O defined at POS,
322 note_variable (const char *s
, type_p t
, options_p o
, struct fileloc
*pos
)
325 n
= XNEW (struct pair
);
334 /* We don't care how long a CONST_DOUBLE is. */
335 #define CONST_DOUBLE_FORMAT "ww"
336 /* We don't want to see codes that are only for generator files. */
337 #undef GENERATOR_FILE
340 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
346 static const char * const rtx_name
[NUM_RTX_CODE
] = {
347 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
352 static const char * const rtx_format
[NUM_RTX_CODE
] = {
353 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
358 static int rtx_next_new
[NUM_RTX_CODE
];
360 /* We also need codes and names for insn notes (not register notes).
361 Note that we do *not* bias the note values here. */
363 #define DEF_INSN_NOTE(NAME) NAME,
364 #include "insn-notes.def"
370 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
371 default field for line number notes. */
372 static const char *const note_insn_name
[NOTE_INSN_MAX
+1] = {
373 #define DEF_INSN_NOTE(NAME) #NAME,
374 #include "insn-notes.def"
378 #undef CONST_DOUBLE_FORMAT
379 #define GENERATOR_FILE
381 /* Generate the contents of the rtx_next array. This really doesn't belong
382 in gengtype at all, but it's needed for adjust_field_rtx_def. */
388 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
392 rtx_next_new
[i
] = -1;
393 if (strncmp (rtx_format
[i
], "iuu", 3) == 0)
395 else if (i
== COND_EXEC
|| i
== SET
|| i
== EXPR_LIST
|| i
== INSN_LIST
)
398 for (k
= strlen (rtx_format
[i
]) - 1; k
>= 0; k
--)
399 if (rtx_format
[i
][k
] == 'e' || rtx_format
[i
][k
] == 'u')
404 /* Write out the contents of the rtx_next array. */
406 write_rtx_next (void)
408 outf_p f
= get_output_file_with_visibility (NULL
);
411 oprintf (f
, "\n/* Used to implement the RTX_NEXT macro. */\n");
412 oprintf (f
, "const unsigned char rtx_next[NUM_RTX_CODE] = {\n");
413 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
414 if (rtx_next_new
[i
] == -1)
415 oprintf (f
, " 0,\n");
418 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n",
423 /* Handle `special("rtx_def")'. This is a special case for field
424 `fld' of struct rtx_def, which is an array of unions whose values
425 are based in a complex way on the type of RTL. */
428 adjust_field_rtx_def (type_p t
, options_p
ARG_UNUSED (opt
))
433 type_p rtx_tp
, rtvec_tp
, tree_tp
, mem_attrs_tp
, note_union_tp
, scalar_tp
;
434 type_p bitmap_tp
, basic_block_tp
, reg_attrs_tp
;
436 if (t
->kind
!= TYPE_UNION
)
438 error_at_line (&lexer_line
,
439 "special `rtx_def' must be applied to a union");
443 nodot
= XNEW (struct options
);
448 rtx_tp
= create_pointer (find_structure ("rtx_def", 0));
449 rtvec_tp
= create_pointer (find_structure ("rtvec_def", 0));
450 tree_tp
= create_pointer (find_structure ("tree_node", 1));
451 mem_attrs_tp
= create_pointer (find_structure ("mem_attrs", 0));
452 reg_attrs_tp
= create_pointer (find_structure ("reg_attrs", 0));
453 bitmap_tp
= create_pointer (find_structure ("bitmap_element_def", 0));
454 basic_block_tp
= create_pointer (find_structure ("basic_block_def", 0));
455 scalar_tp
= create_scalar_type ("rtunion scalar", 14);
458 pair_p note_flds
= NULL
;
461 for (c
= 0; c
<= NOTE_INSN_MAX
; c
++)
463 pair_p old_note_flds
= note_flds
;
465 note_flds
= XNEW (struct pair
);
466 note_flds
->line
.file
= __FILE__
;
467 note_flds
->line
.line
= __LINE__
;
468 note_flds
->opt
= XNEW (struct options
);
469 note_flds
->opt
->next
= nodot
;
470 note_flds
->opt
->name
= "tag";
471 note_flds
->opt
->info
= note_insn_name
[c
];
472 note_flds
->next
= old_note_flds
;
476 /* NOTE_INSN_MAX is used as the default field for line
479 note_flds
->opt
->name
= "default";
480 note_flds
->name
= "rt_str";
481 note_flds
->type
= &string_type
;
484 case NOTE_INSN_BLOCK_BEG
:
485 case NOTE_INSN_BLOCK_END
:
486 note_flds
->name
= "rt_tree";
487 note_flds
->type
= tree_tp
;
490 case NOTE_INSN_EXPECTED_VALUE
:
491 case NOTE_INSN_VAR_LOCATION
:
492 note_flds
->name
= "rt_rtx";
493 note_flds
->type
= rtx_tp
;
497 note_flds
->name
= "rt_int";
498 note_flds
->type
= scalar_tp
;
502 new_structure ("rtx_def_note_subunion", 1, &lexer_line
, note_flds
, NULL
);
505 note_union_tp
= find_structure ("rtx_def_note_subunion", 1);
507 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
509 pair_p old_flds
= flds
;
510 pair_p subfields
= NULL
;
511 size_t aindex
, nmindex
;
515 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
517 pair_p old_subf
= subfields
;
521 switch (rtx_format
[i
][aindex
])
532 if (i
== MEM
&& aindex
== 1)
533 t
= mem_attrs_tp
, subname
= "rt_mem";
534 else if (i
== JUMP_INSN
&& aindex
== 9)
535 t
= rtx_tp
, subname
= "rt_rtx";
536 else if (i
== CODE_LABEL
&& aindex
== 4)
537 t
= scalar_tp
, subname
= "rt_int";
538 else if (i
== CODE_LABEL
&& aindex
== 5)
539 t
= rtx_tp
, subname
= "rt_rtx";
540 else if (i
== LABEL_REF
541 && (aindex
== 1 || aindex
== 2))
542 t
= rtx_tp
, subname
= "rt_rtx";
543 else if (i
== NOTE
&& aindex
== 4)
544 t
= note_union_tp
, subname
= "";
545 else if (i
== NOTE
&& aindex
>= 7)
546 t
= scalar_tp
, subname
= "rt_int";
547 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
548 t
= scalar_tp
, subname
= "rt_int";
549 else if (i
== VALUE
&& aindex
== 0)
550 t
= scalar_tp
, subname
= "rt_int";
551 else if (i
== REG
&& aindex
== 1)
552 t
= scalar_tp
, subname
= "rt_int";
553 else if (i
== REG
&& aindex
== 2)
554 t
= reg_attrs_tp
, subname
= "rt_reg";
555 else if (i
== SCRATCH
&& aindex
== 0)
556 t
= scalar_tp
, subname
= "rt_int";
557 else if (i
== SYMBOL_REF
&& aindex
== 1)
558 t
= scalar_tp
, subname
= "rt_int";
559 else if (i
== SYMBOL_REF
&& aindex
== 2)
560 t
= tree_tp
, subname
= "rt_tree";
561 else if (i
== BARRIER
&& aindex
>= 3)
562 t
= scalar_tp
, subname
= "rt_int";
565 error_at_line (&lexer_line
,
566 "rtx type `%s' has `0' in position %lu, can't handle",
567 rtx_name
[i
], (unsigned long) aindex
);
589 subname
= "rt_rtvec";
608 error_at_line (&lexer_line
,
609 "rtx type `%s' has `%c' in position %lu, can't handle",
610 rtx_name
[i
], rtx_format
[i
][aindex
],
611 (unsigned long)aindex
);
617 subfields
= XNEW (struct pair
);
618 subfields
->next
= old_subf
;
620 subfields
->name
= xasprintf (".fld[%lu].%s", (unsigned long)aindex
,
622 subfields
->line
.file
= __FILE__
;
623 subfields
->line
.line
= __LINE__
;
624 if (t
== note_union_tp
)
626 subfields
->opt
= XNEW (struct options
);
627 subfields
->opt
->next
= nodot
;
628 subfields
->opt
->name
= "desc";
629 subfields
->opt
->info
= "NOTE_LINE_NUMBER (&%0)";
631 else if (t
== basic_block_tp
)
633 /* We don't presently GC basic block structures... */
634 subfields
->opt
= XNEW (struct options
);
635 subfields
->opt
->next
= nodot
;
636 subfields
->opt
->name
= "skip";
637 subfields
->opt
->info
= NULL
;
640 subfields
->opt
= nodot
;
643 flds
= XNEW (struct pair
);
644 flds
->next
= old_flds
;
646 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
647 new_structure (sname
, 0, &lexer_line
, subfields
, NULL
);
648 flds
->type
= find_structure (sname
, 0);
649 flds
->line
.file
= __FILE__
;
650 flds
->line
.line
= __LINE__
;
651 flds
->opt
= XNEW (struct options
);
652 flds
->opt
->next
= nodot
;
653 flds
->opt
->name
= "tag";
654 ftag
= xstrdup (rtx_name
[i
]);
655 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
656 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
657 flds
->opt
->info
= ftag
;
660 new_structure ("rtx_def_subunion", 1, &lexer_line
, flds
, nodot
);
661 return find_structure ("rtx_def_subunion", 1);
664 /* Handle `special("tree_exp")'. This is a special case for
665 field `operands' of struct tree_exp, which although it claims to contain
666 pointers to trees, actually sometimes contains pointers to RTL too.
667 Passed T, the old type of the field, and OPT its options. Returns
668 a new type for the field. */
671 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
676 if (t
->kind
!= TYPE_ARRAY
)
678 error_at_line (&lexer_line
,
679 "special `tree_exp' must be applied to an array");
683 nodot
= XNEW (struct options
);
688 flds
= XNEW (struct pair
);
692 flds
->line
.file
= __FILE__
;
693 flds
->line
.line
= __LINE__
;
694 flds
->opt
= XNEW (struct options
);
695 flds
->opt
->next
= nodot
;
696 flds
->opt
->name
= "length";
697 flds
->opt
->info
= "TREE_CODE_LENGTH (TREE_CODE ((tree) &%0))";
699 options_p oldopt
= flds
->opt
;
700 flds
->opt
= XNEW (struct options
);
701 flds
->opt
->next
= oldopt
;
702 flds
->opt
->name
= "default";
703 flds
->opt
->info
= "";
706 new_structure ("tree_exp_subunion", 1, &lexer_line
, flds
, nodot
);
707 return find_structure ("tree_exp_subunion", 1);
710 /* Perform any special processing on a type T, about to become the type
711 of a field. Return the appropriate type for the field.
713 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
714 - Similarly for arrays of pointer-to-char;
715 - Converts structures for which a parameter is provided to
717 - Handles "special" options.
721 adjust_field_type (type_p t
, options_p opt
)
724 const int pointer_p
= t
->kind
== TYPE_POINTER
;
725 type_p params
[NUM_PARAM
];
729 for (i
= 0; i
< NUM_PARAM
; i
++)
732 for (; opt
; opt
= opt
->next
)
733 if (strcmp (opt
->name
, "length") == 0)
735 else if (strcmp (opt
->name
, "param_is") == 0
736 || (strncmp (opt
->name
, "param", 5) == 0
737 && ISDIGIT (opt
->name
[5])
738 && strcmp (opt
->name
+ 6, "_is") == 0))
740 int num
= ISDIGIT (opt
->name
[5]) ? opt
->name
[5] - '0' : 0;
742 if (! UNION_OR_STRUCT_P (t
)
743 && (t
->kind
!= TYPE_POINTER
|| ! UNION_OR_STRUCT_P (t
->u
.p
)))
745 error_at_line (&lexer_line
,
746 "option `%s' may only be applied to structures or structure pointers",
752 if (params
[num
] != NULL
)
753 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
754 if (! ISDIGIT (opt
->name
[5]))
755 params
[num
] = create_pointer ((type_p
) opt
->info
);
757 params
[num
] = (type_p
) opt
->info
;
759 else if (strcmp (opt
->name
, "special") == 0)
761 const char *special_name
= opt
->info
;
762 if (strcmp (special_name
, "tree_exp") == 0)
763 t
= adjust_field_tree_exp (t
, opt
);
764 else if (strcmp (special_name
, "rtx_def") == 0)
765 t
= adjust_field_rtx_def (t
, opt
);
767 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
776 realt
= find_param_structure (t
, params
);
777 t
= pointer_p
? create_pointer (realt
) : realt
;
782 && t
->u
.p
->kind
== TYPE_SCALAR
783 && (strcmp (t
->u
.p
->u
.sc
, "char") == 0
784 || strcmp (t
->u
.p
->u
.sc
, "unsigned char") == 0))
786 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
787 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
788 && (strcmp (t
->u
.a
.p
->u
.p
->u
.sc
, "char") == 0
789 || strcmp (t
->u
.a
.p
->u
.p
->u
.sc
, "unsigned char") == 0))
790 return create_array (&string_type
, t
->u
.a
.len
);
795 /* Create a union for YYSTYPE, as yacc would do it, given a fieldlist FIELDS
796 and information about the correspondence between token types and fields
797 in TYPEINFO. POS is used for error messages. */
800 note_yacc_type (options_p o
, pair_p fields
, pair_p typeinfo
,
806 for (p
= typeinfo
; p
; p
= p
->next
)
813 if (p
->type
== (type_p
) 1)
818 for (pp
= typeinfo
; pp
; pp
= pp
->next
)
819 if (pp
->type
!= (type_p
) 1
820 && strcmp (pp
->opt
->info
, p
->opt
->info
) == 0)
829 for (m
= fields
; m
; m
= m
->next
)
830 if (strcmp (m
->name
, p
->name
) == 0)
834 error_at_line (&p
->line
,
835 "couldn't match fieldname `%s'", p
->name
);
846 || p
->type
== (type_p
) 1)
852 new_structure ("yy_union", 1, pos
, typeinfo
, o
);
853 do_typedef ("YYSTYPE", find_structure ("yy_union", 1), pos
);
856 static void process_gc_options (options_p
, enum gc_used_enum
,
857 int *, int *, int *, type_p
*);
858 static void set_gc_used_type (type_p
, enum gc_used_enum
, type_p
*);
859 static void set_gc_used (pair_p
);
861 /* Handle OPT for set_gc_used_type. */
864 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
865 int *pass_param
, int *length
, type_p
*nested_ptr
)
868 for (o
= opt
; o
; o
= o
->next
)
869 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
)
870 set_gc_used_type ((type_p
) o
->info
, GC_POINTED_TO
, NULL
);
871 else if (strcmp (o
->name
, "maybe_undef") == 0)
873 else if (strcmp (o
->name
, "use_params") == 0)
875 else if (strcmp (o
->name
, "length") == 0)
877 else if (strcmp (o
->name
, "nested_ptr") == 0)
878 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
)->type
;
881 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
884 set_gc_used_type (type_p t
, enum gc_used_enum level
, type_p param
[NUM_PARAM
])
886 if (t
->gc_used
>= level
)
900 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
,
903 for (f
= t
->u
.s
.fields
; f
; f
= f
->next
)
908 type_p nested_ptr
= NULL
;
909 process_gc_options (f
->opt
, level
, &maybe_undef
, &pass_param
,
910 &length
, &nested_ptr
);
912 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
913 set_gc_used_type (nested_ptr
, GC_POINTED_TO
,
914 pass_param
? param
: NULL
);
915 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
916 set_gc_used_type (f
->type
->u
.p
, GC_USED
, NULL
);
917 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
918 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
, NULL
);
919 else if (pass_param
&& f
->type
->kind
== TYPE_POINTER
&& param
)
920 set_gc_used_type (find_param_structure (f
->type
->u
.p
, param
),
921 GC_POINTED_TO
, NULL
);
923 set_gc_used_type (f
->type
, GC_USED
, pass_param
? param
: NULL
);
929 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
, NULL
);
933 set_gc_used_type (t
->u
.a
.p
, GC_USED
, param
);
936 case TYPE_LANG_STRUCT
:
937 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
938 set_gc_used_type (t
, level
, param
);
941 case TYPE_PARAM_STRUCT
:
944 for (i
= 0; i
< NUM_PARAM
; i
++)
945 if (t
->u
.param_struct
.param
[i
] != 0)
946 set_gc_used_type (t
->u
.param_struct
.param
[i
], GC_USED
, NULL
);
948 if (t
->u
.param_struct
.stru
->gc_used
== GC_POINTED_TO
)
949 level
= GC_POINTED_TO
;
952 t
->u
.param_struct
.stru
->gc_used
= GC_UNUSED
;
953 set_gc_used_type (t
->u
.param_struct
.stru
, level
,
954 t
->u
.param_struct
.param
);
962 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
965 set_gc_used (pair_p variables
)
968 for (p
= variables
; p
; p
= p
->next
)
969 set_gc_used_type (p
->type
, GC_USED
, NULL
);
972 /* File mapping routines. For each input file, there is one output .c file
973 (but some output files have many input files), and there is one .h file
974 for the whole build. */
976 /* The list of output files. */
977 static outf_p output_files
;
979 /* The output header file that is included into pretty much every
981 static outf_p header_file
;
983 /* Number of files specified in gtfiles. */
984 #define NUM_GT_FILES (ARRAY_SIZE (all_files) - 1)
986 /* Number of files in the language files array. */
987 #define NUM_LANG_FILES (ARRAY_SIZE (lang_files) - 1)
989 /* Length of srcdir name. */
990 static int srcdir_len
= 0;
992 #define NUM_BASE_FILES (ARRAY_SIZE (lang_dir_names) - 1)
993 outf_p base_files
[NUM_BASE_FILES
];
995 static outf_p
create_file (const char *, const char *);
996 static const char * get_file_basename (const char *);
998 /* Create and return an outf_p for a new file for NAME, to be called
1002 create_file (const char *name
, const char *oname
)
1004 static const char *const hdr
[] = {
1005 " Copyright (C) 2004 Free Software Foundation, Inc.\n",
1007 "This file is part of GCC.\n",
1009 "GCC is free software; you can redistribute it and/or modify it under\n",
1010 "the terms of the GNU General Public License as published by the Free\n",
1011 "Software Foundation; either version 2, or (at your option) any later\n",
1014 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1015 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1016 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1017 "for more details.\n",
1019 "You should have received a copy of the GNU General Public License\n",
1020 "along with GCC; see the file COPYING. If not, write to the Free\n",
1021 "Software Foundation, 59 Temple Place - Suite 330, Boston, MA\n",
1022 "02111-1307, USA. */\n",
1024 "/* This file is machine generated. Do not edit. */\n"
1029 f
= XCNEW (struct outf
);
1030 f
->next
= output_files
;
1034 oprintf (f
, "/* Type information for %s.\n", name
);
1035 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1036 oprintf (f
, "%s", hdr
[i
]);
1040 /* Print, like fprintf, to O. */
1042 oprintf (outf_p o
, const char *format
, ...)
1048 va_start (ap
, format
);
1049 slength
= xvasprintf (&s
, format
, ap
);
1051 if (o
->bufused
+ slength
> o
->buflength
)
1053 size_t new_len
= o
->buflength
;
1058 } while (o
->bufused
+ slength
>= new_len
);
1059 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1060 o
->buflength
= new_len
;
1062 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1063 o
->bufused
+= slength
;
1068 /* Open the global header file and the language-specific header files. */
1071 open_base_files (void)
1075 header_file
= create_file ("GCC", "gtype-desc.h");
1077 for (i
= 0; i
< NUM_BASE_FILES
; i
++)
1078 base_files
[i
] = create_file (lang_dir_names
[i
],
1079 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1081 /* gtype-desc.c is a little special, so we create it here. */
1083 /* The order of files here matters very much. */
1084 static const char *const ifiles
[] = {
1085 "config.h", "system.h", "coretypes.h", "tm.h", "varray.h",
1086 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1087 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1088 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1089 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1090 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1093 const char *const *ifp
;
1094 outf_p gtype_desc_c
;
1096 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1097 for (ifp
= ifiles
; *ifp
; ifp
++)
1098 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1102 /* Determine the pathname to F relative to $(srcdir). */
1105 get_file_basename (const char *f
)
1107 const char *basename
;
1110 basename
= strrchr (f
, '/');
1117 for (i
= 1; i
< NUM_BASE_FILES
; i
++)
1123 s1
= basename
- strlen (lang_dir_names
[i
]) - 1;
1124 s2
= lang_dir_names
[i
];
1127 if (l1
>= l2
&& IS_DIR_SEPARATOR (s1
[-1]) && !memcmp (s1
, s2
, l2
))
1130 if ((basename
- f
- 1) != srcdir_len
)
1131 fatal ("filename `%s' should be preceded by $srcdir", f
);
1139 /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
1140 INPUT_FILE is used by <lang>.
1142 This function should be written to assume that a file _is_ used
1143 if the situation is unclear. If it wrongly assumes a file _is_ used,
1144 a linker error will result. If it wrongly assumes a file _is not_ used,
1145 some GC roots may be missed, which is a much harder-to-debug problem. */
1148 get_base_file_bitmap (const char *input_file
)
1150 const char *basename
= get_file_basename (input_file
);
1151 const char *slashpos
= strchr (basename
, '/');
1156 /* If the file resides in a language subdirectory (e.g., 'cp'), assume that
1157 it belongs to the corresponding language. The file may belong to other
1158 languages as well (which is checked for below). */
1163 for (i
= 1; i
< NUM_BASE_FILES
; i
++)
1164 if ((size_t)(slashpos
- basename
) == strlen (lang_dir_names
[i
])
1165 && memcmp (basename
, lang_dir_names
[i
], strlen (lang_dir_names
[i
])) == 0)
1167 /* It's in a language directory, set that language. */
1172 /* If it's in any config-lang.in, then set for the languages
1177 for (j
= 0; j
< NUM_LANG_FILES
; j
++)
1179 if (!strcmp(input_file
, lang_files
[j
]))
1181 for (k
= 0; k
< NUM_BASE_FILES
; k
++)
1183 if (!strcmp(lang_dir_names
[k
], langs_for_lang_files
[j
]))
1189 /* Otherwise, set all languages. */
1191 bitmap
= (1 << NUM_BASE_FILES
) - 1;
1196 /* An output file, suitable for definitions, that can see declarations
1197 made in INPUT_FILE and is linked into every language that uses
1201 get_output_file_with_visibility (const char *input_file
)
1205 const char *basename
;
1206 const char *for_name
;
1207 const char *output_name
;
1209 /* This can happen when we need a file with visibility on a
1210 structure that we've never seen. We have to just hope that it's
1211 globally visible. */
1212 if (input_file
== NULL
)
1213 input_file
= "system.h";
1215 /* Determine the output file name. */
1216 basename
= get_file_basename (input_file
);
1218 len
= strlen (basename
);
1219 if ((len
> 2 && memcmp (basename
+len
-2, ".c", 2) == 0)
1220 || (len
> 2 && memcmp (basename
+len
-2, ".y", 2) == 0)
1221 || (len
> 3 && memcmp (basename
+len
-3, ".in", 3) == 0))
1225 output_name
= s
= xasprintf ("gt-%s", basename
);
1226 for (; *s
!= '.'; s
++)
1227 if (! ISALNUM (*s
) && *s
!= '-')
1229 memcpy (s
, ".h", sizeof (".h"));
1230 for_name
= basename
;
1232 /* Some headers get used by more than one front-end; hence, it
1233 would be inappropriate to spew them out to a single gtype-<lang>.h
1234 (and gengtype doesn't know how to direct spewage into multiple
1235 gtype-<lang>.h headers at this time). Instead, we pair up these
1236 headers with source files (and their special purpose gt-*.h headers). */
1237 else if (strcmp (basename
, "c-common.h") == 0)
1238 output_name
= "gt-c-common.h", for_name
= "c-common.c";
1239 else if (strcmp (basename
, "c-tree.h") == 0)
1240 output_name
= "gt-c-decl.h", for_name
= "c-decl.c";
1241 else if (strncmp (basename
, "objc", 4) == 0 && IS_DIR_SEPARATOR (basename
[4])
1242 && strcmp (basename
+ 5, "objc-act.h") == 0)
1243 output_name
= "gt-objc-objc-act.h", for_name
= "objc/objc-act.c";
1248 for (i
= 0; i
< NUM_BASE_FILES
; i
++)
1249 if (memcmp (basename
, lang_dir_names
[i
], strlen (lang_dir_names
[i
])) == 0
1250 && basename
[strlen(lang_dir_names
[i
])] == '/')
1251 return base_files
[i
];
1253 output_name
= "gtype-desc.c";
1257 /* Look through to see if we've ever seen this output filename before. */
1258 for (r
= output_files
; r
; r
= r
->next
)
1259 if (strcmp (r
->name
, output_name
) == 0)
1262 /* If not, create it. */
1263 r
= create_file (for_name
, output_name
);
1268 /* The name of an output file, suitable for definitions, that can see
1269 declarations made in INPUT_FILE and is linked into every language
1270 that uses INPUT_FILE. */
1273 get_output_file_name (const char *input_file
)
1275 return get_output_file_with_visibility (input_file
)->name
;
1278 /* Copy the output to its final destination,
1279 but don't unnecessarily change modification times. */
1282 close_output_files (void)
1286 for (of
= output_files
; of
; of
= of
->next
)
1290 newfile
= fopen (of
->name
, "r");
1291 if (newfile
!= NULL
)
1296 for (i
= 0; i
< of
->bufused
; i
++)
1299 ch
= fgetc (newfile
);
1300 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
1303 no_write_p
= i
== of
->bufused
&& fgetc (newfile
) == EOF
;
1310 newfile
= fopen (of
->name
, "w");
1311 if (newfile
== NULL
)
1313 perror ("opening output file");
1316 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
1318 perror ("writing output file");
1321 if (fclose (newfile
) != 0)
1323 perror ("closing output file");
1336 struct walk_type_data
;
1338 /* For scalars and strings, given the item in 'val'.
1339 For structures, given a pointer to the item in 'val'.
1340 For misc. pointers, given the item in 'val'.
1342 typedef void (*process_field_fn
)
1343 (type_p f
, const struct walk_type_data
*p
);
1344 typedef void (*func_name_fn
)
1345 (type_p s
, const struct walk_type_data
*p
);
1347 /* Parameters for write_types. */
1349 struct write_types_data
1352 const char *param_prefix
;
1353 const char *subfield_marker_routine
;
1354 const char *marker_routine
;
1355 const char *reorder_note_routine
;
1356 const char *comment
;
1359 static void output_escaped_param (struct walk_type_data
*d
,
1360 const char *, const char *);
1361 static void output_mangled_typename (outf_p
, type_p
);
1362 static void walk_type (type_p t
, struct walk_type_data
*d
);
1363 static void write_func_for_structure
1364 (type_p orig_s
, type_p s
, type_p
* param
,
1365 const struct write_types_data
*wtd
);
1366 static void write_types_process_field
1367 (type_p f
, const struct walk_type_data
*d
);
1368 static void write_types (type_p structures
,
1369 type_p param_structs
,
1370 const struct write_types_data
*wtd
);
1371 static void write_types_local_process_field
1372 (type_p f
, const struct walk_type_data
*d
);
1373 static void write_local_func_for_structure
1374 (type_p orig_s
, type_p s
, type_p
* param
);
1375 static void write_local (type_p structures
,
1376 type_p param_structs
);
1377 static void write_enum_defn (type_p structures
, type_p param_structs
);
1378 static int contains_scalar_p (type_p t
);
1379 static void put_mangled_filename (outf_p
, const char *);
1380 static void finish_root_table (struct flist
*flp
, const char *pfx
,
1381 const char *tname
, const char *lastname
,
1383 static void write_root (outf_p
, pair_p
, type_p
, const char *, int,
1384 struct fileloc
*, const char *);
1385 static void write_array (outf_p f
, pair_p v
,
1386 const struct write_types_data
*wtd
);
1387 static void write_roots (pair_p
);
1389 /* Parameters for walk_type. */
1391 struct walk_type_data
1393 process_field_fn process_field
;
1398 const char *prev_val
[4];
1401 struct fileloc
*line
;
1406 const char *reorder_fn
;
1408 bool fn_wants_lvalue
;
1411 /* Print a mangled name representing T to OF. */
1414 output_mangled_typename (outf_p of
, type_p t
)
1418 else switch (t
->kind
)
1422 output_mangled_typename (of
, t
->u
.p
);
1432 case TYPE_LANG_STRUCT
:
1433 oprintf (of
, "%lu%s", (unsigned long) strlen (t
->u
.s
.tag
), t
->u
.s
.tag
);
1435 case TYPE_PARAM_STRUCT
:
1438 for (i
= 0; i
< NUM_PARAM
; i
++)
1439 if (t
->u
.param_struct
.param
[i
] != NULL
)
1440 output_mangled_typename (of
, t
->u
.param_struct
.param
[i
]);
1441 output_mangled_typename (of
, t
->u
.param_struct
.stru
);
1449 /* Print PARAM to D->OF processing escapes. D->VAL references the
1450 current object, D->PREV_VAL the object containing the current
1451 object, ONAME is the name of the option and D->LINE is used to
1452 print error messages. */
1455 output_escaped_param (struct walk_type_data
*d
, const char *param
,
1460 for (p
= param
; *p
; p
++)
1462 oprintf (d
->of
, "%c", *p
);
1466 oprintf (d
->of
, "(%s)", d
->prev_val
[2]);
1469 oprintf (d
->of
, "(%s)", d
->prev_val
[0]);
1472 oprintf (d
->of
, "(%s)", d
->prev_val
[1]);
1476 const char *pp
= d
->val
+ strlen (d
->val
);
1477 while (pp
[-1] == ']')
1480 oprintf (d
->of
, "%s", pp
);
1484 error_at_line (d
->line
, "`%s' option contains bad escape %c%c",
1489 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
1490 which is of type T. Write code to D->OF to constrain execution (at
1491 the point that D->PROCESS_FIELD is called) to the appropriate
1492 cases. Call D->PROCESS_FIELD on subobjects before calling it on
1493 pointers to those objects. D->PREV_VAL lists the objects
1494 containing the current object, D->OPT is a list of options to
1495 apply, D->INDENT is the current indentation level, D->LINE is used
1496 to print error messages, D->BITMAP indicates which languages to
1497 print the structure for, and D->PARAM is the current parameter
1498 (from an enclosing param_is option). */
1501 walk_type (type_p t
, struct walk_type_data
*d
)
1503 const char *length
= NULL
;
1504 const char *desc
= NULL
;
1505 int maybe_undef_p
= 0;
1506 int use_param_num
= -1;
1507 int use_params_p
= 0;
1509 const struct nested_ptr_data
*nested_ptr_d
= NULL
;
1511 d
->needs_cast_p
= false;
1512 for (oo
= d
->opt
; oo
; oo
= oo
->next
)
1513 if (strcmp (oo
->name
, "length") == 0)
1515 else if (strcmp (oo
->name
, "maybe_undef") == 0)
1517 else if (strncmp (oo
->name
, "use_param", 9) == 0
1518 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
1519 use_param_num
= oo
->name
[9] == '\0' ? 0 : oo
->name
[9] - '0';
1520 else if (strcmp (oo
->name
, "use_params") == 0)
1522 else if (strcmp (oo
->name
, "desc") == 0)
1524 else if (strcmp (oo
->name
, "nested_ptr") == 0)
1525 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
;
1526 else if (strcmp (oo
->name
, "dot") == 0)
1528 else if (strcmp (oo
->name
, "tag") == 0)
1530 else if (strcmp (oo
->name
, "special") == 0)
1532 else if (strcmp (oo
->name
, "skip") == 0)
1534 else if (strcmp (oo
->name
, "default") == 0)
1536 else if (strcmp (oo
->name
, "descbits") == 0)
1538 else if (strcmp (oo
->name
, "param_is") == 0)
1540 else if (strncmp (oo
->name
, "param", 5) == 0
1541 && ISDIGIT (oo
->name
[5])
1542 && strcmp (oo
->name
+ 6, "_is") == 0)
1544 else if (strcmp (oo
->name
, "chain_next") == 0)
1546 else if (strcmp (oo
->name
, "chain_prev") == 0)
1548 else if (strcmp (oo
->name
, "reorder") == 0)
1551 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
1558 int pointer_p
= t
->kind
== TYPE_POINTER
;
1562 if (! UNION_OR_STRUCT_P (t
))
1563 error_at_line (d
->line
, "`use_params' option on unimplemented type");
1565 t
= find_param_structure (t
, d
->param
);
1567 t
= create_pointer (t
);
1570 if (use_param_num
!= -1)
1572 if (d
->param
!= NULL
&& d
->param
[use_param_num
] != NULL
)
1574 type_p nt
= d
->param
[use_param_num
];
1576 if (t
->kind
== TYPE_ARRAY
)
1577 nt
= create_array (nt
, t
->u
.a
.len
);
1578 else if (length
!= NULL
&& t
->kind
== TYPE_POINTER
)
1579 nt
= create_pointer (nt
);
1580 d
->needs_cast_p
= (t
->kind
!= TYPE_POINTER
1581 && (nt
->kind
== TYPE_POINTER
1582 || nt
->kind
== TYPE_STRING
));
1586 error_at_line (d
->line
, "no parameter defined for `%s'",
1591 && (t
->kind
!= TYPE_POINTER
|| ! UNION_OR_STRUCT_P (t
->u
.p
)))
1593 error_at_line (d
->line
,
1594 "field `%s' has invalid option `maybe_undef_p'\n",
1603 d
->process_field (t
, d
);
1609 && t
->u
.p
->u
.s
.line
.file
== NULL
)
1611 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
1617 if (! UNION_OR_STRUCT_P (t
->u
.p
)
1618 && t
->u
.p
->kind
!= TYPE_PARAM_STRUCT
)
1620 error_at_line (d
->line
,
1621 "field `%s' is pointer to unimplemented type",
1628 const char *oldprevval2
= d
->prev_val
[2];
1630 if (! UNION_OR_STRUCT_P (nested_ptr_d
->type
))
1632 error_at_line (d
->line
,
1633 "field `%s' has invalid "
1634 "option `nested_ptr'\n",
1639 d
->prev_val
[2] = d
->val
;
1640 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
1642 d
->val
= xasprintf ("x%d", d
->counter
++);
1643 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
1644 (nested_ptr_d
->type
->kind
== TYPE_UNION
1645 ? "union" : "struct"),
1646 nested_ptr_d
->type
->u
.s
.tag
,
1647 d
->fn_wants_lvalue
? "" : "const ",
1649 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
1650 output_escaped_param (d
, nested_ptr_d
->convert_from
,
1652 oprintf (d
->of
, ";\n");
1654 d
->process_field (nested_ptr_d
->type
, d
);
1656 if (d
->fn_wants_lvalue
)
1658 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
1660 d
->prev_val
[2] = d
->val
;
1661 output_escaped_param (d
, nested_ptr_d
->convert_to
,
1663 oprintf (d
->of
, ";\n");
1667 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
1668 d
->val
= d
->prev_val
[2];
1669 d
->prev_val
[2] = oldprevval2
;
1672 d
->process_field (t
->u
.p
, d
);
1676 int loopcounter
= d
->counter
++;
1677 const char *oldval
= d
->val
;
1678 const char *oldprevval3
= d
->prev_val
[3];
1681 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
1683 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
1684 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
, "",
1685 loopcounter
, loopcounter
);
1686 output_escaped_param (d
, length
, "length");
1687 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
1689 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
1691 d
->prev_val
[3] = oldval
;
1692 walk_type (t
->u
.p
, d
);
1695 d
->prev_val
[3] = oldprevval3
;
1698 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
1699 d
->process_field(t
, d
);
1701 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
1708 int loopcounter
= d
->counter
++;
1709 const char *oldval
= d
->val
;
1712 /* If it's an array of scalars, we optimize by not generating
1714 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
1717 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
1719 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
1720 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
, "",
1721 loopcounter
, loopcounter
);
1723 output_escaped_param (d
, length
, "length");
1725 oprintf (d
->of
, "%s", t
->u
.a
.len
);
1726 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
1728 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
1730 walk_type (t
->u
.a
.p
, d
);
1735 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
1737 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
1745 const char *oldval
= d
->val
;
1746 const char *oldprevval1
= d
->prev_val
[1];
1747 const char *oldprevval2
= d
->prev_val
[2];
1748 const int union_p
= t
->kind
== TYPE_UNION
;
1749 int seen_default_p
= 0;
1752 if (! t
->u
.s
.line
.file
)
1753 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
1755 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
1757 error_at_line (d
->line
,
1758 "structure `%s' defined for mismatching languages",
1760 error_at_line (&t
->u
.s
.line
, "one structure defined here");
1763 /* Some things may also be defined in the structure's options. */
1764 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
1765 if (! desc
&& strcmp (o
->name
, "desc") == 0)
1768 d
->prev_val
[2] = oldval
;
1769 d
->prev_val
[1] = oldprevval2
;
1774 error_at_line (d
->line
, "missing `desc' option for union `%s'",
1778 oprintf (d
->of
, "%*sswitch (", d
->indent
, "");
1779 output_escaped_param (d
, desc
, "desc");
1780 oprintf (d
->of
, ")\n");
1782 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
1784 for (f
= t
->u
.s
.fields
; f
; f
= f
->next
)
1787 const char *dot
= ".";
1788 const char *tagid
= NULL
;
1791 int use_param_p
= 0;
1794 d
->reorder_fn
= NULL
;
1795 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
1796 if (strcmp (oo
->name
, "dot") == 0)
1798 else if (strcmp (oo
->name
, "tag") == 0)
1800 else if (strcmp (oo
->name
, "skip") == 0)
1802 else if (strcmp (oo
->name
, "default") == 0)
1804 else if (strcmp (oo
->name
, "reorder") == 0)
1805 d
->reorder_fn
= oo
->info
;
1806 else if (strncmp (oo
->name
, "use_param", 9) == 0
1807 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
1813 if (union_p
&& tagid
)
1815 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
1818 else if (union_p
&& default_p
)
1820 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
1824 else if (! union_p
&& (default_p
|| tagid
))
1825 error_at_line (d
->line
,
1826 "can't use `%s' outside a union on field `%s'",
1827 default_p
? "default" : "tag", f
->name
);
1828 else if (union_p
&& ! (default_p
|| tagid
)
1829 && f
->type
->kind
== TYPE_SCALAR
)
1832 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
1833 d
->line
->file
, d
->line
->line
, f
->name
);
1836 else if (union_p
&& ! (default_p
|| tagid
))
1837 error_at_line (d
->line
,
1838 "field `%s' is missing `tag' or `default' option",
1842 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
1844 d
->used_length
= false;
1846 if (union_p
&& use_param_p
&& d
->param
== NULL
)
1847 oprintf (d
->of
, "%*sgcc_unreachable ();\n", d
->indent
, "");
1849 walk_type (f
->type
, d
);
1855 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
1859 d
->reorder_fn
= NULL
;
1862 d
->prev_val
[1] = oldprevval1
;
1863 d
->prev_val
[2] = oldprevval2
;
1865 if (union_p
&& ! seen_default_p
)
1867 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
1868 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
1872 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
1878 case TYPE_LANG_STRUCT
:
1881 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
1882 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
1885 error_at_line (d
->line
, "structure `%s' differs between languages",
1892 case TYPE_PARAM_STRUCT
:
1894 type_p
*oldparam
= d
->param
;
1896 d
->param
= t
->u
.param_struct
.param
;
1897 walk_type (t
->u
.param_struct
.stru
, d
);
1898 d
->param
= oldparam
;
1907 /* process_field routine for marking routines. */
1910 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
1912 const struct write_types_data
*wtd
;
1913 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
1914 wtd
= (const struct write_types_data
*) d
->cookie
;
1919 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
1920 wtd
->subfield_marker_routine
, cast
, d
->val
);
1921 if (wtd
->param_prefix
)
1923 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
1926 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
1927 output_mangled_typename (d
->of
, d
->orig_s
);
1930 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
1932 if (f
->u
.p
->kind
== TYPE_PARAM_STRUCT
1933 && f
->u
.p
->u
.s
.line
.file
!= NULL
)
1935 oprintf (d
->of
, ", gt_e_");
1936 output_mangled_typename (d
->of
, f
);
1938 else if (UNION_OR_STRUCT_P (f
)
1939 && f
->u
.p
->u
.s
.line
.file
!= NULL
)
1941 oprintf (d
->of
, ", gt_ggc_e_");
1942 output_mangled_typename (d
->of
, f
);
1945 oprintf (d
->of
, ", gt_types_enum_last");
1947 oprintf (d
->of
, ");\n");
1948 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
1949 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
1950 wtd
->reorder_note_routine
, cast
, d
->val
,
1951 d
->prev_val
[3], d
->reorder_fn
);
1955 if (wtd
->param_prefix
== NULL
)
1960 case TYPE_LANG_STRUCT
:
1961 case TYPE_PARAM_STRUCT
:
1962 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
1963 output_mangled_typename (d
->of
, f
);
1964 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
1965 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
1966 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
1967 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
1979 /* For S, a structure that's part of ORIG_S, and using parameters
1980 PARAM, write out a routine that:
1981 - Takes a parameter, a void * but actually of type *S
1982 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
1983 field of S or its substructures and (in some cases) things
1984 that are pointed to by S.
1988 write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
1989 const struct write_types_data
*wtd
)
1991 const char *fn
= s
->u
.s
.line
.file
;
1993 const char *chain_next
= NULL
;
1994 const char *chain_prev
= NULL
;
1996 struct walk_type_data d
;
1998 /* This is a hack, and not the good kind either. */
1999 for (i
= NUM_PARAM
- 1; i
>= 0; i
--)
2000 if (param
&& param
[i
] && param
[i
]->kind
== TYPE_POINTER
2001 && UNION_OR_STRUCT_P (param
[i
]->u
.p
))
2002 fn
= param
[i
]->u
.p
->u
.s
.line
.file
;
2004 memset (&d
, 0, sizeof (d
));
2005 d
.of
= get_output_file_with_visibility (fn
);
2007 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2008 if (strcmp (opt
->name
, "chain_next") == 0)
2009 chain_next
= opt
->info
;
2010 else if (strcmp (opt
->name
, "chain_prev") == 0)
2011 chain_prev
= opt
->info
;
2013 if (chain_prev
!= NULL
&& chain_next
== NULL
)
2014 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
2016 d
.process_field
= write_types_process_field
;
2020 d
.line
= &s
->u
.s
.line
;
2021 d
.bitmap
= s
->u
.s
.bitmap
;
2023 d
.prev_val
[0] = "*x";
2024 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
2025 d
.prev_val
[3] = "x";
2028 oprintf (d
.of
, "\n");
2029 oprintf (d
.of
, "void\n");
2031 oprintf (d
.of
, "gt_%sx_%s", wtd
->prefix
, orig_s
->u
.s
.tag
);
2034 oprintf (d
.of
, "gt_%s_", wtd
->prefix
);
2035 output_mangled_typename (d
.of
, orig_s
);
2037 oprintf (d
.of
, " (void *x_p)\n");
2038 oprintf (d
.of
, "{\n");
2039 oprintf (d
.of
, " %s %s * %sx = (%s %s *)x_p;\n",
2040 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
2041 chain_next
== NULL
? "const " : "",
2042 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2043 if (chain_next
!= NULL
)
2044 oprintf (d
.of
, " %s %s * xlimit = x;\n",
2045 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2046 if (chain_next
== NULL
)
2048 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
2049 if (wtd
->param_prefix
)
2051 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
2052 output_mangled_typename (d
.of
, orig_s
);
2054 if (orig_s
->u
.p
->kind
== TYPE_PARAM_STRUCT
2055 && orig_s
->u
.p
->u
.s
.line
.file
!= NULL
)
2057 oprintf (d
.of
, ", gt_e_");
2058 output_mangled_typename (d
.of
, orig_s
);
2060 else if (UNION_OR_STRUCT_P (orig_s
)
2061 && orig_s
->u
.s
.line
.file
!= NULL
)
2063 oprintf (d
.of
, ", gt_ggc_e_");
2064 output_mangled_typename (d
.of
, orig_s
);
2067 oprintf (d
.of
, ", gt_types_enum_last");
2069 oprintf (d
.of
, "))\n");
2073 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
2074 if (wtd
->param_prefix
)
2076 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
2077 output_mangled_typename (d
.of
, orig_s
);
2079 if (orig_s
->u
.p
->kind
== TYPE_PARAM_STRUCT
2080 && orig_s
->u
.p
->u
.s
.line
.file
!= NULL
)
2082 oprintf (d
.of
, ", gt_e_");
2083 output_mangled_typename (d
.of
, orig_s
);
2085 else if (UNION_OR_STRUCT_P (orig_s
)
2086 && orig_s
->u
.s
.line
.file
!= NULL
)
2088 oprintf (d
.of
, ", gt_ggc_e_");
2089 output_mangled_typename (d
.of
, orig_s
);
2092 oprintf (d
.of
, ", gt_types_enum_last");
2094 oprintf (d
.of
, "))\n");
2095 oprintf (d
.of
, " xlimit = (");
2096 d
.prev_val
[2] = "*xlimit";
2097 output_escaped_param (&d
, chain_next
, "chain_next");
2098 oprintf (d
.of
, ");\n");
2099 if (chain_prev
!= NULL
)
2101 oprintf (d
.of
, " if (x != xlimit)\n");
2102 oprintf (d
.of
, " for (;;)\n");
2103 oprintf (d
.of
, " {\n");
2104 oprintf (d
.of
, " %s %s * const xprev = (",
2105 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2107 d
.prev_val
[2] = "*x";
2108 output_escaped_param (&d
, chain_prev
, "chain_prev");
2109 oprintf (d
.of
, ");\n");
2110 oprintf (d
.of
, " if (xprev == NULL) break;\n");
2111 oprintf (d
.of
, " x = xprev;\n");
2112 oprintf (d
.of
, " (void) %s (xprev",
2113 wtd
->marker_routine
);
2114 if (wtd
->param_prefix
)
2116 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
2117 output_mangled_typename (d
.of
, orig_s
);
2119 if (orig_s
->u
.p
->kind
== TYPE_PARAM_STRUCT
2120 && orig_s
->u
.p
->u
.s
.line
.file
!= NULL
)
2122 oprintf (d
.of
, ", gt_e_");
2123 output_mangled_typename (d
.of
, orig_s
);
2125 else if (UNION_OR_STRUCT_P (orig_s
)
2126 && orig_s
->u
.s
.line
.file
!= NULL
)
2128 oprintf (d
.of
, ", gt_ggc_e_");
2129 output_mangled_typename (d
.of
, orig_s
);
2132 oprintf (d
.of
, ", gt_types_enum_last");
2134 oprintf (d
.of
, ");\n");
2135 oprintf (d
.of
, " }\n");
2137 oprintf (d
.of
, " while (x != xlimit)\n");
2139 oprintf (d
.of
, " {\n");
2141 d
.prev_val
[2] = "*x";
2145 if (chain_next
!= NULL
)
2147 oprintf (d
.of
, " x = (");
2148 output_escaped_param (&d
, chain_next
, "chain_next");
2149 oprintf (d
.of
, ");\n");
2152 oprintf (d
.of
, " }\n");
2153 oprintf (d
.of
, "}\n");
2156 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
2159 write_types (type_p structures
, type_p param_structs
,
2160 const struct write_types_data
*wtd
)
2164 oprintf (header_file
, "\n/* %s*/\n", wtd
->comment
);
2165 for (s
= structures
; s
; s
= s
->next
)
2166 if (s
->gc_used
== GC_POINTED_TO
2167 || s
->gc_used
== GC_MAYBE_POINTED_TO
)
2171 if (s
->gc_used
== GC_MAYBE_POINTED_TO
2172 && s
->u
.s
.line
.file
== NULL
)
2175 oprintf (header_file
, "#define gt_%s_", wtd
->prefix
);
2176 output_mangled_typename (header_file
, s
);
2177 oprintf (header_file
, "(X) do { \\\n");
2178 oprintf (header_file
,
2179 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd
->prefix
,
2181 oprintf (header_file
,
2184 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2185 if (strcmp (opt
->name
, "ptr_alias") == 0)
2187 type_p t
= (type_p
) opt
->info
;
2188 if (t
->kind
== TYPE_STRUCT
2189 || t
->kind
== TYPE_UNION
2190 || t
->kind
== TYPE_LANG_STRUCT
)
2191 oprintf (header_file
,
2192 "#define gt_%sx_%s gt_%sx_%s\n",
2193 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t
->u
.s
.tag
);
2195 error_at_line (&s
->u
.s
.line
,
2196 "structure alias is not a structure");
2202 /* Declare the marker procedure only once. */
2203 oprintf (header_file
,
2204 "extern void gt_%sx_%s (void *);\n",
2205 wtd
->prefix
, s
->u
.s
.tag
);
2207 if (s
->u
.s
.line
.file
== NULL
)
2209 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
2214 if (s
->kind
== TYPE_LANG_STRUCT
)
2217 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
2218 write_func_for_structure (s
, ss
, NULL
, wtd
);
2221 write_func_for_structure (s
, s
, NULL
, wtd
);
2224 for (s
= param_structs
; s
; s
= s
->next
)
2225 if (s
->gc_used
== GC_POINTED_TO
)
2227 type_p
* param
= s
->u
.param_struct
.param
;
2228 type_p stru
= s
->u
.param_struct
.stru
;
2230 /* Declare the marker procedure. */
2231 oprintf (header_file
, "extern void gt_%s_", wtd
->prefix
);
2232 output_mangled_typename (header_file
, s
);
2233 oprintf (header_file
, " (void *);\n");
2235 if (stru
->u
.s
.line
.file
== NULL
)
2237 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
2242 if (stru
->kind
== TYPE_LANG_STRUCT
)
2245 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
2246 write_func_for_structure (s
, ss
, param
, wtd
);
2249 write_func_for_structure (s
, stru
, param
, wtd
);
2253 static const struct write_types_data ggc_wtd
=
2255 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
2256 "GC marker procedures. "
2259 static const struct write_types_data pch_wtd
=
2261 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
2262 "gt_pch_note_reorder",
2263 "PCH type-walking procedures. "
2266 /* Write out the local pointer-walking routines. */
2268 /* process_field routine for local pointer-walking. */
2271 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
2278 case TYPE_LANG_STRUCT
:
2279 case TYPE_PARAM_STRUCT
:
2281 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
2283 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
2294 /* For S, a structure that's part of ORIG_S, and using parameters
2295 PARAM, write out a routine that:
2296 - Is of type gt_note_pointers
2297 - Calls PROCESS_FIELD on each field of S or its substructures.
2301 write_local_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
)
2303 const char *fn
= s
->u
.s
.line
.file
;
2305 struct walk_type_data d
;
2307 /* This is a hack, and not the good kind either. */
2308 for (i
= NUM_PARAM
- 1; i
>= 0; i
--)
2309 if (param
&& param
[i
] && param
[i
]->kind
== TYPE_POINTER
2310 && UNION_OR_STRUCT_P (param
[i
]->u
.p
))
2311 fn
= param
[i
]->u
.p
->u
.s
.line
.file
;
2313 memset (&d
, 0, sizeof (d
));
2314 d
.of
= get_output_file_with_visibility (fn
);
2316 d
.process_field
= write_types_local_process_field
;
2318 d
.line
= &s
->u
.s
.line
;
2319 d
.bitmap
= s
->u
.s
.bitmap
;
2321 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
2322 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
2323 d
.prev_val
[3] = "x";
2325 d
.fn_wants_lvalue
= true;
2327 oprintf (d
.of
, "\n");
2328 oprintf (d
.of
, "void\n");
2329 oprintf (d
.of
, "gt_pch_p_");
2330 output_mangled_typename (d
.of
, orig_s
);
2331 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
2333 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
2334 "\tATTRIBUTE_UNUSED void *cookie)\n");
2335 oprintf (d
.of
, "{\n");
2336 oprintf (d
.of
, " %s %s * const x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
2337 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
2338 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2341 oprintf (d
.of
, "}\n");
2344 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
2347 write_local (type_p structures
, type_p param_structs
)
2351 oprintf (header_file
, "\n/* Local pointer-walking routines. */\n");
2352 for (s
= structures
; s
; s
= s
->next
)
2353 if (s
->gc_used
== GC_POINTED_TO
2354 || s
->gc_used
== GC_MAYBE_POINTED_TO
)
2358 if (s
->u
.s
.line
.file
== NULL
)
2361 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2362 if (strcmp (opt
->name
, "ptr_alias") == 0)
2364 type_p t
= (type_p
) opt
->info
;
2365 if (t
->kind
== TYPE_STRUCT
2366 || t
->kind
== TYPE_UNION
2367 || t
->kind
== TYPE_LANG_STRUCT
)
2369 oprintf (header_file
, "#define gt_pch_p_");
2370 output_mangled_typename (header_file
, s
);
2371 oprintf (header_file
, " gt_pch_p_");
2372 output_mangled_typename (header_file
, t
);
2373 oprintf (header_file
, "\n");
2376 error_at_line (&s
->u
.s
.line
,
2377 "structure alias is not a structure");
2383 /* Declare the marker procedure only once. */
2384 oprintf (header_file
, "extern void gt_pch_p_");
2385 output_mangled_typename (header_file
, s
);
2386 oprintf (header_file
,
2387 "\n (void *, void *, gt_pointer_operator, void *);\n");
2389 if (s
->kind
== TYPE_LANG_STRUCT
)
2392 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
2393 write_local_func_for_structure (s
, ss
, NULL
);
2396 write_local_func_for_structure (s
, s
, NULL
);
2399 for (s
= param_structs
; s
; s
= s
->next
)
2400 if (s
->gc_used
== GC_POINTED_TO
)
2402 type_p
* param
= s
->u
.param_struct
.param
;
2403 type_p stru
= s
->u
.param_struct
.stru
;
2405 /* Declare the marker procedure. */
2406 oprintf (header_file
, "extern void gt_pch_p_");
2407 output_mangled_typename (header_file
, s
);
2408 oprintf (header_file
,
2409 "\n (void *, void *, gt_pointer_operator, void *);\n");
2411 if (stru
->u
.s
.line
.file
== NULL
)
2413 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
2418 if (stru
->kind
== TYPE_LANG_STRUCT
)
2421 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
2422 write_local_func_for_structure (s
, ss
, param
);
2425 write_local_func_for_structure (s
, stru
, param
);
2429 /* Write out the 'enum' definition for gt_types_enum. */
2432 write_enum_defn (type_p structures
, type_p param_structs
)
2436 oprintf (header_file
, "\n/* Enumeration of types known. */\n");
2437 oprintf (header_file
, "enum gt_types_enum {\n");
2438 for (s
= structures
; s
; s
= s
->next
)
2439 if (s
->gc_used
== GC_POINTED_TO
2440 || s
->gc_used
== GC_MAYBE_POINTED_TO
)
2442 if (s
->gc_used
== GC_MAYBE_POINTED_TO
2443 && s
->u
.s
.line
.file
== NULL
)
2446 oprintf (header_file
, " gt_ggc_e_");
2447 output_mangled_typename (header_file
, s
);
2448 oprintf (header_file
, ", \n");
2450 for (s
= param_structs
; s
; s
= s
->next
)
2451 if (s
->gc_used
== GC_POINTED_TO
)
2453 oprintf (header_file
, " gt_e_");
2454 output_mangled_typename (header_file
, s
);
2455 oprintf (header_file
, ", \n");
2457 oprintf (header_file
, " gt_types_enum_last\n");
2458 oprintf (header_file
, "};\n");
2461 /* Might T contain any non-pointer elements? */
2464 contains_scalar_p (type_p t
)
2472 return contains_scalar_p (t
->u
.a
.p
);
2474 /* Could also check for structures that have no non-pointer
2475 fields, but there aren't enough of those to worry about. */
2480 /* Mangle FN and print it to F. */
2483 put_mangled_filename (outf_p f
, const char *fn
)
2485 const char *name
= get_output_file_name (fn
);
2486 for (; *name
!= 0; name
++)
2487 if (ISALNUM (*name
))
2488 oprintf (f
, "%c", *name
);
2490 oprintf (f
, "%c", '_');
2493 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
2494 LASTNAME, and NAME are all strings to insert in various places in
2495 the resulting code. */
2498 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
2499 const char *tname
, const char *name
)
2503 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
2504 if (fli2
->started_p
)
2506 oprintf (fli2
->f
, " %s\n", lastname
);
2507 oprintf (fli2
->f
, "};\n\n");
2510 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
2511 if (fli2
->started_p
)
2513 lang_bitmap bitmap
= get_base_file_bitmap (fli2
->name
);
2516 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
2519 oprintf (base_files
[fnum
],
2520 "extern const struct %s gt_%s_",
2522 put_mangled_filename (base_files
[fnum
], fli2
->name
);
2523 oprintf (base_files
[fnum
], "[];\n");
2529 for (fnum
= 0; fnum
< NUM_BASE_FILES
; fnum
++)
2530 oprintf (base_files
[fnum
],
2531 "const struct %s * const %s[] = {\n",
2536 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
2537 if (fli2
->started_p
)
2539 lang_bitmap bitmap
= get_base_file_bitmap (fli2
->name
);
2542 fli2
->started_p
= 0;
2544 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
2547 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
2548 put_mangled_filename (base_files
[fnum
], fli2
->name
);
2549 oprintf (base_files
[fnum
], ",\n");
2555 for (fnum
= 0; fnum
< NUM_BASE_FILES
; fnum
++)
2557 oprintf (base_files
[fnum
], " NULL\n");
2558 oprintf (base_files
[fnum
], "};\n");
2563 /* Write out to F the table entry and any marker routines needed to
2564 mark NAME as TYPE. The original variable is V, at LINE.
2565 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
2566 is nonzero iff we are building the root table for hash table caches. */
2569 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
2570 struct fileloc
*line
, const char *if_marked
)
2577 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
2580 const char *desc
= NULL
;
2583 for (o
= fld
->opt
; o
; o
= o
->next
)
2584 if (strcmp (o
->name
, "skip") == 0)
2586 else if (strcmp (o
->name
, "desc") == 0)
2589 error_at_line (line
,
2590 "field `%s' of global `%s' has unknown option `%s'",
2591 fld
->name
, name
, o
->name
);
2595 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
2597 pair_p validf
= NULL
;
2600 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
2602 const char *tag
= NULL
;
2605 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
2606 if (strcmp (oo
->name
, "tag") == 0)
2608 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
2611 error_at_line (line
,
2612 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
2613 name
, fld
->name
, validf
->name
,
2614 name
, fld
->name
, ufld
->name
,
2621 newname
= xasprintf ("%s.%s.%s",
2622 name
, fld
->name
, validf
->name
);
2623 write_root (f
, v
, validf
->type
, newname
, 0, line
,
2629 error_at_line (line
,
2630 "global `%s.%s' has `desc' option but is not union",
2635 newname
= xasprintf ("%s.%s", name
, fld
->name
);
2636 write_root (f
, v
, fld
->type
, newname
, 0, line
, if_marked
);
2646 newname
= xasprintf ("%s[0]", name
);
2647 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, if_marked
);
2656 oprintf (f
, " {\n");
2657 oprintf (f
, " &%s,\n", name
);
2660 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
2662 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
2663 else if (ap
== v
->type
)
2664 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
2666 oprintf (f
, " sizeof (%s", v
->name
);
2667 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
2669 oprintf (f
, "),\n");
2673 if (! has_length
&& UNION_OR_STRUCT_P (tp
))
2675 oprintf (f
, " >_ggc_mx_%s,\n", tp
->u
.s
.tag
);
2676 oprintf (f
, " >_pch_nx_%s", tp
->u
.s
.tag
);
2678 else if (! has_length
&& tp
->kind
== TYPE_PARAM_STRUCT
)
2680 oprintf (f
, " >_ggc_m_");
2681 output_mangled_typename (f
, tp
);
2682 oprintf (f
, ",\n >_pch_n_");
2683 output_mangled_typename (f
, tp
);
2686 && (tp
->kind
== TYPE_POINTER
|| UNION_OR_STRUCT_P (tp
)))
2688 oprintf (f
, " >_ggc_ma_%s,\n", name
);
2689 oprintf (f
, " >_pch_na_%s", name
);
2693 error_at_line (line
,
2694 "global `%s' is pointer to unimplemented type",
2698 oprintf (f
, ",\n &%s", if_marked
);
2699 oprintf (f
, "\n },\n");
2705 oprintf (f
, " {\n");
2706 oprintf (f
, " &%s,\n", name
);
2707 oprintf (f
, " 1, \n");
2708 oprintf (f
, " sizeof (%s),\n", v
->name
);
2709 oprintf (f
, " >_ggc_m_S,\n");
2710 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
2711 oprintf (f
, " },\n");
2719 error_at_line (line
,
2720 "global `%s' is unimplemented type",
2725 /* This generates a routine to walk an array. */
2728 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
2730 struct walk_type_data d
;
2733 memset (&d
, 0, sizeof (d
));
2739 d
.bitmap
= get_base_file_bitmap (v
->line
.file
);
2742 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
2744 if (wtd
->param_prefix
)
2746 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
2748 " (void *, void *, gt_pointer_operator, void *);\n");
2749 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
2750 wtd
->param_prefix
, v
->name
);
2752 " ATTRIBUTE_UNUSED void *x_p,\n"
2753 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
2754 " ATTRIBUTE_UNUSED void * cookie)\n");
2755 oprintf (d
.of
, "{\n");
2756 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
2757 d
.process_field
= write_types_local_process_field
;
2758 walk_type (v
->type
, &d
);
2759 oprintf (f
, "}\n\n");
2763 oprintf (f
, "static void gt_%sa_%s (void *);\n",
2764 wtd
->prefix
, v
->name
);
2765 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
2766 wtd
->prefix
, v
->name
);
2768 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
2769 d
.process_field
= write_types_process_field
;
2770 walk_type (v
->type
, &d
);
2772 oprintf (f
, "}\n\n");
2775 /* Output a table describing the locations and types of VARIABLES. */
2778 write_roots (pair_p variables
)
2781 struct flist
*flp
= NULL
;
2783 for (v
= variables
; v
; v
= v
->next
)
2785 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
2787 const char *length
= NULL
;
2788 int deletable_p
= 0;
2791 for (o
= v
->opt
; o
; o
= o
->next
)
2792 if (strcmp (o
->name
, "length") == 0)
2794 else if (strcmp (o
->name
, "deletable") == 0)
2796 else if (strcmp (o
->name
, "param_is") == 0)
2798 else if (strncmp (o
->name
, "param", 5) == 0
2799 && ISDIGIT (o
->name
[5])
2800 && strcmp (o
->name
+ 6, "_is") == 0)
2802 else if (strcmp (o
->name
, "if_marked") == 0)
2805 error_at_line (&v
->line
,
2806 "global `%s' has unknown option `%s'",
2809 for (fli
= flp
; fli
; fli
= fli
->next
)
2814 fli
= XNEW (struct flist
);
2818 fli
->name
= v
->line
.file
;
2821 oprintf (f
, "\n/* GC roots. */\n\n");
2826 && v
->type
->kind
== TYPE_POINTER
2827 && (v
->type
->u
.p
->kind
== TYPE_POINTER
2828 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
2830 write_array (f
, v
, &ggc_wtd
);
2831 write_array (f
, v
, &pch_wtd
);
2835 for (v
= variables
; v
; v
= v
->next
)
2837 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
2843 for (o
= v
->opt
; o
; o
= o
->next
)
2844 if (strcmp (o
->name
, "length") == 0)
2846 else if (strcmp (o
->name
, "deletable") == 0
2847 || strcmp (o
->name
, "if_marked") == 0)
2853 for (fli
= flp
; fli
; fli
= fli
->next
)
2856 if (! fli
->started_p
)
2860 oprintf (f
, "const struct ggc_root_tab gt_ggc_r_");
2861 put_mangled_filename (f
, v
->line
.file
);
2862 oprintf (f
, "[] = {\n");
2865 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
);
2868 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
2871 for (v
= variables
; v
; v
= v
->next
)
2873 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
2878 for (o
= v
->opt
; o
; o
= o
->next
)
2879 if (strcmp (o
->name
, "deletable") == 0)
2881 else if (strcmp (o
->name
, "if_marked") == 0)
2887 for (fli
= flp
; fli
; fli
= fli
->next
)
2890 if (! fli
->started_p
)
2894 oprintf (f
, "const struct ggc_root_tab gt_ggc_rd_");
2895 put_mangled_filename (f
, v
->line
.file
);
2896 oprintf (f
, "[] = {\n");
2899 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
2903 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
2904 "gt_ggc_deletable_rtab");
2906 for (v
= variables
; v
; v
= v
->next
)
2908 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
2910 const char *if_marked
= NULL
;
2914 for (o
= v
->opt
; o
; o
= o
->next
)
2915 if (strcmp (o
->name
, "length") == 0)
2917 else if (strcmp (o
->name
, "if_marked") == 0)
2918 if_marked
= o
->info
;
2920 if (if_marked
== NULL
)
2923 if (v
->type
->kind
!= TYPE_POINTER
2924 || v
->type
->u
.p
->kind
!= TYPE_PARAM_STRUCT
2925 || v
->type
->u
.p
->u
.param_struct
.stru
!= find_structure ("htab", 0))
2927 error_at_line (&v
->line
, "if_marked option used but not hash table");
2931 for (fli
= flp
; fli
; fli
= fli
->next
)
2934 if (! fli
->started_p
)
2938 oprintf (f
, "const struct ggc_cache_tab gt_ggc_rc_");
2939 put_mangled_filename (f
, v
->line
.file
);
2940 oprintf (f
, "[] = {\n");
2943 write_root (f
, v
, v
->type
->u
.p
->u
.param_struct
.param
[0],
2944 v
->name
, length_p
, &v
->line
, if_marked
);
2947 finish_root_table (flp
, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
2948 "gt_ggc_cache_rtab");
2950 for (v
= variables
; v
; v
= v
->next
)
2952 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
2955 int if_marked_p
= 0;
2958 for (o
= v
->opt
; o
; o
= o
->next
)
2959 if (strcmp (o
->name
, "length") == 0)
2961 else if (strcmp (o
->name
, "if_marked") == 0)
2967 for (fli
= flp
; fli
; fli
= fli
->next
)
2970 if (! fli
->started_p
)
2974 oprintf (f
, "const struct ggc_root_tab gt_pch_rc_");
2975 put_mangled_filename (f
, v
->line
.file
);
2976 oprintf (f
, "[] = {\n");
2979 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
);
2982 finish_root_table (flp
, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
2983 "gt_pch_cache_rtab");
2985 for (v
= variables
; v
; v
= v
->next
)
2987 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
2992 for (o
= v
->opt
; o
; o
= o
->next
)
2993 if (strcmp (o
->name
, "deletable") == 0
2994 || strcmp (o
->name
, "if_marked") == 0)
3000 if (! contains_scalar_p (v
->type
))
3003 for (fli
= flp
; fli
; fli
= fli
->next
)
3006 if (! fli
->started_p
)
3010 oprintf (f
, "const struct ggc_root_tab gt_pch_rs_");
3011 put_mangled_filename (f
, v
->line
.file
);
3012 oprintf (f
, "[] = {\n");
3015 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3019 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3020 "gt_pch_scalar_rtab");
3024 extern int main (int argc
, char **argv
);
3026 main(int ARG_UNUSED (argc
), char ** ARG_UNUSED (argv
))
3029 static struct fileloc pos
= { __FILE__
, __LINE__
};
3034 srcdir_len
= strlen (srcdir
);
3036 do_scalar_typedef ("CUMULATIVE_ARGS", &pos
);
3037 do_scalar_typedef ("REAL_VALUE_TYPE", &pos
);
3038 do_scalar_typedef ("uint8", &pos
);
3039 do_scalar_typedef ("jword", &pos
);
3040 do_scalar_typedef ("JCF_u2", &pos
);
3041 #ifdef USE_MAPPED_LOCATION
3042 do_scalar_typedef ("location_t", &pos
);
3043 do_scalar_typedef ("source_locus", &pos
);
3045 do_scalar_typedef ("void", &pos
);
3047 do_typedef ("PTR", create_pointer (resolve_typedef ("void", &pos
)), &pos
);
3049 do_typedef ("HARD_REG_SET", create_array (
3050 create_scalar_type ("unsigned long", strlen ("unsigned long")),
3053 for (i
= 0; i
< NUM_GT_FILES
; i
++)
3056 /* Omit if already seen. */
3057 for (j
= 0; j
< i
; j
++)
3059 if (!strcmp (all_files
[i
], all_files
[j
]))
3066 parse_file (all_files
[i
]);
3067 #ifndef USE_MAPPED_LOCATION
3068 /* temporary kludge - gengtype doesn't handle conditionals.
3069 Manually add source_locus *after* we've processed input.h. */
3071 do_typedef ("source_locus", create_pointer (resolve_typedef ("location_t", &pos
)), &pos
);
3078 set_gc_used (variables
);
3081 write_enum_defn (structures
, param_structs
);
3082 write_types (structures
, param_structs
, &ggc_wtd
);
3083 write_types (structures
, param_structs
, &pch_wtd
);
3084 write_local (structures
, param_structs
);
3085 write_roots (variables
);
3087 close_output_files ();
3089 return (hit_error
!= 0);