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)
133 /* The list of output files. */
136 /* The output header file that is included into pretty much every
141 /* The name of the file containing the list of input files. */
142 static char *inputlist;
144 /* The plugin input files and their number; in that case only
145 a single file is produced. */
146 static input_file **plugin_files;
147 static size_t nb_plugin_files;
149 /* The generated plugin output file and name. */
150 static outf_p plugin_output;
151 static char *plugin_output_filename;
153 /* Our source directory and its length. */
157 /* Variables used for reading and writing the state. */
158 const char *read_state_filename;
159 const char *write_state_filename;
161 /* Variables to help debugging. */
165 /* Level for verbose messages. */
168 /* The backup directory should be in the same file system as the
169 generated files, otherwise the rename(2) system call would fail.
170 If NULL, no backup is made when overwriting a generated file. */
171 static const char* backup_dir; /* (-B) program option. */
174 static outf_p create_file (const char *, const char *);
176 static const char *get_file_basename (const input_file *);
177 static const char *get_file_realbasename (const input_file *);
179 static int get_prefix_langdir_index (const char *);
180 static const char *get_file_langdir (const input_file *);
183 /* Nonzero iff an error has occurred. */
184 bool hit_error = false;
186 static void gen_rtx_next (void);
187 static void write_rtx_next (void);
188 static void open_base_files (void);
189 static void close_output_files (void);
191 /* Report an error at POS, printing MSG. */
194 error_at_line (const struct fileloc *pos, const char *msg, ...)
198 gcc_assert (pos != NULL && pos->file != NULL);
201 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
202 vfprintf (stderr, msg, ap);
203 fputc ('\n', stderr);
209 /* asprintf, but produces fatal message on out-of-memory. */
211 xasprintf (const char *format, ...)
217 va_start (ap, format);
218 n = vasprintf (&result, format, ap);
219 if (result == NULL || n < 0)
220 fatal ("out of memory");
226 /* Input file handling. */
228 /* Table of all input files. */
229 const input_file **gt_files;
232 /* A number of places use the name of this "gengtype.c" file for a
233 location for things that we can't rely on the source to define.
234 Make sure we can still use pointer comparison on filenames. */
235 input_file* this_file;
236 /* The "system.h" file is likewise specially useful. */
237 input_file* system_h_file;
239 /* Vector of per-language directories. */
240 const char **lang_dir_names;
241 size_t num_lang_dirs;
243 /* An array of output files suitable for definitions. There is one
244 BASE_FILES entry for each language. */
245 static outf_p *base_files;
250 /* Utility debugging function, printing the various type counts within
251 a list of types. Called thru the DBGPRINT_COUNT_TYPE macro. */
253 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
255 int nb_types = 0, nb_scalar = 0, nb_string = 0;
256 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
257 int nb_lang_struct = 0, nb_param_struct = 0;
259 for (p = t; p; p = p->next)
282 case TYPE_LANG_STRUCT:
285 case TYPE_PARAM_STRUCT:
292 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
293 lbasename (fil), lin, msg, nb_types);
294 if (nb_scalar > 0 || nb_string > 0)
295 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
296 if (nb_struct > 0 || nb_union > 0)
297 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
298 if (nb_pointer > 0 || nb_array > 0)
299 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
300 if (nb_lang_struct > 0 || nb_param_struct > 0)
301 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
302 nb_lang_struct, nb_param_struct);
303 fprintf (stderr, "\n");
305 #endif /* ENABLE_CHECKING */
307 /* Scan the input file, LIST, and determine how much space we need to
308 store strings in. Also, count the number of language directories
309 and files. The numbers returned are overestimates as they does not
310 consider repeated files. */
312 measure_input_list (FILE *list)
318 num_gt_files = plugin_files ? nb_plugin_files : 0;
319 while ((c = getc (list)) != EOF)
328 /* Add space for a lang_bitmap before the input file name. */
329 n += sizeof (lang_bitmap);
343 /* Read one input line from LIST to HEREP (which is updated). A
344 pointer to the string is returned via LINEP. If it was a language
345 subdirectory in square brackets, strip off the square brackets and
346 return true. Otherwise, leave space before the string for a
347 lang_bitmap, and return false. At EOF, returns false, does not
348 touch *HEREP, and sets *LINEP to NULL. POS is used for
351 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
357 /* Read over whitespace. */
358 while (c == '\n' || c == ' ')
368 /* No space for a lang_bitmap is necessary. Discard the '['. */
371 while (c != ']' && c != '\n' && c != EOF)
380 c = getc (list); /* eat what should be a newline */
381 if (c != '\n' && c != EOF)
382 error_at_line (pos, "junk on line after language tag [%s]", line);
385 error_at_line (pos, "missing close bracket for language tag [%s",
394 /* Leave space for a lang_bitmap. */
395 memset (here, 0, sizeof (lang_bitmap));
396 here += sizeof (lang_bitmap);
403 while (c != EOF && c != '\n');
411 /* Read the list of input files from LIST and compute all of the
412 relevant tables. There is one file per line of the list. At
413 first, all the files on the list are language-generic, but
414 eventually a line will appear which is the name of a language
415 subdirectory in square brackets, like this: [cp]. All subsequent
416 files are specific to that language, until another language
417 subdirectory tag appears. Files can appear more than once, if
418 they apply to more than one language. */
420 read_input_list (const char *listname)
422 FILE *list = fopen (listname, "r");
424 fatal ("cannot open %s: %s", listname, xstrerror (errno));
428 size_t bufsz = measure_input_list (list);
429 char *buf = XNEWVEC (char, bufsz);
431 char *committed = buf;
432 char *limit = buf + bufsz;
437 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
439 epos.file = input_file_by_name (listname);
442 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
443 gt_files = XNEWVEC (const input_file *, num_gt_files);
450 is_language = read_input_line (list, &here, &line, &epos);
451 gcc_assert (here <= limit);
454 else if (is_language)
457 gcc_assert (langno <= num_lang_dirs);
458 for (i = 0; i < langno; i++)
459 if (strcmp (lang_dir_names[i], line) == 0)
461 error_at_line (&epos, "duplicate language tag [%s]",
468 curlangs = 1 << langno;
469 lang_dir_names[langno++] = line;
474 input_file *inpf = input_file_by_name (line);
475 gcc_assert (nfiles <= num_gt_files);
476 for (i = 0; i < nfiles; i++)
477 /* Since the input_file-s are uniquely hash-consed, we
478 can just compare pointers! */
479 if (gt_files[i] == inpf)
481 /* Throw away the string we just read, and add the
482 current language to the existing string's bitmap. */
483 lang_bitmap bmap = get_lang_bitmap (inpf);
485 error_at_line (&epos,
486 "file %s specified more than once "
487 "for language %s", line,
489 0 ? "(all)" : lang_dir_names[langno -
493 set_lang_bitmap (inpf, bmap);
498 set_lang_bitmap (inpf, curlangs);
499 gt_files[nfiles++] = inpf;
502 /* Update the global counts now that we know accurately how many
503 things there are. (We do not bother resizing the arrays down.) */
504 num_lang_dirs = langno;
505 /* Add the plugin files if provided. */
509 for (i = 0; i < nb_plugin_files; i++)
510 gt_files[nfiles++] = plugin_files[i];
512 num_gt_files = nfiles;
515 /* Sanity check: any file that resides in a language subdirectory
516 (e.g. 'cp') ought to belong to the corresponding language.
517 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
518 (Can you even do that? Should you be allowed to?) */
521 for (f = 0; f < num_gt_files; f++)
523 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
524 const char *basename = get_file_basename (gt_files[f]);
525 const char *slashpos = strchr (basename, '/');
530 for (l = 0; l < num_lang_dirs; l++)
531 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
532 && memcmp (basename, lang_dir_names[l],
533 strlen (lang_dir_names[l])) == 0)
535 if (!(bitmap & (1 << l)))
536 error ("%s is in language directory '%s' but is not "
537 "tagged for that language",
538 basename, lang_dir_names[l]);
546 fatal ("error reading %s: %s", listname, xstrerror (errno));
553 /* The one and only TYPE_STRING. */
555 static struct type string_type = {
556 TYPE_STRING, 0, 0, GC_USED, {0}
559 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
560 set to appropriate values at the beginning of main. */
562 static struct type scalar_nonchar = {
563 TYPE_SCALAR, 0, 0, GC_USED, {0}
566 static struct type scalar_char = {
567 TYPE_SCALAR, 0, 0, GC_USED, {0}
570 /* Lists of various things. */
572 static pair_p typedefs;
573 static type_p structures;
574 static type_p param_structs;
575 static pair_p variables;
577 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
578 static type_p adjust_field_tree_exp (type_p t, options_p opt);
579 static type_p adjust_field_rtx_def (type_p t, options_p opt);
581 /* Define S as a typedef to T at POS. */
584 do_typedef (const char *s, type_p t, struct fileloc *pos)
588 /* temporary kludge - gengtype doesn't handle conditionals or
589 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
590 is coming from this file (main() sets them up with safe dummy
592 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
595 for (p = typedefs; p != NULL; p = p->next)
596 if (strcmp (p->name, s) == 0)
600 error_at_line (pos, "type `%s' previously defined", s);
601 error_at_line (&p->line, "previously defined here");
606 p = XNEW (struct pair);
615 /* Define S as a typename of a scalar. Cannot be used to define
616 typedefs of 'char'. Note: is also used for pointer-to-function
617 typedefs (which are therefore not treated as pointers). */
620 do_scalar_typedef (const char *s, struct fileloc *pos)
622 do_typedef (s, &scalar_nonchar, pos);
625 /* Return the type previously defined for S. Use POS to report errors. */
628 resolve_typedef (const char *s, struct fileloc *pos)
631 for (p = typedefs; p != NULL; p = p->next)
632 if (strcmp (p->name, s) == 0)
634 error_at_line (pos, "unidentified type `%s'", s);
635 return &scalar_nonchar; /* treat as "int" */
638 /* Create and return a new structure with tag NAME (or a union iff
639 ISUNION is nonzero), at POS with fields FIELDS and options O. */
642 new_structure (const char *name, int isunion, struct fileloc *pos,
643 pair_p fields, options_p o)
647 lang_bitmap bitmap = get_lang_bitmap (pos->file);
649 for (si = structures; si != NULL; si = si->next)
650 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
653 if (si->kind == TYPE_LANG_STRUCT)
657 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
658 if (si->u.s.bitmap == bitmap)
661 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
664 si = XCNEW (struct type);
665 memcpy (si, ls, sizeof (struct type));
666 ls->kind = TYPE_LANG_STRUCT;
667 ls->u.s.lang_struct = si;
668 ls->u.s.fields = NULL;
670 si->pointer_to = NULL;
671 si->u.s.lang_struct = ls;
676 if (ls != NULL && s == NULL)
678 s = XCNEW (struct type);
679 s->next = ls->u.s.lang_struct;
680 ls->u.s.lang_struct = s;
681 s->u.s.lang_struct = ls;
688 s = XCNEW (struct type);
689 s->next = structures;
693 if (s->u.s.line.file != NULL
694 || (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap)))
696 error_at_line (pos, "duplicate definition of '%s %s'",
697 isunion ? "union" : "struct", s->u.s.tag);
698 error_at_line (&s->u.s.line, "previous definition here");
701 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
704 s->u.s.fields = fields;
706 s->u.s.bitmap = bitmap;
707 if (s->u.s.lang_struct)
708 s->u.s.lang_struct->u.s.bitmap |= bitmap;
713 /* Return the previously-defined structure with tag NAME (or a union
714 iff ISUNION is nonzero), or a new empty structure or union if none
715 was defined previously. */
718 find_structure (const char *name, int isunion)
722 for (s = structures; s != NULL; s = s->next)
723 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
726 s = XCNEW (struct type);
727 s->next = structures;
729 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
735 /* Return the previously-defined parameterized structure for structure
736 T and parameters PARAM, or a new parameterized empty structure or
737 union if none was defined previously. */
740 find_param_structure (type_p t, type_p param[NUM_PARAM])
744 for (res = param_structs; res; res = res->next)
745 if (res->u.param_struct.stru == t
746 && memcmp (res->u.param_struct.param, param,
747 sizeof (type_p) * NUM_PARAM) == 0)
751 res = XCNEW (struct type);
752 res->kind = TYPE_PARAM_STRUCT;
753 res->next = param_structs;
755 res->u.param_struct.stru = t;
756 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
761 /* Return a scalar type with name NAME. */
764 create_scalar_type (const char *name)
766 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
769 return &scalar_nonchar;
772 /* Return a pointer to T. */
775 create_pointer (type_p t)
779 type_p r = XCNEW (struct type);
780 r->kind = TYPE_POINTER;
784 return t->pointer_to;
787 /* Return an array of length LEN. */
790 create_array (type_p t, const char *len)
794 v = XCNEW (struct type);
795 v->kind = TYPE_ARRAY;
801 /* Return an options structure with name NAME and info INFO. NEXT is the
802 next option in the chain. */
805 create_option (options_p next, const char *name, const void *info)
807 options_p o = XNEW (struct options);
810 o->info = (const char *) info;
814 /* Return an options structure for a "nested_ptr" option. */
816 create_nested_ptr_option (options_p next, type_p t,
817 const char *to, const char *from)
819 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
821 d->type = adjust_field_type (t, 0);
823 d->convert_from = from;
824 return create_option (next, "nested_ptr", d);
827 /* Add a variable named S of type T with options O defined at POS,
831 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
834 n = XNEW (struct pair);
843 /* Most-general structure field creator. */
845 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
846 const input_file *inpf, int line)
850 field = XNEW (struct pair);
855 field->line.file = inpf;
856 field->line.line = line;
860 /* Create a field that came from the source code we are scanning,
861 i.e. we have a 'struct fileloc', and possibly options; also,
862 adjust_field_type should be called. */
864 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
867 return create_field_all (next, adjust_field_type (type, opt),
868 name, opt, pos->file, pos->line);
871 /* Create a fake field with the given type and name. NEXT is the next
872 field in the chain. */
873 #define create_field(next,type,name) \
874 create_field_all(next,type,name, 0, this_file, __LINE__)
876 /* Like create_field, but the field is only valid when condition COND
880 create_optional_field_ (pair_p next, type_p type, const char *name,
881 const char *cond, int line)
887 /* Create a fake union type with a single nameless field of type TYPE.
888 The field has a tag of "1". This allows us to make the presence
889 of a field of type TYPE depend on some boolean "desc" being true. */
890 union_fields = create_field (NULL, type, "");
891 union_fields->opt = create_option (union_fields->opt, "dot", "");
892 union_fields->opt = create_option (union_fields->opt, "tag", "1");
893 union_type = new_structure (xasprintf ("%s_%d", "fake_union", id++), 1,
894 &lexer_line, union_fields, NULL);
896 /* Create the field and give it the new fake union type. Add a "desc"
897 tag that specifies the condition under which the field is valid. */
898 return create_field_all (next, union_type, name,
899 create_option (0, "desc", cond), this_file, line);
902 #define create_optional_field(next,type,name,cond) \
903 create_optional_field_(next,type,name,cond,__LINE__)
905 /* Reverse a linked list of 'struct pair's in place. */
907 nreverse_pairs (pair_p list)
909 pair_p prev = 0, p, next;
910 for (p = list; p; p = next)
920 /* We don't care how long a CONST_DOUBLE is. */
921 #define CONST_DOUBLE_FORMAT "ww"
922 /* We don't want to see codes that are only for generator files. */
923 #undef GENERATOR_FILE
927 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
933 static const char *const rtx_name[NUM_RTX_CODE] = {
934 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
939 static const char *const rtx_format[NUM_RTX_CODE] = {
940 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
945 static int rtx_next_new[NUM_RTX_CODE];
947 /* We also need codes and names for insn notes (not register notes).
948 Note that we do *not* bias the note values here. */
951 #define DEF_INSN_NOTE(NAME) NAME,
952 #include "insn-notes.def"
958 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
959 default field for line number notes. */
960 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
961 #define DEF_INSN_NOTE(NAME) #NAME,
962 #include "insn-notes.def"
966 #undef CONST_DOUBLE_FORMAT
967 #define GENERATOR_FILE
969 /* Generate the contents of the rtx_next array. This really doesn't belong
970 in gengtype at all, but it's needed for adjust_field_rtx_def. */
976 for (i = 0; i < NUM_RTX_CODE; i++)
980 rtx_next_new[i] = -1;
981 if (strncmp (rtx_format[i], "iuu", 3) == 0)
983 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
986 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
987 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
992 /* Write out the contents of the rtx_next array. */
994 write_rtx_next (void)
996 outf_p f = get_output_file_with_visibility (NULL);
1001 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1002 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1003 for (i = 0; i < NUM_RTX_CODE; i++)
1004 if (rtx_next_new[i] == -1)
1005 oprintf (f, " 0,\n");
1008 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1009 oprintf (f, "};\n");
1012 /* Handle `special("rtx_def")'. This is a special case for field
1013 `fld' of struct rtx_def, which is an array of unions whose values
1014 are based in a complex way on the type of RTL. */
1017 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1022 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1023 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1025 if (t->kind != TYPE_UNION)
1027 error_at_line (&lexer_line,
1028 "special `rtx_def' must be applied to a union");
1029 return &string_type;
1032 nodot = create_option (NULL, "dot", "");
1034 rtx_tp = create_pointer (find_structure ("rtx_def", 0));
1035 rtvec_tp = create_pointer (find_structure ("rtvec_def", 0));
1036 tree_tp = create_pointer (find_structure ("tree_node", 1));
1037 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", 0));
1038 reg_attrs_tp = create_pointer (find_structure ("reg_attrs", 0));
1039 basic_block_tp = create_pointer (find_structure ("basic_block_def", 0));
1041 create_pointer (find_structure ("constant_descriptor_rtx", 0));
1042 scalar_tp = &scalar_nonchar; /* rtunion int */
1045 pair_p note_flds = NULL;
1048 for (c = 0; c <= NOTE_INSN_MAX; c++)
1053 case NOTE_INSN_DELETED_LABEL:
1054 note_flds = create_field (note_flds, &string_type, "rt_str");
1057 case NOTE_INSN_BLOCK_BEG:
1058 case NOTE_INSN_BLOCK_END:
1059 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1062 case NOTE_INSN_VAR_LOCATION:
1063 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1067 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1070 /* NOTE_INSN_MAX is used as the default field for line
1072 if (c == NOTE_INSN_MAX)
1073 note_flds->opt = create_option (nodot, "default", "");
1075 note_flds->opt = create_option (nodot, "tag", note_insn_name[c]);
1077 note_union_tp = new_structure ("rtx_def_note_subunion", 1,
1078 &lexer_line, note_flds, NULL);
1080 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1084 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1085 sym_flds->opt = create_option (nodot, "default", "");
1087 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1088 sym_flds->opt = create_option (nodot, "tag", "1");
1090 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", 1,
1091 &lexer_line, sym_flds, NULL);
1093 for (i = 0; i < NUM_RTX_CODE; i++)
1095 pair_p subfields = NULL;
1096 size_t aindex, nmindex;
1101 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1104 const char *subname;
1106 switch (rtx_format[i][aindex])
1117 if (i == MEM && aindex == 1)
1118 t = mem_attrs_tp, subname = "rt_mem";
1119 else if (i == JUMP_INSN && aindex == 8)
1120 t = rtx_tp, subname = "rt_rtx";
1121 else if (i == CODE_LABEL && aindex == 5)
1122 t = scalar_tp, subname = "rt_int";
1123 else if (i == CODE_LABEL && aindex == 4)
1124 t = rtx_tp, subname = "rt_rtx";
1125 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1126 t = rtx_tp, subname = "rt_rtx";
1127 else if (i == NOTE && aindex == 4)
1128 t = note_union_tp, subname = "";
1129 else if (i == NOTE && aindex == 5)
1130 t = scalar_tp, subname = "rt_int";
1131 else if (i == NOTE && aindex >= 7)
1132 t = scalar_tp, subname = "rt_int";
1133 else if (i == ADDR_DIFF_VEC && aindex == 4)
1134 t = scalar_tp, subname = "rt_int";
1135 else if (i == VALUE && aindex == 0)
1136 t = scalar_tp, subname = "rt_int";
1137 else if (i == DEBUG_EXPR && aindex == 0)
1138 t = tree_tp, subname = "rt_tree";
1139 else if (i == REG && aindex == 1)
1140 t = scalar_tp, subname = "rt_int";
1141 else if (i == REG && aindex == 2)
1142 t = reg_attrs_tp, subname = "rt_reg";
1143 else if (i == SCRATCH && aindex == 0)
1144 t = scalar_tp, subname = "rt_int";
1145 else if (i == SYMBOL_REF && aindex == 1)
1146 t = scalar_tp, subname = "rt_int";
1147 else if (i == SYMBOL_REF && aindex == 2)
1148 t = symbol_union_tp, subname = "";
1149 else if (i == BARRIER && aindex >= 3)
1150 t = scalar_tp, subname = "rt_int";
1153 error_at_line (&lexer_line,
1154 "rtx type `%s' has `0' in position %lu, can't handle",
1155 rtx_name[i], (unsigned long) aindex);
1177 subname = "rt_rtvec";
1182 subname = "rt_tree";
1191 error_at_line (&lexer_line,
1192 "rtx type `%s' has `%c' in position %lu, can't handle",
1193 rtx_name[i], rtx_format[i][aindex],
1194 (unsigned long) aindex);
1200 subfields = create_field (subfields, t,
1201 xasprintf (".fld[%lu].%s",
1202 (unsigned long) aindex,
1204 subfields->opt = nodot;
1205 if (t == note_union_tp)
1206 subfields->opt = create_option (subfields->opt, "desc",
1208 if (t == symbol_union_tp)
1209 subfields->opt = create_option (subfields->opt, "desc",
1210 "CONSTANT_POOL_ADDRESS_P (&%0)");
1213 if (i == SYMBOL_REF)
1215 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P holds. */
1216 type_p field_tp = find_structure ("block_symbol", 0);
1218 = create_optional_field (subfields, field_tp, "block_sym",
1219 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1222 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1223 substruct = new_structure (sname, 0, &lexer_line, subfields, NULL);
1225 ftag = xstrdup (rtx_name[i]);
1226 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1227 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1229 flds = create_field (flds, substruct, "");
1230 flds->opt = create_option (nodot, "tag", ftag);
1233 return new_structure ("rtx_def_subunion", 1, &lexer_line, flds, nodot);
1236 /* Handle `special("tree_exp")'. This is a special case for
1237 field `operands' of struct tree_exp, which although it claims to contain
1238 pointers to trees, actually sometimes contains pointers to RTL too.
1239 Passed T, the old type of the field, and OPT its options. Returns
1240 a new type for the field. */
1243 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1248 if (t->kind != TYPE_ARRAY)
1250 error_at_line (&lexer_line,
1251 "special `tree_exp' must be applied to an array");
1252 return &string_type;
1255 nodot = create_option (NULL, "dot", "");
1257 flds = create_field (NULL, t, "");
1258 flds->opt = create_option (nodot, "length",
1259 "TREE_OPERAND_LENGTH ((tree) &%0)");
1260 flds->opt = create_option (flds->opt, "default", "");
1262 return new_structure ("tree_exp_subunion", 1, &lexer_line, flds, nodot);
1265 /* Perform any special processing on a type T, about to become the type
1266 of a field. Return the appropriate type for the field.
1268 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1269 - Similarly for arrays of pointer-to-char;
1270 - Converts structures for which a parameter is provided to
1272 - Handles "special" options.
1276 adjust_field_type (type_p t, options_p opt)
1279 const int pointer_p = t->kind == TYPE_POINTER;
1280 type_p params[NUM_PARAM];
1284 for (i = 0; i < NUM_PARAM; i++)
1287 for (; opt; opt = opt->next)
1288 if (strcmp (opt->name, "length") == 0)
1290 else if (strcmp (opt->name, "param_is") == 0
1291 || (strncmp (opt->name, "param", 5) == 0
1292 && ISDIGIT (opt->name[5])
1293 && strcmp (opt->name + 6, "_is") == 0))
1295 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1297 if (!UNION_OR_STRUCT_P (t)
1298 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
1300 error_at_line (&lexer_line,
1301 "option `%s' may only be applied to structures or structure pointers",
1307 if (params[num] != NULL)
1308 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1309 if (!ISDIGIT (opt->name[5]))
1311 create_pointer (CONST_CAST2 (type_p, const char *, opt->info));
1313 params[num] = CONST_CAST2 (type_p, const char *, opt->info);
1315 else if (strcmp (opt->name, "special") == 0)
1317 const char *special_name = opt->info;
1318 if (strcmp (special_name, "tree_exp") == 0)
1319 t = adjust_field_tree_exp (t, opt);
1320 else if (strcmp (special_name, "rtx_def") == 0)
1321 t = adjust_field_rtx_def (t, opt);
1323 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1332 realt = find_param_structure (t, params);
1333 t = pointer_p ? create_pointer (realt) : realt;
1337 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1338 return &string_type;
1339 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1340 && t->u.a.p->u.p->kind == TYPE_SCALAR
1341 && t->u.a.p->u.p->u.scalar_is_char)
1342 return create_array (&string_type, t->u.a.len);
1348 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *);
1349 static void set_gc_used (pair_p);
1351 /* Handle OPT for set_gc_used_type. */
1354 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1355 int *pass_param, int *length, int *skip,
1359 for (o = opt; o; o = o->next)
1360 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO)
1361 set_gc_used_type (CONST_CAST2 (type_p, const char *, o->info),
1362 GC_POINTED_TO, NULL);
1363 else if (strcmp (o->name, "maybe_undef") == 0)
1365 else if (strcmp (o->name, "use_params") == 0)
1367 else if (strcmp (o->name, "length") == 0)
1369 else if (strcmp (o->name, "skip") == 0)
1371 else if (strcmp (o->name, "nested_ptr") == 0)
1372 *nested_ptr = ((const struct nested_ptr_data *) o->info)->type;
1375 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1378 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM])
1380 if (t->gc_used >= level)
1394 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1397 for (f = t->u.s.fields; f; f = f->next)
1399 int maybe_undef = 0;
1403 type_p nested_ptr = NULL;
1404 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1405 &length, &skip, &nested_ptr);
1407 if (nested_ptr && f->type->kind == TYPE_POINTER)
1408 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1409 pass_param ? param : NULL);
1410 else if (length && f->type->kind == TYPE_POINTER)
1411 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1412 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1413 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1414 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1415 set_gc_used_type (find_param_structure (f->type->u.p, param),
1416 GC_POINTED_TO, NULL);
1418 ; /* target type is not used through this field */
1420 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL);
1426 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1430 set_gc_used_type (t->u.a.p, GC_USED, param);
1433 case TYPE_LANG_STRUCT:
1434 for (t = t->u.s.lang_struct; t; t = t->next)
1435 set_gc_used_type (t, level, param);
1438 case TYPE_PARAM_STRUCT:
1441 for (i = 0; i < NUM_PARAM; i++)
1442 if (t->u.param_struct.param[i] != 0)
1443 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1445 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1446 level = GC_POINTED_TO;
1449 t->u.param_struct.stru->gc_used = GC_UNUSED;
1450 set_gc_used_type (t->u.param_struct.stru, level,
1451 t->u.param_struct.param);
1459 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1462 set_gc_used (pair_p variables)
1466 for (p = variables; p; p = p->next)
1468 set_gc_used_type (p->type, GC_USED, NULL);
1471 if (verbosity_level >= 2)
1472 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1475 /* File mapping routines. For each input file, there is one output .c file
1476 (but some output files have many input files), and there is one .h file
1477 for the whole build. */
1479 /* Output file handling. */
1481 /* Create and return an outf_p for a new file for NAME, to be called
1485 create_file (const char *name, const char *oname)
1487 static const char *const hdr[] = {
1488 " Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc.\n",
1490 "This file is part of GCC.\n",
1492 "GCC is free software; you can redistribute it and/or modify it under\n",
1493 "the terms of the GNU General Public License as published by the Free\n",
1494 "Software Foundation; either version 3, or (at your option) any later\n",
1497 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1498 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1499 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1500 "for more details.\n",
1502 "You should have received a copy of the GNU General Public License\n",
1503 "along with GCC; see the file COPYING3. If not see\n",
1504 "<http://www.gnu.org/licenses/>. */\n",
1506 "/* This file is machine generated. Do not edit. */\n"
1511 gcc_assert (name != NULL);
1512 gcc_assert (oname != NULL);
1513 f = XCNEW (struct outf);
1514 f->next = output_files;
1518 oprintf (f, "/* Type information for %s.\n", name);
1519 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1520 oprintf (f, "%s", hdr[i]);
1524 /* Print, like fprintf, to O.
1525 N.B. You might think this could be implemented more efficiently
1526 with vsnprintf(). Unfortunately, there are C libraries that
1527 provide that function but without the C99 semantics for its return
1528 value, making it impossible to know how much space is required. */
1530 oprintf (outf_p o, const char *format, ...)
1536 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1541 va_start (ap, format);
1542 slength = vasprintf (&s, format, ap);
1543 if (s == NULL || (int) slength < 0)
1544 fatal ("out of memory");
1547 if (o->bufused + slength > o->buflength)
1549 size_t new_len = o->buflength;
1556 while (o->bufused + slength >= new_len);
1557 o->buf = XRESIZEVEC (char, o->buf, new_len);
1558 o->buflength = new_len;
1560 memcpy (o->buf + o->bufused, s, slength);
1561 o->bufused += slength;
1565 /* Open the global header file and the language-specific header files. */
1568 open_base_files (void)
1572 if (nb_plugin_files > 0 && plugin_files)
1575 header_file = create_file ("GCC", "gtype-desc.h");
1577 base_files = XNEWVEC (outf_p, num_lang_dirs);
1579 for (i = 0; i < num_lang_dirs; i++)
1580 base_files[i] = create_file (lang_dir_names[i],
1581 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1583 /* gtype-desc.c is a little special, so we create it here. */
1585 /* The order of files here matters very much. */
1586 static const char *const ifiles[] = {
1587 "config.h", "system.h", "coretypes.h", "tm.h",
1588 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1589 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1590 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1591 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1592 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1593 "cfglayout.h", "except.h", "output.h", "gimple.h", "cfgloop.h",
1594 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h", NULL
1596 const char *const *ifp;
1597 outf_p gtype_desc_c;
1599 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1600 for (ifp = ifiles; *ifp; ifp++)
1601 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1603 /* Make sure we handle "cfun" specially. */
1604 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1605 oprintf (gtype_desc_c, "#undef cfun\n");
1609 /* For INPF an input file, return the real basename of INPF, with all
1610 the directory components skipped. */
1613 get_file_realbasename (const input_file *inpf)
1615 const char *f = get_input_file_name (inpf);
1616 const char *lastslash = strrchr (f, '/');
1618 return (lastslash != NULL) ? lastslash + 1 : f;
1621 /* For INPF a filename, return the relative path to INPF from
1622 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1625 get_file_srcdir_relative_path (const input_file *inpf)
1627 const char *f = get_input_file_name (inpf);
1628 if (strlen (f) > srcdir_len
1629 && IS_DIR_SEPARATOR (f[srcdir_len])
1630 && strncmp (f, srcdir, srcdir_len) == 0)
1631 return f + srcdir_len + 1;
1636 /* For INPF an input_file, return the relative path to INPF from
1637 $(srcdir) if the latter is a prefix in INPF, or the real basename
1638 of INPF otherwise. */
1641 get_file_basename (const input_file *inpf)
1643 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1645 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1648 /* For F a filename, return the lang_dir_names relative index of the language
1649 directory that is a prefix in F, if any, -1 otherwise. */
1652 get_prefix_langdir_index (const char *f)
1654 size_t f_len = strlen (f);
1657 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1659 const char *langdir = lang_dir_names[lang_index];
1660 size_t langdir_len = strlen (langdir);
1662 if (f_len > langdir_len
1663 && IS_DIR_SEPARATOR (f[langdir_len])
1664 && memcmp (f, langdir, langdir_len) == 0)
1671 /* For INPF an input file, return the name of language directory where
1672 F is located, if any, NULL otherwise. */
1675 get_file_langdir (const input_file *inpf)
1677 /* Get the relative path to INPF from $(srcdir) and find the
1678 language by comparing the prefix with language directory names.
1679 If INPF is not even srcdir relative, no point in looking
1683 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1686 if (!srcdir_relative_path)
1689 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1690 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1692 else if (lang_index >= 0)
1693 r = lang_dir_names[lang_index];
1700 /* The gt- output file name for INPF. */
1703 get_file_gtfilename (const input_file *inpf)
1705 /* Cook up an initial version of the gt- file name from the file real
1706 basename and the language name, if any. */
1708 const char *basename = get_file_realbasename (inpf);
1709 const char *langdir = get_file_langdir (inpf);
1712 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1713 : xasprintf ("gt-%s", basename));
1715 /* Then replace all non alphanumerics characters by '-' and change the
1716 extension to ".h". We expect the input filename extension was at least
1717 one character long. */
1721 for (; *s != '.'; s++)
1722 if (!ISALNUM (*s) && *s != '-')
1725 memcpy (s, ".h", sizeof (".h"));
1730 /* An output file, suitable for definitions, that can see declarations
1731 made in INPF and is linked into every language that uses INPF.
1732 Since the the result is cached inside INPF, that argument cannot be
1733 declared constant, but is "almost" constant. */
1736 get_output_file_with_visibility (input_file *inpf)
1740 const char *basename;
1741 const char *for_name;
1742 const char *output_name;
1744 /* This can happen when we need a file with visibility on a
1745 structure that we've never seen. We have to just hope that it's
1746 globally visible. */
1748 inpf = system_h_file;
1750 /* In plugin mode, return NULL unless the input_file is one of the
1755 for (i = 0; i < nb_plugin_files; i++)
1756 if (inpf == plugin_files[i])
1757 return plugin_output;
1762 /* Determine the output file name. */
1763 basename = get_file_basename (inpf);
1765 len = strlen (basename);
1766 if ((len > 2 && memcmp (basename + len - 2, ".c", 2) == 0)
1767 || (len > 2 && memcmp (basename + len - 2, ".y", 2) == 0)
1768 || (len > 3 && memcmp (basename + len - 3, ".in", 3) == 0))
1770 output_name = get_file_gtfilename (inpf);
1771 for_name = basename;
1773 /* Some headers get used by more than one front-end; hence, it
1774 would be inappropriate to spew them out to a single gtype-<lang>.h
1775 (and gengtype doesn't know how to direct spewage into multiple
1776 gtype-<lang>.h headers at this time). Instead, we pair up these
1777 headers with source files (and their special purpose gt-*.h headers). */
1778 else if (strncmp (basename, "c-family", 8) == 0
1779 && IS_DIR_SEPARATOR (basename[8])
1780 && strcmp (basename + 9, "c-common.h") == 0)
1781 output_name = "gt-c-family-c-common.h", for_name = "c-family/c-common.c";
1782 else if (strcmp (basename, "c-lang.h") == 0)
1783 output_name = "gt-c-decl.h", for_name = "c-decl.c";
1784 else if (strcmp (basename, "c-tree.h") == 0)
1785 output_name = "gt-c-decl.h", for_name = "c-decl.c";
1786 else if (strncmp (basename, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename[2])
1787 && strcmp (basename + 3, "cp-tree.h") == 0)
1788 output_name = "gt-cp-tree.h", for_name = "cp/tree.c";
1789 else if (strncmp (basename, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename[2])
1790 && strcmp (basename + 3, "decl.h") == 0)
1791 output_name = "gt-cp-decl.h", for_name = "cp/decl.c";
1792 else if (strncmp (basename, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename[2])
1793 && strcmp (basename + 3, "name-lookup.h") == 0)
1794 output_name = "gt-cp-name-lookup.h", for_name = "cp/name-lookup.c";
1795 else if (strncmp (basename, "objc", 4) == 0
1796 && IS_DIR_SEPARATOR (basename[4])
1797 && strcmp (basename + 5, "objc-act.h") == 0)
1798 output_name = "gt-objc-objc-act.h", for_name = "objc/objc-act.c";
1801 int lang_index = get_prefix_langdir_index (basename);
1803 if (lang_index >= 0)
1804 return base_files[lang_index];
1806 output_name = "gtype-desc.c";
1810 /* Look through to see if we've ever seen this output filename before. */
1811 for (r = output_files; r; r = r->next)
1812 if (strcmp (r->name, output_name) == 0)
1815 /* If not, create it. */
1816 r = create_file (for_name, output_name);
1818 gcc_assert (r && r->name);
1822 /* The name of an output file, suitable for definitions, that can see
1823 declarations made in INPF and is linked into every language that
1827 get_output_file_name (input_file* inpf)
1829 outf_p o = get_output_file_with_visibility (inpf);
1835 /* Check if existing file is equal to the in memory buffer. */
1838 is_file_equal (outf_p of)
1840 FILE *newfile = fopen (of->name, "r");
1843 if (newfile == NULL)
1847 for (i = 0; i < of->bufused; i++)
1850 ch = fgetc (newfile);
1851 if (ch == EOF || ch != (unsigned char) of->buf[i])
1861 /* Copy the output to its final destination,
1862 but don't unnecessarily change modification times. */
1865 close_output_files (void)
1867 int nbwrittenfiles = 0;
1870 for (of = output_files; of; of = of->next)
1873 if (!is_file_equal (of))
1875 FILE *newfile = NULL;
1876 char *backupname = NULL;
1877 /* Back up the old version of the output file gt-FOO.c as
1878 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
1881 backupname = concat (backup_dir, "/",
1882 lbasename (of->name), "~", NULL);
1883 if (!access (of->name, F_OK) && rename (of->name, backupname))
1884 fatal ("failed to back up %s as %s: %s",
1885 of->name, backupname, xstrerror (errno));
1888 newfile = fopen (of->name, "w");
1889 if (newfile == NULL)
1890 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
1891 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
1892 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
1893 if (fclose (newfile) != 0)
1894 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
1896 if (verbosity_level >= 2 && backupname)
1897 printf ("%s wrote #%-3d %s backed-up in %s\n",
1898 progname, nbwrittenfiles, of->name, backupname);
1899 else if (verbosity_level >= 1)
1900 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
1905 /* output file remains unchanged. */
1906 if (verbosity_level >= 2)
1907 printf ("%s keep %s\n", progname, of->name);
1911 of->bufused = of->buflength = 0;
1913 if (verbosity_level >= 1)
1914 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
1921 const input_file* file;
1925 struct walk_type_data;
1927 /* For scalars and strings, given the item in 'val'.
1928 For structures, given a pointer to the item in 'val'.
1929 For misc. pointers, given the item in 'val'.
1931 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
1932 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
1934 /* Parameters for write_types. */
1936 struct write_types_data
1939 const char *param_prefix;
1940 const char *subfield_marker_routine;
1941 const char *marker_routine;
1942 const char *reorder_note_routine;
1943 const char *comment;
1944 int skip_hooks; /* skip hook generation if non zero */
1947 static void output_escaped_param (struct walk_type_data *d,
1948 const char *, const char *);
1949 static void output_mangled_typename (outf_p, const_type_p);
1950 static void walk_type (type_p t, struct walk_type_data *d);
1951 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
1952 const struct write_types_data *wtd);
1953 static void write_types_process_field
1954 (type_p f, const struct walk_type_data *d);
1955 static void write_types (outf_p output_header,
1957 type_p param_structs,
1958 const struct write_types_data *wtd);
1959 static void write_types_local_process_field
1960 (type_p f, const struct walk_type_data *d);
1961 static void write_local_func_for_structure
1962 (const_type_p orig_s, type_p s, type_p *param);
1963 static void write_local (outf_p output_header,
1964 type_p structures, type_p param_structs);
1965 static void write_enum_defn (type_p structures, type_p param_structs);
1966 static int contains_scalar_p (type_p t);
1967 static void put_mangled_filename (outf_p, const input_file *);
1968 static void finish_root_table (struct flist *flp, const char *pfx,
1969 const char *tname, const char *lastname,
1971 static void write_root (outf_p, pair_p, type_p, const char *, int,
1972 struct fileloc *, const char *, bool);
1973 static void write_array (outf_p f, pair_p v,
1974 const struct write_types_data *wtd);
1975 static void write_roots (pair_p, bool);
1977 /* Parameters for walk_type. */
1979 struct walk_type_data
1981 process_field_fn process_field;
1986 const char *prev_val[4];
1989 const struct fileloc *line;
1994 const char *reorder_fn;
1996 bool fn_wants_lvalue;
1999 /* Print a mangled name representing T to OF. */
2002 output_mangled_typename (outf_p of, const_type_p t)
2011 output_mangled_typename (of, t->u.p);
2021 case TYPE_LANG_STRUCT:
2022 oprintf (of, "%lu%s", (unsigned long) strlen (t->u.s.tag),
2025 case TYPE_PARAM_STRUCT:
2028 for (i = 0; i < NUM_PARAM; i++)
2029 if (t->u.param_struct.param[i] != NULL)
2030 output_mangled_typename (of, t->u.param_struct.param[i]);
2031 output_mangled_typename (of, t->u.param_struct.stru);
2039 /* Print PARAM to D->OF processing escapes. D->VAL references the
2040 current object, D->PREV_VAL the object containing the current
2041 object, ONAME is the name of the option and D->LINE is used to
2042 print error messages. */
2045 output_escaped_param (struct walk_type_data *d, const char *param,
2050 for (p = param; *p; p++)
2052 oprintf (d->of, "%c", *p);
2057 oprintf (d->of, "(%s)", d->prev_val[2]);
2060 oprintf (d->of, "(%s)", d->prev_val[0]);
2063 oprintf (d->of, "(%s)", d->prev_val[1]);
2067 const char *pp = d->val + strlen (d->val);
2068 while (pp[-1] == ']')
2071 oprintf (d->of, "%s", pp);
2075 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2080 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2081 which is of type T. Write code to D->OF to constrain execution (at
2082 the point that D->PROCESS_FIELD is called) to the appropriate
2083 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2084 pointers to those objects. D->PREV_VAL lists the objects
2085 containing the current object, D->OPT is a list of options to
2086 apply, D->INDENT is the current indentation level, D->LINE is used
2087 to print error messages, D->BITMAP indicates which languages to
2088 print the structure for, and D->PARAM is the current parameter
2089 (from an enclosing param_is option). */
2092 walk_type (type_p t, struct walk_type_data *d)
2094 const char *length = NULL;
2095 const char *desc = NULL;
2096 int maybe_undef_p = 0;
2097 int use_param_num = -1;
2098 int use_params_p = 0;
2100 const struct nested_ptr_data *nested_ptr_d = NULL;
2102 d->needs_cast_p = false;
2103 for (oo = d->opt; oo; oo = oo->next)
2104 if (strcmp (oo->name, "length") == 0)
2106 else if (strcmp (oo->name, "maybe_undef") == 0)
2108 else if (strncmp (oo->name, "use_param", 9) == 0
2109 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2110 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2111 else if (strcmp (oo->name, "use_params") == 0)
2113 else if (strcmp (oo->name, "desc") == 0)
2115 else if (strcmp (oo->name, "mark_hook") == 0)
2117 else if (strcmp (oo->name, "nested_ptr") == 0)
2118 nested_ptr_d = (const struct nested_ptr_data *) oo->info;
2119 else if (strcmp (oo->name, "dot") == 0)
2121 else if (strcmp (oo->name, "tag") == 0)
2123 else if (strcmp (oo->name, "special") == 0)
2125 else if (strcmp (oo->name, "skip") == 0)
2127 else if (strcmp (oo->name, "default") == 0)
2129 else if (strcmp (oo->name, "param_is") == 0)
2131 else if (strncmp (oo->name, "param", 5) == 0
2132 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2134 else if (strcmp (oo->name, "chain_next") == 0)
2136 else if (strcmp (oo->name, "chain_prev") == 0)
2138 else if (strcmp (oo->name, "chain_circular") == 0)
2140 else if (strcmp (oo->name, "reorder") == 0)
2142 else if (strcmp (oo->name, "variable_size") == 0)
2145 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2152 int pointer_p = t->kind == TYPE_POINTER;
2156 if (!UNION_OR_STRUCT_P (t))
2157 error_at_line (d->line, "`use_params' option on unimplemented type");
2159 t = find_param_structure (t, d->param);
2161 t = create_pointer (t);
2164 if (use_param_num != -1)
2166 if (d->param != NULL && d->param[use_param_num] != NULL)
2168 type_p nt = d->param[use_param_num];
2170 if (t->kind == TYPE_ARRAY)
2171 nt = create_array (nt, t->u.a.len);
2172 else if (length != NULL && t->kind == TYPE_POINTER)
2173 nt = create_pointer (nt);
2174 d->needs_cast_p = (t->kind != TYPE_POINTER
2175 && (nt->kind == TYPE_POINTER
2176 || nt->kind == TYPE_STRING));
2180 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2184 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
2186 error_at_line (d->line,
2187 "field `%s' has invalid option `maybe_undef_p'\n",
2196 d->process_field (t, d);
2201 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2203 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2209 if (!UNION_OR_STRUCT_P (t->u.p)
2210 && t->u.p->kind != TYPE_PARAM_STRUCT)
2212 error_at_line (d->line,
2213 "field `%s' is pointer to unimplemented type",
2220 const char *oldprevval2 = d->prev_val[2];
2222 if (!UNION_OR_STRUCT_P (nested_ptr_d->type))
2224 error_at_line (d->line,
2225 "field `%s' has invalid "
2226 "option `nested_ptr'\n", d->val);
2230 d->prev_val[2] = d->val;
2231 oprintf (d->of, "%*s{\n", d->indent, "");
2233 d->val = xasprintf ("x%d", d->counter++);
2234 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2235 (nested_ptr_d->type->kind == TYPE_UNION
2236 ? "union" : "struct"),
2237 nested_ptr_d->type->u.s.tag,
2238 d->fn_wants_lvalue ? "" : "const ", d->val);
2239 oprintf (d->of, "%*s", d->indent + 2, "");
2240 output_escaped_param (d, nested_ptr_d->convert_from,
2242 oprintf (d->of, ";\n");
2244 d->process_field (nested_ptr_d->type, d);
2246 if (d->fn_wants_lvalue)
2248 oprintf (d->of, "%*s%s = ", d->indent, "",
2250 d->prev_val[2] = d->val;
2251 output_escaped_param (d, nested_ptr_d->convert_to,
2253 oprintf (d->of, ";\n");
2257 oprintf (d->of, "%*s}\n", d->indent, "");
2258 d->val = d->prev_val[2];
2259 d->prev_val[2] = oldprevval2;
2262 d->process_field (t->u.p, d);
2266 int loopcounter = d->counter++;
2267 const char *oldval = d->val;
2268 const char *oldprevval3 = d->prev_val[3];
2271 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2273 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2274 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2275 "", loopcounter, loopcounter);
2276 output_escaped_param (d, length, "length");
2277 oprintf (d->of, "); i%d++) {\n", loopcounter);
2279 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2281 d->prev_val[3] = oldval;
2282 walk_type (t->u.p, d);
2285 d->prev_val[3] = oldprevval3;
2288 oprintf (d->of, "%*s}\n", d->indent, "");
2289 d->process_field (t, d);
2291 oprintf (d->of, "%*s}\n", d->indent, "");
2298 int loopcounter = d->counter++;
2299 const char *oldval = d->val;
2302 /* If it's an array of scalars, we optimize by not generating
2304 if (t->u.a.p->kind == TYPE_SCALAR)
2307 /* When walking an array, compute the length and store it in a
2308 local variable before walking the array elements, instead of
2309 recomputing the length expression each time through the loop.
2310 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2311 where the length is stored in the first array element,
2312 because otherwise that operand can get overwritten on the
2314 oprintf (d->of, "%*s{\n", d->indent, "");
2316 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2317 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2318 d->indent, "", loopcounter);
2320 output_escaped_param (d, length, "length");
2322 oprintf (d->of, "%s", t->u.a.len);
2323 oprintf (d->of, ");\n");
2325 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2327 loopcounter, loopcounter, loopcounter, loopcounter);
2329 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2331 walk_type (t->u.a.p, d);
2336 oprintf (d->of, "%*s}\n", d->indent, "");
2338 oprintf (d->of, "%*s}\n", d->indent, "");
2346 const char *oldval = d->val;
2347 const char *oldprevval1 = d->prev_val[1];
2348 const char *oldprevval2 = d->prev_val[2];
2349 const int union_p = t->kind == TYPE_UNION;
2350 int seen_default_p = 0;
2353 if (!t->u.s.line.file)
2354 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2356 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2358 error_at_line (d->line,
2359 "structure `%s' defined for mismatching languages",
2361 error_at_line (&t->u.s.line, "one structure defined here");
2364 /* Some things may also be defined in the structure's options. */
2365 for (o = t->u.s.opt; o; o = o->next)
2366 if (!desc && strcmp (o->name, "desc") == 0)
2369 d->prev_val[2] = oldval;
2370 d->prev_val[1] = oldprevval2;
2375 error_at_line (d->line,
2376 "missing `desc' option for union `%s'",
2380 oprintf (d->of, "%*sswitch (", d->indent, "");
2381 output_escaped_param (d, desc, "desc");
2382 oprintf (d->of, ")\n");
2384 oprintf (d->of, "%*s{\n", d->indent, "");
2386 for (f = t->u.s.fields; f; f = f->next)
2389 const char *dot = ".";
2390 const char *tagid = NULL;
2393 int use_param_p = 0;
2396 d->reorder_fn = NULL;
2397 for (oo = f->opt; oo; oo = oo->next)
2398 if (strcmp (oo->name, "dot") == 0)
2400 else if (strcmp (oo->name, "tag") == 0)
2402 else if (strcmp (oo->name, "skip") == 0)
2404 else if (strcmp (oo->name, "default") == 0)
2406 else if (strcmp (oo->name, "reorder") == 0)
2407 d->reorder_fn = oo->info;
2408 else if (strncmp (oo->name, "use_param", 9) == 0
2409 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2415 if (union_p && tagid)
2417 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
2420 else if (union_p && default_p)
2422 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2426 else if (!union_p && (default_p || tagid))
2427 error_at_line (d->line,
2428 "can't use `%s' outside a union on field `%s'",
2429 default_p ? "default" : "tag", f->name);
2430 else if (union_p && !(default_p || tagid)
2431 && f->type->kind == TYPE_SCALAR)
2434 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2435 get_input_file_name (d->line->file), d->line->line,
2439 else if (union_p && !(default_p || tagid))
2440 error_at_line (d->line,
2441 "field `%s' is missing `tag' or `default' option",
2445 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
2447 d->used_length = false;
2449 if (union_p && use_param_p && d->param == NULL)
2450 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
2452 walk_type (f->type, d);
2458 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2462 d->reorder_fn = NULL;
2465 d->prev_val[1] = oldprevval1;
2466 d->prev_val[2] = oldprevval2;
2468 if (union_p && !seen_default_p)
2470 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2471 oprintf (d->of, "%*s break;\n", d->indent, "");
2475 oprintf (d->of, "%*s}\n", d->indent, "");
2481 case TYPE_LANG_STRUCT:
2484 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
2485 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
2488 error_at_line (d->line, "structure `%s' differs between languages",
2495 case TYPE_PARAM_STRUCT:
2497 type_p *oldparam = d->param;
2499 d->param = t->u.param_struct.param;
2500 walk_type (t->u.param_struct.stru, d);
2501 d->param = oldparam;
2510 /* process_field routine for marking routines. */
2513 write_types_process_field (type_p f, const struct walk_type_data *d)
2515 const struct write_types_data *wtd;
2516 const char *cast = d->needs_cast_p ? "(void *)" : "";
2517 wtd = (const struct write_types_data *) d->cookie;
2522 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
2523 wtd->subfield_marker_routine, cast, d->val);
2524 if (wtd->param_prefix)
2526 oprintf (d->of, ", %s", d->prev_val[3]);
2529 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
2530 output_mangled_typename (d->of, d->orig_s);
2533 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
2535 if (f->u.p->kind == TYPE_PARAM_STRUCT
2536 && f->u.p->u.s.line.file != NULL)
2538 oprintf (d->of, ", gt_e_");
2539 output_mangled_typename (d->of, f);
2541 else if (UNION_OR_STRUCT_P (f) && f->u.p->u.s.line.file != NULL)
2543 oprintf (d->of, ", gt_ggc_e_");
2544 output_mangled_typename (d->of, f);
2547 oprintf (d->of, ", gt_types_enum_last");
2549 oprintf (d->of, ");\n");
2550 if (d->reorder_fn && wtd->reorder_note_routine)
2551 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
2552 wtd->reorder_note_routine, cast, d->val,
2553 d->prev_val[3], d->reorder_fn);
2559 case TYPE_LANG_STRUCT:
2560 case TYPE_PARAM_STRUCT:
2561 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
2562 output_mangled_typename (d->of, f);
2563 oprintf (d->of, " (%s%s);\n", cast, d->val);
2564 if (d->reorder_fn && wtd->reorder_note_routine)
2565 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
2566 wtd->reorder_note_routine, cast, d->val, cast, d->val,
2578 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
2581 output_type_enum (outf_p of, type_p s)
2583 if (s->kind == TYPE_PARAM_STRUCT && s->u.param_struct.line.file != NULL)
2585 oprintf (of, ", gt_e_");
2586 output_mangled_typename (of, s);
2588 else if (UNION_OR_STRUCT_P (s) && s->u.s.line.file != NULL)
2590 oprintf (of, ", gt_ggc_e_");
2591 output_mangled_typename (of, s);
2594 oprintf (of, ", gt_types_enum_last");
2597 /* Return an output file that is suitable for definitions which can
2598 reference struct S */
2601 get_output_file_for_structure (const_type_p s, type_p *param)
2603 const input_file *fn;
2606 gcc_assert (UNION_OR_STRUCT_P (s));
2607 fn = s->u.s.line.file;
2609 /* This is a hack, and not the good kind either. */
2610 for (i = NUM_PARAM - 1; i >= 0; i--)
2611 if (param && param[i] && param[i]->kind == TYPE_POINTER
2612 && UNION_OR_STRUCT_P (param[i]->u.p))
2613 fn = param[i]->u.p->u.s.line.file;
2615 /* The call to get_output_file_with_visibility may update fn by
2616 caching its result inside, so we need the CONST_CAST. */
2617 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
2620 /* For S, a structure that's part of ORIG_S, and using parameters
2621 PARAM, write out a routine that:
2622 - Takes a parameter, a void * but actually of type *S
2623 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
2624 field of S or its substructures and (in some cases) things
2625 that are pointed to by S.
2629 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2630 const struct write_types_data *wtd)
2632 const char *chain_next = NULL;
2633 const char *chain_prev = NULL;
2634 const char *chain_circular = NULL;
2635 const char *mark_hook_name = NULL;
2637 struct walk_type_data d;
2639 memset (&d, 0, sizeof (d));
2640 d.of = get_output_file_for_structure (s, param);
2642 for (opt = s->u.s.opt; opt; opt = opt->next)
2643 if (strcmp (opt->name, "chain_next") == 0)
2644 chain_next = opt->info;
2645 else if (strcmp (opt->name, "chain_prev") == 0)
2646 chain_prev = opt->info;
2647 else if (strcmp (opt->name, "chain_circular") == 0)
2648 chain_circular = opt->info;
2649 else if (strcmp (opt->name, "mark_hook") == 0)
2650 mark_hook_name = opt->info;
2652 if (chain_prev != NULL && chain_next == NULL)
2653 error_at_line (&s->u.s.line, "chain_prev without chain_next");
2654 if (chain_circular != NULL && chain_next != NULL)
2655 error_at_line (&s->u.s.line, "chain_circular with chain_next");
2656 if (chain_circular != NULL)
2657 chain_next = chain_circular;
2659 d.process_field = write_types_process_field;
2663 d.line = &s->u.s.line;
2664 d.bitmap = s->u.s.bitmap;
2666 d.prev_val[0] = "*x";
2667 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
2668 d.prev_val[3] = "x";
2671 oprintf (d.of, "\n");
2672 oprintf (d.of, "void\n");
2674 oprintf (d.of, "gt_%sx_%s", wtd->prefix, orig_s->u.s.tag);
2677 oprintf (d.of, "gt_%s_", wtd->prefix);
2678 output_mangled_typename (d.of, orig_s);
2680 oprintf (d.of, " (void *x_p)\n");
2681 oprintf (d.of, "{\n");
2682 oprintf (d.of, " %s %s * %sx = (%s %s *)x_p;\n",
2683 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
2684 chain_next == NULL ? "const " : "",
2685 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2686 if (chain_next != NULL)
2687 oprintf (d.of, " %s %s * xlimit = x;\n",
2688 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2689 if (chain_next == NULL)
2691 oprintf (d.of, " if (%s (x", wtd->marker_routine);
2692 if (wtd->param_prefix)
2694 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
2695 output_mangled_typename (d.of, orig_s);
2696 output_type_enum (d.of, orig_s);
2698 oprintf (d.of, "))\n");
2702 if (chain_circular != NULL)
2703 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
2705 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
2706 if (wtd->param_prefix)
2708 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
2709 output_mangled_typename (d.of, orig_s);
2710 output_type_enum (d.of, orig_s);
2712 oprintf (d.of, "))\n");
2713 if (chain_circular != NULL)
2714 oprintf (d.of, " return;\n do\n");
2715 if (mark_hook_name && !wtd->skip_hooks)
2717 oprintf (d.of, " {\n");
2718 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
2720 oprintf (d.of, " xlimit = (");
2721 d.prev_val[2] = "*xlimit";
2722 output_escaped_param (&d, chain_next, "chain_next");
2723 oprintf (d.of, ");\n");
2724 if (mark_hook_name && !wtd->skip_hooks)
2725 oprintf (d.of, " }\n");
2726 if (chain_prev != NULL)
2728 oprintf (d.of, " if (x != xlimit)\n");
2729 oprintf (d.of, " for (;;)\n");
2730 oprintf (d.of, " {\n");
2731 oprintf (d.of, " %s %s * const xprev = (",
2732 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2734 d.prev_val[2] = "*x";
2735 output_escaped_param (&d, chain_prev, "chain_prev");
2736 oprintf (d.of, ");\n");
2737 oprintf (d.of, " if (xprev == NULL) break;\n");
2738 oprintf (d.of, " x = xprev;\n");
2739 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
2740 if (wtd->param_prefix)
2742 oprintf (d.of, ", xprev, 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 oprintf (d.of, " }\n");
2749 if (chain_circular != NULL)
2751 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
2752 if (wtd->param_prefix)
2754 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
2755 output_mangled_typename (d.of, orig_s);
2756 output_type_enum (d.of, orig_s);
2758 oprintf (d.of, "));\n");
2759 if (mark_hook_name && !wtd->skip_hooks)
2760 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
2761 oprintf (d.of, " do\n");
2764 oprintf (d.of, " while (x != xlimit)\n");
2766 oprintf (d.of, " {\n");
2767 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
2769 oprintf (d.of, " %s (x);\n", mark_hook_name);
2771 d.prev_val[2] = "*x";
2775 if (chain_next != NULL)
2777 oprintf (d.of, " x = (");
2778 output_escaped_param (&d, chain_next, "chain_next");
2779 oprintf (d.of, ");\n");
2782 oprintf (d.of, " }\n");
2783 if (chain_circular != NULL)
2784 oprintf (d.of, " while (x != xlimit);\n");
2785 oprintf (d.of, "}\n");
2788 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
2791 write_types (outf_p output_header, type_p structures, type_p param_structs,
2792 const struct write_types_data *wtd)
2794 int nbfun = 0; /* Count the emitted functions. */
2797 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
2798 /* We first emit the macros and the declarations. Functions' code is
2799 emitted afterwards. This is needed in plugin mode. */
2800 oprintf (output_header, "/* macros and declarations */\n");
2801 for (s = structures; s; s = s->next)
2802 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
2806 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
2809 oprintf (output_header, "#define gt_%s_", wtd->prefix);
2810 output_mangled_typename (output_header, s);
2811 oprintf (output_header, "(X) do { \\\n");
2812 oprintf (output_header,
2813 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
2815 oprintf (output_header, " } while (0)\n");
2817 for (opt = s->u.s.opt; opt; opt = opt->next)
2818 if (strcmp (opt->name, "ptr_alias") == 0)
2820 const_type_p const t = (const_type_p) opt->info;
2821 if (t->kind == TYPE_STRUCT
2822 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
2823 oprintf (output_header,
2824 "#define gt_%sx_%s gt_%sx_%s\n",
2825 wtd->prefix, s->u.s.tag, wtd->prefix, t->u.s.tag);
2827 error_at_line (&s->u.s.line,
2828 "structure alias is not a structure");
2834 /* Declare the marker procedure only once. */
2835 oprintf (output_header,
2836 "extern void gt_%sx_%s (void *);\n",
2837 wtd->prefix, s->u.s.tag);
2839 if (s->u.s.line.file == NULL)
2841 fprintf (stderr, "warning: structure `%s' used but not defined\n",
2847 for (s = param_structs; s; s = s->next)
2848 if (s->gc_used == GC_POINTED_TO)
2850 type_p stru = s->u.param_struct.stru;
2852 /* Declare the marker procedure. */
2853 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
2854 output_mangled_typename (output_header, s);
2855 oprintf (output_header, " (void *);\n");
2857 if (stru->u.s.line.file == NULL)
2859 fprintf (stderr, "warning: structure `%s' used but not defined\n",
2865 /* At last we emit the functions code. */
2866 oprintf (output_header, "\n/* functions code */\n");
2867 for (s = structures; s; s = s->next)
2868 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
2872 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
2874 for (opt = s->u.s.opt; opt; opt = opt->next)
2875 if (strcmp (opt->name, "ptr_alias") == 0)
2880 if (s->kind == TYPE_LANG_STRUCT)
2883 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
2886 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
2887 nbfun, (void*) ss, ss->u.s.tag);
2888 write_func_for_structure (s, ss, NULL, wtd);
2894 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
2895 nbfun, (void*) s, s->u.s.tag);
2896 write_func_for_structure (s, s, NULL, wtd);
2901 /* Structure s is not possibly pointed to, so can be ignored. */
2902 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
2903 (void*)s, s->u.s.tag,
2907 for (s = param_structs; s; s = s->next)
2908 if (s->gc_used == GC_POINTED_TO)
2910 type_p *param = s->u.param_struct.param;
2911 type_p stru = s->u.param_struct.stru;
2912 if (stru->u.s.line.file == NULL)
2914 if (stru->kind == TYPE_LANG_STRUCT)
2917 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
2920 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
2921 nbfun, (void*) ss, ss->u.s.tag);
2922 write_func_for_structure (s, ss, param, wtd);
2928 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
2930 (void*) stru, stru->u.s.tag);
2931 write_func_for_structure (s, stru, param, wtd);
2936 /* Param structure s is not pointed to, so should be ignored. */
2937 DBGPRINTF ("ignored s @ %p", (void*)s);
2939 if (verbosity_level >= 2)
2940 printf ("%s emitted %d routines for %s\n",
2941 progname, nbfun, wtd->comment);
2944 static const struct write_types_data ggc_wtd = {
2945 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
2946 "GC marker procedures. ",
2950 static const struct write_types_data pch_wtd = {
2951 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
2952 "gt_pch_note_reorder",
2953 "PCH type-walking procedures. ",
2957 /* Write out the local pointer-walking routines. */
2959 /* process_field routine for local pointer-walking. */
2962 write_types_local_process_field (type_p f, const struct walk_type_data *d)
2969 case TYPE_LANG_STRUCT:
2970 case TYPE_PARAM_STRUCT:
2972 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
2974 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
2985 /* For S, a structure that's part of ORIG_S, and using parameters
2986 PARAM, write out a routine that:
2987 - Is of type gt_note_pointers
2988 - Calls PROCESS_FIELD on each field of S or its substructures.
2992 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
2994 struct walk_type_data d;
2996 memset (&d, 0, sizeof (d));
2997 d.of = get_output_file_for_structure (s, param);
2998 d.process_field = write_types_local_process_field;
3000 d.line = &s->u.s.line;
3001 d.bitmap = s->u.s.bitmap;
3003 d.prev_val[0] = d.prev_val[2] = "*x";
3004 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3005 d.prev_val[3] = "x";
3007 d.fn_wants_lvalue = true;
3009 oprintf (d.of, "\n");
3010 oprintf (d.of, "void\n");
3011 oprintf (d.of, "gt_pch_p_");
3012 output_mangled_typename (d.of, orig_s);
3013 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3015 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3016 "\tATTRIBUTE_UNUSED void *cookie)\n");
3017 oprintf (d.of, "{\n");
3018 oprintf (d.of, " %s %s * const x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3019 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3020 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3023 oprintf (d.of, "}\n");
3026 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3029 write_local (outf_p output_header, type_p structures, type_p param_structs)
3035 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3036 for (s = structures; s; s = s->next)
3037 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3041 if (s->u.s.line.file == NULL)
3044 for (opt = s->u.s.opt; opt; opt = opt->next)
3045 if (strcmp (opt->name, "ptr_alias") == 0)
3047 const_type_p const t = (const_type_p) opt->info;
3048 if (t->kind == TYPE_STRUCT
3049 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3051 oprintf (output_header, "#define gt_pch_p_");
3052 output_mangled_typename (output_header, s);
3053 oprintf (output_header, " gt_pch_p_");
3054 output_mangled_typename (output_header, t);
3055 oprintf (output_header, "\n");
3058 error_at_line (&s->u.s.line,
3059 "structure alias is not a structure");
3065 /* Declare the marker procedure only once. */
3066 oprintf (output_header, "extern void gt_pch_p_");
3067 output_mangled_typename (output_header, s);
3068 oprintf (output_header,
3069 "\n (void *, void *, gt_pointer_operator, void *);\n");
3071 if (s->kind == TYPE_LANG_STRUCT)
3074 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3075 write_local_func_for_structure (s, ss, NULL);
3078 write_local_func_for_structure (s, s, NULL);
3081 for (s = param_structs; s; s = s->next)
3082 if (s->gc_used == GC_POINTED_TO)
3084 type_p *param = s->u.param_struct.param;
3085 type_p stru = s->u.param_struct.stru;
3087 /* Declare the marker procedure. */
3088 oprintf (output_header, "extern void gt_pch_p_");
3089 output_mangled_typename (output_header, s);
3090 oprintf (output_header,
3091 "\n (void *, void *, gt_pointer_operator, void *);\n");
3093 if (stru->u.s.line.file == NULL)
3095 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3100 if (stru->kind == TYPE_LANG_STRUCT)
3103 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3104 write_local_func_for_structure (s, ss, param);
3107 write_local_func_for_structure (s, stru, param);
3111 /* Nonzero if S is a type for which typed GC allocators should be output. */
3113 #define USED_BY_TYPED_GC_P(s) \
3114 (((s->kind == TYPE_POINTER) \
3115 && ((s->u.p->gc_used == GC_POINTED_TO) \
3116 || (s->u.p->gc_used == GC_USED))) \
3117 || (UNION_OR_STRUCT_P (s) && \
3118 (((s)->gc_used == GC_POINTED_TO) \
3119 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
3120 && s->u.s.line.file != NULL) \
3121 || ((s)->gc_used == GC_USED \
3122 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
3125 /* Write out the 'enum' definition for gt_types_enum. */
3128 write_enum_defn (type_p structures, type_p param_structs)
3132 int nbparamstruct = 0;
3136 oprintf (header_file, "\n/* Enumeration of types known. */\n");
3137 oprintf (header_file, "enum gt_types_enum {\n");
3138 for (s = structures; s; s = s->next)
3139 if (USED_BY_TYPED_GC_P (s))
3142 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
3143 (void*) s, nbstruct);
3144 if (UNION_OR_STRUCT_P (s))
3145 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
3146 (void*) s, nbstruct, s->u.s.tag);
3147 oprintf (header_file, " gt_ggc_e_");
3148 output_mangled_typename (header_file, s);
3149 oprintf (header_file, ",\n");
3151 for (s = param_structs; s; s = s->next)
3152 if (s->gc_used == GC_POINTED_TO)
3155 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
3156 (void*) s, nbparamstruct);
3157 oprintf (header_file, " gt_e_");
3158 output_mangled_typename (header_file, s);
3159 oprintf (header_file, ",\n");
3161 oprintf (header_file, " gt_types_enum_last\n");
3162 oprintf (header_file, "};\n");
3163 if (verbosity_level >= 2)
3164 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
3165 progname, nbstruct, nbparamstruct);
3169 /* Might T contain any non-pointer elements? */
3172 contains_scalar_p (type_p t)
3180 return contains_scalar_p (t->u.a.p);
3182 /* Could also check for structures that have no non-pointer
3183 fields, but there aren't enough of those to worry about. */
3188 /* Mangle INPF and print it to F. */
3191 put_mangled_filename (outf_p f, const input_file *inpf)
3193 /* The call to get_output_file_name may indirectly update fn since
3194 get_output_file_with_visibility caches its result inside, so we
3195 need the CONST_CAST. */
3196 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
3199 for (; *name != 0; name++)
3200 if (ISALNUM (*name))
3201 oprintf (f, "%c", *name);
3203 oprintf (f, "%c", '_');
3206 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
3207 LASTNAME, and NAME are all strings to insert in various places in
3208 the resulting code. */
3211 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
3212 const char *tname, const char *name)
3216 for (fli2 = flp; fli2; fli2 = fli2->next)
3217 if (fli2->started_p)
3219 oprintf (fli2->f, " %s\n", lastname);
3220 oprintf (fli2->f, "};\n\n");
3223 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
3224 if (fli2->started_p)
3226 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
3229 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
3232 oprintf (base_files[fnum],
3233 "extern const struct %s gt_%s_", tname, pfx);
3234 put_mangled_filename (base_files[fnum], fli2->file);
3235 oprintf (base_files[fnum], "[];\n");
3241 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3242 oprintf (base_files[fnum],
3243 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
3247 for (fli2 = flp; fli2; fli2 = fli2->next)
3248 if (fli2->started_p)
3250 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
3253 fli2->started_p = 0;
3255 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
3258 oprintf (base_files[fnum], " gt_%s_", pfx);
3259 put_mangled_filename (base_files[fnum], fli2->file);
3260 oprintf (base_files[fnum], ",\n");
3266 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3268 oprintf (base_files[fnum], " NULL\n");
3269 oprintf (base_files[fnum], "};\n");
3274 /* Write the first three fields (pointer, count and stride) for
3275 root NAME to F. V and LINE are as for write_root.
3277 Return true if the entry could be written; return false on error. */
3280 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
3286 error_at_line (line, "`%s' is too complex to be a root", name);
3290 oprintf (f, " {\n");
3291 oprintf (f, " &%s,\n", name);
3294 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3296 oprintf (f, " * (%s)", ap->u.a.len);
3297 else if (ap == v->type)
3298 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
3300 oprintf (f, " sizeof (%s", v->name);
3301 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3303 oprintf (f, "),\n");
3307 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
3308 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
3312 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
3313 int has_length, struct fileloc *line, const char *if_marked,
3314 bool emit_pch, type_p field_type, const char *field_name)
3316 /* If the field reference is relative to V, rather than to some
3317 subcomponent of V, we can mark any subarrays with a single stride.
3318 We're effectively treating the field as a global variable in its
3320 if (v && type == v->type)
3325 newv.type = field_type;
3326 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
3329 /* Otherwise, any arrays nested in the structure are too complex to
3331 else if (field_type->kind == TYPE_ARRAY)
3333 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
3334 has_length, line, if_marked, emit_pch);
3337 /* Write out to F the table entry and any marker routines needed to
3338 mark NAME as TYPE. V can be one of three values:
3340 - null, if NAME is too complex to represent using a single
3341 count and stride. In this case, it is an error for NAME to
3342 contain any gc-ed data.
3344 - the outermost array that contains NAME, if NAME is part of an array.
3346 - the C variable that contains NAME, if NAME is not part of an array.
3348 LINE is the line of the C source that declares the root variable.
3349 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
3350 is nonzero iff we are building the root table for hash table caches. */
3353 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
3354 struct fileloc *line, const char *if_marked, bool emit_pch)
3361 for (fld = type->u.s.fields; fld; fld = fld->next)
3364 const char *desc = NULL;
3367 for (o = fld->opt; o; o = o->next)
3368 if (strcmp (o->name, "skip") == 0)
3370 else if (strcmp (o->name, "desc") == 0)
3372 else if (strcmp (o->name, "param_is") == 0)
3375 error_at_line (line,
3376 "field `%s' of global `%s' has unknown option `%s'",
3377 fld->name, name, o->name);
3381 else if (desc && fld->type->kind == TYPE_UNION)
3383 pair_p validf = NULL;
3386 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
3388 const char *tag = NULL;
3391 for (oo = ufld->opt; oo; oo = oo->next)
3392 if (strcmp (oo->name, "tag") == 0)
3394 if (tag == NULL || strcmp (tag, desc) != 0)
3397 error_at_line (line,
3398 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
3399 name, fld->name, validf->name,
3400 name, fld->name, ufld->name, tag);
3404 write_field_root (f, v, type, name, 0, line, if_marked,
3405 emit_pch, validf->type,
3406 ACONCAT ((fld->name, ".",
3407 validf->name, NULL)));
3410 error_at_line (line,
3411 "global `%s.%s' has `desc' option but is not union",
3414 write_field_root (f, v, type, name, 0, line, if_marked,
3415 emit_pch, fld->type, fld->name);
3423 newname = xasprintf ("%s[0]", name);
3424 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
3434 if (!start_root_entry (f, v, name, line))
3439 if (!has_length && UNION_OR_STRUCT_P (tp))
3441 oprintf (f, " >_ggc_mx_%s,\n", tp->u.s.tag);
3443 oprintf (f, " >_pch_nx_%s", tp->u.s.tag);
3445 oprintf (f, " NULL");
3447 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
3449 oprintf (f, " >_ggc_m_");
3450 output_mangled_typename (f, tp);
3453 oprintf (f, ",\n >_pch_n_");
3454 output_mangled_typename (f, tp);
3457 oprintf (f, ",\n NULL");
3460 && (tp->kind == TYPE_POINTER || UNION_OR_STRUCT_P (tp)))
3462 oprintf (f, " >_ggc_ma_%s,\n", name);
3464 oprintf (f, " >_pch_na_%s", name);
3466 oprintf (f, " NULL");
3470 error_at_line (line,
3471 "global `%s' is pointer to unimplemented type",
3475 oprintf (f, ",\n &%s", if_marked);
3476 oprintf (f, "\n },\n");
3482 if (!start_root_entry (f, v, name, line))
3485 oprintf (f, " (gt_pointer_walker) >_ggc_m_S,\n");
3486 oprintf (f, " (gt_pointer_walker) >_pch_n_S\n");
3487 oprintf (f, " },\n");
3495 error_at_line (line, "global `%s' is unimplemented type", name);
3499 /* This generates a routine to walk an array. */
3502 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
3504 struct walk_type_data d;
3507 memset (&d, 0, sizeof (d));
3513 d.bitmap = get_lang_bitmap (v->line.file);
3516 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
3518 if (wtd->param_prefix)
3520 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
3521 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
3522 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
3523 wtd->param_prefix, v->name);
3525 " ATTRIBUTE_UNUSED void *x_p,\n"
3526 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3527 " ATTRIBUTE_UNUSED void * cookie)\n");
3528 oprintf (d.of, "{\n");
3529 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3530 d.process_field = write_types_local_process_field;
3531 walk_type (v->type, &d);
3532 oprintf (f, "}\n\n");
3536 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
3537 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
3538 wtd->prefix, v->name);
3540 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3541 d.process_field = write_types_process_field;
3542 walk_type (v->type, &d);
3544 oprintf (f, "}\n\n");
3547 /* Output a table describing the locations and types of VARIABLES. */
3550 write_roots (pair_p variables, bool emit_pch)
3553 struct flist *flp = NULL;
3555 for (v = variables; v; v = v->next)
3558 get_output_file_with_visibility (CONST_CAST (input_file*,
3561 const char *length = NULL;
3562 int deletable_p = 0;
3565 for (o = v->opt; o; o = o->next)
3566 if (strcmp (o->name, "length") == 0)
3568 else if (strcmp (o->name, "deletable") == 0)
3570 else if (strcmp (o->name, "param_is") == 0)
3572 else if (strncmp (o->name, "param", 5) == 0
3573 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
3575 else if (strcmp (o->name, "if_marked") == 0)
3578 error_at_line (&v->line,
3579 "global `%s' has unknown option `%s'",
3582 for (fli = flp; fli; fli = fli->next)
3583 if (fli->f == f && f)
3587 fli = XNEW (struct flist);
3591 fli->file = v->line.file;
3592 gcc_assert (fli->file);
3595 oprintf (f, "\n/* GC roots. */\n\n");
3600 && v->type->kind == TYPE_POINTER
3601 && (v->type->u.p->kind == TYPE_POINTER
3602 || v->type->u.p->kind == TYPE_STRUCT))
3604 write_array (f, v, &ggc_wtd);
3605 write_array (f, v, &pch_wtd);
3609 for (v = variables; v; v = v->next)
3611 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
3618 for (o = v->opt; o; o = o->next)
3619 if (strcmp (o->name, "length") == 0)
3621 else if (strcmp (o->name, "deletable") == 0
3622 || strcmp (o->name, "if_marked") == 0)
3628 for (fli = flp; fli; fli = fli->next)
3631 if (!fli->started_p)
3635 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
3636 put_mangled_filename (f, v->line.file);
3637 oprintf (f, "[] = {\n");
3640 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
3643 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3646 for (v = variables; v; v = v->next)
3648 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
3654 for (o = v->opt; o; o = o->next)
3655 if (strcmp (o->name, "deletable") == 0)
3657 else if (strcmp (o->name, "if_marked") == 0)
3663 for (fli = flp; fli; fli = fli->next)
3666 if (!fli->started_p)
3670 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
3671 put_mangled_filename (f, v->line.file);
3672 oprintf (f, "[] = {\n");
3675 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3679 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3680 "gt_ggc_deletable_rtab");
3682 for (v = variables; v; v = v->next)
3684 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
3687 const char *if_marked = NULL;
3691 for (o = v->opt; o; o = o->next)
3692 if (strcmp (o->name, "length") == 0)
3694 else if (strcmp (o->name, "if_marked") == 0)
3695 if_marked = o->info;
3697 if (if_marked == NULL)
3700 if (v->type->kind != TYPE_POINTER
3701 || v->type->u.p->kind != TYPE_PARAM_STRUCT
3702 || v->type->u.p->u.param_struct.stru != find_structure ("htab", 0))
3704 error_at_line (&v->line,
3705 "if_marked option used but not hash table");
3709 for (fli = flp; fli; fli = fli->next)
3712 if (!fli->started_p)
3716 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
3717 put_mangled_filename (f, v->line.file);
3718 oprintf (f, "[] = {\n");
3721 write_root (f, v, v->type->u.p->u.param_struct.param[0],
3722 v->name, length_p, &v->line, if_marked, emit_pch);
3725 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
3726 "gt_ggc_cache_rtab");
3731 for (v = variables; v; v = v->next)
3733 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
3737 int if_marked_p = 0;
3740 for (o = v->opt; o; o = o->next)
3741 if (strcmp (o->name, "length") == 0)
3743 else if (strcmp (o->name, "if_marked") == 0)
3749 for (fli = flp; fli; fli = fli->next)
3752 if (!fli->started_p)
3756 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
3757 put_mangled_filename (f, v->line.file);
3758 oprintf (f, "[] = {\n");
3761 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
3764 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3765 "gt_pch_cache_rtab");
3767 for (v = variables; v; v = v->next)
3769 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
3775 for (o = v->opt; o; o = o->next)
3776 if (strcmp (o->name, "deletable") == 0
3777 || strcmp (o->name, "if_marked") == 0)
3783 if (!contains_scalar_p (v->type))
3786 for (fli = flp; fli; fli = fli->next)
3789 if (!fli->started_p)
3793 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
3794 put_mangled_filename (f, v->line.file);
3795 oprintf (f, "[] = {\n");
3798 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3802 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3803 "gt_pch_scalar_rtab");
3806 /* Record the definition of a generic VEC structure, as if we had expanded
3807 the macros in vec.h:
3809 typedef struct VEC_<type>_base GTY(()) {
3812 <type> GTY((length ("%h.num"))) vec[1];
3815 where the GTY(()) tags are only present if is_scalar is _false_. */
3818 note_def_vec (const char *type_name, bool is_scalar, struct fileloc *pos)
3823 type_p len_ty = create_scalar_type ("unsigned");
3824 const char *name = concat ("VEC_", type_name, "_base", (char *) 0);
3828 t = create_scalar_type (type_name);
3833 t = resolve_typedef (type_name, pos);
3834 o = create_option (0, "length", "%h.num");
3837 /* We assemble the field list in reverse order. */
3838 fields = create_field_at (0, create_array (t, "1"), "vec", o, pos);
3839 fields = create_field_at (fields, len_ty, "alloc", 0, pos);
3840 fields = create_field_at (fields, len_ty, "num", 0, pos);
3842 do_typedef (name, new_structure (name, 0, pos, fields, 0), pos);
3845 /* Record the definition of an allocation-specific VEC structure, as if
3846 we had expanded the macros in vec.h:
3848 typedef struct VEC_<type>_<astrat> {
3849 VEC_<type>_base base;
3850 } VEC_<type>_<astrat>;
3853 note_def_vec_alloc (const char *type, const char *astrat, struct fileloc *pos)
3855 const char *astratname = concat ("VEC_", type, "_", astrat, (char *) 0);
3856 const char *basename = concat ("VEC_", type, "_base", (char *) 0);
3858 pair_p field = create_field_at (0, resolve_typedef (basename, pos),
3861 do_typedef (astratname, new_structure (astratname, 0, pos, field, 0), pos);
3864 /* Returns the specifier keyword for a string or union type S, empty string
3868 get_type_specifier (const type_p s)
3870 if (s->kind == TYPE_STRUCT || s->kind == TYPE_LANG_STRUCT)
3872 if (s->kind == TYPE_UNION)
3877 /* TRUE if type S has the GTY variable_size annotation. */
3880 variable_size_p (const type_p s)
3883 for (o = s->u.s.opt; o; o = o->next)
3884 if (strcmp (o->name, "variable_size") == 0)
3892 { any_zone, specific_zone };
3894 /* Writes one typed allocator definition for type identifier TYPE_NAME with
3895 optional type specifier TYPE_SPECIFIER. The allocator name will contain
3896 ALLOCATOR_TYPE. If VARIABLE_SIZE is true, the allocator will have an extra
3897 parameter specifying number of bytes to allocate. If QUANTITY is set to
3898 VECTOR, a vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
3899 the allocator will be zone-specific. */
3902 write_typed_alloc_def (bool variable_size, const char *type_specifier,
3903 const char *type_name, const char *allocator_type,
3904 enum alloc_quantity quantity, enum alloc_zone zone)
3906 bool two_args = variable_size && (quantity == vector);
3907 bool third_arg = ((zone == specific_zone)
3908 && (variable_size || (quantity == vector)));
3910 oprintf (header_file, "#define ggc_alloc_%s%s", allocator_type, type_name);
3911 oprintf (header_file, "(%s%s%s%s%s) ",
3912 (variable_size ? "SIZE" : ""),
3913 (two_args ? ", " : ""),
3914 (quantity == vector) ? "n" : "",
3915 (third_arg ? ", " : ""), (zone == specific_zone) ? "z" : "");
3916 oprintf (header_file, "((%s%s *)", type_specifier, type_name);
3917 oprintf (header_file, "(ggc_internal_%salloc_stat (", allocator_type);
3918 if (zone == specific_zone)
3919 oprintf (header_file, "z, ");
3921 oprintf (header_file, "SIZE");
3923 oprintf (header_file, "sizeof (%s%s)", type_specifier, type_name);
3924 if (quantity == vector)
3925 oprintf (header_file, ", n");
3926 oprintf (header_file, " MEM_STAT_INFO)))\n");
3929 /* Writes a typed allocator definition for a struct or union S. */
3932 write_typed_struct_alloc_def (const type_p s, const char *allocator_type,
3933 enum alloc_quantity quantity,
3934 enum alloc_zone zone)
3936 write_typed_alloc_def (variable_size_p (s), get_type_specifier (s),
3937 s->u.s.tag, allocator_type, quantity, zone);
3940 /* Writes a typed allocator definition for a typedef P. */
3943 write_typed_typedef_alloc_def (const pair_p p, const char *allocator_type,
3944 enum alloc_quantity quantity,
3945 enum alloc_zone zone)
3947 write_typed_alloc_def (variable_size_p (p->type), "", p->name,
3948 allocator_type, quantity, zone);
3951 /* Writes typed allocator definitions for the types in STRUCTURES and
3952 TYPEDEFS that are used by GC. */
3955 write_typed_alloc_defns (const type_p structures, const pair_p typedefs)
3960 oprintf (header_file,
3961 "\n/* Allocators for known structs and unions. */\n\n");
3962 for (s = structures; s; s = s->next)
3964 if (!USED_BY_TYPED_GC_P (s))
3966 write_typed_struct_alloc_def (s, "", single, any_zone);
3967 write_typed_struct_alloc_def (s, "cleared_", single, any_zone);
3968 write_typed_struct_alloc_def (s, "vec_", vector, any_zone);
3969 write_typed_struct_alloc_def (s, "cleared_vec_", vector, any_zone);
3970 write_typed_struct_alloc_def (s, "zone_", single, specific_zone);
3971 write_typed_struct_alloc_def (s, "zone_cleared_", single,
3973 write_typed_struct_alloc_def (s, "zone_vec_", vector, specific_zone);
3974 write_typed_struct_alloc_def (s, "zone_cleared_vec_", vector,
3978 oprintf (header_file, "\n/* Allocators for known typedefs. */\n");
3979 for (p = typedefs; p; p = p->next)
3982 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
3984 write_typed_typedef_alloc_def (p, "", single, any_zone);
3985 write_typed_typedef_alloc_def (p, "cleared_", single, any_zone);
3986 write_typed_typedef_alloc_def (p, "vec_", vector, any_zone);
3987 write_typed_typedef_alloc_def (p, "cleared_vec_", vector, any_zone);
3988 write_typed_typedef_alloc_def (p, "zone_", single, specific_zone);
3989 write_typed_typedef_alloc_def (p, "zone_cleared_", single,
3991 write_typed_typedef_alloc_def (p, "zone_cleared_vec_", vector,
3996 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
3997 guaranteee for somewhat increased readability. If name conflicts do happen,
3998 this funcion will have to be adjusted to be more like
3999 output_mangled_typename. */
4002 output_typename (outf_p of, const_type_p t)
4007 oprintf (of, "str");
4010 oprintf (of, "scalar");
4013 output_typename (of, t->u.p);
4017 case TYPE_LANG_STRUCT:
4018 oprintf (of, "%s", t->u.s.tag);
4020 case TYPE_PARAM_STRUCT:
4023 for (i = 0; i < NUM_PARAM; i++)
4024 if (t->u.param_struct.param[i] != NULL)
4026 output_typename (of, t->u.param_struct.param[i]);
4029 output_typename (of, t->u.param_struct.stru);
4037 /* Writes a typed GC allocator for type S that is suitable as a callback for
4038 the splay tree implementation in libiberty. */
4041 write_splay_tree_allocator_def (const_type_p s)
4043 outf_p of = get_output_file_with_visibility (NULL);
4044 oprintf (of, "void * ggc_alloc_splay_tree_");
4045 output_typename (of, s);
4046 oprintf (of, " (int sz, void * nl)\n");
4047 oprintf (of, "{\n");
4048 oprintf (of, " return ggc_splay_alloc (");
4049 oprintf (of, "gt_e_");
4050 output_mangled_typename (of, s);
4051 oprintf (of, ", sz, nl);\n");
4052 oprintf (of, "}\n\n");
4055 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4056 for the splay tree implementation in libiberty. */
4059 write_splay_tree_allocators (const_type_p param_structs)
4063 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4064 for (s = param_structs; s; s = s->next)
4065 if (s->gc_used == GC_POINTED_TO)
4067 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4068 output_typename (header_file, s);
4069 oprintf (header_file, " (int, void *);\n");
4070 write_splay_tree_allocator_def (s);
4074 static void dump_pair (int indent, pair_p p);
4075 static void dump_type (int indent, type_p p);
4076 static void dump_type_list (int indent, type_p p);
4080 /* Dumps the value of typekind KIND. */
4083 dump_typekind (int indent, enum typekind kind)
4085 printf ("%*ckind = ", indent, ' ');
4089 printf ("TYPE_SCALAR");
4092 printf ("TYPE_STRING");
4095 printf ("TYPE_STRUCT");
4098 printf ("TYPE_UNION");
4101 printf ("TYPE_POINTER");
4104 printf ("TYPE_ARRAY");
4106 case TYPE_LANG_STRUCT:
4107 printf ("TYPE_LANG_STRUCT");
4109 case TYPE_PARAM_STRUCT:
4110 printf ("TYPE_PARAM_STRUCT");
4118 /* Dumps the value of GC_USED flag. */
4121 dump_gc_used (int indent, enum gc_used_enum gc_used)
4123 printf ("%*cgc_used = ", indent, ' ');
4127 printf ("GC_UNUSED");
4132 case GC_MAYBE_POINTED_TO:
4133 printf ("GC_MAYBE_POINTED_TO");
4136 printf ("GC_POINTED_TO");
4144 /* Dumps the type options OPT. */
4147 dump_options (int indent, options_p opt)
4150 printf ("%*coptions = ", indent, ' ');
4154 printf ("%s:%s ", o->name, o->info);
4160 /* Dumps the source file location in LINE. */
4163 dump_fileloc (int indent, struct fileloc line)
4165 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
4166 get_input_file_name (line.file),
4170 /* Recursively dumps the struct, union, or a language-specific
4174 dump_type_u_s (int indent, type_p t)
4178 gcc_assert (t->kind == TYPE_STRUCT || t->kind == TYPE_UNION
4179 || t->kind == TYPE_LANG_STRUCT);
4180 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4181 dump_fileloc (indent, t->u.s.line);
4182 printf ("%*cu.s.fields =\n", indent, ' ');
4183 fields = t->u.s.fields;
4186 dump_pair (indent + INDENT, fields);
4187 fields = fields->next;
4189 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4190 dump_options (indent, t->u.s.opt);
4191 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4192 if (t->kind == TYPE_LANG_STRUCT)
4194 printf ("%*cu.s.lang_struct:\n", indent, ' ');
4195 dump_type_list (indent + INDENT, t->u.s.lang_struct);
4199 /* Recursively dumps the array T. */
4202 dump_type_u_a (int indent, type_p t)
4204 gcc_assert (t->kind == TYPE_ARRAY);
4205 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4206 dump_type_list (indent + INDENT, t->u.a.p);
4209 /* Recursively dumps the parameterized struct T. */
4212 dump_type_u_param_struct (int indent, type_p t)
4215 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
4216 printf ("%*cu.param_struct.stru:\n", indent, ' ');
4217 dump_type_list (indent, t->u.param_struct.stru);
4218 dump_fileloc (indent, t->u.param_struct.line);
4219 for (i = 0; i < NUM_PARAM; i++)
4221 if (t->u.param_struct.param[i] == NULL)
4223 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
4224 dump_type (indent + INDENT, t->u.param_struct.param[i]);
4228 /* Recursively dumps the type list T. */
4231 dump_type_list (int indent, type_p t)
4236 dump_type (indent, p);
4241 static htab_t seen_types;
4243 /* Recursively dumps the type T if it was not dumped previously. */
4246 dump_type (int indent, type_p t)
4250 printf ("%*cType at %p: ", indent, ' ', (void *) t);
4251 slot = htab_find_slot (seen_types, t, INSERT);
4254 printf ("already seen.\n");
4260 dump_typekind (indent, t->kind);
4261 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4262 (void *) t->pointer_to);
4263 dump_gc_used (indent + INDENT, t->gc_used);
4267 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4268 t->u.scalar_is_char ? "true" : "false");
4274 case TYPE_LANG_STRUCT:
4275 dump_type_u_s (indent + INDENT, t);
4278 printf ("%*cp:\n", indent + INDENT, ' ');
4279 dump_type (indent + INDENT, t->u.p);
4282 dump_type_u_a (indent + INDENT, t);
4284 case TYPE_PARAM_STRUCT:
4285 dump_type_u_param_struct (indent + INDENT, t);
4290 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4293 /* Dumps the pair P. */
4296 dump_pair (int indent, pair_p p)
4298 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4299 dump_type (indent, p->type);
4300 dump_fileloc (indent, p->line);
4301 dump_options (indent, p->opt);
4302 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4305 /* Dumps the list of pairs PP. */
4308 dump_pair_list (const char *name, pair_p pp)
4311 printf ("%s:\n", name);
4312 for (p = pp; p != NULL; p = p->next)
4314 printf ("End of %s\n\n", name);
4317 /* Dumps the STRUCTURES. */
4320 dump_structures (const char *name, type_p structures)
4322 printf ("%s:\n", name);
4323 dump_type_list (0, structures);
4324 printf ("End of %s\n\n", name);
4327 /* Dumps the internal structures of gengtype. */
4330 dump_everything (void)
4332 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4333 dump_pair_list ("typedefs", typedefs);
4334 dump_structures ("structures", structures);
4335 dump_structures ("param_structs", param_structs);
4336 dump_pair_list ("variables", variables);
4337 htab_delete (seen_types);
4342 /* Option specification for getopt_long. */
4343 static const struct option gengtype_long_options[] = {
4344 {"help", no_argument, NULL, 'h'},
4345 {"version", no_argument, NULL, 'V'},
4346 {"verbose", no_argument, NULL, 'v'},
4347 {"dump", no_argument, NULL, 'd'},
4348 {"debug", no_argument, NULL, 'D'},
4349 {"plugin", required_argument, NULL, 'P'},
4350 {"srcdir", required_argument, NULL, 'S'},
4351 {"backupdir", required_argument, NULL, 'B'},
4352 {"inputs", required_argument, NULL, 'I'},
4353 {"read-state", required_argument, NULL, 'r'},
4354 {"write-state", required_argument, NULL, 'w'},
4355 /* Terminating NULL placeholder. */
4356 {NULL, no_argument, NULL, 0},
4363 printf ("Usage: %s\n", progname);
4364 printf ("\t -h | --help " " \t# Give this help.\n");
4365 printf ("\t -D | --debug "
4366 " \t# Give debug output to debug %s itself.\n", progname);
4367 printf ("\t -V | --version " " \t# Give version information.\n");
4368 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
4369 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4370 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4371 " \t# Generate for plugin.\n");
4372 printf ("\t -S | --srcdir <GCC-directory> "
4373 " \t# Specify the GCC source directory.\n");
4374 printf ("\t -B | --backupdir <directory> "
4375 " \t# Specify the backup directory for updated files.\n");
4376 printf ("\t -I | --inputs <input-list> "
4377 " \t# Specify the file with source files list.\n");
4378 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
4379 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
4383 print_version (void)
4385 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
4386 printf ("Report bugs: %s\n", bug_report_url);
4389 /* Parse the program options using getopt_long... */
4391 parse_program_options (int argc, char **argv)
4394 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
4395 gengtype_long_options, NULL)) >= 0)
4399 case 'h': /* --help */
4402 case 'V': /* --version */
4405 case 'd': /* --dump */
4408 case 'D': /* --debug */
4411 case 'v': /* --verbose */
4414 case 'P': /* --plugin */
4416 plugin_output_filename = optarg;
4418 fatal ("missing plugin output file name");
4420 case 'S': /* --srcdir */
4424 fatal ("missing source directory");
4425 srcdir_len = strlen (srcdir);
4427 case 'B': /* --backupdir */
4429 backup_dir = optarg;
4431 fatal ("missing backup directory");
4433 case 'I': /* --inputs */
4437 fatal ("missing input list");
4439 case 'r': /* --read-state */
4441 read_state_filename = optarg;
4443 fatal ("missing read state file");
4444 DBGPRINTF ("read state %s\n", optarg);
4446 case 'w': /* --write-state */
4447 DBGPRINTF ("write state %s\n", optarg);
4449 write_state_filename = optarg;
4451 fatal ("missing write state file");
4454 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
4456 fatal ("unexpected flag");
4459 if (plugin_output_filename)
4461 /* In plugin mode we require some input files. */
4464 fatal ("no source files given in plugin mode");
4465 nb_plugin_files = argc - optind;
4466 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
4467 for (i = 0; i < (int) nb_plugin_files; i++)
4469 char *name = argv[i + optind];
4470 plugin_files[i] = input_file_by_name (name);
4477 /******* Manage input files. ******/
4479 /* Hash table of unique input file names. */
4480 static htab_t input_file_htab;
4482 /* Find or allocate a new input_file by hash-consing it. */
4484 input_file_by_name (const char* name)
4487 input_file* f = NULL;
4491 namlen = strlen (name);
4492 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
4495 strcpy (f->inpname, name);
4496 slot = htab_find_slot (input_file_htab, f, INSERT);
4497 gcc_assert (slot != NULL);
4500 /* Already known input file. */
4502 return (input_file*)(*slot);
4504 /* New input file. */
4509 /* Hash table support routines for input_file-s. */
4511 htab_hash_inputfile (const void *p)
4513 const input_file *inpf = (const input_file *) p;
4515 return htab_hash_string (get_input_file_name (inpf));
4519 htab_eq_inputfile (const void *x, const void *y)
4521 const input_file *inpfx = (const input_file *) x;
4522 const input_file *inpfy = (const input_file *) y;
4523 gcc_assert (inpfx != NULL && inpfy != NULL);
4524 return !strcmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
4529 main (int argc, char **argv)
4532 static struct fileloc pos = { NULL, 0 };
4533 outf_p output_header;
4535 /* Mandatory common initializations. */
4536 progname = "gengtype"; /* For fatal and messages. */
4537 /* Create the hash-table used to hash-cons input files. */
4539 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
4540 /* Initialize our special input files. */
4541 this_file = input_file_by_name (__FILE__);
4542 system_h_file = input_file_by_name ("system.h");
4543 /* Set the scalar_is_char union number for predefined scalar types. */
4544 scalar_nonchar.u.scalar_is_char = FALSE;
4545 scalar_char.u.scalar_is_char = TRUE;
4547 parse_program_options (argc, argv);
4552 time_t now = (time_t) 0;
4554 DBGPRINTF ("gengtype started pid %d at %s",
4555 (int) getpid (), ctime (&now));
4557 #endif /* ENABLE_CHECKING */
4559 /* Parse the input list and the input files. */
4560 DBGPRINTF ("inputlist %s", inputlist);
4561 if (read_state_filename)
4563 fatal ("read state %s not implemented yet", read_state_filename);
4564 /* TODO: implement read state. */
4568 /* These types are set up with #define or else outside of where
4569 we can see them. We should initialize them before calling
4571 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
4573 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
4574 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
4575 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
4576 POS_HERE (do_scalar_typedef ("double_int", &pos));
4577 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
4578 POS_HERE (do_scalar_typedef ("uint8", &pos));
4579 POS_HERE (do_scalar_typedef ("jword", &pos));
4580 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
4581 POS_HERE (do_scalar_typedef ("void", &pos));
4582 POS_HERE (do_typedef ("PTR",
4583 create_pointer (resolve_typedef ("void", &pos)),
4586 read_input_list (inputlist);
4587 for (i = 0; i < num_gt_files; i++)
4589 parse_file (get_input_file_name (gt_files[i]));
4590 DBGPRINTF ("parsed file #%d %s",
4591 (int) i, get_input_file_name (gt_files[i]));
4593 if (verbosity_level >= 1)
4594 printf ("%s parsed %d files\n", progname, (int) num_gt_files);
4596 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
4597 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
4601 fatal ("either an input list or a read state file should be given");
4606 if (plugin_output_filename)
4609 /* In plugin mode, we should have read a state file, and have
4610 given at least one plugin file. */
4611 if (!read_state_filename)
4612 fatal ("No read state given in plugin mode for %s",
4613 plugin_output_filename);
4615 if (nb_plugin_files == 0 || !plugin_files)
4616 fatal ("No plugin files given in plugin mode for %s",
4617 plugin_output_filename);
4619 /* Parse our plugin files. */
4620 for (ix = 0; ix < nb_plugin_files; ix++)
4621 parse_file (get_input_file_name (plugin_files[ix]));
4626 plugin_output = create_file ("GCC", plugin_output_filename);
4627 DBGPRINTF ("created plugin_output %p named %s",
4628 (void *) plugin_output, plugin_output->name);
4631 { /* No plugin files, we are in normal mode. */
4633 fatal ("gengtype needs a source directory in normal mode");
4640 /* The call to set_gc_used may indirectly call find_param_structure
4641 hence enlarge the param_structs list of types. */
4642 set_gc_used (variables);
4644 /* We should write the state here, but it is not yet implemented. */
4645 if (write_state_filename)
4647 fatal ("write state %s in not yet implemented", write_state_filename);
4648 /* TODO: implement write state. */
4654 write_enum_defn (structures, param_structs);
4655 write_typed_alloc_defns (structures, typedefs);
4656 output_header = plugin_output ? plugin_output : header_file;
4657 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
4659 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
4662 write_types (output_header, structures, param_structs, &ggc_wtd);
4663 if (plugin_files == NULL)
4665 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
4667 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
4669 write_types (header_file, structures, param_structs, &pch_wtd);
4670 write_local (header_file, structures, param_structs);
4672 write_splay_tree_allocators (param_structs);
4673 write_roots (variables, plugin_files == NULL);
4675 close_output_files ();
4680 /* Don't bother about free-ing any input or plugin file, etc. */