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 /* Level for verbose messages. */
169 /* The backup directory should be in the same file system as the
170 generated files, otherwise the rename(2) system call would fail.
171 If NULL, no backup is made when overwriting a generated file. */
172 static const char* backup_dir
; /* (-B) program option. */
175 static outf_p
create_file (const char *, const char *);
177 static const char *get_file_basename (const char *);
178 static const char *get_file_realbasename (const char *);
179 static const char *get_file_srcdir_relative_path (const char *);
181 static int get_prefix_langdir_index (const char *);
182 static const char *get_file_langdir (const char *);
185 /* Nonzero iff an error has occurred. */
186 bool hit_error
= false;
188 static void gen_rtx_next (void);
189 static void write_rtx_next (void);
190 static void open_base_files (void);
191 static void close_output_files (void);
193 /* Report an error at POS, printing MSG. */
196 error_at_line (const struct fileloc
*pos
, const char *msg
, ...)
202 fprintf (stderr
, "%s:%d: ", pos
->file
, pos
->line
);
203 vfprintf (stderr
, msg
, ap
);
204 fputc ('\n', stderr
);
210 /* asprintf, but produces fatal message on out-of-memory. */
212 xasprintf (const char *format
, ...)
218 va_start (ap
, format
);
219 n
= vasprintf (&result
, format
, ap
);
220 if (result
== NULL
|| n
< 0)
221 fatal ("out of memory");
227 /* Input file handling. */
229 /* Table of all input files. */
230 static const char **gt_files
;
231 static size_t num_gt_files
;
233 /* A number of places use the name of this "gengtype.h" file for a
234 location for things that we can't rely on the source to define.
235 Make sure we can still use pointer comparison on filenames. */
236 const char this_file
[] = __FILE__
;
237 /* The "system.h" file is likewise specially useful. */
238 const char system_h_file
[] = "system.h";
240 /* Vector of per-language directories. */
241 static const char **lang_dir_names
;
242 static size_t num_lang_dirs
;
244 /* An array of output files suitable for definitions. There is one
245 BASE_FILES entry for each language. */
246 static outf_p
*base_files
;
248 /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
249 INPUT_FILE is used by <lang>.
251 This function should be written to assume that a file _is_ used
252 if the situation is unclear. If it wrongly assumes a file _is_ used,
253 a linker error will result. If it wrongly assumes a file _is not_ used,
254 some GC roots may be missed, which is a much harder-to-debug problem.
256 The relevant bitmap is stored immediately before the file's name in the
257 buffer set up by read_input_list. It may be unaligned, so we have to
258 read it byte-by-byte. */
261 get_lang_bitmap (const char *gtfile
)
264 if (gtfile
== this_file
|| gtfile
== system_h_file
)
266 /* Things defined in this "gengtype.c" file or in "system.h" are
267 universal (and there is no space for their lang_bitmap before
268 their file names). */
269 return (((lang_bitmap
) 1) << num_lang_dirs
) - 1;
275 for (i
= -(int) sizeof (lang_bitmap
); i
< 0; i
++)
276 n
= (n
<< CHAR_BIT
) + (unsigned char) gtfile
[i
];
281 /* Set the bitmap returned by get_lang_bitmap. The only legitimate
282 caller of this function is read_input_list. */
284 set_lang_bitmap (char *gtfile
, lang_bitmap n
)
287 for (i
= -1; i
>= -(int) sizeof (lang_bitmap
); i
--)
289 gtfile
[i
] = n
& ((1U << CHAR_BIT
) - 1);
296 /* Utility debugging function, printing the various type counts within
297 a list of types. Called thru the DBGPRINT_COUNT_TYPE macro. */
299 dbgprint_count_type_at (const char *fil
, int lin
, const char *msg
, type_p t
)
301 int nb_types
= 0, nb_scalar
= 0, nb_string
= 0;
302 int nb_struct
= 0, nb_union
= 0, nb_array
= 0, nb_pointer
= 0;
303 int nb_lang_struct
= 0, nb_param_struct
= 0;
305 for (p
= t
; p
; p
= p
->next
)
328 case TYPE_LANG_STRUCT
:
331 case TYPE_PARAM_STRUCT
:
338 fprintf (stderr
, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
339 lbasename (fil
), lin
, msg
, nb_types
);
340 if (nb_scalar
> 0 || nb_string
> 0)
341 fprintf (stderr
, "@@%%@@ %d scalars, %d strings\n", nb_scalar
, nb_string
);
342 if (nb_struct
> 0 || nb_union
> 0)
343 fprintf (stderr
, "@@%%@@ %d structs, %d unions\n", nb_struct
, nb_union
);
344 if (nb_pointer
> 0 || nb_array
> 0)
345 fprintf (stderr
, "@@%%@@ %d pointers, %d arrays\n", nb_pointer
, nb_array
);
346 if (nb_lang_struct
> 0 || nb_param_struct
> 0)
347 fprintf (stderr
, "@@%%@@ %d lang_structs, %d param_structs\n",
348 nb_lang_struct
, nb_param_struct
);
349 fprintf (stderr
, "\n");
351 #endif /* ENABLE_CHECKING */
353 /* Scan the input file, LIST, and determine how much space we need to
354 store strings in. Also, count the number of language directories
355 and files. The numbers returned are overestimates as they does not
356 consider repeated files. */
358 measure_input_list (FILE *list
)
364 num_gt_files
= plugin_files
? nb_plugin_files
: 0;
365 while ((c
= getc (list
)) != EOF
)
374 /* Add space for a lang_bitmap before the input file name. */
375 n
+= sizeof (lang_bitmap
);
389 /* Read one input line from LIST to HEREP (which is updated). A
390 pointer to the string is returned via LINEP. If it was a language
391 subdirectory in square brackets, strip off the square brackets and
392 return true. Otherwise, leave space before the string for a
393 lang_bitmap, and return false. At EOF, returns false, does not
394 touch *HEREP, and sets *LINEP to NULL. POS is used for
397 read_input_line (FILE *list
, char **herep
, char **linep
, struct fileloc
*pos
)
403 /* Read over whitespace. */
404 while (c
== '\n' || c
== ' ')
414 /* No space for a lang_bitmap is necessary. Discard the '['. */
417 while (c
!= ']' && c
!= '\n' && c
!= EOF
)
426 c
= getc (list
); /* eat what should be a newline */
427 if (c
!= '\n' && c
!= EOF
)
428 error_at_line (pos
, "junk on line after language tag [%s]", line
);
431 error_at_line (pos
, "missing close bracket for language tag [%s",
440 /* Leave space for a lang_bitmap. */
441 memset (here
, 0, sizeof (lang_bitmap
));
442 here
+= sizeof (lang_bitmap
);
449 while (c
!= EOF
&& c
!= '\n');
457 /* Read the list of input files from LIST and compute all of the
458 relevant tables. There is one file per line of the list. At
459 first, all the files on the list are language-generic, but
460 eventually a line will appear which is the name of a language
461 subdirectory in square brackets, like this: [cp]. All subsequent
462 files are specific to that language, until another language
463 subdirectory tag appears. Files can appear more than once, if
464 they apply to more than one language. */
466 read_input_list (const char *listname
)
468 FILE *list
= fopen (listname
, "r");
470 fatal ("cannot open %s: %s", listname
, xstrerror (errno
));
474 size_t bufsz
= measure_input_list (list
);
475 char *buf
= XNEWVEC (char, bufsz
);
477 char *committed
= buf
;
478 char *limit
= buf
+ bufsz
;
483 lang_bitmap curlangs
= (1 << num_lang_dirs
) - 1;
485 epos
.file
= listname
;
488 lang_dir_names
= XNEWVEC (const char *, num_lang_dirs
);
489 gt_files
= XNEWVEC (const char *, num_gt_files
);
496 is_language
= read_input_line (list
, &here
, &line
, &epos
);
497 gcc_assert (here
<= limit
);
500 else if (is_language
)
503 gcc_assert (langno
<= num_lang_dirs
);
504 for (i
= 0; i
< langno
; i
++)
505 if (strcmp (lang_dir_names
[i
], line
) == 0)
507 error_at_line (&epos
, "duplicate language tag [%s]",
514 curlangs
= 1 << langno
;
515 lang_dir_names
[langno
++] = line
;
520 gcc_assert (nfiles
<= num_gt_files
);
521 for (i
= 0; i
< nfiles
; i
++)
522 if (strcmp (gt_files
[i
], line
) == 0)
524 /* Throw away the string we just read, and add the
525 current language to the existing string's bitmap. */
526 lang_bitmap bmap
= get_lang_bitmap (gt_files
[i
]);
528 error_at_line (&epos
,
529 "file %s specified more than once "
530 "for language %s", line
,
532 0 ? "(all)" : lang_dir_names
[langno
-
536 set_lang_bitmap (CONST_CAST (char *, gt_files
[i
]), bmap
);
541 set_lang_bitmap (line
, curlangs
);
542 gt_files
[nfiles
++] = line
;
545 /* Update the global counts now that we know accurately how many
546 things there are. (We do not bother resizing the arrays down.) */
547 num_lang_dirs
= langno
;
548 /* Add the plugin files if provided. */
552 for (i
= 0; i
< nb_plugin_files
; i
++)
553 gt_files
[nfiles
++] = plugin_files
[i
];
555 num_gt_files
= nfiles
;
558 /* Sanity check: any file that resides in a language subdirectory
559 (e.g. 'cp') ought to belong to the corresponding language.
560 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
561 (Can you even do that? Should you be allowed to?) */
564 for (f
= 0; f
< num_gt_files
; f
++)
566 lang_bitmap bitmap
= get_lang_bitmap (gt_files
[f
]);
567 const char *basename
= get_file_basename (gt_files
[f
]);
568 const char *slashpos
= strchr (basename
, '/');
573 for (l
= 0; l
< num_lang_dirs
; l
++)
574 if ((size_t) (slashpos
- basename
) == strlen (lang_dir_names
[l
])
575 && memcmp (basename
, lang_dir_names
[l
],
576 strlen (lang_dir_names
[l
])) == 0)
578 if (!(bitmap
& (1 << l
)))
579 error ("%s is in language directory '%s' but is not "
580 "tagged for that language",
581 basename
, lang_dir_names
[l
]);
589 fatal ("error reading %s: %s", listname
, xstrerror (errno
));
596 /* The one and only TYPE_STRING. */
598 static struct type string_type
= {
599 TYPE_STRING
, 0, 0, GC_USED
, {0}
602 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
603 set to appropriate values at the beginning of main. */
605 static struct type scalar_nonchar
= {
606 TYPE_SCALAR
, 0, 0, GC_USED
, {0}
609 static struct type scalar_char
= {
610 TYPE_SCALAR
, 0, 0, GC_USED
, {0}
613 /* Lists of various things. */
615 static pair_p typedefs
;
616 static type_p structures
;
617 static type_p param_structs
;
618 static pair_p variables
;
620 static type_p
find_param_structure (type_p t
, type_p param
[NUM_PARAM
]);
621 static type_p
adjust_field_tree_exp (type_p t
, options_p opt
);
622 static type_p
adjust_field_rtx_def (type_p t
, options_p opt
);
624 /* Define S as a typedef to T at POS. */
627 do_typedef (const char *s
, type_p t
, struct fileloc
*pos
)
631 /* temporary kludge - gengtype doesn't handle conditionals or
632 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
633 is coming from this file (main() sets them up with safe dummy
635 if (!strcmp (s
, "CUMULATIVE_ARGS") && pos
->file
!= this_file
)
638 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
639 if (strcmp (p
->name
, s
) == 0)
643 error_at_line (pos
, "type `%s' previously defined", s
);
644 error_at_line (&p
->line
, "previously defined here");
649 p
= XNEW (struct pair
);
658 /* Define S as a typename of a scalar. Cannot be used to define
659 typedefs of 'char'. Note: is also used for pointer-to-function
660 typedefs (which are therefore not treated as pointers). */
663 do_scalar_typedef (const char *s
, struct fileloc
*pos
)
665 do_typedef (s
, &scalar_nonchar
, pos
);
668 /* Return the type previously defined for S. Use POS to report errors. */
671 resolve_typedef (const char *s
, struct fileloc
*pos
)
674 for (p
= typedefs
; p
!= NULL
; p
= p
->next
)
675 if (strcmp (p
->name
, s
) == 0)
677 error_at_line (pos
, "unidentified type `%s'", s
);
678 return &scalar_nonchar
; /* treat as "int" */
681 /* Create and return a new structure with tag NAME (or a union iff
682 ISUNION is nonzero), at POS with fields FIELDS and options O. */
685 new_structure (const char *name
, int isunion
, struct fileloc
*pos
,
686 pair_p fields
, options_p o
)
690 lang_bitmap bitmap
= get_lang_bitmap (pos
->file
);
692 for (si
= structures
; si
!= NULL
; si
= si
->next
)
693 if (strcmp (name
, si
->u
.s
.tag
) == 0 && UNION_P (si
) == isunion
)
696 if (si
->kind
== TYPE_LANG_STRUCT
)
700 for (si
= ls
->u
.s
.lang_struct
; si
!= NULL
; si
= si
->next
)
701 if (si
->u
.s
.bitmap
== bitmap
)
704 else if (si
->u
.s
.line
.file
!= NULL
&& si
->u
.s
.bitmap
!= bitmap
)
707 si
= XCNEW (struct type
);
708 memcpy (si
, ls
, sizeof (struct type
));
709 ls
->kind
= TYPE_LANG_STRUCT
;
710 ls
->u
.s
.lang_struct
= si
;
711 ls
->u
.s
.fields
= NULL
;
713 si
->pointer_to
= NULL
;
714 si
->u
.s
.lang_struct
= ls
;
719 if (ls
!= NULL
&& s
== NULL
)
721 s
= XCNEW (struct type
);
722 s
->next
= ls
->u
.s
.lang_struct
;
723 ls
->u
.s
.lang_struct
= s
;
724 s
->u
.s
.lang_struct
= ls
;
731 s
= XCNEW (struct type
);
732 s
->next
= structures
;
736 if (s
->u
.s
.line
.file
!= NULL
737 || (s
->u
.s
.lang_struct
&& (s
->u
.s
.lang_struct
->u
.s
.bitmap
& bitmap
)))
739 error_at_line (pos
, "duplicate definition of '%s %s'",
740 isunion
? "union" : "struct", s
->u
.s
.tag
);
741 error_at_line (&s
->u
.s
.line
, "previous definition here");
744 s
->kind
= isunion
? TYPE_UNION
: TYPE_STRUCT
;
747 s
->u
.s
.fields
= fields
;
749 s
->u
.s
.bitmap
= bitmap
;
750 if (s
->u
.s
.lang_struct
)
751 s
->u
.s
.lang_struct
->u
.s
.bitmap
|= bitmap
;
756 /* Return the previously-defined structure with tag NAME (or a union
757 iff ISUNION is nonzero), or a new empty structure or union if none
758 was defined previously. */
761 find_structure (const char *name
, int isunion
)
765 for (s
= structures
; s
!= NULL
; s
= s
->next
)
766 if (strcmp (name
, s
->u
.s
.tag
) == 0 && UNION_P (s
) == isunion
)
769 s
= XCNEW (struct type
);
770 s
->next
= structures
;
772 s
->kind
= isunion
? TYPE_UNION
: TYPE_STRUCT
;
778 /* Return the previously-defined parameterized structure for structure
779 T and parameters PARAM, or a new parameterized empty structure or
780 union if none was defined previously. */
783 find_param_structure (type_p t
, type_p param
[NUM_PARAM
])
787 for (res
= param_structs
; res
; res
= res
->next
)
788 if (res
->u
.param_struct
.stru
== t
789 && memcmp (res
->u
.param_struct
.param
, param
,
790 sizeof (type_p
) * NUM_PARAM
) == 0)
794 res
= XCNEW (struct type
);
795 res
->kind
= TYPE_PARAM_STRUCT
;
796 res
->next
= param_structs
;
798 res
->u
.param_struct
.stru
= t
;
799 memcpy (res
->u
.param_struct
.param
, param
, sizeof (type_p
) * NUM_PARAM
);
804 /* Return a scalar type with name NAME. */
807 create_scalar_type (const char *name
)
809 if (!strcmp (name
, "char") || !strcmp (name
, "unsigned char"))
812 return &scalar_nonchar
;
815 /* Return a pointer to T. */
818 create_pointer (type_p t
)
822 type_p r
= XCNEW (struct type
);
823 r
->kind
= TYPE_POINTER
;
827 return t
->pointer_to
;
830 /* Return an array of length LEN. */
833 create_array (type_p t
, const char *len
)
837 v
= XCNEW (struct type
);
838 v
->kind
= TYPE_ARRAY
;
844 /* Return an options structure with name NAME and info INFO. NEXT is the
845 next option in the chain. */
848 create_option (options_p next
, const char *name
, const void *info
)
850 options_p o
= XNEW (struct options
);
853 o
->info
= (const char *) info
;
857 /* Return an options structure for a "nested_ptr" option. */
859 create_nested_ptr_option (options_p next
, type_p t
,
860 const char *to
, const char *from
)
862 struct nested_ptr_data
*d
= XNEW (struct nested_ptr_data
);
864 d
->type
= adjust_field_type (t
, 0);
866 d
->convert_from
= from
;
867 return create_option (next
, "nested_ptr", d
);
870 /* Add a variable named S of type T with options O defined at POS,
874 note_variable (const char *s
, type_p t
, options_p o
, struct fileloc
*pos
)
877 n
= XNEW (struct pair
);
886 /* Most-general structure field creator. */
888 create_field_all (pair_p next
, type_p type
, const char *name
, options_p opt
,
889 const char *file
, int line
)
893 field
= XNEW (struct pair
);
898 field
->line
.file
= file
;
899 field
->line
.line
= line
;
903 /* Create a field that came from the source code we are scanning,
904 i.e. we have a 'struct fileloc', and possibly options; also,
905 adjust_field_type should be called. */
907 create_field_at (pair_p next
, type_p type
, const char *name
, options_p opt
,
910 return create_field_all (next
, adjust_field_type (type
, opt
),
911 name
, opt
, pos
->file
, pos
->line
);
914 /* Create a fake field with the given type and name. NEXT is the next
915 field in the chain. */
916 #define create_field(next,type,name) \
917 create_field_all(next,type,name, 0, this_file, __LINE__)
919 /* Like create_field, but the field is only valid when condition COND
923 create_optional_field_ (pair_p next
, type_p type
, const char *name
,
924 const char *cond
, int line
)
930 /* Create a fake union type with a single nameless field of type TYPE.
931 The field has a tag of "1". This allows us to make the presence
932 of a field of type TYPE depend on some boolean "desc" being true. */
933 union_fields
= create_field (NULL
, type
, "");
934 union_fields
->opt
= create_option (union_fields
->opt
, "dot", "");
935 union_fields
->opt
= create_option (union_fields
->opt
, "tag", "1");
936 union_type
= new_structure (xasprintf ("%s_%d", "fake_union", id
++), 1,
937 &lexer_line
, union_fields
, NULL
);
939 /* Create the field and give it the new fake union type. Add a "desc"
940 tag that specifies the condition under which the field is valid. */
941 return create_field_all (next
, union_type
, name
,
942 create_option (0, "desc", cond
), this_file
, line
);
945 #define create_optional_field(next,type,name,cond) \
946 create_optional_field_(next,type,name,cond,__LINE__)
948 /* Reverse a linked list of 'struct pair's in place. */
950 nreverse_pairs (pair_p list
)
952 pair_p prev
= 0, p
, next
;
953 for (p
= list
; p
; p
= next
)
963 /* We don't care how long a CONST_DOUBLE is. */
964 #define CONST_DOUBLE_FORMAT "ww"
965 /* We don't want to see codes that are only for generator files. */
966 #undef GENERATOR_FILE
970 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
976 static const char *const rtx_name
[NUM_RTX_CODE
] = {
977 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
982 static const char *const rtx_format
[NUM_RTX_CODE
] = {
983 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
988 static int rtx_next_new
[NUM_RTX_CODE
];
990 /* We also need codes and names for insn notes (not register notes).
991 Note that we do *not* bias the note values here. */
994 #define DEF_INSN_NOTE(NAME) NAME,
995 #include "insn-notes.def"
1001 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1002 default field for line number notes. */
1003 static const char *const note_insn_name
[NOTE_INSN_MAX
+ 1] = {
1004 #define DEF_INSN_NOTE(NAME) #NAME,
1005 #include "insn-notes.def"
1006 #undef DEF_INSN_NOTE
1009 #undef CONST_DOUBLE_FORMAT
1010 #define GENERATOR_FILE
1012 /* Generate the contents of the rtx_next array. This really doesn't belong
1013 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1019 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1023 rtx_next_new
[i
] = -1;
1024 if (strncmp (rtx_format
[i
], "iuu", 3) == 0)
1025 rtx_next_new
[i
] = 2;
1026 else if (i
== COND_EXEC
|| i
== SET
|| i
== EXPR_LIST
|| i
== INSN_LIST
)
1027 rtx_next_new
[i
] = 1;
1029 for (k
= strlen (rtx_format
[i
]) - 1; k
>= 0; k
--)
1030 if (rtx_format
[i
][k
] == 'e' || rtx_format
[i
][k
] == 'u')
1031 rtx_next_new
[i
] = k
;
1035 /* Write out the contents of the rtx_next array. */
1037 write_rtx_next (void)
1039 outf_p f
= get_output_file_with_visibility (NULL
);
1044 oprintf (f
, "\n/* Used to implement the RTX_NEXT macro. */\n");
1045 oprintf (f
, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1046 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1047 if (rtx_next_new
[i
] == -1)
1048 oprintf (f
, " 0,\n");
1051 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new
[i
]);
1052 oprintf (f
, "};\n");
1055 /* Handle `special("rtx_def")'. This is a special case for field
1056 `fld' of struct rtx_def, which is an array of unions whose values
1057 are based in a complex way on the type of RTL. */
1060 adjust_field_rtx_def (type_p t
, options_p
ARG_UNUSED (opt
))
1065 type_p rtx_tp
, rtvec_tp
, tree_tp
, mem_attrs_tp
, note_union_tp
, scalar_tp
;
1066 type_p basic_block_tp
, reg_attrs_tp
, constant_tp
, symbol_union_tp
;
1068 if (t
->kind
!= TYPE_UNION
)
1070 error_at_line (&lexer_line
,
1071 "special `rtx_def' must be applied to a union");
1072 return &string_type
;
1075 nodot
= create_option (NULL
, "dot", "");
1077 rtx_tp
= create_pointer (find_structure ("rtx_def", 0));
1078 rtvec_tp
= create_pointer (find_structure ("rtvec_def", 0));
1079 tree_tp
= create_pointer (find_structure ("tree_node", 1));
1080 mem_attrs_tp
= create_pointer (find_structure ("mem_attrs", 0));
1081 reg_attrs_tp
= create_pointer (find_structure ("reg_attrs", 0));
1082 basic_block_tp
= create_pointer (find_structure ("basic_block_def", 0));
1084 create_pointer (find_structure ("constant_descriptor_rtx", 0));
1085 scalar_tp
= &scalar_nonchar
; /* rtunion int */
1088 pair_p note_flds
= NULL
;
1091 for (c
= 0; c
<= NOTE_INSN_MAX
; c
++)
1096 case NOTE_INSN_DELETED_LABEL
:
1097 note_flds
= create_field (note_flds
, &string_type
, "rt_str");
1100 case NOTE_INSN_BLOCK_BEG
:
1101 case NOTE_INSN_BLOCK_END
:
1102 note_flds
= create_field (note_flds
, tree_tp
, "rt_tree");
1105 case NOTE_INSN_VAR_LOCATION
:
1106 note_flds
= create_field (note_flds
, rtx_tp
, "rt_rtx");
1110 note_flds
= create_field (note_flds
, scalar_tp
, "rt_int");
1113 /* NOTE_INSN_MAX is used as the default field for line
1115 if (c
== NOTE_INSN_MAX
)
1116 note_flds
->opt
= create_option (nodot
, "default", "");
1118 note_flds
->opt
= create_option (nodot
, "tag", note_insn_name
[c
]);
1120 note_union_tp
= new_structure ("rtx_def_note_subunion", 1,
1121 &lexer_line
, note_flds
, NULL
);
1123 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1127 sym_flds
= create_field (NULL
, tree_tp
, "rt_tree");
1128 sym_flds
->opt
= create_option (nodot
, "default", "");
1130 sym_flds
= create_field (sym_flds
, constant_tp
, "rt_constant");
1131 sym_flds
->opt
= create_option (nodot
, "tag", "1");
1133 symbol_union_tp
= new_structure ("rtx_def_symbol_subunion", 1,
1134 &lexer_line
, sym_flds
, NULL
);
1136 for (i
= 0; i
< NUM_RTX_CODE
; i
++)
1138 pair_p subfields
= NULL
;
1139 size_t aindex
, nmindex
;
1144 for (aindex
= 0; aindex
< strlen (rtx_format
[i
]); aindex
++)
1147 const char *subname
;
1149 switch (rtx_format
[i
][aindex
])
1160 if (i
== MEM
&& aindex
== 1)
1161 t
= mem_attrs_tp
, subname
= "rt_mem";
1162 else if (i
== JUMP_INSN
&& aindex
== 8)
1163 t
= rtx_tp
, subname
= "rt_rtx";
1164 else if (i
== CODE_LABEL
&& aindex
== 5)
1165 t
= scalar_tp
, subname
= "rt_int";
1166 else if (i
== CODE_LABEL
&& aindex
== 4)
1167 t
= rtx_tp
, subname
= "rt_rtx";
1168 else if (i
== LABEL_REF
&& (aindex
== 1 || aindex
== 2))
1169 t
= rtx_tp
, subname
= "rt_rtx";
1170 else if (i
== NOTE
&& aindex
== 4)
1171 t
= note_union_tp
, subname
= "";
1172 else if (i
== NOTE
&& aindex
== 5)
1173 t
= scalar_tp
, subname
= "rt_int";
1174 else if (i
== NOTE
&& aindex
>= 7)
1175 t
= scalar_tp
, subname
= "rt_int";
1176 else if (i
== ADDR_DIFF_VEC
&& aindex
== 4)
1177 t
= scalar_tp
, subname
= "rt_int";
1178 else if (i
== VALUE
&& aindex
== 0)
1179 t
= scalar_tp
, subname
= "rt_int";
1180 else if (i
== DEBUG_EXPR
&& aindex
== 0)
1181 t
= tree_tp
, subname
= "rt_tree";
1182 else if (i
== REG
&& aindex
== 1)
1183 t
= scalar_tp
, subname
= "rt_int";
1184 else if (i
== REG
&& aindex
== 2)
1185 t
= reg_attrs_tp
, subname
= "rt_reg";
1186 else if (i
== SCRATCH
&& aindex
== 0)
1187 t
= scalar_tp
, subname
= "rt_int";
1188 else if (i
== SYMBOL_REF
&& aindex
== 1)
1189 t
= scalar_tp
, subname
= "rt_int";
1190 else if (i
== SYMBOL_REF
&& aindex
== 2)
1191 t
= symbol_union_tp
, subname
= "";
1192 else if (i
== BARRIER
&& aindex
>= 3)
1193 t
= scalar_tp
, subname
= "rt_int";
1196 error_at_line (&lexer_line
,
1197 "rtx type `%s' has `0' in position %lu, can't handle",
1198 rtx_name
[i
], (unsigned long) aindex
);
1220 subname
= "rt_rtvec";
1225 subname
= "rt_tree";
1234 error_at_line (&lexer_line
,
1235 "rtx type `%s' has `%c' in position %lu, can't handle",
1236 rtx_name
[i
], rtx_format
[i
][aindex
],
1237 (unsigned long) aindex
);
1243 subfields
= create_field (subfields
, t
,
1244 xasprintf (".fld[%lu].%s",
1245 (unsigned long) aindex
,
1247 subfields
->opt
= nodot
;
1248 if (t
== note_union_tp
)
1249 subfields
->opt
= create_option (subfields
->opt
, "desc",
1251 if (t
== symbol_union_tp
)
1252 subfields
->opt
= create_option (subfields
->opt
, "desc",
1253 "CONSTANT_POOL_ADDRESS_P (&%0)");
1256 if (i
== SYMBOL_REF
)
1258 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P holds. */
1259 type_p field_tp
= find_structure ("block_symbol", 0);
1261 = create_optional_field (subfields
, field_tp
, "block_sym",
1262 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1265 sname
= xasprintf ("rtx_def_%s", rtx_name
[i
]);
1266 substruct
= new_structure (sname
, 0, &lexer_line
, subfields
, NULL
);
1268 ftag
= xstrdup (rtx_name
[i
]);
1269 for (nmindex
= 0; nmindex
< strlen (ftag
); nmindex
++)
1270 ftag
[nmindex
] = TOUPPER (ftag
[nmindex
]);
1272 flds
= create_field (flds
, substruct
, "");
1273 flds
->opt
= create_option (nodot
, "tag", ftag
);
1276 return new_structure ("rtx_def_subunion", 1, &lexer_line
, flds
, nodot
);
1279 /* Handle `special("tree_exp")'. This is a special case for
1280 field `operands' of struct tree_exp, which although it claims to contain
1281 pointers to trees, actually sometimes contains pointers to RTL too.
1282 Passed T, the old type of the field, and OPT its options. Returns
1283 a new type for the field. */
1286 adjust_field_tree_exp (type_p t
, options_p opt ATTRIBUTE_UNUSED
)
1291 if (t
->kind
!= TYPE_ARRAY
)
1293 error_at_line (&lexer_line
,
1294 "special `tree_exp' must be applied to an array");
1295 return &string_type
;
1298 nodot
= create_option (NULL
, "dot", "");
1300 flds
= create_field (NULL
, t
, "");
1301 flds
->opt
= create_option (nodot
, "length",
1302 "TREE_OPERAND_LENGTH ((tree) &%0)");
1303 flds
->opt
= create_option (flds
->opt
, "default", "");
1305 return new_structure ("tree_exp_subunion", 1, &lexer_line
, flds
, nodot
);
1308 /* Perform any special processing on a type T, about to become the type
1309 of a field. Return the appropriate type for the field.
1311 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1312 - Similarly for arrays of pointer-to-char;
1313 - Converts structures for which a parameter is provided to
1315 - Handles "special" options.
1319 adjust_field_type (type_p t
, options_p opt
)
1322 const int pointer_p
= t
->kind
== TYPE_POINTER
;
1323 type_p params
[NUM_PARAM
];
1327 for (i
= 0; i
< NUM_PARAM
; i
++)
1330 for (; opt
; opt
= opt
->next
)
1331 if (strcmp (opt
->name
, "length") == 0)
1333 else if (strcmp (opt
->name
, "param_is") == 0
1334 || (strncmp (opt
->name
, "param", 5) == 0
1335 && ISDIGIT (opt
->name
[5])
1336 && strcmp (opt
->name
+ 6, "_is") == 0))
1338 int num
= ISDIGIT (opt
->name
[5]) ? opt
->name
[5] - '0' : 0;
1340 if (!UNION_OR_STRUCT_P (t
)
1341 && (t
->kind
!= TYPE_POINTER
|| !UNION_OR_STRUCT_P (t
->u
.p
)))
1343 error_at_line (&lexer_line
,
1344 "option `%s' may only be applied to structures or structure pointers",
1350 if (params
[num
] != NULL
)
1351 error_at_line (&lexer_line
, "duplicate `%s' option", opt
->name
);
1352 if (!ISDIGIT (opt
->name
[5]))
1354 create_pointer (CONST_CAST2 (type_p
, const char *, opt
->info
));
1356 params
[num
] = CONST_CAST2 (type_p
, const char *, opt
->info
);
1358 else if (strcmp (opt
->name
, "special") == 0)
1360 const char *special_name
= opt
->info
;
1361 if (strcmp (special_name
, "tree_exp") == 0)
1362 t
= adjust_field_tree_exp (t
, opt
);
1363 else if (strcmp (special_name
, "rtx_def") == 0)
1364 t
= adjust_field_rtx_def (t
, opt
);
1366 error_at_line (&lexer_line
, "unknown special `%s'", special_name
);
1375 realt
= find_param_structure (t
, params
);
1376 t
= pointer_p
? create_pointer (realt
) : realt
;
1380 && pointer_p
&& t
->u
.p
->kind
== TYPE_SCALAR
&& t
->u
.p
->u
.scalar_is_char
)
1381 return &string_type
;
1382 if (t
->kind
== TYPE_ARRAY
&& t
->u
.a
.p
->kind
== TYPE_POINTER
1383 && t
->u
.a
.p
->u
.p
->kind
== TYPE_SCALAR
1384 && t
->u
.a
.p
->u
.p
->u
.scalar_is_char
)
1385 return create_array (&string_type
, t
->u
.a
.len
);
1391 static void set_gc_used_type (type_p
, enum gc_used_enum
, type_p
*);
1392 static void set_gc_used (pair_p
);
1394 /* Handle OPT for set_gc_used_type. */
1397 process_gc_options (options_p opt
, enum gc_used_enum level
, int *maybe_undef
,
1398 int *pass_param
, int *length
, int *skip
,
1402 for (o
= opt
; o
; o
= o
->next
)
1403 if (strcmp (o
->name
, "ptr_alias") == 0 && level
== GC_POINTED_TO
)
1404 set_gc_used_type (CONST_CAST2 (type_p
, const char *, o
->info
),
1405 GC_POINTED_TO
, NULL
);
1406 else if (strcmp (o
->name
, "maybe_undef") == 0)
1408 else if (strcmp (o
->name
, "use_params") == 0)
1410 else if (strcmp (o
->name
, "length") == 0)
1412 else if (strcmp (o
->name
, "skip") == 0)
1414 else if (strcmp (o
->name
, "nested_ptr") == 0)
1415 *nested_ptr
= ((const struct nested_ptr_data
*) o
->info
)->type
;
1418 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1421 set_gc_used_type (type_p t
, enum gc_used_enum level
, type_p param
[NUM_PARAM
])
1423 if (t
->gc_used
>= level
)
1437 process_gc_options (t
->u
.s
.opt
, level
, &dummy
, &dummy
, &dummy
, &dummy
,
1440 for (f
= t
->u
.s
.fields
; f
; f
= f
->next
)
1442 int maybe_undef
= 0;
1446 type_p nested_ptr
= NULL
;
1447 process_gc_options (f
->opt
, level
, &maybe_undef
, &pass_param
,
1448 &length
, &skip
, &nested_ptr
);
1450 if (nested_ptr
&& f
->type
->kind
== TYPE_POINTER
)
1451 set_gc_used_type (nested_ptr
, GC_POINTED_TO
,
1452 pass_param
? param
: NULL
);
1453 else if (length
&& f
->type
->kind
== TYPE_POINTER
)
1454 set_gc_used_type (f
->type
->u
.p
, GC_USED
, NULL
);
1455 else if (maybe_undef
&& f
->type
->kind
== TYPE_POINTER
)
1456 set_gc_used_type (f
->type
->u
.p
, GC_MAYBE_POINTED_TO
, NULL
);
1457 else if (pass_param
&& f
->type
->kind
== TYPE_POINTER
&& param
)
1458 set_gc_used_type (find_param_structure (f
->type
->u
.p
, param
),
1459 GC_POINTED_TO
, NULL
);
1461 ; /* target type is not used through this field */
1463 set_gc_used_type (f
->type
, GC_USED
, pass_param
? param
: NULL
);
1469 set_gc_used_type (t
->u
.p
, GC_POINTED_TO
, NULL
);
1473 set_gc_used_type (t
->u
.a
.p
, GC_USED
, param
);
1476 case TYPE_LANG_STRUCT
:
1477 for (t
= t
->u
.s
.lang_struct
; t
; t
= t
->next
)
1478 set_gc_used_type (t
, level
, param
);
1481 case TYPE_PARAM_STRUCT
:
1484 for (i
= 0; i
< NUM_PARAM
; i
++)
1485 if (t
->u
.param_struct
.param
[i
] != 0)
1486 set_gc_used_type (t
->u
.param_struct
.param
[i
], GC_USED
, NULL
);
1488 if (t
->u
.param_struct
.stru
->gc_used
== GC_POINTED_TO
)
1489 level
= GC_POINTED_TO
;
1492 t
->u
.param_struct
.stru
->gc_used
= GC_UNUSED
;
1493 set_gc_used_type (t
->u
.param_struct
.stru
, level
,
1494 t
->u
.param_struct
.param
);
1502 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1505 set_gc_used (pair_p variables
)
1509 for (p
= variables
; p
; p
= p
->next
)
1511 set_gc_used_type (p
->type
, GC_USED
, NULL
);
1514 if (verbosity_level
>= 2)
1515 printf ("%s used %d GTY-ed variables\n", progname
, nbvars
);
1518 /* File mapping routines. For each input file, there is one output .c file
1519 (but some output files have many input files), and there is one .h file
1520 for the whole build. */
1522 /* Output file handling. */
1524 /* Create and return an outf_p for a new file for NAME, to be called
1528 create_file (const char *name
, const char *oname
)
1530 static const char *const hdr
[] = {
1531 " Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc.\n",
1533 "This file is part of GCC.\n",
1535 "GCC is free software; you can redistribute it and/or modify it under\n",
1536 "the terms of the GNU General Public License as published by the Free\n",
1537 "Software Foundation; either version 3, or (at your option) any later\n",
1540 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1541 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1542 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1543 "for more details.\n",
1545 "You should have received a copy of the GNU General Public License\n",
1546 "along with GCC; see the file COPYING3. If not see\n",
1547 "<http://www.gnu.org/licenses/>. */\n",
1549 "/* This file is machine generated. Do not edit. */\n"
1554 gcc_assert (name
!= NULL
);
1555 gcc_assert (oname
!= NULL
);
1556 f
= XCNEW (struct outf
);
1557 f
->next
= output_files
;
1561 oprintf (f
, "/* Type information for %s.\n", name
);
1562 for (i
= 0; i
< ARRAY_SIZE (hdr
); i
++)
1563 oprintf (f
, "%s", hdr
[i
]);
1567 /* Print, like fprintf, to O.
1568 N.B. You might think this could be implemented more efficiently
1569 with vsnprintf(). Unfortunately, there are C libraries that
1570 provide that function but without the C99 semantics for its return
1571 value, making it impossible to know how much space is required. */
1573 oprintf (outf_p o
, const char *format
, ...)
1579 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1584 va_start (ap
, format
);
1585 slength
= vasprintf (&s
, format
, ap
);
1586 if (s
== NULL
|| (int) slength
< 0)
1587 fatal ("out of memory");
1590 if (o
->bufused
+ slength
> o
->buflength
)
1592 size_t new_len
= o
->buflength
;
1599 while (o
->bufused
+ slength
>= new_len
);
1600 o
->buf
= XRESIZEVEC (char, o
->buf
, new_len
);
1601 o
->buflength
= new_len
;
1603 memcpy (o
->buf
+ o
->bufused
, s
, slength
);
1604 o
->bufused
+= slength
;
1608 /* Open the global header file and the language-specific header files. */
1611 open_base_files (void)
1615 if (nb_plugin_files
> 0 && plugin_files
)
1618 header_file
= create_file ("GCC", "gtype-desc.h");
1620 base_files
= XNEWVEC (outf_p
, num_lang_dirs
);
1622 for (i
= 0; i
< num_lang_dirs
; i
++)
1623 base_files
[i
] = create_file (lang_dir_names
[i
],
1624 xasprintf ("gtype-%s.h", lang_dir_names
[i
]));
1626 /* gtype-desc.c is a little special, so we create it here. */
1628 /* The order of files here matters very much. */
1629 static const char *const ifiles
[] = {
1630 "config.h", "system.h", "coretypes.h", "tm.h",
1631 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1632 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1633 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1634 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1635 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1636 "cfglayout.h", "except.h", "output.h", "gimple.h", "cfgloop.h",
1637 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h", NULL
1639 const char *const *ifp
;
1640 outf_p gtype_desc_c
;
1642 gtype_desc_c
= create_file ("GCC", "gtype-desc.c");
1643 for (ifp
= ifiles
; *ifp
; ifp
++)
1644 oprintf (gtype_desc_c
, "#include \"%s\"\n", *ifp
);
1646 /* Make sure we handle "cfun" specially. */
1647 oprintf (gtype_desc_c
, "\n/* See definition in function.h. */\n");
1648 oprintf (gtype_desc_c
, "#undef cfun\n");
1652 /* For F a filename, return the real basename of F, with all the directory
1653 components skipped. */
1656 get_file_realbasename (const char *f
)
1658 const char *lastslash
= strrchr (f
, '/');
1660 return (lastslash
!= NULL
) ? lastslash
+ 1 : f
;
1663 /* For F a filename, return the relative path to F from $(srcdir) if the
1664 latter is a prefix in F, NULL otherwise. */
1667 get_file_srcdir_relative_path (const char *f
)
1669 if (strlen (f
) > srcdir_len
1670 && IS_DIR_SEPARATOR (f
[srcdir_len
])
1671 && memcmp (f
, srcdir
, srcdir_len
) == 0)
1672 return f
+ srcdir_len
+ 1;
1677 /* For F a filename, return the relative path to F from $(srcdir) if the
1678 latter is a prefix in F, or the real basename of F otherwise. */
1681 get_file_basename (const char *f
)
1683 const char *srcdir_path
= get_file_srcdir_relative_path (f
);
1685 return (srcdir_path
!= NULL
) ? srcdir_path
: get_file_realbasename (f
);
1688 /* For F a filename, return the lang_dir_names relative index of the language
1689 directory that is a prefix in F, if any, -1 otherwise. */
1692 get_prefix_langdir_index (const char *f
)
1694 size_t f_len
= strlen (f
);
1697 for (lang_index
= 0; lang_index
< num_lang_dirs
; lang_index
++)
1699 const char *langdir
= lang_dir_names
[lang_index
];
1700 size_t langdir_len
= strlen (langdir
);
1702 if (f_len
> langdir_len
1703 && IS_DIR_SEPARATOR (f
[langdir_len
])
1704 && memcmp (f
, langdir
, langdir_len
) == 0)
1711 /* For F a filename, return the name of language directory where F is located,
1712 if any, NULL otherwise. */
1715 get_file_langdir (const char *f
)
1717 /* Get the relative path to F from $(srcdir) and find the language by
1718 comparing the prefix with language directory names. If F is not even
1719 srcdir relative, no point in looking further. */
1722 const char *srcdir_relative_path
= get_file_srcdir_relative_path (f
);
1725 if (!srcdir_relative_path
)
1728 lang_index
= get_prefix_langdir_index (srcdir_relative_path
);
1729 if (lang_index
< 0 && strncmp (srcdir_relative_path
, "c-family", 8) == 0)
1731 else if (lang_index
>= 0)
1732 r
= lang_dir_names
[lang_index
];
1739 /* The gt- output file name for F. */
1742 get_file_gtfilename (const char *f
)
1744 /* Cook up an initial version of the gt- file name from the file real
1745 basename and the language name, if any. */
1747 const char *basename
= get_file_realbasename (f
);
1748 const char *langdir
= get_file_langdir (f
);
1751 (langdir
? xasprintf ("gt-%s-%s", langdir
, basename
)
1752 : xasprintf ("gt-%s", basename
));
1754 /* Then replace all non alphanumerics characters by '-' and change the
1755 extension to ".h". We expect the input filename extension was at least
1756 one character long. */
1760 for (; *s
!= '.'; s
++)
1761 if (!ISALNUM (*s
) && *s
!= '-')
1764 memcpy (s
, ".h", sizeof (".h"));
1769 /* An output file, suitable for definitions, that can see declarations
1770 made in INPUT_FILE and is linked into every language that uses
1774 get_output_file_with_visibility (const char *input_file
)
1778 const char *basename
;
1779 const char *for_name
;
1780 const char *output_name
;
1782 /* This can happen when we need a file with visibility on a
1783 structure that we've never seen. We have to just hope that it's
1784 globally visible. */
1785 if (input_file
== NULL
)
1786 input_file
= "system.h";
1788 /* In plugin mode, return NULL unless the input_file is one of the
1793 for (i
= 0; i
< nb_plugin_files
; i
++)
1794 if (strcmp (input_file
, plugin_files
[i
]) == 0)
1795 return plugin_output
;
1800 /* Determine the output file name. */
1801 basename
= get_file_basename (input_file
);
1803 len
= strlen (basename
);
1804 if ((len
> 2 && memcmp (basename
+ len
- 2, ".c", 2) == 0)
1805 || (len
> 2 && memcmp (basename
+ len
- 2, ".y", 2) == 0)
1806 || (len
> 3 && memcmp (basename
+ len
- 3, ".in", 3) == 0))
1808 output_name
= get_file_gtfilename (input_file
);
1809 for_name
= basename
;
1811 /* Some headers get used by more than one front-end; hence, it
1812 would be inappropriate to spew them out to a single gtype-<lang>.h
1813 (and gengtype doesn't know how to direct spewage into multiple
1814 gtype-<lang>.h headers at this time). Instead, we pair up these
1815 headers with source files (and their special purpose gt-*.h headers). */
1816 else if (strncmp (basename
, "c-family", 8) == 0
1817 && IS_DIR_SEPARATOR (basename
[8])
1818 && strcmp (basename
+ 9, "c-common.h") == 0)
1819 output_name
= "gt-c-family-c-common.h", for_name
= "c-family/c-common.c";
1820 else if (strcmp (basename
, "c-lang.h") == 0)
1821 output_name
= "gt-c-decl.h", for_name
= "c-decl.c";
1822 else if (strcmp (basename
, "c-tree.h") == 0)
1823 output_name
= "gt-c-decl.h", for_name
= "c-decl.c";
1824 else if (strncmp (basename
, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename
[2])
1825 && strcmp (basename
+ 3, "cp-tree.h") == 0)
1826 output_name
= "gt-cp-tree.h", for_name
= "cp/tree.c";
1827 else if (strncmp (basename
, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename
[2])
1828 && strcmp (basename
+ 3, "decl.h") == 0)
1829 output_name
= "gt-cp-decl.h", for_name
= "cp/decl.c";
1830 else if (strncmp (basename
, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename
[2])
1831 && strcmp (basename
+ 3, "name-lookup.h") == 0)
1832 output_name
= "gt-cp-name-lookup.h", for_name
= "cp/name-lookup.c";
1833 else if (strncmp (basename
, "objc", 4) == 0
1834 && IS_DIR_SEPARATOR (basename
[4])
1835 && strcmp (basename
+ 5, "objc-act.h") == 0)
1836 output_name
= "gt-objc-objc-act.h", for_name
= "objc/objc-act.c";
1839 int lang_index
= get_prefix_langdir_index (basename
);
1841 if (lang_index
>= 0)
1842 return base_files
[lang_index
];
1844 output_name
= "gtype-desc.c";
1848 /* Look through to see if we've ever seen this output filename before. */
1849 for (r
= output_files
; r
; r
= r
->next
)
1850 if (strcmp (r
->name
, output_name
) == 0)
1853 /* If not, create it. */
1854 r
= create_file (for_name
, output_name
);
1856 gcc_assert (r
&& r
->name
);
1860 /* The name of an output file, suitable for definitions, that can see
1861 declarations made in INPUT_FILE and is linked into every language
1862 that uses INPUT_FILE. */
1865 get_output_file_name (const char *input_file
)
1867 outf_p o
= get_output_file_with_visibility (input_file
);
1873 /* Check if existing file is equal to the in memory buffer. */
1876 is_file_equal (outf_p of
)
1878 FILE *newfile
= fopen (of
->name
, "r");
1881 if (newfile
== NULL
)
1885 for (i
= 0; i
< of
->bufused
; i
++)
1888 ch
= fgetc (newfile
);
1889 if (ch
== EOF
|| ch
!= (unsigned char) of
->buf
[i
])
1899 /* Copy the output to its final destination,
1900 but don't unnecessarily change modification times. */
1903 close_output_files (void)
1905 int nbwrittenfiles
= 0;
1908 for (of
= output_files
; of
; of
= of
->next
)
1911 if (!is_file_equal (of
))
1913 FILE *newfile
= NULL
;
1914 char *backupname
= NULL
;
1915 /* Back up the old version of the output file gt-FOO.c as
1916 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
1919 backupname
= concat (backup_dir
, "/",
1920 lbasename (of
->name
), "~", NULL
);
1921 if (!access (of
->name
, F_OK
) && rename (of
->name
, backupname
))
1922 fatal ("failed to back up %s as %s: %s",
1923 of
->name
, backupname
, xstrerror (errno
));
1926 newfile
= fopen (of
->name
, "w");
1927 if (newfile
== NULL
)
1928 fatal ("opening output file %s: %s", of
->name
, xstrerror (errno
));
1929 if (fwrite (of
->buf
, 1, of
->bufused
, newfile
) != of
->bufused
)
1930 fatal ("writing output file %s: %s", of
->name
, xstrerror (errno
));
1931 if (fclose (newfile
) != 0)
1932 fatal ("closing output file %s: %s", of
->name
, xstrerror (errno
));
1934 if (verbosity_level
>= 2 && backupname
)
1935 printf ("%s wrote #%-3d %s backed-up in %s\n",
1936 progname
, nbwrittenfiles
, of
->name
, backupname
);
1937 else if (verbosity_level
>= 1)
1938 printf ("%s write #%-3d %s\n", progname
, nbwrittenfiles
, of
->name
);
1943 /* output file remains unchanged. */
1944 if (verbosity_level
>= 2)
1945 printf ("%s keep %s\n", progname
, of
->name
);
1949 of
->bufused
= of
->buflength
= 0;
1951 if (verbosity_level
>= 1)
1952 printf ("%s wrote %d files.\n", progname
, nbwrittenfiles
);
1963 struct walk_type_data
;
1965 /* For scalars and strings, given the item in 'val'.
1966 For structures, given a pointer to the item in 'val'.
1967 For misc. pointers, given the item in 'val'.
1969 typedef void (*process_field_fn
) (type_p f
, const struct walk_type_data
* p
);
1970 typedef void (*func_name_fn
) (type_p s
, const struct walk_type_data
* p
);
1972 /* Parameters for write_types. */
1974 struct write_types_data
1977 const char *param_prefix
;
1978 const char *subfield_marker_routine
;
1979 const char *marker_routine
;
1980 const char *reorder_note_routine
;
1981 const char *comment
;
1982 int skip_hooks
; /* skip hook generation if non zero */
1985 static void output_escaped_param (struct walk_type_data
*d
,
1986 const char *, const char *);
1987 static void output_mangled_typename (outf_p
, const_type_p
);
1988 static void walk_type (type_p t
, struct walk_type_data
*d
);
1989 static void write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
1990 const struct write_types_data
*wtd
);
1991 static void write_types_process_field
1992 (type_p f
, const struct walk_type_data
*d
);
1993 static void write_types (outf_p output_header
,
1995 type_p param_structs
,
1996 const struct write_types_data
*wtd
);
1997 static void write_types_local_process_field
1998 (type_p f
, const struct walk_type_data
*d
);
1999 static void write_local_func_for_structure
2000 (const_type_p orig_s
, type_p s
, type_p
*param
);
2001 static void write_local (outf_p output_header
,
2002 type_p structures
, type_p param_structs
);
2003 static void write_enum_defn (type_p structures
, type_p param_structs
);
2004 static int contains_scalar_p (type_p t
);
2005 static void put_mangled_filename (outf_p
, const char *);
2006 static void finish_root_table (struct flist
*flp
, const char *pfx
,
2007 const char *tname
, const char *lastname
,
2009 static void write_root (outf_p
, pair_p
, type_p
, const char *, int,
2010 struct fileloc
*, const char *, bool);
2011 static void write_array (outf_p f
, pair_p v
,
2012 const struct write_types_data
*wtd
);
2013 static void write_roots (pair_p
, bool);
2015 /* Parameters for walk_type. */
2017 struct walk_type_data
2019 process_field_fn process_field
;
2024 const char *prev_val
[4];
2027 const struct fileloc
*line
;
2032 const char *reorder_fn
;
2034 bool fn_wants_lvalue
;
2037 /* Print a mangled name representing T to OF. */
2040 output_mangled_typename (outf_p of
, const_type_p t
)
2049 output_mangled_typename (of
, t
->u
.p
);
2059 case TYPE_LANG_STRUCT
:
2060 oprintf (of
, "%lu%s", (unsigned long) strlen (t
->u
.s
.tag
),
2063 case TYPE_PARAM_STRUCT
:
2066 for (i
= 0; i
< NUM_PARAM
; i
++)
2067 if (t
->u
.param_struct
.param
[i
] != NULL
)
2068 output_mangled_typename (of
, t
->u
.param_struct
.param
[i
]);
2069 output_mangled_typename (of
, t
->u
.param_struct
.stru
);
2077 /* Print PARAM to D->OF processing escapes. D->VAL references the
2078 current object, D->PREV_VAL the object containing the current
2079 object, ONAME is the name of the option and D->LINE is used to
2080 print error messages. */
2083 output_escaped_param (struct walk_type_data
*d
, const char *param
,
2088 for (p
= param
; *p
; p
++)
2090 oprintf (d
->of
, "%c", *p
);
2095 oprintf (d
->of
, "(%s)", d
->prev_val
[2]);
2098 oprintf (d
->of
, "(%s)", d
->prev_val
[0]);
2101 oprintf (d
->of
, "(%s)", d
->prev_val
[1]);
2105 const char *pp
= d
->val
+ strlen (d
->val
);
2106 while (pp
[-1] == ']')
2109 oprintf (d
->of
, "%s", pp
);
2113 error_at_line (d
->line
, "`%s' option contains bad escape %c%c",
2118 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2119 which is of type T. Write code to D->OF to constrain execution (at
2120 the point that D->PROCESS_FIELD is called) to the appropriate
2121 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2122 pointers to those objects. D->PREV_VAL lists the objects
2123 containing the current object, D->OPT is a list of options to
2124 apply, D->INDENT is the current indentation level, D->LINE is used
2125 to print error messages, D->BITMAP indicates which languages to
2126 print the structure for, and D->PARAM is the current parameter
2127 (from an enclosing param_is option). */
2130 walk_type (type_p t
, struct walk_type_data
*d
)
2132 const char *length
= NULL
;
2133 const char *desc
= NULL
;
2134 int maybe_undef_p
= 0;
2135 int use_param_num
= -1;
2136 int use_params_p
= 0;
2138 const struct nested_ptr_data
*nested_ptr_d
= NULL
;
2140 d
->needs_cast_p
= false;
2141 for (oo
= d
->opt
; oo
; oo
= oo
->next
)
2142 if (strcmp (oo
->name
, "length") == 0)
2144 else if (strcmp (oo
->name
, "maybe_undef") == 0)
2146 else if (strncmp (oo
->name
, "use_param", 9) == 0
2147 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2148 use_param_num
= oo
->name
[9] == '\0' ? 0 : oo
->name
[9] - '0';
2149 else if (strcmp (oo
->name
, "use_params") == 0)
2151 else if (strcmp (oo
->name
, "desc") == 0)
2153 else if (strcmp (oo
->name
, "mark_hook") == 0)
2155 else if (strcmp (oo
->name
, "nested_ptr") == 0)
2156 nested_ptr_d
= (const struct nested_ptr_data
*) oo
->info
;
2157 else if (strcmp (oo
->name
, "dot") == 0)
2159 else if (strcmp (oo
->name
, "tag") == 0)
2161 else if (strcmp (oo
->name
, "special") == 0)
2163 else if (strcmp (oo
->name
, "skip") == 0)
2165 else if (strcmp (oo
->name
, "default") == 0)
2167 else if (strcmp (oo
->name
, "descbits") == 0)
2169 else if (strcmp (oo
->name
, "param_is") == 0)
2171 else if (strncmp (oo
->name
, "param", 5) == 0
2172 && ISDIGIT (oo
->name
[5]) && strcmp (oo
->name
+ 6, "_is") == 0)
2174 else if (strcmp (oo
->name
, "chain_next") == 0)
2176 else if (strcmp (oo
->name
, "chain_prev") == 0)
2178 else if (strcmp (oo
->name
, "chain_circular") == 0)
2180 else if (strcmp (oo
->name
, "reorder") == 0)
2182 else if (strcmp (oo
->name
, "variable_size") == 0)
2185 error_at_line (d
->line
, "unknown option `%s'\n", oo
->name
);
2192 int pointer_p
= t
->kind
== TYPE_POINTER
;
2196 if (!UNION_OR_STRUCT_P (t
))
2197 error_at_line (d
->line
, "`use_params' option on unimplemented type");
2199 t
= find_param_structure (t
, d
->param
);
2201 t
= create_pointer (t
);
2204 if (use_param_num
!= -1)
2206 if (d
->param
!= NULL
&& d
->param
[use_param_num
] != NULL
)
2208 type_p nt
= d
->param
[use_param_num
];
2210 if (t
->kind
== TYPE_ARRAY
)
2211 nt
= create_array (nt
, t
->u
.a
.len
);
2212 else if (length
!= NULL
&& t
->kind
== TYPE_POINTER
)
2213 nt
= create_pointer (nt
);
2214 d
->needs_cast_p
= (t
->kind
!= TYPE_POINTER
2215 && (nt
->kind
== TYPE_POINTER
2216 || nt
->kind
== TYPE_STRING
));
2220 error_at_line (d
->line
, "no parameter defined for `%s'", d
->val
);
2224 && (t
->kind
!= TYPE_POINTER
|| !UNION_OR_STRUCT_P (t
->u
.p
)))
2226 error_at_line (d
->line
,
2227 "field `%s' has invalid option `maybe_undef_p'\n",
2236 d
->process_field (t
, d
);
2241 if (maybe_undef_p
&& t
->u
.p
->u
.s
.line
.file
== NULL
)
2243 oprintf (d
->of
, "%*sgcc_assert (!%s);\n", d
->indent
, "", d
->val
);
2249 if (!UNION_OR_STRUCT_P (t
->u
.p
)
2250 && t
->u
.p
->kind
!= TYPE_PARAM_STRUCT
)
2252 error_at_line (d
->line
,
2253 "field `%s' is pointer to unimplemented type",
2260 const char *oldprevval2
= d
->prev_val
[2];
2262 if (!UNION_OR_STRUCT_P (nested_ptr_d
->type
))
2264 error_at_line (d
->line
,
2265 "field `%s' has invalid "
2266 "option `nested_ptr'\n", d
->val
);
2270 d
->prev_val
[2] = d
->val
;
2271 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2273 d
->val
= xasprintf ("x%d", d
->counter
++);
2274 oprintf (d
->of
, "%*s%s %s * %s%s =\n", d
->indent
, "",
2275 (nested_ptr_d
->type
->kind
== TYPE_UNION
2276 ? "union" : "struct"),
2277 nested_ptr_d
->type
->u
.s
.tag
,
2278 d
->fn_wants_lvalue
? "" : "const ", d
->val
);
2279 oprintf (d
->of
, "%*s", d
->indent
+ 2, "");
2280 output_escaped_param (d
, nested_ptr_d
->convert_from
,
2282 oprintf (d
->of
, ";\n");
2284 d
->process_field (nested_ptr_d
->type
, d
);
2286 if (d
->fn_wants_lvalue
)
2288 oprintf (d
->of
, "%*s%s = ", d
->indent
, "",
2290 d
->prev_val
[2] = d
->val
;
2291 output_escaped_param (d
, nested_ptr_d
->convert_to
,
2293 oprintf (d
->of
, ";\n");
2297 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2298 d
->val
= d
->prev_val
[2];
2299 d
->prev_val
[2] = oldprevval2
;
2302 d
->process_field (t
->u
.p
, d
);
2306 int loopcounter
= d
->counter
++;
2307 const char *oldval
= d
->val
;
2308 const char *oldprevval3
= d
->prev_val
[3];
2311 oprintf (d
->of
, "%*sif (%s != NULL) {\n", d
->indent
, "", d
->val
);
2313 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2314 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != (size_t)(", d
->indent
,
2315 "", loopcounter
, loopcounter
);
2316 output_escaped_param (d
, length
, "length");
2317 oprintf (d
->of
, "); i%d++) {\n", loopcounter
);
2319 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2321 d
->prev_val
[3] = oldval
;
2322 walk_type (t
->u
.p
, d
);
2325 d
->prev_val
[3] = oldprevval3
;
2328 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2329 d
->process_field (t
, d
);
2331 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2338 int loopcounter
= d
->counter
++;
2339 const char *oldval
= d
->val
;
2342 /* If it's an array of scalars, we optimize by not generating
2344 if (t
->u
.a
.p
->kind
== TYPE_SCALAR
)
2347 /* When walking an array, compute the length and store it in a
2348 local variable before walking the array elements, instead of
2349 recomputing the length expression each time through the loop.
2350 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2351 where the length is stored in the first array element,
2352 because otherwise that operand can get overwritten on the
2354 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2356 oprintf (d
->of
, "%*ssize_t i%d;\n", d
->indent
, "", loopcounter
);
2357 oprintf (d
->of
, "%*ssize_t l%d = (size_t)(",
2358 d
->indent
, "", loopcounter
);
2360 output_escaped_param (d
, length
, "length");
2362 oprintf (d
->of
, "%s", t
->u
.a
.len
);
2363 oprintf (d
->of
, ");\n");
2365 oprintf (d
->of
, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2367 loopcounter
, loopcounter
, loopcounter
, loopcounter
);
2369 d
->val
= newval
= xasprintf ("%s[i%d]", oldval
, loopcounter
);
2371 walk_type (t
->u
.a
.p
, d
);
2376 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2378 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2386 const char *oldval
= d
->val
;
2387 const char *oldprevval1
= d
->prev_val
[1];
2388 const char *oldprevval2
= d
->prev_val
[2];
2389 const int union_p
= t
->kind
== TYPE_UNION
;
2390 int seen_default_p
= 0;
2393 if (!t
->u
.s
.line
.file
)
2394 error_at_line (d
->line
, "incomplete structure `%s'", t
->u
.s
.tag
);
2396 if ((d
->bitmap
& t
->u
.s
.bitmap
) != d
->bitmap
)
2398 error_at_line (d
->line
,
2399 "structure `%s' defined for mismatching languages",
2401 error_at_line (&t
->u
.s
.line
, "one structure defined here");
2404 /* Some things may also be defined in the structure's options. */
2405 for (o
= t
->u
.s
.opt
; o
; o
= o
->next
)
2406 if (!desc
&& strcmp (o
->name
, "desc") == 0)
2409 d
->prev_val
[2] = oldval
;
2410 d
->prev_val
[1] = oldprevval2
;
2415 error_at_line (d
->line
,
2416 "missing `desc' option for union `%s'",
2420 oprintf (d
->of
, "%*sswitch (", d
->indent
, "");
2421 output_escaped_param (d
, desc
, "desc");
2422 oprintf (d
->of
, ")\n");
2424 oprintf (d
->of
, "%*s{\n", d
->indent
, "");
2426 for (f
= t
->u
.s
.fields
; f
; f
= f
->next
)
2429 const char *dot
= ".";
2430 const char *tagid
= NULL
;
2433 int use_param_p
= 0;
2436 d
->reorder_fn
= NULL
;
2437 for (oo
= f
->opt
; oo
; oo
= oo
->next
)
2438 if (strcmp (oo
->name
, "dot") == 0)
2440 else if (strcmp (oo
->name
, "tag") == 0)
2442 else if (strcmp (oo
->name
, "skip") == 0)
2444 else if (strcmp (oo
->name
, "default") == 0)
2446 else if (strcmp (oo
->name
, "reorder") == 0)
2447 d
->reorder_fn
= oo
->info
;
2448 else if (strncmp (oo
->name
, "use_param", 9) == 0
2449 && (oo
->name
[9] == '\0' || ISDIGIT (oo
->name
[9])))
2455 if (union_p
&& tagid
)
2457 oprintf (d
->of
, "%*scase %s:\n", d
->indent
, "", tagid
);
2460 else if (union_p
&& default_p
)
2462 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
2466 else if (!union_p
&& (default_p
|| tagid
))
2467 error_at_line (d
->line
,
2468 "can't use `%s' outside a union on field `%s'",
2469 default_p
? "default" : "tag", f
->name
);
2470 else if (union_p
&& !(default_p
|| tagid
)
2471 && f
->type
->kind
== TYPE_SCALAR
)
2474 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2475 d
->line
->file
, d
->line
->line
, f
->name
);
2478 else if (union_p
&& !(default_p
|| tagid
))
2479 error_at_line (d
->line
,
2480 "field `%s' is missing `tag' or `default' option",
2484 d
->val
= newval
= xasprintf ("%s%s%s", oldval
, dot
, f
->name
);
2486 d
->used_length
= false;
2488 if (union_p
&& use_param_p
&& d
->param
== NULL
)
2489 oprintf (d
->of
, "%*sgcc_unreachable ();\n", d
->indent
, "");
2491 walk_type (f
->type
, d
);
2497 oprintf (d
->of
, "%*sbreak;\n", d
->indent
, "");
2501 d
->reorder_fn
= NULL
;
2504 d
->prev_val
[1] = oldprevval1
;
2505 d
->prev_val
[2] = oldprevval2
;
2507 if (union_p
&& !seen_default_p
)
2509 oprintf (d
->of
, "%*sdefault:\n", d
->indent
, "");
2510 oprintf (d
->of
, "%*s break;\n", d
->indent
, "");
2514 oprintf (d
->of
, "%*s}\n", d
->indent
, "");
2520 case TYPE_LANG_STRUCT
:
2523 for (nt
= t
->u
.s
.lang_struct
; nt
; nt
= nt
->next
)
2524 if ((d
->bitmap
& nt
->u
.s
.bitmap
) == d
->bitmap
)
2527 error_at_line (d
->line
, "structure `%s' differs between languages",
2534 case TYPE_PARAM_STRUCT
:
2536 type_p
*oldparam
= d
->param
;
2538 d
->param
= t
->u
.param_struct
.param
;
2539 walk_type (t
->u
.param_struct
.stru
, d
);
2540 d
->param
= oldparam
;
2549 /* process_field routine for marking routines. */
2552 write_types_process_field (type_p f
, const struct walk_type_data
*d
)
2554 const struct write_types_data
*wtd
;
2555 const char *cast
= d
->needs_cast_p
? "(void *)" : "";
2556 wtd
= (const struct write_types_data
*) d
->cookie
;
2561 oprintf (d
->of
, "%*s%s (%s%s", d
->indent
, "",
2562 wtd
->subfield_marker_routine
, cast
, d
->val
);
2563 if (wtd
->param_prefix
)
2565 oprintf (d
->of
, ", %s", d
->prev_val
[3]);
2568 oprintf (d
->of
, ", gt_%s_", wtd
->param_prefix
);
2569 output_mangled_typename (d
->of
, d
->orig_s
);
2572 oprintf (d
->of
, ", gt_%sa_%s", wtd
->param_prefix
, d
->prev_val
[0]);
2574 if (f
->u
.p
->kind
== TYPE_PARAM_STRUCT
2575 && f
->u
.p
->u
.s
.line
.file
!= NULL
)
2577 oprintf (d
->of
, ", gt_e_");
2578 output_mangled_typename (d
->of
, f
);
2580 else if (UNION_OR_STRUCT_P (f
) && f
->u
.p
->u
.s
.line
.file
!= NULL
)
2582 oprintf (d
->of
, ", gt_ggc_e_");
2583 output_mangled_typename (d
->of
, f
);
2586 oprintf (d
->of
, ", gt_types_enum_last");
2588 oprintf (d
->of
, ");\n");
2589 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
2590 oprintf (d
->of
, "%*s%s (%s%s, %s, %s);\n", d
->indent
, "",
2591 wtd
->reorder_note_routine
, cast
, d
->val
,
2592 d
->prev_val
[3], d
->reorder_fn
);
2598 case TYPE_LANG_STRUCT
:
2599 case TYPE_PARAM_STRUCT
:
2600 oprintf (d
->of
, "%*sgt_%s_", d
->indent
, "", wtd
->prefix
);
2601 output_mangled_typename (d
->of
, f
);
2602 oprintf (d
->of
, " (%s%s);\n", cast
, d
->val
);
2603 if (d
->reorder_fn
&& wtd
->reorder_note_routine
)
2604 oprintf (d
->of
, "%*s%s (%s%s, %s%s, %s);\n", d
->indent
, "",
2605 wtd
->reorder_note_routine
, cast
, d
->val
, cast
, d
->val
,
2617 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
2620 output_type_enum (outf_p of
, type_p s
)
2622 if (s
->kind
== TYPE_PARAM_STRUCT
&& s
->u
.param_struct
.line
.file
!= NULL
)
2624 oprintf (of
, ", gt_e_");
2625 output_mangled_typename (of
, s
);
2627 else if (UNION_OR_STRUCT_P (s
) && s
->u
.s
.line
.file
!= NULL
)
2629 oprintf (of
, ", gt_ggc_e_");
2630 output_mangled_typename (of
, s
);
2633 oprintf (of
, ", gt_types_enum_last");
2636 /* Return an output file that is suitable for definitions which can
2637 reference struct S */
2640 get_output_file_for_structure (const_type_p s
, type_p
*param
)
2642 const char *fn
= s
->u
.s
.line
.file
;
2645 /* This is a hack, and not the good kind either. */
2646 for (i
= NUM_PARAM
- 1; i
>= 0; i
--)
2647 if (param
&& param
[i
] && param
[i
]->kind
== TYPE_POINTER
2648 && UNION_OR_STRUCT_P (param
[i
]->u
.p
))
2649 fn
= param
[i
]->u
.p
->u
.s
.line
.file
;
2651 return get_output_file_with_visibility (fn
);
2654 /* For S, a structure that's part of ORIG_S, and using parameters
2655 PARAM, write out a routine that:
2656 - Takes a parameter, a void * but actually of type *S
2657 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
2658 field of S or its substructures and (in some cases) things
2659 that are pointed to by S.
2663 write_func_for_structure (type_p orig_s
, type_p s
, type_p
*param
,
2664 const struct write_types_data
*wtd
)
2666 const char *chain_next
= NULL
;
2667 const char *chain_prev
= NULL
;
2668 const char *chain_circular
= NULL
;
2669 const char *mark_hook_name
= NULL
;
2671 struct walk_type_data d
;
2673 memset (&d
, 0, sizeof (d
));
2674 d
.of
= get_output_file_for_structure (s
, param
);
2676 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2677 if (strcmp (opt
->name
, "chain_next") == 0)
2678 chain_next
= opt
->info
;
2679 else if (strcmp (opt
->name
, "chain_prev") == 0)
2680 chain_prev
= opt
->info
;
2681 else if (strcmp (opt
->name
, "chain_circular") == 0)
2682 chain_circular
= opt
->info
;
2683 else if (strcmp (opt
->name
, "mark_hook") == 0)
2684 mark_hook_name
= opt
->info
;
2686 if (chain_prev
!= NULL
&& chain_next
== NULL
)
2687 error_at_line (&s
->u
.s
.line
, "chain_prev without chain_next");
2688 if (chain_circular
!= NULL
&& chain_next
!= NULL
)
2689 error_at_line (&s
->u
.s
.line
, "chain_circular with chain_next");
2690 if (chain_circular
!= NULL
)
2691 chain_next
= chain_circular
;
2693 d
.process_field
= write_types_process_field
;
2697 d
.line
= &s
->u
.s
.line
;
2698 d
.bitmap
= s
->u
.s
.bitmap
;
2700 d
.prev_val
[0] = "*x";
2701 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
2702 d
.prev_val
[3] = "x";
2705 oprintf (d
.of
, "\n");
2706 oprintf (d
.of
, "void\n");
2708 oprintf (d
.of
, "gt_%sx_%s", wtd
->prefix
, orig_s
->u
.s
.tag
);
2711 oprintf (d
.of
, "gt_%s_", wtd
->prefix
);
2712 output_mangled_typename (d
.of
, orig_s
);
2714 oprintf (d
.of
, " (void *x_p)\n");
2715 oprintf (d
.of
, "{\n");
2716 oprintf (d
.of
, " %s %s * %sx = (%s %s *)x_p;\n",
2717 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
2718 chain_next
== NULL
? "const " : "",
2719 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2720 if (chain_next
!= NULL
)
2721 oprintf (d
.of
, " %s %s * xlimit = x;\n",
2722 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2723 if (chain_next
== NULL
)
2725 oprintf (d
.of
, " if (%s (x", wtd
->marker_routine
);
2726 if (wtd
->param_prefix
)
2728 oprintf (d
.of
, ", x, gt_%s_", wtd
->param_prefix
);
2729 output_mangled_typename (d
.of
, orig_s
);
2730 output_type_enum (d
.of
, orig_s
);
2732 oprintf (d
.of
, "))\n");
2736 if (chain_circular
!= NULL
)
2737 oprintf (d
.of
, " if (!%s (xlimit", wtd
->marker_routine
);
2739 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
2740 if (wtd
->param_prefix
)
2742 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
2743 output_mangled_typename (d
.of
, orig_s
);
2744 output_type_enum (d
.of
, orig_s
);
2746 oprintf (d
.of
, "))\n");
2747 if (chain_circular
!= NULL
)
2748 oprintf (d
.of
, " return;\n do\n");
2749 if (mark_hook_name
&& !wtd
->skip_hooks
)
2751 oprintf (d
.of
, " {\n");
2752 oprintf (d
.of
, " %s (xlimit);\n ", mark_hook_name
);
2754 oprintf (d
.of
, " xlimit = (");
2755 d
.prev_val
[2] = "*xlimit";
2756 output_escaped_param (&d
, chain_next
, "chain_next");
2757 oprintf (d
.of
, ");\n");
2758 if (mark_hook_name
&& !wtd
->skip_hooks
)
2759 oprintf (d
.of
, " }\n");
2760 if (chain_prev
!= NULL
)
2762 oprintf (d
.of
, " if (x != xlimit)\n");
2763 oprintf (d
.of
, " for (;;)\n");
2764 oprintf (d
.of
, " {\n");
2765 oprintf (d
.of
, " %s %s * const xprev = (",
2766 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
2768 d
.prev_val
[2] = "*x";
2769 output_escaped_param (&d
, chain_prev
, "chain_prev");
2770 oprintf (d
.of
, ");\n");
2771 oprintf (d
.of
, " if (xprev == NULL) break;\n");
2772 oprintf (d
.of
, " x = xprev;\n");
2773 oprintf (d
.of
, " (void) %s (xprev", wtd
->marker_routine
);
2774 if (wtd
->param_prefix
)
2776 oprintf (d
.of
, ", xprev, gt_%s_", wtd
->param_prefix
);
2777 output_mangled_typename (d
.of
, orig_s
);
2778 output_type_enum (d
.of
, orig_s
);
2780 oprintf (d
.of
, ");\n");
2781 oprintf (d
.of
, " }\n");
2783 if (chain_circular
!= NULL
)
2785 oprintf (d
.of
, " while (%s (xlimit", wtd
->marker_routine
);
2786 if (wtd
->param_prefix
)
2788 oprintf (d
.of
, ", xlimit, gt_%s_", wtd
->param_prefix
);
2789 output_mangled_typename (d
.of
, orig_s
);
2790 output_type_enum (d
.of
, orig_s
);
2792 oprintf (d
.of
, "));\n");
2793 if (mark_hook_name
&& !wtd
->skip_hooks
)
2794 oprintf (d
.of
, " %s (xlimit);\n", mark_hook_name
);
2795 oprintf (d
.of
, " do\n");
2798 oprintf (d
.of
, " while (x != xlimit)\n");
2800 oprintf (d
.of
, " {\n");
2801 if (mark_hook_name
&& chain_next
== NULL
&& !wtd
->skip_hooks
)
2803 oprintf (d
.of
, " %s (x);\n", mark_hook_name
);
2805 d
.prev_val
[2] = "*x";
2809 if (chain_next
!= NULL
)
2811 oprintf (d
.of
, " x = (");
2812 output_escaped_param (&d
, chain_next
, "chain_next");
2813 oprintf (d
.of
, ");\n");
2816 oprintf (d
.of
, " }\n");
2817 if (chain_circular
!= NULL
)
2818 oprintf (d
.of
, " while (x != xlimit);\n");
2819 oprintf (d
.of
, "}\n");
2822 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
2825 write_types (outf_p output_header
, type_p structures
, type_p param_structs
,
2826 const struct write_types_data
*wtd
)
2828 int nbfun
= 0; /* Count the emitted functions. */
2831 oprintf (output_header
, "\n/* %s*/\n", wtd
->comment
);
2832 /* We first emit the macros and the declarations. Functions' code is
2833 emitted afterwards. This is needed in plugin mode. */
2834 oprintf (output_header
, "/* macros and declarations */\n");
2835 for (s
= structures
; s
; s
= s
->next
)
2836 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
2840 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
2843 oprintf (output_header
, "#define gt_%s_", wtd
->prefix
);
2844 output_mangled_typename (output_header
, s
);
2845 oprintf (output_header
, "(X) do { \\\n");
2846 oprintf (output_header
,
2847 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd
->prefix
,
2849 oprintf (output_header
, " } while (0)\n");
2851 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2852 if (strcmp (opt
->name
, "ptr_alias") == 0)
2854 const_type_p
const t
= (const_type_p
) opt
->info
;
2855 if (t
->kind
== TYPE_STRUCT
2856 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
2857 oprintf (output_header
,
2858 "#define gt_%sx_%s gt_%sx_%s\n",
2859 wtd
->prefix
, s
->u
.s
.tag
, wtd
->prefix
, t
->u
.s
.tag
);
2861 error_at_line (&s
->u
.s
.line
,
2862 "structure alias is not a structure");
2868 /* Declare the marker procedure only once. */
2869 oprintf (output_header
,
2870 "extern void gt_%sx_%s (void *);\n",
2871 wtd
->prefix
, s
->u
.s
.tag
);
2873 if (s
->u
.s
.line
.file
== NULL
)
2875 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
2881 for (s
= param_structs
; s
; s
= s
->next
)
2882 if (s
->gc_used
== GC_POINTED_TO
)
2884 type_p stru
= s
->u
.param_struct
.stru
;
2886 /* Declare the marker procedure. */
2887 oprintf (output_header
, "extern void gt_%s_", wtd
->prefix
);
2888 output_mangled_typename (output_header
, s
);
2889 oprintf (output_header
, " (void *);\n");
2891 if (stru
->u
.s
.line
.file
== NULL
)
2893 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
2899 /* At last we emit the functions code. */
2900 oprintf (output_header
, "\n/* functions code */\n");
2901 for (s
= structures
; s
; s
= s
->next
)
2902 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
2906 if (s
->gc_used
== GC_MAYBE_POINTED_TO
&& s
->u
.s
.line
.file
== NULL
)
2908 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
2909 if (strcmp (opt
->name
, "ptr_alias") == 0)
2914 if (s
->kind
== TYPE_LANG_STRUCT
)
2917 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
2920 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
2921 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
2922 write_func_for_structure (s
, ss
, NULL
, wtd
);
2928 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
2929 nbfun
, (void*) s
, s
->u
.s
.tag
);
2930 write_func_for_structure (s
, s
, NULL
, wtd
);
2935 /* Structure s is not possibly pointed to, so can be ignored. */
2936 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
2937 (void*)s
, s
->u
.s
.tag
,
2941 for (s
= param_structs
; s
; s
= s
->next
)
2942 if (s
->gc_used
== GC_POINTED_TO
)
2944 type_p
*param
= s
->u
.param_struct
.param
;
2945 type_p stru
= s
->u
.param_struct
.stru
;
2946 if (stru
->u
.s
.line
.file
== NULL
)
2948 if (stru
->kind
== TYPE_LANG_STRUCT
)
2951 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
2954 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
2955 nbfun
, (void*) ss
, ss
->u
.s
.tag
);
2956 write_func_for_structure (s
, ss
, param
, wtd
);
2962 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
2964 (void*) stru
, stru
->u
.s
.tag
);
2965 write_func_for_structure (s
, stru
, param
, wtd
);
2970 /* Param structure s is not pointed to, so should be ignored. */
2971 DBGPRINTF ("ignored s @ %p", (void*)s
);
2973 if (verbosity_level
>= 2)
2974 printf ("%s emitted %d routines for %s\n",
2975 progname
, nbfun
, wtd
->comment
);
2978 static const struct write_types_data ggc_wtd
= {
2979 "ggc_m", NULL
, "ggc_mark", "ggc_test_and_set_mark", NULL
,
2980 "GC marker procedures. ",
2984 static const struct write_types_data pch_wtd
= {
2985 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
2986 "gt_pch_note_reorder",
2987 "PCH type-walking procedures. ",
2991 /* Write out the local pointer-walking routines. */
2993 /* process_field routine for local pointer-walking. */
2996 write_types_local_process_field (type_p f
, const struct walk_type_data
*d
)
3003 case TYPE_LANG_STRUCT
:
3004 case TYPE_PARAM_STRUCT
:
3006 oprintf (d
->of
, "%*sif ((void *)(%s) == this_obj)\n", d
->indent
, "",
3008 oprintf (d
->of
, "%*s op (&(%s), cookie);\n", d
->indent
, "", d
->val
);
3019 /* For S, a structure that's part of ORIG_S, and using parameters
3020 PARAM, write out a routine that:
3021 - Is of type gt_note_pointers
3022 - Calls PROCESS_FIELD on each field of S or its substructures.
3026 write_local_func_for_structure (const_type_p orig_s
, type_p s
, type_p
*param
)
3028 struct walk_type_data d
;
3030 memset (&d
, 0, sizeof (d
));
3031 d
.of
= get_output_file_for_structure (s
, param
);
3032 d
.process_field
= write_types_local_process_field
;
3034 d
.line
= &s
->u
.s
.line
;
3035 d
.bitmap
= s
->u
.s
.bitmap
;
3037 d
.prev_val
[0] = d
.prev_val
[2] = "*x";
3038 d
.prev_val
[1] = "not valid postage"; /* Guarantee an error. */
3039 d
.prev_val
[3] = "x";
3041 d
.fn_wants_lvalue
= true;
3043 oprintf (d
.of
, "\n");
3044 oprintf (d
.of
, "void\n");
3045 oprintf (d
.of
, "gt_pch_p_");
3046 output_mangled_typename (d
.of
, orig_s
);
3047 oprintf (d
.of
, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3049 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3050 "\tATTRIBUTE_UNUSED void *cookie)\n");
3051 oprintf (d
.of
, "{\n");
3052 oprintf (d
.of
, " %s %s * const x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3053 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
,
3054 s
->kind
== TYPE_UNION
? "union" : "struct", s
->u
.s
.tag
);
3057 oprintf (d
.of
, "}\n");
3060 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3063 write_local (outf_p output_header
, type_p structures
, type_p param_structs
)
3069 oprintf (output_header
, "\n/* Local pointer-walking routines. */\n");
3070 for (s
= structures
; s
; s
= s
->next
)
3071 if (s
->gc_used
== GC_POINTED_TO
|| s
->gc_used
== GC_MAYBE_POINTED_TO
)
3075 if (s
->u
.s
.line
.file
== NULL
)
3078 for (opt
= s
->u
.s
.opt
; opt
; opt
= opt
->next
)
3079 if (strcmp (opt
->name
, "ptr_alias") == 0)
3081 const_type_p
const t
= (const_type_p
) opt
->info
;
3082 if (t
->kind
== TYPE_STRUCT
3083 || t
->kind
== TYPE_UNION
|| t
->kind
== TYPE_LANG_STRUCT
)
3085 oprintf (output_header
, "#define gt_pch_p_");
3086 output_mangled_typename (output_header
, s
);
3087 oprintf (output_header
, " gt_pch_p_");
3088 output_mangled_typename (output_header
, t
);
3089 oprintf (output_header
, "\n");
3092 error_at_line (&s
->u
.s
.line
,
3093 "structure alias is not a structure");
3099 /* Declare the marker procedure only once. */
3100 oprintf (output_header
, "extern void gt_pch_p_");
3101 output_mangled_typename (output_header
, s
);
3102 oprintf (output_header
,
3103 "\n (void *, void *, gt_pointer_operator, void *);\n");
3105 if (s
->kind
== TYPE_LANG_STRUCT
)
3108 for (ss
= s
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3109 write_local_func_for_structure (s
, ss
, NULL
);
3112 write_local_func_for_structure (s
, s
, NULL
);
3115 for (s
= param_structs
; s
; s
= s
->next
)
3116 if (s
->gc_used
== GC_POINTED_TO
)
3118 type_p
*param
= s
->u
.param_struct
.param
;
3119 type_p stru
= s
->u
.param_struct
.stru
;
3121 /* Declare the marker procedure. */
3122 oprintf (output_header
, "extern void gt_pch_p_");
3123 output_mangled_typename (output_header
, s
);
3124 oprintf (output_header
,
3125 "\n (void *, void *, gt_pointer_operator, void *);\n");
3127 if (stru
->u
.s
.line
.file
== NULL
)
3129 fprintf (stderr
, "warning: structure `%s' used but not defined\n",
3134 if (stru
->kind
== TYPE_LANG_STRUCT
)
3137 for (ss
= stru
->u
.s
.lang_struct
; ss
; ss
= ss
->next
)
3138 write_local_func_for_structure (s
, ss
, param
);
3141 write_local_func_for_structure (s
, stru
, param
);
3145 /* Nonzero if S is a type for which typed GC allocators should be output. */
3147 #define USED_BY_TYPED_GC_P(s) \
3148 (((s->kind == TYPE_POINTER) \
3149 && ((s->u.p->gc_used == GC_POINTED_TO) \
3150 || (s->u.p->gc_used == GC_USED))) \
3151 || (UNION_OR_STRUCT_P (s) && \
3152 (((s)->gc_used == GC_POINTED_TO) \
3153 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
3154 && s->u.s.line.file != NULL) \
3155 || ((s)->gc_used == GC_USED \
3156 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
3159 /* Write out the 'enum' definition for gt_types_enum. */
3162 write_enum_defn (type_p structures
, type_p param_structs
)
3166 int nbparamstruct
= 0;
3170 oprintf (header_file
, "\n/* Enumeration of types known. */\n");
3171 oprintf (header_file
, "enum gt_types_enum {\n");
3172 for (s
= structures
; s
; s
= s
->next
)
3173 if (USED_BY_TYPED_GC_P (s
))
3176 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
3177 (void*) s
, nbstruct
);
3178 if (UNION_OR_STRUCT_P (s
))
3179 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
3180 (void*) s
, nbstruct
, s
->u
.s
.tag
);
3181 oprintf (header_file
, " gt_ggc_e_");
3182 output_mangled_typename (header_file
, s
);
3183 oprintf (header_file
, ",\n");
3185 for (s
= param_structs
; s
; s
= s
->next
)
3186 if (s
->gc_used
== GC_POINTED_TO
)
3189 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
3190 (void*) s
, nbparamstruct
);
3191 oprintf (header_file
, " gt_e_");
3192 output_mangled_typename (header_file
, s
);
3193 oprintf (header_file
, ",\n");
3195 oprintf (header_file
, " gt_types_enum_last\n");
3196 oprintf (header_file
, "};\n");
3197 if (verbosity_level
>= 2)
3198 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
3199 progname
, nbstruct
, nbparamstruct
);
3203 /* Might T contain any non-pointer elements? */
3206 contains_scalar_p (type_p t
)
3214 return contains_scalar_p (t
->u
.a
.p
);
3216 /* Could also check for structures that have no non-pointer
3217 fields, but there aren't enough of those to worry about. */
3222 /* Mangle FN and print it to F. */
3225 put_mangled_filename (outf_p f
, const char *fn
)
3227 const char *name
= get_output_file_name (fn
);
3230 for (; *name
!= 0; name
++)
3231 if (ISALNUM (*name
))
3232 oprintf (f
, "%c", *name
);
3234 oprintf (f
, "%c", '_');
3237 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
3238 LASTNAME, and NAME are all strings to insert in various places in
3239 the resulting code. */
3242 finish_root_table (struct flist
*flp
, const char *pfx
, const char *lastname
,
3243 const char *tname
, const char *name
)
3247 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
3248 if (fli2
->started_p
)
3250 oprintf (fli2
->f
, " %s\n", lastname
);
3251 oprintf (fli2
->f
, "};\n\n");
3254 for (fli2
= flp
; fli2
&& base_files
; fli2
= fli2
->next
)
3255 if (fli2
->started_p
)
3257 lang_bitmap bitmap
= get_lang_bitmap (fli2
->name
);
3260 for (fnum
= 0; bitmap
!= 0; fnum
++, bitmap
>>= 1)
3263 oprintf (base_files
[fnum
],
3264 "extern const struct %s gt_%s_", tname
, pfx
);
3265 put_mangled_filename (base_files
[fnum
], fli2
->name
);
3266 oprintf (base_files
[fnum
], "[];\n");
3272 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
3273 oprintf (base_files
[fnum
],
3274 "EXPORTED_CONST struct %s * const %s[] = {\n", tname
, name
);
3278 for (fli2
= flp
; fli2
; fli2
= fli2
->next
)
3279 if (fli2
->started_p
)
3281 lang_bitmap bitmap
= get_lang_bitmap (fli2
->name
);
3284 fli2
->started_p
= 0;
3286 for (fnum
= 0; base_files
&& bitmap
!= 0; fnum
++, bitmap
>>= 1)
3289 oprintf (base_files
[fnum
], " gt_%s_", pfx
);
3290 put_mangled_filename (base_files
[fnum
], fli2
->name
);
3291 oprintf (base_files
[fnum
], ",\n");
3297 for (fnum
= 0; base_files
&& fnum
< num_lang_dirs
; fnum
++)
3299 oprintf (base_files
[fnum
], " NULL\n");
3300 oprintf (base_files
[fnum
], "};\n");
3305 /* Write the first three fields (pointer, count and stride) for
3306 root NAME to F. V and LINE are as for write_root.
3308 Return true if the entry could be written; return false on error. */
3311 start_root_entry (outf_p f
, pair_p v
, const char *name
, struct fileloc
*line
)
3317 error_at_line (line
, "`%s' is too complex to be a root", name
);
3321 oprintf (f
, " {\n");
3322 oprintf (f
, " &%s,\n", name
);
3325 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
3327 oprintf (f
, " * (%s)", ap
->u
.a
.len
);
3328 else if (ap
== v
->type
)
3329 oprintf (f
, " * ARRAY_SIZE (%s)", v
->name
);
3331 oprintf (f
, " sizeof (%s", v
->name
);
3332 for (ap
= v
->type
; ap
->kind
== TYPE_ARRAY
; ap
= ap
->u
.a
.p
)
3334 oprintf (f
, "),\n");
3338 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
3339 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
3343 write_field_root (outf_p f
, pair_p v
, type_p type
, const char *name
,
3344 int has_length
, struct fileloc
*line
, const char *if_marked
,
3345 bool emit_pch
, type_p field_type
, const char *field_name
)
3347 /* If the field reference is relative to V, rather than to some
3348 subcomponent of V, we can mark any subarrays with a single stride.
3349 We're effectively treating the field as a global variable in its
3351 if (v
&& type
== v
->type
)
3356 newv
.type
= field_type
;
3357 newv
.name
= ACONCAT ((v
->name
, ".", field_name
, NULL
));
3360 /* Otherwise, any arrays nested in the structure are too complex to
3362 else if (field_type
->kind
== TYPE_ARRAY
)
3364 write_root (f
, v
, field_type
, ACONCAT ((name
, ".", field_name
, NULL
)),
3365 has_length
, line
, if_marked
, emit_pch
);
3368 /* Write out to F the table entry and any marker routines needed to
3369 mark NAME as TYPE. V can be one of three values:
3371 - null, if NAME is too complex to represent using a single
3372 count and stride. In this case, it is an error for NAME to
3373 contain any gc-ed data.
3375 - the outermost array that contains NAME, if NAME is part of an array.
3377 - the C variable that contains NAME, if NAME is not part of an array.
3379 LINE is the line of the C source that declares the root variable.
3380 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
3381 is nonzero iff we are building the root table for hash table caches. */
3384 write_root (outf_p f
, pair_p v
, type_p type
, const char *name
, int has_length
,
3385 struct fileloc
*line
, const char *if_marked
, bool emit_pch
)
3392 for (fld
= type
->u
.s
.fields
; fld
; fld
= fld
->next
)
3395 const char *desc
= NULL
;
3398 for (o
= fld
->opt
; o
; o
= o
->next
)
3399 if (strcmp (o
->name
, "skip") == 0)
3401 else if (strcmp (o
->name
, "desc") == 0)
3403 else if (strcmp (o
->name
, "param_is") == 0)
3406 error_at_line (line
,
3407 "field `%s' of global `%s' has unknown option `%s'",
3408 fld
->name
, name
, o
->name
);
3412 else if (desc
&& fld
->type
->kind
== TYPE_UNION
)
3414 pair_p validf
= NULL
;
3417 for (ufld
= fld
->type
->u
.s
.fields
; ufld
; ufld
= ufld
->next
)
3419 const char *tag
= NULL
;
3422 for (oo
= ufld
->opt
; oo
; oo
= oo
->next
)
3423 if (strcmp (oo
->name
, "tag") == 0)
3425 if (tag
== NULL
|| strcmp (tag
, desc
) != 0)
3428 error_at_line (line
,
3429 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
3430 name
, fld
->name
, validf
->name
,
3431 name
, fld
->name
, ufld
->name
, tag
);
3435 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
3436 emit_pch
, validf
->type
,
3437 ACONCAT ((fld
->name
, ".",
3438 validf
->name
, NULL
)));
3441 error_at_line (line
,
3442 "global `%s.%s' has `desc' option but is not union",
3445 write_field_root (f
, v
, type
, name
, 0, line
, if_marked
,
3446 emit_pch
, fld
->type
, fld
->name
);
3454 newname
= xasprintf ("%s[0]", name
);
3455 write_root (f
, v
, type
->u
.a
.p
, newname
, has_length
, line
, if_marked
,
3465 if (!start_root_entry (f
, v
, name
, line
))
3470 if (!has_length
&& UNION_OR_STRUCT_P (tp
))
3472 oprintf (f
, " >_ggc_mx_%s,\n", tp
->u
.s
.tag
);
3474 oprintf (f
, " >_pch_nx_%s", tp
->u
.s
.tag
);
3476 oprintf (f
, " NULL");
3478 else if (!has_length
&& tp
->kind
== TYPE_PARAM_STRUCT
)
3480 oprintf (f
, " >_ggc_m_");
3481 output_mangled_typename (f
, tp
);
3484 oprintf (f
, ",\n >_pch_n_");
3485 output_mangled_typename (f
, tp
);
3488 oprintf (f
, ",\n NULL");
3491 && (tp
->kind
== TYPE_POINTER
|| UNION_OR_STRUCT_P (tp
)))
3493 oprintf (f
, " >_ggc_ma_%s,\n", name
);
3495 oprintf (f
, " >_pch_na_%s", name
);
3497 oprintf (f
, " NULL");
3501 error_at_line (line
,
3502 "global `%s' is pointer to unimplemented type",
3506 oprintf (f
, ",\n &%s", if_marked
);
3507 oprintf (f
, "\n },\n");
3513 if (!start_root_entry (f
, v
, name
, line
))
3516 oprintf (f
, " (gt_pointer_walker) >_ggc_m_S,\n");
3517 oprintf (f
, " (gt_pointer_walker) >_pch_n_S\n");
3518 oprintf (f
, " },\n");
3526 error_at_line (line
, "global `%s' is unimplemented type", name
);
3530 /* This generates a routine to walk an array. */
3533 write_array (outf_p f
, pair_p v
, const struct write_types_data
*wtd
)
3535 struct walk_type_data d
;
3538 memset (&d
, 0, sizeof (d
));
3544 d
.bitmap
= get_lang_bitmap (v
->line
.file
);
3547 d
.prev_val
[3] = prevval3
= xasprintf ("&%s", v
->name
);
3549 if (wtd
->param_prefix
)
3551 oprintf (f
, "static void gt_%sa_%s\n", wtd
->param_prefix
, v
->name
);
3552 oprintf (f
, " (void *, void *, gt_pointer_operator, void *);\n");
3553 oprintf (f
, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
3554 wtd
->param_prefix
, v
->name
);
3556 " ATTRIBUTE_UNUSED void *x_p,\n"
3557 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3558 " ATTRIBUTE_UNUSED void * cookie)\n");
3559 oprintf (d
.of
, "{\n");
3560 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
3561 d
.process_field
= write_types_local_process_field
;
3562 walk_type (v
->type
, &d
);
3563 oprintf (f
, "}\n\n");
3567 oprintf (f
, "static void gt_%sa_%s (void *);\n", wtd
->prefix
, v
->name
);
3568 oprintf (f
, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
3569 wtd
->prefix
, v
->name
);
3571 d
.prev_val
[0] = d
.prev_val
[1] = d
.prev_val
[2] = d
.val
= v
->name
;
3572 d
.process_field
= write_types_process_field
;
3573 walk_type (v
->type
, &d
);
3575 oprintf (f
, "}\n\n");
3578 /* Output a table describing the locations and types of VARIABLES. */
3581 write_roots (pair_p variables
, bool emit_pch
)
3584 struct flist
*flp
= NULL
;
3586 for (v
= variables
; v
; v
= v
->next
)
3588 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3590 const char *length
= NULL
;
3591 int deletable_p
= 0;
3594 for (o
= v
->opt
; o
; o
= o
->next
)
3595 if (strcmp (o
->name
, "length") == 0)
3597 else if (strcmp (o
->name
, "deletable") == 0)
3599 else if (strcmp (o
->name
, "param_is") == 0)
3601 else if (strncmp (o
->name
, "param", 5) == 0
3602 && ISDIGIT (o
->name
[5]) && strcmp (o
->name
+ 6, "_is") == 0)
3604 else if (strcmp (o
->name
, "if_marked") == 0)
3607 error_at_line (&v
->line
,
3608 "global `%s' has unknown option `%s'",
3611 for (fli
= flp
; fli
; fli
= fli
->next
)
3612 if (fli
->f
== f
&& f
)
3616 fli
= XNEW (struct flist
);
3620 fli
->name
= v
->line
.file
;
3621 gcc_assert (fli
->name
);
3624 oprintf (f
, "\n/* GC roots. */\n\n");
3629 && v
->type
->kind
== TYPE_POINTER
3630 && (v
->type
->u
.p
->kind
== TYPE_POINTER
3631 || v
->type
->u
.p
->kind
== TYPE_STRUCT
))
3633 write_array (f
, v
, &ggc_wtd
);
3634 write_array (f
, v
, &pch_wtd
);
3638 for (v
= variables
; v
; v
= v
->next
)
3640 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3646 for (o
= v
->opt
; o
; o
= o
->next
)
3647 if (strcmp (o
->name
, "length") == 0)
3649 else if (strcmp (o
->name
, "deletable") == 0
3650 || strcmp (o
->name
, "if_marked") == 0)
3656 for (fli
= flp
; fli
; fli
= fli
->next
)
3659 if (!fli
->started_p
)
3663 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
3664 put_mangled_filename (f
, v
->line
.file
);
3665 oprintf (f
, "[] = {\n");
3668 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
3671 finish_root_table (flp
, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3674 for (v
= variables
; v
; v
= v
->next
)
3676 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3681 for (o
= v
->opt
; o
; o
= o
->next
)
3682 if (strcmp (o
->name
, "deletable") == 0)
3684 else if (strcmp (o
->name
, "if_marked") == 0)
3690 for (fli
= flp
; fli
; fli
= fli
->next
)
3693 if (!fli
->started_p
)
3697 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
3698 put_mangled_filename (f
, v
->line
.file
);
3699 oprintf (f
, "[] = {\n");
3702 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3706 finish_root_table (flp
, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3707 "gt_ggc_deletable_rtab");
3709 for (v
= variables
; v
; v
= v
->next
)
3711 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3713 const char *if_marked
= NULL
;
3717 for (o
= v
->opt
; o
; o
= o
->next
)
3718 if (strcmp (o
->name
, "length") == 0)
3720 else if (strcmp (o
->name
, "if_marked") == 0)
3721 if_marked
= o
->info
;
3723 if (if_marked
== NULL
)
3726 if (v
->type
->kind
!= TYPE_POINTER
3727 || v
->type
->u
.p
->kind
!= TYPE_PARAM_STRUCT
3728 || v
->type
->u
.p
->u
.param_struct
.stru
!= find_structure ("htab", 0))
3730 error_at_line (&v
->line
,
3731 "if_marked option used but not hash table");
3735 for (fli
= flp
; fli
; fli
= fli
->next
)
3738 if (!fli
->started_p
)
3742 oprintf (f
, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
3743 put_mangled_filename (f
, v
->line
.file
);
3744 oprintf (f
, "[] = {\n");
3747 write_root (f
, v
, v
->type
->u
.p
->u
.param_struct
.param
[0],
3748 v
->name
, length_p
, &v
->line
, if_marked
, emit_pch
);
3751 finish_root_table (flp
, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
3752 "gt_ggc_cache_rtab");
3757 for (v
= variables
; v
; v
= v
->next
)
3759 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3762 int if_marked_p
= 0;
3765 for (o
= v
->opt
; o
; o
= o
->next
)
3766 if (strcmp (o
->name
, "length") == 0)
3768 else if (strcmp (o
->name
, "if_marked") == 0)
3774 for (fli
= flp
; fli
; fli
= fli
->next
)
3777 if (!fli
->started_p
)
3781 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
3782 put_mangled_filename (f
, v
->line
.file
);
3783 oprintf (f
, "[] = {\n");
3786 write_root (f
, v
, v
->type
, v
->name
, length_p
, &v
->line
, NULL
, emit_pch
);
3789 finish_root_table (flp
, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3790 "gt_pch_cache_rtab");
3792 for (v
= variables
; v
; v
= v
->next
)
3794 outf_p f
= get_output_file_with_visibility (v
->line
.file
);
3799 for (o
= v
->opt
; o
; o
= o
->next
)
3800 if (strcmp (o
->name
, "deletable") == 0
3801 || strcmp (o
->name
, "if_marked") == 0)
3807 if (!contains_scalar_p (v
->type
))
3810 for (fli
= flp
; fli
; fli
= fli
->next
)
3813 if (!fli
->started_p
)
3817 oprintf (f
, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
3818 put_mangled_filename (f
, v
->line
.file
);
3819 oprintf (f
, "[] = {\n");
3822 oprintf (f
, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3826 finish_root_table (flp
, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3827 "gt_pch_scalar_rtab");
3830 /* Record the definition of a generic VEC structure, as if we had expanded
3831 the macros in vec.h:
3833 typedef struct VEC_<type>_base GTY(()) {
3836 <type> GTY((length ("%h.num"))) vec[1];
3839 where the GTY(()) tags are only present if is_scalar is _false_. */
3842 note_def_vec (const char *type_name
, bool is_scalar
, struct fileloc
*pos
)
3847 type_p len_ty
= create_scalar_type ("unsigned");
3848 const char *name
= concat ("VEC_", type_name
, "_base", (char *) 0);
3852 t
= create_scalar_type (type_name
);
3857 t
= resolve_typedef (type_name
, pos
);
3858 o
= create_option (0, "length", "%h.num");
3861 /* We assemble the field list in reverse order. */
3862 fields
= create_field_at (0, create_array (t
, "1"), "vec", o
, pos
);
3863 fields
= create_field_at (fields
, len_ty
, "alloc", 0, pos
);
3864 fields
= create_field_at (fields
, len_ty
, "num", 0, pos
);
3866 do_typedef (name
, new_structure (name
, 0, pos
, fields
, 0), pos
);
3869 /* Record the definition of an allocation-specific VEC structure, as if
3870 we had expanded the macros in vec.h:
3872 typedef struct VEC_<type>_<astrat> {
3873 VEC_<type>_base base;
3874 } VEC_<type>_<astrat>;
3877 note_def_vec_alloc (const char *type
, const char *astrat
, struct fileloc
*pos
)
3879 const char *astratname
= concat ("VEC_", type
, "_", astrat
, (char *) 0);
3880 const char *basename
= concat ("VEC_", type
, "_base", (char *) 0);
3882 pair_p field
= create_field_at (0, resolve_typedef (basename
, pos
),
3885 do_typedef (astratname
, new_structure (astratname
, 0, pos
, field
, 0), pos
);
3888 /* Returns the specifier keyword for a string or union type S, empty string
3892 get_type_specifier (const type_p s
)
3894 if (s
->kind
== TYPE_STRUCT
|| s
->kind
== TYPE_LANG_STRUCT
)
3896 if (s
->kind
== TYPE_UNION
)
3901 /* TRUE if type S has the GTY variable_size annotation. */
3904 variable_size_p (const type_p s
)
3907 for (o
= s
->u
.s
.opt
; o
; o
= o
->next
)
3908 if (strcmp (o
->name
, "variable_size") == 0)
3916 { any_zone
, specific_zone
};
3918 /* Writes one typed allocator definition for type identifier TYPE_NAME with
3919 optional type specifier TYPE_SPECIFIER. The allocator name will contain
3920 ALLOCATOR_TYPE. If VARIABLE_SIZE is true, the allocator will have an extra
3921 parameter specifying number of bytes to allocate. If QUANTITY is set to
3922 VECTOR, a vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
3923 the allocator will be zone-specific. */
3926 write_typed_alloc_def (bool variable_size
, const char *type_specifier
,
3927 const char *type_name
, const char *allocator_type
,
3928 enum alloc_quantity quantity
, enum alloc_zone zone
)
3930 bool two_args
= variable_size
&& (quantity
== vector
);
3931 bool third_arg
= ((zone
== specific_zone
)
3932 && (variable_size
|| (quantity
== vector
)));
3934 oprintf (header_file
, "#define ggc_alloc_%s%s", allocator_type
, type_name
);
3935 oprintf (header_file
, "(%s%s%s%s%s) ",
3936 (variable_size
? "SIZE" : ""),
3937 (two_args
? ", " : ""),
3938 (quantity
== vector
) ? "n" : "",
3939 (third_arg
? ", " : ""), (zone
== specific_zone
) ? "z" : "");
3940 oprintf (header_file
, "((%s%s *)", type_specifier
, type_name
);
3941 oprintf (header_file
, "(ggc_internal_%salloc_stat (", allocator_type
);
3942 if (zone
== specific_zone
)
3943 oprintf (header_file
, "z, ");
3945 oprintf (header_file
, "SIZE");
3947 oprintf (header_file
, "sizeof (%s%s)", type_specifier
, type_name
);
3948 if (quantity
== vector
)
3949 oprintf (header_file
, ", n");
3950 oprintf (header_file
, " MEM_STAT_INFO)))\n");
3953 /* Writes a typed allocator definition for a struct or union S. */
3956 write_typed_struct_alloc_def (const type_p s
, const char *allocator_type
,
3957 enum alloc_quantity quantity
,
3958 enum alloc_zone zone
)
3960 write_typed_alloc_def (variable_size_p (s
), get_type_specifier (s
),
3961 s
->u
.s
.tag
, allocator_type
, quantity
, zone
);
3964 /* Writes a typed allocator definition for a typedef P. */
3967 write_typed_typedef_alloc_def (const pair_p p
, const char *allocator_type
,
3968 enum alloc_quantity quantity
,
3969 enum alloc_zone zone
)
3971 write_typed_alloc_def (variable_size_p (p
->type
), "", p
->name
,
3972 allocator_type
, quantity
, zone
);
3975 /* Writes typed allocator definitions for the types in STRUCTURES and
3976 TYPEDEFS that are used by GC. */
3979 write_typed_alloc_defns (const type_p structures
, const pair_p typedefs
)
3984 oprintf (header_file
,
3985 "\n/* Allocators for known structs and unions. */\n\n");
3986 for (s
= structures
; s
; s
= s
->next
)
3988 if (!USED_BY_TYPED_GC_P (s
))
3990 write_typed_struct_alloc_def (s
, "", single
, any_zone
);
3991 write_typed_struct_alloc_def (s
, "cleared_", single
, any_zone
);
3992 write_typed_struct_alloc_def (s
, "vec_", vector
, any_zone
);
3993 write_typed_struct_alloc_def (s
, "cleared_vec_", vector
, any_zone
);
3994 write_typed_struct_alloc_def (s
, "zone_", single
, specific_zone
);
3995 write_typed_struct_alloc_def (s
, "zone_cleared_", single
,
3997 write_typed_struct_alloc_def (s
, "zone_vec_", vector
, specific_zone
);
3998 write_typed_struct_alloc_def (s
, "zone_cleared_vec_", vector
,
4002 oprintf (header_file
, "\n/* Allocators for known typedefs. */\n");
4003 for (p
= typedefs
; p
; p
= p
->next
)
4006 if (!USED_BY_TYPED_GC_P (s
) || (strcmp (p
->name
, s
->u
.s
.tag
) == 0))
4008 write_typed_typedef_alloc_def (p
, "", single
, any_zone
);
4009 write_typed_typedef_alloc_def (p
, "cleared_", single
, any_zone
);
4010 write_typed_typedef_alloc_def (p
, "vec_", vector
, any_zone
);
4011 write_typed_typedef_alloc_def (p
, "cleared_vec_", vector
, any_zone
);
4012 write_typed_typedef_alloc_def (p
, "zone_", single
, specific_zone
);
4013 write_typed_typedef_alloc_def (p
, "zone_cleared_", single
,
4015 write_typed_typedef_alloc_def (p
, "zone_cleared_vec_", vector
,
4020 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4021 guaranteee for somewhat increased readability. If name conflicts do happen,
4022 this funcion will have to be adjusted to be more like
4023 output_mangled_typename. */
4026 output_typename (outf_p of
, const_type_p t
)
4031 oprintf (of
, "str");
4034 oprintf (of
, "scalar");
4037 output_typename (of
, t
->u
.p
);
4041 case TYPE_LANG_STRUCT
:
4042 oprintf (of
, "%s", t
->u
.s
.tag
);
4044 case TYPE_PARAM_STRUCT
:
4047 for (i
= 0; i
< NUM_PARAM
; i
++)
4048 if (t
->u
.param_struct
.param
[i
] != NULL
)
4050 output_typename (of
, t
->u
.param_struct
.param
[i
]);
4053 output_typename (of
, t
->u
.param_struct
.stru
);
4061 /* Writes a typed GC allocator for type S that is suitable as a callback for
4062 the splay tree implementation in libiberty. */
4065 write_splay_tree_allocator_def (const_type_p s
)
4067 outf_p of
= get_output_file_for_structure (s
, NULL
);
4068 oprintf (of
, "void * ggc_alloc_splay_tree_");
4069 output_typename (of
, s
);
4070 oprintf (of
, " (int sz, void * nl)\n");
4071 oprintf (of
, "{\n");
4072 oprintf (of
, " return ggc_splay_alloc (");
4073 oprintf (of
, "gt_e_");
4074 output_mangled_typename (of
, s
);
4075 oprintf (of
, ", sz, nl);\n");
4076 oprintf (of
, "}\n\n");
4079 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4080 for the splay tree implementation in libiberty. */
4083 write_splay_tree_allocators (const_type_p param_structs
)
4087 oprintf (header_file
, "\n/* Splay tree callback allocators. */\n");
4088 for (s
= param_structs
; s
; s
= s
->next
)
4089 if (s
->gc_used
== GC_POINTED_TO
)
4091 oprintf (header_file
, "extern void * ggc_alloc_splay_tree_");
4092 output_typename (header_file
, s
);
4093 oprintf (header_file
, " (int, void *);\n");
4094 write_splay_tree_allocator_def (s
);
4098 static void dump_pair (int indent
, pair_p p
);
4099 static void dump_type (int indent
, type_p p
);
4100 static void dump_type_list (int indent
, type_p p
);
4104 /* Dumps the value of typekind KIND. */
4107 dump_typekind (int indent
, enum typekind kind
)
4109 printf ("%*ckind = ", indent
, ' ');
4113 printf ("TYPE_SCALAR");
4116 printf ("TYPE_STRING");
4119 printf ("TYPE_STRUCT");
4122 printf ("TYPE_UNION");
4125 printf ("TYPE_POINTER");
4128 printf ("TYPE_ARRAY");
4130 case TYPE_LANG_STRUCT
:
4131 printf ("TYPE_LANG_STRUCT");
4133 case TYPE_PARAM_STRUCT
:
4134 printf ("TYPE_PARAM_STRUCT");
4142 /* Dumps the value of GC_USED flag. */
4145 dump_gc_used (int indent
, enum gc_used_enum gc_used
)
4147 printf ("%*cgc_used = ", indent
, ' ');
4151 printf ("GC_UNUSED");
4156 case GC_MAYBE_POINTED_TO
:
4157 printf ("GC_MAYBE_POINTED_TO");
4160 printf ("GC_POINTED_TO");
4168 /* Dumps the type options OPT. */
4171 dump_options (int indent
, options_p opt
)
4174 printf ("%*coptions = ", indent
, ' ');
4178 printf ("%s:%s ", o
->name
, o
->info
);
4184 /* Dumps the source file location in LINE. */
4187 dump_fileloc (int indent
, struct fileloc line
)
4189 printf ("%*cfileloc: file = %s, line = %d\n", indent
, ' ', line
.file
,
4193 /* Recursively dumps the struct, union, or a language-specific
4197 dump_type_u_s (int indent
, type_p t
)
4201 gcc_assert (t
->kind
== TYPE_STRUCT
|| t
->kind
== TYPE_UNION
4202 || t
->kind
== TYPE_LANG_STRUCT
);
4203 printf ("%*cu.s.tag = %s\n", indent
, ' ', t
->u
.s
.tag
);
4204 dump_fileloc (indent
, t
->u
.s
.line
);
4205 printf ("%*cu.s.fields =\n", indent
, ' ');
4206 fields
= t
->u
.s
.fields
;
4209 dump_pair (indent
+ INDENT
, fields
);
4210 fields
= fields
->next
;
4212 printf ("%*cend of fields of type %p\n", indent
, ' ', (void *) t
);
4213 dump_options (indent
, t
->u
.s
.opt
);
4214 printf ("%*cu.s.bitmap = %X\n", indent
, ' ', t
->u
.s
.bitmap
);
4215 if (t
->kind
== TYPE_LANG_STRUCT
)
4217 printf ("%*cu.s.lang_struct:\n", indent
, ' ');
4218 dump_type_list (indent
+ INDENT
, t
->u
.s
.lang_struct
);
4222 /* Recursively dumps the array T. */
4225 dump_type_u_a (int indent
, type_p t
)
4227 gcc_assert (t
->kind
== TYPE_ARRAY
);
4228 printf ("%*clen = %s, u.a.p:\n", indent
, ' ', t
->u
.a
.len
);
4229 dump_type_list (indent
+ INDENT
, t
->u
.a
.p
);
4232 /* Recursively dumps the parameterized struct T. */
4235 dump_type_u_param_struct (int indent
, type_p t
)
4238 gcc_assert (t
->kind
== TYPE_PARAM_STRUCT
);
4239 printf ("%*cu.param_struct.stru:\n", indent
, ' ');
4240 dump_type_list (indent
, t
->u
.param_struct
.stru
);
4241 dump_fileloc (indent
, t
->u
.param_struct
.line
);
4242 for (i
= 0; i
< NUM_PARAM
; i
++)
4244 if (t
->u
.param_struct
.param
[i
] == NULL
)
4246 printf ("%*cu.param_struct.param[%d]:\n", indent
, ' ', i
);
4247 dump_type (indent
+ INDENT
, t
->u
.param_struct
.param
[i
]);
4251 /* Recursively dumps the type list T. */
4254 dump_type_list (int indent
, type_p t
)
4259 dump_type (indent
, p
);
4264 static htab_t seen_types
;
4266 /* Recursively dumps the type T if it was not dumped previously. */
4269 dump_type (int indent
, type_p t
)
4273 printf ("%*cType at %p: ", indent
, ' ', (void *) t
);
4274 slot
= htab_find_slot (seen_types
, t
, INSERT
);
4277 printf ("already seen.\n");
4283 dump_typekind (indent
, t
->kind
);
4284 printf ("%*cpointer_to = %p\n", indent
+ INDENT
, ' ',
4285 (void *) t
->pointer_to
);
4286 dump_gc_used (indent
+ INDENT
, t
->gc_used
);
4290 printf ("%*cscalar_is_char = %s\n", indent
+ INDENT
, ' ',
4291 t
->u
.scalar_is_char
? "true" : "false");
4297 case TYPE_LANG_STRUCT
:
4298 dump_type_u_s (indent
+ INDENT
, t
);
4301 printf ("%*cp:\n", indent
+ INDENT
, ' ');
4302 dump_type (indent
+ INDENT
, t
->u
.p
);
4305 dump_type_u_a (indent
+ INDENT
, t
);
4307 case TYPE_PARAM_STRUCT
:
4308 dump_type_u_param_struct (indent
+ INDENT
, t
);
4313 printf ("%*cEnd of type at %p\n", indent
, ' ', (void *) t
);
4316 /* Dumps the pair P. */
4319 dump_pair (int indent
, pair_p p
)
4321 printf ("%*cpair: name = %s\n", indent
, ' ', p
->name
);
4322 dump_type (indent
, p
->type
);
4323 dump_fileloc (indent
, p
->line
);
4324 dump_options (indent
, p
->opt
);
4325 printf ("%*cEnd of pair %s\n", indent
, ' ', p
->name
);
4328 /* Dumps the list of pairs PP. */
4331 dump_pair_list (const char *name
, pair_p pp
)
4334 printf ("%s:\n", name
);
4335 for (p
= pp
; p
!= NULL
; p
= p
->next
)
4337 printf ("End of %s\n\n", name
);
4340 /* Dumps the STRUCTURES. */
4343 dump_structures (const char *name
, type_p structures
)
4345 printf ("%s:\n", name
);
4346 dump_type_list (0, structures
);
4347 printf ("End of %s\n\n", name
);
4350 /* Dumps the internal structures of gengtype. */
4353 dump_everything (void)
4355 seen_types
= htab_create (100, htab_hash_pointer
, htab_eq_pointer
, NULL
);
4356 dump_pair_list ("typedefs", typedefs
);
4357 dump_structures ("structures", structures
);
4358 dump_structures ("param_structs", param_structs
);
4359 dump_pair_list ("variables", variables
);
4360 htab_delete (seen_types
);
4365 /* Option specification for getopt_long. */
4366 static const struct option gengtype_long_options
[] = {
4367 {"help", no_argument
, NULL
, 'h'},
4368 {"version", no_argument
, NULL
, 'V'},
4369 {"verbose", no_argument
, NULL
, 'v'},
4370 {"dump", no_argument
, NULL
, 'd'},
4371 {"debug", no_argument
, NULL
, 'D'},
4372 {"plugin", required_argument
, NULL
, 'P'},
4373 {"srcdir", required_argument
, NULL
, 'S'},
4374 {"backupdir", required_argument
, NULL
, 'B'},
4375 {"inputs", required_argument
, NULL
, 'I'},
4376 {"read-state", required_argument
, NULL
, 'r'},
4377 {"write-state", required_argument
, NULL
, 'w'},
4378 /* Terminating NULL placeholder. */
4379 {NULL
, no_argument
, NULL
, 0},
4386 printf ("Usage: %s\n", progname
);
4387 printf ("\t -h | --help " " \t# Give this help.\n");
4388 printf ("\t -D | --debug "
4389 " \t# Give debug output to debug %s itself.\n", progname
);
4390 printf ("\t -V | --version " " \t# Give version information.\n");
4391 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
4392 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4393 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4394 " \t# Generate for plugin.\n");
4395 printf ("\t -S | --srcdir <GCC-directory> "
4396 " \t# Specify the GCC source directory.\n");
4397 printf ("\t -B | --backupdir <directory> "
4398 " \t# Specify the backup directory for updated files.\n");
4399 printf ("\t -I | --inputs <input-list> "
4400 " \t# Specify the file with source files list.\n");
4401 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
4402 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
4406 print_version (void)
4408 printf ("%s %s%s\n", progname
, pkgversion_string
, version_string
);
4409 printf ("Report bugs: %s\n", bug_report_url
);
4412 /* Parse the program options using getopt_long... */
4414 parse_program_options (int argc
, char **argv
)
4417 while ((opt
= getopt_long (argc
, argv
, "hVvdP:S:B:I:w:r:D",
4418 gengtype_long_options
, NULL
)) >= 0)
4422 case 'h': /* --help */
4425 case 'V': /* --version */
4428 case 'd': /* --dump */
4431 case 'D': /* --debug */
4434 case 'v': /* --verbose */
4437 case 'P': /* --plugin */
4439 plugin_output_filename
= optarg
;
4441 fatal ("missing plugin output file name");
4443 case 'S': /* --srcdir */
4447 fatal ("missing source directory");
4448 srcdir_len
= strlen (srcdir
);
4450 case 'B': /* --backupdir */
4452 backup_dir
= optarg
;
4454 fatal ("missing backup directory");
4456 case 'I': /* --inputs */
4460 fatal ("missing input list");
4462 case 'r': /* --read-state */
4464 read_state_filename
= optarg
;
4466 fatal ("missing read state file");
4467 DBGPRINTF ("read state %s\n", optarg
);
4469 case 'w': /* --write-state */
4470 DBGPRINTF ("write state %s\n", optarg
);
4472 write_state_filename
= optarg
;
4474 fatal ("missing write state file");
4477 fprintf (stderr
, "%s: unknown flag '%c'\n", progname
, opt
);
4479 fatal ("unexpected flag");
4482 if (plugin_output_filename
)
4484 /* In plugin mode we require some input files. */
4487 fatal ("no source files given in plugin mode");
4488 nb_plugin_files
= argc
- optind
;
4489 plugin_files
= XNEWVEC (char*, nb_plugin_files
);
4490 for (i
= 0; i
< (int) nb_plugin_files
; i
++)
4492 char *name
= argv
[i
+ optind
];
4493 plugin_files
[i
] = name
;
4500 main (int argc
, char **argv
)
4503 static struct fileloc pos
= { NULL
, 0 };
4504 outf_p output_header
;
4506 /* Mandatory common initializations. */
4507 progname
= "gengtype"; /* For fatal and messages. */
4508 /* Set the scalar_is_char union number for predefined scalar types. */
4509 scalar_nonchar
.u
.scalar_is_char
= FALSE
;
4510 scalar_char
.u
.scalar_is_char
= TRUE
;
4512 parse_program_options (argc
, argv
);
4517 time_t now
= (time_t) 0;
4519 DBGPRINTF ("gengtype started pid %d at %s",
4520 (int) getpid (), ctime (&now
));
4522 #endif /* ENABLE_CHECKING */
4524 /* Parse the input list and the input files. */
4525 DBGPRINTF ("inputlist %s", inputlist
);
4526 if (read_state_filename
)
4528 fatal ("read state %s not implemented yet", read_state_filename
);
4529 /* TODO: implement read state. */
4533 /* These types are set up with #define or else outside of where
4534 we can see them. We should initialize them before calling
4536 pos
.file
= this_file
;
4537 pos
.line
= __LINE__
+ 1;
4538 do_scalar_typedef ("CUMULATIVE_ARGS", &pos
);
4540 do_scalar_typedef ("REAL_VALUE_TYPE", &pos
);
4542 do_scalar_typedef ("FIXED_VALUE_TYPE", &pos
);
4544 do_scalar_typedef ("double_int", &pos
);
4546 do_scalar_typedef ("uint64_t", &pos
);
4548 do_scalar_typedef ("uint8", &pos
);
4550 do_scalar_typedef ("jword", &pos
);
4552 do_scalar_typedef ("JCF_u2", &pos
);
4554 do_scalar_typedef ("void", &pos
);
4556 do_typedef ("PTR", create_pointer (resolve_typedef ("void", &pos
)),
4558 read_input_list (inputlist
);
4559 for (i
= 0; i
< num_gt_files
; i
++)
4561 parse_file (gt_files
[i
]);
4562 DBGPRINTF ("parsed file #%d %s", (int) i
, gt_files
[i
]);
4564 if (verbosity_level
>= 1)
4565 printf ("%s parsed %d files\n", progname
, (int) num_gt_files
);
4567 DBGPRINT_COUNT_TYPE ("structures after parsing", structures
);
4568 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs
);
4572 fatal ("either an input list or a read state file should be given");
4577 if (plugin_output_filename
)
4580 /* In plugin mode, we should have read a state file, and have
4581 given at least one plugin file. */
4582 if (!read_state_filename
)
4583 fatal ("No read state given in plugin mode for %s",
4584 plugin_output_filename
);
4586 if (nb_plugin_files
== 0 || !plugin_files
)
4587 fatal ("No plugin files given in plugin mode for %s",
4588 plugin_output_filename
);
4590 /* Parse our plugin files. */
4591 for (ix
= 0; ix
< nb_plugin_files
; ix
++)
4592 parse_file (plugin_files
[ix
]);
4597 plugin_output
= create_file ("GCC", plugin_output_filename
);
4598 DBGPRINTF ("created plugin_output %p named %s",
4599 (void *) plugin_output
, plugin_output
->name
);
4602 { /* No plugin files, we are in normal mode. */
4604 fatal ("gengtype needs a source directory in normal mode");
4611 /* The call to set_gc_used may indirectly call find_param_structure
4612 hence enlarge the param_structs list of types. */
4613 set_gc_used (variables
);
4615 /* We should write the state here, but it is not yet implemented. */
4616 if (write_state_filename
)
4618 fatal ("write state %s in not yet implemented", write_state_filename
);
4619 /* TODO: implement write state. */
4625 write_enum_defn (structures
, param_structs
);
4626 write_typed_alloc_defns (structures
, typedefs
);
4627 output_header
= plugin_output
? plugin_output
: header_file
;
4628 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
4630 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
4633 write_types (output_header
, structures
, param_structs
, &ggc_wtd
);
4634 if (plugin_files
== NULL
)
4636 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
4638 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
4640 write_types (header_file
, structures
, param_structs
, &pch_wtd
);
4641 write_local (header_file
, structures
, param_structs
);
4643 write_splay_tree_allocators (param_structs
);
4644 write_roots (variables
, plugin_files
== NULL
);
4646 close_output_files ();
4651 /* Don't bother about free-ing any input or plugin file, etc. */