1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "errors.h" /* for fatal */
25 #include "double-int.h"
26 #include "version.h" /* for version_string & pkgversion_string. */
30 /* Data types, macros, etc. used only in this file. */
32 /* Kinds of types we can understand. */
46 /* A way to pass data through to the output end. */
54 /* Option data for the 'nested_ptr' option. */
55 struct nested_ptr_data
58 const char *convert_to
;
59 const char *convert_from
;
62 /* A name and a type. */
74 /* A description of a type. */
79 /* Used for structures whose definitions we haven't seen so far when
80 we encounter a pointer to it that is annotated with ``maybe_undef''.
81 If after reading in everything we don't have source file
82 information for it, we assume that it never has been defined. */
92 enum gc_used_enum gc_used
;
114 type_p param
[NUM_PARAM
];
121 ((x)->kind == TYPE_UNION || \
122 ((x)->kind == TYPE_LANG_STRUCT \
123 && (x)->u.s.lang_struct->kind == TYPE_UNION))
124 #define UNION_OR_STRUCT_P(x) \
125 ((x)->kind == TYPE_UNION \
126 || (x)->kind == TYPE_STRUCT \
127 || (x)->kind == TYPE_LANG_STRUCT)
131 const char *get_output_file_name (const char *);
134 /* The list of output files. */
137 /* The output header file that is included into pretty much every
142 /* The name of the file containing the list of input files. */
143 static char *inputlist
;
145 /* The plugin input files and their number; in that case only
146 a single file is produced. */
147 static char **plugin_files
;
148 static size_t nb_plugin_files
;
150 /* The generated plugin output file and name. */
151 static outf_p plugin_output
;
152 static char *plugin_output_filename
;
154 /* Our source directory and its length. */
158 /* Variables used for reading and writing the state. */
159 const char *read_state_filename
;
160 const char *write_state_filename
;
162 /* Variables to help debugging. */
166 static outf_p
create_file (const char *, const char *);
168 static const char *get_file_basename (const char *);
169 static const char *get_file_realbasename (const char *);
170 static const char *get_file_srcdir_relative_path (const char *);
172 static int get_prefix_langdir_index (const char *);
173 static const char *get_file_langdir (const char *);
176 /* Nonzero iff an error has occurred. */
177 bool hit_error
= false;
179 static void gen_rtx_next (void);
180 static void write_rtx_next (void);
181 static void open_base_files (void);
182 static void close_output_files (void);
184 /* Report an error at POS, printing MSG. */
187 error_at_line (const struct fileloc
*pos
, const char *msg
, ...)
193 fprintf (stderr
, "%s:%d: ", pos
->file
, pos
->line
);
194 vfprintf (stderr
, msg
, ap
);
195 fputc ('\n', stderr
);
201 /* asprintf, but produces fatal message on out-of-memory. */
203 xasprintf (const char *format
, ...)
209 va_start (ap
, format
);
210 n
= vasprintf (&result
, format
, ap
);
211 if (result
== NULL
|| n
< 0)
212 fatal ("out of memory");
218 /* Input file handling. */
220 /* Table of all input files. */
221 static const char **gt_files
;
222 static size_t num_gt_files
;
224 /* A number of places use the name of this "gengtype.h" file for a
225 location for things that we can't rely on the source to define.
226 Make sure we can still use pointer comparison on filenames. */
227 const char this_file
[] = __FILE__
;
228 /* The "system.h" file is likewise specially useful. */
229 const char system_h_file
[] = "system.h";
231 /* Vector of per-language directories. */
232 static const char **lang_dir_names
;
233 static size_t num_lang_dirs
;
235 /* An array of output files suitable for definitions. There is one
236 BASE_FILES entry for each language. */
237 static outf_p
*base_files
;
239 /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
240 INPUT_FILE is used by <lang>.
242 This function should be written to assume that a file _is_ used
243 if the situation is unclear. If it wrongly assumes a file _is_ used,
244 a linker error will result. If it wrongly assumes a file _is not_ used,
245 some GC roots may be missed, which is a much harder-to-debug problem.
247 The relevant bitmap is stored immediately before the file's name in the
248 buffer set up by read_input_list. It may be unaligned, so we have to
249 read it byte-by-byte. */
252 get_lang_bitmap (const char *gtfile
)
255 if (gtfile
== this_file
|| gtfile
== system_h_file
)
257 /* Things defined in this "gengtype.c" file or in "system.h" are
258 universal (and there is no space for their lang_bitmap before
259 their file names). */
260 return (((lang_bitmap
) 1) << num_lang_dirs
) - 1;
266 for (i
= -(int) sizeof (lang_bitmap
); i
< 0; i
++)
267 n
= (n
<< CHAR_BIT
) + (unsigned char) gtfile
[i
];
272 /* Set the bitmap returned by get_lang_bitmap. The only legitimate
273 caller of this function is read_input_list. */
275 set_lang_bitmap (char *gtfile
, lang_bitmap n
)
278 for (i
= -1; i
>= -(int) sizeof (lang_bitmap
); i
--)
280 gtfile
[i
] = n
& ((1U << CHAR_BIT
) - 1);
287 /* Utility debugging function, printing the various type counts within
288 a list of types. Called thru the DBGPRINT_COUNT_TYPE macro. */
290 dbgprint_count_type_at (const char *fil
, int lin
, const char *msg
, type_p t
)
292 int nb_types
= 0, nb_scalar
= 0, nb_string
= 0;
293 int nb_struct
= 0, nb_union
= 0, nb_array
= 0, nb_pointer
= 0;
294 int nb_lang_struct
= 0, nb_param_struct
= 0;
296 for (p
= t
; p
; p
= p
->next
)
319 case TYPE_LANG_STRUCT
:
322 case TYPE_PARAM_STRUCT
:
329 fprintf (stderr
, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
330 lbasename (fil
), lin
, msg
, nb_types
);
331 if (nb_scalar
> 0 || nb_string
> 0)
332 fprintf (stderr
, "@@%%@@ %d scalars, %d strings\n", nb_scalar
, nb_string
);
333 if (nb_struct
> 0 || nb_union
> 0)
334 fprintf (stderr
, "@@%%@@ %d structs, %d unions\n", nb_struct
, nb_union
);
335 if (nb_pointer
> 0 || nb_array
> 0)
336 fprintf (stderr
, "@@%%@@ %d pointers, %d arrays\n", nb_pointer
, nb_array
);
337 if (nb_lang_struct
> 0 || nb_param_struct
> 0)
338 fprintf (stderr
, "@@%%@@ %d lang_structs, %d param_structs\n",
339 nb_lang_struct
, nb_param_struct
);
340 fprintf (stderr
, "\n");
342 #endif /* ENABLE_CHECKING */
344 /* Scan the input file, LIST, and determine how much space we need to
345 store strings in. Also, count the number of language directories
346 and files. The numbers returned are overestimates as they does not
347 consider repeated files. */
349 measure_input_list (FILE *list
)
355 num_gt_files
= plugin_files
? nb_plugin_files
: 0;
356 while ((c
= getc (list
)) != EOF
)
365 /* Add space for a lang_bitmap before the input file name. */
366 n
+= sizeof (lang_bitmap
);
380 /* Read one input line from LIST to HEREP (which is updated). A
381 pointer to the string is returned via LINEP. If it was a language
382 subdirectory in square brackets, strip off the square brackets and
383 return true. Otherwise, leave space before the string for a
384 lang_bitmap, and return false. At EOF, returns false, does not
385 touch *HEREP, and sets *LINEP to NULL. POS is used for
388 read_input_line (FILE *list
, char **herep
, char **linep
, struct fileloc
*pos
)
394 /* Read over whitespace. */
395 while (c
== '\n' || c
== ' ')
405 /* No space for a lang_bitmap is necessary. Discard the '['. */
408 while (c
!= ']' && c
!= '\n' && c
!= EOF
)
417 c
= getc (list
); /* eat what should be a newline */
418 if (c
!= '\n' && c
!= EOF
)
419 error_at_line (pos
, "junk on line after language tag [%s]", line
);
422 error_at_line (pos
, "missing close bracket for language tag [%s",
431 /* Leave space for a lang_bitmap. */
432 memset (here
, 0, sizeof (lang_bitmap
));
433 here
+= sizeof (lang_bitmap
);
440 while (c
!= EOF
&& c
!= '\n');
448 /* Read the list of input files from LIST and compute all of the
449 relevant tables. There is one file per line of the list. At
450 first, all the files on the list are language-generic, but
451 eventually a line will appear which is the name of a language
452 subdirectory in square brackets, like this: [cp]. All subsequent
453 files are specific to that language, until another language
454 subdirectory tag appears. Files can appear more than once, if
455 they apply to more than one language. */
457 read_input_list (const char *listname
)
459 FILE *list
= fopen (listname
, "r");
461 fatal ("cannot open %s: %s", listname
, xstrerror (errno
));
465 size_t bufsz
= measure_input_list (list
);
466 char *buf
= XNEWVEC (char, bufsz
);
468 char *committed
= buf
;
469 char *limit
= buf
+ bufsz
;
474 lang_bitmap curlangs
= (1 << num_lang_dirs
) - 1;
476 epos
.file
= listname
;
479 lang_dir_names
= XNEWVEC (const char *, num_lang_dirs
);
480 gt_files
= XNEWVEC (const char *, num_gt_files
);
487 is_language
= read_input_line (list
, &here
, &line
, &epos
);
488 gcc_assert (here
<= limit
);
491 else if (is_language
)
494 gcc_assert (langno
<= num_lang_dirs
);
495 for (i
= 0; i
< langno
; i
++)
496 if (strcmp (lang_dir_names
[i
], line
) == 0)
498 error_at_line (&epos
, "duplicate language tag [%s]",
505 curlangs
= 1 << langno
;
506 lang_dir_names
[langno
++] = line
;
511 gcc_assert (nfiles
<= num_gt_files
);
512 for (i
= 0; i
< nfiles
; i
++)
513 if (strcmp (gt_files
[i
], line
) == 0)
515 /* Throw away the string we just read, and add the
516 current language to the existing string's bitmap. */
517 lang_bitmap bmap
= get_lang_bitmap (gt_files
[i
]);
519 error_at_line (&epos
,
520 "file %s specified more than once "
521 "for language %s", line
,
523 0 ? "(all)" : lang_dir_names
[langno
-
527 set_lang_bitmap (CONST_CAST (char *, gt_files
[i
]), bmap
);
532 set_lang_bitmap (line
, curlangs
);
533 gt_files
[nfiles
++] = line
;
536 /* Update the global counts now that we know accurately how many
537 things there are. (We do not bother resizing the arrays down.) */
538 num_lang_dirs
= langno
;
539 /* Add the plugin files if provided. */
543 for (i
= 0; i
< nb_plugin_files
; i
++)
544 gt_files
[nfiles
++] = plugin_files
[i
];
546 num_gt_files
= nfiles
;
549 /* Sanity check: any file that resides in a language subdirectory
550 (e.g. 'cp') ought to belong to the corresponding language.
551 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
552 (Can you even do that? Should you be allowed to?) */
555 for (f
= 0; f
< num_gt_files
; f
++)
557 lang_bitmap bitmap
= get_lang_bitmap (gt_files
[f
]);
558 const char *basename
= get_file_basename (gt_files
[f
]);
559 const char *slashpos
= strchr (basename
, '/');
564 for (l
= 0; l
< num_lang_dirs
; l
++)
565 if ((size_t) (slashpos
- basename
) == strlen (lang_dir_names
[l
])
566 && memcmp (basename
, lang_dir_names
[l
],
567 strlen (lang_dir_names
[l
])) == 0)
569 if (!(bitmap
& (1 << l
)))
570 error ("%s is in language directory '%s' but is not "
571 "tagged for that language",
572 basename
, lang_dir_names
[l
]);
580 fatal ("error reading %s: %s", listname
, xstrerror (errno
));
587 /* The one and only TYPE_STRING. */
589 static struct type string_type
= {
590 TYPE_STRING
, 0, 0, GC_USED
, {0}
593 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
594 set to appropriate values at the beginning of main. */
596 static struct type scalar_nonchar
= {
597 TYPE_SCALAR
, 0, 0, GC_USED
, {0}
600 static struct type scalar_char
= {
601 TYPE_SCALAR
, 0, 0, GC_USED
, {0}
604 /* Lists of various things. */
606 static pair_p typedefs
;
607 static type_p structures
;
608 static type_p param_structs
;
609 static pair_p variables
;
611 static type_p
find_param_structure (type_p t
, type_p param
[NUM_PARAM
]);
612 static type_p
adjust_field_tree_exp (type_p t
, options_p opt
);
613 static type_p
adjust_field_rtx_def (type_p t
, options_p opt
);
615 /* Define S as a typedef to T at POS. */
618 do_typedef (const char *s
, type_p t
, struct fileloc
*pos
)
622 /* temporary kludge - gengtype doesn't handle conditionals or
623 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
624 is coming from this file (main() sets them up with safe dummy
626 if (!strcmp (s
, "CUMULATIVE_ARGS") && pos
->file
!= this_file
)
629 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
630 if (strcmp (p
->name
, s
) == 0)
634 error_at_line (pos
, "type `%s' previously defined", s
);
635 error_at_line (&p
->line
, "previously defined here");
640 p
= XNEW (struct pair
);
649 /* Define S as a typename of a scalar. Cannot be used to define
650 typedefs of 'char'. Note: is also used for pointer-to-function
651 typedefs (which are therefore not treated as pointers). */
654 do_scalar_typedef (const char *s
, struct fileloc
*pos
)
656 do_typedef (s
, &scalar_nonchar
, pos
);
659 /* Return the type previously defined for S. Use POS to report errors. */
662 resolve_typedef (const char *s
, struct fileloc
*pos
)
665 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
666 if (strcmp (p
->name
, s
) == 0)
668 error_at_line (pos
, "unidentified type `%s'", s
);
669 return &scalar_nonchar
; /* treat as "int" */
672 /* Create and return a new structure with tag NAME (or a union iff
673 ISUNION is nonzero), at POS with fields FIELDS and options O. */
676 new_structure (const char *name
, int isunion
, struct fileloc
*pos
,
677 pair_p fields
, options_p o
)
681 lang_bitmap bitmap
= get_lang_bitmap (pos
->file
);
683 /* temporary kludge - gengtype doesn't handle conditionals or
684 macros. Ignore any attempt to define struct location_s, unless
685 it is coming from this file (main() sets it up safely). */
686 if (!strcmp (name
, "location_s") && !isunion
&& pos
->file
!= this_file
)
687 return find_structure (name
, 0);
689 for (si
= structures
; si
!= NULL
; si
= si
->next
)
690 if (strcmp (name
, si
->u
.s
.tag
) == 0 && UNION_P (si
) == isunion
)
693 if (si
->kind
== TYPE_LANG_STRUCT
)
697 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
698 if (si
->u
.s
.bitmap
== bitmap
)
701 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
704 si
= XCNEW (struct type
);
705 memcpy (si
, ls
, sizeof (struct type
));
706 ls
->kind
= TYPE_LANG_STRUCT
;
707 ls
->u
.s
.lang_struct
= si
;
708 ls
->u
.s
.fields
= NULL
;
710 si
->pointer_to
= NULL
;
711 si
->u
.s
.lang_struct
= ls
;
716 if (ls
!= NULL
&& s
== NULL
)
718 s
= XCNEW (struct type
);
719 s
->next
= ls
->u
.s
.lang_struct
;
720 ls
->u
.s
.lang_struct
= s
;
721 s
->u
.s
.lang_struct
= ls
;
728 s
= XCNEW (struct type
);
729 s
->next
= structures
;
733 if (s
->u
.s
.line
.file
!= NULL
734 || (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
)))
736 error_at_line (pos
, "duplicate definition of '%s %s'",
737 isunion
? "union" : "struct", s
->u
.s
.tag
);
738 error_at_line (&s
->u
.s
.line
, "previous definition here");
741 s
->kind
= isunion
? TYPE_UNION
: TYPE_STRUCT
;
744 s
->u
.s
.fields
= fields
;
746 s
->u
.s
.bitmap
= bitmap
;
747 if (s
->u
.s
.lang_struct
)
748 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
750 /* Reset location_s's location to input.h so that we know where to
751 write out its mark routine. */
752 if (!strcmp (name
, "location_s") && !isunion
&& pos
->file
== this_file
)
755 for (n
= 0; n
< num_gt_files
; n
++)
756 if (!strcmp (gt_files
[n
] + strlen (gt_files
[n
]) - strlen ("input.h"),
759 s
->u
.s
.line
.file
= gt_files
[n
];
767 /* Return the previously-defined structure with tag NAME (or a union
768 iff ISUNION is nonzero), or a new empty structure or union if none
769 was defined previously. */
772 find_structure (const char *name
, int isunion
)
776 for (s
= structures
; s
!= NULL
; s
= s
->next
)
777 if (strcmp (name
, s
->u
.s
.tag
) == 0 && UNION_P (s
) == isunion
)
780 s
= XCNEW (struct type
);
781 s
->next
= structures
;
783 s
->kind
= isunion
? TYPE_UNION
: TYPE_STRUCT
;
789 /* Return the previously-defined parameterized structure for structure
790 T and parameters PARAM, or a new parameterized empty structure or
791 union if none was defined previously. */
794 find_param_structure (type_p t
, type_p param
[NUM_PARAM
])
798 for (res
= param_structs
; res
; res
= res
->next
)
799 if (res
->u
.param_struct
.stru
== t
800 && memcmp (res
->u
.param_struct
.param
, param
,
801 sizeof (type_p
) * NUM_PARAM
) == 0)
805 res
= XCNEW (struct type
);
806 res
->kind
= TYPE_PARAM_STRUCT
;
807 res
->next
= param_structs
;
809 res
->u
.param_struct
.stru
= t
;
810 memcpy (res
->u
.param_struct
.param
, param
, sizeof (type_p
) * NUM_PARAM
);
815 /* Return a scalar type with name NAME. */
818 create_scalar_type (const char *name
)
820 if (!strcmp (name
, "char") || !strcmp (name
, "unsigned char"))
823 return &scalar_nonchar
;
826 /* Return a pointer to T. */
829 create_pointer (type_p t
)
833 type_p r
= XCNEW (struct type
);
834 r
->kind
= TYPE_POINTER
;
838 return t
->pointer_to
;
841 /* Return an array of length LEN. */
844 create_array (type_p t
, const char *len
)
848 v
= XCNEW (struct type
);
849 v
->kind
= TYPE_ARRAY
;
855 /* Return an options structure with name NAME and info INFO. NEXT is the
856 next option in the chain. */
859 create_option (options_p next
, const char *name
, const void *info
)
861 options_p o
= XNEW (struct options
);
864 o
->info
= (const char *) info
;
868 /* Return an options structure for a "nested_ptr" option. */
870 create_nested_ptr_option (options_p next
, type_p t
,
871 const char *to
, const char *from
)
873 struct nested_ptr_data
*d
= XNEW (struct nested_ptr_data
);
875 d
->type
= adjust_field_type (t
, 0);
877 d
->convert_from
= from
;
878 return create_option (next
, "nested_ptr", d
);
881 /* Add a variable named S of type T with options O defined at POS,
885 note_variable (const char *s
, type_p t
, options_p o
, struct fileloc
*pos
)
888 n
= XNEW (struct pair
);
897 /* Most-general structure field creator. */
899 create_field_all (pair_p next
, type_p type
, const char *name
, options_p opt
,
900 const char *file
, int line
)
904 field
= XNEW (struct pair
);
909 field
->line
.file
= file
;
910 field
->line
.line
= line
;
914 /* Create a field that came from the source code we are scanning,
915 i.e. we have a 'struct fileloc', and possibly options; also,
916 adjust_field_type should be called. */
918 create_field_at (pair_p next
, type_p type
, const char *name
, options_p opt
,
921 return create_field_all (next
, adjust_field_type (type
, opt
),
922 name
, opt
, pos
->file
, pos
->line
);
925 /* Create a fake field with the given type and name. NEXT is the next
926 field in the chain. */
927 #define create_field(next,type,name) \
928 create_field_all(next,type,name, 0, this_file, __LINE__)
930 /* Like create_field, but the field is only valid when condition COND
934 create_optional_field_ (pair_p next
, type_p type
, const char *name
,
935 const char *cond
, int line
)
941 /* Create a fake union type with a single nameless field of type TYPE.
942 The field has a tag of "1". This allows us to make the presence
943 of a field of type TYPE depend on some boolean "desc" being true. */
944 union_fields
= create_field (NULL
, type
, "");
945 union_fields
->opt
= create_option (union_fields
->opt
, "dot", "");
946 union_fields
->opt
= create_option (union_fields
->opt
, "tag", "1");
947 union_type
= new_structure (xasprintf ("%s_%d", "fake_union", id
++), 1,
948 &lexer_line
, union_fields
, NULL
);
950 /* Create the field and give it the new fake union type. Add a "desc"
951 tag that specifies the condition under which the field is valid. */
952 return create_field_all (next
, union_type
, name
,
953 create_option (0, "desc", cond
), this_file
, line
);
956 #define create_optional_field(next,type,name,cond) \
957 create_optional_field_(next,type,name,cond,__LINE__)
959 /* Reverse a linked list of 'struct pair's in place. */
961 nreverse_pairs (pair_p list
)
963 pair_p prev
= 0, p
, next
;
964 for (p
= list
; p
; p
= next
)
974 /* We don't care how long a CONST_DOUBLE is. */
975 #define CONST_DOUBLE_FORMAT "ww"
976 /* We don't want to see codes that are only for generator files. */
977 #undef GENERATOR_FILE
981 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
987 static const char *const rtx_name
[NUM_RTX_CODE
] = {
988 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
993 static const char *const rtx_format
[NUM_RTX_CODE
] = {
994 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
999 static int rtx_next_new
[NUM_RTX_CODE
];
1001 /* We also need codes and names for insn notes (not register notes).
1002 Note that we do *not* bias the note values here. */
1005 #define DEF_INSN_NOTE(NAME) NAME,
1006 #include "insn-notes.def"
1007 #undef DEF_INSN_NOTE
1012 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1013 default field for line number notes. */
1014 static const char *const note_insn_name
[NOTE_INSN_MAX
+ 1] = {
1015 #define DEF_INSN_NOTE(NAME) #NAME,
1016 #include "insn-notes.def"
1017 #undef DEF_INSN_NOTE
1020 #undef CONST_DOUBLE_FORMAT
1021 #define GENERATOR_FILE
1023 /* Generate the contents of the rtx_next array. This really doesn't belong
1024 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1030 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1034 rtx_next_new
[i
] = -1;
1035 if (strncmp (rtx_format
[i
], "iuu", 3) == 0)
1036 rtx_next_new
[i
] = 2;
1037 else if (i
== COND_EXEC
|| i
== SET
|| i
== EXPR_LIST
|| i
== INSN_LIST
)
1038 rtx_next_new
[i
] = 1;
1040 for (k
= strlen (rtx_format
[i
]) - 1; k
>= 0; k
--)
1041 if (rtx_format
[i
][k
] == 'e' || rtx_format
[i
][k
] == 'u')
1042 rtx_next_new
[i
] = k
;
1046 /* Write out the contents of the rtx_next array. */
1048 write_rtx_next (void)
1050 outf_p f
= get_output_file_with_visibility (NULL
);
1055 oprintf (f
, "\n/* Used to implement the RTX_NEXT macro. */\n");
1056 oprintf (f
, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1057 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1058 if (rtx_next_new
[i
] == -1)
1059 oprintf (f
, " 0,\n");
1062 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new
[i
]);
1063 oprintf (f
, "};\n");
1066 /* Handle `special("rtx_def")'. This is a special case for field
1067 `fld' of struct rtx_def, which is an array of unions whose values
1068 are based in a complex way on the type of RTL. */
1071 adjust_field_rtx_def (type_p t
, options_p
ARG_UNUSED (opt
))
1076 type_p rtx_tp
, rtvec_tp
, tree_tp
, mem_attrs_tp
, note_union_tp
, scalar_tp
;
1077 type_p basic_block_tp
, reg_attrs_tp
, constant_tp
, symbol_union_tp
;
1079 if (t
->kind
!= TYPE_UNION
)
1081 error_at_line (&lexer_line
,
1082 "special `rtx_def' must be applied to a union");
1083 return &string_type
;
1086 nodot
= create_option (NULL
, "dot", "");
1088 rtx_tp
= create_pointer (find_structure ("rtx_def", 0));
1089 rtvec_tp
= create_pointer (find_structure ("rtvec_def", 0));
1090 tree_tp
= create_pointer (find_structure ("tree_node", 1));
1091 mem_attrs_tp
= create_pointer (find_structure ("mem_attrs", 0));
1092 reg_attrs_tp
= create_pointer (find_structure ("reg_attrs", 0));
1093 basic_block_tp
= create_pointer (find_structure ("basic_block_def", 0));
1095 create_pointer (find_structure ("constant_descriptor_rtx", 0));
1096 scalar_tp
= &scalar_nonchar
; /* rtunion int */
1099 pair_p note_flds
= NULL
;
1102 for (c
= 0; c
<= NOTE_INSN_MAX
; c
++)
1107 case NOTE_INSN_DELETED_LABEL
:
1108 note_flds
= create_field (note_flds
, &string_type
, "rt_str");
1111 case NOTE_INSN_BLOCK_BEG
:
1112 case NOTE_INSN_BLOCK_END
:
1113 note_flds
= create_field (note_flds
, tree_tp
, "rt_tree");
1116 case NOTE_INSN_VAR_LOCATION
:
1117 note_flds
= create_field (note_flds
, rtx_tp
, "rt_rtx");
1121 note_flds
= create_field (note_flds
, scalar_tp
, "rt_int");
1124 /* NOTE_INSN_MAX is used as the default field for line
1126 if (c
== NOTE_INSN_MAX
)
1127 note_flds
->opt
= create_option (nodot
, "default", "");
1129 note_flds
->opt
= create_option (nodot
, "tag", note_insn_name
[c
]);
1131 note_union_tp
= new_structure ("rtx_def_note_subunion", 1,
1132 &lexer_line
, note_flds
, NULL
);
1134 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1138 sym_flds
= create_field (NULL
, tree_tp
, "rt_tree");
1139 sym_flds
->opt
= create_option (nodot
, "default", "");
1141 sym_flds
= create_field (sym_flds
, constant_tp
, "rt_constant");
1142 sym_flds
->opt
= create_option (nodot
, "tag", "1");
1144 symbol_union_tp
= new_structure ("rtx_def_symbol_subunion", 1,
1145 &lexer_line
, sym_flds
, NULL
);
1147 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1149 pair_p subfields
= NULL
;
1150 size_t aindex
, nmindex
;
1155 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
1158 const char *subname
;
1160 switch (rtx_format
[i
][aindex
])
1171 if (i
== MEM
&& aindex
== 1)
1172 t
= mem_attrs_tp
, subname
= "rt_mem";
1173 else if (i
== JUMP_INSN
&& aindex
== 8)
1174 t
= rtx_tp
, subname
= "rt_rtx";
1175 else if (i
== CODE_LABEL
&& aindex
== 5)
1176 t
= scalar_tp
, subname
= "rt_int";
1177 else if (i
== CODE_LABEL
&& aindex
== 4)
1178 t
= rtx_tp
, subname
= "rt_rtx";
1179 else if (i
== LABEL_REF
&& (aindex
== 1 || aindex
== 2))
1180 t
= rtx_tp
, subname
= "rt_rtx";
1181 else if (i
== NOTE
&& aindex
== 4)
1182 t
= note_union_tp
, subname
= "";
1183 else if (i
== NOTE
&& aindex
== 5)
1184 t
= scalar_tp
, subname
= "rt_int";
1185 else if (i
== NOTE
&& aindex
>= 7)
1186 t
= scalar_tp
, subname
= "rt_int";
1187 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
1188 t
= scalar_tp
, subname
= "rt_int";
1189 else if (i
== VALUE
&& aindex
== 0)
1190 t
= scalar_tp
, subname
= "rt_int";
1191 else if (i
== DEBUG_EXPR
&& aindex
== 0)
1192 t
= tree_tp
, subname
= "rt_tree";
1193 else if (i
== REG
&& aindex
== 1)
1194 t
= scalar_tp
, subname
= "rt_int";
1195 else if (i
== REG
&& aindex
== 2)
1196 t
= reg_attrs_tp
, subname
= "rt_reg";
1197 else if (i
== SCRATCH
&& aindex
== 0)
1198 t
= scalar_tp
, subname
= "rt_int";
1199 else if (i
== SYMBOL_REF
&& aindex
== 1)
1200 t
= scalar_tp
, subname
= "rt_int";
1201 else if (i
== SYMBOL_REF
&& aindex
== 2)
1202 t
= symbol_union_tp
, subname
= "";
1203 else if (i
== BARRIER
&& aindex
>= 3)
1204 t
= scalar_tp
, subname
= "rt_int";
1207 error_at_line (&lexer_line
,
1208 "rtx type `%s' has `0' in position %lu, can't handle",
1209 rtx_name
[i
], (unsigned long) aindex
);
1231 subname
= "rt_rtvec";
1236 subname
= "rt_tree";
1245 error_at_line (&lexer_line
,
1246 "rtx type `%s' has `%c' in position %lu, can't handle",
1247 rtx_name
[i
], rtx_format
[i
][aindex
],
1248 (unsigned long) aindex
);
1254 subfields
= create_field (subfields
, t
,
1255 xasprintf (".fld[%lu].%s",
1256 (unsigned long) aindex
,
1258 subfields
->opt
= nodot
;
1259 if (t
== note_union_tp
)
1260 subfields
->opt
= create_option (subfields
->opt
, "desc",
1262 if (t
== symbol_union_tp
)
1263 subfields
->opt
= create_option (subfields
->opt
, "desc",
1264 "CONSTANT_POOL_ADDRESS_P (&%0)");
1267 if (i
== SYMBOL_REF
)
1269 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P holds. */
1270 type_p field_tp
= find_structure ("block_symbol", 0);
1272 = create_optional_field (subfields
, field_tp
, "block_sym",
1273 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1276 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
1277 substruct
= new_structure (sname
, 0, &lexer_line
, subfields
, NULL
);
1279 ftag
= xstrdup (rtx_name
[i
]);
1280 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
1281 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
1283 flds
= create_field (flds
, substruct
, "");
1284 flds
->opt
= create_option (nodot
, "tag", ftag
);
1287 return new_structure ("rtx_def_subunion", 1, &lexer_line
, flds
, nodot
);
1290 /* Handle `special("tree_exp")'. This is a special case for
1291 field `operands' of struct tree_exp, which although it claims to contain
1292 pointers to trees, actually sometimes contains pointers to RTL too.
1293 Passed T, the old type of the field, and OPT its options. Returns
1294 a new type for the field. */
1297 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
1302 if (t
->kind
!= TYPE_ARRAY
)
1304 error_at_line (&lexer_line
,
1305 "special `tree_exp' must be applied to an array");
1306 return &string_type
;
1309 nodot
= create_option (NULL
, "dot", "");
1311 flds
= create_field (NULL
, t
, "");
1312 flds
->opt
= create_option (nodot
, "length",
1313 "TREE_OPERAND_LENGTH ((tree) &%0)");
1314 flds
->opt
= create_option (flds
->opt
, "default", "");
1316 return new_structure ("tree_exp_subunion", 1, &lexer_line
, flds
, nodot
);
1319 /* Perform any special processing on a type T, about to become the type
1320 of a field. Return the appropriate type for the field.
1322 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1323 - Similarly for arrays of pointer-to-char;
1324 - Converts structures for which a parameter is provided to
1326 - Handles "special" options.
1330 adjust_field_type (type_p t
, options_p opt
)
1333 const int pointer_p
= t
->kind
== TYPE_POINTER
;
1334 type_p params
[NUM_PARAM
];
1338 for (i
= 0; i
< NUM_PARAM
; i
++)
1341 for (; opt
; opt
= opt
->next
)
1342 if (strcmp (opt
->name
, "length") == 0)
1344 else if (strcmp (opt
->name
, "param_is") == 0
1345 || (strncmp (opt
->name
, "param", 5) == 0
1346 && ISDIGIT (opt
->name
[5])
1347 && strcmp (opt
->name
+ 6, "_is") == 0))
1349 int num
= ISDIGIT (opt
->name
[5]) ? opt
->name
[5] - '0' : 0;
1351 if (!UNION_OR_STRUCT_P (t
)
1352 && (t
->kind
!= TYPE_POINTER
|| !UNION_OR_STRUCT_P (t
->u
.p
)))
1354 error_at_line (&lexer_line
,
1355 "option `%s' may only be applied to structures or structure pointers",
1361 if (params
[num
] != NULL
)
1362 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1363 if (!ISDIGIT (opt
->name
[5]))
1365 create_pointer (CONST_CAST2 (type_p
, const char *, opt
->info
));
1367 params
[num
] = CONST_CAST2 (type_p
, const char *, opt
->info
);
1369 else if (strcmp (opt
->name
, "special") == 0)
1371 const char *special_name
= opt
->info
;
1372 if (strcmp (special_name
, "tree_exp") == 0)
1373 t
= adjust_field_tree_exp (t
, opt
);
1374 else if (strcmp (special_name
, "rtx_def") == 0)
1375 t
= adjust_field_rtx_def (t
, opt
);
1377 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
1386 realt
= find_param_structure (t
, params
);
1387 t
= pointer_p
? create_pointer (realt
) : realt
;
1391 && pointer_p
&& t
->u
.p
->kind
== TYPE_SCALAR
&& t
->u
.p
->u
.scalar_is_char
)
1392 return &string_type
;
1393 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
1394 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
1395 && t
->u
.a
.p
->u
.p
->u
.scalar_is_char
)
1396 return create_array (&string_type
, t
->u
.a
.len
);
1402 static void set_gc_used_type (type_p
, enum gc_used_enum
, type_p
*);
1403 static void set_gc_used (pair_p
);
1405 /* Handle OPT for set_gc_used_type. */
1408 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
1409 int *pass_param
, int *length
, int *skip
,
1413 for (o
= opt
; o
; o
= o
->next
)
1414 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
)
1415 set_gc_used_type (CONST_CAST2 (type_p
, const char *, o
->info
),
1416 GC_POINTED_TO
, NULL
);
1417 else if (strcmp (o
->name
, "maybe_undef") == 0)
1419 else if (strcmp (o
->name
, "use_params") == 0)
1421 else if (strcmp (o
->name
, "length") == 0)
1423 else if (strcmp (o
->name
, "skip") == 0)
1425 else if (strcmp (o
->name
, "nested_ptr") == 0)
1426 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
)->type
;
1429 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1432 set_gc_used_type (type_p t
, enum gc_used_enum level
, type_p param
[NUM_PARAM
])
1434 if (t
->gc_used
>= level
)
1448 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
, &dummy
,
1451 for (f
= t
->u
.s
.fields
; f
; f
= f
->next
)
1453 int maybe_undef
= 0;
1457 type_p nested_ptr
= NULL
;
1458 process_gc_options (f
->opt
, level
, &maybe_undef
, &pass_param
,
1459 &length
, &skip
, &nested_ptr
);
1461 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
1462 set_gc_used_type (nested_ptr
, GC_POINTED_TO
,
1463 pass_param
? param
: NULL
);
1464 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
1465 set_gc_used_type (f
->type
->u
.p
, GC_USED
, NULL
);
1466 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
1467 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
, NULL
);
1468 else if (pass_param
&& f
->type
->kind
== TYPE_POINTER
&& param
)
1469 set_gc_used_type (find_param_structure (f
->type
->u
.p
, param
),
1470 GC_POINTED_TO
, NULL
);
1472 ; /* target type is not used through this field */
1474 set_gc_used_type (f
->type
, GC_USED
, pass_param
? param
: NULL
);
1480 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
, NULL
);
1484 set_gc_used_type (t
->u
.a
.p
, GC_USED
, param
);
1487 case TYPE_LANG_STRUCT
:
1488 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
1489 set_gc_used_type (t
, level
, param
);
1492 case TYPE_PARAM_STRUCT
:
1495 for (i
= 0; i
< NUM_PARAM
; i
++)
1496 if (t
->u
.param_struct
.param
[i
] != 0)
1497 set_gc_used_type (t
->u
.param_struct
.param
[i
], GC_USED
, NULL
);
1499 if (t
->u
.param_struct
.stru
->gc_used
== GC_POINTED_TO
)
1500 level
= GC_POINTED_TO
;
1503 t
->u
.param_struct
.stru
->gc_used
= GC_UNUSED
;
1504 set_gc_used_type (t
->u
.param_struct
.stru
, level
,
1505 t
->u
.param_struct
.param
);
1513 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1516 set_gc_used (pair_p variables
)
1519 for (p
= variables
; p
; p
= p
->next
)
1520 set_gc_used_type (p
->type
, GC_USED
, NULL
);
1523 /* File mapping routines. For each input file, there is one output .c file
1524 (but some output files have many input files), and there is one .h file
1525 for the whole build. */
1527 /* Output file handling. */
1529 /* Create and return an outf_p for a new file for NAME, to be called
1533 create_file (const char *name
, const char *oname
)
1535 static const char *const hdr
[] = {
1536 " Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc.\n",
1538 "This file is part of GCC.\n",
1540 "GCC is free software; you can redistribute it and/or modify it under\n",
1541 "the terms of the GNU General Public License as published by the Free\n",
1542 "Software Foundation; either version 3, or (at your option) any later\n",
1545 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1546 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1547 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1548 "for more details.\n",
1550 "You should have received a copy of the GNU General Public License\n",
1551 "along with GCC; see the file COPYING3. If not see\n",
1552 "<http://www.gnu.org/licenses/>. */\n",
1554 "/* This file is machine generated. Do not edit. */\n"
1559 gcc_assert (name
!= NULL
);
1560 gcc_assert (oname
!= NULL
);
1561 f
= XCNEW (struct outf
);
1562 f
->next
= output_files
;
1566 oprintf (f
, "/* Type information for %s.\n", name
);
1567 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1568 oprintf (f
, "%s", hdr
[i
]);
1572 /* Print, like fprintf, to O.
1573 N.B. You might think this could be implemented more efficiently
1574 with vsnprintf(). Unfortunately, there are C libraries that
1575 provide that function but without the C99 semantics for its return
1576 value, making it impossible to know how much space is required. */
1578 oprintf (outf_p o
, const char *format
, ...)
1584 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1589 va_start (ap
, format
);
1590 slength
= vasprintf (&s
, format
, ap
);
1591 if (s
== NULL
|| (int) slength
< 0)
1592 fatal ("out of memory");
1595 if (o
->bufused
+ slength
> o
->buflength
)
1597 size_t new_len
= o
->buflength
;
1604 while (o
->bufused
+ slength
>= new_len
);
1605 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1606 o
->buflength
= new_len
;
1608 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1609 o
->bufused
+= slength
;
1613 /* Open the global header file and the language-specific header files. */
1616 open_base_files (void)
1620 if (nb_plugin_files
> 0 && plugin_files
)
1623 header_file
= create_file ("GCC", "gtype-desc.h");
1625 base_files
= XNEWVEC (outf_p
, num_lang_dirs
);
1627 for (i
= 0; i
< num_lang_dirs
; i
++)
1628 base_files
[i
] = create_file (lang_dir_names
[i
],
1629 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1631 /* gtype-desc.c is a little special, so we create it here. */
1633 /* The order of files here matters very much. */
1634 static const char *const ifiles
[] = {
1635 "config.h", "system.h", "coretypes.h", "tm.h",
1636 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1637 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1638 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1639 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1640 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1641 "cfglayout.h", "except.h", "output.h", "gimple.h", "cfgloop.h",
1642 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h", NULL
1644 const char *const *ifp
;
1645 outf_p gtype_desc_c
;
1647 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1648 for (ifp
= ifiles
; *ifp
; ifp
++)
1649 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1651 /* Make sure we handle "cfun" specially. */
1652 oprintf (gtype_desc_c
, "\n/* See definition in function.h. */\n");
1653 oprintf (gtype_desc_c
, "#undef cfun\n");
1657 /* For F a filename, return the real basename of F, with all the directory
1658 components skipped. */
1661 get_file_realbasename (const char *f
)
1663 const char *lastslash
= strrchr (f
, '/');
1665 return (lastslash
!= NULL
) ? lastslash
+ 1 : f
;
1668 /* For F a filename, return the relative path to F from $(srcdir) if the
1669 latter is a prefix in F, NULL otherwise. */
1672 get_file_srcdir_relative_path (const char *f
)
1674 if (strlen (f
) > srcdir_len
1675 && IS_DIR_SEPARATOR (f
[srcdir_len
])
1676 && memcmp (f
, srcdir
, srcdir_len
) == 0)
1677 return f
+ srcdir_len
+ 1;
1682 /* For F a filename, return the relative path to F from $(srcdir) if the
1683 latter is a prefix in F, or the real basename of F otherwise. */
1686 get_file_basename (const char *f
)
1688 const char *srcdir_path
= get_file_srcdir_relative_path (f
);
1690 return (srcdir_path
!= NULL
) ? srcdir_path
: get_file_realbasename (f
);
1693 /* For F a filename, return the lang_dir_names relative index of the language
1694 directory that is a prefix in F, if any, -1 otherwise. */
1697 get_prefix_langdir_index (const char *f
)
1699 size_t f_len
= strlen (f
);
1702 for (lang_index
= 0; lang_index
< num_lang_dirs
; lang_index
++)
1704 const char *langdir
= lang_dir_names
[lang_index
];
1705 size_t langdir_len
= strlen (langdir
);
1707 if (f_len
> langdir_len
1708 && IS_DIR_SEPARATOR (f
[langdir_len
])
1709 && memcmp (f
, langdir
, langdir_len
) == 0)
1716 /* For F a filename, return the name of language directory where F is located,
1717 if any, NULL otherwise. */
1720 get_file_langdir (const char *f
)
1722 /* Get the relative path to F from $(srcdir) and find the language by
1723 comparing the prefix with language directory names. If F is not even
1724 srcdir relative, no point in looking further. */
1727 const char *srcdir_relative_path
= get_file_srcdir_relative_path (f
);
1730 if (!srcdir_relative_path
)
1733 lang_index
= get_prefix_langdir_index (srcdir_relative_path
);
1734 if (lang_index
< 0 && strncmp (srcdir_relative_path
, "c-family", 8) == 0)
1736 else if (lang_index
>= 0)
1737 r
= lang_dir_names
[lang_index
];
1744 /* The gt- output file name for F. */
1747 get_file_gtfilename (const char *f
)
1749 /* Cook up an initial version of the gt- file name from the file real
1750 basename and the language name, if any. */
1752 const char *basename
= get_file_realbasename (f
);
1753 const char *langdir
= get_file_langdir (f
);
1756 (langdir
? xasprintf ("gt-%s-%s", langdir
, basename
)
1757 : xasprintf ("gt-%s", basename
));
1759 /* Then replace all non alphanumerics characters by '-' and change the
1760 extension to ".h". We expect the input filename extension was at least
1761 one character long. */
1765 for (; *s
!= '.'; s
++)
1766 if (!ISALNUM (*s
) && *s
!= '-')
1769 memcpy (s
, ".h", sizeof (".h"));
1774 /* An output file, suitable for definitions, that can see declarations
1775 made in INPUT_FILE and is linked into every language that uses
1779 get_output_file_with_visibility (const char *input_file
)
1783 const char *basename
;
1784 const char *for_name
;
1785 const char *output_name
;
1787 /* This can happen when we need a file with visibility on a
1788 structure that we've never seen. We have to just hope that it's
1789 globally visible. */
1790 if (input_file
== NULL
)
1791 input_file
= "system.h";
1793 /* In plugin mode, return NULL unless the input_file is one of the
1798 for (i
= 0; i
< nb_plugin_files
; i
++)
1799 if (strcmp (input_file
, plugin_files
[i
]) == 0)
1800 return plugin_output
;
1805 /* Determine the output file name. */
1806 basename
= get_file_basename (input_file
);
1808 len
= strlen (basename
);
1809 if ((len
> 2 && memcmp (basename
+ len
- 2, ".c", 2) == 0)
1810 || (len
> 2 && memcmp (basename
+ len
- 2, ".y", 2) == 0)
1811 || (len
> 3 && memcmp (basename
+ len
- 3, ".in", 3) == 0))
1813 output_name
= get_file_gtfilename (input_file
);
1814 for_name
= basename
;
1816 /* Some headers get used by more than one front-end; hence, it
1817 would be inappropriate to spew them out to a single gtype-<lang>.h
1818 (and gengtype doesn't know how to direct spewage into multiple
1819 gtype-<lang>.h headers at this time). Instead, we pair up these
1820 headers with source files (and their special purpose gt-*.h headers). */
1821 else if (strncmp (basename
, "c-family", 8) == 0
1822 && IS_DIR_SEPARATOR (basename
[8])
1823 && strcmp (basename
+ 9, "c-common.h") == 0)
1824 output_name
= "gt-c-family-c-common.h", for_name
= "c-family/c-common.c";
1825 else if (strcmp (basename
, "c-lang.h") == 0)
1826 output_name
= "gt-c-decl.h", for_name
= "c-decl.c";
1827 else if (strcmp (basename
, "c-tree.h") == 0)
1828 output_name
= "gt-c-decl.h", for_name
= "c-decl.c";
1829 else if (strncmp (basename
, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename
[2])
1830 && strcmp (basename
+ 3, "cp-tree.h") == 0)
1831 output_name
= "gt-cp-tree.h", for_name
= "cp/tree.c";
1832 else if (strncmp (basename
, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename
[2])
1833 && strcmp (basename
+ 3, "decl.h") == 0)
1834 output_name
= "gt-cp-decl.h", for_name
= "cp/decl.c";
1835 else if (strncmp (basename
, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename
[2])
1836 && strcmp (basename
+ 3, "name-lookup.h") == 0)
1837 output_name
= "gt-cp-name-lookup.h", for_name
= "cp/name-lookup.c";
1838 else if (strncmp (basename
, "objc", 4) == 0
1839 && IS_DIR_SEPARATOR (basename
[4])
1840 && strcmp (basename
+ 5, "objc-act.h") == 0)
1841 output_name
= "gt-objc-objc-act.h", for_name
= "objc/objc-act.c";
1844 int lang_index
= get_prefix_langdir_index (basename
);
1846 if (lang_index
>= 0)
1847 return base_files
[lang_index
];
1849 output_name
= "gtype-desc.c";
1853 /* Look through to see if we've ever seen this output filename before. */
1854 for (r
= output_files
; r
; r
= r
->next
)
1855 if (strcmp (r
->name
, output_name
) == 0)
1858 /* If not, create it. */
1859 r
= create_file (for_name
, output_name
);
1861 gcc_assert (r
&& r
->name
);
1865 /* The name of an output file, suitable for definitions, that can see
1866 declarations made in INPUT_FILE and is linked into every language
1867 that uses INPUT_FILE. */
1870 get_output_file_name (const char *input_file
)
1872 outf_p o
= get_output_file_with_visibility (input_file
);
1878 /* Check if existing file is equal to the in memory buffer. */
1881 is_file_equal (outf_p of
)
1883 FILE *newfile
= fopen (of
->name
, "r");
1886 if (newfile
== NULL
)
1890 for (i
= 0; i
< of
->bufused
; i
++)
1893 ch
= fgetc (newfile
);
1894 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
1904 /* Copy the output to its final destination,
1905 but don't unnecessarily change modification times. */
1908 close_output_files (void)
1912 for (of
= output_files
; of
; of
= of
->next
)
1915 if (!is_file_equal (of
))
1917 FILE *newfile
= fopen (of
->name
, "w");
1918 if (newfile
== NULL
)
1919 fatal ("opening output file %s: %s", of
->name
, xstrerror (errno
));
1920 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
1921 fatal ("writing output file %s: %s", of
->name
, xstrerror (errno
));
1922 if (fclose (newfile
) != 0)
1923 fatal ("closing output file %s: %s", of
->name
, xstrerror (errno
));
1927 of
->bufused
= of
->buflength
= 0;
1939 struct walk_type_data
;
1941 /* For scalars and strings, given the item in 'val'.
1942 For structures, given a pointer to the item in 'val'.
1943 For misc. pointers, given the item in 'val'.
1945 typedef void (*process_field_fn
) (type_p f
, const struct walk_type_data
* p
);
1946 typedef void (*func_name_fn
) (type_p s
, const struct walk_type_data
* p
);
1948 /* Parameters for write_types. */
1950 struct write_types_data
1953 const char *param_prefix
;
1954 const char *subfield_marker_routine
;
1955 const char *marker_routine
;
1956 const char *reorder_note_routine
;
1957 const char *comment
;
1958 int skip_hooks
; /* skip hook generation if non zero */
1961 static void output_escaped_param (struct walk_type_data
*d
,
1962 const char *, const char *);
1963 static void output_mangled_typename (outf_p
, const_type_p
);
1964 static void walk_type (type_p t
, struct walk_type_data
*d
);
1965 static void write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
1966 const struct write_types_data
*wtd
);
1967 static void write_types_process_field
1968 (type_p f
, const struct walk_type_data
*d
);
1969 static void write_types (outf_p output_header
,
1971 type_p param_structs
,
1972 const struct write_types_data
*wtd
);
1973 static void write_types_local_process_field
1974 (type_p f
, const struct walk_type_data
*d
);
1975 static void write_local_func_for_structure
1976 (const_type_p orig_s
, type_p s
, type_p
*param
);
1977 static void write_local (outf_p output_header
,
1978 type_p structures
, type_p param_structs
);
1979 static void write_enum_defn (type_p structures
, type_p param_structs
);
1980 static int contains_scalar_p (type_p t
);
1981 static void put_mangled_filename (outf_p
, const char *);
1982 static void finish_root_table (struct flist
*flp
, const char *pfx
,
1983 const char *tname
, const char *lastname
,
1985 static void write_root (outf_p
, pair_p
, type_p
, const char *, int,
1986 struct fileloc
*, const char *, bool);
1987 static void write_array (outf_p f
, pair_p v
,
1988 const struct write_types_data
*wtd
);
1989 static void write_roots (pair_p
, bool);
1991 /* Parameters for walk_type. */
1993 struct walk_type_data
1995 process_field_fn process_field
;
2000 const char *prev_val
[4];
2003 const struct fileloc
*line
;
2008 const char *reorder_fn
;
2010 bool fn_wants_lvalue
;
2013 /* Print a mangled name representing T to OF. */
2016 output_mangled_typename (outf_p of
, const_type_p t
)
2025 output_mangled_typename (of
, t
->u
.p
);
2035 case TYPE_LANG_STRUCT
:
2036 oprintf (of
, "%lu%s", (unsigned long) strlen (t
->u
.s
.tag
),
2039 case TYPE_PARAM_STRUCT
:
2042 for (i
= 0; i
< NUM_PARAM
; i
++)
2043 if (t
->u
.param_struct
.param
[i
] != NULL
)
2044 output_mangled_typename (of
, t
->u
.param_struct
.param
[i
]);
2045 output_mangled_typename (of
, t
->u
.param_struct
.stru
);
2053 /* Print PARAM to D->OF processing escapes. D->VAL references the
2054 current object, D->PREV_VAL the object containing the current
2055 object, ONAME is the name of the option and D->LINE is used to
2056 print error messages. */
2059 output_escaped_param (struct walk_type_data
*d
, const char *param
,
2064 for (p
= param
; *p
; p
++)
2066 oprintf (d
->of
, "%c", *p
);
2071 oprintf (d
->of
, "(%s)", d
->prev_val
[2]);
2074 oprintf (d
->of
, "(%s)", d
->prev_val
[0]);
2077 oprintf (d
->of
, "(%s)", d
->prev_val
[1]);
2081 const char *pp
= d
->val
+ strlen (d
->val
);
2082 while (pp
[-1] == ']')
2085 oprintf (d
->of
, "%s", pp
);
2089 error_at_line (d
->line
, "`%s' option contains bad escape %c%c",
2094 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2095 which is of type T. Write code to D->OF to constrain execution (at
2096 the point that D->PROCESS_FIELD is called) to the appropriate
2097 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2098 pointers to those objects. D->PREV_VAL lists the objects
2099 containing the current object, D->OPT is a list of options to
2100 apply, D->INDENT is the current indentation level, D->LINE is used
2101 to print error messages, D->BITMAP indicates which languages to
2102 print the structure for, and D->PARAM is the current parameter
2103 (from an enclosing param_is option). */
2106 walk_type (type_p t
, struct walk_type_data
*d
)
2108 const char *length
= NULL
;
2109 const char *desc
= NULL
;
2110 int maybe_undef_p
= 0;
2111 int use_param_num
= -1;
2112 int use_params_p
= 0;
2114 const struct nested_ptr_data
*nested_ptr_d
= NULL
;
2116 d
->needs_cast_p
= false;
2117 for (oo
= d
->opt
; oo
; oo
= oo
->next
)
2118 if (strcmp (oo
->name
, "length") == 0)
2120 else if (strcmp (oo
->name
, "maybe_undef") == 0)
2122 else if (strncmp (oo
->name
, "use_param", 9) == 0
2123 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2124 use_param_num
= oo
->name
[9] == '\0' ? 0 : oo
->name
[9] - '0';
2125 else if (strcmp (oo
->name
, "use_params") == 0)
2127 else if (strcmp (oo
->name
, "desc") == 0)
2129 else if (strcmp (oo
->name
, "mark_hook") == 0)
2131 else if (strcmp (oo
->name
, "nested_ptr") == 0)
2132 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
;
2133 else if (strcmp (oo
->name
, "dot") == 0)
2135 else if (strcmp (oo
->name
, "tag") == 0)
2137 else if (strcmp (oo
->name
, "special") == 0)
2139 else if (strcmp (oo
->name
, "skip") == 0)
2141 else if (strcmp (oo
->name
, "default") == 0)
2143 else if (strcmp (oo
->name
, "descbits") == 0)
2145 else if (strcmp (oo
->name
, "param_is") == 0)
2147 else if (strncmp (oo
->name
, "param", 5) == 0
2148 && ISDIGIT (oo
->name
[5]) && strcmp (oo
->name
+ 6, "_is") == 0)
2150 else if (strcmp (oo
->name
, "chain_next") == 0)
2152 else if (strcmp (oo
->name
, "chain_prev") == 0)
2154 else if (strcmp (oo
->name
, "chain_circular") == 0)
2156 else if (strcmp (oo
->name
, "reorder") == 0)
2158 else if (strcmp (oo
->name
, "variable_size") == 0)
2161 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
2168 int pointer_p
= t
->kind
== TYPE_POINTER
;
2172 if (!UNION_OR_STRUCT_P (t
))
2173 error_at_line (d
->line
, "`use_params' option on unimplemented type");
2175 t
= find_param_structure (t
, d
->param
);
2177 t
= create_pointer (t
);
2180 if (use_param_num
!= -1)
2182 if (d
->param
!= NULL
&& d
->param
[use_param_num
] != NULL
)
2184 type_p nt
= d
->param
[use_param_num
];
2186 if (t
->kind
== TYPE_ARRAY
)
2187 nt
= create_array (nt
, t
->u
.a
.len
);
2188 else if (length
!= NULL
&& t
->kind
== TYPE_POINTER
)
2189 nt
= create_pointer (nt
);
2190 d
->needs_cast_p
= (t
->kind
!= TYPE_POINTER
2191 && (nt
->kind
== TYPE_POINTER
2192 || nt
->kind
== TYPE_STRING
));
2196 error_at_line (d
->line
, "no parameter defined for `%s'", d
->val
);
2200 && (t
->kind
!= TYPE_POINTER
|| !UNION_OR_STRUCT_P (t
->u
.p
)))
2202 error_at_line (d
->line
,
2203 "field `%s' has invalid option `maybe_undef_p'\n",
2212 d
->process_field (t
, d
);
2217 if (maybe_undef_p
&& t
->u
.p
->u
.s
.line
.file
== NULL
)
2219 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
2225 if (!UNION_OR_STRUCT_P (t
->u
.p
)
2226 && t
->u
.p
->kind
!= TYPE_PARAM_STRUCT
)
2228 error_at_line (d
->line
,
2229 "field `%s' is pointer to unimplemented type",
2236 const char *oldprevval2
= d
->prev_val
[2];
2238 if (!UNION_OR_STRUCT_P (nested_ptr_d
->type
))
2240 error_at_line (d
->line
,
2241 "field `%s' has invalid "
2242 "option `nested_ptr'\n", d
->val
);
2246 d
->prev_val
[2] = d
->val
;
2247 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2249 d
->val
= xasprintf ("x%d", d
->counter
++);
2250 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
2251 (nested_ptr_d
->type
->kind
== TYPE_UNION
2252 ? "union" : "struct"),
2253 nested_ptr_d
->type
->u
.s
.tag
,
2254 d
->fn_wants_lvalue
? "" : "const ", d
->val
);
2255 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
2256 output_escaped_param (d
, nested_ptr_d
->convert_from
,
2258 oprintf (d
->of
, ";\n");
2260 d
->process_field (nested_ptr_d
->type
, d
);
2262 if (d
->fn_wants_lvalue
)
2264 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
2266 d
->prev_val
[2] = d
->val
;
2267 output_escaped_param (d
, nested_ptr_d
->convert_to
,
2269 oprintf (d
->of
, ";\n");
2273 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2274 d
->val
= d
->prev_val
[2];
2275 d
->prev_val
[2] = oldprevval2
;
2278 d
->process_field (t
->u
.p
, d
);
2282 int loopcounter
= d
->counter
++;
2283 const char *oldval
= d
->val
;
2284 const char *oldprevval3
= d
->prev_val
[3];
2287 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2289 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2290 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
,
2291 "", loopcounter
, loopcounter
);
2292 output_escaped_param (d
, length
, "length");
2293 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
2295 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2297 d
->prev_val
[3] = oldval
;
2298 walk_type (t
->u
.p
, d
);
2301 d
->prev_val
[3] = oldprevval3
;
2304 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2305 d
->process_field (t
, d
);
2307 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2314 int loopcounter
= d
->counter
++;
2315 const char *oldval
= d
->val
;
2318 /* If it's an array of scalars, we optimize by not generating
2320 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
2323 /* When walking an array, compute the length and store it in a
2324 local variable before walking the array elements, instead of
2325 recomputing the length expression each time through the loop.
2326 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2327 where the length is stored in the first array element,
2328 because otherwise that operand can get overwritten on the
2330 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2332 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2333 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
2334 d
->indent
, "", loopcounter
);
2336 output_escaped_param (d
, length
, "length");
2338 oprintf (d
->of
, "%s", t
->u
.a
.len
);
2339 oprintf (d
->of
, ");\n");
2341 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2343 loopcounter
, loopcounter
, loopcounter
, loopcounter
);
2345 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2347 walk_type (t
->u
.a
.p
, d
);
2352 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2354 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2362 const char *oldval
= d
->val
;
2363 const char *oldprevval1
= d
->prev_val
[1];
2364 const char *oldprevval2
= d
->prev_val
[2];
2365 const int union_p
= t
->kind
== TYPE_UNION
;
2366 int seen_default_p
= 0;
2369 if (!t
->u
.s
.line
.file
)
2370 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
2372 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
2374 error_at_line (d
->line
,
2375 "structure `%s' defined for mismatching languages",
2377 error_at_line (&t
->u
.s
.line
, "one structure defined here");
2380 /* Some things may also be defined in the structure's options. */
2381 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
2382 if (!desc
&& strcmp (o
->name
, "desc") == 0)
2385 d
->prev_val
[2] = oldval
;
2386 d
->prev_val
[1] = oldprevval2
;
2391 error_at_line (d
->line
,
2392 "missing `desc' option for union `%s'",
2396 oprintf (d
->of
, "%*sswitch (", d
->indent
, "");
2397 output_escaped_param (d
, desc
, "desc");
2398 oprintf (d
->of
, ")\n");
2400 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2402 for (f
= t
->u
.s
.fields
; f
; f
= f
->next
)
2405 const char *dot
= ".";
2406 const char *tagid
= NULL
;
2409 int use_param_p
= 0;
2412 d
->reorder_fn
= NULL
;
2413 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
2414 if (strcmp (oo
->name
, "dot") == 0)
2416 else if (strcmp (oo
->name
, "tag") == 0)
2418 else if (strcmp (oo
->name
, "skip") == 0)
2420 else if (strcmp (oo
->name
, "default") == 0)
2422 else if (strcmp (oo
->name
, "reorder") == 0)
2423 d
->reorder_fn
= oo
->info
;
2424 else if (strncmp (oo
->name
, "use_param", 9) == 0
2425 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2431 if (union_p
&& tagid
)
2433 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
2436 else if (union_p
&& default_p
)
2438 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
2442 else if (!union_p
&& (default_p
|| tagid
))
2443 error_at_line (d
->line
,
2444 "can't use `%s' outside a union on field `%s'",
2445 default_p
? "default" : "tag", f
->name
);
2446 else if (union_p
&& !(default_p
|| tagid
)
2447 && f
->type
->kind
== TYPE_SCALAR
)
2450 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2451 d
->line
->file
, d
->line
->line
, f
->name
);
2454 else if (union_p
&& !(default_p
|| tagid
))
2455 error_at_line (d
->line
,
2456 "field `%s' is missing `tag' or `default' option",
2460 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
2462 d
->used_length
= false;
2464 if (union_p
&& use_param_p
&& d
->param
== NULL
)
2465 oprintf (d
->of
, "%*sgcc_unreachable ();\n", d
->indent
, "");
2467 walk_type (f
->type
, d
);
2473 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
2477 d
->reorder_fn
= NULL
;
2480 d
->prev_val
[1] = oldprevval1
;
2481 d
->prev_val
[2] = oldprevval2
;
2483 if (union_p
&& !seen_default_p
)
2485 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
2486 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
2490 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2496 case TYPE_LANG_STRUCT
:
2499 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
2500 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
2503 error_at_line (d
->line
, "structure `%s' differs between languages",
2510 case TYPE_PARAM_STRUCT
:
2512 type_p
*oldparam
= d
->param
;
2514 d
->param
= t
->u
.param_struct
.param
;
2515 walk_type (t
->u
.param_struct
.stru
, d
);
2516 d
->param
= oldparam
;
2525 /* process_field routine for marking routines. */
2528 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
2530 const struct write_types_data
*wtd
;
2531 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
2532 wtd
= (const struct write_types_data
*) d
->cookie
;
2537 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
2538 wtd
->subfield_marker_routine
, cast
, d
->val
);
2539 if (wtd
->param_prefix
)
2541 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
2544 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
2545 output_mangled_typename (d
->of
, d
->orig_s
);
2548 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
2550 if (f
->u
.p
->kind
== TYPE_PARAM_STRUCT
2551 && f
->u
.p
->u
.s
.line
.file
!= NULL
)
2553 oprintf (d
->of
, ", gt_e_");
2554 output_mangled_typename (d
->of
, f
);
2556 else if (UNION_OR_STRUCT_P (f
) && f
->u
.p
->u
.s
.line
.file
!= NULL
)
2558 oprintf (d
->of
, ", gt_ggc_e_");
2559 output_mangled_typename (d
->of
, f
);
2562 oprintf (d
->of
, ", gt_types_enum_last");
2564 oprintf (d
->of
, ");\n");
2565 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
2566 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
2567 wtd
->reorder_note_routine
, cast
, d
->val
,
2568 d
->prev_val
[3], d
->reorder_fn
);
2574 case TYPE_LANG_STRUCT
:
2575 case TYPE_PARAM_STRUCT
:
2576 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
2577 output_mangled_typename (d
->of
, f
);
2578 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
2579 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
2580 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
2581 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
2593 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
2596 output_type_enum (outf_p of
, type_p s
)
2598 if (s
->kind
== TYPE_PARAM_STRUCT
&& s
->u
.param_struct
.line
.file
!= NULL
)
2600 oprintf (of
, ", gt_e_");
2601 output_mangled_typename (of
, s
);
2603 else if (UNION_OR_STRUCT_P (s
) && s
->u
.s
.line
.file
!= NULL
)
2605 oprintf (of
, ", gt_ggc_e_");
2606 output_mangled_typename (of
, s
);
2609 oprintf (of
, ", gt_types_enum_last");
2612 /* Return an output file that is suitable for definitions which can
2613 reference struct S */
2616 get_output_file_for_structure (const_type_p s
, type_p
*param
)
2618 const char *fn
= s
->u
.s
.line
.file
;
2621 /* This is a hack, and not the good kind either. */
2622 for (i
= NUM_PARAM
- 1; i
>= 0; i
--)
2623 if (param
&& param
[i
] && param
[i
]->kind
== TYPE_POINTER
2624 && UNION_OR_STRUCT_P (param
[i
]->u
.p
))
2625 fn
= param
[i
]->u
.p
->u
.s
.line
.file
;
2627 return get_output_file_with_visibility (fn
);
2630 /* For S, a structure that's part of ORIG_S, and using parameters
2631 PARAM, write out a routine that:
2632 - Takes a parameter, a void * but actually of type *S
2633 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
2634 field of S or its substructures and (in some cases) things
2635 that are pointed to by S.
2639 write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
2640 const struct write_types_data
*wtd
)
2642 const char *chain_next
= NULL
;
2643 const char *chain_prev
= NULL
;
2644 const char *chain_circular
= NULL
;
2645 const char *mark_hook_name
= NULL
;
2647 struct walk_type_data d
;
2649 memset (&d
, 0, sizeof (d
));
2650 d
.of
= get_output_file_for_structure (s
, param
);
2652 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2653 if (strcmp (opt
->name
, "chain_next") == 0)
2654 chain_next
= opt
->info
;
2655 else if (strcmp (opt
->name
, "chain_prev") == 0)
2656 chain_prev
= opt
->info
;
2657 else if (strcmp (opt
->name
, "chain_circular") == 0)
2658 chain_circular
= opt
->info
;
2659 else if (strcmp (opt
->name
, "mark_hook") == 0)
2660 mark_hook_name
= opt
->info
;
2662 if (chain_prev
!= NULL
&& chain_next
== NULL
)
2663 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
2664 if (chain_circular
!= NULL
&& chain_next
!= NULL
)
2665 error_at_line (&s
->u
.s
.line
, "chain_circular with chain_next");
2666 if (chain_circular
!= NULL
)
2667 chain_next
= chain_circular
;
2669 d
.process_field
= write_types_process_field
;
2673 d
.line
= &s
->u
.s
.line
;
2674 d
.bitmap
= s
->u
.s
.bitmap
;
2676 d
.prev_val
[0] = "*x";
2677 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
2678 d
.prev_val
[3] = "x";
2681 oprintf (d
.of
, "\n");
2682 oprintf (d
.of
, "void\n");
2684 oprintf (d
.of
, "gt_%sx_%s", wtd
->prefix
, orig_s
->u
.s
.tag
);
2687 oprintf (d
.of
, "gt_%s_", wtd
->prefix
);
2688 output_mangled_typename (d
.of
, orig_s
);
2690 oprintf (d
.of
, " (void *x_p)\n");
2691 oprintf (d
.of
, "{\n");
2692 oprintf (d
.of
, " %s %s * %sx = (%s %s *)x_p;\n",
2693 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
2694 chain_next
== NULL
? "const " : "",
2695 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2696 if (chain_next
!= NULL
)
2697 oprintf (d
.of
, " %s %s * xlimit = x;\n",
2698 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2699 if (chain_next
== NULL
)
2701 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
2702 if (wtd
->param_prefix
)
2704 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
2705 output_mangled_typename (d
.of
, orig_s
);
2706 output_type_enum (d
.of
, orig_s
);
2708 oprintf (d
.of
, "))\n");
2712 if (chain_circular
!= NULL
)
2713 oprintf (d
.of
, " if (!%s (xlimit", wtd
->marker_routine
);
2715 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
2716 if (wtd
->param_prefix
)
2718 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
2719 output_mangled_typename (d
.of
, orig_s
);
2720 output_type_enum (d
.of
, orig_s
);
2722 oprintf (d
.of
, "))\n");
2723 if (chain_circular
!= NULL
)
2724 oprintf (d
.of
, " return;\n do\n");
2725 if (mark_hook_name
&& !wtd
->skip_hooks
)
2727 oprintf (d
.of
, " {\n");
2728 oprintf (d
.of
, " %s (xlimit);\n ", mark_hook_name
);
2730 oprintf (d
.of
, " xlimit = (");
2731 d
.prev_val
[2] = "*xlimit";
2732 output_escaped_param (&d
, chain_next
, "chain_next");
2733 oprintf (d
.of
, ");\n");
2734 if (mark_hook_name
&& !wtd
->skip_hooks
)
2735 oprintf (d
.of
, " }\n");
2736 if (chain_prev
!= NULL
)
2738 oprintf (d
.of
, " if (x != xlimit)\n");
2739 oprintf (d
.of
, " for (;;)\n");
2740 oprintf (d
.of
, " {\n");
2741 oprintf (d
.of
, " %s %s * const xprev = (",
2742 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2744 d
.prev_val
[2] = "*x";
2745 output_escaped_param (&d
, chain_prev
, "chain_prev");
2746 oprintf (d
.of
, ");\n");
2747 oprintf (d
.of
, " if (xprev == NULL) break;\n");
2748 oprintf (d
.of
, " x = xprev;\n");
2749 oprintf (d
.of
, " (void) %s (xprev", wtd
->marker_routine
);
2750 if (wtd
->param_prefix
)
2752 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
2753 output_mangled_typename (d
.of
, orig_s
);
2754 output_type_enum (d
.of
, orig_s
);
2756 oprintf (d
.of
, ");\n");
2757 oprintf (d
.of
, " }\n");
2759 if (chain_circular
!= NULL
)
2761 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
2762 if (wtd
->param_prefix
)
2764 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
2765 output_mangled_typename (d
.of
, orig_s
);
2766 output_type_enum (d
.of
, orig_s
);
2768 oprintf (d
.of
, "));\n");
2769 if (mark_hook_name
&& !wtd
->skip_hooks
)
2770 oprintf (d
.of
, " %s (xlimit);\n", mark_hook_name
);
2771 oprintf (d
.of
, " do\n");
2774 oprintf (d
.of
, " while (x != xlimit)\n");
2776 oprintf (d
.of
, " {\n");
2777 if (mark_hook_name
&& chain_next
== NULL
&& !wtd
->skip_hooks
)
2779 oprintf (d
.of
, " %s (x);\n", mark_hook_name
);
2781 d
.prev_val
[2] = "*x";
2785 if (chain_next
!= NULL
)
2787 oprintf (d
.of
, " x = (");
2788 output_escaped_param (&d
, chain_next
, "chain_next");
2789 oprintf (d
.of
, ");\n");
2792 oprintf (d
.of
, " }\n");
2793 if (chain_circular
!= NULL
)
2794 oprintf (d
.of
, " while (x != xlimit);\n");
2795 oprintf (d
.of
, "}\n");
2798 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
2801 write_types (outf_p output_header
, type_p structures
, type_p param_structs
,
2802 const struct write_types_data
*wtd
)
2806 oprintf (output_header
, "\n/* %s*/\n", wtd
->comment
);
2807 /* We first emit the macros and the declarations. Functions' code is
2808 emitted afterwards. This is needed in plugin mode. */
2809 oprintf (output_header
, "/* macros and declarations */\n");
2810 for (s
= structures
; s
; s
= s
->next
)
2811 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
2815 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
2818 oprintf (output_header
, "#define gt_%s_", wtd
->prefix
);
2819 output_mangled_typename (output_header
, s
);
2820 oprintf (output_header
, "(X) do { \\\n");
2821 oprintf (output_header
,
2822 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd
->prefix
,
2824 oprintf (output_header
, " } while (0)\n");
2826 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2827 if (strcmp (opt
->name
, "ptr_alias") == 0)
2829 const_type_p
const t
= (const_type_p
) opt
->info
;
2830 if (t
->kind
== TYPE_STRUCT
2831 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
2832 oprintf (output_header
,
2833 "#define gt_%sx_%s gt_%sx_%s\n",
2834 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t
->u
.s
.tag
);
2836 error_at_line (&s
->u
.s
.line
,
2837 "structure alias is not a structure");
2843 /* Declare the marker procedure only once. */
2844 oprintf (output_header
,
2845 "extern void gt_%sx_%s (void *);\n",
2846 wtd
->prefix
, s
->u
.s
.tag
);
2848 if (s
->u
.s
.line
.file
== NULL
)
2850 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
2856 for (s
= param_structs
; s
; s
= s
->next
)
2857 if (s
->gc_used
== GC_POINTED_TO
)
2859 type_p stru
= s
->u
.param_struct
.stru
;
2861 /* Declare the marker procedure. */
2862 oprintf (output_header
, "extern void gt_%s_", wtd
->prefix
);
2863 output_mangled_typename (output_header
, s
);
2864 oprintf (output_header
, " (void *);\n");
2866 if (stru
->u
.s
.line
.file
== NULL
)
2868 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
2874 /* At last we emit the functions code. */
2875 oprintf (output_header
, "\n/* functions code */\n");
2876 for (s
= structures
; s
; s
= s
->next
)
2877 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
2881 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
2883 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2884 if (strcmp (opt
->name
, "ptr_alias") == 0)
2889 if (s
->kind
== TYPE_LANG_STRUCT
)
2892 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
2893 write_func_for_structure (s
, ss
, NULL
, wtd
);
2896 write_func_for_structure (s
, s
, NULL
, wtd
);
2898 for (s
= param_structs
; s
; s
= s
->next
)
2899 if (s
->gc_used
== GC_POINTED_TO
)
2901 type_p
*param
= s
->u
.param_struct
.param
;
2902 type_p stru
= s
->u
.param_struct
.stru
;
2903 if (stru
->u
.s
.line
.file
== NULL
)
2905 if (stru
->kind
== TYPE_LANG_STRUCT
)
2908 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
2909 write_func_for_structure (s
, ss
, param
, wtd
);
2912 write_func_for_structure (s
, stru
, param
, wtd
);
2916 static const struct write_types_data ggc_wtd
= {
2917 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
2918 "GC marker procedures. ",
2922 static const struct write_types_data pch_wtd
= {
2923 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
2924 "gt_pch_note_reorder",
2925 "PCH type-walking procedures. ",
2929 /* Write out the local pointer-walking routines. */
2931 /* process_field routine for local pointer-walking. */
2934 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
2941 case TYPE_LANG_STRUCT
:
2942 case TYPE_PARAM_STRUCT
:
2944 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
2946 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
2957 /* For S, a structure that's part of ORIG_S, and using parameters
2958 PARAM, write out a routine that:
2959 - Is of type gt_note_pointers
2960 - Calls PROCESS_FIELD on each field of S or its substructures.
2964 write_local_func_for_structure (const_type_p orig_s
, type_p s
, type_p
*param
)
2966 struct walk_type_data d
;
2968 memset (&d
, 0, sizeof (d
));
2969 d
.of
= get_output_file_for_structure (s
, param
);
2970 d
.process_field
= write_types_local_process_field
;
2972 d
.line
= &s
->u
.s
.line
;
2973 d
.bitmap
= s
->u
.s
.bitmap
;
2975 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
2976 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
2977 d
.prev_val
[3] = "x";
2979 d
.fn_wants_lvalue
= true;
2981 oprintf (d
.of
, "\n");
2982 oprintf (d
.of
, "void\n");
2983 oprintf (d
.of
, "gt_pch_p_");
2984 output_mangled_typename (d
.of
, orig_s
);
2985 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
2987 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
2988 "\tATTRIBUTE_UNUSED void *cookie)\n");
2989 oprintf (d
.of
, "{\n");
2990 oprintf (d
.of
, " %s %s * const x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
2991 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
2992 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2995 oprintf (d
.of
, "}\n");
2998 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3001 write_local (outf_p output_header
, type_p structures
, type_p param_structs
)
3007 oprintf (output_header
, "\n/* Local pointer-walking routines. */\n");
3008 for (s
= structures
; s
; s
= s
->next
)
3009 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3013 if (s
->u
.s
.line
.file
== NULL
)
3016 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3017 if (strcmp (opt
->name
, "ptr_alias") == 0)
3019 const_type_p
const t
= (const_type_p
) opt
->info
;
3020 if (t
->kind
== TYPE_STRUCT
3021 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3023 oprintf (output_header
, "#define gt_pch_p_");
3024 output_mangled_typename (output_header
, s
);
3025 oprintf (output_header
, " gt_pch_p_");
3026 output_mangled_typename (output_header
, t
);
3027 oprintf (output_header
, "\n");
3030 error_at_line (&s
->u
.s
.line
,
3031 "structure alias is not a structure");
3037 /* Declare the marker procedure only once. */
3038 oprintf (output_header
, "extern void gt_pch_p_");
3039 output_mangled_typename (output_header
, s
);
3040 oprintf (output_header
,
3041 "\n (void *, void *, gt_pointer_operator, void *);\n");
3043 if (s
->kind
== TYPE_LANG_STRUCT
)
3046 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3047 write_local_func_for_structure (s
, ss
, NULL
);
3050 write_local_func_for_structure (s
, s
, NULL
);
3053 for (s
= param_structs
; s
; s
= s
->next
)
3054 if (s
->gc_used
== GC_POINTED_TO
)
3056 type_p
*param
= s
->u
.param_struct
.param
;
3057 type_p stru
= s
->u
.param_struct
.stru
;
3059 /* Declare the marker procedure. */
3060 oprintf (output_header
, "extern void gt_pch_p_");
3061 output_mangled_typename (output_header
, s
);
3062 oprintf (output_header
,
3063 "\n (void *, void *, gt_pointer_operator, void *);\n");
3065 if (stru
->u
.s
.line
.file
== NULL
)
3067 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3072 if (stru
->kind
== TYPE_LANG_STRUCT
)
3075 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3076 write_local_func_for_structure (s
, ss
, param
);
3079 write_local_func_for_structure (s
, stru
, param
);
3083 /* Nonzero if S is a type for which typed GC allocators should be output. */
3085 #define USED_BY_TYPED_GC_P(s) \
3086 (((s->kind == TYPE_POINTER) \
3087 && ((s->u.p->gc_used == GC_POINTED_TO) \
3088 || (s->u.p->gc_used == GC_USED))) \
3089 || (UNION_OR_STRUCT_P (s) && \
3090 (((s)->gc_used == GC_POINTED_TO) \
3091 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
3092 && s->u.s.line.file != NULL) \
3093 || ((s)->gc_used == GC_USED \
3094 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
3097 /* Write out the 'enum' definition for gt_types_enum. */
3100 write_enum_defn (type_p structures
, type_p param_structs
)
3106 oprintf (header_file
, "\n/* Enumeration of types known. */\n");
3107 oprintf (header_file
, "enum gt_types_enum {\n");
3108 for (s
= structures
; s
; s
= s
->next
)
3109 if (USED_BY_TYPED_GC_P (s
))
3111 oprintf (header_file
, " gt_ggc_e_");
3112 output_mangled_typename (header_file
, s
);
3113 oprintf (header_file
, ",\n");
3115 for (s
= param_structs
; s
; s
= s
->next
)
3116 if (s
->gc_used
== GC_POINTED_TO
)
3118 oprintf (header_file
, " gt_e_");
3119 output_mangled_typename (header_file
, s
);
3120 oprintf (header_file
, ",\n");
3122 oprintf (header_file
, " gt_types_enum_last\n");
3123 oprintf (header_file
, "};\n");
3126 /* Might T contain any non-pointer elements? */
3129 contains_scalar_p (type_p t
)
3137 return contains_scalar_p (t
->u
.a
.p
);
3139 /* Could also check for structures that have no non-pointer
3140 fields, but there aren't enough of those to worry about. */
3145 /* Mangle FN and print it to F. */
3148 put_mangled_filename (outf_p f
, const char *fn
)
3150 const char *name
= get_output_file_name (fn
);
3153 for (; *name
!= 0; name
++)
3154 if (ISALNUM (*name
))
3155 oprintf (f
, "%c", *name
);
3157 oprintf (f
, "%c", '_');
3160 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
3161 LASTNAME, and NAME are all strings to insert in various places in
3162 the resulting code. */
3165 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
3166 const char *tname
, const char *name
)
3170 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
3171 if (fli2
->started_p
)
3173 oprintf (fli2
->f
, " %s\n", lastname
);
3174 oprintf (fli2
->f
, "};\n\n");
3177 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
3178 if (fli2
->started_p
)
3180 lang_bitmap bitmap
= get_lang_bitmap (fli2
->name
);
3183 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
3186 oprintf (base_files
[fnum
],
3187 "extern const struct %s gt_%s_", tname
, pfx
);
3188 put_mangled_filename (base_files
[fnum
], fli2
->name
);
3189 oprintf (base_files
[fnum
], "[];\n");
3195 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
3196 oprintf (base_files
[fnum
],
3197 "EXPORTED_CONST struct %s * const %s[] = {\n", tname
, name
);
3201 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
3202 if (fli2
->started_p
)
3204 lang_bitmap bitmap
= get_lang_bitmap (fli2
->name
);
3207 fli2
->started_p
= 0;
3209 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
3212 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
3213 put_mangled_filename (base_files
[fnum
], fli2
->name
);
3214 oprintf (base_files
[fnum
], ",\n");
3220 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
3222 oprintf (base_files
[fnum
], " NULL\n");
3223 oprintf (base_files
[fnum
], "};\n");
3228 /* Write the first three fields (pointer, count and stride) for
3229 root NAME to F. V and LINE are as for write_root.
3231 Return true if the entry could be written; return false on error. */
3234 start_root_entry (outf_p f
, pair_p v
, const char *name
, struct fileloc
*line
)
3240 error_at_line (line
, "`%s' is too complex to be a root", name
);
3244 oprintf (f
, " {\n");
3245 oprintf (f
, " &%s,\n", name
);
3248 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
3250 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
3251 else if (ap
== v
->type
)
3252 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
3254 oprintf (f
, " sizeof (%s", v
->name
);
3255 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
3257 oprintf (f
, "),\n");
3261 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
3262 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
3266 write_field_root (outf_p f
, pair_p v
, type_p type
, const char *name
,
3267 int has_length
, struct fileloc
*line
, const char *if_marked
,
3268 bool emit_pch
, type_p field_type
, const char *field_name
)
3270 /* If the field reference is relative to V, rather than to some
3271 subcomponent of V, we can mark any subarrays with a single stride.
3272 We're effectively treating the field as a global variable in its
3274 if (v
&& type
== v
->type
)
3279 newv
.type
= field_type
;
3280 newv
.name
= ACONCAT ((v
->name
, ".", field_name
, NULL
));
3283 /* Otherwise, any arrays nested in the structure are too complex to
3285 else if (field_type
->kind
== TYPE_ARRAY
)
3287 write_root (f
, v
, field_type
, ACONCAT ((name
, ".", field_name
, NULL
)),
3288 has_length
, line
, if_marked
, emit_pch
);
3291 /* Write out to F the table entry and any marker routines needed to
3292 mark NAME as TYPE. V can be one of three values:
3294 - null, if NAME is too complex to represent using a single
3295 count and stride. In this case, it is an error for NAME to
3296 contain any gc-ed data.
3298 - the outermost array that contains NAME, if NAME is part of an array.
3300 - the C variable that contains NAME, if NAME is not part of an array.
3302 LINE is the line of the C source that declares the root variable.
3303 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
3304 is nonzero iff we are building the root table for hash table caches. */
3307 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
3308 struct fileloc
*line
, const char *if_marked
, bool emit_pch
)
3315 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
3318 const char *desc
= NULL
;
3321 for (o
= fld
->opt
; o
; o
= o
->next
)
3322 if (strcmp (o
->name
, "skip") == 0)
3324 else if (strcmp (o
->name
, "desc") == 0)
3326 else if (strcmp (o
->name
, "param_is") == 0)
3329 error_at_line (line
,
3330 "field `%s' of global `%s' has unknown option `%s'",
3331 fld
->name
, name
, o
->name
);
3335 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
3337 pair_p validf
= NULL
;
3340 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
3342 const char *tag
= NULL
;
3345 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
3346 if (strcmp (oo
->name
, "tag") == 0)
3348 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
3351 error_at_line (line
,
3352 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
3353 name
, fld
->name
, validf
->name
,
3354 name
, fld
->name
, ufld
->name
, tag
);
3358 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
3359 emit_pch
, validf
->type
,
3360 ACONCAT ((fld
->name
, ".",
3361 validf
->name
, NULL
)));
3364 error_at_line (line
,
3365 "global `%s.%s' has `desc' option but is not union",
3368 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
3369 emit_pch
, fld
->type
, fld
->name
);
3377 newname
= xasprintf ("%s[0]", name
);
3378 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, if_marked
,
3388 if (!start_root_entry (f
, v
, name
, line
))
3393 if (!has_length
&& UNION_OR_STRUCT_P (tp
))
3395 oprintf (f
, " >_ggc_mx_%s,\n", tp
->u
.s
.tag
);
3397 oprintf (f
, " >_pch_nx_%s", tp
->u
.s
.tag
);
3399 oprintf (f
, " NULL");
3401 else if (!has_length
&& tp
->kind
== TYPE_PARAM_STRUCT
)
3403 oprintf (f
, " >_ggc_m_");
3404 output_mangled_typename (f
, tp
);
3407 oprintf (f
, ",\n >_pch_n_");
3408 output_mangled_typename (f
, tp
);
3411 oprintf (f
, ",\n NULL");
3414 && (tp
->kind
== TYPE_POINTER
|| UNION_OR_STRUCT_P (tp
)))
3416 oprintf (f
, " >_ggc_ma_%s,\n", name
);
3418 oprintf (f
, " >_pch_na_%s", name
);
3420 oprintf (f
, " NULL");
3424 error_at_line (line
,
3425 "global `%s' is pointer to unimplemented type",
3429 oprintf (f
, ",\n &%s", if_marked
);
3430 oprintf (f
, "\n },\n");
3436 if (!start_root_entry (f
, v
, name
, line
))
3439 oprintf (f
, " (gt_pointer_walker) >_ggc_m_S,\n");
3440 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
3441 oprintf (f
, " },\n");
3449 error_at_line (line
, "global `%s' is unimplemented type", name
);
3453 /* This generates a routine to walk an array. */
3456 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
3458 struct walk_type_data d
;
3461 memset (&d
, 0, sizeof (d
));
3467 d
.bitmap
= get_lang_bitmap (v
->line
.file
);
3470 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
3472 if (wtd
->param_prefix
)
3474 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
3475 oprintf (f
, " (void *, void *, gt_pointer_operator, void *);\n");
3476 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
3477 wtd
->param_prefix
, v
->name
);
3479 " ATTRIBUTE_UNUSED void *x_p,\n"
3480 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3481 " ATTRIBUTE_UNUSED void * cookie)\n");
3482 oprintf (d
.of
, "{\n");
3483 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
3484 d
.process_field
= write_types_local_process_field
;
3485 walk_type (v
->type
, &d
);
3486 oprintf (f
, "}\n\n");
3490 oprintf (f
, "static void gt_%sa_%s (void *);\n", wtd
->prefix
, v
->name
);
3491 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
3492 wtd
->prefix
, v
->name
);
3494 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
3495 d
.process_field
= write_types_process_field
;
3496 walk_type (v
->type
, &d
);
3498 oprintf (f
, "}\n\n");
3501 /* Output a table describing the locations and types of VARIABLES. */
3504 write_roots (pair_p variables
, bool emit_pch
)
3507 struct flist
*flp
= NULL
;
3509 for (v
= variables
; v
; v
= v
->next
)
3511 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3513 const char *length
= NULL
;
3514 int deletable_p
= 0;
3517 for (o
= v
->opt
; o
; o
= o
->next
)
3518 if (strcmp (o
->name
, "length") == 0)
3520 else if (strcmp (o
->name
, "deletable") == 0)
3522 else if (strcmp (o
->name
, "param_is") == 0)
3524 else if (strncmp (o
->name
, "param", 5) == 0
3525 && ISDIGIT (o
->name
[5]) && strcmp (o
->name
+ 6, "_is") == 0)
3527 else if (strcmp (o
->name
, "if_marked") == 0)
3530 error_at_line (&v
->line
,
3531 "global `%s' has unknown option `%s'",
3534 for (fli
= flp
; fli
; fli
= fli
->next
)
3535 if (fli
->f
== f
&& f
)
3539 fli
= XNEW (struct flist
);
3543 fli
->name
= v
->line
.file
;
3544 gcc_assert (fli
->name
);
3547 oprintf (f
, "\n/* GC roots. */\n\n");
3552 && v
->type
->kind
== TYPE_POINTER
3553 && (v
->type
->u
.p
->kind
== TYPE_POINTER
3554 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
3556 write_array (f
, v
, &ggc_wtd
);
3557 write_array (f
, v
, &pch_wtd
);
3561 for (v
= variables
; v
; v
= v
->next
)
3563 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3569 for (o
= v
->opt
; o
; o
= o
->next
)
3570 if (strcmp (o
->name
, "length") == 0)
3572 else if (strcmp (o
->name
, "deletable") == 0
3573 || strcmp (o
->name
, "if_marked") == 0)
3579 for (fli
= flp
; fli
; fli
= fli
->next
)
3582 if (!fli
->started_p
)
3586 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
3587 put_mangled_filename (f
, v
->line
.file
);
3588 oprintf (f
, "[] = {\n");
3591 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
3594 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3597 for (v
= variables
; v
; v
= v
->next
)
3599 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3604 for (o
= v
->opt
; o
; o
= o
->next
)
3605 if (strcmp (o
->name
, "deletable") == 0)
3607 else if (strcmp (o
->name
, "if_marked") == 0)
3613 for (fli
= flp
; fli
; fli
= fli
->next
)
3616 if (!fli
->started_p
)
3620 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
3621 put_mangled_filename (f
, v
->line
.file
);
3622 oprintf (f
, "[] = {\n");
3625 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3629 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3630 "gt_ggc_deletable_rtab");
3632 for (v
= variables
; v
; v
= v
->next
)
3634 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3636 const char *if_marked
= NULL
;
3640 for (o
= v
->opt
; o
; o
= o
->next
)
3641 if (strcmp (o
->name
, "length") == 0)
3643 else if (strcmp (o
->name
, "if_marked") == 0)
3644 if_marked
= o
->info
;
3646 if (if_marked
== NULL
)
3649 if (v
->type
->kind
!= TYPE_POINTER
3650 || v
->type
->u
.p
->kind
!= TYPE_PARAM_STRUCT
3651 || v
->type
->u
.p
->u
.param_struct
.stru
!= find_structure ("htab", 0))
3653 error_at_line (&v
->line
,
3654 "if_marked option used but not hash table");
3658 for (fli
= flp
; fli
; fli
= fli
->next
)
3661 if (!fli
->started_p
)
3665 oprintf (f
, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
3666 put_mangled_filename (f
, v
->line
.file
);
3667 oprintf (f
, "[] = {\n");
3670 write_root (f
, v
, v
->type
->u
.p
->u
.param_struct
.param
[0],
3671 v
->name
, length_p
, &v
->line
, if_marked
, emit_pch
);
3674 finish_root_table (flp
, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
3675 "gt_ggc_cache_rtab");
3680 for (v
= variables
; v
; v
= v
->next
)
3682 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3685 int if_marked_p
= 0;
3688 for (o
= v
->opt
; o
; o
= o
->next
)
3689 if (strcmp (o
->name
, "length") == 0)
3691 else if (strcmp (o
->name
, "if_marked") == 0)
3697 for (fli
= flp
; fli
; fli
= fli
->next
)
3700 if (!fli
->started_p
)
3704 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
3705 put_mangled_filename (f
, v
->line
.file
);
3706 oprintf (f
, "[] = {\n");
3709 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
3712 finish_root_table (flp
, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3713 "gt_pch_cache_rtab");
3715 for (v
= variables
; v
; v
= v
->next
)
3717 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3722 for (o
= v
->opt
; o
; o
= o
->next
)
3723 if (strcmp (o
->name
, "deletable") == 0
3724 || strcmp (o
->name
, "if_marked") == 0)
3730 if (!contains_scalar_p (v
->type
))
3733 for (fli
= flp
; fli
; fli
= fli
->next
)
3736 if (!fli
->started_p
)
3740 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
3741 put_mangled_filename (f
, v
->line
.file
);
3742 oprintf (f
, "[] = {\n");
3745 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3749 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3750 "gt_pch_scalar_rtab");
3753 /* Record the definition of a generic VEC structure, as if we had expanded
3754 the macros in vec.h:
3756 typedef struct VEC_<type>_base GTY(()) {
3759 <type> GTY((length ("%h.num"))) vec[1];
3762 where the GTY(()) tags are only present if is_scalar is _false_. */
3765 note_def_vec (const char *type_name
, bool is_scalar
, struct fileloc
*pos
)
3770 type_p len_ty
= create_scalar_type ("unsigned");
3771 const char *name
= concat ("VEC_", type_name
, "_base", (char *) 0);
3775 t
= create_scalar_type (type_name
);
3780 t
= resolve_typedef (type_name
, pos
);
3781 o
= create_option (0, "length", "%h.num");
3784 /* We assemble the field list in reverse order. */
3785 fields
= create_field_at (0, create_array (t
, "1"), "vec", o
, pos
);
3786 fields
= create_field_at (fields
, len_ty
, "alloc", 0, pos
);
3787 fields
= create_field_at (fields
, len_ty
, "num", 0, pos
);
3789 do_typedef (name
, new_structure (name
, 0, pos
, fields
, 0), pos
);
3792 /* Record the definition of an allocation-specific VEC structure, as if
3793 we had expanded the macros in vec.h:
3795 typedef struct VEC_<type>_<astrat> {
3796 VEC_<type>_base base;
3797 } VEC_<type>_<astrat>;
3800 note_def_vec_alloc (const char *type
, const char *astrat
, struct fileloc
*pos
)
3802 const char *astratname
= concat ("VEC_", type
, "_", astrat
, (char *) 0);
3803 const char *basename
= concat ("VEC_", type
, "_base", (char *) 0);
3805 pair_p field
= create_field_at (0, resolve_typedef (basename
, pos
),
3808 do_typedef (astratname
, new_structure (astratname
, 0, pos
, field
, 0), pos
);
3811 /* Returns the specifier keyword for a string or union type S, empty string
3815 get_type_specifier (const type_p s
)
3817 if (s
->kind
== TYPE_STRUCT
|| s
->kind
== TYPE_LANG_STRUCT
)
3819 if (s
->kind
== TYPE_UNION
)
3824 /* TRUE if type S has the GTY variable_size annotation. */
3827 variable_size_p (const type_p s
)
3830 for (o
= s
->u
.s
.opt
; o
; o
= o
->next
)
3831 if (strcmp (o
->name
, "variable_size") == 0)
3839 { any_zone
, specific_zone
};
3841 /* Writes one typed allocator definition for type identifier TYPE_NAME with
3842 optional type specifier TYPE_SPECIFIER. The allocator name will contain
3843 ALLOCATOR_TYPE. If VARIABLE_SIZE is true, the allocator will have an extra
3844 parameter specifying number of bytes to allocate. If QUANTITY is set to
3845 VECTOR, a vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
3846 the allocator will be zone-specific. */
3849 write_typed_alloc_def (bool variable_size
, const char *type_specifier
,
3850 const char *type_name
, const char *allocator_type
,
3851 enum alloc_quantity quantity
, enum alloc_zone zone
)
3853 bool two_args
= variable_size
&& (quantity
== vector
);
3854 bool third_arg
= ((zone
== specific_zone
)
3855 && (variable_size
|| (quantity
== vector
)));
3857 oprintf (header_file
, "#define ggc_alloc_%s%s", allocator_type
, type_name
);
3858 oprintf (header_file
, "(%s%s%s%s%s) ",
3859 (variable_size
? "SIZE" : ""),
3860 (two_args
? ", " : ""),
3861 (quantity
== vector
) ? "n" : "",
3862 (third_arg
? ", " : ""), (zone
== specific_zone
) ? "z" : "");
3863 oprintf (header_file
, "((%s%s *)", type_specifier
, type_name
);
3864 oprintf (header_file
, "(ggc_internal_%salloc_stat (", allocator_type
);
3865 if (zone
== specific_zone
)
3866 oprintf (header_file
, "z, ");
3868 oprintf (header_file
, "SIZE");
3870 oprintf (header_file
, "sizeof (%s%s)", type_specifier
, type_name
);
3871 if (quantity
== vector
)
3872 oprintf (header_file
, ", n");
3873 oprintf (header_file
, " MEM_STAT_INFO)))\n");
3876 /* Writes a typed allocator definition for a struct or union S. */
3879 write_typed_struct_alloc_def (const type_p s
, const char *allocator_type
,
3880 enum alloc_quantity quantity
,
3881 enum alloc_zone zone
)
3883 write_typed_alloc_def (variable_size_p (s
), get_type_specifier (s
),
3884 s
->u
.s
.tag
, allocator_type
, quantity
, zone
);
3887 /* Writes a typed allocator definition for a typedef P. */
3890 write_typed_typedef_alloc_def (const pair_p p
, const char *allocator_type
,
3891 enum alloc_quantity quantity
,
3892 enum alloc_zone zone
)
3894 write_typed_alloc_def (variable_size_p (p
->type
), "", p
->name
,
3895 allocator_type
, quantity
, zone
);
3898 /* Writes typed allocator definitions for the types in STRUCTURES and
3899 TYPEDEFS that are used by GC. */
3902 write_typed_alloc_defns (const type_p structures
, const pair_p typedefs
)
3907 oprintf (header_file
,
3908 "\n/* Allocators for known structs and unions. */\n\n");
3909 for (s
= structures
; s
; s
= s
->next
)
3911 if (!USED_BY_TYPED_GC_P (s
))
3913 write_typed_struct_alloc_def (s
, "", single
, any_zone
);
3914 write_typed_struct_alloc_def (s
, "cleared_", single
, any_zone
);
3915 write_typed_struct_alloc_def (s
, "vec_", vector
, any_zone
);
3916 write_typed_struct_alloc_def (s
, "cleared_vec_", vector
, any_zone
);
3917 write_typed_struct_alloc_def (s
, "zone_", single
, specific_zone
);
3918 write_typed_struct_alloc_def (s
, "zone_cleared_", single
,
3920 write_typed_struct_alloc_def (s
, "zone_vec_", vector
, specific_zone
);
3921 write_typed_struct_alloc_def (s
, "zone_cleared_vec_", vector
,
3925 oprintf (header_file
, "\n/* Allocators for known typedefs. */\n");
3926 for (p
= typedefs
; p
; p
= p
->next
)
3929 if (!USED_BY_TYPED_GC_P (s
) || (strcmp (p
->name
, s
->u
.s
.tag
) == 0))
3931 write_typed_typedef_alloc_def (p
, "", single
, any_zone
);
3932 write_typed_typedef_alloc_def (p
, "cleared_", single
, any_zone
);
3933 write_typed_typedef_alloc_def (p
, "vec_", vector
, any_zone
);
3934 write_typed_typedef_alloc_def (p
, "cleared_vec_", vector
, any_zone
);
3935 write_typed_typedef_alloc_def (p
, "zone_", single
, specific_zone
);
3936 write_typed_typedef_alloc_def (p
, "zone_cleared_", single
,
3938 write_typed_typedef_alloc_def (p
, "zone_cleared_vec_", vector
,
3943 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
3944 guaranteee for somewhat increased readability. If name conflicts do happen,
3945 this funcion will have to be adjusted to be more like
3946 output_mangled_typename. */
3949 output_typename (outf_p of
, const_type_p t
)
3954 oprintf (of
, "str");
3957 oprintf (of
, "scalar");
3960 output_typename (of
, t
->u
.p
);
3964 case TYPE_LANG_STRUCT
:
3965 oprintf (of
, "%s", t
->u
.s
.tag
);
3967 case TYPE_PARAM_STRUCT
:
3970 for (i
= 0; i
< NUM_PARAM
; i
++)
3971 if (t
->u
.param_struct
.param
[i
] != NULL
)
3973 output_typename (of
, t
->u
.param_struct
.param
[i
]);
3976 output_typename (of
, t
->u
.param_struct
.stru
);
3984 /* Writes a typed GC allocator for type S that is suitable as a callback for
3985 the splay tree implementation in libiberty. */
3988 write_splay_tree_allocator_def (const_type_p s
)
3990 outf_p of
= get_output_file_for_structure (s
, NULL
);
3991 oprintf (of
, "void * ggc_alloc_splay_tree_");
3992 output_typename (of
, s
);
3993 oprintf (of
, " (int sz, void * nl)\n");
3994 oprintf (of
, "{\n");
3995 oprintf (of
, " return ggc_splay_alloc (");
3996 oprintf (of
, "gt_e_");
3997 output_mangled_typename (of
, s
);
3998 oprintf (of
, ", sz, nl);\n");
3999 oprintf (of
, "}\n\n");
4002 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4003 for the splay tree implementation in libiberty. */
4006 write_splay_tree_allocators (const_type_p param_structs
)
4010 oprintf (header_file
, "\n/* Splay tree callback allocators. */\n");
4011 for (s
= param_structs
; s
; s
= s
->next
)
4012 if (s
->gc_used
== GC_POINTED_TO
)
4014 oprintf (header_file
, "extern void * ggc_alloc_splay_tree_");
4015 output_typename (header_file
, s
);
4016 oprintf (header_file
, " (int, void *);\n");
4017 write_splay_tree_allocator_def (s
);
4021 static void dump_pair (int indent
, pair_p p
);
4022 static void dump_type (int indent
, type_p p
);
4023 static void dump_type_list (int indent
, type_p p
);
4027 /* Dumps the value of typekind KIND. */
4030 dump_typekind (int indent
, enum typekind kind
)
4032 printf ("%*ckind = ", indent
, ' ');
4036 printf ("TYPE_SCALAR");
4039 printf ("TYPE_STRING");
4042 printf ("TYPE_STRUCT");
4045 printf ("TYPE_UNION");
4048 printf ("TYPE_POINTER");
4051 printf ("TYPE_ARRAY");
4053 case TYPE_LANG_STRUCT
:
4054 printf ("TYPE_LANG_STRUCT");
4056 case TYPE_PARAM_STRUCT
:
4057 printf ("TYPE_PARAM_STRUCT");
4065 /* Dumps the value of GC_USED flag. */
4068 dump_gc_used (int indent
, enum gc_used_enum gc_used
)
4070 printf ("%*cgc_used = ", indent
, ' ');
4074 printf ("GC_UNUSED");
4079 case GC_MAYBE_POINTED_TO
:
4080 printf ("GC_MAYBE_POINTED_TO");
4083 printf ("GC_POINTED_TO");
4091 /* Dumps the type options OPT. */
4094 dump_options (int indent
, options_p opt
)
4097 printf ("%*coptions = ", indent
, ' ');
4101 printf ("%s:%s ", o
->name
, o
->info
);
4107 /* Dumps the source file location in LINE. */
4110 dump_fileloc (int indent
, struct fileloc line
)
4112 printf ("%*cfileloc: file = %s, line = %d\n", indent
, ' ', line
.file
,
4116 /* Recursively dumps the struct, union, or a language-specific
4120 dump_type_u_s (int indent
, type_p t
)
4124 gcc_assert (t
->kind
== TYPE_STRUCT
|| t
->kind
== TYPE_UNION
4125 || t
->kind
== TYPE_LANG_STRUCT
);
4126 printf ("%*cu.s.tag = %s\n", indent
, ' ', t
->u
.s
.tag
);
4127 dump_fileloc (indent
, t
->u
.s
.line
);
4128 printf ("%*cu.s.fields =\n", indent
, ' ');
4129 fields
= t
->u
.s
.fields
;
4132 dump_pair (indent
+ INDENT
, fields
);
4133 fields
= fields
->next
;
4135 printf ("%*cend of fields of type %p\n", indent
, ' ', (void *) t
);
4136 dump_options (indent
, t
->u
.s
.opt
);
4137 printf ("%*cu.s.bitmap = %X\n", indent
, ' ', t
->u
.s
.bitmap
);
4138 if (t
->kind
== TYPE_LANG_STRUCT
)
4140 printf ("%*cu.s.lang_struct:\n", indent
, ' ');
4141 dump_type_list (indent
+ INDENT
, t
->u
.s
.lang_struct
);
4145 /* Recursively dumps the array T. */
4148 dump_type_u_a (int indent
, type_p t
)
4150 gcc_assert (t
->kind
== TYPE_ARRAY
);
4151 printf ("%*clen = %s, u.a.p:\n", indent
, ' ', t
->u
.a
.len
);
4152 dump_type_list (indent
+ INDENT
, t
->u
.a
.p
);
4155 /* Recursively dumps the parameterized struct T. */
4158 dump_type_u_param_struct (int indent
, type_p t
)
4161 gcc_assert (t
->kind
== TYPE_PARAM_STRUCT
);
4162 printf ("%*cu.param_struct.stru:\n", indent
, ' ');
4163 dump_type_list (indent
, t
->u
.param_struct
.stru
);
4164 dump_fileloc (indent
, t
->u
.param_struct
.line
);
4165 for (i
= 0; i
< NUM_PARAM
; i
++)
4167 if (t
->u
.param_struct
.param
[i
] == NULL
)
4169 printf ("%*cu.param_struct.param[%d]:\n", indent
, ' ', i
);
4170 dump_type (indent
+ INDENT
, t
->u
.param_struct
.param
[i
]);
4174 /* Recursively dumps the type list T. */
4177 dump_type_list (int indent
, type_p t
)
4182 dump_type (indent
, p
);
4187 static htab_t seen_types
;
4189 /* Recursively dumps the type T if it was not dumped previously. */
4192 dump_type (int indent
, type_p t
)
4196 printf ("%*cType at %p: ", indent
, ' ', (void *) t
);
4197 slot
= htab_find_slot (seen_types
, t
, INSERT
);
4200 printf ("already seen.\n");
4206 dump_typekind (indent
, t
->kind
);
4207 printf ("%*cpointer_to = %p\n", indent
+ INDENT
, ' ',
4208 (void *) t
->pointer_to
);
4209 dump_gc_used (indent
+ INDENT
, t
->gc_used
);
4213 printf ("%*cscalar_is_char = %s\n", indent
+ INDENT
, ' ',
4214 t
->u
.scalar_is_char
? "true" : "false");
4220 case TYPE_LANG_STRUCT
:
4221 dump_type_u_s (indent
+ INDENT
, t
);
4224 printf ("%*cp:\n", indent
+ INDENT
, ' ');
4225 dump_type (indent
+ INDENT
, t
->u
.p
);
4228 dump_type_u_a (indent
+ INDENT
, t
);
4230 case TYPE_PARAM_STRUCT
:
4231 dump_type_u_param_struct (indent
+ INDENT
, t
);
4236 printf ("%*cEnd of type at %p\n", indent
, ' ', (void *) t
);
4239 /* Dumps the pair P. */
4242 dump_pair (int indent
, pair_p p
)
4244 printf ("%*cpair: name = %s\n", indent
, ' ', p
->name
);
4245 dump_type (indent
, p
->type
);
4246 dump_fileloc (indent
, p
->line
);
4247 dump_options (indent
, p
->opt
);
4248 printf ("%*cEnd of pair %s\n", indent
, ' ', p
->name
);
4251 /* Dumps the list of pairs PP. */
4254 dump_pair_list (const char *name
, pair_p pp
)
4257 printf ("%s:\n", name
);
4258 for (p
= pp
; p
!= NULL
; p
= p
->next
)
4260 printf ("End of %s\n\n", name
);
4263 /* Dumps the STRUCTURES. */
4266 dump_structures (const char *name
, type_p structures
)
4268 printf ("%s:\n", name
);
4269 dump_type_list (0, structures
);
4270 printf ("End of %s\n\n", name
);
4273 /* Dumps the internal structures of gengtype. */
4276 dump_everything (void)
4278 seen_types
= htab_create (100, htab_hash_pointer
, htab_eq_pointer
, NULL
);
4279 dump_pair_list ("typedefs", typedefs
);
4280 dump_structures ("structures", structures
);
4281 dump_structures ("param_structs", param_structs
);
4282 dump_pair_list ("variables", variables
);
4283 htab_delete (seen_types
);
4288 /* Option specification for getopt_long. */
4289 static const struct option gengtype_long_options
[] = {
4290 {"help", no_argument
, NULL
, 'h'},
4291 {"version", no_argument
, NULL
, 'V'},
4292 {"dump", no_argument
, NULL
, 'd'},
4293 {"debug", no_argument
, NULL
, 'D'},
4294 {"plugin", required_argument
, NULL
, 'P'},
4295 {"srcdir", required_argument
, NULL
, 'S'},
4296 {"inputs", required_argument
, NULL
, 'I'},
4297 {"read-state", required_argument
, NULL
, 'r'},
4298 {"write-state", required_argument
, NULL
, 'w'},
4299 /* Terminating NULL placeholder. */
4300 {NULL
, no_argument
, NULL
, 0},
4307 printf ("Usage: %s\n", progname
);
4308 printf ("\t -h | --help " " \t# Give this help.\n");
4309 printf ("\t -D | --debug "
4310 " \t# Give debug output to debug %s itself.\n", progname
);
4311 printf ("\t -V | --version " " \t# Give version information.\n");
4312 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4313 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4314 " \t# Generate for plugin.\n");
4315 printf ("\t -S | --srcdir <GCC-directory> "
4316 " \t# Specify the GCC source directory.\n");
4317 printf ("\t -I | --inputs <input-list> "
4318 " \t# Specify the file with source files list.\n");
4319 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
4320 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
4324 print_version (void)
4326 printf ("%s %s%s\n", progname
, pkgversion_string
, version_string
);
4327 printf ("Report bugs: %s\n", bug_report_url
);
4330 /* Parse the program options using getopt_long... */
4332 parse_program_options (int argc
, char **argv
)
4335 while ((opt
= getopt_long (argc
, argv
, "hVdP:S:I:w:r:D",
4336 gengtype_long_options
, NULL
)) >= 0)
4340 case 'h': /* --help */
4343 case 'V': /* --version */
4346 case 'd': /* --dump */
4349 case 'D': /* --debug */
4352 case 'P': /* --plugin */
4354 plugin_output_filename
= optarg
;
4356 fatal ("missing plugin output file name");
4358 case 'S': /* --srcdir */
4362 fatal ("missing source directory");
4363 srcdir_len
= strlen (srcdir
);
4365 case 'I': /* --inputs */
4369 fatal ("missing input list");
4371 case 'r': /* --read-state */
4373 read_state_filename
= optarg
;
4375 fatal ("missing read state file");
4376 DBGPRINTF ("read state %s\n", optarg
);
4378 case 'w': /* --write-state */
4379 DBGPRINTF ("write state %s\n", optarg
);
4381 write_state_filename
= optarg
;
4383 fatal ("missing write state file");
4386 fprintf (stderr
, "%s: unknown flag '%c'\n", progname
, opt
);
4388 fatal ("unexpected flag");
4391 if (plugin_output_filename
)
4393 /* In plugin mode we require some input files. */
4396 fatal ("no source files given in plugin mode");
4397 nb_plugin_files
= argc
- optind
;
4398 for (i
= 0; i
< (int) nb_plugin_files
; i
++)
4400 char *name
= argv
[i
+ optind
];
4401 plugin_files
[i
] = name
;
4408 main (int argc
, char **argv
)
4411 static struct fileloc pos
= { NULL
, 0 };
4412 outf_p output_header
;
4414 /* Mandatory common initializations. */
4415 progname
= "gengtype"; /* For fatal and messages. */
4416 /* Set the scalar_is_char union number for predefined scalar types. */
4417 scalar_nonchar
.u
.scalar_is_char
= FALSE
;
4418 scalar_char
.u
.scalar_is_char
= TRUE
;
4420 parse_program_options (argc
, argv
);
4425 time_t now
= (time_t) 0;
4427 DBGPRINTF ("gengtype started pid %d at %s",
4428 (int) getpid (), ctime (&now
));
4430 #endif /* ENABLE_CHECKING */
4432 /* Parse the input list and the input files. */
4433 DBGPRINTF ("inputlist %s", inputlist
);
4434 if (read_state_filename
)
4436 fatal ("read state %s not implemented yet", read_state_filename
);
4437 /* TODO: implement read state. */
4441 /* These types are set up with #define or else outside of where
4442 we can see them. We should initialize them before calling
4444 pos
.file
= this_file
;
4445 pos
.line
= __LINE__
+ 1;
4446 do_scalar_typedef ("CUMULATIVE_ARGS", &pos
);
4448 do_scalar_typedef ("REAL_VALUE_TYPE", &pos
);
4450 do_scalar_typedef ("FIXED_VALUE_TYPE", &pos
);
4452 do_scalar_typedef ("double_int", &pos
);
4454 do_scalar_typedef ("uint64_t", &pos
);
4456 do_scalar_typedef ("uint8", &pos
);
4458 do_scalar_typedef ("jword", &pos
);
4460 do_scalar_typedef ("JCF_u2", &pos
);
4462 do_scalar_typedef ("void", &pos
);
4464 do_typedef ("PTR", create_pointer (resolve_typedef ("void", &pos
)),
4466 read_input_list (inputlist
);
4467 for (i
= 0; i
< num_gt_files
; i
++)
4469 parse_file (gt_files
[i
]);
4470 DBGPRINTF ("parsed file #%d %s", (int) i
, gt_files
[i
]);
4472 DBGPRINT_COUNT_TYPE ("structures after parsing", structures
);
4473 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs
);
4477 fatal ("either an input list or a read state file should be given");
4482 if (plugin_output_filename
)
4485 /* In plugin mode, we should have read a state file, and have
4486 given at least one plugin file. */
4487 if (!read_state_filename
)
4488 fatal ("No read state given in plugin mode for %s",
4489 plugin_output_filename
);
4491 if (nb_plugin_files
<= 0 || !plugin_files
)
4492 fatal ("No plugin files given in plugin mode for %s",
4493 plugin_output_filename
);
4495 /* Parse our plugin files. */
4496 for (ix
= 0; ix
< nb_plugin_files
; ix
++)
4497 parse_file (plugin_files
[ix
]);
4502 plugin_output
= create_file ("GCC", plugin_output_filename
);
4503 DBGPRINTF ("created plugin_output %p named %s",
4504 (void *) plugin_output
, plugin_output
->name
);
4507 { /* No plugin files, we are in normal mode. */
4509 fatal ("gengtype needs a source directory in normal mode");
4516 /* The call to set_gc_used may indirectly call find_param_structure
4517 hence enlarge the param_structs list of types. */
4518 set_gc_used (variables
);
4520 /* We should write the state here, but it is not yet implemented. */
4521 if (write_state_filename
)
4523 fatal ("write state %s in not yet implemented", write_state_filename
);
4524 /* TODO: implement write state. */
4530 write_enum_defn (structures
, param_structs
);
4531 write_typed_alloc_defns (structures
, typedefs
);
4532 output_header
= plugin_output
? plugin_output
: header_file
;
4533 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
4535 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
4538 write_types (output_header
, structures
, param_structs
, &ggc_wtd
);
4539 if (plugin_files
== NULL
)
4541 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
4543 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
4545 write_types (header_file
, structures
, param_structs
, &pch_wtd
);
4546 write_local (header_file
, structures
, param_structs
);
4548 write_splay_tree_allocators (param_structs
);
4549 write_roots (variables
, plugin_files
== NULL
);
4551 close_output_files ();
4556 /* Don't bother about free-ing any input or plugin file, etc. */