* arm.h (TARGET_CPU_CPP_BUILTINS): Remove Maverick support.
[official-gcc.git] / gcc / gengtype.c
blob96c75b3ebb255661a023ca1d975cbd05a238e69b
1 /* Process source files and output type information.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 #ifdef GENERATOR_FILE
22 #include "bconfig.h"
23 #else
24 #include "config.h"
25 #endif
26 #include "system.h"
27 #include "errors.h" /* for fatal */
28 #include "getopt.h"
29 #include "double-int.h"
30 #include "version.h" /* for version_string & pkgversion_string. */
31 #include "hashtab.h"
32 #include "xregex.h"
33 #include "obstack.h"
34 #include "gengtype.h"
35 #include "filenames.h"
37 /* Data types, macros, etc. used only in this file. */
40 /* The list of output files. */
41 outf_p output_files;
43 /* The output header file that is included into pretty much every
44 source file. */
45 outf_p header_file;
48 /* The name of the file containing the list of input files. */
49 static char *inputlist;
51 /* The plugin input files and their number; in that case only
52 a single file is produced. */
53 static input_file **plugin_files;
54 static size_t nb_plugin_files;
56 /* The generated plugin output file and name. */
57 static outf_p plugin_output;
58 static char *plugin_output_filename;
60 /* Our source directory and its length. */
61 const char *srcdir;
62 size_t srcdir_len;
64 /* Variables used for reading and writing the state. */
65 const char *read_state_filename;
66 const char *write_state_filename;
68 /* Variables to help debugging. */
69 int do_dump;
70 int do_debug;
72 /* Level for verbose messages. */
73 int verbosity_level;
75 /* We have a type count and use it to set the state_number of newly
76 allocated types to some unique negative number. */
77 static int type_count;
79 /* The backup directory should be in the same file system as the
80 generated files, otherwise the rename(2) system call would fail.
81 If NULL, no backup is made when overwriting a generated file. */
82 static const char* backup_dir; /* (-B) program option. */
85 static outf_p create_file (const char *, const char *);
87 static const char *get_file_basename (const input_file *);
88 static const char *get_file_realbasename (const input_file *);
90 static int get_prefix_langdir_index (const char *);
91 static const char *get_file_langdir (const input_file *);
94 /* Nonzero iff an error has occurred. */
95 bool hit_error = false;
97 static void gen_rtx_next (void);
98 static void write_rtx_next (void);
99 static void open_base_files (void);
100 static void close_output_files (void);
102 /* Report an error at POS, printing MSG. */
104 void
105 error_at_line (const struct fileloc *pos, const char *msg, ...)
107 va_list ap;
109 gcc_assert (pos != NULL && pos->file != NULL);
110 va_start (ap, msg);
112 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
113 vfprintf (stderr, msg, ap);
114 fputc ('\n', stderr);
115 hit_error = true;
117 va_end (ap);
120 /* asprintf, but produces fatal message on out-of-memory. */
121 char *
122 xasprintf (const char *format, ...)
124 int n;
125 char *result;
126 va_list ap;
128 va_start (ap, format);
129 n = vasprintf (&result, format, ap);
130 if (result == NULL || n < 0)
131 fatal ("out of memory");
132 va_end (ap);
134 return result;
137 /* Input file handling. */
139 /* Table of all input files. */
140 const input_file **gt_files;
141 size_t num_gt_files;
143 /* A number of places use the name of this "gengtype.c" file for a
144 location for things that we can't rely on the source to define.
145 Make sure we can still use pointer comparison on filenames. */
146 input_file* this_file;
147 /* The "system.h" file is likewise specially useful. */
148 input_file* system_h_file;
150 /* Vector of per-language directories. */
151 const char **lang_dir_names;
152 size_t num_lang_dirs;
154 /* An array of output files suitable for definitions. There is one
155 BASE_FILES entry for each language. */
156 static outf_p *base_files;
160 #if ENABLE_CHECKING
161 /* Utility debugging function, printing the various type counts within
162 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
163 void
164 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
166 int nb_types = 0, nb_scalar = 0, nb_string = 0;
167 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
168 int nb_lang_struct = 0, nb_param_struct = 0;
169 type_p p = NULL;
170 for (p = t; p; p = p->next)
172 nb_types++;
173 switch (p->kind)
175 case TYPE_SCALAR:
176 nb_scalar++;
177 break;
178 case TYPE_STRING:
179 nb_string++;
180 break;
181 case TYPE_STRUCT:
182 nb_struct++;
183 break;
184 case TYPE_UNION:
185 nb_union++;
186 break;
187 case TYPE_POINTER:
188 nb_pointer++;
189 break;
190 case TYPE_ARRAY:
191 nb_array++;
192 break;
193 case TYPE_LANG_STRUCT:
194 nb_lang_struct++;
195 break;
196 case TYPE_PARAM_STRUCT:
197 nb_param_struct++;
198 break;
199 default:
200 gcc_unreachable ();
203 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
204 lbasename (fil), lin, msg, nb_types);
205 if (nb_scalar > 0 || nb_string > 0)
206 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
207 if (nb_struct > 0 || nb_union > 0)
208 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
209 if (nb_pointer > 0 || nb_array > 0)
210 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
211 if (nb_lang_struct > 0 || nb_param_struct > 0)
212 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
213 nb_lang_struct, nb_param_struct);
214 fprintf (stderr, "\n");
216 #endif /* ENABLE_CHECKING */
218 /* Scan the input file, LIST, and determine how much space we need to
219 store strings in. Also, count the number of language directories
220 and files. The numbers returned are overestimates as they does not
221 consider repeated files. */
222 static size_t
223 measure_input_list (FILE *list)
225 size_t n = 0;
226 int c;
227 bool atbol = true;
228 num_lang_dirs = 0;
229 num_gt_files = plugin_files ? nb_plugin_files : 0;
230 while ((c = getc (list)) != EOF)
232 n++;
233 if (atbol)
235 if (c == '[')
236 num_lang_dirs++;
237 else
239 /* Add space for a lang_bitmap before the input file name. */
240 n += sizeof (lang_bitmap);
241 num_gt_files++;
243 atbol = false;
246 if (c == '\n')
247 atbol = true;
250 rewind (list);
251 return n;
254 /* Read one input line from LIST to HEREP (which is updated). A
255 pointer to the string is returned via LINEP. If it was a language
256 subdirectory in square brackets, strip off the square brackets and
257 return true. Otherwise, leave space before the string for a
258 lang_bitmap, and return false. At EOF, returns false, does not
259 touch *HEREP, and sets *LINEP to NULL. POS is used for
260 diagnostics. */
261 static bool
262 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
264 char *here = *herep;
265 char *line;
266 int c = getc (list);
268 /* Read over whitespace. */
269 while (c == '\n' || c == ' ')
270 c = getc (list);
272 if (c == EOF)
274 *linep = 0;
275 return false;
277 else if (c == '[')
279 /* No space for a lang_bitmap is necessary. Discard the '['. */
280 c = getc (list);
281 line = here;
282 while (c != ']' && c != '\n' && c != EOF)
284 *here++ = c;
285 c = getc (list);
287 *here++ = '\0';
289 if (c == ']')
291 c = getc (list); /* eat what should be a newline */
292 if (c != '\n' && c != EOF)
293 error_at_line (pos, "junk on line after language tag [%s]", line);
295 else
296 error_at_line (pos, "missing close bracket for language tag [%s",
297 line);
299 *herep = here;
300 *linep = line;
301 return true;
303 else
305 /* Leave space for a lang_bitmap. */
306 memset (here, 0, sizeof (lang_bitmap));
307 here += sizeof (lang_bitmap);
308 line = here;
311 *here++ = c;
312 c = getc (list);
314 while (c != EOF && c != '\n');
315 *here++ = '\0';
316 *herep = here;
317 *linep = line;
318 return false;
322 /* Read the list of input files from LIST and compute all of the
323 relevant tables. There is one file per line of the list. At
324 first, all the files on the list are language-generic, but
325 eventually a line will appear which is the name of a language
326 subdirectory in square brackets, like this: [cp]. All subsequent
327 files are specific to that language, until another language
328 subdirectory tag appears. Files can appear more than once, if
329 they apply to more than one language. */
330 static void
331 read_input_list (const char *listname)
333 FILE *list = fopen (listname, "r");
334 if (!list)
335 fatal ("cannot open %s: %s", listname, xstrerror (errno));
336 else
338 struct fileloc epos;
339 size_t bufsz = measure_input_list (list);
340 char *buf = XNEWVEC (char, bufsz);
341 char *here = buf;
342 char *committed = buf;
343 char *limit = buf + bufsz;
344 char *line;
345 bool is_language;
346 size_t langno = 0;
347 size_t nfiles = 0;
348 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
350 epos.file = input_file_by_name (listname);
351 epos.line = 0;
353 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
354 gt_files = XNEWVEC (const input_file *, num_gt_files);
356 for (;;)
358 next_line:
359 epos.line++;
360 committed = here;
361 is_language = read_input_line (list, &here, &line, &epos);
362 gcc_assert (here <= limit);
363 if (line == 0)
364 break;
365 else if (is_language)
367 size_t i;
368 gcc_assert (langno <= num_lang_dirs);
369 for (i = 0; i < langno; i++)
370 if (strcmp (lang_dir_names[i], line) == 0)
372 error_at_line (&epos, "duplicate language tag [%s]",
373 line);
374 curlangs = 1 << i;
375 here = committed;
376 goto next_line;
379 curlangs = 1 << langno;
380 lang_dir_names[langno++] = line;
382 else
384 size_t i;
385 input_file *inpf = input_file_by_name (line);
386 gcc_assert (nfiles <= num_gt_files);
387 for (i = 0; i < nfiles; i++)
388 /* Since the input_file-s are uniquely hash-consed, we
389 can just compare pointers! */
390 if (gt_files[i] == inpf)
392 /* Throw away the string we just read, and add the
393 current language to the existing string's bitmap. */
394 lang_bitmap bmap = get_lang_bitmap (inpf);
395 if (bmap & curlangs)
396 error_at_line (&epos,
397 "file %s specified more than once "
398 "for language %s", line,
399 langno ==
400 0 ? "(all)" : lang_dir_names[langno -
401 1]);
403 bmap |= curlangs;
404 set_lang_bitmap (inpf, bmap);
405 here = committed;
406 goto next_line;
409 set_lang_bitmap (inpf, curlangs);
410 gt_files[nfiles++] = inpf;
413 /* Update the global counts now that we know accurately how many
414 things there are. (We do not bother resizing the arrays down.) */
415 num_lang_dirs = langno;
416 /* Add the plugin files if provided. */
417 if (plugin_files)
419 size_t i;
420 for (i = 0; i < nb_plugin_files; i++)
421 gt_files[nfiles++] = plugin_files[i];
423 num_gt_files = nfiles;
426 /* Sanity check: any file that resides in a language subdirectory
427 (e.g. 'cp') ought to belong to the corresponding language.
428 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
429 (Can you even do that? Should you be allowed to?) */
431 size_t f;
432 for (f = 0; f < num_gt_files; f++)
434 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
435 const char *basename = get_file_basename (gt_files[f]);
436 const char *slashpos = strchr (basename, '/');
437 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
438 const char *slashpos2 = strchr (basename, '\\');
440 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
441 slashpos = slashpos2;
442 #endif
444 if (slashpos)
446 size_t l;
447 for (l = 0; l < num_lang_dirs; l++)
448 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
449 && memcmp (basename, lang_dir_names[l],
450 strlen (lang_dir_names[l])) == 0)
452 if (!(bitmap & (1 << l)))
453 error ("%s is in language directory '%s' but is not "
454 "tagged for that language",
455 basename, lang_dir_names[l]);
456 break;
462 if (ferror (list))
463 fatal ("error reading %s: %s", listname, xstrerror (errno));
465 fclose (list);
470 /* The one and only TYPE_STRING. */
472 struct type string_type = {
473 TYPE_STRING, 0, 0, 0, GC_USED, {0}
476 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
477 set early in main. */
479 struct type scalar_nonchar = {
480 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
483 struct type scalar_char = {
484 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
487 /* Lists of various things. */
489 pair_p typedefs;
490 type_p structures;
491 type_p param_structs;
492 pair_p variables;
494 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
495 static type_p adjust_field_tree_exp (type_p t, options_p opt);
496 static type_p adjust_field_rtx_def (type_p t, options_p opt);
498 /* Define S as a typedef to T at POS. */
500 void
501 do_typedef (const char *s, type_p t, struct fileloc *pos)
503 pair_p p;
505 /* temporary kludge - gengtype doesn't handle conditionals or
506 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
507 is coming from this file (main() sets them up with safe dummy
508 definitions). */
509 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
510 return;
512 for (p = typedefs; p != NULL; p = p->next)
513 if (strcmp (p->name, s) == 0)
515 if (p->type != t)
517 error_at_line (pos, "type `%s' previously defined", s);
518 error_at_line (&p->line, "previously defined here");
520 return;
523 p = XNEW (struct pair);
524 p->next = typedefs;
525 p->name = s;
526 p->type = t;
527 p->line = *pos;
528 p->opt = NULL;
529 typedefs = p;
532 /* Define S as a typename of a scalar. Cannot be used to define
533 typedefs of 'char'. Note: is also used for pointer-to-function
534 typedefs (which are therefore not treated as pointers). */
536 void
537 do_scalar_typedef (const char *s, struct fileloc *pos)
539 do_typedef (s, &scalar_nonchar, pos);
542 /* Return the type previously defined for S. Use POS to report errors. */
544 type_p
545 resolve_typedef (const char *s, struct fileloc *pos)
547 pair_p p;
548 for (p = typedefs; p != NULL; p = p->next)
549 if (strcmp (p->name, s) == 0)
550 return p->type;
551 error_at_line (pos, "unidentified type `%s'", s);
552 return &scalar_nonchar; /* treat as "int" */
555 /* Create and return a new structure with tag NAME (or a union iff
556 ISUNION is nonzero), at POS with fields FIELDS and options O. */
558 type_p
559 new_structure (const char *name, int isunion, struct fileloc *pos,
560 pair_p fields, options_p o)
562 type_p si;
563 type_p s = NULL;
564 lang_bitmap bitmap = get_lang_bitmap (pos->file);
566 for (si = structures; si != NULL; si = si->next)
567 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
569 type_p ls = NULL;
570 if (si->kind == TYPE_LANG_STRUCT)
572 ls = si;
574 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
575 if (si->u.s.bitmap == bitmap)
576 s = si;
578 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
580 ls = si;
581 type_count++;
582 si = XCNEW (struct type);
583 memcpy (si, ls, sizeof (struct type));
584 ls->kind = TYPE_LANG_STRUCT;
585 ls->u.s.lang_struct = si;
586 ls->u.s.fields = NULL;
587 si->next = NULL;
588 si->state_number = -type_count;
589 si->pointer_to = NULL;
590 si->u.s.lang_struct = ls;
592 else
593 s = si;
595 if (ls != NULL && s == NULL)
597 type_count++;
598 s = XCNEW (struct type);
599 s->state_number = -type_count;
600 s->next = ls->u.s.lang_struct;
601 ls->u.s.lang_struct = s;
602 s->u.s.lang_struct = ls;
604 break;
607 if (s == NULL)
609 type_count++;
610 s = XCNEW (struct type);
611 s->state_number = -type_count;
612 s->next = structures;
613 structures = s;
616 if (s->u.s.line.file != NULL
617 || (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap)))
619 error_at_line (pos, "duplicate definition of '%s %s'",
620 isunion ? "union" : "struct", s->u.s.tag);
621 error_at_line (&s->u.s.line, "previous definition here");
624 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
625 s->u.s.tag = name;
626 s->u.s.line = *pos;
627 s->u.s.fields = fields;
628 s->u.s.opt = o;
629 s->u.s.bitmap = bitmap;
630 if (s->u.s.lang_struct)
631 s->u.s.lang_struct->u.s.bitmap |= bitmap;
633 return s;
636 /* Return the previously-defined structure with tag NAME (or a union
637 iff ISUNION is nonzero), or a new empty structure or union if none
638 was defined previously. */
640 type_p
641 find_structure (const char *name, int isunion)
643 type_p s;
645 for (s = structures; s != NULL; s = s->next)
646 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
647 return s;
649 type_count++;
650 s = XCNEW (struct type);
651 s->next = structures;
652 s->state_number = -type_count;
653 structures = s;
654 s->kind = isunion ? TYPE_UNION : TYPE_STRUCT;
655 s->u.s.tag = name;
656 structures = s;
657 return s;
660 /* Return the previously-defined parameterized structure for structure
661 T and parameters PARAM, or a new parameterized empty structure or
662 union if none was defined previously. */
664 static type_p
665 find_param_structure (type_p t, type_p param[NUM_PARAM])
667 type_p res;
669 for (res = param_structs; res; res = res->next)
670 if (res->u.param_struct.stru == t
671 && memcmp (res->u.param_struct.param, param,
672 sizeof (type_p) * NUM_PARAM) == 0)
673 break;
674 if (res == NULL)
676 type_count++;
677 res = XCNEW (struct type);
678 res->kind = TYPE_PARAM_STRUCT;
679 res->next = param_structs;
680 res->state_number = -type_count;
681 param_structs = res;
682 res->u.param_struct.stru = t;
683 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
685 return res;
688 /* Return a scalar type with name NAME. */
690 type_p
691 create_scalar_type (const char *name)
693 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
694 return &scalar_char;
695 else
696 return &scalar_nonchar;
699 /* Return a pointer to T. */
701 type_p
702 create_pointer (type_p t)
704 if (!t->pointer_to)
706 type_p r = XCNEW (struct type);
707 type_count++;
708 r->state_number = -type_count;
709 r->kind = TYPE_POINTER;
710 r->u.p = t;
711 t->pointer_to = r;
713 return t->pointer_to;
716 /* Return an array of length LEN. */
718 type_p
719 create_array (type_p t, const char *len)
721 type_p v;
723 type_count++;
724 v = XCNEW (struct type);
725 v->kind = TYPE_ARRAY;
726 v->state_number = -type_count;
727 v->u.a.p = t;
728 v->u.a.len = len;
729 return v;
732 /* Return a string options structure with name NAME and info INFO.
733 NEXT is the next option in the chain. */
734 options_p
735 create_string_option (options_p next, const char *name, const char *info)
737 options_p o = XNEW (struct options);
738 o->kind = OPTION_STRING;
739 o->next = next;
740 o->name = name;
741 o->info.string = info;
742 return o;
745 /* Create a type options structure with name NAME and info INFO. NEXT
746 is the next option in the chain. */
747 options_p
748 create_type_option (options_p next, const char* name, type_p info)
750 options_p o = XNEW (struct options);
751 o->next = next;
752 o->name = name;
753 o->kind = OPTION_TYPE;
754 o->info.type = info;
755 return o;
758 /* Create a nested pointer options structure with name NAME and info
759 INFO. NEXT is the next option in the chain. */
760 options_p
761 create_nested_option (options_p next, const char* name,
762 struct nested_ptr_data* info)
764 options_p o;
765 o = XNEW (struct options);
766 o->next = next;
767 o->name = name;
768 o->kind = OPTION_NESTED;
769 o->info.nested = info;
770 return o;
773 /* Return an options structure for a "nested_ptr" option. */
774 options_p
775 create_nested_ptr_option (options_p next, type_p t,
776 const char *to, const char *from)
778 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
780 d->type = adjust_field_type (t, 0);
781 d->convert_to = to;
782 d->convert_from = from;
783 return create_nested_option (next, "nested_ptr", d);
786 /* Add a variable named S of type T with options O defined at POS,
787 to `variables'. */
788 void
789 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
791 pair_p n;
792 n = XNEW (struct pair);
793 n->name = s;
794 n->type = t;
795 n->line = *pos;
796 n->opt = o;
797 n->next = variables;
798 variables = n;
801 /* Most-general structure field creator. */
802 static pair_p
803 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
804 const input_file *inpf, int line)
806 pair_p field;
808 field = XNEW (struct pair);
809 field->next = next;
810 field->type = type;
811 field->name = name;
812 field->opt = opt;
813 field->line.file = inpf;
814 field->line.line = line;
815 return field;
818 /* Create a field that came from the source code we are scanning,
819 i.e. we have a 'struct fileloc', and possibly options; also,
820 adjust_field_type should be called. */
821 pair_p
822 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
823 struct fileloc *pos)
825 return create_field_all (next, adjust_field_type (type, opt),
826 name, opt, pos->file, pos->line);
829 /* Create a fake field with the given type and name. NEXT is the next
830 field in the chain. */
831 #define create_field(next,type,name) \
832 create_field_all(next,type,name, 0, this_file, __LINE__)
834 /* Like create_field, but the field is only valid when condition COND
835 is true. */
837 static pair_p
838 create_optional_field_ (pair_p next, type_p type, const char *name,
839 const char *cond, int line)
841 static int id = 1;
842 pair_p union_fields;
843 type_p union_type;
845 /* Create a fake union type with a single nameless field of type TYPE.
846 The field has a tag of "1". This allows us to make the presence
847 of a field of type TYPE depend on some boolean "desc" being true. */
848 union_fields = create_field (NULL, type, "");
849 union_fields->opt =
850 create_string_option (union_fields->opt, "dot", "");
851 union_fields->opt =
852 create_string_option (union_fields->opt, "tag", "1");
853 union_type =
854 new_structure (xasprintf ("%s_%d", "fake_union", id++), 1,
855 &lexer_line, union_fields, NULL);
857 /* Create the field and give it the new fake union type. Add a "desc"
858 tag that specifies the condition under which the field is valid. */
859 return create_field_all (next, union_type, name,
860 create_string_option (0, "desc", cond),
861 this_file, line);
864 #define create_optional_field(next,type,name,cond) \
865 create_optional_field_(next,type,name,cond,__LINE__)
867 /* Reverse a linked list of 'struct pair's in place. */
868 pair_p
869 nreverse_pairs (pair_p list)
871 pair_p prev = 0, p, next;
872 for (p = list; p; p = next)
874 next = p->next;
875 p->next = prev;
876 prev = p;
878 return prev;
882 /* We don't care how long a CONST_DOUBLE is. */
883 #define CONST_DOUBLE_FORMAT "ww"
884 /* We don't want to see codes that are only for generator files. */
885 #undef GENERATOR_FILE
887 enum rtx_code
889 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
890 #include "rtl.def"
891 #undef DEF_RTL_EXPR
892 NUM_RTX_CODE
895 static const char *const rtx_name[NUM_RTX_CODE] = {
896 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
897 #include "rtl.def"
898 #undef DEF_RTL_EXPR
901 static const char *const rtx_format[NUM_RTX_CODE] = {
902 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
903 #include "rtl.def"
904 #undef DEF_RTL_EXPR
907 static int rtx_next_new[NUM_RTX_CODE];
909 /* We also need codes and names for insn notes (not register notes).
910 Note that we do *not* bias the note values here. */
911 enum insn_note
913 #define DEF_INSN_NOTE(NAME) NAME,
914 #include "insn-notes.def"
915 #undef DEF_INSN_NOTE
917 NOTE_INSN_MAX
920 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
921 default field for line number notes. */
922 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
923 #define DEF_INSN_NOTE(NAME) #NAME,
924 #include "insn-notes.def"
925 #undef DEF_INSN_NOTE
928 #undef CONST_DOUBLE_FORMAT
929 #define GENERATOR_FILE
931 /* Generate the contents of the rtx_next array. This really doesn't belong
932 in gengtype at all, but it's needed for adjust_field_rtx_def. */
934 static void
935 gen_rtx_next (void)
937 int i;
938 for (i = 0; i < NUM_RTX_CODE; i++)
940 int k;
942 rtx_next_new[i] = -1;
943 if (strncmp (rtx_format[i], "iuu", 3) == 0)
944 rtx_next_new[i] = 2;
945 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
946 rtx_next_new[i] = 1;
947 else
948 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
949 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
950 rtx_next_new[i] = k;
954 /* Write out the contents of the rtx_next array. */
955 static void
956 write_rtx_next (void)
958 outf_p f = get_output_file_with_visibility (NULL);
959 int i;
960 if (!f)
961 return;
963 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
964 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
965 for (i = 0; i < NUM_RTX_CODE; i++)
966 if (rtx_next_new[i] == -1)
967 oprintf (f, " 0,\n");
968 else
969 oprintf (f,
970 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
971 oprintf (f, "};\n");
974 /* Handle `special("rtx_def")'. This is a special case for field
975 `fld' of struct rtx_def, which is an array of unions whose values
976 are based in a complex way on the type of RTL. */
978 static type_p
979 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
981 pair_p flds = NULL;
982 options_p nodot;
983 int i;
984 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
985 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
987 if (t->kind != TYPE_UNION)
989 error_at_line (&lexer_line,
990 "special `rtx_def' must be applied to a union");
991 return &string_type;
994 nodot = create_string_option (NULL, "dot", "");
996 rtx_tp = create_pointer (find_structure ("rtx_def", 0));
997 rtvec_tp = create_pointer (find_structure ("rtvec_def", 0));
998 tree_tp = create_pointer (find_structure ("tree_node", 1));
999 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", 0));
1000 reg_attrs_tp =
1001 create_pointer (find_structure ("reg_attrs", 0));
1002 basic_block_tp =
1003 create_pointer (find_structure ("basic_block_def", 0));
1004 constant_tp =
1005 create_pointer (find_structure ("constant_descriptor_rtx", 0));
1006 scalar_tp = &scalar_nonchar; /* rtunion int */
1009 pair_p note_flds = NULL;
1010 int c;
1012 for (c = 0; c <= NOTE_INSN_MAX; c++)
1014 switch (c)
1016 case NOTE_INSN_MAX:
1017 case NOTE_INSN_DELETED_LABEL:
1018 case NOTE_INSN_DELETED_DEBUG_LABEL:
1019 note_flds = create_field (note_flds, &string_type, "rt_str");
1020 break;
1022 case NOTE_INSN_BLOCK_BEG:
1023 case NOTE_INSN_BLOCK_END:
1024 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1025 break;
1027 case NOTE_INSN_VAR_LOCATION:
1028 case NOTE_INSN_CALL_ARG_LOCATION:
1029 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1030 break;
1032 default:
1033 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1034 break;
1036 /* NOTE_INSN_MAX is used as the default field for line
1037 number notes. */
1038 if (c == NOTE_INSN_MAX)
1039 note_flds->opt =
1040 create_string_option (nodot, "default", "");
1041 else
1042 note_flds->opt =
1043 create_string_option (nodot, "tag", note_insn_name[c]);
1045 note_union_tp = new_structure ("rtx_def_note_subunion", 1,
1046 &lexer_line, note_flds, NULL);
1048 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1050 pair_p sym_flds;
1051 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1052 sym_flds->opt = create_string_option (nodot, "default", "");
1053 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1054 sym_flds->opt = create_string_option (nodot, "tag", "1");
1055 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", 1,
1056 &lexer_line, sym_flds, NULL);
1058 for (i = 0; i < NUM_RTX_CODE; i++)
1060 pair_p subfields = NULL;
1061 size_t aindex, nmindex;
1062 const char *sname;
1063 type_p substruct;
1064 char *ftag;
1066 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1068 type_p t;
1069 const char *subname;
1071 switch (rtx_format[i][aindex])
1073 case '*':
1074 case 'i':
1075 case 'n':
1076 case 'w':
1077 t = scalar_tp;
1078 subname = "rt_int";
1079 break;
1081 case '0':
1082 if (i == MEM && aindex == 1)
1083 t = mem_attrs_tp, subname = "rt_mem";
1084 else if (i == JUMP_INSN && aindex == 8)
1085 t = rtx_tp, subname = "rt_rtx";
1086 else if (i == CODE_LABEL && aindex == 5)
1087 t = scalar_tp, subname = "rt_int";
1088 else if (i == CODE_LABEL && aindex == 4)
1089 t = rtx_tp, subname = "rt_rtx";
1090 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1091 t = rtx_tp, subname = "rt_rtx";
1092 else if (i == NOTE && aindex == 4)
1093 t = note_union_tp, subname = "";
1094 else if (i == NOTE && aindex == 5)
1095 t = scalar_tp, subname = "rt_int";
1096 else if (i == NOTE && aindex >= 7)
1097 t = scalar_tp, subname = "rt_int";
1098 else if (i == ADDR_DIFF_VEC && aindex == 4)
1099 t = scalar_tp, subname = "rt_int";
1100 else if (i == VALUE && aindex == 0)
1101 t = scalar_tp, subname = "rt_int";
1102 else if (i == DEBUG_EXPR && aindex == 0)
1103 t = tree_tp, subname = "rt_tree";
1104 else if (i == REG && aindex == 1)
1105 t = scalar_tp, subname = "rt_int";
1106 else if (i == REG && aindex == 2)
1107 t = reg_attrs_tp, subname = "rt_reg";
1108 else if (i == SCRATCH && aindex == 0)
1109 t = scalar_tp, subname = "rt_int";
1110 else if (i == SYMBOL_REF && aindex == 1)
1111 t = scalar_tp, subname = "rt_int";
1112 else if (i == SYMBOL_REF && aindex == 2)
1113 t = symbol_union_tp, subname = "";
1114 else if (i == BARRIER && aindex >= 3)
1115 t = scalar_tp, subname = "rt_int";
1116 else if (i == ENTRY_VALUE && aindex == 0)
1117 t = rtx_tp, subname = "rt_rtx";
1118 else
1120 error_at_line
1121 (&lexer_line,
1122 "rtx type `%s' has `0' in position %lu, can't handle",
1123 rtx_name[i], (unsigned long) aindex);
1124 t = &string_type;
1125 subname = "rt_int";
1127 break;
1129 case 's':
1130 case 'S':
1131 case 'T':
1132 t = &string_type;
1133 subname = "rt_str";
1134 break;
1136 case 'e':
1137 case 'u':
1138 t = rtx_tp;
1139 subname = "rt_rtx";
1140 break;
1142 case 'E':
1143 case 'V':
1144 t = rtvec_tp;
1145 subname = "rt_rtvec";
1146 break;
1148 case 't':
1149 t = tree_tp;
1150 subname = "rt_tree";
1151 break;
1153 case 'B':
1154 t = basic_block_tp;
1155 subname = "rt_bb";
1156 break;
1158 default:
1159 error_at_line
1160 (&lexer_line,
1161 "rtx type `%s' has `%c' in position %lu, can't handle",
1162 rtx_name[i], rtx_format[i][aindex],
1163 (unsigned long) aindex);
1164 t = &string_type;
1165 subname = "rt_int";
1166 break;
1169 subfields = create_field (subfields, t,
1170 xasprintf (".fld[%lu].%s",
1171 (unsigned long) aindex,
1172 subname));
1173 subfields->opt = nodot;
1174 if (t == note_union_tp)
1175 subfields->opt =
1176 create_string_option (subfields->opt, "desc",
1177 "NOTE_KIND (&%0)");
1178 if (t == symbol_union_tp)
1179 subfields->opt =
1180 create_string_option (subfields->opt, "desc",
1181 "CONSTANT_POOL_ADDRESS_P (&%0)");
1184 if (i == SYMBOL_REF)
1186 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1187 holds. */
1188 type_p field_tp = find_structure ("block_symbol", 0);
1189 subfields
1190 = create_optional_field (subfields, field_tp, "block_sym",
1191 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1194 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1195 substruct = new_structure (sname, 0, &lexer_line, subfields, NULL);
1197 ftag = xstrdup (rtx_name[i]);
1198 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1199 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1200 flds = create_field (flds, substruct, "");
1201 flds->opt = create_string_option (nodot, "tag", ftag);
1203 return new_structure ("rtx_def_subunion", 1, &lexer_line, flds, nodot);
1206 /* Handle `special("tree_exp")'. This is a special case for
1207 field `operands' of struct tree_exp, which although it claims to contain
1208 pointers to trees, actually sometimes contains pointers to RTL too.
1209 Passed T, the old type of the field, and OPT its options. Returns
1210 a new type for the field. */
1212 static type_p
1213 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1215 pair_p flds;
1216 options_p nodot;
1218 if (t->kind != TYPE_ARRAY)
1220 error_at_line (&lexer_line,
1221 "special `tree_exp' must be applied to an array");
1222 return &string_type;
1225 nodot = create_string_option (NULL, "dot", "");
1227 flds = create_field (NULL, t, "");
1228 flds->opt = create_string_option (nodot, "length",
1229 "TREE_OPERAND_LENGTH ((tree) &%0)");
1230 flds->opt = create_string_option (flds->opt, "default", "");
1232 return new_structure ("tree_exp_subunion", 1, &lexer_line, flds, nodot);
1235 /* Perform any special processing on a type T, about to become the type
1236 of a field. Return the appropriate type for the field.
1237 At present:
1238 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1239 - Similarly for arrays of pointer-to-char;
1240 - Converts structures for which a parameter is provided to
1241 TYPE_PARAM_STRUCT;
1242 - Handles "special" options.
1245 type_p
1246 adjust_field_type (type_p t, options_p opt)
1248 int length_p = 0;
1249 const int pointer_p = t->kind == TYPE_POINTER;
1250 type_p params[NUM_PARAM];
1251 int params_p = 0;
1252 int i;
1254 for (i = 0; i < NUM_PARAM; i++)
1255 params[i] = NULL;
1257 for (; opt; opt = opt->next)
1258 if (strcmp (opt->name, "length") == 0)
1259 length_p = 1;
1260 else if ((strcmp (opt->name, "param_is") == 0
1261 || (strncmp (opt->name, "param", 5) == 0
1262 && ISDIGIT (opt->name[5])
1263 && strcmp (opt->name + 6, "_is") == 0))
1264 && opt->kind == OPTION_TYPE)
1266 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1268 if (!UNION_OR_STRUCT_P (t)
1269 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
1271 error_at_line (&lexer_line,
1272 "option `%s' may only be applied to structures or structure pointers",
1273 opt->name);
1274 return t;
1277 params_p = 1;
1278 if (params[num] != NULL)
1279 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1280 if (!ISDIGIT (opt->name[5]))
1281 params[num] = create_pointer (opt->info.type);
1282 else
1283 params[num] = opt->info.type;
1285 else if (strcmp (opt->name, "special") == 0
1286 && opt->kind == OPTION_STRING)
1288 const char *special_name = opt->info.string;
1289 if (strcmp (special_name, "tree_exp") == 0)
1290 t = adjust_field_tree_exp (t, opt);
1291 else if (strcmp (special_name, "rtx_def") == 0)
1292 t = adjust_field_rtx_def (t, opt);
1293 else
1294 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1297 if (params_p)
1299 type_p realt;
1301 if (pointer_p)
1302 t = t->u.p;
1303 realt = find_param_structure (t, params);
1304 t = pointer_p ? create_pointer (realt) : realt;
1307 if (!length_p
1308 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1309 return &string_type;
1310 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1311 && t->u.a.p->u.p->kind == TYPE_SCALAR
1312 && t->u.a.p->u.p->u.scalar_is_char)
1313 return create_array (&string_type, t->u.a.len);
1315 return t;
1319 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *);
1320 static void set_gc_used (pair_p);
1322 /* Handle OPT for set_gc_used_type. */
1324 static void
1325 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1326 int *pass_param, int *length, int *skip,
1327 type_p *nested_ptr)
1329 options_p o;
1330 for (o = opt; o; o = o->next)
1331 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1332 && o->kind == OPTION_TYPE)
1333 set_gc_used_type (o->info.type,
1334 GC_POINTED_TO, NULL);
1335 else if (strcmp (o->name, "maybe_undef") == 0)
1336 *maybe_undef = 1;
1337 else if (strcmp (o->name, "use_params") == 0)
1338 *pass_param = 1;
1339 else if (strcmp (o->name, "length") == 0)
1340 *length = 1;
1341 else if (strcmp (o->name, "skip") == 0)
1342 *skip = 1;
1343 else if (strcmp (o->name, "nested_ptr") == 0
1344 && o->kind == OPTION_NESTED)
1345 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1349 /* Set the gc_used field of T to LEVEL, and handle the types it references. */
1350 static void
1351 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM])
1353 if (t->gc_used >= level)
1354 return;
1356 t->gc_used = level;
1358 switch (t->kind)
1360 case TYPE_STRUCT:
1361 case TYPE_UNION:
1363 pair_p f;
1364 int dummy;
1365 type_p dummy2;
1367 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1368 &dummy2);
1370 for (f = t->u.s.fields; f; f = f->next)
1372 int maybe_undef = 0;
1373 int pass_param = 0;
1374 int length = 0;
1375 int skip = 0;
1376 type_p nested_ptr = NULL;
1377 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1378 &length, &skip, &nested_ptr);
1380 if (nested_ptr && f->type->kind == TYPE_POINTER)
1381 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1382 pass_param ? param : NULL);
1383 else if (length && f->type->kind == TYPE_POINTER)
1384 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1385 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1386 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1387 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1388 set_gc_used_type (find_param_structure (f->type->u.p, param),
1389 GC_POINTED_TO, NULL);
1390 else if (skip)
1391 ; /* target type is not used through this field */
1392 else
1393 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL);
1395 break;
1398 case TYPE_POINTER:
1399 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1400 break;
1402 case TYPE_ARRAY:
1403 set_gc_used_type (t->u.a.p, GC_USED, param);
1404 break;
1406 case TYPE_LANG_STRUCT:
1407 for (t = t->u.s.lang_struct; t; t = t->next)
1408 set_gc_used_type (t, level, param);
1409 break;
1411 case TYPE_PARAM_STRUCT:
1413 int i;
1414 for (i = 0; i < NUM_PARAM; i++)
1415 if (t->u.param_struct.param[i] != 0)
1416 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1418 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1419 level = GC_POINTED_TO;
1420 else
1421 level = GC_USED;
1422 t->u.param_struct.stru->gc_used = GC_UNUSED;
1423 set_gc_used_type (t->u.param_struct.stru, level,
1424 t->u.param_struct.param);
1425 break;
1427 default:
1428 break;
1432 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1434 static void
1435 set_gc_used (pair_p variables)
1437 int nbvars = 0;
1438 pair_p p;
1439 for (p = variables; p; p = p->next)
1441 set_gc_used_type (p->type, GC_USED, NULL);
1442 nbvars++;
1444 if (verbosity_level >= 2)
1445 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1448 /* File mapping routines. For each input file, there is one output .c file
1449 (but some output files have many input files), and there is one .h file
1450 for the whole build. */
1452 /* Output file handling. */
1454 /* Create and return an outf_p for a new file for NAME, to be called
1455 ONAME. */
1457 static outf_p
1458 create_file (const char *name, const char *oname)
1460 static const char *const hdr[] = {
1461 " Copyright (C) 2004, 2007, 2009 Free Software Foundation, Inc.\n",
1462 "\n",
1463 "This file is part of GCC.\n",
1464 "\n",
1465 "GCC is free software; you can redistribute it and/or modify it under\n",
1466 "the terms of the GNU General Public License as published by the Free\n",
1467 "Software Foundation; either version 3, or (at your option) any later\n",
1468 "version.\n",
1469 "\n",
1470 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1471 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1472 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1473 "for more details.\n",
1474 "\n",
1475 "You should have received a copy of the GNU General Public License\n",
1476 "along with GCC; see the file COPYING3. If not see\n",
1477 "<http://www.gnu.org/licenses/>. */\n",
1478 "\n",
1479 "/* This file is machine generated. Do not edit. */\n"
1481 outf_p f;
1482 size_t i;
1484 gcc_assert (name != NULL);
1485 gcc_assert (oname != NULL);
1486 f = XCNEW (struct outf);
1487 f->next = output_files;
1488 f->name = oname;
1489 output_files = f;
1491 oprintf (f, "/* Type information for %s.\n", name);
1492 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1493 oprintf (f, "%s", hdr[i]);
1494 return f;
1497 /* Print, like fprintf, to O.
1498 N.B. You might think this could be implemented more efficiently
1499 with vsnprintf(). Unfortunately, there are C libraries that
1500 provide that function but without the C99 semantics for its return
1501 value, making it impossible to know how much space is required. */
1502 void
1503 oprintf (outf_p o, const char *format, ...)
1505 char *s;
1506 size_t slength;
1507 va_list ap;
1509 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1510 in that case. */
1511 if (!o)
1512 return;
1514 va_start (ap, format);
1515 slength = vasprintf (&s, format, ap);
1516 if (s == NULL || (int) slength < 0)
1517 fatal ("out of memory");
1518 va_end (ap);
1520 if (o->bufused + slength > o->buflength)
1522 size_t new_len = o->buflength;
1523 if (new_len == 0)
1524 new_len = 1024;
1527 new_len *= 2;
1529 while (o->bufused + slength >= new_len);
1530 o->buf = XRESIZEVEC (char, o->buf, new_len);
1531 o->buflength = new_len;
1533 memcpy (o->buf + o->bufused, s, slength);
1534 o->bufused += slength;
1535 free (s);
1538 /* Open the global header file and the language-specific header files. */
1540 static void
1541 open_base_files (void)
1543 size_t i;
1545 if (nb_plugin_files > 0 && plugin_files)
1546 return;
1548 header_file = create_file ("GCC", "gtype-desc.h");
1550 base_files = XNEWVEC (outf_p, num_lang_dirs);
1552 for (i = 0; i < num_lang_dirs; i++)
1553 base_files[i] = create_file (lang_dir_names[i],
1554 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1556 /* gtype-desc.c is a little special, so we create it here. */
1558 /* The order of files here matters very much. */
1559 static const char *const ifiles[] = {
1560 "config.h", "system.h", "coretypes.h", "tm.h",
1561 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1562 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1563 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1564 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1565 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1566 "except.h", "output.h", "gimple.h", "cfgloop.h",
1567 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1568 "ipa-inline.h", "dwarf2out.h", NULL
1570 const char *const *ifp;
1571 outf_p gtype_desc_c;
1573 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1574 for (ifp = ifiles; *ifp; ifp++)
1575 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1577 /* Make sure we handle "cfun" specially. */
1578 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1579 oprintf (gtype_desc_c, "#undef cfun\n");
1583 /* For INPF an input file, return the real basename of INPF, with all
1584 the directory components skipped. */
1586 static const char *
1587 get_file_realbasename (const input_file *inpf)
1589 return lbasename (get_input_file_name (inpf));
1592 /* For INPF a filename, return the relative path to INPF from
1593 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1595 const char *
1596 get_file_srcdir_relative_path (const input_file *inpf)
1598 const char *f = get_input_file_name (inpf);
1599 if (strlen (f) > srcdir_len
1600 && IS_DIR_SEPARATOR (f[srcdir_len])
1601 && strncmp (f, srcdir, srcdir_len) == 0)
1602 return f + srcdir_len + 1;
1603 else
1604 return NULL;
1607 /* For INPF an input_file, return the relative path to INPF from
1608 $(srcdir) if the latter is a prefix in INPF, or the real basename
1609 of INPF otherwise. */
1611 static const char *
1612 get_file_basename (const input_file *inpf)
1614 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1616 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1619 /* For F a filename, return the lang_dir_names relative index of the language
1620 directory that is a prefix in F, if any, -1 otherwise. */
1622 static int
1623 get_prefix_langdir_index (const char *f)
1625 size_t f_len = strlen (f);
1626 size_t lang_index;
1628 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1630 const char *langdir = lang_dir_names[lang_index];
1631 size_t langdir_len = strlen (langdir);
1633 if (f_len > langdir_len
1634 && IS_DIR_SEPARATOR (f[langdir_len])
1635 && memcmp (f, langdir, langdir_len) == 0)
1636 return lang_index;
1639 return -1;
1642 /* For INPF an input file, return the name of language directory where
1643 F is located, if any, NULL otherwise. */
1645 static const char *
1646 get_file_langdir (const input_file *inpf)
1648 /* Get the relative path to INPF from $(srcdir) and find the
1649 language by comparing the prefix with language directory names.
1650 If INPF is not even srcdir relative, no point in looking
1651 further. */
1653 int lang_index;
1654 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1655 const char *r;
1657 if (!srcdir_relative_path)
1658 return NULL;
1660 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1661 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1662 r = "c-family";
1663 else if (lang_index >= 0)
1664 r = lang_dir_names[lang_index];
1665 else
1666 r = NULL;
1668 return r;
1671 /* The gt- output file name for INPF. */
1673 static const char *
1674 get_file_gtfilename (const input_file *inpf)
1676 /* Cook up an initial version of the gt- file name from the file real
1677 basename and the language name, if any. */
1679 const char *basename = get_file_realbasename (inpf);
1680 const char *langdir = get_file_langdir (inpf);
1682 char *result =
1683 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1684 : xasprintf ("gt-%s", basename));
1686 /* Then replace all non alphanumerics characters by '-' and change the
1687 extension to ".h". We expect the input filename extension was at least
1688 one character long. */
1690 char *s = result;
1692 for (; *s != '.'; s++)
1693 if (!ISALNUM (*s) && *s != '-')
1694 *s = '-';
1696 memcpy (s, ".h", sizeof (".h"));
1698 return result;
1701 /* Each input_file has its associated output file outf_p. The
1702 association is computed by the function
1703 get_output_file_with_visibility. The associated file is cached
1704 inside input_file in its inpoutf field, so is really computed only
1705 once. Associated output file paths (i.e. output_name-s) are
1706 computed by a rule based regexp machinery, using the files_rules
1707 array of struct file_rule_st. A for_name is also computed, giving
1708 the source file name for which the output_file is generated; it is
1709 often the last component of the input_file path. */
1713 Regexpr machinery to compute the output_name and for_name-s of each
1714 input_file. We have a sequence of file rules which gives the POSIX
1715 extended regular expression to match an input file path, and two
1716 transformed strings for the corresponding output_name and the
1717 corresponding for_name. The transformed string contain dollars: $0
1718 is replaced by the entire match, $1 is replaced by the substring
1719 matching the first parenthesis in the regexp, etc. And $$ is replaced
1720 by a single verbatim dollar. The rule order is important. The
1721 general case is last, and the particular cases should come before.
1722 An action routine can, when needed, update the out_name & for_name
1723 and/or return the appropriate output file. It is invoked only when a
1724 rule is triggered. When a rule is triggered, the output_name and
1725 for_name are computed using their transform string in while $$, $0,
1726 $1, ... are suitably replaced. If there is an action, it is called.
1727 In some few cases, the action can directly return the outf_p, but
1728 usually it just updates the output_name and for_name so should free
1729 them before replacing them. The get_output_file_with_visibility
1730 function creates an outf_p only once per each output_name, so it
1731 scans the output_files list for previously seen output file names.
1734 /* Signature of actions in file rules. */
1735 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1738 struct file_rule_st {
1739 const char* frul_srcexpr; /* Source string for regexp. */
1740 int frul_rflags; /* Flags passed to regcomp, usually
1741 * REG_EXTENDED. */
1742 regex_t* frul_re; /* Compiled regular expression
1743 obtained by regcomp. */
1744 const char* frul_tr_out; /* Transformation string for making
1745 * the output_name, with $1 ... $9 for
1746 * subpatterns and $0 for the whole
1747 * matched filename. */
1748 const char* frul_tr_for; /* Tranformation string for making the
1749 for_name. */
1750 frul_actionrout_t* frul_action; /* The action, if non null, is
1751 * called once the rule matches, on
1752 * the transformed out_name &
1753 * for_name. It could change them
1754 * and/or give the output file. */
1757 /* File rule action handling *.h files. */
1758 static outf_p header_dot_h_frul (input_file*, char**, char**);
1760 /* File rule action handling *.c files. */
1761 static outf_p source_dot_c_frul (input_file*, char**, char**);
1763 #define NULL_REGEX (regex_t*)0
1765 /* The prefix in our regexp-s matching the directory. */
1766 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1768 #define NULL_FRULACT (frul_actionrout_t*)0
1770 /* The array of our rules governing file name generation. Rules order
1771 matters, so change with extreme care! */
1773 struct file_rule_st files_rules[] = {
1774 /* The general rule assumes that files in subdirectories belong to a
1775 particular front-end, and files not in subdirectories are shared.
1776 The following rules deal with exceptions - files that are in
1777 subdirectories and yet are shared, and files that are top-level,
1778 but are not shared. */
1780 /* the c-family/ source directory is special. */
1781 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1782 REG_EXTENDED, NULL_REGEX,
1783 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1785 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1786 REG_EXTENDED, NULL_REGEX,
1787 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1789 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1790 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1791 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1793 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1794 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1796 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1797 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1798 REG_EXTENDED, NULL_REGEX,
1799 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1801 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1802 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1803 REG_EXTENDED, NULL_REGEX,
1804 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1806 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1807 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1808 REG_EXTENDED, NULL_REGEX,
1809 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1811 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1812 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1813 REG_EXTENDED, NULL_REGEX,
1814 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1816 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1817 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1818 REG_EXTENDED, NULL_REGEX,
1819 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1821 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1822 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1823 REG_EXTENDED, NULL_REGEX,
1824 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
1826 /* General cases. For header *.h and source *.c files, we need
1827 * special actions to handle the language. */
1829 /* Source *.c files are using get_file_gtfilename to compute their
1830 output_name and get_file_basename to compute their for_name
1831 through the source_dot_c_frul action. */
1832 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
1833 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
1834 /* Common header files get "gtype-desc.c" as their output_name,
1835 * while language specific header files are handled specially. So
1836 * we need the header_dot_h_frul action. */
1837 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
1838 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
1840 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
1841 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
1843 /* Mandatory null last entry signaling end of rules. */
1844 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
1847 /* Special file rules action for handling *.h header files. It gives
1848 "gtype-desc.c" for common headers and corresponding output
1849 files for language-specific header files. */
1850 static outf_p
1851 header_dot_h_frul (input_file* inpf, char**poutname,
1852 char**pforname ATTRIBUTE_UNUSED)
1854 const char *basename = 0;
1855 int lang_index = 0;
1856 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
1857 (void*) inpf, get_input_file_name (inpf),
1858 *poutname, *pforname);
1859 basename = get_file_basename (inpf);
1860 lang_index = get_prefix_langdir_index (basename);
1861 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
1863 if (lang_index >= 0)
1865 /* The header is language specific. Given output_name &
1866 for_name remains unchanged. The base_files array gives the
1867 outf_p. */
1868 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
1869 (void*) base_files[lang_index],
1870 (base_files[lang_index])->name);
1871 return base_files[lang_index];
1873 else
1875 /* The header is common to all front-end languages. So
1876 output_name is "gtype-desc.c" file. The calling function
1877 get_output_file_with_visibility will find its outf_p. */
1878 free (*poutname);
1879 *poutname = xstrdup ("gtype-desc.c");
1880 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
1881 get_input_file_name (inpf));
1882 return NULL;
1887 /* Special file rules action for handling *.c source files using
1888 * get_file_gtfilename to compute their output_name and
1889 * get_file_basename to compute their for_name. The output_name is
1890 * gt-<LANG>-<BASE>.h for language specific source files, and
1891 * gt-<BASE>.h for common source files. */
1892 static outf_p
1893 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
1895 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
1896 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
1897 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
1898 (void*) inpf, get_input_file_name (inpf),
1899 *poutname, *pforname);
1900 DBGPRINTF ("newoutname %s", newoutname);
1901 DBGPRINTF ("newbasename %s", newbasename);
1902 free (*poutname);
1903 free (*pforname);
1904 *poutname = newoutname;
1905 *pforname = newbasename;
1906 return NULL;
1909 /* Utility function for get_output_file_with_visibility which returns
1910 * a malloc-ed substituted string using TRS on matching of the FILNAM
1911 * file name, using the PMATCH array. */
1912 static char*
1913 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
1914 const char *trs)
1916 struct obstack str_obstack;
1917 char *str = NULL;
1918 char *rawstr = NULL;
1919 const char *pt = NULL;
1920 DBGPRINTF ("filnam %s", filnam);
1921 obstack_init (&str_obstack);
1922 for (pt = trs; *pt; pt++) {
1923 char c = *pt;
1924 if (c == '$')
1926 if (pt[1] == '$')
1928 /* A double dollar $$ is substituted by a single verbatim
1929 dollar, but who really uses dollar signs in file
1930 paths? */
1931 obstack_1grow (&str_obstack, '$');
1933 else if (ISDIGIT (pt[1]))
1935 /* Handle $0 $1 ... $9 by appropriate substitution. */
1936 int dolnum = pt[1] - '0';
1937 int so = pmatch[dolnum].rm_so;
1938 int eo = pmatch[dolnum].rm_eo;
1939 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
1940 if (so>=0 && eo>=so)
1941 obstack_grow (&str_obstack, filnam + so, eo - so);
1943 else
1945 /* This can happen only when files_rules is buggy! */
1946 gcc_unreachable();
1948 /* Always skip the character after the dollar. */
1949 pt++;
1951 else
1952 obstack_1grow (&str_obstack, c);
1954 obstack_1grow (&str_obstack, '\0');
1955 rawstr = XOBFINISH (&str_obstack, char *);
1956 str = xstrdup (rawstr);
1957 obstack_free (&str_obstack, NULL);
1958 DBGPRINTF ("matched replacement %s", str);
1959 rawstr = NULL;
1960 return str;
1964 /* An output file, suitable for definitions, that can see declarations
1965 made in INPF and is linked into every language that uses INPF.
1966 Since the result is cached inside INPF, that argument cannot be
1967 declared constant, but is "almost" constant. */
1969 outf_p
1970 get_output_file_with_visibility (input_file *inpf)
1972 outf_p r;
1973 char *for_name = NULL;
1974 char *output_name = NULL;
1975 const char* inpfname;
1977 /* This can happen when we need a file with visibility on a
1978 structure that we've never seen. We have to just hope that it's
1979 globally visible. */
1980 if (inpf == NULL)
1981 inpf = system_h_file;
1983 /* The result is cached in INPF, so return it if already known. */
1984 if (inpf->inpoutf)
1985 return inpf->inpoutf;
1987 /* In plugin mode, return NULL unless the input_file is one of the
1988 plugin_files. */
1989 if (plugin_files)
1991 size_t i;
1992 for (i = 0; i < nb_plugin_files; i++)
1993 if (inpf == plugin_files[i])
1995 inpf->inpoutf = plugin_output;
1996 return plugin_output;
1999 return NULL;
2002 inpfname = get_input_file_name (inpf);
2004 /* Try each rule in sequence in files_rules until one is triggered. */
2006 int rulix = 0;
2007 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2008 (void*) inpf, inpfname);
2010 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2012 DBGPRINTF ("rulix#%d srcexpr %s",
2013 rulix, files_rules[rulix].frul_srcexpr);
2015 if (!files_rules[rulix].frul_re)
2017 /* Compile the regexpr lazily. */
2018 int err = 0;
2019 files_rules[rulix].frul_re = XCNEW (regex_t);
2020 err = regcomp (files_rules[rulix].frul_re,
2021 files_rules[rulix].frul_srcexpr,
2022 files_rules[rulix].frul_rflags);
2023 if (err)
2025 /* The regular expression compilation fails only when
2026 file_rules is buggy. */
2027 gcc_unreachable ();
2031 output_name = NULL;
2032 for_name = NULL;
2034 /* Match the regexpr and trigger the rule if matched. */
2036 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2037 $3, ... $9. */
2038 regmatch_t pmatch[10];
2039 memset (pmatch, 0, sizeof (pmatch));
2040 if (!regexec (files_rules[rulix].frul_re,
2041 inpfname, 10, pmatch, 0))
2043 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2044 (void*) inpf, inpfname, rulix,
2045 files_rules[rulix].frul_srcexpr);
2046 for_name =
2047 matching_file_name_substitute (inpfname, pmatch,
2048 files_rules[rulix].frul_tr_for);
2049 DBGPRINTF ("for_name %s", for_name);
2050 output_name =
2051 matching_file_name_substitute (inpfname, pmatch,
2052 files_rules[rulix].frul_tr_out);
2053 DBGPRINTF ("output_name %s", output_name);
2054 if (files_rules[rulix].frul_action)
2056 /* Invoke our action routine. */
2057 outf_p of = NULL;
2058 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2059 rulix, output_name, for_name);
2060 of =
2061 (files_rules[rulix].frul_action) (inpf,
2062 &output_name, &for_name);
2063 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2064 rulix, (void*)of, output_name, for_name);
2065 /* If the action routine returned something, give it back
2066 immediately and cache it in inpf. */
2067 if (of)
2069 inpf->inpoutf = of;
2070 return of;
2073 /* The rule matched, and had no action, or that action did
2074 not return any output file but could have changed the
2075 output_name or for_name. We break out of the loop on the
2076 files_rules. */
2077 break;
2079 else
2081 /* The regexpr did not match. */
2082 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2083 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2084 continue;
2089 if (!output_name || !for_name)
2091 /* This is impossible, and could only happen if the files_rules is
2092 incomplete or buggy. */
2093 gcc_unreachable ();
2096 /* Look through to see if we've ever seen this output filename
2097 before. If found, cache the result in inpf. */
2098 for (r = output_files; r; r = r->next)
2099 if (filename_cmp (r->name, output_name) == 0)
2101 inpf->inpoutf = r;
2102 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2103 output_name, for_name);
2104 return r;
2107 /* If not found, create it, and cache it in inpf. */
2108 r = create_file (for_name, output_name);
2110 gcc_assert (r && r->name);
2111 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2112 output_name, for_name);
2113 inpf->inpoutf = r;
2114 return r;
2119 /* The name of an output file, suitable for definitions, that can see
2120 declarations made in INPF and is linked into every language that
2121 uses INPF. */
2123 const char *
2124 get_output_file_name (input_file* inpf)
2126 outf_p o = get_output_file_with_visibility (inpf);
2127 if (o)
2128 return o->name;
2129 return NULL;
2132 /* Check if existing file is equal to the in memory buffer. */
2134 static bool
2135 is_file_equal (outf_p of)
2137 FILE *newfile = fopen (of->name, "r");
2138 size_t i;
2139 bool equal;
2140 if (newfile == NULL)
2141 return false;
2143 equal = true;
2144 for (i = 0; i < of->bufused; i++)
2146 int ch;
2147 ch = fgetc (newfile);
2148 if (ch == EOF || ch != (unsigned char) of->buf[i])
2150 equal = false;
2151 break;
2154 fclose (newfile);
2155 return equal;
2158 /* Copy the output to its final destination,
2159 but don't unnecessarily change modification times. */
2161 static void
2162 close_output_files (void)
2164 int nbwrittenfiles = 0;
2165 outf_p of;
2167 for (of = output_files; of; of = of->next)
2170 if (!is_file_equal (of))
2172 FILE *newfile = NULL;
2173 char *backupname = NULL;
2174 /* Back up the old version of the output file gt-FOO.c as
2175 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2176 if (backup_dir)
2178 backupname = concat (backup_dir, "/",
2179 lbasename (of->name), "~", NULL);
2180 if (!access (of->name, F_OK) && rename (of->name, backupname))
2181 fatal ("failed to back up %s as %s: %s",
2182 of->name, backupname, xstrerror (errno));
2185 newfile = fopen (of->name, "w");
2186 if (newfile == NULL)
2187 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2188 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2189 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2190 if (fclose (newfile) != 0)
2191 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2192 nbwrittenfiles++;
2193 if (verbosity_level >= 2 && backupname)
2194 printf ("%s wrote #%-3d %s backed-up in %s\n",
2195 progname, nbwrittenfiles, of->name, backupname);
2196 else if (verbosity_level >= 1)
2197 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2198 free (backupname);
2200 else
2202 /* output file remains unchanged. */
2203 if (verbosity_level >= 2)
2204 printf ("%s keep %s\n", progname, of->name);
2206 free (of->buf);
2207 of->buf = NULL;
2208 of->bufused = of->buflength = 0;
2210 if (verbosity_level >= 1)
2211 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2214 struct flist
2216 struct flist *next;
2217 int started_p;
2218 const input_file* file;
2219 outf_p f;
2222 struct walk_type_data;
2224 /* For scalars and strings, given the item in 'val'.
2225 For structures, given a pointer to the item in 'val'.
2226 For misc. pointers, given the item in 'val'.
2228 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2229 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2231 /* Parameters for write_types. */
2233 struct write_types_data
2235 const char *prefix;
2236 const char *param_prefix;
2237 const char *subfield_marker_routine;
2238 const char *marker_routine;
2239 const char *reorder_note_routine;
2240 const char *comment;
2241 int skip_hooks; /* skip hook generation if non zero */
2244 static void output_escaped_param (struct walk_type_data *d,
2245 const char *, const char *);
2246 static void output_mangled_typename (outf_p, const_type_p);
2247 static void walk_type (type_p t, struct walk_type_data *d);
2248 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2249 const struct write_types_data *wtd);
2250 static void write_types_process_field
2251 (type_p f, const struct walk_type_data *d);
2252 static void write_types (outf_p output_header,
2253 type_p structures,
2254 type_p param_structs,
2255 const struct write_types_data *wtd);
2256 static void write_types_local_process_field
2257 (type_p f, const struct walk_type_data *d);
2258 static void write_local_func_for_structure
2259 (const_type_p orig_s, type_p s, type_p *param);
2260 static void write_local (outf_p output_header,
2261 type_p structures, type_p param_structs);
2262 static void write_enum_defn (type_p structures, type_p param_structs);
2263 static int contains_scalar_p (type_p t);
2264 static void put_mangled_filename (outf_p, const input_file *);
2265 static void finish_root_table (struct flist *flp, const char *pfx,
2266 const char *tname, const char *lastname,
2267 const char *name);
2268 static void write_root (outf_p, pair_p, type_p, const char *, int,
2269 struct fileloc *, const char *, bool);
2270 static void write_array (outf_p f, pair_p v,
2271 const struct write_types_data *wtd);
2272 static void write_roots (pair_p, bool);
2274 /* Parameters for walk_type. */
2276 struct walk_type_data
2278 process_field_fn process_field;
2279 const void *cookie;
2280 outf_p of;
2281 options_p opt;
2282 const char *val;
2283 const char *prev_val[4];
2284 int indent;
2285 int counter;
2286 const struct fileloc *line;
2287 lang_bitmap bitmap;
2288 type_p *param;
2289 int used_length;
2290 type_p orig_s;
2291 const char *reorder_fn;
2292 bool needs_cast_p;
2293 bool fn_wants_lvalue;
2294 bool in_record_p;
2295 int loopcounter;
2298 /* Print a mangled name representing T to OF. */
2300 static void
2301 output_mangled_typename (outf_p of, const_type_p t)
2303 if (t == NULL)
2304 oprintf (of, "Z");
2305 else
2306 switch (t->kind)
2308 case TYPE_NONE:
2309 gcc_unreachable ();
2310 break;
2311 case TYPE_POINTER:
2312 oprintf (of, "P");
2313 output_mangled_typename (of, t->u.p);
2314 break;
2315 case TYPE_SCALAR:
2316 oprintf (of, "I");
2317 break;
2318 case TYPE_STRING:
2319 oprintf (of, "S");
2320 break;
2321 case TYPE_STRUCT:
2322 case TYPE_UNION:
2323 case TYPE_LANG_STRUCT:
2324 oprintf (of, "%lu%s", (unsigned long) strlen (t->u.s.tag),
2325 t->u.s.tag);
2326 break;
2327 case TYPE_PARAM_STRUCT:
2329 int i;
2330 for (i = 0; i < NUM_PARAM; i++)
2331 if (t->u.param_struct.param[i] != NULL)
2332 output_mangled_typename (of, t->u.param_struct.param[i]);
2333 output_mangled_typename (of, t->u.param_struct.stru);
2335 break;
2336 case TYPE_ARRAY:
2337 gcc_unreachable ();
2341 /* Print PARAM to D->OF processing escapes. D->VAL references the
2342 current object, D->PREV_VAL the object containing the current
2343 object, ONAME is the name of the option and D->LINE is used to
2344 print error messages. */
2346 static void
2347 output_escaped_param (struct walk_type_data *d, const char *param,
2348 const char *oname)
2350 const char *p;
2352 for (p = param; *p; p++)
2353 if (*p != '%')
2354 oprintf (d->of, "%c", *p);
2355 else
2356 switch (*++p)
2358 case 'h':
2359 oprintf (d->of, "(%s)", d->prev_val[2]);
2360 break;
2361 case '0':
2362 oprintf (d->of, "(%s)", d->prev_val[0]);
2363 break;
2364 case '1':
2365 oprintf (d->of, "(%s)", d->prev_val[1]);
2366 break;
2367 case 'a':
2369 const char *pp = d->val + strlen (d->val);
2370 while (pp[-1] == ']')
2371 while (*pp != '[')
2372 pp--;
2373 oprintf (d->of, "%s", pp);
2375 break;
2376 default:
2377 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2378 oname, '%', *p);
2382 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2383 which is of type T. Write code to D->OF to constrain execution (at
2384 the point that D->PROCESS_FIELD is called) to the appropriate
2385 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2386 pointers to those objects. D->PREV_VAL lists the objects
2387 containing the current object, D->OPT is a list of options to
2388 apply, D->INDENT is the current indentation level, D->LINE is used
2389 to print error messages, D->BITMAP indicates which languages to
2390 print the structure for, and D->PARAM is the current parameter
2391 (from an enclosing param_is option). */
2393 static void
2394 walk_type (type_p t, struct walk_type_data *d)
2396 const char *length = NULL;
2397 const char *desc = NULL;
2398 int maybe_undef_p = 0;
2399 int use_param_num = -1;
2400 int use_params_p = 0;
2401 int atomic_p = 0;
2402 options_p oo;
2403 const struct nested_ptr_data *nested_ptr_d = NULL;
2405 d->needs_cast_p = false;
2406 for (oo = d->opt; oo; oo = oo->next)
2407 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2408 length = oo->info.string;
2409 else if (strcmp (oo->name, "maybe_undef") == 0)
2410 maybe_undef_p = 1;
2411 else if (strncmp (oo->name, "use_param", 9) == 0
2412 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2413 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2414 else if (strcmp (oo->name, "use_params") == 0)
2415 use_params_p = 1;
2416 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2417 desc = oo->info.string;
2418 else if (strcmp (oo->name, "mark_hook") == 0)
2420 else if (strcmp (oo->name, "nested_ptr") == 0
2421 && oo->kind == OPTION_NESTED)
2422 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2423 else if (strcmp (oo->name, "dot") == 0)
2425 else if (strcmp (oo->name, "tag") == 0)
2427 else if (strcmp (oo->name, "special") == 0)
2429 else if (strcmp (oo->name, "skip") == 0)
2431 else if (strcmp (oo->name, "atomic") == 0)
2432 atomic_p = 1;
2433 else if (strcmp (oo->name, "default") == 0)
2435 else if (strcmp (oo->name, "param_is") == 0)
2437 else if (strncmp (oo->name, "param", 5) == 0
2438 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2440 else if (strcmp (oo->name, "chain_next") == 0)
2442 else if (strcmp (oo->name, "chain_prev") == 0)
2444 else if (strcmp (oo->name, "chain_circular") == 0)
2446 else if (strcmp (oo->name, "reorder") == 0)
2448 else if (strcmp (oo->name, "variable_size") == 0)
2450 else
2451 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2453 if (d->used_length)
2454 length = NULL;
2456 if (use_params_p)
2458 int pointer_p = t->kind == TYPE_POINTER;
2460 if (pointer_p)
2461 t = t->u.p;
2462 if (!UNION_OR_STRUCT_P (t))
2463 error_at_line (d->line, "`use_params' option on unimplemented type");
2464 else
2465 t = find_param_structure (t, d->param);
2466 if (pointer_p)
2467 t = create_pointer (t);
2470 if (use_param_num != -1)
2472 if (d->param != NULL && d->param[use_param_num] != NULL)
2474 type_p nt = d->param[use_param_num];
2476 if (t->kind == TYPE_ARRAY)
2477 nt = create_array (nt, t->u.a.len);
2478 else if (length != NULL && t->kind == TYPE_POINTER)
2479 nt = create_pointer (nt);
2480 d->needs_cast_p = (t->kind != TYPE_POINTER
2481 && (nt->kind == TYPE_POINTER
2482 || nt->kind == TYPE_STRING));
2483 t = nt;
2485 else
2486 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2489 if (maybe_undef_p
2490 && (t->kind != TYPE_POINTER || !UNION_OR_STRUCT_P (t->u.p)))
2492 error_at_line (d->line,
2493 "field `%s' has invalid option `maybe_undef_p'\n",
2494 d->val);
2495 return;
2498 if (atomic_p && (t->kind != TYPE_POINTER))
2500 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2501 return;
2504 switch (t->kind)
2506 case TYPE_SCALAR:
2507 case TYPE_STRING:
2508 d->process_field (t, d);
2509 break;
2511 case TYPE_POINTER:
2513 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2515 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2516 break;
2519 /* If a pointer type is marked as "atomic", we process the
2520 field itself, but we don't walk the data that they point to.
2522 There are two main cases where we walk types: to mark
2523 pointers that are reachable, and to relocate pointers when
2524 writing a PCH file. In both cases, an atomic pointer is
2525 itself marked or relocated, but the memory that it points
2526 to is left untouched. In the case of PCH, that memory will
2527 be read/written unchanged to the PCH file. */
2528 if (atomic_p)
2530 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2531 d->indent += 2;
2532 d->process_field (t, d);
2533 d->indent -= 2;
2534 oprintf (d->of, "%*s}\n", d->indent, "");
2535 break;
2538 if (!length)
2540 if (!UNION_OR_STRUCT_P (t->u.p)
2541 && t->u.p->kind != TYPE_PARAM_STRUCT)
2543 error_at_line (d->line,
2544 "field `%s' is pointer to unimplemented type",
2545 d->val);
2546 break;
2549 if (nested_ptr_d)
2551 const char *oldprevval2 = d->prev_val[2];
2553 if (!UNION_OR_STRUCT_P (nested_ptr_d->type))
2555 error_at_line (d->line,
2556 "field `%s' has invalid "
2557 "option `nested_ptr'\n", d->val);
2558 return;
2561 d->prev_val[2] = d->val;
2562 oprintf (d->of, "%*s{\n", d->indent, "");
2563 d->indent += 2;
2564 d->val = xasprintf ("x%d", d->counter++);
2565 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2566 (nested_ptr_d->type->kind == TYPE_UNION
2567 ? "union" : "struct"),
2568 nested_ptr_d->type->u.s.tag,
2569 d->fn_wants_lvalue ? "" : "const ", d->val);
2570 oprintf (d->of, "%*s", d->indent + 2, "");
2571 output_escaped_param (d, nested_ptr_d->convert_from,
2572 "nested_ptr");
2573 oprintf (d->of, ";\n");
2575 d->process_field (nested_ptr_d->type, d);
2577 if (d->fn_wants_lvalue)
2579 oprintf (d->of, "%*s%s = ", d->indent, "",
2580 d->prev_val[2]);
2581 d->prev_val[2] = d->val;
2582 output_escaped_param (d, nested_ptr_d->convert_to,
2583 "nested_ptr");
2584 oprintf (d->of, ";\n");
2587 d->indent -= 2;
2588 oprintf (d->of, "%*s}\n", d->indent, "");
2589 d->val = d->prev_val[2];
2590 d->prev_val[2] = oldprevval2;
2592 else
2593 d->process_field (t->u.p, d);
2595 else
2597 int loopcounter = d->loopcounter;
2598 const char *oldval = d->val;
2599 const char *oldprevval3 = d->prev_val[3];
2600 char *newval;
2602 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2603 d->indent += 2;
2604 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2605 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2606 "", loopcounter, loopcounter);
2607 if (!d->in_record_p)
2608 output_escaped_param (d, length, "length");
2609 else
2610 oprintf (d->of, "l%d", loopcounter);
2611 oprintf (d->of, "); i%d++) {\n", loopcounter);
2612 d->indent += 2;
2613 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2614 d->used_length = 1;
2615 d->prev_val[3] = oldval;
2616 walk_type (t->u.p, d);
2617 free (newval);
2618 d->val = oldval;
2619 d->prev_val[3] = oldprevval3;
2620 d->used_length = 0;
2621 d->indent -= 2;
2622 oprintf (d->of, "%*s}\n", d->indent, "");
2623 d->process_field (t, d);
2624 d->indent -= 2;
2625 oprintf (d->of, "%*s}\n", d->indent, "");
2628 break;
2630 case TYPE_ARRAY:
2632 int loopcounter;
2633 const char *oldval = d->val;
2634 char *newval;
2636 /* If it's an array of scalars, we optimize by not generating
2637 any code. */
2638 if (t->u.a.p->kind == TYPE_SCALAR)
2639 break;
2641 if (length)
2642 loopcounter = d->loopcounter;
2643 else
2644 loopcounter = d->counter++;
2646 /* When walking an array, compute the length and store it in a
2647 local variable before walking the array elements, instead of
2648 recomputing the length expression each time through the loop.
2649 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2650 where the length is stored in the first array element,
2651 because otherwise that operand can get overwritten on the
2652 first iteration. */
2653 oprintf (d->of, "%*s{\n", d->indent, "");
2654 d->indent += 2;
2655 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2656 if (!d->in_record_p || !length)
2658 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2659 d->indent, "", loopcounter);
2660 if (length)
2661 output_escaped_param (d, length, "length");
2662 else
2663 oprintf (d->of, "%s", t->u.a.len);
2664 oprintf (d->of, ");\n");
2667 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2668 d->indent, "",
2669 loopcounter, loopcounter, loopcounter, loopcounter);
2670 d->indent += 2;
2671 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2672 d->used_length = 1;
2673 walk_type (t->u.a.p, d);
2674 free (newval);
2675 d->used_length = 0;
2676 d->val = oldval;
2677 d->indent -= 2;
2678 oprintf (d->of, "%*s}\n", d->indent, "");
2679 d->indent -= 2;
2680 oprintf (d->of, "%*s}\n", d->indent, "");
2682 break;
2684 case TYPE_STRUCT:
2685 case TYPE_UNION:
2687 pair_p f;
2688 const char *oldval = d->val;
2689 const char *oldprevval1 = d->prev_val[1];
2690 const char *oldprevval2 = d->prev_val[2];
2691 const int union_p = t->kind == TYPE_UNION;
2692 int seen_default_p = 0;
2693 options_p o;
2694 int lengths_seen = 0;
2695 int endcounter;
2696 bool any_length_seen = false;
2698 if (!t->u.s.line.file)
2699 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2701 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2703 error_at_line (d->line,
2704 "structure `%s' defined for mismatching languages",
2705 t->u.s.tag);
2706 error_at_line (&t->u.s.line, "one structure defined here");
2709 /* Some things may also be defined in the structure's options. */
2710 for (o = t->u.s.opt; o; o = o->next)
2711 if (!desc && strcmp (o->name, "desc") == 0
2712 && o->kind == OPTION_STRING)
2713 desc = o->info.string;
2715 d->prev_val[2] = oldval;
2716 d->prev_val[1] = oldprevval2;
2717 if (union_p)
2719 if (desc == NULL)
2721 error_at_line (d->line,
2722 "missing `desc' option for union `%s'",
2723 t->u.s.tag);
2724 desc = "1";
2726 oprintf (d->of, "%*sswitch (", d->indent, "");
2727 output_escaped_param (d, desc, "desc");
2728 oprintf (d->of, ")\n");
2729 d->indent += 2;
2730 oprintf (d->of, "%*s{\n", d->indent, "");
2733 for (f = t->u.s.fields; f; f = f->next)
2735 options_p oo;
2736 int skip_p = 0;
2737 const char *fieldlength = NULL;
2739 d->reorder_fn = NULL;
2740 for (oo = f->opt; oo; oo = oo->next)
2741 if (strcmp (oo->name, "skip") == 0)
2742 skip_p = 1;
2743 else if (strcmp (oo->name, "length") == 0
2744 && oo->kind == OPTION_STRING)
2745 fieldlength = oo->info.string;
2747 if (skip_p)
2748 continue;
2749 if (fieldlength)
2751 lengths_seen++;
2752 d->counter++;
2753 if (!union_p)
2755 if (!any_length_seen)
2757 oprintf (d->of, "%*s{\n", d->indent, "");
2758 d->indent += 2;
2760 any_length_seen = true;
2762 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2763 d->indent, "", d->counter - 1);
2764 output_escaped_param (d, fieldlength, "length");
2765 oprintf (d->of, ");\n");
2769 endcounter = d->counter;
2771 for (f = t->u.s.fields; f; f = f->next)
2773 options_p oo;
2774 const char *dot = ".";
2775 const char *tagid = NULL;
2776 int skip_p = 0;
2777 int default_p = 0;
2778 int use_param_p = 0;
2779 const char *fieldlength = NULL;
2780 char *newval;
2782 d->reorder_fn = NULL;
2783 for (oo = f->opt; oo; oo = oo->next)
2784 if (strcmp (oo->name, "dot") == 0
2785 && oo->kind == OPTION_STRING)
2786 dot = oo->info.string;
2787 else if (strcmp (oo->name, "tag") == 0
2788 && oo->kind == OPTION_STRING)
2789 tagid = oo->info.string;
2790 else if (strcmp (oo->name, "skip") == 0)
2791 skip_p = 1;
2792 else if (strcmp (oo->name, "default") == 0)
2793 default_p = 1;
2794 else if (strcmp (oo->name, "reorder") == 0
2795 && oo->kind == OPTION_STRING)
2796 d->reorder_fn = oo->info.string;
2797 else if (strncmp (oo->name, "use_param", 9) == 0
2798 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2799 use_param_p = 1;
2800 else if (strcmp (oo->name, "length") == 0
2801 && oo->kind == OPTION_STRING)
2802 fieldlength = oo->info.string;
2804 if (skip_p)
2805 continue;
2807 if (union_p && tagid)
2809 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
2810 d->indent += 2;
2812 else if (union_p && default_p)
2814 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2815 d->indent += 2;
2816 seen_default_p = 1;
2818 else if (!union_p && (default_p || tagid))
2819 error_at_line (d->line,
2820 "can't use `%s' outside a union on field `%s'",
2821 default_p ? "default" : "tag", f->name);
2822 else if (union_p && !(default_p || tagid)
2823 && f->type->kind == TYPE_SCALAR)
2825 fprintf (stderr,
2826 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
2827 get_input_file_name (d->line->file), d->line->line,
2828 f->name);
2829 continue;
2831 else if (union_p && !(default_p || tagid))
2832 error_at_line (d->line,
2833 "field `%s' is missing `tag' or `default' option",
2834 f->name);
2836 if (fieldlength)
2838 d->loopcounter = endcounter - lengths_seen--;
2841 d->line = &f->line;
2842 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
2843 d->opt = f->opt;
2844 d->used_length = false;
2845 d->in_record_p = !union_p;
2847 if (union_p && use_param_p && d->param == NULL)
2848 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
2849 else
2850 walk_type (f->type, d);
2852 d->in_record_p = false;
2854 free (newval);
2856 if (union_p)
2858 oprintf (d->of, "%*sbreak;\n", d->indent, "");
2859 d->indent -= 2;
2862 d->reorder_fn = NULL;
2864 d->val = oldval;
2865 d->prev_val[1] = oldprevval1;
2866 d->prev_val[2] = oldprevval2;
2868 if (union_p && !seen_default_p)
2870 oprintf (d->of, "%*sdefault:\n", d->indent, "");
2871 oprintf (d->of, "%*s break;\n", d->indent, "");
2873 if (union_p)
2875 oprintf (d->of, "%*s}\n", d->indent, "");
2876 d->indent -= 2;
2878 if (any_length_seen)
2880 d->indent -= 2;
2881 oprintf (d->of, "%*s}\n", d->indent, "");
2884 break;
2886 case TYPE_LANG_STRUCT:
2888 type_p nt;
2889 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
2890 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
2891 break;
2892 if (nt == NULL)
2893 error_at_line (d->line, "structure `%s' differs between languages",
2894 t->u.s.tag);
2895 else
2896 walk_type (nt, d);
2898 break;
2900 case TYPE_PARAM_STRUCT:
2902 type_p *oldparam = d->param;
2904 d->param = t->u.param_struct.param;
2905 walk_type (t->u.param_struct.stru, d);
2906 d->param = oldparam;
2908 break;
2910 default:
2911 gcc_unreachable ();
2915 /* process_field routine for marking routines. */
2917 static void
2918 write_types_process_field (type_p f, const struct walk_type_data *d)
2920 const struct write_types_data *wtd;
2921 const char *cast = d->needs_cast_p ? "(void *)" : "";
2922 wtd = (const struct write_types_data *) d->cookie;
2924 switch (f->kind)
2926 case TYPE_NONE:
2927 gcc_unreachable ();
2928 case TYPE_POINTER:
2929 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
2930 wtd->subfield_marker_routine, cast, d->val);
2931 if (wtd->param_prefix)
2933 if (f->u.p->kind == TYPE_SCALAR)
2934 /* The current type is a pointer to a scalar (so not
2935 considered like a pointer to instances of user defined
2936 types) and we are seeing it; it means we must be even
2937 more careful about the second argument of the
2938 SUBFIELD_MARKER_ROUTINE call. That argument must
2939 always be the instance of the type for which
2940 write_func_for_structure was called - this really is
2941 what the function SUBFIELD_MARKER_ROUTINE expects.
2942 That is, it must be an instance of the ORIG_S type
2943 parameter of write_func_for_structure. The convention
2944 is that that argument must be "x" in that case (as set
2945 by write_func_for_structure). The problem is, we can't
2946 count on d->prev_val[3] to be always set to "x" in that
2947 case. Sometimes walk_type can set it to something else
2948 (to e.g cooperate with write_array when called from
2949 write_roots). So let's set it to "x" here then. */
2950 oprintf (d->of, ", x");
2951 else
2952 oprintf (d->of, ", %s", d->prev_val[3]);
2953 if (d->orig_s)
2955 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
2956 output_mangled_typename (d->of, d->orig_s);
2958 else
2959 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
2961 if (f->u.p->kind == TYPE_PARAM_STRUCT
2962 && f->u.p->u.s.line.file != NULL)
2964 oprintf (d->of, ", gt_e_");
2965 output_mangled_typename (d->of, f);
2967 else if (UNION_OR_STRUCT_P (f) && f->u.p->u.s.line.file != NULL)
2969 oprintf (d->of, ", gt_ggc_e_");
2970 output_mangled_typename (d->of, f);
2972 else
2973 oprintf (d->of, ", gt_types_enum_last");
2975 oprintf (d->of, ");\n");
2976 if (d->reorder_fn && wtd->reorder_note_routine)
2977 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
2978 wtd->reorder_note_routine, cast, d->val,
2979 d->prev_val[3], d->reorder_fn);
2980 break;
2982 case TYPE_STRING:
2983 case TYPE_STRUCT:
2984 case TYPE_UNION:
2985 case TYPE_LANG_STRUCT:
2986 case TYPE_PARAM_STRUCT:
2987 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
2988 output_mangled_typename (d->of, f);
2989 oprintf (d->of, " (%s%s);\n", cast, d->val);
2990 if (d->reorder_fn && wtd->reorder_note_routine)
2991 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
2992 wtd->reorder_note_routine, cast, d->val, cast, d->val,
2993 d->reorder_fn);
2994 break;
2996 case TYPE_SCALAR:
2997 break;
2999 case TYPE_ARRAY:
3000 gcc_unreachable ();
3004 /* A subroutine of write_func_for_structure. Write the enum tag for S. */
3006 static void
3007 output_type_enum (outf_p of, type_p s)
3009 if (s->kind == TYPE_PARAM_STRUCT && s->u.param_struct.line.file != NULL)
3011 oprintf (of, ", gt_e_");
3012 output_mangled_typename (of, s);
3014 else if (UNION_OR_STRUCT_P (s) && s->u.s.line.file != NULL)
3016 oprintf (of, ", gt_ggc_e_");
3017 output_mangled_typename (of, s);
3019 else
3020 oprintf (of, ", gt_types_enum_last");
3023 /* Return an output file that is suitable for definitions which can
3024 reference struct S */
3026 static outf_p
3027 get_output_file_for_structure (const_type_p s, type_p *param)
3029 const input_file *fn;
3030 int i;
3032 gcc_assert (UNION_OR_STRUCT_P (s));
3033 fn = s->u.s.line.file;
3035 /* This is a hack, and not the good kind either. */
3036 for (i = NUM_PARAM - 1; i >= 0; i--)
3037 if (param && param[i] && param[i]->kind == TYPE_POINTER
3038 && UNION_OR_STRUCT_P (param[i]->u.p))
3039 fn = param[i]->u.p->u.s.line.file;
3041 /* The call to get_output_file_with_visibility may update fn by
3042 caching its result inside, so we need the CONST_CAST. */
3043 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3046 /* For S, a structure that's part of ORIG_S, and using parameters
3047 PARAM, write out a routine that:
3048 - Takes a parameter, a void * but actually of type *S
3049 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3050 field of S or its substructures and (in some cases) things
3051 that are pointed to by S.
3054 static void
3055 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3056 const struct write_types_data *wtd)
3058 const char *chain_next = NULL;
3059 const char *chain_prev = NULL;
3060 const char *chain_circular = NULL;
3061 const char *mark_hook_name = NULL;
3062 options_p opt;
3063 struct walk_type_data d;
3065 memset (&d, 0, sizeof (d));
3066 d.of = get_output_file_for_structure (s, param);
3067 for (opt = s->u.s.opt; opt; opt = opt->next)
3068 if (strcmp (opt->name, "chain_next") == 0
3069 && opt->kind == OPTION_STRING)
3070 chain_next = opt->info.string;
3071 else if (strcmp (opt->name, "chain_prev") == 0
3072 && opt->kind == OPTION_STRING)
3073 chain_prev = opt->info.string;
3074 else if (strcmp (opt->name, "chain_circular") == 0
3075 && opt->kind == OPTION_STRING)
3076 chain_circular = opt->info.string;
3077 else if (strcmp (opt->name, "mark_hook") == 0
3078 && opt->kind == OPTION_STRING)
3079 mark_hook_name = opt->info.string;
3080 if (chain_prev != NULL && chain_next == NULL)
3081 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3082 if (chain_circular != NULL && chain_next != NULL)
3083 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3084 if (chain_circular != NULL)
3085 chain_next = chain_circular;
3087 d.process_field = write_types_process_field;
3088 d.cookie = wtd;
3089 d.orig_s = orig_s;
3090 d.opt = s->u.s.opt;
3091 d.line = &s->u.s.line;
3092 d.bitmap = s->u.s.bitmap;
3093 d.param = param;
3094 d.prev_val[0] = "*x";
3095 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3096 d.prev_val[3] = "x";
3097 d.val = "(*x)";
3099 oprintf (d.of, "\n");
3100 oprintf (d.of, "void\n");
3101 if (param == NULL)
3102 oprintf (d.of, "gt_%sx_%s", wtd->prefix, orig_s->u.s.tag);
3103 else
3105 oprintf (d.of, "gt_%s_", wtd->prefix);
3106 output_mangled_typename (d.of, orig_s);
3108 oprintf (d.of, " (void *x_p)\n");
3109 oprintf (d.of, "{\n");
3110 oprintf (d.of, " %s %s * %sx = (%s %s *)x_p;\n",
3111 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3112 chain_next == NULL ? "const " : "",
3113 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3114 if (chain_next != NULL)
3115 oprintf (d.of, " %s %s * xlimit = x;\n",
3116 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3117 if (chain_next == NULL)
3119 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3120 if (wtd->param_prefix)
3122 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3123 output_mangled_typename (d.of, orig_s);
3124 output_type_enum (d.of, orig_s);
3126 oprintf (d.of, "))\n");
3128 else
3130 if (chain_circular != NULL)
3131 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3132 else
3133 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3134 if (wtd->param_prefix)
3136 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3137 output_mangled_typename (d.of, orig_s);
3138 output_type_enum (d.of, orig_s);
3140 oprintf (d.of, "))\n");
3141 if (chain_circular != NULL)
3142 oprintf (d.of, " return;\n do\n");
3143 if (mark_hook_name && !wtd->skip_hooks)
3145 oprintf (d.of, " {\n");
3146 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3148 oprintf (d.of, " xlimit = (");
3149 d.prev_val[2] = "*xlimit";
3150 output_escaped_param (&d, chain_next, "chain_next");
3151 oprintf (d.of, ");\n");
3152 if (mark_hook_name && !wtd->skip_hooks)
3153 oprintf (d.of, " }\n");
3154 if (chain_prev != NULL)
3156 oprintf (d.of, " if (x != xlimit)\n");
3157 oprintf (d.of, " for (;;)\n");
3158 oprintf (d.of, " {\n");
3159 oprintf (d.of, " %s %s * const xprev = (",
3160 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3162 d.prev_val[2] = "*x";
3163 output_escaped_param (&d, chain_prev, "chain_prev");
3164 oprintf (d.of, ");\n");
3165 oprintf (d.of, " if (xprev == NULL) break;\n");
3166 oprintf (d.of, " x = xprev;\n");
3167 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3168 if (wtd->param_prefix)
3170 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3171 output_mangled_typename (d.of, orig_s);
3172 output_type_enum (d.of, orig_s);
3174 oprintf (d.of, ");\n");
3175 oprintf (d.of, " }\n");
3177 if (chain_circular != NULL)
3179 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3180 if (wtd->param_prefix)
3182 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3183 output_mangled_typename (d.of, orig_s);
3184 output_type_enum (d.of, orig_s);
3186 oprintf (d.of, "));\n");
3187 if (mark_hook_name && !wtd->skip_hooks)
3188 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3189 oprintf (d.of, " do\n");
3191 else
3192 oprintf (d.of, " while (x != xlimit)\n");
3194 oprintf (d.of, " {\n");
3195 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3197 oprintf (d.of, " %s (x);\n", mark_hook_name);
3199 d.prev_val[2] = "*x";
3200 d.indent = 6;
3201 walk_type (s, &d);
3203 if (chain_next != NULL)
3205 oprintf (d.of, " x = (");
3206 output_escaped_param (&d, chain_next, "chain_next");
3207 oprintf (d.of, ");\n");
3210 oprintf (d.of, " }\n");
3211 if (chain_circular != NULL)
3212 oprintf (d.of, " while (x != xlimit);\n");
3213 oprintf (d.of, "}\n");
3216 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3218 static void
3219 write_types (outf_p output_header, type_p structures, type_p param_structs,
3220 const struct write_types_data *wtd)
3222 int nbfun = 0; /* Count the emitted functions. */
3223 type_p s;
3225 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3226 /* We first emit the macros and the declarations. Functions' code is
3227 emitted afterwards. This is needed in plugin mode. */
3228 oprintf (output_header, "/* macros and declarations */\n");
3229 for (s = structures; s; s = s->next)
3230 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3232 options_p opt;
3234 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3235 continue;
3237 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3238 output_mangled_typename (output_header, s);
3239 oprintf (output_header, "(X) do { \\\n");
3240 oprintf (output_header,
3241 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3242 s->u.s.tag);
3243 oprintf (output_header, " } while (0)\n");
3245 for (opt = s->u.s.opt; opt; opt = opt->next)
3246 if (strcmp (opt->name, "ptr_alias") == 0
3247 && opt->kind == OPTION_TYPE)
3249 const_type_p const t = (const_type_p) opt->info.type;
3250 if (t->kind == TYPE_STRUCT
3251 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3252 oprintf (output_header,
3253 "#define gt_%sx_%s gt_%sx_%s\n",
3254 wtd->prefix, s->u.s.tag, wtd->prefix, t->u.s.tag);
3255 else
3256 error_at_line (&s->u.s.line,
3257 "structure alias is not a structure");
3258 break;
3260 if (opt)
3261 continue;
3263 /* Declare the marker procedure only once. */
3264 oprintf (output_header,
3265 "extern void gt_%sx_%s (void *);\n",
3266 wtd->prefix, s->u.s.tag);
3268 if (s->u.s.line.file == NULL)
3270 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3271 s->u.s.tag);
3272 continue;
3276 for (s = param_structs; s; s = s->next)
3277 if (s->gc_used == GC_POINTED_TO)
3279 type_p stru = s->u.param_struct.stru;
3281 /* Declare the marker procedure. */
3282 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3283 output_mangled_typename (output_header, s);
3284 oprintf (output_header, " (void *);\n");
3286 if (stru->u.s.line.file == NULL)
3288 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3289 stru->u.s.tag);
3290 continue;
3294 /* At last we emit the functions code. */
3295 oprintf (output_header, "\n/* functions code */\n");
3296 for (s = structures; s; s = s->next)
3297 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3299 options_p opt;
3301 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3302 continue;
3303 for (opt = s->u.s.opt; opt; opt = opt->next)
3304 if (strcmp (opt->name, "ptr_alias") == 0)
3305 break;
3306 if (opt)
3307 continue;
3309 if (s->kind == TYPE_LANG_STRUCT)
3311 type_p ss;
3312 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3314 nbfun++;
3315 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3316 nbfun, (void*) ss, ss->u.s.tag);
3317 write_func_for_structure (s, ss, NULL, wtd);
3320 else
3322 nbfun++;
3323 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3324 nbfun, (void*) s, s->u.s.tag);
3325 write_func_for_structure (s, s, NULL, wtd);
3328 else
3330 /* Structure s is not possibly pointed to, so can be ignored. */
3331 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3332 (void*)s, s->u.s.tag,
3333 (int) s->gc_used);
3336 for (s = param_structs; s; s = s->next)
3337 if (s->gc_used == GC_POINTED_TO)
3339 type_p *param = s->u.param_struct.param;
3340 type_p stru = s->u.param_struct.stru;
3341 if (stru->u.s.line.file == NULL)
3342 continue;
3343 if (stru->kind == TYPE_LANG_STRUCT)
3345 type_p ss;
3346 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3348 nbfun++;
3349 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3350 nbfun, (void*) ss, ss->u.s.tag);
3351 write_func_for_structure (s, ss, param, wtd);
3354 else
3356 nbfun++;
3357 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3358 nbfun, (void*) s,
3359 (void*) stru, stru->u.s.tag);
3360 write_func_for_structure (s, stru, param, wtd);
3363 else
3365 /* Param structure s is not pointed to, so should be ignored. */
3366 DBGPRINTF ("ignored s @ %p", (void*)s);
3368 if (verbosity_level >= 2)
3369 printf ("%s emitted %d routines for %s\n",
3370 progname, nbfun, wtd->comment);
3373 static const struct write_types_data ggc_wtd = {
3374 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3375 "GC marker procedures. ",
3376 FALSE
3379 static const struct write_types_data pch_wtd = {
3380 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3381 "gt_pch_note_reorder",
3382 "PCH type-walking procedures. ",
3383 TRUE
3386 /* Write out the local pointer-walking routines. */
3388 /* process_field routine for local pointer-walking. */
3390 static void
3391 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3393 switch (f->kind)
3395 case TYPE_POINTER:
3396 case TYPE_STRUCT:
3397 case TYPE_UNION:
3398 case TYPE_LANG_STRUCT:
3399 case TYPE_PARAM_STRUCT:
3400 case TYPE_STRING:
3401 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3402 d->prev_val[3]);
3403 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3404 break;
3406 case TYPE_SCALAR:
3407 break;
3409 default:
3410 gcc_unreachable ();
3414 /* For S, a structure that's part of ORIG_S, and using parameters
3415 PARAM, write out a routine that:
3416 - Is of type gt_note_pointers
3417 - Calls PROCESS_FIELD on each field of S or its substructures.
3420 static void
3421 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3423 struct walk_type_data d;
3425 memset (&d, 0, sizeof (d));
3426 d.of = get_output_file_for_structure (s, param);
3427 d.process_field = write_types_local_process_field;
3428 d.opt = s->u.s.opt;
3429 d.line = &s->u.s.line;
3430 d.bitmap = s->u.s.bitmap;
3431 d.param = param;
3432 d.prev_val[0] = d.prev_val[2] = "*x";
3433 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3434 d.prev_val[3] = "x";
3435 d.val = "(*x)";
3436 d.fn_wants_lvalue = true;
3438 oprintf (d.of, "\n");
3439 oprintf (d.of, "void\n");
3440 oprintf (d.of, "gt_pch_p_");
3441 output_mangled_typename (d.of, orig_s);
3442 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3443 "\tvoid *x_p,\n"
3444 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3445 "\tATTRIBUTE_UNUSED void *cookie)\n");
3446 oprintf (d.of, "{\n");
3447 oprintf (d.of, " %s %s * const x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3448 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3449 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3450 d.indent = 2;
3451 walk_type (s, &d);
3452 oprintf (d.of, "}\n");
3455 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3457 static void
3458 write_local (outf_p output_header, type_p structures, type_p param_structs)
3460 type_p s;
3462 if (!output_header)
3463 return;
3464 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3465 for (s = structures; s; s = s->next)
3466 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3468 options_p opt;
3470 if (s->u.s.line.file == NULL)
3471 continue;
3472 for (opt = s->u.s.opt; opt; opt = opt->next)
3473 if (strcmp (opt->name, "ptr_alias") == 0
3474 && opt->kind == OPTION_TYPE)
3476 const_type_p const t = (const_type_p) opt->info.type;
3477 if (t->kind == TYPE_STRUCT
3478 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3480 oprintf (output_header, "#define gt_pch_p_");
3481 output_mangled_typename (output_header, s);
3482 oprintf (output_header, " gt_pch_p_");
3483 output_mangled_typename (output_header, t);
3484 oprintf (output_header, "\n");
3486 else
3487 error_at_line (&s->u.s.line,
3488 "structure alias is not a structure");
3489 break;
3491 if (opt)
3492 continue;
3494 /* Declare the marker procedure only once. */
3495 oprintf (output_header, "extern void gt_pch_p_");
3496 output_mangled_typename (output_header, s);
3497 oprintf (output_header,
3498 "\n (void *, void *, gt_pointer_operator, void *);\n");
3500 if (s->kind == TYPE_LANG_STRUCT)
3502 type_p ss;
3503 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3504 write_local_func_for_structure (s, ss, NULL);
3506 else
3507 write_local_func_for_structure (s, s, NULL);
3510 for (s = param_structs; s; s = s->next)
3511 if (s->gc_used == GC_POINTED_TO)
3513 type_p *param = s->u.param_struct.param;
3514 type_p stru = s->u.param_struct.stru;
3516 /* Declare the marker procedure. */
3517 oprintf (output_header, "extern void gt_pch_p_");
3518 output_mangled_typename (output_header, s);
3519 oprintf (output_header,
3520 "\n (void *, void *, gt_pointer_operator, void *);\n");
3522 if (stru->u.s.line.file == NULL)
3524 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3525 stru->u.s.tag);
3526 continue;
3529 if (stru->kind == TYPE_LANG_STRUCT)
3531 type_p ss;
3532 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3533 write_local_func_for_structure (s, ss, param);
3535 else
3536 write_local_func_for_structure (s, stru, param);
3540 /* Nonzero if S is a type for which typed GC allocators should be output. */
3542 #define USED_BY_TYPED_GC_P(s) \
3543 (((s->kind == TYPE_POINTER) \
3544 && ((s->u.p->gc_used == GC_POINTED_TO) \
3545 || (s->u.p->gc_used == GC_USED))) \
3546 || (UNION_OR_STRUCT_P (s) && \
3547 (((s)->gc_used == GC_POINTED_TO) \
3548 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
3549 && s->u.s.line.file != NULL) \
3550 || ((s)->gc_used == GC_USED \
3551 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
3554 /* Write out the 'enum' definition for gt_types_enum. */
3556 static void
3557 write_enum_defn (type_p structures, type_p param_structs)
3559 type_p s;
3560 int nbstruct = 0;
3561 int nbparamstruct = 0;
3563 if (!header_file)
3564 return;
3565 oprintf (header_file, "\n/* Enumeration of types known. */\n");
3566 oprintf (header_file, "enum gt_types_enum {\n");
3567 for (s = structures; s; s = s->next)
3568 if (USED_BY_TYPED_GC_P (s))
3570 nbstruct++;
3571 DBGPRINTF ("write_enum_defn s @ %p nbstruct %d",
3572 (void*) s, nbstruct);
3573 if (UNION_OR_STRUCT_P (s))
3574 DBGPRINTF ("write_enum_defn s %p #%d is unionorstruct tagged %s",
3575 (void*) s, nbstruct, s->u.s.tag);
3576 oprintf (header_file, " gt_ggc_e_");
3577 output_mangled_typename (header_file, s);
3578 oprintf (header_file, ",\n");
3580 for (s = param_structs; s; s = s->next)
3581 if (s->gc_used == GC_POINTED_TO)
3583 nbparamstruct++;
3584 DBGPRINTF ("write_enum_defn s %p nbparamstruct %d",
3585 (void*) s, nbparamstruct);
3586 oprintf (header_file, " gt_e_");
3587 output_mangled_typename (header_file, s);
3588 oprintf (header_file, ",\n");
3590 oprintf (header_file, " gt_types_enum_last\n");
3591 oprintf (header_file, "};\n");
3592 if (verbosity_level >= 2)
3593 printf ("%s handled %d GTY-ed structures & %d parameterized structures.\n",
3594 progname, nbstruct, nbparamstruct);
3598 /* Might T contain any non-pointer elements? */
3600 static int
3601 contains_scalar_p (type_p t)
3603 switch (t->kind)
3605 case TYPE_STRING:
3606 case TYPE_POINTER:
3607 return 0;
3608 case TYPE_ARRAY:
3609 return contains_scalar_p (t->u.a.p);
3610 default:
3611 /* Could also check for structures that have no non-pointer
3612 fields, but there aren't enough of those to worry about. */
3613 return 1;
3617 /* Mangle INPF and print it to F. */
3619 static void
3620 put_mangled_filename (outf_p f, const input_file *inpf)
3622 /* The call to get_output_file_name may indirectly update fn since
3623 get_output_file_with_visibility caches its result inside, so we
3624 need the CONST_CAST. */
3625 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
3626 if (!f || !name)
3627 return;
3628 for (; *name != 0; name++)
3629 if (ISALNUM (*name))
3630 oprintf (f, "%c", *name);
3631 else
3632 oprintf (f, "%c", '_');
3635 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
3636 LASTNAME, and NAME are all strings to insert in various places in
3637 the resulting code. */
3639 static void
3640 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
3641 const char *tname, const char *name)
3643 struct flist *fli2;
3645 for (fli2 = flp; fli2; fli2 = fli2->next)
3646 if (fli2->started_p)
3648 oprintf (fli2->f, " %s\n", lastname);
3649 oprintf (fli2->f, "};\n\n");
3652 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
3653 if (fli2->started_p)
3655 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
3656 int fnum;
3658 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
3659 if (bitmap & 1)
3661 oprintf (base_files[fnum],
3662 "extern const struct %s gt_%s_", tname, pfx);
3663 put_mangled_filename (base_files[fnum], fli2->file);
3664 oprintf (base_files[fnum], "[];\n");
3669 size_t fnum;
3670 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3671 oprintf (base_files[fnum],
3672 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
3676 for (fli2 = flp; fli2; fli2 = fli2->next)
3677 if (fli2->started_p)
3679 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
3680 int fnum;
3682 fli2->started_p = 0;
3684 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
3685 if (bitmap & 1)
3687 oprintf (base_files[fnum], " gt_%s_", pfx);
3688 put_mangled_filename (base_files[fnum], fli2->file);
3689 oprintf (base_files[fnum], ",\n");
3694 size_t fnum;
3695 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
3697 oprintf (base_files[fnum], " NULL\n");
3698 oprintf (base_files[fnum], "};\n");
3703 /* Write the first three fields (pointer, count and stride) for
3704 root NAME to F. V and LINE are as for write_root.
3706 Return true if the entry could be written; return false on error. */
3708 static bool
3709 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
3711 type_p ap;
3713 if (!v)
3715 error_at_line (line, "`%s' is too complex to be a root", name);
3716 return false;
3719 oprintf (f, " {\n");
3720 oprintf (f, " &%s,\n", name);
3721 oprintf (f, " 1");
3723 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3724 if (ap->u.a.len[0])
3725 oprintf (f, " * (%s)", ap->u.a.len);
3726 else if (ap == v->type)
3727 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
3728 oprintf (f, ",\n");
3729 oprintf (f, " sizeof (%s", v->name);
3730 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
3731 oprintf (f, "[0]");
3732 oprintf (f, "),\n");
3733 return true;
3736 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
3737 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
3738 of the caller. */
3740 static void
3741 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
3742 int has_length, struct fileloc *line, const char *if_marked,
3743 bool emit_pch, type_p field_type, const char *field_name)
3745 struct pair newv;
3746 /* If the field reference is relative to V, rather than to some
3747 subcomponent of V, we can mark any subarrays with a single stride.
3748 We're effectively treating the field as a global variable in its
3749 own right. */
3750 if (v && type == v->type)
3752 newv = *v;
3753 newv.type = field_type;
3754 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
3755 v = &newv;
3757 /* Otherwise, any arrays nested in the structure are too complex to
3758 handle. */
3759 else if (field_type->kind == TYPE_ARRAY)
3760 v = NULL;
3761 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
3762 has_length, line, if_marked, emit_pch);
3765 /* Write out to F the table entry and any marker routines needed to
3766 mark NAME as TYPE. V can be one of three values:
3768 - null, if NAME is too complex to represent using a single
3769 count and stride. In this case, it is an error for NAME to
3770 contain any gc-ed data.
3772 - the outermost array that contains NAME, if NAME is part of an array.
3774 - the C variable that contains NAME, if NAME is not part of an array.
3776 LINE is the line of the C source that declares the root variable.
3777 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
3778 is nonzero iff we are building the root table for hash table caches. */
3780 static void
3781 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
3782 struct fileloc *line, const char *if_marked, bool emit_pch)
3784 switch (type->kind)
3786 case TYPE_STRUCT:
3788 pair_p fld;
3789 for (fld = type->u.s.fields; fld; fld = fld->next)
3791 int skip_p = 0;
3792 const char *desc = NULL;
3793 options_p o;
3795 for (o = fld->opt; o; o = o->next)
3796 if (strcmp (o->name, "skip") == 0)
3797 skip_p = 1;
3798 else if (strcmp (o->name, "desc") == 0
3799 && o->kind == OPTION_STRING)
3800 desc = o->info.string;
3801 else if (strcmp (o->name, "param_is") == 0)
3803 else
3804 error_at_line (line,
3805 "field `%s' of global `%s' has unknown option `%s'",
3806 fld->name, name, o->name);
3808 if (skip_p)
3809 continue;
3810 else if (desc && fld->type->kind == TYPE_UNION)
3812 pair_p validf = NULL;
3813 pair_p ufld;
3815 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
3817 const char *tag = NULL;
3818 options_p oo;
3819 for (oo = ufld->opt; oo; oo = oo->next)
3820 if (strcmp (oo->name, "tag") == 0
3821 && oo->kind == OPTION_STRING)
3822 tag = oo->info.string;
3823 if (tag == NULL || strcmp (tag, desc) != 0)
3824 continue;
3825 if (validf != NULL)
3826 error_at_line (line,
3827 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
3828 name, fld->name, validf->name,
3829 name, fld->name, ufld->name, tag);
3830 validf = ufld;
3832 if (validf != NULL)
3833 write_field_root (f, v, type, name, 0, line, if_marked,
3834 emit_pch, validf->type,
3835 ACONCAT ((fld->name, ".",
3836 validf->name, NULL)));
3838 else if (desc)
3839 error_at_line (line,
3840 "global `%s.%s' has `desc' option but is not union",
3841 name, fld->name);
3842 else
3843 write_field_root (f, v, type, name, 0, line, if_marked,
3844 emit_pch, fld->type, fld->name);
3847 break;
3849 case TYPE_ARRAY:
3851 char *newname;
3852 newname = xasprintf ("%s[0]", name);
3853 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
3854 emit_pch);
3855 free (newname);
3857 break;
3859 case TYPE_POINTER:
3861 type_p tp;
3863 if (!start_root_entry (f, v, name, line))
3864 return;
3866 tp = type->u.p;
3868 if (!has_length && UNION_OR_STRUCT_P (tp))
3870 oprintf (f, " &gt_ggc_mx_%s,\n", tp->u.s.tag);
3871 if (emit_pch)
3872 oprintf (f, " &gt_pch_nx_%s", tp->u.s.tag);
3873 else
3874 oprintf (f, " NULL");
3876 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
3878 oprintf (f, " &gt_ggc_m_");
3879 output_mangled_typename (f, tp);
3880 if (emit_pch)
3882 oprintf (f, ",\n &gt_pch_n_");
3883 output_mangled_typename (f, tp);
3885 else
3886 oprintf (f, ",\n NULL");
3888 else if (has_length
3889 && (tp->kind == TYPE_POINTER || UNION_OR_STRUCT_P (tp)))
3891 oprintf (f, " &gt_ggc_ma_%s,\n", name);
3892 if (emit_pch)
3893 oprintf (f, " &gt_pch_na_%s", name);
3894 else
3895 oprintf (f, " NULL");
3897 else
3899 error_at_line (line,
3900 "global `%s' is pointer to unimplemented type",
3901 name);
3903 if (if_marked)
3904 oprintf (f, ",\n &%s", if_marked);
3905 oprintf (f, "\n },\n");
3907 break;
3909 case TYPE_STRING:
3911 if (!start_root_entry (f, v, name, line))
3912 return;
3914 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
3915 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
3916 oprintf (f, " },\n");
3918 break;
3920 case TYPE_SCALAR:
3921 break;
3923 default:
3924 error_at_line (line, "global `%s' is unimplemented type", name);
3928 /* This generates a routine to walk an array. */
3930 static void
3931 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
3933 struct walk_type_data d;
3934 char *prevval3;
3936 memset (&d, 0, sizeof (d));
3937 d.of = f;
3938 d.cookie = wtd;
3939 d.indent = 2;
3940 d.line = &v->line;
3941 d.opt = v->opt;
3942 d.bitmap = get_lang_bitmap (v->line.file);
3943 d.param = NULL;
3945 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
3947 if (wtd->param_prefix)
3949 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
3950 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
3951 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
3952 wtd->param_prefix, v->name);
3953 oprintf (d.of,
3954 " ATTRIBUTE_UNUSED void *x_p,\n"
3955 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3956 " ATTRIBUTE_UNUSED void * cookie)\n");
3957 oprintf (d.of, "{\n");
3958 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3959 d.process_field = write_types_local_process_field;
3960 walk_type (v->type, &d);
3961 oprintf (f, "}\n\n");
3964 d.opt = v->opt;
3965 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
3966 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
3967 wtd->prefix, v->name);
3968 oprintf (f, "{\n");
3969 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
3970 d.process_field = write_types_process_field;
3971 walk_type (v->type, &d);
3972 free (prevval3);
3973 oprintf (f, "}\n\n");
3976 /* Output a table describing the locations and types of VARIABLES. */
3978 static void
3979 write_roots (pair_p variables, bool emit_pch)
3981 pair_p v;
3982 struct flist *flp = NULL;
3984 for (v = variables; v; v = v->next)
3986 outf_p f =
3987 get_output_file_with_visibility (CONST_CAST (input_file*,
3988 v->line.file));
3989 struct flist *fli;
3990 const char *length = NULL;
3991 int deletable_p = 0;
3992 options_p o;
3993 for (o = v->opt; o; o = o->next)
3994 if (strcmp (o->name, "length") == 0
3995 && o->kind == OPTION_STRING)
3996 length = o->info.string;
3997 else if (strcmp (o->name, "deletable") == 0)
3998 deletable_p = 1;
3999 else if (strcmp (o->name, "param_is") == 0)
4001 else if (strncmp (o->name, "param", 5) == 0
4002 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4004 else if (strcmp (o->name, "if_marked") == 0)
4006 else
4007 error_at_line (&v->line,
4008 "global `%s' has unknown option `%s'",
4009 v->name, o->name);
4011 for (fli = flp; fli; fli = fli->next)
4012 if (fli->f == f && f)
4013 break;
4014 if (fli == NULL)
4016 fli = XNEW (struct flist);
4017 fli->f = f;
4018 fli->next = flp;
4019 fli->started_p = 0;
4020 fli->file = v->line.file;
4021 gcc_assert (fli->file);
4022 flp = fli;
4024 oprintf (f, "\n/* GC roots. */\n\n");
4027 if (!deletable_p
4028 && length
4029 && v->type->kind == TYPE_POINTER
4030 && (v->type->u.p->kind == TYPE_POINTER
4031 || v->type->u.p->kind == TYPE_STRUCT))
4033 write_array (f, v, &ggc_wtd);
4034 write_array (f, v, &pch_wtd);
4038 for (v = variables; v; v = v->next)
4040 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4041 v->line.file));
4042 struct flist *fli;
4043 int skip_p = 0;
4044 int length_p = 0;
4045 options_p o;
4047 for (o = v->opt; o; o = o->next)
4048 if (strcmp (o->name, "length") == 0)
4049 length_p = 1;
4050 else if (strcmp (o->name, "deletable") == 0
4051 || strcmp (o->name, "if_marked") == 0)
4052 skip_p = 1;
4054 if (skip_p)
4055 continue;
4057 for (fli = flp; fli; fli = fli->next)
4058 if (fli->f == f)
4059 break;
4060 if (!fli->started_p)
4062 fli->started_p = 1;
4064 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4065 put_mangled_filename (f, v->line.file);
4066 oprintf (f, "[] = {\n");
4069 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4072 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4073 "gt_ggc_rtab");
4075 for (v = variables; v; v = v->next)
4077 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4078 v->line.file));
4079 struct flist *fli;
4080 int skip_p = 1;
4081 options_p o;
4083 for (o = v->opt; o; o = o->next)
4084 if (strcmp (o->name, "deletable") == 0)
4085 skip_p = 0;
4086 else if (strcmp (o->name, "if_marked") == 0)
4087 skip_p = 1;
4089 if (skip_p)
4090 continue;
4092 for (fli = flp; fli; fli = fli->next)
4093 if (fli->f == f)
4094 break;
4095 if (!fli->started_p)
4097 fli->started_p = 1;
4099 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4100 put_mangled_filename (f, v->line.file);
4101 oprintf (f, "[] = {\n");
4104 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4105 v->name, v->name);
4108 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4109 "gt_ggc_deletable_rtab");
4111 for (v = variables; v; v = v->next)
4113 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4114 v->line.file));
4115 struct flist *fli;
4116 const char *if_marked = NULL;
4117 int length_p = 0;
4118 options_p o;
4120 for (o = v->opt; o; o = o->next)
4121 if (strcmp (o->name, "length") == 0)
4122 length_p = 1;
4123 else if (strcmp (o->name, "if_marked") == 0
4124 && o->kind == OPTION_STRING)
4125 if_marked = o->info.string;
4126 if (if_marked == NULL)
4127 continue;
4128 if (v->type->kind != TYPE_POINTER
4129 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4130 || v->type->u.p->u.param_struct.stru != find_structure ("htab", 0))
4132 error_at_line (&v->line,
4133 "if_marked option used but not hash table");
4134 continue;
4137 for (fli = flp; fli; fli = fli->next)
4138 if (fli->f == f)
4139 break;
4140 if (!fli->started_p)
4142 fli->started_p = 1;
4144 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4145 put_mangled_filename (f, v->line.file);
4146 oprintf (f, "[] = {\n");
4149 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4150 v->name, length_p, &v->line, if_marked, emit_pch);
4153 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4154 "gt_ggc_cache_rtab");
4156 if (!emit_pch)
4157 return;
4159 for (v = variables; v; v = v->next)
4161 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4162 v->line.file));
4163 struct flist *fli;
4164 int length_p = 0;
4165 int if_marked_p = 0;
4166 options_p o;
4168 for (o = v->opt; o; o = o->next)
4169 if (strcmp (o->name, "length") == 0)
4170 length_p = 1;
4171 else if (strcmp (o->name, "if_marked") == 0)
4172 if_marked_p = 1;
4174 if (!if_marked_p)
4175 continue;
4177 for (fli = flp; fli; fli = fli->next)
4178 if (fli->f == f)
4179 break;
4180 if (!fli->started_p)
4182 fli->started_p = 1;
4184 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4185 put_mangled_filename (f, v->line.file);
4186 oprintf (f, "[] = {\n");
4189 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4192 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4193 "gt_pch_cache_rtab");
4195 for (v = variables; v; v = v->next)
4197 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4198 v->line.file));
4199 struct flist *fli;
4200 int skip_p = 0;
4201 options_p o;
4203 for (o = v->opt; o; o = o->next)
4204 if (strcmp (o->name, "deletable") == 0
4205 || strcmp (o->name, "if_marked") == 0)
4206 skip_p = 1;
4208 if (skip_p)
4209 continue;
4211 if (!contains_scalar_p (v->type))
4212 continue;
4214 for (fli = flp; fli; fli = fli->next)
4215 if (fli->f == f)
4216 break;
4217 if (!fli->started_p)
4219 fli->started_p = 1;
4221 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4222 put_mangled_filename (f, v->line.file);
4223 oprintf (f, "[] = {\n");
4226 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4227 v->name, v->name);
4230 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4231 "gt_pch_scalar_rtab");
4233 /* Record the definition of the vec_prefix structure, as defined in vec.h:
4235 struct vec_prefix GTY(()) {
4236 unsigned num;
4237 unsigned alloc;
4238 }; */
4239 static type_p
4240 vec_prefix_type (void)
4242 static type_p prefix_type = NULL;
4243 if (prefix_type == NULL)
4245 pair_p fields;
4246 static struct fileloc pos = { NULL, 0 };
4247 type_p len_ty = create_scalar_type ("unsigned");
4248 pos.file = input_file_by_name (__FILE__); pos.line = __LINE__;
4249 fields = create_field_at (0, len_ty, "alloc", 0, &pos);
4250 fields = create_field_at (fields, len_ty, "num", 0, &pos);
4251 prefix_type = new_structure ("vec_prefix", 0, &pos, fields, 0);
4252 prefix_type->u.s.bitmap = -1;
4254 return prefix_type;
4257 /* Record the definition of a generic VEC structure, as if we had expanded
4258 the macros in vec.h:
4260 typedef struct VEC_<type>_base GTY(()) {
4261 struct vec_prefix prefix;
4262 <type> GTY((length ("%h.prefix.num"))) vec[1];
4263 } VEC_<type>_base
4265 where the GTY(()) tags are only present if is_scalar is _false_. */
4267 void
4268 note_def_vec (const char *type_name, bool is_scalar, struct fileloc *pos)
4270 pair_p fields;
4271 type_p t;
4272 options_p o;
4273 const char *name = concat ("VEC_", type_name, "_base", (char *) 0);
4275 if (is_scalar)
4277 t = create_scalar_type (type_name);
4278 o = 0;
4280 else
4282 t = resolve_typedef (type_name, pos);
4283 o = create_string_option (0, "length", "%h.prefix.num");
4285 /* We assemble the field list in reverse order. */
4286 fields = create_field_at (0, create_array (t, "1"), "vec", o, pos);
4287 fields = create_field_at (fields, vec_prefix_type (), "prefix", 0, pos);
4289 do_typedef (name, new_structure (name, 0, pos, fields, 0), pos);
4292 /* Record the definition of an allocation-specific VEC structure, as if
4293 we had expanded the macros in vec.h:
4295 typedef struct VEC_<type>_<astrat> {
4296 VEC_<type>_base base;
4297 } VEC_<type>_<astrat>;
4299 void
4300 note_def_vec_alloc (const char *type, const char *astrat, struct fileloc *pos)
4302 const char *astratname = concat ("VEC_", type, "_", astrat, (char *) 0);
4303 const char *basename = concat ("VEC_", type, "_base", (char *) 0);
4305 pair_p field = create_field_at (0, resolve_typedef (basename, pos),
4306 "base", 0, pos);
4308 do_typedef (astratname, new_structure (astratname, 0, pos, field, 0), pos);
4311 /* Returns the specifier keyword for a string or union type S, empty string
4312 otherwise. */
4314 static const char *
4315 get_type_specifier (const type_p s)
4317 if (s->kind == TYPE_STRUCT || s->kind == TYPE_LANG_STRUCT)
4318 return "struct ";
4319 if (s->kind == TYPE_UNION)
4320 return "union ";
4321 return "";
4324 /* TRUE if type S has the GTY variable_size annotation. */
4326 static bool
4327 variable_size_p (const type_p s)
4329 options_p o;
4330 for (o = s->u.s.opt; o; o = o->next)
4331 if (strcmp (o->name, "variable_size") == 0)
4332 return true;
4333 return false;
4336 enum alloc_quantity
4337 { single, vector };
4338 enum alloc_zone
4339 { any_zone, specific_zone };
4341 /* Writes one typed allocator definition into output F for type
4342 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4343 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4344 is true, the allocator will have an extra parameter specifying
4345 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4346 vector allocator will be output, if ZONE is set to SPECIFIC_ZONE,
4347 the allocator will be zone-specific. */
4349 static void
4350 write_typed_alloc_def (outf_p f,
4351 bool variable_size, const char *type_specifier,
4352 const char *type_name, const char *allocator_type,
4353 enum alloc_quantity quantity, enum alloc_zone zone)
4355 bool two_args = variable_size && (quantity == vector);
4356 bool third_arg = ((zone == specific_zone)
4357 && (variable_size || (quantity == vector)));
4358 gcc_assert (f != NULL);
4359 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name);
4360 oprintf (f, "(%s%s%s%s%s) ",
4361 (variable_size ? "SIZE" : ""),
4362 (two_args ? ", " : ""),
4363 (quantity == vector) ? "n" : "",
4364 (third_arg ? ", " : ""), (zone == specific_zone) ? "z" : "");
4365 oprintf (f, "((%s%s *)", type_specifier, type_name);
4366 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4367 if (zone == specific_zone)
4368 oprintf (f, "z, ");
4369 if (variable_size)
4370 oprintf (f, "SIZE");
4371 else
4372 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4373 if (quantity == vector)
4374 oprintf (f, ", n");
4375 oprintf (f, " MEM_STAT_INFO)))\n");
4378 /* Writes a typed allocator definition into output F for a struct or
4379 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4381 static void
4382 write_typed_struct_alloc_def (outf_p f,
4383 const type_p s, const char *allocator_type,
4384 enum alloc_quantity quantity,
4385 enum alloc_zone zone)
4387 gcc_assert (UNION_OR_STRUCT_P (s));
4388 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4389 s->u.s.tag, allocator_type, quantity, zone);
4392 /* Writes a typed allocator definition into output F for a typedef P,
4393 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4395 static void
4396 write_typed_typedef_alloc_def (outf_p f,
4397 const pair_p p, const char *allocator_type,
4398 enum alloc_quantity quantity,
4399 enum alloc_zone zone)
4401 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4402 allocator_type, quantity, zone);
4405 /* Writes typed allocator definitions into output F for the types in
4406 STRUCTURES and TYPEDEFS that are used by GC. */
4408 static void
4409 write_typed_alloc_defns (outf_p f,
4410 const type_p structures, const pair_p typedefs)
4412 type_p s;
4413 pair_p p;
4415 gcc_assert (f != NULL);
4416 oprintf (f,
4417 "\n/* Allocators for known structs and unions. */\n\n");
4418 for (s = structures; s; s = s->next)
4420 if (!USED_BY_TYPED_GC_P (s))
4421 continue;
4422 gcc_assert (UNION_OR_STRUCT_P (s));
4423 /* In plugin mode onput output ggc_alloc macro definitions
4424 relevant to plugin input files. */
4425 if (nb_plugin_files > 0
4426 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
4427 continue;
4428 write_typed_struct_alloc_def (f, s, "", single, any_zone);
4429 write_typed_struct_alloc_def (f, s, "cleared_", single, any_zone);
4430 write_typed_struct_alloc_def (f, s, "vec_", vector, any_zone);
4431 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector, any_zone);
4432 write_typed_struct_alloc_def (f, s, "zone_", single, specific_zone);
4433 write_typed_struct_alloc_def (f, s, "zone_cleared_", single,
4434 specific_zone);
4435 write_typed_struct_alloc_def (f, s, "zone_vec_", vector, specific_zone);
4436 write_typed_struct_alloc_def (f, s, "zone_cleared_vec_", vector,
4437 specific_zone);
4440 oprintf (f, "\n/* Allocators for known typedefs. */\n");
4441 for (p = typedefs; p; p = p->next)
4443 s = p->type;
4444 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4445 continue;
4446 /* In plugin mode onput output ggc_alloc macro definitions
4447 relevant to plugin input files. */
4448 if (nb_plugin_files > 0)
4450 struct fileloc* filoc = type_fileloc(s);
4451 if (!filoc || !filoc->file->inpisplugin)
4452 continue;
4454 write_typed_typedef_alloc_def (f, p, "", single, any_zone);
4455 write_typed_typedef_alloc_def (f, p, "cleared_", single, any_zone);
4456 write_typed_typedef_alloc_def (f, p, "vec_", vector, any_zone);
4457 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector, any_zone);
4458 write_typed_typedef_alloc_def (f, p, "zone_", single, specific_zone);
4459 write_typed_typedef_alloc_def (f, p, "zone_cleared_", single,
4460 specific_zone);
4461 write_typed_typedef_alloc_def (f, p, "zone_cleared_vec_", vector,
4462 specific_zone);
4466 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4467 guaranteee for somewhat increased readability. If name conflicts do happen,
4468 this funcion will have to be adjusted to be more like
4469 output_mangled_typename. */
4471 static void
4472 output_typename (outf_p of, const_type_p t)
4474 switch (t->kind)
4476 case TYPE_STRING:
4477 oprintf (of, "str");
4478 break;
4479 case TYPE_SCALAR:
4480 oprintf (of, "scalar");
4481 break;
4482 case TYPE_POINTER:
4483 output_typename (of, t->u.p);
4484 break;
4485 case TYPE_STRUCT:
4486 case TYPE_UNION:
4487 case TYPE_LANG_STRUCT:
4488 oprintf (of, "%s", t->u.s.tag);
4489 break;
4490 case TYPE_PARAM_STRUCT:
4492 int i;
4493 for (i = 0; i < NUM_PARAM; i++)
4494 if (t->u.param_struct.param[i] != NULL)
4496 output_typename (of, t->u.param_struct.param[i]);
4497 oprintf (of, "_");
4499 output_typename (of, t->u.param_struct.stru);
4500 break;
4502 default:
4503 gcc_unreachable ();
4507 /* Writes a typed GC allocator for type S that is suitable as a callback for
4508 the splay tree implementation in libiberty. */
4510 static void
4511 write_splay_tree_allocator_def (const_type_p s)
4513 outf_p of = get_output_file_with_visibility (NULL);
4514 oprintf (of, "void * ggc_alloc_splay_tree_");
4515 output_typename (of, s);
4516 oprintf (of, " (int sz, void * nl)\n");
4517 oprintf (of, "{\n");
4518 oprintf (of, " return ggc_splay_alloc (");
4519 oprintf (of, "gt_e_");
4520 output_mangled_typename (of, s);
4521 oprintf (of, ", sz, nl);\n");
4522 oprintf (of, "}\n\n");
4525 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4526 for the splay tree implementation in libiberty. */
4528 static void
4529 write_splay_tree_allocators (const_type_p param_structs)
4531 const_type_p s;
4533 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4534 for (s = param_structs; s; s = s->next)
4535 if (s->gc_used == GC_POINTED_TO)
4537 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4538 output_typename (header_file, s);
4539 oprintf (header_file, " (int, void *);\n");
4540 write_splay_tree_allocator_def (s);
4544 static void dump_pair (int indent, pair_p p);
4545 static void dump_type (int indent, type_p p);
4546 static void dump_type_list (int indent, type_p p);
4548 #define INDENT 2
4550 /* Dumps the value of typekind KIND. */
4552 static void
4553 dump_typekind (int indent, enum typekind kind)
4555 printf ("%*ckind = ", indent, ' ');
4556 switch (kind)
4558 case TYPE_SCALAR:
4559 printf ("TYPE_SCALAR");
4560 break;
4561 case TYPE_STRING:
4562 printf ("TYPE_STRING");
4563 break;
4564 case TYPE_STRUCT:
4565 printf ("TYPE_STRUCT");
4566 break;
4567 case TYPE_UNION:
4568 printf ("TYPE_UNION");
4569 break;
4570 case TYPE_POINTER:
4571 printf ("TYPE_POINTER");
4572 break;
4573 case TYPE_ARRAY:
4574 printf ("TYPE_ARRAY");
4575 break;
4576 case TYPE_LANG_STRUCT:
4577 printf ("TYPE_LANG_STRUCT");
4578 break;
4579 case TYPE_PARAM_STRUCT:
4580 printf ("TYPE_PARAM_STRUCT");
4581 break;
4582 default:
4583 gcc_unreachable ();
4585 printf ("\n");
4588 /* Dumps the value of GC_USED flag. */
4590 static void
4591 dump_gc_used (int indent, enum gc_used_enum gc_used)
4593 printf ("%*cgc_used = ", indent, ' ');
4594 switch (gc_used)
4596 case GC_UNUSED:
4597 printf ("GC_UNUSED");
4598 break;
4599 case GC_USED:
4600 printf ("GC_USED");
4601 break;
4602 case GC_MAYBE_POINTED_TO:
4603 printf ("GC_MAYBE_POINTED_TO");
4604 break;
4605 case GC_POINTED_TO:
4606 printf ("GC_POINTED_TO");
4607 break;
4608 default:
4609 gcc_unreachable ();
4611 printf ("\n");
4614 /* Dumps the type options OPT. */
4616 static void
4617 dump_options (int indent, options_p opt)
4619 options_p o;
4620 printf ("%*coptions = ", indent, ' ');
4621 o = opt;
4622 while (o)
4624 switch (o->kind)
4626 case OPTION_STRING:
4627 printf ("%s:string %s ", o->name, o->info.string);
4628 break;
4629 case OPTION_TYPE:
4630 printf ("%s:type ", o->name);
4631 dump_type (indent+1, o->info.type);
4632 break;
4633 case OPTION_NESTED:
4634 printf ("%s:nested ", o->name);
4635 break;
4636 case OPTION_NONE:
4637 gcc_unreachable ();
4639 o = o->next;
4641 printf ("\n");
4644 /* Dumps the source file location in LINE. */
4646 static void
4647 dump_fileloc (int indent, struct fileloc line)
4649 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
4650 get_input_file_name (line.file),
4651 line.line);
4654 /* Recursively dumps the struct, union, or a language-specific
4655 struct T. */
4657 static void
4658 dump_type_u_s (int indent, type_p t)
4660 pair_p fields;
4662 gcc_assert (t->kind == TYPE_STRUCT || t->kind == TYPE_UNION
4663 || t->kind == TYPE_LANG_STRUCT);
4664 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
4665 dump_fileloc (indent, t->u.s.line);
4666 printf ("%*cu.s.fields =\n", indent, ' ');
4667 fields = t->u.s.fields;
4668 while (fields)
4670 dump_pair (indent + INDENT, fields);
4671 fields = fields->next;
4673 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
4674 dump_options (indent, t->u.s.opt);
4675 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
4676 if (t->kind == TYPE_LANG_STRUCT)
4678 printf ("%*cu.s.lang_struct:\n", indent, ' ');
4679 dump_type_list (indent + INDENT, t->u.s.lang_struct);
4683 /* Recursively dumps the array T. */
4685 static void
4686 dump_type_u_a (int indent, type_p t)
4688 gcc_assert (t->kind == TYPE_ARRAY);
4689 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
4690 dump_type_list (indent + INDENT, t->u.a.p);
4693 /* Recursively dumps the parameterized struct T. */
4695 static void
4696 dump_type_u_param_struct (int indent, type_p t)
4698 int i;
4699 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
4700 printf ("%*cu.param_struct.stru:\n", indent, ' ');
4701 dump_type_list (indent, t->u.param_struct.stru);
4702 dump_fileloc (indent, t->u.param_struct.line);
4703 for (i = 0; i < NUM_PARAM; i++)
4705 if (t->u.param_struct.param[i] == NULL)
4706 continue;
4707 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
4708 dump_type (indent + INDENT, t->u.param_struct.param[i]);
4712 /* Recursively dumps the type list T. */
4714 static void
4715 dump_type_list (int indent, type_p t)
4717 type_p p = t;
4718 while (p)
4720 dump_type (indent, p);
4721 p = p->next;
4725 static htab_t seen_types;
4727 /* Recursively dumps the type T if it was not dumped previously. */
4729 static void
4730 dump_type (int indent, type_p t)
4732 PTR *slot;
4734 printf ("%*cType at %p: ", indent, ' ', (void *) t);
4735 slot = htab_find_slot (seen_types, t, INSERT);
4736 if (*slot != NULL)
4738 printf ("already seen.\n");
4739 return;
4741 *slot = t;
4742 printf ("\n");
4744 dump_typekind (indent, t->kind);
4745 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
4746 (void *) t->pointer_to);
4747 dump_gc_used (indent + INDENT, t->gc_used);
4748 switch (t->kind)
4750 case TYPE_SCALAR:
4751 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
4752 t->u.scalar_is_char ? "true" : "false");
4753 break;
4754 case TYPE_STRING:
4755 break;
4756 case TYPE_STRUCT:
4757 case TYPE_UNION:
4758 case TYPE_LANG_STRUCT:
4759 dump_type_u_s (indent + INDENT, t);
4760 break;
4761 case TYPE_POINTER:
4762 printf ("%*cp:\n", indent + INDENT, ' ');
4763 dump_type (indent + INDENT, t->u.p);
4764 break;
4765 case TYPE_ARRAY:
4766 dump_type_u_a (indent + INDENT, t);
4767 break;
4768 case TYPE_PARAM_STRUCT:
4769 dump_type_u_param_struct (indent + INDENT, t);
4770 break;
4771 default:
4772 gcc_unreachable ();
4774 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
4777 /* Dumps the pair P. */
4779 static void
4780 dump_pair (int indent, pair_p p)
4782 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
4783 dump_type (indent, p->type);
4784 dump_fileloc (indent, p->line);
4785 dump_options (indent, p->opt);
4786 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
4789 /* Dumps the list of pairs PP. */
4791 static void
4792 dump_pair_list (const char *name, pair_p pp)
4794 pair_p p;
4795 printf ("%s:\n", name);
4796 for (p = pp; p != NULL; p = p->next)
4797 dump_pair (0, p);
4798 printf ("End of %s\n\n", name);
4801 /* Dumps the STRUCTURES. */
4803 static void
4804 dump_structures (const char *name, type_p structures)
4806 printf ("%s:\n", name);
4807 dump_type_list (0, structures);
4808 printf ("End of %s\n\n", name);
4811 /* Dumps the internal structures of gengtype. This is useful to debug
4812 gengtype itself, or to understand what it does, e.g. for plugin
4813 developers. */
4815 static void
4816 dump_everything (void)
4818 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
4819 dump_pair_list ("typedefs", typedefs);
4820 dump_structures ("structures", structures);
4821 dump_structures ("param_structs", param_structs);
4822 dump_pair_list ("variables", variables);
4823 htab_delete (seen_types);
4828 /* Option specification for getopt_long. */
4829 static const struct option gengtype_long_options[] = {
4830 {"help", no_argument, NULL, 'h'},
4831 {"version", no_argument, NULL, 'V'},
4832 {"verbose", no_argument, NULL, 'v'},
4833 {"dump", no_argument, NULL, 'd'},
4834 {"debug", no_argument, NULL, 'D'},
4835 {"plugin", required_argument, NULL, 'P'},
4836 {"srcdir", required_argument, NULL, 'S'},
4837 {"backupdir", required_argument, NULL, 'B'},
4838 {"inputs", required_argument, NULL, 'I'},
4839 {"read-state", required_argument, NULL, 'r'},
4840 {"write-state", required_argument, NULL, 'w'},
4841 /* Terminating NULL placeholder. */
4842 {NULL, no_argument, NULL, 0},
4846 static void
4847 print_usage (void)
4849 printf ("Usage: %s\n", progname);
4850 printf ("\t -h | --help " " \t# Give this help.\n");
4851 printf ("\t -D | --debug "
4852 " \t# Give debug output to debug %s itself.\n", progname);
4853 printf ("\t -V | --version " " \t# Give version information.\n");
4854 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
4855 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
4856 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
4857 " \t# Generate for plugin.\n");
4858 printf ("\t -S | --srcdir <GCC-directory> "
4859 " \t# Specify the GCC source directory.\n");
4860 printf ("\t -B | --backupdir <directory> "
4861 " \t# Specify the backup directory for updated files.\n");
4862 printf ("\t -I | --inputs <input-list> "
4863 " \t# Specify the file with source files list.\n");
4864 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
4865 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
4868 static void
4869 print_version (void)
4871 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
4872 printf ("Report bugs: %s\n", bug_report_url);
4875 /* Parse the program options using getopt_long... */
4876 static void
4877 parse_program_options (int argc, char **argv)
4879 int opt = -1;
4880 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
4881 gengtype_long_options, NULL)) >= 0)
4883 switch (opt)
4885 case 'h': /* --help */
4886 print_usage ();
4887 break;
4888 case 'V': /* --version */
4889 print_version ();
4890 break;
4891 case 'd': /* --dump */
4892 do_dump = 1;
4893 break;
4894 case 'D': /* --debug */
4895 do_debug = 1;
4896 break;
4897 case 'v': /* --verbose */
4898 verbosity_level++;
4899 break;
4900 case 'P': /* --plugin */
4901 if (optarg)
4902 plugin_output_filename = optarg;
4903 else
4904 fatal ("missing plugin output file name");
4905 break;
4906 case 'S': /* --srcdir */
4907 if (optarg)
4908 srcdir = optarg;
4909 else
4910 fatal ("missing source directory");
4911 srcdir_len = strlen (srcdir);
4912 break;
4913 case 'B': /* --backupdir */
4914 if (optarg)
4915 backup_dir = optarg;
4916 else
4917 fatal ("missing backup directory");
4918 break;
4919 case 'I': /* --inputs */
4920 if (optarg)
4921 inputlist = optarg;
4922 else
4923 fatal ("missing input list");
4924 break;
4925 case 'r': /* --read-state */
4926 if (optarg)
4927 read_state_filename = optarg;
4928 else
4929 fatal ("missing read state file");
4930 DBGPRINTF ("read state %s\n", optarg);
4931 break;
4932 case 'w': /* --write-state */
4933 DBGPRINTF ("write state %s\n", optarg);
4934 if (optarg)
4935 write_state_filename = optarg;
4936 else
4937 fatal ("missing write state file");
4938 break;
4939 default:
4940 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
4941 print_usage ();
4942 fatal ("unexpected flag");
4945 if (plugin_output_filename)
4947 /* In plugin mode we require some input files. */
4948 int i = 0;
4949 if (optind >= argc)
4950 fatal ("no source files given in plugin mode");
4951 nb_plugin_files = argc - optind;
4952 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
4953 for (i = 0; i < (int) nb_plugin_files; i++)
4955 char *name = argv[i + optind];
4956 plugin_files[i] = input_file_by_name (name);
4963 /******* Manage input files. ******/
4965 /* Hash table of unique input file names. */
4966 static htab_t input_file_htab;
4968 /* Find or allocate a new input_file by hash-consing it. */
4969 input_file*
4970 input_file_by_name (const char* name)
4972 PTR* slot;
4973 input_file* f = NULL;
4974 int namlen = 0;
4975 if (!name)
4976 return NULL;
4977 namlen = strlen (name);
4978 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
4979 f->inpbitmap = 0;
4980 f->inpoutf = NULL;
4981 f->inpisplugin = false;
4982 strcpy (f->inpname, name);
4983 slot = htab_find_slot (input_file_htab, f, INSERT);
4984 gcc_assert (slot != NULL);
4985 if (*slot)
4987 /* Already known input file. */
4988 free (f);
4989 return (input_file*)(*slot);
4991 /* New input file. */
4992 *slot = f;
4993 return f;
4996 /* Hash table support routines for input_file-s. */
4997 static hashval_t
4998 htab_hash_inputfile (const void *p)
5000 const input_file *inpf = (const input_file *) p;
5001 gcc_assert (inpf);
5002 return htab_hash_string (get_input_file_name (inpf));
5005 static int
5006 htab_eq_inputfile (const void *x, const void *y)
5008 const input_file *inpfx = (const input_file *) x;
5009 const input_file *inpfy = (const input_file *) y;
5010 gcc_assert (inpfx != NULL && inpfy != NULL);
5011 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5016 main (int argc, char **argv)
5018 size_t i;
5019 static struct fileloc pos = { NULL, 0 };
5020 outf_p output_header;
5022 /* Mandatory common initializations. */
5023 progname = "gengtype"; /* For fatal and messages. */
5024 /* Create the hash-table used to hash-cons input files. */
5025 input_file_htab =
5026 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5027 /* Initialize our special input files. */
5028 this_file = input_file_by_name (__FILE__);
5029 system_h_file = input_file_by_name ("system.h");
5030 /* Set the scalar_is_char union number for predefined scalar types. */
5031 scalar_nonchar.u.scalar_is_char = FALSE;
5032 scalar_char.u.scalar_is_char = TRUE;
5034 parse_program_options (argc, argv);
5036 #if ENABLE_CHECKING
5037 if (do_debug)
5039 time_t now = (time_t) 0;
5040 time (&now);
5041 DBGPRINTF ("gengtype started pid %d at %s",
5042 (int) getpid (), ctime (&now));
5044 #endif /* ENABLE_CHECKING */
5046 /* Parse the input list and the input files. */
5047 DBGPRINTF ("inputlist %s", inputlist);
5048 if (read_state_filename)
5050 if (inputlist)
5051 fatal ("input list %s cannot be given with a read state file %s",
5052 inputlist, read_state_filename);
5053 read_state (read_state_filename);
5054 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5055 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5057 else if (inputlist)
5059 /* These types are set up with #define or else outside of where
5060 we can see them. We should initialize them before calling
5061 read_input_list. */
5062 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5063 Call;} while(0)
5064 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5065 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5066 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5067 POS_HERE (do_scalar_typedef ("double_int", &pos));
5068 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5069 POS_HERE (do_scalar_typedef ("uint8", &pos));
5070 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5071 POS_HERE (do_scalar_typedef ("jword", &pos));
5072 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5073 POS_HERE (do_scalar_typedef ("void", &pos));
5074 POS_HERE (do_typedef ("PTR",
5075 create_pointer (resolve_typedef ("void", &pos)),
5076 &pos));
5077 #undef POS_HERE
5078 read_input_list (inputlist);
5079 for (i = 0; i < num_gt_files; i++)
5081 parse_file (get_input_file_name (gt_files[i]));
5082 DBGPRINTF ("parsed file #%d %s",
5083 (int) i, get_input_file_name (gt_files[i]));
5085 if (verbosity_level >= 1)
5086 printf ("%s parsed %d files with %d GTY types\n",
5087 progname, (int) num_gt_files, type_count);
5089 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5090 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5093 else
5094 fatal ("either an input list or a read state file should be given");
5095 if (hit_error)
5096 return 1;
5099 if (plugin_output_filename)
5101 size_t ix = 0;
5102 /* In plugin mode, we should have read a state file, and have
5103 given at least one plugin file. */
5104 if (!read_state_filename)
5105 fatal ("No read state given in plugin mode for %s",
5106 plugin_output_filename);
5108 if (nb_plugin_files == 0 || !plugin_files)
5109 fatal ("No plugin files given in plugin mode for %s",
5110 plugin_output_filename);
5112 /* Parse our plugin files and augment the state. */
5113 for (ix = 0; ix < nb_plugin_files; ix++)
5115 input_file* pluginput = plugin_files [ix];
5116 pluginput->inpisplugin = true;
5117 parse_file (get_input_file_name (pluginput));
5119 if (hit_error)
5120 return 1;
5122 plugin_output = create_file ("GCC", plugin_output_filename);
5123 DBGPRINTF ("created plugin_output %p named %s",
5124 (void *) plugin_output, plugin_output->name);
5126 else
5127 { /* No plugin files, we are in normal mode. */
5128 if (!srcdir)
5129 fatal ("gengtype needs a source directory in normal mode");
5131 if (hit_error)
5132 return 1;
5134 gen_rtx_next ();
5136 /* The call to set_gc_used may indirectly call find_param_structure
5137 hence enlarge the param_structs list of types. */
5138 set_gc_used (variables);
5140 /* The state at this point is read from the state input file or by
5141 parsing source files and optionally augmented by parsing plugin
5142 source files. Write it now. */
5143 if (write_state_filename)
5145 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5146 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5148 if (hit_error)
5149 fatal ("didn't write state file %s after errors",
5150 write_state_filename);
5152 DBGPRINTF ("before write_state %s", write_state_filename);
5153 write_state (write_state_filename);
5155 if (do_dump)
5156 dump_everything ();
5158 /* After having written the state file we return immediately to
5159 avoid generating any output file. */
5160 if (hit_error)
5161 return 1;
5162 else
5163 return 0;
5167 open_base_files ();
5169 write_enum_defn (structures, param_structs);
5170 output_header = plugin_output ? plugin_output : header_file;
5171 write_typed_alloc_defns (output_header, structures, typedefs);
5172 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5173 structures);
5174 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5175 param_structs);
5177 write_types (output_header, structures, param_structs, &ggc_wtd);
5178 if (plugin_files == NULL)
5180 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5181 structures);
5182 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5183 param_structs);
5184 write_types (header_file, structures, param_structs, &pch_wtd);
5185 write_local (header_file, structures, param_structs);
5187 write_splay_tree_allocators (param_structs);
5188 write_roots (variables, plugin_files == NULL);
5189 write_rtx_next ();
5190 close_output_files ();
5192 if (do_dump)
5193 dump_everything ();
5195 /* Don't bother about free-ing any input or plugin file, etc. */
5197 if (hit_error)
5198 return 1;
5199 return 0;