2013-05-28 Steve Ellcey <sellcey@mips.com>
[official-gcc.git] / gcc / gengtype.c
blobeede79861f3427fd6da02874d4fbb9647c210c66
1 /* Process source files and output type information.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifdef GENERATOR_FILE
21 #include "bconfig.h"
22 #else
23 #include "config.h"
24 #endif
25 #include "system.h"
26 #include "errors.h" /* for fatal */
27 #include "getopt.h"
28 #include "double-int.h"
29 #include "version.h" /* for version_string & pkgversion_string. */
30 #include "hashtab.h"
31 #include "xregex.h"
32 #include "obstack.h"
33 #include "gengtype.h"
34 #include "filenames.h"
36 /* Data types, macros, etc. used only in this file. */
39 /* The list of output files. */
40 outf_p output_files;
42 /* The output header file that is included into pretty much every
43 source file. */
44 outf_p header_file;
47 /* The name of the file containing the list of input files. */
48 static char *inputlist;
50 /* The plugin input files and their number; in that case only
51 a single file is produced. */
52 static input_file **plugin_files;
53 static size_t nb_plugin_files;
55 /* The generated plugin output file and name. */
56 static outf_p plugin_output;
57 static char *plugin_output_filename;
59 /* Our source directory and its length. */
60 const char *srcdir;
61 size_t srcdir_len;
63 /* Variables used for reading and writing the state. */
64 const char *read_state_filename;
65 const char *write_state_filename;
67 /* Variables to help debugging. */
68 int do_dump;
69 int do_debug;
71 /* Level for verbose messages. */
72 int verbosity_level;
74 /* We have a type count and use it to set the state_number of newly
75 allocated types to some unique negative number. */
76 static int type_count;
78 /* The backup directory should be in the same file system as the
79 generated files, otherwise the rename(2) system call would fail.
80 If NULL, no backup is made when overwriting a generated file. */
81 static const char* backup_dir; /* (-B) program option. */
84 static outf_p create_file (const char *, const char *);
86 static const char *get_file_basename (const input_file *);
87 static const char *get_file_realbasename (const input_file *);
89 static int get_prefix_langdir_index (const char *);
90 static const char *get_file_langdir (const input_file *);
92 static void dump_pair (int indent, pair_p p);
93 static void dump_type (int indent, type_p p);
94 static void dump_type_list (int indent, type_p p);
97 /* Nonzero iff an error has occurred. */
98 bool hit_error = false;
100 static void gen_rtx_next (void);
101 static void write_rtx_next (void);
102 static void open_base_files (void);
103 static void close_output_files (void);
105 /* Report an error at POS, printing MSG. */
107 void
108 error_at_line (const struct fileloc *pos, const char *msg, ...)
110 va_list ap;
112 gcc_assert (pos != NULL && pos->file != NULL);
113 va_start (ap, msg);
115 fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
116 vfprintf (stderr, msg, ap);
117 fputc ('\n', stderr);
118 hit_error = true;
120 va_end (ap);
123 /* asprintf, but produces fatal message on out-of-memory. */
124 char *
125 xasprintf (const char *format, ...)
127 int n;
128 char *result;
129 va_list ap;
131 va_start (ap, format);
132 n = vasprintf (&result, format, ap);
133 if (result == NULL || n < 0)
134 fatal ("out of memory");
135 va_end (ap);
137 return result;
140 /* Input file handling. */
142 /* Table of all input files. */
143 const input_file **gt_files;
144 size_t num_gt_files;
146 /* A number of places use the name of this "gengtype.c" file for a
147 location for things that we can't rely on the source to define.
148 Make sure we can still use pointer comparison on filenames. */
149 input_file* this_file;
150 /* The "system.h" file is likewise specially useful. */
151 input_file* system_h_file;
153 /* Vector of per-language directories. */
154 const char **lang_dir_names;
155 size_t num_lang_dirs;
157 /* An array of output files suitable for definitions. There is one
158 BASE_FILES entry for each language. */
159 static outf_p *base_files;
163 #if ENABLE_CHECKING
164 /* Utility debugging function, printing the various type counts within
165 a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
166 void
167 dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
169 int nb_types = 0, nb_scalar = 0, nb_string = 0;
170 int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
171 int nb_lang_struct = 0, nb_param_struct = 0;
172 int nb_user_struct = 0, nb_undefined = 0;
173 type_p p = NULL;
174 for (p = t; p; p = p->next)
176 nb_types++;
177 switch (p->kind)
179 case TYPE_UNDEFINED:
180 nb_undefined++;
181 case TYPE_SCALAR:
182 nb_scalar++;
183 break;
184 case TYPE_STRING:
185 nb_string++;
186 break;
187 case TYPE_STRUCT:
188 nb_struct++;
189 break;
190 case TYPE_USER_STRUCT:
191 nb_user_struct++;
192 break;
193 case TYPE_UNION:
194 nb_union++;
195 break;
196 case TYPE_POINTER:
197 nb_pointer++;
198 break;
199 case TYPE_ARRAY:
200 nb_array++;
201 break;
202 case TYPE_LANG_STRUCT:
203 nb_lang_struct++;
204 break;
205 case TYPE_PARAM_STRUCT:
206 nb_param_struct++;
207 break;
208 case TYPE_NONE:
209 gcc_unreachable ();
212 fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
213 lbasename (fil), lin, msg, nb_types);
214 if (nb_scalar > 0 || nb_string > 0)
215 fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
216 if (nb_struct > 0 || nb_union > 0)
217 fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
218 if (nb_pointer > 0 || nb_array > 0)
219 fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
220 if (nb_lang_struct > 0 || nb_param_struct > 0)
221 fprintf (stderr, "@@%%@@ %d lang_structs, %d param_structs\n",
222 nb_lang_struct, nb_param_struct);
223 if (nb_user_struct > 0)
224 fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
225 if (nb_undefined > 0)
226 fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
227 fprintf (stderr, "\n");
229 #endif /* ENABLE_CHECKING */
231 /* Scan the input file, LIST, and determine how much space we need to
232 store strings in. Also, count the number of language directories
233 and files. The numbers returned are overestimates as they does not
234 consider repeated files. */
235 static size_t
236 measure_input_list (FILE *list)
238 size_t n = 0;
239 int c;
240 bool atbol = true;
241 num_lang_dirs = 0;
242 num_gt_files = plugin_files ? nb_plugin_files : 0;
243 while ((c = getc (list)) != EOF)
245 n++;
246 if (atbol)
248 if (c == '[')
249 num_lang_dirs++;
250 else
252 /* Add space for a lang_bitmap before the input file name. */
253 n += sizeof (lang_bitmap);
254 num_gt_files++;
256 atbol = false;
259 if (c == '\n')
260 atbol = true;
263 rewind (list);
264 return n;
267 /* Read one input line from LIST to HEREP (which is updated). A
268 pointer to the string is returned via LINEP. If it was a language
269 subdirectory in square brackets, strip off the square brackets and
270 return true. Otherwise, leave space before the string for a
271 lang_bitmap, and return false. At EOF, returns false, does not
272 touch *HEREP, and sets *LINEP to NULL. POS is used for
273 diagnostics. */
274 static bool
275 read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
277 char *here = *herep;
278 char *line;
279 int c = getc (list);
281 /* Read over whitespace. */
282 while (c == '\n' || c == ' ')
283 c = getc (list);
285 if (c == EOF)
287 *linep = 0;
288 return false;
290 else if (c == '[')
292 /* No space for a lang_bitmap is necessary. Discard the '['. */
293 c = getc (list);
294 line = here;
295 while (c != ']' && c != '\n' && c != EOF)
297 *here++ = c;
298 c = getc (list);
300 *here++ = '\0';
302 if (c == ']')
304 c = getc (list); /* eat what should be a newline */
305 if (c != '\n' && c != EOF)
306 error_at_line (pos, "junk on line after language tag [%s]", line);
308 else
309 error_at_line (pos, "missing close bracket for language tag [%s",
310 line);
312 *herep = here;
313 *linep = line;
314 return true;
316 else
318 /* Leave space for a lang_bitmap. */
319 memset (here, 0, sizeof (lang_bitmap));
320 here += sizeof (lang_bitmap);
321 line = here;
324 *here++ = c;
325 c = getc (list);
327 while (c != EOF && c != '\n');
328 *here++ = '\0';
329 *herep = here;
330 *linep = line;
331 return false;
335 /* Read the list of input files from LIST and compute all of the
336 relevant tables. There is one file per line of the list. At
337 first, all the files on the list are language-generic, but
338 eventually a line will appear which is the name of a language
339 subdirectory in square brackets, like this: [cp]. All subsequent
340 files are specific to that language, until another language
341 subdirectory tag appears. Files can appear more than once, if
342 they apply to more than one language. */
343 static void
344 read_input_list (const char *listname)
346 FILE *list = fopen (listname, "r");
347 if (!list)
348 fatal ("cannot open %s: %s", listname, xstrerror (errno));
349 else
351 struct fileloc epos;
352 size_t bufsz = measure_input_list (list);
353 char *buf = XNEWVEC (char, bufsz);
354 char *here = buf;
355 char *committed = buf;
356 char *limit = buf + bufsz;
357 char *line;
358 bool is_language;
359 size_t langno = 0;
360 size_t nfiles = 0;
361 lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
363 epos.file = input_file_by_name (listname);
364 epos.line = 0;
366 lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
367 gt_files = XNEWVEC (const input_file *, num_gt_files);
369 for (;;)
371 next_line:
372 epos.line++;
373 committed = here;
374 is_language = read_input_line (list, &here, &line, &epos);
375 gcc_assert (here <= limit);
376 if (line == 0)
377 break;
378 else if (is_language)
380 size_t i;
381 gcc_assert (langno <= num_lang_dirs);
382 for (i = 0; i < langno; i++)
383 if (strcmp (lang_dir_names[i], line) == 0)
385 error_at_line (&epos, "duplicate language tag [%s]",
386 line);
387 curlangs = 1 << i;
388 here = committed;
389 goto next_line;
392 curlangs = 1 << langno;
393 lang_dir_names[langno++] = line;
395 else
397 size_t i;
398 input_file *inpf = input_file_by_name (line);
399 gcc_assert (nfiles <= num_gt_files);
400 for (i = 0; i < nfiles; i++)
401 /* Since the input_file-s are uniquely hash-consed, we
402 can just compare pointers! */
403 if (gt_files[i] == inpf)
405 /* Throw away the string we just read, and add the
406 current language to the existing string's bitmap. */
407 lang_bitmap bmap = get_lang_bitmap (inpf);
408 if (bmap & curlangs)
409 error_at_line (&epos,
410 "file %s specified more than once "
411 "for language %s", line,
412 langno ==
413 0 ? "(all)" : lang_dir_names[langno -
414 1]);
416 bmap |= curlangs;
417 set_lang_bitmap (inpf, bmap);
418 here = committed;
419 goto next_line;
422 set_lang_bitmap (inpf, curlangs);
423 gt_files[nfiles++] = inpf;
426 /* Update the global counts now that we know accurately how many
427 things there are. (We do not bother resizing the arrays down.) */
428 num_lang_dirs = langno;
429 /* Add the plugin files if provided. */
430 if (plugin_files)
432 size_t i;
433 for (i = 0; i < nb_plugin_files; i++)
434 gt_files[nfiles++] = plugin_files[i];
436 num_gt_files = nfiles;
439 /* Sanity check: any file that resides in a language subdirectory
440 (e.g. 'cp') ought to belong to the corresponding language.
441 ??? Still true if for instance ObjC++ is enabled and C++ isn't?
442 (Can you even do that? Should you be allowed to?) */
444 size_t f;
445 for (f = 0; f < num_gt_files; f++)
447 lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
448 const char *basename = get_file_basename (gt_files[f]);
449 const char *slashpos = strchr (basename, '/');
450 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
451 const char *slashpos2 = strchr (basename, '\\');
453 if (!slashpos || (slashpos2 && slashpos2 < slashpos))
454 slashpos = slashpos2;
455 #endif
457 if (slashpos)
459 size_t l;
460 for (l = 0; l < num_lang_dirs; l++)
461 if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
462 && memcmp (basename, lang_dir_names[l],
463 strlen (lang_dir_names[l])) == 0)
465 if (!(bitmap & (1 << l)))
466 error ("%s is in language directory '%s' but is not "
467 "tagged for that language",
468 basename, lang_dir_names[l]);
469 break;
475 if (ferror (list))
476 fatal ("error reading %s: %s", listname, xstrerror (errno));
478 fclose (list);
483 /* The one and only TYPE_STRING. */
485 struct type string_type = {
486 TYPE_STRING, 0, 0, 0, GC_USED, {0}
489 /* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
490 set early in main. */
492 struct type scalar_nonchar = {
493 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
496 struct type scalar_char = {
497 TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
500 /* Lists of various things. */
502 pair_p typedefs = NULL;
503 type_p structures = NULL;
504 type_p param_structs = NULL;
505 pair_p variables = NULL;
507 static type_p find_param_structure (type_p t, type_p param[NUM_PARAM]);
508 static type_p adjust_field_tree_exp (type_p t, options_p opt);
509 static type_p adjust_field_rtx_def (type_p t, options_p opt);
511 /* Define S as a typedef to T at POS. */
513 void
514 do_typedef (const char *s, type_p t, struct fileloc *pos)
516 pair_p p;
518 /* temporary kludge - gengtype doesn't handle conditionals or
519 macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
520 is coming from this file (main() sets them up with safe dummy
521 definitions). */
522 if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
523 return;
525 for (p = typedefs; p != NULL; p = p->next)
526 if (strcmp (p->name, s) == 0)
528 if (p->type != t)
530 error_at_line (pos, "type `%s' previously defined", s);
531 error_at_line (&p->line, "previously defined here");
533 return;
536 p = XNEW (struct pair);
537 p->next = typedefs;
538 p->name = s;
539 p->type = t;
540 p->line = *pos;
541 p->opt = NULL;
542 typedefs = p;
545 /* Define S as a typename of a scalar. Cannot be used to define
546 typedefs of 'char'. Note: is also used for pointer-to-function
547 typedefs (which are therefore not treated as pointers). */
549 void
550 do_scalar_typedef (const char *s, struct fileloc *pos)
552 do_typedef (s, &scalar_nonchar, pos);
556 /* Define TYPE_NAME to be a user defined type at location POS. */
558 type_p
559 create_user_defined_type (const char *type_name, struct fileloc *pos)
561 type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
562 ty->u.s.line = *pos;
563 ty->u.s.bitmap = get_lang_bitmap (pos->file);
564 do_typedef (type_name, ty, pos);
566 /* If TYPE_NAME specifies a template, create references to the types
567 in the template by pretending that each type is a field of TY.
568 This is needed to make sure that the types referenced by the
569 template are marked as used. */
570 char *str = xstrdup (type_name);
571 char *open_bracket = strchr (str, '<');
572 if (open_bracket)
574 /* We only accept simple template declarations (see
575 require_template_declaration), so we only need to parse a
576 comma-separated list of strings, implicitly assumed to
577 be type names. */
578 char *arg = open_bracket + 1;
579 char *type_id = strtok (arg, ",>");
580 pair_p fields = 0;
581 while (type_id)
583 /* Create a new field for every type found inside the template
584 parameter list. */
585 const char *field_name = xstrdup (type_id);
586 type_p arg_type = resolve_typedef (field_name, pos);
587 fields = create_field_at (fields, arg_type, field_name, 0, pos);
588 type_id = strtok (0, ",>");
591 /* Associate the field list to TY. */
592 ty->u.s.fields = fields;
594 free (str);
596 return ty;
600 /* Given a typedef name S, return its associated type. Return NULL if
601 S is not a registered type name. */
603 static type_p
604 type_for_name (const char *s)
606 pair_p p;
607 for (p = typedefs; p != NULL; p = p->next)
608 if (strcmp (p->name, s) == 0)
609 return p->type;
610 return NULL;
614 /* Create an undefined type with name S and location POS. Return the
615 newly created type. */
617 static type_p
618 create_undefined_type (const char *s, struct fileloc *pos)
620 type_p ty = find_structure (s, TYPE_UNDEFINED);
621 ty->u.s.line = *pos;
622 ty->u.s.bitmap = get_lang_bitmap (pos->file);
623 do_typedef (s, ty, pos);
624 return ty;
628 /* Return the type previously defined for S. Use POS to report errors. */
630 type_p
631 resolve_typedef (const char *s, struct fileloc *pos)
633 bool is_template_instance = (strchr (s, '<') != NULL);
634 type_p p = type_for_name (s);
636 /* If we did not find a typedef registered, generate a TYPE_UNDEFINED
637 type for regular type identifiers. If the type identifier S is a
638 template instantiation, however, we treat it as a user defined
639 type.
641 FIXME, this is actually a limitation in gengtype. Supporting
642 template types and their instances would require keeping separate
643 track of the basic types definition and its instances. This
644 essentially forces all template classes in GC to be marked
645 GTY((user)). */
646 if (!p)
647 p = (is_template_instance)
648 ? create_user_defined_type (s, pos)
649 : create_undefined_type (s, pos);
651 return p;
655 /* Create and return a new structure with tag NAME at POS with fields
656 FIELDS and options O. The KIND of structure must be one of
657 TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
659 type_p
660 new_structure (const char *name, enum typekind kind, struct fileloc *pos,
661 pair_p fields, options_p o)
663 type_p si;
664 type_p s = NULL;
665 lang_bitmap bitmap = get_lang_bitmap (pos->file);
666 bool isunion = (kind == TYPE_UNION);
668 gcc_assert (union_or_struct_p (kind));
670 for (si = structures; si != NULL; si = si->next)
671 if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
673 type_p ls = NULL;
674 if (si->kind == TYPE_LANG_STRUCT)
676 ls = si;
678 for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
679 if (si->u.s.bitmap == bitmap)
680 s = si;
682 else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
684 ls = si;
685 type_count++;
686 si = XCNEW (struct type);
687 memcpy (si, ls, sizeof (struct type));
688 ls->kind = TYPE_LANG_STRUCT;
689 ls->u.s.lang_struct = si;
690 ls->u.s.fields = NULL;
691 si->next = NULL;
692 si->state_number = -type_count;
693 si->pointer_to = NULL;
694 si->u.s.lang_struct = ls;
696 else
697 s = si;
699 if (ls != NULL && s == NULL)
701 type_count++;
702 s = XCNEW (struct type);
703 s->state_number = -type_count;
704 s->next = ls->u.s.lang_struct;
705 ls->u.s.lang_struct = s;
706 s->u.s.lang_struct = ls;
708 break;
711 if (s == NULL)
713 type_count++;
714 s = XCNEW (struct type);
715 s->state_number = -type_count;
716 s->next = structures;
717 structures = s;
720 if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
722 error_at_line (pos, "duplicate definition of '%s %s'",
723 isunion ? "union" : "struct", s->u.s.tag);
724 error_at_line (&s->u.s.line, "previous definition here");
727 s->kind = kind;
728 s->u.s.tag = name;
729 s->u.s.line = *pos;
730 s->u.s.fields = fields;
731 s->u.s.opt = o;
732 s->u.s.bitmap = bitmap;
733 if (s->u.s.lang_struct)
734 s->u.s.lang_struct->u.s.bitmap |= bitmap;
736 return s;
739 /* Return the previously-defined structure or union with tag NAME,
740 or a new empty structure or union if none was defined previously.
741 The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
742 TYPE_USER_STRUCT. */
744 type_p
745 find_structure (const char *name, enum typekind kind)
747 type_p s;
748 bool isunion = (kind == TYPE_UNION);
750 gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
752 for (s = structures; s != NULL; s = s->next)
753 if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
754 return s;
756 type_count++;
757 s = XCNEW (struct type);
758 s->next = structures;
759 s->state_number = -type_count;
760 structures = s;
761 s->kind = kind;
762 s->u.s.tag = name;
763 structures = s;
764 return s;
767 /* Return the previously-defined parameterized structure for structure
768 T and parameters PARAM, or a new parameterized empty structure or
769 union if none was defined previously. */
771 static type_p
772 find_param_structure (type_p t, type_p param[NUM_PARAM])
774 type_p res;
776 for (res = param_structs; res; res = res->next)
777 if (res->u.param_struct.stru == t
778 && memcmp (res->u.param_struct.param, param,
779 sizeof (type_p) * NUM_PARAM) == 0)
780 break;
781 if (res == NULL)
783 type_count++;
784 res = XCNEW (struct type);
785 res->kind = TYPE_PARAM_STRUCT;
786 res->next = param_structs;
787 res->state_number = -type_count;
788 param_structs = res;
789 res->u.param_struct.stru = t;
790 memcpy (res->u.param_struct.param, param, sizeof (type_p) * NUM_PARAM);
792 return res;
795 /* Return a scalar type with name NAME. */
797 type_p
798 create_scalar_type (const char *name)
800 if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
801 return &scalar_char;
802 else
803 return &scalar_nonchar;
807 /* Return a pointer to T. */
809 type_p
810 create_pointer (type_p t)
812 if (!t->pointer_to)
814 type_p r = XCNEW (struct type);
815 type_count++;
816 r->state_number = -type_count;
817 r->kind = TYPE_POINTER;
818 r->u.p = t;
819 t->pointer_to = r;
821 return t->pointer_to;
824 /* Return an array of length LEN. */
826 type_p
827 create_array (type_p t, const char *len)
829 type_p v;
831 type_count++;
832 v = XCNEW (struct type);
833 v->kind = TYPE_ARRAY;
834 v->state_number = -type_count;
835 v->u.a.p = t;
836 v->u.a.len = len;
837 return v;
840 /* Return a string options structure with name NAME and info INFO.
841 NEXT is the next option in the chain. */
842 options_p
843 create_string_option (options_p next, const char *name, const char *info)
845 options_p o = XNEW (struct options);
846 o->kind = OPTION_STRING;
847 o->next = next;
848 o->name = name;
849 o->info.string = info;
850 return o;
853 /* Create a type options structure with name NAME and info INFO. NEXT
854 is the next option in the chain. */
855 options_p
856 create_type_option (options_p next, const char* name, type_p info)
858 options_p o = XNEW (struct options);
859 o->next = next;
860 o->name = name;
861 o->kind = OPTION_TYPE;
862 o->info.type = info;
863 return o;
866 /* Create a nested pointer options structure with name NAME and info
867 INFO. NEXT is the next option in the chain. */
868 options_p
869 create_nested_option (options_p next, const char* name,
870 struct nested_ptr_data* info)
872 options_p o;
873 o = XNEW (struct options);
874 o->next = next;
875 o->name = name;
876 o->kind = OPTION_NESTED;
877 o->info.nested = info;
878 return o;
881 /* Return an options structure for a "nested_ptr" option. */
882 options_p
883 create_nested_ptr_option (options_p next, type_p t,
884 const char *to, const char *from)
886 struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
888 d->type = adjust_field_type (t, 0);
889 d->convert_to = to;
890 d->convert_from = from;
891 return create_nested_option (next, "nested_ptr", d);
894 /* Add a variable named S of type T with options O defined at POS,
895 to `variables'. */
896 void
897 note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
899 pair_p n;
900 n = XNEW (struct pair);
901 n->name = s;
902 n->type = t;
903 n->line = *pos;
904 n->opt = o;
905 n->next = variables;
906 variables = n;
909 /* Most-general structure field creator. */
910 static pair_p
911 create_field_all (pair_p next, type_p type, const char *name, options_p opt,
912 const input_file *inpf, int line)
914 pair_p field;
916 field = XNEW (struct pair);
917 field->next = next;
918 field->type = type;
919 field->name = name;
920 field->opt = opt;
921 field->line.file = inpf;
922 field->line.line = line;
923 return field;
926 /* Create a field that came from the source code we are scanning,
927 i.e. we have a 'struct fileloc', and possibly options; also,
928 adjust_field_type should be called. */
929 pair_p
930 create_field_at (pair_p next, type_p type, const char *name, options_p opt,
931 struct fileloc *pos)
933 return create_field_all (next, adjust_field_type (type, opt),
934 name, opt, pos->file, pos->line);
937 /* Create a fake field with the given type and name. NEXT is the next
938 field in the chain. */
939 #define create_field(next,type,name) \
940 create_field_all(next,type,name, 0, this_file, __LINE__)
942 /* Like create_field, but the field is only valid when condition COND
943 is true. */
945 static pair_p
946 create_optional_field_ (pair_p next, type_p type, const char *name,
947 const char *cond, int line)
949 static int id = 1;
950 pair_p union_fields;
951 type_p union_type;
953 /* Create a fake union type with a single nameless field of type TYPE.
954 The field has a tag of "1". This allows us to make the presence
955 of a field of type TYPE depend on some boolean "desc" being true. */
956 union_fields = create_field (NULL, type, "");
957 union_fields->opt =
958 create_string_option (union_fields->opt, "dot", "");
959 union_fields->opt =
960 create_string_option (union_fields->opt, "tag", "1");
961 union_type =
962 new_structure (xasprintf ("%s_%d", "fake_union", id++), TYPE_UNION,
963 &lexer_line, union_fields, NULL);
965 /* Create the field and give it the new fake union type. Add a "desc"
966 tag that specifies the condition under which the field is valid. */
967 return create_field_all (next, union_type, name,
968 create_string_option (0, "desc", cond),
969 this_file, line);
972 #define create_optional_field(next,type,name,cond) \
973 create_optional_field_(next,type,name,cond,__LINE__)
975 /* Reverse a linked list of 'struct pair's in place. */
976 pair_p
977 nreverse_pairs (pair_p list)
979 pair_p prev = 0, p, next;
980 for (p = list; p; p = next)
982 next = p->next;
983 p->next = prev;
984 prev = p;
986 return prev;
990 /* We don't care how long a CONST_DOUBLE is. */
991 #define CONST_DOUBLE_FORMAT "ww"
992 /* We don't want to see codes that are only for generator files. */
993 #undef GENERATOR_FILE
995 enum rtx_code
997 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) ENUM ,
998 #include "rtl.def"
999 #undef DEF_RTL_EXPR
1000 NUM_RTX_CODE
1003 static const char *const rtx_name[NUM_RTX_CODE] = {
1004 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) NAME ,
1005 #include "rtl.def"
1006 #undef DEF_RTL_EXPR
1009 static const char *const rtx_format[NUM_RTX_CODE] = {
1010 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) FORMAT ,
1011 #include "rtl.def"
1012 #undef DEF_RTL_EXPR
1015 static int rtx_next_new[NUM_RTX_CODE];
1017 /* We also need codes and names for insn notes (not register notes).
1018 Note that we do *not* bias the note values here. */
1019 enum insn_note
1021 #define DEF_INSN_NOTE(NAME) NAME,
1022 #include "insn-notes.def"
1023 #undef DEF_INSN_NOTE
1025 NOTE_INSN_MAX
1028 /* We must allocate one more entry here, as we use NOTE_INSN_MAX as the
1029 default field for line number notes. */
1030 static const char *const note_insn_name[NOTE_INSN_MAX + 1] = {
1031 #define DEF_INSN_NOTE(NAME) #NAME,
1032 #include "insn-notes.def"
1033 #undef DEF_INSN_NOTE
1036 #undef CONST_DOUBLE_FORMAT
1037 #define GENERATOR_FILE
1039 /* Generate the contents of the rtx_next array. This really doesn't belong
1040 in gengtype at all, but it's needed for adjust_field_rtx_def. */
1042 static void
1043 gen_rtx_next (void)
1045 int i;
1046 for (i = 0; i < NUM_RTX_CODE; i++)
1048 int k;
1050 rtx_next_new[i] = -1;
1051 if (strncmp (rtx_format[i], "iuu", 3) == 0)
1052 rtx_next_new[i] = 2;
1053 else if (i == COND_EXEC || i == SET || i == EXPR_LIST || i == INSN_LIST)
1054 rtx_next_new[i] = 1;
1055 else
1056 for (k = strlen (rtx_format[i]) - 1; k >= 0; k--)
1057 if (rtx_format[i][k] == 'e' || rtx_format[i][k] == 'u')
1058 rtx_next_new[i] = k;
1062 /* Write out the contents of the rtx_next array. */
1063 static void
1064 write_rtx_next (void)
1066 outf_p f = get_output_file_with_visibility (NULL);
1067 int i;
1068 if (!f)
1069 return;
1071 oprintf (f, "\n/* Used to implement the RTX_NEXT macro. */\n");
1072 oprintf (f, "EXPORTED_CONST unsigned char rtx_next[NUM_RTX_CODE] = {\n");
1073 for (i = 0; i < NUM_RTX_CODE; i++)
1074 if (rtx_next_new[i] == -1)
1075 oprintf (f, " 0,\n");
1076 else
1077 oprintf (f,
1078 " RTX_HDR_SIZE + %d * sizeof (rtunion),\n", rtx_next_new[i]);
1079 oprintf (f, "};\n");
1082 /* Handle `special("rtx_def")'. This is a special case for field
1083 `fld' of struct rtx_def, which is an array of unions whose values
1084 are based in a complex way on the type of RTL. */
1086 static type_p
1087 adjust_field_rtx_def (type_p t, options_p ARG_UNUSED (opt))
1089 pair_p flds = NULL;
1090 options_p nodot;
1091 int i;
1092 type_p rtx_tp, rtvec_tp, tree_tp, mem_attrs_tp, note_union_tp, scalar_tp;
1093 type_p basic_block_tp, reg_attrs_tp, constant_tp, symbol_union_tp;
1095 if (t->kind != TYPE_UNION)
1097 error_at_line (&lexer_line,
1098 "special `rtx_def' must be applied to a union");
1099 return &string_type;
1102 nodot = create_string_option (NULL, "dot", "");
1104 rtx_tp = create_pointer (find_structure ("rtx_def", TYPE_STRUCT));
1105 rtvec_tp = create_pointer (find_structure ("rtvec_def", TYPE_STRUCT));
1106 tree_tp = create_pointer (find_structure ("tree_node", TYPE_UNION));
1107 mem_attrs_tp = create_pointer (find_structure ("mem_attrs", TYPE_STRUCT));
1108 reg_attrs_tp =
1109 create_pointer (find_structure ("reg_attrs", TYPE_STRUCT));
1110 basic_block_tp =
1111 create_pointer (find_structure ("basic_block_def", TYPE_STRUCT));
1112 constant_tp =
1113 create_pointer (find_structure ("constant_descriptor_rtx", TYPE_STRUCT));
1114 scalar_tp = &scalar_nonchar; /* rtunion int */
1117 pair_p note_flds = NULL;
1118 int c;
1120 for (c = 0; c <= NOTE_INSN_MAX; c++)
1122 switch (c)
1124 case NOTE_INSN_MAX:
1125 case NOTE_INSN_DELETED_LABEL:
1126 case NOTE_INSN_DELETED_DEBUG_LABEL:
1127 note_flds = create_field (note_flds, &string_type, "rt_str");
1128 break;
1130 case NOTE_INSN_BLOCK_BEG:
1131 case NOTE_INSN_BLOCK_END:
1132 note_flds = create_field (note_flds, tree_tp, "rt_tree");
1133 break;
1135 case NOTE_INSN_VAR_LOCATION:
1136 case NOTE_INSN_CALL_ARG_LOCATION:
1137 note_flds = create_field (note_flds, rtx_tp, "rt_rtx");
1138 break;
1140 default:
1141 note_flds = create_field (note_flds, scalar_tp, "rt_int");
1142 break;
1144 /* NOTE_INSN_MAX is used as the default field for line
1145 number notes. */
1146 if (c == NOTE_INSN_MAX)
1147 note_flds->opt =
1148 create_string_option (nodot, "default", "");
1149 else
1150 note_flds->opt =
1151 create_string_option (nodot, "tag", note_insn_name[c]);
1153 note_union_tp = new_structure ("rtx_def_note_subunion", TYPE_UNION,
1154 &lexer_line, note_flds, NULL);
1156 /* Create a type to represent the various forms of SYMBOL_REF_DATA. */
1158 pair_p sym_flds;
1159 sym_flds = create_field (NULL, tree_tp, "rt_tree");
1160 sym_flds->opt = create_string_option (nodot, "default", "");
1161 sym_flds = create_field (sym_flds, constant_tp, "rt_constant");
1162 sym_flds->opt = create_string_option (nodot, "tag", "1");
1163 symbol_union_tp = new_structure ("rtx_def_symbol_subunion", TYPE_UNION,
1164 &lexer_line, sym_flds, NULL);
1166 for (i = 0; i < NUM_RTX_CODE; i++)
1168 pair_p subfields = NULL;
1169 size_t aindex, nmindex;
1170 const char *sname;
1171 type_p substruct;
1172 char *ftag;
1174 for (aindex = 0; aindex < strlen (rtx_format[i]); aindex++)
1176 type_p t;
1177 const char *subname;
1179 switch (rtx_format[i][aindex])
1181 case '*':
1182 case 'i':
1183 case 'n':
1184 case 'w':
1185 t = scalar_tp;
1186 subname = "rt_int";
1187 break;
1189 case '0':
1190 if (i == MEM && aindex == 1)
1191 t = mem_attrs_tp, subname = "rt_mem";
1192 else if (i == JUMP_INSN && aindex == 8)
1193 t = rtx_tp, subname = "rt_rtx";
1194 else if (i == CODE_LABEL && aindex == 5)
1195 t = scalar_tp, subname = "rt_int";
1196 else if (i == CODE_LABEL && aindex == 4)
1197 t = rtx_tp, subname = "rt_rtx";
1198 else if (i == LABEL_REF && (aindex == 1 || aindex == 2))
1199 t = rtx_tp, subname = "rt_rtx";
1200 else if (i == NOTE && aindex == 4)
1201 t = note_union_tp, subname = "";
1202 else if (i == NOTE && aindex == 5)
1203 t = scalar_tp, subname = "rt_int";
1204 else if (i == NOTE && aindex >= 7)
1205 t = scalar_tp, subname = "rt_int";
1206 else if (i == ADDR_DIFF_VEC && aindex == 4)
1207 t = scalar_tp, subname = "rt_int";
1208 else if (i == VALUE && aindex == 0)
1209 t = scalar_tp, subname = "rt_int";
1210 else if (i == DEBUG_EXPR && aindex == 0)
1211 t = tree_tp, subname = "rt_tree";
1212 else if (i == REG && aindex == 1)
1213 t = scalar_tp, subname = "rt_int";
1214 else if (i == REG && aindex == 2)
1215 t = reg_attrs_tp, subname = "rt_reg";
1216 else if (i == SCRATCH && aindex == 0)
1217 t = scalar_tp, subname = "rt_int";
1218 else if (i == SYMBOL_REF && aindex == 1)
1219 t = scalar_tp, subname = "rt_int";
1220 else if (i == SYMBOL_REF && aindex == 2)
1221 t = symbol_union_tp, subname = "";
1222 else if (i == JUMP_TABLE_DATA && aindex >= 5)
1223 t = scalar_tp, subname = "rt_int";
1224 else if (i == BARRIER && aindex >= 3)
1225 t = scalar_tp, subname = "rt_int";
1226 else if (i == ENTRY_VALUE && aindex == 0)
1227 t = rtx_tp, subname = "rt_rtx";
1228 else
1230 error_at_line
1231 (&lexer_line,
1232 "rtx type `%s' has `0' in position %lu, can't handle",
1233 rtx_name[i], (unsigned long) aindex);
1234 t = &string_type;
1235 subname = "rt_int";
1237 break;
1239 case 's':
1240 case 'S':
1241 case 'T':
1242 t = &string_type;
1243 subname = "rt_str";
1244 break;
1246 case 'e':
1247 case 'u':
1248 t = rtx_tp;
1249 subname = "rt_rtx";
1250 break;
1252 case 'E':
1253 case 'V':
1254 t = rtvec_tp;
1255 subname = "rt_rtvec";
1256 break;
1258 case 't':
1259 t = tree_tp;
1260 subname = "rt_tree";
1261 break;
1263 case 'B':
1264 t = basic_block_tp;
1265 subname = "rt_bb";
1266 break;
1268 default:
1269 error_at_line
1270 (&lexer_line,
1271 "rtx type `%s' has `%c' in position %lu, can't handle",
1272 rtx_name[i], rtx_format[i][aindex],
1273 (unsigned long) aindex);
1274 t = &string_type;
1275 subname = "rt_int";
1276 break;
1279 subfields = create_field (subfields, t,
1280 xasprintf (".fld[%lu].%s",
1281 (unsigned long) aindex,
1282 subname));
1283 subfields->opt = nodot;
1284 if (t == note_union_tp)
1285 subfields->opt =
1286 create_string_option (subfields->opt, "desc",
1287 "NOTE_KIND (&%0)");
1288 if (t == symbol_union_tp)
1289 subfields->opt =
1290 create_string_option (subfields->opt, "desc",
1291 "CONSTANT_POOL_ADDRESS_P (&%0)");
1294 if (i == SYMBOL_REF)
1296 /* Add the "block_sym" field if SYMBOL_REF_HAS_BLOCK_INFO_P
1297 holds. */
1298 type_p field_tp = find_structure ("block_symbol", TYPE_STRUCT);
1299 subfields
1300 = create_optional_field (subfields, field_tp, "block_sym",
1301 "SYMBOL_REF_HAS_BLOCK_INFO_P (&%0)");
1304 sname = xasprintf ("rtx_def_%s", rtx_name[i]);
1305 substruct = new_structure (sname, TYPE_STRUCT, &lexer_line, subfields,
1306 NULL);
1308 ftag = xstrdup (rtx_name[i]);
1309 for (nmindex = 0; nmindex < strlen (ftag); nmindex++)
1310 ftag[nmindex] = TOUPPER (ftag[nmindex]);
1311 flds = create_field (flds, substruct, "");
1312 flds->opt = create_string_option (nodot, "tag", ftag);
1314 return new_structure ("rtx_def_subunion", TYPE_UNION, &lexer_line, flds,
1315 nodot);
1318 /* Handle `special("tree_exp")'. This is a special case for
1319 field `operands' of struct tree_exp, which although it claims to contain
1320 pointers to trees, actually sometimes contains pointers to RTL too.
1321 Passed T, the old type of the field, and OPT its options. Returns
1322 a new type for the field. */
1324 static type_p
1325 adjust_field_tree_exp (type_p t, options_p opt ATTRIBUTE_UNUSED)
1327 pair_p flds;
1328 options_p nodot;
1330 if (t->kind != TYPE_ARRAY)
1332 error_at_line (&lexer_line,
1333 "special `tree_exp' must be applied to an array");
1334 return &string_type;
1337 nodot = create_string_option (NULL, "dot", "");
1339 flds = create_field (NULL, t, "");
1340 flds->opt = create_string_option (nodot, "length",
1341 "TREE_OPERAND_LENGTH ((tree) &%0)");
1342 flds->opt = create_string_option (flds->opt, "default", "");
1344 return new_structure ("tree_exp_subunion", TYPE_UNION, &lexer_line, flds,
1345 nodot);
1348 /* Perform any special processing on a type T, about to become the type
1349 of a field. Return the appropriate type for the field.
1350 At present:
1351 - Converts pointer-to-char, with no length parameter, to TYPE_STRING;
1352 - Similarly for arrays of pointer-to-char;
1353 - Converts structures for which a parameter is provided to
1354 TYPE_PARAM_STRUCT;
1355 - Handles "special" options.
1358 type_p
1359 adjust_field_type (type_p t, options_p opt)
1361 int length_p = 0;
1362 const int pointer_p = t->kind == TYPE_POINTER;
1363 type_p params[NUM_PARAM];
1364 int params_p = 0;
1365 int i;
1367 for (i = 0; i < NUM_PARAM; i++)
1368 params[i] = NULL;
1370 for (; opt; opt = opt->next)
1371 if (strcmp (opt->name, "length") == 0)
1373 if (length_p)
1374 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1375 if (t->u.p->kind == TYPE_SCALAR || t->u.p->kind == TYPE_STRING)
1377 error_at_line (&lexer_line,
1378 "option `%s' may not be applied to "
1379 "arrays of atomic types", opt->name);
1381 length_p = 1;
1383 else if ((strcmp (opt->name, "param_is") == 0
1384 || (strncmp (opt->name, "param", 5) == 0
1385 && ISDIGIT (opt->name[5])
1386 && strcmp (opt->name + 6, "_is") == 0))
1387 && opt->kind == OPTION_TYPE)
1389 int num = ISDIGIT (opt->name[5]) ? opt->name[5] - '0' : 0;
1391 if (!union_or_struct_p (t)
1392 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
1394 error_at_line (&lexer_line,
1395 "option `%s' may only be applied to structures or structure pointers",
1396 opt->name);
1397 return t;
1400 params_p = 1;
1401 if (params[num] != NULL)
1402 error_at_line (&lexer_line, "duplicate `%s' option", opt->name);
1403 if (!ISDIGIT (opt->name[5]))
1404 params[num] = create_pointer (opt->info.type);
1405 else
1406 params[num] = opt->info.type;
1408 else if (strcmp (opt->name, "special") == 0
1409 && opt->kind == OPTION_STRING)
1411 const char *special_name = opt->info.string;
1412 if (strcmp (special_name, "tree_exp") == 0)
1413 t = adjust_field_tree_exp (t, opt);
1414 else if (strcmp (special_name, "rtx_def") == 0)
1415 t = adjust_field_rtx_def (t, opt);
1416 else
1417 error_at_line (&lexer_line, "unknown special `%s'", special_name);
1420 if (params_p)
1422 type_p realt;
1424 if (pointer_p)
1425 t = t->u.p;
1426 realt = find_param_structure (t, params);
1427 t = pointer_p ? create_pointer (realt) : realt;
1430 if (!length_p
1431 && pointer_p && t->u.p->kind == TYPE_SCALAR && t->u.p->u.scalar_is_char)
1432 return &string_type;
1433 if (t->kind == TYPE_ARRAY && t->u.a.p->kind == TYPE_POINTER
1434 && t->u.a.p->u.p->kind == TYPE_SCALAR
1435 && t->u.a.p->u.p->u.scalar_is_char)
1436 return create_array (&string_type, t->u.a.len);
1438 return t;
1442 static void set_gc_used_type (type_p, enum gc_used_enum, type_p *,
1443 bool = false);
1444 static void set_gc_used (pair_p);
1446 /* Handle OPT for set_gc_used_type. */
1448 static void
1449 process_gc_options (options_p opt, enum gc_used_enum level, int *maybe_undef,
1450 int *pass_param, int *length, int *skip,
1451 type_p *nested_ptr)
1453 options_p o;
1454 for (o = opt; o; o = o->next)
1455 if (strcmp (o->name, "ptr_alias") == 0 && level == GC_POINTED_TO
1456 && o->kind == OPTION_TYPE)
1457 set_gc_used_type (o->info.type,
1458 GC_POINTED_TO, NULL);
1459 else if (strcmp (o->name, "maybe_undef") == 0)
1460 *maybe_undef = 1;
1461 else if (strcmp (o->name, "use_params") == 0)
1462 *pass_param = 1;
1463 else if (strcmp (o->name, "length") == 0)
1464 *length = 1;
1465 else if (strcmp (o->name, "skip") == 0)
1466 *skip = 1;
1467 else if (strcmp (o->name, "nested_ptr") == 0
1468 && o->kind == OPTION_NESTED)
1469 *nested_ptr = ((const struct nested_ptr_data *) o->info.nested)->type;
1473 /* Set the gc_used field of T to LEVEL, and handle the types it references.
1475 If ALLOWED_UNDEFINED_TYPES is true, types of kind TYPE_UNDEFINED
1476 are set to GC_UNUSED. Otherwise, an error is emitted for
1477 TYPE_UNDEFINED types. This is used to support user-defined
1478 template types with non-type arguments.
1480 For instance, when we parse a template type with enum arguments
1481 (e.g. MyType<AnotherType, EnumValue>), the parser created two
1482 artificial fields for 'MyType', one for 'AnotherType', the other
1483 one for 'EnumValue'.
1485 At the time that we parse this type we don't know that 'EnumValue'
1486 is really an enum value, so the parser creates a TYPE_UNDEFINED
1487 type for it. Since 'EnumValue' is never resolved to a known
1488 structure, it will stay with TYPE_UNDEFINED.
1490 Since 'MyType' is a TYPE_USER_STRUCT, we can simply ignore
1491 'EnumValue'. Generating marking code for it would cause
1492 compilation failures since the marking routines assumes that
1493 'EnumValue' is a type. */
1495 static void
1496 set_gc_used_type (type_p t, enum gc_used_enum level, type_p param[NUM_PARAM],
1497 bool allow_undefined_types)
1499 if (t->gc_used >= level)
1500 return;
1502 t->gc_used = level;
1504 switch (t->kind)
1506 case TYPE_STRUCT:
1507 case TYPE_UNION:
1508 case TYPE_USER_STRUCT:
1510 pair_p f;
1511 int dummy;
1512 type_p dummy2;
1513 bool allow_undefined_field_types = (t->kind == TYPE_USER_STRUCT);
1515 process_gc_options (t->u.s.opt, level, &dummy, &dummy, &dummy, &dummy,
1516 &dummy2);
1518 for (f = t->u.s.fields; f; f = f->next)
1520 int maybe_undef = 0;
1521 int pass_param = 0;
1522 int length = 0;
1523 int skip = 0;
1524 type_p nested_ptr = NULL;
1525 process_gc_options (f->opt, level, &maybe_undef, &pass_param,
1526 &length, &skip, &nested_ptr);
1528 if (nested_ptr && f->type->kind == TYPE_POINTER)
1529 set_gc_used_type (nested_ptr, GC_POINTED_TO,
1530 pass_param ? param : NULL);
1531 else if (length && f->type->kind == TYPE_POINTER)
1532 set_gc_used_type (f->type->u.p, GC_USED, NULL);
1533 else if (maybe_undef && f->type->kind == TYPE_POINTER)
1534 set_gc_used_type (f->type->u.p, GC_MAYBE_POINTED_TO, NULL);
1535 else if (pass_param && f->type->kind == TYPE_POINTER && param)
1536 set_gc_used_type (find_param_structure (f->type->u.p, param),
1537 GC_POINTED_TO, NULL);
1538 else if (skip)
1539 ; /* target type is not used through this field */
1540 else
1541 set_gc_used_type (f->type, GC_USED, pass_param ? param : NULL,
1542 allow_undefined_field_types);
1544 break;
1547 case TYPE_UNDEFINED:
1548 if (level > GC_UNUSED)
1550 if (!allow_undefined_types)
1551 error_at_line (&t->u.s.line, "undefined type `%s'", t->u.s.tag);
1552 t->gc_used = GC_UNUSED;
1554 break;
1556 case TYPE_POINTER:
1557 set_gc_used_type (t->u.p, GC_POINTED_TO, NULL);
1558 break;
1560 case TYPE_ARRAY:
1561 set_gc_used_type (t->u.a.p, GC_USED, param);
1562 break;
1564 case TYPE_LANG_STRUCT:
1565 for (t = t->u.s.lang_struct; t; t = t->next)
1566 set_gc_used_type (t, level, param);
1567 break;
1569 case TYPE_PARAM_STRUCT:
1571 int i;
1572 for (i = 0; i < NUM_PARAM; i++)
1573 if (t->u.param_struct.param[i] != 0)
1574 set_gc_used_type (t->u.param_struct.param[i], GC_USED, NULL);
1576 if (t->u.param_struct.stru->gc_used == GC_POINTED_TO)
1577 level = GC_POINTED_TO;
1578 else
1579 level = GC_USED;
1580 t->u.param_struct.stru->gc_used = GC_UNUSED;
1581 set_gc_used_type (t->u.param_struct.stru, level,
1582 t->u.param_struct.param);
1583 break;
1585 default:
1586 break;
1590 /* Set the gc_used fields of all the types pointed to by VARIABLES. */
1592 static void
1593 set_gc_used (pair_p variables)
1595 int nbvars = 0;
1596 pair_p p;
1597 for (p = variables; p; p = p->next)
1599 set_gc_used_type (p->type, GC_USED, NULL);
1600 nbvars++;
1602 if (verbosity_level >= 2)
1603 printf ("%s used %d GTY-ed variables\n", progname, nbvars);
1606 /* File mapping routines. For each input file, there is one output .c file
1607 (but some output files have many input files), and there is one .h file
1608 for the whole build. */
1610 /* Output file handling. */
1612 /* Create and return an outf_p for a new file for NAME, to be called
1613 ONAME. */
1615 static outf_p
1616 create_file (const char *name, const char *oname)
1618 static const char *const hdr[] = {
1619 " Copyright (C) 2004-2013 Free Software Foundation, Inc.\n",
1620 "\n",
1621 "This file is part of GCC.\n",
1622 "\n",
1623 "GCC is free software; you can redistribute it and/or modify it under\n",
1624 "the terms of the GNU General Public License as published by the Free\n",
1625 "Software Foundation; either version 3, or (at your option) any later\n",
1626 "version.\n",
1627 "\n",
1628 "GCC is distributed in the hope that it will be useful, but WITHOUT ANY\n",
1629 "WARRANTY; without even the implied warranty of MERCHANTABILITY or\n",
1630 "FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n",
1631 "for more details.\n",
1632 "\n",
1633 "You should have received a copy of the GNU General Public License\n",
1634 "along with GCC; see the file COPYING3. If not see\n",
1635 "<http://www.gnu.org/licenses/>. */\n",
1636 "\n",
1637 "/* This file is machine generated. Do not edit. */\n"
1639 outf_p f;
1640 size_t i;
1642 gcc_assert (name != NULL);
1643 gcc_assert (oname != NULL);
1644 f = XCNEW (struct outf);
1645 f->next = output_files;
1646 f->name = oname;
1647 output_files = f;
1649 oprintf (f, "/* Type information for %s.\n", name);
1650 for (i = 0; i < ARRAY_SIZE (hdr); i++)
1651 oprintf (f, "%s", hdr[i]);
1652 return f;
1655 /* Print, like fprintf, to O.
1656 N.B. You might think this could be implemented more efficiently
1657 with vsnprintf(). Unfortunately, there are C libraries that
1658 provide that function but without the C99 semantics for its return
1659 value, making it impossible to know how much space is required. */
1660 void
1661 oprintf (outf_p o, const char *format, ...)
1663 char *s;
1664 size_t slength;
1665 va_list ap;
1667 /* In plugin mode, the O could be a NULL pointer, so avoid crashing
1668 in that case. */
1669 if (!o)
1670 return;
1672 va_start (ap, format);
1673 slength = vasprintf (&s, format, ap);
1674 if (s == NULL || (int) slength < 0)
1675 fatal ("out of memory");
1676 va_end (ap);
1678 if (o->bufused + slength > o->buflength)
1680 size_t new_len = o->buflength;
1681 if (new_len == 0)
1682 new_len = 1024;
1685 new_len *= 2;
1687 while (o->bufused + slength >= new_len);
1688 o->buf = XRESIZEVEC (char, o->buf, new_len);
1689 o->buflength = new_len;
1691 memcpy (o->buf + o->bufused, s, slength);
1692 o->bufused += slength;
1693 free (s);
1696 /* Open the global header file and the language-specific header files. */
1698 static void
1699 open_base_files (void)
1701 size_t i;
1703 if (nb_plugin_files > 0 && plugin_files)
1704 return;
1706 header_file = create_file ("GCC", "gtype-desc.h");
1708 base_files = XNEWVEC (outf_p, num_lang_dirs);
1710 for (i = 0; i < num_lang_dirs; i++)
1711 base_files[i] = create_file (lang_dir_names[i],
1712 xasprintf ("gtype-%s.h", lang_dir_names[i]));
1714 /* gtype-desc.c is a little special, so we create it here. */
1716 /* The order of files here matters very much. */
1717 static const char *const ifiles[] = {
1718 "config.h", "system.h", "coretypes.h", "tm.h",
1719 "hashtab.h", "splay-tree.h", "obstack.h", "bitmap.h", "input.h",
1720 "tree.h", "rtl.h", "function.h", "insn-config.h", "expr.h",
1721 "hard-reg-set.h", "basic-block.h", "cselib.h", "insn-addr.h",
1722 "optabs.h", "libfuncs.h", "debug.h", "ggc.h", "cgraph.h",
1723 "tree-flow.h", "reload.h", "cpp-id-data.h", "tree-chrec.h",
1724 "except.h", "output.h", "gimple.h", "cfgloop.h",
1725 "target.h", "ipa-prop.h", "lto-streamer.h", "target-globals.h",
1726 "ipa-inline.h", "dwarf2out.h", NULL
1728 const char *const *ifp;
1729 outf_p gtype_desc_c;
1731 gtype_desc_c = create_file ("GCC", "gtype-desc.c");
1732 for (ifp = ifiles; *ifp; ifp++)
1733 oprintf (gtype_desc_c, "#include \"%s\"\n", *ifp);
1735 /* Make sure we handle "cfun" specially. */
1736 oprintf (gtype_desc_c, "\n/* See definition in function.h. */\n");
1737 oprintf (gtype_desc_c, "#undef cfun\n");
1741 /* For INPF an input file, return the real basename of INPF, with all
1742 the directory components skipped. */
1744 static const char *
1745 get_file_realbasename (const input_file *inpf)
1747 return lbasename (get_input_file_name (inpf));
1750 /* For INPF a filename, return the relative path to INPF from
1751 $(srcdir) if the latter is a prefix in INPF, NULL otherwise. */
1753 const char *
1754 get_file_srcdir_relative_path (const input_file *inpf)
1756 const char *f = get_input_file_name (inpf);
1757 if (strlen (f) > srcdir_len
1758 && IS_DIR_SEPARATOR (f[srcdir_len])
1759 && strncmp (f, srcdir, srcdir_len) == 0)
1760 return f + srcdir_len + 1;
1761 else
1762 return NULL;
1765 /* For INPF an input_file, return the relative path to INPF from
1766 $(srcdir) if the latter is a prefix in INPF, or the real basename
1767 of INPF otherwise. */
1769 static const char *
1770 get_file_basename (const input_file *inpf)
1772 const char *srcdir_path = get_file_srcdir_relative_path (inpf);
1774 return (srcdir_path != NULL) ? srcdir_path : get_file_realbasename (inpf);
1777 /* For F a filename, return the lang_dir_names relative index of the language
1778 directory that is a prefix in F, if any, -1 otherwise. */
1780 static int
1781 get_prefix_langdir_index (const char *f)
1783 size_t f_len = strlen (f);
1784 size_t lang_index;
1786 for (lang_index = 0; lang_index < num_lang_dirs; lang_index++)
1788 const char *langdir = lang_dir_names[lang_index];
1789 size_t langdir_len = strlen (langdir);
1791 if (f_len > langdir_len
1792 && IS_DIR_SEPARATOR (f[langdir_len])
1793 && memcmp (f, langdir, langdir_len) == 0)
1794 return lang_index;
1797 return -1;
1800 /* For INPF an input file, return the name of language directory where
1801 F is located, if any, NULL otherwise. */
1803 static const char *
1804 get_file_langdir (const input_file *inpf)
1806 /* Get the relative path to INPF from $(srcdir) and find the
1807 language by comparing the prefix with language directory names.
1808 If INPF is not even srcdir relative, no point in looking
1809 further. */
1811 int lang_index;
1812 const char *srcdir_relative_path = get_file_srcdir_relative_path (inpf);
1813 const char *r;
1815 if (!srcdir_relative_path)
1816 return NULL;
1818 lang_index = get_prefix_langdir_index (srcdir_relative_path);
1819 if (lang_index < 0 && strncmp (srcdir_relative_path, "c-family", 8) == 0)
1820 r = "c-family";
1821 else if (lang_index >= 0)
1822 r = lang_dir_names[lang_index];
1823 else
1824 r = NULL;
1826 return r;
1829 /* The gt- output file name for INPF. */
1831 static const char *
1832 get_file_gtfilename (const input_file *inpf)
1834 /* Cook up an initial version of the gt- file name from the file real
1835 basename and the language name, if any. */
1837 const char *basename = get_file_realbasename (inpf);
1838 const char *langdir = get_file_langdir (inpf);
1840 char *result =
1841 (langdir ? xasprintf ("gt-%s-%s", langdir, basename)
1842 : xasprintf ("gt-%s", basename));
1844 /* Then replace all non alphanumerics characters by '-' and change the
1845 extension to ".h". We expect the input filename extension was at least
1846 one character long. */
1848 char *s = result;
1850 for (; *s != '.'; s++)
1851 if (!ISALNUM (*s) && *s != '-')
1852 *s = '-';
1854 memcpy (s, ".h", sizeof (".h"));
1856 return result;
1859 /* Each input_file has its associated output file outf_p. The
1860 association is computed by the function
1861 get_output_file_with_visibility. The associated file is cached
1862 inside input_file in its inpoutf field, so is really computed only
1863 once. Associated output file paths (i.e. output_name-s) are
1864 computed by a rule based regexp machinery, using the files_rules
1865 array of struct file_rule_st. A for_name is also computed, giving
1866 the source file name for which the output_file is generated; it is
1867 often the last component of the input_file path. */
1871 Regexpr machinery to compute the output_name and for_name-s of each
1872 input_file. We have a sequence of file rules which gives the POSIX
1873 extended regular expression to match an input file path, and two
1874 transformed strings for the corresponding output_name and the
1875 corresponding for_name. The transformed string contain dollars: $0
1876 is replaced by the entire match, $1 is replaced by the substring
1877 matching the first parenthesis in the regexp, etc. And $$ is replaced
1878 by a single verbatim dollar. The rule order is important. The
1879 general case is last, and the particular cases should come before.
1880 An action routine can, when needed, update the out_name & for_name
1881 and/or return the appropriate output file. It is invoked only when a
1882 rule is triggered. When a rule is triggered, the output_name and
1883 for_name are computed using their transform string in while $$, $0,
1884 $1, ... are suitably replaced. If there is an action, it is called.
1885 In some few cases, the action can directly return the outf_p, but
1886 usually it just updates the output_name and for_name so should free
1887 them before replacing them. The get_output_file_with_visibility
1888 function creates an outf_p only once per each output_name, so it
1889 scans the output_files list for previously seen output file names.
1892 /* Signature of actions in file rules. */
1893 typedef outf_p (frul_actionrout_t) (input_file*, char**, char**);
1896 struct file_rule_st {
1897 const char* frul_srcexpr; /* Source string for regexp. */
1898 int frul_rflags; /* Flags passed to regcomp, usually
1899 * REG_EXTENDED. */
1900 regex_t* frul_re; /* Compiled regular expression
1901 obtained by regcomp. */
1902 const char* frul_tr_out; /* Transformation string for making
1903 * the output_name, with $1 ... $9 for
1904 * subpatterns and $0 for the whole
1905 * matched filename. */
1906 const char* frul_tr_for; /* Tranformation string for making the
1907 for_name. */
1908 frul_actionrout_t* frul_action; /* The action, if non null, is
1909 * called once the rule matches, on
1910 * the transformed out_name &
1911 * for_name. It could change them
1912 * and/or give the output file. */
1915 /* File rule action handling *.h files. */
1916 static outf_p header_dot_h_frul (input_file*, char**, char**);
1918 /* File rule action handling *.c files. */
1919 static outf_p source_dot_c_frul (input_file*, char**, char**);
1921 #define NULL_REGEX (regex_t*)0
1923 /* The prefix in our regexp-s matching the directory. */
1924 #define DIR_PREFIX_REGEX "^(([^/]*/)*)"
1926 #define NULL_FRULACT (frul_actionrout_t*)0
1928 /* The array of our rules governing file name generation. Rules order
1929 matters, so change with extreme care! */
1931 struct file_rule_st files_rules[] = {
1932 /* The general rule assumes that files in subdirectories belong to a
1933 particular front-end, and files not in subdirectories are shared.
1934 The following rules deal with exceptions - files that are in
1935 subdirectories and yet are shared, and files that are top-level,
1936 but are not shared. */
1938 /* the c-family/ source directory is special. */
1939 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.c$",
1940 REG_EXTENDED, NULL_REGEX,
1941 "gt-c-family-$3.h", "c-family/$3.c", NULL_FRULACT},
1943 { DIR_PREFIX_REGEX "c-family/([[:alnum:]_-]*)\\.h$",
1944 REG_EXTENDED, NULL_REGEX,
1945 "gt-c-family-$3.h", "c-family/$3.h", NULL_FRULACT},
1947 /* Both c-lang.h & c-tree.h gives gt-c-c-decl.h for c-decl.c ! */
1948 { DIR_PREFIX_REGEX "c/c-lang\\.h$",
1949 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1951 { DIR_PREFIX_REGEX "c/c-tree\\.h$",
1952 REG_EXTENDED, NULL_REGEX, "gt-c-c-decl.h", "c/c-decl.c", NULL_FRULACT},
1954 /* cp/cp-tree.h gives gt-cp-tree.h for cp/tree.c ! */
1955 { DIR_PREFIX_REGEX "cp/cp-tree\\.h$",
1956 REG_EXTENDED, NULL_REGEX,
1957 "gt-cp-tree.h", "cp/tree.c", NULL_FRULACT },
1959 /* cp/decl.h & cp/decl.c gives gt-cp-decl.h for cp/decl.c ! */
1960 { DIR_PREFIX_REGEX "cp/decl\\.[ch]$",
1961 REG_EXTENDED, NULL_REGEX,
1962 "gt-cp-decl.h", "cp/decl.c", NULL_FRULACT },
1964 /* cp/name-lookup.h gives gt-cp-name-lookup.h for cp/name-lookup.c ! */
1965 { DIR_PREFIX_REGEX "cp/name-lookup\\.h$",
1966 REG_EXTENDED, NULL_REGEX,
1967 "gt-cp-name-lookup.h", "cp/name-lookup.c", NULL_FRULACT },
1969 /* cp/parser.h gives gt-cp-parser.h for cp/parser.c ! */
1970 { DIR_PREFIX_REGEX "cp/parser\\.h$",
1971 REG_EXTENDED, NULL_REGEX,
1972 "gt-cp-parser.h", "cp/parser.c", NULL_FRULACT },
1974 /* objc/objc-act.h gives gt-objc-objc-act.h for objc/objc-act.c ! */
1975 { DIR_PREFIX_REGEX "objc/objc-act\\.h$",
1976 REG_EXTENDED, NULL_REGEX,
1977 "gt-objc-objc-act.h", "objc/objc-act.c", NULL_FRULACT },
1979 /* objc/objc-map.h gives gt-objc-objc-map.h for objc/objc-map.c ! */
1980 { DIR_PREFIX_REGEX "objc/objc-map\\.h$",
1981 REG_EXTENDED, NULL_REGEX,
1982 "gt-objc-objc-map.h", "objc/objc-map.c", NULL_FRULACT },
1984 /* General cases. For header *.h and source *.c files, we need
1985 * special actions to handle the language. */
1987 /* Source *.c files are using get_file_gtfilename to compute their
1988 output_name and get_file_basename to compute their for_name
1989 through the source_dot_c_frul action. */
1990 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.c$",
1991 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.c", source_dot_c_frul},
1992 /* Common header files get "gtype-desc.c" as their output_name,
1993 * while language specific header files are handled specially. So
1994 * we need the header_dot_h_frul action. */
1995 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.h$",
1996 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.h", header_dot_h_frul},
1998 { DIR_PREFIX_REGEX "([[:alnum:]_-]*)\\.in$",
1999 REG_EXTENDED, NULL_REGEX, "gt-$3.h", "$3.in", NULL_FRULACT},
2001 /* Mandatory null last entry signaling end of rules. */
2002 {NULL, 0, NULL_REGEX, NULL, NULL, NULL_FRULACT}
2005 /* Special file rules action for handling *.h header files. It gives
2006 "gtype-desc.c" for common headers and corresponding output
2007 files for language-specific header files. */
2008 static outf_p
2009 header_dot_h_frul (input_file* inpf, char**poutname,
2010 char**pforname ATTRIBUTE_UNUSED)
2012 const char *basename = 0;
2013 int lang_index = 0;
2014 DBGPRINTF ("inpf %p inpname %s outname %s forname %s",
2015 (void*) inpf, get_input_file_name (inpf),
2016 *poutname, *pforname);
2017 basename = get_file_basename (inpf);
2018 lang_index = get_prefix_langdir_index (basename);
2019 DBGPRINTF ("basename %s lang_index %d", basename, lang_index);
2021 if (lang_index >= 0)
2023 /* The header is language specific. Given output_name &
2024 for_name remains unchanged. The base_files array gives the
2025 outf_p. */
2026 DBGPRINTF ("header_dot_h found language specific @ %p '%s'",
2027 (void*) base_files[lang_index],
2028 (base_files[lang_index])->name);
2029 return base_files[lang_index];
2031 else
2033 /* The header is common to all front-end languages. So
2034 output_name is "gtype-desc.c" file. The calling function
2035 get_output_file_with_visibility will find its outf_p. */
2036 free (*poutname);
2037 *poutname = xstrdup ("gtype-desc.c");
2038 DBGPRINTF ("special 'gtype-desc.c' for inpname %s",
2039 get_input_file_name (inpf));
2040 return NULL;
2045 /* Special file rules action for handling *.c source files using
2046 * get_file_gtfilename to compute their output_name and
2047 * get_file_basename to compute their for_name. The output_name is
2048 * gt-<LANG>-<BASE>.h for language specific source files, and
2049 * gt-<BASE>.h for common source files. */
2050 static outf_p
2051 source_dot_c_frul (input_file* inpf, char**poutname, char**pforname)
2053 char *newbasename = CONST_CAST (char*, get_file_basename (inpf));
2054 char *newoutname = CONST_CAST (char*, get_file_gtfilename (inpf));
2055 DBGPRINTF ("inpf %p inpname %s original outname %s forname %s",
2056 (void*) inpf, get_input_file_name (inpf),
2057 *poutname, *pforname);
2058 DBGPRINTF ("newoutname %s", newoutname);
2059 DBGPRINTF ("newbasename %s", newbasename);
2060 free (*poutname);
2061 free (*pforname);
2062 *poutname = newoutname;
2063 *pforname = newbasename;
2064 return NULL;
2067 /* Utility function for get_output_file_with_visibility which returns
2068 * a malloc-ed substituted string using TRS on matching of the FILNAM
2069 * file name, using the PMATCH array. */
2070 static char*
2071 matching_file_name_substitute (const char *filnam, regmatch_t pmatch[10],
2072 const char *trs)
2074 struct obstack str_obstack;
2075 char *str = NULL;
2076 char *rawstr = NULL;
2077 const char *pt = NULL;
2078 DBGPRINTF ("filnam %s", filnam);
2079 obstack_init (&str_obstack);
2080 for (pt = trs; *pt; pt++) {
2081 char c = *pt;
2082 if (c == '$')
2084 if (pt[1] == '$')
2086 /* A double dollar $$ is substituted by a single verbatim
2087 dollar, but who really uses dollar signs in file
2088 paths? */
2089 obstack_1grow (&str_obstack, '$');
2091 else if (ISDIGIT (pt[1]))
2093 /* Handle $0 $1 ... $9 by appropriate substitution. */
2094 int dolnum = pt[1] - '0';
2095 int so = pmatch[dolnum].rm_so;
2096 int eo = pmatch[dolnum].rm_eo;
2097 DBGPRINTF ("so=%d eo=%d dolnum=%d", so, eo, dolnum);
2098 if (so>=0 && eo>=so)
2099 obstack_grow (&str_obstack, filnam + so, eo - so);
2101 else
2103 /* This can happen only when files_rules is buggy! */
2104 gcc_unreachable();
2106 /* Always skip the character after the dollar. */
2107 pt++;
2109 else
2110 obstack_1grow (&str_obstack, c);
2112 obstack_1grow (&str_obstack, '\0');
2113 rawstr = XOBFINISH (&str_obstack, char *);
2114 str = xstrdup (rawstr);
2115 obstack_free (&str_obstack, NULL);
2116 DBGPRINTF ("matched replacement %s", str);
2117 rawstr = NULL;
2118 return str;
2122 /* An output file, suitable for definitions, that can see declarations
2123 made in INPF and is linked into every language that uses INPF.
2124 Since the result is cached inside INPF, that argument cannot be
2125 declared constant, but is "almost" constant. */
2127 outf_p
2128 get_output_file_with_visibility (input_file *inpf)
2130 outf_p r;
2131 char *for_name = NULL;
2132 char *output_name = NULL;
2133 const char* inpfname;
2135 /* This can happen when we need a file with visibility on a
2136 structure that we've never seen. We have to just hope that it's
2137 globally visible. */
2138 if (inpf == NULL)
2139 inpf = system_h_file;
2141 /* The result is cached in INPF, so return it if already known. */
2142 if (inpf->inpoutf)
2143 return inpf->inpoutf;
2145 /* In plugin mode, return NULL unless the input_file is one of the
2146 plugin_files. */
2147 if (plugin_files)
2149 size_t i;
2150 for (i = 0; i < nb_plugin_files; i++)
2151 if (inpf == plugin_files[i])
2153 inpf->inpoutf = plugin_output;
2154 return plugin_output;
2157 return NULL;
2160 inpfname = get_input_file_name (inpf);
2162 /* Try each rule in sequence in files_rules until one is triggered. */
2164 int rulix = 0;
2165 DBGPRINTF ("passing input file @ %p named %s through the files_rules",
2166 (void*) inpf, inpfname);
2168 for (; files_rules[rulix].frul_srcexpr != NULL; rulix++)
2170 DBGPRINTF ("rulix#%d srcexpr %s",
2171 rulix, files_rules[rulix].frul_srcexpr);
2173 if (!files_rules[rulix].frul_re)
2175 /* Compile the regexpr lazily. */
2176 int err = 0;
2177 files_rules[rulix].frul_re = XCNEW (regex_t);
2178 err = regcomp (files_rules[rulix].frul_re,
2179 files_rules[rulix].frul_srcexpr,
2180 files_rules[rulix].frul_rflags);
2181 if (err)
2183 /* The regular expression compilation fails only when
2184 file_rules is buggy. */
2185 gcc_unreachable ();
2189 output_name = NULL;
2190 for_name = NULL;
2192 /* Match the regexpr and trigger the rule if matched. */
2194 /* We have exactly ten pmatch-s, one for each $0, $1, $2,
2195 $3, ... $9. */
2196 regmatch_t pmatch[10];
2197 memset (pmatch, 0, sizeof (pmatch));
2198 if (!regexec (files_rules[rulix].frul_re,
2199 inpfname, 10, pmatch, 0))
2201 DBGPRINTF ("input @ %p filename %s matched rulix#%d pattern %s",
2202 (void*) inpf, inpfname, rulix,
2203 files_rules[rulix].frul_srcexpr);
2204 for_name =
2205 matching_file_name_substitute (inpfname, pmatch,
2206 files_rules[rulix].frul_tr_for);
2207 DBGPRINTF ("for_name %s", for_name);
2208 output_name =
2209 matching_file_name_substitute (inpfname, pmatch,
2210 files_rules[rulix].frul_tr_out);
2211 DBGPRINTF ("output_name %s", output_name);
2212 if (files_rules[rulix].frul_action)
2214 /* Invoke our action routine. */
2215 outf_p of = NULL;
2216 DBGPRINTF ("before action rulix#%d output_name %s for_name %s",
2217 rulix, output_name, for_name);
2218 of =
2219 (files_rules[rulix].frul_action) (inpf,
2220 &output_name, &for_name);
2221 DBGPRINTF ("after action rulix#%d of=%p output_name %s for_name %s",
2222 rulix, (void*)of, output_name, for_name);
2223 /* If the action routine returned something, give it back
2224 immediately and cache it in inpf. */
2225 if (of)
2227 inpf->inpoutf = of;
2228 return of;
2231 /* The rule matched, and had no action, or that action did
2232 not return any output file but could have changed the
2233 output_name or for_name. We break out of the loop on the
2234 files_rules. */
2235 break;
2237 else
2239 /* The regexpr did not match. */
2240 DBGPRINTF ("rulix#%d did not match %s pattern %s",
2241 rulix, inpfname, files_rules[rulix].frul_srcexpr);
2242 continue;
2247 if (!output_name || !for_name)
2249 /* This is impossible, and could only happen if the files_rules is
2250 incomplete or buggy. */
2251 gcc_unreachable ();
2254 /* Look through to see if we've ever seen this output filename
2255 before. If found, cache the result in inpf. */
2256 for (r = output_files; r; r = r->next)
2257 if (filename_cmp (r->name, output_name) == 0)
2259 inpf->inpoutf = r;
2260 DBGPRINTF ("found r @ %p for output_name %s for_name %s", (void*)r,
2261 output_name, for_name);
2262 return r;
2265 /* If not found, create it, and cache it in inpf. */
2266 r = create_file (for_name, output_name);
2268 gcc_assert (r && r->name);
2269 DBGPRINTF ("created r @ %p for output_name %s for_name %s", (void*) r,
2270 output_name, for_name);
2271 inpf->inpoutf = r;
2272 return r;
2277 /* The name of an output file, suitable for definitions, that can see
2278 declarations made in INPF and is linked into every language that
2279 uses INPF. */
2281 const char *
2282 get_output_file_name (input_file* inpf)
2284 outf_p o = get_output_file_with_visibility (inpf);
2285 if (o)
2286 return o->name;
2287 return NULL;
2290 /* Check if existing file is equal to the in memory buffer. */
2292 static bool
2293 is_file_equal (outf_p of)
2295 FILE *newfile = fopen (of->name, "r");
2296 size_t i;
2297 bool equal;
2298 if (newfile == NULL)
2299 return false;
2301 equal = true;
2302 for (i = 0; i < of->bufused; i++)
2304 int ch;
2305 ch = fgetc (newfile);
2306 if (ch == EOF || ch != (unsigned char) of->buf[i])
2308 equal = false;
2309 break;
2312 fclose (newfile);
2313 return equal;
2316 /* Copy the output to its final destination,
2317 but don't unnecessarily change modification times. */
2319 static void
2320 close_output_files (void)
2322 int nbwrittenfiles = 0;
2323 outf_p of;
2325 for (of = output_files; of; of = of->next)
2327 if (!is_file_equal (of))
2329 FILE *newfile = NULL;
2330 char *backupname = NULL;
2331 /* Back up the old version of the output file gt-FOO.c as
2332 BACKUPDIR/gt-FOO.c~ if we have a backup directory. */
2333 if (backup_dir)
2335 backupname = concat (backup_dir, "/",
2336 lbasename (of->name), "~", NULL);
2337 if (!access (of->name, F_OK) && rename (of->name, backupname))
2338 fatal ("failed to back up %s as %s: %s",
2339 of->name, backupname, xstrerror (errno));
2342 newfile = fopen (of->name, "w");
2343 if (newfile == NULL)
2344 fatal ("opening output file %s: %s", of->name, xstrerror (errno));
2345 if (fwrite (of->buf, 1, of->bufused, newfile) != of->bufused)
2346 fatal ("writing output file %s: %s", of->name, xstrerror (errno));
2347 if (fclose (newfile) != 0)
2348 fatal ("closing output file %s: %s", of->name, xstrerror (errno));
2349 nbwrittenfiles++;
2350 if (verbosity_level >= 2 && backupname)
2351 printf ("%s wrote #%-3d %s backed-up in %s\n",
2352 progname, nbwrittenfiles, of->name, backupname);
2353 else if (verbosity_level >= 1)
2354 printf ("%s write #%-3d %s\n", progname, nbwrittenfiles, of->name);
2355 free (backupname);
2357 else
2359 /* output file remains unchanged. */
2360 if (verbosity_level >= 2)
2361 printf ("%s keep %s\n", progname, of->name);
2363 free (of->buf);
2364 of->buf = NULL;
2365 of->bufused = of->buflength = 0;
2367 if (verbosity_level >= 1)
2368 printf ("%s wrote %d files.\n", progname, nbwrittenfiles);
2371 struct flist
2373 struct flist *next;
2374 int started_p;
2375 const input_file* file;
2376 outf_p f;
2379 struct walk_type_data;
2381 /* For scalars and strings, given the item in 'val'.
2382 For structures, given a pointer to the item in 'val'.
2383 For misc. pointers, given the item in 'val'.
2385 typedef void (*process_field_fn) (type_p f, const struct walk_type_data * p);
2386 typedef void (*func_name_fn) (type_p s, const struct walk_type_data * p);
2388 /* Parameters for write_types. */
2390 struct write_types_data
2392 const char *prefix;
2393 const char *param_prefix;
2394 const char *subfield_marker_routine;
2395 const char *marker_routine;
2396 const char *reorder_note_routine;
2397 const char *comment;
2398 int skip_hooks; /* skip hook generation if non zero */
2401 static void output_escaped_param (struct walk_type_data *d,
2402 const char *, const char *);
2403 static void output_mangled_typename (outf_p, const_type_p);
2404 static void walk_type (type_p t, struct walk_type_data *d);
2405 static void write_func_for_structure (type_p orig_s, type_p s, type_p *param,
2406 const struct write_types_data *wtd);
2407 static void write_types_process_field
2408 (type_p f, const struct walk_type_data *d);
2409 static void write_types (outf_p output_header,
2410 type_p structures,
2411 type_p param_structs,
2412 const struct write_types_data *wtd);
2413 static void write_types_local_process_field
2414 (type_p f, const struct walk_type_data *d);
2415 static void write_local_func_for_structure
2416 (const_type_p orig_s, type_p s, type_p *param);
2417 static void write_local (outf_p output_header,
2418 type_p structures, type_p param_structs);
2419 static int contains_scalar_p (type_p t);
2420 static void put_mangled_filename (outf_p, const input_file *);
2421 static void finish_root_table (struct flist *flp, const char *pfx,
2422 const char *tname, const char *lastname,
2423 const char *name);
2424 static void write_root (outf_p, pair_p, type_p, const char *, int,
2425 struct fileloc *, const char *, bool);
2426 static void write_array (outf_p f, pair_p v,
2427 const struct write_types_data *wtd);
2428 static void write_roots (pair_p, bool);
2430 /* Parameters for walk_type. */
2432 struct walk_type_data
2434 process_field_fn process_field;
2435 const void *cookie;
2436 outf_p of;
2437 options_p opt;
2438 const char *val;
2439 const char *prev_val[4];
2440 int indent;
2441 int counter;
2442 const struct fileloc *line;
2443 lang_bitmap bitmap;
2444 type_p *param;
2445 int used_length;
2446 type_p orig_s;
2447 const char *reorder_fn;
2448 bool needs_cast_p;
2449 bool fn_wants_lvalue;
2450 bool in_record_p;
2451 int loopcounter;
2452 bool in_ptr_field;
2453 bool have_this_obj;
2457 /* Given a string TYPE_NAME, representing a C++ typename, return a valid
2458 pre-processor identifier to use in a #define directive. This replaces
2459 special characters used in C++ identifiers like '>', '<' and ':' with
2460 '_'.
2462 If no C++ special characters are found in TYPE_NAME, return
2463 TYPE_NAME. Otherwise, return a copy of TYPE_NAME with the special
2464 characters replaced with '_'. In this case, the caller is
2465 responsible for freeing the allocated string. */
2467 static const char *
2468 filter_type_name (const char *type_name)
2470 if (strchr (type_name, '<') || strchr (type_name, ':'))
2472 size_t i;
2473 char *s = xstrdup (type_name);
2474 for (i = 0; i < strlen (s); i++)
2475 if (s[i] == '<' || s[i] == '>' || s[i] == ':' || s[i] == ',')
2476 s[i] = '_';
2477 return s;
2479 else
2480 return type_name;
2484 /* Print a mangled name representing T to OF. */
2486 static void
2487 output_mangled_typename (outf_p of, const_type_p t)
2489 if (t == NULL)
2490 oprintf (of, "Z");
2491 else
2492 switch (t->kind)
2494 case TYPE_NONE:
2495 case TYPE_UNDEFINED:
2496 gcc_unreachable ();
2497 break;
2498 case TYPE_POINTER:
2499 oprintf (of, "P");
2500 output_mangled_typename (of, t->u.p);
2501 break;
2502 case TYPE_SCALAR:
2503 oprintf (of, "I");
2504 break;
2505 case TYPE_STRING:
2506 oprintf (of, "S");
2507 break;
2508 case TYPE_STRUCT:
2509 case TYPE_UNION:
2510 case TYPE_LANG_STRUCT:
2511 case TYPE_USER_STRUCT:
2513 const char *id_for_tag = filter_type_name (t->u.s.tag);
2514 oprintf (of, "%lu%s", (unsigned long) strlen (id_for_tag),
2515 id_for_tag);
2516 if (id_for_tag != t->u.s.tag)
2517 free (CONST_CAST(char *, id_for_tag));
2519 break;
2520 case TYPE_PARAM_STRUCT:
2522 int i;
2523 for (i = 0; i < NUM_PARAM; i++)
2524 if (t->u.param_struct.param[i] != NULL)
2525 output_mangled_typename (of, t->u.param_struct.param[i]);
2526 output_mangled_typename (of, t->u.param_struct.stru);
2528 break;
2529 case TYPE_ARRAY:
2530 gcc_unreachable ();
2534 /* Print PARAM to D->OF processing escapes. D->VAL references the
2535 current object, D->PREV_VAL the object containing the current
2536 object, ONAME is the name of the option and D->LINE is used to
2537 print error messages. */
2539 static void
2540 output_escaped_param (struct walk_type_data *d, const char *param,
2541 const char *oname)
2543 const char *p;
2545 for (p = param; *p; p++)
2546 if (*p != '%')
2547 oprintf (d->of, "%c", *p);
2548 else
2549 switch (*++p)
2551 case 'h':
2552 oprintf (d->of, "(%s)", d->prev_val[2]);
2553 break;
2554 case '0':
2555 oprintf (d->of, "(%s)", d->prev_val[0]);
2556 break;
2557 case '1':
2558 oprintf (d->of, "(%s)", d->prev_val[1]);
2559 break;
2560 case 'a':
2562 const char *pp = d->val + strlen (d->val);
2563 while (pp[-1] == ']')
2564 while (*pp != '[')
2565 pp--;
2566 oprintf (d->of, "%s", pp);
2568 break;
2569 default:
2570 error_at_line (d->line, "`%s' option contains bad escape %c%c",
2571 oname, '%', *p);
2576 /* Call D->PROCESS_FIELD for every field (or subfield) of D->VAL,
2577 which is of type T. Write code to D->OF to constrain execution (at
2578 the point that D->PROCESS_FIELD is called) to the appropriate
2579 cases. Call D->PROCESS_FIELD on subobjects before calling it on
2580 pointers to those objects. D->PREV_VAL lists the objects
2581 containing the current object, D->OPT is a list of options to
2582 apply, D->INDENT is the current indentation level, D->LINE is used
2583 to print error messages, D->BITMAP indicates which languages to
2584 print the structure for, and D->PARAM is the current parameter
2585 (from an enclosing param_is option). */
2587 static void
2588 walk_type (type_p t, struct walk_type_data *d)
2590 const char *length = NULL;
2591 const char *desc = NULL;
2592 int maybe_undef_p = 0;
2593 int use_param_num = -1;
2594 int use_params_p = 0;
2595 int atomic_p = 0;
2596 options_p oo;
2597 const struct nested_ptr_data *nested_ptr_d = NULL;
2599 d->needs_cast_p = false;
2600 for (oo = d->opt; oo; oo = oo->next)
2601 if (strcmp (oo->name, "length") == 0 && oo->kind == OPTION_STRING)
2602 length = oo->info.string;
2603 else if (strcmp (oo->name, "maybe_undef") == 0)
2604 maybe_undef_p = 1;
2605 else if (strncmp (oo->name, "use_param", 9) == 0
2606 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
2607 use_param_num = oo->name[9] == '\0' ? 0 : oo->name[9] - '0';
2608 else if (strcmp (oo->name, "use_params") == 0)
2609 use_params_p = 1;
2610 else if (strcmp (oo->name, "desc") == 0 && oo->kind == OPTION_STRING)
2611 desc = oo->info.string;
2612 else if (strcmp (oo->name, "mark_hook") == 0)
2614 else if (strcmp (oo->name, "nested_ptr") == 0
2615 && oo->kind == OPTION_NESTED)
2616 nested_ptr_d = (const struct nested_ptr_data *) oo->info.nested;
2617 else if (strcmp (oo->name, "dot") == 0)
2619 else if (strcmp (oo->name, "tag") == 0)
2621 else if (strcmp (oo->name, "special") == 0)
2623 else if (strcmp (oo->name, "skip") == 0)
2625 else if (strcmp (oo->name, "atomic") == 0)
2626 atomic_p = 1;
2627 else if (strcmp (oo->name, "default") == 0)
2629 else if (strcmp (oo->name, "param_is") == 0)
2631 else if (strncmp (oo->name, "param", 5) == 0
2632 && ISDIGIT (oo->name[5]) && strcmp (oo->name + 6, "_is") == 0)
2634 else if (strcmp (oo->name, "chain_next") == 0)
2636 else if (strcmp (oo->name, "chain_prev") == 0)
2638 else if (strcmp (oo->name, "chain_circular") == 0)
2640 else if (strcmp (oo->name, "reorder") == 0)
2642 else if (strcmp (oo->name, "variable_size") == 0)
2644 else
2645 error_at_line (d->line, "unknown option `%s'\n", oo->name);
2647 if (d->used_length)
2648 length = NULL;
2650 if (use_params_p)
2652 int pointer_p = t->kind == TYPE_POINTER;
2654 if (pointer_p)
2655 t = t->u.p;
2656 if (!union_or_struct_p (t))
2657 error_at_line (d->line, "`use_params' option on unimplemented type");
2658 else
2659 t = find_param_structure (t, d->param);
2660 if (pointer_p)
2661 t = create_pointer (t);
2664 if (use_param_num != -1)
2666 if (d->param != NULL && d->param[use_param_num] != NULL)
2668 type_p nt = d->param[use_param_num];
2670 if (t->kind == TYPE_ARRAY)
2671 nt = create_array (nt, t->u.a.len);
2672 else if (length != NULL && t->kind == TYPE_POINTER)
2673 nt = create_pointer (nt);
2674 d->needs_cast_p = (t->kind != TYPE_POINTER
2675 && (nt->kind == TYPE_POINTER
2676 || nt->kind == TYPE_STRING));
2677 t = nt;
2679 else
2680 error_at_line (d->line, "no parameter defined for `%s'", d->val);
2683 if (maybe_undef_p
2684 && (t->kind != TYPE_POINTER || !union_or_struct_p (t->u.p)))
2686 error_at_line (d->line,
2687 "field `%s' has invalid option `maybe_undef_p'\n",
2688 d->val);
2689 return;
2692 if (atomic_p && (t->kind != TYPE_POINTER) && (t->kind != TYPE_STRING))
2694 error_at_line (d->line, "field `%s' has invalid option `atomic'\n", d->val);
2695 return;
2698 switch (t->kind)
2700 case TYPE_SCALAR:
2701 case TYPE_STRING:
2702 d->process_field (t, d);
2703 break;
2705 case TYPE_POINTER:
2707 d->in_ptr_field = true;
2708 if (maybe_undef_p && t->u.p->u.s.line.file == NULL)
2710 oprintf (d->of, "%*sgcc_assert (!%s);\n", d->indent, "", d->val);
2711 break;
2714 /* If a pointer type is marked as "atomic", we process the
2715 field itself, but we don't walk the data that they point to.
2717 There are two main cases where we walk types: to mark
2718 pointers that are reachable, and to relocate pointers when
2719 writing a PCH file. In both cases, an atomic pointer is
2720 itself marked or relocated, but the memory that it points
2721 to is left untouched. In the case of PCH, that memory will
2722 be read/written unchanged to the PCH file. */
2723 if (atomic_p)
2725 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2726 d->indent += 2;
2727 d->process_field (t, d);
2728 d->indent -= 2;
2729 oprintf (d->of, "%*s}\n", d->indent, "");
2730 break;
2733 if (!length)
2735 if (!union_or_struct_p (t->u.p)
2736 && t->u.p->kind != TYPE_PARAM_STRUCT)
2738 error_at_line (d->line,
2739 "field `%s' is pointer to unimplemented type",
2740 d->val);
2741 break;
2744 if (nested_ptr_d)
2746 const char *oldprevval2 = d->prev_val[2];
2748 if (!union_or_struct_p (nested_ptr_d->type))
2750 error_at_line (d->line,
2751 "field `%s' has invalid "
2752 "option `nested_ptr'\n", d->val);
2753 return;
2756 d->prev_val[2] = d->val;
2757 oprintf (d->of, "%*s{\n", d->indent, "");
2758 d->indent += 2;
2759 d->val = xasprintf ("x%d", d->counter++);
2760 oprintf (d->of, "%*s%s %s * %s%s =\n", d->indent, "",
2761 (nested_ptr_d->type->kind == TYPE_UNION
2762 ? "union" : "struct"),
2763 nested_ptr_d->type->u.s.tag,
2764 d->fn_wants_lvalue ? "" : "const ", d->val);
2765 oprintf (d->of, "%*s", d->indent + 2, "");
2766 output_escaped_param (d, nested_ptr_d->convert_from,
2767 "nested_ptr");
2768 oprintf (d->of, ";\n");
2770 d->process_field (nested_ptr_d->type, d);
2772 if (d->fn_wants_lvalue)
2774 oprintf (d->of, "%*s%s = ", d->indent, "",
2775 d->prev_val[2]);
2776 d->prev_val[2] = d->val;
2777 output_escaped_param (d, nested_ptr_d->convert_to,
2778 "nested_ptr");
2779 oprintf (d->of, ";\n");
2782 d->indent -= 2;
2783 oprintf (d->of, "%*s}\n", d->indent, "");
2784 d->val = d->prev_val[2];
2785 d->prev_val[2] = oldprevval2;
2787 else
2788 d->process_field (t->u.p, d);
2790 else
2792 int loopcounter = d->loopcounter;
2793 const char *oldval = d->val;
2794 const char *oldprevval3 = d->prev_val[3];
2795 char *newval;
2797 oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
2798 d->indent += 2;
2799 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2800 oprintf (d->of, "%*sfor (i%d = 0; i%d != (size_t)(", d->indent,
2801 "", loopcounter, loopcounter);
2802 if (!d->in_record_p)
2803 output_escaped_param (d, length, "length");
2804 else
2805 oprintf (d->of, "l%d", loopcounter);
2806 if (d->have_this_obj)
2807 /* Try to unswitch loops (see PR53880). */
2808 oprintf (d->of, ") && ((void *)%s == this_obj", oldval);
2809 oprintf (d->of, "); i%d++) {\n", loopcounter);
2810 d->indent += 2;
2811 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2812 d->used_length = 1;
2813 d->prev_val[3] = oldval;
2814 walk_type (t->u.p, d);
2815 free (newval);
2816 d->val = oldval;
2817 d->prev_val[3] = oldprevval3;
2818 d->used_length = 0;
2819 d->indent -= 2;
2820 oprintf (d->of, "%*s}\n", d->indent, "");
2821 d->process_field (t, d);
2822 d->indent -= 2;
2823 oprintf (d->of, "%*s}\n", d->indent, "");
2825 d->in_ptr_field = false;
2827 break;
2829 case TYPE_ARRAY:
2831 int loopcounter;
2832 const char *oldval = d->val;
2833 char *newval;
2835 /* If it's an array of scalars, we optimize by not generating
2836 any code. */
2837 if (t->u.a.p->kind == TYPE_SCALAR)
2838 break;
2840 if (length)
2841 loopcounter = d->loopcounter;
2842 else
2843 loopcounter = d->counter++;
2845 /* When walking an array, compute the length and store it in a
2846 local variable before walking the array elements, instead of
2847 recomputing the length expression each time through the loop.
2848 This is necessary to handle tcc_vl_exp objects like CALL_EXPR,
2849 where the length is stored in the first array element,
2850 because otherwise that operand can get overwritten on the
2851 first iteration. */
2852 oprintf (d->of, "%*s{\n", d->indent, "");
2853 d->indent += 2;
2854 oprintf (d->of, "%*ssize_t i%d;\n", d->indent, "", loopcounter);
2855 if (!d->in_record_p || !length)
2857 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2858 d->indent, "", loopcounter);
2859 if (length)
2860 output_escaped_param (d, length, "length");
2861 else
2862 oprintf (d->of, "%s", t->u.a.len);
2863 oprintf (d->of, ");\n");
2866 oprintf (d->of, "%*sfor (i%d = 0; i%d != l%d; i%d++) {\n",
2867 d->indent, "",
2868 loopcounter, loopcounter, loopcounter, loopcounter);
2869 d->indent += 2;
2870 d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
2871 d->used_length = 1;
2872 walk_type (t->u.a.p, d);
2873 free (newval);
2874 d->used_length = 0;
2875 d->val = oldval;
2876 d->indent -= 2;
2877 oprintf (d->of, "%*s}\n", d->indent, "");
2878 d->indent -= 2;
2879 oprintf (d->of, "%*s}\n", d->indent, "");
2881 break;
2883 case TYPE_STRUCT:
2884 case TYPE_UNION:
2886 pair_p f;
2887 const char *oldval = d->val;
2888 const char *oldprevval1 = d->prev_val[1];
2889 const char *oldprevval2 = d->prev_val[2];
2890 const char *struct_mark_hook = NULL;
2891 const int union_p = t->kind == TYPE_UNION;
2892 int seen_default_p = 0;
2893 options_p o;
2894 int lengths_seen = 0;
2895 int endcounter;
2896 bool any_length_seen = false;
2898 if (!t->u.s.line.file)
2899 error_at_line (d->line, "incomplete structure `%s'", t->u.s.tag);
2901 if ((d->bitmap & t->u.s.bitmap) != d->bitmap)
2903 error_at_line (d->line,
2904 "structure `%s' defined for mismatching languages",
2905 t->u.s.tag);
2906 error_at_line (&t->u.s.line, "one structure defined here");
2909 /* Some things may also be defined in the structure's options. */
2910 for (o = t->u.s.opt; o; o = o->next)
2911 if (!desc && strcmp (o->name, "desc") == 0
2912 && o->kind == OPTION_STRING)
2913 desc = o->info.string;
2914 else if (!struct_mark_hook && strcmp (o->name, "mark_hook") == 0
2915 && o->kind == OPTION_STRING)
2916 struct_mark_hook = o->info.string;
2918 if (struct_mark_hook)
2919 oprintf (d->of, "%*s%s (&%s);\n",
2920 d->indent, "", struct_mark_hook, oldval);
2922 d->prev_val[2] = oldval;
2923 d->prev_val[1] = oldprevval2;
2924 if (union_p)
2926 if (desc == NULL)
2928 error_at_line (d->line,
2929 "missing `desc' option for union `%s'",
2930 t->u.s.tag);
2931 desc = "1";
2933 oprintf (d->of, "%*sswitch (", d->indent, "");
2934 output_escaped_param (d, desc, "desc");
2935 oprintf (d->of, ")\n");
2936 d->indent += 2;
2937 oprintf (d->of, "%*s{\n", d->indent, "");
2940 for (f = t->u.s.fields; f; f = f->next)
2942 options_p oo;
2943 int skip_p = 0;
2944 const char *fieldlength = NULL;
2946 d->reorder_fn = NULL;
2947 for (oo = f->opt; oo; oo = oo->next)
2948 if (strcmp (oo->name, "skip") == 0)
2949 skip_p = 1;
2950 else if (strcmp (oo->name, "length") == 0
2951 && oo->kind == OPTION_STRING)
2952 fieldlength = oo->info.string;
2954 if (skip_p)
2955 continue;
2956 if (fieldlength)
2958 lengths_seen++;
2959 d->counter++;
2960 if (!union_p)
2962 if (!any_length_seen)
2964 oprintf (d->of, "%*s{\n", d->indent, "");
2965 d->indent += 2;
2967 any_length_seen = true;
2969 oprintf (d->of, "%*ssize_t l%d = (size_t)(",
2970 d->indent, "", d->counter - 1);
2971 output_escaped_param (d, fieldlength, "length");
2972 oprintf (d->of, ");\n");
2976 endcounter = d->counter;
2978 for (f = t->u.s.fields; f; f = f->next)
2980 options_p oo;
2981 const char *dot = ".";
2982 const char *tagid = NULL;
2983 int skip_p = 0;
2984 int default_p = 0;
2985 int use_param_p = 0;
2986 const char *fieldlength = NULL;
2987 char *newval;
2989 d->reorder_fn = NULL;
2990 for (oo = f->opt; oo; oo = oo->next)
2991 if (strcmp (oo->name, "dot") == 0
2992 && oo->kind == OPTION_STRING)
2993 dot = oo->info.string;
2994 else if (strcmp (oo->name, "tag") == 0
2995 && oo->kind == OPTION_STRING)
2996 tagid = oo->info.string;
2997 else if (strcmp (oo->name, "skip") == 0)
2998 skip_p = 1;
2999 else if (strcmp (oo->name, "default") == 0)
3000 default_p = 1;
3001 else if (strcmp (oo->name, "reorder") == 0
3002 && oo->kind == OPTION_STRING)
3003 d->reorder_fn = oo->info.string;
3004 else if (strncmp (oo->name, "use_param", 9) == 0
3005 && (oo->name[9] == '\0' || ISDIGIT (oo->name[9])))
3006 use_param_p = 1;
3007 else if (strcmp (oo->name, "length") == 0
3008 && oo->kind == OPTION_STRING)
3009 fieldlength = oo->info.string;
3011 if (skip_p)
3012 continue;
3014 if (union_p && tagid)
3016 oprintf (d->of, "%*scase %s:\n", d->indent, "", tagid);
3017 d->indent += 2;
3019 else if (union_p && default_p)
3021 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3022 d->indent += 2;
3023 seen_default_p = 1;
3025 else if (!union_p && (default_p || tagid))
3026 error_at_line (d->line,
3027 "can't use `%s' outside a union on field `%s'",
3028 default_p ? "default" : "tag", f->name);
3029 else if (union_p && !(default_p || tagid)
3030 && f->type->kind == TYPE_SCALAR)
3032 fprintf (stderr,
3033 "%s:%d: warning: field `%s' is missing `tag' or `default' option\n",
3034 get_input_file_name (d->line->file), d->line->line,
3035 f->name);
3036 continue;
3038 else if (union_p && !(default_p || tagid))
3039 error_at_line (d->line,
3040 "field `%s' is missing `tag' or `default' option",
3041 f->name);
3043 if (fieldlength)
3045 d->loopcounter = endcounter - lengths_seen--;
3048 d->line = &f->line;
3049 d->val = newval = xasprintf ("%s%s%s", oldval, dot, f->name);
3050 d->opt = f->opt;
3051 d->used_length = false;
3052 d->in_record_p = !union_p;
3054 if (union_p && use_param_p && d->param == NULL)
3055 oprintf (d->of, "%*sgcc_unreachable ();\n", d->indent, "");
3056 else
3057 walk_type (f->type, d);
3059 d->in_record_p = false;
3061 free (newval);
3063 if (union_p)
3065 oprintf (d->of, "%*sbreak;\n", d->indent, "");
3066 d->indent -= 2;
3069 d->reorder_fn = NULL;
3071 d->val = oldval;
3072 d->prev_val[1] = oldprevval1;
3073 d->prev_val[2] = oldprevval2;
3075 if (union_p && !seen_default_p)
3077 oprintf (d->of, "%*sdefault:\n", d->indent, "");
3078 oprintf (d->of, "%*s break;\n", d->indent, "");
3080 if (union_p)
3082 oprintf (d->of, "%*s}\n", d->indent, "");
3083 d->indent -= 2;
3085 if (any_length_seen)
3087 d->indent -= 2;
3088 oprintf (d->of, "%*s}\n", d->indent, "");
3091 break;
3093 case TYPE_LANG_STRUCT:
3095 type_p nt;
3096 for (nt = t->u.s.lang_struct; nt; nt = nt->next)
3097 if ((d->bitmap & nt->u.s.bitmap) == d->bitmap)
3098 break;
3099 if (nt == NULL)
3100 error_at_line (d->line, "structure `%s' differs between languages",
3101 t->u.s.tag);
3102 else
3103 walk_type (nt, d);
3105 break;
3107 case TYPE_PARAM_STRUCT:
3109 type_p *oldparam = d->param;
3111 d->param = t->u.param_struct.param;
3112 walk_type (t->u.param_struct.stru, d);
3113 d->param = oldparam;
3115 break;
3117 case TYPE_USER_STRUCT:
3118 d->process_field (t, d);
3119 break;
3121 case TYPE_NONE:
3122 case TYPE_UNDEFINED:
3123 gcc_unreachable ();
3127 /* process_field routine for marking routines. */
3129 static void
3130 write_types_process_field (type_p f, const struct walk_type_data *d)
3132 const struct write_types_data *wtd;
3133 const char *cast = d->needs_cast_p ? "(void *)" : "";
3134 wtd = (const struct write_types_data *) d->cookie;
3136 switch (f->kind)
3138 case TYPE_NONE:
3139 case TYPE_UNDEFINED:
3140 gcc_unreachable ();
3141 case TYPE_POINTER:
3142 oprintf (d->of, "%*s%s (%s%s", d->indent, "",
3143 wtd->subfield_marker_routine, cast, d->val);
3144 if (wtd->param_prefix)
3146 if (f->u.p->kind == TYPE_SCALAR)
3147 /* The current type is a pointer to a scalar (so not
3148 considered like a pointer to instances of user defined
3149 types) and we are seeing it; it means we must be even
3150 more careful about the second argument of the
3151 SUBFIELD_MARKER_ROUTINE call. That argument must
3152 always be the instance of the type for which
3153 write_func_for_structure was called - this really is
3154 what the function SUBFIELD_MARKER_ROUTINE expects.
3155 That is, it must be an instance of the ORIG_S type
3156 parameter of write_func_for_structure. The convention
3157 is that that argument must be "x" in that case (as set
3158 by write_func_for_structure). The problem is, we can't
3159 count on d->prev_val[3] to be always set to "x" in that
3160 case. Sometimes walk_type can set it to something else
3161 (to e.g cooperate with write_array when called from
3162 write_roots). So let's set it to "x" here then. */
3163 oprintf (d->of, ", x");
3164 else
3165 oprintf (d->of, ", %s", d->prev_val[3]);
3166 if (d->orig_s)
3168 oprintf (d->of, ", gt_%s_", wtd->param_prefix);
3169 output_mangled_typename (d->of, d->orig_s);
3171 else
3172 oprintf (d->of, ", gt_%sa_%s", wtd->param_prefix, d->prev_val[0]);
3174 oprintf (d->of, ");\n");
3175 if (d->reorder_fn && wtd->reorder_note_routine)
3176 oprintf (d->of, "%*s%s (%s%s, %s, %s);\n", d->indent, "",
3177 wtd->reorder_note_routine, cast, d->val,
3178 d->prev_val[3], d->reorder_fn);
3179 break;
3181 case TYPE_STRING:
3182 case TYPE_STRUCT:
3183 case TYPE_UNION:
3184 case TYPE_LANG_STRUCT:
3185 case TYPE_PARAM_STRUCT:
3186 case TYPE_USER_STRUCT:
3187 if (f->kind == TYPE_USER_STRUCT && !d->in_ptr_field)
3189 /* If F is a user-defined type and the field is not a
3190 pointer to the type, then we should not generate the
3191 standard pointer-marking code. All we need to do is call
3192 the user-provided marking function to process the fields
3193 of F. */
3194 oprintf (d->of, "%*sgt_%sx (&(%s));\n", d->indent, "", wtd->prefix,
3195 d->val);
3197 else
3199 oprintf (d->of, "%*sgt_%s_", d->indent, "", wtd->prefix);
3200 output_mangled_typename (d->of, f);
3201 oprintf (d->of, " (%s%s);\n", cast, d->val);
3202 if (d->reorder_fn && wtd->reorder_note_routine)
3203 oprintf (d->of, "%*s%s (%s%s, %s%s, %s);\n", d->indent, "",
3204 wtd->reorder_note_routine, cast, d->val, cast, d->val,
3205 d->reorder_fn);
3207 break;
3209 case TYPE_SCALAR:
3210 break;
3212 case TYPE_ARRAY:
3213 gcc_unreachable ();
3217 /* Return an output file that is suitable for definitions which can
3218 reference struct S */
3220 static outf_p
3221 get_output_file_for_structure (const_type_p s, type_p *param)
3223 const input_file *fn;
3224 int i;
3226 gcc_assert (union_or_struct_p (s));
3227 fn = s->u.s.line.file;
3229 /* This is a hack, and not the good kind either. */
3230 for (i = NUM_PARAM - 1; i >= 0; i--)
3231 if (param && param[i] && param[i]->kind == TYPE_POINTER
3232 && union_or_struct_p (param[i]->u.p))
3233 fn = param[i]->u.p->u.s.line.file;
3235 /* The call to get_output_file_with_visibility may update fn by
3236 caching its result inside, so we need the CONST_CAST. */
3237 return get_output_file_with_visibility (CONST_CAST (input_file*, fn));
3241 /* Returns the specifier keyword for a string or union type S, empty string
3242 otherwise. */
3244 static const char *
3245 get_type_specifier (const type_p s)
3247 if (s->kind == TYPE_STRUCT)
3248 return "struct ";
3249 else if (s->kind == TYPE_LANG_STRUCT)
3250 return get_type_specifier (s->u.s.lang_struct);
3251 else if (s->kind == TYPE_UNION)
3252 return "union ";
3253 return "";
3257 /* Emits a declaration for type TY (assumed to be a union or a
3258 structure) on stream OUT. */
3260 static void
3261 write_type_decl (outf_p out, type_p ty)
3263 if (union_or_struct_p (ty))
3264 oprintf (out, "%s%s", get_type_specifier (ty), ty->u.s.tag);
3265 else if (ty->kind == TYPE_SCALAR)
3267 if (ty->u.scalar_is_char)
3268 oprintf (out, "const char");
3269 else
3270 oprintf (out, "void");
3272 else if (ty->kind == TYPE_POINTER)
3274 write_type_decl (out, ty->u.p);
3275 oprintf (out, " *");
3277 else if (ty->kind == TYPE_ARRAY)
3279 write_type_decl (out, ty->u.a.p);
3280 oprintf (out, " *");
3282 else if (ty->kind == TYPE_STRING)
3284 oprintf (out, "const char *");
3286 else
3287 gcc_unreachable ();
3291 /* Write on OF the name of the marker function for structure S. PREFIX
3292 is the prefix to use (to distinguish ggc from pch markers). */
3294 static void
3295 write_marker_function_name (outf_p of, type_p s, const char *prefix)
3297 if (union_or_struct_p (s))
3299 const char *id_for_tag = filter_type_name (s->u.s.tag);
3300 oprintf (of, "gt_%sx_%s", prefix, id_for_tag);
3301 if (id_for_tag != s->u.s.tag)
3302 free (CONST_CAST(char *, id_for_tag));
3304 else if (s->kind == TYPE_PARAM_STRUCT)
3306 oprintf (of, "gt_%s_", prefix);
3307 output_mangled_typename (of, s);
3309 else
3310 gcc_unreachable ();
3313 /* Write on OF a user-callable routine to act as an entry point for
3314 the marking routine for S, generated by write_func_for_structure.
3315 PREFIX is the prefix to use to distinguish ggc and pch markers. */
3317 static void
3318 write_user_func_for_structure_ptr (outf_p of, type_p s, const char *prefix)
3320 /* Parameterized structures are not supported in user markers. There
3321 is no way for the marker function to know which specific type
3322 to use to generate the call to the void * entry point. For
3323 instance, a marker for struct htab may need to call different
3324 routines to mark the fields, depending on the paramN_is attributes.
3326 A user-defined marker that accepts 'struct htab' as its argument
3327 would not know which variant to call. Generating several entry
3328 points accepting 'struct htab' would cause multiply-defined
3329 errors during compilation. */
3330 gcc_assert (union_or_struct_p (s));
3332 type_p alias_of = NULL;
3333 for (options_p opt = s->u.s.opt; opt; opt = opt->next)
3334 if (strcmp (opt->name, "ptr_alias") == 0)
3336 /* ALIAS_OF is set if ORIG_S is marked "ptr_alias". This means that
3337 we do not generate marking code for ORIG_S here. Instead, a
3338 forwarder #define in gtype-desc.h will cause every call to its
3339 marker to call the target of this alias.
3341 However, we still want to create a user entry code for the
3342 aliased type. So, if ALIAS_OF is set, we only generate the
3343 user-callable marker function. */
3344 alias_of = opt->info.type;
3345 break;
3348 oprintf (of, "\nvoid\n");
3349 oprintf (of, "gt_%sx (", prefix);
3350 write_type_decl (of, s);
3351 oprintf (of, " *& x)\n");
3352 oprintf (of, "{\n");
3353 oprintf (of, " if (x)\n ");
3354 write_marker_function_name (of, alias_of ? alias_of : s, prefix);
3355 oprintf (of, " ((void *) x);\n");
3356 oprintf (of, "}\n");
3360 /* Write a function to mark all the fields of type S on OF. PREFIX
3361 and D are as in write_user_marking_functions. */
3363 static void
3364 write_user_func_for_structure_body (type_p s, const char *prefix,
3365 struct walk_type_data *d)
3367 oprintf (d->of, "\nvoid\n");
3368 oprintf (d->of, "gt_%sx (", prefix);
3369 write_type_decl (d->of, s);
3370 oprintf (d->of, "& x_r ATTRIBUTE_UNUSED)\n");
3371 oprintf (d->of, "{\n");
3372 oprintf (d->of, " ");
3373 write_type_decl (d->of, s);
3374 oprintf (d->of, " * ATTRIBUTE_UNUSED x = &x_r;\n");
3375 d->val = "(*x)";
3376 d->indent = 2;
3377 walk_type (s, d);
3378 oprintf (d->of, "}\n");
3382 /* Emit the user-callable functions needed to mark all the types used
3383 by the user structure S. PREFIX is the prefix to use to
3384 distinguish ggc and pch markers. D contains data needed to pass to
3385 walk_type when traversing the fields of a type.
3387 For every type T referenced by S, two routines are generated: one
3388 that takes 'T *', marks the pointer and calls the second routine,
3389 which just marks the fields of T. */
3391 static void
3392 write_user_marking_functions (type_p s, const char *prefix,
3393 struct walk_type_data *d)
3395 gcc_assert (s->kind == TYPE_USER_STRUCT);
3397 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3399 type_p fld_type = fld->type;
3400 if (fld_type->kind == TYPE_POINTER)
3402 type_p pointed_to_type = fld_type->u.p;
3403 if (union_or_struct_p (pointed_to_type))
3404 write_user_func_for_structure_ptr (d->of, pointed_to_type, prefix);
3406 else if (union_or_struct_p (fld_type))
3407 write_user_func_for_structure_body (fld_type, prefix, d);
3412 /* For S, a structure that's part of ORIG_S, and using parameters
3413 PARAM, write out a routine that:
3414 - Takes a parameter, a void * but actually of type *S
3415 - If SEEN_ROUTINE returns nonzero, calls write_types_process_field on each
3416 field of S or its substructures and (in some cases) things
3417 that are pointed to by S. */
3419 static void
3420 write_func_for_structure (type_p orig_s, type_p s, type_p *param,
3421 const struct write_types_data *wtd)
3423 const char *chain_next = NULL;
3424 const char *chain_prev = NULL;
3425 const char *chain_circular = NULL;
3426 const char *mark_hook_name = NULL;
3427 options_p opt;
3428 struct walk_type_data d;
3430 memset (&d, 0, sizeof (d));
3431 d.of = get_output_file_for_structure (s, param);
3432 for (opt = s->u.s.opt; opt; opt = opt->next)
3433 if (strcmp (opt->name, "chain_next") == 0
3434 && opt->kind == OPTION_STRING)
3435 chain_next = opt->info.string;
3436 else if (strcmp (opt->name, "chain_prev") == 0
3437 && opt->kind == OPTION_STRING)
3438 chain_prev = opt->info.string;
3439 else if (strcmp (opt->name, "chain_circular") == 0
3440 && opt->kind == OPTION_STRING)
3441 chain_circular = opt->info.string;
3442 else if (strcmp (opt->name, "mark_hook") == 0
3443 && opt->kind == OPTION_STRING)
3444 mark_hook_name = opt->info.string;
3445 if (chain_prev != NULL && chain_next == NULL)
3446 error_at_line (&s->u.s.line, "chain_prev without chain_next");
3447 if (chain_circular != NULL && chain_next != NULL)
3448 error_at_line (&s->u.s.line, "chain_circular with chain_next");
3449 if (chain_circular != NULL)
3450 chain_next = chain_circular;
3452 d.process_field = write_types_process_field;
3453 d.cookie = wtd;
3454 d.orig_s = orig_s;
3455 d.opt = s->u.s.opt;
3456 d.line = &s->u.s.line;
3457 d.bitmap = s->u.s.bitmap;
3458 d.param = param;
3459 d.prev_val[0] = "*x";
3460 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3461 d.prev_val[3] = "x";
3462 d.val = "(*x)";
3463 d.have_this_obj = false;
3465 oprintf (d.of, "\n");
3466 oprintf (d.of, "void\n");
3467 write_marker_function_name (d.of, orig_s, wtd->prefix);
3468 oprintf (d.of, " (void *x_p)\n");
3469 oprintf (d.of, "{\n ");
3470 write_type_decl (d.of, s);
3471 oprintf (d.of, " * %sx = (", chain_next == NULL ? "const " : "");
3472 write_type_decl (d.of, s);
3473 oprintf (d.of, " *)x_p;\n");
3474 if (chain_next != NULL)
3476 /* TYPE_USER_STRUCTs should not occur here. These structures
3477 are completely handled by user code. */
3478 gcc_assert (orig_s->kind != TYPE_USER_STRUCT);
3480 oprintf (d.of, " ");
3481 write_type_decl (d.of, s);
3482 oprintf (d.of, " * xlimit = x;\n");
3484 if (chain_next == NULL)
3486 oprintf (d.of, " if (%s (x", wtd->marker_routine);
3487 if (wtd->param_prefix)
3489 oprintf (d.of, ", x, gt_%s_", wtd->param_prefix);
3490 output_mangled_typename (d.of, orig_s);
3492 oprintf (d.of, "))\n");
3494 else
3496 if (chain_circular != NULL)
3497 oprintf (d.of, " if (!%s (xlimit", wtd->marker_routine);
3498 else
3499 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3500 if (wtd->param_prefix)
3502 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3503 output_mangled_typename (d.of, orig_s);
3505 oprintf (d.of, "))\n");
3506 if (chain_circular != NULL)
3507 oprintf (d.of, " return;\n do\n");
3508 if (mark_hook_name && !wtd->skip_hooks)
3510 oprintf (d.of, " {\n");
3511 oprintf (d.of, " %s (xlimit);\n ", mark_hook_name);
3513 oprintf (d.of, " xlimit = (");
3514 d.prev_val[2] = "*xlimit";
3515 output_escaped_param (&d, chain_next, "chain_next");
3516 oprintf (d.of, ");\n");
3517 if (mark_hook_name && !wtd->skip_hooks)
3518 oprintf (d.of, " }\n");
3519 if (chain_prev != NULL)
3521 oprintf (d.of, " if (x != xlimit)\n");
3522 oprintf (d.of, " for (;;)\n");
3523 oprintf (d.of, " {\n");
3524 oprintf (d.of, " %s %s * const xprev = (",
3525 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3527 d.prev_val[2] = "*x";
3528 output_escaped_param (&d, chain_prev, "chain_prev");
3529 oprintf (d.of, ");\n");
3530 oprintf (d.of, " if (xprev == NULL) break;\n");
3531 oprintf (d.of, " x = xprev;\n");
3532 oprintf (d.of, " (void) %s (xprev", wtd->marker_routine);
3533 if (wtd->param_prefix)
3535 oprintf (d.of, ", xprev, gt_%s_", wtd->param_prefix);
3536 output_mangled_typename (d.of, orig_s);
3538 oprintf (d.of, ");\n");
3539 oprintf (d.of, " }\n");
3541 if (chain_circular != NULL)
3543 oprintf (d.of, " while (%s (xlimit", wtd->marker_routine);
3544 if (wtd->param_prefix)
3546 oprintf (d.of, ", xlimit, gt_%s_", wtd->param_prefix);
3547 output_mangled_typename (d.of, orig_s);
3549 oprintf (d.of, "));\n");
3550 if (mark_hook_name && !wtd->skip_hooks)
3551 oprintf (d.of, " %s (xlimit);\n", mark_hook_name);
3552 oprintf (d.of, " do\n");
3554 else
3555 oprintf (d.of, " while (x != xlimit)\n");
3557 oprintf (d.of, " {\n");
3558 if (mark_hook_name && chain_next == NULL && !wtd->skip_hooks)
3560 oprintf (d.of, " %s (x);\n", mark_hook_name);
3563 d.prev_val[2] = "*x";
3564 d.indent = 6;
3565 if (orig_s->kind != TYPE_USER_STRUCT)
3566 walk_type (s, &d);
3567 else
3569 /* User structures have no fields to walk. Simply generate a call
3570 to the user-provided structure marker. */
3571 oprintf (d.of, "%*sgt_%sx (x);\n", d.indent, "", wtd->prefix);
3574 if (chain_next != NULL)
3576 oprintf (d.of, " x = (");
3577 output_escaped_param (&d, chain_next, "chain_next");
3578 oprintf (d.of, ");\n");
3581 oprintf (d.of, " }\n");
3582 if (chain_circular != NULL)
3583 oprintf (d.of, " while (x != xlimit);\n");
3584 oprintf (d.of, "}\n");
3586 if (orig_s->kind == TYPE_USER_STRUCT)
3587 write_user_marking_functions (orig_s, wtd->prefix, &d);
3591 /* Write out marker routines for STRUCTURES and PARAM_STRUCTS. */
3593 static void
3594 write_types (outf_p output_header, type_p structures, type_p param_structs,
3595 const struct write_types_data *wtd)
3597 int nbfun = 0; /* Count the emitted functions. */
3598 type_p s;
3600 oprintf (output_header, "\n/* %s*/\n", wtd->comment);
3602 /* We first emit the macros and the declarations. Functions' code is
3603 emitted afterwards. This is needed in plugin mode. */
3604 oprintf (output_header, "/* Macros and declarations. */\n");
3605 for (s = structures; s; s = s->next)
3606 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3608 options_p opt;
3610 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3611 continue;
3613 const char *s_id_for_tag = filter_type_name (s->u.s.tag);
3615 oprintf (output_header, "#define gt_%s_", wtd->prefix);
3616 output_mangled_typename (output_header, s);
3617 oprintf (output_header, "(X) do { \\\n");
3618 oprintf (output_header,
3619 " if (X != NULL) gt_%sx_%s (X);\\\n", wtd->prefix,
3620 s_id_for_tag);
3621 oprintf (output_header, " } while (0)\n");
3623 for (opt = s->u.s.opt; opt; opt = opt->next)
3624 if (strcmp (opt->name, "ptr_alias") == 0
3625 && opt->kind == OPTION_TYPE)
3627 const_type_p const t = (const_type_p) opt->info.type;
3628 if (t->kind == TYPE_STRUCT
3629 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3631 const char *t_id_for_tag = filter_type_name (t->u.s.tag);
3632 oprintf (output_header,
3633 "#define gt_%sx_%s gt_%sx_%s\n",
3634 wtd->prefix, s->u.s.tag, wtd->prefix, t_id_for_tag);
3635 if (t_id_for_tag != t->u.s.tag)
3636 free (CONST_CAST(char *, t_id_for_tag));
3638 else
3639 error_at_line (&s->u.s.line,
3640 "structure alias is not a structure");
3641 break;
3643 if (opt)
3644 continue;
3646 /* Declare the marker procedure only once. */
3647 oprintf (output_header,
3648 "extern void gt_%sx_%s (void *);\n",
3649 wtd->prefix, s_id_for_tag);
3651 if (s_id_for_tag != s->u.s.tag)
3652 free (CONST_CAST(char *, s_id_for_tag));
3654 if (s->u.s.line.file == NULL)
3656 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3657 s->u.s.tag);
3658 continue;
3662 for (s = param_structs; s; s = s->next)
3663 if (s->gc_used == GC_POINTED_TO)
3665 type_p stru = s->u.param_struct.stru;
3667 /* Declare the marker procedure. */
3668 oprintf (output_header, "extern void gt_%s_", wtd->prefix);
3669 output_mangled_typename (output_header, s);
3670 oprintf (output_header, " (void *);\n");
3672 if (stru->u.s.line.file == NULL)
3674 fprintf (stderr, "warning: structure `%s' used but not defined\n",
3675 stru->u.s.tag);
3676 continue;
3680 /* At last we emit the functions code. */
3681 oprintf (output_header, "\n/* functions code */\n");
3682 for (s = structures; s; s = s->next)
3683 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3685 options_p opt;
3687 if (s->gc_used == GC_MAYBE_POINTED_TO && s->u.s.line.file == NULL)
3688 continue;
3689 for (opt = s->u.s.opt; opt; opt = opt->next)
3690 if (strcmp (opt->name, "ptr_alias") == 0)
3691 break;
3692 if (opt)
3693 continue;
3695 if (s->kind == TYPE_LANG_STRUCT)
3697 type_p ss;
3698 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
3700 nbfun++;
3701 DBGPRINTF ("writing func #%d lang_struct ss @ %p '%s'",
3702 nbfun, (void*) ss, ss->u.s.tag);
3703 write_func_for_structure (s, ss, NULL, wtd);
3706 else
3708 nbfun++;
3709 DBGPRINTF ("writing func #%d struct s @ %p '%s'",
3710 nbfun, (void*) s, s->u.s.tag);
3711 write_func_for_structure (s, s, NULL, wtd);
3714 else
3716 /* Structure s is not possibly pointed to, so can be ignored. */
3717 DBGPRINTF ("ignored s @ %p '%s' gc_used#%d",
3718 (void*)s, s->u.s.tag,
3719 (int) s->gc_used);
3722 for (s = param_structs; s; s = s->next)
3723 if (s->gc_used == GC_POINTED_TO)
3725 type_p *param = s->u.param_struct.param;
3726 type_p stru = s->u.param_struct.stru;
3727 if (stru->u.s.line.file == NULL)
3728 continue;
3729 if (stru->kind == TYPE_LANG_STRUCT)
3731 type_p ss;
3732 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
3734 nbfun++;
3735 DBGPRINTF ("writing func #%d param lang_struct ss @ %p '%s'",
3736 nbfun, (void*) ss, ss->u.s.tag);
3737 write_func_for_structure (s, ss, param, wtd);
3740 else
3742 nbfun++;
3743 DBGPRINTF ("writing func #%d param struct s @ %p stru @ %p '%s'",
3744 nbfun, (void*) s,
3745 (void*) stru, stru->u.s.tag);
3746 write_func_for_structure (s, stru, param, wtd);
3749 else
3751 /* Param structure s is not pointed to, so should be ignored. */
3752 DBGPRINTF ("ignored s @ %p", (void*)s);
3754 if (verbosity_level >= 2)
3755 printf ("%s emitted %d routines for %s\n",
3756 progname, nbfun, wtd->comment);
3759 static const struct write_types_data ggc_wtd = {
3760 "ggc_m", NULL, "ggc_mark", "ggc_test_and_set_mark", NULL,
3761 "GC marker procedures. ",
3762 FALSE
3765 static const struct write_types_data pch_wtd = {
3766 "pch_n", "pch_p", "gt_pch_note_object", "gt_pch_note_object",
3767 "gt_pch_note_reorder",
3768 "PCH type-walking procedures. ",
3769 TRUE
3772 /* Write out the local pointer-walking routines. */
3774 /* process_field routine for local pointer-walking for user-callable
3775 routines. The difference between this and
3776 write_types_local_process_field is that, in this case, we do not
3777 need to check whether the given pointer matches the address of the
3778 parent structure. This check was already generated by the call
3779 to gt_pch_nx in the main gt_pch_p_*() function that is calling
3780 this code. */
3782 static void
3783 write_types_local_user_process_field (type_p f, const struct walk_type_data *d)
3785 switch (f->kind)
3787 case TYPE_POINTER:
3788 case TYPE_STRUCT:
3789 case TYPE_UNION:
3790 case TYPE_LANG_STRUCT:
3791 case TYPE_PARAM_STRUCT:
3792 case TYPE_STRING:
3793 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3794 break;
3796 case TYPE_USER_STRUCT:
3797 if (d->in_ptr_field)
3798 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3799 else
3800 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3801 d->indent, "", d->val);
3802 break;
3804 case TYPE_SCALAR:
3805 break;
3807 case TYPE_ARRAY:
3808 case TYPE_NONE:
3809 case TYPE_UNDEFINED:
3810 gcc_unreachable ();
3815 /* Write a function to PCH walk all the fields of type S on OF.
3816 D contains data needed by walk_type to recurse into the fields of S. */
3818 static void
3819 write_pch_user_walking_for_structure_body (type_p s, struct walk_type_data *d)
3821 oprintf (d->of, "\nvoid\n");
3822 oprintf (d->of, "gt_pch_nx (");
3823 write_type_decl (d->of, s);
3824 oprintf (d->of, "* x ATTRIBUTE_UNUSED,\n"
3825 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3826 "\tATTRIBUTE_UNUSED void *cookie)\n");
3827 oprintf (d->of, "{\n");
3828 d->val = "(*x)";
3829 d->indent = 2;
3830 d->process_field = write_types_local_user_process_field;
3831 walk_type (s, d);
3832 oprintf (d->of, "}\n");
3836 /* Emit the user-callable functions needed to mark all the types used
3837 by the user structure S. PREFIX is the prefix to use to
3838 distinguish ggc and pch markers. CHAIN_NEXT is set if S has the
3839 chain_next option defined. D contains data needed to pass to
3840 walk_type when traversing the fields of a type.
3842 For every type T referenced by S, two routines are generated: one
3843 that takes 'T *', marks the pointer and calls the second routine,
3844 which just marks the fields of T. */
3846 static void
3847 write_pch_user_walking_functions (type_p s, struct walk_type_data *d)
3849 gcc_assert (s->kind == TYPE_USER_STRUCT);
3851 for (pair_p fld = s->u.s.fields; fld; fld = fld->next)
3853 type_p fld_type = fld->type;
3854 if (union_or_struct_p (fld_type))
3855 write_pch_user_walking_for_structure_body (fld_type, d);
3860 /* process_field routine for local pointer-walking. */
3862 static void
3863 write_types_local_process_field (type_p f, const struct walk_type_data *d)
3865 gcc_assert (d->have_this_obj);
3866 switch (f->kind)
3868 case TYPE_POINTER:
3869 case TYPE_STRUCT:
3870 case TYPE_UNION:
3871 case TYPE_LANG_STRUCT:
3872 case TYPE_PARAM_STRUCT:
3873 case TYPE_STRING:
3874 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3875 d->prev_val[3]);
3876 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3877 break;
3879 case TYPE_USER_STRUCT:
3880 oprintf (d->of, "%*sif ((void *)(%s) == this_obj)\n", d->indent, "",
3881 d->prev_val[3]);
3882 if (d->in_ptr_field)
3883 oprintf (d->of, "%*s op (&(%s), cookie);\n", d->indent, "", d->val);
3884 else
3885 oprintf (d->of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3886 d->indent, "", d->val);
3887 break;
3889 case TYPE_SCALAR:
3890 break;
3892 case TYPE_ARRAY:
3893 case TYPE_NONE:
3894 case TYPE_UNDEFINED:
3895 gcc_unreachable ();
3900 /* For S, a structure that's part of ORIG_S, and using parameters
3901 PARAM, write out a routine that:
3902 - Is of type gt_note_pointers
3903 - Calls PROCESS_FIELD on each field of S or its substructures.
3906 static void
3907 write_local_func_for_structure (const_type_p orig_s, type_p s, type_p *param)
3909 struct walk_type_data d;
3911 memset (&d, 0, sizeof (d));
3912 d.of = get_output_file_for_structure (s, param);
3913 d.process_field = write_types_local_process_field;
3914 d.opt = s->u.s.opt;
3915 d.line = &s->u.s.line;
3916 d.bitmap = s->u.s.bitmap;
3917 d.param = param;
3918 d.prev_val[0] = d.prev_val[2] = "*x";
3919 d.prev_val[1] = "not valid postage"; /* Guarantee an error. */
3920 d.prev_val[3] = "x";
3921 d.val = "(*x)";
3922 d.fn_wants_lvalue = true;
3924 oprintf (d.of, "\n");
3925 oprintf (d.of, "void\n");
3926 oprintf (d.of, "gt_pch_p_");
3927 output_mangled_typename (d.of, orig_s);
3928 oprintf (d.of, " (ATTRIBUTE_UNUSED void *this_obj,\n"
3929 "\tvoid *x_p,\n"
3930 "\tATTRIBUTE_UNUSED gt_pointer_operator op,\n"
3931 "\tATTRIBUTE_UNUSED void *cookie)\n");
3932 oprintf (d.of, "{\n");
3933 oprintf (d.of, " %s %s * x ATTRIBUTE_UNUSED = (%s %s *)x_p;\n",
3934 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag,
3935 s->kind == TYPE_UNION ? "union" : "struct", s->u.s.tag);
3936 d.indent = 2;
3937 d.have_this_obj = true;
3939 if (s->kind != TYPE_USER_STRUCT)
3940 walk_type (s, &d);
3941 else
3943 /* User structures have no fields to walk. Simply generate a
3944 call to the user-provided PCH walker. */
3945 oprintf (d.of, "%*sif ((void *)(%s) == this_obj)\n", d.indent, "",
3946 d.prev_val[3]);
3947 oprintf (d.of, "%*s gt_pch_nx (&(%s), op, cookie);\n",
3948 d.indent, "", d.val);
3951 oprintf (d.of, "}\n");
3953 /* Write user-callable entry points for the PCH walking routines. */
3954 if (orig_s->kind == TYPE_USER_STRUCT)
3955 write_pch_user_walking_functions (s, &d);
3958 /* Write out local marker routines for STRUCTURES and PARAM_STRUCTS. */
3960 static void
3961 write_local (outf_p output_header, type_p structures, type_p param_structs)
3963 type_p s;
3965 if (!output_header)
3966 return;
3968 oprintf (output_header, "\n/* Local pointer-walking routines. */\n");
3969 for (s = structures; s; s = s->next)
3970 if (s->gc_used == GC_POINTED_TO || s->gc_used == GC_MAYBE_POINTED_TO)
3972 options_p opt;
3974 if (s->u.s.line.file == NULL)
3975 continue;
3976 for (opt = s->u.s.opt; opt; opt = opt->next)
3977 if (strcmp (opt->name, "ptr_alias") == 0
3978 && opt->kind == OPTION_TYPE)
3980 const_type_p const t = (const_type_p) opt->info.type;
3981 if (t->kind == TYPE_STRUCT
3982 || t->kind == TYPE_UNION || t->kind == TYPE_LANG_STRUCT)
3984 oprintf (output_header, "#define gt_pch_p_");
3985 output_mangled_typename (output_header, s);
3986 oprintf (output_header, " gt_pch_p_");
3987 output_mangled_typename (output_header, t);
3988 oprintf (output_header, "\n");
3990 else
3991 error_at_line (&s->u.s.line,
3992 "structure alias is not a structure");
3993 break;
3995 if (opt)
3996 continue;
3998 /* Declare the marker procedure only once. */
3999 oprintf (output_header, "extern void gt_pch_p_");
4000 output_mangled_typename (output_header, s);
4001 oprintf (output_header,
4002 "\n (void *, void *, gt_pointer_operator, void *);\n");
4004 if (s->kind == TYPE_LANG_STRUCT)
4006 type_p ss;
4007 for (ss = s->u.s.lang_struct; ss; ss = ss->next)
4008 write_local_func_for_structure (s, ss, NULL);
4010 else
4011 write_local_func_for_structure (s, s, NULL);
4014 for (s = param_structs; s; s = s->next)
4015 if (s->gc_used == GC_POINTED_TO)
4017 type_p *param = s->u.param_struct.param;
4018 type_p stru = s->u.param_struct.stru;
4020 /* Declare the marker procedure. */
4021 oprintf (output_header, "extern void gt_pch_p_");
4022 output_mangled_typename (output_header, s);
4023 oprintf (output_header,
4024 "\n (void *, void *, gt_pointer_operator, void *);\n");
4026 if (stru->u.s.line.file == NULL)
4028 fprintf (stderr, "warning: structure `%s' used but not defined\n",
4029 stru->u.s.tag);
4030 continue;
4033 if (stru->kind == TYPE_LANG_STRUCT)
4035 type_p ss;
4036 for (ss = stru->u.s.lang_struct; ss; ss = ss->next)
4037 write_local_func_for_structure (s, ss, param);
4039 else
4040 write_local_func_for_structure (s, stru, param);
4044 /* Nonzero if S is a type for which typed GC allocators should be output. */
4046 #define USED_BY_TYPED_GC_P(s) \
4047 ((s->kind == TYPE_POINTER \
4048 && (s->u.p->gc_used == GC_POINTED_TO \
4049 || s->u.p->gc_used == GC_USED)) \
4050 || (union_or_struct_p (s) \
4051 && ((s)->gc_used == GC_POINTED_TO \
4052 || ((s)->gc_used == GC_MAYBE_POINTED_TO \
4053 && s->u.s.line.file != NULL) \
4054 || ((s)->gc_used == GC_USED \
4055 && strncmp (s->u.s.tag, "anonymous", strlen ("anonymous"))))))
4058 /* Might T contain any non-pointer elements? */
4060 static int
4061 contains_scalar_p (type_p t)
4063 switch (t->kind)
4065 case TYPE_STRING:
4066 case TYPE_POINTER:
4067 return 0;
4068 case TYPE_ARRAY:
4069 return contains_scalar_p (t->u.a.p);
4070 case TYPE_USER_STRUCT:
4071 /* User-marked structures will typically contain pointers. */
4072 return 0;
4073 default:
4074 /* Could also check for structures that have no non-pointer
4075 fields, but there aren't enough of those to worry about. */
4076 return 1;
4080 /* Mangle INPF and print it to F. */
4082 static void
4083 put_mangled_filename (outf_p f, const input_file *inpf)
4085 /* The call to get_output_file_name may indirectly update fn since
4086 get_output_file_with_visibility caches its result inside, so we
4087 need the CONST_CAST. */
4088 const char *name = get_output_file_name (CONST_CAST (input_file*, inpf));
4089 if (!f || !name)
4090 return;
4091 for (; *name != 0; name++)
4092 if (ISALNUM (*name))
4093 oprintf (f, "%c", *name);
4094 else
4095 oprintf (f, "%c", '_');
4098 /* Finish off the currently-created root tables in FLP. PFX, TNAME,
4099 LASTNAME, and NAME are all strings to insert in various places in
4100 the resulting code. */
4102 static void
4103 finish_root_table (struct flist *flp, const char *pfx, const char *lastname,
4104 const char *tname, const char *name)
4106 struct flist *fli2;
4108 for (fli2 = flp; fli2; fli2 = fli2->next)
4109 if (fli2->started_p)
4111 oprintf (fli2->f, " %s\n", lastname);
4112 oprintf (fli2->f, "};\n\n");
4115 for (fli2 = flp; fli2 && base_files; fli2 = fli2->next)
4116 if (fli2->started_p)
4118 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4119 int fnum;
4121 for (fnum = 0; bitmap != 0; fnum++, bitmap >>= 1)
4122 if (bitmap & 1)
4124 oprintf (base_files[fnum],
4125 "extern const struct %s gt_%s_", tname, pfx);
4126 put_mangled_filename (base_files[fnum], fli2->file);
4127 oprintf (base_files[fnum], "[];\n");
4132 size_t fnum;
4133 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4134 oprintf (base_files[fnum],
4135 "EXPORTED_CONST struct %s * const %s[] = {\n", tname, name);
4139 for (fli2 = flp; fli2; fli2 = fli2->next)
4140 if (fli2->started_p)
4142 lang_bitmap bitmap = get_lang_bitmap (fli2->file);
4143 int fnum;
4145 fli2->started_p = 0;
4147 for (fnum = 0; base_files && bitmap != 0; fnum++, bitmap >>= 1)
4148 if (bitmap & 1)
4150 oprintf (base_files[fnum], " gt_%s_", pfx);
4151 put_mangled_filename (base_files[fnum], fli2->file);
4152 oprintf (base_files[fnum], ",\n");
4157 size_t fnum;
4158 for (fnum = 0; base_files && fnum < num_lang_dirs; fnum++)
4160 oprintf (base_files[fnum], " NULL\n");
4161 oprintf (base_files[fnum], "};\n");
4166 /* Write the first three fields (pointer, count and stride) for
4167 root NAME to F. V and LINE are as for write_root.
4169 Return true if the entry could be written; return false on error. */
4171 static bool
4172 start_root_entry (outf_p f, pair_p v, const char *name, struct fileloc *line)
4174 type_p ap;
4176 if (!v)
4178 error_at_line (line, "`%s' is too complex to be a root", name);
4179 return false;
4182 oprintf (f, " {\n");
4183 oprintf (f, " &%s,\n", name);
4184 oprintf (f, " 1");
4186 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4187 if (ap->u.a.len[0])
4188 oprintf (f, " * (%s)", ap->u.a.len);
4189 else if (ap == v->type)
4190 oprintf (f, " * ARRAY_SIZE (%s)", v->name);
4191 oprintf (f, ",\n");
4192 oprintf (f, " sizeof (%s", v->name);
4193 for (ap = v->type; ap->kind == TYPE_ARRAY; ap = ap->u.a.p)
4194 oprintf (f, "[0]");
4195 oprintf (f, "),\n");
4196 return true;
4199 /* A subroutine of write_root for writing the roots for field FIELD_NAME,
4200 which has type FIELD_TYPE. Parameters F to EMIT_PCH are the parameters
4201 of the caller. */
4203 static void
4204 write_field_root (outf_p f, pair_p v, type_p type, const char *name,
4205 int has_length, struct fileloc *line, const char *if_marked,
4206 bool emit_pch, type_p field_type, const char *field_name)
4208 struct pair newv;
4209 /* If the field reference is relative to V, rather than to some
4210 subcomponent of V, we can mark any subarrays with a single stride.
4211 We're effectively treating the field as a global variable in its
4212 own right. */
4213 if (v && type == v->type)
4215 newv = *v;
4216 newv.type = field_type;
4217 newv.name = ACONCAT ((v->name, ".", field_name, NULL));
4218 v = &newv;
4220 /* Otherwise, any arrays nested in the structure are too complex to
4221 handle. */
4222 else if (field_type->kind == TYPE_ARRAY)
4223 v = NULL;
4224 write_root (f, v, field_type, ACONCAT ((name, ".", field_name, NULL)),
4225 has_length, line, if_marked, emit_pch);
4228 /* Write out to F the table entry and any marker routines needed to
4229 mark NAME as TYPE. V can be one of three values:
4231 - null, if NAME is too complex to represent using a single
4232 count and stride. In this case, it is an error for NAME to
4233 contain any gc-ed data.
4235 - the outermost array that contains NAME, if NAME is part of an array.
4237 - the C variable that contains NAME, if NAME is not part of an array.
4239 LINE is the line of the C source that declares the root variable.
4240 HAS_LENGTH is nonzero iff V was a variable-length array. IF_MARKED
4241 is nonzero iff we are building the root table for hash table caches. */
4243 static void
4244 write_root (outf_p f, pair_p v, type_p type, const char *name, int has_length,
4245 struct fileloc *line, const char *if_marked, bool emit_pch)
4247 switch (type->kind)
4249 case TYPE_STRUCT:
4251 pair_p fld;
4252 for (fld = type->u.s.fields; fld; fld = fld->next)
4254 int skip_p = 0;
4255 const char *desc = NULL;
4256 options_p o;
4258 for (o = fld->opt; o; o = o->next)
4259 if (strcmp (o->name, "skip") == 0)
4260 skip_p = 1;
4261 else if (strcmp (o->name, "desc") == 0
4262 && o->kind == OPTION_STRING)
4263 desc = o->info.string;
4264 else if (strcmp (o->name, "param_is") == 0)
4266 else
4267 error_at_line (line,
4268 "field `%s' of global `%s' has unknown option `%s'",
4269 fld->name, name, o->name);
4271 if (skip_p)
4272 continue;
4273 else if (desc && fld->type->kind == TYPE_UNION)
4275 pair_p validf = NULL;
4276 pair_p ufld;
4278 for (ufld = fld->type->u.s.fields; ufld; ufld = ufld->next)
4280 const char *tag = NULL;
4281 options_p oo;
4282 for (oo = ufld->opt; oo; oo = oo->next)
4283 if (strcmp (oo->name, "tag") == 0
4284 && oo->kind == OPTION_STRING)
4285 tag = oo->info.string;
4286 if (tag == NULL || strcmp (tag, desc) != 0)
4287 continue;
4288 if (validf != NULL)
4289 error_at_line (line,
4290 "both `%s.%s.%s' and `%s.%s.%s' have tag `%s'",
4291 name, fld->name, validf->name,
4292 name, fld->name, ufld->name, tag);
4293 validf = ufld;
4295 if (validf != NULL)
4296 write_field_root (f, v, type, name, 0, line, if_marked,
4297 emit_pch, validf->type,
4298 ACONCAT ((fld->name, ".",
4299 validf->name, NULL)));
4301 else if (desc)
4302 error_at_line (line,
4303 "global `%s.%s' has `desc' option but is not union",
4304 name, fld->name);
4305 else
4306 write_field_root (f, v, type, name, 0, line, if_marked,
4307 emit_pch, fld->type, fld->name);
4310 break;
4312 case TYPE_ARRAY:
4314 char *newname;
4315 newname = xasprintf ("%s[0]", name);
4316 write_root (f, v, type->u.a.p, newname, has_length, line, if_marked,
4317 emit_pch);
4318 free (newname);
4320 break;
4322 case TYPE_USER_STRUCT:
4323 error_at_line (line, "`%s' must be a pointer type, because it is "
4324 "a GC root and its type is marked with GTY((user))",
4325 v->name);
4326 break;
4328 case TYPE_POINTER:
4330 type_p tp;
4332 if (!start_root_entry (f, v, name, line))
4333 return;
4335 tp = type->u.p;
4337 if (!has_length && union_or_struct_p (tp))
4339 const char *id_for_tag = filter_type_name (tp->u.s.tag);
4340 oprintf (f, " &gt_ggc_mx_%s,\n", id_for_tag);
4341 if (emit_pch)
4342 oprintf (f, " &gt_pch_nx_%s", id_for_tag);
4343 else
4344 oprintf (f, " NULL");
4345 if (id_for_tag != tp->u.s.tag)
4346 free (CONST_CAST(char *, id_for_tag));
4348 else if (!has_length && tp->kind == TYPE_PARAM_STRUCT)
4350 oprintf (f, " &gt_ggc_m_");
4351 output_mangled_typename (f, tp);
4352 if (emit_pch)
4354 oprintf (f, ",\n &gt_pch_n_");
4355 output_mangled_typename (f, tp);
4357 else
4358 oprintf (f, ",\n NULL");
4360 else if (has_length
4361 && (tp->kind == TYPE_POINTER || union_or_struct_p (tp)))
4363 oprintf (f, " &gt_ggc_ma_%s,\n", name);
4364 if (emit_pch)
4365 oprintf (f, " &gt_pch_na_%s", name);
4366 else
4367 oprintf (f, " NULL");
4369 else
4371 error_at_line (line,
4372 "global `%s' is pointer to unimplemented type",
4373 name);
4375 if (if_marked)
4376 oprintf (f, ",\n &%s", if_marked);
4377 oprintf (f, "\n },\n");
4379 break;
4381 case TYPE_STRING:
4383 if (!start_root_entry (f, v, name, line))
4384 return;
4386 oprintf (f, " (gt_pointer_walker) &gt_ggc_m_S,\n");
4387 oprintf (f, " (gt_pointer_walker) &gt_pch_n_S\n");
4388 oprintf (f, " },\n");
4390 break;
4392 case TYPE_SCALAR:
4393 break;
4395 case TYPE_NONE:
4396 case TYPE_UNDEFINED:
4397 case TYPE_UNION:
4398 case TYPE_LANG_STRUCT:
4399 case TYPE_PARAM_STRUCT:
4400 error_at_line (line, "global `%s' is unimplemented type", name);
4404 /* This generates a routine to walk an array. */
4406 static void
4407 write_array (outf_p f, pair_p v, const struct write_types_data *wtd)
4409 struct walk_type_data d;
4410 char *prevval3;
4412 memset (&d, 0, sizeof (d));
4413 d.of = f;
4414 d.cookie = wtd;
4415 d.indent = 2;
4416 d.line = &v->line;
4417 d.opt = v->opt;
4418 d.bitmap = get_lang_bitmap (v->line.file);
4419 d.param = NULL;
4421 d.prev_val[3] = prevval3 = xasprintf ("&%s", v->name);
4423 if (wtd->param_prefix)
4425 oprintf (f, "static void gt_%sa_%s\n", wtd->param_prefix, v->name);
4426 oprintf (f, " (void *, void *, gt_pointer_operator, void *);\n");
4427 oprintf (f, "static void gt_%sa_%s (ATTRIBUTE_UNUSED void *this_obj,\n",
4428 wtd->param_prefix, v->name);
4429 oprintf (d.of,
4430 " ATTRIBUTE_UNUSED void *x_p,\n"
4431 " ATTRIBUTE_UNUSED gt_pointer_operator op,\n"
4432 " ATTRIBUTE_UNUSED void * cookie)\n");
4433 oprintf (d.of, "{\n");
4434 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4435 d.process_field = write_types_local_process_field;
4436 d.have_this_obj = true;
4437 walk_type (v->type, &d);
4438 oprintf (f, "}\n\n");
4441 d.opt = v->opt;
4442 oprintf (f, "static void gt_%sa_%s (void *);\n", wtd->prefix, v->name);
4443 oprintf (f, "static void\ngt_%sa_%s (ATTRIBUTE_UNUSED void *x_p)\n",
4444 wtd->prefix, v->name);
4445 oprintf (f, "{\n");
4446 d.prev_val[0] = d.prev_val[1] = d.prev_val[2] = d.val = v->name;
4447 d.process_field = write_types_process_field;
4448 d.have_this_obj = false;
4449 walk_type (v->type, &d);
4450 free (prevval3);
4451 oprintf (f, "}\n\n");
4454 /* Output a table describing the locations and types of VARIABLES. */
4456 static void
4457 write_roots (pair_p variables, bool emit_pch)
4459 pair_p v;
4460 struct flist *flp = NULL;
4462 for (v = variables; v; v = v->next)
4464 outf_p f =
4465 get_output_file_with_visibility (CONST_CAST (input_file*,
4466 v->line.file));
4467 struct flist *fli;
4468 const char *length = NULL;
4469 int deletable_p = 0;
4470 options_p o;
4471 for (o = v->opt; o; o = o->next)
4472 if (strcmp (o->name, "length") == 0
4473 && o->kind == OPTION_STRING)
4474 length = o->info.string;
4475 else if (strcmp (o->name, "deletable") == 0)
4476 deletable_p = 1;
4477 else if (strcmp (o->name, "param_is") == 0)
4479 else if (strncmp (o->name, "param", 5) == 0
4480 && ISDIGIT (o->name[5]) && strcmp (o->name + 6, "_is") == 0)
4482 else if (strcmp (o->name, "if_marked") == 0)
4484 else
4485 error_at_line (&v->line,
4486 "global `%s' has unknown option `%s'",
4487 v->name, o->name);
4489 for (fli = flp; fli; fli = fli->next)
4490 if (fli->f == f && f)
4491 break;
4492 if (fli == NULL)
4494 fli = XNEW (struct flist);
4495 fli->f = f;
4496 fli->next = flp;
4497 fli->started_p = 0;
4498 fli->file = v->line.file;
4499 gcc_assert (fli->file);
4500 flp = fli;
4502 oprintf (f, "\n/* GC roots. */\n\n");
4505 if (!deletable_p
4506 && length
4507 && v->type->kind == TYPE_POINTER
4508 && (v->type->u.p->kind == TYPE_POINTER
4509 || v->type->u.p->kind == TYPE_STRUCT))
4511 write_array (f, v, &ggc_wtd);
4512 write_array (f, v, &pch_wtd);
4516 for (v = variables; v; v = v->next)
4518 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4519 v->line.file));
4520 struct flist *fli;
4521 int skip_p = 0;
4522 int length_p = 0;
4523 options_p o;
4525 for (o = v->opt; o; o = o->next)
4526 if (strcmp (o->name, "length") == 0)
4527 length_p = 1;
4528 else if (strcmp (o->name, "deletable") == 0
4529 || strcmp (o->name, "if_marked") == 0)
4530 skip_p = 1;
4532 if (skip_p)
4533 continue;
4535 for (fli = flp; fli; fli = fli->next)
4536 if (fli->f == f)
4537 break;
4538 if (!fli->started_p)
4540 fli->started_p = 1;
4542 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_r_");
4543 put_mangled_filename (f, v->line.file);
4544 oprintf (f, "[] = {\n");
4547 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4550 finish_root_table (flp, "ggc_r", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4551 "gt_ggc_rtab");
4553 for (v = variables; v; v = v->next)
4555 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4556 v->line.file));
4557 struct flist *fli;
4558 int skip_p = 1;
4559 options_p o;
4561 for (o = v->opt; o; o = o->next)
4562 if (strcmp (o->name, "deletable") == 0)
4563 skip_p = 0;
4564 else if (strcmp (o->name, "if_marked") == 0)
4565 skip_p = 1;
4567 if (skip_p)
4568 continue;
4570 for (fli = flp; fli; fli = fli->next)
4571 if (fli->f == f)
4572 break;
4573 if (!fli->started_p)
4575 fli->started_p = 1;
4577 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_");
4578 put_mangled_filename (f, v->line.file);
4579 oprintf (f, "[] = {\n");
4582 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4583 v->name, v->name);
4586 finish_root_table (flp, "ggc_rd", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4587 "gt_ggc_deletable_rtab");
4589 for (v = variables; v; v = v->next)
4591 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4592 v->line.file));
4593 struct flist *fli;
4594 const char *if_marked = NULL;
4595 int length_p = 0;
4596 options_p o;
4598 for (o = v->opt; o; o = o->next)
4599 if (strcmp (o->name, "length") == 0)
4600 length_p = 1;
4601 else if (strcmp (o->name, "if_marked") == 0
4602 && o->kind == OPTION_STRING)
4603 if_marked = o->info.string;
4604 if (if_marked == NULL)
4605 continue;
4606 if (v->type->kind != TYPE_POINTER
4607 || v->type->u.p->kind != TYPE_PARAM_STRUCT
4608 || v->type->u.p->u.param_struct.stru != find_structure ("htab",
4609 TYPE_STRUCT))
4611 error_at_line (&v->line,
4612 "if_marked option used but not hash table");
4613 continue;
4616 for (fli = flp; fli; fli = fli->next)
4617 if (fli->f == f)
4618 break;
4619 if (!fli->started_p)
4621 fli->started_p = 1;
4623 oprintf (f, "EXPORTED_CONST struct ggc_cache_tab gt_ggc_rc_");
4624 put_mangled_filename (f, v->line.file);
4625 oprintf (f, "[] = {\n");
4628 write_root (f, v, v->type->u.p->u.param_struct.param[0],
4629 v->name, length_p, &v->line, if_marked, emit_pch);
4632 finish_root_table (flp, "ggc_rc", "LAST_GGC_CACHE_TAB", "ggc_cache_tab",
4633 "gt_ggc_cache_rtab");
4635 if (!emit_pch)
4636 return;
4638 for (v = variables; v; v = v->next)
4640 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4641 v->line.file));
4642 struct flist *fli;
4643 int length_p = 0;
4644 int if_marked_p = 0;
4645 options_p o;
4647 for (o = v->opt; o; o = o->next)
4648 if (strcmp (o->name, "length") == 0)
4649 length_p = 1;
4650 else if (strcmp (o->name, "if_marked") == 0)
4651 if_marked_p = 1;
4653 if (!if_marked_p)
4654 continue;
4656 for (fli = flp; fli; fli = fli->next)
4657 if (fli->f == f)
4658 break;
4659 if (!fli->started_p)
4661 fli->started_p = 1;
4663 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rc_");
4664 put_mangled_filename (f, v->line.file);
4665 oprintf (f, "[] = {\n");
4668 write_root (f, v, v->type, v->name, length_p, &v->line, NULL, emit_pch);
4671 finish_root_table (flp, "pch_rc", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4672 "gt_pch_cache_rtab");
4674 for (v = variables; v; v = v->next)
4676 outf_p f = get_output_file_with_visibility (CONST_CAST (input_file*,
4677 v->line.file));
4678 struct flist *fli;
4679 int skip_p = 0;
4680 options_p o;
4682 for (o = v->opt; o; o = o->next)
4683 if (strcmp (o->name, "deletable") == 0
4684 || strcmp (o->name, "if_marked") == 0)
4685 skip_p = 1;
4687 if (skip_p)
4688 continue;
4690 if (!contains_scalar_p (v->type))
4691 continue;
4693 for (fli = flp; fli; fli = fli->next)
4694 if (fli->f == f)
4695 break;
4696 if (!fli->started_p)
4698 fli->started_p = 1;
4700 oprintf (f, "EXPORTED_CONST struct ggc_root_tab gt_pch_rs_");
4701 put_mangled_filename (f, v->line.file);
4702 oprintf (f, "[] = {\n");
4705 oprintf (f, " { &%s, 1, sizeof (%s), NULL, NULL },\n",
4706 v->name, v->name);
4709 finish_root_table (flp, "pch_rs", "LAST_GGC_ROOT_TAB", "ggc_root_tab",
4710 "gt_pch_scalar_rtab");
4713 /* TRUE if type S has the GTY variable_size annotation. */
4715 static bool
4716 variable_size_p (const type_p s)
4718 options_p o;
4719 for (o = s->u.s.opt; o; o = o->next)
4720 if (strcmp (o->name, "variable_size") == 0)
4721 return true;
4722 return false;
4725 enum alloc_quantity
4726 { single, vector };
4728 /* Writes one typed allocator definition into output F for type
4729 identifier TYPE_NAME with optional type specifier TYPE_SPECIFIER.
4730 The allocator name will contain ALLOCATOR_TYPE. If VARIABLE_SIZE
4731 is true, the allocator will have an extra parameter specifying
4732 number of bytes to allocate. If QUANTITY is set to VECTOR, a
4733 vector allocator will be output. */
4735 static void
4736 write_typed_alloc_def (outf_p f,
4737 bool variable_size, const char *type_specifier,
4738 const char *type_name, const char *allocator_type,
4739 enum alloc_quantity quantity)
4741 bool two_args = variable_size && (quantity == vector);
4742 gcc_assert (f != NULL);
4743 const char *type_name_as_id = filter_type_name (type_name);
4744 oprintf (f, "#define ggc_alloc_%s%s", allocator_type, type_name_as_id);
4745 oprintf (f, "(%s%s%s) ",
4746 (variable_size ? "SIZE" : ""),
4747 (two_args ? ", " : ""),
4748 (quantity == vector) ? "n" : "");
4749 oprintf (f, "((%s%s *)", type_specifier, type_name);
4750 oprintf (f, "(ggc_internal_%salloc_stat (", allocator_type);
4751 if (variable_size)
4752 oprintf (f, "SIZE");
4753 else
4754 oprintf (f, "sizeof (%s%s)", type_specifier, type_name);
4755 if (quantity == vector)
4756 oprintf (f, ", n");
4757 oprintf (f, " MEM_STAT_INFO)))\n");
4758 if (type_name_as_id != type_name)
4759 free (CONST_CAST(char *, type_name_as_id));
4762 /* Writes a typed allocator definition into output F for a struct or
4763 union S, with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4765 static void
4766 write_typed_struct_alloc_def (outf_p f,
4767 const type_p s, const char *allocator_type,
4768 enum alloc_quantity quantity)
4770 gcc_assert (union_or_struct_p (s));
4771 write_typed_alloc_def (f, variable_size_p (s), get_type_specifier (s),
4772 s->u.s.tag, allocator_type, quantity);
4775 /* Writes a typed allocator definition into output F for a typedef P,
4776 with a given ALLOCATOR_TYPE and QUANTITY for ZONE. */
4778 static void
4779 write_typed_typedef_alloc_def (outf_p f,
4780 const pair_p p, const char *allocator_type,
4781 enum alloc_quantity quantity)
4783 write_typed_alloc_def (f, variable_size_p (p->type), "", p->name,
4784 allocator_type, quantity);
4787 /* Writes typed allocator definitions into output F for the types in
4788 STRUCTURES and TYPEDEFS that are used by GC. */
4790 static void
4791 write_typed_alloc_defns (outf_p f,
4792 const type_p structures, const pair_p typedefs)
4794 type_p s;
4795 pair_p p;
4797 gcc_assert (f != NULL);
4798 oprintf (f,
4799 "\n/* Allocators for known structs and unions. */\n\n");
4800 for (s = structures; s; s = s->next)
4802 if (!USED_BY_TYPED_GC_P (s))
4803 continue;
4804 gcc_assert (union_or_struct_p (s));
4805 /* In plugin mode onput output ggc_alloc macro definitions
4806 relevant to plugin input files. */
4807 if (nb_plugin_files > 0
4808 && ((s->u.s.line.file == NULL) || !s->u.s.line.file->inpisplugin))
4809 continue;
4810 write_typed_struct_alloc_def (f, s, "", single);
4811 write_typed_struct_alloc_def (f, s, "cleared_", single);
4812 write_typed_struct_alloc_def (f, s, "vec_", vector);
4813 write_typed_struct_alloc_def (f, s, "cleared_vec_", vector);
4816 oprintf (f, "\n/* Allocators for known typedefs. */\n");
4817 for (p = typedefs; p; p = p->next)
4819 s = p->type;
4820 if (!USED_BY_TYPED_GC_P (s) || (strcmp (p->name, s->u.s.tag) == 0))
4821 continue;
4822 /* In plugin mode onput output ggc_alloc macro definitions
4823 relevant to plugin input files. */
4824 if (nb_plugin_files > 0)
4826 struct fileloc* filoc = type_fileloc(s);
4827 if (!filoc || !filoc->file->inpisplugin)
4828 continue;
4830 write_typed_typedef_alloc_def (f, p, "", single);
4831 write_typed_typedef_alloc_def (f, p, "cleared_", single);
4832 write_typed_typedef_alloc_def (f, p, "vec_", vector);
4833 write_typed_typedef_alloc_def (f, p, "cleared_vec_", vector);
4837 /* Prints not-as-ugly version of a typename of T to OF. Trades the uniquness
4838 guaranteee for somewhat increased readability. If name conflicts do happen,
4839 this funcion will have to be adjusted to be more like
4840 output_mangled_typename. */
4842 static void
4843 output_typename (outf_p of, const_type_p t)
4845 switch (t->kind)
4847 case TYPE_STRING:
4848 oprintf (of, "str");
4849 break;
4850 case TYPE_SCALAR:
4851 oprintf (of, "scalar");
4852 break;
4853 case TYPE_POINTER:
4854 output_typename (of, t->u.p);
4855 break;
4856 case TYPE_STRUCT:
4857 case TYPE_USER_STRUCT:
4858 case TYPE_UNION:
4859 case TYPE_LANG_STRUCT:
4860 oprintf (of, "%s", t->u.s.tag);
4861 break;
4862 case TYPE_PARAM_STRUCT:
4864 int i;
4865 for (i = 0; i < NUM_PARAM; i++)
4866 if (t->u.param_struct.param[i] != NULL)
4868 output_typename (of, t->u.param_struct.param[i]);
4869 oprintf (of, "_");
4871 output_typename (of, t->u.param_struct.stru);
4872 break;
4874 case TYPE_NONE:
4875 case TYPE_UNDEFINED:
4876 case TYPE_ARRAY:
4877 gcc_unreachable ();
4881 /* Writes a typed GC allocator for type S that is suitable as a callback for
4882 the splay tree implementation in libiberty. */
4884 static void
4885 write_splay_tree_allocator_def (const_type_p s)
4887 outf_p of = get_output_file_with_visibility (NULL);
4888 oprintf (of, "void * ggc_alloc_splay_tree_");
4889 output_typename (of, s);
4890 oprintf (of, " (int sz, void * nl)\n");
4891 oprintf (of, "{\n");
4892 oprintf (of, " return ggc_splay_alloc (sz, nl);\n");
4893 oprintf (of, "}\n\n");
4896 /* Writes typed GC allocators for PARAM_STRUCTS that are suitable as callbacks
4897 for the splay tree implementation in libiberty. */
4899 static void
4900 write_splay_tree_allocators (const_type_p param_structs)
4902 const_type_p s;
4904 oprintf (header_file, "\n/* Splay tree callback allocators. */\n");
4905 for (s = param_structs; s; s = s->next)
4906 if (s->gc_used == GC_POINTED_TO)
4908 oprintf (header_file, "extern void * ggc_alloc_splay_tree_");
4909 output_typename (header_file, s);
4910 oprintf (header_file, " (int, void *);\n");
4911 write_splay_tree_allocator_def (s);
4915 #define INDENT 2
4917 /* Dumps the value of typekind KIND. */
4919 static void
4920 dump_typekind (int indent, enum typekind kind)
4922 printf ("%*ckind = ", indent, ' ');
4923 switch (kind)
4925 case TYPE_SCALAR:
4926 printf ("TYPE_SCALAR");
4927 break;
4928 case TYPE_STRING:
4929 printf ("TYPE_STRING");
4930 break;
4931 case TYPE_STRUCT:
4932 printf ("TYPE_STRUCT");
4933 break;
4934 case TYPE_UNDEFINED:
4935 printf ("TYPE_UNDEFINED");
4936 break;
4937 case TYPE_USER_STRUCT:
4938 printf ("TYPE_USER_STRUCT");
4939 break;
4940 case TYPE_UNION:
4941 printf ("TYPE_UNION");
4942 break;
4943 case TYPE_POINTER:
4944 printf ("TYPE_POINTER");
4945 break;
4946 case TYPE_ARRAY:
4947 printf ("TYPE_ARRAY");
4948 break;
4949 case TYPE_LANG_STRUCT:
4950 printf ("TYPE_LANG_STRUCT");
4951 break;
4952 case TYPE_PARAM_STRUCT:
4953 printf ("TYPE_PARAM_STRUCT");
4954 break;
4955 default:
4956 gcc_unreachable ();
4958 printf ("\n");
4961 /* Dumps the value of GC_USED flag. */
4963 static void
4964 dump_gc_used (int indent, enum gc_used_enum gc_used)
4966 printf ("%*cgc_used = ", indent, ' ');
4967 switch (gc_used)
4969 case GC_UNUSED:
4970 printf ("GC_UNUSED");
4971 break;
4972 case GC_USED:
4973 printf ("GC_USED");
4974 break;
4975 case GC_MAYBE_POINTED_TO:
4976 printf ("GC_MAYBE_POINTED_TO");
4977 break;
4978 case GC_POINTED_TO:
4979 printf ("GC_POINTED_TO");
4980 break;
4981 default:
4982 gcc_unreachable ();
4984 printf ("\n");
4987 /* Dumps the type options OPT. */
4989 static void
4990 dump_options (int indent, options_p opt)
4992 options_p o;
4993 printf ("%*coptions = ", indent, ' ');
4994 o = opt;
4995 while (o)
4997 switch (o->kind)
4999 case OPTION_STRING:
5000 printf ("%s:string %s ", o->name, o->info.string);
5001 break;
5002 case OPTION_TYPE:
5003 printf ("%s:type ", o->name);
5004 dump_type (indent+1, o->info.type);
5005 break;
5006 case OPTION_NESTED:
5007 printf ("%s:nested ", o->name);
5008 break;
5009 case OPTION_NONE:
5010 gcc_unreachable ();
5012 o = o->next;
5014 printf ("\n");
5017 /* Dumps the source file location in LINE. */
5019 static void
5020 dump_fileloc (int indent, struct fileloc line)
5022 printf ("%*cfileloc: file = %s, line = %d\n", indent, ' ',
5023 get_input_file_name (line.file),
5024 line.line);
5027 /* Recursively dumps the struct, union, or a language-specific
5028 struct T. */
5030 static void
5031 dump_type_u_s (int indent, type_p t)
5033 pair_p fields;
5035 gcc_assert (union_or_struct_p (t));
5036 printf ("%*cu.s.tag = %s\n", indent, ' ', t->u.s.tag);
5037 dump_fileloc (indent, t->u.s.line);
5038 printf ("%*cu.s.fields =\n", indent, ' ');
5039 fields = t->u.s.fields;
5040 while (fields)
5042 dump_pair (indent + INDENT, fields);
5043 fields = fields->next;
5045 printf ("%*cend of fields of type %p\n", indent, ' ', (void *) t);
5046 dump_options (indent, t->u.s.opt);
5047 printf ("%*cu.s.bitmap = %X\n", indent, ' ', t->u.s.bitmap);
5048 if (t->kind == TYPE_LANG_STRUCT)
5050 printf ("%*cu.s.lang_struct:\n", indent, ' ');
5051 dump_type_list (indent + INDENT, t->u.s.lang_struct);
5055 /* Recursively dumps the array T. */
5057 static void
5058 dump_type_u_a (int indent, type_p t)
5060 gcc_assert (t->kind == TYPE_ARRAY);
5061 printf ("%*clen = %s, u.a.p:\n", indent, ' ', t->u.a.len);
5062 dump_type_list (indent + INDENT, t->u.a.p);
5065 /* Recursively dumps the parameterized struct T. */
5067 static void
5068 dump_type_u_param_struct (int indent, type_p t)
5070 int i;
5071 gcc_assert (t->kind == TYPE_PARAM_STRUCT);
5072 printf ("%*cu.param_struct.stru:\n", indent, ' ');
5073 dump_type_list (indent, t->u.param_struct.stru);
5074 dump_fileloc (indent, t->u.param_struct.line);
5075 for (i = 0; i < NUM_PARAM; i++)
5077 if (t->u.param_struct.param[i] == NULL)
5078 continue;
5079 printf ("%*cu.param_struct.param[%d]:\n", indent, ' ', i);
5080 dump_type (indent + INDENT, t->u.param_struct.param[i]);
5084 /* Recursively dumps the type list T. */
5086 static void
5087 dump_type_list (int indent, type_p t)
5089 type_p p = t;
5090 while (p)
5092 dump_type (indent, p);
5093 p = p->next;
5097 static htab_t seen_types;
5099 /* Recursively dumps the type T if it was not dumped previously. */
5101 static void
5102 dump_type (int indent, type_p t)
5104 PTR *slot;
5106 if (seen_types == NULL)
5107 seen_types = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL);
5109 printf ("%*cType at %p: ", indent, ' ', (void *) t);
5110 slot = htab_find_slot (seen_types, t, INSERT);
5111 if (*slot != NULL)
5113 printf ("already seen.\n");
5114 return;
5116 *slot = t;
5117 printf ("\n");
5119 dump_typekind (indent, t->kind);
5120 printf ("%*cpointer_to = %p\n", indent + INDENT, ' ',
5121 (void *) t->pointer_to);
5122 dump_gc_used (indent + INDENT, t->gc_used);
5123 switch (t->kind)
5125 case TYPE_SCALAR:
5126 printf ("%*cscalar_is_char = %s\n", indent + INDENT, ' ',
5127 t->u.scalar_is_char ? "true" : "false");
5128 break;
5129 case TYPE_STRING:
5130 break;
5131 case TYPE_STRUCT:
5132 case TYPE_UNION:
5133 case TYPE_LANG_STRUCT:
5134 case TYPE_USER_STRUCT:
5135 dump_type_u_s (indent + INDENT, t);
5136 break;
5137 case TYPE_POINTER:
5138 printf ("%*cp:\n", indent + INDENT, ' ');
5139 dump_type (indent + INDENT, t->u.p);
5140 break;
5141 case TYPE_ARRAY:
5142 dump_type_u_a (indent + INDENT, t);
5143 break;
5144 case TYPE_PARAM_STRUCT:
5145 dump_type_u_param_struct (indent + INDENT, t);
5146 break;
5147 default:
5148 gcc_unreachable ();
5150 printf ("%*cEnd of type at %p\n", indent, ' ', (void *) t);
5153 /* Dumps the pair P. */
5155 static void
5156 dump_pair (int indent, pair_p p)
5158 printf ("%*cpair: name = %s\n", indent, ' ', p->name);
5159 dump_type (indent, p->type);
5160 dump_fileloc (indent, p->line);
5161 dump_options (indent, p->opt);
5162 printf ("%*cEnd of pair %s\n", indent, ' ', p->name);
5165 /* Dumps the list of pairs PP. */
5167 static void
5168 dump_pair_list (const char *name, pair_p pp)
5170 pair_p p;
5171 printf ("%s:\n", name);
5172 for (p = pp; p != NULL; p = p->next)
5173 dump_pair (0, p);
5174 printf ("End of %s\n\n", name);
5177 /* Dumps the STRUCTURES. */
5179 static void
5180 dump_structures (const char *name, type_p structures)
5182 printf ("%s:\n", name);
5183 dump_type_list (0, structures);
5184 printf ("End of %s\n\n", name);
5187 /* Dumps the internal structures of gengtype. This is useful to debug
5188 gengtype itself, or to understand what it does, e.g. for plugin
5189 developers. */
5191 static void
5192 dump_everything (void)
5194 dump_pair_list ("typedefs", typedefs);
5195 dump_structures ("structures", structures);
5196 dump_structures ("param_structs", param_structs);
5197 dump_pair_list ("variables", variables);
5199 /* Allocated with the first call to dump_type. */
5200 htab_delete (seen_types);
5205 /* Option specification for getopt_long. */
5206 static const struct option gengtype_long_options[] = {
5207 {"help", no_argument, NULL, 'h'},
5208 {"version", no_argument, NULL, 'V'},
5209 {"verbose", no_argument, NULL, 'v'},
5210 {"dump", no_argument, NULL, 'd'},
5211 {"debug", no_argument, NULL, 'D'},
5212 {"plugin", required_argument, NULL, 'P'},
5213 {"srcdir", required_argument, NULL, 'S'},
5214 {"backupdir", required_argument, NULL, 'B'},
5215 {"inputs", required_argument, NULL, 'I'},
5216 {"read-state", required_argument, NULL, 'r'},
5217 {"write-state", required_argument, NULL, 'w'},
5218 /* Terminating NULL placeholder. */
5219 {NULL, no_argument, NULL, 0},
5223 static void
5224 print_usage (void)
5226 printf ("Usage: %s\n", progname);
5227 printf ("\t -h | --help " " \t# Give this help.\n");
5228 printf ("\t -D | --debug "
5229 " \t# Give debug output to debug %s itself.\n", progname);
5230 printf ("\t -V | --version " " \t# Give version information.\n");
5231 printf ("\t -v | --verbose \t# Increase verbosity. Can be given several times.\n");
5232 printf ("\t -d | --dump " " \t# Dump state for debugging.\n");
5233 printf ("\t -P | --plugin <output-file> <plugin-src> ... "
5234 " \t# Generate for plugin.\n");
5235 printf ("\t -S | --srcdir <GCC-directory> "
5236 " \t# Specify the GCC source directory.\n");
5237 printf ("\t -B | --backupdir <directory> "
5238 " \t# Specify the backup directory for updated files.\n");
5239 printf ("\t -I | --inputs <input-list> "
5240 " \t# Specify the file with source files list.\n");
5241 printf ("\t -w | --write-state <state-file> " " \t# Write a state file.\n");
5242 printf ("\t -r | --read-state <state-file> " " \t# Read a state file.\n");
5245 static void
5246 print_version (void)
5248 printf ("%s %s%s\n", progname, pkgversion_string, version_string);
5249 printf ("Report bugs: %s\n", bug_report_url);
5252 /* Parse the program options using getopt_long... */
5253 static void
5254 parse_program_options (int argc, char **argv)
5256 int opt = -1;
5257 while ((opt = getopt_long (argc, argv, "hVvdP:S:B:I:w:r:D",
5258 gengtype_long_options, NULL)) >= 0)
5260 switch (opt)
5262 case 'h': /* --help */
5263 print_usage ();
5264 break;
5265 case 'V': /* --version */
5266 print_version ();
5267 break;
5268 case 'd': /* --dump */
5269 do_dump = 1;
5270 break;
5271 case 'D': /* --debug */
5272 do_debug = 1;
5273 break;
5274 case 'v': /* --verbose */
5275 verbosity_level++;
5276 break;
5277 case 'P': /* --plugin */
5278 if (optarg)
5279 plugin_output_filename = optarg;
5280 else
5281 fatal ("missing plugin output file name");
5282 break;
5283 case 'S': /* --srcdir */
5284 if (optarg)
5285 srcdir = optarg;
5286 else
5287 fatal ("missing source directory");
5288 srcdir_len = strlen (srcdir);
5289 break;
5290 case 'B': /* --backupdir */
5291 if (optarg)
5292 backup_dir = optarg;
5293 else
5294 fatal ("missing backup directory");
5295 break;
5296 case 'I': /* --inputs */
5297 if (optarg)
5298 inputlist = optarg;
5299 else
5300 fatal ("missing input list");
5301 break;
5302 case 'r': /* --read-state */
5303 if (optarg)
5304 read_state_filename = optarg;
5305 else
5306 fatal ("missing read state file");
5307 DBGPRINTF ("read state %s\n", optarg);
5308 break;
5309 case 'w': /* --write-state */
5310 DBGPRINTF ("write state %s\n", optarg);
5311 if (optarg)
5312 write_state_filename = optarg;
5313 else
5314 fatal ("missing write state file");
5315 break;
5316 default:
5317 fprintf (stderr, "%s: unknown flag '%c'\n", progname, opt);
5318 print_usage ();
5319 fatal ("unexpected flag");
5322 if (plugin_output_filename)
5324 /* In plugin mode we require some input files. */
5325 int i = 0;
5326 if (optind >= argc)
5327 fatal ("no source files given in plugin mode");
5328 nb_plugin_files = argc - optind;
5329 plugin_files = XNEWVEC (input_file*, nb_plugin_files);
5330 for (i = 0; i < (int) nb_plugin_files; i++)
5332 char *name = argv[i + optind];
5333 plugin_files[i] = input_file_by_name (name);
5340 /******* Manage input files. ******/
5342 /* Hash table of unique input file names. */
5343 static htab_t input_file_htab;
5345 /* Find or allocate a new input_file by hash-consing it. */
5346 input_file*
5347 input_file_by_name (const char* name)
5349 PTR* slot;
5350 input_file* f = NULL;
5351 int namlen = 0;
5352 if (!name)
5353 return NULL;
5354 namlen = strlen (name);
5355 f = XCNEWVAR (input_file, sizeof (input_file)+namlen+2);
5356 f->inpbitmap = 0;
5357 f->inpoutf = NULL;
5358 f->inpisplugin = false;
5359 strcpy (f->inpname, name);
5360 slot = htab_find_slot (input_file_htab, f, INSERT);
5361 gcc_assert (slot != NULL);
5362 if (*slot)
5364 /* Already known input file. */
5365 free (f);
5366 return (input_file*)(*slot);
5368 /* New input file. */
5369 *slot = f;
5370 return f;
5373 /* Hash table support routines for input_file-s. */
5374 static hashval_t
5375 htab_hash_inputfile (const void *p)
5377 const input_file *inpf = (const input_file *) p;
5378 gcc_assert (inpf);
5379 return htab_hash_string (get_input_file_name (inpf));
5382 static int
5383 htab_eq_inputfile (const void *x, const void *y)
5385 const input_file *inpfx = (const input_file *) x;
5386 const input_file *inpfy = (const input_file *) y;
5387 gcc_assert (inpfx != NULL && inpfy != NULL);
5388 return !filename_cmp (get_input_file_name (inpfx), get_input_file_name (inpfy));
5393 main (int argc, char **argv)
5395 size_t i;
5396 static struct fileloc pos = { NULL, 0 };
5397 outf_p output_header;
5399 /* Mandatory common initializations. */
5400 progname = "gengtype"; /* For fatal and messages. */
5401 /* Create the hash-table used to hash-cons input files. */
5402 input_file_htab =
5403 htab_create (800, htab_hash_inputfile, htab_eq_inputfile, NULL);
5404 /* Initialize our special input files. */
5405 this_file = input_file_by_name (__FILE__);
5406 system_h_file = input_file_by_name ("system.h");
5407 /* Set the scalar_is_char union number for predefined scalar types. */
5408 scalar_nonchar.u.scalar_is_char = FALSE;
5409 scalar_char.u.scalar_is_char = TRUE;
5411 parse_program_options (argc, argv);
5413 #if ENABLE_CHECKING
5414 if (do_debug)
5416 time_t now = (time_t) 0;
5417 time (&now);
5418 DBGPRINTF ("gengtype started pid %d at %s",
5419 (int) getpid (), ctime (&now));
5421 #endif /* ENABLE_CHECKING */
5423 /* Parse the input list and the input files. */
5424 DBGPRINTF ("inputlist %s", inputlist);
5425 if (read_state_filename)
5427 if (inputlist)
5428 fatal ("input list %s cannot be given with a read state file %s",
5429 inputlist, read_state_filename);
5430 read_state (read_state_filename);
5431 DBGPRINT_COUNT_TYPE ("structures after read_state", structures);
5432 DBGPRINT_COUNT_TYPE ("param_structs after read_state", param_structs);
5434 else if (inputlist)
5436 /* These types are set up with #define or else outside of where
5437 we can see them. We should initialize them before calling
5438 read_input_list. */
5439 #define POS_HERE(Call) do { pos.file = this_file; pos.line = __LINE__; \
5440 Call;} while(0)
5441 POS_HERE (do_scalar_typedef ("CUMULATIVE_ARGS", &pos));
5442 POS_HERE (do_scalar_typedef ("REAL_VALUE_TYPE", &pos));
5443 POS_HERE (do_scalar_typedef ("FIXED_VALUE_TYPE", &pos));
5444 POS_HERE (do_scalar_typedef ("double_int", &pos));
5445 POS_HERE (do_scalar_typedef ("uint64_t", &pos));
5446 POS_HERE (do_scalar_typedef ("uint8", &pos));
5447 POS_HERE (do_scalar_typedef ("uintptr_t", &pos));
5448 POS_HERE (do_scalar_typedef ("jword", &pos));
5449 POS_HERE (do_scalar_typedef ("JCF_u2", &pos));
5450 POS_HERE (do_scalar_typedef ("void", &pos));
5451 POS_HERE (do_typedef ("PTR",
5452 create_pointer (resolve_typedef ("void", &pos)),
5453 &pos));
5454 #undef POS_HERE
5455 read_input_list (inputlist);
5456 for (i = 0; i < num_gt_files; i++)
5458 parse_file (get_input_file_name (gt_files[i]));
5459 DBGPRINTF ("parsed file #%d %s",
5460 (int) i, get_input_file_name (gt_files[i]));
5462 if (verbosity_level >= 1)
5463 printf ("%s parsed %d files with %d GTY types\n",
5464 progname, (int) num_gt_files, type_count);
5466 DBGPRINT_COUNT_TYPE ("structures after parsing", structures);
5467 DBGPRINT_COUNT_TYPE ("param_structs after parsing", param_structs);
5470 else
5471 fatal ("either an input list or a read state file should be given");
5472 if (hit_error)
5473 return 1;
5476 if (plugin_output_filename)
5478 size_t ix = 0;
5479 /* In plugin mode, we should have read a state file, and have
5480 given at least one plugin file. */
5481 if (!read_state_filename)
5482 fatal ("No read state given in plugin mode for %s",
5483 plugin_output_filename);
5485 if (nb_plugin_files == 0 || !plugin_files)
5486 fatal ("No plugin files given in plugin mode for %s",
5487 plugin_output_filename);
5489 /* Parse our plugin files and augment the state. */
5490 for (ix = 0; ix < nb_plugin_files; ix++)
5492 input_file* pluginput = plugin_files [ix];
5493 pluginput->inpisplugin = true;
5494 parse_file (get_input_file_name (pluginput));
5496 if (hit_error)
5497 return 1;
5499 plugin_output = create_file ("GCC", plugin_output_filename);
5500 DBGPRINTF ("created plugin_output %p named %s",
5501 (void *) plugin_output, plugin_output->name);
5503 else
5504 { /* No plugin files, we are in normal mode. */
5505 if (!srcdir)
5506 fatal ("gengtype needs a source directory in normal mode");
5508 if (hit_error)
5509 return 1;
5511 gen_rtx_next ();
5513 /* The call to set_gc_used may indirectly call find_param_structure
5514 hence enlarge the param_structs list of types. */
5515 set_gc_used (variables);
5517 /* The state at this point is read from the state input file or by
5518 parsing source files and optionally augmented by parsing plugin
5519 source files. Write it now. */
5520 if (write_state_filename)
5522 DBGPRINT_COUNT_TYPE ("structures before write_state", structures);
5523 DBGPRINT_COUNT_TYPE ("param_structs before write_state", param_structs);
5525 if (hit_error)
5526 fatal ("didn't write state file %s after errors",
5527 write_state_filename);
5529 DBGPRINTF ("before write_state %s", write_state_filename);
5530 write_state (write_state_filename);
5532 if (do_dump)
5533 dump_everything ();
5535 /* After having written the state file we return immediately to
5536 avoid generating any output file. */
5537 if (hit_error)
5538 return 1;
5539 else
5540 return 0;
5544 open_base_files ();
5546 output_header = plugin_output ? plugin_output : header_file;
5547 write_typed_alloc_defns (output_header, structures, typedefs);
5548 DBGPRINT_COUNT_TYPE ("structures before write_types outputheader",
5549 structures);
5550 DBGPRINT_COUNT_TYPE ("param_structs before write_types outputheader",
5551 param_structs);
5553 write_types (output_header, structures, param_structs, &ggc_wtd);
5554 if (plugin_files == NULL)
5556 DBGPRINT_COUNT_TYPE ("structures before write_types headerfil",
5557 structures);
5558 DBGPRINT_COUNT_TYPE ("param_structs before write_types headerfil",
5559 param_structs);
5560 write_types (header_file, structures, param_structs, &pch_wtd);
5561 write_local (header_file, structures, param_structs);
5563 write_splay_tree_allocators (param_structs);
5564 write_roots (variables, plugin_files == NULL);
5565 write_rtx_next ();
5566 close_output_files ();
5568 if (do_dump)
5569 dump_everything ();
5571 /* Don't bother about free-ing any input or plugin file, etc. */
5573 if (hit_error)
5574 return 1;
5575 return 0;