* gcc.c-torture/execute/20101011-1.c: Skip on SH.
[official-gcc.git] / gcc / gengtype.c
blobe0ebef4f9f707cb5fdaebf5f19674895d7d7fa29
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
10 version.
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
15 for more details.
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/>. */
21 #include "bconfig.h"
22 #include "system.h"
23 #include "errors.h" /* for fatal */
24 #include "getopt.h"
25 #include "double-int.h"
26 #include "version.h" /* for version_string & pkgversion_string. */
27 #include "hashtab.h"
28 #include "gengtype.h"
30 /* Data types, macros, etc. used only in this file. */
32 /* Kinds of types we can understand. */
33 enum typekind
35 TYPE_SCALAR,
36 TYPE_STRING,
37 TYPE_STRUCT,
38 TYPE_UNION,
39 TYPE_POINTER,
40 TYPE_ARRAY,
41 TYPE_LANG_STRUCT,
42 TYPE_PARAM_STRUCT
46 /* A way to pass data through to the output end. */
47 struct options
49 struct options *next;
50 const char *name;
51 const char *info;
54 /* Option data for the 'nested_ptr' option. */
55 struct nested_ptr_data
57 type_p type;
58 const char *convert_to;
59 const char *convert_from;
62 /* A name and a type. */
63 struct pair
65 pair_p next;
66 const char *name;
67 type_p type;
68 struct fileloc line;
69 options_p opt;
72 #define NUM_PARAM 10
74 /* A description of a type. */
75 enum gc_used_enum
77 GC_UNUSED = 0,
78 GC_USED,
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. */
83 GC_MAYBE_POINTED_TO,
84 GC_POINTED_TO
87 struct type
89 enum typekind kind;
90 type_p next;
91 type_p pointer_to;
92 enum gc_used_enum gc_used;
93 union
95 type_p p;
96 struct
98 const char *tag;
99 struct fileloc line;
100 pair_p fields;
101 options_p opt;
102 lang_bitmap bitmap;
103 type_p lang_struct;
104 } s;
105 bool scalar_is_char;
106 struct
108 type_p p;
109 const char *len;
110 } a;
111 struct
113 type_p stru;
114 type_p param[NUM_PARAM];
115 struct fileloc line;
116 } param_struct;
117 } u;
120 #define UNION_P(x) \
121 ((x)->kind == TYPE_UNION || \
122 ((x)->kind == TYPE_LANG_STRUCT \
123 && (x)->u.s.lang_struct->kind == TYPE_UNION))
124 #define UNION_OR_STRUCT_P(x) \
125 ((x)->kind == TYPE_UNION \
126 || (x)->kind == TYPE_STRUCT \
127 || (x)->kind == TYPE_LANG_STRUCT)
131 const char *get_output_file_name (const char *);
134 /* The list of output files. */
135 outf_p output_files;
137 /* The output header file that is included into pretty much every
138 source file. */
139 outf_p header_file;
142 /* The name of the file containing the list of input files. */
143 static char *inputlist;
145 /* The plugin input files and their number; in that case only
146 a single file is produced. */
147 static char **plugin_files;
148 static size_t nb_plugin_files;
150 /* The generated plugin output file and name. */
151 static outf_p plugin_output;
152 static char *plugin_output_filename;
154 /* Our source directory and its length. */
155 const char *srcdir;
156 size_t srcdir_len;
158 /* Variables used for reading and writing the state. */
159 const char *read_state_filename;
160 const char *write_state_filename;
162 /* Variables to help debugging. */
163 int do_dump;
164 int do_debug;
166 /* Level for verbose messages. */
167 int verbosity_level;
169 /* The backup directory should be in the same file system as the
170 generated files, otherwise the rename(2) system call would fail.
171 If NULL, no backup is made when overwriting a generated file. */
172 static const char* backup_dir; /* (-B) program option. */
175 static outf_p create_file (const char *, const char *);
177 static const char *get_file_basename (const char *);
178 static const char *get_file_realbasename (const char *);
179 static const char *get_file_srcdir_relative_path (const char *);
181 static int get_prefix_langdir_index (const char *);
182 static const char *get_file_langdir (const char *);
185 /* Nonzero iff an error has occurred. */
186 bool hit_error = false;
188 static void gen_rtx_next (void);
189 static void write_rtx_next (void);
190 static void open_base_files (void);
191 static void close_output_files (void);
193 /* Report an error at POS, printing MSG. */
195 void
196 error_at_line (const struct fileloc *pos, const char *msg, ...)
198 va_list ap;
200 va_start (ap, msg);
202 fprintf (stderr, "%s:%d: ", pos->file, pos->line);
203 vfprintf (stderr, msg, ap);
204 fputc ('\n', stderr);
205 hit_error = true;
207 va_end (ap);
210 /* asprintf, but produces fatal message on out-of-memory. */
211 char *
212 xasprintf (const char *format, ...)
214 int n;
215 char *result;
216 va_list ap;
218 va_start (ap, format);
219 n = vasprintf (&result, format, ap);
220 if (result == NULL || n < 0)
221 fatal ("out of memory");
222 va_end (ap);
224 return result;
227 /* Input file handling. */
229 /* Table of all input files. */
230 static const char **gt_files;
231 static size_t num_gt_files;
233 /* A number of places use the name of this "gengtype.h" file for a
234 location for things that we can't rely on the source to define.
235 Make sure we can still use pointer comparison on filenames. */
236 const char this_file[] = __FILE__;
237 /* The "system.h" file is likewise specially useful. */
238 const char system_h_file[] = "system.h";
240 /* Vector of per-language directories. */
241 static const char **lang_dir_names;
242 static size_t num_lang_dirs;
244 /* An array of output files suitable for definitions. There is one
245 BASE_FILES entry for each language. */
246 static outf_p *base_files;
248 /* Return a bitmap which has bit `1 << BASE_FILE_<lang>' set iff
249 INPUT_FILE is used by <lang>.
251 This function should be written to assume that a file _is_ used
252 if the situation is unclear. If it wrongly assumes a file _is_ used,
253 a linker error will result. If it wrongly assumes a file _is not_ used,
254 some GC roots may be missed, which is a much harder-to-debug problem.
256 The relevant bitmap is stored immediately before the file's name in the
257 buffer set up by read_input_list. It may be unaligned, so we have to
258 read it byte-by-byte. */
260 static lang_bitmap
261 get_lang_bitmap (const char *gtfile)
264 if (gtfile == this_file || gtfile == system_h_file)
266 /* Things defined in this "gengtype.c" file or in "system.h" are
267 universal (and there is no space for their lang_bitmap before
268 their file names). */
269 return (((lang_bitmap) 1) << num_lang_dirs) - 1;
271 else
273 lang_bitmap n = 0;
274 int i;
275 for (i = -(int) sizeof (lang_bitmap); i < 0; i++)
276 n = (n << CHAR_BIT) + (unsigned char) gtfile[i];
277 return n;
281 /* Set the bitmap returned by get_lang_bitmap. The only legitimate
282 caller of this function is read_input_list. */
283 static void
284 set_lang_bitmap (char *gtfile, lang_bitmap n)
286 int i;
287 for (i = -1; i >= -(int) sizeof (lang_bitmap); i--)
289 gtfile[i] = n & ((1U << CHAR_BIT) - 1);
290 n >>= CHAR_BIT;
295 #if ENABLE_CHECKING
296 /* Utility debugging function, printing the various type counts within
297 a list of types. Called thru the DBGPRINT_COUNT_TYPE macro. */
298 void
299 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
301 int nb_types = 0, nb_scalar = 0, nb_string = 0;
302 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
303 int nb_lang_struct = 0, nb_param_struct = 0;
304 type_p p = NULL;
305 for (p = t; p; p = p->next)
307 nb_types++;
308 switch (p->kind)
310 case TYPE_SCALAR:
311 nb_scalar++;
312 break;
313 case TYPE_STRING:
314 nb_string++;
315 break;
316 case TYPE_STRUCT:
317 nb_struct++;
318 break;
319 case TYPE_UNION:
320 nb_union++;
321 break;
322 case TYPE_POINTER:
323 nb_pointer++;
324 break;
325 case TYPE_ARRAY:
326 nb_array++;
327 break;
328 case TYPE_LANG_STRUCT:
329 nb_lang_struct++;
330 break;
331 case TYPE_PARAM_STRUCT:
332 nb_param_struct++;
333 break;
334 default:
335 gcc_unreachable ();
338 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
339 lbasename (fil), lin, msg, nb_types);
340 if (nb_scalar > 0 || nb_string > 0)
341 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
342 if (nb_struct > 0 || nb_union > 0)
343 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
344 if (nb_pointer > 0 || nb_array > 0)
345 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
346 if (nb_lang_struct > 0 || nb_param_struct > 0)
347 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
348 nb_lang_struct, nb_param_struct);
349 fprintf (stderr, "\n");
351 #endif /* ENABLE_CHECKING */
353 /* Scan the input file, LIST, and determine how much space we need to
354 store strings in. Also, count the number of language directories
355 and files. The numbers returned are overestimates as they does not
356 consider repeated files. */
357 static size_t
358 measure_input_list (FILE *list)
360 size_t n = 0;
361 int c;
362 bool atbol = true;
363 num_lang_dirs = 0;
364 num_gt_files = plugin_files ? nb_plugin_files : 0;
365 while ((c = getc (list)) != EOF)
367 n++;
368 if (atbol)
370 if (c == '[')
371 num_lang_dirs++;
372 else
374 /* Add space for a lang_bitmap before the input file name. */
375 n += sizeof (lang_bitmap);
376 num_gt_files++;
378 atbol = false;
381 if (c == '\n')
382 atbol = true;
385 rewind (list);
386 return n;
389 /* Read one input line from LIST to HEREP (which is updated). A
390 pointer to the string is returned via LINEP. If it was a language
391 subdirectory in square brackets, strip off the square brackets and
392 return true. Otherwise, leave space before the string for a
393 lang_bitmap, and return false. At EOF, returns false, does not
394 touch *HEREP, and sets *LINEP to NULL. POS is used for
395 diagnostics. */
396 static bool
397 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
399 char *here = *herep;
400 char *line;
401 int c = getc (list);
403 /* Read over whitespace. */
404 while (c == '\n' || c == ' ')
405 c = getc (list);
407 if (c == EOF)
409 *linep = 0;
410 return false;
412 else if (c == '[')
414 /* No space for a lang_bitmap is necessary. Discard the '['. */
415 c = getc (list);
416 line = here;
417 while (c != ']' && c != '\n' && c != EOF)
419 *here++ = c;
420 c = getc (list);
422 *here++ = '\0';
424 if (c == ']')
426 c = getc (list); /* eat what should be a newline */
427 if (c != '\n' && c != EOF)
428 error_at_line (pos, "junk on line after language tag [%s]", line);
430 else
431 error_at_line (pos, "missing close bracket for language tag [%s",
432 line);
434 *herep = here;
435 *linep = line;
436 return true;
438 else
440 /* Leave space for a lang_bitmap. */
441 memset (here, 0, sizeof (lang_bitmap));
442 here += sizeof (lang_bitmap);
443 line = here;
446 *here++ = c;
447 c = getc (list);
449 while (c != EOF && c != '\n');
450 *here++ = '\0';
451 *herep = here;
452 *linep = line;
453 return false;
457 /* Read the list of input files from LIST and compute all of the
458 relevant tables. There is one file per line of the list. At
459 first, all the files on the list are language-generic, but
460 eventually a line will appear which is the name of a language
461 subdirectory in square brackets, like this: [cp]. All subsequent
462 files are specific to that language, until another language
463 subdirectory tag appears. Files can appear more than once, if
464 they apply to more than one language. */
465 static void
466 read_input_list (const char *listname)
468 FILE *list = fopen (listname, "r");
469 if (!list)
470 fatal ("cannot open %s: %s", listname, xstrerror (errno));
471 else
473 struct fileloc epos;
474 size_t bufsz = measure_input_list (list);
475 char *buf = XNEWVEC (char, bufsz);
476 char *here = buf;
477 char *committed = buf;
478 char *limit = buf + bufsz;
479 char *line;
480 bool is_language;
481 size_t langno = 0;
482 size_t nfiles = 0;
483 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
485 epos.file = listname;
486 epos.line = 0;
488 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
489 gt_files = XNEWVEC (const char *, num_gt_files);
491 for (;;)
493 next_line:
494 epos.line++;
495 committed = here;
496 is_language = read_input_line (list, &here, &line, &epos);
497 gcc_assert (here <= limit);
498 if (line == 0)
499 break;
500 else if (is_language)
502 size_t i;
503 gcc_assert (langno <= num_lang_dirs);
504 for (i = 0; i < langno; i++)
505 if (strcmp (lang_dir_names[i], line) == 0)
507 error_at_line (&epos, "duplicate language tag [%s]",
508 line);
509 curlangs = 1 << i;
510 here = committed;
511 goto next_line;
514 curlangs = 1 << langno;
515 lang_dir_names[langno++] = line;
517 else
519 size_t i;
520 gcc_assert (nfiles <= num_gt_files);
521 for (i = 0; i < nfiles; i++)
522 if (strcmp (gt_files[i], line) == 0)
524 /* Throw away the string we just read, and add the
525 current language to the existing string's bitmap. */
526 lang_bitmap bmap = get_lang_bitmap (gt_files[i]);
527 if (bmap & curlangs)
528 error_at_line (&epos,
529 "file %s specified more than once "
530 "for language %s", line,
531 langno ==
532 0 ? "(all)" : lang_dir_names[langno -
533 1]);
535 bmap |= curlangs;
536 set_lang_bitmap (CONST_CAST (char *, gt_files[i]), bmap);
537 here = committed;
538 goto next_line;
541 set_lang_bitmap (line, curlangs);
542 gt_files[nfiles++] = line;
545 /* Update the global counts now that we know accurately how many
546 things there are. (We do not bother resizing the arrays down.) */
547 num_lang_dirs = langno;
548 /* Add the plugin files if provided. */
549 if (plugin_files)
551 size_t i;
552 for (i = 0; i < nb_plugin_files; i++)
553 gt_files[nfiles++] = plugin_files[i];
555 num_gt_files = nfiles;
558 /* Sanity check: any file that resides in a language subdirectory
559 (e.g. 'cp') ought to belong to the corresponding language.
560 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
561 (Can you even do that? Should you be allowed to?) */
563 size_t f;
564 for (f = 0; f < num_gt_files; f++)
566 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
567 const char *basename = get_file_basename (gt_files[f]);
568 const char *slashpos = strchr (basename, '/');
570 if (slashpos)
572 size_t l;
573 for (l = 0; l < num_lang_dirs; l++)
574 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
575 && memcmp (basename, lang_dir_names[l],
576 strlen (lang_dir_names[l])) == 0)
578 if (!(bitmap & (1 << l)))
579 error ("%s is in language directory '%s' but is not "
580 "tagged for that language",
581 basename, lang_dir_names[l]);
582 break;
588 if (ferror (list))
589 fatal ("error reading %s: %s", listname, xstrerror (errno));
591 fclose (list);
596 /* The one and only TYPE_STRING. */
598 static struct type string_type = {
599 TYPE_STRING, 0, 0, GC_USED, {0}
602 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
603 set to appropriate values at the beginning of main. */
605 static struct type scalar_nonchar = {
606 TYPE_SCALAR, 0, 0, GC_USED, {0}
609 static struct type scalar_char = {
610 TYPE_SCALAR, 0, 0, GC_USED, {0}
613 /* Lists of various things. */
615 static pair_p typedefs;
616 static type_p structures;
617 static type_p param_structs;
618 static pair_p variables;
620 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
621 static type_p adjust_field_tree_exp (type_p t, options_p opt);
622 static type_p adjust_field_rtx_def (type_p t, options_p opt);
624 /* Define S as a typedef to T at POS. */
626 void
627 do_typedef (const char *s, type_p t, struct fileloc *pos)
629 pair_p p;
631 /* temporary kludge - gengtype doesn't handle conditionals or
632 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
633 is coming from this file (main() sets them up with safe dummy
634 definitions). */
635 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
636 return;
638 for (p = typedefs; p != NULL; p = p->next)
639 if (strcmp (p->name, s) == 0)
641 if (p->type != t)
643 error_at_line (pos, "type `%s' previously defined", s);
644 error_at_line (&p->line, "previously defined here");
646 return;
649 p = XNEW (struct pair);
650 p->next = typedefs;
651 p->name = s;
652 p->type = t;
653 p->line = *pos;
654 p->opt = NULL;
655 typedefs = p;
658 /* Define S as a typename of a scalar. Cannot be used to define
659 typedefs of 'char'. Note: is also used for pointer-to-function
660 typedefs (which are therefore not treated as pointers). */
662 void
663 do_scalar_typedef (const char *s, struct fileloc *pos)
665 do_typedef (s, &scalar_nonchar, pos);
668 /* Return the type previously defined for S. Use POS to report errors. */
670 type_p
671 resolve_typedef (const char *s, struct fileloc *pos)
673 pair_p p;
674 for (p = typedefs; p != NULL; p = p->next)
675 if (strcmp (p->name, s) == 0)
676 return p->type;
677 error_at_line (pos, "unidentified type `%s'", s);
678 return &scalar_nonchar; /* treat as "int" */
681 /* Create and return a new structure with tag NAME (or a union iff
682 ISUNION is nonzero), at POS with fields FIELDS and options O. */
684 type_p
685 new_structure (const char *name, int isunion, struct fileloc *pos,
686 pair_p fields, options_p o)
688 type_p si;
689 type_p s = NULL;
690 lang_bitmap bitmap = get_lang_bitmap (pos->file);
692 for (si = structures; si != NULL; si = si->next)
693 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
695 type_p ls = NULL;
696 if (si->kind == TYPE_LANG_STRUCT)
698 ls = si;
700 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
701 if (si->u.s.bitmap == bitmap)
702 s = si;
704 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
706 ls = si;
707 si = XCNEW (struct type);
708 memcpy (si, ls, sizeof (struct type));
709 ls->kind = TYPE_LANG_STRUCT;
710 ls->u.s.lang_struct = si;
711 ls->u.s.fields = NULL;
712 si->next = NULL;
713 si->pointer_to = NULL;
714 si->u.s.lang_struct = ls;
716 else
717 s = si;
719 if (ls != NULL && s == NULL)
721 s = XCNEW (struct type);
722 s->next = ls->u.s.lang_struct;
723 ls->u.s.lang_struct = s;
724 s->u.s.lang_struct = ls;
726 break;
729 if (s == NULL)
731 s = XCNEW (struct type);
732 s->next = structures;
733 structures = s;
736 if (s->u.s.line.file != NULL
737 || (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap)))
739 error_at_line (pos, "duplicate definition of '%s %s'",
740 isunion ? "union" : "struct", s->u.s.tag);
741 error_at_line (&s->u.s.line, "previous definition here");
744 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
745 s->u.s.tag = name;
746 s->u.s.line = *pos;
747 s->u.s.fields = fields;
748 s->u.s.opt = o;
749 s->u.s.bitmap = bitmap;
750 if (s->u.s.lang_struct)
751 s->u.s.lang_struct->u.s.bitmap |= bitmap;
753 return s;
756 /* Return the previously-defined structure with tag NAME (or a union
757 iff ISUNION is nonzero), or a new empty structure or union if none
758 was defined previously. */
760 type_p
761 find_structure (const char *name, int isunion)
763 type_p s;
765 for (s = structures; s != NULL; s = s->next)
766 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
767 return s;
769 s = XCNEW (struct type);
770 s->next = structures;
771 structures = s;
772 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
773 s->u.s.tag = name;
774 structures = s;
775 return s;
778 /* Return the previously-defined parameterized structure for structure
779 T and parameters PARAM, or a new parameterized empty structure or
780 union if none was defined previously. */
782 static type_p
783 find_param_structure (type_p t, type_p param[NUM_PARAM])
785 type_p res;
787 for (res = param_structs; res; res = res->next)
788 if (res->u.param_struct.stru == t
789 && memcmp (res->u.param_struct.param, param,
790 sizeof (type_p) * NUM_PARAM) == 0)
791 break;
792 if (res == NULL)
794 res = XCNEW (struct type);
795 res->kind = TYPE_PARAM_STRUCT;
796 res->next = param_structs;
797 param_structs = res;
798 res->u.param_struct.stru = t;
799 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
801 return res;
804 /* Return a scalar type with name NAME. */
806 type_p
807 create_scalar_type (const char *name)
809 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
810 return &scalar_char;
811 else
812 return &scalar_nonchar;
815 /* Return a pointer to T. */
817 type_p
818 create_pointer (type_p t)
820 if (!t->pointer_to)
822 type_p r = XCNEW (struct type);
823 r->kind = TYPE_POINTER;
824 r->u.p = t;
825 t->pointer_to = r;
827 return t->pointer_to;
830 /* Return an array of length LEN. */
832 type_p
833 create_array (type_p t, const char *len)
835 type_p v;
837 v = XCNEW (struct type);
838 v->kind = TYPE_ARRAY;
839 v->u.a.p = t;
840 v->u.a.len = len;
841 return v;
844 /* Return an options structure with name NAME and info INFO. NEXT is the
845 next option in the chain. */
847 options_p
848 create_option (options_p next, const char *name, const void *info)
850 options_p o = XNEW (struct options);
851 o->next = next;
852 o->name = name;
853 o->info = (const char *) info;
854 return o;
857 /* Return an options structure for a "nested_ptr" option. */
858 options_p
859 create_nested_ptr_option (options_p next, type_p t,
860 const char *to, const char *from)
862 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
864 d->type = adjust_field_type (t, 0);
865 d->convert_to = to;
866 d->convert_from = from;
867 return create_option (next, "nested_ptr", d);
870 /* Add a variable named S of type T with options O defined at POS,
871 to `variables'. */
873 void
874 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
876 pair_p n;
877 n = XNEW (struct pair);
878 n->name = s;
879 n->type = t;
880 n->line = *pos;
881 n->opt = o;
882 n->next = variables;
883 variables = n;
886 /* Most-general structure field creator. */
887 static pair_p
888 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
889 const char *file, int line)
891 pair_p field;
893 field = XNEW (struct pair);
894 field->next = next;
895 field->type = type;
896 field->name = name;
897 field->opt = opt;
898 field->line.file = file;
899 field->line.line = line;
900 return field;
903 /* Create a field that came from the source code we are scanning,
904 i.e. we have a 'struct fileloc', and possibly options; also,
905 adjust_field_type should be called. */
906 pair_p
907 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
908 struct fileloc *pos)
910 return create_field_all (next, adjust_field_type (type, opt),
911 name, opt, pos->file, pos->line);
914 /* Create a fake field with the given type and name. NEXT is the next
915 field in the chain. */
916 #define create_field(next,type,name) \
917 create_field_all(next,type,name, 0, this_file, __LINE__)
919 /* Like create_field, but the field is only valid when condition COND
920 is true. */
922 static pair_p
923 create_optional_field_ (pair_p next, type_p type, const char *name,
924 const char *cond, int line)
926 static int id = 1;
927 pair_p union_fields;
928 type_p union_type;
930 /* Create a fake union type with a single nameless field of type TYPE.
931 The field has a tag of "1". This allows us to make the presence
932 of a field of type TYPE depend on some boolean "desc" being true. */
933 union_fields = create_field (NULL, type, "");
934 union_fields->opt = create_option (union_fields->opt, "dot", "");
935 union_fields->opt = create_option (union_fields->opt, "tag", "1");
936 union_type = new_structure (xasprintf ("%s_%d", "fake_union", id++), 1,
937 &lexer_line, union_fields, NULL);
939 /* Create the field and give it the new fake union type. Add a "desc"
940 tag that specifies the condition under which the field is valid. */
941 return create_field_all (next, union_type, name,
942 create_option (0, "desc", cond), this_file, line);
945 #define create_optional_field(next,type,name,cond) \
946 create_optional_field_(next,type,name,cond,__LINE__)
948 /* Reverse a linked list of 'struct pair's in place. */
949 pair_p
950 nreverse_pairs (pair_p list)
952 pair_p prev = 0, p, next;
953 for (p = list; p; p = next)
955 next = p->next;
956 p->next = prev;
957 prev = p;
959 return prev;
963 /* We don't care how long a CONST_DOUBLE is. */
964 #define CONST_DOUBLE_FORMAT "ww"
965 /* We don't want to see codes that are only for generator files. */
966 #undef GENERATOR_FILE
968 enum rtx_code
970 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
971 #include "rtl.def"
972 #undef DEF_RTL_EXPR
973 NUM_RTX_CODE
976 static const char *const rtx_name[NUM_RTX_CODE] = {
977 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
978 #include "rtl.def"
979 #undef DEF_RTL_EXPR
982 static const char *const rtx_format[NUM_RTX_CODE] = {
983 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
984 #include "rtl.def"
985 #undef DEF_RTL_EXPR
988 static int rtx_next_new[NUM_RTX_CODE];
990 /* We also need codes and names for insn notes (not register notes).
991 Note that we do *not* bias the note values here. */
992 enum insn_note
994 #define DEF_INSN_NOTE(NAME) NAME,
995 #include "insn-notes.def"
996 #undef DEF_INSN_NOTE
998 NOTE_INSN_MAX
1001 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1002 default field for line number notes. */
1003 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1004 #define DEF_INSN_NOTE(NAME) #NAME,
1005 #include "insn-notes.def"
1006 #undef DEF_INSN_NOTE
1009 #undef CONST_DOUBLE_FORMAT
1010 #define GENERATOR_FILE
1012 /* Generate the contents of the rtx_next array. This really doesn't belong
1013 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1015 static void
1016 gen_rtx_next (void)
1018 int i;
1019 for (i = 0; i < NUM_RTX_CODE; i++)
1021 int k;
1023 rtx_next_new[i] = -1;
1024 if (strncmp (rtx_format[i], "iuu", 3) == 0)
1025 rtx_next_new[i] = 2;
1026 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1027 rtx_next_new[i] = 1;
1028 else
1029 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1030 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1031 rtx_next_new[i] = k;
1035 /* Write out the contents of the rtx_next array. */
1036 static void
1037 write_rtx_next (void)
1039 outf_p f = get_output_file_with_visibility (NULL);
1040 int i;
1041 if (!f)
1042 return;
1044 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1045 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1046 for (i = 0; i < NUM_RTX_CODE; i++)
1047 if (rtx_next_new[i] == -1)
1048 oprintf (f, " 0,\n");
1049 else
1050 oprintf (f,
1051 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1052 oprintf (f, "};\n");
1055 /* Handle `special("rtx_def")'. This is a special case for field
1056 `fld' of struct rtx_def, which is an array of unions whose values
1057 are based in a complex way on the type of RTL. */
1059 static type_p
1060 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1062 pair_p flds = NULL;
1063 options_p nodot;
1064 int i;
1065 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1066 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1068 if (t->kind != TYPE_UNION)
1070 error_at_line (&lexer_line,
1071 "special `rtx_def' must be applied to a union");
1072 return &string_type;
1075 nodot = create_option (NULL, "dot", "");
1077 rtx_tp = create_pointer (find_structure ("rtx_def", 0));
1078 rtvec_tp = create_pointer (find_structure ("rtvec_def", 0));
1079 tree_tp = create_pointer (find_structure ("tree_node", 1));
1080 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", 0));
1081 reg_attrs_tp = create_pointer (find_structure ("reg_attrs", 0));
1082 basic_block_tp = create_pointer (find_structure ("basic_block_def", 0));
1083 constant_tp =
1084 create_pointer (find_structure ("constant_descriptor_rtx", 0));
1085 scalar_tp = &scalar_nonchar; /* rtunion int */
1088 pair_p note_flds = NULL;
1089 int c;
1091 for (c = 0; c <= NOTE_INSN_MAX; c++)
1093 switch (c)
1095 case NOTE_INSN_MAX:
1096 case NOTE_INSN_DELETED_LABEL:
1097 note_flds = create_field (note_flds, &string_type, "rt_str");
1098 break;
1100 case NOTE_INSN_BLOCK_BEG:
1101 case NOTE_INSN_BLOCK_END:
1102 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1103 break;
1105 case NOTE_INSN_VAR_LOCATION:
1106 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1107 break;
1109 default:
1110 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1111 break;
1113 /* NOTE_INSN_MAX is used as the default field for line
1114 number notes. */
1115 if (c == NOTE_INSN_MAX)
1116 note_flds->opt = create_option (nodot, "default", "");
1117 else
1118 note_flds->opt = create_option (nodot, "tag", note_insn_name[c]);
1120 note_union_tp = new_structure ("rtx_def_note_subunion", 1,
1121 &lexer_line, note_flds, NULL);
1123 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1125 pair_p sym_flds;
1127 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1128 sym_flds->opt = create_option (nodot, "default", "");
1130 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1131 sym_flds->opt = create_option (nodot, "tag", "1");
1133 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", 1,
1134 &lexer_line, sym_flds, NULL);
1136 for (i = 0; i < NUM_RTX_CODE; i++)
1138 pair_p subfields = NULL;
1139 size_t aindex, nmindex;
1140 const char *sname;
1141 type_p substruct;
1142 char *ftag;
1144 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1146 type_p t;
1147 const char *subname;
1149 switch (rtx_format[i][aindex])
1151 case '*':
1152 case 'i':
1153 case 'n':
1154 case 'w':
1155 t = scalar_tp;
1156 subname = "rt_int";
1157 break;
1159 case '0':
1160 if (i == MEM && aindex == 1)
1161 t = mem_attrs_tp, subname = "rt_mem";
1162 else if (i == JUMP_INSN && aindex == 8)
1163 t = rtx_tp, subname = "rt_rtx";
1164 else if (i == CODE_LABEL && aindex == 5)
1165 t = scalar_tp, subname = "rt_int";
1166 else if (i == CODE_LABEL && aindex == 4)
1167 t = rtx_tp, subname = "rt_rtx";
1168 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1169 t = rtx_tp, subname = "rt_rtx";
1170 else if (i == NOTE && aindex == 4)
1171 t = note_union_tp, subname = "";
1172 else if (i == NOTE && aindex == 5)
1173 t = scalar_tp, subname = "rt_int";
1174 else if (i == NOTE && aindex >= 7)
1175 t = scalar_tp, subname = "rt_int";
1176 else if (i == ADDR_DIFF_VEC && aindex == 4)
1177 t = scalar_tp, subname = "rt_int";
1178 else if (i == VALUE && aindex == 0)
1179 t = scalar_tp, subname = "rt_int";
1180 else if (i == DEBUG_EXPR && aindex == 0)
1181 t = tree_tp, subname = "rt_tree";
1182 else if (i == REG && aindex == 1)
1183 t = scalar_tp, subname = "rt_int";
1184 else if (i == REG && aindex == 2)
1185 t = reg_attrs_tp, subname = "rt_reg";
1186 else if (i == SCRATCH && aindex == 0)
1187 t = scalar_tp, subname = "rt_int";
1188 else if (i == SYMBOL_REF && aindex == 1)
1189 t = scalar_tp, subname = "rt_int";
1190 else if (i == SYMBOL_REF && aindex == 2)
1191 t = symbol_union_tp, subname = "";
1192 else if (i == BARRIER && aindex >= 3)
1193 t = scalar_tp, subname = "rt_int";
1194 else
1196 error_at_line (&lexer_line,
1197 "rtx type `%s' has `0' in position %lu, can't handle",
1198 rtx_name[i], (unsigned long) aindex);
1199 t = &string_type;
1200 subname = "rt_int";
1202 break;
1204 case 's':
1205 case 'S':
1206 case 'T':
1207 t = &string_type;
1208 subname = "rt_str";
1209 break;
1211 case 'e':
1212 case 'u':
1213 t = rtx_tp;
1214 subname = "rt_rtx";
1215 break;
1217 case 'E':
1218 case 'V':
1219 t = rtvec_tp;
1220 subname = "rt_rtvec";
1221 break;
1223 case 't':
1224 t = tree_tp;
1225 subname = "rt_tree";
1226 break;
1228 case 'B':
1229 t = basic_block_tp;
1230 subname = "rt_bb";
1231 break;
1233 default:
1234 error_at_line (&lexer_line,
1235 "rtx type `%s' has `%c' in position %lu, can't handle",
1236 rtx_name[i], rtx_format[i][aindex],
1237 (unsigned long) aindex);
1238 t = &string_type;
1239 subname = "rt_int";
1240 break;
1243 subfields = create_field (subfields, t,
1244 xasprintf (".fld[%lu].%s",
1245 (unsigned long) aindex,
1246 subname));
1247 subfields->opt = nodot;
1248 if (t == note_union_tp)
1249 subfields->opt = create_option (subfields->opt, "desc",
1250 "NOTE_KIND (&%0)");
1251 if (t == symbol_union_tp)
1252 subfields->opt = create_option (subfields->opt, "desc",
1253 "CONSTANT_POOL_ADDRESS_P (&%0)");
1256 if (i == SYMBOL_REF)
1258 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P holds. */
1259 type_p field_tp = find_structure ("block_symbol", 0);
1260 subfields
1261 = create_optional_field (subfields, field_tp, "block_sym",
1262 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1265 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1266 substruct = new_structure (sname, 0, &lexer_line, subfields, NULL);
1268 ftag = xstrdup (rtx_name[i]);
1269 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1270 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1272 flds = create_field (flds, substruct, "");
1273 flds->opt = create_option (nodot, "tag", ftag);
1276 return new_structure ("rtx_def_subunion", 1, &lexer_line, flds, nodot);
1279 /* Handle `special("tree_exp")'. This is a special case for
1280 field `operands' of struct tree_exp, which although it claims to contain
1281 pointers to trees, actually sometimes contains pointers to RTL too.
1282 Passed T, the old type of the field, and OPT its options. Returns
1283 a new type for the field. */
1285 static type_p
1286 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1288 pair_p flds;
1289 options_p nodot;
1291 if (t->kind != TYPE_ARRAY)
1293 error_at_line (&lexer_line,
1294 "special `tree_exp' must be applied to an array");
1295 return &string_type;
1298 nodot = create_option (NULL, "dot", "");
1300 flds = create_field (NULL, t, "");
1301 flds->opt = create_option (nodot, "length",
1302 "TREE_OPERAND_LENGTH ((tree) &%0)");
1303 flds->opt = create_option (flds->opt, "default", "");
1305 return new_structure ("tree_exp_subunion", 1, &lexer_line, flds, nodot);
1308 /* Perform any special processing on a type T, about to become the type
1309 of a field. Return the appropriate type for the field.
1310 At present:
1311 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1312 - Similarly for arrays of pointer-to-char;
1313 - Converts structures for which a parameter is provided to
1314 TYPE_PARAM_STRUCT;
1315 - Handles "special" options.
1318 type_p
1319 adjust_field_type (type_p t, options_p opt)
1321 int length_p = 0;
1322 const int pointer_p = t->kind == TYPE_POINTER;
1323 type_p params[NUM_PARAM];
1324 int params_p = 0;
1325 int i;
1327 for (i = 0; i < NUM_PARAM; i++)
1328 params[i] = NULL;
1330 for (; opt; opt = opt->next)
1331 if (strcmp (opt->name, "length") == 0)
1332 length_p = 1;
1333 else if (strcmp (opt->name, "param_is") == 0
1334 || (strncmp (opt->name, "param", 5) == 0
1335 && ISDIGIT (opt->name[5])
1336 && strcmp (opt->name + 6, "_is") == 0))
1338 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1340 if (!UNION_OR_STRUCT_P (t)
1341 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
1343 error_at_line (&lexer_line,
1344 "option `%s' may only be applied to structures or structure pointers",
1345 opt->name);
1346 return t;
1349 params_p = 1;
1350 if (params[num] != NULL)
1351 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1352 if (!ISDIGIT (opt->name[5]))
1353 params[num] =
1354 create_pointer (CONST_CAST2 (type_p, const char *, opt->info));
1355 else
1356 params[num] = CONST_CAST2 (type_p, const char *, opt->info);
1358 else if (strcmp (opt->name, "special") == 0)
1360 const char *special_name = opt->info;
1361 if (strcmp (special_name, "tree_exp") == 0)
1362 t = adjust_field_tree_exp (t, opt);
1363 else if (strcmp (special_name, "rtx_def") == 0)
1364 t = adjust_field_rtx_def (t, opt);
1365 else
1366 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1369 if (params_p)
1371 type_p realt;
1373 if (pointer_p)
1374 t = t->u.p;
1375 realt = find_param_structure (t, params);
1376 t = pointer_p ? create_pointer (realt) : realt;
1379 if (!length_p
1380 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1381 return &string_type;
1382 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1383 && t->u.a.p->u.p->kind == TYPE_SCALAR
1384 && t->u.a.p->u.p->u.scalar_is_char)
1385 return create_array (&string_type, t->u.a.len);
1387 return t;
1391 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *);
1392 static void set_gc_used (pair_p);
1394 /* Handle OPT for set_gc_used_type. */
1396 static void
1397 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1398 int *pass_param, int *length, int *skip,
1399 type_p *nested_ptr)
1401 options_p o;
1402 for (o = opt; o; o = o->next)
1403 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO)
1404 set_gc_used_type (CONST_CAST2 (type_p, const char *, o->info),
1405 GC_POINTED_TO, NULL);
1406 else if (strcmp (o->name, "maybe_undef") == 0)
1407 *maybe_undef = 1;
1408 else if (strcmp (o->name, "use_params") == 0)
1409 *pass_param = 1;
1410 else if (strcmp (o->name, "length") == 0)
1411 *length = 1;
1412 else if (strcmp (o->name, "skip") == 0)
1413 *skip = 1;
1414 else if (strcmp (o->name, "nested_ptr") == 0)
1415 *nested_ptr = ((const struct nested_ptr_data *) o->info)->type;
1418 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1420 static void
1421 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM])
1423 if (t->gc_used >= level)
1424 return;
1426 t->gc_used = level;
1428 switch (t->kind)
1430 case TYPE_STRUCT:
1431 case TYPE_UNION:
1433 pair_p f;
1434 int dummy;
1435 type_p dummy2;
1437 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1438 &dummy2);
1440 for (f = t->u.s.fields; f; f = f->next)
1442 int maybe_undef = 0;
1443 int pass_param = 0;
1444 int length = 0;
1445 int skip = 0;
1446 type_p nested_ptr = NULL;
1447 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1448 &length, &skip, &nested_ptr);
1450 if (nested_ptr && f->type->kind == TYPE_POINTER)
1451 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1452 pass_param ? param : NULL);
1453 else if (length && f->type->kind == TYPE_POINTER)
1454 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1455 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1456 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1457 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1458 set_gc_used_type (find_param_structure (f->type->u.p, param),
1459 GC_POINTED_TO, NULL);
1460 else if (skip)
1461 ; /* target type is not used through this field */
1462 else
1463 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL);
1465 break;
1468 case TYPE_POINTER:
1469 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1470 break;
1472 case TYPE_ARRAY:
1473 set_gc_used_type (t->u.a.p, GC_USED, param);
1474 break;
1476 case TYPE_LANG_STRUCT:
1477 for (t = t->u.s.lang_struct; t; t = t->next)
1478 set_gc_used_type (t, level, param);
1479 break;
1481 case TYPE_PARAM_STRUCT:
1483 int i;
1484 for (i = 0; i < NUM_PARAM; i++)
1485 if (t->u.param_struct.param[i] != 0)
1486 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1488 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1489 level = GC_POINTED_TO;
1490 else
1491 level = GC_USED;
1492 t->u.param_struct.stru->gc_used = GC_UNUSED;
1493 set_gc_used_type (t->u.param_struct.stru, level,
1494 t->u.param_struct.param);
1495 break;
1497 default:
1498 break;
1502 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1504 static void
1505 set_gc_used (pair_p variables)
1507 int nbvars = 0;
1508 pair_p p;
1509 for (p = variables; p; p = p->next)
1511 set_gc_used_type (p->type, GC_USED, NULL);
1512 nbvars++;
1514 if (verbosity_level >= 2)
1515 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1518 /* File mapping routines. For each input file, there is one output .c file
1519 (but some output files have many input files), and there is one .h file
1520 for the whole build. */
1522 /* Output file handling. */
1524 /* Create and return an outf_p for a new file for NAME, to be called
1525 ONAME. */
1527 static outf_p
1528 create_file (const char *name, const char *oname)
1530 static const char *const hdr[] = {
1531 " Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc.\n",
1532 "\n",
1533 "This file is part of GCC.\n",
1534 "\n",
1535 "GCC is free software; you can redistribute it and/or modify it under\n",
1536 "the terms of the GNU General Public License as published by the Free\n",
1537 "Software Foundation; either version 3, or (at your option) any later\n",
1538 "version.\n",
1539 "\n",
1540 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1541 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1542 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1543 "for more details.\n",
1544 "\n",
1545 "You should have received a copy of the GNU General Public License\n",
1546 "along with GCC; see the file COPYING3. If not see\n",
1547 "<http://www.gnu.org/licenses/>. */\n",
1548 "\n",
1549 "/* This file is machine generated. Do not edit. */\n"
1551 outf_p f;
1552 size_t i;
1554 gcc_assert (name != NULL);
1555 gcc_assert (oname != NULL);
1556 f = XCNEW (struct outf);
1557 f->next = output_files;
1558 f->name = oname;
1559 output_files = f;
1561 oprintf (f, "/* Type information for %s.\n", name);
1562 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1563 oprintf (f, "%s", hdr[i]);
1564 return f;
1567 /* Print, like fprintf, to O.
1568 N.B. You might think this could be implemented more efficiently
1569 with vsnprintf(). Unfortunately, there are C libraries that
1570 provide that function but without the C99 semantics for its return
1571 value, making it impossible to know how much space is required. */
1572 void
1573 oprintf (outf_p o, const char *format, ...)
1575 char *s;
1576 size_t slength;
1577 va_list ap;
1579 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1580 in that case. */
1581 if (!o)
1582 return;
1584 va_start (ap, format);
1585 slength = vasprintf (&s, format, ap);
1586 if (s == NULL || (int) slength < 0)
1587 fatal ("out of memory");
1588 va_end (ap);
1590 if (o->bufused + slength > o->buflength)
1592 size_t new_len = o->buflength;
1593 if (new_len == 0)
1594 new_len = 1024;
1597 new_len *= 2;
1599 while (o->bufused + slength >= new_len);
1600 o->buf = XRESIZEVEC (char, o->buf, new_len);
1601 o->buflength = new_len;
1603 memcpy (o->buf + o->bufused, s, slength);
1604 o->bufused += slength;
1605 free (s);
1608 /* Open the global header file and the language-specific header files. */
1610 static void
1611 open_base_files (void)
1613 size_t i;
1615 if (nb_plugin_files > 0 && plugin_files)
1616 return;
1618 header_file = create_file ("GCC", "gtype-desc.h");
1620 base_files = XNEWVEC (outf_p, num_lang_dirs);
1622 for (i = 0; i < num_lang_dirs; i++)
1623 base_files[i] = create_file (lang_dir_names[i],
1624 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1626 /* gtype-desc.c is a little special, so we create it here. */
1628 /* The order of files here matters very much. */
1629 static const char *const ifiles[] = {
1630 "config.h", "system.h", "coretypes.h", "tm.h",
1631 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1632 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1633 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1634 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1635 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1636 "cfglayout.h", "except.h", "output.h", "gimple.h", "cfgloop.h",
1637 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h", NULL
1639 const char *const *ifp;
1640 outf_p gtype_desc_c;
1642 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1643 for (ifp = ifiles; *ifp; ifp++)
1644 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1646 /* Make sure we handle "cfun" specially. */
1647 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1648 oprintf (gtype_desc_c, "#undef cfun\n");
1652 /* For F a filename, return the real basename of F, with all the directory
1653 components skipped. */
1655 static const char *
1656 get_file_realbasename (const char *f)
1658 const char *lastslash = strrchr (f, '/');
1660 return (lastslash != NULL) ? lastslash + 1 : f;
1663 /* For F a filename, return the relative path to F from $(srcdir) if the
1664 latter is a prefix in F, NULL otherwise. */
1666 static const char *
1667 get_file_srcdir_relative_path (const char *f)
1669 if (strlen (f) > srcdir_len
1670 && IS_DIR_SEPARATOR (f[srcdir_len])
1671 && memcmp (f, srcdir, srcdir_len) == 0)
1672 return f + srcdir_len + 1;
1673 else
1674 return NULL;
1677 /* For F a filename, return the relative path to F from $(srcdir) if the
1678 latter is a prefix in F, or the real basename of F otherwise. */
1680 static const char *
1681 get_file_basename (const char *f)
1683 const char *srcdir_path = get_file_srcdir_relative_path (f);
1685 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (f);
1688 /* For F a filename, return the lang_dir_names relative index of the language
1689 directory that is a prefix in F, if any, -1 otherwise. */
1691 static int
1692 get_prefix_langdir_index (const char *f)
1694 size_t f_len = strlen (f);
1695 size_t lang_index;
1697 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1699 const char *langdir = lang_dir_names[lang_index];
1700 size_t langdir_len = strlen (langdir);
1702 if (f_len > langdir_len
1703 && IS_DIR_SEPARATOR (f[langdir_len])
1704 && memcmp (f, langdir, langdir_len) == 0)
1705 return lang_index;
1708 return -1;
1711 /* For F a filename, return the name of language directory where F is located,
1712 if any, NULL otherwise. */
1714 static const char *
1715 get_file_langdir (const char *f)
1717 /* Get the relative path to F from $(srcdir) and find the language by
1718 comparing the prefix with language directory names. If F is not even
1719 srcdir relative, no point in looking further. */
1721 int lang_index;
1722 const char *srcdir_relative_path = get_file_srcdir_relative_path (f);
1723 const char *r;
1725 if (!srcdir_relative_path)
1726 return NULL;
1728 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1729 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1730 r = "c-family";
1731 else if (lang_index >= 0)
1732 r = lang_dir_names[lang_index];
1733 else
1734 r = NULL;
1736 return r;
1739 /* The gt- output file name for F. */
1741 static const char *
1742 get_file_gtfilename (const char *f)
1744 /* Cook up an initial version of the gt- file name from the file real
1745 basename and the language name, if any. */
1747 const char *basename = get_file_realbasename (f);
1748 const char *langdir = get_file_langdir (f);
1750 char *result =
1751 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1752 : xasprintf ("gt-%s", basename));
1754 /* Then replace all non alphanumerics characters by '-' and change the
1755 extension to ".h". We expect the input filename extension was at least
1756 one character long. */
1758 char *s = result;
1760 for (; *s != '.'; s++)
1761 if (!ISALNUM (*s) && *s != '-')
1762 *s = '-';
1764 memcpy (s, ".h", sizeof (".h"));
1766 return result;
1769 /* An output file, suitable for definitions, that can see declarations
1770 made in INPUT_FILE and is linked into every language that uses
1771 INPUT_FILE. */
1773 outf_p
1774 get_output_file_with_visibility (const char *input_file)
1776 outf_p r;
1777 size_t len;
1778 const char *basename;
1779 const char *for_name;
1780 const char *output_name;
1782 /* This can happen when we need a file with visibility on a
1783 structure that we've never seen. We have to just hope that it's
1784 globally visible. */
1785 if (input_file == NULL)
1786 input_file = "system.h";
1788 /* In plugin mode, return NULL unless the input_file is one of the
1789 plugin_files. */
1790 if (plugin_files)
1792 size_t i;
1793 for (i = 0; i < nb_plugin_files; i++)
1794 if (strcmp (input_file, plugin_files[i]) == 0)
1795 return plugin_output;
1797 return NULL;
1800 /* Determine the output file name. */
1801 basename = get_file_basename (input_file);
1803 len = strlen (basename);
1804 if ((len > 2 && memcmp (basename + len - 2, ".c", 2) == 0)
1805 || (len > 2 && memcmp (basename + len - 2, ".y", 2) == 0)
1806 || (len > 3 && memcmp (basename + len - 3, ".in", 3) == 0))
1808 output_name = get_file_gtfilename (input_file);
1809 for_name = basename;
1811 /* Some headers get used by more than one front-end; hence, it
1812 would be inappropriate to spew them out to a single gtype-<lang>.h
1813 (and gengtype doesn't know how to direct spewage into multiple
1814 gtype-<lang>.h headers at this time). Instead, we pair up these
1815 headers with source files (and their special purpose gt-*.h headers). */
1816 else if (strncmp (basename, "c-family", 8) == 0
1817 && IS_DIR_SEPARATOR (basename[8])
1818 && strcmp (basename + 9, "c-common.h") == 0)
1819 output_name = "gt-c-family-c-common.h", for_name = "c-family/c-common.c";
1820 else if (strcmp (basename, "c-lang.h") == 0)
1821 output_name = "gt-c-decl.h", for_name = "c-decl.c";
1822 else if (strcmp (basename, "c-tree.h") == 0)
1823 output_name = "gt-c-decl.h", for_name = "c-decl.c";
1824 else if (strncmp (basename, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename[2])
1825 && strcmp (basename + 3, "cp-tree.h") == 0)
1826 output_name = "gt-cp-tree.h", for_name = "cp/tree.c";
1827 else if (strncmp (basename, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename[2])
1828 && strcmp (basename + 3, "decl.h") == 0)
1829 output_name = "gt-cp-decl.h", for_name = "cp/decl.c";
1830 else if (strncmp (basename, "cp", 2) == 0 && IS_DIR_SEPARATOR (basename[2])
1831 && strcmp (basename + 3, "name-lookup.h") == 0)
1832 output_name = "gt-cp-name-lookup.h", for_name = "cp/name-lookup.c";
1833 else if (strncmp (basename, "objc", 4) == 0
1834 && IS_DIR_SEPARATOR (basename[4])
1835 && strcmp (basename + 5, "objc-act.h") == 0)
1836 output_name = "gt-objc-objc-act.h", for_name = "objc/objc-act.c";
1837 else
1839 int lang_index = get_prefix_langdir_index (basename);
1841 if (lang_index >= 0)
1842 return base_files[lang_index];
1844 output_name = "gtype-desc.c";
1845 for_name = NULL;
1848 /* Look through to see if we've ever seen this output filename before. */
1849 for (r = output_files; r; r = r->next)
1850 if (strcmp (r->name, output_name) == 0)
1851 return r;
1853 /* If not, create it. */
1854 r = create_file (for_name, output_name);
1856 gcc_assert (r && r->name);
1857 return r;
1860 /* The name of an output file, suitable for definitions, that can see
1861 declarations made in INPUT_FILE and is linked into every language
1862 that uses INPUT_FILE. */
1864 const char *
1865 get_output_file_name (const char *input_file)
1867 outf_p o = get_output_file_with_visibility (input_file);
1868 if (o)
1869 return o->name;
1870 return NULL;
1873 /* Check if existing file is equal to the in memory buffer. */
1875 static bool
1876 is_file_equal (outf_p of)
1878 FILE *newfile = fopen (of->name, "r");
1879 size_t i;
1880 bool equal;
1881 if (newfile == NULL)
1882 return false;
1884 equal = true;
1885 for (i = 0; i < of->bufused; i++)
1887 int ch;
1888 ch = fgetc (newfile);
1889 if (ch == EOF || ch != (unsigned char) of->buf[i])
1891 equal = false;
1892 break;
1895 fclose (newfile);
1896 return equal;
1899 /* Copy the output to its final destination,
1900 but don't unnecessarily change modification times. */
1902 static void
1903 close_output_files (void)
1905 int nbwrittenfiles = 0;
1906 outf_p of;
1908 for (of = output_files; of; of = of->next)
1911 if (!is_file_equal (of))
1913 FILE *newfile = NULL;
1914 char *backupname = NULL;
1915 /* Back up the old version of the output file gt-FOO.c as
1916 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
1917 if (backup_dir)
1919 backupname = concat (backup_dir, "/",
1920 lbasename (of->name), "~", NULL);
1921 if (!access (of->name, F_OK) && rename (of->name, backupname))
1922 fatal ("failed to back up %s as %s: %s",
1923 of->name, backupname, xstrerror (errno));
1926 newfile = fopen (of->name, "w");
1927 if (newfile == NULL)
1928 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
1929 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
1930 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
1931 if (fclose (newfile) != 0)
1932 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
1933 nbwrittenfiles++;
1934 if (verbosity_level >= 2 && backupname)
1935 printf ("%s wrote #%-3d %s backed-up in %s\n",
1936 progname, nbwrittenfiles, of->name, backupname);
1937 else if (verbosity_level >= 1)
1938 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
1939 free (backupname);
1941 else
1943 /* output file remains unchanged. */
1944 if (verbosity_level >= 2)
1945 printf ("%s keep %s\n", progname, of->name);
1947 free (of->buf);
1948 of->buf = NULL;
1949 of->bufused = of->buflength = 0;
1951 if (verbosity_level >= 1)
1952 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
1955 struct flist
1957 struct flist *next;
1958 int started_p;
1959 const char *name;
1960 outf_p f;
1963 struct walk_type_data;
1965 /* For scalars and strings, given the item in 'val'.
1966 For structures, given a pointer to the item in 'val'.
1967 For misc. pointers, given the item in 'val'.
1969 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
1970 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
1972 /* Parameters for write_types. */
1974 struct write_types_data
1976 const char *prefix;
1977 const char *param_prefix;
1978 const char *subfield_marker_routine;
1979 const char *marker_routine;
1980 const char *reorder_note_routine;
1981 const char *comment;
1982 int skip_hooks; /* skip hook generation if non zero */
1985 static void output_escaped_param (struct walk_type_data *d,
1986 const char *, const char *);
1987 static void output_mangled_typename (outf_p, const_type_p);
1988 static void walk_type (type_p t, struct walk_type_data *d);
1989 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
1990 const struct write_types_data *wtd);
1991 static void write_types_process_field
1992 (type_p f, const struct walk_type_data *d);
1993 static void write_types (outf_p output_header,
1994 type_p structures,
1995 type_p param_structs,
1996 const struct write_types_data *wtd);
1997 static void write_types_local_process_field
1998 (type_p f, const struct walk_type_data *d);
1999 static void write_local_func_for_structure
2000 (const_type_p orig_s, type_p s, type_p *param);
2001 static void write_local (outf_p output_header,
2002 type_p structures, type_p param_structs);
2003 static void write_enum_defn (type_p structures, type_p param_structs);
2004 static int contains_scalar_p (type_p t);
2005 static void put_mangled_filename (outf_p, const char *);
2006 static void finish_root_table (struct flist *flp, const char *pfx,
2007 const char *tname, const char *lastname,
2008 const char *name);
2009 static void write_root (outf_p, pair_p, type_p, const char *, int,
2010 struct fileloc *, const char *, bool);
2011 static void write_array (outf_p f, pair_p v,
2012 const struct write_types_data *wtd);
2013 static void write_roots (pair_p, bool);
2015 /* Parameters for walk_type. */
2017 struct walk_type_data
2019 process_field_fn process_field;
2020 const void *cookie;
2021 outf_p of;
2022 options_p opt;
2023 const char *val;
2024 const char *prev_val[4];
2025 int indent;
2026 int counter;
2027 const struct fileloc *line;
2028 lang_bitmap bitmap;
2029 type_p *param;
2030 int used_length;
2031 type_p orig_s;
2032 const char *reorder_fn;
2033 bool needs_cast_p;
2034 bool fn_wants_lvalue;
2037 /* Print a mangled name representing T to OF. */
2039 static void
2040 output_mangled_typename (outf_p of, const_type_p t)
2042 if (t == NULL)
2043 oprintf (of, "Z");
2044 else
2045 switch (t->kind)
2047 case TYPE_POINTER:
2048 oprintf (of, "P");
2049 output_mangled_typename (of, t->u.p);
2050 break;
2051 case TYPE_SCALAR:
2052 oprintf (of, "I");
2053 break;
2054 case TYPE_STRING:
2055 oprintf (of, "S");
2056 break;
2057 case TYPE_STRUCT:
2058 case TYPE_UNION:
2059 case TYPE_LANG_STRUCT:
2060 oprintf (of, "%lu%s", (unsigned long) strlen (t->u.s.tag),
2061 t->u.s.tag);
2062 break;
2063 case TYPE_PARAM_STRUCT:
2065 int i;
2066 for (i = 0; i < NUM_PARAM; i++)
2067 if (t->u.param_struct.param[i] != NULL)
2068 output_mangled_typename (of, t->u.param_struct.param[i]);
2069 output_mangled_typename (of, t->u.param_struct.stru);
2071 break;
2072 case TYPE_ARRAY:
2073 gcc_unreachable ();
2077 /* Print PARAM to D->OF processing escapes. D->VAL references the
2078 current object, D->PREV_VAL the object containing the current
2079 object, ONAME is the name of the option and D->LINE is used to
2080 print error messages. */
2082 static void
2083 output_escaped_param (struct walk_type_data *d, const char *param,
2084 const char *oname)
2086 const char *p;
2088 for (p = param; *p; p++)
2089 if (*p != '%')
2090 oprintf (d->of, "%c", *p);
2091 else
2092 switch (*++p)
2094 case 'h':
2095 oprintf (d->of, "(%s)", d->prev_val[2]);
2096 break;
2097 case '0':
2098 oprintf (d->of, "(%s)", d->prev_val[0]);
2099 break;
2100 case '1':
2101 oprintf (d->of, "(%s)", d->prev_val[1]);
2102 break;
2103 case 'a':
2105 const char *pp = d->val + strlen (d->val);
2106 while (pp[-1] == ']')
2107 while (*pp != '[')
2108 pp--;
2109 oprintf (d->of, "%s", pp);
2111 break;
2112 default:
2113 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2114 oname, '%', *p);
2118 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2119 which is of type T. Write code to D->OF to constrain execution (at
2120 the point that D->PROCESS_FIELD is called) to the appropriate
2121 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2122 pointers to those objects. D->PREV_VAL lists the objects
2123 containing the current object, D->OPT is a list of options to
2124 apply, D->INDENT is the current indentation level, D->LINE is used
2125 to print error messages, D->BITMAP indicates which languages to
2126 print the structure for, and D->PARAM is the current parameter
2127 (from an enclosing param_is option). */
2129 static void
2130 walk_type (type_p t, struct walk_type_data *d)
2132 const char *length = NULL;
2133 const char *desc = NULL;
2134 int maybe_undef_p = 0;
2135 int use_param_num = -1;
2136 int use_params_p = 0;
2137 options_p oo;
2138 const struct nested_ptr_data *nested_ptr_d = NULL;
2140 d->needs_cast_p = false;
2141 for (oo = d->opt; oo; oo = oo->next)
2142 if (strcmp (oo->name, "length") == 0)
2143 length = oo->info;
2144 else if (strcmp (oo->name, "maybe_undef") == 0)
2145 maybe_undef_p = 1;
2146 else if (strncmp (oo->name, "use_param", 9) == 0
2147 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2148 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2149 else if (strcmp (oo->name, "use_params") == 0)
2150 use_params_p = 1;
2151 else if (strcmp (oo->name, "desc") == 0)
2152 desc = oo->info;
2153 else if (strcmp (oo->name, "mark_hook") == 0)
2155 else if (strcmp (oo->name, "nested_ptr") == 0)
2156 nested_ptr_d = (const struct nested_ptr_data *) oo->info;
2157 else if (strcmp (oo->name, "dot") == 0)
2159 else if (strcmp (oo->name, "tag") == 0)
2161 else if (strcmp (oo->name, "special") == 0)
2163 else if (strcmp (oo->name, "skip") == 0)
2165 else if (strcmp (oo->name, "default") == 0)
2167 else if (strcmp (oo->name, "descbits") == 0)
2169 else if (strcmp (oo->name, "param_is") == 0)
2171 else if (strncmp (oo->name, "param", 5) == 0
2172 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2174 else if (strcmp (oo->name, "chain_next") == 0)
2176 else if (strcmp (oo->name, "chain_prev") == 0)
2178 else if (strcmp (oo->name, "chain_circular") == 0)
2180 else if (strcmp (oo->name, "reorder") == 0)
2182 else if (strcmp (oo->name, "variable_size") == 0)
2184 else
2185 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2187 if (d->used_length)
2188 length = NULL;
2190 if (use_params_p)
2192 int pointer_p = t->kind == TYPE_POINTER;
2194 if (pointer_p)
2195 t = t->u.p;
2196 if (!UNION_OR_STRUCT_P (t))
2197 error_at_line (d->line, "`use_params' option on unimplemented type");
2198 else
2199 t = find_param_structure (t, d->param);
2200 if (pointer_p)
2201 t = create_pointer (t);
2204 if (use_param_num != -1)
2206 if (d->param != NULL && d->param[use_param_num] != NULL)
2208 type_p nt = d->param[use_param_num];
2210 if (t->kind == TYPE_ARRAY)
2211 nt = create_array (nt, t->u.a.len);
2212 else if (length != NULL && t->kind == TYPE_POINTER)
2213 nt = create_pointer (nt);
2214 d->needs_cast_p = (t->kind != TYPE_POINTER
2215 && (nt->kind == TYPE_POINTER
2216 || nt->kind == TYPE_STRING));
2217 t = nt;
2219 else
2220 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2223 if (maybe_undef_p
2224 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
2226 error_at_line (d->line,
2227 "field `%s' has invalid option `maybe_undef_p'\n",
2228 d->val);
2229 return;
2232 switch (t->kind)
2234 case TYPE_SCALAR:
2235 case TYPE_STRING:
2236 d->process_field (t, d);
2237 break;
2239 case TYPE_POINTER:
2241 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2243 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2244 break;
2247 if (!length)
2249 if (!UNION_OR_STRUCT_P (t->u.p)
2250 && t->u.p->kind != TYPE_PARAM_STRUCT)
2252 error_at_line (d->line,
2253 "field `%s' is pointer to unimplemented type",
2254 d->val);
2255 break;
2258 if (nested_ptr_d)
2260 const char *oldprevval2 = d->prev_val[2];
2262 if (!UNION_OR_STRUCT_P (nested_ptr_d->type))
2264 error_at_line (d->line,
2265 "field `%s' has invalid "
2266 "option `nested_ptr'\n", d->val);
2267 return;
2270 d->prev_val[2] = d->val;
2271 oprintf (d->of, "%*s{\n", d->indent, "");
2272 d->indent += 2;
2273 d->val = xasprintf ("x%d", d->counter++);
2274 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2275 (nested_ptr_d->type->kind == TYPE_UNION
2276 ? "union" : "struct"),
2277 nested_ptr_d->type->u.s.tag,
2278 d->fn_wants_lvalue ? "" : "const ", d->val);
2279 oprintf (d->of, "%*s", d->indent + 2, "");
2280 output_escaped_param (d, nested_ptr_d->convert_from,
2281 "nested_ptr");
2282 oprintf (d->of, ";\n");
2284 d->process_field (nested_ptr_d->type, d);
2286 if (d->fn_wants_lvalue)
2288 oprintf (d->of, "%*s%s = ", d->indent, "",
2289 d->prev_val[2]);
2290 d->prev_val[2] = d->val;
2291 output_escaped_param (d, nested_ptr_d->convert_to,
2292 "nested_ptr");
2293 oprintf (d->of, ";\n");
2296 d->indent -= 2;
2297 oprintf (d->of, "%*s}\n", d->indent, "");
2298 d->val = d->prev_val[2];
2299 d->prev_val[2] = oldprevval2;
2301 else
2302 d->process_field (t->u.p, d);
2304 else
2306 int loopcounter = d->counter++;
2307 const char *oldval = d->val;
2308 const char *oldprevval3 = d->prev_val[3];
2309 char *newval;
2311 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2312 d->indent += 2;
2313 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2314 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2315 "", loopcounter, loopcounter);
2316 output_escaped_param (d, length, "length");
2317 oprintf (d->of, "); i%d++) {\n", loopcounter);
2318 d->indent += 2;
2319 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2320 d->used_length = 1;
2321 d->prev_val[3] = oldval;
2322 walk_type (t->u.p, d);
2323 free (newval);
2324 d->val = oldval;
2325 d->prev_val[3] = oldprevval3;
2326 d->used_length = 0;
2327 d->indent -= 2;
2328 oprintf (d->of, "%*s}\n", d->indent, "");
2329 d->process_field (t, d);
2330 d->indent -= 2;
2331 oprintf (d->of, "%*s}\n", d->indent, "");
2334 break;
2336 case TYPE_ARRAY:
2338 int loopcounter = d->counter++;
2339 const char *oldval = d->val;
2340 char *newval;
2342 /* If it's an array of scalars, we optimize by not generating
2343 any code. */
2344 if (t->u.a.p->kind == TYPE_SCALAR)
2345 break;
2347 /* When walking an array, compute the length and store it in a
2348 local variable before walking the array elements, instead of
2349 recomputing the length expression each time through the loop.
2350 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2351 where the length is stored in the first array element,
2352 because otherwise that operand can get overwritten on the
2353 first iteration. */
2354 oprintf (d->of, "%*s{\n", d->indent, "");
2355 d->indent += 2;
2356 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2357 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2358 d->indent, "", loopcounter);
2359 if (length)
2360 output_escaped_param (d, length, "length");
2361 else
2362 oprintf (d->of, "%s", t->u.a.len);
2363 oprintf (d->of, ");\n");
2365 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2366 d->indent, "",
2367 loopcounter, loopcounter, loopcounter, loopcounter);
2368 d->indent += 2;
2369 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2370 d->used_length = 1;
2371 walk_type (t->u.a.p, d);
2372 free (newval);
2373 d->used_length = 0;
2374 d->val = oldval;
2375 d->indent -= 2;
2376 oprintf (d->of, "%*s}\n", d->indent, "");
2377 d->indent -= 2;
2378 oprintf (d->of, "%*s}\n", d->indent, "");
2380 break;
2382 case TYPE_STRUCT:
2383 case TYPE_UNION:
2385 pair_p f;
2386 const char *oldval = d->val;
2387 const char *oldprevval1 = d->prev_val[1];
2388 const char *oldprevval2 = d->prev_val[2];
2389 const int union_p = t->kind == TYPE_UNION;
2390 int seen_default_p = 0;
2391 options_p o;
2393 if (!t->u.s.line.file)
2394 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2396 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2398 error_at_line (d->line,
2399 "structure `%s' defined for mismatching languages",
2400 t->u.s.tag);
2401 error_at_line (&t->u.s.line, "one structure defined here");
2404 /* Some things may also be defined in the structure's options. */
2405 for (o = t->u.s.opt; o; o = o->next)
2406 if (!desc && strcmp (o->name, "desc") == 0)
2407 desc = o->info;
2409 d->prev_val[2] = oldval;
2410 d->prev_val[1] = oldprevval2;
2411 if (union_p)
2413 if (desc == NULL)
2415 error_at_line (d->line,
2416 "missing `desc' option for union `%s'",
2417 t->u.s.tag);
2418 desc = "1";
2420 oprintf (d->of, "%*sswitch (", d->indent, "");
2421 output_escaped_param (d, desc, "desc");
2422 oprintf (d->of, ")\n");
2423 d->indent += 2;
2424 oprintf (d->of, "%*s{\n", d->indent, "");
2426 for (f = t->u.s.fields; f; f = f->next)
2428 options_p oo;
2429 const char *dot = ".";
2430 const char *tagid = NULL;
2431 int skip_p = 0;
2432 int default_p = 0;
2433 int use_param_p = 0;
2434 char *newval;
2436 d->reorder_fn = NULL;
2437 for (oo = f->opt; oo; oo = oo->next)
2438 if (strcmp (oo->name, "dot") == 0)
2439 dot = oo->info;
2440 else if (strcmp (oo->name, "tag") == 0)
2441 tagid = oo->info;
2442 else if (strcmp (oo->name, "skip") == 0)
2443 skip_p = 1;
2444 else if (strcmp (oo->name, "default") == 0)
2445 default_p = 1;
2446 else if (strcmp (oo->name, "reorder") == 0)
2447 d->reorder_fn = oo->info;
2448 else if (strncmp (oo->name, "use_param", 9) == 0
2449 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2450 use_param_p = 1;
2452 if (skip_p)
2453 continue;
2455 if (union_p && tagid)
2457 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
2458 d->indent += 2;
2460 else if (union_p && default_p)
2462 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2463 d->indent += 2;
2464 seen_default_p = 1;
2466 else if (!union_p && (default_p || tagid))
2467 error_at_line (d->line,
2468 "can't use `%s' outside a union on field `%s'",
2469 default_p ? "default" : "tag", f->name);
2470 else if (union_p && !(default_p || tagid)
2471 && f->type->kind == TYPE_SCALAR)
2473 fprintf (stderr,
2474 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2475 d->line->file, d->line->line, f->name);
2476 continue;
2478 else if (union_p && !(default_p || tagid))
2479 error_at_line (d->line,
2480 "field `%s' is missing `tag' or `default' option",
2481 f->name);
2483 d->line = &f->line;
2484 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
2485 d->opt = f->opt;
2486 d->used_length = false;
2488 if (union_p && use_param_p && d->param == NULL)
2489 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
2490 else
2491 walk_type (f->type, d);
2493 free (newval);
2495 if (union_p)
2497 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2498 d->indent -= 2;
2501 d->reorder_fn = NULL;
2503 d->val = oldval;
2504 d->prev_val[1] = oldprevval1;
2505 d->prev_val[2] = oldprevval2;
2507 if (union_p && !seen_default_p)
2509 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2510 oprintf (d->of, "%*s break;\n", d->indent, "");
2512 if (union_p)
2514 oprintf (d->of, "%*s}\n", d->indent, "");
2515 d->indent -= 2;
2518 break;
2520 case TYPE_LANG_STRUCT:
2522 type_p nt;
2523 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
2524 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
2525 break;
2526 if (nt == NULL)
2527 error_at_line (d->line, "structure `%s' differs between languages",
2528 t->u.s.tag);
2529 else
2530 walk_type (nt, d);
2532 break;
2534 case TYPE_PARAM_STRUCT:
2536 type_p *oldparam = d->param;
2538 d->param = t->u.param_struct.param;
2539 walk_type (t->u.param_struct.stru, d);
2540 d->param = oldparam;
2542 break;
2544 default:
2545 gcc_unreachable ();
2549 /* process_field routine for marking routines. */
2551 static void
2552 write_types_process_field (type_p f, const struct walk_type_data *d)
2554 const struct write_types_data *wtd;
2555 const char *cast = d->needs_cast_p ? "(void *)" : "";
2556 wtd = (const struct write_types_data *) d->cookie;
2558 switch (f->kind)
2560 case TYPE_POINTER:
2561 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
2562 wtd->subfield_marker_routine, cast, d->val);
2563 if (wtd->param_prefix)
2565 oprintf (d->of, ", %s", d->prev_val[3]);
2566 if (d->orig_s)
2568 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
2569 output_mangled_typename (d->of, d->orig_s);
2571 else
2572 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
2574 if (f->u.p->kind == TYPE_PARAM_STRUCT
2575 && f->u.p->u.s.line.file != NULL)
2577 oprintf (d->of, ", gt_e_");
2578 output_mangled_typename (d->of, f);
2580 else if (UNION_OR_STRUCT_P (f) && f->u.p->u.s.line.file != NULL)
2582 oprintf (d->of, ", gt_ggc_e_");
2583 output_mangled_typename (d->of, f);
2585 else
2586 oprintf (d->of, ", gt_types_enum_last");
2588 oprintf (d->of, ");\n");
2589 if (d->reorder_fn && wtd->reorder_note_routine)
2590 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
2591 wtd->reorder_note_routine, cast, d->val,
2592 d->prev_val[3], d->reorder_fn);
2593 break;
2595 case TYPE_STRING:
2596 case TYPE_STRUCT:
2597 case TYPE_UNION:
2598 case TYPE_LANG_STRUCT:
2599 case TYPE_PARAM_STRUCT:
2600 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
2601 output_mangled_typename (d->of, f);
2602 oprintf (d->of, " (%s%s);\n", cast, d->val);
2603 if (d->reorder_fn && wtd->reorder_note_routine)
2604 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
2605 wtd->reorder_note_routine, cast, d->val, cast, d->val,
2606 d->reorder_fn);
2607 break;
2609 case TYPE_SCALAR:
2610 break;
2612 default:
2613 gcc_unreachable ();
2617 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
2619 static void
2620 output_type_enum (outf_p of, type_p s)
2622 if (s->kind == TYPE_PARAM_STRUCT && s->u.param_struct.line.file != NULL)
2624 oprintf (of, ", gt_e_");
2625 output_mangled_typename (of, s);
2627 else if (UNION_OR_STRUCT_P (s) && s->u.s.line.file != NULL)
2629 oprintf (of, ", gt_ggc_e_");
2630 output_mangled_typename (of, s);
2632 else
2633 oprintf (of, ", gt_types_enum_last");
2636 /* Return an output file that is suitable for definitions which can
2637 reference struct S */
2639 static outf_p
2640 get_output_file_for_structure (const_type_p s, type_p *param)
2642 const char *fn = s->u.s.line.file;
2643 int i;
2645 /* This is a hack, and not the good kind either. */
2646 for (i = NUM_PARAM - 1; i >= 0; i--)
2647 if (param && param[i] && param[i]->kind == TYPE_POINTER
2648 && UNION_OR_STRUCT_P (param[i]->u.p))
2649 fn = param[i]->u.p->u.s.line.file;
2651 return get_output_file_with_visibility (fn);
2654 /* For S, a structure that's part of ORIG_S, and using parameters
2655 PARAM, write out a routine that:
2656 - Takes a parameter, a void * but actually of type *S
2657 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
2658 field of S or its substructures and (in some cases) things
2659 that are pointed to by S.
2662 static void
2663 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2664 const struct write_types_data *wtd)
2666 const char *chain_next = NULL;
2667 const char *chain_prev = NULL;
2668 const char *chain_circular = NULL;
2669 const char *mark_hook_name = NULL;
2670 options_p opt;
2671 struct walk_type_data d;
2673 memset (&d, 0, sizeof (d));
2674 d.of = get_output_file_for_structure (s, param);
2676 for (opt = s->u.s.opt; opt; opt = opt->next)
2677 if (strcmp (opt->name, "chain_next") == 0)
2678 chain_next = opt->info;
2679 else if (strcmp (opt->name, "chain_prev") == 0)
2680 chain_prev = opt->info;
2681 else if (strcmp (opt->name, "chain_circular") == 0)
2682 chain_circular = opt->info;
2683 else if (strcmp (opt->name, "mark_hook") == 0)
2684 mark_hook_name = opt->info;
2686 if (chain_prev != NULL && chain_next == NULL)
2687 error_at_line (&s->u.s.line, "chain_prev without chain_next");
2688 if (chain_circular != NULL && chain_next != NULL)
2689 error_at_line (&s->u.s.line, "chain_circular with chain_next");
2690 if (chain_circular != NULL)
2691 chain_next = chain_circular;
2693 d.process_field = write_types_process_field;
2694 d.cookie = wtd;
2695 d.orig_s = orig_s;
2696 d.opt = s->u.s.opt;
2697 d.line = &s->u.s.line;
2698 d.bitmap = s->u.s.bitmap;
2699 d.param = param;
2700 d.prev_val[0] = "*x";
2701 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
2702 d.prev_val[3] = "x";
2703 d.val = "(*x)";
2705 oprintf (d.of, "\n");
2706 oprintf (d.of, "void\n");
2707 if (param == NULL)
2708 oprintf (d.of, "gt_%sx_%s", wtd->prefix, orig_s->u.s.tag);
2709 else
2711 oprintf (d.of, "gt_%s_", wtd->prefix);
2712 output_mangled_typename (d.of, orig_s);
2714 oprintf (d.of, " (void *x_p)\n");
2715 oprintf (d.of, "{\n");
2716 oprintf (d.of, " %s %s * %sx = (%s %s *)x_p;\n",
2717 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
2718 chain_next == NULL ? "const " : "",
2719 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2720 if (chain_next != NULL)
2721 oprintf (d.of, " %s %s * xlimit = x;\n",
2722 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2723 if (chain_next == NULL)
2725 oprintf (d.of, " if (%s (x", wtd->marker_routine);
2726 if (wtd->param_prefix)
2728 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
2729 output_mangled_typename (d.of, orig_s);
2730 output_type_enum (d.of, orig_s);
2732 oprintf (d.of, "))\n");
2734 else
2736 if (chain_circular != NULL)
2737 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
2738 else
2739 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
2740 if (wtd->param_prefix)
2742 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
2743 output_mangled_typename (d.of, orig_s);
2744 output_type_enum (d.of, orig_s);
2746 oprintf (d.of, "))\n");
2747 if (chain_circular != NULL)
2748 oprintf (d.of, " return;\n do\n");
2749 if (mark_hook_name && !wtd->skip_hooks)
2751 oprintf (d.of, " {\n");
2752 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
2754 oprintf (d.of, " xlimit = (");
2755 d.prev_val[2] = "*xlimit";
2756 output_escaped_param (&d, chain_next, "chain_next");
2757 oprintf (d.of, ");\n");
2758 if (mark_hook_name && !wtd->skip_hooks)
2759 oprintf (d.of, " }\n");
2760 if (chain_prev != NULL)
2762 oprintf (d.of, " if (x != xlimit)\n");
2763 oprintf (d.of, " for (;;)\n");
2764 oprintf (d.of, " {\n");
2765 oprintf (d.of, " %s %s * const xprev = (",
2766 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
2768 d.prev_val[2] = "*x";
2769 output_escaped_param (&d, chain_prev, "chain_prev");
2770 oprintf (d.of, ");\n");
2771 oprintf (d.of, " if (xprev == NULL) break;\n");
2772 oprintf (d.of, " x = xprev;\n");
2773 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
2774 if (wtd->param_prefix)
2776 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
2777 output_mangled_typename (d.of, orig_s);
2778 output_type_enum (d.of, orig_s);
2780 oprintf (d.of, ");\n");
2781 oprintf (d.of, " }\n");
2783 if (chain_circular != NULL)
2785 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
2786 if (wtd->param_prefix)
2788 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
2789 output_mangled_typename (d.of, orig_s);
2790 output_type_enum (d.of, orig_s);
2792 oprintf (d.of, "));\n");
2793 if (mark_hook_name && !wtd->skip_hooks)
2794 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
2795 oprintf (d.of, " do\n");
2797 else
2798 oprintf (d.of, " while (x != xlimit)\n");
2800 oprintf (d.of, " {\n");
2801 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
2803 oprintf (d.of, " %s (x);\n", mark_hook_name);
2805 d.prev_val[2] = "*x";
2806 d.indent = 6;
2807 walk_type (s, &d);
2809 if (chain_next != NULL)
2811 oprintf (d.of, " x = (");
2812 output_escaped_param (&d, chain_next, "chain_next");
2813 oprintf (d.of, ");\n");
2816 oprintf (d.of, " }\n");
2817 if (chain_circular != NULL)
2818 oprintf (d.of, " while (x != xlimit);\n");
2819 oprintf (d.of, "}\n");
2822 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
2824 static void
2825 write_types (outf_p output_header, type_p structures, type_p param_structs,
2826 const struct write_types_data *wtd)
2828 int nbfun = 0; /* Count the emitted functions. */
2829 type_p s;
2831 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
2832 /* We first emit the macros and the declarations. Functions' code is
2833 emitted afterwards. This is needed in plugin mode. */
2834 oprintf (output_header, "/* macros and declarations */\n");
2835 for (s = structures; s; s = s->next)
2836 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
2838 options_p opt;
2840 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
2841 continue;
2843 oprintf (output_header, "#define gt_%s_", wtd->prefix);
2844 output_mangled_typename (output_header, s);
2845 oprintf (output_header, "(X) do { \\\n");
2846 oprintf (output_header,
2847 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
2848 s->u.s.tag);
2849 oprintf (output_header, " } while (0)\n");
2851 for (opt = s->u.s.opt; opt; opt = opt->next)
2852 if (strcmp (opt->name, "ptr_alias") == 0)
2854 const_type_p const t = (const_type_p) opt->info;
2855 if (t->kind == TYPE_STRUCT
2856 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
2857 oprintf (output_header,
2858 "#define gt_%sx_%s gt_%sx_%s\n",
2859 wtd->prefix, s->u.s.tag, wtd->prefix, t->u.s.tag);
2860 else
2861 error_at_line (&s->u.s.line,
2862 "structure alias is not a structure");
2863 break;
2865 if (opt)
2866 continue;
2868 /* Declare the marker procedure only once. */
2869 oprintf (output_header,
2870 "extern void gt_%sx_%s (void *);\n",
2871 wtd->prefix, s->u.s.tag);
2873 if (s->u.s.line.file == NULL)
2875 fprintf (stderr, "warning: structure `%s' used but not defined\n",
2876 s->u.s.tag);
2877 continue;
2881 for (s = param_structs; s; s = s->next)
2882 if (s->gc_used == GC_POINTED_TO)
2884 type_p stru = s->u.param_struct.stru;
2886 /* Declare the marker procedure. */
2887 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
2888 output_mangled_typename (output_header, s);
2889 oprintf (output_header, " (void *);\n");
2891 if (stru->u.s.line.file == NULL)
2893 fprintf (stderr, "warning: structure `%s' used but not defined\n",
2894 s->u.s.tag);
2895 continue;
2899 /* At last we emit the functions code. */
2900 oprintf (output_header, "\n/* functions code */\n");
2901 for (s = structures; s; s = s->next)
2902 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
2904 options_p opt;
2906 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
2907 continue;
2908 for (opt = s->u.s.opt; opt; opt = opt->next)
2909 if (strcmp (opt->name, "ptr_alias") == 0)
2910 break;
2911 if (opt)
2912 continue;
2914 if (s->kind == TYPE_LANG_STRUCT)
2916 type_p ss;
2917 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
2919 nbfun++;
2920 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
2921 nbfun, (void*) ss, ss->u.s.tag);
2922 write_func_for_structure (s, ss, NULL, wtd);
2925 else
2927 nbfun++;
2928 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
2929 nbfun, (void*) s, s->u.s.tag);
2930 write_func_for_structure (s, s, NULL, wtd);
2933 else
2935 /* Structure s is not possibly pointed to, so can be ignored. */
2936 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
2937 (void*)s, s->u.s.tag,
2938 (int) s->gc_used);
2941 for (s = param_structs; s; s = s->next)
2942 if (s->gc_used == GC_POINTED_TO)
2944 type_p *param = s->u.param_struct.param;
2945 type_p stru = s->u.param_struct.stru;
2946 if (stru->u.s.line.file == NULL)
2947 continue;
2948 if (stru->kind == TYPE_LANG_STRUCT)
2950 type_p ss;
2951 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
2953 nbfun++;
2954 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
2955 nbfun, (void*) ss, ss->u.s.tag);
2956 write_func_for_structure (s, ss, param, wtd);
2959 else
2961 nbfun++;
2962 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
2963 nbfun, (void*) s,
2964 (void*) stru, stru->u.s.tag);
2965 write_func_for_structure (s, stru, param, wtd);
2968 else
2970 /* Param structure s is not pointed to, so should be ignored. */
2971 DBGPRINTF ("ignored s @ %p", (void*)s);
2973 if (verbosity_level >= 2)
2974 printf ("%s emitted %d routines for %s\n",
2975 progname, nbfun, wtd->comment);
2978 static const struct write_types_data ggc_wtd = {
2979 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
2980 "GC marker procedures. ",
2981 FALSE
2984 static const struct write_types_data pch_wtd = {
2985 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
2986 "gt_pch_note_reorder",
2987 "PCH type-walking procedures. ",
2988 TRUE
2991 /* Write out the local pointer-walking routines. */
2993 /* process_field routine for local pointer-walking. */
2995 static void
2996 write_types_local_process_field (type_p f, const struct walk_type_data *d)
2998 switch (f->kind)
3000 case TYPE_POINTER:
3001 case TYPE_STRUCT:
3002 case TYPE_UNION:
3003 case TYPE_LANG_STRUCT:
3004 case TYPE_PARAM_STRUCT:
3005 case TYPE_STRING:
3006 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3007 d->prev_val[3]);
3008 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3009 break;
3011 case TYPE_SCALAR:
3012 break;
3014 default:
3015 gcc_unreachable ();
3019 /* For S, a structure that's part of ORIG_S, and using parameters
3020 PARAM, write out a routine that:
3021 - Is of type gt_note_pointers
3022 - Calls PROCESS_FIELD on each field of S or its substructures.
3025 static void
3026 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3028 struct walk_type_data d;
3030 memset (&d, 0, sizeof (d));
3031 d.of = get_output_file_for_structure (s, param);
3032 d.process_field = write_types_local_process_field;
3033 d.opt = s->u.s.opt;
3034 d.line = &s->u.s.line;
3035 d.bitmap = s->u.s.bitmap;
3036 d.param = param;
3037 d.prev_val[0] = d.prev_val[2] = "*x";
3038 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3039 d.prev_val[3] = "x";
3040 d.val = "(*x)";
3041 d.fn_wants_lvalue = true;
3043 oprintf (d.of, "\n");
3044 oprintf (d.of, "void\n");
3045 oprintf (d.of, "gt_pch_p_");
3046 output_mangled_typename (d.of, orig_s);
3047 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3048 "\tvoid *x_p,\n"
3049 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3050 "\tATTRIBUTE_UNUSED void *cookie)\n");
3051 oprintf (d.of, "{\n");
3052 oprintf (d.of, " %s %s * const x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3053 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3054 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3055 d.indent = 2;
3056 walk_type (s, &d);
3057 oprintf (d.of, "}\n");
3060 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3062 static void
3063 write_local (outf_p output_header, type_p structures, type_p param_structs)
3065 type_p s;
3067 if (!output_header)
3068 return;
3069 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3070 for (s = structures; s; s = s->next)
3071 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3073 options_p opt;
3075 if (s->u.s.line.file == NULL)
3076 continue;
3078 for (opt = s->u.s.opt; opt; opt = opt->next)
3079 if (strcmp (opt->name, "ptr_alias") == 0)
3081 const_type_p const t = (const_type_p) opt->info;
3082 if (t->kind == TYPE_STRUCT
3083 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3085 oprintf (output_header, "#define gt_pch_p_");
3086 output_mangled_typename (output_header, s);
3087 oprintf (output_header, " gt_pch_p_");
3088 output_mangled_typename (output_header, t);
3089 oprintf (output_header, "\n");
3091 else
3092 error_at_line (&s->u.s.line,
3093 "structure alias is not a structure");
3094 break;
3096 if (opt)
3097 continue;
3099 /* Declare the marker procedure only once. */
3100 oprintf (output_header, "extern void gt_pch_p_");
3101 output_mangled_typename (output_header, s);
3102 oprintf (output_header,
3103 "\n (void *, void *, gt_pointer_operator, void *);\n");
3105 if (s->kind == TYPE_LANG_STRUCT)
3107 type_p ss;
3108 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3109 write_local_func_for_structure (s, ss, NULL);
3111 else
3112 write_local_func_for_structure (s, s, NULL);
3115 for (s = param_structs; s; s = s->next)
3116 if (s->gc_used == GC_POINTED_TO)
3118 type_p *param = s->u.param_struct.param;
3119 type_p stru = s->u.param_struct.stru;
3121 /* Declare the marker procedure. */
3122 oprintf (output_header, "extern void gt_pch_p_");
3123 output_mangled_typename (output_header, s);
3124 oprintf (output_header,
3125 "\n (void *, void *, gt_pointer_operator, void *);\n");
3127 if (stru->u.s.line.file == NULL)
3129 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3130 s->u.s.tag);
3131 continue;
3134 if (stru->kind == TYPE_LANG_STRUCT)
3136 type_p ss;
3137 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3138 write_local_func_for_structure (s, ss, param);
3140 else
3141 write_local_func_for_structure (s, stru, param);
3145 /* Nonzero if S is a type for which typed GC allocators should be output. */
3147 #define USED_BY_TYPED_GC_P(s) \
3148 (((s->kind == TYPE_POINTER) \
3149 && ((s->u.p->gc_used == GC_POINTED_TO) \
3150 || (s->u.p->gc_used == GC_USED))) \
3151 || (UNION_OR_STRUCT_P (s) && \
3152 (((s)->gc_used == GC_POINTED_TO) \
3153 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
3154 && s->u.s.line.file != NULL) \
3155 || ((s)->gc_used == GC_USED \
3156 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
3159 /* Write out the 'enum' definition for gt_types_enum. */
3161 static void
3162 write_enum_defn (type_p structures, type_p param_structs)
3164 type_p s;
3165 int nbstruct = 0;
3166 int nbparamstruct = 0;
3168 if (!header_file)
3169 return;
3170 oprintf (header_file, "\n/* Enumeration of types known. */\n");
3171 oprintf (header_file, "enum gt_types_enum {\n");
3172 for (s = structures; s; s = s->next)
3173 if (USED_BY_TYPED_GC_P (s))
3175 nbstruct++;
3176 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
3177 (void*) s, nbstruct);
3178 if (UNION_OR_STRUCT_P (s))
3179 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
3180 (void*) s, nbstruct, s->u.s.tag);
3181 oprintf (header_file, " gt_ggc_e_");
3182 output_mangled_typename (header_file, s);
3183 oprintf (header_file, ",\n");
3185 for (s = param_structs; s; s = s->next)
3186 if (s->gc_used == GC_POINTED_TO)
3188 nbparamstruct++;
3189 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
3190 (void*) s, nbparamstruct);
3191 oprintf (header_file, " gt_e_");
3192 output_mangled_typename (header_file, s);
3193 oprintf (header_file, ",\n");
3195 oprintf (header_file, " gt_types_enum_last\n");
3196 oprintf (header_file, "};\n");
3197 if (verbosity_level >= 2)
3198 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
3199 progname, nbstruct, nbparamstruct);
3203 /* Might T contain any non-pointer elements? */
3205 static int
3206 contains_scalar_p (type_p t)
3208 switch (t->kind)
3210 case TYPE_STRING:
3211 case TYPE_POINTER:
3212 return 0;
3213 case TYPE_ARRAY:
3214 return contains_scalar_p (t->u.a.p);
3215 default:
3216 /* Could also check for structures that have no non-pointer
3217 fields, but there aren't enough of those to worry about. */
3218 return 1;
3222 /* Mangle FN and print it to F. */
3224 static void
3225 put_mangled_filename (outf_p f, const char *fn)
3227 const char *name = get_output_file_name (fn);
3228 if (!f || !name)
3229 return;
3230 for (; *name != 0; name++)
3231 if (ISALNUM (*name))
3232 oprintf (f, "%c", *name);
3233 else
3234 oprintf (f, "%c", '_');
3237 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
3238 LASTNAME, and NAME are all strings to insert in various places in
3239 the resulting code. */
3241 static void
3242 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
3243 const char *tname, const char *name)
3245 struct flist *fli2;
3247 for (fli2 = flp; fli2; fli2 = fli2->next)
3248 if (fli2->started_p)
3250 oprintf (fli2->f, " %s\n", lastname);
3251 oprintf (fli2->f, "};\n\n");
3254 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
3255 if (fli2->started_p)
3257 lang_bitmap bitmap = get_lang_bitmap (fli2->name);
3258 int fnum;
3260 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
3261 if (bitmap & 1)
3263 oprintf (base_files[fnum],
3264 "extern const struct %s gt_%s_", tname, pfx);
3265 put_mangled_filename (base_files[fnum], fli2->name);
3266 oprintf (base_files[fnum], "[];\n");
3271 size_t fnum;
3272 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3273 oprintf (base_files[fnum],
3274 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
3278 for (fli2 = flp; fli2; fli2 = fli2->next)
3279 if (fli2->started_p)
3281 lang_bitmap bitmap = get_lang_bitmap (fli2->name);
3282 int fnum;
3284 fli2->started_p = 0;
3286 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
3287 if (bitmap & 1)
3289 oprintf (base_files[fnum], " gt_%s_", pfx);
3290 put_mangled_filename (base_files[fnum], fli2->name);
3291 oprintf (base_files[fnum], ",\n");
3296 size_t fnum;
3297 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3299 oprintf (base_files[fnum], " NULL\n");
3300 oprintf (base_files[fnum], "};\n");
3305 /* Write the first three fields (pointer, count and stride) for
3306 root NAME to F. V and LINE are as for write_root.
3308 Return true if the entry could be written; return false on error. */
3310 static bool
3311 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
3313 type_p ap;
3315 if (!v)
3317 error_at_line (line, "`%s' is too complex to be a root", name);
3318 return false;
3321 oprintf (f, " {\n");
3322 oprintf (f, " &%s,\n", name);
3323 oprintf (f, " 1");
3325 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3326 if (ap->u.a.len[0])
3327 oprintf (f, " * (%s)", ap->u.a.len);
3328 else if (ap == v->type)
3329 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
3330 oprintf (f, ",\n");
3331 oprintf (f, " sizeof (%s", v->name);
3332 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3333 oprintf (f, "[0]");
3334 oprintf (f, "),\n");
3335 return true;
3338 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
3339 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
3340 of the caller. */
3342 static void
3343 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
3344 int has_length, struct fileloc *line, const char *if_marked,
3345 bool emit_pch, type_p field_type, const char *field_name)
3347 /* If the field reference is relative to V, rather than to some
3348 subcomponent of V, we can mark any subarrays with a single stride.
3349 We're effectively treating the field as a global variable in its
3350 own right. */
3351 if (v && type == v->type)
3353 struct pair newv;
3355 newv = *v;
3356 newv.type = field_type;
3357 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
3358 v = &newv;
3360 /* Otherwise, any arrays nested in the structure are too complex to
3361 handle. */
3362 else if (field_type->kind == TYPE_ARRAY)
3363 v = NULL;
3364 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
3365 has_length, line, if_marked, emit_pch);
3368 /* Write out to F the table entry and any marker routines needed to
3369 mark NAME as TYPE. V can be one of three values:
3371 - null, if NAME is too complex to represent using a single
3372 count and stride. In this case, it is an error for NAME to
3373 contain any gc-ed data.
3375 - the outermost array that contains NAME, if NAME is part of an array.
3377 - the C variable that contains NAME, if NAME is not part of an array.
3379 LINE is the line of the C source that declares the root variable.
3380 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
3381 is nonzero iff we are building the root table for hash table caches. */
3383 static void
3384 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
3385 struct fileloc *line, const char *if_marked, bool emit_pch)
3387 switch (type->kind)
3389 case TYPE_STRUCT:
3391 pair_p fld;
3392 for (fld = type->u.s.fields; fld; fld = fld->next)
3394 int skip_p = 0;
3395 const char *desc = NULL;
3396 options_p o;
3398 for (o = fld->opt; o; o = o->next)
3399 if (strcmp (o->name, "skip") == 0)
3400 skip_p = 1;
3401 else if (strcmp (o->name, "desc") == 0)
3402 desc = o->info;
3403 else if (strcmp (o->name, "param_is") == 0)
3405 else
3406 error_at_line (line,
3407 "field `%s' of global `%s' has unknown option `%s'",
3408 fld->name, name, o->name);
3410 if (skip_p)
3411 continue;
3412 else if (desc && fld->type->kind == TYPE_UNION)
3414 pair_p validf = NULL;
3415 pair_p ufld;
3417 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
3419 const char *tag = NULL;
3420 options_p oo;
3422 for (oo = ufld->opt; oo; oo = oo->next)
3423 if (strcmp (oo->name, "tag") == 0)
3424 tag = oo->info;
3425 if (tag == NULL || strcmp (tag, desc) != 0)
3426 continue;
3427 if (validf != NULL)
3428 error_at_line (line,
3429 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
3430 name, fld->name, validf->name,
3431 name, fld->name, ufld->name, tag);
3432 validf = ufld;
3434 if (validf != NULL)
3435 write_field_root (f, v, type, name, 0, line, if_marked,
3436 emit_pch, validf->type,
3437 ACONCAT ((fld->name, ".",
3438 validf->name, NULL)));
3440 else if (desc)
3441 error_at_line (line,
3442 "global `%s.%s' has `desc' option but is not union",
3443 name, fld->name);
3444 else
3445 write_field_root (f, v, type, name, 0, line, if_marked,
3446 emit_pch, fld->type, fld->name);
3449 break;
3451 case TYPE_ARRAY:
3453 char *newname;
3454 newname = xasprintf ("%s[0]", name);
3455 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
3456 emit_pch);
3457 free (newname);
3459 break;
3461 case TYPE_POINTER:
3463 type_p tp;
3465 if (!start_root_entry (f, v, name, line))
3466 return;
3468 tp = type->u.p;
3470 if (!has_length && UNION_OR_STRUCT_P (tp))
3472 oprintf (f, " &gt_ggc_mx_%s,\n", tp->u.s.tag);
3473 if (emit_pch)
3474 oprintf (f, " &gt_pch_nx_%s", tp->u.s.tag);
3475 else
3476 oprintf (f, " NULL");
3478 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
3480 oprintf (f, " &gt_ggc_m_");
3481 output_mangled_typename (f, tp);
3482 if (emit_pch)
3484 oprintf (f, ",\n &gt_pch_n_");
3485 output_mangled_typename (f, tp);
3487 else
3488 oprintf (f, ",\n NULL");
3490 else if (has_length
3491 && (tp->kind == TYPE_POINTER || UNION_OR_STRUCT_P (tp)))
3493 oprintf (f, " &gt_ggc_ma_%s,\n", name);
3494 if (emit_pch)
3495 oprintf (f, " &gt_pch_na_%s", name);
3496 else
3497 oprintf (f, " NULL");
3499 else
3501 error_at_line (line,
3502 "global `%s' is pointer to unimplemented type",
3503 name);
3505 if (if_marked)
3506 oprintf (f, ",\n &%s", if_marked);
3507 oprintf (f, "\n },\n");
3509 break;
3511 case TYPE_STRING:
3513 if (!start_root_entry (f, v, name, line))
3514 return;
3516 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
3517 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
3518 oprintf (f, " },\n");
3520 break;
3522 case TYPE_SCALAR:
3523 break;
3525 default:
3526 error_at_line (line, "global `%s' is unimplemented type", name);
3530 /* This generates a routine to walk an array. */
3532 static void
3533 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
3535 struct walk_type_data d;
3536 char *prevval3;
3538 memset (&d, 0, sizeof (d));
3539 d.of = f;
3540 d.cookie = wtd;
3541 d.indent = 2;
3542 d.line = &v->line;
3543 d.opt = v->opt;
3544 d.bitmap = get_lang_bitmap (v->line.file);
3545 d.param = NULL;
3547 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
3549 if (wtd->param_prefix)
3551 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
3552 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
3553 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
3554 wtd->param_prefix, v->name);
3555 oprintf (d.of,
3556 " ATTRIBUTE_UNUSED void *x_p,\n"
3557 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3558 " ATTRIBUTE_UNUSED void * cookie)\n");
3559 oprintf (d.of, "{\n");
3560 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3561 d.process_field = write_types_local_process_field;
3562 walk_type (v->type, &d);
3563 oprintf (f, "}\n\n");
3566 d.opt = v->opt;
3567 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
3568 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
3569 wtd->prefix, v->name);
3570 oprintf (f, "{\n");
3571 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3572 d.process_field = write_types_process_field;
3573 walk_type (v->type, &d);
3574 free (prevval3);
3575 oprintf (f, "}\n\n");
3578 /* Output a table describing the locations and types of VARIABLES. */
3580 static void
3581 write_roots (pair_p variables, bool emit_pch)
3583 pair_p v;
3584 struct flist *flp = NULL;
3586 for (v = variables; v; v = v->next)
3588 outf_p f = get_output_file_with_visibility (v->line.file);
3589 struct flist *fli;
3590 const char *length = NULL;
3591 int deletable_p = 0;
3592 options_p o;
3594 for (o = v->opt; o; o = o->next)
3595 if (strcmp (o->name, "length") == 0)
3596 length = o->info;
3597 else if (strcmp (o->name, "deletable") == 0)
3598 deletable_p = 1;
3599 else if (strcmp (o->name, "param_is") == 0)
3601 else if (strncmp (o->name, "param", 5) == 0
3602 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
3604 else if (strcmp (o->name, "if_marked") == 0)
3606 else
3607 error_at_line (&v->line,
3608 "global `%s' has unknown option `%s'",
3609 v->name, o->name);
3611 for (fli = flp; fli; fli = fli->next)
3612 if (fli->f == f && f)
3613 break;
3614 if (fli == NULL)
3616 fli = XNEW (struct flist);
3617 fli->f = f;
3618 fli->next = flp;
3619 fli->started_p = 0;
3620 fli->name = v->line.file;
3621 gcc_assert (fli->name);
3622 flp = fli;
3624 oprintf (f, "\n/* GC roots. */\n\n");
3627 if (!deletable_p
3628 && length
3629 && v->type->kind == TYPE_POINTER
3630 && (v->type->u.p->kind == TYPE_POINTER
3631 || v->type->u.p->kind == TYPE_STRUCT))
3633 write_array (f, v, &ggc_wtd);
3634 write_array (f, v, &pch_wtd);
3638 for (v = variables; v; v = v->next)
3640 outf_p f = get_output_file_with_visibility (v->line.file);
3641 struct flist *fli;
3642 int skip_p = 0;
3643 int length_p = 0;
3644 options_p o;
3646 for (o = v->opt; o; o = o->next)
3647 if (strcmp (o->name, "length") == 0)
3648 length_p = 1;
3649 else if (strcmp (o->name, "deletable") == 0
3650 || strcmp (o->name, "if_marked") == 0)
3651 skip_p = 1;
3653 if (skip_p)
3654 continue;
3656 for (fli = flp; fli; fli = fli->next)
3657 if (fli->f == f)
3658 break;
3659 if (!fli->started_p)
3661 fli->started_p = 1;
3663 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
3664 put_mangled_filename (f, v->line.file);
3665 oprintf (f, "[] = {\n");
3668 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
3671 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3672 "gt_ggc_rtab");
3674 for (v = variables; v; v = v->next)
3676 outf_p f = get_output_file_with_visibility (v->line.file);
3677 struct flist *fli;
3678 int skip_p = 1;
3679 options_p o;
3681 for (o = v->opt; o; o = o->next)
3682 if (strcmp (o->name, "deletable") == 0)
3683 skip_p = 0;
3684 else if (strcmp (o->name, "if_marked") == 0)
3685 skip_p = 1;
3687 if (skip_p)
3688 continue;
3690 for (fli = flp; fli; fli = fli->next)
3691 if (fli->f == f)
3692 break;
3693 if (!fli->started_p)
3695 fli->started_p = 1;
3697 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
3698 put_mangled_filename (f, v->line.file);
3699 oprintf (f, "[] = {\n");
3702 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3703 v->name, v->name);
3706 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3707 "gt_ggc_deletable_rtab");
3709 for (v = variables; v; v = v->next)
3711 outf_p f = get_output_file_with_visibility (v->line.file);
3712 struct flist *fli;
3713 const char *if_marked = NULL;
3714 int length_p = 0;
3715 options_p o;
3717 for (o = v->opt; o; o = o->next)
3718 if (strcmp (o->name, "length") == 0)
3719 length_p = 1;
3720 else if (strcmp (o->name, "if_marked") == 0)
3721 if_marked = o->info;
3723 if (if_marked == NULL)
3724 continue;
3726 if (v->type->kind != TYPE_POINTER
3727 || v->type->u.p->kind != TYPE_PARAM_STRUCT
3728 || v->type->u.p->u.param_struct.stru != find_structure ("htab", 0))
3730 error_at_line (&v->line,
3731 "if_marked option used but not hash table");
3732 continue;
3735 for (fli = flp; fli; fli = fli->next)
3736 if (fli->f == f)
3737 break;
3738 if (!fli->started_p)
3740 fli->started_p = 1;
3742 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
3743 put_mangled_filename (f, v->line.file);
3744 oprintf (f, "[] = {\n");
3747 write_root (f, v, v->type->u.p->u.param_struct.param[0],
3748 v->name, length_p, &v->line, if_marked, emit_pch);
3751 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
3752 "gt_ggc_cache_rtab");
3754 if (!emit_pch)
3755 return;
3757 for (v = variables; v; v = v->next)
3759 outf_p f = get_output_file_with_visibility (v->line.file);
3760 struct flist *fli;
3761 int length_p = 0;
3762 int if_marked_p = 0;
3763 options_p o;
3765 for (o = v->opt; o; o = o->next)
3766 if (strcmp (o->name, "length") == 0)
3767 length_p = 1;
3768 else if (strcmp (o->name, "if_marked") == 0)
3769 if_marked_p = 1;
3771 if (!if_marked_p)
3772 continue;
3774 for (fli = flp; fli; fli = fli->next)
3775 if (fli->f == f)
3776 break;
3777 if (!fli->started_p)
3779 fli->started_p = 1;
3781 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
3782 put_mangled_filename (f, v->line.file);
3783 oprintf (f, "[] = {\n");
3786 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
3789 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3790 "gt_pch_cache_rtab");
3792 for (v = variables; v; v = v->next)
3794 outf_p f = get_output_file_with_visibility (v->line.file);
3795 struct flist *fli;
3796 int skip_p = 0;
3797 options_p o;
3799 for (o = v->opt; o; o = o->next)
3800 if (strcmp (o->name, "deletable") == 0
3801 || strcmp (o->name, "if_marked") == 0)
3802 skip_p = 1;
3804 if (skip_p)
3805 continue;
3807 if (!contains_scalar_p (v->type))
3808 continue;
3810 for (fli = flp; fli; fli = fli->next)
3811 if (fli->f == f)
3812 break;
3813 if (!fli->started_p)
3815 fli->started_p = 1;
3817 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
3818 put_mangled_filename (f, v->line.file);
3819 oprintf (f, "[] = {\n");
3822 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
3823 v->name, v->name);
3826 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
3827 "gt_pch_scalar_rtab");
3830 /* Record the definition of a generic VEC structure, as if we had expanded
3831 the macros in vec.h:
3833 typedef struct VEC_<type>_base GTY(()) {
3834 unsigned num;
3835 unsigned alloc;
3836 <type> GTY((length ("%h.num"))) vec[1];
3837 } VEC_<type>_base
3839 where the GTY(()) tags are only present if is_scalar is _false_. */
3841 void
3842 note_def_vec (const char *type_name, bool is_scalar, struct fileloc *pos)
3844 pair_p fields;
3845 type_p t;
3846 options_p o;
3847 type_p len_ty = create_scalar_type ("unsigned");
3848 const char *name = concat ("VEC_", type_name, "_base", (char *) 0);
3850 if (is_scalar)
3852 t = create_scalar_type (type_name);
3853 o = 0;
3855 else
3857 t = resolve_typedef (type_name, pos);
3858 o = create_option (0, "length", "%h.num");
3861 /* We assemble the field list in reverse order. */
3862 fields = create_field_at (0, create_array (t, "1"), "vec", o, pos);
3863 fields = create_field_at (fields, len_ty, "alloc", 0, pos);
3864 fields = create_field_at (fields, len_ty, "num", 0, pos);
3866 do_typedef (name, new_structure (name, 0, pos, fields, 0), pos);
3869 /* Record the definition of an allocation-specific VEC structure, as if
3870 we had expanded the macros in vec.h:
3872 typedef struct VEC_<type>_<astrat> {
3873 VEC_<type>_base base;
3874 } VEC_<type>_<astrat>;
3876 void
3877 note_def_vec_alloc (const char *type, const char *astrat, struct fileloc *pos)
3879 const char *astratname = concat ("VEC_", type, "_", astrat, (char *) 0);
3880 const char *basename = concat ("VEC_", type, "_base", (char *) 0);
3882 pair_p field = create_field_at (0, resolve_typedef (basename, pos),
3883 "base", 0, pos);
3885 do_typedef (astratname, new_structure (astratname, 0, pos, field, 0), pos);
3888 /* Returns the specifier keyword for a string or union type S, empty string
3889 otherwise. */
3891 static const char *
3892 get_type_specifier (const type_p s)
3894 if (s->kind == TYPE_STRUCT || s->kind == TYPE_LANG_STRUCT)
3895 return "struct ";
3896 if (s->kind == TYPE_UNION)
3897 return "union ";
3898 return "";
3901 /* TRUE if type S has the GTY variable_size annotation. */
3903 static bool
3904 variable_size_p (const type_p s)
3906 options_p o;
3907 for (o = s->u.s.opt; o; o = o->next)
3908 if (strcmp (o->name, "variable_size") == 0)
3909 return true;
3910 return false;
3913 enum alloc_quantity
3914 { single, vector };
3915 enum alloc_zone
3916 { any_zone, specific_zone };
3918 /* Writes one typed allocator definition for type identifier TYPE_NAME with
3919 optional type specifier TYPE_SPECIFIER. The allocator name will contain
3920 ALLOCATOR_TYPE. If VARIABLE_SIZE is true, the allocator will have an extra
3921 parameter specifying number of bytes to allocate. If QUANTITY is set to
3922 VECTOR, a vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
3923 the allocator will be zone-specific. */
3925 static void
3926 write_typed_alloc_def (bool variable_size, const char *type_specifier,
3927 const char *type_name, const char *allocator_type,
3928 enum alloc_quantity quantity, enum alloc_zone zone)
3930 bool two_args = variable_size && (quantity == vector);
3931 bool third_arg = ((zone == specific_zone)
3932 && (variable_size || (quantity == vector)));
3934 oprintf (header_file, "#define ggc_alloc_%s%s", allocator_type, type_name);
3935 oprintf (header_file, "(%s%s%s%s%s) ",
3936 (variable_size ? "SIZE" : ""),
3937 (two_args ? ", " : ""),
3938 (quantity == vector) ? "n" : "",
3939 (third_arg ? ", " : ""), (zone == specific_zone) ? "z" : "");
3940 oprintf (header_file, "((%s%s *)", type_specifier, type_name);
3941 oprintf (header_file, "(ggc_internal_%salloc_stat (", allocator_type);
3942 if (zone == specific_zone)
3943 oprintf (header_file, "z, ");
3944 if (variable_size)
3945 oprintf (header_file, "SIZE");
3946 else
3947 oprintf (header_file, "sizeof (%s%s)", type_specifier, type_name);
3948 if (quantity == vector)
3949 oprintf (header_file, ", n");
3950 oprintf (header_file, " MEM_STAT_INFO)))\n");
3953 /* Writes a typed allocator definition for a struct or union S. */
3955 static void
3956 write_typed_struct_alloc_def (const type_p s, const char *allocator_type,
3957 enum alloc_quantity quantity,
3958 enum alloc_zone zone)
3960 write_typed_alloc_def (variable_size_p (s), get_type_specifier (s),
3961 s->u.s.tag, allocator_type, quantity, zone);
3964 /* Writes a typed allocator definition for a typedef P. */
3966 static void
3967 write_typed_typedef_alloc_def (const pair_p p, const char *allocator_type,
3968 enum alloc_quantity quantity,
3969 enum alloc_zone zone)
3971 write_typed_alloc_def (variable_size_p (p->type), "", p->name,
3972 allocator_type, quantity, zone);
3975 /* Writes typed allocator definitions for the types in STRUCTURES and
3976 TYPEDEFS that are used by GC. */
3978 static void
3979 write_typed_alloc_defns (const type_p structures, const pair_p typedefs)
3981 type_p s;
3982 pair_p p;
3984 oprintf (header_file,
3985 "\n/* Allocators for known structs and unions. */\n\n");
3986 for (s = structures; s; s = s->next)
3988 if (!USED_BY_TYPED_GC_P (s))
3989 continue;
3990 write_typed_struct_alloc_def (s, "", single, any_zone);
3991 write_typed_struct_alloc_def (s, "cleared_", single, any_zone);
3992 write_typed_struct_alloc_def (s, "vec_", vector, any_zone);
3993 write_typed_struct_alloc_def (s, "cleared_vec_", vector, any_zone);
3994 write_typed_struct_alloc_def (s, "zone_", single, specific_zone);
3995 write_typed_struct_alloc_def (s, "zone_cleared_", single,
3996 specific_zone);
3997 write_typed_struct_alloc_def (s, "zone_vec_", vector, specific_zone);
3998 write_typed_struct_alloc_def (s, "zone_cleared_vec_", vector,
3999 specific_zone);
4002 oprintf (header_file, "\n/* Allocators for known typedefs. */\n");
4003 for (p = typedefs; p; p = p->next)
4005 s = p->type;
4006 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4007 continue;
4008 write_typed_typedef_alloc_def (p, "", single, any_zone);
4009 write_typed_typedef_alloc_def (p, "cleared_", single, any_zone);
4010 write_typed_typedef_alloc_def (p, "vec_", vector, any_zone);
4011 write_typed_typedef_alloc_def (p, "cleared_vec_", vector, any_zone);
4012 write_typed_typedef_alloc_def (p, "zone_", single, specific_zone);
4013 write_typed_typedef_alloc_def (p, "zone_cleared_", single,
4014 specific_zone);
4015 write_typed_typedef_alloc_def (p, "zone_cleared_vec_", vector,
4016 specific_zone);
4020 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4021 guaranteee for somewhat increased readability. If name conflicts do happen,
4022 this funcion will have to be adjusted to be more like
4023 output_mangled_typename. */
4025 static void
4026 output_typename (outf_p of, const_type_p t)
4028 switch (t->kind)
4030 case TYPE_STRING:
4031 oprintf (of, "str");
4032 break;
4033 case TYPE_SCALAR:
4034 oprintf (of, "scalar");
4035 break;
4036 case TYPE_POINTER:
4037 output_typename (of, t->u.p);
4038 break;
4039 case TYPE_STRUCT:
4040 case TYPE_UNION:
4041 case TYPE_LANG_STRUCT:
4042 oprintf (of, "%s", t->u.s.tag);
4043 break;
4044 case TYPE_PARAM_STRUCT:
4046 int i;
4047 for (i = 0; i < NUM_PARAM; i++)
4048 if (t->u.param_struct.param[i] != NULL)
4050 output_typename (of, t->u.param_struct.param[i]);
4051 oprintf (of, "_");
4053 output_typename (of, t->u.param_struct.stru);
4054 break;
4056 default:
4057 gcc_unreachable ();
4061 /* Writes a typed GC allocator for type S that is suitable as a callback for
4062 the splay tree implementation in libiberty. */
4064 static void
4065 write_splay_tree_allocator_def (const_type_p s)
4067 outf_p of = get_output_file_for_structure (s, NULL);
4068 oprintf (of, "void * ggc_alloc_splay_tree_");
4069 output_typename (of, s);
4070 oprintf (of, " (int sz, void * nl)\n");
4071 oprintf (of, "{\n");
4072 oprintf (of, " return ggc_splay_alloc (");
4073 oprintf (of, "gt_e_");
4074 output_mangled_typename (of, s);
4075 oprintf (of, ", sz, nl);\n");
4076 oprintf (of, "}\n\n");
4079 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4080 for the splay tree implementation in libiberty. */
4082 static void
4083 write_splay_tree_allocators (const_type_p param_structs)
4085 const_type_p s;
4087 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4088 for (s = param_structs; s; s = s->next)
4089 if (s->gc_used == GC_POINTED_TO)
4091 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4092 output_typename (header_file, s);
4093 oprintf (header_file, " (int, void *);\n");
4094 write_splay_tree_allocator_def (s);
4098 static void dump_pair (int indent, pair_p p);
4099 static void dump_type (int indent, type_p p);
4100 static void dump_type_list (int indent, type_p p);
4102 #define INDENT 2
4104 /* Dumps the value of typekind KIND. */
4106 static void
4107 dump_typekind (int indent, enum typekind kind)
4109 printf ("%*ckind = ", indent, ' ');
4110 switch (kind)
4112 case TYPE_SCALAR:
4113 printf ("TYPE_SCALAR");
4114 break;
4115 case TYPE_STRING:
4116 printf ("TYPE_STRING");
4117 break;
4118 case TYPE_STRUCT:
4119 printf ("TYPE_STRUCT");
4120 break;
4121 case TYPE_UNION:
4122 printf ("TYPE_UNION");
4123 break;
4124 case TYPE_POINTER:
4125 printf ("TYPE_POINTER");
4126 break;
4127 case TYPE_ARRAY:
4128 printf ("TYPE_ARRAY");
4129 break;
4130 case TYPE_LANG_STRUCT:
4131 printf ("TYPE_LANG_STRUCT");
4132 break;
4133 case TYPE_PARAM_STRUCT:
4134 printf ("TYPE_PARAM_STRUCT");
4135 break;
4136 default:
4137 gcc_unreachable ();
4139 printf ("\n");
4142 /* Dumps the value of GC_USED flag. */
4144 static void
4145 dump_gc_used (int indent, enum gc_used_enum gc_used)
4147 printf ("%*cgc_used = ", indent, ' ');
4148 switch (gc_used)
4150 case GC_UNUSED:
4151 printf ("GC_UNUSED");
4152 break;
4153 case GC_USED:
4154 printf ("GC_USED");
4155 break;
4156 case GC_MAYBE_POINTED_TO:
4157 printf ("GC_MAYBE_POINTED_TO");
4158 break;
4159 case GC_POINTED_TO:
4160 printf ("GC_POINTED_TO");
4161 break;
4162 default:
4163 gcc_unreachable ();
4165 printf ("\n");
4168 /* Dumps the type options OPT. */
4170 static void
4171 dump_options (int indent, options_p opt)
4173 options_p o;
4174 printf ("%*coptions = ", indent, ' ');
4175 o = opt;
4176 while (o)
4178 printf ("%s:%s ", o->name, o->info);
4179 o = o->next;
4181 printf ("\n");
4184 /* Dumps the source file location in LINE. */
4186 static void
4187 dump_fileloc (int indent, struct fileloc line)
4189 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ', line.file,
4190 line.line);
4193 /* Recursively dumps the struct, union, or a language-specific
4194 struct T. */
4196 static void
4197 dump_type_u_s (int indent, type_p t)
4199 pair_p fields;
4201 gcc_assert (t->kind == TYPE_STRUCT || t->kind == TYPE_UNION
4202 || t->kind == TYPE_LANG_STRUCT);
4203 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4204 dump_fileloc (indent, t->u.s.line);
4205 printf ("%*cu.s.fields =\n", indent, ' ');
4206 fields = t->u.s.fields;
4207 while (fields)
4209 dump_pair (indent + INDENT, fields);
4210 fields = fields->next;
4212 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4213 dump_options (indent, t->u.s.opt);
4214 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4215 if (t->kind == TYPE_LANG_STRUCT)
4217 printf ("%*cu.s.lang_struct:\n", indent, ' ');
4218 dump_type_list (indent + INDENT, t->u.s.lang_struct);
4222 /* Recursively dumps the array T. */
4224 static void
4225 dump_type_u_a (int indent, type_p t)
4227 gcc_assert (t->kind == TYPE_ARRAY);
4228 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4229 dump_type_list (indent + INDENT, t->u.a.p);
4232 /* Recursively dumps the parameterized struct T. */
4234 static void
4235 dump_type_u_param_struct (int indent, type_p t)
4237 int i;
4238 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
4239 printf ("%*cu.param_struct.stru:\n", indent, ' ');
4240 dump_type_list (indent, t->u.param_struct.stru);
4241 dump_fileloc (indent, t->u.param_struct.line);
4242 for (i = 0; i < NUM_PARAM; i++)
4244 if (t->u.param_struct.param[i] == NULL)
4245 continue;
4246 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
4247 dump_type (indent + INDENT, t->u.param_struct.param[i]);
4251 /* Recursively dumps the type list T. */
4253 static void
4254 dump_type_list (int indent, type_p t)
4256 type_p p = t;
4257 while (p)
4259 dump_type (indent, p);
4260 p = p->next;
4264 static htab_t seen_types;
4266 /* Recursively dumps the type T if it was not dumped previously. */
4268 static void
4269 dump_type (int indent, type_p t)
4271 PTR *slot;
4273 printf ("%*cType at %p: ", indent, ' ', (void *) t);
4274 slot = htab_find_slot (seen_types, t, INSERT);
4275 if (*slot != NULL)
4277 printf ("already seen.\n");
4278 return;
4280 *slot = t;
4281 printf ("\n");
4283 dump_typekind (indent, t->kind);
4284 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4285 (void *) t->pointer_to);
4286 dump_gc_used (indent + INDENT, t->gc_used);
4287 switch (t->kind)
4289 case TYPE_SCALAR:
4290 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4291 t->u.scalar_is_char ? "true" : "false");
4292 break;
4293 case TYPE_STRING:
4294 break;
4295 case TYPE_STRUCT:
4296 case TYPE_UNION:
4297 case TYPE_LANG_STRUCT:
4298 dump_type_u_s (indent + INDENT, t);
4299 break;
4300 case TYPE_POINTER:
4301 printf ("%*cp:\n", indent + INDENT, ' ');
4302 dump_type (indent + INDENT, t->u.p);
4303 break;
4304 case TYPE_ARRAY:
4305 dump_type_u_a (indent + INDENT, t);
4306 break;
4307 case TYPE_PARAM_STRUCT:
4308 dump_type_u_param_struct (indent + INDENT, t);
4309 break;
4310 default:
4311 gcc_unreachable ();
4313 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4316 /* Dumps the pair P. */
4318 static void
4319 dump_pair (int indent, pair_p p)
4321 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4322 dump_type (indent, p->type);
4323 dump_fileloc (indent, p->line);
4324 dump_options (indent, p->opt);
4325 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4328 /* Dumps the list of pairs PP. */
4330 static void
4331 dump_pair_list (const char *name, pair_p pp)
4333 pair_p p;
4334 printf ("%s:\n", name);
4335 for (p = pp; p != NULL; p = p->next)
4336 dump_pair (0, p);
4337 printf ("End of %s\n\n", name);
4340 /* Dumps the STRUCTURES. */
4342 static void
4343 dump_structures (const char *name, type_p structures)
4345 printf ("%s:\n", name);
4346 dump_type_list (0, structures);
4347 printf ("End of %s\n\n", name);
4350 /* Dumps the internal structures of gengtype. */
4352 static void
4353 dump_everything (void)
4355 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4356 dump_pair_list ("typedefs", typedefs);
4357 dump_structures ("structures", structures);
4358 dump_structures ("param_structs", param_structs);
4359 dump_pair_list ("variables", variables);
4360 htab_delete (seen_types);
4365 /* Option specification for getopt_long. */
4366 static const struct option gengtype_long_options[] = {
4367 {"help", no_argument, NULL, 'h'},
4368 {"version", no_argument, NULL, 'V'},
4369 {"verbose", no_argument, NULL, 'v'},
4370 {"dump", no_argument, NULL, 'd'},
4371 {"debug", no_argument, NULL, 'D'},
4372 {"plugin", required_argument, NULL, 'P'},
4373 {"srcdir", required_argument, NULL, 'S'},
4374 {"backupdir", required_argument, NULL, 'B'},
4375 {"inputs", required_argument, NULL, 'I'},
4376 {"read-state", required_argument, NULL, 'r'},
4377 {"write-state", required_argument, NULL, 'w'},
4378 /* Terminating NULL placeholder. */
4379 {NULL, no_argument, NULL, 0},
4383 static void
4384 print_usage (void)
4386 printf ("Usage: %s\n", progname);
4387 printf ("\t -h | --help " " \t# Give this help.\n");
4388 printf ("\t -D | --debug "
4389 " \t# Give debug output to debug %s itself.\n", progname);
4390 printf ("\t -V | --version " " \t# Give version information.\n");
4391 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
4392 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4393 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4394 " \t# Generate for plugin.\n");
4395 printf ("\t -S | --srcdir <GCC-directory> "
4396 " \t# Specify the GCC source directory.\n");
4397 printf ("\t -B | --backupdir <directory> "
4398 " \t# Specify the backup directory for updated files.\n");
4399 printf ("\t -I | --inputs <input-list> "
4400 " \t# Specify the file with source files list.\n");
4401 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
4402 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
4405 static void
4406 print_version (void)
4408 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
4409 printf ("Report bugs: %s\n", bug_report_url);
4412 /* Parse the program options using getopt_long... */
4413 static void
4414 parse_program_options (int argc, char **argv)
4416 int opt = -1;
4417 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
4418 gengtype_long_options, NULL)) >= 0)
4420 switch (opt)
4422 case 'h': /* --help */
4423 print_usage ();
4424 break;
4425 case 'V': /* --version */
4426 print_version ();
4427 break;
4428 case 'd': /* --dump */
4429 do_dump = 1;
4430 break;
4431 case 'D': /* --debug */
4432 do_debug = 1;
4433 break;
4434 case 'v': /* --verbose */
4435 verbosity_level++;
4436 break;
4437 case 'P': /* --plugin */
4438 if (optarg)
4439 plugin_output_filename = optarg;
4440 else
4441 fatal ("missing plugin output file name");
4442 break;
4443 case 'S': /* --srcdir */
4444 if (optarg)
4445 srcdir = optarg;
4446 else
4447 fatal ("missing source directory");
4448 srcdir_len = strlen (srcdir);
4449 break;
4450 case 'B': /* --backupdir */
4451 if (optarg)
4452 backup_dir = optarg;
4453 else
4454 fatal ("missing backup directory");
4455 break;
4456 case 'I': /* --inputs */
4457 if (optarg)
4458 inputlist = optarg;
4459 else
4460 fatal ("missing input list");
4461 break;
4462 case 'r': /* --read-state */
4463 if (optarg)
4464 read_state_filename = optarg;
4465 else
4466 fatal ("missing read state file");
4467 DBGPRINTF ("read state %s\n", optarg);
4468 break;
4469 case 'w': /* --write-state */
4470 DBGPRINTF ("write state %s\n", optarg);
4471 if (optarg)
4472 write_state_filename = optarg;
4473 else
4474 fatal ("missing write state file");
4475 break;
4476 default:
4477 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
4478 print_usage ();
4479 fatal ("unexpected flag");
4482 if (plugin_output_filename)
4484 /* In plugin mode we require some input files. */
4485 int i = 0;
4486 if (optind >= argc)
4487 fatal ("no source files given in plugin mode");
4488 nb_plugin_files = argc - optind;
4489 plugin_files = XNEWVEC (char*, nb_plugin_files);
4490 for (i = 0; i < (int) nb_plugin_files; i++)
4492 char *name = argv[i + optind];
4493 plugin_files[i] = name;
4500 main (int argc, char **argv)
4502 size_t i;
4503 static struct fileloc pos = { NULL, 0 };
4504 outf_p output_header;
4506 /* Mandatory common initializations. */
4507 progname = "gengtype"; /* For fatal and messages. */
4508 /* Set the scalar_is_char union number for predefined scalar types. */
4509 scalar_nonchar.u.scalar_is_char = FALSE;
4510 scalar_char.u.scalar_is_char = TRUE;
4512 parse_program_options (argc, argv);
4514 #if ENABLE_CHECKING
4515 if (do_debug)
4517 time_t now = (time_t) 0;
4518 time (&now);
4519 DBGPRINTF ("gengtype started pid %d at %s",
4520 (int) getpid (), ctime (&now));
4522 #endif /* ENABLE_CHECKING */
4524 /* Parse the input list and the input files. */
4525 DBGPRINTF ("inputlist %s", inputlist);
4526 if (read_state_filename)
4528 fatal ("read state %s not implemented yet", read_state_filename);
4529 /* TODO: implement read state. */
4531 else if (inputlist)
4533 /* These types are set up with #define or else outside of where
4534 we can see them. We should initialize them before calling
4535 read_input_list. */
4536 pos.file = this_file;
4537 pos.line = __LINE__ + 1;
4538 do_scalar_typedef ("CUMULATIVE_ARGS", &pos);
4539 pos.line++;
4540 do_scalar_typedef ("REAL_VALUE_TYPE", &pos);
4541 pos.line++;
4542 do_scalar_typedef ("FIXED_VALUE_TYPE", &pos);
4543 pos.line++;
4544 do_scalar_typedef ("double_int", &pos);
4545 pos.line++;
4546 do_scalar_typedef ("uint64_t", &pos);
4547 pos.line++;
4548 do_scalar_typedef ("uint8", &pos);
4549 pos.line++;
4550 do_scalar_typedef ("jword", &pos);
4551 pos.line++;
4552 do_scalar_typedef ("JCF_u2", &pos);
4553 pos.line++;
4554 do_scalar_typedef ("void", &pos);
4555 pos.line++;
4556 do_typedef ("PTR", create_pointer (resolve_typedef ("void", &pos)),
4557 &pos);
4558 read_input_list (inputlist);
4559 for (i = 0; i < num_gt_files; i++)
4561 parse_file (gt_files[i]);
4562 DBGPRINTF ("parsed file #%d %s", (int) i, gt_files[i]);
4564 if (verbosity_level >= 1)
4565 printf ("%s parsed %d files\n", progname, (int) num_gt_files);
4567 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
4568 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
4571 else
4572 fatal ("either an input list or a read state file should be given");
4573 if (hit_error)
4574 return 1;
4577 if (plugin_output_filename)
4579 size_t ix = 0;
4580 /* In plugin mode, we should have read a state file, and have
4581 given at least one plugin file. */
4582 if (!read_state_filename)
4583 fatal ("No read state given in plugin mode for %s",
4584 plugin_output_filename);
4586 if (nb_plugin_files == 0 || !plugin_files)
4587 fatal ("No plugin files given in plugin mode for %s",
4588 plugin_output_filename);
4590 /* Parse our plugin files. */
4591 for (ix = 0; ix < nb_plugin_files; ix++)
4592 parse_file (plugin_files[ix]);
4594 if (hit_error)
4595 return 1;
4597 plugin_output = create_file ("GCC", plugin_output_filename);
4598 DBGPRINTF ("created plugin_output %p named %s",
4599 (void *) plugin_output, plugin_output->name);
4601 else
4602 { /* No plugin files, we are in normal mode. */
4603 if (!srcdir)
4604 fatal ("gengtype needs a source directory in normal mode");
4606 if (hit_error)
4607 return 1;
4609 gen_rtx_next ();
4611 /* The call to set_gc_used may indirectly call find_param_structure
4612 hence enlarge the param_structs list of types. */
4613 set_gc_used (variables);
4615 /* We should write the state here, but it is not yet implemented. */
4616 if (write_state_filename)
4618 fatal ("write state %s in not yet implemented", write_state_filename);
4619 /* TODO: implement write state. */
4623 open_base_files ();
4625 write_enum_defn (structures, param_structs);
4626 write_typed_alloc_defns (structures, typedefs);
4627 output_header = plugin_output ? plugin_output : header_file;
4628 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
4629 structures);
4630 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
4631 param_structs);
4633 write_types (output_header, structures, param_structs, &ggc_wtd);
4634 if (plugin_files == NULL)
4636 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
4637 structures);
4638 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
4639 param_structs);
4640 write_types (header_file, structures, param_structs, &pch_wtd);
4641 write_local (header_file, structures, param_structs);
4643 write_splay_tree_allocators (param_structs);
4644 write_roots (variables, plugin_files == NULL);
4645 write_rtx_next ();
4646 close_output_files ();
4648 if (do_dump)
4649 dump_everything ();
4651 /* Don't bother about free-ing any input or plugin file, etc. */
4653 if (hit_error)
4654 return 1;
4655 return 0;